cadence_qspi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012
  4. * Altera Corporation <www.altera.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <malloc.h>
  10. #include <spi.h>
  11. #include <linux/errno.h>
  12. #include "cadence_qspi.h"
  13. #define CQSPI_STIG_READ 0
  14. #define CQSPI_STIG_WRITE 1
  15. #define CQSPI_INDIRECT_READ 2
  16. #define CQSPI_INDIRECT_WRITE 3
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static int cadence_spi_write_speed(struct udevice *bus, uint hz)
  19. {
  20. struct cadence_spi_platdata *plat = bus->platdata;
  21. struct cadence_spi_priv *priv = dev_get_priv(bus);
  22. cadence_qspi_apb_config_baudrate_div(priv->regbase,
  23. CONFIG_CQSPI_REF_CLK, hz);
  24. /* Reconfigure delay timing if speed is changed. */
  25. cadence_qspi_apb_delay(priv->regbase, CONFIG_CQSPI_REF_CLK, hz,
  26. plat->tshsl_ns, plat->tsd2d_ns,
  27. plat->tchsh_ns, plat->tslch_ns);
  28. return 0;
  29. }
  30. /* Calibration sequence to determine the read data capture delay register */
  31. static int spi_calibration(struct udevice *bus, uint hz)
  32. {
  33. struct cadence_spi_priv *priv = dev_get_priv(bus);
  34. void *base = priv->regbase;
  35. u8 opcode_rdid = 0x9F;
  36. unsigned int idcode = 0, temp = 0;
  37. int err = 0, i, range_lo = -1, range_hi = -1;
  38. /* start with slowest clock (1 MHz) */
  39. cadence_spi_write_speed(bus, 1000000);
  40. /* configure the read data capture delay register to 0 */
  41. cadence_qspi_apb_readdata_capture(base, 1, 0);
  42. /* Enable QSPI */
  43. cadence_qspi_apb_controller_enable(base);
  44. /* read the ID which will be our golden value */
  45. err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
  46. 3, (u8 *)&idcode);
  47. if (err) {
  48. puts("SF: Calibration failed (read)\n");
  49. return err;
  50. }
  51. /* use back the intended clock and find low range */
  52. cadence_spi_write_speed(bus, hz);
  53. for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
  54. /* Disable QSPI */
  55. cadence_qspi_apb_controller_disable(base);
  56. /* reconfigure the read data capture delay register */
  57. cadence_qspi_apb_readdata_capture(base, 1, i);
  58. /* Enable back QSPI */
  59. cadence_qspi_apb_controller_enable(base);
  60. /* issue a RDID to get the ID value */
  61. err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
  62. 3, (u8 *)&temp);
  63. if (err) {
  64. puts("SF: Calibration failed (read)\n");
  65. return err;
  66. }
  67. /* search for range lo */
  68. if (range_lo == -1 && temp == idcode) {
  69. range_lo = i;
  70. continue;
  71. }
  72. /* search for range hi */
  73. if (range_lo != -1 && temp != idcode) {
  74. range_hi = i - 1;
  75. break;
  76. }
  77. range_hi = i;
  78. }
  79. if (range_lo == -1) {
  80. puts("SF: Calibration failed (low range)\n");
  81. return err;
  82. }
  83. /* Disable QSPI for subsequent initialization */
  84. cadence_qspi_apb_controller_disable(base);
  85. /* configure the final value for read data capture delay register */
  86. cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
  87. debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
  88. (range_hi + range_lo) / 2, range_lo, range_hi);
  89. /* just to ensure we do once only when speed or chip select change */
  90. priv->qspi_calibrated_hz = hz;
  91. priv->qspi_calibrated_cs = spi_chip_select(bus);
  92. return 0;
  93. }
  94. static int cadence_spi_set_speed(struct udevice *bus, uint hz)
  95. {
  96. struct cadence_spi_platdata *plat = bus->platdata;
  97. struct cadence_spi_priv *priv = dev_get_priv(bus);
  98. int err;
  99. if (hz > plat->max_hz)
  100. hz = plat->max_hz;
  101. /* Disable QSPI */
  102. cadence_qspi_apb_controller_disable(priv->regbase);
  103. /*
  104. * Calibration required for different current SCLK speed, requested
  105. * SCLK speed or chip select
  106. */
  107. if (priv->previous_hz != hz ||
  108. priv->qspi_calibrated_hz != hz ||
  109. priv->qspi_calibrated_cs != spi_chip_select(bus)) {
  110. err = spi_calibration(bus, hz);
  111. if (err)
  112. return err;
  113. /* prevent calibration run when same as previous request */
  114. priv->previous_hz = hz;
  115. }
  116. /* Enable QSPI */
  117. cadence_qspi_apb_controller_enable(priv->regbase);
  118. debug("%s: speed=%d\n", __func__, hz);
  119. return 0;
  120. }
  121. static int cadence_spi_probe(struct udevice *bus)
  122. {
  123. struct cadence_spi_platdata *plat = bus->platdata;
  124. struct cadence_spi_priv *priv = dev_get_priv(bus);
  125. priv->regbase = plat->regbase;
  126. priv->ahbbase = plat->ahbbase;
  127. if (!priv->qspi_is_init) {
  128. cadence_qspi_apb_controller_init(plat);
  129. priv->qspi_is_init = 1;
  130. }
  131. return 0;
  132. }
  133. static int cadence_spi_set_mode(struct udevice *bus, uint mode)
  134. {
  135. struct cadence_spi_priv *priv = dev_get_priv(bus);
  136. /* Disable QSPI */
  137. cadence_qspi_apb_controller_disable(priv->regbase);
  138. /* Set SPI mode */
  139. cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
  140. /* Enable QSPI */
  141. cadence_qspi_apb_controller_enable(priv->regbase);
  142. return 0;
  143. }
  144. static int cadence_spi_xfer(struct udevice *dev, unsigned int bitlen,
  145. const void *dout, void *din, unsigned long flags)
  146. {
  147. struct udevice *bus = dev->parent;
  148. struct cadence_spi_platdata *plat = bus->platdata;
  149. struct cadence_spi_priv *priv = dev_get_priv(bus);
  150. struct dm_spi_slave_platdata *dm_plat = dev_get_parent_platdata(dev);
  151. void *base = priv->regbase;
  152. u8 *cmd_buf = priv->cmd_buf;
  153. size_t data_bytes;
  154. int err = 0;
  155. u32 mode = CQSPI_STIG_WRITE;
  156. if (flags & SPI_XFER_BEGIN) {
  157. /* copy command to local buffer */
  158. priv->cmd_len = bitlen / 8;
  159. memcpy(cmd_buf, dout, priv->cmd_len);
  160. }
  161. if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) {
  162. /* if start and end bit are set, the data bytes is 0. */
  163. data_bytes = 0;
  164. } else {
  165. data_bytes = bitlen / 8;
  166. }
  167. debug("%s: len=%zu [bytes]\n", __func__, data_bytes);
  168. /* Set Chip select */
  169. cadence_qspi_apb_chipselect(base, spi_chip_select(dev),
  170. plat->is_decoded_cs);
  171. if ((flags & SPI_XFER_END) || (flags == 0)) {
  172. if (priv->cmd_len == 0) {
  173. printf("QSPI: Error, command is empty.\n");
  174. return -1;
  175. }
  176. if (din && data_bytes) {
  177. /* read */
  178. /* Use STIG if no address. */
  179. if (!CQSPI_IS_ADDR(priv->cmd_len))
  180. mode = CQSPI_STIG_READ;
  181. else
  182. mode = CQSPI_INDIRECT_READ;
  183. } else if (dout && !(flags & SPI_XFER_BEGIN)) {
  184. /* write */
  185. if (!CQSPI_IS_ADDR(priv->cmd_len))
  186. mode = CQSPI_STIG_WRITE;
  187. else
  188. mode = CQSPI_INDIRECT_WRITE;
  189. }
  190. switch (mode) {
  191. case CQSPI_STIG_READ:
  192. err = cadence_qspi_apb_command_read(
  193. base, priv->cmd_len, cmd_buf,
  194. data_bytes, din);
  195. break;
  196. case CQSPI_STIG_WRITE:
  197. err = cadence_qspi_apb_command_write(base,
  198. priv->cmd_len, cmd_buf,
  199. data_bytes, dout);
  200. break;
  201. case CQSPI_INDIRECT_READ:
  202. err = cadence_qspi_apb_indirect_read_setup(plat,
  203. priv->cmd_len, dm_plat->mode, cmd_buf);
  204. if (!err) {
  205. err = cadence_qspi_apb_indirect_read_execute
  206. (plat, data_bytes, din);
  207. }
  208. break;
  209. case CQSPI_INDIRECT_WRITE:
  210. err = cadence_qspi_apb_indirect_write_setup
  211. (plat, priv->cmd_len, cmd_buf);
  212. if (!err) {
  213. err = cadence_qspi_apb_indirect_write_execute
  214. (plat, data_bytes, dout);
  215. }
  216. break;
  217. default:
  218. err = -1;
  219. break;
  220. }
  221. if (flags & SPI_XFER_END) {
  222. /* clear command buffer */
  223. memset(cmd_buf, 0, sizeof(priv->cmd_buf));
  224. priv->cmd_len = 0;
  225. }
  226. }
  227. return err;
  228. }
  229. static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
  230. {
  231. struct cadence_spi_platdata *plat = bus->platdata;
  232. const void *blob = gd->fdt_blob;
  233. int node = dev_of_offset(bus);
  234. int subnode;
  235. plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
  236. plat->ahbbase = (void *)devfdt_get_addr_index(bus, 1);
  237. plat->is_decoded_cs = fdtdec_get_bool(blob, node, "cdns,is-decoded-cs");
  238. plat->fifo_depth = fdtdec_get_uint(blob, node, "cdns,fifo-depth", 128);
  239. plat->fifo_width = fdtdec_get_uint(blob, node, "cdns,fifo-width", 4);
  240. plat->trigger_address = fdtdec_get_uint(blob, node,
  241. "cdns,trigger-address", 0);
  242. /* All other paramters are embedded in the child node */
  243. subnode = fdt_first_subnode(blob, node);
  244. if (subnode < 0) {
  245. printf("Error: subnode with SPI flash config missing!\n");
  246. return -ENODEV;
  247. }
  248. /* Use 500 KHz as a suitable default */
  249. plat->max_hz = fdtdec_get_uint(blob, subnode, "spi-max-frequency",
  250. 500000);
  251. /* Read other parameters from DT */
  252. plat->page_size = fdtdec_get_uint(blob, subnode, "page-size", 256);
  253. plat->block_size = fdtdec_get_uint(blob, subnode, "block-size", 16);
  254. plat->tshsl_ns = fdtdec_get_uint(blob, subnode, "cdns,tshsl-ns", 200);
  255. plat->tsd2d_ns = fdtdec_get_uint(blob, subnode, "cdns,tsd2d-ns", 255);
  256. plat->tchsh_ns = fdtdec_get_uint(blob, subnode, "cdns,tchsh-ns", 20);
  257. plat->tslch_ns = fdtdec_get_uint(blob, subnode, "cdns,tslch-ns", 20);
  258. debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
  259. __func__, plat->regbase, plat->ahbbase, plat->max_hz,
  260. plat->page_size);
  261. return 0;
  262. }
  263. static const struct dm_spi_ops cadence_spi_ops = {
  264. .xfer = cadence_spi_xfer,
  265. .set_speed = cadence_spi_set_speed,
  266. .set_mode = cadence_spi_set_mode,
  267. /*
  268. * cs_info is not needed, since we require all chip selects to be
  269. * in the device tree explicitly
  270. */
  271. };
  272. static const struct udevice_id cadence_spi_ids[] = {
  273. { .compatible = "cadence,qspi" },
  274. { }
  275. };
  276. U_BOOT_DRIVER(cadence_spi) = {
  277. .name = "cadence_spi",
  278. .id = UCLASS_SPI,
  279. .of_match = cadence_spi_ids,
  280. .ops = &cadence_spi_ops,
  281. .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
  282. .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
  283. .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
  284. .probe = cadence_spi_probe,
  285. };