723-net-mdio-Add-Realtek-Otto-auxiliary-controller.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. From ffb7da9aa25765b2115e7ff3ee4f6dafa60f5421 Mon Sep 17 00:00:00 2001
  2. From: Sander Vanheule <[email protected]>
  3. Date: Fri, 27 Dec 2024 14:55:31 +0100
  4. Subject: [PATCH] net: mdio: Add Realtek Otto auxiliary controller
  5. SoCs in Realtek's Otto platform such as the RTL8380, RTL8391, and
  6. RTL9302 have a simple auxiliary MDIO controller that is commonly used to
  7. manage RTL8231 GPIO expanders on switch devices.
  8. Add a new MDIO controller driver supporting the RTL838x (maple), RTL839x
  9. (cypress), and RTL930x (longan) SoCs.
  10. Signed-off-by: Sander Vanheule <[email protected]>
  11. ---
  12. drivers/net/mdio/Kconfig | 10 ++
  13. drivers/net/mdio/Makefile | 1 +
  14. drivers/net/mdio/mdio-realtek-otto-aux.c | 175 +++++++++++++++++++++++
  15. 3 files changed, 186 insertions(+)
  16. create mode 100644 drivers/net/mdio/mdio-realtek-otto-aux.c
  17. --- a/drivers/net/mdio/Kconfig
  18. +++ b/drivers/net/mdio/Kconfig
  19. @@ -207,6 +207,16 @@ config MDIO_REGMAP
  20. regmap. Users willing to use this driver must explicitly select
  21. REGMAP.
  22. +config MDIO_REALTEK_OTTO_AUX
  23. + tristate "Realtek Otto auxiliary MDIO interface support"
  24. + default MACH_REALTEK_RTL
  25. + depends on MACH_REALTEK_RTL
  26. + depends on MFD_SYSCON
  27. + select MDIO_DEVRES
  28. + help
  29. + This driver supports the auxilairy MDIO bus on RTL838x SoCs. This bus
  30. + is typically used to attach RTL8231 GPIO extenders.
  31. +
  32. config MDIO_THUNDER
  33. tristate "ThunderX SOCs MDIO buses"
  34. depends on 64BIT
  35. --- a/drivers/net/mdio/Makefile
  36. +++ b/drivers/net/mdio/Makefile
  37. @@ -20,6 +20,7 @@ obj-$(CONFIG_MDIO_MSCC_MIIM) += mdio-ms
  38. obj-$(CONFIG_MDIO_MVUSB) += mdio-mvusb.o
  39. obj-$(CONFIG_MDIO_OCTEON) += mdio-octeon.o
  40. obj-$(CONFIG_MDIO_REGMAP) += mdio-regmap.o
  41. +obj-$(CONFIG_MDIO_REALTEK_OTTO_AUX) += mdio-realtek-otto-aux.o
  42. obj-$(CONFIG_MDIO_SMBUS) += mdio-smbus.o
  43. obj-$(CONFIG_MDIO_SUN4I) += mdio-sun4i.o
  44. obj-$(CONFIG_MDIO_THUNDER) += mdio-thunder.o
  45. --- /dev/null
  46. +++ b/drivers/net/mdio/mdio-realtek-otto-aux.c
  47. @@ -0,0 +1,175 @@
  48. +// SPDX-License-Identifier: GPL-2.0-or-later
  49. +
  50. +#include <linux/mfd/core.h>
  51. +#include <linux/mfd/syscon.h>
  52. +#include <linux/module.h>
  53. +#include <linux/of.h>
  54. +#include <linux/of_mdio.h>
  55. +#include <linux/of_platform.h>
  56. +#include <linux/phy.h>
  57. +#include <linux/platform_device.h>
  58. +#include <linux/regmap.h>
  59. +
  60. +#define RTL8380_EXT_GPIO_INDIRECT_ACCESS 0xA09C
  61. +#define RTL8390_EXT_GPIO_INDIRECT_ACCESS 0x0224
  62. +#define RTL9300_EXT_GPIO_INDIRECT_ACCESS 0xC620
  63. +
  64. +#define RTL83XX_AUX_MDIO_DATA_OFFSET 16
  65. +#define RTL83XX_AUX_MDIO_RCMD_FAIL 0
  66. +
  67. +#define RTL93XX_AUX_MDIO_DATA_OFFSET 12
  68. +#define RTL93XX_AUX_MDIO_RCMD_FAIL BIT(28)
  69. +
  70. +#define REALTEK_AUX_MDIO_REG GENMASK(11, 7)
  71. +#define REALTEK_AUX_MDIO_PHY_ADDR GENMASK(6, 2)
  72. +#define REALTEK_AUX_MDIO_WRITE BIT(1)
  73. +#define REALTEK_AUX_MDIO_READ 0
  74. +#define REALTEK_AUX_MDIO_EXEC BIT(0)
  75. +
  76. +struct realtek_aux_mdio_info {
  77. + unsigned int cmd_reg;
  78. + unsigned int data_offset;
  79. + unsigned int rcmd_fail_mask;
  80. + unsigned int timeout_us;
  81. +};
  82. +
  83. +static const struct realtek_aux_mdio_info info_rtl838x = {
  84. + .cmd_reg = RTL8380_EXT_GPIO_INDIRECT_ACCESS,
  85. + .data_offset = RTL83XX_AUX_MDIO_DATA_OFFSET,
  86. + .rcmd_fail_mask = RTL83XX_AUX_MDIO_RCMD_FAIL,
  87. + .timeout_us = 1700,
  88. +};
  89. +
  90. +static const struct realtek_aux_mdio_info info_rtl839x = {
  91. + .cmd_reg = RTL8390_EXT_GPIO_INDIRECT_ACCESS,
  92. + .data_offset = RTL83XX_AUX_MDIO_DATA_OFFSET,
  93. + .rcmd_fail_mask = RTL83XX_AUX_MDIO_RCMD_FAIL,
  94. + .timeout_us = 4120,
  95. +};
  96. +
  97. +static const struct realtek_aux_mdio_info info_rtl930x = {
  98. + .cmd_reg = RTL9300_EXT_GPIO_INDIRECT_ACCESS,
  99. + .data_offset = RTL93XX_AUX_MDIO_DATA_OFFSET,
  100. + .rcmd_fail_mask = RTL93XX_AUX_MDIO_RCMD_FAIL,
  101. + .timeout_us = 19000,
  102. +};
  103. +
  104. +struct realtek_aux_mdio_ctrl {
  105. + struct device *dev;
  106. + struct regmap *map;
  107. + const struct realtek_aux_mdio_info *info;
  108. +};
  109. +
  110. +#define mii_bus_to_ctrl(bus) ((struct realtek_aux_mdio_ctrl *) bus->priv)
  111. +
  112. +static int realtek_aux_mdio_cmd(struct realtek_aux_mdio_ctrl *ctrl, int addr, int regnum,
  113. + u32 rw_bit, u16 *data)
  114. +{
  115. + unsigned int cmd;
  116. + int err;
  117. +
  118. + cmd = rw_bit | REALTEK_AUX_MDIO_EXEC;
  119. + cmd |= FIELD_PREP(REALTEK_AUX_MDIO_PHY_ADDR, addr);
  120. + cmd |= FIELD_PREP(REALTEK_AUX_MDIO_REG, regnum);
  121. +
  122. + if (rw_bit == REALTEK_AUX_MDIO_WRITE)
  123. + cmd |= *data << ctrl->info->data_offset;
  124. +
  125. + err = regmap_write(ctrl->map, ctrl->info->cmd_reg, cmd);
  126. + if (err)
  127. + return err;
  128. +
  129. + err = regmap_read_poll_timeout_atomic(ctrl->map, ctrl->info->cmd_reg, cmd,
  130. + !(cmd & REALTEK_AUX_MDIO_EXEC), 3, ctrl->info->timeout_us);
  131. + if (err)
  132. + return err;
  133. +
  134. + if (rw_bit == REALTEK_AUX_MDIO_READ) {
  135. + if (cmd & ctrl->info->rcmd_fail_mask)
  136. + return -EIO;
  137. +
  138. + *data = (cmd >> ctrl->info->data_offset) & GENMASK(15, 0);
  139. + }
  140. +
  141. + return 0;
  142. +}
  143. +
  144. +static int realtek_aux_mdio_read(struct mii_bus *bus, int addr, int regnum)
  145. +{
  146. + struct realtek_aux_mdio_ctrl *ctrl = mii_bus_to_ctrl(bus);
  147. + u16 data;
  148. + int err;
  149. +
  150. + err = realtek_aux_mdio_cmd(ctrl, addr, regnum, REALTEK_AUX_MDIO_READ, &data);
  151. +
  152. + if (err)
  153. + return err;
  154. + else
  155. + return data;
  156. +}
  157. +
  158. +static int realtek_aux_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val)
  159. +{
  160. + struct realtek_aux_mdio_ctrl *ctrl = mii_bus_to_ctrl(bus);
  161. +
  162. + return realtek_aux_mdio_cmd(ctrl, addr, regnum, REALTEK_AUX_MDIO_WRITE, &val);
  163. +}
  164. +
  165. +static int realtek_aux_mdio_probe(struct platform_device *pdev)
  166. +{
  167. + struct device_node *np = pdev->dev.of_node;
  168. + struct realtek_aux_mdio_ctrl *ctrl;
  169. + struct mii_bus *bus;
  170. +
  171. + bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*ctrl));
  172. + if (!bus)
  173. + return -ENOMEM;
  174. +
  175. + ctrl = bus->priv;
  176. + ctrl->dev = &pdev->dev;
  177. + ctrl->info = (const struct realtek_aux_mdio_info *) device_get_match_data(ctrl->dev);
  178. + ctrl->map = syscon_node_to_regmap(np->parent);
  179. + if (IS_ERR(ctrl->map))
  180. + return PTR_ERR(ctrl->map);
  181. +
  182. + bus->name = "Realtek auxiliary MDIO bus";
  183. + snprintf(bus->id, MII_BUS_ID_SIZE, "realtek-aux-mdio") ;
  184. + bus->parent = ctrl->dev;
  185. + bus->read = realtek_aux_mdio_read;
  186. + bus->write = realtek_aux_mdio_write;
  187. + /* Don't have interrupts */
  188. + for (unsigned int i = 0; i < PHY_MAX_ADDR; i++)
  189. + bus->irq[i] = PHY_POLL;
  190. +
  191. + return devm_of_mdiobus_register(ctrl->dev, bus, np);
  192. +}
  193. +
  194. +static const struct of_device_id realtek_aux_mdio_of_match[] = {
  195. + {
  196. + .compatible = "realtek,rtl8380-aux-mdio",
  197. + .data = &info_rtl838x,
  198. + },
  199. + {
  200. + .compatible = "realtek,rtl8390-aux-mdio",
  201. + .data = &info_rtl839x,
  202. + },
  203. + {
  204. + .compatible = "realtek,rtl9300-aux-mdio",
  205. + .data = &info_rtl930x,
  206. + },
  207. + { /* sentinel */ }
  208. +};
  209. +MODULE_DEVICE_TABLE(of, realtek_aux_mdio_of_match);
  210. +
  211. +static struct platform_driver realtek_aux_mdio_driver = {
  212. + .driver = {
  213. + .name = "realtek-otto-aux-mdio",
  214. + .of_match_table = realtek_aux_mdio_of_match
  215. + },
  216. + .probe = realtek_aux_mdio_probe,
  217. +};
  218. +module_platform_driver(realtek_aux_mdio_driver);
  219. +
  220. +MODULE_AUTHOR("Sander Vanheule <[email protected]>");
  221. +MODULE_DESCRIPTION("Realtek otto auxiliary MDIO bus");
  222. +MODULE_LICENSE("GPL v2");