fsl_pq_mdio: Fix compiler/sparse warnings (part 1)
[safe/jmp/linux-2.6] / drivers / net / fsl_pq_mdio.c
1 /*
2  * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
3  * Provides Bus interface for MIIM regs
4  *
5  * Author: Andy Fleming <afleming@freescale.com>
6  * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
7  *
8  * Copyright 2002-2004, 2008-2009 Freescale Semiconductor, Inc.
9  *
10  * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/platform_device.h>
34 #include <linux/crc32.h>
35 #include <linux/mii.h>
36 #include <linux/phy.h>
37 #include <linux/of.h>
38 #include <linux/of_mdio.h>
39 #include <linux/of_platform.h>
40
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/uaccess.h>
44 #include <asm/ucc.h>
45
46 #include "gianfar.h"
47 #include "fsl_pq_mdio.h"
48
49 /*
50  * Write value to the PHY at mii_id at register regnum,
51  * on the bus attached to the local interface, which may be different from the
52  * generic mdio bus (tied to a single interface), waiting until the write is
53  * done before returning. This is helpful in programming interfaces like
54  * the TBI which control interfaces like onchip SERDES and are always tied to
55  * the local mdio pins, which may not be the same as system mdio bus, used for
56  * controlling the external PHYs, for example.
57  */
58 int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
59                 int regnum, u16 value)
60 {
61         /* Set the PHY address and the register address we want to write */
62         out_be32(&regs->miimadd, (mii_id << 8) | regnum);
63
64         /* Write out the value we want */
65         out_be32(&regs->miimcon, value);
66
67         /* Wait for the transaction to finish */
68         while (in_be32(&regs->miimind) & MIIMIND_BUSY)
69                 cpu_relax();
70
71         return 0;
72 }
73
74 /*
75  * Read the bus for PHY at addr mii_id, register regnum, and
76  * return the value.  Clears miimcom first.  All PHY operation
77  * done on the bus attached to the local interface,
78  * which may be different from the generic mdio bus
79  * This is helpful in programming interfaces like
80  * the TBI which, in turn, control interfaces like onchip SERDES
81  * and are always tied to the local mdio pins, which may not be the
82  * same as system mdio bus, used for controlling the external PHYs, for eg.
83  */
84 int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
85                 int mii_id, int regnum)
86 {
87         u16 value;
88
89         /* Set the PHY address and the register address we want to read */
90         out_be32(&regs->miimadd, (mii_id << 8) | regnum);
91
92         /* Clear miimcom, and then initiate a read */
93         out_be32(&regs->miimcom, 0);
94         out_be32(&regs->miimcom, MII_READ_COMMAND);
95
96         /* Wait for the transaction to finish */
97         while (in_be32(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
98                 cpu_relax();
99
100         /* Grab the value of the register from miimstat */
101         value = in_be32(&regs->miimstat);
102
103         return value;
104 }
105
106 /*
107  * Write value to the PHY at mii_id at register regnum,
108  * on the bus, waiting until the write is done before returning.
109  */
110 int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
111 {
112         struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
113
114         /* Write to the local MII regs */
115         return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
116 }
117
118 /*
119  * Read the bus for PHY at addr mii_id, register regnum, and
120  * return the value.  Clears miimcom first.
121  */
122 int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
123 {
124         struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
125
126         /* Read the local MII regs */
127         return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
128 }
129
130 /* Reset the MIIM registers, and wait for the bus to free */
131 static int fsl_pq_mdio_reset(struct mii_bus *bus)
132 {
133         struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
134         int timeout = PHY_INIT_TIMEOUT;
135
136         mutex_lock(&bus->mdio_lock);
137
138         /* Reset the management interface */
139         out_be32(&regs->miimcfg, MIIMCFG_RESET);
140
141         /* Setup the MII Mgmt clock speed */
142         out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
143
144         /* Wait until the bus is free */
145         while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
146                 cpu_relax();
147
148         mutex_unlock(&bus->mdio_lock);
149
150         if (timeout < 0) {
151                 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
152                                 bus->name);
153                 return -EBUSY;
154         }
155
156         return 0;
157 }
158
159 void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
160 {
161         const u32 *addr;
162         u64 taddr = OF_BAD_ADDR;
163
164         addr = of_get_address(np, 0, NULL, NULL);
165         if (addr)
166                 taddr = of_translate_address(np, addr);
167
168         snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
169                 (unsigned long long)taddr);
170 }
171 EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
172
173 /* Scan the bus in reverse, looking for an empty spot */
174 static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
175 {
176         int i;
177
178         for (i = PHY_MAX_ADDR; i > 0; i--) {
179                 u32 phy_id;
180
181                 if (get_phy_id(new_bus, i, &phy_id))
182                         return -1;
183
184                 if (phy_id == 0xffffffff)
185                         break;
186         }
187
188         return i;
189 }
190
191
192 #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
193 static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
194 {
195         struct gfar __iomem *enet_regs;
196         u32 __iomem *ioremap_tbipa;
197         u64 addr, size;
198
199         /*
200          * This is mildly evil, but so is our hardware for doing this.
201          * Also, we have to cast back to struct gfar because of
202          * definition weirdness done in gianfar.h.
203          */
204         if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
205                 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
206                 of_device_is_compatible(np, "gianfar")) {
207                 enet_regs = (struct gfar __iomem *)regs;
208                 return &enet_regs->tbipa;
209         } else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
210                         of_device_is_compatible(np, "fsl,etsec2-tbi")) {
211                 addr = of_translate_address(np, of_get_address(np, 1, &size, NULL));
212                 ioremap_tbipa = ioremap(addr, size);
213                 return ioremap_tbipa;
214         } else
215                 return NULL;
216 }
217 #endif
218
219
220 #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
221 static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
222 {
223         struct device_node *np = NULL;
224         int err = 0;
225
226         for_each_compatible_node(np, NULL, "ucc_geth") {
227                 struct resource tempres;
228
229                 err = of_address_to_resource(np, 0, &tempres);
230                 if (err)
231                         continue;
232
233                 /* if our mdio regs fall within this UCC regs range */
234                 if ((start >= tempres.start) && (end <= tempres.end)) {
235                         /* Find the id of the UCC */
236                         const u32 *id;
237
238                         id = of_get_property(np, "cell-index", NULL);
239                         if (!id) {
240                                 id = of_get_property(np, "device-id", NULL);
241                                 if (!id)
242                                         continue;
243                         }
244
245                         *ucc_id = *id;
246
247                         return 0;
248                 }
249         }
250
251         if (err)
252                 return err;
253         else
254                 return -EINVAL;
255 }
256 #endif
257
258
259 static int fsl_pq_mdio_probe(struct of_device *ofdev,
260                 const struct of_device_id *match)
261 {
262         struct device_node *np = ofdev->node;
263         struct device_node *tbi;
264         struct fsl_pq_mdio __iomem *regs = NULL;
265         void __iomem *map;
266         u32 __iomem *tbipa;
267         struct mii_bus *new_bus;
268         int tbiaddr = -1;
269         u64 addr = 0, size = 0;
270         int err = 0;
271
272         new_bus = mdiobus_alloc();
273         if (NULL == new_bus)
274                 return -ENOMEM;
275
276         new_bus->name = "Freescale PowerQUICC MII Bus",
277         new_bus->read = &fsl_pq_mdio_read,
278         new_bus->write = &fsl_pq_mdio_write,
279         new_bus->reset = &fsl_pq_mdio_reset,
280         fsl_pq_mdio_bus_name(new_bus->id, np);
281
282         /* Set the PHY base address */
283         addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
284         map = ioremap(addr, size);
285         if (!map) {
286                 err = -ENOMEM;
287                 goto err_free_bus;
288         }
289
290         if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
291                         of_device_is_compatible(np, "fsl,gianfar-tbi") ||
292                         of_device_is_compatible(np, "fsl,ucc-mdio") ||
293                         of_device_is_compatible(np, "ucc_geth_phy"))
294                 map -= offsetof(struct fsl_pq_mdio, miimcfg);
295         regs = map;
296
297         new_bus->priv = (void __force *)regs;
298
299         new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
300
301         if (NULL == new_bus->irq) {
302                 err = -ENOMEM;
303                 goto err_unmap_regs;
304         }
305
306         new_bus->parent = &ofdev->dev;
307         dev_set_drvdata(&ofdev->dev, new_bus);
308
309         if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
310                         of_device_is_compatible(np, "fsl,gianfar-tbi") ||
311                         of_device_is_compatible(np, "fsl,etsec2-mdio") ||
312                         of_device_is_compatible(np, "fsl,etsec2-tbi") ||
313                         of_device_is_compatible(np, "gianfar")) {
314 #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
315                 tbipa = get_gfar_tbipa(regs, np);
316                 if (!tbipa) {
317                         err = -EINVAL;
318                         goto err_free_irqs;
319                 }
320 #else
321                 err = -ENODEV;
322                 goto err_free_irqs;
323 #endif
324         } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
325                         of_device_is_compatible(np, "ucc_geth_phy")) {
326 #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
327                 u32 id;
328                 static u32 mii_mng_master;
329
330                 tbipa = &regs->utbipar;
331
332                 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
333                         goto err_free_irqs;
334
335                 if (!mii_mng_master) {
336                         mii_mng_master = id;
337                         ucc_set_qe_mux_mii_mng(id - 1);
338                 }
339 #else
340                 err = -ENODEV;
341                 goto err_free_irqs;
342 #endif
343         } else {
344                 err = -ENODEV;
345                 goto err_free_irqs;
346         }
347
348         for_each_child_of_node(np, tbi) {
349                 if (!strncmp(tbi->type, "tbi-phy", 8))
350                         break;
351         }
352
353         if (tbi) {
354                 const u32 *prop = of_get_property(tbi, "reg", NULL);
355
356                 if (prop)
357                         tbiaddr = *prop;
358         }
359
360         if (tbiaddr == -1) {
361                 out_be32(tbipa, 0);
362
363                 tbiaddr = fsl_pq_mdio_find_free(new_bus);
364         }
365
366         /*
367          * We define TBIPA at 0 to be illegal, opting to fail for boards that
368          * have PHYs at 1-31, rather than change tbipa and rescan.
369          */
370         if (tbiaddr == 0) {
371                 err = -EBUSY;
372
373                 goto err_free_irqs;
374         }
375
376         out_be32(tbipa, tbiaddr);
377
378         err = of_mdiobus_register(new_bus, np);
379         if (err) {
380                 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
381                                 new_bus->name);
382                 goto err_free_irqs;
383         }
384
385         return 0;
386
387 err_free_irqs:
388         kfree(new_bus->irq);
389 err_unmap_regs:
390         iounmap(regs);
391 err_free_bus:
392         kfree(new_bus);
393
394         return err;
395 }
396
397
398 static int fsl_pq_mdio_remove(struct of_device *ofdev)
399 {
400         struct device *device = &ofdev->dev;
401         struct mii_bus *bus = dev_get_drvdata(device);
402
403         mdiobus_unregister(bus);
404
405         dev_set_drvdata(device, NULL);
406
407         iounmap((void __iomem *)bus->priv);
408         bus->priv = NULL;
409         mdiobus_free(bus);
410
411         return 0;
412 }
413
414 static struct of_device_id fsl_pq_mdio_match[] = {
415         {
416                 .type = "mdio",
417                 .compatible = "ucc_geth_phy",
418         },
419         {
420                 .type = "mdio",
421                 .compatible = "gianfar",
422         },
423         {
424                 .compatible = "fsl,ucc-mdio",
425         },
426         {
427                 .compatible = "fsl,gianfar-tbi",
428         },
429         {
430                 .compatible = "fsl,gianfar-mdio",
431         },
432         {
433                 .compatible = "fsl,etsec2-tbi",
434         },
435         {
436                 .compatible = "fsl,etsec2-mdio",
437         },
438         {},
439 };
440 MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
441
442 static struct of_platform_driver fsl_pq_mdio_driver = {
443         .name = "fsl-pq_mdio",
444         .probe = fsl_pq_mdio_probe,
445         .remove = fsl_pq_mdio_remove,
446         .match_table = fsl_pq_mdio_match,
447 };
448
449 int __init fsl_pq_mdio_init(void)
450 {
451         return of_register_platform_driver(&fsl_pq_mdio_driver);
452 }
453 module_init(fsl_pq_mdio_init);
454
455 void fsl_pq_mdio_exit(void)
456 {
457         of_unregister_platform_driver(&fsl_pq_mdio_driver);
458 }
459 module_exit(fsl_pq_mdio_exit);