rb4xx-cpld.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CPLD driver for the MikroTik RouterBoard 4xx series
  4. *
  5. * This driver provides access to a CPLD that interfaces between the SoC SPI bus
  6. * and other devices. Behind the CPLD there is a NAND flash chip and five LEDs.
  7. *
  8. * The CPLD supports SPI two-wire mode, in which two bits are transferred per
  9. * SPI clock cycle. The second bit is transmitted with the SoC's CS2 pin.
  10. *
  11. * The CPLD also acts as a GPIO expander.
  12. *
  13. * Copyright (C) 2008-2011 Gabor Juhos <[email protected]>
  14. * Copyright (C) 2008 Imre Kaloz <[email protected]>
  15. * Copyright (C) 2015 Bert Vermeulen <[email protected]>
  16. * Copyright (C) 2020 Christopher Hill <[email protected]>
  17. *
  18. * This file was based on the driver for Linux 2.6.22 published by
  19. * MikroTik for their RouterBoard 4xx series devices.
  20. */
  21. #include <linux/mfd/core.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/module.h>
  24. #include <linux/of_platform.h>
  25. #include <mfd/rb4xx-cpld.h>
  26. /* CPLD commands */
  27. #define CPLD_CMD_WRITE_NAND 0x08 /* send cmd, n x send data, send idle */
  28. #define CPLD_CMD_WRITE_CFG 0x09 /* send cmd, n x send cfg */
  29. #define CPLD_CMD_READ_NAND 0x0a /* send cmd, send idle, n x read data */
  30. #define CPLD_CMD_READ_FAST 0x0b /* send cmd, 4 x idle, n x read data */
  31. #define CPLD_CMD_GPIO8_HIGH 0x0c /* send cmd */
  32. #define CPLD_CMD_GPIO8_LOW 0x0d /* send cmd */
  33. static int rb4xx_cpld_write_nand(struct rb4xx_cpld *cpld, const void *tx_buf,
  34. unsigned int len)
  35. {
  36. struct spi_message m;
  37. static const u8 cmd = CPLD_CMD_WRITE_NAND;
  38. struct spi_transfer t[3] = {
  39. {
  40. .tx_buf = &cmd,
  41. .len = sizeof(cmd),
  42. }, {
  43. .tx_buf = tx_buf,
  44. .len = len,
  45. .tx_nbits = SPI_NBITS_DUAL,
  46. }, {
  47. .len = 1,
  48. .tx_nbits = SPI_NBITS_DUAL,
  49. },
  50. };
  51. spi_message_init(&m);
  52. spi_message_add_tail(&t[0], &m);
  53. spi_message_add_tail(&t[1], &m);
  54. spi_message_add_tail(&t[2], &m);
  55. return spi_sync(cpld->spi, &m);
  56. }
  57. static int rb4xx_cpld_read_nand(struct rb4xx_cpld *cpld, void *rx_buf,
  58. unsigned int len)
  59. {
  60. struct spi_message m;
  61. static const u8 cmd[2] = {
  62. CPLD_CMD_READ_NAND, 0
  63. };
  64. struct spi_transfer t[2] = {
  65. {
  66. .tx_buf = &cmd,
  67. .len = sizeof(cmd),
  68. }, {
  69. .rx_buf = rx_buf,
  70. .len = len,
  71. },
  72. };
  73. spi_message_init(&m);
  74. spi_message_add_tail(&t[0], &m);
  75. spi_message_add_tail(&t[1], &m);
  76. return spi_sync(cpld->spi, &m);
  77. }
  78. static int rb4xx_cpld_cmd(struct rb4xx_cpld *cpld, const void *tx_buf,
  79. unsigned int len)
  80. {
  81. struct spi_message m;
  82. struct spi_transfer t = {
  83. .tx_buf = tx_buf,
  84. .len = len,
  85. };
  86. spi_message_init(&m);
  87. spi_message_add_tail(&t, &m);
  88. return spi_sync(cpld->spi, &m);
  89. }
  90. static int rb4xx_cpld_gpio_set_0_7(struct rb4xx_cpld *cpld, u8 values)
  91. {
  92. /* GPIO 0-7 change can be sent via command + bitfield */
  93. u8 cmd[2] = {
  94. CPLD_CMD_WRITE_CFG, values
  95. };
  96. return rb4xx_cpld_cmd(cpld, &cmd, 2);
  97. }
  98. static int rb4xx_cpld_gpio_set_8(struct rb4xx_cpld *cpld, u8 value)
  99. {
  100. /* GPIO 8 uses dedicated high/low commands */
  101. u8 cmd = CPLD_CMD_GPIO8_HIGH | !!(value);
  102. return rb4xx_cpld_cmd(cpld, &cmd, 1);
  103. }
  104. static const struct mfd_cell rb4xx_cpld_cells[] = {
  105. {
  106. .name = "mikrotik,rb4xx-gpio",
  107. .of_compatible = "mikrotik,rb4xx-gpio",
  108. }, {
  109. .name = "mikrotik,rb4xx-nand",
  110. .of_compatible = "mikrotik,rb4xx-nand",
  111. },
  112. };
  113. static int rb4xx_cpld_probe(struct spi_device *spi)
  114. {
  115. struct device *dev = &spi->dev;
  116. struct rb4xx_cpld *cpld;
  117. int ret;
  118. cpld = devm_kzalloc(dev, sizeof(*cpld), GFP_KERNEL);
  119. if (!cpld)
  120. return -ENOMEM;
  121. dev_set_drvdata(dev, cpld);
  122. cpld->spi = spi;
  123. cpld->write_nand = rb4xx_cpld_write_nand;
  124. cpld->read_nand = rb4xx_cpld_read_nand;
  125. cpld->gpio_set_0_7 = rb4xx_cpld_gpio_set_0_7;
  126. cpld->gpio_set_8 = rb4xx_cpld_gpio_set_8;
  127. spi->mode = SPI_MODE_0 | SPI_TX_DUAL;
  128. ret = spi_setup(spi);
  129. if (ret)
  130. return ret;
  131. return devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
  132. rb4xx_cpld_cells,
  133. ARRAY_SIZE(rb4xx_cpld_cells),
  134. NULL, 0, NULL);
  135. }
  136. static const struct of_device_id rb4xx_cpld_dt_match[] = {
  137. { .compatible = "mikrotik,rb4xx-cpld", },
  138. { },
  139. };
  140. MODULE_DEVICE_TABLE(of, rb4xx_cpld_dt_match);
  141. static struct spi_driver rb4xx_cpld_driver = {
  142. .probe = rb4xx_cpld_probe,
  143. .driver = {
  144. .name = "rb4xx-cpld",
  145. .bus = &spi_bus_type,
  146. .of_match_table = rb4xx_cpld_dt_match,
  147. },
  148. };
  149. module_spi_driver(rb4xx_cpld_driver);
  150. MODULE_DESCRIPTION("Mikrotik RB4xx CPLD driver");
  151. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  152. MODULE_AUTHOR("Imre Kaloz <[email protected]>");
  153. MODULE_AUTHOR("Bert Vermeulen <[email protected]>");
  154. MODULE_AUTHOR("Christopher Hill <[email protected]");
  155. MODULE_LICENSE("GPL v2");