spi-armada-3700.c 24 KB

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