mt7621_spi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Stefan Roese <sr@denx.de>
  4. *
  5. * Derived from the Linux driver version drivers/spi/spi-mt7621.c
  6. * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
  7. * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
  8. * Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <spi.h>
  13. #include <wait_bit.h>
  14. #include <linux/io.h>
  15. #define SPI_MSG_SIZE_MAX 32 /* SPI message chunk size */
  16. /* Enough for SPI NAND page read / write with page size 2048 bytes */
  17. #define SPI_MSG_SIZE_OVERALL (2048 + 16)
  18. #define MT7621_SPI_TRANS 0x00
  19. #define MT7621_SPI_TRANS_START BIT(8)
  20. #define MT7621_SPI_TRANS_BUSY BIT(16)
  21. #define MT7621_SPI_OPCODE 0x04
  22. #define MT7621_SPI_DATA0 0x08
  23. #define MT7621_SPI_DATA4 0x18
  24. #define MT7621_SPI_MASTER 0x28
  25. #define MT7621_SPI_MOREBUF 0x2c
  26. #define MT7621_SPI_POLAR 0x38
  27. #define MT7621_LSB_FIRST BIT(3)
  28. #define MT7621_CPOL BIT(4)
  29. #define MT7621_CPHA BIT(5)
  30. #define MASTER_MORE_BUFMODE BIT(2)
  31. #define MASTER_RS_CLK_SEL GENMASK(27, 16)
  32. #define MASTER_RS_CLK_SEL_SHIFT 16
  33. #define MASTER_RS_SLAVE_SEL GENMASK(31, 29)
  34. struct mt7621_spi {
  35. void __iomem *base;
  36. unsigned int sys_freq;
  37. u32 data[(SPI_MSG_SIZE_OVERALL / 4) + 1];
  38. int tx_len;
  39. };
  40. static void mt7621_spi_reset(struct mt7621_spi *rs, int duplex)
  41. {
  42. setbits_le32(rs->base + MT7621_SPI_MASTER,
  43. MASTER_RS_SLAVE_SEL | MASTER_MORE_BUFMODE);
  44. }
  45. static void mt7621_spi_set_cs(struct mt7621_spi *rs, int cs, int enable)
  46. {
  47. u32 val = 0;
  48. debug("%s: cs#%d -> %s\n", __func__, cs, enable ? "enable" : "disable");
  49. if (enable)
  50. val = BIT(cs);
  51. iowrite32(val, rs->base + MT7621_SPI_POLAR);
  52. }
  53. static int mt7621_spi_set_mode(struct udevice *bus, uint mode)
  54. {
  55. struct mt7621_spi *rs = dev_get_priv(bus);
  56. u32 reg;
  57. debug("%s: mode=0x%08x\n", __func__, mode);
  58. reg = ioread32(rs->base + MT7621_SPI_MASTER);
  59. reg &= ~MT7621_LSB_FIRST;
  60. if (mode & SPI_LSB_FIRST)
  61. reg |= MT7621_LSB_FIRST;
  62. reg &= ~(MT7621_CPHA | MT7621_CPOL);
  63. switch (mode & (SPI_CPOL | SPI_CPHA)) {
  64. case SPI_MODE_0:
  65. break;
  66. case SPI_MODE_1:
  67. reg |= MT7621_CPHA;
  68. break;
  69. case SPI_MODE_2:
  70. reg |= MT7621_CPOL;
  71. break;
  72. case SPI_MODE_3:
  73. reg |= MT7621_CPOL | MT7621_CPHA;
  74. break;
  75. }
  76. iowrite32(reg, rs->base + MT7621_SPI_MASTER);
  77. return 0;
  78. }
  79. static int mt7621_spi_set_speed(struct udevice *bus, uint speed)
  80. {
  81. struct mt7621_spi *rs = dev_get_priv(bus);
  82. u32 rate;
  83. u32 reg;
  84. debug("%s: speed=%d\n", __func__, speed);
  85. rate = DIV_ROUND_UP(rs->sys_freq, speed);
  86. debug("rate:%u\n", rate);
  87. if (rate > 4097)
  88. return -EINVAL;
  89. if (rate < 2)
  90. rate = 2;
  91. reg = ioread32(rs->base + MT7621_SPI_MASTER);
  92. reg &= ~MASTER_RS_CLK_SEL;
  93. reg |= (rate - 2) << MASTER_RS_CLK_SEL_SHIFT;
  94. iowrite32(reg, rs->base + MT7621_SPI_MASTER);
  95. return 0;
  96. }
  97. static inline int mt7621_spi_wait_till_ready(struct mt7621_spi *rs)
  98. {
  99. int ret;
  100. ret = wait_for_bit_le32(rs->base + MT7621_SPI_TRANS,
  101. MT7621_SPI_TRANS_BUSY, 0, 10, 0);
  102. if (ret)
  103. pr_err("Timeout in %s!\n", __func__);
  104. return ret;
  105. }
  106. static int mt7621_spi_xfer(struct udevice *dev, unsigned int bitlen,
  107. const void *dout, void *din, unsigned long flags)
  108. {
  109. struct udevice *bus = dev->parent;
  110. struct mt7621_spi *rs = dev_get_priv(bus);
  111. const u8 *tx_buf = dout;
  112. u8 *ptr = (u8 *)dout;
  113. u8 *rx_buf = din;
  114. int total_size = bitlen >> 3;
  115. int chunk_size;
  116. int rx_len = 0;
  117. u32 data[(SPI_MSG_SIZE_MAX / 4) + 1] = { 0 };
  118. u32 val;
  119. int i;
  120. debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
  121. total_size, flags);
  122. /*
  123. * This driver only supports half-duplex, so complain and bail out
  124. * upon full-duplex messages
  125. */
  126. if (dout && din) {
  127. printf("Only half-duplex SPI transfer supported\n");
  128. return -EIO;
  129. }
  130. if (dout) {
  131. debug("TX-DATA: ");
  132. for (i = 0; i < total_size; i++)
  133. debug("%02x ", *ptr++);
  134. debug("\n");
  135. }
  136. mt7621_spi_wait_till_ready(rs);
  137. /*
  138. * Set CS active upon start of SPI message. This message can
  139. * be split upon multiple calls to this xfer function
  140. */
  141. if (flags & SPI_XFER_BEGIN)
  142. mt7621_spi_set_cs(rs, spi_chip_select(dev), 1);
  143. while (total_size > 0) {
  144. /* Don't exceed the max xfer size */
  145. chunk_size = min_t(int, total_size, SPI_MSG_SIZE_MAX);
  146. /*
  147. * We might have some TX data buffered from the last xfer
  148. * message. Make sure, that this does not exceed the max
  149. * xfer size
  150. */
  151. if (rs->tx_len > 4)
  152. chunk_size -= rs->tx_len;
  153. if (din)
  154. rx_len = chunk_size;
  155. if (tx_buf) {
  156. /* Check if this message does not exceed the buffer */
  157. if ((chunk_size + rs->tx_len) > SPI_MSG_SIZE_OVERALL) {
  158. printf("TX message size too big (%d)\n",
  159. chunk_size + rs->tx_len);
  160. return -EMSGSIZE;
  161. }
  162. /*
  163. * Write all TX data into internal buffer to collect
  164. * all TX messages into one buffer (might be split into
  165. * multiple calls to this function)
  166. */
  167. for (i = 0; i < chunk_size; i++, rs->tx_len++) {
  168. rs->data[rs->tx_len / 4] |=
  169. tx_buf[i] << (8 * (rs->tx_len & 3));
  170. }
  171. }
  172. if (flags & SPI_XFER_END) {
  173. /* Write TX data into controller */
  174. if (rs->tx_len) {
  175. rs->data[0] = swab32(rs->data[0]);
  176. if (rs->tx_len < 4)
  177. rs->data[0] >>= (4 - rs->tx_len) * 8;
  178. for (i = 0; i < rs->tx_len; i += 4) {
  179. iowrite32(rs->data[i / 4], rs->base +
  180. MT7621_SPI_OPCODE + i);
  181. }
  182. }
  183. /* Write length into controller */
  184. val = (min_t(int, rs->tx_len, 4) * 8) << 24;
  185. if (rs->tx_len > 4)
  186. val |= (rs->tx_len - 4) * 8;
  187. val |= (rx_len * 8) << 12;
  188. iowrite32(val, rs->base + MT7621_SPI_MOREBUF);
  189. /* Start the xfer */
  190. setbits_le32(rs->base + MT7621_SPI_TRANS,
  191. MT7621_SPI_TRANS_START);
  192. /* Wait until xfer is finished on bus */
  193. mt7621_spi_wait_till_ready(rs);
  194. /* Reset TX length and TX buffer for next xfer */
  195. rs->tx_len = 0;
  196. memset(rs->data, 0, sizeof(rs->data));
  197. }
  198. for (i = 0; i < rx_len; i += 4)
  199. data[i / 4] = ioread32(rs->base + MT7621_SPI_DATA0 + i);
  200. if (rx_len) {
  201. debug("RX-DATA: ");
  202. for (i = 0; i < rx_len; i++) {
  203. rx_buf[i] = data[i / 4] >> (8 * (i & 3));
  204. debug("%02x ", rx_buf[i]);
  205. }
  206. debug("\n");
  207. }
  208. if (tx_buf)
  209. tx_buf += chunk_size;
  210. if (rx_buf)
  211. rx_buf += chunk_size;
  212. total_size -= chunk_size;
  213. }
  214. /* Wait until xfer is finished on bus and de-assert CS */
  215. mt7621_spi_wait_till_ready(rs);
  216. if (flags & SPI_XFER_END)
  217. mt7621_spi_set_cs(rs, spi_chip_select(dev), 0);
  218. return 0;
  219. }
  220. static int mt7621_spi_probe(struct udevice *dev)
  221. {
  222. struct mt7621_spi *rs = dev_get_priv(dev);
  223. rs->base = dev_remap_addr(dev);
  224. if (!rs->base)
  225. return -EINVAL;
  226. /*
  227. * Read input clock via DT for now. At some point this should be
  228. * replaced by implementing a clock driver for this SoC and getting
  229. * the SPI frequency via this clock driver.
  230. */
  231. rs->sys_freq = dev_read_u32_default(dev, "clock-frequency", 0);
  232. if (!rs->sys_freq) {
  233. printf("Please provide clock-frequency!\n");
  234. return -EINVAL;
  235. }
  236. mt7621_spi_reset(rs, 0);
  237. return 0;
  238. }
  239. static const struct dm_spi_ops mt7621_spi_ops = {
  240. .set_mode = mt7621_spi_set_mode,
  241. .set_speed = mt7621_spi_set_speed,
  242. .xfer = mt7621_spi_xfer,
  243. /*
  244. * cs_info is not needed, since we require all chip selects to be
  245. * in the device tree explicitly
  246. */
  247. };
  248. static const struct udevice_id mt7621_spi_ids[] = {
  249. { .compatible = "ralink,mt7621-spi" },
  250. { }
  251. };
  252. U_BOOT_DRIVER(mt7621_spi) = {
  253. .name = "mt7621_spi",
  254. .id = UCLASS_SPI,
  255. .of_match = mt7621_spi_ids,
  256. .ops = &mt7621_spi_ops,
  257. .priv_auto_alloc_size = sizeof(struct mt7621_spi),
  258. .probe = mt7621_spi_probe,
  259. };