blob: fb8c8d9dcf29249a939dc2e20a8bf3c118c405b1 [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;
Anton Vorontsov2951d642009-11-04 12:52:56 +0000265 void __iomem *map;
Andy Fleming1577ece2009-02-04 16:42:12 -0800266 u32 __iomem *tbipa;
267 struct mii_bus *new_bus;
268 int tbiaddr = -1;
Anton Vorontsov2951d642009-11-04 12:52:56 +0000269 u64 addr = 0, size = 0;
Andy Fleming1577ece2009-02-04 16:42:12 -0800270 int err = 0;
271
272 new_bus = mdiobus_alloc();
273 if (NULL == new_bus)
274 return -ENOMEM;
275
276 new_bus->name = "Freescale PowerQUICC MII Bus",
277 new_bus->read = &fsl_pq_mdio_read,
278 new_bus->write = &fsl_pq_mdio_write,
279 new_bus->reset = &fsl_pq_mdio_reset,
280 fsl_pq_mdio_bus_name(new_bus->id, np);
281
282 /* Set the PHY base address */
Anton Vorontsov2951d642009-11-04 12:52:56 +0000283 addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
284 map = ioremap(addr, size);
285 if (!map) {
Andy Fleming1577ece2009-02-04 16:42:12 -0800286 err = -ENOMEM;
287 goto err_free_bus;
288 }
289
Anton Vorontsov2951d642009-11-04 12:52:56 +0000290 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
291 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
292 of_device_is_compatible(np, "fsl,ucc-mdio") ||
293 of_device_is_compatible(np, "ucc_geth_phy"))
294 map -= offsetof(struct fsl_pq_mdio, miimcfg);
295 regs = map;
296
Andy Fleming1577ece2009-02-04 16:42:12 -0800297 new_bus->priv = (void __force *)regs;
298
Grant Likely324931b2009-04-25 12:53:07 +0000299 new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
Andy Fleming1577ece2009-02-04 16:42:12 -0800300
301 if (NULL == new_bus->irq) {
302 err = -ENOMEM;
303 goto err_unmap_regs;
304 }
305
306 new_bus->parent = &ofdev->dev;
307 dev_set_drvdata(&ofdev->dev, new_bus);
308
309 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
Anton Vorontsov30196842009-03-21 13:30:05 -0700310 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000311 of_device_is_compatible(np, "fsl,etsec2-mdio") ||
312 of_device_is_compatible(np, "fsl,etsec2-tbi") ||
Andy Fleming1577ece2009-02-04 16:42:12 -0800313 of_device_is_compatible(np, "gianfar")) {
Ionut Nicue2a61fa2009-06-24 22:23:39 +0000314#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000315 tbipa = get_gfar_tbipa(regs, np);
316 if (!tbipa) {
317 err = -EINVAL;
318 goto err_free_irqs;
319 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800320#else
321 err = -ENODEV;
322 goto err_free_irqs;
323#endif
324 } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
325 of_device_is_compatible(np, "ucc_geth_phy")) {
Ionut Nicue2a61fa2009-06-24 22:23:39 +0000326#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
Andy Fleming1577ece2009-02-04 16:42:12 -0800327 u32 id;
Haiying Wangfbcc0e22009-06-02 04:04:14 +0000328 static u32 mii_mng_master;
Andy Fleming1577ece2009-02-04 16:42:12 -0800329
330 tbipa = &regs->utbipar;
331
332 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
333 goto err_free_irqs;
334
Haiying Wangfbcc0e22009-06-02 04:04:14 +0000335 if (!mii_mng_master) {
336 mii_mng_master = id;
337 ucc_set_qe_mux_mii_mng(id - 1);
338 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800339#else
340 err = -ENODEV;
341 goto err_free_irqs;
342#endif
343 } else {
344 err = -ENODEV;
345 goto err_free_irqs;
346 }
347
348 for_each_child_of_node(np, tbi) {
349 if (!strncmp(tbi->type, "tbi-phy", 8))
350 break;
351 }
352
353 if (tbi) {
354 const u32 *prop = of_get_property(tbi, "reg", NULL);
355
356 if (prop)
357 tbiaddr = *prop;
358 }
359
360 if (tbiaddr == -1) {
361 out_be32(tbipa, 0);
362
363 tbiaddr = fsl_pq_mdio_find_free(new_bus);
364 }
365
366 /*
367 * We define TBIPA at 0 to be illegal, opting to fail for boards that
368 * have PHYs at 1-31, rather than change tbipa and rescan.
369 */
370 if (tbiaddr == 0) {
371 err = -EBUSY;
372
373 goto err_free_irqs;
374 }
375
376 out_be32(tbipa, tbiaddr);
377
Grant Likely324931b2009-04-25 12:53:07 +0000378 err = of_mdiobus_register(new_bus, np);
Andy Fleming1577ece2009-02-04 16:42:12 -0800379 if (err) {
380 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
381 new_bus->name);
382 goto err_free_irqs;
383 }
384
385 return 0;
386
387err_free_irqs:
388 kfree(new_bus->irq);
389err_unmap_regs:
390 iounmap(regs);
391err_free_bus:
392 kfree(new_bus);
393
394 return err;
395}
396
397
398static int fsl_pq_mdio_remove(struct of_device *ofdev)
399{
400 struct device *device = &ofdev->dev;
401 struct mii_bus *bus = dev_get_drvdata(device);
402
403 mdiobus_unregister(bus);
404
405 dev_set_drvdata(device, NULL);
406
407 iounmap((void __iomem *)bus->priv);
408 bus->priv = NULL;
409 mdiobus_free(bus);
410
411 return 0;
412}
413
414static struct of_device_id fsl_pq_mdio_match[] = {
415 {
416 .type = "mdio",
417 .compatible = "ucc_geth_phy",
418 },
419 {
420 .type = "mdio",
421 .compatible = "gianfar",
422 },
423 {
424 .compatible = "fsl,ucc-mdio",
425 },
426 {
427 .compatible = "fsl,gianfar-tbi",
428 },
429 {
430 .compatible = "fsl,gianfar-mdio",
431 },
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000432 {
433 .compatible = "fsl,etsec2-tbi",
434 },
435 {
436 .compatible = "fsl,etsec2-mdio",
437 },
Andy Fleming1577ece2009-02-04 16:42:12 -0800438 {},
439};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700440MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
Andy Fleming1577ece2009-02-04 16:42:12 -0800441
442static struct of_platform_driver fsl_pq_mdio_driver = {
443 .name = "fsl-pq_mdio",
444 .probe = fsl_pq_mdio_probe,
445 .remove = fsl_pq_mdio_remove,
446 .match_table = fsl_pq_mdio_match,
447};
448
449int __init fsl_pq_mdio_init(void)
450{
451 return of_register_platform_driver(&fsl_pq_mdio_driver);
452}
Grant Likely434e7b02009-04-25 12:53:44 +0000453module_init(fsl_pq_mdio_init);
Andy Fleming1577ece2009-02-04 16:42:12 -0800454
455void fsl_pq_mdio_exit(void)
456{
457 of_unregister_platform_driver(&fsl_pq_mdio_driver);
458}
Andy Fleming1577ece2009-02-04 16:42:12 -0800459module_exit(fsl_pq_mdio_exit);