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