spi-sirf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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_FIFO_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. gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
  395. }
  396. }
  397. static int
  398. spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  399. {
  400. struct sirfsoc_spi *sspi;
  401. u8 bits_per_word = 0;
  402. int hz = 0;
  403. u32 regval;
  404. u32 txfifo_ctrl, rxfifo_ctrl;
  405. u32 fifo_size = SIRFSOC_SPI_FIFO_SIZE / 4;
  406. sspi = spi_master_get_devdata(spi->master);
  407. bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
  408. hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  409. regval = (sspi->ctrl_freq / (2 * hz)) - 1;
  410. if (regval > 0xFFFF || regval < 0) {
  411. dev_err(&spi->dev, "Speed %d not supported\n", hz);
  412. return -EINVAL;
  413. }
  414. switch (bits_per_word) {
  415. case 8:
  416. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
  417. sspi->rx_word = spi_sirfsoc_rx_word_u8;
  418. sspi->tx_word = spi_sirfsoc_tx_word_u8;
  419. break;
  420. case 12:
  421. case 16:
  422. regval |= (bits_per_word == 12) ? SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
  423. SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
  424. sspi->rx_word = spi_sirfsoc_rx_word_u16;
  425. sspi->tx_word = spi_sirfsoc_tx_word_u16;
  426. break;
  427. case 32:
  428. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
  429. sspi->rx_word = spi_sirfsoc_rx_word_u32;
  430. sspi->tx_word = spi_sirfsoc_tx_word_u32;
  431. break;
  432. default:
  433. BUG();
  434. }
  435. sspi->word_width = DIV_ROUND_UP(bits_per_word, 8);
  436. txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  437. sspi->word_width;
  438. rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  439. sspi->word_width;
  440. if (!(spi->mode & SPI_CS_HIGH))
  441. regval |= SIRFSOC_SPI_CS_IDLE_STAT;
  442. if (!(spi->mode & SPI_LSB_FIRST))
  443. regval |= SIRFSOC_SPI_TRAN_MSB;
  444. if (spi->mode & SPI_CPOL)
  445. regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
  446. /*
  447. * Data should be driven at least 1/2 cycle before the fetch edge to make
  448. * sure that data gets stable at the fetch edge.
  449. */
  450. if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
  451. (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA)))
  452. regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
  453. else
  454. regval |= SIRFSOC_SPI_DRV_POS_EDGE;
  455. writel(SIRFSOC_SPI_FIFO_SC(fifo_size - 2) |
  456. SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
  457. SIRFSOC_SPI_FIFO_HC(2),
  458. sspi->base + SIRFSOC_SPI_TXFIFO_LEVEL_CHK);
  459. writel(SIRFSOC_SPI_FIFO_SC(2) |
  460. SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
  461. SIRFSOC_SPI_FIFO_HC(fifo_size - 2),
  462. sspi->base + SIRFSOC_SPI_RXFIFO_LEVEL_CHK);
  463. writel(txfifo_ctrl, sspi->base + SIRFSOC_SPI_TXFIFO_CTRL);
  464. writel(rxfifo_ctrl, sspi->base + SIRFSOC_SPI_RXFIFO_CTRL);
  465. if (t && t->tx_buf && !t->rx_buf && (t->len <= SIRFSOC_MAX_CMD_BYTES)) {
  466. regval |= (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) |
  467. SIRFSOC_SPI_CMD_MODE);
  468. sspi->tx_by_cmd = true;
  469. } else {
  470. regval &= ~SIRFSOC_SPI_CMD_MODE;
  471. sspi->tx_by_cmd = false;
  472. }
  473. writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
  474. if (IS_DMA_VALID(t)) {
  475. /* Enable DMA mode for RX, TX */
  476. writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
  477. writel(SIRFSOC_SPI_RX_DMA_FLUSH, sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
  478. } else {
  479. /* Enable IO mode for RX, TX */
  480. writel(SIRFSOC_SPI_IO_MODE_SEL, sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
  481. writel(SIRFSOC_SPI_IO_MODE_SEL, sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
  482. }
  483. return 0;
  484. }
  485. static int spi_sirfsoc_setup(struct spi_device *spi)
  486. {
  487. if (!spi->max_speed_hz)
  488. return -EINVAL;
  489. return spi_sirfsoc_setup_transfer(spi, NULL);
  490. }
  491. static int spi_sirfsoc_probe(struct platform_device *pdev)
  492. {
  493. struct sirfsoc_spi *sspi;
  494. struct spi_master *master;
  495. struct resource *mem_res;
  496. int num_cs, cs_gpio, irq;
  497. int i;
  498. int ret;
  499. ret = of_property_read_u32(pdev->dev.of_node,
  500. "sirf,spi-num-chipselects", &num_cs);
  501. if (ret < 0) {
  502. dev_err(&pdev->dev, "Unable to get chip select number\n");
  503. goto err_cs;
  504. }
  505. master = spi_alloc_master(&pdev->dev, sizeof(*sspi) + sizeof(int) * num_cs);
  506. if (!master) {
  507. dev_err(&pdev->dev, "Unable to allocate SPI master\n");
  508. return -ENOMEM;
  509. }
  510. platform_set_drvdata(pdev, master);
  511. sspi = spi_master_get_devdata(master);
  512. master->num_chipselect = num_cs;
  513. for (i = 0; i < master->num_chipselect; i++) {
  514. cs_gpio = of_get_named_gpio(pdev->dev.of_node, "cs-gpios", i);
  515. if (cs_gpio < 0) {
  516. dev_err(&pdev->dev, "can't get cs gpio from DT\n");
  517. ret = -ENODEV;
  518. goto free_master;
  519. }
  520. sspi->chipselect[i] = cs_gpio;
  521. if (cs_gpio == 0)
  522. continue; /* use cs from spi controller */
  523. ret = gpio_request(cs_gpio, DRIVER_NAME);
  524. if (ret) {
  525. while (i > 0) {
  526. i--;
  527. if (sspi->chipselect[i] > 0)
  528. gpio_free(sspi->chipselect[i]);
  529. }
  530. dev_err(&pdev->dev, "fail to request cs gpios\n");
  531. goto free_master;
  532. }
  533. }
  534. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  535. sspi->base = devm_ioremap_resource(&pdev->dev, mem_res);
  536. if (IS_ERR(sspi->base)) {
  537. ret = PTR_ERR(sspi->base);
  538. goto free_master;
  539. }
  540. irq = platform_get_irq(pdev, 0);
  541. if (irq < 0) {
  542. ret = -ENXIO;
  543. goto free_master;
  544. }
  545. ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
  546. DRIVER_NAME, sspi);
  547. if (ret)
  548. goto free_master;
  549. sspi->bitbang.master = master;
  550. sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
  551. sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
  552. sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
  553. sspi->bitbang.master->setup = spi_sirfsoc_setup;
  554. master->bus_num = pdev->id;
  555. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH;
  556. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
  557. SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
  558. sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
  559. /* request DMA channels */
  560. sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx");
  561. if (!sspi->rx_chan) {
  562. dev_err(&pdev->dev, "can not allocate rx dma channel\n");
  563. ret = -ENODEV;
  564. goto free_master;
  565. }
  566. sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx");
  567. if (!sspi->tx_chan) {
  568. dev_err(&pdev->dev, "can not allocate tx dma channel\n");
  569. ret = -ENODEV;
  570. goto free_rx_dma;
  571. }
  572. sspi->clk = clk_get(&pdev->dev, NULL);
  573. if (IS_ERR(sspi->clk)) {
  574. ret = PTR_ERR(sspi->clk);
  575. goto free_tx_dma;
  576. }
  577. clk_prepare_enable(sspi->clk);
  578. sspi->ctrl_freq = clk_get_rate(sspi->clk);
  579. init_completion(&sspi->rx_done);
  580. init_completion(&sspi->tx_done);
  581. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  582. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  583. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  584. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  585. /* We are not using dummy delay between command and data */
  586. writel(0, sspi->base + SIRFSOC_SPI_DUMMY_DELAY_CTL);
  587. sspi->dummypage = kmalloc(2 * PAGE_SIZE, GFP_KERNEL);
  588. if (!sspi->dummypage) {
  589. ret = -ENOMEM;
  590. goto free_clk;
  591. }
  592. ret = spi_bitbang_start(&sspi->bitbang);
  593. if (ret)
  594. goto free_dummypage;
  595. dev_info(&pdev->dev, "registerred, bus number = %d\n", master->bus_num);
  596. return 0;
  597. free_dummypage:
  598. kfree(sspi->dummypage);
  599. free_clk:
  600. clk_disable_unprepare(sspi->clk);
  601. clk_put(sspi->clk);
  602. free_tx_dma:
  603. dma_release_channel(sspi->tx_chan);
  604. free_rx_dma:
  605. dma_release_channel(sspi->rx_chan);
  606. free_master:
  607. spi_master_put(master);
  608. err_cs:
  609. return ret;
  610. }
  611. static int spi_sirfsoc_remove(struct platform_device *pdev)
  612. {
  613. struct spi_master *master;
  614. struct sirfsoc_spi *sspi;
  615. int i;
  616. master = platform_get_drvdata(pdev);
  617. sspi = spi_master_get_devdata(master);
  618. spi_bitbang_stop(&sspi->bitbang);
  619. for (i = 0; i < master->num_chipselect; i++) {
  620. if (sspi->chipselect[i] > 0)
  621. gpio_free(sspi->chipselect[i]);
  622. }
  623. kfree(sspi->dummypage);
  624. clk_disable_unprepare(sspi->clk);
  625. clk_put(sspi->clk);
  626. dma_release_channel(sspi->rx_chan);
  627. dma_release_channel(sspi->tx_chan);
  628. spi_master_put(master);
  629. return 0;
  630. }
  631. #ifdef CONFIG_PM_SLEEP
  632. static int spi_sirfsoc_suspend(struct device *dev)
  633. {
  634. struct spi_master *master = dev_get_drvdata(dev);
  635. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  636. int ret;
  637. ret = spi_master_suspend(master);
  638. if (ret)
  639. return ret;
  640. clk_disable(sspi->clk);
  641. return 0;
  642. }
  643. static int spi_sirfsoc_resume(struct device *dev)
  644. {
  645. struct spi_master *master = dev_get_drvdata(dev);
  646. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  647. clk_enable(sspi->clk);
  648. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  649. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  650. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  651. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  652. return spi_master_resume(master);
  653. }
  654. #endif
  655. static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend,
  656. spi_sirfsoc_resume);
  657. static const struct of_device_id spi_sirfsoc_of_match[] = {
  658. { .compatible = "sirf,prima2-spi", },
  659. { .compatible = "sirf,marco-spi", },
  660. {}
  661. };
  662. MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match);
  663. static struct platform_driver spi_sirfsoc_driver = {
  664. .driver = {
  665. .name = DRIVER_NAME,
  666. .owner = THIS_MODULE,
  667. .pm = &spi_sirfsoc_pm_ops,
  668. .of_match_table = spi_sirfsoc_of_match,
  669. },
  670. .probe = spi_sirfsoc_probe,
  671. .remove = spi_sirfsoc_remove,
  672. };
  673. module_platform_driver(spi_sirfsoc_driver);
  674. MODULE_DESCRIPTION("SiRF SoC SPI master driver");
  675. MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>, "
  676. "Barry Song <Baohua.Song@csr.com>");
  677. MODULE_LICENSE("GPL v2");