spi-sirf.c 22 KB

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