sxgbe_mdio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* 10G controller driver for Samsung SoCs
  2. *
  3. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Siva Reddy Kallam <siva.kallam@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/io.h>
  14. #include <linux/mii.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/phy.h>
  18. #include <linux/slab.h>
  19. #include <linux/sxgbe_platform.h>
  20. #include "sxgbe_common.h"
  21. #include "sxgbe_reg.h"
  22. #define SXGBE_SMA_WRITE_CMD 0x01 /* write command */
  23. #define SXGBE_SMA_PREAD_CMD 0x02 /* post read increament address */
  24. #define SXGBE_SMA_READ_CMD 0x03 /* read command */
  25. #define SXGBE_SMA_SKIP_ADDRFRM 0x00040000 /* skip the address frame */
  26. #define SXGBE_MII_BUSY 0x00400000 /* mii busy */
  27. static int sxgbe_mdio_busy_wait(void __iomem *ioaddr, unsigned int mii_data)
  28. {
  29. unsigned long fin_time = jiffies + 3 * HZ; /* 3 seconds */
  30. while (!time_after(jiffies, fin_time)) {
  31. if (!(readl(ioaddr + mii_data) & SXGBE_MII_BUSY))
  32. return 0;
  33. cpu_relax();
  34. }
  35. return -EBUSY;
  36. }
  37. static void sxgbe_mdio_ctrl_data(struct sxgbe_priv_data *sp, u32 cmd,
  38. u16 phydata)
  39. {
  40. u32 reg = phydata;
  41. reg |= (cmd << 16) | SXGBE_SMA_SKIP_ADDRFRM |
  42. ((sp->clk_csr & 0x7) << 19) | SXGBE_MII_BUSY;
  43. writel(reg, sp->ioaddr + sp->hw->mii.data);
  44. }
  45. static void sxgbe_mdio_c45(struct sxgbe_priv_data *sp, u32 cmd, int phyaddr,
  46. int phyreg, u16 phydata)
  47. {
  48. u32 reg;
  49. /* set mdio address register */
  50. reg = ((phyreg >> 16) & 0x1f) << 21;
  51. reg |= (phyaddr << 16) | (phyreg & 0xffff);
  52. writel(reg, sp->ioaddr + sp->hw->mii.addr);
  53. sxgbe_mdio_ctrl_data(sp, cmd, phydata);
  54. }
  55. static void sxgbe_mdio_c22(struct sxgbe_priv_data *sp, u32 cmd, int phyaddr,
  56. int phyreg, u16 phydata)
  57. {
  58. u32 reg;
  59. writel(1 << phyaddr, sp->ioaddr + SXGBE_MDIO_CLAUSE22_PORT_REG);
  60. /* set mdio address register */
  61. reg = (phyaddr << 16) | (phyreg & 0x1f);
  62. writel(reg, sp->ioaddr + sp->hw->mii.addr);
  63. sxgbe_mdio_ctrl_data(sp, cmd, phydata);
  64. }
  65. static int sxgbe_mdio_access(struct sxgbe_priv_data *sp, u32 cmd, int phyaddr,
  66. int phyreg, u16 phydata)
  67. {
  68. const struct mii_regs *mii = &sp->hw->mii;
  69. int rc;
  70. rc = sxgbe_mdio_busy_wait(sp->ioaddr, mii->data);
  71. if (rc < 0)
  72. return rc;
  73. if (phyreg & MII_ADDR_C45) {
  74. sxgbe_mdio_c45(sp, cmd, phyaddr, phyreg, phydata);
  75. } else {
  76. /* Ports 0-3 only support C22. */
  77. if (phyaddr >= 4)
  78. return -ENODEV;
  79. sxgbe_mdio_c22(sp, cmd, phyaddr, phyreg, phydata);
  80. }
  81. return sxgbe_mdio_busy_wait(sp->ioaddr, mii->data);
  82. }
  83. /**
  84. * sxgbe_mdio_read
  85. * @bus: points to the mii_bus structure
  86. * @phyaddr: address of phy port
  87. * @phyreg: address of register with in phy register
  88. * Description: this function used for C45 and C22 MDIO Read
  89. */
  90. static int sxgbe_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
  91. {
  92. struct net_device *ndev = bus->priv;
  93. struct sxgbe_priv_data *priv = netdev_priv(ndev);
  94. int rc;
  95. rc = sxgbe_mdio_access(priv, SXGBE_SMA_READ_CMD, phyaddr, phyreg, 0);
  96. if (rc < 0)
  97. return rc;
  98. return readl(priv->ioaddr + priv->hw->mii.data) & 0xffff;
  99. }
  100. /**
  101. * sxgbe_mdio_write
  102. * @bus: points to the mii_bus structure
  103. * @phyaddr: address of phy port
  104. * @phyreg: address of phy registers
  105. * @phydata: data to be written into phy register
  106. * Description: this function is used for C45 and C22 MDIO write
  107. */
  108. static int sxgbe_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
  109. u16 phydata)
  110. {
  111. struct net_device *ndev = bus->priv;
  112. struct sxgbe_priv_data *priv = netdev_priv(ndev);
  113. return sxgbe_mdio_access(priv, SXGBE_SMA_WRITE_CMD, phyaddr, phyreg,
  114. phydata);
  115. }
  116. int sxgbe_mdio_register(struct net_device *ndev)
  117. {
  118. struct mii_bus *mdio_bus;
  119. struct sxgbe_priv_data *priv = netdev_priv(ndev);
  120. struct sxgbe_mdio_bus_data *mdio_data = priv->plat->mdio_bus_data;
  121. int err, phy_addr;
  122. int *irqlist;
  123. bool phy_found = false;
  124. bool act;
  125. /* allocate the new mdio bus */
  126. mdio_bus = mdiobus_alloc();
  127. if (!mdio_bus) {
  128. netdev_err(ndev, "%s: mii bus allocation failed\n", __func__);
  129. return -ENOMEM;
  130. }
  131. if (mdio_data->irqs)
  132. irqlist = mdio_data->irqs;
  133. else
  134. irqlist = priv->mii_irq;
  135. /* assign mii bus fields */
  136. mdio_bus->name = "sxgbe";
  137. mdio_bus->read = &sxgbe_mdio_read;
  138. mdio_bus->write = &sxgbe_mdio_write;
  139. snprintf(mdio_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  140. mdio_bus->name, priv->plat->bus_id);
  141. mdio_bus->priv = ndev;
  142. mdio_bus->phy_mask = mdio_data->phy_mask;
  143. mdio_bus->parent = priv->device;
  144. /* register with kernel subsystem */
  145. err = mdiobus_register(mdio_bus);
  146. if (err != 0) {
  147. netdev_err(ndev, "mdiobus register failed\n");
  148. goto mdiobus_err;
  149. }
  150. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  151. struct phy_device *phy = mdio_bus->phy_map[phy_addr];
  152. if (phy) {
  153. char irq_num[4];
  154. char *irq_str;
  155. /* If an IRQ was provided to be assigned after
  156. * the bus probe, do it here.
  157. */
  158. if ((mdio_data->irqs == NULL) &&
  159. (mdio_data->probed_phy_irq > 0)) {
  160. irqlist[phy_addr] = mdio_data->probed_phy_irq;
  161. phy->irq = mdio_data->probed_phy_irq;
  162. }
  163. /* If we're going to bind the MAC to this PHY bus,
  164. * and no PHY number was provided to the MAC,
  165. * use the one probed here.
  166. */
  167. if (priv->plat->phy_addr == -1)
  168. priv->plat->phy_addr = phy_addr;
  169. act = (priv->plat->phy_addr == phy_addr);
  170. switch (phy->irq) {
  171. case PHY_POLL:
  172. irq_str = "POLL";
  173. break;
  174. case PHY_IGNORE_INTERRUPT:
  175. irq_str = "IGNORE";
  176. break;
  177. default:
  178. sprintf(irq_num, "%d", phy->irq);
  179. irq_str = irq_num;
  180. break;
  181. }
  182. netdev_info(ndev, "PHY ID %08x at %d IRQ %s (%s)%s\n",
  183. phy->phy_id, phy_addr, irq_str,
  184. dev_name(&phy->dev), act ? " active" : "");
  185. phy_found = true;
  186. }
  187. }
  188. if (!phy_found) {
  189. netdev_err(ndev, "PHY not found\n");
  190. goto phyfound_err;
  191. }
  192. priv->mii = mdio_bus;
  193. return 0;
  194. phyfound_err:
  195. err = -ENODEV;
  196. mdiobus_unregister(mdio_bus);
  197. mdiobus_err:
  198. mdiobus_free(mdio_bus);
  199. return err;
  200. }
  201. int sxgbe_mdio_unregister(struct net_device *ndev)
  202. {
  203. struct sxgbe_priv_data *priv = netdev_priv(ndev);
  204. if (!priv->mii)
  205. return 0;
  206. mdiobus_unregister(priv->mii);
  207. priv->mii->priv = NULL;
  208. mdiobus_free(priv->mii);
  209. priv->mii = NULL;
  210. return 0;
  211. }