spi-gxp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Copyright (C) 2022 Hewlett-Packard Development Company, L.P. */
  3. #include <linux/iopoll.h>
  4. #include <linux/of.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/spi/spi.h>
  7. #include <linux/spi/spi-mem.h>
  8. #define GXP_SPI0_MAX_CHIPSELECT 2
  9. #define GXP_SPI_SLEEP_TIME 1
  10. #define GXP_SPI_TIMEOUT (130 * 1000000 / GXP_SPI_SLEEP_TIME)
  11. #define MANUAL_MODE 0
  12. #define DIRECT_MODE 1
  13. #define SPILDAT_LEN 256
  14. #define OFFSET_SPIMCFG 0x0
  15. #define OFFSET_SPIMCTRL 0x4
  16. #define OFFSET_SPICMD 0x5
  17. #define OFFSET_SPIDCNT 0x6
  18. #define OFFSET_SPIADDR 0x8
  19. #define OFFSET_SPIINTSTS 0xc
  20. #define SPIMCTRL_START 0x01
  21. #define SPIMCTRL_BUSY 0x02
  22. #define SPIMCTRL_DIR 0x08
  23. struct gxp_spi;
  24. struct gxp_spi_chip {
  25. struct gxp_spi *spifi;
  26. u32 cs;
  27. };
  28. struct gxp_spi_data {
  29. u32 max_cs;
  30. u32 mode_bits;
  31. };
  32. struct gxp_spi {
  33. const struct gxp_spi_data *data;
  34. void __iomem *reg_base;
  35. void __iomem *dat_base;
  36. void __iomem *dir_base;
  37. struct device *dev;
  38. struct gxp_spi_chip chips[GXP_SPI0_MAX_CHIPSELECT];
  39. };
  40. static void gxp_spi_set_mode(struct gxp_spi *spifi, int mode)
  41. {
  42. u8 value;
  43. void __iomem *reg_base = spifi->reg_base;
  44. value = readb(reg_base + OFFSET_SPIMCTRL);
  45. if (mode == MANUAL_MODE) {
  46. writeb(0x55, reg_base + OFFSET_SPICMD);
  47. writeb(0xaa, reg_base + OFFSET_SPICMD);
  48. value &= ~0x30;
  49. } else {
  50. value |= 0x30;
  51. }
  52. writeb(value, reg_base + OFFSET_SPIMCTRL);
  53. }
  54. static int gxp_spi_read_reg(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
  55. {
  56. int ret;
  57. struct gxp_spi *spifi = chip->spifi;
  58. void __iomem *reg_base = spifi->reg_base;
  59. u32 value;
  60. value = readl(reg_base + OFFSET_SPIMCFG);
  61. value &= ~(1 << 24);
  62. value |= (chip->cs << 24);
  63. value &= ~(0x07 << 16);
  64. value &= ~(0x1f << 19);
  65. writel(value, reg_base + OFFSET_SPIMCFG);
  66. writel(0, reg_base + OFFSET_SPIADDR);
  67. writeb(op->cmd.opcode, reg_base + OFFSET_SPICMD);
  68. writew(op->data.nbytes, reg_base + OFFSET_SPIDCNT);
  69. value = readb(reg_base + OFFSET_SPIMCTRL);
  70. value &= ~SPIMCTRL_DIR;
  71. value |= SPIMCTRL_START;
  72. writeb(value, reg_base + OFFSET_SPIMCTRL);
  73. ret = readb_poll_timeout(reg_base + OFFSET_SPIMCTRL, value,
  74. !(value & SPIMCTRL_BUSY),
  75. GXP_SPI_SLEEP_TIME, GXP_SPI_TIMEOUT);
  76. if (ret) {
  77. dev_warn(spifi->dev, "read reg busy time out\n");
  78. return ret;
  79. }
  80. memcpy_fromio(op->data.buf.in, spifi->dat_base, op->data.nbytes);
  81. return ret;
  82. }
  83. static int gxp_spi_write_reg(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
  84. {
  85. int ret;
  86. struct gxp_spi *spifi = chip->spifi;
  87. void __iomem *reg_base = spifi->reg_base;
  88. u32 value;
  89. value = readl(reg_base + OFFSET_SPIMCFG);
  90. value &= ~(1 << 24);
  91. value |= (chip->cs << 24);
  92. value &= ~(0x07 << 16);
  93. value &= ~(0x1f << 19);
  94. writel(value, reg_base + OFFSET_SPIMCFG);
  95. writel(0, reg_base + OFFSET_SPIADDR);
  96. writeb(op->cmd.opcode, reg_base + OFFSET_SPICMD);
  97. memcpy_toio(spifi->dat_base, op->data.buf.in, op->data.nbytes);
  98. writew(op->data.nbytes, reg_base + OFFSET_SPIDCNT);
  99. value = readb(reg_base + OFFSET_SPIMCTRL);
  100. value |= SPIMCTRL_DIR;
  101. value |= SPIMCTRL_START;
  102. writeb(value, reg_base + OFFSET_SPIMCTRL);
  103. ret = readb_poll_timeout(reg_base + OFFSET_SPIMCTRL, value,
  104. !(value & SPIMCTRL_BUSY),
  105. GXP_SPI_SLEEP_TIME, GXP_SPI_TIMEOUT);
  106. if (ret)
  107. dev_warn(spifi->dev, "write reg busy time out\n");
  108. return ret;
  109. }
  110. static ssize_t gxp_spi_read(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
  111. {
  112. struct gxp_spi *spifi = chip->spifi;
  113. u32 offset = op->addr.val;
  114. if (chip->cs == 0)
  115. offset += 0x4000000;
  116. memcpy_fromio(op->data.buf.in, spifi->dir_base + offset, op->data.nbytes);
  117. return 0;
  118. }
  119. static ssize_t gxp_spi_write(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
  120. {
  121. struct gxp_spi *spifi = chip->spifi;
  122. void __iomem *reg_base = spifi->reg_base;
  123. u32 write_len;
  124. u32 value;
  125. int ret;
  126. write_len = op->data.nbytes;
  127. if (write_len > SPILDAT_LEN)
  128. write_len = SPILDAT_LEN;
  129. value = readl(reg_base + OFFSET_SPIMCFG);
  130. value &= ~(1 << 24);
  131. value |= (chip->cs << 24);
  132. value &= ~(0x07 << 16);
  133. value |= (op->addr.nbytes << 16);
  134. value &= ~(0x1f << 19);
  135. writel(value, reg_base + OFFSET_SPIMCFG);
  136. writel(op->addr.val, reg_base + OFFSET_SPIADDR);
  137. writeb(op->cmd.opcode, reg_base + OFFSET_SPICMD);
  138. writew(write_len, reg_base + OFFSET_SPIDCNT);
  139. memcpy_toio(spifi->dat_base, op->data.buf.in, write_len);
  140. value = readb(reg_base + OFFSET_SPIMCTRL);
  141. value |= SPIMCTRL_DIR;
  142. value |= SPIMCTRL_START;
  143. writeb(value, reg_base + OFFSET_SPIMCTRL);
  144. ret = readb_poll_timeout(reg_base + OFFSET_SPIMCTRL, value,
  145. !(value & SPIMCTRL_BUSY),
  146. GXP_SPI_SLEEP_TIME, GXP_SPI_TIMEOUT);
  147. if (ret) {
  148. dev_warn(spifi->dev, "write busy time out\n");
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static int do_gxp_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
  154. {
  155. struct gxp_spi *spifi = spi_controller_get_devdata(mem->spi->controller);
  156. struct gxp_spi_chip *chip = &spifi->chips[spi_get_chipselect(mem->spi, 0)];
  157. int ret;
  158. if (op->data.dir == SPI_MEM_DATA_IN) {
  159. if (!op->addr.nbytes)
  160. ret = gxp_spi_read_reg(chip, op);
  161. else
  162. ret = gxp_spi_read(chip, op);
  163. } else {
  164. if (!op->addr.nbytes)
  165. ret = gxp_spi_write_reg(chip, op);
  166. else
  167. ret = gxp_spi_write(chip, op);
  168. }
  169. return ret;
  170. }
  171. static int gxp_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
  172. {
  173. int ret;
  174. ret = do_gxp_exec_mem_op(mem, op);
  175. if (ret)
  176. dev_err(&mem->spi->dev, "operation failed: %d", ret);
  177. return ret;
  178. }
  179. static const struct spi_controller_mem_ops gxp_spi_mem_ops = {
  180. .exec_op = gxp_exec_mem_op,
  181. };
  182. static int gxp_spi_setup(struct spi_device *spi)
  183. {
  184. struct gxp_spi *spifi = spi_controller_get_devdata(spi->controller);
  185. unsigned int cs = spi_get_chipselect(spi, 0);
  186. struct gxp_spi_chip *chip = &spifi->chips[cs];
  187. chip->spifi = spifi;
  188. chip->cs = cs;
  189. gxp_spi_set_mode(spifi, MANUAL_MODE);
  190. return 0;
  191. }
  192. static int gxp_spifi_probe(struct platform_device *pdev)
  193. {
  194. struct device *dev = &pdev->dev;
  195. const struct gxp_spi_data *data;
  196. struct spi_controller *ctlr;
  197. struct gxp_spi *spifi;
  198. int ret;
  199. data = of_device_get_match_data(&pdev->dev);
  200. ctlr = devm_spi_alloc_host(dev, sizeof(*spifi));
  201. if (!ctlr)
  202. return -ENOMEM;
  203. spifi = spi_controller_get_devdata(ctlr);
  204. platform_set_drvdata(pdev, spifi);
  205. spifi->data = data;
  206. spifi->dev = dev;
  207. spifi->reg_base = devm_platform_ioremap_resource(pdev, 0);
  208. if (IS_ERR(spifi->reg_base))
  209. return PTR_ERR(spifi->reg_base);
  210. spifi->dat_base = devm_platform_ioremap_resource(pdev, 1);
  211. if (IS_ERR(spifi->dat_base))
  212. return PTR_ERR(spifi->dat_base);
  213. spifi->dir_base = devm_platform_ioremap_resource(pdev, 2);
  214. if (IS_ERR(spifi->dir_base))
  215. return PTR_ERR(spifi->dir_base);
  216. ctlr->mode_bits = data->mode_bits;
  217. ctlr->bus_num = pdev->id;
  218. ctlr->mem_ops = &gxp_spi_mem_ops;
  219. ctlr->setup = gxp_spi_setup;
  220. ctlr->num_chipselect = data->max_cs;
  221. ctlr->dev.of_node = dev->of_node;
  222. ret = devm_spi_register_controller(dev, ctlr);
  223. if (ret) {
  224. return dev_err_probe(&pdev->dev, ret,
  225. "failed to register spi controller\n");
  226. }
  227. return 0;
  228. }
  229. static const struct gxp_spi_data gxp_spifi_data = {
  230. .max_cs = 2,
  231. .mode_bits = 0,
  232. };
  233. static const struct of_device_id gxp_spifi_match[] = {
  234. {.compatible = "hpe,gxp-spifi", .data = &gxp_spifi_data },
  235. { /* null */ }
  236. };
  237. MODULE_DEVICE_TABLE(of, gxp_spifi_match);
  238. static struct platform_driver gxp_spifi_driver = {
  239. .probe = gxp_spifi_probe,
  240. .driver = {
  241. .name = "gxp-spifi",
  242. .of_match_table = gxp_spifi_match,
  243. },
  244. };
  245. module_platform_driver(gxp_spifi_driver);
  246. MODULE_DESCRIPTION("HPE GXP SPI Flash Interface driver");
  247. MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>");
  248. MODULE_LICENSE("GPL");