blob: 753bd313e6fdaaabe4063fc068e84d8d05b42983 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Leilk Liua5682312015-08-07 15:19:50 +08002/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: Leilk Liu <leilk.liu@mediatek.com>
Leilk Liua5682312015-08-07 15:19:50 +08005 */
6
7#include <linux/clk.h>
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/interrupt.h>
Leilk Liudd69a0a2015-08-24 11:45:15 +080011#include <linux/io.h>
Leilk Liua5682312015-08-07 15:19:50 +080012#include <linux/ioport.h>
13#include <linux/module.h>
14#include <linux/of.h>
Leilk Liu37457602015-10-26 16:09:44 +080015#include <linux/of_gpio.h>
Leilk Liua5682312015-08-07 15:19:50 +080016#include <linux/platform_device.h>
17#include <linux/platform_data/spi-mt65xx.h>
18#include <linux/pm_runtime.h>
19#include <linux/spi/spi.h>
luhua.xufdeae8f2019-09-11 05:55:31 -040020#include <linux/dma-mapping.h>
Leilk Liua5682312015-08-07 15:19:50 +080021
22#define SPI_CFG0_REG 0x0000
23#define SPI_CFG1_REG 0x0004
24#define SPI_TX_SRC_REG 0x0008
25#define SPI_RX_DST_REG 0x000c
26#define SPI_TX_DATA_REG 0x0010
27#define SPI_RX_DATA_REG 0x0014
28#define SPI_CMD_REG 0x0018
29#define SPI_STATUS0_REG 0x001c
30#define SPI_PAD_SEL_REG 0x0024
Leilk Liu058fe492017-06-12 09:24:39 +080031#define SPI_CFG2_REG 0x0028
luhua.xufdeae8f2019-09-11 05:55:31 -040032#define SPI_TX_SRC_REG_64 0x002c
33#define SPI_RX_DST_REG_64 0x0030
Leilk Liua5682312015-08-07 15:19:50 +080034
35#define SPI_CFG0_SCK_HIGH_OFFSET 0
36#define SPI_CFG0_SCK_LOW_OFFSET 8
37#define SPI_CFG0_CS_HOLD_OFFSET 16
38#define SPI_CFG0_CS_SETUP_OFFSET 24
Leilk Liu058fe492017-06-12 09:24:39 +080039#define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0
40#define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16
Leilk Liua5682312015-08-07 15:19:50 +080041
42#define SPI_CFG1_CS_IDLE_OFFSET 0
43#define SPI_CFG1_PACKET_LOOP_OFFSET 8
44#define SPI_CFG1_PACKET_LENGTH_OFFSET 16
Mason Zhangf84d8662021-07-13 19:40:49 +080045#define SPI_CFG1_GET_TICK_DLY_OFFSET 29
Leilk Liua5682312015-08-07 15:19:50 +080046
Mason Zhangf84d8662021-07-13 19:40:49 +080047#define SPI_CFG1_GET_TICK_DLY_MASK 0xe0000000
Leilk Liua5682312015-08-07 15:19:50 +080048#define SPI_CFG1_CS_IDLE_MASK 0xff
49#define SPI_CFG1_PACKET_LOOP_MASK 0xff00
50#define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
leilk.liu44b37eb2020-07-01 17:00:20 +080051#define SPI_CFG2_SCK_HIGH_OFFSET 0
52#define SPI_CFG2_SCK_LOW_OFFSET 16
Leilk Liua5682312015-08-07 15:19:50 +080053
Leilk Liua71d6ea2015-08-20 17:19:08 +080054#define SPI_CMD_ACT BIT(0)
55#define SPI_CMD_RESUME BIT(1)
Leilk Liua5682312015-08-07 15:19:50 +080056#define SPI_CMD_RST BIT(2)
57#define SPI_CMD_PAUSE_EN BIT(4)
58#define SPI_CMD_DEASSERT BIT(5)
Leilk Liu058fe492017-06-12 09:24:39 +080059#define SPI_CMD_SAMPLE_SEL BIT(6)
60#define SPI_CMD_CS_POL BIT(7)
Leilk Liua5682312015-08-07 15:19:50 +080061#define SPI_CMD_CPHA BIT(8)
62#define SPI_CMD_CPOL BIT(9)
63#define SPI_CMD_RX_DMA BIT(10)
64#define SPI_CMD_TX_DMA BIT(11)
65#define SPI_CMD_TXMSBF BIT(12)
66#define SPI_CMD_RXMSBF BIT(13)
67#define SPI_CMD_RX_ENDIAN BIT(14)
68#define SPI_CMD_TX_ENDIAN BIT(15)
69#define SPI_CMD_FINISH_IE BIT(16)
70#define SPI_CMD_PAUSE_IE BIT(17)
71
Leilk Liua5682312015-08-07 15:19:50 +080072#define MT8173_SPI_MAX_PAD_SEL 3
73
Leilk Liu50f8fec2015-08-24 11:45:16 +080074#define MTK_SPI_PAUSE_INT_STATUS 0x2
75
Leilk Liua5682312015-08-07 15:19:50 +080076#define MTK_SPI_IDLE 0
77#define MTK_SPI_PAUSED 1
78
Daniel Kurtz1ce24862017-01-27 00:21:54 +080079#define MTK_SPI_MAX_FIFO_SIZE 32U
Leilk Liua5682312015-08-07 15:19:50 +080080#define MTK_SPI_PACKET_SIZE 1024
luhua.xufdeae8f2019-09-11 05:55:31 -040081#define MTK_SPI_32BITS_MASK (0xffffffff)
82
83#define DMA_ADDR_EXT_BITS (36)
84#define DMA_ADDR_DEF_BITS (32)
Leilk Liua5682312015-08-07 15:19:50 +080085
86struct mtk_spi_compatible {
Leilk Liuaf579372015-08-20 17:19:07 +080087 bool need_pad_sel;
88 /* Must explicitly send dummy Tx bytes to do Rx only transfer */
89 bool must_tx;
Leilk Liu058fe492017-06-12 09:24:39 +080090 /* some IC design adjust cfg register to enhance time accuracy */
91 bool enhance_timing;
luhua.xufdeae8f2019-09-11 05:55:31 -040092 /* some IC support DMA addr extension */
93 bool dma_ext;
Mason Zhang162a31e2021-06-29 18:08:15 +080094 /* some IC no need unprepare SPI clk */
95 bool no_need_unprepare;
Leilk Liua5682312015-08-07 15:19:50 +080096};
97
98struct mtk_spi {
99 void __iomem *base;
100 u32 state;
Leilk Liu37457602015-10-26 16:09:44 +0800101 int pad_num;
102 u32 *pad_sel;
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800103 struct clk *parent_clk, *sel_clk, *spi_clk;
Leilk Liua5682312015-08-07 15:19:50 +0800104 struct spi_transfer *cur_transfer;
105 u32 xfer_len;
Peter Shih00bca732018-09-10 11:54:21 +0800106 u32 num_xfered;
Leilk Liua5682312015-08-07 15:19:50 +0800107 struct scatterlist *tx_sgl, *rx_sgl;
108 u32 tx_sgl_len, rx_sgl_len;
109 const struct mtk_spi_compatible *dev_comp;
Mason Zhang162a31e2021-06-29 18:08:15 +0800110 u32 spi_clk_hz;
Leilk Liua5682312015-08-07 15:19:50 +0800111};
112
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800113static const struct mtk_spi_compatible mtk_common_compat;
Leilk Liufc4f2262017-06-12 09:24:40 +0800114
leilk.liu@mediatek.comb6b1f2d2017-06-20 16:21:07 +0800115static const struct mtk_spi_compatible mt2712_compat = {
116 .must_tx = true,
117};
118
luhua.xu2c231e02019-09-11 05:55:30 -0400119static const struct mtk_spi_compatible mt6765_compat = {
120 .need_pad_sel = true,
121 .must_tx = true,
122 .enhance_timing = true,
luhua.xufdeae8f2019-09-11 05:55:31 -0400123 .dma_ext = true,
luhua.xu2c231e02019-09-11 05:55:30 -0400124};
125
Leilk Liufc4f2262017-06-12 09:24:40 +0800126static const struct mtk_spi_compatible mt7622_compat = {
127 .must_tx = true,
128 .enhance_timing = true,
129};
130
Leilk Liua5682312015-08-07 15:19:50 +0800131static const struct mtk_spi_compatible mt8173_compat = {
Leilk Liuaf579372015-08-20 17:19:07 +0800132 .need_pad_sel = true,
133 .must_tx = true,
Leilk Liua5682312015-08-07 15:19:50 +0800134};
135
Leilk Liub654aa62018-11-01 14:02:19 +0800136static const struct mtk_spi_compatible mt8183_compat = {
137 .need_pad_sel = true,
138 .must_tx = true,
139 .enhance_timing = true,
140};
141
Mason Zhang162a31e2021-06-29 18:08:15 +0800142static const struct mtk_spi_compatible mt6893_compat = {
143 .need_pad_sel = true,
144 .must_tx = true,
145 .enhance_timing = true,
146 .dma_ext = true,
147 .no_need_unprepare = true,
148};
149
Leilk Liua5682312015-08-07 15:19:50 +0800150/*
151 * A piece of default chip info unless the platform
152 * supplies it.
153 */
154static const struct mtk_chip_config mtk_default_chip_info = {
Leilk Liu058fe492017-06-12 09:24:39 +0800155 .sample_sel = 0,
Mason Zhangf84d8662021-07-13 19:40:49 +0800156 .tick_delay = 0,
Leilk Liua5682312015-08-07 15:19:50 +0800157};
158
159static const struct of_device_id mtk_spi_of_match[] = {
Leilk Liu15bcdefd2015-12-31 10:59:01 +0800160 { .compatible = "mediatek,mt2701-spi",
161 .data = (void *)&mtk_common_compat,
162 },
leilk.liu@mediatek.comb6b1f2d2017-06-20 16:21:07 +0800163 { .compatible = "mediatek,mt2712-spi",
164 .data = (void *)&mt2712_compat,
165 },
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800166 { .compatible = "mediatek,mt6589-spi",
167 .data = (void *)&mtk_common_compat,
168 },
luhua.xu2c231e02019-09-11 05:55:30 -0400169 { .compatible = "mediatek,mt6765-spi",
170 .data = (void *)&mt6765_compat,
171 },
Leilk Liufc4f2262017-06-12 09:24:40 +0800172 { .compatible = "mediatek,mt7622-spi",
173 .data = (void *)&mt7622_compat,
174 },
Leilk Liu942779c2018-11-20 16:41:08 +0800175 { .compatible = "mediatek,mt7629-spi",
176 .data = (void *)&mt7622_compat,
177 },
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800178 { .compatible = "mediatek,mt8135-spi",
179 .data = (void *)&mtk_common_compat,
180 },
181 { .compatible = "mediatek,mt8173-spi",
182 .data = (void *)&mt8173_compat,
183 },
Leilk Liub654aa62018-11-01 14:02:19 +0800184 { .compatible = "mediatek,mt8183-spi",
185 .data = (void *)&mt8183_compat,
186 },
leilk.liu8cf125c2020-07-21 20:24:36 +0800187 { .compatible = "mediatek,mt8192-spi",
188 .data = (void *)&mt6765_compat,
189 },
Mason Zhang162a31e2021-06-29 18:08:15 +0800190 { .compatible = "mediatek,mt6893-spi",
191 .data = (void *)&mt6893_compat,
192 },
Leilk Liua5682312015-08-07 15:19:50 +0800193 {}
194};
195MODULE_DEVICE_TABLE(of, mtk_spi_of_match);
196
197static void mtk_spi_reset(struct mtk_spi *mdata)
198{
199 u32 reg_val;
200
201 /* set the software reset bit in SPI_CMD_REG. */
202 reg_val = readl(mdata->base + SPI_CMD_REG);
203 reg_val |= SPI_CMD_RST;
204 writel(reg_val, mdata->base + SPI_CMD_REG);
205
206 reg_val = readl(mdata->base + SPI_CMD_REG);
207 reg_val &= ~SPI_CMD_RST;
208 writel(reg_val, mdata->base + SPI_CMD_REG);
209}
210
Mason Zhang04e6bb02021-08-04 21:37:47 +0800211static int mtk_spi_set_hw_cs_timing(struct spi_device *spi)
212{
213 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
214 struct spi_delay *cs_setup = &spi->cs_setup;
215 struct spi_delay *cs_hold = &spi->cs_hold;
216 struct spi_delay *cs_inactive = &spi->cs_inactive;
Mason Zhang5c842e52021-08-09 13:59:12 +0800217 u32 setup, hold, inactive;
Mason Zhang04e6bb02021-08-04 21:37:47 +0800218 u32 reg_val;
219 int delay;
220
221 delay = spi_delay_to_ns(cs_setup, NULL);
222 if (delay < 0)
223 return delay;
224 setup = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000;
225
226 delay = spi_delay_to_ns(cs_hold, NULL);
227 if (delay < 0)
228 return delay;
229 hold = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000;
230
231 delay = spi_delay_to_ns(cs_inactive, NULL);
232 if (delay < 0)
233 return delay;
234 inactive = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000;
235
Dafna Hirschfeld3672bb82021-10-01 17:21:53 +0200236 if (hold || setup) {
237 reg_val = readl(mdata->base + SPI_CFG0_REG);
238 if (mdata->dev_comp->enhance_timing) {
239 if (hold) {
240 hold = min_t(u32, hold, 0x10000);
241 reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
242 reg_val |= (((hold - 1) & 0xffff)
243 << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
244 }
245 if (setup) {
246 setup = min_t(u32, setup, 0x10000);
247 reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
248 reg_val |= (((setup - 1) & 0xffff)
249 << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
250 }
251 } else {
252 if (hold) {
253 hold = min_t(u32, hold, 0x100);
254 reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET);
255 reg_val |= (((hold - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET);
256 }
257 if (setup) {
258 setup = min_t(u32, setup, 0x100);
259 reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET);
260 reg_val |= (((setup - 1) & 0xff)
261 << SPI_CFG0_CS_SETUP_OFFSET);
262 }
263 }
264 writel(reg_val, mdata->base + SPI_CFG0_REG);
Mason Zhang04e6bb02021-08-04 21:37:47 +0800265 }
Mason Zhang04e6bb02021-08-04 21:37:47 +0800266
Dafna Hirschfeld3672bb82021-10-01 17:21:53 +0200267 if (inactive) {
268 inactive = min_t(u32, inactive, 0x100);
269 reg_val = readl(mdata->base + SPI_CFG1_REG);
270 reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
271 reg_val |= (((inactive - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET);
272 writel(reg_val, mdata->base + SPI_CFG1_REG);
273 }
Mason Zhang04e6bb02021-08-04 21:37:47 +0800274
275 return 0;
276}
277
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800278static int mtk_spi_prepare_message(struct spi_master *master,
279 struct spi_message *msg)
Leilk Liua5682312015-08-07 15:19:50 +0800280{
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800281 u16 cpha, cpol;
Leilk Liua5682312015-08-07 15:19:50 +0800282 u32 reg_val;
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800283 struct spi_device *spi = msg->spi;
Leilk Liu58a984c72015-10-26 16:09:43 +0800284 struct mtk_chip_config *chip_config = spi->controller_data;
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800285 struct mtk_spi *mdata = spi_master_get_devdata(master);
286
287 cpha = spi->mode & SPI_CPHA ? 1 : 0;
288 cpol = spi->mode & SPI_CPOL ? 1 : 0;
289
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800290 reg_val = readl(mdata->base + SPI_CMD_REG);
291 if (cpha)
292 reg_val |= SPI_CMD_CPHA;
293 else
294 reg_val &= ~SPI_CMD_CPHA;
295 if (cpol)
296 reg_val |= SPI_CMD_CPOL;
297 else
298 reg_val &= ~SPI_CMD_CPOL;
Leilk Liua5682312015-08-07 15:19:50 +0800299
300 /* set the mlsbx and mlsbtx */
Leilk Liu3e582c62019-06-05 11:07:04 +0800301 if (spi->mode & SPI_LSB_FIRST) {
Leilk Liua71d6ea2015-08-20 17:19:08 +0800302 reg_val &= ~SPI_CMD_TXMSBF;
Leilk Liua71d6ea2015-08-20 17:19:08 +0800303 reg_val &= ~SPI_CMD_RXMSBF;
Leilk Liu3e582c62019-06-05 11:07:04 +0800304 } else {
305 reg_val |= SPI_CMD_TXMSBF;
306 reg_val |= SPI_CMD_RXMSBF;
307 }
Leilk Liua5682312015-08-07 15:19:50 +0800308
309 /* set the tx/rx endian */
Leilk Liu44f636d2015-08-20 17:19:06 +0800310#ifdef __LITTLE_ENDIAN
311 reg_val &= ~SPI_CMD_TX_ENDIAN;
312 reg_val &= ~SPI_CMD_RX_ENDIAN;
313#else
314 reg_val |= SPI_CMD_TX_ENDIAN;
315 reg_val |= SPI_CMD_RX_ENDIAN;
316#endif
Leilk Liua5682312015-08-07 15:19:50 +0800317
Leilk Liu058fe492017-06-12 09:24:39 +0800318 if (mdata->dev_comp->enhance_timing) {
Luhua Xuae7c2d32019-11-18 12:57:16 +0800319 /* set CS polarity */
320 if (spi->mode & SPI_CS_HIGH)
Leilk Liu058fe492017-06-12 09:24:39 +0800321 reg_val |= SPI_CMD_CS_POL;
322 else
323 reg_val &= ~SPI_CMD_CS_POL;
Luhua Xuae7c2d32019-11-18 12:57:16 +0800324
Leilk Liu058fe492017-06-12 09:24:39 +0800325 if (chip_config->sample_sel)
326 reg_val |= SPI_CMD_SAMPLE_SEL;
327 else
328 reg_val &= ~SPI_CMD_SAMPLE_SEL;
329 }
330
Leilk Liua5682312015-08-07 15:19:50 +0800331 /* set finish and pause interrupt always enable */
Leilk Liu15293322015-08-27 21:09:04 +0800332 reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE;
Leilk Liua5682312015-08-07 15:19:50 +0800333
334 /* disable dma mode */
335 reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
336
337 /* disable deassert mode */
338 reg_val &= ~SPI_CMD_DEASSERT;
339
340 writel(reg_val, mdata->base + SPI_CMD_REG);
341
342 /* pad select */
343 if (mdata->dev_comp->need_pad_sel)
Leilk Liu37457602015-10-26 16:09:44 +0800344 writel(mdata->pad_sel[spi->chip_select],
345 mdata->base + SPI_PAD_SEL_REG);
Leilk Liua5682312015-08-07 15:19:50 +0800346
Mason Zhangf84d8662021-07-13 19:40:49 +0800347 /* tick delay */
348 reg_val = readl(mdata->base + SPI_CFG1_REG);
349 reg_val &= ~SPI_CFG1_GET_TICK_DLY_MASK;
350 reg_val |= ((chip_config->tick_delay & 0x7)
351 << SPI_CFG1_GET_TICK_DLY_OFFSET);
352 writel(reg_val, mdata->base + SPI_CFG1_REG);
353
Mason Zhang04e6bb02021-08-04 21:37:47 +0800354 /* set hw cs timing */
355 mtk_spi_set_hw_cs_timing(spi);
Leilk Liua5682312015-08-07 15:19:50 +0800356 return 0;
357}
358
359static void mtk_spi_set_cs(struct spi_device *spi, bool enable)
360{
361 u32 reg_val;
362 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
363
Luhua Xuae7c2d32019-11-18 12:57:16 +0800364 if (spi->mode & SPI_CS_HIGH)
365 enable = !enable;
366
Leilk Liua5682312015-08-07 15:19:50 +0800367 reg_val = readl(mdata->base + SPI_CMD_REG);
Leilk Liu6583d202015-09-07 19:37:57 +0800368 if (!enable) {
Leilk Liua5682312015-08-07 15:19:50 +0800369 reg_val |= SPI_CMD_PAUSE_EN;
Leilk Liu6583d202015-09-07 19:37:57 +0800370 writel(reg_val, mdata->base + SPI_CMD_REG);
371 } else {
Leilk Liua5682312015-08-07 15:19:50 +0800372 reg_val &= ~SPI_CMD_PAUSE_EN;
Leilk Liu6583d202015-09-07 19:37:57 +0800373 writel(reg_val, mdata->base + SPI_CMD_REG);
374 mdata->state = MTK_SPI_IDLE;
375 mtk_spi_reset(mdata);
376 }
Leilk Liua5682312015-08-07 15:19:50 +0800377}
378
379static void mtk_spi_prepare_transfer(struct spi_master *master,
380 struct spi_transfer *xfer)
381{
Mason Zhang162a31e2021-06-29 18:08:15 +0800382 u32 div, sck_time, reg_val;
Leilk Liua5682312015-08-07 15:19:50 +0800383 struct mtk_spi *mdata = spi_master_get_devdata(master);
384
Mason Zhang162a31e2021-06-29 18:08:15 +0800385 if (xfer->speed_hz < mdata->spi_clk_hz / 2)
386 div = DIV_ROUND_UP(mdata->spi_clk_hz, xfer->speed_hz);
Leilk Liua5682312015-08-07 15:19:50 +0800387 else
388 div = 1;
389
Leilk Liu2ce0acf2015-08-24 11:45:18 +0800390 sck_time = (div + 1) / 2;
Leilk Liua5682312015-08-07 15:19:50 +0800391
Leilk Liu058fe492017-06-12 09:24:39 +0800392 if (mdata->dev_comp->enhance_timing) {
leilk.liu9f6e7e82021-02-07 11:09:53 +0800393 reg_val = readl(mdata->base + SPI_CFG2_REG);
394 reg_val &= ~(0xffff << SPI_CFG2_SCK_HIGH_OFFSET);
395 reg_val |= (((sck_time - 1) & 0xffff)
leilk.liu44b37eb2020-07-01 17:00:20 +0800396 << SPI_CFG2_SCK_HIGH_OFFSET);
leilk.liu9f6e7e82021-02-07 11:09:53 +0800397 reg_val &= ~(0xffff << SPI_CFG2_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800398 reg_val |= (((sck_time - 1) & 0xffff)
leilk.liu44b37eb2020-07-01 17:00:20 +0800399 << SPI_CFG2_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800400 writel(reg_val, mdata->base + SPI_CFG2_REG);
Leilk Liu058fe492017-06-12 09:24:39 +0800401 } else {
leilk.liu9f6e7e82021-02-07 11:09:53 +0800402 reg_val = readl(mdata->base + SPI_CFG0_REG);
403 reg_val &= ~(0xff << SPI_CFG0_SCK_HIGH_OFFSET);
404 reg_val |= (((sck_time - 1) & 0xff)
Leilk Liu058fe492017-06-12 09:24:39 +0800405 << SPI_CFG0_SCK_HIGH_OFFSET);
leilk.liu9f6e7e82021-02-07 11:09:53 +0800406 reg_val &= ~(0xff << SPI_CFG0_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800407 reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800408 writel(reg_val, mdata->base + SPI_CFG0_REG);
409 }
Leilk Liua5682312015-08-07 15:19:50 +0800410}
411
412static void mtk_spi_setup_packet(struct spi_master *master)
413{
414 u32 packet_size, packet_loop, reg_val;
415 struct mtk_spi *mdata = spi_master_get_devdata(master);
416
Leilk Liu50f8fec2015-08-24 11:45:16 +0800417 packet_size = min_t(u32, mdata->xfer_len, MTK_SPI_PACKET_SIZE);
Leilk Liua5682312015-08-07 15:19:50 +0800418 packet_loop = mdata->xfer_len / packet_size;
419
420 reg_val = readl(mdata->base + SPI_CFG1_REG);
Leilk Liu50f8fec2015-08-24 11:45:16 +0800421 reg_val &= ~(SPI_CFG1_PACKET_LENGTH_MASK | SPI_CFG1_PACKET_LOOP_MASK);
Leilk Liua5682312015-08-07 15:19:50 +0800422 reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
423 reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
424 writel(reg_val, mdata->base + SPI_CFG1_REG);
425}
426
427static void mtk_spi_enable_transfer(struct spi_master *master)
428{
Leilk Liu50f8fec2015-08-24 11:45:16 +0800429 u32 cmd;
Leilk Liua5682312015-08-07 15:19:50 +0800430 struct mtk_spi *mdata = spi_master_get_devdata(master);
431
432 cmd = readl(mdata->base + SPI_CMD_REG);
433 if (mdata->state == MTK_SPI_IDLE)
Leilk Liua71d6ea2015-08-20 17:19:08 +0800434 cmd |= SPI_CMD_ACT;
Leilk Liua5682312015-08-07 15:19:50 +0800435 else
Leilk Liua71d6ea2015-08-20 17:19:08 +0800436 cmd |= SPI_CMD_RESUME;
Leilk Liua5682312015-08-07 15:19:50 +0800437 writel(cmd, mdata->base + SPI_CMD_REG);
438}
439
Leilk Liu50f8fec2015-08-24 11:45:16 +0800440static int mtk_spi_get_mult_delta(u32 xfer_len)
Leilk Liua5682312015-08-07 15:19:50 +0800441{
Leilk Liu50f8fec2015-08-24 11:45:16 +0800442 u32 mult_delta;
Leilk Liua5682312015-08-07 15:19:50 +0800443
444 if (xfer_len > MTK_SPI_PACKET_SIZE)
445 mult_delta = xfer_len % MTK_SPI_PACKET_SIZE;
446 else
447 mult_delta = 0;
448
449 return mult_delta;
450}
451
452static void mtk_spi_update_mdata_len(struct spi_master *master)
453{
454 int mult_delta;
455 struct mtk_spi *mdata = spi_master_get_devdata(master);
456
457 if (mdata->tx_sgl_len && mdata->rx_sgl_len) {
458 if (mdata->tx_sgl_len > mdata->rx_sgl_len) {
459 mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
460 mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
461 mdata->rx_sgl_len = mult_delta;
462 mdata->tx_sgl_len -= mdata->xfer_len;
463 } else {
464 mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
465 mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
466 mdata->tx_sgl_len = mult_delta;
467 mdata->rx_sgl_len -= mdata->xfer_len;
468 }
469 } else if (mdata->tx_sgl_len) {
470 mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
471 mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
472 mdata->tx_sgl_len = mult_delta;
473 } else if (mdata->rx_sgl_len) {
474 mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
475 mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
476 mdata->rx_sgl_len = mult_delta;
477 }
478}
479
480static void mtk_spi_setup_dma_addr(struct spi_master *master,
481 struct spi_transfer *xfer)
482{
483 struct mtk_spi *mdata = spi_master_get_devdata(master);
484
luhua.xufdeae8f2019-09-11 05:55:31 -0400485 if (mdata->tx_sgl) {
486 writel((u32)(xfer->tx_dma & MTK_SPI_32BITS_MASK),
487 mdata->base + SPI_TX_SRC_REG);
488#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
489 if (mdata->dev_comp->dma_ext)
490 writel((u32)(xfer->tx_dma >> 32),
491 mdata->base + SPI_TX_SRC_REG_64);
492#endif
493 }
494
495 if (mdata->rx_sgl) {
496 writel((u32)(xfer->rx_dma & MTK_SPI_32BITS_MASK),
497 mdata->base + SPI_RX_DST_REG);
498#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
499 if (mdata->dev_comp->dma_ext)
500 writel((u32)(xfer->rx_dma >> 32),
501 mdata->base + SPI_RX_DST_REG_64);
502#endif
503 }
Leilk Liua5682312015-08-07 15:19:50 +0800504}
505
506static int mtk_spi_fifo_transfer(struct spi_master *master,
507 struct spi_device *spi,
508 struct spi_transfer *xfer)
509{
Nicolas Boichatde327e42015-12-27 18:17:06 +0800510 int cnt, remainder;
511 u32 reg_val;
Leilk Liua5682312015-08-07 15:19:50 +0800512 struct mtk_spi *mdata = spi_master_get_devdata(master);
513
514 mdata->cur_transfer = xfer;
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800515 mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len);
Peter Shih00bca732018-09-10 11:54:21 +0800516 mdata->num_xfered = 0;
Leilk Liua5682312015-08-07 15:19:50 +0800517 mtk_spi_prepare_transfer(master, xfer);
518 mtk_spi_setup_packet(master);
519
Guenter Roeck0d5c3952021-08-01 20:00:23 -0700520 if (xfer->tx_buf) {
521 cnt = xfer->len / 4;
Peter Hess3a70dd22021-07-06 14:16:09 +0200522 iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt);
Guenter Roeck0d5c3952021-08-01 20:00:23 -0700523 remainder = xfer->len % 4;
524 if (remainder > 0) {
525 reg_val = 0;
Peter Hess3a70dd22021-07-06 14:16:09 +0200526 memcpy(&reg_val, xfer->tx_buf + (cnt * 4), remainder);
527 writel(reg_val, mdata->base + SPI_TX_DATA_REG);
528 }
Nicolas Boichatde327e42015-12-27 18:17:06 +0800529 }
530
Leilk Liua5682312015-08-07 15:19:50 +0800531 mtk_spi_enable_transfer(master);
532
533 return 1;
534}
535
536static int mtk_spi_dma_transfer(struct spi_master *master,
537 struct spi_device *spi,
538 struct spi_transfer *xfer)
539{
540 int cmd;
541 struct mtk_spi *mdata = spi_master_get_devdata(master);
542
543 mdata->tx_sgl = NULL;
544 mdata->rx_sgl = NULL;
545 mdata->tx_sgl_len = 0;
546 mdata->rx_sgl_len = 0;
547 mdata->cur_transfer = xfer;
Peter Shih00bca732018-09-10 11:54:21 +0800548 mdata->num_xfered = 0;
Leilk Liua5682312015-08-07 15:19:50 +0800549
550 mtk_spi_prepare_transfer(master, xfer);
551
552 cmd = readl(mdata->base + SPI_CMD_REG);
553 if (xfer->tx_buf)
554 cmd |= SPI_CMD_TX_DMA;
555 if (xfer->rx_buf)
556 cmd |= SPI_CMD_RX_DMA;
557 writel(cmd, mdata->base + SPI_CMD_REG);
558
559 if (xfer->tx_buf)
560 mdata->tx_sgl = xfer->tx_sg.sgl;
561 if (xfer->rx_buf)
562 mdata->rx_sgl = xfer->rx_sg.sgl;
563
564 if (mdata->tx_sgl) {
565 xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
566 mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
567 }
568 if (mdata->rx_sgl) {
569 xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
570 mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
571 }
572
573 mtk_spi_update_mdata_len(master);
574 mtk_spi_setup_packet(master);
575 mtk_spi_setup_dma_addr(master, xfer);
576 mtk_spi_enable_transfer(master);
577
578 return 1;
579}
580
581static int mtk_spi_transfer_one(struct spi_master *master,
582 struct spi_device *spi,
583 struct spi_transfer *xfer)
584{
585 if (master->can_dma(master, spi, xfer))
586 return mtk_spi_dma_transfer(master, spi, xfer);
587 else
588 return mtk_spi_fifo_transfer(master, spi, xfer);
589}
590
591static bool mtk_spi_can_dma(struct spi_master *master,
592 struct spi_device *spi,
593 struct spi_transfer *xfer)
594{
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800595 /* Buffers for DMA transactions must be 4-byte aligned */
596 return (xfer->len > MTK_SPI_MAX_FIFO_SIZE &&
597 (unsigned long)xfer->tx_buf % 4 == 0 &&
598 (unsigned long)xfer->rx_buf % 4 == 0);
Leilk Liua5682312015-08-07 15:19:50 +0800599}
600
Leilk Liu58a984c72015-10-26 16:09:43 +0800601static int mtk_spi_setup(struct spi_device *spi)
602{
603 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
604
605 if (!spi->controller_data)
606 spi->controller_data = (void *)&mtk_default_chip_info;
607
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800608 if (mdata->dev_comp->need_pad_sel && gpio_is_valid(spi->cs_gpio))
Leilk Liu37457602015-10-26 16:09:44 +0800609 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
610
Leilk Liu58a984c72015-10-26 16:09:43 +0800611 return 0;
612}
613
Leilk Liua5682312015-08-07 15:19:50 +0800614static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id)
615{
Peter Shih00bca732018-09-10 11:54:21 +0800616 u32 cmd, reg_val, cnt, remainder, len;
Leilk Liua5682312015-08-07 15:19:50 +0800617 struct spi_master *master = dev_id;
618 struct mtk_spi *mdata = spi_master_get_devdata(master);
619 struct spi_transfer *trans = mdata->cur_transfer;
620
621 reg_val = readl(mdata->base + SPI_STATUS0_REG);
Leilk Liu50f8fec2015-08-24 11:45:16 +0800622 if (reg_val & MTK_SPI_PAUSE_INT_STATUS)
Leilk Liua5682312015-08-07 15:19:50 +0800623 mdata->state = MTK_SPI_PAUSED;
624 else
625 mdata->state = MTK_SPI_IDLE;
626
Benjamin Gaignardf83a96e2022-01-31 15:17:08 +0100627 if (!master->can_dma(master, NULL, trans)) {
Leilk Liua5682312015-08-07 15:19:50 +0800628 if (trans->rx_buf) {
Nicolas Boichatde327e42015-12-27 18:17:06 +0800629 cnt = mdata->xfer_len / 4;
Leilk Liu44f636d2015-08-20 17:19:06 +0800630 ioread32_rep(mdata->base + SPI_RX_DATA_REG,
Peter Shih00bca732018-09-10 11:54:21 +0800631 trans->rx_buf + mdata->num_xfered, cnt);
Nicolas Boichatde327e42015-12-27 18:17:06 +0800632 remainder = mdata->xfer_len % 4;
633 if (remainder > 0) {
634 reg_val = readl(mdata->base + SPI_RX_DATA_REG);
Peter Shih00bca732018-09-10 11:54:21 +0800635 memcpy(trans->rx_buf +
636 mdata->num_xfered +
637 (cnt * 4),
638 &reg_val,
639 remainder);
Nicolas Boichatde327e42015-12-27 18:17:06 +0800640 }
Leilk Liua5682312015-08-07 15:19:50 +0800641 }
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800642
Peter Shih00bca732018-09-10 11:54:21 +0800643 mdata->num_xfered += mdata->xfer_len;
644 if (mdata->num_xfered == trans->len) {
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800645 spi_finalize_current_transfer(master);
646 return IRQ_HANDLED;
647 }
648
Peter Shih00bca732018-09-10 11:54:21 +0800649 len = trans->len - mdata->num_xfered;
650 mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, len);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800651 mtk_spi_setup_packet(master);
652
Leilk Liua4d8f642018-10-31 16:49:16 +0800653 cnt = mdata->xfer_len / 4;
Peter Shih00bca732018-09-10 11:54:21 +0800654 iowrite32_rep(mdata->base + SPI_TX_DATA_REG,
655 trans->tx_buf + mdata->num_xfered, cnt);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800656
Leilk Liua4d8f642018-10-31 16:49:16 +0800657 remainder = mdata->xfer_len % 4;
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800658 if (remainder > 0) {
659 reg_val = 0;
Peter Shih00bca732018-09-10 11:54:21 +0800660 memcpy(&reg_val,
661 trans->tx_buf + (cnt * 4) + mdata->num_xfered,
662 remainder);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800663 writel(reg_val, mdata->base + SPI_TX_DATA_REG);
664 }
665
666 mtk_spi_enable_transfer(master);
667
Leilk Liua5682312015-08-07 15:19:50 +0800668 return IRQ_HANDLED;
669 }
670
671 if (mdata->tx_sgl)
672 trans->tx_dma += mdata->xfer_len;
673 if (mdata->rx_sgl)
674 trans->rx_dma += mdata->xfer_len;
675
676 if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) {
677 mdata->tx_sgl = sg_next(mdata->tx_sgl);
678 if (mdata->tx_sgl) {
679 trans->tx_dma = sg_dma_address(mdata->tx_sgl);
680 mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
681 }
682 }
683 if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) {
684 mdata->rx_sgl = sg_next(mdata->rx_sgl);
685 if (mdata->rx_sgl) {
686 trans->rx_dma = sg_dma_address(mdata->rx_sgl);
687 mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
688 }
689 }
690
691 if (!mdata->tx_sgl && !mdata->rx_sgl) {
692 /* spi disable dma */
693 cmd = readl(mdata->base + SPI_CMD_REG);
694 cmd &= ~SPI_CMD_TX_DMA;
695 cmd &= ~SPI_CMD_RX_DMA;
696 writel(cmd, mdata->base + SPI_CMD_REG);
697
698 spi_finalize_current_transfer(master);
699 return IRQ_HANDLED;
700 }
701
702 mtk_spi_update_mdata_len(master);
703 mtk_spi_setup_packet(master);
704 mtk_spi_setup_dma_addr(master, trans);
705 mtk_spi_enable_transfer(master);
706
707 return IRQ_HANDLED;
708}
709
710static int mtk_spi_probe(struct platform_device *pdev)
711{
712 struct spi_master *master;
713 struct mtk_spi *mdata;
714 const struct of_device_id *of_id;
luhua.xufdeae8f2019-09-11 05:55:31 -0400715 int i, irq, ret, addr_bits;
Leilk Liua5682312015-08-07 15:19:50 +0800716
717 master = spi_alloc_master(&pdev->dev, sizeof(*mdata));
718 if (!master) {
719 dev_err(&pdev->dev, "failed to alloc spi master\n");
720 return -ENOMEM;
721 }
722
723 master->auto_runtime_pm = true;
724 master->dev.of_node = pdev->dev.of_node;
Leilk Liu3e582c62019-06-05 11:07:04 +0800725 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Leilk Liua5682312015-08-07 15:19:50 +0800726
727 master->set_cs = mtk_spi_set_cs;
Leilk Liua5682312015-08-07 15:19:50 +0800728 master->prepare_message = mtk_spi_prepare_message;
729 master->transfer_one = mtk_spi_transfer_one;
730 master->can_dma = mtk_spi_can_dma;
Leilk Liu58a984c72015-10-26 16:09:43 +0800731 master->setup = mtk_spi_setup;
leilk.liu9f6e7e82021-02-07 11:09:53 +0800732 master->set_cs_timing = mtk_spi_set_hw_cs_timing;
Leilk Liua5682312015-08-07 15:19:50 +0800733
734 of_id = of_match_node(mtk_spi_of_match, pdev->dev.of_node);
735 if (!of_id) {
736 dev_err(&pdev->dev, "failed to probe of_node\n");
737 ret = -EINVAL;
738 goto err_put_master;
739 }
740
741 mdata = spi_master_get_devdata(master);
742 mdata->dev_comp = of_id->data;
Luhua Xuae7c2d32019-11-18 12:57:16 +0800743
744 if (mdata->dev_comp->enhance_timing)
745 master->mode_bits |= SPI_CS_HIGH;
746
Leilk Liua5682312015-08-07 15:19:50 +0800747 if (mdata->dev_comp->must_tx)
748 master->flags = SPI_MASTER_MUST_TX;
749
750 if (mdata->dev_comp->need_pad_sel) {
Leilk Liu37457602015-10-26 16:09:44 +0800751 mdata->pad_num = of_property_count_u32_elems(
752 pdev->dev.of_node,
753 "mediatek,pad-select");
754 if (mdata->pad_num < 0) {
755 dev_err(&pdev->dev,
756 "No 'mediatek,pad-select' property\n");
757 ret = -EINVAL;
Leilk Liua5682312015-08-07 15:19:50 +0800758 goto err_put_master;
759 }
760
Leilk Liu37457602015-10-26 16:09:44 +0800761 mdata->pad_sel = devm_kmalloc_array(&pdev->dev, mdata->pad_num,
762 sizeof(u32), GFP_KERNEL);
763 if (!mdata->pad_sel) {
764 ret = -ENOMEM;
Leilk Liua5682312015-08-07 15:19:50 +0800765 goto err_put_master;
766 }
Leilk Liu37457602015-10-26 16:09:44 +0800767
768 for (i = 0; i < mdata->pad_num; i++) {
769 of_property_read_u32_index(pdev->dev.of_node,
770 "mediatek,pad-select",
771 i, &mdata->pad_sel[i]);
772 if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL) {
773 dev_err(&pdev->dev, "wrong pad-sel[%d]: %u\n",
774 i, mdata->pad_sel[i]);
775 ret = -EINVAL;
776 goto err_put_master;
777 }
778 }
Leilk Liua5682312015-08-07 15:19:50 +0800779 }
780
781 platform_set_drvdata(pdev, master);
Markus Elfring5dd381e2019-09-21 14:45:40 +0200782 mdata->base = devm_platform_ioremap_resource(pdev, 0);
Leilk Liua5682312015-08-07 15:19:50 +0800783 if (IS_ERR(mdata->base)) {
784 ret = PTR_ERR(mdata->base);
785 goto err_put_master;
786 }
787
788 irq = platform_get_irq(pdev, 0);
789 if (irq < 0) {
Leilk Liua5682312015-08-07 15:19:50 +0800790 ret = irq;
791 goto err_put_master;
792 }
793
794 if (!pdev->dev.dma_mask)
795 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
796
797 ret = devm_request_irq(&pdev->dev, irq, mtk_spi_interrupt,
798 IRQF_TRIGGER_NONE, dev_name(&pdev->dev), master);
799 if (ret) {
800 dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
801 goto err_put_master;
802 }
803
Leilk Liua5682312015-08-07 15:19:50 +0800804 mdata->parent_clk = devm_clk_get(&pdev->dev, "parent-clk");
805 if (IS_ERR(mdata->parent_clk)) {
806 ret = PTR_ERR(mdata->parent_clk);
807 dev_err(&pdev->dev, "failed to get parent-clk: %d\n", ret);
808 goto err_put_master;
809 }
810
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800811 mdata->sel_clk = devm_clk_get(&pdev->dev, "sel-clk");
812 if (IS_ERR(mdata->sel_clk)) {
Javier Martinez Canillase26d15f2015-09-15 14:46:45 +0200813 ret = PTR_ERR(mdata->sel_clk);
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800814 dev_err(&pdev->dev, "failed to get sel-clk: %d\n", ret);
815 goto err_put_master;
816 }
817
818 mdata->spi_clk = devm_clk_get(&pdev->dev, "spi-clk");
819 if (IS_ERR(mdata->spi_clk)) {
Javier Martinez Canillase26d15f2015-09-15 14:46:45 +0200820 ret = PTR_ERR(mdata->spi_clk);
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800821 dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret);
822 goto err_put_master;
823 }
824
Leilk Liua5682312015-08-07 15:19:50 +0800825 ret = clk_prepare_enable(mdata->spi_clk);
826 if (ret < 0) {
827 dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret);
828 goto err_put_master;
829 }
830
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800831 ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800832 if (ret < 0) {
833 dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret);
Leilk Liue38da372015-11-25 17:50:38 +0800834 clk_disable_unprepare(mdata->spi_clk);
835 goto err_put_master;
Leilk Liua5682312015-08-07 15:19:50 +0800836 }
837
Mason Zhang162a31e2021-06-29 18:08:15 +0800838 mdata->spi_clk_hz = clk_get_rate(mdata->spi_clk);
839
840 if (mdata->dev_comp->no_need_unprepare)
841 clk_disable(mdata->spi_clk);
842 else
843 clk_disable_unprepare(mdata->spi_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800844
845 pm_runtime_enable(&pdev->dev);
846
Leilk Liu37457602015-10-26 16:09:44 +0800847 if (mdata->dev_comp->need_pad_sel) {
848 if (mdata->pad_num != master->num_chipselect) {
849 dev_err(&pdev->dev,
850 "pad_num does not match num_chipselect(%d != %d)\n",
851 mdata->pad_num, master->num_chipselect);
852 ret = -EINVAL;
Leilk Liue38da372015-11-25 17:50:38 +0800853 goto err_disable_runtime_pm;
Leilk Liu37457602015-10-26 16:09:44 +0800854 }
855
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800856 if (!master->cs_gpios && master->num_chipselect > 1) {
857 dev_err(&pdev->dev,
858 "cs_gpios not specified and num_chipselect > 1\n");
859 ret = -EINVAL;
Leilk Liue38da372015-11-25 17:50:38 +0800860 goto err_disable_runtime_pm;
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800861 }
862
863 if (master->cs_gpios) {
864 for (i = 0; i < master->num_chipselect; i++) {
865 ret = devm_gpio_request(&pdev->dev,
866 master->cs_gpios[i],
867 dev_name(&pdev->dev));
868 if (ret) {
869 dev_err(&pdev->dev,
870 "can't get CS GPIO %i\n", i);
Leilk Liue38da372015-11-25 17:50:38 +0800871 goto err_disable_runtime_pm;
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800872 }
Leilk Liu37457602015-10-26 16:09:44 +0800873 }
874 }
875 }
876
luhua.xufdeae8f2019-09-11 05:55:31 -0400877 if (mdata->dev_comp->dma_ext)
878 addr_bits = DMA_ADDR_EXT_BITS;
879 else
880 addr_bits = DMA_ADDR_DEF_BITS;
881 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(addr_bits));
882 if (ret)
883 dev_notice(&pdev->dev, "SPI dma_set_mask(%d) failed, ret:%d\n",
884 addr_bits, ret);
885
Mason Zhangc934fec2021-07-13 19:42:48 +0800886 ret = devm_spi_register_master(&pdev->dev, master);
887 if (ret) {
888 dev_err(&pdev->dev, "failed to register master (%d)\n", ret);
889 goto err_disable_runtime_pm;
890 }
891
Leilk Liua5682312015-08-07 15:19:50 +0800892 return 0;
893
Leilk Liue38da372015-11-25 17:50:38 +0800894err_disable_runtime_pm:
895 pm_runtime_disable(&pdev->dev);
Leilk Liua5682312015-08-07 15:19:50 +0800896err_put_master:
897 spi_master_put(master);
898
899 return ret;
900}
901
902static int mtk_spi_remove(struct platform_device *pdev)
903{
904 struct spi_master *master = platform_get_drvdata(pdev);
905 struct mtk_spi *mdata = spi_master_get_devdata(master);
906
907 pm_runtime_disable(&pdev->dev);
908
909 mtk_spi_reset(mdata);
Leilk Liua5682312015-08-07 15:19:50 +0800910
Mason Zhang162a31e2021-06-29 18:08:15 +0800911 if (mdata->dev_comp->no_need_unprepare)
912 clk_unprepare(mdata->spi_clk);
913
Leilk Liua5682312015-08-07 15:19:50 +0800914 return 0;
915}
916
917#ifdef CONFIG_PM_SLEEP
918static int mtk_spi_suspend(struct device *dev)
919{
920 int ret;
921 struct spi_master *master = dev_get_drvdata(dev);
922 struct mtk_spi *mdata = spi_master_get_devdata(master);
923
924 ret = spi_master_suspend(master);
925 if (ret)
926 return ret;
927
928 if (!pm_runtime_suspended(dev))
929 clk_disable_unprepare(mdata->spi_clk);
930
931 return ret;
932}
933
934static int mtk_spi_resume(struct device *dev)
935{
936 int ret;
937 struct spi_master *master = dev_get_drvdata(dev);
938 struct mtk_spi *mdata = spi_master_get_devdata(master);
939
940 if (!pm_runtime_suspended(dev)) {
941 ret = clk_prepare_enable(mdata->spi_clk);
Leilk Liu13da5a02015-08-24 11:45:17 +0800942 if (ret < 0) {
943 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
Leilk Liua5682312015-08-07 15:19:50 +0800944 return ret;
Leilk Liu13da5a02015-08-24 11:45:17 +0800945 }
Leilk Liua5682312015-08-07 15:19:50 +0800946 }
947
948 ret = spi_master_resume(master);
949 if (ret < 0)
950 clk_disable_unprepare(mdata->spi_clk);
951
952 return ret;
953}
954#endif /* CONFIG_PM_SLEEP */
955
956#ifdef CONFIG_PM
957static int mtk_spi_runtime_suspend(struct device *dev)
958{
959 struct spi_master *master = dev_get_drvdata(dev);
960 struct mtk_spi *mdata = spi_master_get_devdata(master);
961
Mason Zhang162a31e2021-06-29 18:08:15 +0800962 if (mdata->dev_comp->no_need_unprepare)
963 clk_disable(mdata->spi_clk);
964 else
965 clk_disable_unprepare(mdata->spi_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800966
967 return 0;
968}
969
970static int mtk_spi_runtime_resume(struct device *dev)
971{
972 struct spi_master *master = dev_get_drvdata(dev);
973 struct mtk_spi *mdata = spi_master_get_devdata(master);
Leilk Liu13da5a02015-08-24 11:45:17 +0800974 int ret;
Leilk Liua5682312015-08-07 15:19:50 +0800975
Mason Zhang162a31e2021-06-29 18:08:15 +0800976 if (mdata->dev_comp->no_need_unprepare)
977 ret = clk_enable(mdata->spi_clk);
978 else
979 ret = clk_prepare_enable(mdata->spi_clk);
Leilk Liu13da5a02015-08-24 11:45:17 +0800980 if (ret < 0) {
981 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
982 return ret;
983 }
984
985 return 0;
Leilk Liua5682312015-08-07 15:19:50 +0800986}
987#endif /* CONFIG_PM */
988
989static const struct dev_pm_ops mtk_spi_pm = {
990 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume)
991 SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend,
992 mtk_spi_runtime_resume, NULL)
993};
994
kbuild test robot4299aaa2015-08-07 22:33:11 +0800995static struct platform_driver mtk_spi_driver = {
Leilk Liua5682312015-08-07 15:19:50 +0800996 .driver = {
997 .name = "mtk-spi",
998 .pm = &mtk_spi_pm,
999 .of_match_table = mtk_spi_of_match,
1000 },
1001 .probe = mtk_spi_probe,
1002 .remove = mtk_spi_remove,
1003};
1004
1005module_platform_driver(mtk_spi_driver);
1006
1007MODULE_DESCRIPTION("MTK SPI Controller driver");
1008MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
1009MODULE_LICENSE("GPL v2");
Axel Line4001882015-08-11 09:15:30 +08001010MODULE_ALIAS("platform:mtk-spi");