spi-sirf.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. 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 = (struct sirf_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, "registerred, 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");