fsl_pq_mdio.c 10 KB

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