fsl_pq_mdio.c 13 KB

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