2 * Driver for Amlogic Meson SPI communication controller (SPICC)
4 * Copyright (C) BayLibre, SAS
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/bitfield.h>
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/spi/spi.h>
21 #include <linux/types.h>
22 #include <linux/interrupt.h>
23 #include <linux/reset.h>
26 * The Meson SPICC controller could support DMA based transfers, but is not
27 * implemented by the vendor code, and while having the registers documentation
28 * it has never worked on the GXL Hardware.
29 * The PIO mode is the only mode implemented, and due to badly designed HW :
30 * - all transfers are cutted in 16 words burst because the FIFO hangs on
31 * TX underflow, and there is no TX "Half-Empty" interrupt, so we go by
32 * FIFO max size chunk only
33 * - CS management is dumb, and goes UP between every burst, so is really a
34 * "Data Valid" signal than a Chip Select, GPIO link should be used instead
35 * to have a CS go down over the full transfer
38 #define SPICC_MAX_BURST 128
41 #define SPICC_RXDATA 0x00
43 #define SPICC_TXDATA 0x04
45 #define SPICC_CONREG 0x08
46 #define SPICC_ENABLE BIT(0)
47 #define SPICC_MODE_MASTER BIT(1)
48 #define SPICC_XCH BIT(2)
49 #define SPICC_SMC BIT(3)
50 #define SPICC_POL BIT(4)
51 #define SPICC_PHA BIT(5)
52 #define SPICC_SSCTL BIT(6)
53 #define SPICC_SSPOL BIT(7)
54 #define SPICC_DRCTL_MASK GENMASK(9, 8)
55 #define SPICC_DRCTL_IGNORE 0
56 #define SPICC_DRCTL_FALLING 1
57 #define SPICC_DRCTL_LOWLEVEL 2
58 #define SPICC_CS_MASK GENMASK(13, 12)
59 #define SPICC_DATARATE_MASK GENMASK(18, 16)
60 #define SPICC_DATARATE_DIV4 0
61 #define SPICC_DATARATE_DIV8 1
62 #define SPICC_DATARATE_DIV16 2
63 #define SPICC_DATARATE_DIV32 3
64 #define SPICC_BITLENGTH_MASK GENMASK(24, 19)
65 #define SPICC_BURSTLENGTH_MASK GENMASK(31, 25)
67 #define SPICC_INTREG 0x0c
68 #define SPICC_TE_EN BIT(0) /* TX FIFO Empty Interrupt */
69 #define SPICC_TH_EN BIT(1) /* TX FIFO Half-Full Interrupt */
70 #define SPICC_TF_EN BIT(2) /* TX FIFO Full Interrupt */
71 #define SPICC_RR_EN BIT(3) /* RX FIFO Ready Interrupt */
72 #define SPICC_RH_EN BIT(4) /* RX FIFO Half-Full Interrupt */
73 #define SPICC_RF_EN BIT(5) /* RX FIFO Full Interrupt */
74 #define SPICC_RO_EN BIT(6) /* RX FIFO Overflow Interrupt */
75 #define SPICC_TC_EN BIT(7) /* Transfert Complete Interrupt */
77 #define SPICC_DMAREG 0x10
78 #define SPICC_DMA_ENABLE BIT(0)
79 #define SPICC_TXFIFO_THRESHOLD_MASK GENMASK(5, 1)
80 #define SPICC_RXFIFO_THRESHOLD_MASK GENMASK(10, 6)
81 #define SPICC_READ_BURST_MASK GENMASK(14, 11)
82 #define SPICC_WRITE_BURST_MASK GENMASK(18, 15)
83 #define SPICC_DMA_URGENT BIT(19)
84 #define SPICC_DMA_THREADID_MASK GENMASK(25, 20)
85 #define SPICC_DMA_BURSTNUM_MASK GENMASK(31, 26)
87 #define SPICC_STATREG 0x14
88 #define SPICC_TE BIT(0) /* TX FIFO Empty Interrupt */
89 #define SPICC_TH BIT(1) /* TX FIFO Half-Full Interrupt */
90 #define SPICC_TF BIT(2) /* TX FIFO Full Interrupt */
91 #define SPICC_RR BIT(3) /* RX FIFO Ready Interrupt */
92 #define SPICC_RH BIT(4) /* RX FIFO Half-Full Interrupt */
93 #define SPICC_RF BIT(5) /* RX FIFO Full Interrupt */
94 #define SPICC_RO BIT(6) /* RX FIFO Overflow Interrupt */
95 #define SPICC_TC BIT(7) /* Transfert Complete Interrupt */
97 #define SPICC_PERIODREG 0x18
98 #define SPICC_PERIOD GENMASK(14, 0) /* Wait cycles */
100 #define SPICC_TESTREG 0x1c
101 #define SPICC_TXCNT_MASK GENMASK(4, 0) /* TX FIFO Counter */
102 #define SPICC_RXCNT_MASK GENMASK(9, 5) /* RX FIFO Counter */
103 #define SPICC_SMSTATUS_MASK GENMASK(12, 10) /* State Machine Status */
104 #define SPICC_LBC_RO BIT(13) /* Loop Back Control Read-Only */
105 #define SPICC_LBC_W1 BIT(14) /* Loop Back Control Write-Only */
106 #define SPICC_SWAP_RO BIT(14) /* RX FIFO Data Swap Read-Only */
107 #define SPICC_SWAP_W1 BIT(15) /* RX FIFO Data Swap Write-Only */
108 #define SPICC_DLYCTL_RO_MASK GENMASK(20, 15) /* Delay Control Read-Only */
109 #define SPICC_MO_DELAY_MASK GENMASK(17, 16) /* Master Output Delay */
110 #define SPICC_MO_NO_DELAY 0
111 #define SPICC_MO_DELAY_1_CYCLE 1
112 #define SPICC_MO_DELAY_2_CYCLE 2
113 #define SPICC_MO_DELAY_3_CYCLE 3
114 #define SPICC_MI_DELAY_MASK GENMASK(19, 18) /* Master Input Delay */
115 #define SPICC_MI_NO_DELAY 0
116 #define SPICC_MI_DELAY_1_CYCLE 1
117 #define SPICC_MI_DELAY_2_CYCLE 2
118 #define SPICC_MI_DELAY_3_CYCLE 3
119 #define SPICC_MI_CAP_DELAY_MASK GENMASK(21, 20) /* Master Capture Delay */
120 #define SPICC_CAP_AHEAD_2_CYCLE 0
121 #define SPICC_CAP_AHEAD_1_CYCLE 1
122 #define SPICC_CAP_NO_DELAY 2
123 #define SPICC_CAP_DELAY_1_CYCLE 3
124 #define SPICC_FIFORST_RO_MASK GENMASK(22, 21) /* FIFO Softreset Read-Only */
125 #define SPICC_FIFORST_W1_MASK GENMASK(23, 22) /* FIFO Softreset Write-Only */
127 #define SPICC_DRADDR 0x20 /* Read Address of DMA */
129 #define SPICC_DWADDR 0x24 /* Write Address of DMA */
131 #define SPICC_ENH_CTL0 0x38 /* Enhanced Feature */
132 #define SPICC_ENH_CLK_CS_DELAY_MASK GENMASK(15, 0)
133 #define SPICC_ENH_DATARATE_MASK GENMASK(23, 16)
134 #define SPICC_ENH_DATARATE_EN BIT(24)
135 #define SPICC_ENH_MOSI_OEN BIT(25)
136 #define SPICC_ENH_CLK_OEN BIT(26)
137 #define SPICC_ENH_CS_OEN BIT(27)
138 #define SPICC_ENH_CLK_CS_DELAY_EN BIT(28)
139 #define SPICC_ENH_MAIN_CLK_AO BIT(29)
141 #define writel_bits_relaxed(mask, val, addr) \
142 writel_relaxed((readl_relaxed(addr) & ~(mask)) | (val), addr)
144 struct meson_spicc_data {
145 unsigned int max_speed_hz;
146 unsigned int min_speed_hz;
147 unsigned int fifo_size;
149 bool has_enhance_clk_div;
153 struct meson_spicc_device {
154 struct spi_master *master;
155 struct platform_device *pdev;
160 struct spi_message *message;
161 struct spi_transfer *xfer;
162 const struct meson_spicc_data *data;
165 unsigned int bytes_per_word;
166 unsigned long tx_remain;
167 unsigned long rx_remain;
168 unsigned long xfer_remain;
171 static void meson_spicc_oen_enable(struct meson_spicc_device *spicc)
175 if (!spicc->data->has_oen)
178 conf = readl_relaxed(spicc->base + SPICC_ENH_CTL0) |
179 SPICC_ENH_MOSI_OEN | SPICC_ENH_CLK_OEN | SPICC_ENH_CS_OEN;
181 writel_relaxed(conf, spicc->base + SPICC_ENH_CTL0);
184 static inline bool meson_spicc_txfull(struct meson_spicc_device *spicc)
186 return !!FIELD_GET(SPICC_TF,
187 readl_relaxed(spicc->base + SPICC_STATREG));
190 static inline bool meson_spicc_rxready(struct meson_spicc_device *spicc)
192 return FIELD_GET(SPICC_RH | SPICC_RR | SPICC_RF,
193 readl_relaxed(spicc->base + SPICC_STATREG));
196 static inline u32 meson_spicc_pull_data(struct meson_spicc_device *spicc)
198 unsigned int bytes = spicc->bytes_per_word;
199 unsigned int byte_shift = 0;
204 byte = *spicc->tx_buf++;
205 data |= (byte & 0xff) << byte_shift;
213 static inline void meson_spicc_push_data(struct meson_spicc_device *spicc,
216 unsigned int bytes = spicc->bytes_per_word;
217 unsigned int byte_shift = 0;
221 byte = (data >> byte_shift) & 0xff;
222 *spicc->rx_buf++ = byte;
229 static inline void meson_spicc_rx(struct meson_spicc_device *spicc)
232 while (spicc->rx_remain &&
233 meson_spicc_rxready(spicc))
234 meson_spicc_push_data(spicc,
235 readl_relaxed(spicc->base + SPICC_RXDATA));
238 static inline void meson_spicc_tx(struct meson_spicc_device *spicc)
240 /* Fill Up TX FIFO */
241 while (spicc->tx_remain &&
242 !meson_spicc_txfull(spicc))
243 writel_relaxed(meson_spicc_pull_data(spicc),
244 spicc->base + SPICC_TXDATA);
247 static inline void meson_spicc_setup_burst(struct meson_spicc_device *spicc)
250 unsigned int burst_len = min_t(unsigned int,
252 spicc->bytes_per_word,
253 spicc->data->fifo_size);
254 /* Setup Xfer variables */
255 spicc->tx_remain = burst_len;
256 spicc->rx_remain = burst_len;
257 spicc->xfer_remain -= burst_len * spicc->bytes_per_word;
259 /* Setup burst length */
260 writel_bits_relaxed(SPICC_BURSTLENGTH_MASK,
261 FIELD_PREP(SPICC_BURSTLENGTH_MASK,
263 spicc->base + SPICC_CONREG);
266 meson_spicc_tx(spicc);
269 static irqreturn_t meson_spicc_irq(int irq, void *data)
271 struct meson_spicc_device *spicc = (void *) data;
273 writel_bits_relaxed(SPICC_TC, SPICC_TC, spicc->base + SPICC_STATREG);
276 meson_spicc_rx(spicc);
278 if (!spicc->xfer_remain) {
279 /* Disable all IRQs */
280 writel(0, spicc->base + SPICC_INTREG);
282 spi_finalize_current_transfer(spicc->master);
288 meson_spicc_setup_burst(spicc);
291 writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG);
296 static void meson_spicc_auto_io_delay(struct meson_spicc_device *spicc)
299 u32 mi_delay, cap_delay;
302 if (spicc->data->has_enhance_clk_div) {
303 div = FIELD_GET(SPICC_ENH_DATARATE_MASK,
304 readl_relaxed(spicc->base + SPICC_ENH_CTL0));
308 div = FIELD_GET(SPICC_DATARATE_MASK,
309 readl_relaxed(spicc->base + SPICC_CONREG));
314 mi_delay = SPICC_MI_NO_DELAY;
315 cap_delay = SPICC_CAP_AHEAD_2_CYCLE;
316 hz = clk_get_rate(spicc->clk);
319 cap_delay = SPICC_CAP_DELAY_1_CYCLE;
320 else if (hz >= 80000000)
321 cap_delay = SPICC_CAP_NO_DELAY;
322 else if (hz >= 40000000)
323 cap_delay = SPICC_CAP_AHEAD_1_CYCLE;
325 mi_delay = SPICC_MI_DELAY_3_CYCLE;
327 mi_delay = SPICC_MI_DELAY_2_CYCLE;
329 mi_delay = SPICC_MI_DELAY_1_CYCLE;
331 conf = readl_relaxed(spicc->base + SPICC_TESTREG);
332 conf &= ~(SPICC_MO_DELAY_MASK | SPICC_MI_DELAY_MASK
333 | SPICC_MI_CAP_DELAY_MASK);
334 conf |= FIELD_PREP(SPICC_MI_DELAY_MASK, mi_delay);
335 conf |= FIELD_PREP(SPICC_MI_CAP_DELAY_MASK, cap_delay);
336 writel_relaxed(conf, spicc->base + SPICC_TESTREG);
339 static void meson_spicc_setup_xfer(struct meson_spicc_device *spicc,
340 struct spi_transfer *xfer)
344 /* Read original configuration */
345 conf = conf_orig = readl_relaxed(spicc->base + SPICC_CONREG);
347 /* Setup word width */
348 conf &= ~SPICC_BITLENGTH_MASK;
349 conf |= FIELD_PREP(SPICC_BITLENGTH_MASK,
350 (spicc->bytes_per_word << 3) - 1);
352 /* Ignore if unchanged */
353 if (conf != conf_orig)
354 writel_relaxed(conf, spicc->base + SPICC_CONREG);
356 clk_set_rate(spicc->clk, xfer->speed_hz);
358 meson_spicc_auto_io_delay(spicc);
360 writel_relaxed(0, spicc->base + SPICC_DMAREG);
363 static void meson_spicc_reset_fifo(struct meson_spicc_device *spicc)
365 if (spicc->data->has_oen)
366 writel_bits_relaxed(SPICC_ENH_MAIN_CLK_AO,
367 SPICC_ENH_MAIN_CLK_AO,
368 spicc->base + SPICC_ENH_CTL0);
370 writel_bits_relaxed(SPICC_FIFORST_W1_MASK, SPICC_FIFORST_W1_MASK,
371 spicc->base + SPICC_TESTREG);
373 while (meson_spicc_rxready(spicc))
374 readl_relaxed(spicc->base + SPICC_RXDATA);
376 if (spicc->data->has_oen)
377 writel_bits_relaxed(SPICC_ENH_MAIN_CLK_AO, 0,
378 spicc->base + SPICC_ENH_CTL0);
381 static int meson_spicc_transfer_one(struct spi_master *master,
382 struct spi_device *spi,
383 struct spi_transfer *xfer)
385 struct meson_spicc_device *spicc = spi_master_get_devdata(master);
387 /* Store current transfer */
390 /* Setup transfer parameters */
391 spicc->tx_buf = (u8 *)xfer->tx_buf;
392 spicc->rx_buf = (u8 *)xfer->rx_buf;
393 spicc->xfer_remain = xfer->len;
395 /* Pre-calculate word size */
396 spicc->bytes_per_word =
397 DIV_ROUND_UP(spicc->xfer->bits_per_word, 8);
399 if (xfer->len % spicc->bytes_per_word)
402 /* Setup transfer parameters */
403 meson_spicc_setup_xfer(spicc, xfer);
405 meson_spicc_reset_fifo(spicc);
408 meson_spicc_setup_burst(spicc);
411 writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG);
413 /* Enable interrupts */
414 writel_relaxed(SPICC_TC_EN, spicc->base + SPICC_INTREG);
419 static int meson_spicc_prepare_message(struct spi_master *master,
420 struct spi_message *message)
422 struct meson_spicc_device *spicc = spi_master_get_devdata(master);
423 struct spi_device *spi = message->spi;
426 /* Store current message */
427 spicc->message = message;
430 conf |= SPICC_ENABLE;
431 conf |= SPICC_MODE_MASTER;
435 /* Setup transfer mode */
436 if (spi->mode & SPI_CPOL)
441 if (spi->mode & SPI_CPHA)
448 if (spi->mode & SPI_CS_HIGH)
451 conf &= ~SPICC_SSPOL;
453 if (spi->mode & SPI_READY)
454 conf |= FIELD_PREP(SPICC_DRCTL_MASK, SPICC_DRCTL_LOWLEVEL);
456 conf |= FIELD_PREP(SPICC_DRCTL_MASK, SPICC_DRCTL_IGNORE);
459 conf |= FIELD_PREP(SPICC_CS_MASK, spi->chip_select);
461 /* Default Clock rate core/4 */
463 /* Default 8bit word */
464 conf |= FIELD_PREP(SPICC_BITLENGTH_MASK, 8 - 1);
466 writel_relaxed(conf, spicc->base + SPICC_CONREG);
468 /* Setup no wait cycles by default */
469 writel_relaxed(0, spicc->base + SPICC_PERIODREG);
471 writel_bits_relaxed(SPICC_LBC_W1, 0, spicc->base + SPICC_TESTREG);
476 static int meson_spicc_unprepare_transfer(struct spi_master *master)
478 struct meson_spicc_device *spicc = spi_master_get_devdata(master);
480 /* Disable all IRQs */
481 writel(0, spicc->base + SPICC_INTREG);
483 device_reset_optional(&spicc->pdev->dev);
488 static int meson_spicc_setup(struct spi_device *spi)
490 if (!spi->controller_state)
491 spi->controller_state = spi_master_get_devdata(spi->master);
496 static void meson_spicc_cleanup(struct spi_device *spi)
498 spi->controller_state = NULL;
503 * x-----------------x x------------x x------\
504 * |---| pow2 fixed div |---| pow2 div |----| |
505 * | x-----------------x x------------x | |
506 * src ---| | mux |-- out
507 * | x-----------------x x------------x | |
508 * |---| enh fixed div |---| enh div |0---| |
509 * x-----------------x x------------x x------/
511 * Clk path for GX series:
512 * src -> pow2 fixed div -> pow2 div -> out
514 * Clk path for AXG series:
515 * src -> pow2 fixed div -> pow2 div -> mux -> out
516 * src -> enh fixed div -> enh div -> mux -> out
518 * Clk path for G12A series:
519 * pclk -> pow2 fixed div -> pow2 div -> mux -> out
520 * pclk -> enh fixed div -> enh div -> mux -> out
523 static int meson_spicc_clk_init(struct meson_spicc_device *spicc)
525 struct device *dev = &spicc->pdev->dev;
526 struct clk_fixed_factor *pow2_fixed_div, *enh_fixed_div;
527 struct clk_divider *pow2_div, *enh_div;
529 struct clk_init_data init;
531 struct clk_parent_data parent_data[2];
534 memset(&init, 0, sizeof(init));
535 memset(&parent_data, 0, sizeof(parent_data));
537 init.parent_data = parent_data;
539 /* algorithm for pow2 div: rate = freq / 4 / (2 ^ N) */
541 pow2_fixed_div = devm_kzalloc(dev, sizeof(*pow2_fixed_div), GFP_KERNEL);
545 snprintf(name, sizeof(name), "%s#pow2_fixed_div", dev_name(dev));
547 init.ops = &clk_fixed_factor_ops;
549 if (spicc->data->has_pclk)
550 parent_data[0].hw = __clk_get_hw(spicc->pclk);
552 parent_data[0].hw = __clk_get_hw(spicc->core);
553 init.num_parents = 1;
555 pow2_fixed_div->mult = 1,
556 pow2_fixed_div->div = 4,
557 pow2_fixed_div->hw.init = &init;
559 clk = devm_clk_register(dev, &pow2_fixed_div->hw);
560 if (WARN_ON(IS_ERR(clk)))
563 pow2_div = devm_kzalloc(dev, sizeof(*pow2_div), GFP_KERNEL);
567 snprintf(name, sizeof(name), "%s#pow2_div", dev_name(dev));
569 init.ops = &clk_divider_ops;
570 init.flags = CLK_SET_RATE_PARENT;
571 parent_data[0].hw = &pow2_fixed_div->hw;
572 init.num_parents = 1;
574 pow2_div->shift = 16,
576 pow2_div->flags = CLK_DIVIDER_POWER_OF_TWO,
577 pow2_div->reg = spicc->base + SPICC_CONREG;
578 pow2_div->hw.init = &init;
580 clk = devm_clk_register(dev, &pow2_div->hw);
581 if (WARN_ON(IS_ERR(clk)))
584 if (!spicc->data->has_enhance_clk_div) {
589 /* algorithm for enh div: rate = freq / 2 / (N + 1) */
591 enh_fixed_div = devm_kzalloc(dev, sizeof(*enh_fixed_div), GFP_KERNEL);
595 snprintf(name, sizeof(name), "%s#enh_fixed_div", dev_name(dev));
597 init.ops = &clk_fixed_factor_ops;
599 if (spicc->data->has_pclk)
600 parent_data[0].hw = __clk_get_hw(spicc->pclk);
602 parent_data[0].hw = __clk_get_hw(spicc->core);
603 init.num_parents = 1;
605 enh_fixed_div->mult = 1,
606 enh_fixed_div->div = 2,
607 enh_fixed_div->hw.init = &init;
609 clk = devm_clk_register(dev, &enh_fixed_div->hw);
610 if (WARN_ON(IS_ERR(clk)))
613 enh_div = devm_kzalloc(dev, sizeof(*enh_div), GFP_KERNEL);
617 snprintf(name, sizeof(name), "%s#enh_div", dev_name(dev));
619 init.ops = &clk_divider_ops;
620 init.flags = CLK_SET_RATE_PARENT;
621 parent_data[0].hw = &enh_fixed_div->hw;
622 init.num_parents = 1;
626 enh_div->reg = spicc->base + SPICC_ENH_CTL0;
627 enh_div->hw.init = &init;
629 clk = devm_clk_register(dev, &enh_div->hw);
630 if (WARN_ON(IS_ERR(clk)))
633 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
637 snprintf(name, sizeof(name), "%s#sel", dev_name(dev));
639 init.ops = &clk_mux_ops;
640 parent_data[0].hw = &pow2_div->hw;
641 parent_data[1].hw = &enh_div->hw;
642 init.num_parents = 2;
643 init.flags = CLK_SET_RATE_PARENT;
647 mux->reg = spicc->base + SPICC_ENH_CTL0;
648 mux->hw.init = &init;
650 spicc->clk = devm_clk_register(dev, &mux->hw);
651 if (WARN_ON(IS_ERR(spicc->clk)))
652 return PTR_ERR(spicc->clk);
657 static int meson_spicc_probe(struct platform_device *pdev)
659 struct spi_master *master;
660 struct meson_spicc_device *spicc;
663 master = spi_alloc_master(&pdev->dev, sizeof(*spicc));
665 dev_err(&pdev->dev, "master allocation failed\n");
668 spicc = spi_master_get_devdata(master);
669 spicc->master = master;
671 spicc->data = of_device_get_match_data(&pdev->dev);
673 dev_err(&pdev->dev, "failed to get match data\n");
679 platform_set_drvdata(pdev, spicc);
681 spicc->base = devm_platform_ioremap_resource(pdev, 0);
682 if (IS_ERR(spicc->base)) {
683 dev_err(&pdev->dev, "io resource mapping failed\n");
684 ret = PTR_ERR(spicc->base);
688 /* Set master mode and enable controller */
689 writel_relaxed(SPICC_ENABLE | SPICC_MODE_MASTER,
690 spicc->base + SPICC_CONREG);
692 /* Disable all IRQs */
693 writel_relaxed(0, spicc->base + SPICC_INTREG);
695 irq = platform_get_irq(pdev, 0);
701 ret = devm_request_irq(&pdev->dev, irq, meson_spicc_irq,
704 dev_err(&pdev->dev, "irq request failed\n");
708 spicc->core = devm_clk_get(&pdev->dev, "core");
709 if (IS_ERR(spicc->core)) {
710 dev_err(&pdev->dev, "core clock request failed\n");
711 ret = PTR_ERR(spicc->core);
715 if (spicc->data->has_pclk) {
716 spicc->pclk = devm_clk_get(&pdev->dev, "pclk");
717 if (IS_ERR(spicc->pclk)) {
718 dev_err(&pdev->dev, "pclk clock request failed\n");
719 ret = PTR_ERR(spicc->pclk);
724 ret = clk_prepare_enable(spicc->core);
726 dev_err(&pdev->dev, "core clock enable failed\n");
730 ret = clk_prepare_enable(spicc->pclk);
732 dev_err(&pdev->dev, "pclk clock enable failed\n");
736 device_reset_optional(&pdev->dev);
738 master->num_chipselect = 4;
739 master->dev.of_node = pdev->dev.of_node;
740 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH;
741 master->bits_per_word_mask = SPI_BPW_MASK(32) |
745 master->flags = (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
746 master->min_speed_hz = spicc->data->min_speed_hz;
747 master->max_speed_hz = spicc->data->max_speed_hz;
748 master->setup = meson_spicc_setup;
749 master->cleanup = meson_spicc_cleanup;
750 master->prepare_message = meson_spicc_prepare_message;
751 master->unprepare_transfer_hardware = meson_spicc_unprepare_transfer;
752 master->transfer_one = meson_spicc_transfer_one;
753 master->use_gpio_descriptors = true;
755 meson_spicc_oen_enable(spicc);
757 ret = meson_spicc_clk_init(spicc);
759 dev_err(&pdev->dev, "clock registration failed\n");
763 ret = devm_spi_register_master(&pdev->dev, master);
765 dev_err(&pdev->dev, "spi master registration failed\n");
772 clk_disable_unprepare(spicc->pclk);
775 clk_disable_unprepare(spicc->core);
778 spi_master_put(master);
783 static int meson_spicc_remove(struct platform_device *pdev)
785 struct meson_spicc_device *spicc = platform_get_drvdata(pdev);
788 writel(0, spicc->base + SPICC_CONREG);
790 clk_disable_unprepare(spicc->core);
791 clk_disable_unprepare(spicc->pclk);
793 spi_master_put(spicc->master);
798 static const struct meson_spicc_data meson_spicc_gx_data = {
799 .max_speed_hz = 30000000,
800 .min_speed_hz = 325000,
804 static const struct meson_spicc_data meson_spicc_axg_data = {
805 .max_speed_hz = 80000000,
806 .min_speed_hz = 325000,
809 .has_enhance_clk_div = true,
812 static const struct meson_spicc_data meson_spicc_g12a_data = {
813 .max_speed_hz = 166666666,
814 .min_speed_hz = 50000,
817 .has_enhance_clk_div = true,
821 static const struct of_device_id meson_spicc_of_match[] = {
823 .compatible = "amlogic,meson-gx-spicc",
824 .data = &meson_spicc_gx_data,
827 .compatible = "amlogic,meson-axg-spicc",
828 .data = &meson_spicc_axg_data,
831 .compatible = "amlogic,meson-g12a-spicc",
832 .data = &meson_spicc_g12a_data,
836 MODULE_DEVICE_TABLE(of, meson_spicc_of_match);
838 static struct platform_driver meson_spicc_driver = {
839 .probe = meson_spicc_probe,
840 .remove = meson_spicc_remove,
842 .name = "meson-spicc",
843 .of_match_table = of_match_ptr(meson_spicc_of_match),
847 module_platform_driver(meson_spicc_driver);
849 MODULE_DESCRIPTION("Meson SPI Communication Controller driver");
850 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
851 MODULE_LICENSE("GPL");