0017-spi-Add-SiFive-SPI-driver.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. From 912588aa9f43bc085aa79428275935ac1327620e Mon Sep 17 00:00:00 2001
  2. From: Bhargav Shah <bhargavshah1988@gmail.com>
  3. Date: Sun, 27 Jan 2019 00:19:43 +0530
  4. Subject: [PATCH 17/21] spi: Add SiFive SPI driver
  5. This patch adds SiFive SPI driver. The driver is 100% DM driver
  6. and it determines input clock using clk framework.
  7. The SiFive SPI block is found on SiFive FU540 SOC and is used to
  8. access flash and MMC devices on SiFive Unleashed board.
  9. This driver implementation is inspired from the Linux SiFive SPI
  10. driver available in Linux-5.2 or higher and SiFive FSBL sources.
  11. Signed-off-by: Bhargav Shah <bhargavshah1988@gmail.com>
  12. Signed-off-by: Anup Patel <anup.patel@wdc.com>
  13. Upstream-Status: Submitted
  14. ---
  15. drivers/spi/Kconfig | 8 +
  16. drivers/spi/Makefile | 1 +
  17. drivers/spi/spi-sifive.c | 405 +++++++++++++++++++++++++++++++++++++++
  18. 3 files changed, 414 insertions(+)
  19. create mode 100644 drivers/spi/spi-sifive.c
  20. diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
  21. index eb32f082fe..2712bad310 100644
  22. --- a/drivers/spi/Kconfig
  23. +++ b/drivers/spi/Kconfig
  24. @@ -224,6 +224,14 @@ config SANDBOX_SPI
  25. };
  26. };
  27. +config SIFIVE_SPI
  28. + bool "SiFive SPI driver"
  29. + help
  30. + This driver supports the SiFive SPI IP. If unsure say N.
  31. + Enable the SiFive SPI controller driver.
  32. +
  33. + The SiFive SPI controller driver is found on various SiFive SoCs.
  34. +
  35. config SPI_SUNXI
  36. bool "Allwinner SoC SPI controllers"
  37. help
  38. diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
  39. index 8be9a4baa2..09a9d3697e 100644
  40. --- a/drivers/spi/Makefile
  41. +++ b/drivers/spi/Makefile
  42. @@ -49,6 +49,7 @@ obj-$(CONFIG_PL022_SPI) += pl022_spi.o
  43. obj-$(CONFIG_RENESAS_RPC_SPI) += renesas_rpc_spi.o
  44. obj-$(CONFIG_ROCKCHIP_SPI) += rk_spi.o
  45. obj-$(CONFIG_SANDBOX_SPI) += sandbox_spi.o
  46. +obj-$(CONFIG_SIFIVE_SPI) += spi-sifive.o
  47. obj-$(CONFIG_SPI_SUNXI) += spi-sunxi.o
  48. obj-$(CONFIG_SH_SPI) += sh_spi.o
  49. obj-$(CONFIG_SH_QSPI) += sh_qspi.o
  50. diff --git a/drivers/spi/spi-sifive.c b/drivers/spi/spi-sifive.c
  51. new file mode 100644
  52. index 0000000000..70eebc0463
  53. --- /dev/null
  54. +++ b/drivers/spi/spi-sifive.c
  55. @@ -0,0 +1,405 @@
  56. +// SPDX-License-Identifier: GPL-2.0+
  57. +/*
  58. + * Copyright 2018 SiFive, Inc.
  59. + * Copyright 2019 Bhargav Shah <bhargavshah1988@gmail.com>
  60. + *
  61. + * SiFive SPI controller driver (master mode only)
  62. + */
  63. +
  64. +#include <common.h>
  65. +#include <dm.h>
  66. +#include <malloc.h>
  67. +#include <spi.h>
  68. +#include <asm/io.h>
  69. +#include <linux/log2.h>
  70. +#include <clk.h>
  71. +
  72. +DECLARE_GLOBAL_DATA_PTR;
  73. +
  74. +#define SIFIVE_SPI_MAX_CS 32
  75. +
  76. +#define SIFIVE_SPI_DEFAULT_DEPTH 8
  77. +#define SIFIVE_SPI_DEFAULT_BITS 8
  78. +
  79. +/* register offsets */
  80. +#define SIFIVE_SPI_REG_SCKDIV 0x00 /* Serial clock divisor */
  81. +#define SIFIVE_SPI_REG_SCKMODE 0x04 /* Serial clock mode */
  82. +#define SIFIVE_SPI_REG_CSID 0x10 /* Chip select ID */
  83. +#define SIFIVE_SPI_REG_CSDEF 0x14 /* Chip select default */
  84. +#define SIFIVE_SPI_REG_CSMODE 0x18 /* Chip select mode */
  85. +#define SIFIVE_SPI_REG_DELAY0 0x28 /* Delay control 0 */
  86. +#define SIFIVE_SPI_REG_DELAY1 0x2c /* Delay control 1 */
  87. +#define SIFIVE_SPI_REG_FMT 0x40 /* Frame format */
  88. +#define SIFIVE_SPI_REG_TXDATA 0x48 /* Tx FIFO data */
  89. +#define SIFIVE_SPI_REG_RXDATA 0x4c /* Rx FIFO data */
  90. +#define SIFIVE_SPI_REG_TXMARK 0x50 /* Tx FIFO watermark */
  91. +#define SIFIVE_SPI_REG_RXMARK 0x54 /* Rx FIFO watermark */
  92. +#define SIFIVE_SPI_REG_FCTRL 0x60 /* SPI flash interface control */
  93. +#define SIFIVE_SPI_REG_FFMT 0x64 /* SPI flash instruction format */
  94. +#define SIFIVE_SPI_REG_IE 0x70 /* Interrupt Enable Register */
  95. +#define SIFIVE_SPI_REG_IP 0x74 /* Interrupt Pendings Register */
  96. +
  97. +/* sckdiv bits */
  98. +#define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU
  99. +
  100. +/* sckmode bits */
  101. +#define SIFIVE_SPI_SCKMODE_PHA BIT(0)
  102. +#define SIFIVE_SPI_SCKMODE_POL BIT(1)
  103. +#define SIFIVE_SPI_SCKMODE_MODE_MASK (SIFIVE_SPI_SCKMODE_PHA | \
  104. + SIFIVE_SPI_SCKMODE_POL)
  105. +
  106. +/* csmode bits */
  107. +#define SIFIVE_SPI_CSMODE_MODE_AUTO 0U
  108. +#define SIFIVE_SPI_CSMODE_MODE_HOLD 2U
  109. +#define SIFIVE_SPI_CSMODE_MODE_OFF 3U
  110. +
  111. +/* delay0 bits */
  112. +#define SIFIVE_SPI_DELAY0_CSSCK(x) ((u32)(x))
  113. +#define SIFIVE_SPI_DELAY0_CSSCK_MASK 0xffU
  114. +#define SIFIVE_SPI_DELAY0_SCKCS(x) ((u32)(x) << 16)
  115. +#define SIFIVE_SPI_DELAY0_SCKCS_MASK (0xffU << 16)
  116. +
  117. +/* delay1 bits */
  118. +#define SIFIVE_SPI_DELAY1_INTERCS(x) ((u32)(x))
  119. +#define SIFIVE_SPI_DELAY1_INTERCS_MASK 0xffU
  120. +#define SIFIVE_SPI_DELAY1_INTERXFR(x) ((u32)(x) << 16)
  121. +#define SIFIVE_SPI_DELAY1_INTERXFR_MASK (0xffU << 16)
  122. +
  123. +/* fmt bits */
  124. +#define SIFIVE_SPI_FMT_PROTO_SINGLE 0U
  125. +#define SIFIVE_SPI_FMT_PROTO_DUAL 1U
  126. +#define SIFIVE_SPI_FMT_PROTO_QUAD 2U
  127. +#define SIFIVE_SPI_FMT_PROTO_MASK 3U
  128. +#define SIFIVE_SPI_FMT_ENDIAN BIT(2)
  129. +#define SIFIVE_SPI_FMT_DIR BIT(3)
  130. +#define SIFIVE_SPI_FMT_LEN(x) ((u32)(x) << 16)
  131. +#define SIFIVE_SPI_FMT_LEN_MASK (0xfU << 16)
  132. +
  133. +/* txdata bits */
  134. +#define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU
  135. +#define SIFIVE_SPI_TXDATA_FULL BIT(31)
  136. +
  137. +/* rxdata bits */
  138. +#define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU
  139. +#define SIFIVE_SPI_RXDATA_EMPTY BIT(31)
  140. +
  141. +/* ie and ip bits */
  142. +#define SIFIVE_SPI_IP_TXWM BIT(0)
  143. +#define SIFIVE_SPI_IP_RXWM BIT(1)
  144. +
  145. +struct sifive_spi {
  146. + void *regs; /* base address of the registers */
  147. + u32 fifo_depth;
  148. + u32 bits_per_word;
  149. + u32 cs_inactive; /* Level of the CS pins when inactive*/
  150. + u32 freq;
  151. + u32 num_cs;
  152. +};
  153. +
  154. +static void sifive_spi_write(struct sifive_spi *spi, int offset, u32 value)
  155. +{
  156. + writel(value, spi->regs + offset);
  157. +}
  158. +
  159. +static u32 sifive_spi_read(struct sifive_spi *spi, int offset)
  160. +{
  161. + return readl(spi->regs + offset);
  162. +}
  163. +
  164. +static void sifive_spi_prep_device(struct sifive_spi *spi,
  165. + struct dm_spi_slave_platdata *slave)
  166. +{
  167. + /* Update the chip select polarity */
  168. + if (slave->mode & SPI_CS_HIGH)
  169. + spi->cs_inactive &= ~BIT(slave->cs);
  170. + else
  171. + spi->cs_inactive |= BIT(slave->cs);
  172. + sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, spi->cs_inactive);
  173. +
  174. + /* Select the correct device */
  175. + sifive_spi_write(spi, SIFIVE_SPI_REG_CSID, slave->cs);
  176. +}
  177. +
  178. +static int sifive_spi_set_cs(struct sifive_spi *spi,
  179. + struct dm_spi_slave_platdata *slave)
  180. +{
  181. + u32 cs_mode = SIFIVE_SPI_CSMODE_MODE_HOLD;
  182. +
  183. + if (slave->cs > spi->num_cs)
  184. + return -EINVAL;
  185. +
  186. + if (slave->mode & SPI_CS_HIGH)
  187. + cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;
  188. +
  189. + sifive_spi_write(spi, SIFIVE_SPI_REG_CSMODE, cs_mode);
  190. +
  191. + return 0;
  192. +}
  193. +
  194. +static void sifive_spi_clear_cs(struct sifive_spi *spi)
  195. +{
  196. + sifive_spi_write(spi, SIFIVE_SPI_REG_CSMODE,
  197. + SIFIVE_SPI_CSMODE_MODE_AUTO);
  198. +}
  199. +
  200. +static void sifive_spi_prep_transfer(struct sifive_spi *spi,
  201. + bool is_rx_xfer,
  202. + struct dm_spi_slave_platdata *slave)
  203. +{
  204. + u32 cr;
  205. +
  206. + /* Modify the SPI protocol mode */
  207. + cr = sifive_spi_read(spi, SIFIVE_SPI_REG_FMT);
  208. +
  209. + /* Bits per word ? */
  210. + cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
  211. + cr |= SIFIVE_SPI_FMT_LEN(SIFIVE_SPI_DEFAULT_BITS);
  212. +
  213. + /* LSB first? */
  214. + cr &= ~SIFIVE_SPI_FMT_ENDIAN;
  215. + if (slave->mode & SPI_LSB_FIRST)
  216. + cr |= SIFIVE_SPI_FMT_ENDIAN;
  217. +
  218. + /* Number of wires ? */
  219. + cr &= ~SIFIVE_SPI_FMT_PROTO_MASK;
  220. + if ((slave->mode & SPI_TX_QUAD) || (slave->mode & SPI_RX_QUAD))
  221. + cr |= SIFIVE_SPI_FMT_PROTO_QUAD;
  222. + else if ((slave->mode & SPI_TX_DUAL) || (slave->mode & SPI_RX_DUAL))
  223. + cr |= SIFIVE_SPI_FMT_PROTO_DUAL;
  224. + else
  225. + cr |= SIFIVE_SPI_FMT_PROTO_SINGLE;
  226. +
  227. + /* SPI direction in/out ? */
  228. + cr &= ~SIFIVE_SPI_FMT_DIR;
  229. + if (!is_rx_xfer)
  230. + cr |= SIFIVE_SPI_FMT_DIR;
  231. +
  232. + sifive_spi_write(spi, SIFIVE_SPI_REG_FMT, cr);
  233. +}
  234. +
  235. +static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
  236. +{
  237. + u32 data;
  238. +
  239. + do {
  240. + data = sifive_spi_read(spi, SIFIVE_SPI_REG_RXDATA);
  241. + } while (data & SIFIVE_SPI_RXDATA_EMPTY);
  242. +
  243. + if (rx_ptr)
  244. + *rx_ptr = data & SIFIVE_SPI_RXDATA_DATA_MASK;
  245. +}
  246. +
  247. +static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr)
  248. +{
  249. + u32 data;
  250. + u8 tx_data = (tx_ptr) ? *tx_ptr & SIFIVE_SPI_TXDATA_DATA_MASK :
  251. + SIFIVE_SPI_TXDATA_DATA_MASK;
  252. +
  253. + do {
  254. + data = sifive_spi_read(spi, SIFIVE_SPI_REG_TXDATA);
  255. + } while (data & SIFIVE_SPI_TXDATA_FULL);
  256. +
  257. + sifive_spi_write(spi, SIFIVE_SPI_REG_TXDATA, tx_data);
  258. +}
  259. +
  260. +static u8 sifive_spi_txrx(struct sifive_spi *spi, const u8 *tx_ptr)
  261. +{
  262. + u8 rx = 0;
  263. +
  264. + sifive_spi_tx(spi, tx_ptr);
  265. + sifive_spi_rx(spi, &rx);
  266. +
  267. + return rx;
  268. +}
  269. +
  270. +static int sifive_spi_claim_bus(struct udevice *dev)
  271. +{
  272. + int ret;
  273. + struct udevice *bus = dev->parent;
  274. + struct sifive_spi *spi = dev_get_priv(bus);
  275. + struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
  276. +
  277. + sifive_spi_prep_device(spi, slave);
  278. +
  279. + ret = sifive_spi_set_cs(spi, slave);
  280. + if (ret)
  281. + return ret;
  282. +
  283. + return 0;
  284. +}
  285. +
  286. +static int sifive_spi_release_bus(struct udevice *dev)
  287. +{
  288. + struct sifive_spi *spi = dev_get_priv(dev->parent);
  289. +
  290. + sifive_spi_clear_cs(spi);
  291. +
  292. + return 0;
  293. +}
  294. +
  295. +static int sifive_spi_xfer(struct udevice *dev, unsigned int bitlen,
  296. + const void *dout, void *din, unsigned long flags)
  297. +{
  298. + struct udevice *bus = dev->parent;
  299. + struct sifive_spi *spi = dev_get_priv(bus);
  300. + struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
  301. + const unsigned char *tx_ptr = dout;
  302. + unsigned char *rx_ptr = din;
  303. + u32 remaining_len;
  304. +
  305. + sifive_spi_prep_transfer(spi, true, slave);
  306. +
  307. + remaining_len = bitlen / 8;
  308. +
  309. + while (remaining_len) {
  310. + int n_words, tx_words, rx_words;
  311. +
  312. + n_words = min(remaining_len, spi->fifo_depth);
  313. +
  314. + /* Enqueue n_words for transmission */
  315. + if (tx_ptr) {
  316. + for (tx_words = 0; tx_words < n_words; ++tx_words) {
  317. + sifive_spi_txrx(spi, tx_ptr);
  318. + tx_ptr++;
  319. + }
  320. + }
  321. +
  322. + /* Read out all the data from the RX FIFO */
  323. + if (rx_ptr) {
  324. + for (rx_words = 0; rx_words < n_words; ++rx_words) {
  325. + *rx_ptr = sifive_spi_txrx(spi, NULL);
  326. + rx_ptr++;
  327. + }
  328. + }
  329. +
  330. + remaining_len -= n_words;
  331. + }
  332. +
  333. + return 0;
  334. +}
  335. +
  336. +static int sifive_spi_set_speed(struct udevice *bus, uint speed)
  337. +{
  338. + struct sifive_spi *spi = dev_get_priv(bus);
  339. + u32 scale;
  340. +
  341. + if (speed > spi->freq)
  342. + speed = spi->freq;
  343. +
  344. + /* Cofigure max speed */
  345. + scale = (DIV_ROUND_UP(spi->freq >> 1, speed) - 1)
  346. + & SIFIVE_SPI_SCKDIV_DIV_MASK;
  347. + sifive_spi_write(spi, SIFIVE_SPI_REG_SCKDIV, scale);
  348. + return 0;
  349. +}
  350. +
  351. +static int sifive_spi_set_mode(struct udevice *bus, uint mode)
  352. +{
  353. + struct sifive_spi *spi = dev_get_priv(bus);
  354. + u32 cr;
  355. +
  356. + /* Switch clock mode bits */
  357. + cr = sifive_spi_read(spi, SIFIVE_SPI_REG_SCKMODE) &
  358. + ~SIFIVE_SPI_SCKMODE_MODE_MASK;
  359. + if (mode & SPI_CPHA)
  360. + cr |= SIFIVE_SPI_SCKMODE_PHA;
  361. + if (mode & SPI_CPOL)
  362. + cr |= SIFIVE_SPI_SCKMODE_POL;
  363. +
  364. + sifive_spi_write(spi, SIFIVE_SPI_REG_SCKMODE, cr);
  365. +
  366. + return 0;
  367. +}
  368. +
  369. +static int sifive_cs_info(struct udevice *bus, uint cs,
  370. + struct spi_cs_info *info)
  371. +{
  372. + return 0;
  373. +}
  374. +
  375. +static void sifive_spi_init_hw(struct sifive_spi *spi)
  376. +{
  377. + u32 cs_bits;
  378. +
  379. + /* probe the number of CS lines */
  380. + spi->cs_inactive = sifive_spi_read(spi, SIFIVE_SPI_REG_CSDEF);
  381. + sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, 0xffffffffU);
  382. + cs_bits = sifive_spi_read(spi, SIFIVE_SPI_REG_CSDEF);
  383. + sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, spi->cs_inactive);
  384. + if (!cs_bits) {
  385. + printf("Could not auto probe CS lines\n");
  386. + return;
  387. + }
  388. +
  389. + spi->num_cs = ilog2(cs_bits) + 1;
  390. + if (spi->num_cs > SIFIVE_SPI_MAX_CS) {
  391. + printf("Invalid number of spi slaves\n");
  392. + return;
  393. + }
  394. +
  395. + /* Watermark interrupts are disabled by default */
  396. + sifive_spi_write(spi, SIFIVE_SPI_REG_IE, 0);
  397. +
  398. + /* Set CS/SCK Delays and Inactive Time to defaults */
  399. + sifive_spi_write(spi, SIFIVE_SPI_REG_DELAY0,
  400. + SIFIVE_SPI_DELAY0_CSSCK(1) |
  401. + SIFIVE_SPI_DELAY0_SCKCS(1));
  402. + sifive_spi_write(spi, SIFIVE_SPI_REG_DELAY1,
  403. + SIFIVE_SPI_DELAY1_INTERCS(1) |
  404. + SIFIVE_SPI_DELAY1_INTERXFR(0));
  405. +
  406. + /* Exit specialized memory-mapped SPI flash mode */
  407. + sifive_spi_write(spi, SIFIVE_SPI_REG_FCTRL, 0);
  408. +}
  409. +
  410. +static int sifive_spi_probe(struct udevice *bus)
  411. +{
  412. + struct sifive_spi *spi = dev_get_priv(bus);
  413. + struct clk clkdev;
  414. + int ret;
  415. +
  416. + spi->regs = (void *)(ulong)dev_remap_addr(bus);
  417. + if (!spi->regs)
  418. + return -ENODEV;
  419. +
  420. + spi->fifo_depth = dev_read_u32_default(bus,
  421. + "sifive,fifo-depth",
  422. + SIFIVE_SPI_DEFAULT_DEPTH);
  423. +
  424. + spi->bits_per_word = dev_read_u32_default(bus,
  425. + "sifive,max-bits-per-word",
  426. + SIFIVE_SPI_DEFAULT_BITS);
  427. +
  428. + ret = clk_get_by_index(bus, 0, &clkdev);
  429. + if (ret)
  430. + return ret;
  431. + spi->freq = clk_get_rate(&clkdev);
  432. +
  433. + /* init the sifive spi hw */
  434. + sifive_spi_init_hw(spi);
  435. +
  436. + return 0;
  437. +}
  438. +
  439. +static const struct dm_spi_ops sifive_spi_ops = {
  440. + .claim_bus = sifive_spi_claim_bus,
  441. + .release_bus = sifive_spi_release_bus,
  442. + .xfer = sifive_spi_xfer,
  443. + .set_speed = sifive_spi_set_speed,
  444. + .set_mode = sifive_spi_set_mode,
  445. + .cs_info = sifive_cs_info,
  446. +};
  447. +
  448. +static const struct udevice_id sifive_spi_ids[] = {
  449. + { .compatible = "sifive,spi0" },
  450. + { }
  451. +};
  452. +
  453. +U_BOOT_DRIVER(sifive_spi) = {
  454. + .name = "sifive_spi",
  455. + .id = UCLASS_SPI,
  456. + .of_match = sifive_spi_ids,
  457. + .ops = &sifive_spi_ops,
  458. + .priv_auto_alloc_size = sizeof(struct sifive_spi),
  459. + .probe = sifive_spi_probe,
  460. +};
  461. --
  462. 2.22.0