1 // SPDX-License-Identifier: (GPL-2.0)
3 * Microchip CoreSPI SPI controller driver
5 * Copyright (c) 2018-2022 Microchip Technology Inc. and its subsidiaries
7 * Author: Daire McNamara <daire.mcnamara@microchip.com>
8 * Author: Conor Dooley <conor.dooley@microchip.com>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
23 #define MAX_LEN (0xffff)
25 #define DEFAULT_FRAMESIZE (8)
26 #define FIFO_DEPTH (32)
27 #define CLK_GEN_MODE1_MAX (255)
28 #define CLK_GEN_MODE0_MAX (15)
29 #define CLK_GEN_MIN (0)
30 #define MODE_X_MASK_SHIFT (24)
32 #define CONTROL_ENABLE BIT(0)
33 #define CONTROL_MASTER BIT(1)
34 #define CONTROL_RX_DATA_INT BIT(4)
35 #define CONTROL_TX_DATA_INT BIT(5)
36 #define CONTROL_RX_OVER_INT BIT(6)
37 #define CONTROL_TX_UNDER_INT BIT(7)
38 #define CONTROL_SPO BIT(24)
39 #define CONTROL_SPH BIT(25)
40 #define CONTROL_SPS BIT(26)
41 #define CONTROL_FRAMEURUN BIT(27)
42 #define CONTROL_CLKMODE BIT(28)
43 #define CONTROL_BIGFIFO BIT(29)
44 #define CONTROL_OENOFF BIT(30)
45 #define CONTROL_RESET BIT(31)
47 #define CONTROL_MODE_MASK GENMASK(3, 2)
48 #define MOTOROLA_MODE (0)
49 #define CONTROL_FRAMECNT_MASK GENMASK(23, 8)
50 #define CONTROL_FRAMECNT_SHIFT (8)
52 #define STATUS_ACTIVE BIT(14)
53 #define STATUS_SSEL BIT(13)
54 #define STATUS_FRAMESTART BIT(12)
55 #define STATUS_TXFIFO_EMPTY_NEXT_READ BIT(11)
56 #define STATUS_TXFIFO_EMPTY BIT(10)
57 #define STATUS_TXFIFO_FULL_NEXT_WRITE BIT(9)
58 #define STATUS_TXFIFO_FULL BIT(8)
59 #define STATUS_RXFIFO_EMPTY_NEXT_READ BIT(7)
60 #define STATUS_RXFIFO_EMPTY BIT(6)
61 #define STATUS_RXFIFO_FULL_NEXT_WRITE BIT(5)
62 #define STATUS_RXFIFO_FULL BIT(4)
63 #define STATUS_TX_UNDERRUN BIT(3)
64 #define STATUS_RX_OVERFLOW BIT(2)
65 #define STATUS_RXDAT_RXED BIT(1)
66 #define STATUS_TXDAT_SENT BIT(0)
68 #define INT_TXDONE BIT(0)
69 #define INT_RXRDY BIT(1)
70 #define INT_RX_CHANNEL_OVERFLOW BIT(2)
71 #define INT_TX_CHANNEL_UNDERRUN BIT(3)
73 #define INT_ENABLE_MASK (CONTROL_RX_DATA_INT | CONTROL_TX_DATA_INT | \
74 CONTROL_RX_OVER_INT | CONTROL_TX_UNDER_INT)
76 #define REG_CONTROL (0x00)
77 #define REG_FRAME_SIZE (0x04)
78 #define REG_STATUS (0x08)
79 #define REG_INT_CLEAR (0x0c)
80 #define REG_RX_DATA (0x10)
81 #define REG_TX_DATA (0x14)
82 #define REG_CLK_GEN (0x18)
83 #define REG_SLAVE_SELECT (0x1c)
84 #define SSEL_MASK GENMASK(7, 0)
85 #define SSEL_DIRECT BIT(8)
86 #define SSELOUT_SHIFT 9
87 #define SSELOUT BIT(SSELOUT_SHIFT)
88 #define REG_MIS (0x20)
89 #define REG_RIS (0x24)
90 #define REG_CONTROL2 (0x28)
91 #define REG_COMMAND (0x2c)
92 #define REG_PKTSIZE (0x30)
93 #define REG_CMD_SIZE (0x34)
94 #define REG_HWSTATUS (0x38)
95 #define REG_STAT8 (0x3c)
96 #define REG_CTRL2 (0x48)
97 #define REG_FRAMESUP (0x50)
104 u32 clk_gen; /* divider for spi output clock generated by the controller */
112 static inline u32 mchp_corespi_read(struct mchp_corespi *spi, unsigned int reg)
114 return readl(spi->regs + reg);
117 static inline void mchp_corespi_write(struct mchp_corespi *spi, unsigned int reg, u32 val)
119 writel(val, spi->regs + reg);
122 static inline void mchp_corespi_enable(struct mchp_corespi *spi)
124 u32 control = mchp_corespi_read(spi, REG_CONTROL);
126 control |= CONTROL_ENABLE;
128 mchp_corespi_write(spi, REG_CONTROL, control);
131 static inline void mchp_corespi_disable(struct mchp_corespi *spi)
133 u32 control = mchp_corespi_read(spi, REG_CONTROL);
135 control &= ~CONTROL_ENABLE;
137 mchp_corespi_write(spi, REG_CONTROL, control);
140 static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi)
145 fifo_max = min(spi->rx_len, FIFO_DEPTH);
147 while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)) {
148 data = mchp_corespi_read(spi, REG_RX_DATA);
151 *spi->rx_buf++ = data;
158 static void mchp_corespi_enable_ints(struct mchp_corespi *spi)
160 u32 control, mask = INT_ENABLE_MASK;
162 mchp_corespi_disable(spi);
164 control = mchp_corespi_read(spi, REG_CONTROL);
167 mchp_corespi_write(spi, REG_CONTROL, control);
169 control |= CONTROL_ENABLE;
170 mchp_corespi_write(spi, REG_CONTROL, control);
173 static void mchp_corespi_disable_ints(struct mchp_corespi *spi)
175 u32 control, mask = INT_ENABLE_MASK;
177 mchp_corespi_disable(spi);
179 control = mchp_corespi_read(spi, REG_CONTROL);
181 mchp_corespi_write(spi, REG_CONTROL, control);
183 control |= CONTROL_ENABLE;
184 mchp_corespi_write(spi, REG_CONTROL, control);
187 static inline void mchp_corespi_set_xfer_size(struct mchp_corespi *spi, int len)
193 * Disable the SPI controller. Writes to transfer length have
194 * no effect when the controller is enabled.
196 mchp_corespi_disable(spi);
199 * The lower 16 bits of the frame count are stored in the control reg
200 * for legacy reasons, but the upper 16 written to a different register:
201 * FRAMESUP. While both the upper and lower bits can be *READ* from the
202 * FRAMESUP register, writing to the lower 16 bits is a NOP
204 lenpart = len & 0xffff;
206 control = mchp_corespi_read(spi, REG_CONTROL);
207 control &= ~CONTROL_FRAMECNT_MASK;
208 control |= lenpart << CONTROL_FRAMECNT_SHIFT;
209 mchp_corespi_write(spi, REG_CONTROL, control);
211 lenpart = len & 0xffff0000;
212 mchp_corespi_write(spi, REG_FRAMESUP, lenpart);
214 control |= CONTROL_ENABLE;
215 mchp_corespi_write(spi, REG_CONTROL, control);
218 static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi)
223 fifo_max = min(spi->tx_len, FIFO_DEPTH);
224 mchp_corespi_set_xfer_size(spi, fifo_max);
226 while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_TXFIFO_FULL)) {
227 byte = spi->tx_buf ? *spi->tx_buf++ : 0xaa;
228 mchp_corespi_write(spi, REG_TX_DATA, byte);
236 static inline void mchp_corespi_set_framesize(struct mchp_corespi *spi, int bt)
241 * Disable the SPI controller. Writes to the frame size have
242 * no effect when the controller is enabled.
244 mchp_corespi_disable(spi);
246 mchp_corespi_write(spi, REG_FRAME_SIZE, bt);
248 control = mchp_corespi_read(spi, REG_CONTROL);
249 control |= CONTROL_ENABLE;
250 mchp_corespi_write(spi, REG_CONTROL, control);
253 static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
256 struct mchp_corespi *corespi = spi_master_get_devdata(spi->master);
258 reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
259 reg &= ~BIT(spi->chip_select);
260 reg |= !disable << spi->chip_select;
262 mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
265 static int mchp_corespi_setup(struct spi_device *spi)
267 struct mchp_corespi *corespi = spi_master_get_devdata(spi->master);
271 * Active high slaves need to be specifically set to their inactive
272 * states during probe by adding them to the "control group" & thus
273 * driving their select line low.
275 if (spi->mode & SPI_CS_HIGH) {
276 reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
277 reg |= BIT(spi->chip_select);
278 mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
283 static void mchp_corespi_init(struct spi_master *master, struct mchp_corespi *spi)
285 unsigned long clk_hz;
286 u32 control = mchp_corespi_read(spi, REG_CONTROL);
288 control |= CONTROL_MASTER;
290 control &= ~CONTROL_MODE_MASK;
291 control |= MOTOROLA_MODE;
293 mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE);
295 /* max. possible spi clock rate is the apb clock rate */
296 clk_hz = clk_get_rate(spi->clk);
297 master->max_speed_hz = clk_hz;
300 * The controller must be configured so that it doesn't remove Chip
301 * Select until the entire message has been transferred, even if at
302 * some points TX FIFO becomes empty.
304 * BIGFIFO mode is also enabled, which sets the fifo depth to 32 frames
305 * for the 8 bit transfers that this driver uses.
307 control = mchp_corespi_read(spi, REG_CONTROL);
308 control |= CONTROL_SPS | CONTROL_BIGFIFO;
310 mchp_corespi_write(spi, REG_CONTROL, control);
312 mchp_corespi_enable_ints(spi);
315 * It is required to enable direct mode, otherwise control over the chip
316 * select is relinquished to the hardware. SSELOUT is enabled too so we
317 * can deal with active high slaves.
319 mchp_corespi_write(spi, REG_SLAVE_SELECT, SSELOUT | SSEL_DIRECT);
321 control = mchp_corespi_read(spi, REG_CONTROL);
323 control &= ~CONTROL_RESET;
324 control |= CONTROL_ENABLE;
326 mchp_corespi_write(spi, REG_CONTROL, control);
329 static inline void mchp_corespi_set_clk_gen(struct mchp_corespi *spi)
333 mchp_corespi_disable(spi);
335 control = mchp_corespi_read(spi, REG_CONTROL);
337 control |= CONTROL_CLKMODE;
339 control &= ~CONTROL_CLKMODE;
341 mchp_corespi_write(spi, REG_CLK_GEN, spi->clk_gen);
342 mchp_corespi_write(spi, REG_CONTROL, control);
343 mchp_corespi_write(spi, REG_CONTROL, control | CONTROL_ENABLE);
346 static inline void mchp_corespi_set_mode(struct mchp_corespi *spi, unsigned int mode)
348 u32 control, mode_val;
350 switch (mode & SPI_MODE_X_MASK) {
355 mode_val = CONTROL_SPH;
358 mode_val = CONTROL_SPO;
361 mode_val = CONTROL_SPH | CONTROL_SPO;
366 * Disable the SPI controller. Writes to the frame size have
367 * no effect when the controller is enabled.
369 mchp_corespi_disable(spi);
371 control = mchp_corespi_read(spi, REG_CONTROL);
372 control &= ~(SPI_MODE_X_MASK << MODE_X_MASK_SHIFT);
375 mchp_corespi_write(spi, REG_CONTROL, control);
377 control |= CONTROL_ENABLE;
378 mchp_corespi_write(spi, REG_CONTROL, control);
381 static irqreturn_t mchp_corespi_interrupt(int irq, void *dev_id)
383 struct spi_master *master = dev_id;
384 struct mchp_corespi *spi = spi_master_get_devdata(master);
385 u32 intfield = mchp_corespi_read(spi, REG_MIS) & 0xf;
386 bool finalise = false;
388 /* Interrupt line may be shared and not for us at all */
392 if (intfield & INT_TXDONE) {
393 mchp_corespi_write(spi, REG_INT_CLEAR, INT_TXDONE);
396 mchp_corespi_read_fifo(spi);
399 mchp_corespi_write_fifo(spi);
405 if (intfield & INT_RXRDY)
406 mchp_corespi_write(spi, REG_INT_CLEAR, INT_RXRDY);
408 if (intfield & INT_RX_CHANNEL_OVERFLOW) {
409 mchp_corespi_write(spi, REG_INT_CLEAR, INT_RX_CHANNEL_OVERFLOW);
411 dev_err(&master->dev,
412 "%s: RX OVERFLOW: rxlen: %d, txlen: %d\n", __func__,
413 spi->rx_len, spi->tx_len);
416 if (intfield & INT_TX_CHANNEL_UNDERRUN) {
417 mchp_corespi_write(spi, REG_INT_CLEAR, INT_TX_CHANNEL_UNDERRUN);
419 dev_err(&master->dev,
420 "%s: TX UNDERFLOW: rxlen: %d, txlen: %d\n", __func__,
421 spi->rx_len, spi->tx_len);
425 spi_finalize_current_transfer(master);
430 static int mchp_corespi_calculate_clkgen(struct mchp_corespi *spi,
431 unsigned long target_hz)
433 unsigned long clk_hz, spi_hz, clk_gen;
435 clk_hz = clk_get_rate(spi->clk);
438 spi_hz = min(target_hz, clk_hz);
441 * There are two possible clock modes for the controller generated
442 * clock's division ratio:
443 * CLK_MODE = 0: 1 / (2^(CLK_GEN + 1)) where CLK_GEN = 0 to 15.
444 * CLK_MODE = 1: 1 / (2 * CLK_GEN + 1) where CLK_GEN = 0 to 255.
445 * First try mode 1, fall back to 0 and if we have tried both modes and
446 * we /still/ can't get a good setting, we then throw the toys out of
447 * the pram and give up
448 * clk_gen is the register name for the clock divider on MPFS.
450 clk_gen = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
451 if (clk_gen > CLK_GEN_MODE1_MAX || clk_gen <= CLK_GEN_MIN) {
452 clk_gen = DIV_ROUND_UP(clk_hz, spi_hz);
453 clk_gen = fls(clk_gen) - 1;
455 if (clk_gen > CLK_GEN_MODE0_MAX)
463 spi->clk_gen = clk_gen;
467 static int mchp_corespi_transfer_one(struct spi_master *master,
468 struct spi_device *spi_dev,
469 struct spi_transfer *xfer)
471 struct mchp_corespi *spi = spi_master_get_devdata(master);
474 ret = mchp_corespi_calculate_clkgen(spi, (unsigned long)xfer->speed_hz);
476 dev_err(&master->dev, "failed to set clk_gen for target %u Hz\n", xfer->speed_hz);
480 mchp_corespi_set_clk_gen(spi);
482 spi->tx_buf = xfer->tx_buf;
483 spi->rx_buf = xfer->rx_buf;
484 spi->tx_len = xfer->len;
485 spi->rx_len = xfer->len;
488 mchp_corespi_set_xfer_size(spi, (spi->tx_len > FIFO_DEPTH)
489 ? FIFO_DEPTH : spi->tx_len);
492 mchp_corespi_write_fifo(spi);
496 static int mchp_corespi_prepare_message(struct spi_master *master,
497 struct spi_message *msg)
499 struct spi_device *spi_dev = msg->spi;
500 struct mchp_corespi *spi = spi_master_get_devdata(master);
502 mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE);
503 mchp_corespi_set_mode(spi, spi_dev->mode);
508 static int mchp_corespi_probe(struct platform_device *pdev)
510 struct spi_master *master;
511 struct mchp_corespi *spi;
512 struct resource *res;
516 master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi));
518 return dev_err_probe(&pdev->dev, -ENOMEM,
519 "unable to allocate master for SPI controller\n");
521 platform_set_drvdata(pdev, master);
523 if (of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs))
526 master->num_chipselect = num_cs;
527 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
528 master->setup = mchp_corespi_setup;
529 master->bits_per_word_mask = SPI_BPW_MASK(8);
530 master->transfer_one = mchp_corespi_transfer_one;
531 master->prepare_message = mchp_corespi_prepare_message;
532 master->set_cs = mchp_corespi_set_cs;
533 master->dev.of_node = pdev->dev.of_node;
535 spi = spi_master_get_devdata(master);
537 spi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
538 if (IS_ERR(spi->regs))
539 return PTR_ERR(spi->regs);
541 spi->irq = platform_get_irq(pdev, 0);
543 return dev_err_probe(&pdev->dev, -ENXIO,
544 "invalid IRQ %d for SPI controller\n",
547 ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
548 IRQF_SHARED, dev_name(&pdev->dev), master);
550 return dev_err_probe(&pdev->dev, ret,
551 "could not request irq\n");
553 spi->clk = devm_clk_get(&pdev->dev, NULL);
554 if (IS_ERR(spi->clk))
555 return dev_err_probe(&pdev->dev, PTR_ERR(spi->clk),
556 "could not get clk\n");
558 ret = clk_prepare_enable(spi->clk);
560 return dev_err_probe(&pdev->dev, ret,
561 "failed to enable clock\n");
563 mchp_corespi_init(master, spi);
565 ret = devm_spi_register_master(&pdev->dev, master);
567 mchp_corespi_disable(spi);
568 clk_disable_unprepare(spi->clk);
569 return dev_err_probe(&pdev->dev, ret,
570 "unable to register master for SPI controller\n");
573 dev_info(&pdev->dev, "Registered SPI controller %d\n", master->bus_num);
578 static int mchp_corespi_remove(struct platform_device *pdev)
580 struct spi_master *master = platform_get_drvdata(pdev);
581 struct mchp_corespi *spi = spi_master_get_devdata(master);
583 mchp_corespi_disable_ints(spi);
584 clk_disable_unprepare(spi->clk);
585 mchp_corespi_disable(spi);
590 #define MICROCHIP_SPI_PM_OPS (NULL)
593 * Platform driver data structure
596 #if defined(CONFIG_OF)
597 static const struct of_device_id mchp_corespi_dt_ids[] = {
598 { .compatible = "microchip,mpfs-spi" },
601 MODULE_DEVICE_TABLE(of, mchp_corespi_dt_ids);
604 static struct platform_driver mchp_corespi_driver = {
605 .probe = mchp_corespi_probe,
607 .name = "microchip-corespi",
608 .pm = MICROCHIP_SPI_PM_OPS,
609 .of_match_table = of_match_ptr(mchp_corespi_dt_ids),
611 .remove = mchp_corespi_remove,
613 module_platform_driver(mchp_corespi_driver);
614 MODULE_DESCRIPTION("Microchip coreSPI SPI controller driver");
615 MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");
616 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
617 MODULE_LICENSE("GPL");