0065-spi-Add-Qualcomm-QUP-SPI-controller-support.patch 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. From 24884115a6029995dba2561b1ee810f28a34271a Mon Sep 17 00:00:00 2001
  2. From: "Ivan T. Ivanov" <[email protected]>
  3. Date: Thu, 13 Feb 2014 18:21:38 +0200
  4. Subject: [PATCH 065/182] spi: Add Qualcomm QUP SPI controller support
  5. Qualcomm Universal Peripheral (QUP) core is an AHB slave that
  6. provides a common data path (an output FIFO and an input FIFO)
  7. for serial peripheral interface (SPI) mini-core. SPI in master
  8. mode supports up to 50MHz, up to four chip selects, programmable
  9. data path from 4 bits to 32 bits and numerous protocol variants.
  10. Cc: Alok Chauhan <[email protected]>
  11. Cc: Gilad Avidov <[email protected]>
  12. Cc: Kiran Gunda <[email protected]>
  13. Cc: Sagar Dharia <[email protected]>
  14. Cc: [email protected]
  15. Signed-off-by: Ivan T. Ivanov <[email protected]>
  16. Signed-off-by: Mark Brown <[email protected]>
  17. ---
  18. drivers/spi/Kconfig | 13 +
  19. drivers/spi/Makefile | 1 +
  20. drivers/spi/spi-qup.c | 837 +++++++++++++++++++++++++++++++++++++++++++++++++
  21. 3 files changed, 851 insertions(+)
  22. create mode 100644 drivers/spi/spi-qup.c
  23. --- a/drivers/spi/Kconfig
  24. +++ b/drivers/spi/Kconfig
  25. @@ -390,6 +390,19 @@ config SPI_RSPI
  26. help
  27. SPI driver for Renesas RSPI and QSPI blocks.
  28. +config SPI_QUP
  29. + tristate "Qualcomm SPI controller with QUP interface"
  30. + depends on ARCH_MSM_DT
  31. + help
  32. + Qualcomm Universal Peripheral (QUP) core is an AHB slave that
  33. + provides a common data path (an output FIFO and an input FIFO)
  34. + for serial peripheral interface (SPI) mini-core. SPI in master
  35. + mode supports up to 50MHz, up to four chip selects, programmable
  36. + data path from 4 bits to 32 bits and numerous protocol variants.
  37. +
  38. + This driver can also be built as a module. If so, the module
  39. + will be called spi_qup.
  40. +
  41. config SPI_S3C24XX
  42. tristate "Samsung S3C24XX series SPI"
  43. depends on ARCH_S3C24XX
  44. --- a/drivers/spi/Makefile
  45. +++ b/drivers/spi/Makefile
  46. @@ -60,6 +60,7 @@ spi-pxa2xx-platform-$(CONFIG_SPI_PXA2XX_
  47. spi-pxa2xx-platform-$(CONFIG_SPI_PXA2XX_DMA) += spi-pxa2xx-dma.o
  48. obj-$(CONFIG_SPI_PXA2XX) += spi-pxa2xx-platform.o
  49. obj-$(CONFIG_SPI_PXA2XX_PCI) += spi-pxa2xx-pci.o
  50. +obj-$(CONFIG_SPI_QUP) += spi-qup.o
  51. obj-$(CONFIG_SPI_RSPI) += spi-rspi.o
  52. obj-$(CONFIG_SPI_S3C24XX) += spi-s3c24xx-hw.o
  53. spi-s3c24xx-hw-y := spi-s3c24xx.o
  54. --- /dev/null
  55. +++ b/drivers/spi/spi-qup.c
  56. @@ -0,0 +1,837 @@
  57. +/*
  58. + * Copyright (c) 2008-2014, The Linux foundation. All rights reserved.
  59. + *
  60. + * This program is free software; you can redistribute it and/or modify
  61. + * it under the terms of the GNU General Public License rev 2 and
  62. + * only rev 2 as published by the free Software foundation.
  63. + *
  64. + * This program is distributed in the hope that it will be useful,
  65. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  66. + * MERCHANTABILITY or fITNESS fOR A PARTICULAR PURPOSE. See the
  67. + * GNU General Public License for more details.
  68. + */
  69. +
  70. +#include <linux/clk.h>
  71. +#include <linux/delay.h>
  72. +#include <linux/err.h>
  73. +#include <linux/interrupt.h>
  74. +#include <linux/io.h>
  75. +#include <linux/list.h>
  76. +#include <linux/module.h>
  77. +#include <linux/of.h>
  78. +#include <linux/platform_device.h>
  79. +#include <linux/pm_runtime.h>
  80. +#include <linux/spi/spi.h>
  81. +
  82. +#define QUP_CONFIG 0x0000
  83. +#define QUP_STATE 0x0004
  84. +#define QUP_IO_M_MODES 0x0008
  85. +#define QUP_SW_RESET 0x000c
  86. +#define QUP_OPERATIONAL 0x0018
  87. +#define QUP_ERROR_FLAGS 0x001c
  88. +#define QUP_ERROR_FLAGS_EN 0x0020
  89. +#define QUP_OPERATIONAL_MASK 0x0028
  90. +#define QUP_HW_VERSION 0x0030
  91. +#define QUP_MX_OUTPUT_CNT 0x0100
  92. +#define QUP_OUTPUT_FIFO 0x0110
  93. +#define QUP_MX_WRITE_CNT 0x0150
  94. +#define QUP_MX_INPUT_CNT 0x0200
  95. +#define QUP_MX_READ_CNT 0x0208
  96. +#define QUP_INPUT_FIFO 0x0218
  97. +
  98. +#define SPI_CONFIG 0x0300
  99. +#define SPI_IO_CONTROL 0x0304
  100. +#define SPI_ERROR_FLAGS 0x0308
  101. +#define SPI_ERROR_FLAGS_EN 0x030c
  102. +
  103. +/* QUP_CONFIG fields */
  104. +#define QUP_CONFIG_SPI_MODE (1 << 8)
  105. +#define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13)
  106. +#define QUP_CONFIG_NO_INPUT BIT(7)
  107. +#define QUP_CONFIG_NO_OUTPUT BIT(6)
  108. +#define QUP_CONFIG_N 0x001f
  109. +
  110. +/* QUP_STATE fields */
  111. +#define QUP_STATE_VALID BIT(2)
  112. +#define QUP_STATE_RESET 0
  113. +#define QUP_STATE_RUN 1
  114. +#define QUP_STATE_PAUSE 3
  115. +#define QUP_STATE_MASK 3
  116. +#define QUP_STATE_CLEAR 2
  117. +
  118. +#define QUP_HW_VERSION_2_1_1 0x20010001
  119. +
  120. +/* QUP_IO_M_MODES fields */
  121. +#define QUP_IO_M_PACK_EN BIT(15)
  122. +#define QUP_IO_M_UNPACK_EN BIT(14)
  123. +#define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12
  124. +#define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10
  125. +#define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT)
  126. +#define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT)
  127. +
  128. +#define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0)
  129. +#define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2)
  130. +#define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5)
  131. +#define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7)
  132. +
  133. +#define QUP_IO_M_MODE_FIFO 0
  134. +#define QUP_IO_M_MODE_BLOCK 1
  135. +#define QUP_IO_M_MODE_DMOV 2
  136. +#define QUP_IO_M_MODE_BAM 3
  137. +
  138. +/* QUP_OPERATIONAL fields */
  139. +#define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11)
  140. +#define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10)
  141. +#define QUP_OP_IN_SERVICE_FLAG BIT(9)
  142. +#define QUP_OP_OUT_SERVICE_FLAG BIT(8)
  143. +#define QUP_OP_IN_FIFO_FULL BIT(7)
  144. +#define QUP_OP_OUT_FIFO_FULL BIT(6)
  145. +#define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5)
  146. +#define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4)
  147. +
  148. +/* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */
  149. +#define QUP_ERROR_OUTPUT_OVER_RUN BIT(5)
  150. +#define QUP_ERROR_INPUT_UNDER_RUN BIT(4)
  151. +#define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3)
  152. +#define QUP_ERROR_INPUT_OVER_RUN BIT(2)
  153. +
  154. +/* SPI_CONFIG fields */
  155. +#define SPI_CONFIG_HS_MODE BIT(10)
  156. +#define SPI_CONFIG_INPUT_FIRST BIT(9)
  157. +#define SPI_CONFIG_LOOPBACK BIT(8)
  158. +
  159. +/* SPI_IO_CONTROL fields */
  160. +#define SPI_IO_C_FORCE_CS BIT(11)
  161. +#define SPI_IO_C_CLK_IDLE_HIGH BIT(10)
  162. +#define SPI_IO_C_MX_CS_MODE BIT(8)
  163. +#define SPI_IO_C_CS_N_POLARITY_0 BIT(4)
  164. +#define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2)
  165. +#define SPI_IO_C_CS_SELECT_MASK 0x000c
  166. +#define SPI_IO_C_TRISTATE_CS BIT(1)
  167. +#define SPI_IO_C_NO_TRI_STATE BIT(0)
  168. +
  169. +/* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */
  170. +#define SPI_ERROR_CLK_OVER_RUN BIT(1)
  171. +#define SPI_ERROR_CLK_UNDER_RUN BIT(0)
  172. +
  173. +#define SPI_NUM_CHIPSELECTS 4
  174. +
  175. +/* high speed mode is when bus rate is greater then 26MHz */
  176. +#define SPI_HS_MIN_RATE 26000000
  177. +#define SPI_MAX_RATE 50000000
  178. +
  179. +#define SPI_DELAY_THRESHOLD 1
  180. +#define SPI_DELAY_RETRY 10
  181. +
  182. +struct spi_qup_device {
  183. + int select;
  184. + u16 mode;
  185. +};
  186. +
  187. +struct spi_qup {
  188. + void __iomem *base;
  189. + struct device *dev;
  190. + struct clk *cclk; /* core clock */
  191. + struct clk *iclk; /* interface clock */
  192. + int irq;
  193. + u32 max_speed_hz;
  194. + spinlock_t lock;
  195. +
  196. + int in_fifo_sz;
  197. + int out_fifo_sz;
  198. + int in_blk_sz;
  199. + int out_blk_sz;
  200. +
  201. + struct spi_transfer *xfer;
  202. + struct completion done;
  203. + int error;
  204. + int w_size; /* bytes per SPI word */
  205. + int tx_bytes;
  206. + int rx_bytes;
  207. +};
  208. +
  209. +
  210. +static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
  211. +{
  212. + u32 opstate = readl_relaxed(controller->base + QUP_STATE);
  213. +
  214. + return opstate & QUP_STATE_VALID;
  215. +}
  216. +
  217. +static int spi_qup_set_state(struct spi_qup *controller, u32 state)
  218. +{
  219. + unsigned long loop;
  220. + u32 cur_state;
  221. +
  222. + loop = 0;
  223. + while (!spi_qup_is_valid_state(controller)) {
  224. +
  225. + usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
  226. +
  227. + if (++loop > SPI_DELAY_RETRY)
  228. + return -EIO;
  229. + }
  230. +
  231. + if (loop)
  232. + dev_dbg(controller->dev, "invalid state for %ld,us %d\n",
  233. + loop, state);
  234. +
  235. + cur_state = readl_relaxed(controller->base + QUP_STATE);
  236. + /*
  237. + * Per spec: for PAUSE_STATE to RESET_STATE, two writes
  238. + * of (b10) are required
  239. + */
  240. + if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) &&
  241. + (state == QUP_STATE_RESET)) {
  242. + writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
  243. + writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
  244. + } else {
  245. + cur_state &= ~QUP_STATE_MASK;
  246. + cur_state |= state;
  247. + writel_relaxed(cur_state, controller->base + QUP_STATE);
  248. + }
  249. +
  250. + loop = 0;
  251. + while (!spi_qup_is_valid_state(controller)) {
  252. +
  253. + usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
  254. +
  255. + if (++loop > SPI_DELAY_RETRY)
  256. + return -EIO;
  257. + }
  258. +
  259. + return 0;
  260. +}
  261. +
  262. +
  263. +static void spi_qup_fifo_read(struct spi_qup *controller,
  264. + struct spi_transfer *xfer)
  265. +{
  266. + u8 *rx_buf = xfer->rx_buf;
  267. + u32 word, state;
  268. + int idx, shift, w_size;
  269. +
  270. + w_size = controller->w_size;
  271. +
  272. + while (controller->rx_bytes < xfer->len) {
  273. +
  274. + state = readl_relaxed(controller->base + QUP_OPERATIONAL);
  275. + if (0 == (state & QUP_OP_IN_FIFO_NOT_EMPTY))
  276. + break;
  277. +
  278. + word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
  279. +
  280. + if (!rx_buf) {
  281. + controller->rx_bytes += w_size;
  282. + continue;
  283. + }
  284. +
  285. + for (idx = 0; idx < w_size; idx++, controller->rx_bytes++) {
  286. + /*
  287. + * The data format depends on bytes per SPI word:
  288. + * 4 bytes: 0x12345678
  289. + * 2 bytes: 0x00001234
  290. + * 1 byte : 0x00000012
  291. + */
  292. + shift = BITS_PER_BYTE;
  293. + shift *= (w_size - idx - 1);
  294. + rx_buf[controller->rx_bytes] = word >> shift;
  295. + }
  296. + }
  297. +}
  298. +
  299. +static void spi_qup_fifo_write(struct spi_qup *controller,
  300. + struct spi_transfer *xfer)
  301. +{
  302. + const u8 *tx_buf = xfer->tx_buf;
  303. + u32 word, state, data;
  304. + int idx, w_size;
  305. +
  306. + w_size = controller->w_size;
  307. +
  308. + while (controller->tx_bytes < xfer->len) {
  309. +
  310. + state = readl_relaxed(controller->base + QUP_OPERATIONAL);
  311. + if (state & QUP_OP_OUT_FIFO_FULL)
  312. + break;
  313. +
  314. + word = 0;
  315. + for (idx = 0; idx < w_size; idx++, controller->tx_bytes++) {
  316. +
  317. + if (!tx_buf) {
  318. + controller->tx_bytes += w_size;
  319. + break;
  320. + }
  321. +
  322. + data = tx_buf[controller->tx_bytes];
  323. + word |= data << (BITS_PER_BYTE * (3 - idx));
  324. + }
  325. +
  326. + writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO);
  327. + }
  328. +}
  329. +
  330. +static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
  331. +{
  332. + struct spi_qup *controller = dev_id;
  333. + struct spi_transfer *xfer;
  334. + u32 opflags, qup_err, spi_err;
  335. + unsigned long flags;
  336. + int error = 0;
  337. +
  338. + spin_lock_irqsave(&controller->lock, flags);
  339. + xfer = controller->xfer;
  340. + controller->xfer = NULL;
  341. + spin_unlock_irqrestore(&controller->lock, flags);
  342. +
  343. + qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS);
  344. + spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS);
  345. + opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
  346. +
  347. + writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS);
  348. + writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS);
  349. + writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
  350. +
  351. + if (!xfer) {
  352. + dev_err_ratelimited(controller->dev, "unexpected irq %x08 %x08 %x08\n",
  353. + qup_err, spi_err, opflags);
  354. + return IRQ_HANDLED;
  355. + }
  356. +
  357. + if (qup_err) {
  358. + if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN)
  359. + dev_warn(controller->dev, "OUTPUT_OVER_RUN\n");
  360. + if (qup_err & QUP_ERROR_INPUT_UNDER_RUN)
  361. + dev_warn(controller->dev, "INPUT_UNDER_RUN\n");
  362. + if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN)
  363. + dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n");
  364. + if (qup_err & QUP_ERROR_INPUT_OVER_RUN)
  365. + dev_warn(controller->dev, "INPUT_OVER_RUN\n");
  366. +
  367. + error = -EIO;
  368. + }
  369. +
  370. + if (spi_err) {
  371. + if (spi_err & SPI_ERROR_CLK_OVER_RUN)
  372. + dev_warn(controller->dev, "CLK_OVER_RUN\n");
  373. + if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
  374. + dev_warn(controller->dev, "CLK_UNDER_RUN\n");
  375. +
  376. + error = -EIO;
  377. + }
  378. +
  379. + if (opflags & QUP_OP_IN_SERVICE_FLAG)
  380. + spi_qup_fifo_read(controller, xfer);
  381. +
  382. + if (opflags & QUP_OP_OUT_SERVICE_FLAG)
  383. + spi_qup_fifo_write(controller, xfer);
  384. +
  385. + spin_lock_irqsave(&controller->lock, flags);
  386. + controller->error = error;
  387. + controller->xfer = xfer;
  388. + spin_unlock_irqrestore(&controller->lock, flags);
  389. +
  390. + if (controller->rx_bytes == xfer->len || error)
  391. + complete(&controller->done);
  392. +
  393. + return IRQ_HANDLED;
  394. +}
  395. +
  396. +
  397. +/* set clock freq ... bits per word */
  398. +static int spi_qup_io_config(struct spi_qup *controller,
  399. + struct spi_qup_device *chip,
  400. + struct spi_transfer *xfer)
  401. +{
  402. + u32 config, iomode, mode;
  403. + int ret, n_words, w_size;
  404. +
  405. + if (chip->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
  406. + dev_err(controller->dev, "too big size for loopback %d > %d\n",
  407. + xfer->len, controller->in_fifo_sz);
  408. + return -EIO;
  409. + }
  410. +
  411. + ret = clk_set_rate(controller->cclk, xfer->speed_hz);
  412. + if (ret) {
  413. + dev_err(controller->dev, "fail to set frequency %d",
  414. + xfer->speed_hz);
  415. + return -EIO;
  416. + }
  417. +
  418. + if (spi_qup_set_state(controller, QUP_STATE_RESET)) {
  419. + dev_err(controller->dev, "cannot set RESET state\n");
  420. + return -EIO;
  421. + }
  422. +
  423. + w_size = 4;
  424. + if (xfer->bits_per_word <= 8)
  425. + w_size = 1;
  426. + else if (xfer->bits_per_word <= 16)
  427. + w_size = 2;
  428. +
  429. + n_words = xfer->len / w_size;
  430. + controller->w_size = w_size;
  431. +
  432. + if (n_words <= controller->in_fifo_sz) {
  433. + mode = QUP_IO_M_MODE_FIFO;
  434. + writel_relaxed(n_words, controller->base + QUP_MX_READ_CNT);
  435. + writel_relaxed(n_words, controller->base + QUP_MX_WRITE_CNT);
  436. + /* must be zero for FIFO */
  437. + writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
  438. + writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  439. + } else {
  440. + mode = QUP_IO_M_MODE_BLOCK;
  441. + writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT);
  442. + writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT);
  443. + /* must be zero for BLOCK and BAM */
  444. + writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  445. + writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  446. + }
  447. +
  448. + iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
  449. + /* Set input and output transfer mode */
  450. + iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
  451. + iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
  452. + iomode |= (mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
  453. + iomode |= (mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
  454. +
  455. + writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
  456. +
  457. + config = readl_relaxed(controller->base + SPI_CONFIG);
  458. +
  459. + if (chip->mode & SPI_LOOP)
  460. + config |= SPI_CONFIG_LOOPBACK;
  461. + else
  462. + config &= ~SPI_CONFIG_LOOPBACK;
  463. +
  464. + if (chip->mode & SPI_CPHA)
  465. + config &= ~SPI_CONFIG_INPUT_FIRST;
  466. + else
  467. + config |= SPI_CONFIG_INPUT_FIRST;
  468. +
  469. + /*
  470. + * HS_MODE improves signal stability for spi-clk high rates,
  471. + * but is invalid in loop back mode.
  472. + */
  473. + if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(chip->mode & SPI_LOOP))
  474. + config |= SPI_CONFIG_HS_MODE;
  475. + else
  476. + config &= ~SPI_CONFIG_HS_MODE;
  477. +
  478. + writel_relaxed(config, controller->base + SPI_CONFIG);
  479. +
  480. + config = readl_relaxed(controller->base + QUP_CONFIG);
  481. + config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N);
  482. + config |= xfer->bits_per_word - 1;
  483. + config |= QUP_CONFIG_SPI_MODE;
  484. + writel_relaxed(config, controller->base + QUP_CONFIG);
  485. +
  486. + writel_relaxed(0, controller->base + QUP_OPERATIONAL_MASK);
  487. + return 0;
  488. +}
  489. +
  490. +static void spi_qup_set_cs(struct spi_device *spi, bool enable)
  491. +{
  492. + struct spi_qup *controller = spi_master_get_devdata(spi->master);
  493. + struct spi_qup_device *chip = spi_get_ctldata(spi);
  494. +
  495. + u32 iocontol, mask;
  496. +
  497. + iocontol = readl_relaxed(controller->base + SPI_IO_CONTROL);
  498. +
  499. + /* Disable auto CS toggle and use manual */
  500. + iocontol &= ~SPI_IO_C_MX_CS_MODE;
  501. + iocontol |= SPI_IO_C_FORCE_CS;
  502. +
  503. + iocontol &= ~SPI_IO_C_CS_SELECT_MASK;
  504. + iocontol |= SPI_IO_C_CS_SELECT(chip->select);
  505. +
  506. + mask = SPI_IO_C_CS_N_POLARITY_0 << chip->select;
  507. +
  508. + if (enable)
  509. + iocontol |= mask;
  510. + else
  511. + iocontol &= ~mask;
  512. +
  513. + writel_relaxed(iocontol, controller->base + SPI_IO_CONTROL);
  514. +}
  515. +
  516. +static int spi_qup_transfer_one(struct spi_master *master,
  517. + struct spi_device *spi,
  518. + struct spi_transfer *xfer)
  519. +{
  520. + struct spi_qup *controller = spi_master_get_devdata(master);
  521. + struct spi_qup_device *chip = spi_get_ctldata(spi);
  522. + unsigned long timeout, flags;
  523. + int ret = -EIO;
  524. +
  525. + ret = spi_qup_io_config(controller, chip, xfer);
  526. + if (ret)
  527. + return ret;
  528. +
  529. + timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
  530. + timeout = DIV_ROUND_UP(xfer->len * 8, timeout);
  531. + timeout = 100 * msecs_to_jiffies(timeout);
  532. +
  533. + reinit_completion(&controller->done);
  534. +
  535. + spin_lock_irqsave(&controller->lock, flags);
  536. + controller->xfer = xfer;
  537. + controller->error = 0;
  538. + controller->rx_bytes = 0;
  539. + controller->tx_bytes = 0;
  540. + spin_unlock_irqrestore(&controller->lock, flags);
  541. +
  542. + if (spi_qup_set_state(controller, QUP_STATE_RUN)) {
  543. + dev_warn(controller->dev, "cannot set RUN state\n");
  544. + goto exit;
  545. + }
  546. +
  547. + if (spi_qup_set_state(controller, QUP_STATE_PAUSE)) {
  548. + dev_warn(controller->dev, "cannot set PAUSE state\n");
  549. + goto exit;
  550. + }
  551. +
  552. + spi_qup_fifo_write(controller, xfer);
  553. +
  554. + if (spi_qup_set_state(controller, QUP_STATE_RUN)) {
  555. + dev_warn(controller->dev, "cannot set EXECUTE state\n");
  556. + goto exit;
  557. + }
  558. +
  559. + if (!wait_for_completion_timeout(&controller->done, timeout))
  560. + ret = -ETIMEDOUT;
  561. +exit:
  562. + spi_qup_set_state(controller, QUP_STATE_RESET);
  563. + spin_lock_irqsave(&controller->lock, flags);
  564. + controller->xfer = NULL;
  565. + if (!ret)
  566. + ret = controller->error;
  567. + spin_unlock_irqrestore(&controller->lock, flags);
  568. + return ret;
  569. +}
  570. +
  571. +static int spi_qup_setup(struct spi_device *spi)
  572. +{
  573. + struct spi_qup *controller = spi_master_get_devdata(spi->master);
  574. + struct spi_qup_device *chip = spi_get_ctldata(spi);
  575. +
  576. + if (spi->chip_select >= spi->master->num_chipselect) {
  577. + dev_err(controller->dev, "invalid chip_select %d\n",
  578. + spi->chip_select);
  579. + return -EINVAL;
  580. + }
  581. +
  582. + if (spi->max_speed_hz > controller->max_speed_hz) {
  583. + dev_err(controller->dev, "invalid max_speed_hz %d\n",
  584. + spi->max_speed_hz);
  585. + return -EINVAL;
  586. + }
  587. +
  588. + if (!chip) {
  589. + /* First setup */
  590. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  591. + if (!chip) {
  592. + dev_err(controller->dev, "no memory for chip data\n");
  593. + return -ENOMEM;
  594. + }
  595. +
  596. + chip->mode = spi->mode;
  597. + chip->select = spi->chip_select;
  598. + spi_set_ctldata(spi, chip);
  599. + }
  600. +
  601. + return 0;
  602. +}
  603. +
  604. +static void spi_qup_cleanup(struct spi_device *spi)
  605. +{
  606. + struct spi_qup_device *chip = spi_get_ctldata(spi);
  607. +
  608. + if (!chip)
  609. + return;
  610. +
  611. + spi_set_ctldata(spi, NULL);
  612. + kfree(chip);
  613. +}
  614. +
  615. +static int spi_qup_probe(struct platform_device *pdev)
  616. +{
  617. + struct spi_master *master;
  618. + struct clk *iclk, *cclk;
  619. + struct spi_qup *controller;
  620. + struct resource *res;
  621. + struct device *dev;
  622. + void __iomem *base;
  623. + u32 data, max_freq, iomode;
  624. + int ret, irq, size;
  625. +
  626. + dev = &pdev->dev;
  627. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  628. + base = devm_ioremap_resource(dev, res);
  629. + if (IS_ERR(base))
  630. + return PTR_ERR(base);
  631. +
  632. + irq = platform_get_irq(pdev, 0);
  633. +
  634. + if (irq < 0)
  635. + return irq;
  636. +
  637. + cclk = devm_clk_get(dev, "core");
  638. + if (IS_ERR(cclk))
  639. + return PTR_ERR(cclk);
  640. +
  641. + iclk = devm_clk_get(dev, "iface");
  642. + if (IS_ERR(iclk))
  643. + return PTR_ERR(iclk);
  644. +
  645. + /* This is optional parameter */
  646. + if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq))
  647. + max_freq = SPI_MAX_RATE;
  648. +
  649. + if (!max_freq || max_freq > SPI_MAX_RATE) {
  650. + dev_err(dev, "invalid clock frequency %d\n", max_freq);
  651. + return -ENXIO;
  652. + }
  653. +
  654. + ret = clk_prepare_enable(cclk);
  655. + if (ret) {
  656. + dev_err(dev, "cannot enable core clock\n");
  657. + return ret;
  658. + }
  659. +
  660. + ret = clk_prepare_enable(iclk);
  661. + if (ret) {
  662. + clk_disable_unprepare(cclk);
  663. + dev_err(dev, "cannot enable iface clock\n");
  664. + return ret;
  665. + }
  666. +
  667. + data = readl_relaxed(base + QUP_HW_VERSION);
  668. +
  669. + if (data < QUP_HW_VERSION_2_1_1) {
  670. + clk_disable_unprepare(cclk);
  671. + clk_disable_unprepare(iclk);
  672. + dev_err(dev, "v.%08x is not supported\n", data);
  673. + return -ENXIO;
  674. + }
  675. +
  676. + master = spi_alloc_master(dev, sizeof(struct spi_qup));
  677. + if (!master) {
  678. + clk_disable_unprepare(cclk);
  679. + clk_disable_unprepare(iclk);
  680. + dev_err(dev, "cannot allocate master\n");
  681. + return -ENOMEM;
  682. + }
  683. +
  684. + master->bus_num = pdev->id;
  685. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  686. + master->num_chipselect = SPI_NUM_CHIPSELECTS;
  687. + master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  688. + master->setup = spi_qup_setup;
  689. + master->cleanup = spi_qup_cleanup;
  690. + master->set_cs = spi_qup_set_cs;
  691. + master->transfer_one = spi_qup_transfer_one;
  692. + master->dev.of_node = pdev->dev.of_node;
  693. + master->auto_runtime_pm = true;
  694. +
  695. + platform_set_drvdata(pdev, master);
  696. +
  697. + controller = spi_master_get_devdata(master);
  698. +
  699. + controller->dev = dev;
  700. + controller->base = base;
  701. + controller->iclk = iclk;
  702. + controller->cclk = cclk;
  703. + controller->irq = irq;
  704. + controller->max_speed_hz = max_freq;
  705. +
  706. + spin_lock_init(&controller->lock);
  707. + init_completion(&controller->done);
  708. +
  709. + iomode = readl_relaxed(base + QUP_IO_M_MODES);
  710. +
  711. + size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode);
  712. + if (size)
  713. + controller->out_blk_sz = size * 16;
  714. + else
  715. + controller->out_blk_sz = 4;
  716. +
  717. + size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode);
  718. + if (size)
  719. + controller->in_blk_sz = size * 16;
  720. + else
  721. + controller->in_blk_sz = 4;
  722. +
  723. + size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode);
  724. + controller->out_fifo_sz = controller->out_blk_sz * (2 << size);
  725. +
  726. + size = QUP_IO_M_INPUT_FIFO_SIZE(iomode);
  727. + controller->in_fifo_sz = controller->in_blk_sz * (2 << size);
  728. +
  729. + dev_info(dev, "v.%08x IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
  730. + data, controller->in_blk_sz, controller->in_fifo_sz,
  731. + controller->out_blk_sz, controller->out_fifo_sz);
  732. +
  733. + writel_relaxed(1, base + QUP_SW_RESET);
  734. +
  735. + ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  736. + if (ret) {
  737. + dev_err(dev, "cannot set RESET state\n");
  738. + goto error;
  739. + }
  740. +
  741. + writel_relaxed(0, base + QUP_OPERATIONAL);
  742. + writel_relaxed(0, base + QUP_IO_M_MODES);
  743. + writel_relaxed(0, base + QUP_OPERATIONAL_MASK);
  744. + writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN,
  745. + base + SPI_ERROR_FLAGS_EN);
  746. +
  747. + writel_relaxed(0, base + SPI_CONFIG);
  748. + writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL);
  749. +
  750. + ret = devm_request_irq(dev, irq, spi_qup_qup_irq,
  751. + IRQF_TRIGGER_HIGH, pdev->name, controller);
  752. + if (ret)
  753. + goto error;
  754. +
  755. + ret = devm_spi_register_master(dev, master);
  756. + if (ret)
  757. + goto error;
  758. +
  759. + pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
  760. + pm_runtime_use_autosuspend(dev);
  761. + pm_runtime_set_active(dev);
  762. + pm_runtime_enable(dev);
  763. + return 0;
  764. +
  765. +error:
  766. + clk_disable_unprepare(cclk);
  767. + clk_disable_unprepare(iclk);
  768. + spi_master_put(master);
  769. + return ret;
  770. +}
  771. +
  772. +#ifdef CONFIG_PM_RUNTIME
  773. +static int spi_qup_pm_suspend_runtime(struct device *device)
  774. +{
  775. + struct spi_master *master = dev_get_drvdata(device);
  776. + struct spi_qup *controller = spi_master_get_devdata(master);
  777. + u32 config;
  778. +
  779. + /* Enable clocks auto gaiting */
  780. + config = readl(controller->base + QUP_CONFIG);
  781. + config |= QUP_CLOCK_AUTO_GATE;
  782. + writel_relaxed(config, controller->base + QUP_CONFIG);
  783. + return 0;
  784. +}
  785. +
  786. +static int spi_qup_pm_resume_runtime(struct device *device)
  787. +{
  788. + struct spi_master *master = dev_get_drvdata(device);
  789. + struct spi_qup *controller = spi_master_get_devdata(master);
  790. + u32 config;
  791. +
  792. + /* Disable clocks auto gaiting */
  793. + config = readl_relaxed(controller->base + QUP_CONFIG);
  794. + config &= ~QUP_CLOCK_AUTO_GATE;
  795. + writel_relaxed(config, controller->base + QUP_CONFIG);
  796. + return 0;
  797. +}
  798. +#endif /* CONFIG_PM_RUNTIME */
  799. +
  800. +#ifdef CONFIG_PM_SLEEP
  801. +static int spi_qup_suspend(struct device *device)
  802. +{
  803. + struct spi_master *master = dev_get_drvdata(device);
  804. + struct spi_qup *controller = spi_master_get_devdata(master);
  805. + int ret;
  806. +
  807. + ret = spi_master_suspend(master);
  808. + if (ret)
  809. + return ret;
  810. +
  811. + ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  812. + if (ret)
  813. + return ret;
  814. +
  815. + clk_disable_unprepare(controller->cclk);
  816. + clk_disable_unprepare(controller->iclk);
  817. + return 0;
  818. +}
  819. +
  820. +static int spi_qup_resume(struct device *device)
  821. +{
  822. + struct spi_master *master = dev_get_drvdata(device);
  823. + struct spi_qup *controller = spi_master_get_devdata(master);
  824. + int ret;
  825. +
  826. + ret = clk_prepare_enable(controller->iclk);
  827. + if (ret)
  828. + return ret;
  829. +
  830. + ret = clk_prepare_enable(controller->cclk);
  831. + if (ret)
  832. + return ret;
  833. +
  834. + ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  835. + if (ret)
  836. + return ret;
  837. +
  838. + return spi_master_resume(master);
  839. +}
  840. +#endif /* CONFIG_PM_SLEEP */
  841. +
  842. +static int spi_qup_remove(struct platform_device *pdev)
  843. +{
  844. + struct spi_master *master = dev_get_drvdata(&pdev->dev);
  845. + struct spi_qup *controller = spi_master_get_devdata(master);
  846. + int ret;
  847. +
  848. + ret = pm_runtime_get_sync(&pdev->dev);
  849. + if (ret)
  850. + return ret;
  851. +
  852. + ret = spi_qup_set_state(controller, QUP_STATE_RESET);
  853. + if (ret)
  854. + return ret;
  855. +
  856. + clk_disable_unprepare(controller->cclk);
  857. + clk_disable_unprepare(controller->iclk);
  858. +
  859. + pm_runtime_put_noidle(&pdev->dev);
  860. + pm_runtime_disable(&pdev->dev);
  861. + spi_master_put(master);
  862. + return 0;
  863. +}
  864. +
  865. +static struct of_device_id spi_qup_dt_match[] = {
  866. + { .compatible = "qcom,spi-qup-v2.1.1", },
  867. + { .compatible = "qcom,spi-qup-v2.2.1", },
  868. + { }
  869. +};
  870. +MODULE_DEVICE_TABLE(of, spi_qup_dt_match);
  871. +
  872. +static const struct dev_pm_ops spi_qup_dev_pm_ops = {
  873. + SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume)
  874. + SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime,
  875. + spi_qup_pm_resume_runtime,
  876. + NULL)
  877. +};
  878. +
  879. +static struct platform_driver spi_qup_driver = {
  880. + .driver = {
  881. + .name = "spi_qup",
  882. + .owner = THIS_MODULE,
  883. + .pm = &spi_qup_dev_pm_ops,
  884. + .of_match_table = spi_qup_dt_match,
  885. + },
  886. + .probe = spi_qup_probe,
  887. + .remove = spi_qup_remove,
  888. +};
  889. +module_platform_driver(spi_qup_driver);
  890. +
  891. +MODULE_LICENSE("GPL v2");
  892. +MODULE_VERSION("0.4");
  893. +MODULE_ALIAS("platform:spi_qup");