spi-armada-3700.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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_WFIFO_THRS_BIT 28
  77. #define A3700_SPI_RFIFO_THRS_BIT 24
  78. #define A3700_SPI_FIFO_THRS_MASK 0x7
  79. #define A3700_SPI_DATA_PIN_MASK 0x3
  80. /* A3700_SPI_IF_HDR_CNT_REG */
  81. #define A3700_SPI_DUMMY_CNT_BIT 12
  82. #define A3700_SPI_DUMMY_CNT_MASK 0x7
  83. #define A3700_SPI_RMODE_CNT_BIT 8
  84. #define A3700_SPI_RMODE_CNT_MASK 0x3
  85. #define A3700_SPI_ADDR_CNT_BIT 4
  86. #define A3700_SPI_ADDR_CNT_MASK 0x7
  87. #define A3700_SPI_INSTR_CNT_BIT 0
  88. #define A3700_SPI_INSTR_CNT_MASK 0x3
  89. /* A3700_SPI_IF_TIME_REG */
  90. #define A3700_SPI_CLK_CAPT_EDGE BIT(7)
  91. /* Flags and macros for struct a3700_spi */
  92. #define A3700_INSTR_CNT 1
  93. #define A3700_ADDR_CNT 3
  94. #define A3700_DUMMY_CNT 1
  95. struct a3700_spi {
  96. struct spi_master *master;
  97. void __iomem *base;
  98. struct clk *clk;
  99. unsigned int irq;
  100. unsigned int flags;
  101. bool xmit_data;
  102. const u8 *tx_buf;
  103. u8 *rx_buf;
  104. size_t buf_len;
  105. u8 byte_len;
  106. u32 wait_mask;
  107. struct completion done;
  108. u32 addr_cnt;
  109. u32 instr_cnt;
  110. size_t hdr_cnt;
  111. };
  112. static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
  113. {
  114. return readl(a3700_spi->base + offset);
  115. }
  116. static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
  117. {
  118. writel(data, a3700_spi->base + offset);
  119. }
  120. static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
  121. {
  122. u32 val;
  123. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  124. val &= ~A3700_SPI_AUTO_CS;
  125. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  126. }
  127. static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
  128. {
  129. u32 val;
  130. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  131. val |= (A3700_SPI_EN << cs);
  132. spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
  133. }
  134. static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
  135. unsigned int cs)
  136. {
  137. u32 val;
  138. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  139. val &= ~(A3700_SPI_EN << cs);
  140. spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
  141. }
  142. static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
  143. unsigned int pin_mode)
  144. {
  145. u32 val;
  146. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  147. val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
  148. val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
  149. switch (pin_mode) {
  150. case 1:
  151. break;
  152. case 2:
  153. val |= A3700_SPI_DATA_PIN0;
  154. break;
  155. case 4:
  156. val |= A3700_SPI_DATA_PIN1;
  157. break;
  158. default:
  159. dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
  160. return -EINVAL;
  161. }
  162. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  163. return 0;
  164. }
  165. static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi)
  166. {
  167. u32 val;
  168. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  169. val |= A3700_SPI_FIFO_MODE;
  170. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  171. }
  172. static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
  173. unsigned int mode_bits)
  174. {
  175. u32 val;
  176. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  177. if (mode_bits & SPI_CPOL)
  178. val |= A3700_SPI_CLK_POL;
  179. else
  180. val &= ~A3700_SPI_CLK_POL;
  181. if (mode_bits & SPI_CPHA)
  182. val |= A3700_SPI_CLK_PHA;
  183. else
  184. val &= ~A3700_SPI_CLK_PHA;
  185. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  186. }
  187. static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
  188. unsigned int speed_hz, u16 mode)
  189. {
  190. u32 val;
  191. u32 prescale;
  192. prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
  193. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  194. val = val & ~A3700_SPI_CLK_PRESCALE_MASK;
  195. val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK);
  196. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  197. if (prescale <= 2) {
  198. val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG);
  199. val |= A3700_SPI_CLK_CAPT_EDGE;
  200. spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val);
  201. }
  202. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  203. val &= ~(A3700_SPI_CLK_POL | A3700_SPI_CLK_PHA);
  204. if (mode & SPI_CPOL)
  205. val |= A3700_SPI_CLK_POL;
  206. if (mode & SPI_CPHA)
  207. val |= A3700_SPI_CLK_PHA;
  208. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  209. }
  210. static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len)
  211. {
  212. u32 val;
  213. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  214. if (len == 4)
  215. val |= A3700_SPI_BYTE_LEN;
  216. else
  217. val &= ~A3700_SPI_BYTE_LEN;
  218. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  219. a3700_spi->byte_len = len;
  220. }
  221. static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi)
  222. {
  223. int timeout = A3700_SPI_TIMEOUT;
  224. u32 val;
  225. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  226. val |= A3700_SPI_FIFO_FLUSH;
  227. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  228. while (--timeout) {
  229. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  230. if (!(val & A3700_SPI_FIFO_FLUSH))
  231. return 0;
  232. udelay(1);
  233. }
  234. return -ETIMEDOUT;
  235. }
  236. static int a3700_spi_init(struct a3700_spi *a3700_spi)
  237. {
  238. struct spi_master *master = a3700_spi->master;
  239. u32 val;
  240. int i, ret = 0;
  241. /* Reset SPI unit */
  242. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  243. val |= A3700_SPI_SRST;
  244. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  245. udelay(A3700_SPI_TIMEOUT);
  246. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  247. val &= ~A3700_SPI_SRST;
  248. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  249. /* Disable AUTO_CS and deactivate all chip-selects */
  250. a3700_spi_auto_cs_unset(a3700_spi);
  251. for (i = 0; i < master->num_chipselect; i++)
  252. a3700_spi_deactivate_cs(a3700_spi, i);
  253. /* Enable FIFO mode */
  254. a3700_spi_fifo_mode_set(a3700_spi);
  255. /* Set SPI mode */
  256. a3700_spi_mode_set(a3700_spi, master->mode_bits);
  257. /* Reset counters */
  258. spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
  259. spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0);
  260. /* Mask the interrupts and clear cause bits */
  261. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
  262. spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U);
  263. return ret;
  264. }
  265. static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id)
  266. {
  267. struct spi_master *master = dev_id;
  268. struct a3700_spi *a3700_spi;
  269. u32 cause;
  270. a3700_spi = spi_master_get_devdata(master);
  271. /* Get interrupt causes */
  272. cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG);
  273. if (!cause || !(a3700_spi->wait_mask & cause))
  274. return IRQ_NONE;
  275. /* mask and acknowledge the SPI interrupts */
  276. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
  277. spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause);
  278. /* Wake up the transfer */
  279. if (a3700_spi->wait_mask & cause)
  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. return true;
  321. }
  322. static bool a3700_spi_transfer_wait(struct spi_device *spi,
  323. unsigned int bit_mask)
  324. {
  325. struct a3700_spi *a3700_spi;
  326. a3700_spi = spi_master_get_devdata(spi->master);
  327. a3700_spi->wait_mask = bit_mask;
  328. return a3700_spi_wait_completion(spi);
  329. }
  330. static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
  331. unsigned int bytes)
  332. {
  333. u32 val;
  334. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  335. val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
  336. val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
  337. val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
  338. val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
  339. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  340. }
  341. static void a3700_spi_transfer_setup(struct spi_device *spi,
  342. struct spi_transfer *xfer)
  343. {
  344. struct a3700_spi *a3700_spi;
  345. unsigned int byte_len;
  346. a3700_spi = spi_master_get_devdata(spi->master);
  347. a3700_spi_clock_set(a3700_spi, xfer->speed_hz, spi->mode);
  348. byte_len = xfer->bits_per_word >> 3;
  349. a3700_spi_fifo_thres_set(a3700_spi, byte_len);
  350. }
  351. static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
  352. {
  353. struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
  354. if (!enable)
  355. a3700_spi_activate_cs(a3700_spi, spi->chip_select);
  356. else
  357. a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
  358. }
  359. static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
  360. {
  361. u32 instr_cnt = 0, addr_cnt = 0, dummy_cnt = 0;
  362. u32 val = 0;
  363. /* Clear the header registers */
  364. spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
  365. spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
  366. spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
  367. /* Set header counters */
  368. if (a3700_spi->tx_buf) {
  369. if (a3700_spi->buf_len <= a3700_spi->instr_cnt) {
  370. instr_cnt = a3700_spi->buf_len;
  371. } else if (a3700_spi->buf_len <= (a3700_spi->instr_cnt +
  372. a3700_spi->addr_cnt)) {
  373. instr_cnt = a3700_spi->instr_cnt;
  374. addr_cnt = a3700_spi->buf_len - instr_cnt;
  375. } else if (a3700_spi->buf_len <= a3700_spi->hdr_cnt) {
  376. instr_cnt = a3700_spi->instr_cnt;
  377. addr_cnt = a3700_spi->addr_cnt;
  378. /* Need to handle the normal write case with 1 byte
  379. * data
  380. */
  381. if (!a3700_spi->tx_buf[instr_cnt + addr_cnt])
  382. dummy_cnt = a3700_spi->buf_len - instr_cnt -
  383. addr_cnt;
  384. }
  385. val |= ((instr_cnt & A3700_SPI_INSTR_CNT_MASK)
  386. << A3700_SPI_INSTR_CNT_BIT);
  387. val |= ((addr_cnt & A3700_SPI_ADDR_CNT_MASK)
  388. << A3700_SPI_ADDR_CNT_BIT);
  389. val |= ((dummy_cnt & A3700_SPI_DUMMY_CNT_MASK)
  390. << A3700_SPI_DUMMY_CNT_BIT);
  391. }
  392. spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
  393. /* Update the buffer length to be transferred */
  394. a3700_spi->buf_len -= (instr_cnt + addr_cnt + dummy_cnt);
  395. /* Set Instruction */
  396. val = 0;
  397. while (instr_cnt--) {
  398. val = (val << 8) | a3700_spi->tx_buf[0];
  399. a3700_spi->tx_buf++;
  400. }
  401. spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, val);
  402. /* Set Address */
  403. val = 0;
  404. while (addr_cnt--) {
  405. val = (val << 8) | a3700_spi->tx_buf[0];
  406. a3700_spi->tx_buf++;
  407. }
  408. spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
  409. }
  410. static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
  411. {
  412. u32 val;
  413. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  414. return (val & A3700_SPI_WFIFO_FULL);
  415. }
  416. static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
  417. {
  418. u32 val;
  419. int i = 0;
  420. while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
  421. val = 0;
  422. if (a3700_spi->buf_len >= 4) {
  423. val = cpu_to_le32(*(u32 *)a3700_spi->tx_buf);
  424. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
  425. a3700_spi->buf_len -= 4;
  426. a3700_spi->tx_buf += 4;
  427. } else {
  428. /*
  429. * If the remained buffer length is less than 4-bytes,
  430. * we should pad the write buffer with all ones. So that
  431. * it avoids overwrite the unexpected bytes following
  432. * the last one.
  433. */
  434. val = GENMASK(31, 0);
  435. while (a3700_spi->buf_len) {
  436. val &= ~(0xff << (8 * i));
  437. val |= *a3700_spi->tx_buf++ << (8 * i);
  438. i++;
  439. a3700_spi->buf_len--;
  440. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG,
  441. val);
  442. }
  443. break;
  444. }
  445. }
  446. return 0;
  447. }
  448. static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
  449. {
  450. u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  451. return (val & A3700_SPI_RFIFO_EMPTY);
  452. }
  453. static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
  454. {
  455. u32 val;
  456. while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
  457. val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
  458. if (a3700_spi->buf_len >= 4) {
  459. u32 data = le32_to_cpu(val);
  460. memcpy(a3700_spi->rx_buf, &data, 4);
  461. a3700_spi->buf_len -= 4;
  462. a3700_spi->rx_buf += 4;
  463. } else {
  464. /*
  465. * When remain bytes is not larger than 4, we should
  466. * avoid memory overwriting and just write the left rx
  467. * buffer bytes.
  468. */
  469. while (a3700_spi->buf_len) {
  470. *a3700_spi->rx_buf = val & 0xff;
  471. val >>= 8;
  472. a3700_spi->buf_len--;
  473. a3700_spi->rx_buf++;
  474. }
  475. }
  476. }
  477. return 0;
  478. }
  479. static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
  480. {
  481. int timeout = A3700_SPI_TIMEOUT;
  482. u32 val;
  483. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  484. val |= A3700_SPI_XFER_STOP;
  485. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  486. while (--timeout) {
  487. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  488. if (!(val & A3700_SPI_XFER_START))
  489. break;
  490. udelay(1);
  491. }
  492. a3700_spi_fifo_flush(a3700_spi);
  493. val &= ~A3700_SPI_XFER_STOP;
  494. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  495. }
  496. static int a3700_spi_prepare_message(struct spi_master *master,
  497. struct spi_message *message)
  498. {
  499. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  500. struct spi_device *spi = message->spi;
  501. int ret;
  502. ret = clk_enable(a3700_spi->clk);
  503. if (ret) {
  504. dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
  505. return ret;
  506. }
  507. /* Flush the FIFOs */
  508. ret = a3700_spi_fifo_flush(a3700_spi);
  509. if (ret)
  510. return ret;
  511. a3700_spi_bytelen_set(a3700_spi, 4);
  512. return 0;
  513. }
  514. static int a3700_spi_transfer_one(struct spi_master *master,
  515. struct spi_device *spi,
  516. struct spi_transfer *xfer)
  517. {
  518. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  519. int ret = 0, timeout = A3700_SPI_TIMEOUT;
  520. unsigned int nbits = 0;
  521. u32 val;
  522. a3700_spi_transfer_setup(spi, xfer);
  523. a3700_spi->tx_buf = xfer->tx_buf;
  524. a3700_spi->rx_buf = xfer->rx_buf;
  525. a3700_spi->buf_len = xfer->len;
  526. /* SPI transfer headers */
  527. a3700_spi_header_set(a3700_spi);
  528. if (xfer->tx_buf)
  529. nbits = xfer->tx_nbits;
  530. else if (xfer->rx_buf)
  531. nbits = xfer->rx_nbits;
  532. a3700_spi_pin_mode_set(a3700_spi, nbits);
  533. if (xfer->rx_buf) {
  534. /* Set read data length */
  535. spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
  536. a3700_spi->buf_len);
  537. /* Start READ transfer */
  538. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  539. val &= ~A3700_SPI_RW_EN;
  540. val |= A3700_SPI_XFER_START;
  541. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  542. } else if (xfer->tx_buf) {
  543. /* Start Write transfer */
  544. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  545. val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
  546. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  547. /*
  548. * If there are data to be written to the SPI device, xmit_data
  549. * flag is set true; otherwise the instruction in SPI_INSTR does
  550. * not require data to be written to the SPI device, then
  551. * xmit_data flag is set false.
  552. */
  553. a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
  554. }
  555. while (a3700_spi->buf_len) {
  556. if (a3700_spi->tx_buf) {
  557. /* Wait wfifo ready */
  558. if (!a3700_spi_transfer_wait(spi,
  559. A3700_SPI_WFIFO_RDY)) {
  560. dev_err(&spi->dev,
  561. "wait wfifo ready timed out\n");
  562. ret = -ETIMEDOUT;
  563. goto error;
  564. }
  565. /* Fill up the wfifo */
  566. ret = a3700_spi_fifo_write(a3700_spi);
  567. if (ret)
  568. goto error;
  569. } else if (a3700_spi->rx_buf) {
  570. /* Wait rfifo ready */
  571. if (!a3700_spi_transfer_wait(spi,
  572. A3700_SPI_RFIFO_RDY)) {
  573. dev_err(&spi->dev,
  574. "wait rfifo ready timed out\n");
  575. ret = -ETIMEDOUT;
  576. goto error;
  577. }
  578. /* Drain out the rfifo */
  579. ret = a3700_spi_fifo_read(a3700_spi);
  580. if (ret)
  581. goto error;
  582. }
  583. }
  584. /*
  585. * Stop a write transfer in fifo mode:
  586. * - wait all the bytes in wfifo to be shifted out
  587. * - set XFER_STOP bit
  588. * - wait XFER_START bit clear
  589. * - clear XFER_STOP bit
  590. * Stop a read transfer in fifo mode:
  591. * - the hardware is to reset the XFER_START bit
  592. * after the number of bytes indicated in DIN_CNT
  593. * register
  594. * - just wait XFER_START bit clear
  595. */
  596. if (a3700_spi->tx_buf) {
  597. if (a3700_spi->xmit_data) {
  598. /*
  599. * If there are data written to the SPI device, wait
  600. * until SPI_WFIFO_EMPTY is 1 to wait for all data to
  601. * transfer out of write FIFO.
  602. */
  603. if (!a3700_spi_transfer_wait(spi,
  604. A3700_SPI_WFIFO_EMPTY)) {
  605. dev_err(&spi->dev, "wait wfifo empty timed out\n");
  606. return -ETIMEDOUT;
  607. }
  608. } else {
  609. /*
  610. * If the instruction in SPI_INSTR does not require data
  611. * to be written to the SPI device, wait until SPI_RDY
  612. * is 1 for the SPI interface to be in idle.
  613. */
  614. if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
  615. dev_err(&spi->dev, "wait xfer ready timed out\n");
  616. return -ETIMEDOUT;
  617. }
  618. }
  619. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  620. val |= A3700_SPI_XFER_STOP;
  621. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  622. }
  623. while (--timeout) {
  624. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  625. if (!(val & A3700_SPI_XFER_START))
  626. break;
  627. udelay(1);
  628. }
  629. if (timeout == 0) {
  630. dev_err(&spi->dev, "wait transfer start clear timed out\n");
  631. ret = -ETIMEDOUT;
  632. goto error;
  633. }
  634. val &= ~A3700_SPI_XFER_STOP;
  635. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  636. goto out;
  637. error:
  638. a3700_spi_transfer_abort_fifo(a3700_spi);
  639. out:
  640. spi_finalize_current_transfer(master);
  641. return ret;
  642. }
  643. static int a3700_spi_unprepare_message(struct spi_master *master,
  644. struct spi_message *message)
  645. {
  646. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  647. clk_disable(a3700_spi->clk);
  648. return 0;
  649. }
  650. static const struct of_device_id a3700_spi_dt_ids[] = {
  651. { .compatible = "marvell,armada-3700-spi", .data = NULL },
  652. {},
  653. };
  654. MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
  655. static int a3700_spi_probe(struct platform_device *pdev)
  656. {
  657. struct device *dev = &pdev->dev;
  658. struct device_node *of_node = dev->of_node;
  659. struct resource *res;
  660. struct spi_master *master;
  661. struct a3700_spi *spi;
  662. u32 num_cs = 0;
  663. int irq, ret = 0;
  664. master = spi_alloc_master(dev, sizeof(*spi));
  665. if (!master) {
  666. dev_err(dev, "master allocation failed\n");
  667. ret = -ENOMEM;
  668. goto out;
  669. }
  670. if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
  671. dev_err(dev, "could not find num-cs\n");
  672. ret = -ENXIO;
  673. goto error;
  674. }
  675. master->bus_num = pdev->id;
  676. master->dev.of_node = of_node;
  677. master->mode_bits = SPI_MODE_3;
  678. master->num_chipselect = num_cs;
  679. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
  680. master->prepare_message = a3700_spi_prepare_message;
  681. master->transfer_one = a3700_spi_transfer_one;
  682. master->unprepare_message = a3700_spi_unprepare_message;
  683. master->set_cs = a3700_spi_set_cs;
  684. master->flags = SPI_MASTER_HALF_DUPLEX;
  685. master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
  686. SPI_RX_QUAD | SPI_TX_QUAD);
  687. platform_set_drvdata(pdev, master);
  688. spi = spi_master_get_devdata(master);
  689. memset(spi, 0, sizeof(struct a3700_spi));
  690. spi->master = master;
  691. spi->instr_cnt = A3700_INSTR_CNT;
  692. spi->addr_cnt = A3700_ADDR_CNT;
  693. spi->hdr_cnt = A3700_INSTR_CNT + A3700_ADDR_CNT +
  694. A3700_DUMMY_CNT;
  695. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  696. spi->base = devm_ioremap_resource(dev, res);
  697. if (IS_ERR(spi->base)) {
  698. ret = PTR_ERR(spi->base);
  699. goto error;
  700. }
  701. irq = platform_get_irq(pdev, 0);
  702. if (irq < 0) {
  703. dev_err(dev, "could not get irq: %d\n", irq);
  704. ret = -ENXIO;
  705. goto error;
  706. }
  707. spi->irq = irq;
  708. init_completion(&spi->done);
  709. spi->clk = devm_clk_get(dev, NULL);
  710. if (IS_ERR(spi->clk)) {
  711. dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
  712. goto error;
  713. }
  714. ret = clk_prepare(spi->clk);
  715. if (ret) {
  716. dev_err(dev, "could not prepare clk: %d\n", ret);
  717. goto error;
  718. }
  719. ret = a3700_spi_init(spi);
  720. if (ret)
  721. goto error_clk;
  722. ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
  723. dev_name(dev), master);
  724. if (ret) {
  725. dev_err(dev, "could not request IRQ: %d\n", ret);
  726. goto error_clk;
  727. }
  728. ret = devm_spi_register_master(dev, master);
  729. if (ret) {
  730. dev_err(dev, "Failed to register master\n");
  731. goto error_clk;
  732. }
  733. return 0;
  734. error_clk:
  735. clk_disable_unprepare(spi->clk);
  736. error:
  737. spi_master_put(master);
  738. out:
  739. return ret;
  740. }
  741. static int a3700_spi_remove(struct platform_device *pdev)
  742. {
  743. struct spi_master *master = platform_get_drvdata(pdev);
  744. struct a3700_spi *spi = spi_master_get_devdata(master);
  745. clk_unprepare(spi->clk);
  746. spi_master_put(master);
  747. return 0;
  748. }
  749. static struct platform_driver a3700_spi_driver = {
  750. .driver = {
  751. .name = DRIVER_NAME,
  752. .owner = THIS_MODULE,
  753. .of_match_table = of_match_ptr(a3700_spi_dt_ids),
  754. },
  755. .probe = a3700_spi_probe,
  756. .remove = a3700_spi_remove,
  757. };
  758. module_platform_driver(a3700_spi_driver);
  759. MODULE_DESCRIPTION("Armada-3700 SPI driver");
  760. MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
  761. MODULE_LICENSE("GPL");
  762. MODULE_ALIAS("platform:" DRIVER_NAME);