blob: 4065b7c01ecb58b8da4ebb38cb415a2402800ba0 [file] [log] [blame]
Andy Fleming1577ece2009-02-04 16:42:12 -08001/*
2 * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
3 * Provides Bus interface for MIIM regs
4 *
5 * Author: Andy Fleming <afleming@freescale.com>
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +00006 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Andy Fleming1577ece2009-02-04 16:42:12 -08007 *
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +00008 * Copyright 2002-2004, 2008-2009 Freescale Semiconductor, Inc.
Andy Fleming1577ece2009-02-04 16:42:12 -08009 *
10 * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
11 *
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.
16 *
17 */
18
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>
Grant Likely324931b2009-04-25 12:53:07 +000038#include <linux/of_mdio.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080039#include <linux/of_platform.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/uaccess.h>
44#include <asm/ucc.h>
45
46#include "gianfar.h"
47#include "fsl_pq_mdio.h"
48
49/*
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.
57 */
58int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
59 int regnum, u16 value)
60{
61 /* Set the PHY address and the register address we want to write */
62 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
63
64 /* Write out the value we want */
65 out_be32(&regs->miimcon, value);
66
67 /* Wait for the transaction to finish */
68 while (in_be32(&regs->miimind) & MIIMIND_BUSY)
69 cpu_relax();
70
71 return 0;
72}
73
74/*
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.
83 */
84int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
85 int mii_id, int regnum)
86{
87 u16 value;
88
89 /* Set the PHY address and the register address we want to read */
90 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
91
92 /* Clear miimcom, and then initiate a read */
93 out_be32(&regs->miimcom, 0);
94 out_be32(&regs->miimcom, MII_READ_COMMAND);
95
96 /* Wait for the transaction to finish */
97 while (in_be32(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
98 cpu_relax();
99
100 /* Grab the value of the register from miimstat */
101 value = in_be32(&regs->miimstat);
102
103 return value;
104}
105
106/*
107 * Write value to the PHY at mii_id at register regnum,
108 * on the bus, waiting until the write is done before returning.
109 */
110int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
111{
112 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
113
114 /* Write to the local MII regs */
115 return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
116}
117
118/*
119 * Read the bus for PHY at addr mii_id, register regnum, and
120 * return the value. Clears miimcom first.
121 */
122int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
123{
124 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
125
126 /* Read the local MII regs */
127 return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
128}
129
130/* Reset the MIIM registers, and wait for the bus to free */
131static int fsl_pq_mdio_reset(struct mii_bus *bus)
132{
133 struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
David S. Miller508827f2009-03-05 02:06:47 -0800134 int timeout = PHY_INIT_TIMEOUT;
Andy Fleming1577ece2009-02-04 16:42:12 -0800135
136 mutex_lock(&bus->mdio_lock);
137
138 /* Reset the management interface */
139 out_be32(&regs->miimcfg, MIIMCFG_RESET);
140
141 /* Setup the MII Mgmt clock speed */
142 out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
143
144 /* Wait until the bus is free */
145 while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
146 cpu_relax();
147
148 mutex_unlock(&bus->mdio_lock);
149
David S. Miller508827f2009-03-05 02:06:47 -0800150 if (timeout < 0) {
Andy Fleming1577ece2009-02-04 16:42:12 -0800151 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
152 bus->name);
153 return -EBUSY;
154 }
155
156 return 0;
157}
158
Andy Fleming1577ece2009-02-04 16:42:12 -0800159void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
160{
Anton Vorontsov18f27382009-03-19 06:48:08 +0000161 const u32 *addr;
162 u64 taddr = OF_BAD_ADDR;
Andy Fleming1577ece2009-02-04 16:42:12 -0800163
Anton Vorontsov18f27382009-03-19 06:48:08 +0000164 addr = of_get_address(np, 0, NULL, NULL);
165 if (addr)
166 taddr = of_translate_address(np, addr);
Andy Fleming1577ece2009-02-04 16:42:12 -0800167
Anton Vorontsov18f27382009-03-19 06:48:08 +0000168 snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
169 (unsigned long long)taddr);
Andy Fleming1577ece2009-02-04 16:42:12 -0800170}
Segher Boessenkoolb6bc9782009-04-02 13:57:30 -0700171EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
Andy Fleming1577ece2009-02-04 16:42:12 -0800172
173/* Scan the bus in reverse, looking for an empty spot */
174static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
175{
176 int i;
177
178 for (i = PHY_MAX_ADDR; i > 0; i--) {
179 u32 phy_id;
180
181 if (get_phy_id(new_bus, i, &phy_id))
182 return -1;
183
184 if (phy_id == 0xffffffff)
185 break;
186 }
187
188 return i;
189}
190
191
Ionut Nicue2a61fa2009-06-24 22:23:39 +0000192#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000193static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
Andy Fleming1577ece2009-02-04 16:42:12 -0800194{
195 struct gfar __iomem *enet_regs;
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000196 u32 __iomem *ioremap_tbipa;
197 u64 addr, size;
Andy Fleming1577ece2009-02-04 16:42:12 -0800198
199 /*
200 * This is mildly evil, but so is our hardware for doing this.
201 * Also, we have to cast back to struct gfar because of
202 * definition weirdness done in gianfar.h.
203 */
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000204 if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
205 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
206 of_device_is_compatible(np, "gianfar")) {
207 enet_regs = (struct gfar __iomem *)regs;
208 return &enet_regs->tbipa;
209 } else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
210 of_device_is_compatible(np, "fsl,etsec2-tbi")) {
211 addr = of_translate_address(np, of_get_address(np, 1, &size, NULL));
212 ioremap_tbipa = ioremap(addr, size);
213 return ioremap_tbipa;
214 } else
215 return NULL;
Andy Fleming1577ece2009-02-04 16:42:12 -0800216}
217#endif
218
219
Ionut Nicue2a61fa2009-06-24 22:23:39 +0000220#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
Andy Fleming1577ece2009-02-04 16:42:12 -0800221static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
222{
223 struct device_node *np = NULL;
224 int err = 0;
225
226 for_each_compatible_node(np, NULL, "ucc_geth") {
227 struct resource tempres;
228
229 err = of_address_to_resource(np, 0, &tempres);
230 if (err)
231 continue;
232
233 /* if our mdio regs fall within this UCC regs range */
234 if ((start >= tempres.start) && (end <= tempres.end)) {
235 /* Find the id of the UCC */
236 const u32 *id;
237
238 id = of_get_property(np, "cell-index", NULL);
239 if (!id) {
240 id = of_get_property(np, "device-id", NULL);
241 if (!id)
242 continue;
243 }
244
245 *ucc_id = *id;
246
247 return 0;
248 }
249 }
250
251 if (err)
252 return err;
253 else
254 return -EINVAL;
255}
256#endif
257
258
259static int fsl_pq_mdio_probe(struct of_device *ofdev,
260 const struct of_device_id *match)
261{
262 struct device_node *np = ofdev->node;
263 struct device_node *tbi;
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000264 struct fsl_pq_mdio __iomem *regs = NULL;
Andy Fleming1577ece2009-02-04 16:42:12 -0800265 u32 __iomem *tbipa;
266 struct mii_bus *new_bus;
267 int tbiaddr = -1;
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000268 u64 addr = 0, size = 0, ioremap_miimcfg = 0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800269 int err = 0;
270
271 new_bus = mdiobus_alloc();
272 if (NULL == new_bus)
273 return -ENOMEM;
274
275 new_bus->name = "Freescale PowerQUICC MII Bus",
276 new_bus->read = &fsl_pq_mdio_read,
277 new_bus->write = &fsl_pq_mdio_write,
278 new_bus->reset = &fsl_pq_mdio_reset,
279 fsl_pq_mdio_bus_name(new_bus->id, np);
280
281 /* Set the PHY base address */
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000282 if (of_device_is_compatible(np,"fsl,gianfar-mdio") ||
283 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
284 of_device_is_compatible(np, "fsl,ucc-mdio") ||
285 of_device_is_compatible(np,"ucc_geth_phy" )) {
286 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
287 ioremap_miimcfg = container_of(addr, struct fsl_pq_mdio, miimcfg);
288 regs = ioremap(ioremap_miimcfg, size +
289 offsetof(struct fsl_pq_mdio, miimcfg));
290 } else if (of_device_is_compatible(np,"fsl,etsec2-mdio") ||
291 of_device_is_compatible(np, "fsl,etsec2-tbi")) {
292 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
293 regs = ioremap(addr, size);
294 } else {
295 err = -EINVAL;
296 goto err_free_bus;
297 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800298
299 if (NULL == regs) {
300 err = -ENOMEM;
301 goto err_free_bus;
302 }
303
304 new_bus->priv = (void __force *)regs;
305
Grant Likely324931b2009-04-25 12:53:07 +0000306 new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
Andy Fleming1577ece2009-02-04 16:42:12 -0800307
308 if (NULL == new_bus->irq) {
309 err = -ENOMEM;
310 goto err_unmap_regs;
311 }
312
313 new_bus->parent = &ofdev->dev;
314 dev_set_drvdata(&ofdev->dev, new_bus);
315
316 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
Anton Vorontsov30196842009-03-21 13:30:05 -0700317 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000318 of_device_is_compatible(np, "fsl,etsec2-mdio") ||
319 of_device_is_compatible(np, "fsl,etsec2-tbi") ||
Andy Fleming1577ece2009-02-04 16:42:12 -0800320 of_device_is_compatible(np, "gianfar")) {
Ionut Nicue2a61fa2009-06-24 22:23:39 +0000321#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000322 tbipa = get_gfar_tbipa(regs, np);
323 if (!tbipa) {
324 err = -EINVAL;
325 goto err_free_irqs;
326 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800327#else
328 err = -ENODEV;
329 goto err_free_irqs;
330#endif
331 } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
332 of_device_is_compatible(np, "ucc_geth_phy")) {
Ionut Nicue2a61fa2009-06-24 22:23:39 +0000333#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
Andy Fleming1577ece2009-02-04 16:42:12 -0800334 u32 id;
Haiying Wangfbcc0e22009-06-02 04:04:14 +0000335 static u32 mii_mng_master;
Andy Fleming1577ece2009-02-04 16:42:12 -0800336
337 tbipa = &regs->utbipar;
338
339 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
340 goto err_free_irqs;
341
Haiying Wangfbcc0e22009-06-02 04:04:14 +0000342 if (!mii_mng_master) {
343 mii_mng_master = id;
344 ucc_set_qe_mux_mii_mng(id - 1);
345 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800346#else
347 err = -ENODEV;
348 goto err_free_irqs;
349#endif
350 } else {
351 err = -ENODEV;
352 goto err_free_irqs;
353 }
354
355 for_each_child_of_node(np, tbi) {
356 if (!strncmp(tbi->type, "tbi-phy", 8))
357 break;
358 }
359
360 if (tbi) {
361 const u32 *prop = of_get_property(tbi, "reg", NULL);
362
363 if (prop)
364 tbiaddr = *prop;
365 }
366
367 if (tbiaddr == -1) {
368 out_be32(tbipa, 0);
369
370 tbiaddr = fsl_pq_mdio_find_free(new_bus);
371 }
372
373 /*
374 * We define TBIPA at 0 to be illegal, opting to fail for boards that
375 * have PHYs at 1-31, rather than change tbipa and rescan.
376 */
377 if (tbiaddr == 0) {
378 err = -EBUSY;
379
380 goto err_free_irqs;
381 }
382
383 out_be32(tbipa, tbiaddr);
384
Grant Likely324931b2009-04-25 12:53:07 +0000385 err = of_mdiobus_register(new_bus, np);
Andy Fleming1577ece2009-02-04 16:42:12 -0800386 if (err) {
387 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
388 new_bus->name);
389 goto err_free_irqs;
390 }
391
392 return 0;
393
394err_free_irqs:
395 kfree(new_bus->irq);
396err_unmap_regs:
397 iounmap(regs);
398err_free_bus:
399 kfree(new_bus);
400
401 return err;
402}
403
404
405static int fsl_pq_mdio_remove(struct of_device *ofdev)
406{
407 struct device *device = &ofdev->dev;
408 struct mii_bus *bus = dev_get_drvdata(device);
409
410 mdiobus_unregister(bus);
411
412 dev_set_drvdata(device, NULL);
413
414 iounmap((void __iomem *)bus->priv);
415 bus->priv = NULL;
416 mdiobus_free(bus);
417
418 return 0;
419}
420
421static struct of_device_id fsl_pq_mdio_match[] = {
422 {
423 .type = "mdio",
424 .compatible = "ucc_geth_phy",
425 },
426 {
427 .type = "mdio",
428 .compatible = "gianfar",
429 },
430 {
431 .compatible = "fsl,ucc-mdio",
432 },
433 {
434 .compatible = "fsl,gianfar-tbi",
435 },
436 {
437 .compatible = "fsl,gianfar-mdio",
438 },
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000439 {
440 .compatible = "fsl,etsec2-tbi",
441 },
442 {
443 .compatible = "fsl,etsec2-mdio",
444 },
Andy Fleming1577ece2009-02-04 16:42:12 -0800445 {},
446};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700447MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
Andy Fleming1577ece2009-02-04 16:42:12 -0800448
449static struct of_platform_driver fsl_pq_mdio_driver = {
450 .name = "fsl-pq_mdio",
451 .probe = fsl_pq_mdio_probe,
452 .remove = fsl_pq_mdio_remove,
453 .match_table = fsl_pq_mdio_match,
454};
455
456int __init fsl_pq_mdio_init(void)
457{
458 return of_register_platform_driver(&fsl_pq_mdio_driver);
459}
Grant Likely434e7b02009-04-25 12:53:44 +0000460module_init(fsl_pq_mdio_init);
Andy Fleming1577ece2009-02-04 16:42:12 -0800461
462void fsl_pq_mdio_exit(void)
463{
464 of_unregister_platform_driver(&fsl_pq_mdio_driver);
465}
Andy Fleming1577ece2009-02-04 16:42:12 -0800466module_exit(fsl_pq_mdio_exit);