fsl_pq_mdio.c 11 KB

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