fsl_pq_mdio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
  3. * Provides Bus interface for MIIM regs
  4. *
  5. * Author: Andy Fleming <afleming@freescale.com>
  6. *
  7. * Copyright (c) 2002-2004,2008 Freescale Semiconductor, Inc.
  8. *
  9. * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mm.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/crc32.h>
  33. #include <linux/mii.h>
  34. #include <linux/phy.h>
  35. #include <linux/of.h>
  36. #include <linux/of_platform.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/ucc.h>
  41. #include "gianfar.h"
  42. #include "fsl_pq_mdio.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 fsl_pq_local_mdio_write(struct fsl_pq_mdio __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. out_be32(&regs->miimadd, (mii_id << 8) | regnum);
  57. /* Write out the value we want */
  58. out_be32(&regs->miimcon, value);
  59. /* Wait for the transaction to finish */
  60. while (in_be32(&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, in turn, 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 fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
  75. int mii_id, int regnum)
  76. {
  77. u16 value;
  78. /* Set the PHY address and the register address we want to read */
  79. out_be32(&regs->miimadd, (mii_id << 8) | regnum);
  80. /* Clear miimcom, and then initiate a read */
  81. out_be32(&regs->miimcom, 0);
  82. out_be32(&regs->miimcom, MII_READ_COMMAND);
  83. /* Wait for the transaction to finish */
  84. while (in_be32(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  85. cpu_relax();
  86. /* Grab the value of the register from miimstat */
  87. value = in_be32(&regs->miimstat);
  88. return value;
  89. }
  90. /*
  91. * Write value to the PHY at mii_id at register regnum,
  92. * on the bus, waiting until the write is done before returning.
  93. */
  94. int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  95. {
  96. struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
  97. /* Write to the local MII regs */
  98. return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
  99. }
  100. /*
  101. * Read the bus for PHY at addr mii_id, register regnum, and
  102. * return the value. Clears miimcom first.
  103. */
  104. int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  105. {
  106. struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
  107. /* Read the local MII regs */
  108. return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
  109. }
  110. /* Reset the MIIM registers, and wait for the bus to free */
  111. static int fsl_pq_mdio_reset(struct mii_bus *bus)
  112. {
  113. struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
  114. unsigned int timeout = PHY_INIT_TIMEOUT;
  115. mutex_lock(&bus->mdio_lock);
  116. /* Reset the management interface */
  117. out_be32(&regs->miimcfg, MIIMCFG_RESET);
  118. /* Setup the MII Mgmt clock speed */
  119. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  120. /* Wait until the bus is free */
  121. while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
  122. cpu_relax();
  123. mutex_unlock(&bus->mdio_lock);
  124. if(timeout == 0) {
  125. printk(KERN_ERR "%s: The MII Bus is stuck!\n",
  126. bus->name);
  127. return -EBUSY;
  128. }
  129. return 0;
  130. }
  131. /* Allocate an array which provides irq #s for each PHY on the given bus */
  132. static int *create_irq_map(struct device_node *np)
  133. {
  134. int *irqs;
  135. int i;
  136. struct device_node *child = NULL;
  137. irqs = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  138. if (!irqs)
  139. return NULL;
  140. for (i = 0; i < PHY_MAX_ADDR; i++)
  141. irqs[i] = PHY_POLL;
  142. while ((child = of_get_next_child(np, child)) != NULL) {
  143. int irq = irq_of_parse_and_map(child, 0);
  144. const u32 *id;
  145. if (irq == NO_IRQ)
  146. continue;
  147. id = of_get_property(child, "reg", NULL);
  148. if (!id)
  149. continue;
  150. if (*id < PHY_MAX_ADDR && *id >= 0)
  151. irqs[*id] = irq;
  152. else
  153. printk(KERN_WARNING "%s: "
  154. "%d is not a valid PHY address\n",
  155. np->full_name, *id);
  156. }
  157. return irqs;
  158. }
  159. void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
  160. {
  161. const u32 *reg;
  162. reg = of_get_property(np, "reg", NULL);
  163. snprintf(name, MII_BUS_ID_SIZE, "%s@%x", np->name, reg ? *reg : 0);
  164. }
  165. /* Scan the bus in reverse, looking for an empty spot */
  166. static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
  167. {
  168. int i;
  169. for (i = PHY_MAX_ADDR; i > 0; i--) {
  170. u32 phy_id;
  171. if (get_phy_id(new_bus, i, &phy_id))
  172. return -1;
  173. if (phy_id == 0xffffffff)
  174. break;
  175. }
  176. return i;
  177. }
  178. #ifdef CONFIG_GIANFAR
  179. static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs)
  180. {
  181. struct gfar __iomem *enet_regs;
  182. /*
  183. * This is mildly evil, but so is our hardware for doing this.
  184. * Also, we have to cast back to struct gfar because of
  185. * definition weirdness done in gianfar.h.
  186. */
  187. enet_regs = (struct gfar __iomem *)
  188. ((char __iomem *)regs - offsetof(struct gfar, gfar_mii_regs));
  189. return &enet_regs->tbipa;
  190. }
  191. #endif
  192. #ifdef CONFIG_UCC_GETH
  193. static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
  194. {
  195. struct device_node *np = NULL;
  196. int err = 0;
  197. for_each_compatible_node(np, NULL, "ucc_geth") {
  198. struct resource tempres;
  199. err = of_address_to_resource(np, 0, &tempres);
  200. if (err)
  201. continue;
  202. /* if our mdio regs fall within this UCC regs range */
  203. if ((start >= tempres.start) && (end <= tempres.end)) {
  204. /* Find the id of the UCC */
  205. const u32 *id;
  206. id = of_get_property(np, "cell-index", NULL);
  207. if (!id) {
  208. id = of_get_property(np, "device-id", NULL);
  209. if (!id)
  210. continue;
  211. }
  212. *ucc_id = *id;
  213. return 0;
  214. }
  215. }
  216. if (err)
  217. return err;
  218. else
  219. return -EINVAL;
  220. }
  221. #endif
  222. static int fsl_pq_mdio_probe(struct of_device *ofdev,
  223. const struct of_device_id *match)
  224. {
  225. struct device_node *np = ofdev->node;
  226. struct device_node *tbi;
  227. struct fsl_pq_mdio __iomem *regs;
  228. u32 __iomem *tbipa;
  229. struct mii_bus *new_bus;
  230. int tbiaddr = -1;
  231. u64 addr, size;
  232. int err = 0;
  233. new_bus = mdiobus_alloc();
  234. if (NULL == new_bus)
  235. return -ENOMEM;
  236. new_bus->name = "Freescale PowerQUICC MII Bus",
  237. new_bus->read = &fsl_pq_mdio_read,
  238. new_bus->write = &fsl_pq_mdio_write,
  239. new_bus->reset = &fsl_pq_mdio_reset,
  240. fsl_pq_mdio_bus_name(new_bus->id, np);
  241. /* Set the PHY base address */
  242. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  243. regs = ioremap(addr, size);
  244. if (NULL == regs) {
  245. err = -ENOMEM;
  246. goto err_free_bus;
  247. }
  248. new_bus->priv = (void __force *)regs;
  249. new_bus->irq = create_irq_map(np);
  250. if (NULL == new_bus->irq) {
  251. err = -ENOMEM;
  252. goto err_unmap_regs;
  253. }
  254. new_bus->parent = &ofdev->dev;
  255. dev_set_drvdata(&ofdev->dev, new_bus);
  256. if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  257. of_device_is_compatible(np, "gianfar")) {
  258. #ifdef CONFIG_GIANFAR
  259. tbipa = get_gfar_tbipa(regs);
  260. #else
  261. err = -ENODEV;
  262. goto err_free_irqs;
  263. #endif
  264. } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
  265. of_device_is_compatible(np, "ucc_geth_phy")) {
  266. #ifdef CONFIG_UCC_GETH
  267. u32 id;
  268. tbipa = &regs->utbipar;
  269. if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
  270. goto err_free_irqs;
  271. ucc_set_qe_mux_mii_mng(id - 1);
  272. #else
  273. err = -ENODEV;
  274. goto err_free_irqs;
  275. #endif
  276. } else {
  277. err = -ENODEV;
  278. goto err_free_irqs;
  279. }
  280. for_each_child_of_node(np, tbi) {
  281. if (!strncmp(tbi->type, "tbi-phy", 8))
  282. break;
  283. }
  284. if (tbi) {
  285. const u32 *prop = of_get_property(tbi, "reg", NULL);
  286. if (prop)
  287. tbiaddr = *prop;
  288. }
  289. if (tbiaddr == -1) {
  290. out_be32(tbipa, 0);
  291. tbiaddr = fsl_pq_mdio_find_free(new_bus);
  292. }
  293. /*
  294. * We define TBIPA at 0 to be illegal, opting to fail for boards that
  295. * have PHYs at 1-31, rather than change tbipa and rescan.
  296. */
  297. if (tbiaddr == 0) {
  298. err = -EBUSY;
  299. goto err_free_irqs;
  300. }
  301. out_be32(tbipa, tbiaddr);
  302. /*
  303. * The TBIPHY-only buses will find PHYs at every address,
  304. * so we mask them all but the TBI
  305. */
  306. if (!of_device_is_compatible(np, "fsl,gianfar-mdio"))
  307. new_bus->phy_mask = ~(1 << tbiaddr);
  308. err = mdiobus_register(new_bus);
  309. if (err) {
  310. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  311. new_bus->name);
  312. goto err_free_irqs;
  313. }
  314. return 0;
  315. err_free_irqs:
  316. kfree(new_bus->irq);
  317. err_unmap_regs:
  318. iounmap(regs);
  319. err_free_bus:
  320. kfree(new_bus);
  321. return err;
  322. }
  323. static int fsl_pq_mdio_remove(struct of_device *ofdev)
  324. {
  325. struct device *device = &ofdev->dev;
  326. struct mii_bus *bus = dev_get_drvdata(device);
  327. mdiobus_unregister(bus);
  328. dev_set_drvdata(device, NULL);
  329. iounmap((void __iomem *)bus->priv);
  330. bus->priv = NULL;
  331. mdiobus_free(bus);
  332. return 0;
  333. }
  334. static struct of_device_id fsl_pq_mdio_match[] = {
  335. {
  336. .type = "mdio",
  337. .compatible = "ucc_geth_phy",
  338. },
  339. {
  340. .type = "mdio",
  341. .compatible = "gianfar",
  342. },
  343. {
  344. .compatible = "fsl,ucc-mdio",
  345. },
  346. {
  347. .compatible = "fsl,gianfar-tbi",
  348. },
  349. {
  350. .compatible = "fsl,gianfar-mdio",
  351. },
  352. {},
  353. };
  354. static struct of_platform_driver fsl_pq_mdio_driver = {
  355. .name = "fsl-pq_mdio",
  356. .probe = fsl_pq_mdio_probe,
  357. .remove = fsl_pq_mdio_remove,
  358. .match_table = fsl_pq_mdio_match,
  359. };
  360. int __init fsl_pq_mdio_init(void)
  361. {
  362. return of_register_platform_driver(&fsl_pq_mdio_driver);
  363. }
  364. void fsl_pq_mdio_exit(void)
  365. {
  366. of_unregister_platform_driver(&fsl_pq_mdio_driver);
  367. }
  368. subsys_initcall_sync(fsl_pq_mdio_init);
  369. module_exit(fsl_pq_mdio_exit);