spi-sirf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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/pinctrl/pinmux.h>
  22. #define DRIVER_NAME "sirfsoc_spi"
  23. #define SIRFSOC_SPI_CTRL 0x0000
  24. #define SIRFSOC_SPI_CMD 0x0004
  25. #define SIRFSOC_SPI_TX_RX_EN 0x0008
  26. #define SIRFSOC_SPI_INT_EN 0x000C
  27. #define SIRFSOC_SPI_INT_STATUS 0x0010
  28. #define SIRFSOC_SPI_TX_DMA_IO_CTRL 0x0100
  29. #define SIRFSOC_SPI_TX_DMA_IO_LEN 0x0104
  30. #define SIRFSOC_SPI_TXFIFO_CTRL 0x0108
  31. #define SIRFSOC_SPI_TXFIFO_LEVEL_CHK 0x010C
  32. #define SIRFSOC_SPI_TXFIFO_OP 0x0110
  33. #define SIRFSOC_SPI_TXFIFO_STATUS 0x0114
  34. #define SIRFSOC_SPI_TXFIFO_DATA 0x0118
  35. #define SIRFSOC_SPI_RX_DMA_IO_CTRL 0x0120
  36. #define SIRFSOC_SPI_RX_DMA_IO_LEN 0x0124
  37. #define SIRFSOC_SPI_RXFIFO_CTRL 0x0128
  38. #define SIRFSOC_SPI_RXFIFO_LEVEL_CHK 0x012C
  39. #define SIRFSOC_SPI_RXFIFO_OP 0x0130
  40. #define SIRFSOC_SPI_RXFIFO_STATUS 0x0134
  41. #define SIRFSOC_SPI_RXFIFO_DATA 0x0138
  42. #define SIRFSOC_SPI_DUMMY_DELAY_CTL 0x0144
  43. /* SPI CTRL register defines */
  44. #define SIRFSOC_SPI_SLV_MODE BIT(16)
  45. #define SIRFSOC_SPI_CMD_MODE BIT(17)
  46. #define SIRFSOC_SPI_CS_IO_OUT BIT(18)
  47. #define SIRFSOC_SPI_CS_IO_MODE BIT(19)
  48. #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
  49. #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
  50. #define SIRFSOC_SPI_TRAN_MSB BIT(22)
  51. #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
  52. #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
  53. #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
  54. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
  55. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
  56. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
  57. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
  58. #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
  59. #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
  60. #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
  61. /* Interrupt Enable */
  62. #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
  63. #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
  64. #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
  65. #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
  66. #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
  67. #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
  68. #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
  69. #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
  70. #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
  71. #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
  72. #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
  73. #define SIRFSOC_SPI_INT_MASK_ALL 0x1FFF
  74. /* Interrupt status */
  75. #define SIRFSOC_SPI_RX_DONE BIT(0)
  76. #define SIRFSOC_SPI_TX_DONE BIT(1)
  77. #define SIRFSOC_SPI_RX_OFLOW BIT(2)
  78. #define SIRFSOC_SPI_TX_UFLOW BIT(3)
  79. #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
  80. #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
  81. #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
  82. #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
  83. #define SIRFSOC_SPI_FRM_END BIT(10)
  84. /* TX RX enable */
  85. #define SIRFSOC_SPI_RX_EN BIT(0)
  86. #define SIRFSOC_SPI_TX_EN BIT(1)
  87. #define SIRFSOC_SPI_CMD_TX_EN BIT(2)
  88. #define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
  89. #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
  90. /* FIFO OPs */
  91. #define SIRFSOC_SPI_FIFO_RESET BIT(0)
  92. #define SIRFSOC_SPI_FIFO_START BIT(1)
  93. /* FIFO CTRL */
  94. #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
  95. #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
  96. #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
  97. /* FIFO Status */
  98. #define SIRFSOC_SPI_FIFO_LEVEL_MASK 0xFF
  99. #define SIRFSOC_SPI_FIFO_FULL BIT(8)
  100. #define SIRFSOC_SPI_FIFO_EMPTY BIT(9)
  101. /* 256 bytes rx/tx FIFO */
  102. #define SIRFSOC_SPI_FIFO_SIZE 256
  103. #define SIRFSOC_SPI_DAT_FRM_LEN_MAX (64 * 1024)
  104. #define SIRFSOC_SPI_FIFO_SC(x) ((x) & 0x3F)
  105. #define SIRFSOC_SPI_FIFO_LC(x) (((x) & 0x3F) << 10)
  106. #define SIRFSOC_SPI_FIFO_HC(x) (((x) & 0x3F) << 20)
  107. #define SIRFSOC_SPI_FIFO_THD(x) (((x) & 0xFF) << 2)
  108. struct sirfsoc_spi {
  109. struct spi_bitbang bitbang;
  110. struct completion done;
  111. void __iomem *base;
  112. u32 ctrl_freq; /* SPI controller clock speed */
  113. struct clk *clk;
  114. struct pinmux *pmx;
  115. /* rx & tx bufs from the spi_transfer */
  116. const void *tx;
  117. void *rx;
  118. /* place received word into rx buffer */
  119. void (*rx_word) (struct sirfsoc_spi *);
  120. /* get word from tx buffer for sending */
  121. void (*tx_word) (struct sirfsoc_spi *);
  122. /* number of words left to be tranmitted/received */
  123. unsigned int left_tx_cnt;
  124. unsigned int left_rx_cnt;
  125. /* tasklet to push tx msg into FIFO */
  126. struct tasklet_struct tasklet_tx;
  127. int chipselect[0];
  128. };
  129. static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi)
  130. {
  131. u32 data;
  132. u8 *rx = sspi->rx;
  133. data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
  134. if (rx) {
  135. *rx++ = (u8) data;
  136. sspi->rx = rx;
  137. }
  138. sspi->left_rx_cnt--;
  139. }
  140. static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi)
  141. {
  142. u32 data = 0;
  143. const u8 *tx = sspi->tx;
  144. if (tx) {
  145. data = *tx++;
  146. sspi->tx = tx;
  147. }
  148. writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
  149. sspi->left_tx_cnt--;
  150. }
  151. static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi)
  152. {
  153. u32 data;
  154. u16 *rx = sspi->rx;
  155. data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
  156. if (rx) {
  157. *rx++ = (u16) data;
  158. sspi->rx = rx;
  159. }
  160. sspi->left_rx_cnt--;
  161. }
  162. static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi)
  163. {
  164. u32 data = 0;
  165. const u16 *tx = sspi->tx;
  166. if (tx) {
  167. data = *tx++;
  168. sspi->tx = tx;
  169. }
  170. writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
  171. sspi->left_tx_cnt--;
  172. }
  173. static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi)
  174. {
  175. u32 data;
  176. u32 *rx = sspi->rx;
  177. data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
  178. if (rx) {
  179. *rx++ = (u32) data;
  180. sspi->rx = rx;
  181. }
  182. sspi->left_rx_cnt--;
  183. }
  184. static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi)
  185. {
  186. u32 data = 0;
  187. const u32 *tx = sspi->tx;
  188. if (tx) {
  189. data = *tx++;
  190. sspi->tx = tx;
  191. }
  192. writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
  193. sspi->left_tx_cnt--;
  194. }
  195. static void spi_sirfsoc_tasklet_tx(unsigned long arg)
  196. {
  197. struct sirfsoc_spi *sspi = (struct sirfsoc_spi *)arg;
  198. /* Fill Tx FIFO while there are left words to be transmitted */
  199. while (!((readl(sspi->base + SIRFSOC_SPI_TXFIFO_STATUS) &
  200. SIRFSOC_SPI_FIFO_FULL)) &&
  201. sspi->left_tx_cnt)
  202. sspi->tx_word(sspi);
  203. }
  204. static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id)
  205. {
  206. struct sirfsoc_spi *sspi = dev_id;
  207. u32 spi_stat = readl(sspi->base + SIRFSOC_SPI_INT_STATUS);
  208. writel(spi_stat, sspi->base + SIRFSOC_SPI_INT_STATUS);
  209. /* Error Conditions */
  210. if (spi_stat & SIRFSOC_SPI_RX_OFLOW ||
  211. spi_stat & SIRFSOC_SPI_TX_UFLOW) {
  212. complete(&sspi->done);
  213. writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
  214. }
  215. if (spi_stat & SIRFSOC_SPI_FRM_END) {
  216. while (!((readl(sspi->base + SIRFSOC_SPI_RXFIFO_STATUS)
  217. & SIRFSOC_SPI_FIFO_EMPTY)) &&
  218. sspi->left_rx_cnt)
  219. sspi->rx_word(sspi);
  220. /* Received all words */
  221. if ((sspi->left_rx_cnt == 0) && (sspi->left_tx_cnt == 0)) {
  222. complete(&sspi->done);
  223. writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
  224. }
  225. }
  226. if (spi_stat & SIRFSOC_SPI_RXFIFO_THD_REACH ||
  227. spi_stat & SIRFSOC_SPI_TXFIFO_THD_REACH ||
  228. spi_stat & SIRFSOC_SPI_RX_FIFO_FULL ||
  229. spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY)
  230. tasklet_schedule(&sspi->tasklet_tx);
  231. return IRQ_HANDLED;
  232. }
  233. static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t)
  234. {
  235. struct sirfsoc_spi *sspi;
  236. int timeout = t->len * 10;
  237. sspi = spi_master_get_devdata(spi->master);
  238. sspi->tx = t->tx_buf;
  239. sspi->rx = t->rx_buf;
  240. sspi->left_tx_cnt = sspi->left_rx_cnt = t->len;
  241. INIT_COMPLETION(sspi->done);
  242. writel(SIRFSOC_SPI_INT_MASK_ALL, sspi->base + SIRFSOC_SPI_INT_STATUS);
  243. if (t->len == 1) {
  244. writel(readl(sspi->base + SIRFSOC_SPI_CTRL) |
  245. SIRFSOC_SPI_ENA_AUTO_CLR,
  246. sspi->base + SIRFSOC_SPI_CTRL);
  247. writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
  248. writel(0, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
  249. } else if ((t->len > 1) && (t->len < SIRFSOC_SPI_DAT_FRM_LEN_MAX)) {
  250. writel(readl(sspi->base + SIRFSOC_SPI_CTRL) |
  251. SIRFSOC_SPI_MUL_DAT_MODE |
  252. SIRFSOC_SPI_ENA_AUTO_CLR,
  253. sspi->base + SIRFSOC_SPI_CTRL);
  254. writel(t->len - 1, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
  255. writel(t->len - 1, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
  256. } else {
  257. writel(readl(sspi->base + SIRFSOC_SPI_CTRL),
  258. sspi->base + SIRFSOC_SPI_CTRL);
  259. writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
  260. writel(0, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
  261. }
  262. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  263. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  264. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  265. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  266. /* Send the first word to trigger the whole tx/rx process */
  267. sspi->tx_word(sspi);
  268. writel(SIRFSOC_SPI_RX_OFLOW_INT_EN | SIRFSOC_SPI_TX_UFLOW_INT_EN |
  269. SIRFSOC_SPI_RXFIFO_THD_INT_EN | SIRFSOC_SPI_TXFIFO_THD_INT_EN |
  270. SIRFSOC_SPI_FRM_END_INT_EN | SIRFSOC_SPI_RXFIFO_FULL_INT_EN |
  271. SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN, sspi->base + SIRFSOC_SPI_INT_EN);
  272. writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN, sspi->base + SIRFSOC_SPI_TX_RX_EN);
  273. if (wait_for_completion_timeout(&sspi->done, timeout) == 0)
  274. dev_err(&spi->dev, "transfer timeout\n");
  275. /* TX, RX FIFO stop */
  276. writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  277. writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  278. writel(0, sspi->base + SIRFSOC_SPI_TX_RX_EN);
  279. writel(0, sspi->base + SIRFSOC_SPI_INT_EN);
  280. return t->len - sspi->left_rx_cnt;
  281. }
  282. static void spi_sirfsoc_chipselect(struct spi_device *spi, int value)
  283. {
  284. struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master);
  285. if (sspi->chipselect[spi->chip_select] == 0) {
  286. u32 regval = readl(sspi->base + SIRFSOC_SPI_CTRL);
  287. regval |= SIRFSOC_SPI_CS_IO_OUT;
  288. switch (value) {
  289. case BITBANG_CS_ACTIVE:
  290. if (spi->mode & SPI_CS_HIGH)
  291. regval |= SIRFSOC_SPI_CS_IO_OUT;
  292. else
  293. regval &= ~SIRFSOC_SPI_CS_IO_OUT;
  294. break;
  295. case BITBANG_CS_INACTIVE:
  296. if (spi->mode & SPI_CS_HIGH)
  297. regval &= ~SIRFSOC_SPI_CS_IO_OUT;
  298. else
  299. regval |= SIRFSOC_SPI_CS_IO_OUT;
  300. break;
  301. }
  302. writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
  303. } else {
  304. int gpio = sspi->chipselect[spi->chip_select];
  305. gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
  306. }
  307. }
  308. static int
  309. spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  310. {
  311. struct sirfsoc_spi *sspi;
  312. u8 bits_per_word = 0;
  313. int hz = 0;
  314. u32 regval;
  315. u32 txfifo_ctrl, rxfifo_ctrl;
  316. u32 fifo_size = SIRFSOC_SPI_FIFO_SIZE / 4;
  317. sspi = spi_master_get_devdata(spi->master);
  318. bits_per_word = t && t->bits_per_word ? t->bits_per_word :
  319. spi->bits_per_word;
  320. hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  321. /* Enable IO mode for RX, TX */
  322. writel(SIRFSOC_SPI_IO_MODE_SEL, sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
  323. writel(SIRFSOC_SPI_IO_MODE_SEL, sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
  324. regval = (sspi->ctrl_freq / (2 * hz)) - 1;
  325. if (regval > 0xFFFF || regval < 0) {
  326. dev_err(&spi->dev, "Speed %d not supported\n", hz);
  327. return -EINVAL;
  328. }
  329. switch (bits_per_word) {
  330. case 8:
  331. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
  332. sspi->rx_word = spi_sirfsoc_rx_word_u8;
  333. sspi->tx_word = spi_sirfsoc_tx_word_u8;
  334. txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  335. SIRFSOC_SPI_FIFO_WIDTH_BYTE;
  336. rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  337. SIRFSOC_SPI_FIFO_WIDTH_BYTE;
  338. break;
  339. case 12:
  340. case 16:
  341. regval |= (bits_per_word == 12) ? SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
  342. SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
  343. sspi->rx_word = spi_sirfsoc_rx_word_u16;
  344. sspi->tx_word = spi_sirfsoc_tx_word_u16;
  345. txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  346. SIRFSOC_SPI_FIFO_WIDTH_WORD;
  347. rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  348. SIRFSOC_SPI_FIFO_WIDTH_WORD;
  349. break;
  350. case 32:
  351. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
  352. sspi->rx_word = spi_sirfsoc_rx_word_u32;
  353. sspi->tx_word = spi_sirfsoc_tx_word_u32;
  354. txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  355. SIRFSOC_SPI_FIFO_WIDTH_DWORD;
  356. rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
  357. SIRFSOC_SPI_FIFO_WIDTH_DWORD;
  358. break;
  359. default:
  360. dev_err(&spi->dev, "Bits per word %d not supported\n",
  361. bits_per_word);
  362. return -EINVAL;
  363. }
  364. if (!(spi->mode & SPI_CS_HIGH))
  365. regval |= SIRFSOC_SPI_CS_IDLE_STAT;
  366. if (!(spi->mode & SPI_LSB_FIRST))
  367. regval |= SIRFSOC_SPI_TRAN_MSB;
  368. if (spi->mode & SPI_CPOL)
  369. regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
  370. /*
  371. * Data should be driven at least 1/2 cycle before the fetch edge to make
  372. * sure that data gets stable at the fetch edge.
  373. */
  374. if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
  375. (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA)))
  376. regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
  377. else
  378. regval |= SIRFSOC_SPI_DRV_POS_EDGE;
  379. writel(SIRFSOC_SPI_FIFO_SC(fifo_size - 2) |
  380. SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
  381. SIRFSOC_SPI_FIFO_HC(2),
  382. sspi->base + SIRFSOC_SPI_TXFIFO_LEVEL_CHK);
  383. writel(SIRFSOC_SPI_FIFO_SC(2) |
  384. SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
  385. SIRFSOC_SPI_FIFO_HC(fifo_size - 2),
  386. sspi->base + SIRFSOC_SPI_RXFIFO_LEVEL_CHK);
  387. writel(txfifo_ctrl, sspi->base + SIRFSOC_SPI_TXFIFO_CTRL);
  388. writel(rxfifo_ctrl, sspi->base + SIRFSOC_SPI_RXFIFO_CTRL);
  389. writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
  390. return 0;
  391. }
  392. static int spi_sirfsoc_setup(struct spi_device *spi)
  393. {
  394. struct sirfsoc_spi *sspi;
  395. if (!spi->max_speed_hz)
  396. return -EINVAL;
  397. sspi = spi_master_get_devdata(spi->master);
  398. if (!spi->bits_per_word)
  399. spi->bits_per_word = 8;
  400. return spi_sirfsoc_setup_transfer(spi, NULL);
  401. }
  402. static int __devinit spi_sirfsoc_probe(struct platform_device *pdev)
  403. {
  404. struct sirfsoc_spi *sspi;
  405. struct spi_master *master;
  406. struct resource *mem_res;
  407. int num_cs, cs_gpio, irq;
  408. int i;
  409. int ret;
  410. ret = of_property_read_u32(pdev->dev.of_node,
  411. "sirf,spi-num-chipselects", &num_cs);
  412. if (ret < 0) {
  413. dev_err(&pdev->dev, "Unable to get chip select number\n");
  414. goto err_cs;
  415. }
  416. master = spi_alloc_master(&pdev->dev, sizeof(*sspi) + sizeof(int) * num_cs);
  417. if (!master) {
  418. dev_err(&pdev->dev, "Unable to allocate SPI master\n");
  419. return -ENOMEM;
  420. }
  421. platform_set_drvdata(pdev, master);
  422. sspi = spi_master_get_devdata(master);
  423. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  424. if (!mem_res) {
  425. dev_err(&pdev->dev, "Unable to get IO resource\n");
  426. ret = -ENODEV;
  427. goto free_master;
  428. }
  429. master->num_chipselect = num_cs;
  430. for (i = 0; i < master->num_chipselect; i++) {
  431. cs_gpio = of_get_named_gpio(pdev->dev.of_node, "cs-gpios", i);
  432. if (cs_gpio < 0) {
  433. dev_err(&pdev->dev, "can't get cs gpio from DT\n");
  434. ret = -ENODEV;
  435. goto free_master;
  436. }
  437. sspi->chipselect[i] = cs_gpio;
  438. if (cs_gpio == 0)
  439. continue; /* use cs from spi controller */
  440. ret = gpio_request(cs_gpio, DRIVER_NAME);
  441. if (ret) {
  442. while (i > 0) {
  443. i--;
  444. if (sspi->chipselect[i] > 0)
  445. gpio_free(sspi->chipselect[i]);
  446. }
  447. dev_err(&pdev->dev, "fail to request cs gpios\n");
  448. goto free_master;
  449. }
  450. }
  451. sspi->base = devm_request_and_ioremap(&pdev->dev, mem_res);
  452. if (!sspi->base) {
  453. dev_err(&pdev->dev, "IO remap failed!\n");
  454. ret = -ENOMEM;
  455. goto free_master;
  456. }
  457. irq = platform_get_irq(pdev, 0);
  458. if (irq < 0) {
  459. ret = -ENXIO;
  460. goto free_master;
  461. }
  462. ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
  463. DRIVER_NAME, sspi);
  464. if (ret)
  465. goto free_master;
  466. sspi->bitbang.master = spi_master_get(master);
  467. sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
  468. sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
  469. sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
  470. sspi->bitbang.master->setup = spi_sirfsoc_setup;
  471. master->bus_num = pdev->id;
  472. sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
  473. sspi->pmx = pinmux_get(&pdev->dev, NULL);
  474. ret = IS_ERR(sspi->pmx);
  475. if (ret)
  476. goto free_master;
  477. pinmux_enable(sspi->pmx);
  478. sspi->clk = clk_get(&pdev->dev, NULL);
  479. if (IS_ERR(sspi->clk)) {
  480. ret = -EINVAL;
  481. goto free_pmx;
  482. }
  483. clk_enable(sspi->clk);
  484. sspi->ctrl_freq = clk_get_rate(sspi->clk);
  485. init_completion(&sspi->done);
  486. tasklet_init(&sspi->tasklet_tx, spi_sirfsoc_tasklet_tx,
  487. (unsigned long)sspi);
  488. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  489. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  490. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  491. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  492. /* We are not using dummy delay between command and data */
  493. writel(0, sspi->base + SIRFSOC_SPI_DUMMY_DELAY_CTL);
  494. ret = spi_bitbang_start(&sspi->bitbang);
  495. if (ret)
  496. goto free_clk;
  497. dev_info(&pdev->dev, "registerred, bus number = %d\n", master->bus_num);
  498. return 0;
  499. free_clk:
  500. clk_disable(sspi->clk);
  501. clk_put(sspi->clk);
  502. free_pmx:
  503. pinmux_disable(sspi->pmx);
  504. pinmux_put(sspi->pmx);
  505. free_master:
  506. spi_master_put(master);
  507. err_cs:
  508. return ret;
  509. }
  510. static int __devexit spi_sirfsoc_remove(struct platform_device *pdev)
  511. {
  512. struct spi_master *master;
  513. struct sirfsoc_spi *sspi;
  514. int i;
  515. master = platform_get_drvdata(pdev);
  516. sspi = spi_master_get_devdata(master);
  517. spi_bitbang_stop(&sspi->bitbang);
  518. for (i = 0; i < master->num_chipselect; i++) {
  519. if (sspi->chipselect[i] > 0)
  520. gpio_free(sspi->chipselect[i]);
  521. }
  522. clk_disable(sspi->clk);
  523. clk_put(sspi->clk);
  524. pinmux_disable(sspi->pmx);
  525. pinmux_put(sspi->pmx);
  526. spi_master_put(master);
  527. return 0;
  528. }
  529. #ifdef CONFIG_PM
  530. static int spi_sirfsoc_suspend(struct device *dev)
  531. {
  532. struct platform_device *pdev = to_platform_device(dev);
  533. struct spi_master *master = platform_get_drvdata(pdev);
  534. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  535. clk_disable(sspi->clk);
  536. return 0;
  537. }
  538. static int spi_sirfsoc_resume(struct device *dev)
  539. {
  540. struct platform_device *pdev = to_platform_device(dev);
  541. struct spi_master *master = platform_get_drvdata(pdev);
  542. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  543. clk_enable(sspi->clk);
  544. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  545. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  546. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
  547. writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
  548. return 0;
  549. }
  550. static const struct dev_pm_ops spi_sirfsoc_pm_ops = {
  551. .suspend = spi_sirfsoc_suspend,
  552. .resume = spi_sirfsoc_resume,
  553. };
  554. #endif
  555. static const struct of_device_id spi_sirfsoc_of_match[] = {
  556. { .compatible = "sirf,prima2-spi", },
  557. {}
  558. };
  559. MODULE_DEVICE_TABLE(of, sirfsoc_spi_of_match);
  560. static struct platform_driver spi_sirfsoc_driver = {
  561. .driver = {
  562. .name = DRIVER_NAME,
  563. .owner = THIS_MODULE,
  564. #ifdef CONFIG_PM
  565. .pm = &spi_sirfsoc_pm_ops,
  566. #endif
  567. .of_match_table = spi_sirfsoc_of_match,
  568. },
  569. .probe = spi_sirfsoc_probe,
  570. .remove = __devexit_p(spi_sirfsoc_remove),
  571. };
  572. module_platform_driver(spi_sirfsoc_driver);
  573. MODULE_DESCRIPTION("SiRF SoC SPI master driver");
  574. MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>, "
  575. "Barry Song <Baohua.Song@csr.com>");
  576. MODULE_LICENSE("GPL v2");