1 // SPDX-License-Identifier: GPL-2.0-only
3 // HiSilicon SPI Controller Driver for Kunpeng SoCs
5 // Copyright (c) 2021 HiSilicon Technologies Co., Ltd.
6 // Author: Jay Fang <f.fangjian@huawei.com>
8 // This code is based on spi-dw-core.c.
10 #include <linux/acpi.h>
11 #include <linux/bitfield.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/spi/spi.h>
21 /* Register offsets */
22 #define HISI_SPI_CSCR 0x00 /* cs control register */
23 #define HISI_SPI_CR 0x04 /* spi common control register */
24 #define HISI_SPI_ENR 0x08 /* spi enable register */
25 #define HISI_SPI_FIFOC 0x0c /* fifo level control register */
26 #define HISI_SPI_IMR 0x10 /* interrupt mask register */
27 #define HISI_SPI_DIN 0x14 /* data in register */
28 #define HISI_SPI_DOUT 0x18 /* data out register */
29 #define HISI_SPI_SR 0x1c /* status register */
30 #define HISI_SPI_RISR 0x20 /* raw interrupt status register */
31 #define HISI_SPI_ISR 0x24 /* interrupt status register */
32 #define HISI_SPI_ICR 0x28 /* interrupt clear register */
33 #define HISI_SPI_VERSION 0xe0 /* version register */
35 /* Bit fields in HISI_SPI_CR */
36 #define CR_LOOP_MASK GENMASK(1, 1)
37 #define CR_CPOL_MASK GENMASK(2, 2)
38 #define CR_CPHA_MASK GENMASK(3, 3)
39 #define CR_DIV_PRE_MASK GENMASK(11, 4)
40 #define CR_DIV_POST_MASK GENMASK(19, 12)
41 #define CR_BPW_MASK GENMASK(24, 20)
42 #define CR_SPD_MODE_MASK GENMASK(25, 25)
44 /* Bit fields in HISI_SPI_FIFOC */
45 #define FIFOC_TX_MASK GENMASK(5, 3)
46 #define FIFOC_RX_MASK GENMASK(11, 9)
48 /* Bit fields in HISI_SPI_IMR, 4 bits */
49 #define IMR_RXOF BIT(0) /* Receive Overflow */
50 #define IMR_RXTO BIT(1) /* Receive Timeout */
51 #define IMR_RX BIT(2) /* Receive */
52 #define IMR_TX BIT(3) /* Transmit */
53 #define IMR_MASK (IMR_RXOF | IMR_RXTO | IMR_RX | IMR_TX)
55 /* Bit fields in HISI_SPI_SR, 5 bits */
56 #define SR_TXE BIT(0) /* Transmit FIFO empty */
57 #define SR_TXNF BIT(1) /* Transmit FIFO not full */
58 #define SR_RXNE BIT(2) /* Receive FIFO not empty */
59 #define SR_RXF BIT(3) /* Receive FIFO full */
60 #define SR_BUSY BIT(4) /* Busy Flag */
62 /* Bit fields in HISI_SPI_ISR, 4 bits */
63 #define ISR_RXOF BIT(0) /* Receive Overflow */
64 #define ISR_RXTO BIT(1) /* Receive Timeout */
65 #define ISR_RX BIT(2) /* Receive */
66 #define ISR_TX BIT(3) /* Transmit */
67 #define ISR_MASK (ISR_RXOF | ISR_RXTO | ISR_RX | ISR_TX)
69 /* Bit fields in HISI_SPI_ICR, 2 bits */
70 #define ICR_RXOF BIT(0) /* Receive Overflow */
71 #define ICR_RXTO BIT(1) /* Receive Timeout */
72 #define ICR_MASK (ICR_RXOF | ICR_RXTO)
74 #define DIV_POST_MAX 0xFF
75 #define DIV_POST_MIN 0x00
76 #define DIV_PRE_MAX 0xFE
77 #define DIV_PRE_MIN 0x02
78 #define CLK_DIV_MAX ((1 + DIV_POST_MAX) * DIV_PRE_MAX)
79 #define CLK_DIV_MIN ((1 + DIV_POST_MIN) * DIV_PRE_MIN)
81 #define DEFAULT_NUM_CS 1
83 #define HISI_SPI_WAIT_TIMEOUT_MS 10UL
85 enum hisi_spi_rx_level_trig {
95 enum hisi_spi_tx_level_trig {
96 HISI_SPI_TX_1_OR_LESS,
97 HISI_SPI_TX_4_OR_LESS,
98 HISI_SPI_TX_8_OR_LESS,
99 HISI_SPI_TX_16_OR_LESS,
100 HISI_SPI_TX_32_OR_LESS,
101 HISI_SPI_TX_64_OR_LESS,
102 HISI_SPI_TX_128_OR_LESS
105 enum hisi_spi_frame_n_bytes {
106 HISI_SPI_N_BYTES_NULL,
108 HISI_SPI_N_BYTES_U16,
109 HISI_SPI_N_BYTES_U32 = 4
112 /* Slave spi_dev related */
113 struct hisi_chip_data {
115 u32 speed_hz; /* baud rate */
116 u16 clk_div; /* baud rate divider */
118 /* clk_div = (1 + div_post) * div_pre */
119 u8 div_post; /* value from 0 to 255 */
120 u8 div_pre; /* value from 2 to 254 (even only!) */
128 u32 fifo_len; /* depth of the FIFO buffer */
130 /* Current message transfer state info */
135 u8 n_bytes; /* current is a 1/2/4 bytes op */
138 static u32 hisi_spi_busy(struct hisi_spi *hs)
140 return readl(hs->regs + HISI_SPI_SR) & SR_BUSY;
143 static u32 hisi_spi_rx_not_empty(struct hisi_spi *hs)
145 return readl(hs->regs + HISI_SPI_SR) & SR_RXNE;
148 static u32 hisi_spi_tx_not_full(struct hisi_spi *hs)
150 return readl(hs->regs + HISI_SPI_SR) & SR_TXNF;
153 static void hisi_spi_flush_fifo(struct hisi_spi *hs)
155 unsigned long limit = loops_per_jiffy << 1;
158 while (hisi_spi_rx_not_empty(hs))
159 readl(hs->regs + HISI_SPI_DOUT);
160 } while (hisi_spi_busy(hs) && limit--);
163 /* Disable the controller and all interrupts */
164 static void hisi_spi_disable(struct hisi_spi *hs)
166 writel(0, hs->regs + HISI_SPI_ENR);
167 writel(IMR_MASK, hs->regs + HISI_SPI_IMR);
168 writel(ICR_MASK, hs->regs + HISI_SPI_ICR);
171 static u8 hisi_spi_n_bytes(struct spi_transfer *transfer)
173 if (transfer->bits_per_word <= 8)
174 return HISI_SPI_N_BYTES_U8;
175 else if (transfer->bits_per_word <= 16)
176 return HISI_SPI_N_BYTES_U16;
178 return HISI_SPI_N_BYTES_U32;
181 static void hisi_spi_reader(struct hisi_spi *hs)
183 u32 max = min_t(u32, hs->rx_len, hs->fifo_len);
186 while (hisi_spi_rx_not_empty(hs) && max--) {
187 rxw = readl(hs->regs + HISI_SPI_DOUT);
188 /* Check the transfer's original "rx" is not null */
190 switch (hs->n_bytes) {
191 case HISI_SPI_N_BYTES_U8:
192 *(u8 *)(hs->rx) = rxw;
194 case HISI_SPI_N_BYTES_U16:
195 *(u16 *)(hs->rx) = rxw;
197 case HISI_SPI_N_BYTES_U32:
198 *(u32 *)(hs->rx) = rxw;
201 hs->rx += hs->n_bytes;
207 static void hisi_spi_writer(struct hisi_spi *hs)
209 u32 max = min_t(u32, hs->tx_len, hs->fifo_len);
212 while (hisi_spi_tx_not_full(hs) && max--) {
213 /* Check the transfer's original "tx" is not null */
215 switch (hs->n_bytes) {
216 case HISI_SPI_N_BYTES_U8:
217 txw = *(u8 *)(hs->tx);
219 case HISI_SPI_N_BYTES_U16:
220 txw = *(u16 *)(hs->tx);
222 case HISI_SPI_N_BYTES_U32:
223 txw = *(u32 *)(hs->tx);
226 hs->tx += hs->n_bytes;
228 writel(txw, hs->regs + HISI_SPI_DIN);
233 static void __hisi_calc_div_reg(struct hisi_chip_data *chip)
235 chip->div_pre = DIV_PRE_MAX;
236 while (chip->div_pre >= DIV_PRE_MIN) {
237 if (chip->clk_div % chip->div_pre == 0)
243 if (chip->div_pre > chip->clk_div)
244 chip->div_pre = chip->clk_div;
246 chip->div_post = (chip->clk_div / chip->div_pre) - 1;
249 static u32 hisi_calc_effective_speed(struct spi_controller *master,
250 struct hisi_chip_data *chip, u32 speed_hz)
254 /* Note clock divider doesn't support odd numbers */
255 chip->clk_div = DIV_ROUND_UP(master->max_speed_hz, speed_hz) + 1;
256 chip->clk_div &= 0xfffe;
257 if (chip->clk_div > CLK_DIV_MAX)
258 chip->clk_div = CLK_DIV_MAX;
260 effective_speed = master->max_speed_hz / chip->clk_div;
261 if (chip->speed_hz != effective_speed) {
262 __hisi_calc_div_reg(chip);
263 chip->speed_hz = effective_speed;
266 return effective_speed;
269 static u32 hisi_spi_prepare_cr(struct spi_device *spi)
271 u32 cr = FIELD_PREP(CR_SPD_MODE_MASK, 1);
273 cr |= FIELD_PREP(CR_CPHA_MASK, (spi->mode & SPI_CPHA) ? 1 : 0);
274 cr |= FIELD_PREP(CR_CPOL_MASK, (spi->mode & SPI_CPOL) ? 1 : 0);
275 cr |= FIELD_PREP(CR_LOOP_MASK, (spi->mode & SPI_LOOP) ? 1 : 0);
280 static void hisi_spi_hw_init(struct hisi_spi *hs)
282 hisi_spi_disable(hs);
284 /* FIFO default config */
285 writel(FIELD_PREP(FIFOC_TX_MASK, HISI_SPI_TX_64_OR_LESS) |
286 FIELD_PREP(FIFOC_RX_MASK, HISI_SPI_RX_16),
287 hs->regs + HISI_SPI_FIFOC);
292 static irqreturn_t hisi_spi_irq(int irq, void *dev_id)
294 struct spi_controller *master = dev_id;
295 struct hisi_spi *hs = spi_controller_get_devdata(master);
296 u32 irq_status = readl(hs->regs + HISI_SPI_ISR) & ISR_MASK;
301 if (!master->cur_msg)
305 if (irq_status & ISR_RXOF) {
306 dev_err(hs->dev, "interrupt_transfer: fifo overflow\n");
307 master->cur_msg->status = -EIO;
308 goto finalize_transfer;
312 * Read data from the Rx FIFO every time. If there is
313 * nothing left to receive, finalize the transfer.
317 goto finalize_transfer;
319 /* Send data out when Tx FIFO IRQ triggered */
320 if (irq_status & ISR_TX)
326 hisi_spi_disable(hs);
327 spi_finalize_current_transfer(master);
331 static int hisi_spi_transfer_one(struct spi_controller *master,
332 struct spi_device *spi, struct spi_transfer *transfer)
334 struct hisi_spi *hs = spi_controller_get_devdata(master);
335 struct hisi_chip_data *chip = spi_get_ctldata(spi);
338 /* Update per transfer options for speed and bpw */
339 transfer->effective_speed_hz =
340 hisi_calc_effective_speed(master, chip, transfer->speed_hz);
341 cr |= FIELD_PREP(CR_DIV_PRE_MASK, chip->div_pre);
342 cr |= FIELD_PREP(CR_DIV_POST_MASK, chip->div_post);
343 cr |= FIELD_PREP(CR_BPW_MASK, transfer->bits_per_word - 1);
344 writel(cr, hs->regs + HISI_SPI_CR);
346 hisi_spi_flush_fifo(hs);
348 hs->n_bytes = hisi_spi_n_bytes(transfer);
349 hs->tx = transfer->tx_buf;
350 hs->tx_len = transfer->len / hs->n_bytes;
351 hs->rx = transfer->rx_buf;
352 hs->rx_len = hs->tx_len;
355 * Ensure that the transfer data above has been updated
356 * before the interrupt to start.
360 /* Enable all interrupts and the controller */
361 writel(~(u32)IMR_MASK, hs->regs + HISI_SPI_IMR);
362 writel(1, hs->regs + HISI_SPI_ENR);
367 static void hisi_spi_handle_err(struct spi_controller *master,
368 struct spi_message *msg)
370 struct hisi_spi *hs = spi_controller_get_devdata(master);
372 hisi_spi_disable(hs);
375 * Wait for interrupt handler that is
376 * already in timeout to complete.
378 msleep(HISI_SPI_WAIT_TIMEOUT_MS);
381 static int hisi_spi_setup(struct spi_device *spi)
383 struct hisi_chip_data *chip;
385 /* Only alloc on first setup */
386 chip = spi_get_ctldata(spi);
388 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
391 spi_set_ctldata(spi, chip);
394 chip->cr = hisi_spi_prepare_cr(spi);
399 static void hisi_spi_cleanup(struct spi_device *spi)
401 struct hisi_chip_data *chip = spi_get_ctldata(spi);
404 spi_set_ctldata(spi, NULL);
407 static int hisi_spi_probe(struct platform_device *pdev)
409 struct device *dev = &pdev->dev;
410 struct spi_controller *master;
414 irq = platform_get_irq(pdev, 0);
418 master = devm_spi_alloc_master(dev, sizeof(*hs));
422 platform_set_drvdata(pdev, master);
424 hs = spi_controller_get_devdata(master);
428 hs->regs = devm_platform_ioremap_resource(pdev, 0);
429 if (IS_ERR(hs->regs))
430 return PTR_ERR(hs->regs);
432 /* Specify maximum SPI clocking speed (master only) by firmware */
433 ret = device_property_read_u32(dev, "spi-max-frequency",
434 &master->max_speed_hz);
436 dev_err(dev, "failed to get max SPI clocking speed, ret=%d\n",
441 ret = device_property_read_u16(dev, "num-cs",
442 &master->num_chipselect);
444 master->num_chipselect = DEFAULT_NUM_CS;
446 master->use_gpio_descriptors = true;
447 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
448 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
449 master->bus_num = pdev->id;
450 master->setup = hisi_spi_setup;
451 master->cleanup = hisi_spi_cleanup;
452 master->transfer_one = hisi_spi_transfer_one;
453 master->handle_err = hisi_spi_handle_err;
454 master->dev.fwnode = dev->fwnode;
456 hisi_spi_hw_init(hs);
458 ret = devm_request_irq(dev, hs->irq, hisi_spi_irq, 0, dev_name(dev),
461 dev_err(dev, "failed to get IRQ=%d, ret=%d\n", hs->irq, ret);
465 ret = spi_register_controller(master);
467 dev_err(dev, "failed to register spi master, ret=%d\n", ret);
471 dev_info(dev, "hw version:0x%x max-freq:%u kHz\n",
472 readl(hs->regs + HISI_SPI_VERSION),
473 master->max_speed_hz / 1000);
478 static int hisi_spi_remove(struct platform_device *pdev)
480 struct spi_controller *master = platform_get_drvdata(pdev);
482 spi_unregister_controller(master);
487 static const struct acpi_device_id hisi_spi_acpi_match[] = {
491 MODULE_DEVICE_TABLE(acpi, hisi_spi_acpi_match);
493 static struct platform_driver hisi_spi_driver = {
494 .probe = hisi_spi_probe,
495 .remove = hisi_spi_remove,
497 .name = "hisi-kunpeng-spi",
498 .acpi_match_table = hisi_spi_acpi_match,
501 module_platform_driver(hisi_spi_driver);
503 MODULE_AUTHOR("Jay Fang <f.fangjian@huawei.com>");
504 MODULE_DESCRIPTION("HiSilicon SPI Controller Driver for Kunpeng SoCs");
505 MODULE_LICENSE("GPL v2");