realtek-smi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Realtek Simple Management Interface (SMI) driver
  3. * It can be discussed how "simple" this interface is.
  4. *
  5. * The SMI protocol piggy-backs the MDIO MDC and MDIO signals levels
  6. * but the protocol is not MDIO at all. Instead it is a Realtek
  7. * pecularity that need to bit-bang the lines in a special way to
  8. * communicate with the switch.
  9. *
  10. * ASICs we intend to support with this driver:
  11. *
  12. * RTL8366 - The original version, apparently
  13. * RTL8369 - Similar enough to have the same datsheet as RTL8366
  14. * RTL8366RB - Probably reads out "RTL8366 revision B", has a quite
  15. * different register layout from the other two
  16. * RTL8366S - Is this "RTL8366 super"?
  17. * RTL8367 - Has an OpenWRT driver as well
  18. * RTL8368S - Seems to be an alternative name for RTL8366RB
  19. * RTL8370 - Also uses SMI
  20. *
  21. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  22. * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
  23. * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
  24. * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
  25. * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/device.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/of.h>
  33. #include <linux/delay.h>
  34. #include <linux/gpio/consumer.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/regmap.h>
  37. #include <linux/bitops.h>
  38. #include <linux/if_bridge.h>
  39. #include "realtek.h"
  40. #include "realtek-smi.h"
  41. #include "rtl83xx.h"
  42. #define REALTEK_SMI_ACK_RETRY_COUNT 5
  43. static inline void realtek_smi_clk_delay(struct realtek_priv *priv)
  44. {
  45. ndelay(priv->variant->clk_delay);
  46. }
  47. static void realtek_smi_start(struct realtek_priv *priv)
  48. {
  49. /* Set GPIO pins to output mode, with initial state:
  50. * SCK = 0, SDA = 1
  51. */
  52. gpiod_direction_output(priv->mdc, 0);
  53. gpiod_direction_output(priv->mdio, 1);
  54. realtek_smi_clk_delay(priv);
  55. /* CLK 1: 0 -> 1, 1 -> 0 */
  56. gpiod_set_value(priv->mdc, 1);
  57. realtek_smi_clk_delay(priv);
  58. gpiod_set_value(priv->mdc, 0);
  59. realtek_smi_clk_delay(priv);
  60. /* CLK 2: */
  61. gpiod_set_value(priv->mdc, 1);
  62. realtek_smi_clk_delay(priv);
  63. gpiod_set_value(priv->mdio, 0);
  64. realtek_smi_clk_delay(priv);
  65. gpiod_set_value(priv->mdc, 0);
  66. realtek_smi_clk_delay(priv);
  67. gpiod_set_value(priv->mdio, 1);
  68. }
  69. static void realtek_smi_stop(struct realtek_priv *priv)
  70. {
  71. realtek_smi_clk_delay(priv);
  72. gpiod_set_value(priv->mdio, 0);
  73. gpiod_set_value(priv->mdc, 1);
  74. realtek_smi_clk_delay(priv);
  75. gpiod_set_value(priv->mdio, 1);
  76. realtek_smi_clk_delay(priv);
  77. gpiod_set_value(priv->mdc, 1);
  78. realtek_smi_clk_delay(priv);
  79. gpiod_set_value(priv->mdc, 0);
  80. realtek_smi_clk_delay(priv);
  81. gpiod_set_value(priv->mdc, 1);
  82. /* Add a click */
  83. realtek_smi_clk_delay(priv);
  84. gpiod_set_value(priv->mdc, 0);
  85. realtek_smi_clk_delay(priv);
  86. gpiod_set_value(priv->mdc, 1);
  87. /* Set GPIO pins to input mode */
  88. gpiod_direction_input(priv->mdio);
  89. gpiod_direction_input(priv->mdc);
  90. }
  91. static void realtek_smi_write_bits(struct realtek_priv *priv, u32 data, u32 len)
  92. {
  93. for (; len > 0; len--) {
  94. realtek_smi_clk_delay(priv);
  95. /* Prepare data */
  96. gpiod_set_value(priv->mdio, !!(data & (1 << (len - 1))));
  97. realtek_smi_clk_delay(priv);
  98. /* Clocking */
  99. gpiod_set_value(priv->mdc, 1);
  100. realtek_smi_clk_delay(priv);
  101. gpiod_set_value(priv->mdc, 0);
  102. }
  103. }
  104. static void realtek_smi_read_bits(struct realtek_priv *priv, u32 len, u32 *data)
  105. {
  106. gpiod_direction_input(priv->mdio);
  107. for (*data = 0; len > 0; len--) {
  108. u32 u;
  109. realtek_smi_clk_delay(priv);
  110. /* Clocking */
  111. gpiod_set_value(priv->mdc, 1);
  112. realtek_smi_clk_delay(priv);
  113. u = !!gpiod_get_value(priv->mdio);
  114. gpiod_set_value(priv->mdc, 0);
  115. *data |= (u << (len - 1));
  116. }
  117. gpiod_direction_output(priv->mdio, 0);
  118. }
  119. static int realtek_smi_wait_for_ack(struct realtek_priv *priv)
  120. {
  121. int retry_cnt;
  122. retry_cnt = 0;
  123. do {
  124. u32 ack;
  125. realtek_smi_read_bits(priv, 1, &ack);
  126. if (ack == 0)
  127. break;
  128. if (++retry_cnt > REALTEK_SMI_ACK_RETRY_COUNT) {
  129. dev_err(priv->dev, "ACK timeout\n");
  130. return -ETIMEDOUT;
  131. }
  132. } while (1);
  133. return 0;
  134. }
  135. static int realtek_smi_write_byte(struct realtek_priv *priv, u8 data)
  136. {
  137. realtek_smi_write_bits(priv, data, 8);
  138. return realtek_smi_wait_for_ack(priv);
  139. }
  140. static int realtek_smi_write_byte_noack(struct realtek_priv *priv, u8 data)
  141. {
  142. realtek_smi_write_bits(priv, data, 8);
  143. return 0;
  144. }
  145. static int realtek_smi_read_byte0(struct realtek_priv *priv, u8 *data)
  146. {
  147. u32 t;
  148. /* Read data */
  149. realtek_smi_read_bits(priv, 8, &t);
  150. *data = (t & 0xff);
  151. /* Send an ACK */
  152. realtek_smi_write_bits(priv, 0x00, 1);
  153. return 0;
  154. }
  155. static int realtek_smi_read_byte1(struct realtek_priv *priv, u8 *data)
  156. {
  157. u32 t;
  158. /* Read data */
  159. realtek_smi_read_bits(priv, 8, &t);
  160. *data = (t & 0xff);
  161. /* Send an ACK */
  162. realtek_smi_write_bits(priv, 0x01, 1);
  163. return 0;
  164. }
  165. static int realtek_smi_read_reg(struct realtek_priv *priv, u32 addr, u32 *data)
  166. {
  167. unsigned long flags;
  168. u8 lo = 0;
  169. u8 hi = 0;
  170. int ret;
  171. spin_lock_irqsave(&priv->lock, flags);
  172. realtek_smi_start(priv);
  173. /* Send READ command */
  174. ret = realtek_smi_write_byte(priv, priv->variant->cmd_read);
  175. if (ret)
  176. goto out;
  177. /* Set ADDR[7:0] */
  178. ret = realtek_smi_write_byte(priv, addr & 0xff);
  179. if (ret)
  180. goto out;
  181. /* Set ADDR[15:8] */
  182. ret = realtek_smi_write_byte(priv, addr >> 8);
  183. if (ret)
  184. goto out;
  185. /* Read DATA[7:0] */
  186. realtek_smi_read_byte0(priv, &lo);
  187. /* Read DATA[15:8] */
  188. realtek_smi_read_byte1(priv, &hi);
  189. *data = ((u32)lo) | (((u32)hi) << 8);
  190. ret = 0;
  191. out:
  192. realtek_smi_stop(priv);
  193. spin_unlock_irqrestore(&priv->lock, flags);
  194. return ret;
  195. }
  196. static int realtek_smi_write_reg(struct realtek_priv *priv,
  197. u32 addr, u32 data, bool ack)
  198. {
  199. unsigned long flags;
  200. int ret;
  201. spin_lock_irqsave(&priv->lock, flags);
  202. realtek_smi_start(priv);
  203. /* Send WRITE command */
  204. ret = realtek_smi_write_byte(priv, priv->variant->cmd_write);
  205. if (ret)
  206. goto out;
  207. /* Set ADDR[7:0] */
  208. ret = realtek_smi_write_byte(priv, addr & 0xff);
  209. if (ret)
  210. goto out;
  211. /* Set ADDR[15:8] */
  212. ret = realtek_smi_write_byte(priv, addr >> 8);
  213. if (ret)
  214. goto out;
  215. /* Write DATA[7:0] */
  216. ret = realtek_smi_write_byte(priv, data & 0xff);
  217. if (ret)
  218. goto out;
  219. /* Write DATA[15:8] */
  220. if (ack)
  221. ret = realtek_smi_write_byte(priv, data >> 8);
  222. else
  223. ret = realtek_smi_write_byte_noack(priv, data >> 8);
  224. if (ret)
  225. goto out;
  226. ret = 0;
  227. out:
  228. realtek_smi_stop(priv);
  229. spin_unlock_irqrestore(&priv->lock, flags);
  230. return ret;
  231. }
  232. /* There is one single case when we need to use this accessor and that
  233. * is when issueing soft reset. Since the device reset as soon as we write
  234. * that bit, no ACK will come back for natural reasons.
  235. */
  236. static int realtek_smi_write_reg_noack(void *ctx, u32 reg, u32 val)
  237. {
  238. return realtek_smi_write_reg(ctx, reg, val, false);
  239. }
  240. /* Regmap accessors */
  241. static int realtek_smi_write(void *ctx, u32 reg, u32 val)
  242. {
  243. struct realtek_priv *priv = ctx;
  244. return realtek_smi_write_reg(priv, reg, val, true);
  245. }
  246. static int realtek_smi_read(void *ctx, u32 reg, u32 *val)
  247. {
  248. struct realtek_priv *priv = ctx;
  249. return realtek_smi_read_reg(priv, reg, val);
  250. }
  251. static const struct realtek_interface_info realtek_smi_info = {
  252. .reg_read = realtek_smi_read,
  253. .reg_write = realtek_smi_write,
  254. };
  255. /**
  256. * realtek_smi_probe() - Probe a platform device for an SMI-connected switch
  257. * @pdev: platform_device to probe on.
  258. *
  259. * This function should be used as the .probe in a platform_driver. After
  260. * calling the common probe function for both interfaces, it initializes the
  261. * values specific for SMI-connected devices. Finally, it calls a common
  262. * function to register the DSA switch.
  263. *
  264. * Context: Can sleep. Takes and releases priv->map_lock.
  265. * Return: Returns 0 on success, a negative error on failure.
  266. */
  267. int realtek_smi_probe(struct platform_device *pdev)
  268. {
  269. struct device *dev = &pdev->dev;
  270. struct realtek_priv *priv;
  271. int ret;
  272. priv = rtl83xx_probe(dev, &realtek_smi_info);
  273. if (IS_ERR(priv))
  274. return PTR_ERR(priv);
  275. /* Fetch MDIO pins */
  276. priv->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW);
  277. if (IS_ERR(priv->mdc)) {
  278. rtl83xx_remove(priv);
  279. return PTR_ERR(priv->mdc);
  280. }
  281. priv->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW);
  282. if (IS_ERR(priv->mdio)) {
  283. rtl83xx_remove(priv);
  284. return PTR_ERR(priv->mdio);
  285. }
  286. priv->write_reg_noack = realtek_smi_write_reg_noack;
  287. ret = rtl83xx_register_switch(priv);
  288. if (ret) {
  289. rtl83xx_remove(priv);
  290. return ret;
  291. }
  292. return 0;
  293. }
  294. EXPORT_SYMBOL_NS_GPL(realtek_smi_probe, REALTEK_DSA);
  295. /**
  296. * realtek_smi_remove() - Remove the driver of a SMI-connected switch
  297. * @pdev: platform_device to be removed.
  298. *
  299. * This function should be used as the .remove_new in a platform_driver. First
  300. * it unregisters the DSA switch and then it calls the common remove function.
  301. *
  302. * Context: Can sleep.
  303. * Return: Nothing.
  304. */
  305. void realtek_smi_remove(struct platform_device *pdev)
  306. {
  307. struct realtek_priv *priv = platform_get_drvdata(pdev);
  308. if (!priv)
  309. return;
  310. rtl83xx_unregister_switch(priv);
  311. rtl83xx_remove(priv);
  312. }
  313. EXPORT_SYMBOL_NS_GPL(realtek_smi_remove, REALTEK_DSA);
  314. /**
  315. * realtek_smi_shutdown() - Shutdown the driver of a SMI-connected switch
  316. * @pdev: platform_device shutting down.
  317. *
  318. * This function should be used as the .shutdown in a platform_driver. It calls
  319. * the common shutdown function.
  320. *
  321. * Context: Can sleep.
  322. * Return: Nothing.
  323. */
  324. void realtek_smi_shutdown(struct platform_device *pdev)
  325. {
  326. struct realtek_priv *priv = platform_get_drvdata(pdev);
  327. if (!priv)
  328. return;
  329. rtl83xx_shutdown(priv);
  330. }
  331. EXPORT_SYMBOL_NS_GPL(realtek_smi_shutdown, REALTEK_DSA);