lan9252.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) KEBA Industrial Automation Gmbh 2024
  4. *
  5. * Driver for LAN9252 on KEBA CP500 devices
  6. *
  7. * This driver is used for updating the configuration of the LAN9252 controller
  8. * on KEBA CP500 devices. The LAN9252 is connected over SPI, which is also named
  9. * PDI.
  10. */
  11. #include <linux/spi/spi.h>
  12. #include <linux/mii.h>
  13. /* SPI commands */
  14. #define LAN9252_SPI_READ 0x3
  15. #define LAN9252_SPI_WRITE 0x2
  16. struct lan9252_read_cmd {
  17. u8 cmd;
  18. u8 addr_0;
  19. u8 addr_1;
  20. } __packed;
  21. struct lan9252_write_cmd {
  22. u8 cmd;
  23. u8 addr_0;
  24. u8 addr_1;
  25. u32 data;
  26. } __packed;
  27. /* byte test register */
  28. #define LAN9252_BYTE_TEST 0x64
  29. #define LAN9252_BYTE_TEST_VALUE 0x87654321
  30. /* hardware configuration register */
  31. #define LAN9252_HW_CFG 0x74
  32. #define LAN9252_HW_CFG_READY 0x08000000
  33. /* EtherCAT CSR interface data register */
  34. #define LAN9252_ECAT_CSR_DATA 0x300
  35. /* EtherCAT CSR interface command register */
  36. #define LAN9252_ECAT_CSR_CMD 0x304
  37. #define LAN9252_ECAT_CSR_BUSY 0x80000000
  38. #define LAN9252_ECAT_CSR_READ 0x40000000
  39. /* EtherCAT slave controller MII register */
  40. #define LAN9252_ESC_MII 0x510
  41. #define LAN9252_ESC_MII_BUSY 0x8000
  42. #define LAN9252_ESC_MII_CMD_ERR 0x4000
  43. #define LAN9252_ESC_MII_READ_ERR 0x2000
  44. #define LAN9252_ESC_MII_ERR_MASK (LAN9252_ESC_MII_CMD_ERR | \
  45. LAN9252_ESC_MII_READ_ERR)
  46. #define LAN9252_ESC_MII_WRITE 0x0200
  47. #define LAN9252_ESC_MII_READ 0x0100
  48. /* EtherCAT slave controller PHY address register */
  49. #define LAN9252_ESC_PHY_ADDR 0x512
  50. /* EtherCAT slave controller PHY register address register */
  51. #define LAN9252_ESC_PHY_REG_ADDR 0x513
  52. /* EtherCAT slave controller PHY data register */
  53. #define LAN9252_ESC_PHY_DATA 0x514
  54. /* EtherCAT slave controller PDI access state register */
  55. #define LAN9252_ESC_MII_PDI 0x517
  56. #define LAN9252_ESC_MII_ACCESS_PDI 0x01
  57. #define LAN9252_ESC_MII_ACCESS_ECAT 0x00
  58. /* PHY address */
  59. #define PHY_ADDRESS 2
  60. #define SPI_RETRY_COUNT 10
  61. #define SPI_WAIT_US 100
  62. #define SPI_CSR_WAIT_US 500
  63. static int lan9252_spi_read(struct spi_device *spi, u16 addr, u32 *data)
  64. {
  65. struct lan9252_read_cmd cmd;
  66. cmd.cmd = LAN9252_SPI_READ;
  67. cmd.addr_0 = (addr >> 8) & 0xFF;
  68. cmd.addr_1 = addr & 0xFF;
  69. return spi_write_then_read(spi, (u8 *)&cmd,
  70. sizeof(struct lan9252_read_cmd),
  71. (u8 *)data, sizeof(u32));
  72. }
  73. static int lan9252_spi_write(struct spi_device *spi, u16 addr, u32 data)
  74. {
  75. struct lan9252_write_cmd cmd;
  76. cmd.cmd = LAN9252_SPI_WRITE;
  77. cmd.addr_0 = (addr >> 8) & 0xFF;
  78. cmd.addr_1 = addr & 0xFF;
  79. cmd.data = data;
  80. return spi_write(spi, (u8 *)&cmd, sizeof(struct lan9252_write_cmd));
  81. }
  82. static bool lan9252_init(struct spi_device *spi)
  83. {
  84. u32 data;
  85. int ret;
  86. ret = lan9252_spi_read(spi, LAN9252_BYTE_TEST, &data);
  87. if (ret || data != LAN9252_BYTE_TEST_VALUE)
  88. return false;
  89. ret = lan9252_spi_read(spi, LAN9252_HW_CFG, &data);
  90. if (ret || !(data & LAN9252_HW_CFG_READY))
  91. return false;
  92. return true;
  93. }
  94. static u8 lan9252_esc_get_size(u16 addr)
  95. {
  96. if (addr == LAN9252_ESC_MII || addr == LAN9252_ESC_PHY_DATA)
  97. return 2;
  98. return 1;
  99. }
  100. static int lan9252_esc_wait(struct spi_device *spi)
  101. {
  102. ktime_t timeout = ktime_add_us(ktime_get(), SPI_WAIT_US);
  103. u32 data;
  104. int ret;
  105. /* wait while CSR command is busy */
  106. for (;;) {
  107. ret = lan9252_spi_read(spi, LAN9252_ECAT_CSR_CMD, &data);
  108. if (ret)
  109. return ret;
  110. if (!(data & LAN9252_ECAT_CSR_BUSY))
  111. return 0;
  112. if (ktime_compare(ktime_get(), timeout) > 0) {
  113. ret = lan9252_spi_read(spi, LAN9252_ECAT_CSR_CMD, &data);
  114. if (ret)
  115. return ret;
  116. break;
  117. }
  118. }
  119. return (!(data & LAN9252_ECAT_CSR_BUSY)) ? 0 : -ETIMEDOUT;
  120. }
  121. static int lan9252_esc_read(struct spi_device *spi, u16 addr, u32 *data)
  122. {
  123. u32 csr_cmd;
  124. u8 size;
  125. int ret;
  126. size = lan9252_esc_get_size(addr);
  127. csr_cmd = LAN9252_ECAT_CSR_BUSY | LAN9252_ECAT_CSR_READ;
  128. csr_cmd |= (size << 16) | addr;
  129. ret = lan9252_spi_write(spi, LAN9252_ECAT_CSR_CMD, csr_cmd);
  130. if (ret)
  131. return ret;
  132. ret = lan9252_esc_wait(spi);
  133. if (ret)
  134. return ret;
  135. ret = lan9252_spi_read(spi, LAN9252_ECAT_CSR_DATA, data);
  136. if (ret)
  137. return ret;
  138. return 0;
  139. }
  140. static int lan9252_esc_write(struct spi_device *spi, u16 addr, u32 data)
  141. {
  142. u32 csr_cmd;
  143. u8 size;
  144. int ret;
  145. ret = lan9252_spi_write(spi, LAN9252_ECAT_CSR_DATA, data);
  146. if (ret)
  147. return ret;
  148. size = lan9252_esc_get_size(addr);
  149. csr_cmd = LAN9252_ECAT_CSR_BUSY;
  150. csr_cmd |= (size << 16) | addr;
  151. ret = lan9252_spi_write(spi, LAN9252_ECAT_CSR_CMD, csr_cmd);
  152. if (ret)
  153. return ret;
  154. ret = lan9252_esc_wait(spi);
  155. if (ret)
  156. return ret;
  157. return 0;
  158. }
  159. static int lan9252_access_mii(struct spi_device *spi, bool access)
  160. {
  161. u32 data;
  162. if (access)
  163. data = LAN9252_ESC_MII_ACCESS_PDI;
  164. else
  165. data = LAN9252_ESC_MII_ACCESS_ECAT;
  166. return lan9252_esc_write(spi, LAN9252_ESC_MII_PDI, data);
  167. }
  168. static int lan9252_mii_wait(struct spi_device *spi)
  169. {
  170. ktime_t timeout = ktime_add_us(ktime_get(), SPI_CSR_WAIT_US);
  171. u32 data;
  172. int ret;
  173. /* wait while MII control state machine is busy */
  174. for (;;) {
  175. ret = lan9252_esc_read(spi, LAN9252_ESC_MII, &data);
  176. if (ret)
  177. return ret;
  178. if (data & LAN9252_ESC_MII_ERR_MASK)
  179. return -EIO;
  180. if (!(data & LAN9252_ESC_MII_BUSY))
  181. return 0;
  182. if (ktime_compare(ktime_get(), timeout) > 0) {
  183. ret = lan9252_esc_read(spi, LAN9252_ESC_MII, &data);
  184. if (ret)
  185. return ret;
  186. if (data & LAN9252_ESC_MII_ERR_MASK)
  187. return -EIO;
  188. break;
  189. }
  190. }
  191. return (!(data & LAN9252_ESC_MII_BUSY)) ? 0 : -ETIMEDOUT;
  192. }
  193. static int lan9252_mii_read(struct spi_device *spi, u8 phy_addr, u8 reg_addr,
  194. u32 *data)
  195. {
  196. int ret;
  197. ret = lan9252_esc_write(spi, LAN9252_ESC_PHY_ADDR, phy_addr);
  198. if (ret)
  199. return ret;
  200. ret = lan9252_esc_write(spi, LAN9252_ESC_PHY_REG_ADDR, reg_addr);
  201. if (ret)
  202. return ret;
  203. ret = lan9252_esc_write(spi, LAN9252_ESC_MII, LAN9252_ESC_MII_READ);
  204. if (ret)
  205. return ret;
  206. ret = lan9252_mii_wait(spi);
  207. if (ret)
  208. return ret;
  209. return lan9252_esc_read(spi, LAN9252_ESC_PHY_DATA, data);
  210. }
  211. static int lan9252_mii_write(struct spi_device *spi, u8 phy_addr, u8 reg_addr,
  212. u32 data)
  213. {
  214. int ret;
  215. ret = lan9252_esc_write(spi, LAN9252_ESC_PHY_ADDR, phy_addr);
  216. if (ret)
  217. return ret;
  218. ret = lan9252_esc_write(spi, LAN9252_ESC_PHY_REG_ADDR, reg_addr);
  219. if (ret)
  220. return ret;
  221. ret = lan9252_esc_write(spi, LAN9252_ESC_PHY_DATA, data);
  222. if (ret)
  223. return ret;
  224. ret = lan9252_esc_write(spi, LAN9252_ESC_MII, LAN9252_ESC_MII_WRITE);
  225. if (ret)
  226. return ret;
  227. return lan9252_mii_wait(spi);
  228. }
  229. static int lan9252_probe(struct spi_device *spi)
  230. {
  231. u32 data;
  232. int retry = SPI_RETRY_COUNT;
  233. int ret;
  234. /* execute specified initialization sequence */
  235. while (retry && !lan9252_init(spi))
  236. retry--;
  237. if (retry == 0) {
  238. dev_err(&spi->dev,
  239. "Can't initialize LAN9252 SPI communication!");
  240. return -EIO;
  241. }
  242. /* enable access to MII management for PDI */
  243. ret = lan9252_access_mii(spi, true);
  244. if (ret) {
  245. dev_err(&spi->dev, "Can't enable access to MII management!");
  246. return ret;
  247. }
  248. /*
  249. * check PHY configuration and configure if necessary
  250. * - full duplex
  251. * - auto negotiation disabled
  252. * - 100 Mbps
  253. */
  254. ret = lan9252_mii_read(spi, PHY_ADDRESS, MII_BMCR, &data);
  255. if (ret) {
  256. dev_err(&spi->dev, "Can't read LAN9252 configuration!");
  257. goto out;
  258. }
  259. if (!(data & BMCR_FULLDPLX) || (data & BMCR_ANENABLE) ||
  260. !(data & BMCR_SPEED100)) {
  261. /*
  262. */
  263. data &= ~(BMCR_ANENABLE);
  264. data |= (BMCR_FULLDPLX | BMCR_SPEED100);
  265. ret = lan9252_mii_write(spi, PHY_ADDRESS, MII_BMCR, data);
  266. if (ret)
  267. dev_err(&spi->dev,
  268. "Can't write LAN9252 configuration!");
  269. }
  270. dev_info(&spi->dev, "LAN9252 PHY configuration");
  271. out:
  272. /* disable access to MII management for PDI */
  273. lan9252_access_mii(spi, false);
  274. return ret;
  275. }
  276. static const struct spi_device_id lan9252_id[] = {
  277. {"lan9252"},
  278. {}
  279. };
  280. MODULE_DEVICE_TABLE(spi, lan9252_id);
  281. static struct spi_driver lan9252_driver = {
  282. .driver = {
  283. .name = "lan9252",
  284. },
  285. .probe = lan9252_probe,
  286. .id_table = lan9252_id,
  287. };
  288. module_spi_driver(lan9252_driver);
  289. MODULE_AUTHOR("Petar Bojanic <boja@keba.com>");
  290. MODULE_AUTHOR("Gerhard Engleder <eg@keba.com>");
  291. MODULE_DESCRIPTION("KEBA LAN9252 driver");
  292. MODULE_LICENSE("GPL");