fsl_pq_mdio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. 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 *addr;
  162. u64 taddr = OF_BAD_ADDR;
  163. addr = of_get_address(np, 0, NULL, NULL);
  164. if (addr)
  165. taddr = of_translate_address(np, addr);
  166. snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
  167. (unsigned long long)taddr);
  168. }
  169. EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
  170. /* Scan the bus in reverse, looking for an empty spot */
  171. static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
  172. {
  173. int i;
  174. for (i = PHY_MAX_ADDR; i > 0; i--) {
  175. u32 phy_id;
  176. if (get_phy_id(new_bus, i, &phy_id))
  177. return -1;
  178. if (phy_id == 0xffffffff)
  179. break;
  180. }
  181. return i;
  182. }
  183. #ifdef CONFIG_GIANFAR
  184. static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs)
  185. {
  186. struct gfar __iomem *enet_regs;
  187. /*
  188. * This is mildly evil, but so is our hardware for doing this.
  189. * Also, we have to cast back to struct gfar because of
  190. * definition weirdness done in gianfar.h.
  191. */
  192. enet_regs = (struct gfar __iomem *)
  193. ((char __iomem *)regs - offsetof(struct gfar, gfar_mii_regs));
  194. return &enet_regs->tbipa;
  195. }
  196. #endif
  197. #ifdef CONFIG_UCC_GETH
  198. static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
  199. {
  200. struct device_node *np = NULL;
  201. int err = 0;
  202. for_each_compatible_node(np, NULL, "ucc_geth") {
  203. struct resource tempres;
  204. err = of_address_to_resource(np, 0, &tempres);
  205. if (err)
  206. continue;
  207. /* if our mdio regs fall within this UCC regs range */
  208. if ((start >= tempres.start) && (end <= tempres.end)) {
  209. /* Find the id of the UCC */
  210. const u32 *id;
  211. id = of_get_property(np, "cell-index", NULL);
  212. if (!id) {
  213. id = of_get_property(np, "device-id", NULL);
  214. if (!id)
  215. continue;
  216. }
  217. *ucc_id = *id;
  218. return 0;
  219. }
  220. }
  221. if (err)
  222. return err;
  223. else
  224. return -EINVAL;
  225. }
  226. #endif
  227. static int fsl_pq_mdio_probe(struct of_device *ofdev,
  228. const struct of_device_id *match)
  229. {
  230. struct device_node *np = ofdev->node;
  231. struct device_node *tbi;
  232. struct fsl_pq_mdio __iomem *regs;
  233. u32 __iomem *tbipa;
  234. struct mii_bus *new_bus;
  235. int tbiaddr = -1;
  236. u64 addr, size;
  237. int err = 0;
  238. new_bus = mdiobus_alloc();
  239. if (NULL == new_bus)
  240. return -ENOMEM;
  241. new_bus->name = "Freescale PowerQUICC MII Bus",
  242. new_bus->read = &fsl_pq_mdio_read,
  243. new_bus->write = &fsl_pq_mdio_write,
  244. new_bus->reset = &fsl_pq_mdio_reset,
  245. fsl_pq_mdio_bus_name(new_bus->id, np);
  246. /* Set the PHY base address */
  247. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  248. regs = ioremap(addr, size);
  249. if (NULL == regs) {
  250. err = -ENOMEM;
  251. goto err_free_bus;
  252. }
  253. new_bus->priv = (void __force *)regs;
  254. new_bus->irq = create_irq_map(np);
  255. if (NULL == new_bus->irq) {
  256. err = -ENOMEM;
  257. goto err_unmap_regs;
  258. }
  259. new_bus->parent = &ofdev->dev;
  260. dev_set_drvdata(&ofdev->dev, new_bus);
  261. if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  262. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  263. of_device_is_compatible(np, "gianfar")) {
  264. #ifdef CONFIG_GIANFAR
  265. tbipa = get_gfar_tbipa(regs);
  266. #else
  267. err = -ENODEV;
  268. goto err_free_irqs;
  269. #endif
  270. } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
  271. of_device_is_compatible(np, "ucc_geth_phy")) {
  272. #ifdef CONFIG_UCC_GETH
  273. u32 id;
  274. tbipa = &regs->utbipar;
  275. if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
  276. goto err_free_irqs;
  277. ucc_set_qe_mux_mii_mng(id - 1);
  278. #else
  279. err = -ENODEV;
  280. goto err_free_irqs;
  281. #endif
  282. } else {
  283. err = -ENODEV;
  284. goto err_free_irqs;
  285. }
  286. for_each_child_of_node(np, tbi) {
  287. if (!strncmp(tbi->type, "tbi-phy", 8))
  288. break;
  289. }
  290. if (tbi) {
  291. const u32 *prop = of_get_property(tbi, "reg", NULL);
  292. if (prop)
  293. tbiaddr = *prop;
  294. }
  295. if (tbiaddr == -1) {
  296. out_be32(tbipa, 0);
  297. tbiaddr = fsl_pq_mdio_find_free(new_bus);
  298. }
  299. /*
  300. * We define TBIPA at 0 to be illegal, opting to fail for boards that
  301. * have PHYs at 1-31, rather than change tbipa and rescan.
  302. */
  303. if (tbiaddr == 0) {
  304. err = -EBUSY;
  305. goto err_free_irqs;
  306. }
  307. out_be32(tbipa, tbiaddr);
  308. /*
  309. * The TBIPHY-only buses will find PHYs at every address,
  310. * so we mask them all but the TBI
  311. */
  312. if (of_device_is_compatible(np, "fsl,gianfar-tbi"))
  313. new_bus->phy_mask = ~(1 << tbiaddr);
  314. err = mdiobus_register(new_bus);
  315. if (err) {
  316. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  317. new_bus->name);
  318. goto err_free_irqs;
  319. }
  320. return 0;
  321. err_free_irqs:
  322. kfree(new_bus->irq);
  323. err_unmap_regs:
  324. iounmap(regs);
  325. err_free_bus:
  326. kfree(new_bus);
  327. return err;
  328. }
  329. static int fsl_pq_mdio_remove(struct of_device *ofdev)
  330. {
  331. struct device *device = &ofdev->dev;
  332. struct mii_bus *bus = dev_get_drvdata(device);
  333. mdiobus_unregister(bus);
  334. dev_set_drvdata(device, NULL);
  335. iounmap((void __iomem *)bus->priv);
  336. bus->priv = NULL;
  337. mdiobus_free(bus);
  338. return 0;
  339. }
  340. static struct of_device_id fsl_pq_mdio_match[] = {
  341. {
  342. .type = "mdio",
  343. .compatible = "ucc_geth_phy",
  344. },
  345. {
  346. .type = "mdio",
  347. .compatible = "gianfar",
  348. },
  349. {
  350. .compatible = "fsl,ucc-mdio",
  351. },
  352. {
  353. .compatible = "fsl,gianfar-tbi",
  354. },
  355. {
  356. .compatible = "fsl,gianfar-mdio",
  357. },
  358. {},
  359. };
  360. static struct of_platform_driver fsl_pq_mdio_driver = {
  361. .name = "fsl-pq_mdio",
  362. .probe = fsl_pq_mdio_probe,
  363. .remove = fsl_pq_mdio_remove,
  364. .match_table = fsl_pq_mdio_match,
  365. };
  366. int __init fsl_pq_mdio_init(void)
  367. {
  368. return of_register_platform_driver(&fsl_pq_mdio_driver);
  369. }
  370. void fsl_pq_mdio_exit(void)
  371. {
  372. of_unregister_platform_driver(&fsl_pq_mdio_driver);
  373. }
  374. subsys_initcall_sync(fsl_pq_mdio_init);
  375. module_exit(fsl_pq_mdio_exit);