blob: e129e0aaa045512f26e84a18def908225e1843b7 [file] [log] [blame]
Eugene Surovegin37448f72005-10-10 16:58:14 -07001/*
2 * drivers/net/ibm_emac/ibm_emac_zmii.c
3 *
4 * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
5 *
6 * Copyright (c) 2004, 2005 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
8 *
9 * Based on original work by
10 * Armin Kuster <akuster@mvista.com>
11 * Copyright 2001 MontaVista Softare Inc.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 */
19#include <linux/config.h>
20#include <linux/kernel.h>
21#include <linux/ethtool.h>
22#include <asm/io.h>
23
24#include "ibm_emac_core.h"
25#include "ibm_emac_debug.h"
26
27/* ZMIIx_FER */
28#define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
29#define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
30 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
31
32#define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
33#define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
34#define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
35
36/* ZMIIx_SSR */
37#define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
38#define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
39#define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
40
41/* ZMII only supports MII, RMII and SMII
42 * we also support autodetection for backward compatibility
43 */
44static inline int zmii_valid_mode(int mode)
45{
46 return mode == PHY_MODE_MII ||
47 mode == PHY_MODE_RMII ||
48 mode == PHY_MODE_SMII ||
49 mode == PHY_MODE_NA;
50}
51
52static inline const char *zmii_mode_name(int mode)
53{
54 switch (mode) {
55 case PHY_MODE_MII:
56 return "MII";
57 case PHY_MODE_RMII:
58 return "RMII";
59 case PHY_MODE_SMII:
60 return "SMII";
61 default:
62 BUG();
63 }
64}
65
66static inline u32 zmii_mode_mask(int mode, int input)
67{
68 switch (mode) {
69 case PHY_MODE_MII:
70 return ZMII_FER_MII(input);
71 case PHY_MODE_RMII:
72 return ZMII_FER_RMII(input);
73 case PHY_MODE_SMII:
74 return ZMII_FER_SMII(input);
75 default:
76 return 0;
77 }
78}
79
80static int __init zmii_init(struct ocp_device *ocpdev, int input, int *mode)
81{
82 struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
Al Virob43de2d2005-12-01 10:15:21 -050083 struct zmii_regs __iomem *p;
Eugene Surovegin37448f72005-10-10 16:58:14 -070084
85 ZMII_DBG("%d: init(%d, %d)" NL, ocpdev->def->index, input, *mode);
86
87 if (!dev) {
88 dev = kzalloc(sizeof(struct ibm_ocp_zmii), GFP_KERNEL);
89 if (!dev) {
90 printk(KERN_ERR
91 "zmii%d: couldn't allocate device structure!\n",
92 ocpdev->def->index);
93 return -ENOMEM;
94 }
95 dev->mode = PHY_MODE_NA;
96
Al Virob43de2d2005-12-01 10:15:21 -050097 p = ioremap(ocpdev->def->paddr, sizeof(struct zmii_regs));
Eugene Surovegin37448f72005-10-10 16:58:14 -070098 if (!p) {
99 printk(KERN_ERR
100 "zmii%d: could not ioremap device registers!\n",
101 ocpdev->def->index);
102 kfree(dev);
103 return -ENOMEM;
104 }
105 dev->base = p;
106 ocp_set_drvdata(ocpdev, dev);
107
108 /* We may need FER value for autodetection later */
109 dev->fer_save = in_be32(&p->fer);
110
111 /* Disable all inputs by default */
112 out_be32(&p->fer, 0);
113 } else
114 p = dev->base;
115
116 if (!zmii_valid_mode(*mode)) {
117 /* Probably an EMAC connected to RGMII,
118 * but it still may need ZMII for MDIO
119 */
120 goto out;
121 }
122
123 /* Autodetect ZMII mode if not specified.
124 * This is only for backward compatibility with the old driver.
125 * Please, always specify PHY mode in your board port to avoid
126 * any surprises.
127 */
128 if (dev->mode == PHY_MODE_NA) {
129 if (*mode == PHY_MODE_NA) {
130 u32 r = dev->fer_save;
131
132 ZMII_DBG("%d: autodetecting mode, FER = 0x%08x" NL,
133 ocpdev->def->index, r);
134
135 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
136 dev->mode = PHY_MODE_MII;
137 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
138 dev->mode = PHY_MODE_RMII;
139 else
140 dev->mode = PHY_MODE_SMII;
141 } else
142 dev->mode = *mode;
143
144 printk(KERN_NOTICE "zmii%d: bridge in %s mode\n",
145 ocpdev->def->index, zmii_mode_name(dev->mode));
146 } else {
147 /* All inputs must use the same mode */
148 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
149 printk(KERN_ERR
150 "zmii%d: invalid mode %d specified for input %d\n",
151 ocpdev->def->index, *mode, input);
152 return -EINVAL;
153 }
154 }
155
156 /* Report back correct PHY mode,
157 * it may be used during PHY initialization.
158 */
159 *mode = dev->mode;
160
161 /* Enable this input */
162 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
163 out:
164 ++dev->users;
165 return 0;
166}
167
168int __init zmii_attach(void *emac)
169{
170 struct ocp_enet_private *dev = emac;
171 struct ocp_func_emac_data *emacdata = dev->def->additions;
172
173 if (emacdata->zmii_idx >= 0) {
174 dev->zmii_input = emacdata->zmii_mux;
175 dev->zmii_dev =
176 ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_ZMII,
177 emacdata->zmii_idx);
178 if (!dev->zmii_dev) {
179 printk(KERN_ERR "emac%d: unknown zmii%d!\n",
180 dev->def->index, emacdata->zmii_idx);
181 return -ENODEV;
182 }
183 if (zmii_init
184 (dev->zmii_dev, dev->zmii_input, &emacdata->phy_mode)) {
185 printk(KERN_ERR
186 "emac%d: zmii%d initialization failed!\n",
187 dev->def->index, emacdata->zmii_idx);
188 return -ENODEV;
189 }
190 }
191 return 0;
192}
193
194void __zmii_enable_mdio(struct ocp_device *ocpdev, int input)
195{
196 struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
197 u32 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
198
199 ZMII_DBG2("%d: mdio(%d)" NL, ocpdev->def->index, input);
200
201 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
202}
203
204void __zmii_set_speed(struct ocp_device *ocpdev, int input, int speed)
205{
206 struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
207 u32 ssr = in_be32(&dev->base->ssr);
208
209 ZMII_DBG("%d: speed(%d, %d)" NL, ocpdev->def->index, input, speed);
210
211 if (speed == SPEED_100)
212 ssr |= ZMII_SSR_SP(input);
213 else
214 ssr &= ~ZMII_SSR_SP(input);
215
216 out_be32(&dev->base->ssr, ssr);
217}
218
219void __exit __zmii_fini(struct ocp_device *ocpdev, int input)
220{
221 struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
222 BUG_ON(!dev || dev->users == 0);
223
224 ZMII_DBG("%d: fini(%d)" NL, ocpdev->def->index, input);
225
226 /* Disable this input */
227 out_be32(&dev->base->fer,
228 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
229
230 if (!--dev->users) {
231 /* Free everything if this is the last user */
232 ocp_set_drvdata(ocpdev, NULL);
Al Virob43de2d2005-12-01 10:15:21 -0500233 iounmap(dev->base);
Eugene Surovegin37448f72005-10-10 16:58:14 -0700234 kfree(dev);
235 }
236}
237
238int __zmii_get_regs_len(struct ocp_device *ocpdev)
239{
240 return sizeof(struct emac_ethtool_regs_subhdr) +
241 sizeof(struct zmii_regs);
242}
243
244void *zmii_dump_regs(struct ocp_device *ocpdev, void *buf)
245{
246 struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
247 struct emac_ethtool_regs_subhdr *hdr = buf;
248 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
249
250 hdr->version = 0;
251 hdr->index = ocpdev->def->index;
252 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
253 return regs + 1;
254}