blob: b4fa823ed201a16a9ceeb29c79150bd18123f9c5 [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 *
Benjamin Herrenschmidt17cf8032007-12-05 11:14:33 +11006 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
7 * <benh@kernel.crashing.org>
8 *
9 * Based on the arch/ppc version of the driver:
10 *
David Gibson1d3bb992007-08-23 13:56:01 +100011 * Copyright (c) 2004, 2005 Zultys Technologies.
12 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
13 *
14 * Based on original work by
15 * Armin Kuster <akuster@mvista.com>
16 * Copyright 2001 MontaVista Softare Inc.
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version.
22 *
23 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
David Gibson1d3bb992007-08-23 13:56:01 +100025#include <linux/kernel.h>
26#include <linux/ethtool.h>
27#include <asm/io.h>
28
29#include "emac.h"
30#include "core.h"
31
32/* ZMIIx_FER */
33#define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
34#define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
35 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
36
37#define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
38#define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
39#define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
40
41/* ZMIIx_SSR */
42#define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
43#define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
44#define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
45
46/* ZMII only supports MII, RMII and SMII
47 * we also support autodetection for backward compatibility
48 */
49static inline int zmii_valid_mode(int mode)
50{
51 return mode == PHY_MODE_MII ||
52 mode == PHY_MODE_RMII ||
53 mode == PHY_MODE_SMII ||
54 mode == PHY_MODE_NA;
55}
56
57static inline const char *zmii_mode_name(int mode)
58{
59 switch (mode) {
60 case PHY_MODE_MII:
61 return "MII";
62 case PHY_MODE_RMII:
63 return "RMII";
64 case PHY_MODE_SMII:
65 return "SMII";
66 default:
67 BUG();
68 }
69}
70
71static inline u32 zmii_mode_mask(int mode, int input)
72{
73 switch (mode) {
74 case PHY_MODE_MII:
75 return ZMII_FER_MII(input);
76 case PHY_MODE_RMII:
77 return ZMII_FER_RMII(input);
78 case PHY_MODE_SMII:
79 return ZMII_FER_SMII(input);
80 default:
81 return 0;
82 }
83}
84
85int __devinit zmii_attach(struct of_device *ofdev, int input, int *mode)
86{
87 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
Al Viroeb4d84f2007-10-14 19:36:10 +010088 struct zmii_regs __iomem *p = dev->base;
David Gibson1d3bb992007-08-23 13:56:01 +100089
90 ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
91
Benjamin Herrenschmidt96853062007-12-05 11:14:27 +110092 if (!zmii_valid_mode(*mode)) {
David Gibson1d3bb992007-08-23 13:56:01 +100093 /* Probably an EMAC connected to RGMII,
94 * but it still may need ZMII for MDIO so
95 * we don't fail here.
96 */
Benjamin Herrenschmidt96853062007-12-05 11:14:27 +110097 dev->users++;
David Gibson1d3bb992007-08-23 13:56:01 +100098 return 0;
Benjamin Herrenschmidt96853062007-12-05 11:14:27 +110099 }
David Gibson1d3bb992007-08-23 13:56:01 +1000100
101 mutex_lock(&dev->lock);
102
103 /* Autodetect ZMII mode if not specified.
104 * This is only for backward compatibility with the old driver.
105 * Please, always specify PHY mode in your board port to avoid
106 * any surprises.
107 */
108 if (dev->mode == PHY_MODE_NA) {
109 if (*mode == PHY_MODE_NA) {
110 u32 r = dev->fer_save;
111
112 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
113
114 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
115 dev->mode = PHY_MODE_MII;
116 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
117 dev->mode = PHY_MODE_RMII;
118 else
119 dev->mode = PHY_MODE_SMII;
120 } else
121 dev->mode = *mode;
122
123 printk(KERN_NOTICE "%s: bridge in %s mode\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700124 ofdev->dev.of_node->full_name,
125 zmii_mode_name(dev->mode));
David Gibson1d3bb992007-08-23 13:56:01 +1000126 } else {
127 /* All inputs must use the same mode */
128 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
129 printk(KERN_ERR
130 "%s: invalid mode %d specified for input %d\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700131 ofdev->dev.of_node->full_name, *mode, input);
David Gibson1d3bb992007-08-23 13:56:01 +1000132 mutex_unlock(&dev->lock);
133 return -EINVAL;
134 }
135 }
136
137 /* Report back correct PHY mode,
138 * it may be used during PHY initialization.
139 */
140 *mode = dev->mode;
141
142 /* Enable this input */
143 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
144 ++dev->users;
145
146 mutex_unlock(&dev->lock);
147
148 return 0;
149}
150
151void zmii_get_mdio(struct of_device *ofdev, int input)
152{
153 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
154 u32 fer;
155
156 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
157
158 mutex_lock(&dev->lock);
159
160 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
161 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
162}
163
164void zmii_put_mdio(struct of_device *ofdev, int input)
165{
166 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
167
168 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
169 mutex_unlock(&dev->lock);
170}
171
172
173void zmii_set_speed(struct of_device *ofdev, int input, int speed)
174{
175 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
176 u32 ssr;
177
178 mutex_lock(&dev->lock);
179
180 ssr = in_be32(&dev->base->ssr);
181
182 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
183
184 if (speed == SPEED_100)
185 ssr |= ZMII_SSR_SP(input);
186 else
187 ssr &= ~ZMII_SSR_SP(input);
188
189 out_be32(&dev->base->ssr, ssr);
190
191 mutex_unlock(&dev->lock);
192}
193
Josh Boyer51d4a1c2008-04-22 10:46:43 +1000194void zmii_detach(struct of_device *ofdev, int input)
David Gibson1d3bb992007-08-23 13:56:01 +1000195{
196 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
197
198 BUG_ON(!dev || dev->users == 0);
199
200 mutex_lock(&dev->lock);
201
202 ZMII_DBG(dev, "detach(%d)" NL, input);
203
204 /* Disable this input */
205 out_be32(&dev->base->fer,
206 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
207
208 --dev->users;
209
210 mutex_unlock(&dev->lock);
211}
212
213int zmii_get_regs_len(struct of_device *ofdev)
214{
215 return sizeof(struct emac_ethtool_regs_subhdr) +
216 sizeof(struct zmii_regs);
217}
218
219void *zmii_dump_regs(struct of_device *ofdev, void *buf)
220{
221 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
222 struct emac_ethtool_regs_subhdr *hdr = buf;
223 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
224
225 hdr->version = 0;
226 hdr->index = 0; /* for now, are there chips with more than one
227 * zmii ? if yes, then we'll add a cell_index
228 * like we do for emac
229 */
230 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
231 return regs + 1;
232}
233
234static int __devinit zmii_probe(struct of_device *ofdev,
235 const struct of_device_id *match)
236{
Grant Likely61c7a082010-04-13 16:12:29 -0700237 struct device_node *np = ofdev->dev.of_node;
David Gibson1d3bb992007-08-23 13:56:01 +1000238 struct zmii_instance *dev;
239 struct resource regs;
240 int rc;
241
242 rc = -ENOMEM;
243 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
244 if (dev == NULL) {
245 printk(KERN_ERR "%s: could not allocate ZMII device!\n",
246 np->full_name);
247 goto err_gone;
248 }
249
250 mutex_init(&dev->lock);
251 dev->ofdev = ofdev;
252 dev->mode = PHY_MODE_NA;
253
254 rc = -ENXIO;
255 if (of_address_to_resource(np, 0, &regs)) {
256 printk(KERN_ERR "%s: Can't get registers address\n",
257 np->full_name);
258 goto err_free;
259 }
260
261 rc = -ENOMEM;
Al Viroeb4d84f2007-10-14 19:36:10 +0100262 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
David Gibson1d3bb992007-08-23 13:56:01 +1000263 sizeof(struct zmii_regs));
264 if (dev->base == NULL) {
265 printk(KERN_ERR "%s: Can't map device registers!\n",
266 np->full_name);
267 goto err_free;
268 }
269
270 /* We may need FER value for autodetection later */
271 dev->fer_save = in_be32(&dev->base->fer);
272
273 /* Disable all inputs by default */
274 out_be32(&dev->base->fer, 0);
275
276 printk(KERN_INFO
Grant Likely61c7a082010-04-13 16:12:29 -0700277 "ZMII %s initialized\n", ofdev->dev.of_node->full_name);
David Gibson1d3bb992007-08-23 13:56:01 +1000278 wmb();
279 dev_set_drvdata(&ofdev->dev, dev);
280
281 return 0;
282
283 err_free:
284 kfree(dev);
285 err_gone:
286 return rc;
287}
288
289static int __devexit zmii_remove(struct of_device *ofdev)
290{
291 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
292
293 dev_set_drvdata(&ofdev->dev, NULL);
294
295 WARN_ON(dev->users != 0);
296
297 iounmap(dev->base);
298 kfree(dev);
299
300 return 0;
301}
302
303static struct of_device_id zmii_match[] =
304{
305 {
306 .compatible = "ibm,zmii",
307 },
308 /* For backward compat with old DT */
309 {
310 .type = "emac-zmii",
311 },
312 {},
313};
314
315static struct of_platform_driver zmii_driver = {
316 .name = "emac-zmii",
317 .match_table = zmii_match,
318
319 .probe = zmii_probe,
320 .remove = zmii_remove,
321};
322
323int __init zmii_init(void)
324{
325 return of_register_platform_driver(&zmii_driver);
326}
327
328void zmii_exit(void)
329{
330 of_unregister_platform_driver(&zmii_driver);
331}