ibm_newemac: Fix ZMII refcounting bug
[linux-2.6/libata-dev.git] / drivers / net / ibm_newemac / zmii.c
blob0f8cfbc0c618f5574a532458549cc0e7992d94fe
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 dev->users++;
92 return 0;
95 mutex_lock(&dev->lock);
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.
102 if (dev->mode == PHY_MODE_NA) {
103 if (*mode == PHY_MODE_NA) {
104 u32 r = dev->fer_save;
106 ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
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;
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;
130 /* Report back correct PHY mode,
131 * it may be used during PHY initialization.
133 *mode = dev->mode;
135 /* Enable this input */
136 out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
137 ++dev->users;
139 mutex_unlock(&dev->lock);
141 return 0;
144 void zmii_get_mdio(struct of_device *ofdev, int input)
146 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
147 u32 fer;
149 ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
151 mutex_lock(&dev->lock);
153 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
154 out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
157 void zmii_put_mdio(struct of_device *ofdev, int input)
159 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
161 ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
162 mutex_unlock(&dev->lock);
166 void zmii_set_speed(struct of_device *ofdev, int input, int speed)
168 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
169 u32 ssr;
171 mutex_lock(&dev->lock);
173 ssr = in_be32(&dev->base->ssr);
175 ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
177 if (speed == SPEED_100)
178 ssr |= ZMII_SSR_SP(input);
179 else
180 ssr &= ~ZMII_SSR_SP(input);
182 out_be32(&dev->base->ssr, ssr);
184 mutex_unlock(&dev->lock);
187 void __devexit zmii_detach(struct of_device *ofdev, int input)
189 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
191 BUG_ON(!dev || dev->users == 0);
193 mutex_lock(&dev->lock);
195 ZMII_DBG(dev, "detach(%d)" NL, input);
197 /* Disable this input */
198 out_be32(&dev->base->fer,
199 in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
201 --dev->users;
203 mutex_unlock(&dev->lock);
206 int zmii_get_regs_len(struct of_device *ofdev)
208 return sizeof(struct emac_ethtool_regs_subhdr) +
209 sizeof(struct zmii_regs);
212 void *zmii_dump_regs(struct of_device *ofdev, void *buf)
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);
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
223 memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
224 return regs + 1;
227 static int __devinit zmii_probe(struct of_device *ofdev,
228 const struct of_device_id *match)
230 struct device_node *np = ofdev->node;
231 struct zmii_instance *dev;
232 struct resource regs;
233 int rc;
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;
243 mutex_init(&dev->lock);
244 dev->ofdev = ofdev;
245 dev->mode = PHY_MODE_NA;
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;
254 rc = -ENOMEM;
255 dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
256 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;
263 /* We may need FER value for autodetection later */
264 dev->fer_save = in_be32(&dev->base->fer);
266 /* Disable all inputs by default */
267 out_be32(&dev->base->fer, 0);
269 printk(KERN_INFO
270 "ZMII %s initialized\n", ofdev->node->full_name);
271 wmb();
272 dev_set_drvdata(&ofdev->dev, dev);
274 return 0;
276 err_free:
277 kfree(dev);
278 err_gone:
279 return rc;
282 static int __devexit zmii_remove(struct of_device *ofdev)
284 struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
286 dev_set_drvdata(&ofdev->dev, NULL);
288 WARN_ON(dev->users != 0);
290 iounmap(dev->base);
291 kfree(dev);
293 return 0;
296 static struct of_device_id zmii_match[] =
299 .compatible = "ibm,zmii",
301 /* For backward compat with old DT */
303 .type = "emac-zmii",
308 static struct of_platform_driver zmii_driver = {
309 .name = "emac-zmii",
310 .match_table = zmii_match,
312 .probe = zmii_probe,
313 .remove = zmii_remove,
316 int __init zmii_init(void)
318 return of_register_platform_driver(&zmii_driver);
321 void zmii_exit(void)
323 of_unregister_platform_driver(&zmii_driver);