Pull bugzilla-9429 into release branch
[pv_ops_mirror.git] / drivers / net / ibm_newemac / zmii.c
blob2219ec2740e056f37f12619613c94f49c4e6fcd0
1 /*
2 * drivers/net/ibm_newemac/zmii.c
4 * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
6 * Copyright (c) 2004, 2005 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Based on original work by
10 * Armin Kuster <akuster@mvista.com>
11 * Copyright 2001 MontaVista Softare Inc.
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.
19 #include <linux/kernel.h>
20 #include <linux/ethtool.h>
21 #include <asm/io.h>
23 #include "emac.h"
24 #include "core.h"
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))
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))
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))
40 /* ZMII only supports MII, RMII and SMII
41 * we also support autodetection for backward compatibility
43 static inline int zmii_valid_mode(int mode)
45 return mode == PHY_MODE_MII ||
46 mode == PHY_MODE_RMII ||
47 mode == PHY_MODE_SMII ||
48 mode == PHY_MODE_NA;
51 static inline const char *zmii_mode_name(int mode)
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();
65 static inline u32 zmii_mode_mask(int mode, int input)
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;
79 int __devinit zmii_attach(struct of_device *ofdev, int input, int *mode)
81 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
82 struct zmii_regs __iomem *p = dev->base;
84 ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
86 if (!zmii_valid_mode(*mode))
87 /* Probably an EMAC connected to RGMII,
88 * but it still may need ZMII for MDIO so
89 * we don't fail here.
91 return 0;
93 mutex_lock(&dev->lock);
95 /* Autodetect ZMII mode if not specified.
96 * This is only for backward compatibility with the old driver.
97 * Please, always specify PHY mode in your board port to avoid
98 * any surprises.
100 if (dev->mode == PHY_MODE_NA) {
101 if (*mode == PHY_MODE_NA) {
102 u32 r = dev->fer_save;
104 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
106 if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
107 dev->mode = PHY_MODE_MII;
108 else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
109 dev->mode = PHY_MODE_RMII;
110 else
111 dev->mode = PHY_MODE_SMII;
112 } else
113 dev->mode = *mode;
115 printk(KERN_NOTICE "%s: bridge in %s mode\n",
116 ofdev->node->full_name, zmii_mode_name(dev->mode));
117 } else {
118 /* All inputs must use the same mode */
119 if (*mode != PHY_MODE_NA && *mode != dev->mode) {
120 printk(KERN_ERR
121 "%s: invalid mode %d specified for input %d\n",
122 ofdev->node->full_name, *mode, input);
123 mutex_unlock(&dev->lock);
124 return -EINVAL;
128 /* Report back correct PHY mode,
129 * it may be used during PHY initialization.
131 *mode = dev->mode;
133 /* Enable this input */
134 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
135 ++dev->users;
137 mutex_unlock(&dev->lock);
139 return 0;
142 void zmii_get_mdio(struct of_device *ofdev, int input)
144 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
145 u32 fer;
147 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
149 mutex_lock(&dev->lock);
151 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
152 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
155 void zmii_put_mdio(struct of_device *ofdev, int input)
157 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
159 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
160 mutex_unlock(&dev->lock);
164 void zmii_set_speed(struct of_device *ofdev, int input, int speed)
166 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
167 u32 ssr;
169 mutex_lock(&dev->lock);
171 ssr = in_be32(&dev->base->ssr);
173 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
175 if (speed == SPEED_100)
176 ssr |= ZMII_SSR_SP(input);
177 else
178 ssr &= ~ZMII_SSR_SP(input);
180 out_be32(&dev->base->ssr, ssr);
182 mutex_unlock(&dev->lock);
185 void __devexit zmii_detach(struct of_device *ofdev, int input)
187 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
189 BUG_ON(!dev || dev->users == 0);
191 mutex_lock(&dev->lock);
193 ZMII_DBG(dev, "detach(%d)" NL, input);
195 /* Disable this input */
196 out_be32(&dev->base->fer,
197 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
199 --dev->users;
201 mutex_unlock(&dev->lock);
204 int zmii_get_regs_len(struct of_device *ofdev)
206 return sizeof(struct emac_ethtool_regs_subhdr) +
207 sizeof(struct zmii_regs);
210 void *zmii_dump_regs(struct of_device *ofdev, void *buf)
212 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
213 struct emac_ethtool_regs_subhdr *hdr = buf;
214 struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
216 hdr->version = 0;
217 hdr->index = 0; /* for now, are there chips with more than one
218 * zmii ? if yes, then we'll add a cell_index
219 * like we do for emac
221 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
222 return regs + 1;
225 static int __devinit zmii_probe(struct of_device *ofdev,
226 const struct of_device_id *match)
228 struct device_node *np = ofdev->node;
229 struct zmii_instance *dev;
230 struct resource regs;
231 int rc;
233 rc = -ENOMEM;
234 dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
235 if (dev == NULL) {
236 printk(KERN_ERR "%s: could not allocate ZMII device!\n",
237 np->full_name);
238 goto err_gone;
241 mutex_init(&dev->lock);
242 dev->ofdev = ofdev;
243 dev->mode = PHY_MODE_NA;
245 rc = -ENXIO;
246 if (of_address_to_resource(np, 0, &regs)) {
247 printk(KERN_ERR "%s: Can't get registers address\n",
248 np->full_name);
249 goto err_free;
252 rc = -ENOMEM;
253 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
254 sizeof(struct zmii_regs));
255 if (dev->base == NULL) {
256 printk(KERN_ERR "%s: Can't map device registers!\n",
257 np->full_name);
258 goto err_free;
261 /* We may need FER value for autodetection later */
262 dev->fer_save = in_be32(&dev->base->fer);
264 /* Disable all inputs by default */
265 out_be32(&dev->base->fer, 0);
267 printk(KERN_INFO
268 "ZMII %s initialized\n", ofdev->node->full_name);
269 wmb();
270 dev_set_drvdata(&ofdev->dev, dev);
272 return 0;
274 err_free:
275 kfree(dev);
276 err_gone:
277 return rc;
280 static int __devexit zmii_remove(struct of_device *ofdev)
282 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
284 dev_set_drvdata(&ofdev->dev, NULL);
286 WARN_ON(dev->users != 0);
288 iounmap(dev->base);
289 kfree(dev);
291 return 0;
294 static struct of_device_id zmii_match[] =
297 .compatible = "ibm,zmii",
299 /* For backward compat with old DT */
301 .type = "emac-zmii",
306 static struct of_platform_driver zmii_driver = {
307 .name = "emac-zmii",
308 .match_table = zmii_match,
310 .probe = zmii_probe,
311 .remove = zmii_remove,
314 int __init zmii_init(void)
316 return of_register_platform_driver(&zmii_driver);
319 void zmii_exit(void)
321 of_unregister_platform_driver(&zmii_driver);