Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / net / fsl_pq_mdio.c
blobaa1eb88c21fc725196f4ff084b073b41298a4aa3
1 /*
2 * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
3 * Provides Bus interface for MIIM regs
5 * Author: Andy Fleming <afleming@freescale.com>
7 * Copyright (c) 2002-2004,2008 Freescale Semiconductor, Inc.
9 * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/mm.h>
31 #include <linux/module.h>
32 #include <linux/platform_device.h>
33 #include <linux/crc32.h>
34 #include <linux/mii.h>
35 #include <linux/phy.h>
36 #include <linux/of.h>
37 #include <linux/of_platform.h>
39 #include <asm/io.h>
40 #include <asm/irq.h>
41 #include <asm/uaccess.h>
42 #include <asm/ucc.h>
44 #include "gianfar.h"
45 #include "fsl_pq_mdio.h"
48 * Write value to the PHY at mii_id at register regnum,
49 * on the bus attached to the local interface, which may be different from the
50 * generic mdio bus (tied to a single interface), waiting until the write is
51 * done before returning. This is helpful in programming interfaces like
52 * the TBI which control interfaces like onchip SERDES and are always tied to
53 * the local mdio pins, which may not be the same as system mdio bus, used for
54 * controlling the external PHYs, for example.
56 int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
57 int regnum, u16 value)
59 /* Set the PHY address and the register address we want to write */
60 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
62 /* Write out the value we want */
63 out_be32(&regs->miimcon, value);
65 /* Wait for the transaction to finish */
66 while (in_be32(&regs->miimind) & MIIMIND_BUSY)
67 cpu_relax();
69 return 0;
73 * Read the bus for PHY at addr mii_id, register regnum, and
74 * return the value. Clears miimcom first. All PHY operation
75 * done on the bus attached to the local interface,
76 * which may be different from the generic mdio bus
77 * This is helpful in programming interfaces like
78 * the TBI which, in turn, control interfaces like onchip SERDES
79 * and are always tied to the local mdio pins, which may not be the
80 * same as system mdio bus, used for controlling the external PHYs, for eg.
82 int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
83 int mii_id, int regnum)
85 u16 value;
87 /* Set the PHY address and the register address we want to read */
88 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
90 /* Clear miimcom, and then initiate a read */
91 out_be32(&regs->miimcom, 0);
92 out_be32(&regs->miimcom, MII_READ_COMMAND);
94 /* Wait for the transaction to finish */
95 while (in_be32(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
96 cpu_relax();
98 /* Grab the value of the register from miimstat */
99 value = in_be32(&regs->miimstat);
101 return value;
105 * Write value to the PHY at mii_id at register regnum,
106 * on the bus, waiting until the write is done before returning.
108 int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
110 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
112 /* Write to the local MII regs */
113 return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
117 * Read the bus for PHY at addr mii_id, register regnum, and
118 * return the value. Clears miimcom first.
120 int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
122 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
124 /* Read the local MII regs */
125 return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
128 /* Reset the MIIM registers, and wait for the bus to free */
129 static int fsl_pq_mdio_reset(struct mii_bus *bus)
131 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
132 int timeout = PHY_INIT_TIMEOUT;
134 mutex_lock(&bus->mdio_lock);
136 /* Reset the management interface */
137 out_be32(&regs->miimcfg, MIIMCFG_RESET);
139 /* Setup the MII Mgmt clock speed */
140 out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
142 /* Wait until the bus is free */
143 while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
144 cpu_relax();
146 mutex_unlock(&bus->mdio_lock);
148 if (timeout < 0) {
149 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
150 bus->name);
151 return -EBUSY;
154 return 0;
157 /* Allocate an array which provides irq #s for each PHY on the given bus */
158 static int *create_irq_map(struct device_node *np)
160 int *irqs;
161 int i;
162 struct device_node *child = NULL;
164 irqs = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
166 if (!irqs)
167 return NULL;
169 for (i = 0; i < PHY_MAX_ADDR; i++)
170 irqs[i] = PHY_POLL;
172 while ((child = of_get_next_child(np, child)) != NULL) {
173 int irq = irq_of_parse_and_map(child, 0);
174 const u32 *id;
176 if (irq == NO_IRQ)
177 continue;
179 id = of_get_property(child, "reg", NULL);
181 if (!id)
182 continue;
184 if (*id < PHY_MAX_ADDR && *id >= 0)
185 irqs[*id] = irq;
186 else
187 printk(KERN_WARNING "%s: "
188 "%d is not a valid PHY address\n",
189 np->full_name, *id);
192 return irqs;
195 void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
197 const u32 *addr;
198 u64 taddr = OF_BAD_ADDR;
200 addr = of_get_address(np, 0, NULL, NULL);
201 if (addr)
202 taddr = of_translate_address(np, addr);
204 snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
205 (unsigned long long)taddr);
207 EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
209 /* Scan the bus in reverse, looking for an empty spot */
210 static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
212 int i;
214 for (i = PHY_MAX_ADDR; i > 0; i--) {
215 u32 phy_id;
217 if (get_phy_id(new_bus, i, &phy_id))
218 return -1;
220 if (phy_id == 0xffffffff)
221 break;
224 return i;
228 #ifdef CONFIG_GIANFAR
229 static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs)
231 struct gfar __iomem *enet_regs;
234 * This is mildly evil, but so is our hardware for doing this.
235 * Also, we have to cast back to struct gfar because of
236 * definition weirdness done in gianfar.h.
238 enet_regs = (struct gfar __iomem *)
239 ((char __iomem *)regs - offsetof(struct gfar, gfar_mii_regs));
241 return &enet_regs->tbipa;
243 #endif
246 #ifdef CONFIG_UCC_GETH
247 static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
249 struct device_node *np = NULL;
250 int err = 0;
252 for_each_compatible_node(np, NULL, "ucc_geth") {
253 struct resource tempres;
255 err = of_address_to_resource(np, 0, &tempres);
256 if (err)
257 continue;
259 /* if our mdio regs fall within this UCC regs range */
260 if ((start >= tempres.start) && (end <= tempres.end)) {
261 /* Find the id of the UCC */
262 const u32 *id;
264 id = of_get_property(np, "cell-index", NULL);
265 if (!id) {
266 id = of_get_property(np, "device-id", NULL);
267 if (!id)
268 continue;
271 *ucc_id = *id;
273 return 0;
277 if (err)
278 return err;
279 else
280 return -EINVAL;
282 #endif
285 static int fsl_pq_mdio_probe(struct of_device *ofdev,
286 const struct of_device_id *match)
288 struct device_node *np = ofdev->node;
289 struct device_node *tbi;
290 struct fsl_pq_mdio __iomem *regs;
291 u32 __iomem *tbipa;
292 struct mii_bus *new_bus;
293 int tbiaddr = -1;
294 u64 addr, size;
295 int err = 0;
297 new_bus = mdiobus_alloc();
298 if (NULL == new_bus)
299 return -ENOMEM;
301 new_bus->name = "Freescale PowerQUICC MII Bus",
302 new_bus->read = &fsl_pq_mdio_read,
303 new_bus->write = &fsl_pq_mdio_write,
304 new_bus->reset = &fsl_pq_mdio_reset,
305 fsl_pq_mdio_bus_name(new_bus->id, np);
307 /* Set the PHY base address */
308 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
309 regs = ioremap(addr, size);
311 if (NULL == regs) {
312 err = -ENOMEM;
313 goto err_free_bus;
316 new_bus->priv = (void __force *)regs;
318 new_bus->irq = create_irq_map(np);
320 if (NULL == new_bus->irq) {
321 err = -ENOMEM;
322 goto err_unmap_regs;
325 new_bus->parent = &ofdev->dev;
326 dev_set_drvdata(&ofdev->dev, new_bus);
328 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
329 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
330 of_device_is_compatible(np, "gianfar")) {
331 #ifdef CONFIG_GIANFAR
332 tbipa = get_gfar_tbipa(regs);
333 #else
334 err = -ENODEV;
335 goto err_free_irqs;
336 #endif
337 } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
338 of_device_is_compatible(np, "ucc_geth_phy")) {
339 #ifdef CONFIG_UCC_GETH
340 u32 id;
342 tbipa = &regs->utbipar;
344 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
345 goto err_free_irqs;
347 ucc_set_qe_mux_mii_mng(id - 1);
348 #else
349 err = -ENODEV;
350 goto err_free_irqs;
351 #endif
352 } else {
353 err = -ENODEV;
354 goto err_free_irqs;
357 for_each_child_of_node(np, tbi) {
358 if (!strncmp(tbi->type, "tbi-phy", 8))
359 break;
362 if (tbi) {
363 const u32 *prop = of_get_property(tbi, "reg", NULL);
365 if (prop)
366 tbiaddr = *prop;
369 if (tbiaddr == -1) {
370 out_be32(tbipa, 0);
372 tbiaddr = fsl_pq_mdio_find_free(new_bus);
376 * We define TBIPA at 0 to be illegal, opting to fail for boards that
377 * have PHYs at 1-31, rather than change tbipa and rescan.
379 if (tbiaddr == 0) {
380 err = -EBUSY;
382 goto err_free_irqs;
385 out_be32(tbipa, tbiaddr);
388 * The TBIPHY-only buses will find PHYs at every address,
389 * so we mask them all but the TBI
391 if (of_device_is_compatible(np, "fsl,gianfar-tbi"))
392 new_bus->phy_mask = ~(1 << tbiaddr);
394 err = mdiobus_register(new_bus);
396 if (err) {
397 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
398 new_bus->name);
399 goto err_free_irqs;
402 return 0;
404 err_free_irqs:
405 kfree(new_bus->irq);
406 err_unmap_regs:
407 iounmap(regs);
408 err_free_bus:
409 kfree(new_bus);
411 return err;
415 static int fsl_pq_mdio_remove(struct of_device *ofdev)
417 struct device *device = &ofdev->dev;
418 struct mii_bus *bus = dev_get_drvdata(device);
420 mdiobus_unregister(bus);
422 dev_set_drvdata(device, NULL);
424 iounmap((void __iomem *)bus->priv);
425 bus->priv = NULL;
426 mdiobus_free(bus);
428 return 0;
431 static struct of_device_id fsl_pq_mdio_match[] = {
433 .type = "mdio",
434 .compatible = "ucc_geth_phy",
437 .type = "mdio",
438 .compatible = "gianfar",
441 .compatible = "fsl,ucc-mdio",
444 .compatible = "fsl,gianfar-tbi",
447 .compatible = "fsl,gianfar-mdio",
452 static struct of_platform_driver fsl_pq_mdio_driver = {
453 .name = "fsl-pq_mdio",
454 .probe = fsl_pq_mdio_probe,
455 .remove = fsl_pq_mdio_remove,
456 .match_table = fsl_pq_mdio_match,
459 int __init fsl_pq_mdio_init(void)
461 return of_register_platform_driver(&fsl_pq_mdio_driver);
464 void fsl_pq_mdio_exit(void)
466 of_unregister_platform_driver(&fsl_pq_mdio_driver);
468 subsys_initcall_sync(fsl_pq_mdio_init);
469 module_exit(fsl_pq_mdio_exit);