spi-armada-3700.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 SPI_NBITS_SINGLE:
  151. break;
  152. case SPI_NBITS_DUAL:
  153. val |= A3700_SPI_DATA_PIN0;
  154. break;
  155. case SPI_NBITS_QUAD:
  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. complete(&a3700_spi->done);
  280. return IRQ_HANDLED;
  281. }
  282. static bool a3700_spi_wait_completion(struct spi_device *spi)
  283. {
  284. struct a3700_spi *a3700_spi;
  285. unsigned int timeout;
  286. unsigned int ctrl_reg;
  287. unsigned long timeout_jiffies;
  288. a3700_spi = spi_master_get_devdata(spi->master);
  289. /* SPI interrupt is edge-triggered, which means an interrupt will
  290. * be generated only when detecting a specific status bit changed
  291. * from '0' to '1'. So when we start waiting for a interrupt, we
  292. * need to check status bit in control reg first, if it is already 1,
  293. * then we do not need to wait for interrupt
  294. */
  295. ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  296. if (a3700_spi->wait_mask & ctrl_reg)
  297. return true;
  298. reinit_completion(&a3700_spi->done);
  299. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG,
  300. a3700_spi->wait_mask);
  301. timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT);
  302. timeout = wait_for_completion_timeout(&a3700_spi->done,
  303. timeout_jiffies);
  304. a3700_spi->wait_mask = 0;
  305. if (timeout)
  306. return true;
  307. /* there might be the case that right after we checked the
  308. * status bits in this routine and before start to wait for
  309. * interrupt by wait_for_completion_timeout, the interrupt
  310. * happens, to avoid missing it we need to double check
  311. * status bits in control reg, if it is already 1, then
  312. * consider that we have the interrupt successfully and
  313. * return true.
  314. */
  315. ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  316. if (a3700_spi->wait_mask & ctrl_reg)
  317. return true;
  318. spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
  319. return true;
  320. }
  321. static bool a3700_spi_transfer_wait(struct spi_device *spi,
  322. unsigned int bit_mask)
  323. {
  324. struct a3700_spi *a3700_spi;
  325. a3700_spi = spi_master_get_devdata(spi->master);
  326. a3700_spi->wait_mask = bit_mask;
  327. return a3700_spi_wait_completion(spi);
  328. }
  329. static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
  330. unsigned int bytes)
  331. {
  332. u32 val;
  333. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  334. val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
  335. val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
  336. val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
  337. val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
  338. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  339. }
  340. static void a3700_spi_transfer_setup(struct spi_device *spi,
  341. struct spi_transfer *xfer)
  342. {
  343. struct a3700_spi *a3700_spi;
  344. unsigned int byte_len;
  345. a3700_spi = spi_master_get_devdata(spi->master);
  346. a3700_spi_clock_set(a3700_spi, xfer->speed_hz, spi->mode);
  347. byte_len = xfer->bits_per_word >> 3;
  348. a3700_spi_fifo_thres_set(a3700_spi, byte_len);
  349. }
  350. static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
  351. {
  352. struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
  353. if (!enable)
  354. a3700_spi_activate_cs(a3700_spi, spi->chip_select);
  355. else
  356. a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
  357. }
  358. static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
  359. {
  360. u32 instr_cnt = 0, addr_cnt = 0, dummy_cnt = 0;
  361. u32 val = 0;
  362. /* Clear the header registers */
  363. spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
  364. spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
  365. spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
  366. /* Set header counters */
  367. if (a3700_spi->tx_buf) {
  368. if (a3700_spi->buf_len <= a3700_spi->instr_cnt) {
  369. instr_cnt = a3700_spi->buf_len;
  370. } else if (a3700_spi->buf_len <= (a3700_spi->instr_cnt +
  371. a3700_spi->addr_cnt)) {
  372. instr_cnt = a3700_spi->instr_cnt;
  373. addr_cnt = a3700_spi->buf_len - instr_cnt;
  374. } else if (a3700_spi->buf_len <= a3700_spi->hdr_cnt) {
  375. instr_cnt = a3700_spi->instr_cnt;
  376. addr_cnt = a3700_spi->addr_cnt;
  377. /* Need to handle the normal write case with 1 byte
  378. * data
  379. */
  380. if (!a3700_spi->tx_buf[instr_cnt + addr_cnt])
  381. dummy_cnt = a3700_spi->buf_len - instr_cnt -
  382. addr_cnt;
  383. }
  384. val |= ((instr_cnt & A3700_SPI_INSTR_CNT_MASK)
  385. << A3700_SPI_INSTR_CNT_BIT);
  386. val |= ((addr_cnt & A3700_SPI_ADDR_CNT_MASK)
  387. << A3700_SPI_ADDR_CNT_BIT);
  388. val |= ((dummy_cnt & A3700_SPI_DUMMY_CNT_MASK)
  389. << A3700_SPI_DUMMY_CNT_BIT);
  390. }
  391. spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
  392. /* Update the buffer length to be transferred */
  393. a3700_spi->buf_len -= (instr_cnt + addr_cnt + dummy_cnt);
  394. /* Set Instruction */
  395. val = 0;
  396. while (instr_cnt--) {
  397. val = (val << 8) | a3700_spi->tx_buf[0];
  398. a3700_spi->tx_buf++;
  399. }
  400. spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, val);
  401. /* Set Address */
  402. val = 0;
  403. while (addr_cnt--) {
  404. val = (val << 8) | a3700_spi->tx_buf[0];
  405. a3700_spi->tx_buf++;
  406. }
  407. spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
  408. }
  409. static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
  410. {
  411. u32 val;
  412. val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  413. return (val & A3700_SPI_WFIFO_FULL);
  414. }
  415. static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
  416. {
  417. u32 val;
  418. int i = 0;
  419. while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
  420. val = 0;
  421. if (a3700_spi->buf_len >= 4) {
  422. val = cpu_to_le32(*(u32 *)a3700_spi->tx_buf);
  423. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
  424. a3700_spi->buf_len -= 4;
  425. a3700_spi->tx_buf += 4;
  426. } else {
  427. /*
  428. * If the remained buffer length is less than 4-bytes,
  429. * we should pad the write buffer with all ones. So that
  430. * it avoids overwrite the unexpected bytes following
  431. * the last one.
  432. */
  433. val = GENMASK(31, 0);
  434. while (a3700_spi->buf_len) {
  435. val &= ~(0xff << (8 * i));
  436. val |= *a3700_spi->tx_buf++ << (8 * i);
  437. i++;
  438. a3700_spi->buf_len--;
  439. spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG,
  440. val);
  441. }
  442. break;
  443. }
  444. }
  445. return 0;
  446. }
  447. static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
  448. {
  449. u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
  450. return (val & A3700_SPI_RFIFO_EMPTY);
  451. }
  452. static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
  453. {
  454. u32 val;
  455. while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
  456. val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
  457. if (a3700_spi->buf_len >= 4) {
  458. u32 data = le32_to_cpu(val);
  459. memcpy(a3700_spi->rx_buf, &data, 4);
  460. a3700_spi->buf_len -= 4;
  461. a3700_spi->rx_buf += 4;
  462. } else {
  463. /*
  464. * When remain bytes is not larger than 4, we should
  465. * avoid memory overwriting and just write the left rx
  466. * buffer bytes.
  467. */
  468. while (a3700_spi->buf_len) {
  469. *a3700_spi->rx_buf = val & 0xff;
  470. val >>= 8;
  471. a3700_spi->buf_len--;
  472. a3700_spi->rx_buf++;
  473. }
  474. }
  475. }
  476. return 0;
  477. }
  478. static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
  479. {
  480. int timeout = A3700_SPI_TIMEOUT;
  481. u32 val;
  482. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  483. val |= A3700_SPI_XFER_STOP;
  484. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  485. while (--timeout) {
  486. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  487. if (!(val & A3700_SPI_XFER_START))
  488. break;
  489. udelay(1);
  490. }
  491. a3700_spi_fifo_flush(a3700_spi);
  492. val &= ~A3700_SPI_XFER_STOP;
  493. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  494. }
  495. static int a3700_spi_prepare_message(struct spi_master *master,
  496. struct spi_message *message)
  497. {
  498. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  499. struct spi_device *spi = message->spi;
  500. int ret;
  501. ret = clk_enable(a3700_spi->clk);
  502. if (ret) {
  503. dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
  504. return ret;
  505. }
  506. /* Flush the FIFOs */
  507. ret = a3700_spi_fifo_flush(a3700_spi);
  508. if (ret)
  509. return ret;
  510. a3700_spi_bytelen_set(a3700_spi, 4);
  511. return 0;
  512. }
  513. static int a3700_spi_transfer_one(struct spi_master *master,
  514. struct spi_device *spi,
  515. struct spi_transfer *xfer)
  516. {
  517. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  518. int ret = 0, timeout = A3700_SPI_TIMEOUT;
  519. unsigned int nbits = 0;
  520. u32 val;
  521. a3700_spi_transfer_setup(spi, xfer);
  522. a3700_spi->tx_buf = xfer->tx_buf;
  523. a3700_spi->rx_buf = xfer->rx_buf;
  524. a3700_spi->buf_len = xfer->len;
  525. /* SPI transfer headers */
  526. a3700_spi_header_set(a3700_spi);
  527. if (xfer->tx_buf)
  528. nbits = xfer->tx_nbits;
  529. else if (xfer->rx_buf)
  530. nbits = xfer->rx_nbits;
  531. a3700_spi_pin_mode_set(a3700_spi, nbits);
  532. if (xfer->rx_buf) {
  533. /* Set read data length */
  534. spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
  535. a3700_spi->buf_len);
  536. /* Start READ transfer */
  537. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  538. val &= ~A3700_SPI_RW_EN;
  539. val |= A3700_SPI_XFER_START;
  540. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  541. } else if (xfer->tx_buf) {
  542. /* Start Write transfer */
  543. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  544. val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
  545. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  546. /*
  547. * If there are data to be written to the SPI device, xmit_data
  548. * flag is set true; otherwise the instruction in SPI_INSTR does
  549. * not require data to be written to the SPI device, then
  550. * xmit_data flag is set false.
  551. */
  552. a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
  553. }
  554. while (a3700_spi->buf_len) {
  555. if (a3700_spi->tx_buf) {
  556. /* Wait wfifo ready */
  557. if (!a3700_spi_transfer_wait(spi,
  558. A3700_SPI_WFIFO_RDY)) {
  559. dev_err(&spi->dev,
  560. "wait wfifo ready timed out\n");
  561. ret = -ETIMEDOUT;
  562. goto error;
  563. }
  564. /* Fill up the wfifo */
  565. ret = a3700_spi_fifo_write(a3700_spi);
  566. if (ret)
  567. goto error;
  568. } else if (a3700_spi->rx_buf) {
  569. /* Wait rfifo ready */
  570. if (!a3700_spi_transfer_wait(spi,
  571. A3700_SPI_RFIFO_RDY)) {
  572. dev_err(&spi->dev,
  573. "wait rfifo ready timed out\n");
  574. ret = -ETIMEDOUT;
  575. goto error;
  576. }
  577. /* Drain out the rfifo */
  578. ret = a3700_spi_fifo_read(a3700_spi);
  579. if (ret)
  580. goto error;
  581. }
  582. }
  583. /*
  584. * Stop a write transfer in fifo mode:
  585. * - wait all the bytes in wfifo to be shifted out
  586. * - set XFER_STOP bit
  587. * - wait XFER_START bit clear
  588. * - clear XFER_STOP bit
  589. * Stop a read transfer in fifo mode:
  590. * - the hardware is to reset the XFER_START bit
  591. * after the number of bytes indicated in DIN_CNT
  592. * register
  593. * - just wait XFER_START bit clear
  594. */
  595. if (a3700_spi->tx_buf) {
  596. if (a3700_spi->xmit_data) {
  597. /*
  598. * If there are data written to the SPI device, wait
  599. * until SPI_WFIFO_EMPTY is 1 to wait for all data to
  600. * transfer out of write FIFO.
  601. */
  602. if (!a3700_spi_transfer_wait(spi,
  603. A3700_SPI_WFIFO_EMPTY)) {
  604. dev_err(&spi->dev, "wait wfifo empty timed out\n");
  605. return -ETIMEDOUT;
  606. }
  607. } else {
  608. /*
  609. * If the instruction in SPI_INSTR does not require data
  610. * to be written to the SPI device, wait until SPI_RDY
  611. * is 1 for the SPI interface to be in idle.
  612. */
  613. if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
  614. dev_err(&spi->dev, "wait xfer ready timed out\n");
  615. return -ETIMEDOUT;
  616. }
  617. }
  618. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  619. val |= A3700_SPI_XFER_STOP;
  620. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  621. }
  622. while (--timeout) {
  623. val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
  624. if (!(val & A3700_SPI_XFER_START))
  625. break;
  626. udelay(1);
  627. }
  628. if (timeout == 0) {
  629. dev_err(&spi->dev, "wait transfer start clear timed out\n");
  630. ret = -ETIMEDOUT;
  631. goto error;
  632. }
  633. val &= ~A3700_SPI_XFER_STOP;
  634. spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
  635. goto out;
  636. error:
  637. a3700_spi_transfer_abort_fifo(a3700_spi);
  638. out:
  639. spi_finalize_current_transfer(master);
  640. return ret;
  641. }
  642. static int a3700_spi_unprepare_message(struct spi_master *master,
  643. struct spi_message *message)
  644. {
  645. struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
  646. clk_disable(a3700_spi->clk);
  647. return 0;
  648. }
  649. static const struct of_device_id a3700_spi_dt_ids[] = {
  650. { .compatible = "marvell,armada-3700-spi", .data = NULL },
  651. {},
  652. };
  653. MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
  654. static int a3700_spi_probe(struct platform_device *pdev)
  655. {
  656. struct device *dev = &pdev->dev;
  657. struct device_node *of_node = dev->of_node;
  658. struct resource *res;
  659. struct spi_master *master;
  660. struct a3700_spi *spi;
  661. u32 num_cs = 0;
  662. int irq, ret = 0;
  663. master = spi_alloc_master(dev, sizeof(*spi));
  664. if (!master) {
  665. dev_err(dev, "master allocation failed\n");
  666. ret = -ENOMEM;
  667. goto out;
  668. }
  669. if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
  670. dev_err(dev, "could not find num-cs\n");
  671. ret = -ENXIO;
  672. goto error;
  673. }
  674. master->bus_num = pdev->id;
  675. master->dev.of_node = of_node;
  676. master->mode_bits = SPI_MODE_3;
  677. master->num_chipselect = num_cs;
  678. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
  679. master->prepare_message = a3700_spi_prepare_message;
  680. master->transfer_one = a3700_spi_transfer_one;
  681. master->unprepare_message = a3700_spi_unprepare_message;
  682. master->set_cs = a3700_spi_set_cs;
  683. master->flags = SPI_MASTER_HALF_DUPLEX;
  684. master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
  685. SPI_RX_QUAD | SPI_TX_QUAD);
  686. platform_set_drvdata(pdev, master);
  687. spi = spi_master_get_devdata(master);
  688. memset(spi, 0, sizeof(struct a3700_spi));
  689. spi->master = master;
  690. spi->instr_cnt = A3700_INSTR_CNT;
  691. spi->addr_cnt = A3700_ADDR_CNT;
  692. spi->hdr_cnt = A3700_INSTR_CNT + A3700_ADDR_CNT +
  693. A3700_DUMMY_CNT;
  694. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  695. spi->base = devm_ioremap_resource(dev, res);
  696. if (IS_ERR(spi->base)) {
  697. ret = PTR_ERR(spi->base);
  698. goto error;
  699. }
  700. irq = platform_get_irq(pdev, 0);
  701. if (irq < 0) {
  702. dev_err(dev, "could not get irq: %d\n", irq);
  703. ret = -ENXIO;
  704. goto error;
  705. }
  706. spi->irq = irq;
  707. init_completion(&spi->done);
  708. spi->clk = devm_clk_get(dev, NULL);
  709. if (IS_ERR(spi->clk)) {
  710. dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
  711. goto error;
  712. }
  713. ret = clk_prepare(spi->clk);
  714. if (ret) {
  715. dev_err(dev, "could not prepare clk: %d\n", ret);
  716. goto error;
  717. }
  718. ret = a3700_spi_init(spi);
  719. if (ret)
  720. goto error_clk;
  721. ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
  722. dev_name(dev), master);
  723. if (ret) {
  724. dev_err(dev, "could not request IRQ: %d\n", ret);
  725. goto error_clk;
  726. }
  727. ret = devm_spi_register_master(dev, master);
  728. if (ret) {
  729. dev_err(dev, "Failed to register master\n");
  730. goto error_clk;
  731. }
  732. return 0;
  733. error_clk:
  734. clk_disable_unprepare(spi->clk);
  735. error:
  736. spi_master_put(master);
  737. out:
  738. return ret;
  739. }
  740. static int a3700_spi_remove(struct platform_device *pdev)
  741. {
  742. struct spi_master *master = platform_get_drvdata(pdev);
  743. struct a3700_spi *spi = spi_master_get_devdata(master);
  744. clk_unprepare(spi->clk);
  745. return 0;
  746. }
  747. static struct platform_driver a3700_spi_driver = {
  748. .driver = {
  749. .name = DRIVER_NAME,
  750. .of_match_table = of_match_ptr(a3700_spi_dt_ids),
  751. },
  752. .probe = a3700_spi_probe,
  753. .remove = a3700_spi_remove,
  754. };
  755. module_platform_driver(a3700_spi_driver);
  756. MODULE_DESCRIPTION("Armada-3700 SPI driver");
  757. MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
  758. MODULE_LICENSE("GPL");
  759. MODULE_ALIAS("platform:" DRIVER_NAME);