spi-sirf.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. /* SPI CTRL register defines */
  28. #define SIRFSOC_SPI_SLV_MODE BIT(16)
  29. #define SIRFSOC_SPI_CMD_MODE BIT(17)
  30. #define SIRFSOC_SPI_CS_IO_OUT BIT(18)
  31. #define SIRFSOC_SPI_CS_IO_MODE BIT(19)
  32. #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
  33. #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
  34. #define SIRFSOC_SPI_TRAN_MSB BIT(22)
  35. #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
  36. #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
  37. #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
  38. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
  39. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
  40. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
  41. #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
  42. #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
  43. #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
  44. #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
  45. /* Interrupt Enable */
  46. #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
  47. #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
  48. #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
  49. #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
  50. #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
  51. #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
  52. #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
  53. #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
  54. #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
  55. #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
  56. #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
  57. /* Interrupt status */
  58. #define SIRFSOC_SPI_RX_DONE BIT(0)
  59. #define SIRFSOC_SPI_TX_DONE BIT(1)
  60. #define SIRFSOC_SPI_RX_OFLOW BIT(2)
  61. #define SIRFSOC_SPI_TX_UFLOW BIT(3)
  62. #define SIRFSOC_SPI_RX_IO_DMA BIT(4)
  63. #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
  64. #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
  65. #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
  66. #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
  67. #define SIRFSOC_SPI_FRM_END BIT(10)
  68. /* TX RX enable */
  69. #define SIRFSOC_SPI_RX_EN BIT(0)
  70. #define SIRFSOC_SPI_TX_EN BIT(1)
  71. #define SIRFSOC_SPI_CMD_TX_EN BIT(2)
  72. #define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
  73. #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
  74. /* FIFO OPs */
  75. #define SIRFSOC_SPI_FIFO_RESET BIT(0)
  76. #define SIRFSOC_SPI_FIFO_START BIT(1)
  77. /* FIFO CTRL */
  78. #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
  79. #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
  80. #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
  81. /* USP related */
  82. #define SIRFSOC_USP_SYNC_MODE BIT(0)
  83. #define SIRFSOC_USP_SLV_MODE BIT(1)
  84. #define SIRFSOC_USP_LSB BIT(4)
  85. #define SIRFSOC_USP_EN BIT(5)
  86. #define SIRFSOC_USP_RXD_FALLING_EDGE BIT(6)
  87. #define SIRFSOC_USP_TXD_FALLING_EDGE BIT(7)
  88. #define SIRFSOC_USP_CS_HIGH_VALID BIT(9)
  89. #define SIRFSOC_USP_SCLK_IDLE_STAT BIT(11)
  90. #define SIRFSOC_USP_TFS_IO_MODE BIT(14)
  91. #define SIRFSOC_USP_TFS_IO_INPUT BIT(19)
  92. #define SIRFSOC_USP_RXD_DELAY_LEN_MASK 0xFF
  93. #define SIRFSOC_USP_TXD_DELAY_LEN_MASK 0xFF
  94. #define SIRFSOC_USP_RXD_DELAY_OFFSET 0
  95. #define SIRFSOC_USP_TXD_DELAY_OFFSET 8
  96. #define SIRFSOC_USP_RXD_DELAY_LEN 1
  97. #define SIRFSOC_USP_TXD_DELAY_LEN 1
  98. #define SIRFSOC_USP_CLK_DIVISOR_OFFSET 21
  99. #define SIRFSOC_USP_CLK_DIVISOR_MASK 0x3FF
  100. #define SIRFSOC_USP_CLK_10_11_MASK 0x3
  101. #define SIRFSOC_USP_CLK_10_11_OFFSET 30
  102. #define SIRFSOC_USP_CLK_12_15_MASK 0xF
  103. #define SIRFSOC_USP_CLK_12_15_OFFSET 24
  104. #define SIRFSOC_USP_TX_DATA_OFFSET 0
  105. #define SIRFSOC_USP_TX_SYNC_OFFSET 8
  106. #define SIRFSOC_USP_TX_FRAME_OFFSET 16
  107. #define SIRFSOC_USP_TX_SHIFTER_OFFSET 24
  108. #define SIRFSOC_USP_TX_DATA_MASK 0xFF
  109. #define SIRFSOC_USP_TX_SYNC_MASK 0xFF
  110. #define SIRFSOC_USP_TX_FRAME_MASK 0xFF
  111. #define SIRFSOC_USP_TX_SHIFTER_MASK 0x1F
  112. #define SIRFSOC_USP_RX_DATA_OFFSET 0
  113. #define SIRFSOC_USP_RX_FRAME_OFFSET 8
  114. #define SIRFSOC_USP_RX_SHIFTER_OFFSET 16
  115. #define SIRFSOC_USP_RX_DATA_MASK 0xFF
  116. #define SIRFSOC_USP_RX_FRAME_MASK 0xFF
  117. #define SIRFSOC_USP_RX_SHIFTER_MASK 0x1F
  118. #define SIRFSOC_USP_CS_HIGH_VALUE BIT(1)
  119. #define SIRFSOC_SPI_FIFO_SC_OFFSET 0
  120. #define SIRFSOC_SPI_FIFO_LC_OFFSET 10
  121. #define SIRFSOC_SPI_FIFO_HC_OFFSET 20
  122. #define SIRFSOC_SPI_FIFO_FULL_MASK(s) (1 << ((s)->fifo_full_offset))
  123. #define SIRFSOC_SPI_FIFO_EMPTY_MASK(s) (1 << ((s)->fifo_full_offset + 1))
  124. #define SIRFSOC_SPI_FIFO_THD_MASK(s) ((s)->fifo_size - 1)
  125. #define SIRFSOC_SPI_FIFO_THD_OFFSET 2
  126. #define SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(s, val) \
  127. ((val) & (s)->fifo_level_chk_mask)
  128. enum sirf_spi_type {
  129. SIRF_REAL_SPI,
  130. SIRF_USP_SPI_P2,
  131. SIRF_USP_SPI_A7,
  132. };
  133. /*
  134. * only if the rx/tx buffer and transfer size are 4-bytes aligned, we use dma
  135. * due to the limitation of dma controller
  136. */
  137. #define ALIGNED(x) (!((u32)x & 0x3))
  138. #define IS_DMA_VALID(x) (x && ALIGNED(x->tx_buf) && ALIGNED(x->rx_buf) && \
  139. ALIGNED(x->len) && (x->len < 2 * PAGE_SIZE))
  140. #define SIRFSOC_MAX_CMD_BYTES 4
  141. #define SIRFSOC_SPI_DEFAULT_FRQ 1000000
  142. struct sirf_spi_register {
  143. /*SPI and USP-SPI common*/
  144. u32 tx_rx_en;
  145. u32 int_en;
  146. u32 int_st;
  147. u32 tx_dma_io_ctrl;
  148. u32 tx_dma_io_len;
  149. u32 txfifo_ctrl;
  150. u32 txfifo_level_chk;
  151. u32 txfifo_op;
  152. u32 txfifo_st;
  153. u32 txfifo_data;
  154. u32 rx_dma_io_ctrl;
  155. u32 rx_dma_io_len;
  156. u32 rxfifo_ctrl;
  157. u32 rxfifo_level_chk;
  158. u32 rxfifo_op;
  159. u32 rxfifo_st;
  160. u32 rxfifo_data;
  161. /*SPI self*/
  162. u32 spi_ctrl;
  163. u32 spi_cmd;
  164. u32 spi_dummy_delay_ctrl;
  165. /*USP-SPI self*/
  166. u32 usp_mode1;
  167. u32 usp_mode2;
  168. u32 usp_tx_frame_ctrl;
  169. u32 usp_rx_frame_ctrl;
  170. u32 usp_pin_io_data;
  171. u32 usp_risc_dsp_mode;
  172. u32 usp_async_param_reg;
  173. u32 usp_irda_x_mode_div;
  174. u32 usp_sm_cfg;
  175. u32 usp_int_en_clr;
  176. };
  177. static const struct sirf_spi_register real_spi_register = {
  178. .tx_rx_en = 0x8,
  179. .int_en = 0xc,
  180. .int_st = 0x10,
  181. .tx_dma_io_ctrl = 0x100,
  182. .tx_dma_io_len = 0x104,
  183. .txfifo_ctrl = 0x108,
  184. .txfifo_level_chk = 0x10c,
  185. .txfifo_op = 0x110,
  186. .txfifo_st = 0x114,
  187. .txfifo_data = 0x118,
  188. .rx_dma_io_ctrl = 0x120,
  189. .rx_dma_io_len = 0x124,
  190. .rxfifo_ctrl = 0x128,
  191. .rxfifo_level_chk = 0x12c,
  192. .rxfifo_op = 0x130,
  193. .rxfifo_st = 0x134,
  194. .rxfifo_data = 0x138,
  195. .spi_ctrl = 0x0,
  196. .spi_cmd = 0x4,
  197. .spi_dummy_delay_ctrl = 0x144,
  198. };
  199. static const struct sirf_spi_register usp_spi_register = {
  200. .tx_rx_en = 0x10,
  201. .int_en = 0x14,
  202. .int_st = 0x18,
  203. .tx_dma_io_ctrl = 0x100,
  204. .tx_dma_io_len = 0x104,
  205. .txfifo_ctrl = 0x108,
  206. .txfifo_level_chk = 0x10c,
  207. .txfifo_op = 0x110,
  208. .txfifo_st = 0x114,
  209. .txfifo_data = 0x118,
  210. .rx_dma_io_ctrl = 0x120,
  211. .rx_dma_io_len = 0x124,
  212. .rxfifo_ctrl = 0x128,
  213. .rxfifo_level_chk = 0x12c,
  214. .rxfifo_op = 0x130,
  215. .rxfifo_st = 0x134,
  216. .rxfifo_data = 0x138,
  217. .usp_mode1 = 0x0,
  218. .usp_mode2 = 0x4,
  219. .usp_tx_frame_ctrl = 0x8,
  220. .usp_rx_frame_ctrl = 0xc,
  221. .usp_pin_io_data = 0x1c,
  222. .usp_risc_dsp_mode = 0x20,
  223. .usp_async_param_reg = 0x24,
  224. .usp_irda_x_mode_div = 0x28,
  225. .usp_sm_cfg = 0x2c,
  226. .usp_int_en_clr = 0x140,
  227. };
  228. struct sirfsoc_spi {
  229. struct spi_bitbang bitbang;
  230. struct completion rx_done;
  231. struct completion tx_done;
  232. void __iomem *base;
  233. u32 ctrl_freq; /* SPI controller clock speed */
  234. struct clk *clk;
  235. /* rx & tx bufs from the spi_transfer */
  236. const void *tx;
  237. void *rx;
  238. /* place received word into rx buffer */
  239. void (*rx_word) (struct sirfsoc_spi *);
  240. /* get word from tx buffer for sending */
  241. void (*tx_word) (struct sirfsoc_spi *);
  242. /* number of words left to be tranmitted/received */
  243. unsigned int left_tx_word;
  244. unsigned int left_rx_word;
  245. /* rx & tx DMA channels */
  246. struct dma_chan *rx_chan;
  247. struct dma_chan *tx_chan;
  248. dma_addr_t src_start;
  249. dma_addr_t dst_start;
  250. int word_width; /* in bytes */
  251. /*
  252. * if tx size is not more than 4 and rx size is NULL, use
  253. * command model
  254. */
  255. bool tx_by_cmd;
  256. bool hw_cs;
  257. enum sirf_spi_type type;
  258. const struct sirf_spi_register *regs;
  259. unsigned int fifo_size;
  260. /* fifo empty offset is (fifo full offset + 1)*/
  261. unsigned int fifo_full_offset;
  262. /* fifo_level_chk_mask is (fifo_size/4 - 1) */
  263. unsigned int fifo_level_chk_mask;
  264. unsigned int dat_max_frm_len;
  265. };
  266. struct sirf_spi_comp_data {
  267. const struct sirf_spi_register *regs;
  268. enum sirf_spi_type type;
  269. unsigned int dat_max_frm_len;
  270. unsigned int fifo_size;
  271. void (*hwinit)(struct sirfsoc_spi *sspi);
  272. };
  273. static void sirfsoc_usp_hwinit(struct sirfsoc_spi *sspi)
  274. {
  275. /* reset USP and let USP can operate */
  276. writel(readl(sspi->base + sspi->regs->usp_mode1) &
  277. ~SIRFSOC_USP_EN, sspi->base + sspi->regs->usp_mode1);
  278. writel(readl(sspi->base + sspi->regs->usp_mode1) |
  279. SIRFSOC_USP_EN, sspi->base + sspi->regs->usp_mode1);
  280. }
  281. static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi)
  282. {
  283. u32 data;
  284. u8 *rx = sspi->rx;
  285. data = readl(sspi->base + sspi->regs->rxfifo_data);
  286. if (rx) {
  287. *rx++ = (u8) data;
  288. sspi->rx = rx;
  289. }
  290. sspi->left_rx_word--;
  291. }
  292. static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi)
  293. {
  294. u32 data = 0;
  295. const u8 *tx = sspi->tx;
  296. if (tx) {
  297. data = *tx++;
  298. sspi->tx = tx;
  299. }
  300. writel(data, sspi->base + sspi->regs->txfifo_data);
  301. sspi->left_tx_word--;
  302. }
  303. static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi)
  304. {
  305. u32 data;
  306. u16 *rx = sspi->rx;
  307. data = readl(sspi->base + sspi->regs->rxfifo_data);
  308. if (rx) {
  309. *rx++ = (u16) data;
  310. sspi->rx = rx;
  311. }
  312. sspi->left_rx_word--;
  313. }
  314. static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi)
  315. {
  316. u32 data = 0;
  317. const u16 *tx = sspi->tx;
  318. if (tx) {
  319. data = *tx++;
  320. sspi->tx = tx;
  321. }
  322. writel(data, sspi->base + sspi->regs->txfifo_data);
  323. sspi->left_tx_word--;
  324. }
  325. static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi)
  326. {
  327. u32 data;
  328. u32 *rx = sspi->rx;
  329. data = readl(sspi->base + sspi->regs->rxfifo_data);
  330. if (rx) {
  331. *rx++ = (u32) data;
  332. sspi->rx = rx;
  333. }
  334. sspi->left_rx_word--;
  335. }
  336. static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi)
  337. {
  338. u32 data = 0;
  339. const u32 *tx = sspi->tx;
  340. if (tx) {
  341. data = *tx++;
  342. sspi->tx = tx;
  343. }
  344. writel(data, sspi->base + sspi->regs->txfifo_data);
  345. sspi->left_tx_word--;
  346. }
  347. static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id)
  348. {
  349. struct sirfsoc_spi *sspi = dev_id;
  350. u32 spi_stat;
  351. spi_stat = readl(sspi->base + sspi->regs->int_st);
  352. if (sspi->tx_by_cmd && sspi->type == SIRF_REAL_SPI
  353. && (spi_stat & SIRFSOC_SPI_FRM_END)) {
  354. complete(&sspi->tx_done);
  355. writel(0x0, sspi->base + sspi->regs->int_en);
  356. writel(readl(sspi->base + sspi->regs->int_st),
  357. sspi->base + sspi->regs->int_st);
  358. return IRQ_HANDLED;
  359. }
  360. /* Error Conditions */
  361. if (spi_stat & SIRFSOC_SPI_RX_OFLOW ||
  362. spi_stat & SIRFSOC_SPI_TX_UFLOW) {
  363. complete(&sspi->tx_done);
  364. complete(&sspi->rx_done);
  365. switch (sspi->type) {
  366. case SIRF_REAL_SPI:
  367. case SIRF_USP_SPI_P2:
  368. writel(0x0, sspi->base + sspi->regs->int_en);
  369. break;
  370. case SIRF_USP_SPI_A7:
  371. writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
  372. break;
  373. }
  374. writel(readl(sspi->base + sspi->regs->int_st),
  375. sspi->base + sspi->regs->int_st);
  376. return IRQ_HANDLED;
  377. }
  378. if (spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY)
  379. complete(&sspi->tx_done);
  380. while (!(readl(sspi->base + sspi->regs->int_st) &
  381. SIRFSOC_SPI_RX_IO_DMA))
  382. cpu_relax();
  383. complete(&sspi->rx_done);
  384. switch (sspi->type) {
  385. case SIRF_REAL_SPI:
  386. case SIRF_USP_SPI_P2:
  387. writel(0x0, sspi->base + sspi->regs->int_en);
  388. break;
  389. case SIRF_USP_SPI_A7:
  390. writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
  391. break;
  392. }
  393. writel(readl(sspi->base + sspi->regs->int_st),
  394. sspi->base + sspi->regs->int_st);
  395. return IRQ_HANDLED;
  396. }
  397. static void spi_sirfsoc_dma_fini_callback(void *data)
  398. {
  399. struct completion *dma_complete = data;
  400. complete(dma_complete);
  401. }
  402. static void spi_sirfsoc_cmd_transfer(struct spi_device *spi,
  403. struct spi_transfer *t)
  404. {
  405. struct sirfsoc_spi *sspi;
  406. int timeout = t->len * 10;
  407. u32 cmd;
  408. sspi = spi_master_get_devdata(spi->master);
  409. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
  410. writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->txfifo_op);
  411. memcpy(&cmd, sspi->tx, t->len);
  412. if (sspi->word_width == 1 && !(spi->mode & SPI_LSB_FIRST))
  413. cmd = cpu_to_be32(cmd) >>
  414. ((SIRFSOC_MAX_CMD_BYTES - t->len) * 8);
  415. if (sspi->word_width == 2 && t->len == 4 &&
  416. (!(spi->mode & SPI_LSB_FIRST)))
  417. cmd = ((cmd & 0xffff) << 16) | (cmd >> 16);
  418. writel(cmd, sspi->base + sspi->regs->spi_cmd);
  419. writel(SIRFSOC_SPI_FRM_END_INT_EN,
  420. sspi->base + sspi->regs->int_en);
  421. writel(SIRFSOC_SPI_CMD_TX_EN,
  422. sspi->base + sspi->regs->tx_rx_en);
  423. if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
  424. dev_err(&spi->dev, "cmd transfer timeout\n");
  425. return;
  426. }
  427. sspi->left_rx_word -= t->len;
  428. }
  429. static void spi_sirfsoc_dma_transfer(struct spi_device *spi,
  430. struct spi_transfer *t)
  431. {
  432. struct sirfsoc_spi *sspi;
  433. struct dma_async_tx_descriptor *rx_desc, *tx_desc;
  434. int timeout = t->len * 10;
  435. sspi = spi_master_get_devdata(spi->master);
  436. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->rxfifo_op);
  437. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
  438. switch (sspi->type) {
  439. case SIRF_REAL_SPI:
  440. writel(SIRFSOC_SPI_FIFO_START,
  441. sspi->base + sspi->regs->rxfifo_op);
  442. writel(SIRFSOC_SPI_FIFO_START,
  443. sspi->base + sspi->regs->txfifo_op);
  444. writel(0, sspi->base + sspi->regs->int_en);
  445. break;
  446. case SIRF_USP_SPI_P2:
  447. writel(0x0, sspi->base + sspi->regs->rxfifo_op);
  448. writel(0x0, sspi->base + sspi->regs->txfifo_op);
  449. writel(0, sspi->base + sspi->regs->int_en);
  450. break;
  451. case SIRF_USP_SPI_A7:
  452. writel(0x0, sspi->base + sspi->regs->rxfifo_op);
  453. writel(0x0, sspi->base + sspi->regs->txfifo_op);
  454. writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
  455. break;
  456. }
  457. writel(readl(sspi->base + sspi->regs->int_st),
  458. sspi->base + sspi->regs->int_st);
  459. if (sspi->left_tx_word < sspi->dat_max_frm_len) {
  460. switch (sspi->type) {
  461. case SIRF_REAL_SPI:
  462. writel(readl(sspi->base + sspi->regs->spi_ctrl) |
  463. SIRFSOC_SPI_ENA_AUTO_CLR |
  464. SIRFSOC_SPI_MUL_DAT_MODE,
  465. sspi->base + sspi->regs->spi_ctrl);
  466. writel(sspi->left_tx_word - 1,
  467. sspi->base + sspi->regs->tx_dma_io_len);
  468. writel(sspi->left_tx_word - 1,
  469. sspi->base + sspi->regs->rx_dma_io_len);
  470. break;
  471. case SIRF_USP_SPI_P2:
  472. case SIRF_USP_SPI_A7:
  473. /*USP simulate SPI, tx/rx_dma_io_len indicates bytes*/
  474. writel(sspi->left_tx_word * sspi->word_width,
  475. sspi->base + sspi->regs->tx_dma_io_len);
  476. writel(sspi->left_tx_word * sspi->word_width,
  477. sspi->base + sspi->regs->rx_dma_io_len);
  478. break;
  479. }
  480. } else {
  481. if (sspi->type == SIRF_REAL_SPI)
  482. writel(readl(sspi->base + sspi->regs->spi_ctrl),
  483. sspi->base + sspi->regs->spi_ctrl);
  484. writel(0, sspi->base + sspi->regs->tx_dma_io_len);
  485. writel(0, sspi->base + sspi->regs->rx_dma_io_len);
  486. }
  487. sspi->dst_start = dma_map_single(&spi->dev, sspi->rx, t->len,
  488. (t->tx_buf != t->rx_buf) ?
  489. DMA_FROM_DEVICE : DMA_BIDIRECTIONAL);
  490. rx_desc = dmaengine_prep_slave_single(sspi->rx_chan,
  491. sspi->dst_start, t->len, DMA_DEV_TO_MEM,
  492. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  493. rx_desc->callback = spi_sirfsoc_dma_fini_callback;
  494. rx_desc->callback_param = &sspi->rx_done;
  495. sspi->src_start = dma_map_single(&spi->dev, (void *)sspi->tx, t->len,
  496. (t->tx_buf != t->rx_buf) ?
  497. DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  498. tx_desc = dmaengine_prep_slave_single(sspi->tx_chan,
  499. sspi->src_start, t->len, DMA_MEM_TO_DEV,
  500. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  501. tx_desc->callback = spi_sirfsoc_dma_fini_callback;
  502. tx_desc->callback_param = &sspi->tx_done;
  503. dmaengine_submit(tx_desc);
  504. dmaengine_submit(rx_desc);
  505. dma_async_issue_pending(sspi->tx_chan);
  506. dma_async_issue_pending(sspi->rx_chan);
  507. writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
  508. sspi->base + sspi->regs->tx_rx_en);
  509. if (sspi->type == SIRF_USP_SPI_P2 ||
  510. sspi->type == SIRF_USP_SPI_A7) {
  511. writel(SIRFSOC_SPI_FIFO_START,
  512. sspi->base + sspi->regs->rxfifo_op);
  513. writel(SIRFSOC_SPI_FIFO_START,
  514. sspi->base + sspi->regs->txfifo_op);
  515. }
  516. if (wait_for_completion_timeout(&sspi->rx_done, timeout) == 0) {
  517. dev_err(&spi->dev, "transfer timeout\n");
  518. dmaengine_terminate_all(sspi->rx_chan);
  519. } else
  520. sspi->left_rx_word = 0;
  521. /*
  522. * we only wait tx-done event if transferring by DMA. for PIO,
  523. * we get rx data by writing tx data, so if rx is done, tx has
  524. * done earlier
  525. */
  526. if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
  527. dev_err(&spi->dev, "transfer timeout\n");
  528. if (sspi->type == SIRF_USP_SPI_P2 ||
  529. sspi->type == SIRF_USP_SPI_A7)
  530. writel(0, sspi->base + sspi->regs->tx_rx_en);
  531. dmaengine_terminate_all(sspi->tx_chan);
  532. }
  533. dma_unmap_single(&spi->dev, sspi->src_start, t->len, DMA_TO_DEVICE);
  534. dma_unmap_single(&spi->dev, sspi->dst_start, t->len, DMA_FROM_DEVICE);
  535. /* TX, RX FIFO stop */
  536. writel(0, sspi->base + sspi->regs->rxfifo_op);
  537. writel(0, sspi->base + sspi->regs->txfifo_op);
  538. if (sspi->left_tx_word >= sspi->dat_max_frm_len)
  539. writel(0, sspi->base + sspi->regs->tx_rx_en);
  540. if (sspi->type == SIRF_USP_SPI_P2 ||
  541. sspi->type == SIRF_USP_SPI_A7)
  542. writel(0, sspi->base + sspi->regs->tx_rx_en);
  543. }
  544. static void spi_sirfsoc_pio_transfer(struct spi_device *spi,
  545. struct spi_transfer *t)
  546. {
  547. struct sirfsoc_spi *sspi;
  548. int timeout = t->len * 10;
  549. unsigned int data_units;
  550. sspi = spi_master_get_devdata(spi->master);
  551. do {
  552. writel(SIRFSOC_SPI_FIFO_RESET,
  553. sspi->base + sspi->regs->rxfifo_op);
  554. writel(SIRFSOC_SPI_FIFO_RESET,
  555. sspi->base + sspi->regs->txfifo_op);
  556. switch (sspi->type) {
  557. case SIRF_USP_SPI_P2:
  558. writel(0x0, sspi->base + sspi->regs->rxfifo_op);
  559. writel(0x0, sspi->base + sspi->regs->txfifo_op);
  560. writel(0, sspi->base + sspi->regs->int_en);
  561. writel(readl(sspi->base + sspi->regs->int_st),
  562. sspi->base + sspi->regs->int_st);
  563. writel(min((sspi->left_tx_word * sspi->word_width),
  564. sspi->fifo_size),
  565. sspi->base + sspi->regs->tx_dma_io_len);
  566. writel(min((sspi->left_rx_word * sspi->word_width),
  567. sspi->fifo_size),
  568. sspi->base + sspi->regs->rx_dma_io_len);
  569. break;
  570. case SIRF_USP_SPI_A7:
  571. writel(0x0, sspi->base + sspi->regs->rxfifo_op);
  572. writel(0x0, sspi->base + sspi->regs->txfifo_op);
  573. writel(~0UL, sspi->base + sspi->regs->usp_int_en_clr);
  574. writel(readl(sspi->base + sspi->regs->int_st),
  575. sspi->base + sspi->regs->int_st);
  576. writel(min((sspi->left_tx_word * sspi->word_width),
  577. sspi->fifo_size),
  578. sspi->base + sspi->regs->tx_dma_io_len);
  579. writel(min((sspi->left_rx_word * sspi->word_width),
  580. sspi->fifo_size),
  581. sspi->base + sspi->regs->rx_dma_io_len);
  582. break;
  583. case SIRF_REAL_SPI:
  584. writel(SIRFSOC_SPI_FIFO_START,
  585. sspi->base + sspi->regs->rxfifo_op);
  586. writel(SIRFSOC_SPI_FIFO_START,
  587. sspi->base + sspi->regs->txfifo_op);
  588. writel(0, sspi->base + sspi->regs->int_en);
  589. writel(readl(sspi->base + sspi->regs->int_st),
  590. sspi->base + sspi->regs->int_st);
  591. writel(readl(sspi->base + sspi->regs->spi_ctrl) |
  592. SIRFSOC_SPI_MUL_DAT_MODE |
  593. SIRFSOC_SPI_ENA_AUTO_CLR,
  594. sspi->base + sspi->regs->spi_ctrl);
  595. data_units = sspi->fifo_size / sspi->word_width;
  596. writel(min(sspi->left_tx_word, data_units) - 1,
  597. sspi->base + sspi->regs->tx_dma_io_len);
  598. writel(min(sspi->left_rx_word, data_units) - 1,
  599. sspi->base + sspi->regs->rx_dma_io_len);
  600. break;
  601. }
  602. while (!((readl(sspi->base + sspi->regs->txfifo_st)
  603. & SIRFSOC_SPI_FIFO_FULL_MASK(sspi))) &&
  604. sspi->left_tx_word)
  605. sspi->tx_word(sspi);
  606. writel(SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN |
  607. SIRFSOC_SPI_TX_UFLOW_INT_EN |
  608. SIRFSOC_SPI_RX_OFLOW_INT_EN |
  609. SIRFSOC_SPI_RX_IO_DMA_INT_EN,
  610. sspi->base + sspi->regs->int_en);
  611. writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
  612. sspi->base + sspi->regs->tx_rx_en);
  613. if (sspi->type == SIRF_USP_SPI_P2 ||
  614. sspi->type == SIRF_USP_SPI_A7) {
  615. writel(SIRFSOC_SPI_FIFO_START,
  616. sspi->base + sspi->regs->rxfifo_op);
  617. writel(SIRFSOC_SPI_FIFO_START,
  618. sspi->base + sspi->regs->txfifo_op);
  619. }
  620. if (!wait_for_completion_timeout(&sspi->tx_done, timeout) ||
  621. !wait_for_completion_timeout(&sspi->rx_done, timeout)) {
  622. dev_err(&spi->dev, "transfer timeout\n");
  623. if (sspi->type == SIRF_USP_SPI_P2 ||
  624. sspi->type == SIRF_USP_SPI_A7)
  625. writel(0, sspi->base + sspi->regs->tx_rx_en);
  626. break;
  627. }
  628. while (!((readl(sspi->base + sspi->regs->rxfifo_st)
  629. & SIRFSOC_SPI_FIFO_EMPTY_MASK(sspi))) &&
  630. sspi->left_rx_word)
  631. sspi->rx_word(sspi);
  632. if (sspi->type == SIRF_USP_SPI_P2 ||
  633. sspi->type == SIRF_USP_SPI_A7)
  634. writel(0, sspi->base + sspi->regs->tx_rx_en);
  635. writel(0, sspi->base + sspi->regs->rxfifo_op);
  636. writel(0, sspi->base + sspi->regs->txfifo_op);
  637. } while (sspi->left_tx_word != 0 || sspi->left_rx_word != 0);
  638. }
  639. static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t)
  640. {
  641. struct sirfsoc_spi *sspi;
  642. sspi = spi_master_get_devdata(spi->master);
  643. sspi->tx = t->tx_buf;
  644. sspi->rx = t->rx_buf;
  645. sspi->left_tx_word = sspi->left_rx_word = t->len / sspi->word_width;
  646. reinit_completion(&sspi->rx_done);
  647. reinit_completion(&sspi->tx_done);
  648. /*
  649. * in the transfer, if transfer data using command register with rx_buf
  650. * null, just fill command data into command register and wait for its
  651. * completion.
  652. */
  653. if (sspi->type == SIRF_REAL_SPI && sspi->tx_by_cmd)
  654. spi_sirfsoc_cmd_transfer(spi, t);
  655. else if (IS_DMA_VALID(t))
  656. spi_sirfsoc_dma_transfer(spi, t);
  657. else
  658. spi_sirfsoc_pio_transfer(spi, t);
  659. return t->len - sspi->left_rx_word * sspi->word_width;
  660. }
  661. static void spi_sirfsoc_chipselect(struct spi_device *spi, int value)
  662. {
  663. struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master);
  664. if (sspi->hw_cs) {
  665. u32 regval;
  666. switch (sspi->type) {
  667. case SIRF_REAL_SPI:
  668. regval = readl(sspi->base + sspi->regs->spi_ctrl);
  669. switch (value) {
  670. case BITBANG_CS_ACTIVE:
  671. if (spi->mode & SPI_CS_HIGH)
  672. regval |= SIRFSOC_SPI_CS_IO_OUT;
  673. else
  674. regval &= ~SIRFSOC_SPI_CS_IO_OUT;
  675. break;
  676. case BITBANG_CS_INACTIVE:
  677. if (spi->mode & SPI_CS_HIGH)
  678. regval &= ~SIRFSOC_SPI_CS_IO_OUT;
  679. else
  680. regval |= SIRFSOC_SPI_CS_IO_OUT;
  681. break;
  682. }
  683. writel(regval, sspi->base + sspi->regs->spi_ctrl);
  684. break;
  685. case SIRF_USP_SPI_P2:
  686. case SIRF_USP_SPI_A7:
  687. regval = readl(sspi->base +
  688. sspi->regs->usp_pin_io_data);
  689. switch (value) {
  690. case BITBANG_CS_ACTIVE:
  691. if (spi->mode & SPI_CS_HIGH)
  692. regval |= SIRFSOC_USP_CS_HIGH_VALUE;
  693. else
  694. regval &= ~(SIRFSOC_USP_CS_HIGH_VALUE);
  695. break;
  696. case BITBANG_CS_INACTIVE:
  697. if (spi->mode & SPI_CS_HIGH)
  698. regval &= ~(SIRFSOC_USP_CS_HIGH_VALUE);
  699. else
  700. regval |= SIRFSOC_USP_CS_HIGH_VALUE;
  701. break;
  702. }
  703. writel(regval,
  704. sspi->base + sspi->regs->usp_pin_io_data);
  705. break;
  706. }
  707. } else {
  708. switch (value) {
  709. case BITBANG_CS_ACTIVE:
  710. gpio_direction_output(spi->cs_gpio,
  711. spi->mode & SPI_CS_HIGH ? 1 : 0);
  712. break;
  713. case BITBANG_CS_INACTIVE:
  714. gpio_direction_output(spi->cs_gpio,
  715. spi->mode & SPI_CS_HIGH ? 0 : 1);
  716. break;
  717. }
  718. }
  719. }
  720. static int spi_sirfsoc_config_mode(struct spi_device *spi)
  721. {
  722. struct sirfsoc_spi *sspi;
  723. u32 regval, usp_mode1;
  724. sspi = spi_master_get_devdata(spi->master);
  725. regval = readl(sspi->base + sspi->regs->spi_ctrl);
  726. usp_mode1 = readl(sspi->base + sspi->regs->usp_mode1);
  727. if (!(spi->mode & SPI_CS_HIGH)) {
  728. regval |= SIRFSOC_SPI_CS_IDLE_STAT;
  729. usp_mode1 &= ~SIRFSOC_USP_CS_HIGH_VALID;
  730. } else {
  731. regval &= ~SIRFSOC_SPI_CS_IDLE_STAT;
  732. usp_mode1 |= SIRFSOC_USP_CS_HIGH_VALID;
  733. }
  734. if (!(spi->mode & SPI_LSB_FIRST)) {
  735. regval |= SIRFSOC_SPI_TRAN_MSB;
  736. usp_mode1 &= ~SIRFSOC_USP_LSB;
  737. } else {
  738. regval &= ~SIRFSOC_SPI_TRAN_MSB;
  739. usp_mode1 |= SIRFSOC_USP_LSB;
  740. }
  741. if (spi->mode & SPI_CPOL) {
  742. regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
  743. usp_mode1 |= SIRFSOC_USP_SCLK_IDLE_STAT;
  744. } else {
  745. regval &= ~SIRFSOC_SPI_CLK_IDLE_STAT;
  746. usp_mode1 &= ~SIRFSOC_USP_SCLK_IDLE_STAT;
  747. }
  748. /*
  749. * Data should be driven at least 1/2 cycle before the fetch edge
  750. * to make sure that data gets stable at the fetch edge.
  751. */
  752. if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
  753. (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA))) {
  754. regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
  755. usp_mode1 |= (SIRFSOC_USP_TXD_FALLING_EDGE |
  756. SIRFSOC_USP_RXD_FALLING_EDGE);
  757. } else {
  758. regval |= SIRFSOC_SPI_DRV_POS_EDGE;
  759. usp_mode1 &= ~(SIRFSOC_USP_RXD_FALLING_EDGE |
  760. SIRFSOC_USP_TXD_FALLING_EDGE);
  761. }
  762. writel((SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size - 2) <<
  763. SIRFSOC_SPI_FIFO_SC_OFFSET) |
  764. (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size / 2) <<
  765. SIRFSOC_SPI_FIFO_LC_OFFSET) |
  766. (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, 2) <<
  767. SIRFSOC_SPI_FIFO_HC_OFFSET),
  768. sspi->base + sspi->regs->txfifo_level_chk);
  769. writel((SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, 2) <<
  770. SIRFSOC_SPI_FIFO_SC_OFFSET) |
  771. (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size / 2) <<
  772. SIRFSOC_SPI_FIFO_LC_OFFSET) |
  773. (SIRFSOC_SPI_FIFO_LEVEL_CHK_MASK(sspi, sspi->fifo_size - 2) <<
  774. SIRFSOC_SPI_FIFO_HC_OFFSET),
  775. sspi->base + sspi->regs->rxfifo_level_chk);
  776. /*
  777. * it should never set to hardware cs mode because in hardware cs mode,
  778. * cs signal can't controlled by driver.
  779. */
  780. switch (sspi->type) {
  781. case SIRF_REAL_SPI:
  782. regval |= SIRFSOC_SPI_CS_IO_MODE;
  783. writel(regval, sspi->base + sspi->regs->spi_ctrl);
  784. break;
  785. case SIRF_USP_SPI_P2:
  786. case SIRF_USP_SPI_A7:
  787. usp_mode1 |= SIRFSOC_USP_SYNC_MODE;
  788. usp_mode1 |= SIRFSOC_USP_TFS_IO_MODE;
  789. usp_mode1 &= ~SIRFSOC_USP_TFS_IO_INPUT;
  790. writel(usp_mode1, sspi->base + sspi->regs->usp_mode1);
  791. break;
  792. }
  793. return 0;
  794. }
  795. static int
  796. spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  797. {
  798. struct sirfsoc_spi *sspi;
  799. u8 bits_per_word = 0;
  800. int hz = 0;
  801. u32 regval, txfifo_ctrl, rxfifo_ctrl, tx_frm_ctl, rx_frm_ctl, usp_mode2;
  802. sspi = spi_master_get_devdata(spi->master);
  803. bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
  804. hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  805. usp_mode2 = regval = (sspi->ctrl_freq / (2 * hz)) - 1;
  806. if (regval > 0xFFFF || regval < 0) {
  807. dev_err(&spi->dev, "Speed %d not supported\n", hz);
  808. return -EINVAL;
  809. }
  810. switch (bits_per_word) {
  811. case 8:
  812. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
  813. sspi->rx_word = spi_sirfsoc_rx_word_u8;
  814. sspi->tx_word = spi_sirfsoc_tx_word_u8;
  815. break;
  816. case 12:
  817. case 16:
  818. regval |= (bits_per_word == 12) ?
  819. SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
  820. SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
  821. sspi->rx_word = spi_sirfsoc_rx_word_u16;
  822. sspi->tx_word = spi_sirfsoc_tx_word_u16;
  823. break;
  824. case 32:
  825. regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
  826. sspi->rx_word = spi_sirfsoc_rx_word_u32;
  827. sspi->tx_word = spi_sirfsoc_tx_word_u32;
  828. break;
  829. default:
  830. dev_err(&spi->dev, "bpw %d not supported\n", bits_per_word);
  831. return -EINVAL;
  832. }
  833. sspi->word_width = DIV_ROUND_UP(bits_per_word, 8);
  834. txfifo_ctrl = (((sspi->fifo_size / 2) &
  835. SIRFSOC_SPI_FIFO_THD_MASK(sspi))
  836. << SIRFSOC_SPI_FIFO_THD_OFFSET) |
  837. (sspi->word_width >> 1);
  838. rxfifo_ctrl = (((sspi->fifo_size / 2) &
  839. SIRFSOC_SPI_FIFO_THD_MASK(sspi))
  840. << SIRFSOC_SPI_FIFO_THD_OFFSET) |
  841. (sspi->word_width >> 1);
  842. writel(txfifo_ctrl, sspi->base + sspi->regs->txfifo_ctrl);
  843. writel(rxfifo_ctrl, sspi->base + sspi->regs->rxfifo_ctrl);
  844. if (sspi->type == SIRF_USP_SPI_P2 ||
  845. sspi->type == SIRF_USP_SPI_A7) {
  846. tx_frm_ctl = 0;
  847. tx_frm_ctl |= ((bits_per_word - 1) & SIRFSOC_USP_TX_DATA_MASK)
  848. << SIRFSOC_USP_TX_DATA_OFFSET;
  849. tx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_TXD_DELAY_LEN
  850. - 1) & SIRFSOC_USP_TX_SYNC_MASK) <<
  851. SIRFSOC_USP_TX_SYNC_OFFSET;
  852. tx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_TXD_DELAY_LEN
  853. + 2 - 1) & SIRFSOC_USP_TX_FRAME_MASK) <<
  854. SIRFSOC_USP_TX_FRAME_OFFSET;
  855. tx_frm_ctl |= ((bits_per_word - 1) &
  856. SIRFSOC_USP_TX_SHIFTER_MASK) <<
  857. SIRFSOC_USP_TX_SHIFTER_OFFSET;
  858. rx_frm_ctl = 0;
  859. rx_frm_ctl |= ((bits_per_word - 1) & SIRFSOC_USP_RX_DATA_MASK)
  860. << SIRFSOC_USP_RX_DATA_OFFSET;
  861. rx_frm_ctl |= ((bits_per_word + 1 + SIRFSOC_USP_RXD_DELAY_LEN
  862. + 2 - 1) & SIRFSOC_USP_RX_FRAME_MASK) <<
  863. SIRFSOC_USP_RX_FRAME_OFFSET;
  864. rx_frm_ctl |= ((bits_per_word - 1)
  865. & SIRFSOC_USP_RX_SHIFTER_MASK) <<
  866. SIRFSOC_USP_RX_SHIFTER_OFFSET;
  867. writel(tx_frm_ctl | (((usp_mode2 >> 10) &
  868. SIRFSOC_USP_CLK_10_11_MASK) <<
  869. SIRFSOC_USP_CLK_10_11_OFFSET),
  870. sspi->base + sspi->regs->usp_tx_frame_ctrl);
  871. writel(rx_frm_ctl | (((usp_mode2 >> 12) &
  872. SIRFSOC_USP_CLK_12_15_MASK) <<
  873. SIRFSOC_USP_CLK_12_15_OFFSET),
  874. sspi->base + sspi->regs->usp_rx_frame_ctrl);
  875. writel(readl(sspi->base + sspi->regs->usp_mode2) |
  876. ((usp_mode2 & SIRFSOC_USP_CLK_DIVISOR_MASK) <<
  877. SIRFSOC_USP_CLK_DIVISOR_OFFSET) |
  878. (SIRFSOC_USP_RXD_DELAY_LEN <<
  879. SIRFSOC_USP_RXD_DELAY_OFFSET) |
  880. (SIRFSOC_USP_TXD_DELAY_LEN <<
  881. SIRFSOC_USP_TXD_DELAY_OFFSET),
  882. sspi->base + sspi->regs->usp_mode2);
  883. }
  884. if (sspi->type == SIRF_REAL_SPI)
  885. writel(regval, sspi->base + sspi->regs->spi_ctrl);
  886. spi_sirfsoc_config_mode(spi);
  887. if (sspi->type == SIRF_REAL_SPI) {
  888. if (t && t->tx_buf && !t->rx_buf &&
  889. (t->len <= SIRFSOC_MAX_CMD_BYTES)) {
  890. sspi->tx_by_cmd = true;
  891. writel(readl(sspi->base + sspi->regs->spi_ctrl) |
  892. (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) |
  893. SIRFSOC_SPI_CMD_MODE),
  894. sspi->base + sspi->regs->spi_ctrl);
  895. } else {
  896. sspi->tx_by_cmd = false;
  897. writel(readl(sspi->base + sspi->regs->spi_ctrl) &
  898. ~SIRFSOC_SPI_CMD_MODE,
  899. sspi->base + sspi->regs->spi_ctrl);
  900. }
  901. }
  902. if (IS_DMA_VALID(t)) {
  903. /* Enable DMA mode for RX, TX */
  904. writel(0, sspi->base + sspi->regs->tx_dma_io_ctrl);
  905. writel(SIRFSOC_SPI_RX_DMA_FLUSH,
  906. sspi->base + sspi->regs->rx_dma_io_ctrl);
  907. } else {
  908. /* Enable IO mode for RX, TX */
  909. writel(SIRFSOC_SPI_IO_MODE_SEL,
  910. sspi->base + sspi->regs->tx_dma_io_ctrl);
  911. writel(SIRFSOC_SPI_IO_MODE_SEL,
  912. sspi->base + sspi->regs->rx_dma_io_ctrl);
  913. }
  914. return 0;
  915. }
  916. static int spi_sirfsoc_setup(struct spi_device *spi)
  917. {
  918. struct sirfsoc_spi *sspi;
  919. int ret = 0;
  920. sspi = spi_master_get_devdata(spi->master);
  921. if (spi->cs_gpio == -ENOENT)
  922. sspi->hw_cs = true;
  923. else {
  924. sspi->hw_cs = false;
  925. if (!spi_get_ctldata(spi)) {
  926. void *cs = kmalloc(sizeof(int), GFP_KERNEL);
  927. if (!cs) {
  928. ret = -ENOMEM;
  929. goto exit;
  930. }
  931. ret = gpio_is_valid(spi->cs_gpio);
  932. if (!ret) {
  933. dev_err(&spi->dev, "no valid gpio\n");
  934. ret = -ENOENT;
  935. goto exit;
  936. }
  937. ret = gpio_request(spi->cs_gpio, DRIVER_NAME);
  938. if (ret) {
  939. dev_err(&spi->dev, "failed to request gpio\n");
  940. goto exit;
  941. }
  942. spi_set_ctldata(spi, cs);
  943. }
  944. }
  945. spi_sirfsoc_config_mode(spi);
  946. spi_sirfsoc_chipselect(spi, BITBANG_CS_INACTIVE);
  947. exit:
  948. return ret;
  949. }
  950. static void spi_sirfsoc_cleanup(struct spi_device *spi)
  951. {
  952. if (spi_get_ctldata(spi)) {
  953. gpio_free(spi->cs_gpio);
  954. kfree(spi_get_ctldata(spi));
  955. }
  956. }
  957. static const struct sirf_spi_comp_data sirf_real_spi = {
  958. .regs = &real_spi_register,
  959. .type = SIRF_REAL_SPI,
  960. .dat_max_frm_len = 64 * 1024,
  961. .fifo_size = 256,
  962. };
  963. static const struct sirf_spi_comp_data sirf_usp_spi_p2 = {
  964. .regs = &usp_spi_register,
  965. .type = SIRF_USP_SPI_P2,
  966. .dat_max_frm_len = 1024 * 1024,
  967. .fifo_size = 128,
  968. .hwinit = sirfsoc_usp_hwinit,
  969. };
  970. static const struct sirf_spi_comp_data sirf_usp_spi_a7 = {
  971. .regs = &usp_spi_register,
  972. .type = SIRF_USP_SPI_A7,
  973. .dat_max_frm_len = 1024 * 1024,
  974. .fifo_size = 512,
  975. .hwinit = sirfsoc_usp_hwinit,
  976. };
  977. static const struct of_device_id spi_sirfsoc_of_match[] = {
  978. { .compatible = "sirf,prima2-spi", .data = &sirf_real_spi},
  979. { .compatible = "sirf,prima2-usp-spi", .data = &sirf_usp_spi_p2},
  980. { .compatible = "sirf,atlas7-usp-spi", .data = &sirf_usp_spi_a7},
  981. {}
  982. };
  983. MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match);
  984. static int spi_sirfsoc_probe(struct platform_device *pdev)
  985. {
  986. struct sirfsoc_spi *sspi;
  987. struct spi_master *master;
  988. struct resource *mem_res;
  989. const struct sirf_spi_comp_data *spi_comp_data;
  990. int irq;
  991. int ret;
  992. const struct of_device_id *match;
  993. ret = device_reset(&pdev->dev);
  994. if (ret) {
  995. dev_err(&pdev->dev, "SPI reset failed!\n");
  996. return ret;
  997. }
  998. master = spi_alloc_master(&pdev->dev, sizeof(*sspi));
  999. if (!master) {
  1000. dev_err(&pdev->dev, "Unable to allocate SPI master\n");
  1001. return -ENOMEM;
  1002. }
  1003. match = of_match_node(spi_sirfsoc_of_match, pdev->dev.of_node);
  1004. platform_set_drvdata(pdev, master);
  1005. sspi = spi_master_get_devdata(master);
  1006. sspi->fifo_full_offset = ilog2(sspi->fifo_size);
  1007. spi_comp_data = match->data;
  1008. sspi->regs = spi_comp_data->regs;
  1009. sspi->type = spi_comp_data->type;
  1010. sspi->fifo_level_chk_mask = (sspi->fifo_size / 4) - 1;
  1011. sspi->dat_max_frm_len = spi_comp_data->dat_max_frm_len;
  1012. sspi->fifo_size = spi_comp_data->fifo_size;
  1013. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1014. sspi->base = devm_ioremap_resource(&pdev->dev, mem_res);
  1015. if (IS_ERR(sspi->base)) {
  1016. ret = PTR_ERR(sspi->base);
  1017. goto free_master;
  1018. }
  1019. irq = platform_get_irq(pdev, 0);
  1020. if (irq < 0) {
  1021. ret = -ENXIO;
  1022. goto free_master;
  1023. }
  1024. ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
  1025. DRIVER_NAME, sspi);
  1026. if (ret)
  1027. goto free_master;
  1028. sspi->bitbang.master = master;
  1029. sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
  1030. sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
  1031. sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
  1032. sspi->bitbang.master->setup = spi_sirfsoc_setup;
  1033. sspi->bitbang.master->cleanup = spi_sirfsoc_cleanup;
  1034. master->bus_num = pdev->id;
  1035. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH;
  1036. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
  1037. SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
  1038. master->max_speed_hz = SIRFSOC_SPI_DEFAULT_FRQ;
  1039. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  1040. sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
  1041. /* request DMA channels */
  1042. sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx");
  1043. if (!sspi->rx_chan) {
  1044. dev_err(&pdev->dev, "can not allocate rx dma channel\n");
  1045. ret = -ENODEV;
  1046. goto free_master;
  1047. }
  1048. sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx");
  1049. if (!sspi->tx_chan) {
  1050. dev_err(&pdev->dev, "can not allocate tx dma channel\n");
  1051. ret = -ENODEV;
  1052. goto free_rx_dma;
  1053. }
  1054. sspi->clk = clk_get(&pdev->dev, NULL);
  1055. if (IS_ERR(sspi->clk)) {
  1056. ret = PTR_ERR(sspi->clk);
  1057. goto free_tx_dma;
  1058. }
  1059. clk_prepare_enable(sspi->clk);
  1060. if (spi_comp_data->hwinit)
  1061. spi_comp_data->hwinit(sspi);
  1062. sspi->ctrl_freq = clk_get_rate(sspi->clk);
  1063. init_completion(&sspi->rx_done);
  1064. init_completion(&sspi->tx_done);
  1065. ret = spi_bitbang_start(&sspi->bitbang);
  1066. if (ret)
  1067. goto free_clk;
  1068. dev_info(&pdev->dev, "registered, bus number = %d\n", master->bus_num);
  1069. return 0;
  1070. free_clk:
  1071. clk_disable_unprepare(sspi->clk);
  1072. clk_put(sspi->clk);
  1073. free_tx_dma:
  1074. dma_release_channel(sspi->tx_chan);
  1075. free_rx_dma:
  1076. dma_release_channel(sspi->rx_chan);
  1077. free_master:
  1078. spi_master_put(master);
  1079. return ret;
  1080. }
  1081. static int spi_sirfsoc_remove(struct platform_device *pdev)
  1082. {
  1083. struct spi_master *master;
  1084. struct sirfsoc_spi *sspi;
  1085. master = platform_get_drvdata(pdev);
  1086. sspi = spi_master_get_devdata(master);
  1087. spi_bitbang_stop(&sspi->bitbang);
  1088. clk_disable_unprepare(sspi->clk);
  1089. clk_put(sspi->clk);
  1090. dma_release_channel(sspi->rx_chan);
  1091. dma_release_channel(sspi->tx_chan);
  1092. spi_master_put(master);
  1093. return 0;
  1094. }
  1095. #ifdef CONFIG_PM_SLEEP
  1096. static int spi_sirfsoc_suspend(struct device *dev)
  1097. {
  1098. struct spi_master *master = dev_get_drvdata(dev);
  1099. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  1100. int ret;
  1101. ret = spi_master_suspend(master);
  1102. if (ret)
  1103. return ret;
  1104. clk_disable(sspi->clk);
  1105. return 0;
  1106. }
  1107. static int spi_sirfsoc_resume(struct device *dev)
  1108. {
  1109. struct spi_master *master = dev_get_drvdata(dev);
  1110. struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
  1111. clk_enable(sspi->clk);
  1112. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->txfifo_op);
  1113. writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + sspi->regs->rxfifo_op);
  1114. writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->txfifo_op);
  1115. writel(SIRFSOC_SPI_FIFO_START, sspi->base + sspi->regs->rxfifo_op);
  1116. return 0;
  1117. }
  1118. #endif
  1119. static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend,
  1120. spi_sirfsoc_resume);
  1121. static struct platform_driver spi_sirfsoc_driver = {
  1122. .driver = {
  1123. .name = DRIVER_NAME,
  1124. .pm = &spi_sirfsoc_pm_ops,
  1125. .of_match_table = spi_sirfsoc_of_match,
  1126. },
  1127. .probe = spi_sirfsoc_probe,
  1128. .remove = spi_sirfsoc_remove,
  1129. };
  1130. module_platform_driver(spi_sirfsoc_driver);
  1131. MODULE_DESCRIPTION("SiRF SoC SPI master driver");
  1132. MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>");
  1133. MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>");
  1134. MODULE_AUTHOR("Qipan Li <Qipan.Li@csr.com>");
  1135. MODULE_LICENSE("GPL v2");