ecspi.c 22 KB

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