mac80211: fix ampdu_action tx_start ssn
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / fsl_pq_mdio.c
blob25fabb3eedc58c5045fbd4f8055c69c23f6fd063
1 /*
2 * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
3 * Provides Bus interface for MIIM regs
5 * Author: Andy Fleming <afleming@freescale.com>
6 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
8 * Copyright 2002-2004, 2008-2009 Freescale Semiconductor, Inc.
10 * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
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/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>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/uaccess.h>
44 #include <asm/ucc.h>
46 #include "gianfar.h"
47 #include "fsl_pq_mdio.h"
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.
58 int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
59 int regnum, u16 value)
61 /* Set the PHY address and the register address we want to write */
62 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
64 /* Write out the value we want */
65 out_be32(&regs->miimcon, value);
67 /* Wait for the transaction to finish */
68 while (in_be32(&regs->miimind) & MIIMIND_BUSY)
69 cpu_relax();
71 return 0;
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.
84 int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
85 int mii_id, int regnum)
87 u16 value;
89 /* Set the PHY address and the register address we want to read */
90 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
92 /* Clear miimcom, and then initiate a read */
93 out_be32(&regs->miimcom, 0);
94 out_be32(&regs->miimcom, MII_READ_COMMAND);
96 /* Wait for the transaction to finish */
97 while (in_be32(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
98 cpu_relax();
100 /* Grab the value of the register from miimstat */
101 value = in_be32(&regs->miimstat);
103 return value;
106 static struct fsl_pq_mdio __iomem *fsl_pq_mdio_get_regs(struct mii_bus *bus)
108 return (void __iomem __force *)bus->priv;
112 * Write value to the PHY at mii_id at register regnum,
113 * on the bus, waiting until the write is done before returning.
115 int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
117 struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
119 /* Write to the local MII regs */
120 return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
124 * Read the bus for PHY at addr mii_id, register regnum, and
125 * return the value. Clears miimcom first.
127 int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
129 struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
131 /* Read the local MII regs */
132 return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
135 /* Reset the MIIM registers, and wait for the bus to free */
136 static int fsl_pq_mdio_reset(struct mii_bus *bus)
138 struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
139 int timeout = PHY_INIT_TIMEOUT;
141 mutex_lock(&bus->mdio_lock);
143 /* Reset the management interface */
144 out_be32(&regs->miimcfg, MIIMCFG_RESET);
146 /* Setup the MII Mgmt clock speed */
147 out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
149 /* Wait until the bus is free */
150 while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
151 cpu_relax();
153 mutex_unlock(&bus->mdio_lock);
155 if (timeout < 0) {
156 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
157 bus->name);
158 return -EBUSY;
161 return 0;
164 void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
166 const u32 *addr;
167 u64 taddr = OF_BAD_ADDR;
169 addr = of_get_address(np, 0, NULL, NULL);
170 if (addr)
171 taddr = of_translate_address(np, addr);
173 snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
174 (unsigned long long)taddr);
176 EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
178 /* Scan the bus in reverse, looking for an empty spot */
179 static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
181 int i;
183 for (i = PHY_MAX_ADDR; i > 0; i--) {
184 u32 phy_id;
186 if (get_phy_id(new_bus, i, &phy_id))
187 return -1;
189 if (phy_id == 0xffffffff)
190 break;
193 return i;
197 #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
198 static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
200 struct gfar __iomem *enet_regs;
201 u32 __iomem *ioremap_tbipa;
202 u64 addr, size;
205 * This is mildly evil, but so is our hardware for doing this.
206 * Also, we have to cast back to struct gfar because of
207 * definition weirdness done in gianfar.h.
209 if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
210 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
211 of_device_is_compatible(np, "gianfar")) {
212 enet_regs = (struct gfar __iomem *)regs;
213 return &enet_regs->tbipa;
214 } else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
215 of_device_is_compatible(np, "fsl,etsec2-tbi")) {
216 addr = of_translate_address(np, of_get_address(np, 1, &size, NULL));
217 ioremap_tbipa = ioremap(addr, size);
218 return ioremap_tbipa;
219 } else
220 return NULL;
222 #endif
225 #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
226 static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
228 struct device_node *np = NULL;
229 int err = 0;
231 for_each_compatible_node(np, NULL, "ucc_geth") {
232 struct resource tempres;
234 err = of_address_to_resource(np, 0, &tempres);
235 if (err)
236 continue;
238 /* if our mdio regs fall within this UCC regs range */
239 if ((start >= tempres.start) && (end <= tempres.end)) {
240 /* Find the id of the UCC */
241 const u32 *id;
243 id = of_get_property(np, "cell-index", NULL);
244 if (!id) {
245 id = of_get_property(np, "device-id", NULL);
246 if (!id)
247 continue;
250 *ucc_id = *id;
252 return 0;
256 if (err)
257 return err;
258 else
259 return -EINVAL;
261 #endif
264 static int fsl_pq_mdio_probe(struct of_device *ofdev,
265 const struct of_device_id *match)
267 struct device_node *np = ofdev->node;
268 struct device_node *tbi;
269 struct fsl_pq_mdio __iomem *regs = NULL;
270 void __iomem *map;
271 u32 __iomem *tbipa;
272 struct mii_bus *new_bus;
273 int tbiaddr = -1;
274 u64 addr = 0, size = 0;
275 int err = 0;
277 new_bus = mdiobus_alloc();
278 if (NULL == new_bus)
279 return -ENOMEM;
281 new_bus->name = "Freescale PowerQUICC MII Bus",
282 new_bus->read = &fsl_pq_mdio_read,
283 new_bus->write = &fsl_pq_mdio_write,
284 new_bus->reset = &fsl_pq_mdio_reset,
285 fsl_pq_mdio_bus_name(new_bus->id, np);
287 /* Set the PHY base address */
288 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
289 map = ioremap(addr, size);
290 if (!map) {
291 err = -ENOMEM;
292 goto err_free_bus;
295 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
296 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
297 of_device_is_compatible(np, "fsl,ucc-mdio") ||
298 of_device_is_compatible(np, "ucc_geth_phy"))
299 map -= offsetof(struct fsl_pq_mdio, miimcfg);
300 regs = map;
302 new_bus->priv = (void __force *)regs;
304 new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
306 if (NULL == new_bus->irq) {
307 err = -ENOMEM;
308 goto err_unmap_regs;
311 new_bus->parent = &ofdev->dev;
312 dev_set_drvdata(&ofdev->dev, new_bus);
314 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
315 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
316 of_device_is_compatible(np, "fsl,etsec2-mdio") ||
317 of_device_is_compatible(np, "fsl,etsec2-tbi") ||
318 of_device_is_compatible(np, "gianfar")) {
319 #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
320 tbipa = get_gfar_tbipa(regs, np);
321 if (!tbipa) {
322 err = -EINVAL;
323 goto err_free_irqs;
325 #else
326 err = -ENODEV;
327 goto err_free_irqs;
328 #endif
329 } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
330 of_device_is_compatible(np, "ucc_geth_phy")) {
331 #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
332 u32 id;
333 static u32 mii_mng_master;
335 tbipa = &regs->utbipar;
337 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
338 goto err_free_irqs;
340 if (!mii_mng_master) {
341 mii_mng_master = id;
342 ucc_set_qe_mux_mii_mng(id - 1);
344 #else
345 err = -ENODEV;
346 goto err_free_irqs;
347 #endif
348 } else {
349 err = -ENODEV;
350 goto err_free_irqs;
353 for_each_child_of_node(np, tbi) {
354 if (!strncmp(tbi->type, "tbi-phy", 8))
355 break;
358 if (tbi) {
359 const u32 *prop = of_get_property(tbi, "reg", NULL);
361 if (prop)
362 tbiaddr = *prop;
365 if (tbiaddr == -1) {
366 out_be32(tbipa, 0);
368 tbiaddr = fsl_pq_mdio_find_free(new_bus);
372 * We define TBIPA at 0 to be illegal, opting to fail for boards that
373 * have PHYs at 1-31, rather than change tbipa and rescan.
375 if (tbiaddr == 0) {
376 err = -EBUSY;
378 goto err_free_irqs;
381 out_be32(tbipa, tbiaddr);
383 err = of_mdiobus_register(new_bus, np);
384 if (err) {
385 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
386 new_bus->name);
387 goto err_free_irqs;
390 return 0;
392 err_free_irqs:
393 kfree(new_bus->irq);
394 err_unmap_regs:
395 iounmap(regs);
396 err_free_bus:
397 kfree(new_bus);
399 return err;
403 static int fsl_pq_mdio_remove(struct of_device *ofdev)
405 struct device *device = &ofdev->dev;
406 struct mii_bus *bus = dev_get_drvdata(device);
408 mdiobus_unregister(bus);
410 dev_set_drvdata(device, NULL);
412 iounmap(fsl_pq_mdio_get_regs(bus));
413 bus->priv = NULL;
414 mdiobus_free(bus);
416 return 0;
419 static struct of_device_id fsl_pq_mdio_match[] = {
421 .type = "mdio",
422 .compatible = "ucc_geth_phy",
425 .type = "mdio",
426 .compatible = "gianfar",
429 .compatible = "fsl,ucc-mdio",
432 .compatible = "fsl,gianfar-tbi",
435 .compatible = "fsl,gianfar-mdio",
438 .compatible = "fsl,etsec2-tbi",
441 .compatible = "fsl,etsec2-mdio",
445 MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
447 static struct of_platform_driver fsl_pq_mdio_driver = {
448 .name = "fsl-pq_mdio",
449 .probe = fsl_pq_mdio_probe,
450 .remove = fsl_pq_mdio_remove,
451 .match_table = fsl_pq_mdio_match,
454 int __init fsl_pq_mdio_init(void)
456 return of_register_platform_driver(&fsl_pq_mdio_driver);
458 module_init(fsl_pq_mdio_init);
460 void fsl_pq_mdio_exit(void)
462 of_unregister_platform_driver(&fsl_pq_mdio_driver);
464 module_exit(fsl_pq_mdio_exit);
465 MODULE_LICENSE("GPL");