fsl_pq_mdio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. * Return the TBIPA address, starting from the address
  171. * of the mapped GFAR MDIO registers (struct gfar)
  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. static uint32_t __iomem *get_gfar_tbipa_from_mdio(void __iomem *p)
  177. {
  178. struct gfar __iomem *enet_regs = p;
  179. return &enet_regs->tbipa;
  180. }
  181. /*
  182. * Return the TBIPA address, starting from the address
  183. * of the mapped GFAR MII registers (gfar_mii_regs[] within struct gfar)
  184. */
  185. static uint32_t __iomem *get_gfar_tbipa_from_mii(void __iomem *p)
  186. {
  187. return get_gfar_tbipa_from_mdio(container_of(p, struct gfar, gfar_mii_regs));
  188. }
  189. /*
  190. * Return the TBIPAR address for an eTSEC2 node
  191. */
  192. static uint32_t __iomem *get_etsec_tbipa(void __iomem *p)
  193. {
  194. return p;
  195. }
  196. #endif
  197. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  198. /*
  199. * Return the TBIPAR address for a QE MDIO node, starting from the address
  200. * of the mapped MII registers (struct fsl_pq_mii)
  201. */
  202. static uint32_t __iomem *get_ucc_tbipa(void __iomem *p)
  203. {
  204. struct fsl_pq_mdio __iomem *mdio = container_of(p, struct fsl_pq_mdio, mii);
  205. return &mdio->utbipar;
  206. }
  207. /*
  208. * Find the UCC node that controls the given MDIO node
  209. *
  210. * For some reason, the QE MDIO nodes are not children of the UCC devices
  211. * that control them. Therefore, we need to scan all UCC nodes looking for
  212. * the one that encompases the given MDIO node. We do this by comparing
  213. * physical addresses. The 'start' and 'end' addresses of the MDIO node are
  214. * passed, and the correct UCC node will cover the entire address range.
  215. *
  216. * This assumes that there is only one QE MDIO node in the entire device tree.
  217. */
  218. static void ucc_configure(phys_addr_t start, phys_addr_t end)
  219. {
  220. static bool found_mii_master;
  221. struct device_node *np = NULL;
  222. if (found_mii_master)
  223. return;
  224. for_each_compatible_node(np, NULL, "ucc_geth") {
  225. struct resource res;
  226. const uint32_t *iprop;
  227. uint32_t id;
  228. int ret;
  229. ret = of_address_to_resource(np, 0, &res);
  230. if (ret < 0) {
  231. pr_debug("fsl-pq-mdio: no address range in node %s\n",
  232. np->full_name);
  233. continue;
  234. }
  235. /* if our mdio regs fall within this UCC regs range */
  236. if ((start < res.start) || (end > res.end))
  237. continue;
  238. iprop = of_get_property(np, "cell-index", NULL);
  239. if (!iprop) {
  240. iprop = of_get_property(np, "device-id", NULL);
  241. if (!iprop) {
  242. pr_debug("fsl-pq-mdio: no UCC ID in node %s\n",
  243. np->full_name);
  244. continue;
  245. }
  246. }
  247. id = be32_to_cpup(iprop);
  248. /*
  249. * cell-index and device-id for QE nodes are
  250. * numbered from 1, not 0.
  251. */
  252. if (ucc_set_qe_mux_mii_mng(id - 1) < 0) {
  253. pr_debug("fsl-pq-mdio: invalid UCC ID in node %s\n",
  254. np->full_name);
  255. continue;
  256. }
  257. pr_debug("fsl-pq-mdio: setting node UCC%u to MII master\n", id);
  258. found_mii_master = true;
  259. }
  260. }
  261. #endif
  262. static const struct of_device_id fsl_pq_mdio_match[] = {
  263. #if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
  264. {
  265. .compatible = "fsl,gianfar-tbi",
  266. .data = &(struct fsl_pq_mdio_data) {
  267. .mii_offset = 0,
  268. .get_tbipa = get_gfar_tbipa_from_mii,
  269. },
  270. },
  271. {
  272. .compatible = "fsl,gianfar-mdio",
  273. .data = &(struct fsl_pq_mdio_data) {
  274. .mii_offset = 0,
  275. .get_tbipa = get_gfar_tbipa_from_mii,
  276. },
  277. },
  278. {
  279. .type = "mdio",
  280. .compatible = "gianfar",
  281. .data = &(struct fsl_pq_mdio_data) {
  282. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  283. .get_tbipa = get_gfar_tbipa_from_mdio,
  284. },
  285. },
  286. {
  287. .compatible = "fsl,etsec2-tbi",
  288. .data = &(struct fsl_pq_mdio_data) {
  289. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  290. .get_tbipa = get_etsec_tbipa,
  291. },
  292. },
  293. {
  294. .compatible = "fsl,etsec2-mdio",
  295. .data = &(struct fsl_pq_mdio_data) {
  296. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  297. .get_tbipa = get_etsec_tbipa,
  298. },
  299. },
  300. #endif
  301. #if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
  302. {
  303. .compatible = "fsl,ucc-mdio",
  304. .data = &(struct fsl_pq_mdio_data) {
  305. .mii_offset = 0,
  306. .get_tbipa = get_ucc_tbipa,
  307. .ucc_configure = ucc_configure,
  308. },
  309. },
  310. {
  311. /* Legacy UCC MDIO node */
  312. .type = "mdio",
  313. .compatible = "ucc_geth_phy",
  314. .data = &(struct fsl_pq_mdio_data) {
  315. .mii_offset = 0,
  316. .get_tbipa = get_ucc_tbipa,
  317. .ucc_configure = ucc_configure,
  318. },
  319. },
  320. #endif
  321. /* No Kconfig option for Fman support yet */
  322. {
  323. .compatible = "fsl,fman-mdio",
  324. .data = &(struct fsl_pq_mdio_data) {
  325. .mii_offset = 0,
  326. /* Fman TBI operations are handled elsewhere */
  327. },
  328. },
  329. {},
  330. };
  331. MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
  332. static int fsl_pq_mdio_probe(struct platform_device *pdev)
  333. {
  334. const struct of_device_id *id =
  335. of_match_device(fsl_pq_mdio_match, &pdev->dev);
  336. const struct fsl_pq_mdio_data *data;
  337. struct device_node *np = pdev->dev.of_node;
  338. struct resource res;
  339. struct device_node *tbi;
  340. struct fsl_pq_mdio_priv *priv;
  341. struct mii_bus *new_bus;
  342. int err;
  343. if (!id) {
  344. dev_err(&pdev->dev, "Failed to match device\n");
  345. return -ENODEV;
  346. }
  347. data = id->data;
  348. dev_dbg(&pdev->dev, "found %s compatible node\n", id->compatible);
  349. new_bus = mdiobus_alloc_size(sizeof(*priv));
  350. if (!new_bus)
  351. return -ENOMEM;
  352. priv = new_bus->priv;
  353. new_bus->name = "Freescale PowerQUICC MII Bus",
  354. new_bus->read = &fsl_pq_mdio_read;
  355. new_bus->write = &fsl_pq_mdio_write;
  356. new_bus->reset = &fsl_pq_mdio_reset;
  357. new_bus->irq = priv->irqs;
  358. err = of_address_to_resource(np, 0, &res);
  359. if (err < 0) {
  360. dev_err(&pdev->dev, "could not obtain address information\n");
  361. goto error;
  362. }
  363. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s@%llx", np->name,
  364. (unsigned long long)res.start);
  365. priv->map = of_iomap(np, 0);
  366. if (!priv->map) {
  367. err = -ENOMEM;
  368. goto error;
  369. }
  370. /*
  371. * Some device tree nodes represent only the MII registers, and
  372. * others represent the MAC and MII registers. The 'mii_offset' field
  373. * contains the offset of the MII registers inside the mapped register
  374. * space.
  375. */
  376. if (data->mii_offset > resource_size(&res)) {
  377. dev_err(&pdev->dev, "invalid register map\n");
  378. err = -EINVAL;
  379. goto error;
  380. }
  381. priv->regs = priv->map + data->mii_offset;
  382. new_bus->parent = &pdev->dev;
  383. platform_set_drvdata(pdev, new_bus);
  384. if (data->get_tbipa) {
  385. for_each_child_of_node(np, tbi) {
  386. if (strcmp(tbi->type, "tbi-phy") == 0) {
  387. dev_dbg(&pdev->dev, "found TBI PHY node %s\n",
  388. strrchr(tbi->full_name, '/') + 1);
  389. break;
  390. }
  391. }
  392. if (tbi) {
  393. const u32 *prop = of_get_property(tbi, "reg", NULL);
  394. uint32_t __iomem *tbipa;
  395. if (!prop) {
  396. dev_err(&pdev->dev,
  397. "missing 'reg' property in node %s\n",
  398. tbi->full_name);
  399. err = -EBUSY;
  400. goto error;
  401. }
  402. tbipa = data->get_tbipa(priv->map);
  403. /*
  404. * Add consistency check to make sure TBI is contained
  405. * within the mapped range (not because we would get a
  406. * segfault, rather to catch bugs in computing TBI
  407. * address). Print error message but continue anyway.
  408. */
  409. if ((void *)tbipa > priv->map + resource_size(&res) - 4)
  410. dev_err(&pdev->dev, "invalid register map (should be at least 0x%04zx to contain TBI address)\n",
  411. ((void *)tbipa - priv->map) + 4);
  412. iowrite32be(be32_to_cpup(prop), tbipa);
  413. }
  414. }
  415. if (data->ucc_configure)
  416. data->ucc_configure(res.start, res.end);
  417. err = of_mdiobus_register(new_bus, np);
  418. if (err) {
  419. dev_err(&pdev->dev, "cannot register %s as MDIO bus\n",
  420. new_bus->name);
  421. goto error;
  422. }
  423. return 0;
  424. error:
  425. if (priv->map)
  426. iounmap(priv->map);
  427. kfree(new_bus);
  428. return err;
  429. }
  430. static int fsl_pq_mdio_remove(struct platform_device *pdev)
  431. {
  432. struct device *device = &pdev->dev;
  433. struct mii_bus *bus = dev_get_drvdata(device);
  434. struct fsl_pq_mdio_priv *priv = bus->priv;
  435. mdiobus_unregister(bus);
  436. iounmap(priv->map);
  437. mdiobus_free(bus);
  438. return 0;
  439. }
  440. static struct platform_driver fsl_pq_mdio_driver = {
  441. .driver = {
  442. .name = "fsl-pq_mdio",
  443. .of_match_table = fsl_pq_mdio_match,
  444. },
  445. .probe = fsl_pq_mdio_probe,
  446. .remove = fsl_pq_mdio_remove,
  447. };
  448. module_platform_driver(fsl_pq_mdio_driver);
  449. MODULE_LICENSE("GPL");