blob: 90f4c3cecb934002b1fbc27a7067c7f2ef0b0a42 [file] [log] [blame]
developer24202202022-09-09 19:59:45 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2022 MediaTek Inc. All Rights Reserved.
4 *
5 * Author: SkyLake.Huang <skylake.huang@mediatek.com>
6 */
7
8#include <clk.h>
9#include <cpu_func.h>
10#include <div64.h>
11#include <dm.h>
12#include <spi.h>
13#include <spi-mem.h>
14#include <stdbool.h>
15#include <watchdog.h>
16#include <dm/device.h>
17#include <dm/device_compat.h>
18#include <dm/devres.h>
19#include <dm/pinctrl.h>
20#include <linux/bitops.h>
21#include <linux/completion.h>
22#include <linux/dma-mapping.h>
23#include <linux/io.h>
24#include <linux/iopoll.h>
Tom Rini5ba346a2022-10-28 20:27:08 -040025#include <linux/sizes.h>
developer24202202022-09-09 19:59:45 +080026
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_IRQ_REG 0x001c
35#define SPI_STATUS_REG 0x0020
36#define SPI_PAD_SEL_REG 0x0024
37#define SPI_CFG2_REG 0x0028
38#define SPI_TX_SRC_REG_64 0x002c
39#define SPI_RX_DST_REG_64 0x0030
40#define SPI_CFG3_IPM_REG 0x0040
41
42#define SPI_CFG0_SCK_HIGH_OFFSET 0
43#define SPI_CFG0_SCK_LOW_OFFSET 8
44#define SPI_CFG0_CS_HOLD_OFFSET 16
45#define SPI_CFG0_CS_SETUP_OFFSET 24
46#define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0
47#define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16
48
49#define SPI_CFG1_CS_IDLE_OFFSET 0
50#define SPI_CFG1_PACKET_LOOP_OFFSET 8
51#define SPI_CFG1_PACKET_LENGTH_OFFSET 16
52#define SPI_CFG1_GET_TICKDLY_OFFSET 29
53
54#define SPI_CFG1_GET_TICKDLY_MASK GENMASK(31, 29)
55#define SPI_CFG1_CS_IDLE_MASK 0xff
56#define SPI_CFG1_PACKET_LOOP_MASK 0xff00
57#define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
58#define SPI_CFG1_IPM_PACKET_LENGTH_MASK GENMASK(31, 16)
59#define SPI_CFG2_SCK_HIGH_OFFSET 0
60#define SPI_CFG2_SCK_LOW_OFFSET 16
61#define SPI_CFG2_SCK_HIGH_MASK GENMASK(15, 0)
62#define SPI_CFG2_SCK_LOW_MASK GENMASK(31, 16)
63
64#define SPI_CMD_ACT BIT(0)
65#define SPI_CMD_RESUME BIT(1)
66#define SPI_CMD_RST BIT(2)
67#define SPI_CMD_PAUSE_EN BIT(4)
68#define SPI_CMD_DEASSERT BIT(5)
69#define SPI_CMD_SAMPLE_SEL BIT(6)
70#define SPI_CMD_CS_POL BIT(7)
71#define SPI_CMD_CPHA BIT(8)
72#define SPI_CMD_CPOL BIT(9)
73#define SPI_CMD_RX_DMA BIT(10)
74#define SPI_CMD_TX_DMA BIT(11)
75#define SPI_CMD_TXMSBF BIT(12)
76#define SPI_CMD_RXMSBF BIT(13)
77#define SPI_CMD_RX_ENDIAN BIT(14)
78#define SPI_CMD_TX_ENDIAN BIT(15)
79#define SPI_CMD_FINISH_IE BIT(16)
80#define SPI_CMD_PAUSE_IE BIT(17)
81#define SPI_CMD_IPM_NONIDLE_MODE BIT(19)
82#define SPI_CMD_IPM_SPIM_LOOP BIT(21)
83#define SPI_CMD_IPM_GET_TICKDLY_OFFSET 22
84
85#define SPI_CMD_IPM_GET_TICKDLY_MASK GENMASK(24, 22)
86
87#define PIN_MODE_CFG(x) ((x) / 2)
88
89#define SPI_CFG3_IPM_PIN_MODE_OFFSET 0
90#define SPI_CFG3_IPM_HALF_DUPLEX_DIR BIT(2)
91#define SPI_CFG3_IPM_HALF_DUPLEX_EN BIT(3)
92#define SPI_CFG3_IPM_XMODE_EN BIT(4)
93#define SPI_CFG3_IPM_NODATA_FLAG BIT(5)
94#define SPI_CFG3_IPM_CMD_BYTELEN_OFFSET 8
95#define SPI_CFG3_IPM_ADDR_BYTELEN_OFFSET 12
96#define SPI_CFG3_IPM_DUMMY_BYTELEN_OFFSET 16
97
98#define SPI_CFG3_IPM_CMD_PIN_MODE_MASK GENMASK(1, 0)
99#define SPI_CFG3_IPM_CMD_BYTELEN_MASK GENMASK(11, 8)
100#define SPI_CFG3_IPM_ADDR_BYTELEN_MASK GENMASK(15, 12)
101#define SPI_CFG3_IPM_DUMMY_BYTELEN_MASK GENMASK(19, 16)
102
103#define MT8173_SPI_MAX_PAD_SEL 3
104
105#define MTK_SPI_PAUSE_INT_STATUS 0x2
106
107#define MTK_SPI_IDLE 0
108#define MTK_SPI_PAUSED 1
109
110#define MTK_SPI_MAX_FIFO_SIZE 32U
111#define MTK_SPI_PACKET_SIZE 1024
112#define MTK_SPI_IPM_PACKET_SIZE SZ_64K
113#define MTK_SPI_IPM_PACKET_LOOP SZ_256
114
115#define MTK_SPI_32BITS_MASK 0xffffffff
116
117#define DMA_ADDR_EXT_BITS 36
118#define DMA_ADDR_DEF_BITS 32
119
120#define CLK_TO_US(freq, clkcnt) DIV_ROUND_UP((clkcnt), (freq) / 1000000)
121
122/* struct mtk_spim_capability
123 * @enhance_timing: Some IC design adjust cfg register to enhance time accuracy
124 * @dma_ext: Some IC support DMA addr extension
125 * @ipm_design: The IPM IP design improves some features, and supports dual/quad mode
126 * @support_quad: Whether quad mode is supported
127 */
128struct mtk_spim_capability {
129 bool enhance_timing;
130 bool dma_ext;
131 bool ipm_design;
132 bool support_quad;
133};
134
135/* struct mtk_spim_priv
136 * @base: Base address of the spi controller
137 * @state: Controller state
138 * @sel_clk: Pad clock
139 * @spi_clk: Core clock
developer9f5fbaf2023-07-19 17:15:54 +0800140 * @pll_clk_rate: Controller's PLL source clock rate, which is different
141 * from SPI bus clock rate
developer24202202022-09-09 19:59:45 +0800142 * @xfer_len: Current length of data for transfer
143 * @hw_cap: Controller capabilities
144 * @tick_dly: Used to postpone SPI sampling time
145 * @sample_sel: Sample edge of MISO
146 * @dev: udevice of this spi controller
147 * @tx_dma: Tx DMA address
148 * @rx_dma: Rx DMA address
149 */
150struct mtk_spim_priv {
151 void __iomem *base;
152 u32 state;
153 struct clk sel_clk, spi_clk;
developer9f5fbaf2023-07-19 17:15:54 +0800154 u32 pll_clk_rate;
developer24202202022-09-09 19:59:45 +0800155 u32 xfer_len;
156 struct mtk_spim_capability hw_cap;
157 u32 tick_dly;
158 u32 sample_sel;
159
160 struct device *dev;
161 dma_addr_t tx_dma;
162 dma_addr_t rx_dma;
163};
164
165static void mtk_spim_reset(struct mtk_spim_priv *priv)
166{
167 /* set the software reset bit in SPI_CMD_REG. */
168 setbits_le32(priv->base + SPI_CMD_REG, SPI_CMD_RST);
169 clrbits_le32(priv->base + SPI_CMD_REG, SPI_CMD_RST);
170}
171
172static int mtk_spim_hw_init(struct spi_slave *slave)
173{
174 struct udevice *bus = dev_get_parent(slave->dev);
175 struct mtk_spim_priv *priv = dev_get_priv(bus);
176 u16 cpha, cpol;
177 u32 reg_val;
178
179 cpha = slave->mode & SPI_CPHA ? 1 : 0;
180 cpol = slave->mode & SPI_CPOL ? 1 : 0;
181
182 if (priv->hw_cap.enhance_timing) {
183 if (priv->hw_cap.ipm_design) {
184 /* CFG3 reg only used for spi-mem,
185 * here write to default value
186 */
187 writel(0x0, priv->base + SPI_CFG3_IPM_REG);
188 clrsetbits_le32(priv->base + SPI_CMD_REG,
189 SPI_CMD_IPM_GET_TICKDLY_MASK,
190 priv->tick_dly <<
191 SPI_CMD_IPM_GET_TICKDLY_OFFSET);
192 } else {
193 clrsetbits_le32(priv->base + SPI_CFG1_REG,
194 SPI_CFG1_GET_TICKDLY_MASK,
195 priv->tick_dly <<
196 SPI_CFG1_GET_TICKDLY_OFFSET);
197 }
198 }
199
200 reg_val = readl(priv->base + SPI_CMD_REG);
201 if (priv->hw_cap.ipm_design) {
202 /* SPI transfer without idle time until packet length done */
203 reg_val |= SPI_CMD_IPM_NONIDLE_MODE;
204 if (slave->mode & SPI_LOOP)
205 reg_val |= SPI_CMD_IPM_SPIM_LOOP;
206 else
207 reg_val &= ~SPI_CMD_IPM_SPIM_LOOP;
208 }
209
210 if (cpha)
211 reg_val |= SPI_CMD_CPHA;
212 else
213 reg_val &= ~SPI_CMD_CPHA;
214 if (cpol)
215 reg_val |= SPI_CMD_CPOL;
216 else
217 reg_val &= ~SPI_CMD_CPOL;
218
219 /* set the mlsbx and mlsbtx */
220 if (slave->mode & SPI_LSB_FIRST) {
221 reg_val &= ~SPI_CMD_TXMSBF;
222 reg_val &= ~SPI_CMD_RXMSBF;
223 } else {
224 reg_val |= SPI_CMD_TXMSBF;
225 reg_val |= SPI_CMD_RXMSBF;
226 }
227
228 /* do not reverse tx/rx endian */
229 reg_val &= ~SPI_CMD_TX_ENDIAN;
230 reg_val &= ~SPI_CMD_RX_ENDIAN;
231
232 if (priv->hw_cap.enhance_timing) {
233 /* set CS polarity */
234 if (slave->mode & SPI_CS_HIGH)
235 reg_val |= SPI_CMD_CS_POL;
236 else
237 reg_val &= ~SPI_CMD_CS_POL;
238
239 if (priv->sample_sel)
240 reg_val |= SPI_CMD_SAMPLE_SEL;
241 else
242 reg_val &= ~SPI_CMD_SAMPLE_SEL;
243 }
244
developer92aef702023-07-19 17:16:02 +0800245 /* Disable interrupt enable for pause mode & normal mode */
246 reg_val &= ~(SPI_CMD_PAUSE_IE | SPI_CMD_FINISH_IE);
247
developer24202202022-09-09 19:59:45 +0800248 /* disable dma mode */
249 reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
250
251 /* disable deassert mode */
252 reg_val &= ~SPI_CMD_DEASSERT;
253
254 writel(reg_val, priv->base + SPI_CMD_REG);
255
256 return 0;
257}
258
259static void mtk_spim_prepare_transfer(struct mtk_spim_priv *priv,
260 u32 speed_hz)
261{
developer9f5fbaf2023-07-19 17:15:54 +0800262 u32 div, sck_time, cs_time, reg_val;
developer24202202022-09-09 19:59:45 +0800263
developer9f5fbaf2023-07-19 17:15:54 +0800264 if (speed_hz <= priv->pll_clk_rate / 4)
265 div = DIV_ROUND_UP(priv->pll_clk_rate, speed_hz);
developer24202202022-09-09 19:59:45 +0800266 else
267 div = 4;
268
269 sck_time = (div + 1) / 2;
270 cs_time = sck_time * 2;
271
272 if (priv->hw_cap.enhance_timing) {
273 reg_val = ((sck_time - 1) & 0xffff)
274 << SPI_CFG2_SCK_HIGH_OFFSET;
275 reg_val |= ((sck_time - 1) & 0xffff)
276 << SPI_CFG2_SCK_LOW_OFFSET;
277 writel(reg_val, priv->base + SPI_CFG2_REG);
278
279 reg_val = ((cs_time - 1) & 0xffff)
280 << SPI_ADJUST_CFG0_CS_HOLD_OFFSET;
281 reg_val |= ((cs_time - 1) & 0xffff)
282 << SPI_ADJUST_CFG0_CS_SETUP_OFFSET;
283 writel(reg_val, priv->base + SPI_CFG0_REG);
284 } else {
285 reg_val = ((sck_time - 1) & 0xff)
286 << SPI_CFG0_SCK_HIGH_OFFSET;
287 reg_val |= ((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET;
288 reg_val |= ((cs_time - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET;
289 reg_val |= ((cs_time - 1) & 0xff) << SPI_CFG0_CS_SETUP_OFFSET;
290 writel(reg_val, priv->base + SPI_CFG0_REG);
291 }
292
293 reg_val = readl(priv->base + SPI_CFG1_REG);
294 reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
295 reg_val |= ((cs_time - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET;
296 writel(reg_val, priv->base + SPI_CFG1_REG);
297}
298
299/**
300 * mtk_spim_setup_packet() - setup packet format.
301 * @priv: controller priv
302 *
303 * This controller sents/receives data in packets. The packet size is
304 * configurable.
305 *
306 * This function calculates the maximum packet size available for current
307 * data, and calculates the number of packets required to sent/receive data
308 * as much as possible.
309 */
310static void mtk_spim_setup_packet(struct mtk_spim_priv *priv)
311{
312 u32 packet_size, packet_loop, reg_val;
313
314 /* Calculate maximum packet size */
315 if (priv->hw_cap.ipm_design)
316 packet_size = min_t(u32,
317 priv->xfer_len,
318 MTK_SPI_IPM_PACKET_SIZE);
319 else
320 packet_size = min_t(u32,
321 priv->xfer_len,
322 MTK_SPI_PACKET_SIZE);
323
324 /* Calculates number of packets to sent/receive */
325 packet_loop = priv->xfer_len / packet_size;
326
327 reg_val = readl(priv->base + SPI_CFG1_REG);
328 if (priv->hw_cap.ipm_design)
329 reg_val &= ~SPI_CFG1_IPM_PACKET_LENGTH_MASK;
330 else
331 reg_val &= ~SPI_CFG1_PACKET_LENGTH_MASK;
332
333 reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
334
335 reg_val &= ~SPI_CFG1_PACKET_LOOP_MASK;
336
337 reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
338
339 writel(reg_val, priv->base + SPI_CFG1_REG);
340}
341
342static void mtk_spim_enable_transfer(struct mtk_spim_priv *priv)
343{
344 u32 cmd;
345
346 cmd = readl(priv->base + SPI_CMD_REG);
347 if (priv->state == MTK_SPI_IDLE)
348 cmd |= SPI_CMD_ACT;
349 else
350 cmd |= SPI_CMD_RESUME;
351 writel(cmd, priv->base + SPI_CMD_REG);
352}
353
354static bool mtk_spim_supports_op(struct spi_slave *slave,
355 const struct spi_mem_op *op)
356{
357 struct udevice *bus = dev_get_parent(slave->dev);
358 struct mtk_spim_priv *priv = dev_get_priv(bus);
359
360 if (op->cmd.buswidth == 0 || op->cmd.buswidth > 4 ||
361 op->addr.buswidth > 4 || op->dummy.buswidth > 4 ||
362 op->data.buswidth > 4)
363 return false;
364
365 if (!priv->hw_cap.support_quad && (op->cmd.buswidth > 2 ||
366 op->addr.buswidth > 2 || op->dummy.buswidth > 2 ||
367 op->data.buswidth > 2))
368 return false;
369
370 if (op->addr.nbytes && op->dummy.nbytes &&
371 op->addr.buswidth != op->dummy.buswidth)
372 return false;
373
374 if (op->addr.nbytes + op->dummy.nbytes > 16)
375 return false;
376
377 if (op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) {
378 if (op->data.nbytes / MTK_SPI_IPM_PACKET_SIZE >
379 MTK_SPI_IPM_PACKET_LOOP ||
380 op->data.nbytes % MTK_SPI_IPM_PACKET_SIZE != 0)
381 return false;
382 }
383
384 return true;
385}
386
387static void mtk_spim_setup_dma_xfer(struct mtk_spim_priv *priv,
388 const struct spi_mem_op *op)
389{
390 writel((u32)(priv->tx_dma & MTK_SPI_32BITS_MASK),
391 priv->base + SPI_TX_SRC_REG);
392
393 if (priv->hw_cap.dma_ext)
394 writel((u32)(priv->tx_dma >> 32),
395 priv->base + SPI_TX_SRC_REG_64);
396
397 if (op->data.dir == SPI_MEM_DATA_IN) {
398 writel((u32)(priv->rx_dma & MTK_SPI_32BITS_MASK),
399 priv->base + SPI_RX_DST_REG);
400
401 if (priv->hw_cap.dma_ext)
402 writel((u32)(priv->rx_dma >> 32),
403 priv->base + SPI_RX_DST_REG_64);
404 }
405}
406
407static int mtk_spim_transfer_wait(struct spi_slave *slave,
408 const struct spi_mem_op *op)
409{
410 struct udevice *bus = dev_get_parent(slave->dev);
411 struct mtk_spim_priv *priv = dev_get_priv(bus);
Nicolò Veronese892d7772023-10-04 00:14:26 +0200412 u32 pll_clk, sck_l, sck_h, clk_count, reg;
developer24202202022-09-09 19:59:45 +0800413 ulong us = 1;
414 int ret = 0;
415
416 if (op->data.dir == SPI_MEM_NO_DATA)
417 clk_count = 32;
418 else
419 clk_count = op->data.nbytes;
420
Nicolò Veronese892d7772023-10-04 00:14:26 +0200421 pll_clk = priv->pll_clk_rate;
developer24202202022-09-09 19:59:45 +0800422 sck_l = readl(priv->base + SPI_CFG2_REG) >> SPI_CFG2_SCK_LOW_OFFSET;
423 sck_h = readl(priv->base + SPI_CFG2_REG) & SPI_CFG2_SCK_HIGH_MASK;
Nicolò Veronese892d7772023-10-04 00:14:26 +0200424 do_div(pll_clk, sck_l + sck_h + 2);
developer24202202022-09-09 19:59:45 +0800425
Nicolò Veronese892d7772023-10-04 00:14:26 +0200426 us = CLK_TO_US(pll_clk, clk_count * 8);
developer24202202022-09-09 19:59:45 +0800427 us += 1000 * 1000; /* 1s tolerance */
428
429 if (us > UINT_MAX)
430 us = UINT_MAX;
431
432 ret = readl_poll_timeout(priv->base + SPI_STATUS_REG, reg,
433 reg & 0x1, us);
434 if (ret < 0) {
435 dev_err(priv->dev, "transfer timeout, val: 0x%lx\n", us);
436 return -ETIMEDOUT;
437 }
438
439 return 0;
440}
441
442static int mtk_spim_exec_op(struct spi_slave *slave,
443 const struct spi_mem_op *op)
444{
445 struct udevice *bus = dev_get_parent(slave->dev);
446 struct mtk_spim_priv *priv = dev_get_priv(bus);
447 u32 reg_val, nio = 1, tx_size;
448 char *tx_tmp_buf;
449 char *rx_tmp_buf;
450 int i, ret = 0;
451
452 mtk_spim_reset(priv);
453 mtk_spim_hw_init(slave);
454 mtk_spim_prepare_transfer(priv, slave->max_hz);
455
456 reg_val = readl(priv->base + SPI_CFG3_IPM_REG);
457 /* opcode byte len */
458 reg_val &= ~SPI_CFG3_IPM_CMD_BYTELEN_MASK;
459 reg_val |= 1 << SPI_CFG3_IPM_CMD_BYTELEN_OFFSET;
460
461 /* addr & dummy byte len */
462 if (op->addr.nbytes || op->dummy.nbytes)
463 reg_val |= (op->addr.nbytes + op->dummy.nbytes) <<
464 SPI_CFG3_IPM_ADDR_BYTELEN_OFFSET;
465
466 /* data byte len */
467 if (!op->data.nbytes) {
468 reg_val |= SPI_CFG3_IPM_NODATA_FLAG;
469 writel(0, priv->base + SPI_CFG1_REG);
470 } else {
471 reg_val &= ~SPI_CFG3_IPM_NODATA_FLAG;
472 priv->xfer_len = op->data.nbytes;
473 mtk_spim_setup_packet(priv);
474 }
475
476 if (op->addr.nbytes || op->dummy.nbytes) {
477 if (op->addr.buswidth == 1 || op->dummy.buswidth == 1)
478 reg_val |= SPI_CFG3_IPM_XMODE_EN;
479 else
480 reg_val &= ~SPI_CFG3_IPM_XMODE_EN;
481 }
482
483 if (op->addr.buswidth == 2 ||
484 op->dummy.buswidth == 2 ||
485 op->data.buswidth == 2)
486 nio = 2;
487 else if (op->addr.buswidth == 4 ||
488 op->dummy.buswidth == 4 ||
489 op->data.buswidth == 4)
490 nio = 4;
491
492 reg_val &= ~SPI_CFG3_IPM_CMD_PIN_MODE_MASK;
493 reg_val |= PIN_MODE_CFG(nio) << SPI_CFG3_IPM_PIN_MODE_OFFSET;
494
495 reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_EN;
496 if (op->data.dir == SPI_MEM_DATA_IN)
497 reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_DIR;
498 else
499 reg_val &= ~SPI_CFG3_IPM_HALF_DUPLEX_DIR;
500 writel(reg_val, priv->base + SPI_CFG3_IPM_REG);
501
502 tx_size = 1 + op->addr.nbytes + op->dummy.nbytes;
503 if (op->data.dir == SPI_MEM_DATA_OUT)
504 tx_size += op->data.nbytes;
505
506 tx_size = max(tx_size, (u32)32);
507
508 /* Fill up tx data */
509 tx_tmp_buf = kzalloc(tx_size, GFP_KERNEL);
510 if (!tx_tmp_buf) {
511 ret = -ENOMEM;
512 goto exit;
513 }
514
515 tx_tmp_buf[0] = op->cmd.opcode;
516
517 if (op->addr.nbytes) {
518 for (i = 0; i < op->addr.nbytes; i++)
519 tx_tmp_buf[i + 1] = op->addr.val >>
520 (8 * (op->addr.nbytes - i - 1));
521 }
522
523 if (op->dummy.nbytes)
524 memset(tx_tmp_buf + op->addr.nbytes + 1, 0xff,
525 op->dummy.nbytes);
526
527 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
528 memcpy(tx_tmp_buf + op->dummy.nbytes + op->addr.nbytes + 1,
529 op->data.buf.out, op->data.nbytes);
530 /* Finish filling up tx data */
531
532 priv->tx_dma = dma_map_single(tx_tmp_buf, tx_size, DMA_TO_DEVICE);
533 if (dma_mapping_error(priv->dev, priv->tx_dma)) {
534 ret = -ENOMEM;
535 goto tx_free;
536 }
537
538 if (op->data.dir == SPI_MEM_DATA_IN) {
539 if (!IS_ALIGNED((size_t)op->data.buf.in, 4)) {
540 rx_tmp_buf = kzalloc(op->data.nbytes, GFP_KERNEL);
541 if (!rx_tmp_buf) {
542 ret = -ENOMEM;
543 goto tx_unmap;
544 }
545 } else {
546 rx_tmp_buf = op->data.buf.in;
547 }
548
549 priv->rx_dma = dma_map_single(rx_tmp_buf, op->data.nbytes,
550 DMA_FROM_DEVICE);
551 if (dma_mapping_error(priv->dev, priv->rx_dma)) {
552 ret = -ENOMEM;
553 goto rx_free;
554 }
555 }
556
557 reg_val = readl(priv->base + SPI_CMD_REG);
558 reg_val |= SPI_CMD_TX_DMA;
559 if (op->data.dir == SPI_MEM_DATA_IN)
560 reg_val |= SPI_CMD_RX_DMA;
561
562 writel(reg_val, priv->base + SPI_CMD_REG);
563
564 mtk_spim_setup_dma_xfer(priv, op);
565
566 mtk_spim_enable_transfer(priv);
567
568 /* Wait for the interrupt. */
569 ret = mtk_spim_transfer_wait(slave, op);
570 if (ret)
571 goto rx_unmap;
572
573 if (op->data.dir == SPI_MEM_DATA_IN &&
574 !IS_ALIGNED((size_t)op->data.buf.in, 4))
575 memcpy(op->data.buf.in, rx_tmp_buf, op->data.nbytes);
576
577rx_unmap:
578 /* spi disable dma */
579 reg_val = readl(priv->base + SPI_CMD_REG);
580 reg_val &= ~SPI_CMD_TX_DMA;
581 if (op->data.dir == SPI_MEM_DATA_IN)
582 reg_val &= ~SPI_CMD_RX_DMA;
583 writel(reg_val, priv->base + SPI_CMD_REG);
584
585 writel(0, priv->base + SPI_TX_SRC_REG);
586 writel(0, priv->base + SPI_RX_DST_REG);
587
588 if (op->data.dir == SPI_MEM_DATA_IN)
589 dma_unmap_single(priv->rx_dma,
590 op->data.nbytes, DMA_FROM_DEVICE);
591rx_free:
592 if (op->data.dir == SPI_MEM_DATA_IN &&
593 !IS_ALIGNED((size_t)op->data.buf.in, 4))
594 kfree(rx_tmp_buf);
595tx_unmap:
596 dma_unmap_single(priv->tx_dma,
597 tx_size, DMA_TO_DEVICE);
598tx_free:
599 kfree(tx_tmp_buf);
600exit:
601 return ret;
602}
603
604static int mtk_spim_adjust_op_size(struct spi_slave *slave,
605 struct spi_mem_op *op)
606{
607 int opcode_len;
608
609 if (!op->data.nbytes)
610 return 0;
611
612 if (op->data.dir != SPI_MEM_NO_DATA) {
613 opcode_len = 1 + op->addr.nbytes + op->dummy.nbytes;
614 if (opcode_len + op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) {
615 op->data.nbytes = MTK_SPI_IPM_PACKET_SIZE - opcode_len;
616 /* force data buffer dma-aligned. */
617 op->data.nbytes -= op->data.nbytes % 4;
618 }
619 }
620
621 return 0;
622}
623
624static int mtk_spim_get_attr(struct mtk_spim_priv *priv, struct udevice *dev)
625{
626 int ret;
627
628 priv->hw_cap.enhance_timing = dev_read_bool(dev, "enhance_timing");
629 priv->hw_cap.dma_ext = dev_read_bool(dev, "dma_ext");
630 priv->hw_cap.ipm_design = dev_read_bool(dev, "ipm_design");
631 priv->hw_cap.support_quad = dev_read_bool(dev, "support_quad");
632
633 ret = dev_read_u32(dev, "tick_dly", &priv->tick_dly);
634 if (ret < 0)
635 dev_err(priv->dev, "tick dly not set.\n");
636
637 ret = dev_read_u32(dev, "sample_sel", &priv->sample_sel);
638 if (ret < 0)
639 dev_err(priv->dev, "sample sel not set.\n");
640
641 return ret;
642}
643
644static int mtk_spim_probe(struct udevice *dev)
645{
646 struct mtk_spim_priv *priv = dev_get_priv(dev);
647 int ret;
648
Johan Jonker2f9f7752023-03-13 01:32:44 +0100649 priv->base = devfdt_get_addr_ptr(dev);
developer24202202022-09-09 19:59:45 +0800650 if (!priv->base)
651 return -EINVAL;
652
653 mtk_spim_get_attr(priv, dev);
654
655 ret = clk_get_by_name(dev, "sel-clk", &priv->sel_clk);
656 if (ret < 0) {
657 dev_err(dev, "failed to get sel-clk\n");
658 return ret;
659 }
660
661 ret = clk_get_by_name(dev, "spi-clk", &priv->spi_clk);
662 if (ret < 0) {
663 dev_err(dev, "failed to get spi-clk\n");
664 return ret;
665 }
666
667 clk_enable(&priv->sel_clk);
668 clk_enable(&priv->spi_clk);
669
developer9f5fbaf2023-07-19 17:15:54 +0800670 priv->pll_clk_rate = clk_get_rate(&priv->spi_clk);
671 if (priv->pll_clk_rate == 0)
672 return -EINVAL;
673
developer24202202022-09-09 19:59:45 +0800674 return 0;
675}
676
677static int mtk_spim_set_speed(struct udevice *dev, uint speed)
678{
679 return 0;
680}
681
682static int mtk_spim_set_mode(struct udevice *dev, uint mode)
683{
684 return 0;
685}
686
687static const struct spi_controller_mem_ops mtk_spim_mem_ops = {
688 .adjust_op_size = mtk_spim_adjust_op_size,
689 .supports_op = mtk_spim_supports_op,
690 .exec_op = mtk_spim_exec_op
691};
692
693static const struct dm_spi_ops mtk_spim_ops = {
694 .mem_ops = &mtk_spim_mem_ops,
695 .set_speed = mtk_spim_set_speed,
696 .set_mode = mtk_spim_set_mode,
697};
698
699static const struct udevice_id mtk_spim_ids[] = {
700 { .compatible = "mediatek,ipm-spi" },
701 {}
702};
703
704U_BOOT_DRIVER(mtk_spim) = {
705 .name = "mtk_spim",
706 .id = UCLASS_SPI,
707 .of_match = mtk_spim_ids,
708 .ops = &mtk_spim_ops,
709 .priv_auto = sizeof(struct mtk_spim_priv),
710 .probe = mtk_spim_probe,
711};