fsl_pq_mdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/mii.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_mdio.h>
  28. #include <linux/of_device.h>
  29. #include <asm/io.h>
  30. #include <asm/ucc.h> /* for ucc_set_qe_mux_mii_mng() */
  31. #include "gianfar.h"
  32. #define MIIMIND_BUSY 0x00000001
  33. #define MIIMIND_NOTVALID 0x00000004
  34. #define MIIMCFG_INIT_VALUE 0x00000007
  35. #define MIIMCFG_RESET 0x80000000
  36. #define MII_READ_COMMAND 0x00000001
  37. struct fsl_pq_mii {
  38. u32 miimcfg; /* MII management configuration reg */
  39. u32 miimcom; /* MII management command reg */
  40. u32 miimadd; /* MII management address reg */
  41. u32 miimcon; /* MII management control reg */
  42. u32 miimstat; /* MII management status reg */
  43. u32 miimind; /* MII management indication reg */
  44. };
  45. struct fsl_pq_mdio {
  46. u8 res1[16];
  47. u32 ieventm; /* MDIO Interrupt event register (for etsec2)*/
  48. u32 imaskm; /* MDIO Interrupt mask register (for etsec2)*/
  49. u8 res2[4];
  50. u32 emapm; /* MDIO Event mapping register (for etsec2)*/
  51. u8 res3[1280];
  52. struct fsl_pq_mii mii;
  53. u8 res4[28];
  54. u32 utbipar; /* TBI phy address reg (only on UCC) */
  55. u8 res5[2728];
  56. } __packed;
  57. /* Number of microseconds to wait for an MII register to respond */
  58. #define MII_TIMEOUT 1000
  59. struct fsl_pq_mdio_priv {
  60. void __iomem *map;
  61. struct fsl_pq_mii __iomem *regs;
  62. int irqs[PHY_MAX_ADDR];
  63. };
  64. /*
  65. * Per-device-type data. Each type of device tree node that we support gets
  66. * one of these.
  67. *
  68. * @mii_offset: the offset of the MII registers within the memory map of the
  69. * node. Some nodes define only the MII registers, and some define the whole
  70. * MAC (which includes the MII registers).
  71. *
  72. * @get_tbipa: determines the address of the TBIPA register
  73. *
  74. * @ucc_configure: a special function for extra QE configuration
  75. */
  76. struct fsl_pq_mdio_data {
  77. unsigned int mii_offset; /* offset of the MII registers */
  78. uint32_t __iomem * (*get_tbipa)(void __iomem *p);
  79. void (*ucc_configure)(phys_addr_t start, phys_addr_t end);
  80. };
  81. /*
  82. * Write value to the PHY at mii_id at register regnum, on the bus attached
  83. * to the local interface, which may be different from the generic mdio bus
  84. * (tied to a single interface), waiting until the write is done before
  85. * returning. This is helpful in programming interfaces like the TBI which
  86. * control interfaces like onchip SERDES and are always tied to the local
  87. * mdio pins, which may not be the same as system mdio bus, used for
  88. * controlling the external PHYs, for example.
  89. */
  90. static int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  91. u16 value)
  92. {
  93. struct fsl_pq_mdio_priv *priv = bus->priv;
  94. struct fsl_pq_mii __iomem *regs = priv->regs;
  95. u32 status;
  96. /* Set the PHY address and the register address we want to write */
  97. out_be32(&regs->miimadd, (mii_id << 8) | regnum);
  98. /* Write out the value we want */
  99. out_be32(&regs->miimcon, value);
  100. /* Wait for the transaction to finish */
  101. status = spin_event_timeout(!(in_be32(&regs->miimind) & MIIMIND_BUSY),
  102. MII_TIMEOUT, 0);
  103. return status ? 0 : -ETIMEDOUT;
  104. }
  105. /*
  106. * Read the bus for PHY at addr mii_id, register regnum, and return the value.
  107. * Clears miimcom first.
  108. *
  109. * All PHY operation done on the bus attached to the local interface, which
  110. * may be different from the generic mdio bus. This is helpful in programming
  111. * interfaces like the TBI which, in turn, control interfaces like on-chip
  112. * SERDES and are always tied to the local mdio pins, which may not be the
  113. * same as system mdio bus, used for controlling the external PHYs, for eg.
  114. */
  115. static int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  116. {
  117. struct fsl_pq_mdio_priv *priv = bus->priv;
  118. struct fsl_pq_mii __iomem *regs = priv->regs;
  119. u32 status;
  120. u16 value;
  121. /* Set the PHY address and the register address we want to read */
  122. out_be32(&regs->miimadd, (mii_id << 8) | regnum);
  123. /* Clear miimcom, and then initiate a read */
  124. out_be32(&regs->miimcom, 0);
  125. out_be32(&regs->miimcom, MII_READ_COMMAND);
  126. /* Wait for the transaction to finish, normally less than 100us */
  127. status = spin_event_timeout(!(in_be32(&regs->miimind) &
  128. (MIIMIND_NOTVALID | MIIMIND_BUSY)),
  129. MII_TIMEOUT, 0);
  130. if (!status)
  131. return -ETIMEDOUT;
  132. /* Grab the value of the register from miimstat */
  133. value = in_be32(&regs->miimstat);
  134. dev_dbg(&bus->dev, "read %04x from address %x/%x\n", value, mii_id, regnum);
  135. return value;
  136. }
  137. /* Reset the MIIM registers, and wait for the bus to free */
  138. static int fsl_pq_mdio_reset(struct mii_bus *bus)
  139. {
  140. struct fsl_pq_mdio_priv *priv = bus->priv;
  141. struct fsl_pq_mii __iomem *regs = priv->regs;
  142. u32 status;
  143. mutex_lock(&bus->mdio_lock);
  144. /* Reset the management interface */
  145. out_be32(&regs->miimcfg, MIIMCFG_RESET);
  146. /* Setup the MII Mgmt clock speed */
  147. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  148. /* Wait until the bus is free */
  149. status = spin_event_timeout(!(in_be32(&regs->miimind) & MIIMIND_BUSY),
  150. MII_TIMEOUT, 0);
  151. mutex_unlock(&bus->mdio_lock);
  152. if (!status) {
  153. dev_err(&bus->dev, "timeout waiting for MII bus\n");
  154. return -EBUSY;
  155. }
  156. return 0;
  157. }
  158. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  159. /*
  160. * This is mildly evil, but so is our hardware for doing this.
  161. * Also, we have to cast back to struct gfar because of
  162. * definition weirdness done in gianfar.h.
  163. */
  164. static uint32_t __iomem *get_gfar_tbipa(void __iomem *p)
  165. {
  166. struct gfar __iomem *enet_regs = p;
  167. return &enet_regs->tbipa;
  168. }
  169. /*
  170. * Return the TBIPAR address for an eTSEC2 node
  171. */
  172. static uint32_t __iomem *get_etsec_tbipa(void __iomem *p)
  173. {
  174. return p;
  175. }
  176. #endif
  177. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  178. /*
  179. * Return the TBIPAR address for a QE MDIO node
  180. */
  181. static uint32_t __iomem *get_ucc_tbipa(void __iomem *p)
  182. {
  183. struct fsl_pq_mdio __iomem *mdio = p;
  184. return &mdio->utbipar;
  185. }
  186. /*
  187. * Find the UCC node that controls the given MDIO node
  188. *
  189. * For some reason, the QE MDIO nodes are not children of the UCC devices
  190. * that control them. Therefore, we need to scan all UCC nodes looking for
  191. * the one that encompases the given MDIO node. We do this by comparing
  192. * physical addresses. The 'start' and 'end' addresses of the MDIO node are
  193. * passed, and the correct UCC node will cover the entire address range.
  194. *
  195. * This assumes that there is only one QE MDIO node in the entire device tree.
  196. */
  197. static void ucc_configure(phys_addr_t start, phys_addr_t end)
  198. {
  199. static bool found_mii_master;
  200. struct device_node *np = NULL;
  201. if (found_mii_master)
  202. return;
  203. for_each_compatible_node(np, NULL, "ucc_geth") {
  204. struct resource res;
  205. const uint32_t *iprop;
  206. uint32_t id;
  207. int ret;
  208. ret = of_address_to_resource(np, 0, &res);
  209. if (ret < 0) {
  210. pr_debug("fsl-pq-mdio: no address range in node %s\n",
  211. np->full_name);
  212. continue;
  213. }
  214. /* if our mdio regs fall within this UCC regs range */
  215. if ((start < res.start) || (end > res.end))
  216. continue;
  217. iprop = of_get_property(np, "cell-index", NULL);
  218. if (!iprop) {
  219. iprop = of_get_property(np, "device-id", NULL);
  220. if (!iprop) {
  221. pr_debug("fsl-pq-mdio: no UCC ID in node %s\n",
  222. np->full_name);
  223. continue;
  224. }
  225. }
  226. id = be32_to_cpup(iprop);
  227. /*
  228. * cell-index and device-id for QE nodes are
  229. * numbered from 1, not 0.
  230. */
  231. if (ucc_set_qe_mux_mii_mng(id - 1) < 0) {
  232. pr_debug("fsl-pq-mdio: invalid UCC ID in node %s\n",
  233. np->full_name);
  234. continue;
  235. }
  236. pr_debug("fsl-pq-mdio: setting node UCC%u to MII master\n", id);
  237. found_mii_master = true;
  238. }
  239. }
  240. #endif
  241. static struct of_device_id fsl_pq_mdio_match[] = {
  242. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  243. {
  244. .compatible = "fsl,gianfar-tbi",
  245. .data = &(struct fsl_pq_mdio_data) {
  246. .mii_offset = 0,
  247. .get_tbipa = get_gfar_tbipa,
  248. },
  249. },
  250. {
  251. .compatible = "fsl,gianfar-mdio",
  252. .data = &(struct fsl_pq_mdio_data) {
  253. .mii_offset = 0,
  254. .get_tbipa = get_gfar_tbipa,
  255. },
  256. },
  257. {
  258. .type = "mdio",
  259. .compatible = "gianfar",
  260. .data = &(struct fsl_pq_mdio_data) {
  261. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  262. .get_tbipa = get_gfar_tbipa,
  263. },
  264. },
  265. {
  266. .compatible = "fsl,etsec2-tbi",
  267. .data = &(struct fsl_pq_mdio_data) {
  268. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  269. .get_tbipa = get_etsec_tbipa,
  270. },
  271. },
  272. {
  273. .compatible = "fsl,etsec2-mdio",
  274. .data = &(struct fsl_pq_mdio_data) {
  275. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  276. .get_tbipa = get_etsec_tbipa,
  277. },
  278. },
  279. #endif
  280. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  281. {
  282. .compatible = "fsl,ucc-mdio",
  283. .data = &(struct fsl_pq_mdio_data) {
  284. .mii_offset = 0,
  285. .get_tbipa = get_ucc_tbipa,
  286. .ucc_configure = ucc_configure,
  287. },
  288. },
  289. {
  290. /* Legacy UCC MDIO node */
  291. .type = "mdio",
  292. .compatible = "ucc_geth_phy",
  293. .data = &(struct fsl_pq_mdio_data) {
  294. .mii_offset = 0,
  295. .get_tbipa = get_ucc_tbipa,
  296. .ucc_configure = ucc_configure,
  297. },
  298. },
  299. #endif
  300. /* No Kconfig option for Fman support yet */
  301. {
  302. .compatible = "fsl,fman-mdio",
  303. .data = &(struct fsl_pq_mdio_data) {
  304. .mii_offset = 0,
  305. /* Fman TBI operations are handled elsewhere */
  306. },
  307. },
  308. {},
  309. };
  310. MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
  311. static int fsl_pq_mdio_probe(struct platform_device *pdev)
  312. {
  313. const struct of_device_id *id =
  314. of_match_device(fsl_pq_mdio_match, &pdev->dev);
  315. const struct fsl_pq_mdio_data *data = id->data;
  316. struct device_node *np = pdev->dev.of_node;
  317. struct resource res;
  318. struct device_node *tbi;
  319. struct fsl_pq_mdio_priv *priv;
  320. struct mii_bus *new_bus;
  321. int err;
  322. dev_dbg(&pdev->dev, "found %s compatible node\n", id->compatible);
  323. new_bus = mdiobus_alloc_size(sizeof(*priv));
  324. if (!new_bus)
  325. return -ENOMEM;
  326. priv = new_bus->priv;
  327. new_bus->name = "Freescale PowerQUICC MII Bus",
  328. new_bus->read = &fsl_pq_mdio_read;
  329. new_bus->write = &fsl_pq_mdio_write;
  330. new_bus->reset = &fsl_pq_mdio_reset;
  331. new_bus->irq = priv->irqs;
  332. err = of_address_to_resource(np, 0, &res);
  333. if (err < 0) {
  334. dev_err(&pdev->dev, "could not obtain address information\n");
  335. goto error;
  336. }
  337. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s@%llx", np->name,
  338. (unsigned long long)res.start);
  339. priv->map = of_iomap(np, 0);
  340. if (!priv->map) {
  341. err = -ENOMEM;
  342. goto error;
  343. }
  344. /*
  345. * Some device tree nodes represent only the MII registers, and
  346. * others represent the MAC and MII registers. The 'mii_offset' field
  347. * contains the offset of the MII registers inside the mapped register
  348. * space.
  349. */
  350. if (data->mii_offset > resource_size(&res)) {
  351. dev_err(&pdev->dev, "invalid register map\n");
  352. err = -EINVAL;
  353. goto error;
  354. }
  355. priv->regs = priv->map + data->mii_offset;
  356. new_bus->parent = &pdev->dev;
  357. dev_set_drvdata(&pdev->dev, new_bus);
  358. if (data->get_tbipa) {
  359. for_each_child_of_node(np, tbi) {
  360. if (strcmp(tbi->type, "tbi-phy") == 0) {
  361. dev_dbg(&pdev->dev, "found TBI PHY node %s\n",
  362. strrchr(tbi->full_name, '/') + 1);
  363. break;
  364. }
  365. }
  366. if (tbi) {
  367. const u32 *prop = of_get_property(tbi, "reg", NULL);
  368. uint32_t __iomem *tbipa;
  369. if (!prop) {
  370. dev_err(&pdev->dev,
  371. "missing 'reg' property in node %s\n",
  372. tbi->full_name);
  373. err = -EBUSY;
  374. goto error;
  375. }
  376. tbipa = data->get_tbipa(priv->map);
  377. out_be32(tbipa, be32_to_cpup(prop));
  378. }
  379. }
  380. if (data->ucc_configure)
  381. data->ucc_configure(res.start, res.end);
  382. err = of_mdiobus_register(new_bus, np);
  383. if (err) {
  384. dev_err(&pdev->dev, "cannot register %s as MDIO bus\n",
  385. new_bus->name);
  386. goto error;
  387. }
  388. return 0;
  389. error:
  390. if (priv->map)
  391. iounmap(priv->map);
  392. kfree(new_bus);
  393. return err;
  394. }
  395. static int fsl_pq_mdio_remove(struct platform_device *pdev)
  396. {
  397. struct device *device = &pdev->dev;
  398. struct mii_bus *bus = dev_get_drvdata(device);
  399. struct fsl_pq_mdio_priv *priv = bus->priv;
  400. mdiobus_unregister(bus);
  401. dev_set_drvdata(device, NULL);
  402. iounmap(priv->map);
  403. mdiobus_free(bus);
  404. return 0;
  405. }
  406. static struct platform_driver fsl_pq_mdio_driver = {
  407. .driver = {
  408. .name = "fsl-pq_mdio",
  409. .owner = THIS_MODULE,
  410. .of_match_table = fsl_pq_mdio_match,
  411. },
  412. .probe = fsl_pq_mdio_probe,
  413. .remove = fsl_pq_mdio_remove,
  414. };
  415. module_platform_driver(fsl_pq_mdio_driver);
  416. MODULE_LICENSE("GPL");