spi-armada-3700.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Marvell Armada-3700 SPI controller driver
  3. *
  4. * Copyright (C) 2016 Marvell Ltd.
  5. *
  6. * Author: Wilson Ding <dingwei@marvell.com>
  7. * Author: Romain Perier <romain.perier@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_device.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/spi/spi.h>
  26. #define DRIVER_NAME "armada_3700_spi"
  27. #define A3700_SPI_MAX_SPEED_HZ 100000000
  28. #define A3700_SPI_MAX_PRESCALE 30
  29. #define A3700_SPI_TIMEOUT 10
  30. /* SPI Register Offest */
  31. #define A3700_SPI_IF_CTRL_REG 0x00
  32. #define A3700_SPI_IF_CFG_REG 0x04
  33. #define A3700_SPI_DATA_OUT_REG 0x08
  34. #define A3700_SPI_DATA_IN_REG 0x0C
  35. #define A3700_SPI_IF_INST_REG 0x10
  36. #define A3700_SPI_IF_ADDR_REG 0x14
  37. #define A3700_SPI_IF_RMODE_REG 0x18
  38. #define A3700_SPI_IF_HDR_CNT_REG 0x1C
  39. #define A3700_SPI_IF_DIN_CNT_REG 0x20
  40. #define A3700_SPI_IF_TIME_REG 0x24
  41. #define A3700_SPI_INT_STAT_REG 0x28
  42. #define A3700_SPI_INT_MASK_REG 0x2C
  43. /* A3700_SPI_IF_CTRL_REG */
  44. #define A3700_SPI_EN BIT(16)
  45. #define A3700_SPI_ADDR_NOT_CONFIG BIT(12)
  46. #define A3700_SPI_WFIFO_OVERFLOW BIT(11)
  47. #define A3700_SPI_WFIFO_UNDERFLOW BIT(10)
  48. #define A3700_SPI_RFIFO_OVERFLOW BIT(9)
  49. #define A3700_SPI_RFIFO_UNDERFLOW BIT(8)
  50. #define A3700_SPI_WFIFO_FULL BIT(7)
  51. #define A3700_SPI_WFIFO_EMPTY BIT(6)
  52. #define A3700_SPI_RFIFO_FULL BIT(5)
  53. #define A3700_SPI_RFIFO_EMPTY BIT(4)
  54. #define A3700_SPI_WFIFO_RDY BIT(3)
  55. #define A3700_SPI_RFIFO_RDY BIT(2)
  56. #define A3700_SPI_XFER_RDY BIT(1)
  57. #define A3700_SPI_XFER_DONE BIT(0)
  58. /* A3700_SPI_IF_CFG_REG */
  59. #define A3700_SPI_WFIFO_THRS BIT(28)
  60. #define A3700_SPI_RFIFO_THRS BIT(24)
  61. #define A3700_SPI_AUTO_CS BIT(20)
  62. #define A3700_SPI_DMA_RD_EN BIT(18)
  63. #define A3700_SPI_FIFO_MODE BIT(17)
  64. #define A3700_SPI_SRST BIT(16)
  65. #define A3700_SPI_XFER_START BIT(15)
  66. #define A3700_SPI_XFER_STOP BIT(14)
  67. #define A3700_SPI_INST_PIN BIT(13)
  68. #define A3700_SPI_ADDR_PIN BIT(12)
  69. #define A3700_SPI_DATA_PIN1 BIT(11)
  70. #define A3700_SPI_DATA_PIN0 BIT(10)
  71. #define A3700_SPI_FIFO_FLUSH BIT(9)
  72. #define A3700_SPI_RW_EN BIT(8)
  73. #define A3700_SPI_CLK_POL BIT(7)
  74. #define A3700_SPI_CLK_PHA BIT(6)
  75. #define A3700_SPI_BYTE_LEN BIT(5)
  76. #define A3700_SPI_CLK_PRESCALE BIT(0)
  77. #define A3700_SPI_CLK_PRESCALE_MASK (0x1f)
  78. #define A3700_SPI_CLK_EVEN_OFFS (0x10)
  79. #define A3700_SPI_WFIFO_THRS_BIT 28
  80. #define A3700_SPI_RFIFO_THRS_BIT 24
  81. #define A3700_SPI_FIFO_THRS_MASK 0x7
  82. #define A3700_SPI_DATA_PIN_MASK 0x3
  83. /* A3700_SPI_IF_HDR_CNT_REG */
  84. #define A3700_SPI_DUMMY_CNT_BIT 12
  85. #define A3700_SPI_DUMMY_CNT_MASK 0x7
  86. #define A3700_SPI_RMODE_CNT_BIT 8
  87. #define A3700_SPI_RMODE_CNT_MASK 0x3
  88. #define A3700_SPI_ADDR_CNT_BIT 4
  89. #define A3700_SPI_ADDR_CNT_MASK 0x7
  90. #define A3700_SPI_INSTR_CNT_BIT 0
  91. #define A3700_SPI_INSTR_CNT_MASK 0x3
  92. /* A3700_SPI_IF_TIME_REG */
  93. #define A3700_SPI_CLK_CAPT_EDGE BIT(7)
  94. struct a3700_spi {
  95. struct spi_master *master;
  96. void __iomem *base;
  97. struct clk *clk;
  98. unsigned int irq;
  99. unsigned int flags;
  100. bool xmit_data;
  101. const u8 *tx_buf;
  102. u8 *rx_buf;
  103. size_t buf_len;
  104. u8 byte_len;
  105. u32 wait_mask;
  106. struct completion done;
  107. };
  108. static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
  109. {
  110. return readl(a3700_spi->base + offset);
  111. }
  112. static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
  113. {
  114. writel(data, a3700_spi->base + offset);
  115. }
  116. static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
  117. {
  118. u32 val;
  119. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  120. val &= ~A3700_SPI_AUTO_CS;
  121. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  122. }
  123. static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
  124. {
  125. u32 val;
  126. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  127. val |= (A3700_SPI_EN << cs);
  128. spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
  129. }
  130. static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
  131. unsigned int cs)
  132. {
  133. u32 val;
  134. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  135. val &= ~(A3700_SPI_EN << cs);
  136. spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
  137. }
  138. static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
  139. unsigned int pin_mode, bool receiving)
  140. {
  141. u32 val;
  142. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  143. val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
  144. val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
  145. switch (pin_mode) {
  146. case SPI_NBITS_SINGLE:
  147. break;
  148. case SPI_NBITS_DUAL:
  149. val |= A3700_SPI_DATA_PIN0;
  150. break;
  151. case SPI_NBITS_QUAD:
  152. val |= A3700_SPI_DATA_PIN1;
  153. /* RX during address reception uses 4-pin */
  154. if (receiving)
  155. val |= A3700_SPI_ADDR_PIN;
  156. break;
  157. default:
  158. dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
  159. return -EINVAL;
  160. }
  161. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  162. return 0;
  163. }
  164. static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi, bool enable)
  165. {
  166. u32 val;
  167. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  168. if (enable)
  169. val |= A3700_SPI_FIFO_MODE;
  170. else
  171. val &= ~A3700_SPI_FIFO_MODE;
  172. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  173. }
  174. static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
  175. unsigned int mode_bits)
  176. {
  177. u32 val;
  178. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  179. if (mode_bits & SPI_CPOL)
  180. val |= A3700_SPI_CLK_POL;
  181. else
  182. val &= ~A3700_SPI_CLK_POL;
  183. if (mode_bits & SPI_CPHA)
  184. val |= A3700_SPI_CLK_PHA;
  185. else
  186. val &= ~A3700_SPI_CLK_PHA;
  187. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  188. }
  189. static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
  190. unsigned int speed_hz)
  191. {
  192. u32 val;
  193. u32 prescale;
  194. prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
  195. /* For prescaler values over 15, we can only set it by steps of 2.
  196. * Starting from A3700_SPI_CLK_EVEN_OFFS, we set values from 0 up to
  197. * 30. We only use this range from 16 to 30.
  198. */
  199. if (prescale > 15)
  200. prescale = A3700_SPI_CLK_EVEN_OFFS + DIV_ROUND_UP(prescale, 2);
  201. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  202. val = val & ~A3700_SPI_CLK_PRESCALE_MASK;
  203. val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK);
  204. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  205. if (prescale <= 2) {
  206. val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG);
  207. val |= A3700_SPI_CLK_CAPT_EDGE;
  208. spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val);
  209. }
  210. }
  211. static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len)
  212. {
  213. u32 val;
  214. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  215. if (len == 4)
  216. val |= A3700_SPI_BYTE_LEN;
  217. else
  218. val &= ~A3700_SPI_BYTE_LEN;
  219. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  220. a3700_spi->byte_len = len;
  221. }
  222. static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi)
  223. {
  224. int timeout = A3700_SPI_TIMEOUT;
  225. u32 val;
  226. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  227. val |= A3700_SPI_FIFO_FLUSH;
  228. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  229. while (--timeout) {
  230. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  231. if (!(val & A3700_SPI_FIFO_FLUSH))
  232. return 0;
  233. udelay(1);
  234. }
  235. return -ETIMEDOUT;
  236. }
  237. static int a3700_spi_init(struct a3700_spi *a3700_spi)
  238. {
  239. struct spi_master *master = a3700_spi->master;
  240. u32 val;
  241. int i, ret = 0;
  242. /* Reset SPI unit */
  243. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  244. val |= A3700_SPI_SRST;
  245. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  246. udelay(A3700_SPI_TIMEOUT);
  247. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  248. val &= ~A3700_SPI_SRST;
  249. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  250. /* Disable AUTO_CS and deactivate all chip-selects */
  251. a3700_spi_auto_cs_unset(a3700_spi);
  252. for (i = 0; i < master->num_chipselect; i++)
  253. a3700_spi_deactivate_cs(a3700_spi, i);
  254. /* Enable FIFO mode */
  255. a3700_spi_fifo_mode_set(a3700_spi, true);
  256. /* Set SPI mode */
  257. a3700_spi_mode_set(a3700_spi, master->mode_bits);
  258. /* Reset counters */
  259. spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
  260. spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0);
  261. /* Mask the interrupts and clear cause bits */
  262. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
  263. spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U);
  264. return ret;
  265. }
  266. static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id)
  267. {
  268. struct spi_master *master = dev_id;
  269. struct a3700_spi *a3700_spi;
  270. u32 cause;
  271. a3700_spi = spi_master_get_devdata(master);
  272. /* Get interrupt causes */
  273. cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG);
  274. if (!cause || !(a3700_spi->wait_mask & cause))
  275. return IRQ_NONE;
  276. /* mask and acknowledge the SPI interrupts */
  277. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
  278. spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause);
  279. /* Wake up the transfer */
  280. complete(&a3700_spi->done);
  281. return IRQ_HANDLED;
  282. }
  283. static bool a3700_spi_wait_completion(struct spi_device *spi)
  284. {
  285. struct a3700_spi *a3700_spi;
  286. unsigned int timeout;
  287. unsigned int ctrl_reg;
  288. unsigned long timeout_jiffies;
  289. a3700_spi = spi_master_get_devdata(spi->master);
  290. /* SPI interrupt is edge-triggered, which means an interrupt will
  291. * be generated only when detecting a specific status bit changed
  292. * from '0' to '1'. So when we start waiting for a interrupt, we
  293. * need to check status bit in control reg first, if it is already 1,
  294. * then we do not need to wait for interrupt
  295. */
  296. ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  297. if (a3700_spi->wait_mask & ctrl_reg)
  298. return true;
  299. reinit_completion(&a3700_spi->done);
  300. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG,
  301. a3700_spi->wait_mask);
  302. timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT);
  303. timeout = wait_for_completion_timeout(&a3700_spi->done,
  304. timeout_jiffies);
  305. a3700_spi->wait_mask = 0;
  306. if (timeout)
  307. return true;
  308. /* there might be the case that right after we checked the
  309. * status bits in this routine and before start to wait for
  310. * interrupt by wait_for_completion_timeout, the interrupt
  311. * happens, to avoid missing it we need to double check
  312. * status bits in control reg, if it is already 1, then
  313. * consider that we have the interrupt successfully and
  314. * return true.
  315. */
  316. ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  317. if (a3700_spi->wait_mask & ctrl_reg)
  318. return true;
  319. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
  320. /* Timeout was reached */
  321. return false;
  322. }
  323. static bool a3700_spi_transfer_wait(struct spi_device *spi,
  324. unsigned int bit_mask)
  325. {
  326. struct a3700_spi *a3700_spi;
  327. a3700_spi = spi_master_get_devdata(spi->master);
  328. a3700_spi->wait_mask = bit_mask;
  329. return a3700_spi_wait_completion(spi);
  330. }
  331. static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
  332. unsigned int bytes)
  333. {
  334. u32 val;
  335. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  336. val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
  337. val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
  338. val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
  339. val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
  340. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  341. }
  342. static void a3700_spi_transfer_setup(struct spi_device *spi,
  343. struct spi_transfer *xfer)
  344. {
  345. struct a3700_spi *a3700_spi;
  346. a3700_spi = spi_master_get_devdata(spi->master);
  347. a3700_spi_clock_set(a3700_spi, xfer->speed_hz);
  348. /* Use 4 bytes long transfers. Each transfer method has its way to deal
  349. * with the remaining bytes for non 4-bytes aligned transfers.
  350. */
  351. a3700_spi_bytelen_set(a3700_spi, 4);
  352. /* Initialize the working buffers */
  353. a3700_spi->tx_buf = xfer->tx_buf;
  354. a3700_spi->rx_buf = xfer->rx_buf;
  355. a3700_spi->buf_len = xfer->len;
  356. }
  357. static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
  358. {
  359. struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
  360. if (!enable)
  361. a3700_spi_activate_cs(a3700_spi, spi->chip_select);
  362. else
  363. a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
  364. }
  365. static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
  366. {
  367. unsigned int addr_cnt;
  368. u32 val = 0;
  369. /* Clear the header registers */
  370. spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
  371. spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
  372. spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
  373. spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
  374. /* Set header counters */
  375. if (a3700_spi->tx_buf) {
  376. /*
  377. * when tx data is not 4 bytes aligned, there will be unexpected
  378. * bytes out of SPI output register, since it always shifts out
  379. * as whole 4 bytes. This might cause incorrect transaction with
  380. * some devices. To avoid that, use SPI header count feature to
  381. * transfer up to 3 bytes of data first, and then make the rest
  382. * of data 4-byte aligned.
  383. */
  384. addr_cnt = a3700_spi->buf_len % 4;
  385. if (addr_cnt) {
  386. val = (addr_cnt & A3700_SPI_ADDR_CNT_MASK)
  387. << A3700_SPI_ADDR_CNT_BIT;
  388. spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
  389. /* Update the buffer length to be transferred */
  390. a3700_spi->buf_len -= addr_cnt;
  391. /* transfer 1~3 bytes through address count */
  392. val = 0;
  393. while (addr_cnt--) {
  394. val = (val << 8) | a3700_spi->tx_buf[0];
  395. a3700_spi->tx_buf++;
  396. }
  397. spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
  398. }
  399. }
  400. }
  401. static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
  402. {
  403. u32 val;
  404. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  405. return (val & A3700_SPI_WFIFO_FULL);
  406. }
  407. static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
  408. {
  409. u32 val;
  410. while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
  411. val = *(u32 *)a3700_spi->tx_buf;
  412. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
  413. a3700_spi->buf_len -= 4;
  414. a3700_spi->tx_buf += 4;
  415. }
  416. return 0;
  417. }
  418. static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
  419. {
  420. u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  421. return (val & A3700_SPI_RFIFO_EMPTY);
  422. }
  423. static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
  424. {
  425. u32 val;
  426. while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
  427. val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
  428. if (a3700_spi->buf_len >= 4) {
  429. memcpy(a3700_spi->rx_buf, &val, 4);
  430. a3700_spi->buf_len -= 4;
  431. a3700_spi->rx_buf += 4;
  432. } else {
  433. /*
  434. * When remain bytes is not larger than 4, we should
  435. * avoid memory overwriting and just write the left rx
  436. * buffer bytes.
  437. */
  438. while (a3700_spi->buf_len) {
  439. *a3700_spi->rx_buf = val & 0xff;
  440. val >>= 8;
  441. a3700_spi->buf_len--;
  442. a3700_spi->rx_buf++;
  443. }
  444. }
  445. }
  446. return 0;
  447. }
  448. static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
  449. {
  450. int timeout = A3700_SPI_TIMEOUT;
  451. u32 val;
  452. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  453. val |= A3700_SPI_XFER_STOP;
  454. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  455. while (--timeout) {
  456. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  457. if (!(val & A3700_SPI_XFER_START))
  458. break;
  459. udelay(1);
  460. }
  461. a3700_spi_fifo_flush(a3700_spi);
  462. val &= ~A3700_SPI_XFER_STOP;
  463. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  464. }
  465. static int a3700_spi_prepare_message(struct spi_master *master,
  466. struct spi_message *message)
  467. {
  468. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  469. struct spi_device *spi = message->spi;
  470. int ret;
  471. ret = clk_enable(a3700_spi->clk);
  472. if (ret) {
  473. dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
  474. return ret;
  475. }
  476. /* Flush the FIFOs */
  477. ret = a3700_spi_fifo_flush(a3700_spi);
  478. if (ret)
  479. return ret;
  480. a3700_spi_mode_set(a3700_spi, spi->mode);
  481. return 0;
  482. }
  483. static int a3700_spi_transfer_one_fifo(struct spi_master *master,
  484. struct spi_device *spi,
  485. struct spi_transfer *xfer)
  486. {
  487. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  488. int ret = 0, timeout = A3700_SPI_TIMEOUT;
  489. unsigned int nbits = 0, byte_len;
  490. u32 val;
  491. /* Make sure we use FIFO mode */
  492. a3700_spi_fifo_mode_set(a3700_spi, true);
  493. /* Configure FIFO thresholds */
  494. byte_len = xfer->bits_per_word >> 3;
  495. a3700_spi_fifo_thres_set(a3700_spi, byte_len);
  496. if (xfer->tx_buf)
  497. nbits = xfer->tx_nbits;
  498. else if (xfer->rx_buf)
  499. nbits = xfer->rx_nbits;
  500. a3700_spi_pin_mode_set(a3700_spi, nbits, xfer->rx_buf ? true : false);
  501. /* Flush the FIFOs */
  502. a3700_spi_fifo_flush(a3700_spi);
  503. /* Transfer first bytes of data when buffer is not 4-byte aligned */
  504. a3700_spi_header_set(a3700_spi);
  505. if (xfer->rx_buf) {
  506. /* Clear WFIFO, since it's last 2 bytes are shifted out during
  507. * a read operation
  508. */
  509. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, 0);
  510. /* Set read data length */
  511. spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
  512. a3700_spi->buf_len);
  513. /* Start READ transfer */
  514. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  515. val &= ~A3700_SPI_RW_EN;
  516. val |= A3700_SPI_XFER_START;
  517. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  518. } else if (xfer->tx_buf) {
  519. /* Start Write transfer */
  520. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  521. val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
  522. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  523. /*
  524. * If there are data to be written to the SPI device, xmit_data
  525. * flag is set true; otherwise the instruction in SPI_INSTR does
  526. * not require data to be written to the SPI device, then
  527. * xmit_data flag is set false.
  528. */
  529. a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
  530. }
  531. while (a3700_spi->buf_len) {
  532. if (a3700_spi->tx_buf) {
  533. /* Wait wfifo ready */
  534. if (!a3700_spi_transfer_wait(spi,
  535. A3700_SPI_WFIFO_RDY)) {
  536. dev_err(&spi->dev,
  537. "wait wfifo ready timed out\n");
  538. ret = -ETIMEDOUT;
  539. goto error;
  540. }
  541. /* Fill up the wfifo */
  542. ret = a3700_spi_fifo_write(a3700_spi);
  543. if (ret)
  544. goto error;
  545. } else if (a3700_spi->rx_buf) {
  546. /* Wait rfifo ready */
  547. if (!a3700_spi_transfer_wait(spi,
  548. A3700_SPI_RFIFO_RDY)) {
  549. dev_err(&spi->dev,
  550. "wait rfifo ready timed out\n");
  551. ret = -ETIMEDOUT;
  552. goto error;
  553. }
  554. /* Drain out the rfifo */
  555. ret = a3700_spi_fifo_read(a3700_spi);
  556. if (ret)
  557. goto error;
  558. }
  559. }
  560. /*
  561. * Stop a write transfer in fifo mode:
  562. * - wait all the bytes in wfifo to be shifted out
  563. * - set XFER_STOP bit
  564. * - wait XFER_START bit clear
  565. * - clear XFER_STOP bit
  566. * Stop a read transfer in fifo mode:
  567. * - the hardware is to reset the XFER_START bit
  568. * after the number of bytes indicated in DIN_CNT
  569. * register
  570. * - just wait XFER_START bit clear
  571. */
  572. if (a3700_spi->tx_buf) {
  573. if (a3700_spi->xmit_data) {
  574. /*
  575. * If there are data written to the SPI device, wait
  576. * until SPI_WFIFO_EMPTY is 1 to wait for all data to
  577. * transfer out of write FIFO.
  578. */
  579. if (!a3700_spi_transfer_wait(spi,
  580. A3700_SPI_WFIFO_EMPTY)) {
  581. dev_err(&spi->dev, "wait wfifo empty timed out\n");
  582. return -ETIMEDOUT;
  583. }
  584. }
  585. if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
  586. dev_err(&spi->dev, "wait xfer ready timed out\n");
  587. return -ETIMEDOUT;
  588. }
  589. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  590. val |= A3700_SPI_XFER_STOP;
  591. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  592. }
  593. while (--timeout) {
  594. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  595. if (!(val & A3700_SPI_XFER_START))
  596. break;
  597. udelay(1);
  598. }
  599. if (timeout == 0) {
  600. dev_err(&spi->dev, "wait transfer start clear timed out\n");
  601. ret = -ETIMEDOUT;
  602. goto error;
  603. }
  604. val &= ~A3700_SPI_XFER_STOP;
  605. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  606. goto out;
  607. error:
  608. a3700_spi_transfer_abort_fifo(a3700_spi);
  609. out:
  610. spi_finalize_current_transfer(master);
  611. return ret;
  612. }
  613. static int a3700_spi_transfer_one_full_duplex(struct spi_master *master,
  614. struct spi_device *spi,
  615. struct spi_transfer *xfer)
  616. {
  617. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  618. u32 val;
  619. /* Disable FIFO mode */
  620. a3700_spi_fifo_mode_set(a3700_spi, false);
  621. while (a3700_spi->buf_len) {
  622. /* When we have less than 4 bytes to transfer, switch to 1 byte
  623. * mode. This is reset after each transfer
  624. */
  625. if (a3700_spi->buf_len < 4)
  626. a3700_spi_bytelen_set(a3700_spi, 1);
  627. if (a3700_spi->byte_len == 1)
  628. val = *a3700_spi->tx_buf;
  629. else
  630. val = *(u32 *)a3700_spi->tx_buf;
  631. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
  632. /* Wait for all the data to be shifted in / out */
  633. while (!(spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG) &
  634. A3700_SPI_XFER_DONE))
  635. cpu_relax();
  636. val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
  637. memcpy(a3700_spi->rx_buf, &val, a3700_spi->byte_len);
  638. a3700_spi->buf_len -= a3700_spi->byte_len;
  639. a3700_spi->tx_buf += a3700_spi->byte_len;
  640. a3700_spi->rx_buf += a3700_spi->byte_len;
  641. }
  642. spi_finalize_current_transfer(master);
  643. return 0;
  644. }
  645. static int a3700_spi_transfer_one(struct spi_master *master,
  646. struct spi_device *spi,
  647. struct spi_transfer *xfer)
  648. {
  649. a3700_spi_transfer_setup(spi, xfer);
  650. if (xfer->tx_buf && xfer->rx_buf)
  651. return a3700_spi_transfer_one_full_duplex(master, spi, xfer);
  652. return a3700_spi_transfer_one_fifo(master, spi, xfer);
  653. }
  654. static int a3700_spi_unprepare_message(struct spi_master *master,
  655. struct spi_message *message)
  656. {
  657. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  658. clk_disable(a3700_spi->clk);
  659. return 0;
  660. }
  661. static const struct of_device_id a3700_spi_dt_ids[] = {
  662. { .compatible = "marvell,armada-3700-spi", .data = NULL },
  663. {},
  664. };
  665. MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
  666. static int a3700_spi_probe(struct platform_device *pdev)
  667. {
  668. struct device *dev = &pdev->dev;
  669. struct device_node *of_node = dev->of_node;
  670. struct resource *res;
  671. struct spi_master *master;
  672. struct a3700_spi *spi;
  673. u32 num_cs = 0;
  674. int irq, ret = 0;
  675. master = spi_alloc_master(dev, sizeof(*spi));
  676. if (!master) {
  677. dev_err(dev, "master allocation failed\n");
  678. ret = -ENOMEM;
  679. goto out;
  680. }
  681. if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
  682. dev_err(dev, "could not find num-cs\n");
  683. ret = -ENXIO;
  684. goto error;
  685. }
  686. master->bus_num = pdev->id;
  687. master->dev.of_node = of_node;
  688. master->mode_bits = SPI_MODE_3;
  689. master->num_chipselect = num_cs;
  690. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
  691. master->prepare_message = a3700_spi_prepare_message;
  692. master->transfer_one = a3700_spi_transfer_one;
  693. master->unprepare_message = a3700_spi_unprepare_message;
  694. master->set_cs = a3700_spi_set_cs;
  695. master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
  696. SPI_RX_QUAD | SPI_TX_QUAD);
  697. platform_set_drvdata(pdev, master);
  698. spi = spi_master_get_devdata(master);
  699. memset(spi, 0, sizeof(struct a3700_spi));
  700. spi->master = master;
  701. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  702. spi->base = devm_ioremap_resource(dev, res);
  703. if (IS_ERR(spi->base)) {
  704. ret = PTR_ERR(spi->base);
  705. goto error;
  706. }
  707. irq = platform_get_irq(pdev, 0);
  708. if (irq < 0) {
  709. dev_err(dev, "could not get irq: %d\n", irq);
  710. ret = -ENXIO;
  711. goto error;
  712. }
  713. spi->irq = irq;
  714. init_completion(&spi->done);
  715. spi->clk = devm_clk_get(dev, NULL);
  716. if (IS_ERR(spi->clk)) {
  717. dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
  718. goto error;
  719. }
  720. ret = clk_prepare(spi->clk);
  721. if (ret) {
  722. dev_err(dev, "could not prepare clk: %d\n", ret);
  723. goto error;
  724. }
  725. master->max_speed_hz = min_t(unsigned long, A3700_SPI_MAX_SPEED_HZ,
  726. clk_get_rate(spi->clk));
  727. master->min_speed_hz = DIV_ROUND_UP(clk_get_rate(spi->clk),
  728. A3700_SPI_MAX_PRESCALE);
  729. ret = a3700_spi_init(spi);
  730. if (ret)
  731. goto error_clk;
  732. ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
  733. dev_name(dev), master);
  734. if (ret) {
  735. dev_err(dev, "could not request IRQ: %d\n", ret);
  736. goto error_clk;
  737. }
  738. ret = devm_spi_register_master(dev, master);
  739. if (ret) {
  740. dev_err(dev, "Failed to register master\n");
  741. goto error_clk;
  742. }
  743. return 0;
  744. error_clk:
  745. clk_disable_unprepare(spi->clk);
  746. error:
  747. spi_master_put(master);
  748. out:
  749. return ret;
  750. }
  751. static int a3700_spi_remove(struct platform_device *pdev)
  752. {
  753. struct spi_master *master = platform_get_drvdata(pdev);
  754. struct a3700_spi *spi = spi_master_get_devdata(master);
  755. clk_unprepare(spi->clk);
  756. return 0;
  757. }
  758. static struct platform_driver a3700_spi_driver = {
  759. .driver = {
  760. .name = DRIVER_NAME,
  761. .of_match_table = of_match_ptr(a3700_spi_dt_ids),
  762. },
  763. .probe = a3700_spi_probe,
  764. .remove = a3700_spi_remove,
  765. };
  766. module_platform_driver(a3700_spi_driver);
  767. MODULE_DESCRIPTION("Armada-3700 SPI driver");
  768. MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
  769. MODULE_LICENSE("GPL");
  770. MODULE_ALIAS("platform:" DRIVER_NAME);