2 * drivers/net/ucc_geth_mii.c
4 * QE UCC Gigabit Ethernet Driver -- MII Management Bus Implementation
5 * Provides Bus interface for MII Management regs in the UCC register space
7 * Copyright (C) 2007 Freescale Semiconductor, Inc.
9 * Authors: Li Yang <leoli@freescale.com>
10 * Kim Phillips <kim.phillips@freescale.com>
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.
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/unistd.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/crc32.h>
36 #include <linux/mii.h>
37 #include <linux/phy.h>
38 #include <linux/fsl_devices.h>
39 #include <linux/of_platform.h>
43 #include <asm/uaccess.h>
46 #include "ucc_geth_mii.h"
51 #define vdbg(format, arg...) printk(KERN_DEBUG , format "\n" , ## arg)
53 #define vdbg(format, arg...) do {} while(0)
56 #define MII_DRV_DESC "QE UCC Ethernet Controller MII Bus"
57 #define MII_DRV_NAME "fsl-uec_mdio"
59 /* Write value to the PHY for this device to the register at regnum, */
60 /* waiting until the write is done before it returns. All PHY */
61 /* configuration has to be done through the master UEC MIIM regs */
62 int uec_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
64 struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
66 /* Setting up the MII Mangement Address Register */
67 out_be32(®s->miimadd,
68 (mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | regnum);
70 /* Setting up the MII Mangement Control Register with the value */
71 out_be32(®s->miimcon, value);
73 /* Wait till MII management write is complete */
74 while ((in_be32(®s->miimind)) & MIIMIND_BUSY)
80 /* Reads from register regnum in the PHY for device dev, */
81 /* returning the value. Clears miimcom first. All PHY */
82 /* configuration has to be done through the TSEC1 MIIM regs */
83 int uec_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
85 struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
88 /* Setting up the MII Mangement Address Register */
89 out_be32(®s->miimadd,
90 (mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | regnum);
92 /* Clear miimcom, perform an MII management read cycle */
93 out_be32(®s->miimcom, 0);
94 out_be32(®s->miimcom, MIIMCOM_READ_CYCLE);
96 /* Wait till MII management write is complete */
97 while ((in_be32(®s->miimind)) & (MIIMIND_BUSY | MIIMIND_NOT_VALID))
100 /* Read MII management status */
101 value = in_be32(®s->miimstat);
106 /* Reset the MIIM registers, and wait for the bus to free */
107 static int uec_mdio_reset(struct mii_bus *bus)
109 struct ucc_mii_mng __iomem *regs = (void __iomem *)bus->priv;
110 unsigned int timeout = PHY_INIT_TIMEOUT;
112 mutex_lock(&bus->mdio_lock);
114 /* Reset the management interface */
115 out_be32(®s->miimcfg, MIIMCFG_RESET_MANAGEMENT);
117 /* Setup the MII Mgmt clock speed */
118 out_be32(®s->miimcfg, MIIMCFG_MANAGEMENT_CLOCK_DIVIDE_BY_112);
120 /* Wait until the bus is free */
121 while ((in_be32(®s->miimind) & MIIMIND_BUSY) && timeout--)
124 mutex_unlock(&bus->mdio_lock);
127 printk(KERN_ERR "%s: The MII Bus is stuck!\n", bus->name);
134 static int uec_mdio_probe(struct of_device *ofdev, const struct of_device_id *match)
136 struct device *device = &ofdev->dev;
137 struct device_node *np = ofdev->node, *tempnp = NULL;
138 struct device_node *child = NULL;
139 struct ucc_mii_mng __iomem *regs;
140 struct mii_bus *new_bus;
144 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
149 new_bus->name = "UCC Ethernet Controller MII Bus";
150 new_bus->read = &uec_mdio_read;
151 new_bus->write = &uec_mdio_write;
152 new_bus->reset = &uec_mdio_reset;
154 memset(&res, 0, sizeof(res));
156 err = of_address_to_resource(np, 0, &res);
160 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
162 new_bus->irq = kmalloc(32 * sizeof(int), GFP_KERNEL);
164 if (NULL == new_bus->irq) {
169 for (k = 0; k < 32; k++)
170 new_bus->irq[k] = PHY_POLL;
172 while ((child = of_get_next_child(np, child)) != NULL) {
173 int irq = irq_of_parse_and_map(child, 0);
175 const u32 *id = of_get_property(child, "reg", NULL);
176 new_bus->irq[*id] = irq;
180 /* Set the base address */
181 regs = ioremap(res.start, sizeof(struct ucc_mii_mng));
188 new_bus->priv = (void __force *)regs;
190 new_bus->dev = device;
191 dev_set_drvdata(device, new_bus);
193 /* Read MII management master from device tree */
194 while ((tempnp = of_find_compatible_node(tempnp, "network", "ucc_geth"))
196 struct resource tempres;
198 err = of_address_to_resource(tempnp, 0, &tempres);
200 goto bus_register_fail;
202 /* if our mdio regs fall within this UCC regs range */
203 if ((res.start >= tempres.start) &&
204 (res.end <= tempres.end)) {
205 /* set this UCC to be the MII master */
208 id = of_get_property(tempnp, "cell-index", NULL);
210 id = of_get_property(tempnp, "device-id", NULL);
212 goto bus_register_fail;
215 ucc_set_qe_mux_mii_mng(*id - 1);
217 /* assign the TBI an address which won't
218 * conflict with the PHYs */
219 out_be32(®s->utbipar, UTBIPAR_INIT_TBIPA);
224 err = mdiobus_register(new_bus);
226 printk(KERN_ERR "%s: Cannot register as MDIO bus\n",
228 goto bus_register_fail;
243 static int uec_mdio_remove(struct of_device *ofdev)
245 struct device *device = &ofdev->dev;
246 struct mii_bus *bus = dev_get_drvdata(device);
248 mdiobus_unregister(bus);
250 dev_set_drvdata(device, NULL);
252 iounmap((void __iomem *)bus->priv);
259 static struct of_device_id uec_mdio_match[] = {
262 .compatible = "ucc_geth_phy",
265 .compatible = "fsl,ucc-mdio",
270 static struct of_platform_driver uec_mdio_driver = {
271 .name = MII_DRV_NAME,
272 .probe = uec_mdio_probe,
273 .remove = uec_mdio_remove,
274 .match_table = uec_mdio_match,
277 int __init uec_mdio_init(void)
279 return of_register_platform_driver(&uec_mdio_driver);
282 /* called from __init ucc_geth_init, therefore can not be __exit */
283 void uec_mdio_exit(void)
285 of_unregister_platform_driver(&uec_mdio_driver);