spi-armada-3700.c 22 KB

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