fsl_pq_mdio.c 11 KB

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