fsl_pq_mdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. void __iomem *map;
  219. u32 __iomem *tbipa;
  220. struct mii_bus *new_bus;
  221. int tbiaddr = -1;
  222. u64 addr = 0, size = 0;
  223. int err = 0;
  224. new_bus = mdiobus_alloc();
  225. if (NULL == new_bus)
  226. return -ENOMEM;
  227. new_bus->name = "Freescale PowerQUICC MII Bus",
  228. new_bus->read = &fsl_pq_mdio_read,
  229. new_bus->write = &fsl_pq_mdio_write,
  230. new_bus->reset = &fsl_pq_mdio_reset,
  231. fsl_pq_mdio_bus_name(new_bus->id, np);
  232. /* Set the PHY base address */
  233. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  234. map = ioremap(addr, size);
  235. if (!map) {
  236. err = -ENOMEM;
  237. goto err_free_bus;
  238. }
  239. if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  240. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  241. of_device_is_compatible(np, "fsl,ucc-mdio") ||
  242. of_device_is_compatible(np, "ucc_geth_phy"))
  243. map -= offsetof(struct fsl_pq_mdio, miimcfg);
  244. regs = map;
  245. new_bus->priv = (void __force *)regs;
  246. new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  247. if (NULL == new_bus->irq) {
  248. err = -ENOMEM;
  249. goto err_unmap_regs;
  250. }
  251. new_bus->parent = &ofdev->dev;
  252. dev_set_drvdata(&ofdev->dev, new_bus);
  253. if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
  254. of_device_is_compatible(np, "fsl,gianfar-tbi") ||
  255. of_device_is_compatible(np, "fsl,etsec2-mdio") ||
  256. of_device_is_compatible(np, "fsl,etsec2-tbi") ||
  257. of_device_is_compatible(np, "gianfar")) {
  258. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  259. tbipa = get_gfar_tbipa(regs, np);
  260. if (!tbipa) {
  261. err = -EINVAL;
  262. goto err_free_irqs;
  263. }
  264. #else
  265. err = -ENODEV;
  266. goto err_free_irqs;
  267. #endif
  268. } else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
  269. of_device_is_compatible(np, "ucc_geth_phy")) {
  270. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  271. u32 id;
  272. static u32 mii_mng_master;
  273. tbipa = &regs->utbipar;
  274. if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
  275. goto err_free_irqs;
  276. if (!mii_mng_master) {
  277. mii_mng_master = id;
  278. ucc_set_qe_mux_mii_mng(id - 1);
  279. }
  280. #else
  281. err = -ENODEV;
  282. goto err_free_irqs;
  283. #endif
  284. } else {
  285. err = -ENODEV;
  286. goto err_free_irqs;
  287. }
  288. for_each_child_of_node(np, tbi) {
  289. if (!strncmp(tbi->type, "tbi-phy", 8))
  290. break;
  291. }
  292. if (tbi) {
  293. const u32 *prop = of_get_property(tbi, "reg", NULL);
  294. if (prop)
  295. tbiaddr = *prop;
  296. }
  297. if (tbiaddr == -1) {
  298. out_be32(tbipa, 0);
  299. tbiaddr = fsl_pq_mdio_find_free(new_bus);
  300. }
  301. /*
  302. * We define TBIPA at 0 to be illegal, opting to fail for boards that
  303. * have PHYs at 1-31, rather than change tbipa and rescan.
  304. */
  305. if (tbiaddr == 0) {
  306. err = -EBUSY;
  307. goto err_free_irqs;
  308. }
  309. out_be32(tbipa, tbiaddr);
  310. err = of_mdiobus_register(new_bus, np);
  311. if (err) {
  312. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  313. new_bus->name);
  314. goto err_free_irqs;
  315. }
  316. return 0;
  317. err_free_irqs:
  318. kfree(new_bus->irq);
  319. err_unmap_regs:
  320. iounmap(regs);
  321. err_free_bus:
  322. kfree(new_bus);
  323. return err;
  324. }
  325. static int fsl_pq_mdio_remove(struct of_device *ofdev)
  326. {
  327. struct device *device = &ofdev->dev;
  328. struct mii_bus *bus = dev_get_drvdata(device);
  329. mdiobus_unregister(bus);
  330. dev_set_drvdata(device, NULL);
  331. iounmap((void __iomem *)bus->priv);
  332. bus->priv = NULL;
  333. mdiobus_free(bus);
  334. return 0;
  335. }
  336. static struct of_device_id fsl_pq_mdio_match[] = {
  337. {
  338. .type = "mdio",
  339. .compatible = "ucc_geth_phy",
  340. },
  341. {
  342. .type = "mdio",
  343. .compatible = "gianfar",
  344. },
  345. {
  346. .compatible = "fsl,ucc-mdio",
  347. },
  348. {
  349. .compatible = "fsl,gianfar-tbi",
  350. },
  351. {
  352. .compatible = "fsl,gianfar-mdio",
  353. },
  354. {
  355. .compatible = "fsl,etsec2-tbi",
  356. },
  357. {
  358. .compatible = "fsl,etsec2-mdio",
  359. },
  360. {},
  361. };
  362. MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
  363. static struct of_platform_driver fsl_pq_mdio_driver = {
  364. .name = "fsl-pq_mdio",
  365. .probe = fsl_pq_mdio_probe,
  366. .remove = fsl_pq_mdio_remove,
  367. .match_table = fsl_pq_mdio_match,
  368. };
  369. int __init fsl_pq_mdio_init(void)
  370. {
  371. return of_register_platform_driver(&fsl_pq_mdio_driver);
  372. }
  373. module_init(fsl_pq_mdio_init);
  374. void fsl_pq_mdio_exit(void)
  375. {
  376. of_unregister_platform_driver(&fsl_pq_mdio_driver);
  377. }
  378. module_exit(fsl_pq_mdio_exit);