spi-sirf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. * SPI bus driver for CSR SiRFprimaII
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/clk.h>
  12. #include <linux/completion.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spi_bitbang.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/dma-direction.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/reset.h>
  26. #define DRIVER_NAME "sirfsoc_spi"
  27. #define SIRFSOC_SPI_CTRL 0x0000
  28. #define SIRFSOC_SPI_CMD 0x0004
  29. #define SIRFSOC_SPI_TX_RX_EN 0x0008
  30. #define SIRFSOC_SPI_INT_EN 0x000C
  31. #define SIRFSOC_SPI_INT_STATUS 0x0010
  32. #define SIRFSOC_SPI_TX_DMA_IO_CTRL 0x0100
  33. #define SIRFSOC_SPI_TX_DMA_IO_LEN 0x0104
  34. #define SIRFSOC_SPI_TXFIFO_CTRL 0x0108
  35. #define SIRFSOC_SPI_TXFIFO_LEVEL_CHK 0x010C
  36. #define SIRFSOC_SPI_TXFIFO_OP 0x0110
  37. #define SIRFSOC_SPI_TXFIFO_STATUS 0x0114
  38. #define SIRFSOC_SPI_TXFIFO_DATA 0x0118
  39. #define SIRFSOC_SPI_RX_DMA_IO_CTRL 0x0120
  40. #define SIRFSOC_SPI_RX_DMA_IO_LEN 0x0124
  41. #define SIRFSOC_SPI_RXFIFO_CTRL 0x0128
  42. #define SIRFSOC_SPI_RXFIFO_LEVEL_CHK 0x012C
  43. #define SIRFSOC_SPI_RXFIFO_OP 0x0130
  44. #define SIRFSOC_SPI_RXFIFO_STATUS 0x0134
  45. #define SIRFSOC_SPI_RXFIFO_DATA 0x0138
  46. #define SIRFSOC_SPI_DUMMY_DELAY_CTL 0x0144
  47. /* SPI CTRL register defines */
  48. #define SIRFSOC_SPI_SLV_MODE BIT(16)
  49. #define SIRFSOC_SPI_CMD_MODE BIT(17)
  50. #define SIRFSOC_SPI_CS_IO_OUT BIT(18)
  51. #define SIRFSOC_SPI_CS_IO_MODE BIT(19)
  52. #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
  53. #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
  54. #define SIRFSOC_SPI_TRAN_MSB BIT(22)
  55. #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
  56. #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
  57. #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
  58. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
  59. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
  60. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
  61. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
  62. #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
  63. #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
  64. #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
  65. /* Interrupt Enable */
  66. #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
  67. #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
  68. #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
  69. #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
  70. #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
  71. #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
  72. #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
  73. #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
  74. #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
  75. #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
  76. #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
  77. #define SIRFSOC_SPI_INT_MASK_ALL 0x1FFF
  78. /* Interrupt status */
  79. #define SIRFSOC_SPI_RX_DONE BIT(0)
  80. #define SIRFSOC_SPI_TX_DONE BIT(1)
  81. #define SIRFSOC_SPI_RX_OFLOW BIT(2)
  82. #define SIRFSOC_SPI_TX_UFLOW BIT(3)
  83. #define SIRFSOC_SPI_RX_IO_DMA BIT(4)
  84. #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
  85. #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
  86. #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
  87. #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
  88. #define SIRFSOC_SPI_FRM_END BIT(10)
  89. /* TX RX enable */
  90. #define SIRFSOC_SPI_RX_EN BIT(0)
  91. #define SIRFSOC_SPI_TX_EN BIT(1)
  92. #define SIRFSOC_SPI_CMD_TX_EN BIT(2)
  93. #define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
  94. #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
  95. /* FIFO OPs */
  96. #define SIRFSOC_SPI_FIFO_RESET BIT(0)
  97. #define SIRFSOC_SPI_FIFO_START BIT(1)
  98. /* FIFO CTRL */
  99. #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
  100. #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
  101. #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
  102. /* FIFO Status */
  103. #define SIRFSOC_SPI_FIFO_LEVEL_MASK 0xFF
  104. #define SIRFSOC_SPI_FIFO_FULL BIT(8)
  105. #define SIRFSOC_SPI_FIFO_EMPTY BIT(9)
  106. /* 256 bytes rx/tx FIFO */
  107. #define SIRFSOC_SPI_FIFO_SIZE 256
  108. #define SIRFSOC_SPI_DAT_FRM_LEN_MAX (64 * 1024)
  109. #define SIRFSOC_SPI_FIFO_SC(x) ((x) & 0x3F)
  110. #define SIRFSOC_SPI_FIFO_LC(x) (((x) & 0x3F) << 10)
  111. #define SIRFSOC_SPI_FIFO_HC(x) (((x) & 0x3F) << 20)
  112. #define SIRFSOC_SPI_FIFO_THD(x) (((x) & 0xFF) << 2)
  113. /*
  114. * only if the rx/tx buffer and transfer size are 4-bytes aligned, we use dma
  115. * due to the limitation of dma controller
  116. */
  117. #define ALIGNED(x) (!((u32)x & 0x3))
  118. #define IS_DMA_VALID(x) (x && ALIGNED(x->tx_buf) && ALIGNED(x->rx_buf) && \
  119. ALIGNED(x->len) && (x->len < 2 * PAGE_SIZE))
  120. #define SIRFSOC_MAX_CMD_BYTES 4
  121. #define SIRFSOC_SPI_DEFAULT_FRQ 1000000
  122. struct sirfsoc_spi {
  123. struct spi_bitbang bitbang;
  124. struct completion rx_done;
  125. struct completion tx_done;
  126. void __iomem *base;
  127. u32 ctrl_freq; /* SPI controller clock speed */
  128. struct clk *clk;
  129. /* rx & tx bufs from the spi_transfer */
  130. const void *tx;
  131. void *rx;
  132. /* place received word into rx buffer */
  133. void (*rx_word) (struct sirfsoc_spi *);
  134. /* get word from tx buffer for sending */
  135. void (*tx_word) (struct sirfsoc_spi *);
  136. /* number of words left to be tranmitted/received */
  137. unsigned int left_tx_word;
  138. unsigned int left_rx_word;
  139. /* rx & tx DMA channels */
  140. struct dma_chan *rx_chan;
  141. struct dma_chan *tx_chan;
  142. dma_addr_t src_start;
  143. dma_addr_t dst_start;
  144. void *dummypage;
  145. int word_width; /* in bytes */
  146. /*
  147. * if tx size is not more than 4 and rx size is NULL, use
  148. * command model
  149. */
  150. bool tx_by_cmd;
  151. bool hw_cs;
  152. };
  153. static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi)
  154. {
  155. u32 data;
  156. u8 *rx = sspi->rx;
  157. data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
  158. if (rx) {
  159. *rx++ = (u8) data;
  160. sspi->rx = rx;
  161. }
  162. sspi->left_rx_word--;
  163. }
  164. static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi)
  165. {
  166. u32 data = 0;
  167. const u8 *tx = sspi->tx;
  168. if (tx) {
  169. data = *tx++;
  170. sspi->tx = tx;
  171. }
  172. writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
  173. sspi->left_tx_word--;
  174. }
  175. static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi)
  176. {
  177. u32 data;
  178. u16 *rx = sspi->rx;
  179. data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
  180. if (rx) {
  181. *rx++ = (u16) data;
  182. sspi->rx = rx;
  183. }
  184. sspi->left_rx_word--;
  185. }
  186. static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi)
  187. {
  188. u32 data = 0;
  189. const u16 *tx = sspi->tx;
  190. if (tx) {
  191. data = *tx++;
  192. sspi->tx = tx;
  193. }
  194. writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
  195. sspi->left_tx_word--;
  196. }
  197. static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi)
  198. {
  199. u32 data;
  200. u32 *rx = sspi->rx;
  201. data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
  202. if (rx) {
  203. *rx++ = (u32) data;
  204. sspi->rx = rx;
  205. }
  206. sspi->left_rx_word--;
  207. }
  208. static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi)
  209. {
  210. u32 data = 0;
  211. const u32 *tx = sspi->tx;
  212. if (tx) {
  213. data = *tx++;
  214. sspi->tx = tx;
  215. }
  216. writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
  217. sspi->left_tx_word--;
  218. }
  219. static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id)
  220. {
  221. struct sirfsoc_spi *sspi = dev_id;
  222. u32 spi_stat = readl(sspi->base + SIRFSOC_SPI_INT_STATUS);
  223. if (sspi->tx_by_cmd && (spi_stat & SIRFSOC_SPI_FRM_END)) {
  224. complete(&sspi->tx_done);
  225. writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
  226. writel(SIRFSOC_SPI_INT_MASK_ALL,
  227. sspi->base + SIRFSOC_SPI_INT_STATUS);
  228. return IRQ_HANDLED;
  229. }
  230. /* Error Conditions */
  231. if (spi_stat & SIRFSOC_SPI_RX_OFLOW ||
  232. spi_stat & SIRFSOC_SPI_TX_UFLOW) {
  233. complete(&sspi->tx_done);
  234. complete(&sspi->rx_done);
  235. writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
  236. writel(SIRFSOC_SPI_INT_MASK_ALL,
  237. sspi->base + SIRFSOC_SPI_INT_STATUS);
  238. return IRQ_HANDLED;
  239. }
  240. if (spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY)
  241. complete(&sspi->tx_done);
  242. while (!(readl(sspi->base + SIRFSOC_SPI_INT_STATUS) &
  243. SIRFSOC_SPI_RX_IO_DMA))
  244. cpu_relax();
  245. complete(&sspi->rx_done);
  246. writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
  247. writel(SIRFSOC_SPI_INT_MASK_ALL,
  248. sspi->base + SIRFSOC_SPI_INT_STATUS);
  249. return IRQ_HANDLED;
  250. }
  251. static void spi_sirfsoc_dma_fini_callback(void *data)
  252. {
  253. struct completion *dma_complete = data;
  254. complete(dma_complete);
  255. }
  256. static void spi_sirfsoc_cmd_transfer(struct spi_device *spi,
  257. struct spi_transfer *t)
  258. {
  259. struct sirfsoc_spi *sspi;
  260. int timeout = t->len * 10;
  261. u32 cmd;
  262. sspi = spi_master_get_devdata(spi->master);
  263. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  264. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  265. memcpy(&cmd, sspi->tx, t->len);
  266. if (sspi->word_width == 1 && !(spi->mode & SPI_LSB_FIRST))
  267. cmd = cpu_to_be32(cmd) >>
  268. ((SIRFSOC_MAX_CMD_BYTES - t->len) * 8);
  269. if (sspi->word_width == 2 && t->len == 4 &&
  270. (!(spi->mode & SPI_LSB_FIRST)))
  271. cmd = ((cmd & 0xffff) << 16) | (cmd >> 16);
  272. writel(cmd, sspi->base + SIRFSOC_SPI_CMD);
  273. writel(SIRFSOC_SPI_FRM_END_INT_EN,
  274. sspi->base + SIRFSOC_SPI_INT_EN);
  275. writel(SIRFSOC_SPI_CMD_TX_EN,
  276. sspi->base + SIRFSOC_SPI_TX_RX_EN);
  277. if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
  278. dev_err(&spi->dev, "cmd transfer timeout\n");
  279. return;
  280. }
  281. sspi->left_rx_word -= t->len;
  282. }
  283. static void spi_sirfsoc_dma_transfer(struct spi_device *spi,
  284. struct spi_transfer *t)
  285. {
  286. struct sirfsoc_spi *sspi;
  287. struct dma_async_tx_descriptor *rx_desc, *tx_desc;
  288. int timeout = t->len * 10;
  289. sspi = spi_master_get_devdata(spi->master);
  290. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  291. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  292. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  293. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  294. writel(0, sspi->base + SIRFSOC_SPI_INT_EN);
  295. writel(SIRFSOC_SPI_INT_MASK_ALL, sspi->base + SIRFSOC_SPI_INT_STATUS);
  296. if (sspi->left_tx_word < SIRFSOC_SPI_DAT_FRM_LEN_MAX) {
  297. writel(readl(sspi->base + SIRFSOC_SPI_CTRL) |
  298. SIRFSOC_SPI_ENA_AUTO_CLR | SIRFSOC_SPI_MUL_DAT_MODE,
  299. sspi->base + SIRFSOC_SPI_CTRL);
  300. writel(sspi->left_tx_word - 1,
  301. sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
  302. writel(sspi->left_tx_word - 1,
  303. sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
  304. } else {
  305. writel(readl(sspi->base + SIRFSOC_SPI_CTRL),
  306. sspi->base + SIRFSOC_SPI_CTRL);
  307. writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
  308. writel(0, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
  309. }
  310. sspi->dst_start = dma_map_single(&spi->dev, sspi->rx, t->len,
  311. (t->tx_buf != t->rx_buf) ?
  312. DMA_FROM_DEVICE : DMA_BIDIRECTIONAL);
  313. rx_desc = dmaengine_prep_slave_single(sspi->rx_chan,
  314. sspi->dst_start, t->len, DMA_DEV_TO_MEM,
  315. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  316. rx_desc->callback = spi_sirfsoc_dma_fini_callback;
  317. rx_desc->callback_param = &sspi->rx_done;
  318. sspi->src_start = dma_map_single(&spi->dev, (void *)sspi->tx, t->len,
  319. (t->tx_buf != t->rx_buf) ?
  320. DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  321. tx_desc = dmaengine_prep_slave_single(sspi->tx_chan,
  322. sspi->src_start, t->len, DMA_MEM_TO_DEV,
  323. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  324. tx_desc->callback = spi_sirfsoc_dma_fini_callback;
  325. tx_desc->callback_param = &sspi->tx_done;
  326. dmaengine_submit(tx_desc);
  327. dmaengine_submit(rx_desc);
  328. dma_async_issue_pending(sspi->tx_chan);
  329. dma_async_issue_pending(sspi->rx_chan);
  330. writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
  331. sspi->base + SIRFSOC_SPI_TX_RX_EN);
  332. if (wait_for_completion_timeout(&sspi->rx_done, timeout) == 0) {
  333. dev_err(&spi->dev, "transfer timeout\n");
  334. dmaengine_terminate_all(sspi->rx_chan);
  335. } else
  336. sspi->left_rx_word = 0;
  337. /*
  338. * we only wait tx-done event if transferring by DMA. for PIO,
  339. * we get rx data by writing tx data, so if rx is done, tx has
  340. * done earlier
  341. */
  342. if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
  343. dev_err(&spi->dev, "transfer timeout\n");
  344. dmaengine_terminate_all(sspi->tx_chan);
  345. }
  346. dma_unmap_single(&spi->dev, sspi->src_start, t->len, DMA_TO_DEVICE);
  347. dma_unmap_single(&spi->dev, sspi->dst_start, t->len, DMA_FROM_DEVICE);
  348. /* TX, RX FIFO stop */
  349. writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  350. writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  351. if (sspi->left_tx_word >= SIRFSOC_SPI_DAT_FRM_LEN_MAX)
  352. writel(0, sspi->base + SIRFSOC_SPI_TX_RX_EN);
  353. }
  354. static void spi_sirfsoc_pio_transfer(struct spi_device *spi,
  355. struct spi_transfer *t)
  356. {
  357. struct sirfsoc_spi *sspi;
  358. int timeout = t->len * 10;
  359. sspi = spi_master_get_devdata(spi->master);
  360. do {
  361. writel(SIRFSOC_SPI_FIFO_RESET,
  362. sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  363. writel(SIRFSOC_SPI_FIFO_RESET,
  364. sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  365. writel(SIRFSOC_SPI_FIFO_START,
  366. sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  367. writel(SIRFSOC_SPI_FIFO_START,
  368. sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  369. writel(0, sspi->base + SIRFSOC_SPI_INT_EN);
  370. writel(SIRFSOC_SPI_INT_MASK_ALL,
  371. sspi->base + SIRFSOC_SPI_INT_STATUS);
  372. writel(readl(sspi->base + SIRFSOC_SPI_CTRL) |
  373. SIRFSOC_SPI_MUL_DAT_MODE | SIRFSOC_SPI_ENA_AUTO_CLR,
  374. sspi->base + SIRFSOC_SPI_CTRL);
  375. writel(min(sspi->left_tx_word, (u32)(256 / sspi->word_width))
  376. - 1, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
  377. writel(min(sspi->left_rx_word, (u32)(256 / sspi->word_width))
  378. - 1, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
  379. while (!((readl(sspi->base + SIRFSOC_SPI_TXFIFO_STATUS)
  380. & SIRFSOC_SPI_FIFO_FULL)) && sspi->left_tx_word)
  381. sspi->tx_word(sspi);
  382. writel(SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN |
  383. SIRFSOC_SPI_TX_UFLOW_INT_EN |
  384. SIRFSOC_SPI_RX_OFLOW_INT_EN |
  385. SIRFSOC_SPI_RX_IO_DMA_INT_EN,
  386. sspi->base + SIRFSOC_SPI_INT_EN);
  387. writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
  388. sspi->base + SIRFSOC_SPI_TX_RX_EN);
  389. if (!wait_for_completion_timeout(&sspi->tx_done, timeout) ||
  390. !wait_for_completion_timeout(&sspi->rx_done, timeout)) {
  391. dev_err(&spi->dev, "transfer timeout\n");
  392. break;
  393. }
  394. while (!((readl(sspi->base + SIRFSOC_SPI_RXFIFO_STATUS)
  395. & SIRFSOC_SPI_FIFO_EMPTY)) && sspi->left_rx_word)
  396. sspi->rx_word(sspi);
  397. writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  398. writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  399. } while (sspi->left_tx_word != 0 || sspi->left_rx_word != 0);
  400. }
  401. static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t)
  402. {
  403. struct sirfsoc_spi *sspi;
  404. sspi = spi_master_get_devdata(spi->master);
  405. sspi->tx = t->tx_buf ? t->tx_buf : sspi->dummypage;
  406. sspi->rx = t->rx_buf ? t->rx_buf : sspi->dummypage;
  407. sspi->left_tx_word = sspi->left_rx_word = t->len / sspi->word_width;
  408. reinit_completion(&sspi->rx_done);
  409. reinit_completion(&sspi->tx_done);
  410. /*
  411. * in the transfer, if transfer data using command register with rx_buf
  412. * null, just fill command data into command register and wait for its
  413. * completion.
  414. */
  415. if (sspi->tx_by_cmd)
  416. spi_sirfsoc_cmd_transfer(spi, t);
  417. else if (IS_DMA_VALID(t))
  418. spi_sirfsoc_dma_transfer(spi, t);
  419. else
  420. spi_sirfsoc_pio_transfer(spi, t);
  421. return t->len - sspi->left_rx_word * sspi->word_width;
  422. }
  423. static void spi_sirfsoc_chipselect(struct spi_device *spi, int value)
  424. {
  425. struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master);
  426. if (sspi->hw_cs) {
  427. u32 regval = readl(sspi->base + SIRFSOC_SPI_CTRL);
  428. switch (value) {
  429. case BITBANG_CS_ACTIVE:
  430. if (spi->mode & SPI_CS_HIGH)
  431. regval |= SIRFSOC_SPI_CS_IO_OUT;
  432. else
  433. regval &= ~SIRFSOC_SPI_CS_IO_OUT;
  434. break;
  435. case BITBANG_CS_INACTIVE:
  436. if (spi->mode & SPI_CS_HIGH)
  437. regval &= ~SIRFSOC_SPI_CS_IO_OUT;
  438. else
  439. regval |= SIRFSOC_SPI_CS_IO_OUT;
  440. break;
  441. }
  442. writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
  443. } else {
  444. switch (value) {
  445. case BITBANG_CS_ACTIVE:
  446. gpio_direction_output(spi->cs_gpio,
  447. spi->mode & SPI_CS_HIGH ? 1 : 0);
  448. break;
  449. case BITBANG_CS_INACTIVE:
  450. gpio_direction_output(spi->cs_gpio,
  451. spi->mode & SPI_CS_HIGH ? 0 : 1);
  452. break;
  453. }
  454. }
  455. }
  456. static int
  457. spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  458. {
  459. struct sirfsoc_spi *sspi;
  460. u8 bits_per_word = 0;
  461. int hz = 0;
  462. u32 regval;
  463. u32 txfifo_ctrl, rxfifo_ctrl;
  464. u32 fifo_size = SIRFSOC_SPI_FIFO_SIZE / 4;
  465. sspi = spi_master_get_devdata(spi->master);
  466. bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
  467. hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  468. regval = (sspi->ctrl_freq / (2 * hz)) - 1;
  469. if (regval > 0xFFFF || regval < 0) {
  470. dev_err(&spi->dev, "Speed %d not supported\n", hz);
  471. return -EINVAL;
  472. }
  473. switch (bits_per_word) {
  474. case 8:
  475. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
  476. sspi->rx_word = spi_sirfsoc_rx_word_u8;
  477. sspi->tx_word = spi_sirfsoc_tx_word_u8;
  478. break;
  479. case 12:
  480. case 16:
  481. regval |= (bits_per_word == 12) ?
  482. SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
  483. SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
  484. sspi->rx_word = spi_sirfsoc_rx_word_u16;
  485. sspi->tx_word = spi_sirfsoc_tx_word_u16;
  486. break;
  487. case 32:
  488. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
  489. sspi->rx_word = spi_sirfsoc_rx_word_u32;
  490. sspi->tx_word = spi_sirfsoc_tx_word_u32;
  491. break;
  492. default:
  493. BUG();
  494. }
  495. sspi->word_width = DIV_ROUND_UP(bits_per_word, 8);
  496. txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  497. (sspi->word_width >> 1);
  498. rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  499. (sspi->word_width >> 1);
  500. if (!(spi->mode & SPI_CS_HIGH))
  501. regval |= SIRFSOC_SPI_CS_IDLE_STAT;
  502. if (!(spi->mode & SPI_LSB_FIRST))
  503. regval |= SIRFSOC_SPI_TRAN_MSB;
  504. if (spi->mode & SPI_CPOL)
  505. regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
  506. /*
  507. * Data should be driven at least 1/2 cycle before the fetch edge
  508. * to make sure that data gets stable at the fetch edge.
  509. */
  510. if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
  511. (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA)))
  512. regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
  513. else
  514. regval |= SIRFSOC_SPI_DRV_POS_EDGE;
  515. writel(SIRFSOC_SPI_FIFO_SC(fifo_size - 2) |
  516. SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
  517. SIRFSOC_SPI_FIFO_HC(2),
  518. sspi->base + SIRFSOC_SPI_TXFIFO_LEVEL_CHK);
  519. writel(SIRFSOC_SPI_FIFO_SC(2) |
  520. SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
  521. SIRFSOC_SPI_FIFO_HC(fifo_size - 2),
  522. sspi->base + SIRFSOC_SPI_RXFIFO_LEVEL_CHK);
  523. writel(txfifo_ctrl, sspi->base + SIRFSOC_SPI_TXFIFO_CTRL);
  524. writel(rxfifo_ctrl, sspi->base + SIRFSOC_SPI_RXFIFO_CTRL);
  525. if (t && t->tx_buf && !t->rx_buf && (t->len <= SIRFSOC_MAX_CMD_BYTES)) {
  526. regval |= (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) |
  527. SIRFSOC_SPI_CMD_MODE);
  528. sspi->tx_by_cmd = true;
  529. } else {
  530. regval &= ~SIRFSOC_SPI_CMD_MODE;
  531. sspi->tx_by_cmd = false;
  532. }
  533. /*
  534. * it should never set to hardware cs mode because in hardware cs mode,
  535. * cs signal can't controlled by driver.
  536. */
  537. regval |= SIRFSOC_SPI_CS_IO_MODE;
  538. writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
  539. if (IS_DMA_VALID(t)) {
  540. /* Enable DMA mode for RX, TX */
  541. writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
  542. writel(SIRFSOC_SPI_RX_DMA_FLUSH,
  543. sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
  544. } else {
  545. /* Enable IO mode for RX, TX */
  546. writel(SIRFSOC_SPI_IO_MODE_SEL,
  547. sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
  548. writel(SIRFSOC_SPI_IO_MODE_SEL,
  549. sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
  550. }
  551. return 0;
  552. }
  553. static int spi_sirfsoc_setup(struct spi_device *spi)
  554. {
  555. struct sirfsoc_spi *sspi;
  556. sspi = spi_master_get_devdata(spi->master);
  557. if (spi->cs_gpio == -ENOENT)
  558. sspi->hw_cs = true;
  559. else
  560. sspi->hw_cs = false;
  561. return spi_sirfsoc_setup_transfer(spi, NULL);
  562. }
  563. static int spi_sirfsoc_probe(struct platform_device *pdev)
  564. {
  565. struct sirfsoc_spi *sspi;
  566. struct spi_master *master;
  567. struct resource *mem_res;
  568. int irq;
  569. int i, ret;
  570. ret = device_reset(&pdev->dev);
  571. if (ret) {
  572. dev_err(&pdev->dev, "SPI reset failed!\n");
  573. return ret;
  574. }
  575. master = spi_alloc_master(&pdev->dev, sizeof(*sspi));
  576. if (!master) {
  577. dev_err(&pdev->dev, "Unable to allocate SPI master\n");
  578. return -ENOMEM;
  579. }
  580. platform_set_drvdata(pdev, master);
  581. sspi = spi_master_get_devdata(master);
  582. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. sspi->base = devm_ioremap_resource(&pdev->dev, mem_res);
  584. if (IS_ERR(sspi->base)) {
  585. ret = PTR_ERR(sspi->base);
  586. goto free_master;
  587. }
  588. irq = platform_get_irq(pdev, 0);
  589. if (irq < 0) {
  590. ret = -ENXIO;
  591. goto free_master;
  592. }
  593. ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
  594. DRIVER_NAME, sspi);
  595. if (ret)
  596. goto free_master;
  597. sspi->bitbang.master = master;
  598. sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
  599. sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
  600. sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
  601. sspi->bitbang.master->setup = spi_sirfsoc_setup;
  602. master->bus_num = pdev->id;
  603. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH;
  604. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
  605. SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
  606. master->max_speed_hz = SIRFSOC_SPI_DEFAULT_FRQ;
  607. sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
  608. /* request DMA channels */
  609. sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx");
  610. if (!sspi->rx_chan) {
  611. dev_err(&pdev->dev, "can not allocate rx dma channel\n");
  612. ret = -ENODEV;
  613. goto free_master;
  614. }
  615. sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx");
  616. if (!sspi->tx_chan) {
  617. dev_err(&pdev->dev, "can not allocate tx dma channel\n");
  618. ret = -ENODEV;
  619. goto free_rx_dma;
  620. }
  621. sspi->clk = clk_get(&pdev->dev, NULL);
  622. if (IS_ERR(sspi->clk)) {
  623. ret = PTR_ERR(sspi->clk);
  624. goto free_tx_dma;
  625. }
  626. clk_prepare_enable(sspi->clk);
  627. sspi->ctrl_freq = clk_get_rate(sspi->clk);
  628. init_completion(&sspi->rx_done);
  629. init_completion(&sspi->tx_done);
  630. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  631. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  632. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  633. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  634. /* We are not using dummy delay between command and data */
  635. writel(0, sspi->base + SIRFSOC_SPI_DUMMY_DELAY_CTL);
  636. sspi->dummypage = kmalloc(2 * PAGE_SIZE, GFP_KERNEL);
  637. if (!sspi->dummypage) {
  638. ret = -ENOMEM;
  639. goto free_clk;
  640. }
  641. ret = spi_bitbang_start(&sspi->bitbang);
  642. if (ret)
  643. goto free_dummypage;
  644. for (i = 0; master->cs_gpios && i < master->num_chipselect; i++) {
  645. if (master->cs_gpios[i] == -ENOENT)
  646. continue;
  647. if (!gpio_is_valid(master->cs_gpios[i])) {
  648. dev_err(&pdev->dev, "no valid gpio\n");
  649. ret = -EINVAL;
  650. goto free_dummypage;
  651. }
  652. ret = devm_gpio_request(&pdev->dev,
  653. master->cs_gpios[i], DRIVER_NAME);
  654. if (ret) {
  655. dev_err(&pdev->dev, "failed to request gpio\n");
  656. goto free_dummypage;
  657. }
  658. }
  659. dev_info(&pdev->dev, "registerred, bus number = %d\n", master->bus_num);
  660. return 0;
  661. free_dummypage:
  662. kfree(sspi->dummypage);
  663. free_clk:
  664. clk_disable_unprepare(sspi->clk);
  665. clk_put(sspi->clk);
  666. free_tx_dma:
  667. dma_release_channel(sspi->tx_chan);
  668. free_rx_dma:
  669. dma_release_channel(sspi->rx_chan);
  670. free_master:
  671. spi_master_put(master);
  672. return ret;
  673. }
  674. static int spi_sirfsoc_remove(struct platform_device *pdev)
  675. {
  676. struct spi_master *master;
  677. struct sirfsoc_spi *sspi;
  678. master = platform_get_drvdata(pdev);
  679. sspi = spi_master_get_devdata(master);
  680. spi_bitbang_stop(&sspi->bitbang);
  681. kfree(sspi->dummypage);
  682. clk_disable_unprepare(sspi->clk);
  683. clk_put(sspi->clk);
  684. dma_release_channel(sspi->rx_chan);
  685. dma_release_channel(sspi->tx_chan);
  686. spi_master_put(master);
  687. return 0;
  688. }
  689. #ifdef CONFIG_PM_SLEEP
  690. static int spi_sirfsoc_suspend(struct device *dev)
  691. {
  692. struct spi_master *master = dev_get_drvdata(dev);
  693. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  694. int ret;
  695. ret = spi_master_suspend(master);
  696. if (ret)
  697. return ret;
  698. clk_disable(sspi->clk);
  699. return 0;
  700. }
  701. static int spi_sirfsoc_resume(struct device *dev)
  702. {
  703. struct spi_master *master = dev_get_drvdata(dev);
  704. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  705. clk_enable(sspi->clk);
  706. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  707. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  708. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  709. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  710. return spi_master_resume(master);
  711. }
  712. #endif
  713. static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend,
  714. spi_sirfsoc_resume);
  715. static const struct of_device_id spi_sirfsoc_of_match[] = {
  716. { .compatible = "sirf,prima2-spi", },
  717. { .compatible = "sirf,marco-spi", },
  718. {}
  719. };
  720. MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match);
  721. static struct platform_driver spi_sirfsoc_driver = {
  722. .driver = {
  723. .name = DRIVER_NAME,
  724. .owner = THIS_MODULE,
  725. .pm = &spi_sirfsoc_pm_ops,
  726. .of_match_table = spi_sirfsoc_of_match,
  727. },
  728. .probe = spi_sirfsoc_probe,
  729. .remove = spi_sirfsoc_remove,
  730. };
  731. module_platform_driver(spi_sirfsoc_driver);
  732. MODULE_DESCRIPTION("SiRF SoC SPI master driver");
  733. MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>");
  734. MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>");
  735. MODULE_LICENSE("GPL v2");