fsl_pq_mdio.c 11 KB

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