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