blob: 9527b28d70d1976374b812d9a37fbfaaf1c79aaf [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 Likely22ae7822010-07-29 11:49:01 -060038#include <linux/of_address.h>
Grant Likely324931b2009-04-25 12:53:07 +000039#include <linux/of_mdio.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080040#include <linux/of_platform.h>
41
42#include <asm/io.h>
43#include <asm/irq.h>
44#include <asm/uaccess.h>
45#include <asm/ucc.h>
46
47#include "gianfar.h"
48#include "fsl_pq_mdio.h"
49
Timur Tabi59399c52012-07-09 16:57:36 -050050/* Number of microseconds to wait for an MII register to respond */
51#define MII_TIMEOUT 1000
52
Anton Vorontsovb3319b12009-12-30 08:23:34 +000053struct fsl_pq_mdio_priv {
54 void __iomem *map;
55 struct fsl_pq_mdio __iomem *regs;
56};
57
Andy Fleming1577ece2009-02-04 16:42:12 -080058/*
59 * Write value to the PHY at mii_id at register regnum,
60 * on the bus attached to the local interface, which may be different from the
61 * generic mdio bus (tied to a single interface), waiting until the write is
62 * done before returning. This is helpful in programming interfaces like
63 * the TBI which control interfaces like onchip SERDES and are always tied to
64 * the local mdio pins, which may not be the same as system mdio bus, used for
65 * controlling the external PHYs, for example.
66 */
67int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
68 int regnum, u16 value)
69{
Timur Tabi59399c52012-07-09 16:57:36 -050070 u32 status;
71
Andy Fleming1577ece2009-02-04 16:42:12 -080072 /* Set the PHY address and the register address we want to write */
73 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
74
75 /* Write out the value we want */
76 out_be32(&regs->miimcon, value);
77
78 /* Wait for the transaction to finish */
Timur Tabi59399c52012-07-09 16:57:36 -050079 status = spin_event_timeout(!(in_be32(&regs->miimind) & MIIMIND_BUSY),
80 MII_TIMEOUT, 0);
Andy Fleming1577ece2009-02-04 16:42:12 -080081
Timur Tabi59399c52012-07-09 16:57:36 -050082 return status ? 0 : -ETIMEDOUT;
Andy Fleming1577ece2009-02-04 16:42:12 -080083}
84
85/*
86 * Read the bus for PHY at addr mii_id, register regnum, and
87 * return the value. Clears miimcom first. All PHY operation
88 * done on the bus attached to the local interface,
89 * which may be different from the generic mdio bus
90 * This is helpful in programming interfaces like
91 * the TBI which, in turn, control interfaces like onchip SERDES
92 * and are always tied to the local mdio pins, which may not be the
93 * same as system mdio bus, used for controlling the external PHYs, for eg.
94 */
95int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
96 int mii_id, int regnum)
97{
98 u16 value;
Timur Tabi59399c52012-07-09 16:57:36 -050099 u32 status;
Andy Fleming1577ece2009-02-04 16:42:12 -0800100
101 /* Set the PHY address and the register address we want to read */
102 out_be32(&regs->miimadd, (mii_id << 8) | regnum);
103
104 /* Clear miimcom, and then initiate a read */
105 out_be32(&regs->miimcom, 0);
106 out_be32(&regs->miimcom, MII_READ_COMMAND);
107
Timur Tabi59399c52012-07-09 16:57:36 -0500108 /* Wait for the transaction to finish, normally less than 100us */
109 status = spin_event_timeout(!(in_be32(&regs->miimind) &
110 (MIIMIND_NOTVALID | MIIMIND_BUSY)),
111 MII_TIMEOUT, 0);
112 if (!status)
113 return -ETIMEDOUT;
Andy Fleming1577ece2009-02-04 16:42:12 -0800114
115 /* Grab the value of the register from miimstat */
116 value = in_be32(&regs->miimstat);
117
118 return value;
119}
120
Anton Vorontsov6748f602009-11-04 12:52:57 +0000121static struct fsl_pq_mdio __iomem *fsl_pq_mdio_get_regs(struct mii_bus *bus)
122{
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000123 struct fsl_pq_mdio_priv *priv = bus->priv;
124
125 return priv->regs;
Anton Vorontsov6748f602009-11-04 12:52:57 +0000126}
127
Andy Fleming1577ece2009-02-04 16:42:12 -0800128/*
129 * Write value to the PHY at mii_id at register regnum,
130 * on the bus, waiting until the write is done before returning.
131 */
132int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
133{
Anton Vorontsov6748f602009-11-04 12:52:57 +0000134 struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
Andy Fleming1577ece2009-02-04 16:42:12 -0800135
136 /* Write to the local MII regs */
Eric Dumazet807540b2010-09-23 05:40:09 +0000137 return fsl_pq_local_mdio_write(regs, mii_id, regnum, value);
Andy Fleming1577ece2009-02-04 16:42:12 -0800138}
139
140/*
141 * Read the bus for PHY at addr mii_id, register regnum, and
142 * return the value. Clears miimcom first.
143 */
144int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
145{
Anton Vorontsov6748f602009-11-04 12:52:57 +0000146 struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
Andy Fleming1577ece2009-02-04 16:42:12 -0800147
148 /* Read the local MII regs */
Eric Dumazet807540b2010-09-23 05:40:09 +0000149 return fsl_pq_local_mdio_read(regs, mii_id, regnum);
Andy Fleming1577ece2009-02-04 16:42:12 -0800150}
151
152/* Reset the MIIM registers, and wait for the bus to free */
153static int fsl_pq_mdio_reset(struct mii_bus *bus)
154{
Anton Vorontsov6748f602009-11-04 12:52:57 +0000155 struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
Timur Tabi59399c52012-07-09 16:57:36 -0500156 u32 status;
Andy Fleming1577ece2009-02-04 16:42:12 -0800157
158 mutex_lock(&bus->mdio_lock);
159
160 /* Reset the management interface */
161 out_be32(&regs->miimcfg, MIIMCFG_RESET);
162
163 /* Setup the MII Mgmt clock speed */
164 out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
165
166 /* Wait until the bus is free */
Timur Tabi59399c52012-07-09 16:57:36 -0500167 status = spin_event_timeout(!(in_be32(&regs->miimind) & MIIMIND_BUSY),
168 MII_TIMEOUT, 0);
Andy Fleming1577ece2009-02-04 16:42:12 -0800169
170 mutex_unlock(&bus->mdio_lock);
171
Timur Tabi59399c52012-07-09 16:57:36 -0500172 if (!status) {
Andy Fleming1577ece2009-02-04 16:42:12 -0800173 printk(KERN_ERR "%s: The MII Bus is stuck!\n",
174 bus->name);
175 return -EBUSY;
176 }
177
178 return 0;
179}
180
Andy Fleming1577ece2009-02-04 16:42:12 -0800181void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
182{
Anton Vorontsov18f27382009-03-19 06:48:08 +0000183 const u32 *addr;
184 u64 taddr = OF_BAD_ADDR;
Andy Fleming1577ece2009-02-04 16:42:12 -0800185
Anton Vorontsov18f27382009-03-19 06:48:08 +0000186 addr = of_get_address(np, 0, NULL, NULL);
187 if (addr)
188 taddr = of_translate_address(np, addr);
Andy Fleming1577ece2009-02-04 16:42:12 -0800189
Anton Vorontsov18f27382009-03-19 06:48:08 +0000190 snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
191 (unsigned long long)taddr);
Andy Fleming1577ece2009-02-04 16:42:12 -0800192}
Segher Boessenkoolb6bc9782009-04-02 13:57:30 -0700193EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
Andy Fleming1577ece2009-02-04 16:42:12 -0800194
Andy Fleming1577ece2009-02-04 16:42:12 -0800195
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000196static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
Andy Fleming1577ece2009-02-04 16:42:12 -0800197{
Andy Fleming952c5ca2011-11-11 05:10:39 +0000198#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
Andy Fleming1577ece2009-02-04 16:42:12 -0800199 struct gfar __iomem *enet_regs;
200
201 /*
202 * This is mildly evil, but so is our hardware for doing this.
203 * Also, we have to cast back to struct gfar because of
204 * definition weirdness done in gianfar.h.
205 */
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000206 if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
207 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
208 of_device_is_compatible(np, "gianfar")) {
209 enet_regs = (struct gfar __iomem *)regs;
210 return &enet_regs->tbipa;
211 } else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
212 of_device_is_compatible(np, "fsl,etsec2-tbi")) {
Anton Vorontsov3b1fd3e2010-04-23 07:12:35 +0000213 return of_iomap(np, 1);
Andy Fleming952c5ca2011-11-11 05:10:39 +0000214 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800215#endif
Andy Fleming952c5ca2011-11-11 05:10:39 +0000216 return NULL;
217}
Andy Fleming1577ece2009-02-04 16:42:12 -0800218
219
Andy Fleming1577ece2009-02-04 16:42:12 -0800220static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
221{
Andy Fleming952c5ca2011-11-11 05:10:39 +0000222#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
Andy Fleming1577ece2009-02-04 16:42:12 -0800223 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;
Andy Fleming952c5ca2011-11-11 05:10:39 +0000255#else
256 return -ENODEV;
Andy Fleming1577ece2009-02-04 16:42:12 -0800257#endif
Andy Fleming952c5ca2011-11-11 05:10:39 +0000258}
Andy Fleming1577ece2009-02-04 16:42:12 -0800259
Grant Likely74888762011-02-22 21:05:51 -0700260static int fsl_pq_mdio_probe(struct platform_device *ofdev)
Andy Fleming1577ece2009-02-04 16:42:12 -0800261{
Grant Likely61c7a082010-04-13 16:12:29 -0700262 struct device_node *np = ofdev->dev.of_node;
Andy Fleming1577ece2009-02-04 16:42:12 -0800263 struct device_node *tbi;
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000264 struct fsl_pq_mdio_priv *priv;
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000265 struct fsl_pq_mdio __iomem *regs = NULL;
Anton Vorontsov2951d642009-11-04 12:52:56 +0000266 void __iomem *map;
Andy Fleming1577ece2009-02-04 16:42:12 -0800267 u32 __iomem *tbipa;
268 struct mii_bus *new_bus;
269 int tbiaddr = -1;
Anton Vorontsov3b1fd3e2010-04-23 07:12:35 +0000270 const u32 *addrp;
Anton Vorontsov2951d642009-11-04 12:52:56 +0000271 u64 addr = 0, size = 0;
Anton Vorontsov08d18f32010-05-14 04:27:30 +0000272 int err;
Andy Fleming1577ece2009-02-04 16:42:12 -0800273
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000274 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
275 if (!priv)
276 return -ENOMEM;
277
Andy Fleming1577ece2009-02-04 16:42:12 -0800278 new_bus = mdiobus_alloc();
Anton Vorontsov08d18f32010-05-14 04:27:30 +0000279 if (!new_bus) {
280 err = -ENOMEM;
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000281 goto err_free_priv;
Anton Vorontsov08d18f32010-05-14 04:27:30 +0000282 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800283
284 new_bus->name = "Freescale PowerQUICC MII Bus",
285 new_bus->read = &fsl_pq_mdio_read,
286 new_bus->write = &fsl_pq_mdio_write,
287 new_bus->reset = &fsl_pq_mdio_reset,
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000288 new_bus->priv = priv;
Andy Fleming1577ece2009-02-04 16:42:12 -0800289 fsl_pq_mdio_bus_name(new_bus->id, np);
290
Anton Vorontsov3b1fd3e2010-04-23 07:12:35 +0000291 addrp = of_get_address(np, 0, &size, NULL);
292 if (!addrp) {
293 err = -EINVAL;
294 goto err_free_bus;
295 }
296
Andy Fleming1577ece2009-02-04 16:42:12 -0800297 /* Set the PHY base address */
Anton Vorontsov3b1fd3e2010-04-23 07:12:35 +0000298 addr = of_translate_address(np, addrp);
299 if (addr == OF_BAD_ADDR) {
300 err = -EINVAL;
301 goto err_free_bus;
302 }
303
Anton Vorontsov2951d642009-11-04 12:52:56 +0000304 map = ioremap(addr, size);
305 if (!map) {
Andy Fleming1577ece2009-02-04 16:42:12 -0800306 err = -ENOMEM;
307 goto err_free_bus;
308 }
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000309 priv->map = map;
Andy Fleming1577ece2009-02-04 16:42:12 -0800310
Anton Vorontsov2951d642009-11-04 12:52:56 +0000311 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
312 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
313 of_device_is_compatible(np, "fsl,ucc-mdio") ||
314 of_device_is_compatible(np, "ucc_geth_phy"))
315 map -= offsetof(struct fsl_pq_mdio, miimcfg);
316 regs = map;
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000317 priv->regs = regs;
Andy Fleming1577ece2009-02-04 16:42:12 -0800318
Grant Likely324931b2009-04-25 12:53:07 +0000319 new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
Andy Fleming1577ece2009-02-04 16:42:12 -0800320
321 if (NULL == new_bus->irq) {
322 err = -ENOMEM;
323 goto err_unmap_regs;
324 }
325
326 new_bus->parent = &ofdev->dev;
327 dev_set_drvdata(&ofdev->dev, new_bus);
328
329 if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
Anton Vorontsov30196842009-03-21 13:30:05 -0700330 of_device_is_compatible(np, "fsl,gianfar-tbi") ||
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000331 of_device_is_compatible(np, "fsl,etsec2-mdio") ||
332 of_device_is_compatible(np, "fsl,etsec2-tbi") ||
Andy Fleming1577ece2009-02-04 16:42:12 -0800333 of_device_is_compatible(np, "gianfar")) {
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000334 tbipa = get_gfar_tbipa(regs, np);
335 if (!tbipa) {
336 err = -EINVAL;
337 goto err_free_irqs;
338 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800339 } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
340 of_device_is_compatible(np, "ucc_geth_phy")) {
Andy Fleming1577ece2009-02-04 16:42:12 -0800341 u32 id;
Haiying Wangfbcc0e22009-06-02 04:04:14 +0000342 static u32 mii_mng_master;
Andy Fleming1577ece2009-02-04 16:42:12 -0800343
344 tbipa = &regs->utbipar;
345
346 if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
347 goto err_free_irqs;
348
Haiying Wangfbcc0e22009-06-02 04:04:14 +0000349 if (!mii_mng_master) {
350 mii_mng_master = id;
351 ucc_set_qe_mux_mii_mng(id - 1);
352 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800353 } else {
354 err = -ENODEV;
355 goto err_free_irqs;
356 }
357
358 for_each_child_of_node(np, tbi) {
359 if (!strncmp(tbi->type, "tbi-phy", 8))
360 break;
361 }
362
363 if (tbi) {
364 const u32 *prop = of_get_property(tbi, "reg", NULL);
365
366 if (prop)
367 tbiaddr = *prop;
Baruch Siachc3e072f2011-11-14 08:21:30 +0200368
Kenth Eriksson464b57d2012-03-27 22:05:54 +0000369 if (tbiaddr == -1) {
370 err = -EBUSY;
371 goto err_free_irqs;
372 } else {
373 out_be32(tbipa, tbiaddr);
374 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800375 }
376
Grant Likely324931b2009-04-25 12:53:07 +0000377 err = of_mdiobus_register(new_bus, np);
Andy Fleming1577ece2009-02-04 16:42:12 -0800378 if (err) {
379 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
380 new_bus->name);
381 goto err_free_irqs;
382 }
383
384 return 0;
385
386err_free_irqs:
387 kfree(new_bus->irq);
388err_unmap_regs:
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000389 iounmap(priv->map);
Andy Fleming1577ece2009-02-04 16:42:12 -0800390err_free_bus:
391 kfree(new_bus);
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000392err_free_priv:
393 kfree(priv);
Andy Fleming1577ece2009-02-04 16:42:12 -0800394 return err;
395}
396
397
Grant Likely2dc11582010-08-06 09:25:50 -0600398static int fsl_pq_mdio_remove(struct platform_device *ofdev)
Andy Fleming1577ece2009-02-04 16:42:12 -0800399{
400 struct device *device = &ofdev->dev;
401 struct mii_bus *bus = dev_get_drvdata(device);
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000402 struct fsl_pq_mdio_priv *priv = bus->priv;
Andy Fleming1577ece2009-02-04 16:42:12 -0800403
404 mdiobus_unregister(bus);
405
406 dev_set_drvdata(device, NULL);
407
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000408 iounmap(priv->map);
Andy Fleming1577ece2009-02-04 16:42:12 -0800409 bus->priv = NULL;
410 mdiobus_free(bus);
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000411 kfree(priv);
Andy Fleming1577ece2009-02-04 16:42:12 -0800412
413 return 0;
414}
415
416static struct of_device_id fsl_pq_mdio_match[] = {
417 {
418 .type = "mdio",
419 .compatible = "ucc_geth_phy",
420 },
421 {
422 .type = "mdio",
423 .compatible = "gianfar",
424 },
425 {
426 .compatible = "fsl,ucc-mdio",
427 },
428 {
429 .compatible = "fsl,gianfar-tbi",
430 },
431 {
432 .compatible = "fsl,gianfar-mdio",
433 },
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000434 {
435 .compatible = "fsl,etsec2-tbi",
436 },
437 {
438 .compatible = "fsl,etsec2-mdio",
439 },
Andy Fleming1577ece2009-02-04 16:42:12 -0800440 {},
441};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700442MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
Andy Fleming1577ece2009-02-04 16:42:12 -0800443
Grant Likely74888762011-02-22 21:05:51 -0700444static struct platform_driver fsl_pq_mdio_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700445 .driver = {
446 .name = "fsl-pq_mdio",
447 .owner = THIS_MODULE,
448 .of_match_table = fsl_pq_mdio_match,
449 },
Andy Fleming1577ece2009-02-04 16:42:12 -0800450 .probe = fsl_pq_mdio_probe,
451 .remove = fsl_pq_mdio_remove,
Andy Fleming1577ece2009-02-04 16:42:12 -0800452};
453
Axel Lindb62f682011-11-27 16:44:17 +0000454module_platform_driver(fsl_pq_mdio_driver);
Andy Fleming1577ece2009-02-04 16:42:12 -0800455
Sebastian Siewior26062892009-11-06 08:50:28 +0000456MODULE_LICENSE("GPL");