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>
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>
38 #include <linux/of_mdio.h>
39 #include <linux/of_platform.h>
43 #include <asm/uaccess.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(®s
->miimadd
, (mii_id
<< 8) | regnum
);
64 /* Write out the value we want */
65 out_be32(®s
->miimcon
, value
);
67 /* Wait for the transaction to finish */
68 while (in_be32(®s
->miimind
) & MIIMIND_BUSY
)
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
)
89 /* Set the PHY address and the register address we want to read */
90 out_be32(®s
->miimadd
, (mii_id
<< 8) | regnum
);
92 /* Clear miimcom, and then initiate a read */
93 out_be32(®s
->miimcom
, 0);
94 out_be32(®s
->miimcom
, MII_READ_COMMAND
);
96 /* Wait for the transaction to finish */
97 while (in_be32(®s
->miimind
) & (MIIMIND_NOTVALID
| MIIMIND_BUSY
))
100 /* Grab the value of the register from miimstat */
101 value
= in_be32(®s
->miimstat
);
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(®s
->miimcfg
, MIIMCFG_RESET
);
146 /* Setup the MII Mgmt clock speed */
147 out_be32(®s
->miimcfg
, MIIMCFG_INIT_VALUE
);
149 /* Wait until the bus is free */
150 while ((in_be32(®s
->miimind
) & MIIMIND_BUSY
) && timeout
--)
153 mutex_unlock(&bus
->mdio_lock
);
156 printk(KERN_ERR
"%s: The MII Bus is stuck!\n",
164 void fsl_pq_mdio_bus_name(char *name
, struct device_node
*np
)
167 u64 taddr
= OF_BAD_ADDR
;
169 addr
= of_get_address(np
, 0, NULL
, NULL
);
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
)
183 for (i
= PHY_MAX_ADDR
; i
> 0; i
--) {
186 if (get_phy_id(new_bus
, i
, &phy_id
))
189 if (phy_id
== 0xffffffff)
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
;
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
;
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
;
231 for_each_compatible_node(np
, NULL
, "ucc_geth") {
232 struct resource tempres
;
234 err
= of_address_to_resource(np
, 0, &tempres
);
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 */
243 id
= of_get_property(np
, "cell-index", NULL
);
245 id
= of_get_property(np
, "device-id", NULL
);
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
;
272 struct mii_bus
*new_bus
;
274 u64 addr
= 0, size
= 0;
277 new_bus
= mdiobus_alloc();
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
);
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
);
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
) {
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
);
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)
333 static u32 mii_mng_master
;
335 tbipa
= ®s
->utbipar
;
337 if ((err
= get_ucc_id_for_range(addr
, addr
+ size
, &id
)))
340 if (!mii_mng_master
) {
342 ucc_set_qe_mux_mii_mng(id
- 1);
353 for_each_child_of_node(np
, tbi
) {
354 if (!strncmp(tbi
->type
, "tbi-phy", 8))
359 const u32
*prop
= of_get_property(tbi
, "reg", NULL
);
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.
381 out_be32(tbipa
, tbiaddr
);
383 err
= of_mdiobus_register(new_bus
, np
);
385 printk (KERN_ERR
"%s: Cannot register as MDIO bus\n",
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
));
419 static struct of_device_id fsl_pq_mdio_match
[] = {
422 .compatible
= "ucc_geth_phy",
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");