fsl_pq_mdio.c 12 KB

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