fsl_pq_mdio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <soc/fsl/qe/ucc.h>
  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. };
  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. unsigned int timeout;
  96. /* Set the PHY address and the register address we want to write */
  97. iowrite32be((mii_id << 8) | regnum, &regs->miimadd);
  98. /* Write out the value we want */
  99. iowrite32be(value, &regs->miimcon);
  100. /* Wait for the transaction to finish */
  101. timeout = MII_TIMEOUT;
  102. while ((ioread32be(&regs->miimind) & MIIMIND_BUSY) && timeout) {
  103. cpu_relax();
  104. timeout--;
  105. }
  106. return timeout ? 0 : -ETIMEDOUT;
  107. }
  108. /*
  109. * Read the bus for PHY at addr mii_id, register regnum, and return the value.
  110. * Clears miimcom first.
  111. *
  112. * All PHY operation done on the bus attached to the local interface, which
  113. * may be different from the generic mdio bus. This is helpful in programming
  114. * interfaces like the TBI which, in turn, control interfaces like on-chip
  115. * SERDES and are always tied to the local mdio pins, which may not be the
  116. * same as system mdio bus, used for controlling the external PHYs, for eg.
  117. */
  118. static int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  119. {
  120. struct fsl_pq_mdio_priv *priv = bus->priv;
  121. struct fsl_pq_mii __iomem *regs = priv->regs;
  122. unsigned int timeout;
  123. u16 value;
  124. /* Set the PHY address and the register address we want to read */
  125. iowrite32be((mii_id << 8) | regnum, &regs->miimadd);
  126. /* Clear miimcom, and then initiate a read */
  127. iowrite32be(0, &regs->miimcom);
  128. iowrite32be(MII_READ_COMMAND, &regs->miimcom);
  129. /* Wait for the transaction to finish, normally less than 100us */
  130. timeout = MII_TIMEOUT;
  131. while ((ioread32be(&regs->miimind) &
  132. (MIIMIND_NOTVALID | MIIMIND_BUSY)) && timeout) {
  133. cpu_relax();
  134. timeout--;
  135. }
  136. if (!timeout)
  137. return -ETIMEDOUT;
  138. /* Grab the value of the register from miimstat */
  139. value = ioread32be(&regs->miimstat);
  140. dev_dbg(&bus->dev, "read %04x from address %x/%x\n", value, mii_id, regnum);
  141. return value;
  142. }
  143. /* Reset the MIIM registers, and wait for the bus to free */
  144. static int fsl_pq_mdio_reset(struct mii_bus *bus)
  145. {
  146. struct fsl_pq_mdio_priv *priv = bus->priv;
  147. struct fsl_pq_mii __iomem *regs = priv->regs;
  148. unsigned int timeout;
  149. mutex_lock(&bus->mdio_lock);
  150. /* Reset the management interface */
  151. iowrite32be(MIIMCFG_RESET, &regs->miimcfg);
  152. /* Setup the MII Mgmt clock speed */
  153. iowrite32be(MIIMCFG_INIT_VALUE, &regs->miimcfg);
  154. /* Wait until the bus is free */
  155. timeout = MII_TIMEOUT;
  156. while ((ioread32be(&regs->miimind) & MIIMIND_BUSY) && timeout) {
  157. cpu_relax();
  158. timeout--;
  159. }
  160. mutex_unlock(&bus->mdio_lock);
  161. if (!timeout) {
  162. dev_err(&bus->dev, "timeout waiting for MII bus\n");
  163. return -EBUSY;
  164. }
  165. return 0;
  166. }
  167. #if IS_ENABLED(CONFIG_GIANFAR)
  168. /*
  169. * Return the TBIPA address, starting from the address
  170. * of the mapped GFAR MDIO registers (struct gfar)
  171. * This is mildly evil, but so is our hardware for doing this.
  172. * Also, we have to cast back to struct gfar because of
  173. * definition weirdness done in gianfar.h.
  174. */
  175. static uint32_t __iomem *get_gfar_tbipa_from_mdio(void __iomem *p)
  176. {
  177. struct gfar __iomem *enet_regs = p;
  178. return &enet_regs->tbipa;
  179. }
  180. /*
  181. * Return the TBIPA address, starting from the address
  182. * of the mapped GFAR MII registers (gfar_mii_regs[] within struct gfar)
  183. */
  184. static uint32_t __iomem *get_gfar_tbipa_from_mii(void __iomem *p)
  185. {
  186. return get_gfar_tbipa_from_mdio(container_of(p, struct gfar, gfar_mii_regs));
  187. }
  188. /*
  189. * Return the TBIPAR address for an eTSEC2 node
  190. */
  191. static uint32_t __iomem *get_etsec_tbipa(void __iomem *p)
  192. {
  193. return p;
  194. }
  195. #endif
  196. #if IS_ENABLED(CONFIG_UCC_GETH)
  197. /*
  198. * Return the TBIPAR address for a QE MDIO node, starting from the address
  199. * of the mapped MII registers (struct fsl_pq_mii)
  200. */
  201. static uint32_t __iomem *get_ucc_tbipa(void __iomem *p)
  202. {
  203. struct fsl_pq_mdio __iomem *mdio = container_of(p, struct fsl_pq_mdio, mii);
  204. return &mdio->utbipar;
  205. }
  206. /*
  207. * Find the UCC node that controls the given MDIO node
  208. *
  209. * For some reason, the QE MDIO nodes are not children of the UCC devices
  210. * that control them. Therefore, we need to scan all UCC nodes looking for
  211. * the one that encompases the given MDIO node. We do this by comparing
  212. * physical addresses. The 'start' and 'end' addresses of the MDIO node are
  213. * passed, and the correct UCC node will cover the entire address range.
  214. *
  215. * This assumes that there is only one QE MDIO node in the entire device tree.
  216. */
  217. static void ucc_configure(phys_addr_t start, phys_addr_t end)
  218. {
  219. static bool found_mii_master;
  220. struct device_node *np = NULL;
  221. if (found_mii_master)
  222. return;
  223. for_each_compatible_node(np, NULL, "ucc_geth") {
  224. struct resource res;
  225. const uint32_t *iprop;
  226. uint32_t id;
  227. int ret;
  228. ret = of_address_to_resource(np, 0, &res);
  229. if (ret < 0) {
  230. pr_debug("fsl-pq-mdio: no address range in node %pOF\n",
  231. np);
  232. continue;
  233. }
  234. /* if our mdio regs fall within this UCC regs range */
  235. if ((start < res.start) || (end > res.end))
  236. continue;
  237. iprop = of_get_property(np, "cell-index", NULL);
  238. if (!iprop) {
  239. iprop = of_get_property(np, "device-id", NULL);
  240. if (!iprop) {
  241. pr_debug("fsl-pq-mdio: no UCC ID in node %pOF\n",
  242. np);
  243. continue;
  244. }
  245. }
  246. id = be32_to_cpup(iprop);
  247. /*
  248. * cell-index and device-id for QE nodes are
  249. * numbered from 1, not 0.
  250. */
  251. if (ucc_set_qe_mux_mii_mng(id - 1) < 0) {
  252. pr_debug("fsl-pq-mdio: invalid UCC ID in node %pOF\n",
  253. np);
  254. continue;
  255. }
  256. pr_debug("fsl-pq-mdio: setting node UCC%u to MII master\n", id);
  257. found_mii_master = true;
  258. }
  259. }
  260. #endif
  261. static const struct of_device_id fsl_pq_mdio_match[] = {
  262. #if IS_ENABLED(CONFIG_GIANFAR)
  263. {
  264. .compatible = "fsl,gianfar-tbi",
  265. .data = &(struct fsl_pq_mdio_data) {
  266. .mii_offset = 0,
  267. .get_tbipa = get_gfar_tbipa_from_mii,
  268. },
  269. },
  270. {
  271. .compatible = "fsl,gianfar-mdio",
  272. .data = &(struct fsl_pq_mdio_data) {
  273. .mii_offset = 0,
  274. .get_tbipa = get_gfar_tbipa_from_mii,
  275. },
  276. },
  277. {
  278. .type = "mdio",
  279. .compatible = "gianfar",
  280. .data = &(struct fsl_pq_mdio_data) {
  281. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  282. .get_tbipa = get_gfar_tbipa_from_mdio,
  283. },
  284. },
  285. {
  286. .compatible = "fsl,etsec2-tbi",
  287. .data = &(struct fsl_pq_mdio_data) {
  288. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  289. .get_tbipa = get_etsec_tbipa,
  290. },
  291. },
  292. {
  293. .compatible = "fsl,etsec2-mdio",
  294. .data = &(struct fsl_pq_mdio_data) {
  295. .mii_offset = offsetof(struct fsl_pq_mdio, mii),
  296. .get_tbipa = get_etsec_tbipa,
  297. },
  298. },
  299. #endif
  300. #if IS_ENABLED(CONFIG_UCC_GETH)
  301. {
  302. .compatible = "fsl,ucc-mdio",
  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. {
  310. /* Legacy UCC MDIO node */
  311. .type = "mdio",
  312. .compatible = "ucc_geth_phy",
  313. .data = &(struct fsl_pq_mdio_data) {
  314. .mii_offset = 0,
  315. .get_tbipa = get_ucc_tbipa,
  316. .ucc_configure = ucc_configure,
  317. },
  318. },
  319. #endif
  320. /* No Kconfig option for Fman support yet */
  321. {
  322. .compatible = "fsl,fman-mdio",
  323. .data = &(struct fsl_pq_mdio_data) {
  324. .mii_offset = 0,
  325. /* Fman TBI operations are handled elsewhere */
  326. },
  327. },
  328. {},
  329. };
  330. MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
  331. static void set_tbipa(const u32 tbipa_val, struct platform_device *pdev,
  332. uint32_t __iomem * (*get_tbipa)(void __iomem *),
  333. void __iomem *reg_map, struct resource *reg_res)
  334. {
  335. struct device_node *np = pdev->dev.of_node;
  336. uint32_t __iomem *tbipa;
  337. bool tbipa_mapped;
  338. tbipa = of_iomap(np, 1);
  339. if (tbipa) {
  340. tbipa_mapped = true;
  341. } else {
  342. tbipa_mapped = false;
  343. tbipa = (*get_tbipa)(reg_map);
  344. /*
  345. * Add consistency check to make sure TBI is contained within
  346. * the mapped range (not because we would get a segfault,
  347. * rather to catch bugs in computing TBI address). Print error
  348. * message but continue anyway.
  349. */
  350. if ((void *)tbipa > reg_map + resource_size(reg_res) - 4)
  351. dev_err(&pdev->dev, "invalid register map (should be at least 0x%04zx to contain TBI address)\n",
  352. ((void *)tbipa - reg_map) + 4);
  353. }
  354. iowrite32be(be32_to_cpu(tbipa_val), tbipa);
  355. if (tbipa_mapped)
  356. iounmap(tbipa);
  357. }
  358. static int fsl_pq_mdio_probe(struct platform_device *pdev)
  359. {
  360. const struct of_device_id *id =
  361. of_match_device(fsl_pq_mdio_match, &pdev->dev);
  362. const struct fsl_pq_mdio_data *data;
  363. struct device_node *np = pdev->dev.of_node;
  364. struct resource res;
  365. struct device_node *tbi;
  366. struct fsl_pq_mdio_priv *priv;
  367. struct mii_bus *new_bus;
  368. int err;
  369. if (!id) {
  370. dev_err(&pdev->dev, "Failed to match device\n");
  371. return -ENODEV;
  372. }
  373. data = id->data;
  374. dev_dbg(&pdev->dev, "found %s compatible node\n", id->compatible);
  375. new_bus = mdiobus_alloc_size(sizeof(*priv));
  376. if (!new_bus)
  377. return -ENOMEM;
  378. priv = new_bus->priv;
  379. new_bus->name = "Freescale PowerQUICC MII Bus",
  380. new_bus->read = &fsl_pq_mdio_read;
  381. new_bus->write = &fsl_pq_mdio_write;
  382. new_bus->reset = &fsl_pq_mdio_reset;
  383. err = of_address_to_resource(np, 0, &res);
  384. if (err < 0) {
  385. dev_err(&pdev->dev, "could not obtain address information\n");
  386. goto error;
  387. }
  388. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s@%llx", np->name,
  389. (unsigned long long)res.start);
  390. priv->map = of_iomap(np, 0);
  391. if (!priv->map) {
  392. err = -ENOMEM;
  393. goto error;
  394. }
  395. /*
  396. * Some device tree nodes represent only the MII registers, and
  397. * others represent the MAC and MII registers. The 'mii_offset' field
  398. * contains the offset of the MII registers inside the mapped register
  399. * space.
  400. */
  401. if (data->mii_offset > resource_size(&res)) {
  402. dev_err(&pdev->dev, "invalid register map\n");
  403. err = -EINVAL;
  404. goto error;
  405. }
  406. priv->regs = priv->map + data->mii_offset;
  407. new_bus->parent = &pdev->dev;
  408. platform_set_drvdata(pdev, new_bus);
  409. if (data->get_tbipa) {
  410. for_each_child_of_node(np, tbi) {
  411. if (strcmp(tbi->type, "tbi-phy") == 0) {
  412. dev_dbg(&pdev->dev, "found TBI PHY node %pOFP\n",
  413. tbi);
  414. break;
  415. }
  416. }
  417. if (tbi) {
  418. const u32 *prop = of_get_property(tbi, "reg", NULL);
  419. if (!prop) {
  420. dev_err(&pdev->dev,
  421. "missing 'reg' property in node %pOF\n",
  422. tbi);
  423. err = -EBUSY;
  424. goto error;
  425. }
  426. set_tbipa(*prop, pdev,
  427. data->get_tbipa, priv->map, &res);
  428. }
  429. }
  430. if (data->ucc_configure)
  431. data->ucc_configure(res.start, res.end);
  432. err = of_mdiobus_register(new_bus, np);
  433. if (err) {
  434. dev_err(&pdev->dev, "cannot register %s as MDIO bus\n",
  435. new_bus->name);
  436. goto error;
  437. }
  438. return 0;
  439. error:
  440. if (priv->map)
  441. iounmap(priv->map);
  442. kfree(new_bus);
  443. return err;
  444. }
  445. static int fsl_pq_mdio_remove(struct platform_device *pdev)
  446. {
  447. struct device *device = &pdev->dev;
  448. struct mii_bus *bus = dev_get_drvdata(device);
  449. struct fsl_pq_mdio_priv *priv = bus->priv;
  450. mdiobus_unregister(bus);
  451. iounmap(priv->map);
  452. mdiobus_free(bus);
  453. return 0;
  454. }
  455. static struct platform_driver fsl_pq_mdio_driver = {
  456. .driver = {
  457. .name = "fsl-pq_mdio",
  458. .of_match_table = fsl_pq_mdio_match,
  459. },
  460. .probe = fsl_pq_mdio_probe,
  461. .remove = fsl_pq_mdio_remove,
  462. };
  463. module_platform_driver(fsl_pq_mdio_driver);
  464. MODULE_LICENSE("GPL");