ecspi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. #include "FreeRTOS.h"
  2. #include "chip.h"
  3. #include "board.h"
  4. #include <string.h>
  5. #define SPI1_CS0_GPIO 86
  6. #define USE_DMA_MIN_THRESHOLD 32
  7. #define USE_DMA_MAX_THRESHOLD 512
  8. #define MALLOC_DMA_MEM_SIZE 0x1000
  9. #define ARK_ECSPI_RXDATA 0x50
  10. #define ARK_ECSPI_TXDATA 0x460
  11. /* generic defines to abstract from the different register layouts */
  12. #define ARK_INT_RR (1 << 0) /* Receive data ready interrupt */
  13. #define ARK_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
  14. #define ARK_INT_TC (1 << 2) /* Transfer Completed interrupt */
  15. #define ARK_INT_TF (1 << 3)
  16. #define ARK_INT_TR (1 << 4) /* Transmit FIFO request interrupt */
  17. /* The maximum bytes that a sdma BD can transfer.*/
  18. #define MAX_SDMA_BD_BYTES (1 << 15)
  19. #define ARK_ECSPI_CTRL_MAX_BURST 512
  20. #define ARK_ECSPI_CTRL 0x08
  21. #define ARK_ECSPI_CTRL_ENABLE (1 << 0)
  22. #define ARK_ECSPI_CTRL_XCH (1 << 2)
  23. #define ARK_ECSPI_CTRL_SMC (1 << 3)
  24. #define ARK_ECSPI_CTRL_MODE_MASK (0xf << 4)
  25. #define ARK_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
  26. #define ARK_ECSPI_CTRL_POSTDIV_OFFSET 8
  27. #define ARK_ECSPI_CTRL_PREDIV_OFFSET 12
  28. #define ARK_ECSPI_CTRL_CS(cs) ((cs) << 18)
  29. #define ARK_ECSPI_CTRL_BL_OFFSET 20
  30. #define ARK_ECSPI_CTRL_BL_MASK (0xfff << 20)
  31. #define ARK_ECSPI_CONFIG 0x0c
  32. #define ARK_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
  33. #define ARK_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
  34. #define ARK_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
  35. #define ARK_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
  36. #define ARK_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
  37. #define ARK_ECSPI_INT 0x10
  38. #define ARK_ECSPI_INT_TEEN (1 << 0)
  39. #define ARK_ECSPI_INT_TREN (1 << 1)
  40. #define ARK_ECSPI_INT_TFEN (1 << 2)
  41. #define ARK_ECSPI_INT_RREN (1 << 3)
  42. #define ARK_ECSPI_INT_TCEN (1 << 7)
  43. #define ARK_ECSPI_DMA 0x14
  44. #define ARK_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
  45. #define ARK_ECSPI_DMA_RX_WML(wml) ((((wml) & 0x3f) - 1) << 16)
  46. #define ARK_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
  47. #define ARK_ECSPI_DMA_TEDEN (1 << 7)
  48. #define ARK_ECSPI_DMA_RXDEN (1 << 23)
  49. #define ARK_ECSPI_DMA_RXTDEN (1UL << 31)
  50. #define ARK_ECSPI_STAT 0x18
  51. #define ARK_ECSPI_STAT_REN (1 << 8)
  52. #define ARK_ECSPI_STAT_RR (1 << 3)
  53. #define ARK_ECSPI_TESTREG 0x20
  54. #define ARK_ECSPI_TESTREG_LBC BIT(31)
  55. struct ark_ecspi_data;
  56. struct ark_spi_devtype_data {
  57. void (*intctrl)(struct ark_ecspi_data *, int);
  58. int (*config)(struct ark_ecspi_data *);
  59. void (*trigger)(struct ark_ecspi_data *);
  60. int (*rx_available)(struct ark_ecspi_data *);
  61. void (*reset)(struct ark_ecspi_data *);
  62. unsigned int fifo_size;
  63. bool has_dmamode;
  64. };
  65. struct ark_ecspi_data {
  66. struct spi_slave slave;
  67. QueueHandle_t xfer_done;
  68. unsigned int base;
  69. unsigned int irq;
  70. unsigned int spi_clk;
  71. unsigned int spi_bus_clk;
  72. unsigned int speed_hz;
  73. unsigned int bits_per_word;
  74. unsigned int spi_drctl;
  75. unsigned int count, remainder;
  76. void (*tx)(struct ark_ecspi_data *);
  77. void (*rx)(struct ark_ecspi_data *);
  78. unsigned char *rx_buf;
  79. const unsigned char *tx_buf;
  80. unsigned int txfifo; /* number of words pushed in tx FIFO */
  81. unsigned int read_u32;
  82. unsigned int word_mask;
  83. unsigned int cs_gpio;
  84. bool is_arke;
  85. /* DMA */
  86. bool usedma;
  87. u32 wml;
  88. QueueHandle_t dma_rx_completion;
  89. QueueHandle_t dma_tx_completion;
  90. struct spi_message dma_message;
  91. struct spi_message pio_message;
  92. struct dma_chan *dma_tx;
  93. struct dma_chan *dma_rx;
  94. char *rx_dummy_buffer;
  95. char *tx_dummy_buffer;
  96. const struct ark_spi_devtype_data *devtype_data;
  97. };
  98. static void ark_spi_buf_rx_u8(struct ark_ecspi_data *aspi)
  99. {
  100. unsigned int val = readl(aspi->base + ARK_ECSPI_RXDATA);
  101. if (aspi->rx_buf) {
  102. if (aspi->is_arke)
  103. *(u8*)aspi->rx_buf = val & 0xff;
  104. else
  105. *(u8*)aspi->rx_buf = (val >> 24) & 0xff;
  106. aspi->rx_buf += 1;
  107. }
  108. }
  109. static void ark_spi_buf_rx_u16(struct ark_ecspi_data *aspi)
  110. {
  111. unsigned int val = readl(aspi->base + ARK_ECSPI_RXDATA);
  112. if (aspi->rx_buf) {
  113. if (aspi->is_arke)
  114. *(u16*)aspi->rx_buf = val & 0xffff;
  115. else
  116. *(u16*)aspi->rx_buf = (val >> 16) & 0xffff;
  117. aspi->rx_buf += 2;
  118. }
  119. }
  120. static void ark_spi_buf_tx_u8(struct ark_ecspi_data *aspi)
  121. {
  122. u32 val = 0;
  123. if (aspi->tx_buf) {
  124. if (aspi->is_arke)
  125. val = *(u8 *)aspi->tx_buf;
  126. else
  127. val = *(u8 *)aspi->tx_buf << 24;
  128. aspi->tx_buf += 1;
  129. }
  130. aspi->count -= 1;
  131. writel(val, aspi->base + ARK_ECSPI_TXDATA);
  132. }
  133. static void ark_spi_buf_tx_u16(struct ark_ecspi_data *aspi)
  134. {
  135. u32 val = 0;
  136. if (aspi->tx_buf) {
  137. if (aspi->is_arke)
  138. val = *(u16 *)aspi->tx_buf;
  139. else
  140. val = *(u16 *)aspi->tx_buf << 16;
  141. aspi->tx_buf += 2;
  142. }
  143. aspi->count -=2;
  144. writel(val, aspi->base + ARK_ECSPI_TXDATA);
  145. }
  146. static int ark_spi_bytes_per_word(const int bits_per_word)
  147. {
  148. return DIV_ROUND_UP(bits_per_word, BITS_PER_BYTE);
  149. }
  150. static void ark_spi_buf_rx_swap_u32(struct ark_ecspi_data *aspi)
  151. {
  152. unsigned int val = readl(aspi->base + ARK_ECSPI_RXDATA);
  153. if (aspi->rx_buf) {
  154. val &= aspi->word_mask;
  155. *(u32 *)aspi->rx_buf = val;
  156. aspi->rx_buf += sizeof(u32);
  157. }
  158. }
  159. static void ark_spi_buf_rx_swap(struct ark_ecspi_data *aspi)
  160. {
  161. unsigned int bytes_per_word;
  162. bytes_per_word = ark_spi_bytes_per_word(aspi->bits_per_word);
  163. if (aspi->read_u32) {
  164. ark_spi_buf_rx_swap_u32(aspi);
  165. return;
  166. }
  167. if (bytes_per_word == 1)
  168. ark_spi_buf_rx_u8(aspi);
  169. else if (bytes_per_word == 2)
  170. ark_spi_buf_rx_u16(aspi);
  171. }
  172. static void ark_spi_buf_tx_swap_u32(struct ark_ecspi_data *aspi)
  173. {
  174. u32 val = 0;
  175. if (aspi->tx_buf) {
  176. val = *(u32 *)aspi->tx_buf;
  177. val &= aspi->word_mask;
  178. aspi->tx_buf += sizeof(u32);
  179. }
  180. aspi->count -= sizeof(u32);
  181. writel(val, aspi->base + ARK_ECSPI_TXDATA);
  182. }
  183. static void ark_spi_buf_tx_swap(struct ark_ecspi_data *aspi)
  184. {
  185. u32 ctrl, val;
  186. unsigned int bytes_per_word;
  187. if (aspi->count == aspi->remainder) {
  188. ctrl = readl(aspi->base + ARK_ECSPI_CTRL);
  189. ctrl &= ~ARK_ECSPI_CTRL_BL_MASK;
  190. if (aspi->count > ARK_ECSPI_CTRL_MAX_BURST) {
  191. aspi->remainder = aspi->count %
  192. ARK_ECSPI_CTRL_MAX_BURST;
  193. val = ARK_ECSPI_CTRL_MAX_BURST * 8 - 1;
  194. } else if (aspi->count >= sizeof(u32)) {
  195. aspi->remainder = aspi->count % sizeof(u32);
  196. val = (aspi->count - aspi->remainder) * 8 - 1;
  197. } else {
  198. aspi->remainder = 0;
  199. val = aspi->bits_per_word - 1;
  200. aspi->read_u32 = 0;
  201. }
  202. ctrl |= (val << ARK_ECSPI_CTRL_BL_OFFSET);
  203. writel(ctrl, aspi->base + ARK_ECSPI_CTRL);
  204. }
  205. if (aspi->count >= sizeof(u32)) {
  206. ark_spi_buf_tx_swap_u32(aspi);
  207. return;
  208. }
  209. bytes_per_word = ark_spi_bytes_per_word(aspi->bits_per_word);
  210. if (bytes_per_word == 1)
  211. ark_spi_buf_tx_u8(aspi);
  212. else if (bytes_per_word == 2)
  213. ark_spi_buf_tx_u16(aspi);
  214. }
  215. /* ARK eCSPI */
  216. static unsigned int ark_ecspi_clkdiv(struct ark_ecspi_data *aspi,
  217. unsigned int fspi, unsigned int *fres)
  218. {
  219. /*
  220. * there are two 4-bit dividers, the pre-divider divides by
  221. * $pre, the post-divider by 2^$post
  222. */
  223. unsigned int pre, post;
  224. unsigned int fin = aspi->spi_clk;
  225. if (fspi > fin)
  226. return 0;
  227. post = fls(fin) - fls(fspi);
  228. if (fin > fspi << post)
  229. post++;
  230. /* now we have: (fin <= fspi << post) with post being minimal */
  231. post = configMAX(4U, post) - 4;
  232. if (post > 0xf) {
  233. TRACE_ERROR("cannot set clock freq: %u (base freq: %u)\n",
  234. fspi, fin);
  235. return 0xff;
  236. }
  237. pre = DIV_ROUND_UP(fin, fspi << post) - 1;
  238. TRACE_DEBUG("%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
  239. __func__, fin, fspi, post, pre);
  240. /* Resulting frequency for the SCLK line. */
  241. *fres = (fin / (pre + 1)) >> post;
  242. return (pre << ARK_ECSPI_CTRL_PREDIV_OFFSET) |
  243. (post << ARK_ECSPI_CTRL_POSTDIV_OFFSET);
  244. }
  245. static void ark_ecspi_intctrl(struct ark_ecspi_data *aspi, int enable)
  246. {
  247. unsigned val = 0;
  248. if (enable & ARK_INT_TE)
  249. val |= ARK_ECSPI_INT_TEEN;
  250. if (enable & ARK_INT_RR)
  251. val |= ARK_ECSPI_INT_RREN;
  252. if (enable & ARK_INT_TC)
  253. val |= ARK_ECSPI_INT_TCEN;
  254. if (enable & ARK_INT_TF)
  255. val |= ARK_ECSPI_INT_TFEN;
  256. if (enable & ARK_INT_TR)
  257. val |= ARK_ECSPI_INT_TREN;
  258. writel(val, aspi->base + ARK_ECSPI_INT);
  259. }
  260. static void ark_ecspi_trigger(struct ark_ecspi_data *aspi)
  261. {
  262. u32 reg;
  263. reg = readl(aspi->base + ARK_ECSPI_CTRL);
  264. reg |= ARK_ECSPI_CTRL_XCH;
  265. writel(reg, aspi->base + ARK_ECSPI_CTRL);
  266. }
  267. static int ark_ecspi_config(struct ark_ecspi_data *aspi)
  268. {
  269. unsigned int ctrl = ARK_ECSPI_CTRL_ENABLE;
  270. unsigned int clk = aspi->speed_hz, delay, reg;
  271. unsigned int cfg = readl(aspi->base + ARK_ECSPI_CONFIG);
  272. unsigned int chip_select = aspi->slave.cs;
  273. /*
  274. * The hardware seems to have a race condition when changing modes. The
  275. * current assumption is that the selection of the channel arrives
  276. * earlier in the hardware than the mode bits when they are written at
  277. * the same time.
  278. * So set master mode for all channels as we do not support slave mode.
  279. */
  280. #ifndef SPI1_SLAVE_MODE
  281. ctrl |= ARK_ECSPI_CTRL_MODE_MASK;
  282. #endif
  283. /*
  284. * Enable SPI_RDY handling (falling edge/level triggered).
  285. */
  286. if (aspi->slave.mode & SPI_READY)
  287. ctrl |= ARK_ECSPI_CTRL_DRCTL(aspi->spi_drctl);
  288. /* set clock speed */
  289. ctrl |= ark_ecspi_clkdiv(aspi, aspi->speed_hz, &clk);
  290. aspi->spi_bus_clk = clk;
  291. /* set chip select to use */
  292. ctrl |= ARK_ECSPI_CTRL_CS(chip_select);
  293. if (aspi->usedma) {
  294. #ifdef SPI1_SLAVE_MODE
  295. if (aspi->dma_message.send_buf)
  296. ctrl |= (aspi->dma_message.length * 8 - 1) << ARK_ECSPI_CTRL_BL_OFFSET;
  297. else
  298. ctrl |= (32 - 1) << ARK_ECSPI_CTRL_BL_OFFSET;
  299. #else
  300. ctrl |= (32 - 1) << ARK_ECSPI_CTRL_BL_OFFSET;
  301. #endif
  302. } else {
  303. ctrl |= (aspi->bits_per_word - 1) << ARK_ECSPI_CTRL_BL_OFFSET;
  304. }
  305. cfg |= ARK_ECSPI_CONFIG_SBBCTRL(chip_select);
  306. if (aspi->slave.mode & SPI_CPHA)
  307. cfg |= ARK_ECSPI_CONFIG_SCLKPHA(chip_select);
  308. else
  309. cfg &= ~ARK_ECSPI_CONFIG_SCLKPHA(chip_select);
  310. if (aspi->slave.mode & SPI_CPOL) {
  311. cfg |= ARK_ECSPI_CONFIG_SCLKPOL(chip_select);
  312. cfg |= ARK_ECSPI_CONFIG_SCLKCTL(chip_select);
  313. } else {
  314. cfg &= ~ARK_ECSPI_CONFIG_SCLKPOL(chip_select);
  315. cfg &= ~ARK_ECSPI_CONFIG_SCLKCTL(chip_select);
  316. }
  317. if (aspi->slave.mode & SPI_CS_HIGH)
  318. cfg |= ARK_ECSPI_CONFIG_SSBPOL(chip_select);
  319. else
  320. cfg &= ~ARK_ECSPI_CONFIG_SSBPOL(chip_select);
  321. if (aspi->usedma) {
  322. ctrl |= ARK_ECSPI_CTRL_SMC;
  323. }
  324. /* CTRL register always go first to bring out controller from reset */
  325. writel(ctrl, aspi->base + ARK_ECSPI_CTRL);
  326. reg = readl(aspi->base + ARK_ECSPI_TESTREG);
  327. if (aspi->slave.mode & SPI_LOOP)
  328. reg |= ARK_ECSPI_TESTREG_LBC;
  329. else
  330. reg &= ~ARK_ECSPI_TESTREG_LBC;
  331. writel(reg, aspi->base + ARK_ECSPI_TESTREG);
  332. writel(cfg, aspi->base + ARK_ECSPI_CONFIG);
  333. /*
  334. * Wait until the changes in the configuration register CONFIGREG
  335. * propagate into the hardware. It takes exactly one tick of the
  336. * SCLK clock, but we will wait two SCLK clock just to be sure. The
  337. * effect of the delay it takes for the hardware to apply changes
  338. * is noticable if the SCLK clock run very slow. In such a case, if
  339. * the polarity of SCLK should be inverted, the GPIO ChipSelect might
  340. * be asserted before the SCLK polarity changes, which would disrupt
  341. * the SPI communication as the device on the other end would consider
  342. * the change of SCLK polarity as a clock tick already.
  343. */
  344. delay = (2 * 1000000) / clk;
  345. if (delay < 10) /* SCLK is faster than 100 kHz */
  346. udelay(delay);
  347. else /* SCLK is _very_ slow */
  348. udelay(delay + 10);
  349. /* enable rx fifo */
  350. writel(ARK_ECSPI_STAT_REN, aspi->base + ARK_ECSPI_STAT);
  351. /*
  352. * Configure the DMA register: setup the watermark
  353. * and enable DMA request.
  354. */
  355. if (aspi->usedma)
  356. writel(ARK_ECSPI_DMA_RX_WML(aspi->wml) |
  357. ARK_ECSPI_DMA_TX_WML(aspi->wml) |
  358. ARK_ECSPI_DMA_RXT_WML(aspi->wml) |
  359. ARK_ECSPI_DMA_TEDEN | ARK_ECSPI_DMA_RXDEN |
  360. ARK_ECSPI_DMA_RXTDEN, aspi->base + ARK_ECSPI_DMA);
  361. else
  362. writel(ARK_ECSPI_DMA_RX_WML(aspi->wml) |
  363. ARK_ECSPI_DMA_TX_WML(aspi->wml),
  364. aspi->base + ARK_ECSPI_DMA);
  365. return 0;
  366. }
  367. static int ark_ecspi_rx_available(struct ark_ecspi_data *aspi)
  368. {
  369. return readl(aspi->base + ARK_ECSPI_STAT) & ARK_ECSPI_STAT_RR;
  370. }
  371. static void ark_ecspi_reset(struct ark_ecspi_data *aspi)
  372. {
  373. /* drain receive buffer */
  374. /* while (ark_ecspi_rx_available(aspi))
  375. readl(aspi->base + ARK_ECSPI_RXDATA); */
  376. unsigned int ctrl = readl(aspi->base + ARK_ECSPI_CTRL);
  377. ctrl &= ~ARK_ECSPI_CTRL_ENABLE;
  378. writel(ctrl, aspi->base + ARK_ECSPI_CTRL);
  379. udelay(10);
  380. ctrl |= ARK_ECSPI_CTRL_ENABLE;
  381. writel(ctrl, aspi->base + ARK_ECSPI_CTRL);
  382. }
  383. static struct ark_spi_devtype_data ark_ecspi_devtype_data = {
  384. .intctrl = ark_ecspi_intctrl,
  385. .config = ark_ecspi_config,
  386. .trigger = ark_ecspi_trigger,
  387. .rx_available = ark_ecspi_rx_available,
  388. .reset = ark_ecspi_reset,
  389. .fifo_size = 64,
  390. .has_dmamode = false,
  391. };
  392. static void ark_spi_chipselect(struct ark_ecspi_data *aspi, int is_active)
  393. {
  394. int dev_is_lowactive = !(aspi->slave.mode & SPI_CS_HIGH);
  395. if (aspi->slave.mode & SPI_NO_CS)
  396. return;
  397. gpio_direction_output(aspi->cs_gpio, is_active ^ dev_is_lowactive);
  398. }
  399. static void ark_spi_push(struct ark_ecspi_data *aspi)
  400. {
  401. while (aspi->txfifo < aspi->devtype_data->fifo_size) {
  402. if (!aspi->count)
  403. break;
  404. if (aspi->txfifo && (aspi->count == aspi->remainder))
  405. break;
  406. aspi->tx(aspi);
  407. aspi->txfifo++;
  408. }
  409. aspi->devtype_data->trigger(aspi);
  410. }
  411. static void ark_spi_isr(void *param)
  412. {
  413. struct ark_ecspi_data *aspi = param;
  414. while (aspi->devtype_data->rx_available(aspi)) {
  415. aspi->rx(aspi);
  416. aspi->txfifo--;
  417. }
  418. if (aspi->count) {
  419. ark_spi_push(aspi);
  420. return;
  421. }
  422. if (aspi->txfifo) {
  423. /* No data left to push, but still waiting for rx data,
  424. * enable receive data available interrupt.
  425. */
  426. aspi->devtype_data->intctrl(
  427. aspi, ARK_INT_RR);
  428. return;
  429. }
  430. aspi->devtype_data->intctrl(aspi, 0);
  431. xQueueSendFromISR(aspi->xfer_done, NULL, 0);
  432. }
  433. static int ark_spi_setupxfer(struct ark_ecspi_data *aspi,
  434. struct spi_configuration *configuration)
  435. {
  436. u32 mask;
  437. if (!configuration)
  438. return 0;
  439. aspi->slave.mode = configuration->mode;
  440. aspi->bits_per_word = configuration->data_width;
  441. aspi->speed_hz = configuration->max_hz;
  442. /* Initialize the functions for transfer */
  443. aspi->remainder = 0;
  444. aspi->read_u32 = 1;
  445. mask = (1 << aspi->bits_per_word) - 1;
  446. aspi->rx = ark_spi_buf_rx_swap;
  447. aspi->tx = ark_spi_buf_tx_swap;
  448. if (aspi->bits_per_word <= 8)
  449. aspi->word_mask = mask << 24 | mask << 16
  450. | mask << 8 | mask;
  451. else if (aspi->bits_per_word <= 16)
  452. aspi->word_mask = mask << 16 | mask;
  453. else
  454. aspi->word_mask = mask;
  455. // aspi->devtype_data->config(aspi);
  456. return 0;
  457. }
  458. static int ark_spi_calculate_timeout(struct ark_ecspi_data *aspi, int size)
  459. {
  460. unsigned long timeout = 0;
  461. /* Time with actual data transfer and CS change delay related to HW */
  462. timeout = (8 + 4) * size / aspi->spi_bus_clk;
  463. /* Add extra second for scheduler related activities */
  464. timeout += 1;
  465. /* Double calculated timeout */
  466. return pdMS_TO_TICKS(2 * timeout * MSEC_PER_SEC);
  467. }
  468. static bool ark_spi_can_dma(struct ark_ecspi_data *aspi, struct spi_message *transfer)
  469. {
  470. const u32 mszs[] = {1, 4, 8, 16};
  471. int idx = ARRAY_SIZE(mszs) - 1;
  472. struct spi_message *dma_xfer = &aspi->dma_message;
  473. struct spi_message *pio_xfer = &aspi->pio_message;
  474. int len, remainder;
  475. if (!aspi->dma_rx)
  476. return false;
  477. pio_xfer->length = 0;
  478. memcpy(dma_xfer, transfer, sizeof(struct spi_message));
  479. remainder = transfer->length & 3;
  480. len = transfer->length - remainder;
  481. if ((len < USE_DMA_MIN_THRESHOLD) || (len > USE_DMA_MAX_THRESHOLD))
  482. return false;
  483. if ((u32)transfer->send_buf & 3 || (u32)transfer->recv_buf & 3)
  484. return false;
  485. if (remainder) {
  486. dma_xfer->length = len;
  487. memcpy(pio_xfer, transfer, sizeof(struct spi_message));
  488. pio_xfer->length = remainder;
  489. if (pio_xfer->send_buf)
  490. pio_xfer->send_buf = (u8*)pio_xfer->send_buf + len;
  491. if (pio_xfer->recv_buf)
  492. pio_xfer->recv_buf = (u8*)pio_xfer->recv_buf + len;
  493. }
  494. /* dw dma busrt should be 16,8,4,1 */
  495. for (; idx >= 0; idx--) {
  496. if (!(len % (mszs[idx] * 4)))
  497. break;
  498. }
  499. aspi->wml = mszs[idx];
  500. return true;
  501. }
  502. static void ark_spi_sdma_exit(struct ark_ecspi_data *aspi)
  503. {
  504. if (aspi->dma_rx) {
  505. dma_release_channel(aspi->dma_rx);
  506. aspi->dma_rx = NULL;
  507. }
  508. if (aspi->dma_tx) {
  509. dma_release_channel(aspi->dma_tx);
  510. aspi->dma_tx = NULL;
  511. }
  512. if (aspi->rx_dummy_buffer) {
  513. vPortFree(aspi->rx_dummy_buffer);
  514. aspi->rx_dummy_buffer = NULL;
  515. }
  516. if (aspi->tx_dummy_buffer) {
  517. vPortFree(aspi->tx_dummy_buffer);
  518. aspi->tx_dummy_buffer = NULL;
  519. }
  520. }
  521. static int ark_spi_sdma_init(struct ark_ecspi_data *aspi)
  522. {
  523. int ret;
  524. /* Prepare for TX DMA: */
  525. aspi->dma_tx = dma_request_channel(ECSPI_TX_DMA_CH);
  526. if (IS_ERR(aspi->dma_tx)) {
  527. ret = PTR_ERR(aspi->dma_tx);
  528. TRACE_DEBUG("can't get the TX DMA channel, error %d!\n", ret);
  529. aspi->dma_tx = NULL;
  530. goto err;
  531. }
  532. /* Prepare for RX : */
  533. aspi->dma_rx = dma_request_channel(ECSPI_RX_DMA_CH);
  534. if (IS_ERR(aspi->dma_rx)) {
  535. ret = PTR_ERR(aspi->dma_rx);
  536. TRACE_DEBUG("can't get the RX DMA channel, error %d\n", ret);
  537. aspi->dma_rx = NULL;
  538. goto err;
  539. }
  540. aspi->rx_dummy_buffer = pvPortMalloc(MALLOC_DMA_MEM_SIZE);
  541. if (!aspi->rx_dummy_buffer) {
  542. ret = -ENOMEM;
  543. goto err;
  544. }
  545. aspi->tx_dummy_buffer = pvPortMalloc(MALLOC_DMA_MEM_SIZE);
  546. if (!aspi->tx_dummy_buffer) {
  547. ret = -ENOMEM;
  548. goto err;
  549. }
  550. aspi->dma_rx_completion = xQueueCreate(1, 0);
  551. aspi->dma_tx_completion = xQueueCreate(1, 0);
  552. return 0;
  553. err:
  554. ark_spi_sdma_exit(aspi);
  555. return ret;
  556. }
  557. static void ark_spi_dma_rx_callback(void *cookie, unsigned mask)
  558. {
  559. struct ark_ecspi_data *aspi = (struct ark_ecspi_data *)cookie;
  560. struct spi_message *dma_message = &aspi->dma_message;
  561. /* Invalidate cache after read */
  562. /* rx_dummy_buffer shoule align to CACHE_LINE_SIZE */
  563. CP15_invalidate_dcache_for_dma((u32)aspi->rx_dummy_buffer,
  564. (u32)aspi->rx_dummy_buffer + dma_message->length);
  565. if (dma_message->recv_buf)
  566. memcpy(dma_message->recv_buf, aspi->rx_dummy_buffer, dma_message->length);
  567. xQueueSendFromISR(aspi->dma_rx_completion, NULL, 0);
  568. }
  569. static void ark_spi_dma_tx_callback(void *cookie, unsigned int mask)
  570. {
  571. struct ark_ecspi_data *aspi = (struct ark_ecspi_data *)cookie;
  572. xQueueSendFromISR(aspi->dma_tx_completion, NULL, 0);
  573. }
  574. static int ark_spi_dma_transfer(struct ark_ecspi_data *aspi, struct spi_message *message)
  575. {
  576. struct dma_config rx = {0}, tx = {0};
  577. unsigned long transfer_timeout;
  578. rx.direction = DMA_DEV_TO_MEM;
  579. rx.src_id = SPI1_RX;
  580. rx.src_addr = aspi->base + ARK_ECSPI_RXDATA;
  581. rx.dst_addr = (unsigned int)aspi->rx_dummy_buffer;
  582. rx.dst_addr_width = rx.src_addr_width = DMA_BUSWIDTH_4_BYTES;
  583. rx.src_maxburst = rx.dst_maxburst = aspi->wml;
  584. rx.transfer_size = message->length;
  585. rx.dst_master_id = 0;
  586. rx.src_master_id = 0;
  587. dma_config_channel(aspi->dma_rx, &rx);
  588. dma_register_complete_callback(aspi->dma_rx, ark_spi_dma_rx_callback, aspi);
  589. tx.direction = DMA_MEM_TO_DEV;
  590. tx.dst_id = SPI1_TX;
  591. tx.src_addr = (unsigned int)aspi->tx_dummy_buffer;
  592. tx.dst_addr = aspi->base + ARK_ECSPI_TXDATA;
  593. tx.dst_addr_width = tx.src_addr_width = DMA_BUSWIDTH_4_BYTES;
  594. tx.src_maxburst = tx.dst_maxburst = aspi->wml;
  595. tx.transfer_size = message->length;
  596. tx.dst_master_id = 0;
  597. tx.src_master_id = 0;
  598. dma_config_channel(aspi->dma_tx, &tx);
  599. dma_register_complete_callback(aspi->dma_tx, ark_spi_dma_tx_callback, aspi);
  600. xQueueReset(aspi->dma_rx_completion);
  601. dma_start_channel(aspi->dma_rx);
  602. memset(aspi->tx_dummy_buffer, 0xff, message->length);
  603. if (message->send_buf)
  604. memcpy(aspi->tx_dummy_buffer, message->send_buf, message->length);
  605. xQueueReset(aspi->dma_tx_completion);
  606. /* Flush cache before write */
  607. CP15_flush_dcache_for_dma((u32)aspi->tx_dummy_buffer,
  608. (u32)aspi->tx_dummy_buffer + message->length);
  609. dma_start_channel(aspi->dma_tx);
  610. transfer_timeout = ark_spi_calculate_timeout(aspi, message->length);
  611. /* Wait SDMA to finish the data transfer.*/
  612. if (xQueueReceive(aspi->dma_tx_completion, NULL,
  613. transfer_timeout) != pdTRUE) {
  614. printf("I/O Error in DMA TX\n");
  615. dma_stop_channel(aspi->dma_tx);
  616. dma_stop_channel(aspi->dma_rx);
  617. return -ETIMEDOUT;
  618. }
  619. if (xQueueReceive(aspi->dma_rx_completion, NULL,
  620. transfer_timeout) != pdTRUE) {
  621. printf("I/O Error in DMA RX\n");
  622. aspi->devtype_data->reset(aspi);
  623. dma_stop_channel(aspi->dma_rx);
  624. return -ETIMEDOUT;
  625. }
  626. return message->length;
  627. }
  628. static int ark_spi_pio_xfer(struct ark_ecspi_data *aspi, struct spi_message *message)
  629. {
  630. unsigned long transfer_timeout;
  631. int ret;
  632. void *tx_dummy_buf = NULL;
  633. void *rx_dummy_buf = NULL;
  634. if ((unsigned int)message->send_buf & 3) {
  635. tx_dummy_buf = pvPortMalloc(message->length);
  636. if (!tx_dummy_buf) return -ENOMEM;
  637. aspi->tx_buf = tx_dummy_buf;
  638. memcpy(tx_dummy_buf, message->send_buf, message->length);
  639. } else {
  640. aspi->tx_buf = message->send_buf;
  641. }
  642. if ((unsigned int)message->recv_buf & 3) {
  643. rx_dummy_buf = pvPortMalloc(message->length);
  644. if (!rx_dummy_buf) return -ENOMEM;
  645. aspi->rx_buf = rx_dummy_buf;
  646. } else {
  647. aspi->rx_buf = message->recv_buf;
  648. }
  649. aspi->remainder = aspi->count = message->length;
  650. aspi->read_u32 = 1;
  651. aspi->txfifo = 0;
  652. xQueueReset(aspi->xfer_done);
  653. ark_spi_push(aspi);
  654. aspi->devtype_data->intctrl(aspi, ARK_INT_TR);
  655. transfer_timeout = ark_spi_calculate_timeout(aspi, message->length);
  656. if (xQueueReceive(aspi->xfer_done, NULL, transfer_timeout) != pdTRUE) {
  657. TRACE_ERROR("I/O Error in PIO\n");
  658. aspi->devtype_data->reset(aspi);
  659. if (message->cs_release)
  660. ark_spi_chipselect(aspi, 0);
  661. ret = -ETIMEDOUT;
  662. } else {
  663. if (rx_dummy_buf)
  664. memcpy(message->recv_buf, rx_dummy_buf, message->length);
  665. ret = message->length;
  666. }
  667. if (rx_dummy_buf)
  668. vPortFree(rx_dummy_buf);
  669. if (tx_dummy_buf)
  670. vPortFree(tx_dummy_buf);
  671. return ret;
  672. }
  673. static int ecspi_configure(struct spi_slave *slave, struct spi_configuration *configuration)
  674. {
  675. struct ark_ecspi_data *aspi = (struct ark_ecspi_data *)slave;
  676. return ark_spi_setupxfer(aspi, configuration);
  677. }
  678. static int ecspi_xfer(struct spi_slave *slave, struct spi_message *message)
  679. {
  680. struct ark_ecspi_data *aspi = (struct ark_ecspi_data *)slave;
  681. int ret = 0;
  682. ark_ecspi_reset(aspi);
  683. #ifndef SPI1_SLAVE_MODE
  684. if (message->cs_take)
  685. ark_spi_chipselect(aspi, 1);
  686. #endif
  687. if (ark_spi_can_dma(aspi, message))
  688. aspi->usedma = 1;
  689. else
  690. aspi->usedma = 0;
  691. aspi->devtype_data->config(aspi);
  692. if (aspi->usedma) {
  693. if ((ret = ark_spi_dma_transfer(aspi, &aspi->dma_message)) < 0)
  694. goto end;
  695. if (aspi->pio_message.length > 0 &&
  696. (ret = ark_spi_pio_xfer(aspi, &aspi->pio_message)) < 0)
  697. goto end;
  698. ret = message->length;
  699. goto end;
  700. }
  701. ret = ark_spi_pio_xfer(aspi, message);
  702. end:
  703. #ifndef SPI1_SLAVE_MODE
  704. if (message->cs_release)
  705. ark_spi_chipselect(aspi, 0);
  706. #endif
  707. return ret;
  708. };
  709. static int ark_ecspi_probe(struct ark_ecspi_data *aspi, char *spi_bus_name)
  710. {
  711. int ret;
  712. aspi->devtype_data = &ark_ecspi_devtype_data;
  713. aspi->xfer_done = xQueueCreate(1, 0);
  714. request_irq(aspi->irq, 0, ark_spi_isr, aspi);
  715. aspi->wml = aspi->devtype_data->fifo_size / 2;
  716. if (aspi->devtype_data->has_dmamode) {
  717. ret = ark_spi_sdma_init(aspi);
  718. if (ret < 0)
  719. TRACE_ERROR("dma setup error %d, use pio\n", ret);
  720. }
  721. aspi->devtype_data->reset(aspi);
  722. aspi->devtype_data->intctrl(aspi, 0);
  723. strncpy(aspi->slave.name, spi_bus_name, 16);
  724. spi_add_slave(&aspi->slave);
  725. return 0;
  726. }
  727. int ecspi_init(void)
  728. {
  729. struct ark_ecspi_data *aspi1 = pvPortMalloc(sizeof(struct ark_ecspi_data));
  730. if (!aspi1)
  731. return -ENOMEM;
  732. memset(aspi1, 0, sizeof(struct ark_ecspi_data));
  733. sys_soft_reset(softreset_ssp1);
  734. aspi1->base = REGS_SPI1_BASE;
  735. aspi1->irq = SPI1_IRQn;
  736. aspi1->spi_clk = ulClkGetRate(CLK_SPI1);
  737. aspi1->cs_gpio = SPI1_CS0_GPIO;
  738. aspi1->slave.mode = SPI_MODE_0;
  739. aspi1->slave.cs = 0;
  740. aspi1->slave.xfer = ecspi_xfer;
  741. aspi1->slave.configure = ecspi_configure;
  742. ark_ecspi_probe(aspi1, "ec_spi1");
  743. return 0;
  744. }