meson_spifc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  4. * Copyright (C) 2018 BayLibre, SAS
  5. * Author: Neil Armstrong <narmstrong@baylibre.com>
  6. *
  7. * Amlogic Meson SPI Flash Controller driver
  8. */
  9. #include <common.h>
  10. #include <spi.h>
  11. #include <clk.h>
  12. #include <dm.h>
  13. #include <regmap.h>
  14. #include <errno.h>
  15. #include <asm/io.h>
  16. #include <linux/bitfield.h>
  17. /* register map */
  18. #define REG_CMD 0x00
  19. #define REG_ADDR 0x04
  20. #define REG_CTRL 0x08
  21. #define REG_CTRL1 0x0c
  22. #define REG_STATUS 0x10
  23. #define REG_CTRL2 0x14
  24. #define REG_CLOCK 0x18
  25. #define REG_USER 0x1c
  26. #define REG_USER1 0x20
  27. #define REG_USER2 0x24
  28. #define REG_USER3 0x28
  29. #define REG_USER4 0x2c
  30. #define REG_SLAVE 0x30
  31. #define REG_SLAVE1 0x34
  32. #define REG_SLAVE2 0x38
  33. #define REG_SLAVE3 0x3c
  34. #define REG_C0 0x40
  35. #define REG_B8 0x60
  36. #define REG_MAX 0x7c
  37. /* register fields */
  38. #define CMD_USER BIT(18)
  39. #define CTRL_ENABLE_AHB BIT(17)
  40. #define CLOCK_SOURCE BIT(31)
  41. #define CLOCK_DIV_SHIFT 12
  42. #define CLOCK_DIV_MASK (0x3f << CLOCK_DIV_SHIFT)
  43. #define CLOCK_CNT_HIGH_SHIFT 6
  44. #define CLOCK_CNT_HIGH_MASK (0x3f << CLOCK_CNT_HIGH_SHIFT)
  45. #define CLOCK_CNT_LOW_SHIFT 0
  46. #define CLOCK_CNT_LOW_MASK (0x3f << CLOCK_CNT_LOW_SHIFT)
  47. #define USER_DIN_EN_MS BIT(0)
  48. #define USER_CMP_MODE BIT(2)
  49. #define USER_CLK_NOT_INV BIT(7)
  50. #define USER_UC_DOUT_SEL BIT(27)
  51. #define USER_UC_DIN_SEL BIT(28)
  52. #define USER_UC_MASK ((BIT(5) - 1) << 27)
  53. #define USER1_BN_UC_DOUT_SHIFT 17
  54. #define USER1_BN_UC_DOUT_MASK (0xff << 16)
  55. #define USER1_BN_UC_DIN_SHIFT 8
  56. #define USER1_BN_UC_DIN_MASK (0xff << 8)
  57. #define USER4_CS_POL_HIGH BIT(23)
  58. #define USER4_IDLE_CLK_HIGH BIT(29)
  59. #define USER4_CS_ACT BIT(30)
  60. #define SLAVE_TRST_DONE BIT(4)
  61. #define SLAVE_OP_MODE BIT(30)
  62. #define SLAVE_SW_RST BIT(31)
  63. #define SPIFC_BUFFER_SIZE 64
  64. struct meson_spifc_priv {
  65. struct regmap *regmap;
  66. struct clk clk;
  67. };
  68. /**
  69. * meson_spifc_drain_buffer() - copy data from device buffer to memory
  70. * @spifc: the Meson SPI device
  71. * @buf: the destination buffer
  72. * @len: number of bytes to copy
  73. */
  74. static void meson_spifc_drain_buffer(struct meson_spifc_priv *spifc,
  75. u8 *buf, int len)
  76. {
  77. u32 data;
  78. int i = 0;
  79. while (i < len) {
  80. regmap_read(spifc->regmap, REG_C0 + i, &data);
  81. if (len - i >= 4) {
  82. *((u32 *)buf) = data;
  83. buf += 4;
  84. } else {
  85. memcpy(buf, &data, len - i);
  86. break;
  87. }
  88. i += 4;
  89. }
  90. }
  91. /**
  92. * meson_spifc_fill_buffer() - copy data from memory to device buffer
  93. * @spifc: the Meson SPI device
  94. * @buf: the source buffer
  95. * @len: number of bytes to copy
  96. */
  97. static void meson_spifc_fill_buffer(struct meson_spifc_priv *spifc,
  98. const u8 *buf, int len)
  99. {
  100. u32 data = 0;
  101. int i = 0;
  102. while (i < len) {
  103. if (len - i >= 4)
  104. data = *(u32 *)buf;
  105. else
  106. memcpy(&data, buf, len - i);
  107. regmap_write(spifc->regmap, REG_C0 + i, data);
  108. buf += 4;
  109. i += 4;
  110. }
  111. }
  112. /**
  113. * meson_spifc_txrx() - transfer a chunk of data
  114. * @spifc: the Meson SPI device
  115. * @dout: data buffer for TX
  116. * @din: data buffer for RX
  117. * @offset: offset of the data to transfer
  118. * @len: length of the data to transfer
  119. * @last_xfer: whether this is the last transfer of the message
  120. * @last_chunk: whether this is the last chunk of the transfer
  121. * Return: 0 on success, a negative value on error
  122. */
  123. static int meson_spifc_txrx(struct meson_spifc_priv *spifc,
  124. const u8 *dout, u8 *din, int offset,
  125. int len, bool last_xfer, bool last_chunk)
  126. {
  127. bool keep_cs = true;
  128. u32 data;
  129. int ret;
  130. if (dout)
  131. meson_spifc_fill_buffer(spifc, dout + offset, len);
  132. /* enable DOUT stage */
  133. regmap_update_bits(spifc->regmap, REG_USER, USER_UC_MASK,
  134. USER_UC_DOUT_SEL);
  135. regmap_write(spifc->regmap, REG_USER1,
  136. (8 * len - 1) << USER1_BN_UC_DOUT_SHIFT);
  137. /* enable data input during DOUT */
  138. regmap_update_bits(spifc->regmap, REG_USER, USER_DIN_EN_MS,
  139. USER_DIN_EN_MS);
  140. if (last_chunk && last_xfer)
  141. keep_cs = false;
  142. regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_ACT,
  143. keep_cs ? USER4_CS_ACT : 0);
  144. /* clear transition done bit */
  145. regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_TRST_DONE, 0);
  146. /* start transfer */
  147. regmap_update_bits(spifc->regmap, REG_CMD, CMD_USER, CMD_USER);
  148. /* wait for the current operation to terminate */
  149. ret = regmap_read_poll_timeout(spifc->regmap, REG_SLAVE, data,
  150. (data & SLAVE_TRST_DONE),
  151. 0, 5 * CONFIG_SYS_HZ);
  152. if (!ret && din)
  153. meson_spifc_drain_buffer(spifc, din + offset, len);
  154. return ret;
  155. }
  156. /**
  157. * meson_spifc_xfer() - perform a single transfer
  158. * @dev: the SPI controller device
  159. * @bitlen: length of the transfer
  160. * @dout: data buffer for TX
  161. * @din: data buffer for RX
  162. * @flags: transfer flags
  163. * Return: 0 on success, a negative value on error
  164. */
  165. static int meson_spifc_xfer(struct udevice *slave, unsigned int bitlen,
  166. const void *dout, void *din, unsigned long flags)
  167. {
  168. struct meson_spifc_priv *spifc = dev_get_priv(slave->parent);
  169. int blen = bitlen / 8;
  170. int len, done = 0, ret = 0;
  171. if (bitlen % 8)
  172. return -EINVAL;
  173. debug("xfer len %d (%d) dout %p din %p\n", bitlen, blen, dout, din);
  174. regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB, 0);
  175. while (done < blen && !ret) {
  176. len = min_t(int, blen - done, SPIFC_BUFFER_SIZE);
  177. ret = meson_spifc_txrx(spifc, dout, din, done, len,
  178. flags & SPI_XFER_END,
  179. done + len >= blen);
  180. done += len;
  181. }
  182. regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB,
  183. CTRL_ENABLE_AHB);
  184. return ret;
  185. }
  186. /**
  187. * meson_spifc_set_speed() - program the clock divider
  188. * @dev: the SPI controller device
  189. * @speed: desired speed in Hz
  190. */
  191. static int meson_spifc_set_speed(struct udevice *dev, uint speed)
  192. {
  193. struct meson_spifc_priv *spifc = dev_get_priv(dev);
  194. unsigned long parent, value;
  195. int n;
  196. parent = clk_get_rate(&spifc->clk);
  197. n = max_t(int, parent / speed - 1, 1);
  198. debug("parent %lu, speed %u, n %d\n", parent, speed, n);
  199. value = (n << CLOCK_DIV_SHIFT) & CLOCK_DIV_MASK;
  200. value |= (n << CLOCK_CNT_LOW_SHIFT) & CLOCK_CNT_LOW_MASK;
  201. value |= (((n + 1) / 2 - 1) << CLOCK_CNT_HIGH_SHIFT) &
  202. CLOCK_CNT_HIGH_MASK;
  203. regmap_write(spifc->regmap, REG_CLOCK, value);
  204. return 0;
  205. }
  206. /**
  207. * meson_spifc_set_mode() - setups the SPI bus mode
  208. * @dev: the SPI controller device
  209. * @mode: desired mode bitfield
  210. * Return: 0 on success, -ENODEV on error
  211. */
  212. static int meson_spifc_set_mode(struct udevice *dev, uint mode)
  213. {
  214. struct meson_spifc_priv *spifc = dev_get_priv(dev);
  215. if (mode & (SPI_CPHA | SPI_RX_QUAD | SPI_RX_DUAL |
  216. SPI_TX_QUAD | SPI_TX_DUAL))
  217. return -ENODEV;
  218. regmap_update_bits(spifc->regmap, REG_USER, USER_CLK_NOT_INV,
  219. mode & SPI_CPOL ? USER_CLK_NOT_INV : 0);
  220. regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_POL_HIGH,
  221. mode & SPI_CS_HIGH ? USER4_CS_POL_HIGH : 0);
  222. return 0;
  223. }
  224. /**
  225. * meson_spifc_hw_init() - reset and initialize the SPI controller
  226. * @spifc: the Meson SPI device
  227. */
  228. static void meson_spifc_hw_init(struct meson_spifc_priv *spifc)
  229. {
  230. /* reset device */
  231. regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_SW_RST,
  232. SLAVE_SW_RST);
  233. /* disable compatible mode */
  234. regmap_update_bits(spifc->regmap, REG_USER, USER_CMP_MODE, 0);
  235. /* set master mode */
  236. regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_OP_MODE, 0);
  237. }
  238. static const struct dm_spi_ops meson_spifc_ops = {
  239. .xfer = meson_spifc_xfer,
  240. .set_speed = meson_spifc_set_speed,
  241. .set_mode = meson_spifc_set_mode,
  242. };
  243. static int meson_spifc_probe(struct udevice *dev)
  244. {
  245. struct meson_spifc_priv *priv = dev_get_priv(dev);
  246. int ret;
  247. ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
  248. if (ret)
  249. return ret;
  250. ret = clk_get_by_index(dev, 0, &priv->clk);
  251. if (ret)
  252. return ret;
  253. ret = clk_enable(&priv->clk);
  254. if (ret)
  255. return ret;
  256. meson_spifc_hw_init(priv);
  257. return 0;
  258. }
  259. static const struct udevice_id meson_spifc_ids[] = {
  260. { .compatible = "amlogic,meson-gxbb-spifc", },
  261. { }
  262. };
  263. U_BOOT_DRIVER(meson_spifc) = {
  264. .name = "meson_spifc",
  265. .id = UCLASS_SPI,
  266. .of_match = meson_spifc_ids,
  267. .ops = &meson_spifc_ops,
  268. .probe = meson_spifc_probe,
  269. .priv_auto_alloc_size = sizeof(struct meson_spifc_priv),
  270. };