spi-qup.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. * Copyright (c) 2008-2014, The Linux foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License rev 2 and
  6. * only rev 2 as published by the free Software foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or fITNESS fOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/dmaengine.h>
  26. #include <linux/dma-mapping.h>
  27. #define QUP_CONFIG 0x0000
  28. #define QUP_STATE 0x0004
  29. #define QUP_IO_M_MODES 0x0008
  30. #define QUP_SW_RESET 0x000c
  31. #define QUP_OPERATIONAL 0x0018
  32. #define QUP_ERROR_FLAGS 0x001c
  33. #define QUP_ERROR_FLAGS_EN 0x0020
  34. #define QUP_OPERATIONAL_MASK 0x0028
  35. #define QUP_HW_VERSION 0x0030
  36. #define QUP_MX_OUTPUT_CNT 0x0100
  37. #define QUP_OUTPUT_FIFO 0x0110
  38. #define QUP_MX_WRITE_CNT 0x0150
  39. #define QUP_MX_INPUT_CNT 0x0200
  40. #define QUP_MX_READ_CNT 0x0208
  41. #define QUP_INPUT_FIFO 0x0218
  42. #define SPI_CONFIG 0x0300
  43. #define SPI_IO_CONTROL 0x0304
  44. #define SPI_ERROR_FLAGS 0x0308
  45. #define SPI_ERROR_FLAGS_EN 0x030c
  46. /* QUP_CONFIG fields */
  47. #define QUP_CONFIG_SPI_MODE (1 << 8)
  48. #define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13)
  49. #define QUP_CONFIG_NO_INPUT BIT(7)
  50. #define QUP_CONFIG_NO_OUTPUT BIT(6)
  51. #define QUP_CONFIG_N 0x001f
  52. /* QUP_STATE fields */
  53. #define QUP_STATE_VALID BIT(2)
  54. #define QUP_STATE_RESET 0
  55. #define QUP_STATE_RUN 1
  56. #define QUP_STATE_PAUSE 3
  57. #define QUP_STATE_MASK 3
  58. #define QUP_STATE_CLEAR 2
  59. #define QUP_HW_VERSION_2_1_1 0x20010001
  60. /* QUP_IO_M_MODES fields */
  61. #define QUP_IO_M_PACK_EN BIT(15)
  62. #define QUP_IO_M_UNPACK_EN BIT(14)
  63. #define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12
  64. #define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10
  65. #define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT)
  66. #define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT)
  67. #define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0)
  68. #define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2)
  69. #define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5)
  70. #define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7)
  71. #define QUP_IO_M_MODE_FIFO 0
  72. #define QUP_IO_M_MODE_BLOCK 1
  73. #define QUP_IO_M_MODE_DMOV 2
  74. #define QUP_IO_M_MODE_BAM 3
  75. /* QUP_OPERATIONAL fields */
  76. #define QUP_OP_IN_BLOCK_READ_REQ BIT(13)
  77. #define QUP_OP_OUT_BLOCK_WRITE_REQ BIT(12)
  78. #define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11)
  79. #define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10)
  80. #define QUP_OP_IN_SERVICE_FLAG BIT(9)
  81. #define QUP_OP_OUT_SERVICE_FLAG BIT(8)
  82. #define QUP_OP_IN_FIFO_FULL BIT(7)
  83. #define QUP_OP_OUT_FIFO_FULL BIT(6)
  84. #define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5)
  85. #define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4)
  86. /* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */
  87. #define QUP_ERROR_OUTPUT_OVER_RUN BIT(5)
  88. #define QUP_ERROR_INPUT_UNDER_RUN BIT(4)
  89. #define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3)
  90. #define QUP_ERROR_INPUT_OVER_RUN BIT(2)
  91. /* SPI_CONFIG fields */
  92. #define SPI_CONFIG_HS_MODE BIT(10)
  93. #define SPI_CONFIG_INPUT_FIRST BIT(9)
  94. #define SPI_CONFIG_LOOPBACK BIT(8)
  95. /* SPI_IO_CONTROL fields */
  96. #define SPI_IO_C_FORCE_CS BIT(11)
  97. #define SPI_IO_C_CLK_IDLE_HIGH BIT(10)
  98. #define SPI_IO_C_MX_CS_MODE BIT(8)
  99. #define SPI_IO_C_CS_N_POLARITY_0 BIT(4)
  100. #define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2)
  101. #define SPI_IO_C_CS_SELECT_MASK 0x000c
  102. #define SPI_IO_C_TRISTATE_CS BIT(1)
  103. #define SPI_IO_C_NO_TRI_STATE BIT(0)
  104. /* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */
  105. #define SPI_ERROR_CLK_OVER_RUN BIT(1)
  106. #define SPI_ERROR_CLK_UNDER_RUN BIT(0)
  107. #define SPI_NUM_CHIPSELECTS 4
  108. #define SPI_MAX_XFER (SZ_64K - 64)
  109. /* high speed mode is when bus rate is greater then 26MHz */
  110. #define SPI_HS_MIN_RATE 26000000
  111. #define SPI_MAX_RATE 50000000
  112. #define SPI_DELAY_THRESHOLD 1
  113. #define SPI_DELAY_RETRY 10
  114. struct spi_qup {
  115. void __iomem *base;
  116. struct device *dev;
  117. struct clk *cclk; /* core clock */
  118. struct clk *iclk; /* interface clock */
  119. int irq;
  120. spinlock_t lock;
  121. int in_fifo_sz;
  122. int out_fifo_sz;
  123. int in_blk_sz;
  124. int out_blk_sz;
  125. struct spi_transfer *xfer;
  126. struct completion done;
  127. int error;
  128. int w_size; /* bytes per SPI word */
  129. int n_words;
  130. int tx_bytes;
  131. int rx_bytes;
  132. const u8 *tx_buf;
  133. u8 *rx_buf;
  134. int qup_v1;
  135. int mode;
  136. struct dma_slave_config rx_conf;
  137. struct dma_slave_config tx_conf;
  138. };
  139. static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer);
  140. static inline bool spi_qup_is_flag_set(struct spi_qup *controller, u32 flag)
  141. {
  142. u32 opflag = readl_relaxed(controller->base + QUP_OPERATIONAL);
  143. return (opflag & flag) != 0;
  144. }
  145. static inline bool spi_qup_is_dma_xfer(int mode)
  146. {
  147. if (mode == QUP_IO_M_MODE_DMOV || mode == QUP_IO_M_MODE_BAM)
  148. return true;
  149. return false;
  150. }
  151. /* get's the transaction size length */
  152. static inline unsigned int spi_qup_len(struct spi_qup *controller)
  153. {
  154. return controller->n_words * controller->w_size;
  155. }
  156. static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
  157. {
  158. u32 opstate = readl_relaxed(controller->base + QUP_STATE);
  159. return opstate & QUP_STATE_VALID;
  160. }
  161. static int spi_qup_set_state(struct spi_qup *controller, u32 state)
  162. {
  163. unsigned long loop;
  164. u32 cur_state;
  165. loop = 0;
  166. while (!spi_qup_is_valid_state(controller)) {
  167. usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
  168. if (++loop > SPI_DELAY_RETRY)
  169. return -EIO;
  170. }
  171. if (loop)
  172. dev_dbg(controller->dev, "invalid state for %ld,us %d\n",
  173. loop, state);
  174. cur_state = readl_relaxed(controller->base + QUP_STATE);
  175. /*
  176. * Per spec: for PAUSE_STATE to RESET_STATE, two writes
  177. * of (b10) are required
  178. */
  179. if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) &&
  180. (state == QUP_STATE_RESET)) {
  181. writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
  182. writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
  183. } else {
  184. cur_state &= ~QUP_STATE_MASK;
  185. cur_state |= state;
  186. writel_relaxed(cur_state, controller->base + QUP_STATE);
  187. }
  188. loop = 0;
  189. while (!spi_qup_is_valid_state(controller)) {
  190. usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
  191. if (++loop > SPI_DELAY_RETRY)
  192. return -EIO;
  193. }
  194. return 0;
  195. }
  196. static void spi_qup_read_from_fifo(struct spi_qup *controller, u32 num_words)
  197. {
  198. u8 *rx_buf = controller->rx_buf;
  199. int i, shift, num_bytes;
  200. u32 word;
  201. for (; num_words; num_words--) {
  202. word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
  203. num_bytes = min_t(int, spi_qup_len(controller) -
  204. controller->rx_bytes,
  205. controller->w_size);
  206. if (!rx_buf) {
  207. controller->rx_bytes += num_bytes;
  208. continue;
  209. }
  210. for (i = 0; i < num_bytes; i++, controller->rx_bytes++) {
  211. /*
  212. * The data format depends on bytes per SPI word:
  213. * 4 bytes: 0x12345678
  214. * 2 bytes: 0x00001234
  215. * 1 byte : 0x00000012
  216. */
  217. shift = BITS_PER_BYTE;
  218. shift *= (controller->w_size - i - 1);
  219. rx_buf[controller->rx_bytes] = word >> shift;
  220. }
  221. }
  222. }
  223. static void spi_qup_read(struct spi_qup *controller, u32 *opflags)
  224. {
  225. u32 remainder, words_per_block, num_words;
  226. bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
  227. remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->rx_bytes,
  228. controller->w_size);
  229. words_per_block = controller->in_blk_sz >> 2;
  230. do {
  231. /* ACK by clearing service flag */
  232. writel_relaxed(QUP_OP_IN_SERVICE_FLAG,
  233. controller->base + QUP_OPERATIONAL);
  234. if (is_block_mode) {
  235. num_words = (remainder > words_per_block) ?
  236. words_per_block : remainder;
  237. } else {
  238. if (!spi_qup_is_flag_set(controller,
  239. QUP_OP_IN_FIFO_NOT_EMPTY))
  240. break;
  241. num_words = 1;
  242. }
  243. /* read up to the maximum transfer size available */
  244. spi_qup_read_from_fifo(controller, num_words);
  245. remainder -= num_words;
  246. /* if block mode, check to see if next block is available */
  247. if (is_block_mode && !spi_qup_is_flag_set(controller,
  248. QUP_OP_IN_BLOCK_READ_REQ))
  249. break;
  250. } while (remainder);
  251. /*
  252. * Due to extra stickiness of the QUP_OP_IN_SERVICE_FLAG during block
  253. * reads, it has to be cleared again at the very end. However, be sure
  254. * to refresh opflags value because MAX_INPUT_DONE_FLAG may now be
  255. * present and this is used to determine if transaction is complete
  256. */
  257. *opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
  258. if (is_block_mode && *opflags & QUP_OP_MAX_INPUT_DONE_FLAG)
  259. writel_relaxed(QUP_OP_IN_SERVICE_FLAG,
  260. controller->base + QUP_OPERATIONAL);
  261. }
  262. static void spi_qup_write_to_fifo(struct spi_qup *controller, u32 num_words)
  263. {
  264. const u8 *tx_buf = controller->tx_buf;
  265. int i, num_bytes;
  266. u32 word, data;
  267. for (; num_words; num_words--) {
  268. word = 0;
  269. num_bytes = min_t(int, spi_qup_len(controller) -
  270. controller->tx_bytes,
  271. controller->w_size);
  272. if (tx_buf)
  273. for (i = 0; i < num_bytes; i++) {
  274. data = tx_buf[controller->tx_bytes + i];
  275. word |= data << (BITS_PER_BYTE * (3 - i));
  276. }
  277. controller->tx_bytes += num_bytes;
  278. writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO);
  279. }
  280. }
  281. static void spi_qup_dma_done(void *data)
  282. {
  283. struct spi_qup *qup = data;
  284. complete(&qup->done);
  285. }
  286. static void spi_qup_write(struct spi_qup *controller)
  287. {
  288. bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
  289. u32 remainder, words_per_block, num_words;
  290. remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->tx_bytes,
  291. controller->w_size);
  292. words_per_block = controller->out_blk_sz >> 2;
  293. do {
  294. /* ACK by clearing service flag */
  295. writel_relaxed(QUP_OP_OUT_SERVICE_FLAG,
  296. controller->base + QUP_OPERATIONAL);
  297. if (is_block_mode) {
  298. num_words = (remainder > words_per_block) ?
  299. words_per_block : remainder;
  300. } else {
  301. if (spi_qup_is_flag_set(controller,
  302. QUP_OP_OUT_FIFO_FULL))
  303. break;
  304. num_words = 1;
  305. }
  306. spi_qup_write_to_fifo(controller, num_words);
  307. remainder -= num_words;
  308. /* if block mode, check to see if next block is available */
  309. if (is_block_mode && !spi_qup_is_flag_set(controller,
  310. QUP_OP_OUT_BLOCK_WRITE_REQ))
  311. break;
  312. } while (remainder);
  313. }
  314. static int spi_qup_prep_sg(struct spi_master *master, struct scatterlist *sgl,
  315. unsigned int nents, enum dma_transfer_direction dir,
  316. dma_async_tx_callback callback)
  317. {
  318. struct spi_qup *qup = spi_master_get_devdata(master);
  319. unsigned long flags = DMA_PREP_INTERRUPT | DMA_PREP_FENCE;
  320. struct dma_async_tx_descriptor *desc;
  321. struct dma_chan *chan;
  322. dma_cookie_t cookie;
  323. if (dir == DMA_MEM_TO_DEV)
  324. chan = master->dma_tx;
  325. else
  326. chan = master->dma_rx;
  327. desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
  328. if (IS_ERR_OR_NULL(desc))
  329. return desc ? PTR_ERR(desc) : -EINVAL;
  330. desc->callback = callback;
  331. desc->callback_param = qup;
  332. cookie = dmaengine_submit(desc);
  333. return dma_submit_error(cookie);
  334. }
  335. static void spi_qup_dma_terminate(struct spi_master *master,
  336. struct spi_transfer *xfer)
  337. {
  338. if (xfer->tx_buf)
  339. dmaengine_terminate_all(master->dma_tx);
  340. if (xfer->rx_buf)
  341. dmaengine_terminate_all(master->dma_rx);
  342. }
  343. static u32 spi_qup_sgl_get_nents_len(struct scatterlist *sgl, u32 max,
  344. u32 *nents)
  345. {
  346. struct scatterlist *sg;
  347. u32 total = 0;
  348. *nents = 0;
  349. for (sg = sgl; sg; sg = sg_next(sg)) {
  350. unsigned int len = sg_dma_len(sg);
  351. /* check for overflow as well as limit */
  352. if (((total + len) < total) || ((total + len) > max))
  353. break;
  354. total += len;
  355. (*nents)++;
  356. }
  357. return total;
  358. }
  359. static int spi_qup_do_dma(struct spi_device *spi, struct spi_transfer *xfer,
  360. unsigned long timeout)
  361. {
  362. dma_async_tx_callback rx_done = NULL, tx_done = NULL;
  363. struct spi_master *master = spi->master;
  364. struct spi_qup *qup = spi_master_get_devdata(master);
  365. struct scatterlist *tx_sgl, *rx_sgl;
  366. int ret;
  367. if (xfer->rx_buf)
  368. rx_done = spi_qup_dma_done;
  369. else if (xfer->tx_buf)
  370. tx_done = spi_qup_dma_done;
  371. rx_sgl = xfer->rx_sg.sgl;
  372. tx_sgl = xfer->tx_sg.sgl;
  373. do {
  374. u32 rx_nents, tx_nents;
  375. if (rx_sgl)
  376. qup->n_words = spi_qup_sgl_get_nents_len(rx_sgl,
  377. SPI_MAX_XFER, &rx_nents) / qup->w_size;
  378. if (tx_sgl)
  379. qup->n_words = spi_qup_sgl_get_nents_len(tx_sgl,
  380. SPI_MAX_XFER, &tx_nents) / qup->w_size;
  381. if (!qup->n_words)
  382. return -EIO;
  383. ret = spi_qup_io_config(spi, xfer);
  384. if (ret)
  385. return ret;
  386. /* before issuing the descriptors, set the QUP to run */
  387. ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  388. if (ret) {
  389. dev_warn(qup->dev, "cannot set RUN state\n");
  390. return ret;
  391. }
  392. if (rx_sgl) {
  393. ret = spi_qup_prep_sg(master, rx_sgl, rx_nents,
  394. DMA_DEV_TO_MEM, rx_done);
  395. if (ret)
  396. return ret;
  397. dma_async_issue_pending(master->dma_rx);
  398. }
  399. if (tx_sgl) {
  400. ret = spi_qup_prep_sg(master, tx_sgl, tx_nents,
  401. DMA_MEM_TO_DEV, tx_done);
  402. if (ret)
  403. return ret;
  404. dma_async_issue_pending(master->dma_tx);
  405. }
  406. if (!wait_for_completion_timeout(&qup->done, timeout))
  407. return -ETIMEDOUT;
  408. for (; rx_sgl && rx_nents--; rx_sgl = sg_next(rx_sgl))
  409. ;
  410. for (; tx_sgl && tx_nents--; tx_sgl = sg_next(tx_sgl))
  411. ;
  412. } while (rx_sgl || tx_sgl);
  413. return 0;
  414. }
  415. static int spi_qup_do_pio(struct spi_device *spi, struct spi_transfer *xfer,
  416. unsigned long timeout)
  417. {
  418. struct spi_master *master = spi->master;
  419. struct spi_qup *qup = spi_master_get_devdata(master);
  420. int ret, n_words, iterations, offset = 0;
  421. n_words = qup->n_words;
  422. iterations = n_words / SPI_MAX_XFER; /* round down */
  423. qup->rx_buf = xfer->rx_buf;
  424. qup->tx_buf = xfer->tx_buf;
  425. do {
  426. if (iterations)
  427. qup->n_words = SPI_MAX_XFER;
  428. else
  429. qup->n_words = n_words % SPI_MAX_XFER;
  430. if (qup->tx_buf && offset)
  431. qup->tx_buf = xfer->tx_buf + offset * SPI_MAX_XFER;
  432. if (qup->rx_buf && offset)
  433. qup->rx_buf = xfer->rx_buf + offset * SPI_MAX_XFER;
  434. /*
  435. * if the transaction is small enough, we need
  436. * to fallback to FIFO mode
  437. */
  438. if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
  439. qup->mode = QUP_IO_M_MODE_FIFO;
  440. ret = spi_qup_io_config(spi, xfer);
  441. if (ret)
  442. return ret;
  443. ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  444. if (ret) {
  445. dev_warn(qup->dev, "cannot set RUN state\n");
  446. return ret;
  447. }
  448. ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
  449. if (ret) {
  450. dev_warn(qup->dev, "cannot set PAUSE state\n");
  451. return ret;
  452. }
  453. if (qup->mode == QUP_IO_M_MODE_FIFO)
  454. spi_qup_write(qup);
  455. ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  456. if (ret) {
  457. dev_warn(qup->dev, "cannot set RUN state\n");
  458. return ret;
  459. }
  460. if (!wait_for_completion_timeout(&qup->done, timeout))
  461. return -ETIMEDOUT;
  462. offset++;
  463. } while (iterations--);
  464. return 0;
  465. }
  466. static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
  467. {
  468. struct spi_qup *controller = dev_id;
  469. u32 opflags, qup_err, spi_err;
  470. int error = 0;
  471. qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS);
  472. spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS);
  473. opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
  474. writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS);
  475. writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS);
  476. if (qup_err) {
  477. if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN)
  478. dev_warn(controller->dev, "OUTPUT_OVER_RUN\n");
  479. if (qup_err & QUP_ERROR_INPUT_UNDER_RUN)
  480. dev_warn(controller->dev, "INPUT_UNDER_RUN\n");
  481. if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN)
  482. dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n");
  483. if (qup_err & QUP_ERROR_INPUT_OVER_RUN)
  484. dev_warn(controller->dev, "INPUT_OVER_RUN\n");
  485. error = -EIO;
  486. }
  487. if (spi_err) {
  488. if (spi_err & SPI_ERROR_CLK_OVER_RUN)
  489. dev_warn(controller->dev, "CLK_OVER_RUN\n");
  490. if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
  491. dev_warn(controller->dev, "CLK_UNDER_RUN\n");
  492. error = -EIO;
  493. }
  494. if (spi_qup_is_dma_xfer(controller->mode)) {
  495. writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
  496. } else {
  497. if (opflags & QUP_OP_IN_SERVICE_FLAG)
  498. spi_qup_read(controller, &opflags);
  499. if (opflags & QUP_OP_OUT_SERVICE_FLAG)
  500. spi_qup_write(controller);
  501. }
  502. if ((opflags & QUP_OP_MAX_INPUT_DONE_FLAG) || error)
  503. complete(&controller->done);
  504. return IRQ_HANDLED;
  505. }
  506. /* set clock freq ... bits per word, determine mode */
  507. static int spi_qup_io_prep(struct spi_device *spi, struct spi_transfer *xfer)
  508. {
  509. struct spi_qup *controller = spi_master_get_devdata(spi->master);
  510. int ret;
  511. if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
  512. dev_err(controller->dev, "too big size for loopback %d > %d\n",
  513. xfer->len, controller->in_fifo_sz);
  514. return -EIO;
  515. }
  516. ret = clk_set_rate(controller->cclk, xfer->speed_hz);
  517. if (ret) {
  518. dev_err(controller->dev, "fail to set frequency %d",
  519. xfer->speed_hz);
  520. return -EIO;
  521. }
  522. controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8);
  523. controller->n_words = xfer->len / controller->w_size;
  524. if (controller->n_words <= (controller->in_fifo_sz / sizeof(u32)))
  525. controller->mode = QUP_IO_M_MODE_FIFO;
  526. else if (spi->master->can_dma &&
  527. spi->master->can_dma(spi->master, spi, xfer) &&
  528. spi->master->cur_msg_mapped)
  529. controller->mode = QUP_IO_M_MODE_BAM;
  530. else
  531. controller->mode = QUP_IO_M_MODE_BLOCK;
  532. return 0;
  533. }
  534. /* prep qup for another spi transaction of specific type */
  535. static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer)
  536. {
  537. struct spi_qup *controller = spi_master_get_devdata(spi->master);
  538. u32 config, iomode, control;
  539. unsigned long flags;
  540. spin_lock_irqsave(&controller->lock, flags);
  541. controller->xfer = xfer;
  542. controller->error = 0;
  543. controller->rx_bytes = 0;
  544. controller->tx_bytes = 0;
  545. spin_unlock_irqrestore(&controller->lock, flags);
  546. if (spi_qup_set_state(controller, QUP_STATE_RESET)) {
  547. dev_err(controller->dev, "cannot set RESET state\n");
  548. return -EIO;
  549. }
  550. switch (controller->mode) {
  551. case QUP_IO_M_MODE_FIFO:
  552. writel_relaxed(controller->n_words,
  553. controller->base + QUP_MX_READ_CNT);
  554. writel_relaxed(controller->n_words,
  555. controller->base + QUP_MX_WRITE_CNT);
  556. /* must be zero for FIFO */
  557. writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
  558. writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  559. break;
  560. case QUP_IO_M_MODE_BAM:
  561. writel_relaxed(controller->n_words,
  562. controller->base + QUP_MX_INPUT_CNT);
  563. writel_relaxed(controller->n_words,
  564. controller->base + QUP_MX_OUTPUT_CNT);
  565. /* must be zero for BLOCK and BAM */
  566. writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  567. writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  568. if (!controller->qup_v1) {
  569. void __iomem *input_cnt;
  570. input_cnt = controller->base + QUP_MX_INPUT_CNT;
  571. /*
  572. * for DMA transfers, both QUP_MX_INPUT_CNT and
  573. * QUP_MX_OUTPUT_CNT must be zero to all cases but one.
  574. * That case is a non-balanced transfer when there is
  575. * only a rx_buf.
  576. */
  577. if (xfer->tx_buf)
  578. writel_relaxed(0, input_cnt);
  579. else
  580. writel_relaxed(controller->n_words, input_cnt);
  581. writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  582. }
  583. break;
  584. case QUP_IO_M_MODE_BLOCK:
  585. reinit_completion(&controller->done);
  586. writel_relaxed(controller->n_words,
  587. controller->base + QUP_MX_INPUT_CNT);
  588. writel_relaxed(controller->n_words,
  589. controller->base + QUP_MX_OUTPUT_CNT);
  590. /* must be zero for BLOCK and BAM */
  591. writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  592. writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  593. break;
  594. default:
  595. dev_err(controller->dev, "unknown mode = %d\n",
  596. controller->mode);
  597. return -EIO;
  598. }
  599. iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
  600. /* Set input and output transfer mode */
  601. iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
  602. if (!spi_qup_is_dma_xfer(controller->mode))
  603. iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
  604. else
  605. iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN;
  606. iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
  607. iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
  608. writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
  609. control = readl_relaxed(controller->base + SPI_IO_CONTROL);
  610. if (spi->mode & SPI_CPOL)
  611. control |= SPI_IO_C_CLK_IDLE_HIGH;
  612. else
  613. control &= ~SPI_IO_C_CLK_IDLE_HIGH;
  614. writel_relaxed(control, controller->base + SPI_IO_CONTROL);
  615. config = readl_relaxed(controller->base + SPI_CONFIG);
  616. if (spi->mode & SPI_LOOP)
  617. config |= SPI_CONFIG_LOOPBACK;
  618. else
  619. config &= ~SPI_CONFIG_LOOPBACK;
  620. if (spi->mode & SPI_CPHA)
  621. config &= ~SPI_CONFIG_INPUT_FIRST;
  622. else
  623. config |= SPI_CONFIG_INPUT_FIRST;
  624. /*
  625. * HS_MODE improves signal stability for spi-clk high rates,
  626. * but is invalid in loop back mode.
  627. */
  628. if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP))
  629. config |= SPI_CONFIG_HS_MODE;
  630. else
  631. config &= ~SPI_CONFIG_HS_MODE;
  632. writel_relaxed(config, controller->base + SPI_CONFIG);
  633. config = readl_relaxed(controller->base + QUP_CONFIG);
  634. config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N);
  635. config |= xfer->bits_per_word - 1;
  636. config |= QUP_CONFIG_SPI_MODE;
  637. if (spi_qup_is_dma_xfer(controller->mode)) {
  638. if (!xfer->tx_buf)
  639. config |= QUP_CONFIG_NO_OUTPUT;
  640. if (!xfer->rx_buf)
  641. config |= QUP_CONFIG_NO_INPUT;
  642. }
  643. writel_relaxed(config, controller->base + QUP_CONFIG);
  644. /* only write to OPERATIONAL_MASK when register is present */
  645. if (!controller->qup_v1) {
  646. u32 mask = 0;
  647. /*
  648. * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO
  649. * status change in BAM mode
  650. */
  651. if (spi_qup_is_dma_xfer(controller->mode))
  652. mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG;
  653. writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK);
  654. }
  655. return 0;
  656. }
  657. static int spi_qup_transfer_one(struct spi_master *master,
  658. struct spi_device *spi,
  659. struct spi_transfer *xfer)
  660. {
  661. struct spi_qup *controller = spi_master_get_devdata(master);
  662. unsigned long timeout, flags;
  663. int ret = -EIO;
  664. ret = spi_qup_io_prep(spi, xfer);
  665. if (ret)
  666. return ret;
  667. timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
  668. timeout = DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER,
  669. xfer->len) * 8, timeout);
  670. timeout = 100 * msecs_to_jiffies(timeout);
  671. reinit_completion(&controller->done);
  672. spin_lock_irqsave(&controller->lock, flags);
  673. controller->xfer = xfer;
  674. controller->error = 0;
  675. controller->rx_bytes = 0;
  676. controller->tx_bytes = 0;
  677. spin_unlock_irqrestore(&controller->lock, flags);
  678. if (spi_qup_is_dma_xfer(controller->mode))
  679. ret = spi_qup_do_dma(spi, xfer, timeout);
  680. else
  681. ret = spi_qup_do_pio(spi, xfer, timeout);
  682. if (ret)
  683. goto exit;
  684. exit:
  685. spi_qup_set_state(controller, QUP_STATE_RESET);
  686. spin_lock_irqsave(&controller->lock, flags);
  687. if (!ret)
  688. ret = controller->error;
  689. spin_unlock_irqrestore(&controller->lock, flags);
  690. if (ret && spi_qup_is_dma_xfer(controller->mode))
  691. spi_qup_dma_terminate(master, xfer);
  692. return ret;
  693. }
  694. static bool spi_qup_can_dma(struct spi_master *master, struct spi_device *spi,
  695. struct spi_transfer *xfer)
  696. {
  697. struct spi_qup *qup = spi_master_get_devdata(master);
  698. size_t dma_align = dma_get_cache_alignment();
  699. int n_words;
  700. if (xfer->rx_buf) {
  701. if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) ||
  702. IS_ERR_OR_NULL(master->dma_rx))
  703. return false;
  704. if (qup->qup_v1 && (xfer->len % qup->in_blk_sz))
  705. return false;
  706. }
  707. if (xfer->tx_buf) {
  708. if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) ||
  709. IS_ERR_OR_NULL(master->dma_tx))
  710. return false;
  711. if (qup->qup_v1 && (xfer->len % qup->out_blk_sz))
  712. return false;
  713. }
  714. n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8);
  715. if (n_words <= (qup->in_fifo_sz / sizeof(u32)))
  716. return false;
  717. return true;
  718. }
  719. static void spi_qup_release_dma(struct spi_master *master)
  720. {
  721. if (!IS_ERR_OR_NULL(master->dma_rx))
  722. dma_release_channel(master->dma_rx);
  723. if (!IS_ERR_OR_NULL(master->dma_tx))
  724. dma_release_channel(master->dma_tx);
  725. }
  726. static int spi_qup_init_dma(struct spi_master *master, resource_size_t base)
  727. {
  728. struct spi_qup *spi = spi_master_get_devdata(master);
  729. struct dma_slave_config *rx_conf = &spi->rx_conf,
  730. *tx_conf = &spi->tx_conf;
  731. struct device *dev = spi->dev;
  732. int ret;
  733. /* allocate dma resources, if available */
  734. master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
  735. if (IS_ERR(master->dma_rx))
  736. return PTR_ERR(master->dma_rx);
  737. master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
  738. if (IS_ERR(master->dma_tx)) {
  739. ret = PTR_ERR(master->dma_tx);
  740. goto err_tx;
  741. }
  742. /* set DMA parameters */
  743. rx_conf->direction = DMA_DEV_TO_MEM;
  744. rx_conf->device_fc = 1;
  745. rx_conf->src_addr = base + QUP_INPUT_FIFO;
  746. rx_conf->src_maxburst = spi->in_blk_sz;
  747. tx_conf->direction = DMA_MEM_TO_DEV;
  748. tx_conf->device_fc = 1;
  749. tx_conf->dst_addr = base + QUP_OUTPUT_FIFO;
  750. tx_conf->dst_maxburst = spi->out_blk_sz;
  751. ret = dmaengine_slave_config(master->dma_rx, rx_conf);
  752. if (ret) {
  753. dev_err(dev, "failed to configure RX channel\n");
  754. goto err;
  755. }
  756. ret = dmaengine_slave_config(master->dma_tx, tx_conf);
  757. if (ret) {
  758. dev_err(dev, "failed to configure TX channel\n");
  759. goto err;
  760. }
  761. return 0;
  762. err:
  763. dma_release_channel(master->dma_tx);
  764. err_tx:
  765. dma_release_channel(master->dma_rx);
  766. return ret;
  767. }
  768. static void spi_qup_set_cs(struct spi_device *spi, bool val)
  769. {
  770. struct spi_qup *controller;
  771. u32 spi_ioc;
  772. u32 spi_ioc_orig;
  773. controller = spi_master_get_devdata(spi->master);
  774. spi_ioc = readl_relaxed(controller->base + SPI_IO_CONTROL);
  775. spi_ioc_orig = spi_ioc;
  776. if (!val)
  777. spi_ioc |= SPI_IO_C_FORCE_CS;
  778. else
  779. spi_ioc &= ~SPI_IO_C_FORCE_CS;
  780. if (spi_ioc != spi_ioc_orig)
  781. writel_relaxed(spi_ioc, controller->base + SPI_IO_CONTROL);
  782. }
  783. static int spi_qup_probe(struct platform_device *pdev)
  784. {
  785. struct spi_master *master;
  786. struct clk *iclk, *cclk;
  787. struct spi_qup *controller;
  788. struct resource *res;
  789. struct device *dev;
  790. void __iomem *base;
  791. u32 max_freq, iomode, num_cs;
  792. int ret, irq, size;
  793. dev = &pdev->dev;
  794. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  795. base = devm_ioremap_resource(dev, res);
  796. if (IS_ERR(base))
  797. return PTR_ERR(base);
  798. irq = platform_get_irq(pdev, 0);
  799. if (irq < 0)
  800. return irq;
  801. cclk = devm_clk_get(dev, "core");
  802. if (IS_ERR(cclk))
  803. return PTR_ERR(cclk);
  804. iclk = devm_clk_get(dev, "iface");
  805. if (IS_ERR(iclk))
  806. return PTR_ERR(iclk);
  807. /* This is optional parameter */
  808. if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq))
  809. max_freq = SPI_MAX_RATE;
  810. if (!max_freq || max_freq > SPI_MAX_RATE) {
  811. dev_err(dev, "invalid clock frequency %d\n", max_freq);
  812. return -ENXIO;
  813. }
  814. ret = clk_prepare_enable(cclk);
  815. if (ret) {
  816. dev_err(dev, "cannot enable core clock\n");
  817. return ret;
  818. }
  819. ret = clk_prepare_enable(iclk);
  820. if (ret) {
  821. clk_disable_unprepare(cclk);
  822. dev_err(dev, "cannot enable iface clock\n");
  823. return ret;
  824. }
  825. master = spi_alloc_master(dev, sizeof(struct spi_qup));
  826. if (!master) {
  827. clk_disable_unprepare(cclk);
  828. clk_disable_unprepare(iclk);
  829. dev_err(dev, "cannot allocate master\n");
  830. return -ENOMEM;
  831. }
  832. /* use num-cs unless not present or out of range */
  833. if (of_property_read_u32(dev->of_node, "num-cs", &num_cs) ||
  834. num_cs > SPI_NUM_CHIPSELECTS)
  835. master->num_chipselect = SPI_NUM_CHIPSELECTS;
  836. else
  837. master->num_chipselect = num_cs;
  838. master->bus_num = pdev->id;
  839. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  840. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  841. master->max_speed_hz = max_freq;
  842. master->transfer_one = spi_qup_transfer_one;
  843. master->dev.of_node = pdev->dev.of_node;
  844. master->auto_runtime_pm = true;
  845. master->dma_alignment = dma_get_cache_alignment();
  846. master->max_dma_len = SPI_MAX_XFER;
  847. platform_set_drvdata(pdev, master);
  848. controller = spi_master_get_devdata(master);
  849. controller->dev = dev;
  850. controller->base = base;
  851. controller->iclk = iclk;
  852. controller->cclk = cclk;
  853. controller->irq = irq;
  854. ret = spi_qup_init_dma(master, res->start);
  855. if (ret == -EPROBE_DEFER)
  856. goto error;
  857. else if (!ret)
  858. master->can_dma = spi_qup_can_dma;
  859. controller->qup_v1 = (int)of_device_get_match_data(dev);
  860. if (!controller->qup_v1)
  861. master->set_cs = spi_qup_set_cs;
  862. spin_lock_init(&controller->lock);
  863. init_completion(&controller->done);
  864. iomode = readl_relaxed(base + QUP_IO_M_MODES);
  865. size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode);
  866. if (size)
  867. controller->out_blk_sz = size * 16;
  868. else
  869. controller->out_blk_sz = 4;
  870. size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode);
  871. if (size)
  872. controller->in_blk_sz = size * 16;
  873. else
  874. controller->in_blk_sz = 4;
  875. size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode);
  876. controller->out_fifo_sz = controller->out_blk_sz * (2 << size);
  877. size = QUP_IO_M_INPUT_FIFO_SIZE(iomode);
  878. controller->in_fifo_sz = controller->in_blk_sz * (2 << size);
  879. dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  880. controller->in_blk_sz, controller->in_fifo_sz,
  881. controller->out_blk_sz, controller->out_fifo_sz);
  882. writel_relaxed(1, base + QUP_SW_RESET);
  883. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  884. if (ret) {
  885. dev_err(dev, "cannot set RESET state\n");
  886. goto error_dma;
  887. }
  888. writel_relaxed(0, base + QUP_OPERATIONAL);
  889. writel_relaxed(0, base + QUP_IO_M_MODES);
  890. if (!controller->qup_v1)
  891. writel_relaxed(0, base + QUP_OPERATIONAL_MASK);
  892. writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN,
  893. base + SPI_ERROR_FLAGS_EN);
  894. /* if earlier version of the QUP, disable INPUT_OVERRUN */
  895. if (controller->qup_v1)
  896. writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN |
  897. QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN,
  898. base + QUP_ERROR_FLAGS_EN);
  899. writel_relaxed(0, base + SPI_CONFIG);
  900. writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL);
  901. ret = devm_request_irq(dev, irq, spi_qup_qup_irq,
  902. IRQF_TRIGGER_HIGH, pdev->name, controller);
  903. if (ret)
  904. goto error_dma;
  905. pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
  906. pm_runtime_use_autosuspend(dev);
  907. pm_runtime_set_active(dev);
  908. pm_runtime_enable(dev);
  909. ret = devm_spi_register_master(dev, master);
  910. if (ret)
  911. goto disable_pm;
  912. return 0;
  913. disable_pm:
  914. pm_runtime_disable(&pdev->dev);
  915. error_dma:
  916. spi_qup_release_dma(master);
  917. error:
  918. clk_disable_unprepare(cclk);
  919. clk_disable_unprepare(iclk);
  920. spi_master_put(master);
  921. return ret;
  922. }
  923. #ifdef CONFIG_PM
  924. static int spi_qup_pm_suspend_runtime(struct device *device)
  925. {
  926. struct spi_master *master = dev_get_drvdata(device);
  927. struct spi_qup *controller = spi_master_get_devdata(master);
  928. u32 config;
  929. /* Enable clocks auto gaiting */
  930. config = readl(controller->base + QUP_CONFIG);
  931. config |= QUP_CONFIG_CLOCK_AUTO_GATE;
  932. writel_relaxed(config, controller->base + QUP_CONFIG);
  933. clk_disable_unprepare(controller->cclk);
  934. clk_disable_unprepare(controller->iclk);
  935. return 0;
  936. }
  937. static int spi_qup_pm_resume_runtime(struct device *device)
  938. {
  939. struct spi_master *master = dev_get_drvdata(device);
  940. struct spi_qup *controller = spi_master_get_devdata(master);
  941. u32 config;
  942. int ret;
  943. ret = clk_prepare_enable(controller->iclk);
  944. if (ret)
  945. return ret;
  946. ret = clk_prepare_enable(controller->cclk);
  947. if (ret)
  948. return ret;
  949. /* Disable clocks auto gaiting */
  950. config = readl_relaxed(controller->base + QUP_CONFIG);
  951. config &= ~QUP_CONFIG_CLOCK_AUTO_GATE;
  952. writel_relaxed(config, controller->base + QUP_CONFIG);
  953. return 0;
  954. }
  955. #endif /* CONFIG_PM */
  956. #ifdef CONFIG_PM_SLEEP
  957. static int spi_qup_suspend(struct device *device)
  958. {
  959. struct spi_master *master = dev_get_drvdata(device);
  960. struct spi_qup *controller = spi_master_get_devdata(master);
  961. int ret;
  962. ret = spi_master_suspend(master);
  963. if (ret)
  964. return ret;
  965. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  966. if (ret)
  967. return ret;
  968. if (!pm_runtime_suspended(device)) {
  969. clk_disable_unprepare(controller->cclk);
  970. clk_disable_unprepare(controller->iclk);
  971. }
  972. return 0;
  973. }
  974. static int spi_qup_resume(struct device *device)
  975. {
  976. struct spi_master *master = dev_get_drvdata(device);
  977. struct spi_qup *controller = spi_master_get_devdata(master);
  978. int ret;
  979. ret = clk_prepare_enable(controller->iclk);
  980. if (ret)
  981. return ret;
  982. ret = clk_prepare_enable(controller->cclk);
  983. if (ret)
  984. return ret;
  985. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  986. if (ret)
  987. return ret;
  988. return spi_master_resume(master);
  989. }
  990. #endif /* CONFIG_PM_SLEEP */
  991. static int spi_qup_remove(struct platform_device *pdev)
  992. {
  993. struct spi_master *master = dev_get_drvdata(&pdev->dev);
  994. struct spi_qup *controller = spi_master_get_devdata(master);
  995. int ret;
  996. ret = pm_runtime_get_sync(&pdev->dev);
  997. if (ret < 0)
  998. return ret;
  999. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  1000. if (ret)
  1001. return ret;
  1002. spi_qup_release_dma(master);
  1003. clk_disable_unprepare(controller->cclk);
  1004. clk_disable_unprepare(controller->iclk);
  1005. pm_runtime_put_noidle(&pdev->dev);
  1006. pm_runtime_disable(&pdev->dev);
  1007. return 0;
  1008. }
  1009. static const struct of_device_id spi_qup_dt_match[] = {
  1010. { .compatible = "qcom,spi-qup-v1.1.1", .data = (void *)1, },
  1011. { .compatible = "qcom,spi-qup-v2.1.1", },
  1012. { .compatible = "qcom,spi-qup-v2.2.1", },
  1013. { }
  1014. };
  1015. MODULE_DEVICE_TABLE(of, spi_qup_dt_match);
  1016. static const struct dev_pm_ops spi_qup_dev_pm_ops = {
  1017. SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume)
  1018. SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime,
  1019. spi_qup_pm_resume_runtime,
  1020. NULL)
  1021. };
  1022. static struct platform_driver spi_qup_driver = {
  1023. .driver = {
  1024. .name = "spi_qup",
  1025. .pm = &spi_qup_dev_pm_ops,
  1026. .of_match_table = spi_qup_dt_match,
  1027. },
  1028. .probe = spi_qup_probe,
  1029. .remove = spi_qup_remove,
  1030. };
  1031. module_platform_driver(spi_qup_driver);
  1032. MODULE_LICENSE("GPL v2");
  1033. MODULE_ALIAS("platform:spi_qup");