fsl_pq_mdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. static struct fsl_pq_mdio __iomem *fsl_pq_mdio_get_regs(struct mii_bus *bus)
  93. {
  94. return (void __iomem __force *)bus->priv;
  95. }
  96. /*
  97. * Write value to the PHY at mii_id at register regnum,
  98. * on the bus, waiting until the write is done before returning.
  99. */
  100. int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
  101. {
  102. struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
  103. /* Write to the local MII regs */
  104. return(fsl_pq_local_mdio_write(regs, mii_id, regnum, value));
  105. }
  106. /*
  107. * Read the bus for PHY at addr mii_id, register regnum, and
  108. * return the value. Clears miimcom first.
  109. */
  110. int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  111. {
  112. struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
  113. /* Read the local MII regs */
  114. return(fsl_pq_local_mdio_read(regs, mii_id, regnum));
  115. }
  116. /* Reset the MIIM registers, and wait for the bus to free */
  117. static int fsl_pq_mdio_reset(struct mii_bus *bus)
  118. {
  119. struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
  120. int timeout = PHY_INIT_TIMEOUT;
  121. mutex_lock(&bus->mdio_lock);
  122. /* Reset the management interface */
  123. out_be32(&regs->miimcfg, MIIMCFG_RESET);
  124. /* Setup the MII Mgmt clock speed */
  125. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  126. /* Wait until the bus is free */
  127. while ((in_be32(&regs->miimind) & MIIMIND_BUSY) && timeout--)
  128. cpu_relax();
  129. mutex_unlock(&bus->mdio_lock);
  130. if (timeout < 0) {
  131. printk(KERN_ERR "%s: The MII Bus is stuck!\n",
  132. bus->name);
  133. return -EBUSY;
  134. }
  135. return 0;
  136. }
  137. void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
  138. {
  139. const u32 *addr;
  140. u64 taddr = OF_BAD_ADDR;
  141. addr = of_get_address(np, 0, NULL, NULL);
  142. if (addr)
  143. taddr = of_translate_address(np, addr);
  144. snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
  145. (unsigned long long)taddr);
  146. }
  147. EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
  148. /* Scan the bus in reverse, looking for an empty spot */
  149. static int fsl_pq_mdio_find_free(struct mii_bus *new_bus)
  150. {
  151. int i;
  152. for (i = PHY_MAX_ADDR; i > 0; i--) {
  153. u32 phy_id;
  154. if (get_phy_id(new_bus, i, &phy_id))
  155. return -1;
  156. if (phy_id == 0xffffffff)
  157. break;
  158. }
  159. return i;
  160. }
  161. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  162. static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
  163. {
  164. struct gfar __iomem *enet_regs;
  165. u32 __iomem *ioremap_tbipa;
  166. u64 addr, size;
  167. /*
  168. * This is mildly evil, but so is our hardware for doing this.
  169. * Also, we have to cast back to struct gfar because of
  170. * definition weirdness done in gianfar.h.
  171. */
  172. if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  173. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  174. of_device_is_compatible(np, "gianfar")) {
  175. enet_regs = (struct gfar __iomem *)regs;
  176. return &enet_regs->tbipa;
  177. } else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
  178. of_device_is_compatible(np, "fsl,etsec2-tbi")) {
  179. addr = of_translate_address(np, of_get_address(np, 1, &size, NULL));
  180. ioremap_tbipa = ioremap(addr, size);
  181. return ioremap_tbipa;
  182. } else
  183. return NULL;
  184. }
  185. #endif
  186. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  187. static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
  188. {
  189. struct device_node *np = NULL;
  190. int err = 0;
  191. for_each_compatible_node(np, NULL, "ucc_geth") {
  192. struct resource tempres;
  193. err = of_address_to_resource(np, 0, &tempres);
  194. if (err)
  195. continue;
  196. /* if our mdio regs fall within this UCC regs range */
  197. if ((start >= tempres.start) && (end <= tempres.end)) {
  198. /* Find the id of the UCC */
  199. const u32 *id;
  200. id = of_get_property(np, "cell-index", NULL);
  201. if (!id) {
  202. id = of_get_property(np, "device-id", NULL);
  203. if (!id)
  204. continue;
  205. }
  206. *ucc_id = *id;
  207. return 0;
  208. }
  209. }
  210. if (err)
  211. return err;
  212. else
  213. return -EINVAL;
  214. }
  215. #endif
  216. static int fsl_pq_mdio_probe(struct of_device *ofdev,
  217. const struct of_device_id *match)
  218. {
  219. struct device_node *np = ofdev->node;
  220. struct device_node *tbi;
  221. struct fsl_pq_mdio __iomem *regs = NULL;
  222. void __iomem *map;
  223. u32 __iomem *tbipa;
  224. struct mii_bus *new_bus;
  225. int tbiaddr = -1;
  226. u64 addr = 0, size = 0;
  227. int err = 0;
  228. new_bus = mdiobus_alloc();
  229. if (NULL == new_bus)
  230. return -ENOMEM;
  231. new_bus->name = "Freescale PowerQUICC MII Bus",
  232. new_bus->read = &fsl_pq_mdio_read,
  233. new_bus->write = &fsl_pq_mdio_write,
  234. new_bus->reset = &fsl_pq_mdio_reset,
  235. fsl_pq_mdio_bus_name(new_bus->id, np);
  236. /* Set the PHY base address */
  237. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  238. map = ioremap(addr, size);
  239. if (!map) {
  240. err = -ENOMEM;
  241. goto err_free_bus;
  242. }
  243. if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  244. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  245. of_device_is_compatible(np, "fsl,ucc-mdio") ||
  246. of_device_is_compatible(np, "ucc_geth_phy"))
  247. map -= offsetof(struct fsl_pq_mdio, miimcfg);
  248. regs = map;
  249. new_bus->priv = (void __force *)regs;
  250. new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  251. if (NULL == new_bus->irq) {
  252. err = -ENOMEM;
  253. goto err_unmap_regs;
  254. }
  255. new_bus->parent = &ofdev->dev;
  256. dev_set_drvdata(&ofdev->dev, new_bus);
  257. if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  258. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  259. of_device_is_compatible(np, "fsl,etsec2-mdio") ||
  260. of_device_is_compatible(np, "fsl,etsec2-tbi") ||
  261. of_device_is_compatible(np, "gianfar")) {
  262. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  263. tbipa = get_gfar_tbipa(regs, np);
  264. if (!tbipa) {
  265. err = -EINVAL;
  266. goto err_free_irqs;
  267. }
  268. #else
  269. err = -ENODEV;
  270. goto err_free_irqs;
  271. #endif
  272. } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
  273. of_device_is_compatible(np, "ucc_geth_phy")) {
  274. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  275. u32 id;
  276. static u32 mii_mng_master;
  277. tbipa = &regs->utbipar;
  278. if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
  279. goto err_free_irqs;
  280. if (!mii_mng_master) {
  281. mii_mng_master = id;
  282. ucc_set_qe_mux_mii_mng(id - 1);
  283. }
  284. #else
  285. err = -ENODEV;
  286. goto err_free_irqs;
  287. #endif
  288. } else {
  289. err = -ENODEV;
  290. goto err_free_irqs;
  291. }
  292. for_each_child_of_node(np, tbi) {
  293. if (!strncmp(tbi->type, "tbi-phy", 8))
  294. break;
  295. }
  296. if (tbi) {
  297. const u32 *prop = of_get_property(tbi, "reg", NULL);
  298. if (prop)
  299. tbiaddr = *prop;
  300. }
  301. if (tbiaddr == -1) {
  302. out_be32(tbipa, 0);
  303. tbiaddr = fsl_pq_mdio_find_free(new_bus);
  304. }
  305. /*
  306. * We define TBIPA at 0 to be illegal, opting to fail for boards that
  307. * have PHYs at 1-31, rather than change tbipa and rescan.
  308. */
  309. if (tbiaddr == 0) {
  310. err = -EBUSY;
  311. goto err_free_irqs;
  312. }
  313. out_be32(tbipa, tbiaddr);
  314. err = of_mdiobus_register(new_bus, np);
  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(fsl_pq_mdio_get_regs(bus));
  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. .compatible = "fsl,etsec2-tbi",
  360. },
  361. {
  362. .compatible = "fsl,etsec2-mdio",
  363. },
  364. {},
  365. };
  366. MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
  367. static struct of_platform_driver fsl_pq_mdio_driver = {
  368. .name = "fsl-pq_mdio",
  369. .probe = fsl_pq_mdio_probe,
  370. .remove = fsl_pq_mdio_remove,
  371. .match_table = fsl_pq_mdio_match,
  372. };
  373. int __init fsl_pq_mdio_init(void)
  374. {
  375. return of_register_platform_driver(&fsl_pq_mdio_driver);
  376. }
  377. module_init(fsl_pq_mdio_init);
  378. void fsl_pq_mdio_exit(void)
  379. {
  380. of_unregister_platform_driver(&fsl_pq_mdio_driver);
  381. }
  382. module_exit(fsl_pq_mdio_exit);
  383. MODULE_LICENSE("GPL");