fsl_pq_mdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
  7. *
  8. * Copyright 2002-2004, 2008-2009 Freescale Semiconductor, Inc.
  9. *
  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. #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_mdio.h>
  38. #include <linux/of_platform.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/ucc.h>
  43. #include "gianfar.h"
  44. #include "fsl_pq_mdio.h"
  45. /*
  46. * Write value to the PHY at mii_id at register regnum,
  47. * on the bus attached to the local interface, which may be different from the
  48. * generic mdio bus (tied to a single interface), waiting until the write is
  49. * done before returning. This is helpful in programming interfaces like
  50. * the TBI which control interfaces like onchip SERDES and are always tied to
  51. * the local mdio pins, which may not be the same as system mdio bus, used for
  52. * controlling the external PHYs, for example.
  53. */
  54. int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
  55. int regnum, u16 value)
  56. {
  57. /* Set the PHY address and the register address we want to write */
  58. out_be32(&regs->miimadd, (mii_id << 8) | regnum);
  59. /* Write out the value we want */
  60. out_be32(&regs->miimcon, value);
  61. /* Wait for the transaction to finish */
  62. while (in_be32(&regs->miimind) & MIIMIND_BUSY)
  63. cpu_relax();
  64. return 0;
  65. }
  66. /*
  67. * Read the bus for PHY at addr mii_id, register regnum, and
  68. * return the value. Clears miimcom first. All PHY operation
  69. * done on the bus attached to the local interface,
  70. * which may be different from the generic mdio bus
  71. * This is helpful in programming interfaces like
  72. * the TBI which, in turn, control interfaces like onchip SERDES
  73. * and are always tied to the local mdio pins, which may not be the
  74. * same as system mdio bus, used for controlling the external PHYs, for eg.
  75. */
  76. int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
  77. int mii_id, int regnum)
  78. {
  79. u16 value;
  80. /* Set the PHY address and the register address we want to read */
  81. out_be32(&regs->miimadd, (mii_id << 8) | regnum);
  82. /* Clear miimcom, and then initiate a read */
  83. out_be32(&regs->miimcom, 0);
  84. out_be32(&regs->miimcom, MII_READ_COMMAND);
  85. /* Wait for the transaction to finish */
  86. while (in_be32(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  87. cpu_relax();
  88. /* Grab the value of the register from miimstat */
  89. value = in_be32(&regs->miimstat);
  90. return value;
  91. }
  92. /*
  93. * Write value to the PHY at mii_id at register regnum,
  94. * on the bus, waiting until the write is done before returning.
  95. */
  96. int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  97. {
  98. struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
  99. /* Write to the local MII regs */
  100. return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
  101. }
  102. /*
  103. * Read the bus for PHY at addr mii_id, register regnum, and
  104. * return the value. Clears miimcom first.
  105. */
  106. int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  107. {
  108. struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
  109. /* Read the local MII regs */
  110. return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
  111. }
  112. /* Reset the MIIM registers, and wait for the bus to free */
  113. static int fsl_pq_mdio_reset(struct mii_bus *bus)
  114. {
  115. struct fsl_pq_mdio __iomem *regs = (void __iomem *)bus->priv;
  116. int timeout = PHY_INIT_TIMEOUT;
  117. mutex_lock(&bus->mdio_lock);
  118. /* Reset the management interface */
  119. out_be32(&regs->miimcfg, MIIMCFG_RESET);
  120. /* Setup the MII Mgmt clock speed */
  121. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  122. /* Wait until the bus is free */
  123. while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
  124. cpu_relax();
  125. mutex_unlock(&bus->mdio_lock);
  126. if (timeout < 0) {
  127. printk(KERN_ERR "%s: The MII Bus is stuck!\n",
  128. bus->name);
  129. return -EBUSY;
  130. }
  131. return 0;
  132. }
  133. void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
  134. {
  135. const u32 *addr;
  136. u64 taddr = OF_BAD_ADDR;
  137. addr = of_get_address(np, 0, NULL, NULL);
  138. if (addr)
  139. taddr = of_translate_address(np, addr);
  140. snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
  141. (unsigned long long)taddr);
  142. }
  143. EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
  144. /* Scan the bus in reverse, looking for an empty spot */
  145. static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
  146. {
  147. int i;
  148. for (i = PHY_MAX_ADDR; i > 0; i--) {
  149. u32 phy_id;
  150. if (get_phy_id(new_bus, i, &phy_id))
  151. return -1;
  152. if (phy_id == 0xffffffff)
  153. break;
  154. }
  155. return i;
  156. }
  157. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  158. static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
  159. {
  160. struct gfar __iomem *enet_regs;
  161. u32 __iomem *ioremap_tbipa;
  162. u64 addr, size;
  163. /*
  164. * This is mildly evil, but so is our hardware for doing this.
  165. * Also, we have to cast back to struct gfar because of
  166. * definition weirdness done in gianfar.h.
  167. */
  168. if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  169. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  170. of_device_is_compatible(np, "gianfar")) {
  171. enet_regs = (struct gfar __iomem *)regs;
  172. return &enet_regs->tbipa;
  173. } else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
  174. of_device_is_compatible(np, "fsl,etsec2-tbi")) {
  175. addr = of_translate_address(np, of_get_address(np, 1, &size, NULL));
  176. ioremap_tbipa = ioremap(addr, size);
  177. return ioremap_tbipa;
  178. } else
  179. return NULL;
  180. }
  181. #endif
  182. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  183. static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
  184. {
  185. struct device_node *np = NULL;
  186. int err = 0;
  187. for_each_compatible_node(np, NULL, "ucc_geth") {
  188. struct resource tempres;
  189. err = of_address_to_resource(np, 0, &tempres);
  190. if (err)
  191. continue;
  192. /* if our mdio regs fall within this UCC regs range */
  193. if ((start >= tempres.start) && (end <= tempres.end)) {
  194. /* Find the id of the UCC */
  195. const u32 *id;
  196. id = of_get_property(np, "cell-index", NULL);
  197. if (!id) {
  198. id = of_get_property(np, "device-id", NULL);
  199. if (!id)
  200. continue;
  201. }
  202. *ucc_id = *id;
  203. return 0;
  204. }
  205. }
  206. if (err)
  207. return err;
  208. else
  209. return -EINVAL;
  210. }
  211. #endif
  212. static int fsl_pq_mdio_probe(struct of_device *ofdev,
  213. const struct of_device_id *match)
  214. {
  215. struct device_node *np = ofdev->node;
  216. struct device_node *tbi;
  217. struct fsl_pq_mdio __iomem *regs = NULL;
  218. u32 __iomem *tbipa;
  219. struct mii_bus *new_bus;
  220. int tbiaddr = -1;
  221. u64 addr = 0, size = 0, ioremap_miimcfg = 0;
  222. int err = 0;
  223. new_bus = mdiobus_alloc();
  224. if (NULL == new_bus)
  225. return -ENOMEM;
  226. new_bus->name = "Freescale PowerQUICC MII Bus",
  227. new_bus->read = &fsl_pq_mdio_read,
  228. new_bus->write = &fsl_pq_mdio_write,
  229. new_bus->reset = &fsl_pq_mdio_reset,
  230. fsl_pq_mdio_bus_name(new_bus->id, np);
  231. /* Set the PHY base address */
  232. if (of_device_is_compatible(np,"fsl,gianfar-mdio") ||
  233. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  234. of_device_is_compatible(np, "fsl,ucc-mdio") ||
  235. of_device_is_compatible(np,"ucc_geth_phy" )) {
  236. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  237. ioremap_miimcfg = container_of(addr, struct fsl_pq_mdio, miimcfg);
  238. regs = ioremap(ioremap_miimcfg, size +
  239. offsetof(struct fsl_pq_mdio, miimcfg));
  240. } else if (of_device_is_compatible(np,"fsl,etsec2-mdio") ||
  241. of_device_is_compatible(np, "fsl,etsec2-tbi")) {
  242. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  243. regs = ioremap(addr, size);
  244. } else {
  245. err = -EINVAL;
  246. goto err_free_bus;
  247. }
  248. if (NULL == regs) {
  249. err = -ENOMEM;
  250. goto err_free_bus;
  251. }
  252. new_bus->priv = (void __force *)regs;
  253. new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  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, "fsl,etsec2-mdio") ||
  263. of_device_is_compatible(np, "fsl,etsec2-tbi") ||
  264. of_device_is_compatible(np, "gianfar")) {
  265. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  266. tbipa = get_gfar_tbipa(regs, np);
  267. if (!tbipa) {
  268. err = -EINVAL;
  269. goto err_free_irqs;
  270. }
  271. #else
  272. err = -ENODEV;
  273. goto err_free_irqs;
  274. #endif
  275. } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
  276. of_device_is_compatible(np, "ucc_geth_phy")) {
  277. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  278. u32 id;
  279. static u32 mii_mng_master;
  280. tbipa = &regs->utbipar;
  281. if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
  282. goto err_free_irqs;
  283. if (!mii_mng_master) {
  284. mii_mng_master = id;
  285. ucc_set_qe_mux_mii_mng(id - 1);
  286. }
  287. #else
  288. err = -ENODEV;
  289. goto err_free_irqs;
  290. #endif
  291. } else {
  292. err = -ENODEV;
  293. goto err_free_irqs;
  294. }
  295. for_each_child_of_node(np, tbi) {
  296. if (!strncmp(tbi->type, "tbi-phy", 8))
  297. break;
  298. }
  299. if (tbi) {
  300. const u32 *prop = of_get_property(tbi, "reg", NULL);
  301. if (prop)
  302. tbiaddr = *prop;
  303. }
  304. if (tbiaddr == -1) {
  305. out_be32(tbipa, 0);
  306. tbiaddr = fsl_pq_mdio_find_free(new_bus);
  307. }
  308. /*
  309. * We define TBIPA at 0 to be illegal, opting to fail for boards that
  310. * have PHYs at 1-31, rather than change tbipa and rescan.
  311. */
  312. if (tbiaddr == 0) {
  313. err = -EBUSY;
  314. goto err_free_irqs;
  315. }
  316. out_be32(tbipa, tbiaddr);
  317. err = of_mdiobus_register(new_bus, np);
  318. if (err) {
  319. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  320. new_bus->name);
  321. goto err_free_irqs;
  322. }
  323. return 0;
  324. err_free_irqs:
  325. kfree(new_bus->irq);
  326. err_unmap_regs:
  327. iounmap(regs);
  328. err_free_bus:
  329. kfree(new_bus);
  330. return err;
  331. }
  332. static int fsl_pq_mdio_remove(struct of_device *ofdev)
  333. {
  334. struct device *device = &ofdev->dev;
  335. struct mii_bus *bus = dev_get_drvdata(device);
  336. mdiobus_unregister(bus);
  337. dev_set_drvdata(device, NULL);
  338. iounmap((void __iomem *)bus->priv);
  339. bus->priv = NULL;
  340. mdiobus_free(bus);
  341. return 0;
  342. }
  343. static struct of_device_id fsl_pq_mdio_match[] = {
  344. {
  345. .type = "mdio",
  346. .compatible = "ucc_geth_phy",
  347. },
  348. {
  349. .type = "mdio",
  350. .compatible = "gianfar",
  351. },
  352. {
  353. .compatible = "fsl,ucc-mdio",
  354. },
  355. {
  356. .compatible = "fsl,gianfar-tbi",
  357. },
  358. {
  359. .compatible = "fsl,gianfar-mdio",
  360. },
  361. {
  362. .compatible = "fsl,etsec2-tbi",
  363. },
  364. {
  365. .compatible = "fsl,etsec2-mdio",
  366. },
  367. {},
  368. };
  369. MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
  370. static struct of_platform_driver fsl_pq_mdio_driver = {
  371. .name = "fsl-pq_mdio",
  372. .probe = fsl_pq_mdio_probe,
  373. .remove = fsl_pq_mdio_remove,
  374. .match_table = fsl_pq_mdio_match,
  375. };
  376. int __init fsl_pq_mdio_init(void)
  377. {
  378. return of_register_platform_driver(&fsl_pq_mdio_driver);
  379. }
  380. module_init(fsl_pq_mdio_init);
  381. void fsl_pq_mdio_exit(void)
  382. {
  383. of_unregister_platform_driver(&fsl_pq_mdio_driver);
  384. }
  385. module_exit(fsl_pq_mdio_exit);