fsl_pq_mdio.c 9.9 KB

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