spi-qup.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. for (sg = sgl; sg; sg = sg_next(sg)) {
  349. unsigned int len = sg_dma_len(sg);
  350. /* check for overflow as well as limit */
  351. if (((total + len) < total) || ((total + len) > max))
  352. break;
  353. total += len;
  354. (*nents)++;
  355. }
  356. return total;
  357. }
  358. static int spi_qup_do_dma(struct spi_device *spi, struct spi_transfer *xfer,
  359. unsigned long timeout)
  360. {
  361. dma_async_tx_callback rx_done = NULL, tx_done = NULL;
  362. struct spi_master *master = spi->master;
  363. struct spi_qup *qup = spi_master_get_devdata(master);
  364. struct scatterlist *tx_sgl, *rx_sgl;
  365. int ret;
  366. if (xfer->rx_buf)
  367. rx_done = spi_qup_dma_done;
  368. else if (xfer->tx_buf)
  369. tx_done = spi_qup_dma_done;
  370. rx_sgl = xfer->rx_sg.sgl;
  371. tx_sgl = xfer->tx_sg.sgl;
  372. do {
  373. u32 rx_nents = 0, tx_nents = 0;
  374. if (rx_sgl)
  375. qup->n_words = spi_qup_sgl_get_nents_len(rx_sgl,
  376. SPI_MAX_XFER, &rx_nents) / qup->w_size;
  377. if (tx_sgl)
  378. qup->n_words = spi_qup_sgl_get_nents_len(tx_sgl,
  379. SPI_MAX_XFER, &tx_nents) / qup->w_size;
  380. if (!qup->n_words)
  381. return -EIO;
  382. ret = spi_qup_io_config(spi, xfer);
  383. if (ret)
  384. return ret;
  385. /* before issuing the descriptors, set the QUP to run */
  386. ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  387. if (ret) {
  388. dev_warn(qup->dev, "cannot set RUN state\n");
  389. return ret;
  390. }
  391. if (rx_sgl) {
  392. ret = spi_qup_prep_sg(master, rx_sgl, rx_nents,
  393. DMA_DEV_TO_MEM, rx_done);
  394. if (ret)
  395. return ret;
  396. dma_async_issue_pending(master->dma_rx);
  397. }
  398. if (tx_sgl) {
  399. ret = spi_qup_prep_sg(master, tx_sgl, tx_nents,
  400. DMA_MEM_TO_DEV, tx_done);
  401. if (ret)
  402. return ret;
  403. dma_async_issue_pending(master->dma_tx);
  404. }
  405. if (!wait_for_completion_timeout(&qup->done, timeout))
  406. return -ETIMEDOUT;
  407. for (; rx_sgl && rx_nents--; rx_sgl = sg_next(rx_sgl))
  408. ;
  409. for (; tx_sgl && tx_nents--; tx_sgl = sg_next(tx_sgl))
  410. ;
  411. } while (rx_sgl || tx_sgl);
  412. return 0;
  413. }
  414. static int spi_qup_do_pio(struct spi_device *spi, struct spi_transfer *xfer,
  415. unsigned long timeout)
  416. {
  417. struct spi_master *master = spi->master;
  418. struct spi_qup *qup = spi_master_get_devdata(master);
  419. int ret, n_words, iterations, offset = 0;
  420. n_words = qup->n_words;
  421. iterations = n_words / SPI_MAX_XFER; /* round down */
  422. qup->rx_buf = xfer->rx_buf;
  423. qup->tx_buf = xfer->tx_buf;
  424. do {
  425. if (iterations)
  426. qup->n_words = SPI_MAX_XFER;
  427. else
  428. qup->n_words = n_words % SPI_MAX_XFER;
  429. if (qup->tx_buf && offset)
  430. qup->tx_buf = xfer->tx_buf + offset * SPI_MAX_XFER;
  431. if (qup->rx_buf && offset)
  432. qup->rx_buf = xfer->rx_buf + offset * SPI_MAX_XFER;
  433. /*
  434. * if the transaction is small enough, we need
  435. * to fallback to FIFO mode
  436. */
  437. if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
  438. qup->mode = QUP_IO_M_MODE_FIFO;
  439. ret = spi_qup_io_config(spi, xfer);
  440. if (ret)
  441. return ret;
  442. ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  443. if (ret) {
  444. dev_warn(qup->dev, "cannot set RUN state\n");
  445. return ret;
  446. }
  447. ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
  448. if (ret) {
  449. dev_warn(qup->dev, "cannot set PAUSE state\n");
  450. return ret;
  451. }
  452. if (qup->mode == QUP_IO_M_MODE_FIFO)
  453. spi_qup_write(qup);
  454. ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  455. if (ret) {
  456. dev_warn(qup->dev, "cannot set RUN state\n");
  457. return ret;
  458. }
  459. if (!wait_for_completion_timeout(&qup->done, timeout))
  460. return -ETIMEDOUT;
  461. offset++;
  462. } while (iterations--);
  463. return 0;
  464. }
  465. static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
  466. {
  467. struct spi_qup *controller = dev_id;
  468. u32 opflags, qup_err, spi_err;
  469. int error = 0;
  470. qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS);
  471. spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS);
  472. opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
  473. writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS);
  474. writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS);
  475. if (qup_err) {
  476. if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN)
  477. dev_warn(controller->dev, "OUTPUT_OVER_RUN\n");
  478. if (qup_err & QUP_ERROR_INPUT_UNDER_RUN)
  479. dev_warn(controller->dev, "INPUT_UNDER_RUN\n");
  480. if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN)
  481. dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n");
  482. if (qup_err & QUP_ERROR_INPUT_OVER_RUN)
  483. dev_warn(controller->dev, "INPUT_OVER_RUN\n");
  484. error = -EIO;
  485. }
  486. if (spi_err) {
  487. if (spi_err & SPI_ERROR_CLK_OVER_RUN)
  488. dev_warn(controller->dev, "CLK_OVER_RUN\n");
  489. if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
  490. dev_warn(controller->dev, "CLK_UNDER_RUN\n");
  491. error = -EIO;
  492. }
  493. if (spi_qup_is_dma_xfer(controller->mode)) {
  494. writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
  495. } else {
  496. if (opflags & QUP_OP_IN_SERVICE_FLAG)
  497. spi_qup_read(controller, &opflags);
  498. if (opflags & QUP_OP_OUT_SERVICE_FLAG)
  499. spi_qup_write(controller);
  500. }
  501. if ((opflags & QUP_OP_MAX_INPUT_DONE_FLAG) || error)
  502. complete(&controller->done);
  503. return IRQ_HANDLED;
  504. }
  505. /* set clock freq ... bits per word, determine mode */
  506. static int spi_qup_io_prep(struct spi_device *spi, struct spi_transfer *xfer)
  507. {
  508. struct spi_qup *controller = spi_master_get_devdata(spi->master);
  509. int ret;
  510. if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
  511. dev_err(controller->dev, "too big size for loopback %d > %d\n",
  512. xfer->len, controller->in_fifo_sz);
  513. return -EIO;
  514. }
  515. ret = clk_set_rate(controller->cclk, xfer->speed_hz);
  516. if (ret) {
  517. dev_err(controller->dev, "fail to set frequency %d",
  518. xfer->speed_hz);
  519. return -EIO;
  520. }
  521. controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8);
  522. controller->n_words = xfer->len / controller->w_size;
  523. if (controller->n_words <= (controller->in_fifo_sz / sizeof(u32)))
  524. controller->mode = QUP_IO_M_MODE_FIFO;
  525. else if (spi->master->can_dma &&
  526. spi->master->can_dma(spi->master, spi, xfer) &&
  527. spi->master->cur_msg_mapped)
  528. controller->mode = QUP_IO_M_MODE_BAM;
  529. else
  530. controller->mode = QUP_IO_M_MODE_BLOCK;
  531. return 0;
  532. }
  533. /* prep qup for another spi transaction of specific type */
  534. static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer)
  535. {
  536. struct spi_qup *controller = spi_master_get_devdata(spi->master);
  537. u32 config, iomode, control;
  538. unsigned long flags;
  539. spin_lock_irqsave(&controller->lock, flags);
  540. controller->xfer = xfer;
  541. controller->error = 0;
  542. controller->rx_bytes = 0;
  543. controller->tx_bytes = 0;
  544. spin_unlock_irqrestore(&controller->lock, flags);
  545. if (spi_qup_set_state(controller, QUP_STATE_RESET)) {
  546. dev_err(controller->dev, "cannot set RESET state\n");
  547. return -EIO;
  548. }
  549. switch (controller->mode) {
  550. case QUP_IO_M_MODE_FIFO:
  551. writel_relaxed(controller->n_words,
  552. controller->base + QUP_MX_READ_CNT);
  553. writel_relaxed(controller->n_words,
  554. controller->base + QUP_MX_WRITE_CNT);
  555. /* must be zero for FIFO */
  556. writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
  557. writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  558. break;
  559. case QUP_IO_M_MODE_BAM:
  560. writel_relaxed(controller->n_words,
  561. controller->base + QUP_MX_INPUT_CNT);
  562. writel_relaxed(controller->n_words,
  563. controller->base + QUP_MX_OUTPUT_CNT);
  564. /* must be zero for BLOCK and BAM */
  565. writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  566. writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  567. if (!controller->qup_v1) {
  568. void __iomem *input_cnt;
  569. input_cnt = controller->base + QUP_MX_INPUT_CNT;
  570. /*
  571. * for DMA transfers, both QUP_MX_INPUT_CNT and
  572. * QUP_MX_OUTPUT_CNT must be zero to all cases but one.
  573. * That case is a non-balanced transfer when there is
  574. * only a rx_buf.
  575. */
  576. if (xfer->tx_buf)
  577. writel_relaxed(0, input_cnt);
  578. else
  579. writel_relaxed(controller->n_words, input_cnt);
  580. writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  581. }
  582. break;
  583. case QUP_IO_M_MODE_BLOCK:
  584. reinit_completion(&controller->done);
  585. writel_relaxed(controller->n_words,
  586. controller->base + QUP_MX_INPUT_CNT);
  587. writel_relaxed(controller->n_words,
  588. controller->base + QUP_MX_OUTPUT_CNT);
  589. /* must be zero for BLOCK and BAM */
  590. writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  591. writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  592. break;
  593. default:
  594. dev_err(controller->dev, "unknown mode = %d\n",
  595. controller->mode);
  596. return -EIO;
  597. }
  598. iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
  599. /* Set input and output transfer mode */
  600. iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
  601. if (!spi_qup_is_dma_xfer(controller->mode))
  602. iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
  603. else
  604. iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN;
  605. iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
  606. iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
  607. writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
  608. control = readl_relaxed(controller->base + SPI_IO_CONTROL);
  609. if (spi->mode & SPI_CPOL)
  610. control |= SPI_IO_C_CLK_IDLE_HIGH;
  611. else
  612. control &= ~SPI_IO_C_CLK_IDLE_HIGH;
  613. writel_relaxed(control, controller->base + SPI_IO_CONTROL);
  614. config = readl_relaxed(controller->base + SPI_CONFIG);
  615. if (spi->mode & SPI_LOOP)
  616. config |= SPI_CONFIG_LOOPBACK;
  617. else
  618. config &= ~SPI_CONFIG_LOOPBACK;
  619. if (spi->mode & SPI_CPHA)
  620. config &= ~SPI_CONFIG_INPUT_FIRST;
  621. else
  622. config |= SPI_CONFIG_INPUT_FIRST;
  623. /*
  624. * HS_MODE improves signal stability for spi-clk high rates,
  625. * but is invalid in loop back mode.
  626. */
  627. if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP))
  628. config |= SPI_CONFIG_HS_MODE;
  629. else
  630. config &= ~SPI_CONFIG_HS_MODE;
  631. writel_relaxed(config, controller->base + SPI_CONFIG);
  632. config = readl_relaxed(controller->base + QUP_CONFIG);
  633. config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N);
  634. config |= xfer->bits_per_word - 1;
  635. config |= QUP_CONFIG_SPI_MODE;
  636. if (spi_qup_is_dma_xfer(controller->mode)) {
  637. if (!xfer->tx_buf)
  638. config |= QUP_CONFIG_NO_OUTPUT;
  639. if (!xfer->rx_buf)
  640. config |= QUP_CONFIG_NO_INPUT;
  641. }
  642. writel_relaxed(config, controller->base + QUP_CONFIG);
  643. /* only write to OPERATIONAL_MASK when register is present */
  644. if (!controller->qup_v1) {
  645. u32 mask = 0;
  646. /*
  647. * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO
  648. * status change in BAM mode
  649. */
  650. if (spi_qup_is_dma_xfer(controller->mode))
  651. mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG;
  652. writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK);
  653. }
  654. return 0;
  655. }
  656. static int spi_qup_transfer_one(struct spi_master *master,
  657. struct spi_device *spi,
  658. struct spi_transfer *xfer)
  659. {
  660. struct spi_qup *controller = spi_master_get_devdata(master);
  661. unsigned long timeout, flags;
  662. int ret = -EIO;
  663. ret = spi_qup_io_prep(spi, xfer);
  664. if (ret)
  665. return ret;
  666. timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
  667. timeout = DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER,
  668. xfer->len) * 8, timeout);
  669. timeout = 100 * msecs_to_jiffies(timeout);
  670. reinit_completion(&controller->done);
  671. spin_lock_irqsave(&controller->lock, flags);
  672. controller->xfer = xfer;
  673. controller->error = 0;
  674. controller->rx_bytes = 0;
  675. controller->tx_bytes = 0;
  676. spin_unlock_irqrestore(&controller->lock, flags);
  677. if (spi_qup_is_dma_xfer(controller->mode))
  678. ret = spi_qup_do_dma(spi, xfer, timeout);
  679. else
  680. ret = spi_qup_do_pio(spi, xfer, timeout);
  681. if (ret)
  682. goto exit;
  683. exit:
  684. spi_qup_set_state(controller, QUP_STATE_RESET);
  685. spin_lock_irqsave(&controller->lock, flags);
  686. if (!ret)
  687. ret = controller->error;
  688. spin_unlock_irqrestore(&controller->lock, flags);
  689. if (ret && spi_qup_is_dma_xfer(controller->mode))
  690. spi_qup_dma_terminate(master, xfer);
  691. return ret;
  692. }
  693. static bool spi_qup_can_dma(struct spi_master *master, struct spi_device *spi,
  694. struct spi_transfer *xfer)
  695. {
  696. struct spi_qup *qup = spi_master_get_devdata(master);
  697. size_t dma_align = dma_get_cache_alignment();
  698. int n_words;
  699. if (xfer->rx_buf) {
  700. if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) ||
  701. IS_ERR_OR_NULL(master->dma_rx))
  702. return false;
  703. if (qup->qup_v1 && (xfer->len % qup->in_blk_sz))
  704. return false;
  705. }
  706. if (xfer->tx_buf) {
  707. if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) ||
  708. IS_ERR_OR_NULL(master->dma_tx))
  709. return false;
  710. if (qup->qup_v1 && (xfer->len % qup->out_blk_sz))
  711. return false;
  712. }
  713. n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8);
  714. if (n_words <= (qup->in_fifo_sz / sizeof(u32)))
  715. return false;
  716. return true;
  717. }
  718. static void spi_qup_release_dma(struct spi_master *master)
  719. {
  720. if (!IS_ERR_OR_NULL(master->dma_rx))
  721. dma_release_channel(master->dma_rx);
  722. if (!IS_ERR_OR_NULL(master->dma_tx))
  723. dma_release_channel(master->dma_tx);
  724. }
  725. static int spi_qup_init_dma(struct spi_master *master, resource_size_t base)
  726. {
  727. struct spi_qup *spi = spi_master_get_devdata(master);
  728. struct dma_slave_config *rx_conf = &spi->rx_conf,
  729. *tx_conf = &spi->tx_conf;
  730. struct device *dev = spi->dev;
  731. int ret;
  732. /* allocate dma resources, if available */
  733. master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
  734. if (IS_ERR(master->dma_rx))
  735. return PTR_ERR(master->dma_rx);
  736. master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
  737. if (IS_ERR(master->dma_tx)) {
  738. ret = PTR_ERR(master->dma_tx);
  739. goto err_tx;
  740. }
  741. /* set DMA parameters */
  742. rx_conf->direction = DMA_DEV_TO_MEM;
  743. rx_conf->device_fc = 1;
  744. rx_conf->src_addr = base + QUP_INPUT_FIFO;
  745. rx_conf->src_maxburst = spi->in_blk_sz;
  746. tx_conf->direction = DMA_MEM_TO_DEV;
  747. tx_conf->device_fc = 1;
  748. tx_conf->dst_addr = base + QUP_OUTPUT_FIFO;
  749. tx_conf->dst_maxburst = spi->out_blk_sz;
  750. ret = dmaengine_slave_config(master->dma_rx, rx_conf);
  751. if (ret) {
  752. dev_err(dev, "failed to configure RX channel\n");
  753. goto err;
  754. }
  755. ret = dmaengine_slave_config(master->dma_tx, tx_conf);
  756. if (ret) {
  757. dev_err(dev, "failed to configure TX channel\n");
  758. goto err;
  759. }
  760. return 0;
  761. err:
  762. dma_release_channel(master->dma_tx);
  763. err_tx:
  764. dma_release_channel(master->dma_rx);
  765. return ret;
  766. }
  767. static void spi_qup_set_cs(struct spi_device *spi, bool val)
  768. {
  769. struct spi_qup *controller;
  770. u32 spi_ioc;
  771. u32 spi_ioc_orig;
  772. controller = spi_master_get_devdata(spi->master);
  773. spi_ioc = readl_relaxed(controller->base + SPI_IO_CONTROL);
  774. spi_ioc_orig = spi_ioc;
  775. if (!val)
  776. spi_ioc |= SPI_IO_C_FORCE_CS;
  777. else
  778. spi_ioc &= ~SPI_IO_C_FORCE_CS;
  779. if (spi_ioc != spi_ioc_orig)
  780. writel_relaxed(spi_ioc, controller->base + SPI_IO_CONTROL);
  781. }
  782. static int spi_qup_probe(struct platform_device *pdev)
  783. {
  784. struct spi_master *master;
  785. struct clk *iclk, *cclk;
  786. struct spi_qup *controller;
  787. struct resource *res;
  788. struct device *dev;
  789. void __iomem *base;
  790. u32 max_freq, iomode, num_cs;
  791. int ret, irq, size;
  792. dev = &pdev->dev;
  793. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  794. base = devm_ioremap_resource(dev, res);
  795. if (IS_ERR(base))
  796. return PTR_ERR(base);
  797. irq = platform_get_irq(pdev, 0);
  798. if (irq < 0)
  799. return irq;
  800. cclk = devm_clk_get(dev, "core");
  801. if (IS_ERR(cclk))
  802. return PTR_ERR(cclk);
  803. iclk = devm_clk_get(dev, "iface");
  804. if (IS_ERR(iclk))
  805. return PTR_ERR(iclk);
  806. /* This is optional parameter */
  807. if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq))
  808. max_freq = SPI_MAX_RATE;
  809. if (!max_freq || max_freq > SPI_MAX_RATE) {
  810. dev_err(dev, "invalid clock frequency %d\n", max_freq);
  811. return -ENXIO;
  812. }
  813. ret = clk_prepare_enable(cclk);
  814. if (ret) {
  815. dev_err(dev, "cannot enable core clock\n");
  816. return ret;
  817. }
  818. ret = clk_prepare_enable(iclk);
  819. if (ret) {
  820. clk_disable_unprepare(cclk);
  821. dev_err(dev, "cannot enable iface clock\n");
  822. return ret;
  823. }
  824. master = spi_alloc_master(dev, sizeof(struct spi_qup));
  825. if (!master) {
  826. clk_disable_unprepare(cclk);
  827. clk_disable_unprepare(iclk);
  828. dev_err(dev, "cannot allocate master\n");
  829. return -ENOMEM;
  830. }
  831. /* use num-cs unless not present or out of range */
  832. if (of_property_read_u32(dev->of_node, "num-cs", &num_cs) ||
  833. num_cs > SPI_NUM_CHIPSELECTS)
  834. master->num_chipselect = SPI_NUM_CHIPSELECTS;
  835. else
  836. master->num_chipselect = num_cs;
  837. master->bus_num = pdev->id;
  838. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  839. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  840. master->max_speed_hz = max_freq;
  841. master->transfer_one = spi_qup_transfer_one;
  842. master->dev.of_node = pdev->dev.of_node;
  843. master->auto_runtime_pm = true;
  844. master->dma_alignment = dma_get_cache_alignment();
  845. master->max_dma_len = SPI_MAX_XFER;
  846. platform_set_drvdata(pdev, master);
  847. controller = spi_master_get_devdata(master);
  848. controller->dev = dev;
  849. controller->base = base;
  850. controller->iclk = iclk;
  851. controller->cclk = cclk;
  852. controller->irq = irq;
  853. ret = spi_qup_init_dma(master, res->start);
  854. if (ret == -EPROBE_DEFER)
  855. goto error;
  856. else if (!ret)
  857. master->can_dma = spi_qup_can_dma;
  858. controller->qup_v1 = (uintptr_t)of_device_get_match_data(dev);
  859. if (!controller->qup_v1)
  860. master->set_cs = spi_qup_set_cs;
  861. spin_lock_init(&controller->lock);
  862. init_completion(&controller->done);
  863. iomode = readl_relaxed(base + QUP_IO_M_MODES);
  864. size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode);
  865. if (size)
  866. controller->out_blk_sz = size * 16;
  867. else
  868. controller->out_blk_sz = 4;
  869. size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode);
  870. if (size)
  871. controller->in_blk_sz = size * 16;
  872. else
  873. controller->in_blk_sz = 4;
  874. size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode);
  875. controller->out_fifo_sz = controller->out_blk_sz * (2 << size);
  876. size = QUP_IO_M_INPUT_FIFO_SIZE(iomode);
  877. controller->in_fifo_sz = controller->in_blk_sz * (2 << size);
  878. dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  879. controller->in_blk_sz, controller->in_fifo_sz,
  880. controller->out_blk_sz, controller->out_fifo_sz);
  881. writel_relaxed(1, base + QUP_SW_RESET);
  882. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  883. if (ret) {
  884. dev_err(dev, "cannot set RESET state\n");
  885. goto error_dma;
  886. }
  887. writel_relaxed(0, base + QUP_OPERATIONAL);
  888. writel_relaxed(0, base + QUP_IO_M_MODES);
  889. if (!controller->qup_v1)
  890. writel_relaxed(0, base + QUP_OPERATIONAL_MASK);
  891. writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN,
  892. base + SPI_ERROR_FLAGS_EN);
  893. /* if earlier version of the QUP, disable INPUT_OVERRUN */
  894. if (controller->qup_v1)
  895. writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN |
  896. QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN,
  897. base + QUP_ERROR_FLAGS_EN);
  898. writel_relaxed(0, base + SPI_CONFIG);
  899. writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL);
  900. ret = devm_request_irq(dev, irq, spi_qup_qup_irq,
  901. IRQF_TRIGGER_HIGH, pdev->name, controller);
  902. if (ret)
  903. goto error_dma;
  904. pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
  905. pm_runtime_use_autosuspend(dev);
  906. pm_runtime_set_active(dev);
  907. pm_runtime_enable(dev);
  908. ret = devm_spi_register_master(dev, master);
  909. if (ret)
  910. goto disable_pm;
  911. return 0;
  912. disable_pm:
  913. pm_runtime_disable(&pdev->dev);
  914. error_dma:
  915. spi_qup_release_dma(master);
  916. error:
  917. clk_disable_unprepare(cclk);
  918. clk_disable_unprepare(iclk);
  919. spi_master_put(master);
  920. return ret;
  921. }
  922. #ifdef CONFIG_PM
  923. static int spi_qup_pm_suspend_runtime(struct device *device)
  924. {
  925. struct spi_master *master = dev_get_drvdata(device);
  926. struct spi_qup *controller = spi_master_get_devdata(master);
  927. u32 config;
  928. /* Enable clocks auto gaiting */
  929. config = readl(controller->base + QUP_CONFIG);
  930. config |= QUP_CONFIG_CLOCK_AUTO_GATE;
  931. writel_relaxed(config, controller->base + QUP_CONFIG);
  932. clk_disable_unprepare(controller->cclk);
  933. clk_disable_unprepare(controller->iclk);
  934. return 0;
  935. }
  936. static int spi_qup_pm_resume_runtime(struct device *device)
  937. {
  938. struct spi_master *master = dev_get_drvdata(device);
  939. struct spi_qup *controller = spi_master_get_devdata(master);
  940. u32 config;
  941. int ret;
  942. ret = clk_prepare_enable(controller->iclk);
  943. if (ret)
  944. return ret;
  945. ret = clk_prepare_enable(controller->cclk);
  946. if (ret)
  947. return ret;
  948. /* Disable clocks auto gaiting */
  949. config = readl_relaxed(controller->base + QUP_CONFIG);
  950. config &= ~QUP_CONFIG_CLOCK_AUTO_GATE;
  951. writel_relaxed(config, controller->base + QUP_CONFIG);
  952. return 0;
  953. }
  954. #endif /* CONFIG_PM */
  955. #ifdef CONFIG_PM_SLEEP
  956. static int spi_qup_suspend(struct device *device)
  957. {
  958. struct spi_master *master = dev_get_drvdata(device);
  959. struct spi_qup *controller = spi_master_get_devdata(master);
  960. int ret;
  961. if (pm_runtime_suspended(device)) {
  962. ret = spi_qup_pm_resume_runtime(device);
  963. if (ret)
  964. return ret;
  965. }
  966. ret = spi_master_suspend(master);
  967. if (ret)
  968. return ret;
  969. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  970. if (ret)
  971. return ret;
  972. clk_disable_unprepare(controller->cclk);
  973. clk_disable_unprepare(controller->iclk);
  974. return 0;
  975. }
  976. static int spi_qup_resume(struct device *device)
  977. {
  978. struct spi_master *master = dev_get_drvdata(device);
  979. struct spi_qup *controller = spi_master_get_devdata(master);
  980. int ret;
  981. ret = clk_prepare_enable(controller->iclk);
  982. if (ret)
  983. return ret;
  984. ret = clk_prepare_enable(controller->cclk);
  985. if (ret)
  986. return ret;
  987. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  988. if (ret)
  989. return ret;
  990. return spi_master_resume(master);
  991. }
  992. #endif /* CONFIG_PM_SLEEP */
  993. static int spi_qup_remove(struct platform_device *pdev)
  994. {
  995. struct spi_master *master = dev_get_drvdata(&pdev->dev);
  996. struct spi_qup *controller = spi_master_get_devdata(master);
  997. int ret;
  998. ret = pm_runtime_get_sync(&pdev->dev);
  999. if (ret < 0)
  1000. return ret;
  1001. ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  1002. if (ret)
  1003. return ret;
  1004. spi_qup_release_dma(master);
  1005. clk_disable_unprepare(controller->cclk);
  1006. clk_disable_unprepare(controller->iclk);
  1007. pm_runtime_put_noidle(&pdev->dev);
  1008. pm_runtime_disable(&pdev->dev);
  1009. return 0;
  1010. }
  1011. static const struct of_device_id spi_qup_dt_match[] = {
  1012. { .compatible = "qcom,spi-qup-v1.1.1", .data = (void *)1, },
  1013. { .compatible = "qcom,spi-qup-v2.1.1", },
  1014. { .compatible = "qcom,spi-qup-v2.2.1", },
  1015. { }
  1016. };
  1017. MODULE_DEVICE_TABLE(of, spi_qup_dt_match);
  1018. static const struct dev_pm_ops spi_qup_dev_pm_ops = {
  1019. SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume)
  1020. SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime,
  1021. spi_qup_pm_resume_runtime,
  1022. NULL)
  1023. };
  1024. static struct platform_driver spi_qup_driver = {
  1025. .driver = {
  1026. .name = "spi_qup",
  1027. .pm = &spi_qup_dev_pm_ops,
  1028. .of_match_table = spi_qup_dt_match,
  1029. },
  1030. .probe = spi_qup_probe,
  1031. .remove = spi_qup_remove,
  1032. };
  1033. module_platform_driver(spi_qup_driver);
  1034. MODULE_LICENSE("GPL v2");
  1035. MODULE_ALIAS("platform:spi_qup");