spi-gxp.c 7.5 KB

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