gianfar_mii.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * drivers/net/gianfar_mii.c
  3. *
  4. * Gianfar Ethernet Driver -- MIIM bus implementation
  5. * Provides Bus interface for MIIM regs
  6. *
  7. * Author: Andy Fleming
  8. * Maintainer: Kumar Gala
  9. *
  10. * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
  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. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/unistd.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/crc32.h>
  34. #include <linux/mii.h>
  35. #include <linux/phy.h>
  36. #include <linux/of.h>
  37. #include <linux/of_platform.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/uaccess.h>
  41. #include "gianfar.h"
  42. #include "gianfar_mii.h"
  43. /*
  44. * Write value to the PHY at mii_id at register regnum,
  45. * on the bus attached to the local interface, which may be different from the
  46. * generic mdio bus (tied to a single interface), waiting until the write is
  47. * done before returning. This is helpful in programming interfaces like
  48. * the TBI which control interfaces like onchip SERDES and are always tied to
  49. * the local mdio pins, which may not be the same as system mdio bus, used for
  50. * controlling the external PHYs, for example.
  51. */
  52. int gfar_local_mdio_write(struct gfar_mii __iomem *regs, int mii_id,
  53. int regnum, u16 value)
  54. {
  55. /* Set the PHY address and the register address we want to write */
  56. gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
  57. /* Write out the value we want */
  58. gfar_write(&regs->miimcon, value);
  59. /* Wait for the transaction to finish */
  60. while (gfar_read(&regs->miimind) & MIIMIND_BUSY)
  61. cpu_relax();
  62. return 0;
  63. }
  64. /*
  65. * Read the bus for PHY at addr mii_id, register regnum, and
  66. * return the value. Clears miimcom first. All PHY operation
  67. * done on the bus attached to the local interface,
  68. * which may be different from the generic mdio bus
  69. * This is helpful in programming interfaces like
  70. * the TBI which, inturn, control interfaces like onchip SERDES
  71. * and are always tied to the local mdio pins, which may not be the
  72. * same as system mdio bus, used for controlling the external PHYs, for eg.
  73. */
  74. int gfar_local_mdio_read(struct gfar_mii __iomem *regs, int mii_id, int regnum)
  75. {
  76. u16 value;
  77. /* Set the PHY address and the register address we want to read */
  78. gfar_write(&regs->miimadd, (mii_id << 8) | regnum);
  79. /* Clear miimcom, and then initiate a read */
  80. gfar_write(&regs->miimcom, 0);
  81. gfar_write(&regs->miimcom, MII_READ_COMMAND);
  82. /* Wait for the transaction to finish */
  83. while (gfar_read(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  84. cpu_relax();
  85. /* Grab the value of the register from miimstat */
  86. value = gfar_read(&regs->miimstat);
  87. return value;
  88. }
  89. /* Write value to the PHY at mii_id at register regnum,
  90. * on the bus, waiting until the write is done before returning.
  91. * All PHY configuration is done through the TSEC1 MIIM regs */
  92. int gfar_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  93. {
  94. struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
  95. /* Write to the local MII regs */
  96. return(gfar_local_mdio_write(regs, mii_id, regnum, value));
  97. }
  98. /* Read the bus for PHY at addr mii_id, register regnum, and
  99. * return the value. Clears miimcom first. All PHY
  100. * configuration has to be done through the TSEC1 MIIM regs */
  101. int gfar_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  102. {
  103. struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
  104. /* Read the local MII regs */
  105. return(gfar_local_mdio_read(regs, mii_id, regnum));
  106. }
  107. /* Reset the MIIM registers, and wait for the bus to free */
  108. static int gfar_mdio_reset(struct mii_bus *bus)
  109. {
  110. struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
  111. unsigned int timeout = PHY_INIT_TIMEOUT;
  112. mutex_lock(&bus->mdio_lock);
  113. /* Reset the management interface */
  114. gfar_write(&regs->miimcfg, MIIMCFG_RESET);
  115. /* Setup the MII Mgmt clock speed */
  116. gfar_write(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  117. /* Wait until the bus is free */
  118. while ((gfar_read(&regs->miimind) & MIIMIND_BUSY) &&
  119. --timeout)
  120. cpu_relax();
  121. mutex_unlock(&bus->mdio_lock);
  122. if(timeout == 0) {
  123. printk(KERN_ERR "%s: The MII Bus is stuck!\n",
  124. bus->name);
  125. return -EBUSY;
  126. }
  127. return 0;
  128. }
  129. /* Allocate an array which provides irq #s for each PHY on the given bus */
  130. static int *create_irq_map(struct device_node *np)
  131. {
  132. int *irqs;
  133. int i;
  134. struct device_node *child = NULL;
  135. irqs = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  136. if (!irqs)
  137. return NULL;
  138. for (i = 0; i < PHY_MAX_ADDR; i++)
  139. irqs[i] = PHY_POLL;
  140. while ((child = of_get_next_child(np, child)) != NULL) {
  141. int irq = irq_of_parse_and_map(child, 0);
  142. const u32 *id;
  143. if (irq == NO_IRQ)
  144. continue;
  145. id = of_get_property(child, "reg", NULL);
  146. if (!id)
  147. continue;
  148. if (*id < PHY_MAX_ADDR && *id >= 0)
  149. irqs[*id] = irq;
  150. else
  151. printk(KERN_WARNING "%s: "
  152. "%d is not a valid PHY address\n",
  153. np->full_name, *id);
  154. }
  155. return irqs;
  156. }
  157. void gfar_mdio_bus_name(char *name, struct device_node *np)
  158. {
  159. const u32 *reg;
  160. reg = of_get_property(np, "reg", NULL);
  161. snprintf(name, MII_BUS_ID_SIZE, "%s@%x", np->name, reg ? *reg : 0);
  162. }
  163. /* Scan the bus in reverse, looking for an empty spot */
  164. static int gfar_mdio_find_free(struct mii_bus *new_bus)
  165. {
  166. int i;
  167. for (i = PHY_MAX_ADDR; i > 0; i--) {
  168. u32 phy_id;
  169. if (get_phy_id(new_bus, i, &phy_id))
  170. return -1;
  171. if (phy_id == 0xffffffff)
  172. break;
  173. }
  174. return i;
  175. }
  176. static int gfar_mdio_probe(struct of_device *ofdev,
  177. const struct of_device_id *match)
  178. {
  179. struct gfar_mii __iomem *regs;
  180. struct gfar __iomem *enet_regs;
  181. struct mii_bus *new_bus;
  182. int err = 0;
  183. u64 addr, size;
  184. struct device_node *np = ofdev->node;
  185. struct device_node *tbi;
  186. int tbiaddr = -1;
  187. new_bus = mdiobus_alloc();
  188. if (NULL == new_bus)
  189. return -ENOMEM;
  190. new_bus->name = "Gianfar MII Bus",
  191. new_bus->read = &gfar_mdio_read,
  192. new_bus->write = &gfar_mdio_write,
  193. new_bus->reset = &gfar_mdio_reset,
  194. gfar_mdio_bus_name(new_bus->id, np);
  195. /* Set the PHY base address */
  196. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  197. regs = ioremap(addr, size);
  198. if (NULL == regs) {
  199. err = -ENOMEM;
  200. goto err_free_bus;
  201. }
  202. new_bus->priv = (void __force *)regs;
  203. new_bus->irq = create_irq_map(np);
  204. if (new_bus->irq == NULL) {
  205. err = -ENOMEM;
  206. goto err_unmap_regs;
  207. }
  208. new_bus->parent = &ofdev->dev;
  209. dev_set_drvdata(&ofdev->dev, new_bus);
  210. /*
  211. * This is mildly evil, but so is our hardware for doing this.
  212. * Also, we have to cast back to struct gfar_mii because of
  213. * definition weirdness done in gianfar.h.
  214. */
  215. enet_regs = (struct gfar __iomem *)
  216. ((char *)regs - offsetof(struct gfar, gfar_mii_regs));
  217. for_each_child_of_node(np, tbi) {
  218. if (!strncmp(tbi->type, "tbi-phy", 8))
  219. break;
  220. }
  221. if (tbi) {
  222. const u32 *prop = of_get_property(tbi, "reg", NULL);
  223. if (prop)
  224. tbiaddr = *prop;
  225. }
  226. if (tbiaddr == -1) {
  227. gfar_write(&enet_regs->tbipa, 0);
  228. tbiaddr = gfar_mdio_find_free(new_bus);
  229. }
  230. /*
  231. * We define TBIPA at 0 to be illegal, opting to fail for boards that
  232. * have PHYs at 1-31, rather than change tbipa and rescan.
  233. */
  234. if (tbiaddr == 0) {
  235. err = -EBUSY;
  236. goto err_free_irqs;
  237. }
  238. gfar_write(&enet_regs->tbipa, tbiaddr);
  239. /*
  240. * The TBIPHY-only buses will find PHYs at every address,
  241. * so we mask them all but the TBI
  242. */
  243. if (!of_device_is_compatible(np, "fsl,gianfar-mdio"))
  244. new_bus->phy_mask = ~(1 << tbiaddr);
  245. err = mdiobus_register(new_bus);
  246. if (err != 0) {
  247. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  248. new_bus->name);
  249. goto err_free_irqs;
  250. }
  251. return 0;
  252. err_free_irqs:
  253. kfree(new_bus->irq);
  254. err_unmap_regs:
  255. iounmap(regs);
  256. err_free_bus:
  257. mdiobus_free(new_bus);
  258. return err;
  259. }
  260. static int gfar_mdio_remove(struct of_device *ofdev)
  261. {
  262. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  263. mdiobus_unregister(bus);
  264. dev_set_drvdata(&ofdev->dev, NULL);
  265. iounmap((void __iomem *)bus->priv);
  266. bus->priv = NULL;
  267. kfree(bus->irq);
  268. mdiobus_free(bus);
  269. return 0;
  270. }
  271. static struct of_device_id gfar_mdio_match[] =
  272. {
  273. {
  274. .compatible = "fsl,gianfar-mdio",
  275. },
  276. {
  277. .compatible = "fsl,gianfar-tbi",
  278. },
  279. {
  280. .type = "mdio",
  281. .compatible = "gianfar",
  282. },
  283. {},
  284. };
  285. static struct of_platform_driver gianfar_mdio_driver = {
  286. .name = "fsl-gianfar_mdio",
  287. .match_table = gfar_mdio_match,
  288. .probe = gfar_mdio_probe,
  289. .remove = gfar_mdio_remove,
  290. };
  291. int __init gfar_mdio_init(void)
  292. {
  293. return of_register_platform_driver(&gianfar_mdio_driver);
  294. }
  295. void gfar_mdio_exit(void)
  296. {
  297. of_unregister_platform_driver(&gianfar_mdio_driver);
  298. }