700-net-add-qualcomm-mdio.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. --- a/drivers/net/phy/Kconfig
  2. +++ b/drivers/net/phy/Kconfig
  3. @@ -577,6 +577,13 @@ config XILINX_GMII2RGMII
  4. the Reduced Gigabit Media Independent Interface(RGMII) between
  5. Ethernet physical media devices and the Gigabit Ethernet controller.
  6. +config MDIO_IPQ40XX
  7. + tristate "Qualcomm Atheros ipq40xx MDIO interface"
  8. + depends on HAS_IOMEM && OF
  9. + ---help---
  10. + This driver supports the MDIO interface found in Qualcomm
  11. + Atheros ipq40xx Soc chip.
  12. +
  13. endif # PHYLIB
  14. config MICREL_KS8995MA
  15. --- a/drivers/net/phy/Makefile
  16. +++ b/drivers/net/phy/Makefile
  17. @@ -50,6 +50,7 @@ obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium
  18. obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
  19. obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o
  20. obj-$(CONFIG_MDIO_I2C) += mdio-i2c.o
  21. +obj-$(CONFIG_MDIO_IPQ40XX) += mdio-ipq40xx.o
  22. obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o
  23. obj-$(CONFIG_MDIO_MSCC_MIIM) += mdio-mscc-miim.o
  24. obj-$(CONFIG_MDIO_OCTEON) += mdio-octeon.o
  25. --- /dev/null
  26. +++ b/drivers/net/phy/mdio-ipq40xx.c
  27. @@ -0,0 +1,196 @@
  28. +/*
  29. + * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  30. + *
  31. + * Permission to use, copy, modify, and/or distribute this software for
  32. + * any purpose with or without fee is hereby granted, provided that the
  33. + * above copyright notice and this permission notice appear in all copies.
  34. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  35. + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  36. + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  37. + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  38. + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  39. + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  40. + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  41. + */
  42. +
  43. +#include <linux/delay.h>
  44. +#include <linux/kernel.h>
  45. +#include <linux/module.h>
  46. +#include <linux/mutex.h>
  47. +#include <linux/io.h>
  48. +#include <linux/of_address.h>
  49. +#include <linux/of_mdio.h>
  50. +#include <linux/phy.h>
  51. +#include <linux/platform_device.h>
  52. +
  53. +#define MDIO_CTRL_0_REG 0x40
  54. +#define MDIO_CTRL_1_REG 0x44
  55. +#define MDIO_CTRL_2_REG 0x48
  56. +#define MDIO_CTRL_3_REG 0x4c
  57. +#define MDIO_CTRL_4_REG 0x50
  58. +#define MDIO_CTRL_4_ACCESS_BUSY BIT(16)
  59. +#define MDIO_CTRL_4_ACCESS_START BIT(8)
  60. +#define MDIO_CTRL_4_ACCESS_CODE_READ 0
  61. +#define MDIO_CTRL_4_ACCESS_CODE_WRITE 1
  62. +#define CTRL_0_REG_DEFAULT_VALUE 0x150FF
  63. +
  64. +#define IPQ40XX_MDIO_RETRY 1000
  65. +#define IPQ40XX_MDIO_DELAY 10
  66. +
  67. +struct ipq40xx_mdio_data {
  68. + struct mii_bus *mii_bus;
  69. + void __iomem *membase;
  70. + struct device *dev;
  71. +};
  72. +
  73. +static int ipq40xx_mdio_wait_busy(struct ipq40xx_mdio_data *am)
  74. +{
  75. + int i;
  76. +
  77. + for (i = 0; i < IPQ40XX_MDIO_RETRY; i++) {
  78. + unsigned int busy;
  79. +
  80. + busy = readl(am->membase + MDIO_CTRL_4_REG) &
  81. + MDIO_CTRL_4_ACCESS_BUSY;
  82. + if (!busy)
  83. + return 0;
  84. +
  85. + /* BUSY might take to be cleard by 15~20 times of loop */
  86. + udelay(IPQ40XX_MDIO_DELAY);
  87. + }
  88. +
  89. + dev_err(am->dev, "%s: MDIO operation timed out\n", am->mii_bus->name);
  90. +
  91. + return -ETIMEDOUT;
  92. +}
  93. +
  94. +static int ipq40xx_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  95. +{
  96. + struct ipq40xx_mdio_data *am = bus->priv;
  97. + int value = 0;
  98. + unsigned int cmd = 0;
  99. +
  100. + lockdep_assert_held(&bus->mdio_lock);
  101. +
  102. + if (ipq40xx_mdio_wait_busy(am))
  103. + return -ETIMEDOUT;
  104. +
  105. + /* issue the phy address and reg */
  106. + writel((mii_id << 8) | regnum, am->membase + MDIO_CTRL_1_REG);
  107. +
  108. + cmd = MDIO_CTRL_4_ACCESS_START|MDIO_CTRL_4_ACCESS_CODE_READ;
  109. +
  110. + /* issue read command */
  111. + writel(cmd, am->membase + MDIO_CTRL_4_REG);
  112. +
  113. + /* Wait read complete */
  114. + if (ipq40xx_mdio_wait_busy(am))
  115. + return -ETIMEDOUT;
  116. +
  117. + /* Read data */
  118. + value = readl(am->membase + MDIO_CTRL_3_REG);
  119. +
  120. + return value;
  121. +}
  122. +
  123. +static int ipq40xx_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  124. + u16 value)
  125. +{
  126. + struct ipq40xx_mdio_data *am = bus->priv;
  127. + unsigned int cmd = 0;
  128. +
  129. + lockdep_assert_held(&bus->mdio_lock);
  130. +
  131. + if (ipq40xx_mdio_wait_busy(am))
  132. + return -ETIMEDOUT;
  133. +
  134. + /* issue the phy address and reg */
  135. + writel((mii_id << 8) | regnum, am->membase + MDIO_CTRL_1_REG);
  136. +
  137. + /* issue write data */
  138. + writel(value, am->membase + MDIO_CTRL_2_REG);
  139. +
  140. + cmd = MDIO_CTRL_4_ACCESS_START|MDIO_CTRL_4_ACCESS_CODE_WRITE;
  141. + /* issue write command */
  142. + writel(cmd, am->membase + MDIO_CTRL_4_REG);
  143. +
  144. + /* Wait write complete */
  145. + if (ipq40xx_mdio_wait_busy(am))
  146. + return -ETIMEDOUT;
  147. +
  148. + return 0;
  149. +}
  150. +
  151. +static int ipq40xx_mdio_probe(struct platform_device *pdev)
  152. +{
  153. + struct ipq40xx_mdio_data *am;
  154. + struct resource *res;
  155. + int i;
  156. +
  157. + am = devm_kzalloc(&pdev->dev, sizeof(*am), GFP_KERNEL);
  158. + if (!am)
  159. + return -ENOMEM;
  160. +
  161. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. + if (!res) {
  163. + dev_err(&pdev->dev, "no iomem resource found\n");
  164. + return -ENXIO;
  165. + }
  166. +
  167. + am->membase = devm_ioremap_resource(&pdev->dev, res);
  168. + if (IS_ERR(am->membase)) {
  169. + dev_err(&pdev->dev, "unable to ioremap registers\n");
  170. + return PTR_ERR(am->membase);
  171. + }
  172. +
  173. + am->mii_bus = devm_mdiobus_alloc(&pdev->dev);
  174. + if (!am->mii_bus)
  175. + return -ENOMEM;
  176. +
  177. + writel(CTRL_0_REG_DEFAULT_VALUE, am->membase + MDIO_CTRL_0_REG);
  178. +
  179. + am->mii_bus->name = "ipq40xx_mdio";
  180. + am->mii_bus->read = ipq40xx_mdio_read;
  181. + am->mii_bus->write = ipq40xx_mdio_write;
  182. + am->mii_bus->priv = am;
  183. + am->mii_bus->parent = &pdev->dev;
  184. + snprintf(am->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(&pdev->dev));
  185. +
  186. + am->dev = &pdev->dev;
  187. + platform_set_drvdata(pdev, am);
  188. +
  189. + return of_mdiobus_register(am->mii_bus, pdev->dev.of_node);
  190. +}
  191. +
  192. +static int ipq40xx_mdio_remove(struct platform_device *pdev)
  193. +{
  194. + struct ipq40xx_mdio_data *am = platform_get_drvdata(pdev);
  195. +
  196. + mdiobus_unregister(am->mii_bus);
  197. +
  198. + return 0;
  199. +}
  200. +
  201. +static const struct of_device_id ipq40xx_mdio_dt_ids[] = {
  202. + { .compatible = "qcom,ipq4019-mdio" },
  203. + { }
  204. +};
  205. +MODULE_DEVICE_TABLE(of, ipq40xx_mdio_dt_ids);
  206. +
  207. +static struct platform_driver ipq40xx_mdio_driver = {
  208. + .probe = ipq40xx_mdio_probe,
  209. + .remove = ipq40xx_mdio_remove,
  210. + .driver = {
  211. + .name = "ipq40xx-mdio",
  212. + .of_match_table = ipq40xx_mdio_dt_ids,
  213. + },
  214. +};
  215. +
  216. +module_platform_driver(ipq40xx_mdio_driver);
  217. +
  218. +#define DRV_VERSION "1.0"
  219. +
  220. +MODULE_DESCRIPTION("IPQ40XX MDIO interface driver");
  221. +MODULE_AUTHOR("Qualcomm Atheros");
  222. +MODULE_VERSION(DRV_VERSION);
  223. +MODULE_LICENSE("Dual BSD/GPL");