blob: 0f8cfbc0c618f5574a532458549cc0e7992d94fe [file] [log] [blame]
David Gibson1d3bb992007-08-23 13:56:01 +10001/*
2 * drivers/net/ibm_newemac/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/kernel.h>
20#include <linux/ethtool.h>
21#include <asm/io.h>
22
23#include "emac.h"
24#include "core.h"
25
26/* ZMIIx_FER */
27#define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
28#define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
29 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
30
31#define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
32#define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
33#define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
34
35/* ZMIIx_SSR */
36#define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
37#define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
38#define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
39
40/* ZMII only supports MII, RMII and SMII
41 * we also support autodetection for backward compatibility
42 */
43static inline int zmii_valid_mode(int mode)
44{
45 return mode == PHY_MODE_MII ||
46 mode == PHY_MODE_RMII ||
47 mode == PHY_MODE_SMII ||
48 mode == PHY_MODE_NA;
49}
50
51static inline const char *zmii_mode_name(int mode)
52{
53 switch (mode) {
54 case PHY_MODE_MII:
55 return "MII";
56 case PHY_MODE_RMII:
57 return "RMII";
58 case PHY_MODE_SMII:
59 return "SMII";
60 default:
61 BUG();
62 }
63}
64
65static inline u32 zmii_mode_mask(int mode, int input)
66{
67 switch (mode) {
68 case PHY_MODE_MII:
69 return ZMII_FER_MII(input);
70 case PHY_MODE_RMII:
71 return ZMII_FER_RMII(input);
72 case PHY_MODE_SMII:
73 return ZMII_FER_SMII(input);
74 default:
75 return 0;
76 }
77}
78
79int __devinit zmii_attach(struct of_device *ofdev, int input, int *mode)
80{
81 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +010082 struct zmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +100083
84 ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
85
Benjamin Herrenschmidt96853062007-12-05 11:14:27 +110086 if (!zmii_valid_mode(*mode)) {
David Gibson1d3bb992007-08-23 13:56:01 +100087 /* Probably an EMAC connected to RGMII,
88 * but it still may need ZMII for MDIO so
89 * we don't fail here.
90 */
Benjamin Herrenschmidt96853062007-12-05 11:14:27 +110091 dev->users++;
David Gibson1d3bb992007-08-23 13:56:01 +100092 return 0;
Benjamin Herrenschmidt96853062007-12-05 11:14:27 +110093 }
David Gibson1d3bb992007-08-23 13:56:01 +100094
95 mutex_lock(&dev->lock);
96
97 /* Autodetect ZMII mode if not specified.
98 * This is only for backward compatibility with the old driver.
99 * Please, always specify PHY mode in your board port to avoid
100 * any surprises.
101 */
102 if (dev->mode == PHY_MODE_NA) {
103 if (*mode == PHY_MODE_NA) {
104 u32 r = dev->fer_save;
105
106 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
107
108 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
109 dev->mode = PHY_MODE_MII;
110 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
111 dev->mode = PHY_MODE_RMII;
112 else
113 dev->mode = PHY_MODE_SMII;
114 } else
115 dev->mode = *mode;
116
117 printk(KERN_NOTICE "%s: bridge in %s mode\n",
118 ofdev->node->full_name, zmii_mode_name(dev->mode));
119 } else {
120 /* All inputs must use the same mode */
121 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
122 printk(KERN_ERR
123 "%s: invalid mode %d specified for input %d\n",
124 ofdev->node->full_name, *mode, input);
125 mutex_unlock(&dev->lock);
126 return -EINVAL;
127 }
128 }
129
130 /* Report back correct PHY mode,
131 * it may be used during PHY initialization.
132 */
133 *mode = dev->mode;
134
135 /* Enable this input */
136 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
137 ++dev->users;
138
139 mutex_unlock(&dev->lock);
140
141 return 0;
142}
143
144void zmii_get_mdio(struct of_device *ofdev, int input)
145{
146 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
147 u32 fer;
148
149 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
150
151 mutex_lock(&dev->lock);
152
153 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
154 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
155}
156
157void zmii_put_mdio(struct of_device *ofdev, int input)
158{
159 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
160
161 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
162 mutex_unlock(&dev->lock);
163}
164
165
166void zmii_set_speed(struct of_device *ofdev, int input, int speed)
167{
168 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
169 u32 ssr;
170
171 mutex_lock(&dev->lock);
172
173 ssr = in_be32(&dev->base->ssr);
174
175 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
176
177 if (speed == SPEED_100)
178 ssr |= ZMII_SSR_SP(input);
179 else
180 ssr &= ~ZMII_SSR_SP(input);
181
182 out_be32(&dev->base->ssr, ssr);
183
184 mutex_unlock(&dev->lock);
185}
186
187void __devexit zmii_detach(struct of_device *ofdev, int input)
188{
189 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
190
191 BUG_ON(!dev || dev->users == 0);
192
193 mutex_lock(&dev->lock);
194
195 ZMII_DBG(dev, "detach(%d)" NL, input);
196
197 /* Disable this input */
198 out_be32(&dev->base->fer,
199 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
200
201 --dev->users;
202
203 mutex_unlock(&dev->lock);
204}
205
206int zmii_get_regs_len(struct of_device *ofdev)
207{
208 return sizeof(struct emac_ethtool_regs_subhdr) +
209 sizeof(struct zmii_regs);
210}
211
212void *zmii_dump_regs(struct of_device *ofdev, void *buf)
213{
214 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
215 struct emac_ethtool_regs_subhdr *hdr = buf;
216 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
217
218 hdr->version = 0;
219 hdr->index = 0; /* for now, are there chips with more than one
220 * zmii ? if yes, then we'll add a cell_index
221 * like we do for emac
222 */
223 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
224 return regs + 1;
225}
226
227static int __devinit zmii_probe(struct of_device *ofdev,
228 const struct of_device_id *match)
229{
230 struct device_node *np = ofdev->node;
231 struct zmii_instance *dev;
232 struct resource regs;
233 int rc;
234
235 rc = -ENOMEM;
236 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
237 if (dev == NULL) {
238 printk(KERN_ERR "%s: could not allocate ZMII device!\n",
239 np->full_name);
240 goto err_gone;
241 }
242
243 mutex_init(&dev->lock);
244 dev->ofdev = ofdev;
245 dev->mode = PHY_MODE_NA;
246
247 rc = -ENXIO;
248 if (of_address_to_resource(np, 0, &regs)) {
249 printk(KERN_ERR "%s: Can't get registers address\n",
250 np->full_name);
251 goto err_free;
252 }
253
254 rc = -ENOMEM;
Al Viroeb4d84f2007-10-14 19:36:10 +0100255 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
David Gibson1d3bb992007-08-23 13:56:01 +1000256 sizeof(struct zmii_regs));
257 if (dev->base == NULL) {
258 printk(KERN_ERR "%s: Can't map device registers!\n",
259 np->full_name);
260 goto err_free;
261 }
262
263 /* We may need FER value for autodetection later */
264 dev->fer_save = in_be32(&dev->base->fer);
265
266 /* Disable all inputs by default */
267 out_be32(&dev->base->fer, 0);
268
269 printk(KERN_INFO
270 "ZMII %s initialized\n", ofdev->node->full_name);
271 wmb();
272 dev_set_drvdata(&ofdev->dev, dev);
273
274 return 0;
275
276 err_free:
277 kfree(dev);
278 err_gone:
279 return rc;
280}
281
282static int __devexit zmii_remove(struct of_device *ofdev)
283{
284 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
285
286 dev_set_drvdata(&ofdev->dev, NULL);
287
288 WARN_ON(dev->users != 0);
289
290 iounmap(dev->base);
291 kfree(dev);
292
293 return 0;
294}
295
296static struct of_device_id zmii_match[] =
297{
298 {
299 .compatible = "ibm,zmii",
300 },
301 /* For backward compat with old DT */
302 {
303 .type = "emac-zmii",
304 },
305 {},
306};
307
308static struct of_platform_driver zmii_driver = {
309 .name = "emac-zmii",
310 .match_table = zmii_match,
311
312 .probe = zmii_probe,
313 .remove = zmii_remove,
314};
315
316int __init zmii_init(void)
317{
318 return of_register_platform_driver(&zmii_driver);
319}
320
321void zmii_exit(void)
322{
323 of_unregister_platform_driver(&zmii_driver);
324}