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>
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>
37 #include <linux/of_mdio.h>
38 #include <linux/of_platform.h>
42 #include <asm/uaccess.h>
46 #include "fsl_pq_mdio.h"
49 * Write value to the PHY at mii_id at register regnum,
50 * on the bus attached to the local interface, which may be different from the
51 * generic mdio bus (tied to a single interface), waiting until the write is
52 * done before returning. This is helpful in programming interfaces like
53 * the TBI which control interfaces like onchip SERDES and are always tied to
54 * the local mdio pins, which may not be the same as system mdio bus, used for
55 * controlling the external PHYs, for example.
57 int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
58 int regnum, u16 value)
60 /* Set the PHY address and the register address we want to write */
61 out_be32(®s->miimadd, (mii_id << 8) | regnum);
63 /* Write out the value we want */
64 out_be32(®s->miimcon, value);
66 /* Wait for the transaction to finish */
67 while (in_be32(®s->miimind) & MIIMIND_BUSY)
74 * Read the bus for PHY at addr mii_id, register regnum, and
75 * return the value. Clears miimcom first. All PHY operation
76 * done on the bus attached to the local interface,
77 * which may be different from the generic mdio bus
78 * This is helpful in programming interfaces like
79 * the TBI which, in turn, control interfaces like onchip SERDES
80 * and are always tied to the local mdio pins, which may not be the
81 * same as system mdio bus, used for controlling the external PHYs, for eg.
83 int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
84 int mii_id, int regnum)
88 /* Set the PHY address and the register address we want to read */
89 out_be32(®s->miimadd, (mii_id << 8) | regnum);
91 /* Clear miimcom, and then initiate a read */
92 out_be32(®s->miimcom, 0);
93 out_be32(®s->miimcom, MII_READ_COMMAND);
95 /* Wait for the transaction to finish */
96 while (in_be32(®s->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
99 /* Grab the value of the register from miimstat */
100 value = in_be32(®s->miimstat);
106 * Write value to the PHY at mii_id at register regnum,
107 * on the bus, waiting until the write is done before returning.
109 int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
111 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
113 /* Write to the local MII regs */
114 return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
118 * Read the bus for PHY at addr mii_id, register regnum, and
119 * return the value. Clears miimcom first.
121 int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
123 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
125 /* Read the local MII regs */
126 return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
129 /* Reset the MIIM registers, and wait for the bus to free */
130 static int fsl_pq_mdio_reset(struct mii_bus *bus)
132 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
133 int timeout = PHY_INIT_TIMEOUT;
135 mutex_lock(&bus->mdio_lock);
137 /* Reset the management interface */
138 out_be32(®s->miimcfg, MIIMCFG_RESET);
140 /* Setup the MII Mgmt clock speed */
141 out_be32(®s->miimcfg, MIIMCFG_INIT_VALUE);
143 /* Wait until the bus is free */
144 while ((in_be32(®s->miimind) & MIIMIND_BUSY) && timeout--)
147 mutex_unlock(&bus->mdio_lock);
150 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
158 void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
161 u64 taddr = OF_BAD_ADDR;
163 addr = of_get_address(np, 0, NULL, NULL);
165 taddr = of_translate_address(np, addr);
167 snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
168 (unsigned long long)taddr);
170 EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
172 /* Scan the bus in reverse, looking for an empty spot */
173 static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
177 for (i = PHY_MAX_ADDR; i > 0; i--) {
180 if (get_phy_id(new_bus, i, &phy_id))
183 if (phy_id == 0xffffffff)
191 #ifdef CONFIG_GIANFAR
192 static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs)
194 struct gfar __iomem *enet_regs;
197 * This is mildly evil, but so is our hardware for doing this.
198 * Also, we have to cast back to struct gfar because of
199 * definition weirdness done in gianfar.h.
201 enet_regs = (struct gfar __iomem *)
202 ((char __iomem *)regs - offsetof(struct gfar, gfar_mii_regs));
204 return &enet_regs->tbipa;
209 #ifdef CONFIG_UCC_GETH
210 static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
212 struct device_node *np = NULL;
215 for_each_compatible_node(np, NULL, "ucc_geth") {
216 struct resource tempres;
218 err = of_address_to_resource(np, 0, &tempres);
222 /* if our mdio regs fall within this UCC regs range */
223 if ((start >= tempres.start) && (end <= tempres.end)) {
224 /* Find the id of the UCC */
227 id = of_get_property(np, "cell-index", NULL);
229 id = of_get_property(np, "device-id", NULL);
248 static int fsl_pq_mdio_probe(struct of_device *ofdev,
249 const struct of_device_id *match)
251 struct device_node *np = ofdev->node;
252 struct device_node *tbi;
253 struct fsl_pq_mdio __iomem *regs;
255 struct mii_bus *new_bus;
260 new_bus = mdiobus_alloc();
264 new_bus->name = "Freescale PowerQUICC MII Bus",
265 new_bus->read = &fsl_pq_mdio_read,
266 new_bus->write = &fsl_pq_mdio_write,
267 new_bus->reset = &fsl_pq_mdio_reset,
268 fsl_pq_mdio_bus_name(new_bus->id, np);
270 /* Set the PHY base address */
271 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
272 regs = ioremap(addr, size);
279 new_bus->priv = (void __force *)regs;
281 new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
283 if (NULL == new_bus->irq) {
288 new_bus->parent = &ofdev->dev;
289 dev_set_drvdata(&ofdev->dev, new_bus);
291 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
292 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
293 of_device_is_compatible(np, "gianfar")) {
294 #ifdef CONFIG_GIANFAR
295 tbipa = get_gfar_tbipa(regs);
300 } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
301 of_device_is_compatible(np, "ucc_geth_phy")) {
302 #ifdef CONFIG_UCC_GETH
304 static u32 mii_mng_master;
306 tbipa = ®s->utbipar;
308 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
311 if (!mii_mng_master) {
313 ucc_set_qe_mux_mii_mng(id - 1);
324 for_each_child_of_node(np, tbi) {
325 if (!strncmp(tbi->type, "tbi-phy", 8))
330 const u32 *prop = of_get_property(tbi, "reg", NULL);
339 tbiaddr = fsl_pq_mdio_find_free(new_bus);
343 * We define TBIPA at 0 to be illegal, opting to fail for boards that
344 * have PHYs at 1-31, rather than change tbipa and rescan.
352 out_be32(tbipa, tbiaddr);
354 err = of_mdiobus_register(new_bus, np);
356 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
374 static int fsl_pq_mdio_remove(struct of_device *ofdev)
376 struct device *device = &ofdev->dev;
377 struct mii_bus *bus = dev_get_drvdata(device);
379 mdiobus_unregister(bus);
381 dev_set_drvdata(device, NULL);
383 iounmap((void __iomem *)bus->priv);
390 static struct of_device_id fsl_pq_mdio_match[] = {
393 .compatible = "ucc_geth_phy",
397 .compatible = "gianfar",
400 .compatible = "fsl,ucc-mdio",
403 .compatible = "fsl,gianfar-tbi",
406 .compatible = "fsl,gianfar-mdio",
411 static struct of_platform_driver fsl_pq_mdio_driver = {
412 .name = "fsl-pq_mdio",
413 .probe = fsl_pq_mdio_probe,
414 .remove = fsl_pq_mdio_remove,
415 .match_table = fsl_pq_mdio_match,
418 int __init fsl_pq_mdio_init(void)
420 return of_register_platform_driver(&fsl_pq_mdio_driver);
422 module_init(fsl_pq_mdio_init);
424 void fsl_pq_mdio_exit(void)
426 of_unregister_platform_driver(&fsl_pq_mdio_driver);
428 module_exit(fsl_pq_mdio_exit);