1 // SPDX-License-Identifier: GPL-2.0
3 * SPI bus driver for the Ingenic SoCs
4 * Copyright (c) 2017-2021 Artur Rojek <contact@artur-rojek.eu>
5 * Copyright (c) 2017-2021 Paul Cercueil <paul@crapouillou.net>
6 * Copyright (c) 2022 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/spi/spi.h>
19 #include "internals.h"
22 #define REG_SSICR0 0x4
23 #define REG_SSICR1 0x8
25 #define REG_SSIGR 0x18
27 #define REG_SSICR0_TENDIAN_LSB BIT(19)
28 #define REG_SSICR0_RENDIAN_LSB BIT(17)
29 #define REG_SSICR0_SSIE BIT(15)
30 #define REG_SSICR0_LOOP BIT(10)
31 #define REG_SSICR0_EACLRUN BIT(7)
32 #define REG_SSICR0_FSEL BIT(6)
33 #define REG_SSICR0_TFLUSH BIT(2)
34 #define REG_SSICR0_RFLUSH BIT(1)
36 #define REG_SSICR1_FRMHL_MASK (BIT(31) | BIT(30))
37 #define REG_SSICR1_FRMHL BIT(30)
38 #define REG_SSICR1_LFST BIT(25)
39 #define REG_SSICR1_UNFIN BIT(23)
40 #define REG_SSICR1_PHA BIT(1)
41 #define REG_SSICR1_POL BIT(0)
43 #define REG_SSISR_END BIT(7)
44 #define REG_SSISR_BUSY BIT(6)
45 #define REG_SSISR_TFF BIT(5)
46 #define REG_SSISR_RFE BIT(4)
47 #define REG_SSISR_RFHF BIT(2)
48 #define REG_SSISR_UNDR BIT(1)
49 #define REG_SSISR_OVER BIT(0)
51 #define SPI_INGENIC_FIFO_SIZE 128u
54 u32 bits_per_word_mask
;
55 struct reg_field flen_field
;
58 unsigned int max_speed_hz
;
59 unsigned int max_native_cs
;
63 const struct jz_soc_info
*soc_info
;
65 struct resource
*mem_res
;
68 struct regmap_field
*flen_field
;
71 static int spi_ingenic_wait(struct ingenic_spi
*priv
,
77 return regmap_read_poll_timeout(priv
->map
, REG_SSISR
, val
,
78 !!(val
& mask
) == condition
,
82 static void spi_ingenic_set_cs(struct spi_device
*spi
, bool disable
)
84 struct ingenic_spi
*priv
= spi_controller_get_devdata(spi
->controller
);
87 regmap_clear_bits(priv
->map
, REG_SSICR1
, REG_SSICR1_UNFIN
);
88 regmap_clear_bits(priv
->map
, REG_SSISR
,
89 REG_SSISR_UNDR
| REG_SSISR_OVER
);
91 spi_ingenic_wait(priv
, REG_SSISR_END
, true);
93 regmap_set_bits(priv
->map
, REG_SSICR1
, REG_SSICR1_UNFIN
);
96 regmap_set_bits(priv
->map
, REG_SSICR0
,
97 REG_SSICR0_RFLUSH
| REG_SSICR0_TFLUSH
);
100 static void spi_ingenic_prepare_transfer(struct ingenic_spi
*priv
,
101 struct spi_device
*spi
,
102 struct spi_transfer
*xfer
)
104 unsigned long clk_hz
= clk_get_rate(priv
->clk
);
105 u32 cdiv
, speed_hz
= xfer
->speed_hz
?: spi
->max_speed_hz
,
106 bits_per_word
= xfer
->bits_per_word
?: spi
->bits_per_word
;
108 cdiv
= clk_hz
/ (speed_hz
* 2);
109 cdiv
= clamp(cdiv
, 1u, 0x100u
) - 1;
111 regmap_write(priv
->map
, REG_SSIGR
, cdiv
);
113 regmap_field_write(priv
->flen_field
, bits_per_word
- 2);
116 static void spi_ingenic_finalize_transfer(void *controller
)
118 spi_finalize_current_transfer(controller
);
121 static struct dma_async_tx_descriptor
*
122 spi_ingenic_prepare_dma(struct spi_controller
*ctlr
, struct dma_chan
*chan
,
123 struct sg_table
*sg
, enum dma_transfer_direction dir
,
126 struct ingenic_spi
*priv
= spi_controller_get_devdata(ctlr
);
127 struct dma_slave_config cfg
= {
129 .src_addr
= priv
->mem_res
->start
+ REG_SSIDR
,
130 .dst_addr
= priv
->mem_res
->start
+ REG_SSIDR
,
132 struct dma_async_tx_descriptor
*desc
;
137 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
138 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
139 cfg
.src_maxburst
= cfg
.dst_maxburst
= 4;
140 } else if (bits
> 8) {
141 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
142 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
143 cfg
.src_maxburst
= cfg
.dst_maxburst
= 2;
145 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
146 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
147 cfg
.src_maxburst
= cfg
.dst_maxburst
= 1;
150 ret
= dmaengine_slave_config(chan
, &cfg
);
154 desc
= dmaengine_prep_slave_sg(chan
, sg
->sgl
, sg
->nents
, dir
,
157 return ERR_PTR(-ENOMEM
);
159 if (dir
== DMA_DEV_TO_MEM
) {
160 desc
->callback
= spi_ingenic_finalize_transfer
;
161 desc
->callback_param
= ctlr
;
164 cookie
= dmaengine_submit(desc
);
166 ret
= dma_submit_error(cookie
);
168 dmaengine_desc_free(desc
);
175 static int spi_ingenic_dma_tx(struct spi_controller
*ctlr
,
176 struct spi_transfer
*xfer
, unsigned int bits
)
178 struct dma_async_tx_descriptor
*rx_desc
, *tx_desc
;
180 rx_desc
= spi_ingenic_prepare_dma(ctlr
, ctlr
->dma_rx
,
181 &xfer
->rx_sg
, DMA_DEV_TO_MEM
, bits
);
183 return PTR_ERR(rx_desc
);
185 tx_desc
= spi_ingenic_prepare_dma(ctlr
, ctlr
->dma_tx
,
186 &xfer
->tx_sg
, DMA_MEM_TO_DEV
, bits
);
187 if (IS_ERR(tx_desc
)) {
188 dmaengine_terminate_async(ctlr
->dma_rx
);
189 dmaengine_desc_free(rx_desc
);
190 return PTR_ERR(tx_desc
);
193 dma_async_issue_pending(ctlr
->dma_rx
);
194 dma_async_issue_pending(ctlr
->dma_tx
);
199 #define SPI_INGENIC_TX(x) \
200 static int spi_ingenic_tx##x(struct ingenic_spi *priv, \
201 struct spi_transfer *xfer) \
203 unsigned int count = xfer->len / (x / 8); \
204 unsigned int prefill = min(count, SPI_INGENIC_FIFO_SIZE); \
205 const u##x *tx_buf = xfer->tx_buf; \
206 u##x *rx_buf = xfer->rx_buf; \
207 unsigned int i, val; \
210 /* Fill up the TX fifo */ \
211 for (i = 0; i < prefill; i++) { \
212 val = tx_buf ? tx_buf[i] : 0; \
214 regmap_write(priv->map, REG_SSIDR, val); \
217 for (i = 0; i < count; i++) { \
218 err = spi_ingenic_wait(priv, REG_SSISR_RFE, false); \
222 regmap_read(priv->map, REG_SSIDR, &val); \
226 if (i < count - prefill) { \
227 val = tx_buf ? tx_buf[i + prefill] : 0; \
229 regmap_write(priv->map, REG_SSIDR, val); \
238 #undef SPI_INGENIC_TX
240 static int spi_ingenic_transfer_one(struct spi_controller
*ctlr
,
241 struct spi_device
*spi
,
242 struct spi_transfer
*xfer
)
244 struct ingenic_spi
*priv
= spi_controller_get_devdata(ctlr
);
245 unsigned int bits
= xfer
->bits_per_word
?: spi
->bits_per_word
;
247 spi_ingenic_prepare_transfer(priv
, spi
, xfer
);
249 if (spi_xfer_is_dma_mapped(ctlr
, spi
, xfer
))
250 return spi_ingenic_dma_tx(ctlr
, xfer
, bits
);
253 return spi_ingenic_tx32(priv
, xfer
);
256 return spi_ingenic_tx16(priv
, xfer
);
258 return spi_ingenic_tx8(priv
, xfer
);
261 static int spi_ingenic_prepare_message(struct spi_controller
*ctlr
,
262 struct spi_message
*message
)
264 struct ingenic_spi
*priv
= spi_controller_get_devdata(ctlr
);
265 struct spi_device
*spi
= message
->spi
;
266 unsigned int cs
= REG_SSICR1_FRMHL
<< spi_get_chipselect(spi
, 0);
267 unsigned int ssicr0_mask
= REG_SSICR0_LOOP
| REG_SSICR0_FSEL
;
268 unsigned int ssicr1_mask
= REG_SSICR1_PHA
| REG_SSICR1_POL
| cs
;
269 unsigned int ssicr0
= 0, ssicr1
= 0;
271 if (priv
->soc_info
->has_trendian
) {
272 ssicr0_mask
|= REG_SSICR0_RENDIAN_LSB
| REG_SSICR0_TENDIAN_LSB
;
274 if (spi
->mode
& SPI_LSB_FIRST
)
275 ssicr0
|= REG_SSICR0_RENDIAN_LSB
| REG_SSICR0_TENDIAN_LSB
;
277 ssicr1_mask
|= REG_SSICR1_LFST
;
279 if (spi
->mode
& SPI_LSB_FIRST
)
280 ssicr1
|= REG_SSICR1_LFST
;
283 if (spi
->mode
& SPI_LOOP
)
284 ssicr0
|= REG_SSICR0_LOOP
;
285 if (spi_get_chipselect(spi
, 0))
286 ssicr0
|= REG_SSICR0_FSEL
;
288 if (spi
->mode
& SPI_CPHA
)
289 ssicr1
|= REG_SSICR1_PHA
;
290 if (spi
->mode
& SPI_CPOL
)
291 ssicr1
|= REG_SSICR1_POL
;
292 if (spi
->mode
& SPI_CS_HIGH
)
295 regmap_update_bits(priv
->map
, REG_SSICR0
, ssicr0_mask
, ssicr0
);
296 regmap_update_bits(priv
->map
, REG_SSICR1
, ssicr1_mask
, ssicr1
);
301 static int spi_ingenic_prepare_hardware(struct spi_controller
*ctlr
)
303 struct ingenic_spi
*priv
= spi_controller_get_devdata(ctlr
);
306 ret
= clk_prepare_enable(priv
->clk
);
310 regmap_write(priv
->map
, REG_SSICR0
, REG_SSICR0_EACLRUN
);
311 regmap_write(priv
->map
, REG_SSICR1
, 0);
312 regmap_write(priv
->map
, REG_SSISR
, 0);
313 regmap_set_bits(priv
->map
, REG_SSICR0
, REG_SSICR0_SSIE
);
318 static int spi_ingenic_unprepare_hardware(struct spi_controller
*ctlr
)
320 struct ingenic_spi
*priv
= spi_controller_get_devdata(ctlr
);
322 regmap_clear_bits(priv
->map
, REG_SSICR0
, REG_SSICR0_SSIE
);
324 clk_disable_unprepare(priv
->clk
);
329 static bool spi_ingenic_can_dma(struct spi_controller
*ctlr
,
330 struct spi_device
*spi
,
331 struct spi_transfer
*xfer
)
333 struct dma_slave_caps caps
;
336 ret
= dma_get_slave_caps(ctlr
->dma_tx
, &caps
);
338 dev_err(&spi
->dev
, "Unable to get slave caps: %d\n", ret
);
342 return !caps
.max_sg_burst
||
343 xfer
->len
<= caps
.max_sg_burst
* SPI_INGENIC_FIFO_SIZE
;
346 static int spi_ingenic_request_dma(struct spi_controller
*ctlr
,
349 struct dma_chan
*chan
;
351 chan
= dma_request_chan(dev
, "tx");
353 return PTR_ERR(chan
);
356 chan
= dma_request_chan(dev
, "rx");
358 return PTR_ERR(chan
);
361 ctlr
->can_dma
= spi_ingenic_can_dma
;
366 static void spi_ingenic_release_dma(void *data
)
368 struct spi_controller
*ctlr
= data
;
371 dma_release_channel(ctlr
->dma_tx
);
373 dma_release_channel(ctlr
->dma_rx
);
376 static const struct regmap_config spi_ingenic_regmap_config
= {
380 .max_register
= REG_SSIGR
,
383 static int spi_ingenic_probe(struct platform_device
*pdev
)
385 const struct jz_soc_info
*pdata
;
386 struct device
*dev
= &pdev
->dev
;
387 struct spi_controller
*ctlr
;
388 struct ingenic_spi
*priv
;
392 pdata
= of_device_get_match_data(dev
);
394 dev_err(dev
, "Missing platform data.\n");
398 ctlr
= devm_spi_alloc_host(dev
, sizeof(*priv
));
400 dev_err(dev
, "Unable to allocate SPI controller.\n");
404 priv
= spi_controller_get_devdata(ctlr
);
405 priv
->soc_info
= pdata
;
407 priv
->clk
= devm_clk_get(dev
, NULL
);
408 if (IS_ERR(priv
->clk
)) {
409 return dev_err_probe(dev
, PTR_ERR(priv
->clk
),
410 "Unable to get clock.\n");
413 base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &priv
->mem_res
);
415 return PTR_ERR(base
);
417 priv
->map
= devm_regmap_init_mmio(dev
, base
, &spi_ingenic_regmap_config
);
418 if (IS_ERR(priv
->map
))
419 return PTR_ERR(priv
->map
);
421 priv
->flen_field
= devm_regmap_field_alloc(dev
, priv
->map
,
423 if (IS_ERR(priv
->flen_field
))
424 return PTR_ERR(priv
->flen_field
);
426 if (device_property_read_u32(dev
, "num-cs", &num_cs
))
427 num_cs
= pdata
->max_native_cs
;
429 platform_set_drvdata(pdev
, ctlr
);
431 ctlr
->prepare_transfer_hardware
= spi_ingenic_prepare_hardware
;
432 ctlr
->unprepare_transfer_hardware
= spi_ingenic_unprepare_hardware
;
433 ctlr
->prepare_message
= spi_ingenic_prepare_message
;
434 ctlr
->set_cs
= spi_ingenic_set_cs
;
435 ctlr
->transfer_one
= spi_ingenic_transfer_one
;
436 ctlr
->mode_bits
= SPI_MODE_3
| SPI_LSB_FIRST
| SPI_LOOP
| SPI_CS_HIGH
;
437 ctlr
->flags
= SPI_CONTROLLER_MUST_RX
| SPI_CONTROLLER_MUST_TX
;
438 ctlr
->max_dma_len
= SPI_INGENIC_FIFO_SIZE
;
439 ctlr
->bits_per_word_mask
= pdata
->bits_per_word_mask
;
440 ctlr
->min_speed_hz
= 7200;
441 ctlr
->max_speed_hz
= pdata
->max_speed_hz
;
442 ctlr
->use_gpio_descriptors
= true;
443 ctlr
->max_native_cs
= pdata
->max_native_cs
;
444 ctlr
->num_chipselect
= num_cs
;
445 ctlr
->dev
.of_node
= pdev
->dev
.of_node
;
447 if (spi_ingenic_request_dma(ctlr
, dev
))
448 dev_warn(dev
, "DMA not available.\n");
450 ret
= devm_add_action_or_reset(dev
, spi_ingenic_release_dma
, ctlr
);
452 dev_err(dev
, "Unable to add action.\n");
456 ret
= devm_spi_register_controller(dev
, ctlr
);
458 dev_err(dev
, "Unable to register SPI controller.\n");
463 static const struct jz_soc_info jz4750_soc_info
= {
464 .bits_per_word_mask
= SPI_BPW_RANGE_MASK(2, 17),
465 .flen_field
= REG_FIELD(REG_SSICR1
, 4, 7),
466 .has_trendian
= false,
468 .max_speed_hz
= 54000000,
472 static const struct jz_soc_info jz4780_soc_info
= {
473 .bits_per_word_mask
= SPI_BPW_RANGE_MASK(2, 32),
474 .flen_field
= REG_FIELD(REG_SSICR1
, 3, 7),
475 .has_trendian
= true,
477 .max_speed_hz
= 54000000,
481 static const struct jz_soc_info x1000_soc_info
= {
482 .bits_per_word_mask
= SPI_BPW_RANGE_MASK(2, 32),
483 .flen_field
= REG_FIELD(REG_SSICR1
, 3, 7),
484 .has_trendian
= true,
486 .max_speed_hz
= 50000000,
490 static const struct jz_soc_info x2000_soc_info
= {
491 .bits_per_word_mask
= SPI_BPW_RANGE_MASK(2, 32),
492 .flen_field
= REG_FIELD(REG_SSICR1
, 3, 7),
493 .has_trendian
= true,
495 .max_speed_hz
= 50000000,
499 static const struct of_device_id spi_ingenic_of_match
[] = {
500 { .compatible
= "ingenic,jz4750-spi", .data
= &jz4750_soc_info
},
501 { .compatible
= "ingenic,jz4775-spi", .data
= &jz4780_soc_info
},
502 { .compatible
= "ingenic,jz4780-spi", .data
= &jz4780_soc_info
},
503 { .compatible
= "ingenic,x1000-spi", .data
= &x1000_soc_info
},
504 { .compatible
= "ingenic,x2000-spi", .data
= &x2000_soc_info
},
507 MODULE_DEVICE_TABLE(of
, spi_ingenic_of_match
);
509 static struct platform_driver spi_ingenic_driver
= {
511 .name
= "spi-ingenic",
512 .of_match_table
= spi_ingenic_of_match
,
514 .probe
= spi_ingenic_probe
,
517 module_platform_driver(spi_ingenic_driver
);
518 MODULE_DESCRIPTION("SPI bus driver for the Ingenic SoCs");
519 MODULE_AUTHOR("Artur Rojek <contact@artur-rojek.eu>");
520 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
521 MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
522 MODULE_LICENSE("GPL");