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