spi-mt65xx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: Leilk Liu <leilk.liu@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/platform_data/spi-mt65xx.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/spi/spi.h>
  27. #define SPI_CFG0_REG 0x0000
  28. #define SPI_CFG1_REG 0x0004
  29. #define SPI_TX_SRC_REG 0x0008
  30. #define SPI_RX_DST_REG 0x000c
  31. #define SPI_TX_DATA_REG 0x0010
  32. #define SPI_RX_DATA_REG 0x0014
  33. #define SPI_CMD_REG 0x0018
  34. #define SPI_STATUS0_REG 0x001c
  35. #define SPI_PAD_SEL_REG 0x0024
  36. #define SPI_CFG2_REG 0x0028
  37. #define SPI_CFG0_SCK_HIGH_OFFSET 0
  38. #define SPI_CFG0_SCK_LOW_OFFSET 8
  39. #define SPI_CFG0_CS_HOLD_OFFSET 16
  40. #define SPI_CFG0_CS_SETUP_OFFSET 24
  41. #define SPI_ADJUST_CFG0_SCK_LOW_OFFSET 16
  42. #define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0
  43. #define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16
  44. #define SPI_CFG1_CS_IDLE_OFFSET 0
  45. #define SPI_CFG1_PACKET_LOOP_OFFSET 8
  46. #define SPI_CFG1_PACKET_LENGTH_OFFSET 16
  47. #define SPI_CFG1_GET_TICK_DLY_OFFSET 30
  48. #define SPI_CFG1_CS_IDLE_MASK 0xff
  49. #define SPI_CFG1_PACKET_LOOP_MASK 0xff00
  50. #define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
  51. #define SPI_CMD_ACT BIT(0)
  52. #define SPI_CMD_RESUME BIT(1)
  53. #define SPI_CMD_RST BIT(2)
  54. #define SPI_CMD_PAUSE_EN BIT(4)
  55. #define SPI_CMD_DEASSERT BIT(5)
  56. #define SPI_CMD_SAMPLE_SEL BIT(6)
  57. #define SPI_CMD_CS_POL BIT(7)
  58. #define SPI_CMD_CPHA BIT(8)
  59. #define SPI_CMD_CPOL BIT(9)
  60. #define SPI_CMD_RX_DMA BIT(10)
  61. #define SPI_CMD_TX_DMA BIT(11)
  62. #define SPI_CMD_TXMSBF BIT(12)
  63. #define SPI_CMD_RXMSBF BIT(13)
  64. #define SPI_CMD_RX_ENDIAN BIT(14)
  65. #define SPI_CMD_TX_ENDIAN BIT(15)
  66. #define SPI_CMD_FINISH_IE BIT(16)
  67. #define SPI_CMD_PAUSE_IE BIT(17)
  68. #define MT8173_SPI_MAX_PAD_SEL 3
  69. #define MTK_SPI_PAUSE_INT_STATUS 0x2
  70. #define MTK_SPI_IDLE 0
  71. #define MTK_SPI_PAUSED 1
  72. #define MTK_SPI_MAX_FIFO_SIZE 32U
  73. #define MTK_SPI_PACKET_SIZE 1024
  74. struct mtk_spi_compatible {
  75. bool need_pad_sel;
  76. /* Must explicitly send dummy Tx bytes to do Rx only transfer */
  77. bool must_tx;
  78. /* some IC design adjust cfg register to enhance time accuracy */
  79. bool enhance_timing;
  80. };
  81. struct mtk_spi {
  82. void __iomem *base;
  83. u32 state;
  84. int pad_num;
  85. u32 *pad_sel;
  86. struct clk *parent_clk, *sel_clk, *spi_clk;
  87. struct spi_transfer *cur_transfer;
  88. u32 xfer_len;
  89. u32 num_xfered;
  90. struct scatterlist *tx_sgl, *rx_sgl;
  91. u32 tx_sgl_len, rx_sgl_len;
  92. const struct mtk_spi_compatible *dev_comp;
  93. };
  94. static const struct mtk_spi_compatible mtk_common_compat;
  95. static const struct mtk_spi_compatible mt2712_compat = {
  96. .must_tx = true,
  97. };
  98. static const struct mtk_spi_compatible mt7622_compat = {
  99. .must_tx = true,
  100. .enhance_timing = true,
  101. };
  102. static const struct mtk_spi_compatible mt8173_compat = {
  103. .need_pad_sel = true,
  104. .must_tx = true,
  105. };
  106. /*
  107. * A piece of default chip info unless the platform
  108. * supplies it.
  109. */
  110. static const struct mtk_chip_config mtk_default_chip_info = {
  111. .rx_mlsb = 1,
  112. .tx_mlsb = 1,
  113. .cs_pol = 0,
  114. .sample_sel = 0,
  115. };
  116. static const struct of_device_id mtk_spi_of_match[] = {
  117. { .compatible = "mediatek,mt2701-spi",
  118. .data = (void *)&mtk_common_compat,
  119. },
  120. { .compatible = "mediatek,mt2712-spi",
  121. .data = (void *)&mt2712_compat,
  122. },
  123. { .compatible = "mediatek,mt6589-spi",
  124. .data = (void *)&mtk_common_compat,
  125. },
  126. { .compatible = "mediatek,mt7622-spi",
  127. .data = (void *)&mt7622_compat,
  128. },
  129. { .compatible = "mediatek,mt8135-spi",
  130. .data = (void *)&mtk_common_compat,
  131. },
  132. { .compatible = "mediatek,mt8173-spi",
  133. .data = (void *)&mt8173_compat,
  134. },
  135. {}
  136. };
  137. MODULE_DEVICE_TABLE(of, mtk_spi_of_match);
  138. static void mtk_spi_reset(struct mtk_spi *mdata)
  139. {
  140. u32 reg_val;
  141. /* set the software reset bit in SPI_CMD_REG. */
  142. reg_val = readl(mdata->base + SPI_CMD_REG);
  143. reg_val |= SPI_CMD_RST;
  144. writel(reg_val, mdata->base + SPI_CMD_REG);
  145. reg_val = readl(mdata->base + SPI_CMD_REG);
  146. reg_val &= ~SPI_CMD_RST;
  147. writel(reg_val, mdata->base + SPI_CMD_REG);
  148. }
  149. static int mtk_spi_prepare_message(struct spi_master *master,
  150. struct spi_message *msg)
  151. {
  152. u16 cpha, cpol;
  153. u32 reg_val;
  154. struct spi_device *spi = msg->spi;
  155. struct mtk_chip_config *chip_config = spi->controller_data;
  156. struct mtk_spi *mdata = spi_master_get_devdata(master);
  157. cpha = spi->mode & SPI_CPHA ? 1 : 0;
  158. cpol = spi->mode & SPI_CPOL ? 1 : 0;
  159. reg_val = readl(mdata->base + SPI_CMD_REG);
  160. if (cpha)
  161. reg_val |= SPI_CMD_CPHA;
  162. else
  163. reg_val &= ~SPI_CMD_CPHA;
  164. if (cpol)
  165. reg_val |= SPI_CMD_CPOL;
  166. else
  167. reg_val &= ~SPI_CMD_CPOL;
  168. /* set the mlsbx and mlsbtx */
  169. if (chip_config->tx_mlsb)
  170. reg_val |= SPI_CMD_TXMSBF;
  171. else
  172. reg_val &= ~SPI_CMD_TXMSBF;
  173. if (chip_config->rx_mlsb)
  174. reg_val |= SPI_CMD_RXMSBF;
  175. else
  176. reg_val &= ~SPI_CMD_RXMSBF;
  177. /* set the tx/rx endian */
  178. #ifdef __LITTLE_ENDIAN
  179. reg_val &= ~SPI_CMD_TX_ENDIAN;
  180. reg_val &= ~SPI_CMD_RX_ENDIAN;
  181. #else
  182. reg_val |= SPI_CMD_TX_ENDIAN;
  183. reg_val |= SPI_CMD_RX_ENDIAN;
  184. #endif
  185. if (mdata->dev_comp->enhance_timing) {
  186. if (chip_config->cs_pol)
  187. reg_val |= SPI_CMD_CS_POL;
  188. else
  189. reg_val &= ~SPI_CMD_CS_POL;
  190. if (chip_config->sample_sel)
  191. reg_val |= SPI_CMD_SAMPLE_SEL;
  192. else
  193. reg_val &= ~SPI_CMD_SAMPLE_SEL;
  194. }
  195. /* set finish and pause interrupt always enable */
  196. reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE;
  197. /* disable dma mode */
  198. reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
  199. /* disable deassert mode */
  200. reg_val &= ~SPI_CMD_DEASSERT;
  201. writel(reg_val, mdata->base + SPI_CMD_REG);
  202. /* pad select */
  203. if (mdata->dev_comp->need_pad_sel)
  204. writel(mdata->pad_sel[spi->chip_select],
  205. mdata->base + SPI_PAD_SEL_REG);
  206. return 0;
  207. }
  208. static void mtk_spi_set_cs(struct spi_device *spi, bool enable)
  209. {
  210. u32 reg_val;
  211. struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
  212. reg_val = readl(mdata->base + SPI_CMD_REG);
  213. if (!enable) {
  214. reg_val |= SPI_CMD_PAUSE_EN;
  215. writel(reg_val, mdata->base + SPI_CMD_REG);
  216. } else {
  217. reg_val &= ~SPI_CMD_PAUSE_EN;
  218. writel(reg_val, mdata->base + SPI_CMD_REG);
  219. mdata->state = MTK_SPI_IDLE;
  220. mtk_spi_reset(mdata);
  221. }
  222. }
  223. static void mtk_spi_prepare_transfer(struct spi_master *master,
  224. struct spi_transfer *xfer)
  225. {
  226. u32 spi_clk_hz, div, sck_time, cs_time, reg_val = 0;
  227. struct mtk_spi *mdata = spi_master_get_devdata(master);
  228. spi_clk_hz = clk_get_rate(mdata->spi_clk);
  229. if (xfer->speed_hz < spi_clk_hz / 2)
  230. div = DIV_ROUND_UP(spi_clk_hz, xfer->speed_hz);
  231. else
  232. div = 1;
  233. sck_time = (div + 1) / 2;
  234. cs_time = sck_time * 2;
  235. if (mdata->dev_comp->enhance_timing) {
  236. reg_val |= (((sck_time - 1) & 0xffff)
  237. << SPI_CFG0_SCK_HIGH_OFFSET);
  238. reg_val |= (((sck_time - 1) & 0xffff)
  239. << SPI_ADJUST_CFG0_SCK_LOW_OFFSET);
  240. writel(reg_val, mdata->base + SPI_CFG2_REG);
  241. reg_val |= (((cs_time - 1) & 0xffff)
  242. << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
  243. reg_val |= (((cs_time - 1) & 0xffff)
  244. << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
  245. writel(reg_val, mdata->base + SPI_CFG0_REG);
  246. } else {
  247. reg_val |= (((sck_time - 1) & 0xff)
  248. << SPI_CFG0_SCK_HIGH_OFFSET);
  249. reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET);
  250. reg_val |= (((cs_time - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET);
  251. reg_val |= (((cs_time - 1) & 0xff) << SPI_CFG0_CS_SETUP_OFFSET);
  252. writel(reg_val, mdata->base + SPI_CFG0_REG);
  253. }
  254. reg_val = readl(mdata->base + SPI_CFG1_REG);
  255. reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
  256. reg_val |= (((cs_time - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET);
  257. writel(reg_val, mdata->base + SPI_CFG1_REG);
  258. }
  259. static void mtk_spi_setup_packet(struct spi_master *master)
  260. {
  261. u32 packet_size, packet_loop, reg_val;
  262. struct mtk_spi *mdata = spi_master_get_devdata(master);
  263. packet_size = min_t(u32, mdata->xfer_len, MTK_SPI_PACKET_SIZE);
  264. packet_loop = mdata->xfer_len / packet_size;
  265. reg_val = readl(mdata->base + SPI_CFG1_REG);
  266. reg_val &= ~(SPI_CFG1_PACKET_LENGTH_MASK | SPI_CFG1_PACKET_LOOP_MASK);
  267. reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
  268. reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
  269. writel(reg_val, mdata->base + SPI_CFG1_REG);
  270. }
  271. static void mtk_spi_enable_transfer(struct spi_master *master)
  272. {
  273. u32 cmd;
  274. struct mtk_spi *mdata = spi_master_get_devdata(master);
  275. cmd = readl(mdata->base + SPI_CMD_REG);
  276. if (mdata->state == MTK_SPI_IDLE)
  277. cmd |= SPI_CMD_ACT;
  278. else
  279. cmd |= SPI_CMD_RESUME;
  280. writel(cmd, mdata->base + SPI_CMD_REG);
  281. }
  282. static int mtk_spi_get_mult_delta(u32 xfer_len)
  283. {
  284. u32 mult_delta;
  285. if (xfer_len > MTK_SPI_PACKET_SIZE)
  286. mult_delta = xfer_len % MTK_SPI_PACKET_SIZE;
  287. else
  288. mult_delta = 0;
  289. return mult_delta;
  290. }
  291. static void mtk_spi_update_mdata_len(struct spi_master *master)
  292. {
  293. int mult_delta;
  294. struct mtk_spi *mdata = spi_master_get_devdata(master);
  295. if (mdata->tx_sgl_len && mdata->rx_sgl_len) {
  296. if (mdata->tx_sgl_len > mdata->rx_sgl_len) {
  297. mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
  298. mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
  299. mdata->rx_sgl_len = mult_delta;
  300. mdata->tx_sgl_len -= mdata->xfer_len;
  301. } else {
  302. mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
  303. mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
  304. mdata->tx_sgl_len = mult_delta;
  305. mdata->rx_sgl_len -= mdata->xfer_len;
  306. }
  307. } else if (mdata->tx_sgl_len) {
  308. mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
  309. mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
  310. mdata->tx_sgl_len = mult_delta;
  311. } else if (mdata->rx_sgl_len) {
  312. mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
  313. mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
  314. mdata->rx_sgl_len = mult_delta;
  315. }
  316. }
  317. static void mtk_spi_setup_dma_addr(struct spi_master *master,
  318. struct spi_transfer *xfer)
  319. {
  320. struct mtk_spi *mdata = spi_master_get_devdata(master);
  321. if (mdata->tx_sgl)
  322. writel(xfer->tx_dma, mdata->base + SPI_TX_SRC_REG);
  323. if (mdata->rx_sgl)
  324. writel(xfer->rx_dma, mdata->base + SPI_RX_DST_REG);
  325. }
  326. static int mtk_spi_fifo_transfer(struct spi_master *master,
  327. struct spi_device *spi,
  328. struct spi_transfer *xfer)
  329. {
  330. int cnt, remainder;
  331. u32 reg_val;
  332. struct mtk_spi *mdata = spi_master_get_devdata(master);
  333. mdata->cur_transfer = xfer;
  334. mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len);
  335. mdata->num_xfered = 0;
  336. mtk_spi_prepare_transfer(master, xfer);
  337. mtk_spi_setup_packet(master);
  338. cnt = xfer->len / 4;
  339. iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt);
  340. remainder = xfer->len % 4;
  341. if (remainder > 0) {
  342. reg_val = 0;
  343. memcpy(&reg_val, xfer->tx_buf + (cnt * 4), remainder);
  344. writel(reg_val, mdata->base + SPI_TX_DATA_REG);
  345. }
  346. mtk_spi_enable_transfer(master);
  347. return 1;
  348. }
  349. static int mtk_spi_dma_transfer(struct spi_master *master,
  350. struct spi_device *spi,
  351. struct spi_transfer *xfer)
  352. {
  353. int cmd;
  354. struct mtk_spi *mdata = spi_master_get_devdata(master);
  355. mdata->tx_sgl = NULL;
  356. mdata->rx_sgl = NULL;
  357. mdata->tx_sgl_len = 0;
  358. mdata->rx_sgl_len = 0;
  359. mdata->cur_transfer = xfer;
  360. mdata->num_xfered = 0;
  361. mtk_spi_prepare_transfer(master, xfer);
  362. cmd = readl(mdata->base + SPI_CMD_REG);
  363. if (xfer->tx_buf)
  364. cmd |= SPI_CMD_TX_DMA;
  365. if (xfer->rx_buf)
  366. cmd |= SPI_CMD_RX_DMA;
  367. writel(cmd, mdata->base + SPI_CMD_REG);
  368. if (xfer->tx_buf)
  369. mdata->tx_sgl = xfer->tx_sg.sgl;
  370. if (xfer->rx_buf)
  371. mdata->rx_sgl = xfer->rx_sg.sgl;
  372. if (mdata->tx_sgl) {
  373. xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
  374. mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
  375. }
  376. if (mdata->rx_sgl) {
  377. xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
  378. mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
  379. }
  380. mtk_spi_update_mdata_len(master);
  381. mtk_spi_setup_packet(master);
  382. mtk_spi_setup_dma_addr(master, xfer);
  383. mtk_spi_enable_transfer(master);
  384. return 1;
  385. }
  386. static int mtk_spi_transfer_one(struct spi_master *master,
  387. struct spi_device *spi,
  388. struct spi_transfer *xfer)
  389. {
  390. if (master->can_dma(master, spi, xfer))
  391. return mtk_spi_dma_transfer(master, spi, xfer);
  392. else
  393. return mtk_spi_fifo_transfer(master, spi, xfer);
  394. }
  395. static bool mtk_spi_can_dma(struct spi_master *master,
  396. struct spi_device *spi,
  397. struct spi_transfer *xfer)
  398. {
  399. /* Buffers for DMA transactions must be 4-byte aligned */
  400. return (xfer->len > MTK_SPI_MAX_FIFO_SIZE &&
  401. (unsigned long)xfer->tx_buf % 4 == 0 &&
  402. (unsigned long)xfer->rx_buf % 4 == 0);
  403. }
  404. static int mtk_spi_setup(struct spi_device *spi)
  405. {
  406. struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
  407. if (!spi->controller_data)
  408. spi->controller_data = (void *)&mtk_default_chip_info;
  409. if (mdata->dev_comp->need_pad_sel && gpio_is_valid(spi->cs_gpio))
  410. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  411. return 0;
  412. }
  413. static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id)
  414. {
  415. u32 cmd, reg_val, cnt, remainder, len;
  416. struct spi_master *master = dev_id;
  417. struct mtk_spi *mdata = spi_master_get_devdata(master);
  418. struct spi_transfer *trans = mdata->cur_transfer;
  419. reg_val = readl(mdata->base + SPI_STATUS0_REG);
  420. if (reg_val & MTK_SPI_PAUSE_INT_STATUS)
  421. mdata->state = MTK_SPI_PAUSED;
  422. else
  423. mdata->state = MTK_SPI_IDLE;
  424. if (!master->can_dma(master, master->cur_msg->spi, trans)) {
  425. if (trans->rx_buf) {
  426. cnt = mdata->xfer_len / 4;
  427. ioread32_rep(mdata->base + SPI_RX_DATA_REG,
  428. trans->rx_buf + mdata->num_xfered, cnt);
  429. remainder = mdata->xfer_len % 4;
  430. if (remainder > 0) {
  431. reg_val = readl(mdata->base + SPI_RX_DATA_REG);
  432. memcpy(trans->rx_buf +
  433. mdata->num_xfered +
  434. (cnt * 4),
  435. &reg_val,
  436. remainder);
  437. }
  438. }
  439. mdata->num_xfered += mdata->xfer_len;
  440. if (mdata->num_xfered == trans->len) {
  441. spi_finalize_current_transfer(master);
  442. return IRQ_HANDLED;
  443. }
  444. len = trans->len - mdata->num_xfered;
  445. mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, len);
  446. mtk_spi_setup_packet(master);
  447. cnt = mdata->xfer_len / 4;
  448. iowrite32_rep(mdata->base + SPI_TX_DATA_REG,
  449. trans->tx_buf + mdata->num_xfered, cnt);
  450. remainder = mdata->xfer_len % 4;
  451. if (remainder > 0) {
  452. reg_val = 0;
  453. memcpy(&reg_val,
  454. trans->tx_buf + (cnt * 4) + mdata->num_xfered,
  455. remainder);
  456. writel(reg_val, mdata->base + SPI_TX_DATA_REG);
  457. }
  458. mtk_spi_enable_transfer(master);
  459. return IRQ_HANDLED;
  460. }
  461. if (mdata->tx_sgl)
  462. trans->tx_dma += mdata->xfer_len;
  463. if (mdata->rx_sgl)
  464. trans->rx_dma += mdata->xfer_len;
  465. if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) {
  466. mdata->tx_sgl = sg_next(mdata->tx_sgl);
  467. if (mdata->tx_sgl) {
  468. trans->tx_dma = sg_dma_address(mdata->tx_sgl);
  469. mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
  470. }
  471. }
  472. if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) {
  473. mdata->rx_sgl = sg_next(mdata->rx_sgl);
  474. if (mdata->rx_sgl) {
  475. trans->rx_dma = sg_dma_address(mdata->rx_sgl);
  476. mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
  477. }
  478. }
  479. if (!mdata->tx_sgl && !mdata->rx_sgl) {
  480. /* spi disable dma */
  481. cmd = readl(mdata->base + SPI_CMD_REG);
  482. cmd &= ~SPI_CMD_TX_DMA;
  483. cmd &= ~SPI_CMD_RX_DMA;
  484. writel(cmd, mdata->base + SPI_CMD_REG);
  485. spi_finalize_current_transfer(master);
  486. return IRQ_HANDLED;
  487. }
  488. mtk_spi_update_mdata_len(master);
  489. mtk_spi_setup_packet(master);
  490. mtk_spi_setup_dma_addr(master, trans);
  491. mtk_spi_enable_transfer(master);
  492. return IRQ_HANDLED;
  493. }
  494. static int mtk_spi_probe(struct platform_device *pdev)
  495. {
  496. struct spi_master *master;
  497. struct mtk_spi *mdata;
  498. const struct of_device_id *of_id;
  499. struct resource *res;
  500. int i, irq, ret;
  501. master = spi_alloc_master(&pdev->dev, sizeof(*mdata));
  502. if (!master) {
  503. dev_err(&pdev->dev, "failed to alloc spi master\n");
  504. return -ENOMEM;
  505. }
  506. master->auto_runtime_pm = true;
  507. master->dev.of_node = pdev->dev.of_node;
  508. master->mode_bits = SPI_CPOL | SPI_CPHA;
  509. master->set_cs = mtk_spi_set_cs;
  510. master->prepare_message = mtk_spi_prepare_message;
  511. master->transfer_one = mtk_spi_transfer_one;
  512. master->can_dma = mtk_spi_can_dma;
  513. master->setup = mtk_spi_setup;
  514. of_id = of_match_node(mtk_spi_of_match, pdev->dev.of_node);
  515. if (!of_id) {
  516. dev_err(&pdev->dev, "failed to probe of_node\n");
  517. ret = -EINVAL;
  518. goto err_put_master;
  519. }
  520. mdata = spi_master_get_devdata(master);
  521. mdata->dev_comp = of_id->data;
  522. if (mdata->dev_comp->must_tx)
  523. master->flags = SPI_MASTER_MUST_TX;
  524. if (mdata->dev_comp->need_pad_sel) {
  525. mdata->pad_num = of_property_count_u32_elems(
  526. pdev->dev.of_node,
  527. "mediatek,pad-select");
  528. if (mdata->pad_num < 0) {
  529. dev_err(&pdev->dev,
  530. "No 'mediatek,pad-select' property\n");
  531. ret = -EINVAL;
  532. goto err_put_master;
  533. }
  534. mdata->pad_sel = devm_kmalloc_array(&pdev->dev, mdata->pad_num,
  535. sizeof(u32), GFP_KERNEL);
  536. if (!mdata->pad_sel) {
  537. ret = -ENOMEM;
  538. goto err_put_master;
  539. }
  540. for (i = 0; i < mdata->pad_num; i++) {
  541. of_property_read_u32_index(pdev->dev.of_node,
  542. "mediatek,pad-select",
  543. i, &mdata->pad_sel[i]);
  544. if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL) {
  545. dev_err(&pdev->dev, "wrong pad-sel[%d]: %u\n",
  546. i, mdata->pad_sel[i]);
  547. ret = -EINVAL;
  548. goto err_put_master;
  549. }
  550. }
  551. }
  552. platform_set_drvdata(pdev, master);
  553. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  554. if (!res) {
  555. ret = -ENODEV;
  556. dev_err(&pdev->dev, "failed to determine base address\n");
  557. goto err_put_master;
  558. }
  559. mdata->base = devm_ioremap_resource(&pdev->dev, res);
  560. if (IS_ERR(mdata->base)) {
  561. ret = PTR_ERR(mdata->base);
  562. goto err_put_master;
  563. }
  564. irq = platform_get_irq(pdev, 0);
  565. if (irq < 0) {
  566. dev_err(&pdev->dev, "failed to get irq (%d)\n", irq);
  567. ret = irq;
  568. goto err_put_master;
  569. }
  570. if (!pdev->dev.dma_mask)
  571. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  572. ret = devm_request_irq(&pdev->dev, irq, mtk_spi_interrupt,
  573. IRQF_TRIGGER_NONE, dev_name(&pdev->dev), master);
  574. if (ret) {
  575. dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
  576. goto err_put_master;
  577. }
  578. mdata->parent_clk = devm_clk_get(&pdev->dev, "parent-clk");
  579. if (IS_ERR(mdata->parent_clk)) {
  580. ret = PTR_ERR(mdata->parent_clk);
  581. dev_err(&pdev->dev, "failed to get parent-clk: %d\n", ret);
  582. goto err_put_master;
  583. }
  584. mdata->sel_clk = devm_clk_get(&pdev->dev, "sel-clk");
  585. if (IS_ERR(mdata->sel_clk)) {
  586. ret = PTR_ERR(mdata->sel_clk);
  587. dev_err(&pdev->dev, "failed to get sel-clk: %d\n", ret);
  588. goto err_put_master;
  589. }
  590. mdata->spi_clk = devm_clk_get(&pdev->dev, "spi-clk");
  591. if (IS_ERR(mdata->spi_clk)) {
  592. ret = PTR_ERR(mdata->spi_clk);
  593. dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret);
  594. goto err_put_master;
  595. }
  596. ret = clk_prepare_enable(mdata->spi_clk);
  597. if (ret < 0) {
  598. dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret);
  599. goto err_put_master;
  600. }
  601. ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk);
  602. if (ret < 0) {
  603. dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret);
  604. clk_disable_unprepare(mdata->spi_clk);
  605. goto err_put_master;
  606. }
  607. clk_disable_unprepare(mdata->spi_clk);
  608. pm_runtime_enable(&pdev->dev);
  609. ret = devm_spi_register_master(&pdev->dev, master);
  610. if (ret) {
  611. dev_err(&pdev->dev, "failed to register master (%d)\n", ret);
  612. goto err_disable_runtime_pm;
  613. }
  614. if (mdata->dev_comp->need_pad_sel) {
  615. if (mdata->pad_num != master->num_chipselect) {
  616. dev_err(&pdev->dev,
  617. "pad_num does not match num_chipselect(%d != %d)\n",
  618. mdata->pad_num, master->num_chipselect);
  619. ret = -EINVAL;
  620. goto err_disable_runtime_pm;
  621. }
  622. if (!master->cs_gpios && master->num_chipselect > 1) {
  623. dev_err(&pdev->dev,
  624. "cs_gpios not specified and num_chipselect > 1\n");
  625. ret = -EINVAL;
  626. goto err_disable_runtime_pm;
  627. }
  628. if (master->cs_gpios) {
  629. for (i = 0; i < master->num_chipselect; i++) {
  630. ret = devm_gpio_request(&pdev->dev,
  631. master->cs_gpios[i],
  632. dev_name(&pdev->dev));
  633. if (ret) {
  634. dev_err(&pdev->dev,
  635. "can't get CS GPIO %i\n", i);
  636. goto err_disable_runtime_pm;
  637. }
  638. }
  639. }
  640. }
  641. return 0;
  642. err_disable_runtime_pm:
  643. pm_runtime_disable(&pdev->dev);
  644. err_put_master:
  645. spi_master_put(master);
  646. return ret;
  647. }
  648. static int mtk_spi_remove(struct platform_device *pdev)
  649. {
  650. struct spi_master *master = platform_get_drvdata(pdev);
  651. struct mtk_spi *mdata = spi_master_get_devdata(master);
  652. pm_runtime_disable(&pdev->dev);
  653. mtk_spi_reset(mdata);
  654. return 0;
  655. }
  656. #ifdef CONFIG_PM_SLEEP
  657. static int mtk_spi_suspend(struct device *dev)
  658. {
  659. int ret;
  660. struct spi_master *master = dev_get_drvdata(dev);
  661. struct mtk_spi *mdata = spi_master_get_devdata(master);
  662. ret = spi_master_suspend(master);
  663. if (ret)
  664. return ret;
  665. if (!pm_runtime_suspended(dev))
  666. clk_disable_unprepare(mdata->spi_clk);
  667. return ret;
  668. }
  669. static int mtk_spi_resume(struct device *dev)
  670. {
  671. int ret;
  672. struct spi_master *master = dev_get_drvdata(dev);
  673. struct mtk_spi *mdata = spi_master_get_devdata(master);
  674. if (!pm_runtime_suspended(dev)) {
  675. ret = clk_prepare_enable(mdata->spi_clk);
  676. if (ret < 0) {
  677. dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
  678. return ret;
  679. }
  680. }
  681. ret = spi_master_resume(master);
  682. if (ret < 0)
  683. clk_disable_unprepare(mdata->spi_clk);
  684. return ret;
  685. }
  686. #endif /* CONFIG_PM_SLEEP */
  687. #ifdef CONFIG_PM
  688. static int mtk_spi_runtime_suspend(struct device *dev)
  689. {
  690. struct spi_master *master = dev_get_drvdata(dev);
  691. struct mtk_spi *mdata = spi_master_get_devdata(master);
  692. clk_disable_unprepare(mdata->spi_clk);
  693. return 0;
  694. }
  695. static int mtk_spi_runtime_resume(struct device *dev)
  696. {
  697. struct spi_master *master = dev_get_drvdata(dev);
  698. struct mtk_spi *mdata = spi_master_get_devdata(master);
  699. int ret;
  700. ret = clk_prepare_enable(mdata->spi_clk);
  701. if (ret < 0) {
  702. dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
  703. return ret;
  704. }
  705. return 0;
  706. }
  707. #endif /* CONFIG_PM */
  708. static const struct dev_pm_ops mtk_spi_pm = {
  709. SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume)
  710. SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend,
  711. mtk_spi_runtime_resume, NULL)
  712. };
  713. static struct platform_driver mtk_spi_driver = {
  714. .driver = {
  715. .name = "mtk-spi",
  716. .pm = &mtk_spi_pm,
  717. .of_match_table = mtk_spi_of_match,
  718. },
  719. .probe = mtk_spi_probe,
  720. .remove = mtk_spi_remove,
  721. };
  722. module_platform_driver(mtk_spi_driver);
  723. MODULE_DESCRIPTION("MTK SPI Controller driver");
  724. MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
  725. MODULE_LICENSE("GPL v2");
  726. MODULE_ALIAS("platform:mtk-spi");