ibm_new_emac: Nuke SET_MODULE_OWNER() use
[safe/jmp/linux-2.6] / drivers / net / ibm_newemac / zmii.c
1 /*
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  */
43 static 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
51 static 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
65 static 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
79 int __devinit zmii_attach(struct of_device *ofdev, int input, int *mode)
80 {
81         struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
82         struct zmii_regs *p = dev->base;
83
84         ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
85
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.
90                  */
91                 return 0;
92
93         mutex_lock(&dev->lock);
94
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.
99          */
100         if (dev->mode == PHY_MODE_NA) {
101                 if (*mode == PHY_MODE_NA) {
102                         u32 r = dev->fer_save;
103
104                         ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
105
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;
114
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;
125                 }
126         }
127
128         /* Report back correct PHY mode,
129          * it may be used during PHY initialization.
130          */
131         *mode = dev->mode;
132
133         /* Enable this input */
134         out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
135         ++dev->users;
136
137         mutex_unlock(&dev->lock);
138
139         return 0;
140 }
141
142 void zmii_get_mdio(struct of_device *ofdev, int input)
143 {
144         struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
145         u32 fer;
146
147         ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
148
149         mutex_lock(&dev->lock);
150
151         fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
152         out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
153 }
154
155 void zmii_put_mdio(struct of_device *ofdev, int input)
156 {
157         struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
158
159         ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
160         mutex_unlock(&dev->lock);
161 }
162
163
164 void zmii_set_speed(struct of_device *ofdev, int input, int speed)
165 {
166         struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
167         u32 ssr;
168
169         mutex_lock(&dev->lock);
170
171         ssr = in_be32(&dev->base->ssr);
172
173         ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
174
175         if (speed == SPEED_100)
176                 ssr |= ZMII_SSR_SP(input);
177         else
178                 ssr &= ~ZMII_SSR_SP(input);
179
180         out_be32(&dev->base->ssr, ssr);
181
182         mutex_unlock(&dev->lock);
183 }
184
185 void __devexit zmii_detach(struct of_device *ofdev, int input)
186 {
187         struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
188
189         BUG_ON(!dev || dev->users == 0);
190
191         mutex_lock(&dev->lock);
192
193         ZMII_DBG(dev, "detach(%d)" NL, input);
194
195         /* Disable this input */
196         out_be32(&dev->base->fer,
197                  in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
198
199         --dev->users;
200
201         mutex_unlock(&dev->lock);
202 }
203
204 int zmii_get_regs_len(struct of_device *ofdev)
205 {
206         return sizeof(struct emac_ethtool_regs_subhdr) +
207                 sizeof(struct zmii_regs);
208 }
209
210 void *zmii_dump_regs(struct of_device *ofdev, void *buf)
211 {
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);
215
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
220                          */
221         memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
222         return regs + 1;
223 }
224
225 static int __devinit zmii_probe(struct of_device *ofdev,
226                                 const struct of_device_id *match)
227 {
228         struct device_node *np = ofdev->node;
229         struct zmii_instance *dev;
230         struct resource regs;
231         int rc;
232
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;
239         }
240
241         mutex_init(&dev->lock);
242         dev->ofdev = ofdev;
243         dev->mode = PHY_MODE_NA;
244
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;
250         }
251
252         rc = -ENOMEM;
253         dev->base = (struct zmii_regs *)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;
259         }
260
261         /* We may need FER value for autodetection later */
262         dev->fer_save = in_be32(&dev->base->fer);
263
264         /* Disable all inputs by default */
265         out_be32(&dev->base->fer, 0);
266
267         printk(KERN_INFO
268                "ZMII %s initialized\n", ofdev->node->full_name);
269         wmb();
270         dev_set_drvdata(&ofdev->dev, dev);
271
272         return 0;
273
274  err_free:
275         kfree(dev);
276  err_gone:
277         return rc;
278 }
279
280 static int __devexit zmii_remove(struct of_device *ofdev)
281 {
282         struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
283
284         dev_set_drvdata(&ofdev->dev, NULL);
285
286         WARN_ON(dev->users != 0);
287
288         iounmap(dev->base);
289         kfree(dev);
290
291         return 0;
292 }
293
294 static struct of_device_id zmii_match[] =
295 {
296         {
297                 .compatible     = "ibm,zmii",
298         },
299         /* For backward compat with old DT */
300         {
301                 .type           = "emac-zmii",
302         },
303         {},
304 };
305
306 static struct of_platform_driver zmii_driver = {
307         .name = "emac-zmii",
308         .match_table = zmii_match,
309
310         .probe = zmii_probe,
311         .remove = zmii_remove,
312 };
313
314 int __init zmii_init(void)
315 {
316         return of_register_platform_driver(&zmii_driver);
317 }
318
319 void zmii_exit(void)
320 {
321         of_unregister_platform_driver(&zmii_driver);
322 }