2 * Broadcom BCM63XX High Speed SPI Controller driver
4 * Copyright 2000-2010 Broadcom Corporation
5 * Copyright 2012-2013 Jonas Gorski <jogo@openwrt.org>
7 * Licensed under the GNU/GPL. See COPYING for details.
10 #include <linux/kernel.h>
11 #include <linux/init.h>
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/spi/spi.h>
21 #include <linux/mutex.h>
23 #include <linux/reset.h>
24 #include <linux/pm_runtime.h>
26 #define HSSPI_GLOBAL_CTRL_REG 0x0
27 #define GLOBAL_CTRL_CS_POLARITY_SHIFT 0
28 #define GLOBAL_CTRL_CS_POLARITY_MASK 0x000000ff
29 #define GLOBAL_CTRL_PLL_CLK_CTRL_SHIFT 8
30 #define GLOBAL_CTRL_PLL_CLK_CTRL_MASK 0x0000ff00
31 #define GLOBAL_CTRL_CLK_GATE_SSOFF BIT(16)
32 #define GLOBAL_CTRL_CLK_POLARITY BIT(17)
33 #define GLOBAL_CTRL_MOSI_IDLE BIT(18)
35 #define HSSPI_GLOBAL_EXT_TRIGGER_REG 0x4
37 #define HSSPI_INT_STATUS_REG 0x8
38 #define HSSPI_INT_STATUS_MASKED_REG 0xc
39 #define HSSPI_INT_MASK_REG 0x10
41 #define HSSPI_PINGx_CMD_DONE(i) BIT((i * 8) + 0)
42 #define HSSPI_PINGx_RX_OVER(i) BIT((i * 8) + 1)
43 #define HSSPI_PINGx_TX_UNDER(i) BIT((i * 8) + 2)
44 #define HSSPI_PINGx_POLL_TIMEOUT(i) BIT((i * 8) + 3)
45 #define HSSPI_PINGx_CTRL_INVAL(i) BIT((i * 8) + 4)
47 #define HSSPI_INT_CLEAR_ALL 0xff001f1f
49 #define HSSPI_PINGPONG_COMMAND_REG(x) (0x80 + (x) * 0x40)
50 #define PINGPONG_CMD_COMMAND_MASK 0xf
51 #define PINGPONG_COMMAND_NOOP 0
52 #define PINGPONG_COMMAND_START_NOW 1
53 #define PINGPONG_COMMAND_START_TRIGGER 2
54 #define PINGPONG_COMMAND_HALT 3
55 #define PINGPONG_COMMAND_FLUSH 4
56 #define PINGPONG_CMD_PROFILE_SHIFT 8
57 #define PINGPONG_CMD_SS_SHIFT 12
59 #define HSSPI_PINGPONG_STATUS_REG(x) (0x84 + (x) * 0x40)
61 #define HSSPI_PROFILE_CLK_CTRL_REG(x) (0x100 + (x) * 0x20)
62 #define CLK_CTRL_FREQ_CTRL_MASK 0x0000ffff
63 #define CLK_CTRL_SPI_CLK_2X_SEL BIT(14)
64 #define CLK_CTRL_ACCUM_RST_ON_LOOP BIT(15)
66 #define HSSPI_PROFILE_SIGNAL_CTRL_REG(x) (0x104 + (x) * 0x20)
67 #define SIGNAL_CTRL_LATCH_RISING BIT(12)
68 #define SIGNAL_CTRL_LAUNCH_RISING BIT(13)
69 #define SIGNAL_CTRL_ASYNC_INPUT_PATH BIT(16)
71 #define HSSPI_PROFILE_MODE_CTRL_REG(x) (0x108 + (x) * 0x20)
72 #define MODE_CTRL_MULTIDATA_RD_STRT_SHIFT 8
73 #define MODE_CTRL_MULTIDATA_WR_STRT_SHIFT 12
74 #define MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT 16
75 #define MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT 18
76 #define MODE_CTRL_MODE_3WIRE BIT(20)
77 #define MODE_CTRL_PREPENDBYTE_CNT_SHIFT 24
79 #define HSSPI_FIFO_REG(x) (0x200 + (x) * 0x200)
82 #define HSSPI_OP_MULTIBIT BIT(11)
83 #define HSSPI_OP_CODE_SHIFT 13
84 #define HSSPI_OP_SLEEP (0 << HSSPI_OP_CODE_SHIFT)
85 #define HSSPI_OP_READ_WRITE (1 << HSSPI_OP_CODE_SHIFT)
86 #define HSSPI_OP_WRITE (2 << HSSPI_OP_CODE_SHIFT)
87 #define HSSPI_OP_READ (3 << HSSPI_OP_CODE_SHIFT)
88 #define HSSPI_OP_SETIRQ (4 << HSSPI_OP_CODE_SHIFT)
90 #define HSSPI_BUFFER_LEN 512
91 #define HSSPI_OPCODE_LEN 2
93 #define HSSPI_MAX_PREPEND_LEN 15
95 #define HSSPI_MAX_SYNC_CLOCK 30000000
97 #define HSSPI_SPI_MAX_CS 8
98 #define HSSPI_BUS_NUM 1 /* 0 is legacy SPI */
100 struct bcm63xx_hsspi {
101 struct completion done;
102 struct mutex bus_mutex;
104 struct platform_device *pdev;
114 static void bcm63xx_hsspi_set_cs(struct bcm63xx_hsspi *bs, unsigned int cs,
119 mutex_lock(&bs->bus_mutex);
120 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
123 if (active == !(bs->cs_polarity & BIT(cs)))
126 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
127 mutex_unlock(&bs->bus_mutex);
130 static void bcm63xx_hsspi_set_clk(struct bcm63xx_hsspi *bs,
131 struct spi_device *spi, int hz)
133 unsigned int profile = spi->chip_select;
136 reg = DIV_ROUND_UP(2048, DIV_ROUND_UP(bs->speed_hz, hz));
137 __raw_writel(CLK_CTRL_ACCUM_RST_ON_LOOP | reg,
138 bs->regs + HSSPI_PROFILE_CLK_CTRL_REG(profile));
140 reg = __raw_readl(bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
141 if (hz > HSSPI_MAX_SYNC_CLOCK)
142 reg |= SIGNAL_CTRL_ASYNC_INPUT_PATH;
144 reg &= ~SIGNAL_CTRL_ASYNC_INPUT_PATH;
145 __raw_writel(reg, bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
147 mutex_lock(&bs->bus_mutex);
148 /* setup clock polarity */
149 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
150 reg &= ~GLOBAL_CTRL_CLK_POLARITY;
151 if (spi->mode & SPI_CPOL)
152 reg |= GLOBAL_CTRL_CLK_POLARITY;
153 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
154 mutex_unlock(&bs->bus_mutex);
157 static int bcm63xx_hsspi_do_txrx(struct spi_device *spi, struct spi_transfer *t)
159 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
160 unsigned int chip_select = spi->chip_select;
162 int pending = t->len;
163 int step_size = HSSPI_BUFFER_LEN;
164 const u8 *tx = t->tx_buf;
167 bcm63xx_hsspi_set_clk(bs, spi, t->speed_hz);
168 bcm63xx_hsspi_set_cs(bs, spi->chip_select, true);
171 opcode = HSSPI_OP_READ_WRITE;
173 opcode = HSSPI_OP_WRITE;
175 opcode = HSSPI_OP_READ;
177 if (opcode != HSSPI_OP_READ)
178 step_size -= HSSPI_OPCODE_LEN;
180 if ((opcode == HSSPI_OP_READ && t->rx_nbits == SPI_NBITS_DUAL) ||
181 (opcode == HSSPI_OP_WRITE && t->tx_nbits == SPI_NBITS_DUAL))
182 opcode |= HSSPI_OP_MULTIBIT;
184 __raw_writel(1 << MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT |
185 1 << MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT | 0xff,
186 bs->regs + HSSPI_PROFILE_MODE_CTRL_REG(chip_select));
188 while (pending > 0) {
189 int curr_step = min_t(int, step_size, pending);
191 reinit_completion(&bs->done);
193 memcpy_toio(bs->fifo + HSSPI_OPCODE_LEN, tx, curr_step);
197 __raw_writew(opcode | curr_step, bs->fifo);
199 /* enable interrupt */
200 __raw_writel(HSSPI_PINGx_CMD_DONE(0),
201 bs->regs + HSSPI_INT_MASK_REG);
203 /* start the transfer */
204 __raw_writel(!chip_select << PINGPONG_CMD_SS_SHIFT |
205 chip_select << PINGPONG_CMD_PROFILE_SHIFT |
206 PINGPONG_COMMAND_START_NOW,
207 bs->regs + HSSPI_PINGPONG_COMMAND_REG(0));
209 if (wait_for_completion_timeout(&bs->done, HZ) == 0) {
210 dev_err(&bs->pdev->dev, "transfer timed out!\n");
215 memcpy_fromio(rx, bs->fifo, curr_step);
219 pending -= curr_step;
225 static int bcm63xx_hsspi_setup(struct spi_device *spi)
227 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
230 reg = __raw_readl(bs->regs +
231 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
232 reg &= ~(SIGNAL_CTRL_LAUNCH_RISING | SIGNAL_CTRL_LATCH_RISING);
233 if (spi->mode & SPI_CPHA)
234 reg |= SIGNAL_CTRL_LAUNCH_RISING;
236 reg |= SIGNAL_CTRL_LATCH_RISING;
237 __raw_writel(reg, bs->regs +
238 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
240 mutex_lock(&bs->bus_mutex);
241 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
243 /* only change actual polarities if there is no transfer */
244 if ((reg & GLOBAL_CTRL_CS_POLARITY_MASK) == bs->cs_polarity) {
245 if (spi->mode & SPI_CS_HIGH)
246 reg |= BIT(spi->chip_select);
248 reg &= ~BIT(spi->chip_select);
249 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
252 if (spi->mode & SPI_CS_HIGH)
253 bs->cs_polarity |= BIT(spi->chip_select);
255 bs->cs_polarity &= ~BIT(spi->chip_select);
257 mutex_unlock(&bs->bus_mutex);
262 static int bcm63xx_hsspi_transfer_one(struct spi_master *master,
263 struct spi_message *msg)
265 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
266 struct spi_transfer *t;
267 struct spi_device *spi = msg->spi;
268 int status = -EINVAL;
272 /* This controller does not support keeping CS active during idle.
273 * To work around this, we use the following ugly hack:
275 * a. Invert the target chip select's polarity so it will be active.
276 * b. Select a "dummy" chip select to use as the hardware target.
277 * c. Invert the dummy chip select's polarity so it will be inactive
278 * during the actual transfers.
279 * d. Tell the hardware to send to the dummy chip select. Thanks to
280 * the multiplexed nature of SPI the actual target will receive
281 * the transfer and we see its response.
283 * e. At the end restore the polarities again to their default values.
286 dummy_cs = !spi->chip_select;
287 bcm63xx_hsspi_set_cs(bs, dummy_cs, true);
289 list_for_each_entry(t, &msg->transfers, transfer_list) {
290 status = bcm63xx_hsspi_do_txrx(spi, t);
294 msg->actual_length += t->len;
296 spi_transfer_delay_exec(t);
299 bcm63xx_hsspi_set_cs(bs, spi->chip_select, false);
302 mutex_lock(&bs->bus_mutex);
303 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
304 reg &= ~GLOBAL_CTRL_CS_POLARITY_MASK;
305 reg |= bs->cs_polarity;
306 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
307 mutex_unlock(&bs->bus_mutex);
309 msg->status = status;
310 spi_finalize_current_message(master);
315 static irqreturn_t bcm63xx_hsspi_interrupt(int irq, void *dev_id)
317 struct bcm63xx_hsspi *bs = (struct bcm63xx_hsspi *)dev_id;
319 if (__raw_readl(bs->regs + HSSPI_INT_STATUS_MASKED_REG) == 0)
322 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
323 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
330 static int bcm63xx_hsspi_probe(struct platform_device *pdev)
332 struct spi_master *master;
333 struct bcm63xx_hsspi *bs;
335 struct device *dev = &pdev->dev;
336 struct clk *clk, *pll_clk = NULL;
338 u32 reg, rate, num_cs = HSSPI_SPI_MAX_CS;
339 struct reset_control *reset;
341 irq = platform_get_irq(pdev, 0);
345 regs = devm_platform_ioremap_resource(pdev, 0);
347 return PTR_ERR(regs);
349 clk = devm_clk_get(dev, "hsspi");
354 reset = devm_reset_control_get_optional_exclusive(dev, NULL);
356 return PTR_ERR(reset);
358 ret = clk_prepare_enable(clk);
362 ret = reset_control_reset(reset);
364 dev_err(dev, "unable to reset device: %d\n", ret);
365 goto out_disable_clk;
368 rate = clk_get_rate(clk);
370 pll_clk = devm_clk_get(dev, "pll");
372 if (IS_ERR(pll_clk)) {
373 ret = PTR_ERR(pll_clk);
374 goto out_disable_clk;
377 ret = clk_prepare_enable(pll_clk);
379 goto out_disable_clk;
381 rate = clk_get_rate(pll_clk);
384 goto out_disable_pll_clk;
388 master = spi_alloc_master(&pdev->dev, sizeof(*bs));
391 goto out_disable_pll_clk;
394 bs = spi_master_get_devdata(master);
397 bs->pll_clk = pll_clk;
400 bs->fifo = (u8 __iomem *)(bs->regs + HSSPI_FIFO_REG(0));
402 mutex_init(&bs->bus_mutex);
403 init_completion(&bs->done);
405 master->dev.of_node = dev->of_node;
407 master->bus_num = HSSPI_BUS_NUM;
409 of_property_read_u32(dev->of_node, "num-cs", &num_cs);
411 dev_warn(dev, "unsupported number of cs (%i), reducing to 8\n",
413 num_cs = HSSPI_SPI_MAX_CS;
415 master->num_chipselect = num_cs;
416 master->setup = bcm63xx_hsspi_setup;
417 master->transfer_one_message = bcm63xx_hsspi_transfer_one;
418 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
419 SPI_RX_DUAL | SPI_TX_DUAL;
420 master->bits_per_word_mask = SPI_BPW_MASK(8);
421 master->auto_runtime_pm = true;
423 platform_set_drvdata(pdev, master);
425 /* Initialize the hardware */
426 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
428 /* clean up any pending interrupts */
429 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
431 /* read out default CS polarities */
432 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
433 bs->cs_polarity = reg & GLOBAL_CTRL_CS_POLARITY_MASK;
434 __raw_writel(reg | GLOBAL_CTRL_CLK_GATE_SSOFF,
435 bs->regs + HSSPI_GLOBAL_CTRL_REG);
437 ret = devm_request_irq(dev, irq, bcm63xx_hsspi_interrupt, IRQF_SHARED,
443 pm_runtime_enable(&pdev->dev);
445 /* register and we are done */
446 ret = devm_spi_register_master(dev, master);
453 pm_runtime_disable(&pdev->dev);
455 spi_master_put(master);
457 clk_disable_unprepare(pll_clk);
459 clk_disable_unprepare(clk);
464 static int bcm63xx_hsspi_remove(struct platform_device *pdev)
466 struct spi_master *master = platform_get_drvdata(pdev);
467 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
469 /* reset the hardware and block queue progress */
470 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
471 clk_disable_unprepare(bs->pll_clk);
472 clk_disable_unprepare(bs->clk);
477 #ifdef CONFIG_PM_SLEEP
478 static int bcm63xx_hsspi_suspend(struct device *dev)
480 struct spi_master *master = dev_get_drvdata(dev);
481 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
483 spi_master_suspend(master);
484 clk_disable_unprepare(bs->pll_clk);
485 clk_disable_unprepare(bs->clk);
490 static int bcm63xx_hsspi_resume(struct device *dev)
492 struct spi_master *master = dev_get_drvdata(dev);
493 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
496 ret = clk_prepare_enable(bs->clk);
501 ret = clk_prepare_enable(bs->pll_clk);
503 clk_disable_unprepare(bs->clk);
508 spi_master_resume(master);
514 static SIMPLE_DEV_PM_OPS(bcm63xx_hsspi_pm_ops, bcm63xx_hsspi_suspend,
515 bcm63xx_hsspi_resume);
517 static const struct of_device_id bcm63xx_hsspi_of_match[] = {
518 { .compatible = "brcm,bcm6328-hsspi", },
521 MODULE_DEVICE_TABLE(of, bcm63xx_hsspi_of_match);
523 static struct platform_driver bcm63xx_hsspi_driver = {
525 .name = "bcm63xx-hsspi",
526 .pm = &bcm63xx_hsspi_pm_ops,
527 .of_match_table = bcm63xx_hsspi_of_match,
529 .probe = bcm63xx_hsspi_probe,
530 .remove = bcm63xx_hsspi_remove,
533 module_platform_driver(bcm63xx_hsspi_driver);
535 MODULE_ALIAS("platform:bcm63xx_hsspi");
536 MODULE_DESCRIPTION("Broadcom BCM63xx High Speed SPI Controller driver");
537 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
538 MODULE_LICENSE("GPL");