Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / spi / spi-bcm63xx-hsspi.c
blob1f08d7553f079c29c8995ce4a0517d313f85d50f
1 /*
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.
8 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/io.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>
22 #include <linux/of.h>
23 #include <linux/reset.h>
25 #define HSSPI_GLOBAL_CTRL_REG 0x0
26 #define GLOBAL_CTRL_CS_POLARITY_SHIFT 0
27 #define GLOBAL_CTRL_CS_POLARITY_MASK 0x000000ff
28 #define GLOBAL_CTRL_PLL_CLK_CTRL_SHIFT 8
29 #define GLOBAL_CTRL_PLL_CLK_CTRL_MASK 0x0000ff00
30 #define GLOBAL_CTRL_CLK_GATE_SSOFF BIT(16)
31 #define GLOBAL_CTRL_CLK_POLARITY BIT(17)
32 #define GLOBAL_CTRL_MOSI_IDLE BIT(18)
34 #define HSSPI_GLOBAL_EXT_TRIGGER_REG 0x4
36 #define HSSPI_INT_STATUS_REG 0x8
37 #define HSSPI_INT_STATUS_MASKED_REG 0xc
38 #define HSSPI_INT_MASK_REG 0x10
40 #define HSSPI_PINGx_CMD_DONE(i) BIT((i * 8) + 0)
41 #define HSSPI_PINGx_RX_OVER(i) BIT((i * 8) + 1)
42 #define HSSPI_PINGx_TX_UNDER(i) BIT((i * 8) + 2)
43 #define HSSPI_PINGx_POLL_TIMEOUT(i) BIT((i * 8) + 3)
44 #define HSSPI_PINGx_CTRL_INVAL(i) BIT((i * 8) + 4)
46 #define HSSPI_INT_CLEAR_ALL 0xff001f1f
48 #define HSSPI_PINGPONG_COMMAND_REG(x) (0x80 + (x) * 0x40)
49 #define PINGPONG_CMD_COMMAND_MASK 0xf
50 #define PINGPONG_COMMAND_NOOP 0
51 #define PINGPONG_COMMAND_START_NOW 1
52 #define PINGPONG_COMMAND_START_TRIGGER 2
53 #define PINGPONG_COMMAND_HALT 3
54 #define PINGPONG_COMMAND_FLUSH 4
55 #define PINGPONG_CMD_PROFILE_SHIFT 8
56 #define PINGPONG_CMD_SS_SHIFT 12
58 #define HSSPI_PINGPONG_STATUS_REG(x) (0x84 + (x) * 0x40)
60 #define HSSPI_PROFILE_CLK_CTRL_REG(x) (0x100 + (x) * 0x20)
61 #define CLK_CTRL_FREQ_CTRL_MASK 0x0000ffff
62 #define CLK_CTRL_SPI_CLK_2X_SEL BIT(14)
63 #define CLK_CTRL_ACCUM_RST_ON_LOOP BIT(15)
65 #define HSSPI_PROFILE_SIGNAL_CTRL_REG(x) (0x104 + (x) * 0x20)
66 #define SIGNAL_CTRL_LATCH_RISING BIT(12)
67 #define SIGNAL_CTRL_LAUNCH_RISING BIT(13)
68 #define SIGNAL_CTRL_ASYNC_INPUT_PATH BIT(16)
70 #define HSSPI_PROFILE_MODE_CTRL_REG(x) (0x108 + (x) * 0x20)
71 #define MODE_CTRL_MULTIDATA_RD_STRT_SHIFT 8
72 #define MODE_CTRL_MULTIDATA_WR_STRT_SHIFT 12
73 #define MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT 16
74 #define MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT 18
75 #define MODE_CTRL_MODE_3WIRE BIT(20)
76 #define MODE_CTRL_PREPENDBYTE_CNT_SHIFT 24
78 #define HSSPI_FIFO_REG(x) (0x200 + (x) * 0x200)
81 #define HSSPI_OP_MULTIBIT BIT(11)
82 #define HSSPI_OP_CODE_SHIFT 13
83 #define HSSPI_OP_SLEEP (0 << HSSPI_OP_CODE_SHIFT)
84 #define HSSPI_OP_READ_WRITE (1 << HSSPI_OP_CODE_SHIFT)
85 #define HSSPI_OP_WRITE (2 << HSSPI_OP_CODE_SHIFT)
86 #define HSSPI_OP_READ (3 << HSSPI_OP_CODE_SHIFT)
87 #define HSSPI_OP_SETIRQ (4 << HSSPI_OP_CODE_SHIFT)
89 #define HSSPI_BUFFER_LEN 512
90 #define HSSPI_OPCODE_LEN 2
92 #define HSSPI_MAX_PREPEND_LEN 15
94 #define HSSPI_MAX_SYNC_CLOCK 30000000
96 #define HSSPI_SPI_MAX_CS 8
97 #define HSSPI_BUS_NUM 1 /* 0 is legacy SPI */
99 struct bcm63xx_hsspi {
100 struct completion done;
101 struct mutex bus_mutex;
103 struct platform_device *pdev;
104 struct clk *clk;
105 struct clk *pll_clk;
106 void __iomem *regs;
107 u8 __iomem *fifo;
109 u32 speed_hz;
110 u8 cs_polarity;
113 static void bcm63xx_hsspi_set_cs(struct bcm63xx_hsspi *bs, unsigned int cs,
114 bool active)
116 u32 reg;
118 mutex_lock(&bs->bus_mutex);
119 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
121 reg &= ~BIT(cs);
122 if (active == !(bs->cs_polarity & BIT(cs)))
123 reg |= BIT(cs);
125 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
126 mutex_unlock(&bs->bus_mutex);
129 static void bcm63xx_hsspi_set_clk(struct bcm63xx_hsspi *bs,
130 struct spi_device *spi, int hz)
132 unsigned int profile = spi->chip_select;
133 u32 reg;
135 reg = DIV_ROUND_UP(2048, DIV_ROUND_UP(bs->speed_hz, hz));
136 __raw_writel(CLK_CTRL_ACCUM_RST_ON_LOOP | reg,
137 bs->regs + HSSPI_PROFILE_CLK_CTRL_REG(profile));
139 reg = __raw_readl(bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
140 if (hz > HSSPI_MAX_SYNC_CLOCK)
141 reg |= SIGNAL_CTRL_ASYNC_INPUT_PATH;
142 else
143 reg &= ~SIGNAL_CTRL_ASYNC_INPUT_PATH;
144 __raw_writel(reg, bs->regs + HSSPI_PROFILE_SIGNAL_CTRL_REG(profile));
146 mutex_lock(&bs->bus_mutex);
147 /* setup clock polarity */
148 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
149 reg &= ~GLOBAL_CTRL_CLK_POLARITY;
150 if (spi->mode & SPI_CPOL)
151 reg |= GLOBAL_CTRL_CLK_POLARITY;
152 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
153 mutex_unlock(&bs->bus_mutex);
156 static int bcm63xx_hsspi_do_txrx(struct spi_device *spi, struct spi_transfer *t)
158 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
159 unsigned int chip_select = spi->chip_select;
160 u16 opcode = 0;
161 int pending = t->len;
162 int step_size = HSSPI_BUFFER_LEN;
163 const u8 *tx = t->tx_buf;
164 u8 *rx = t->rx_buf;
166 bcm63xx_hsspi_set_clk(bs, spi, t->speed_hz);
167 bcm63xx_hsspi_set_cs(bs, spi->chip_select, true);
169 if (tx && rx)
170 opcode = HSSPI_OP_READ_WRITE;
171 else if (tx)
172 opcode = HSSPI_OP_WRITE;
173 else if (rx)
174 opcode = HSSPI_OP_READ;
176 if (opcode != HSSPI_OP_READ)
177 step_size -= HSSPI_OPCODE_LEN;
179 if ((opcode == HSSPI_OP_READ && t->rx_nbits == SPI_NBITS_DUAL) ||
180 (opcode == HSSPI_OP_WRITE && t->tx_nbits == SPI_NBITS_DUAL))
181 opcode |= HSSPI_OP_MULTIBIT;
183 __raw_writel(1 << MODE_CTRL_MULTIDATA_WR_SIZE_SHIFT |
184 1 << MODE_CTRL_MULTIDATA_RD_SIZE_SHIFT | 0xff,
185 bs->regs + HSSPI_PROFILE_MODE_CTRL_REG(chip_select));
187 while (pending > 0) {
188 int curr_step = min_t(int, step_size, pending);
190 reinit_completion(&bs->done);
191 if (tx) {
192 memcpy_toio(bs->fifo + HSSPI_OPCODE_LEN, tx, curr_step);
193 tx += curr_step;
196 __raw_writew(opcode | curr_step, bs->fifo);
198 /* enable interrupt */
199 __raw_writel(HSSPI_PINGx_CMD_DONE(0),
200 bs->regs + HSSPI_INT_MASK_REG);
202 /* start the transfer */
203 __raw_writel(!chip_select << PINGPONG_CMD_SS_SHIFT |
204 chip_select << PINGPONG_CMD_PROFILE_SHIFT |
205 PINGPONG_COMMAND_START_NOW,
206 bs->regs + HSSPI_PINGPONG_COMMAND_REG(0));
208 if (wait_for_completion_timeout(&bs->done, HZ) == 0) {
209 dev_err(&bs->pdev->dev, "transfer timed out!\n");
210 return -ETIMEDOUT;
213 if (rx) {
214 memcpy_fromio(rx, bs->fifo, curr_step);
215 rx += curr_step;
218 pending -= curr_step;
221 return 0;
224 static int bcm63xx_hsspi_setup(struct spi_device *spi)
226 struct bcm63xx_hsspi *bs = spi_master_get_devdata(spi->master);
227 u32 reg;
229 reg = __raw_readl(bs->regs +
230 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
231 reg &= ~(SIGNAL_CTRL_LAUNCH_RISING | SIGNAL_CTRL_LATCH_RISING);
232 if (spi->mode & SPI_CPHA)
233 reg |= SIGNAL_CTRL_LAUNCH_RISING;
234 else
235 reg |= SIGNAL_CTRL_LATCH_RISING;
236 __raw_writel(reg, bs->regs +
237 HSSPI_PROFILE_SIGNAL_CTRL_REG(spi->chip_select));
239 mutex_lock(&bs->bus_mutex);
240 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
242 /* only change actual polarities if there is no transfer */
243 if ((reg & GLOBAL_CTRL_CS_POLARITY_MASK) == bs->cs_polarity) {
244 if (spi->mode & SPI_CS_HIGH)
245 reg |= BIT(spi->chip_select);
246 else
247 reg &= ~BIT(spi->chip_select);
248 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
251 if (spi->mode & SPI_CS_HIGH)
252 bs->cs_polarity |= BIT(spi->chip_select);
253 else
254 bs->cs_polarity &= ~BIT(spi->chip_select);
256 mutex_unlock(&bs->bus_mutex);
258 return 0;
261 static int bcm63xx_hsspi_transfer_one(struct spi_master *master,
262 struct spi_message *msg)
264 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
265 struct spi_transfer *t;
266 struct spi_device *spi = msg->spi;
267 int status = -EINVAL;
268 int dummy_cs;
269 u32 reg;
271 /* This controller does not support keeping CS active during idle.
272 * To work around this, we use the following ugly hack:
274 * a. Invert the target chip select's polarity so it will be active.
275 * b. Select a "dummy" chip select to use as the hardware target.
276 * c. Invert the dummy chip select's polarity so it will be inactive
277 * during the actual transfers.
278 * d. Tell the hardware to send to the dummy chip select. Thanks to
279 * the multiplexed nature of SPI the actual target will receive
280 * the transfer and we see its response.
282 * e. At the end restore the polarities again to their default values.
285 dummy_cs = !spi->chip_select;
286 bcm63xx_hsspi_set_cs(bs, dummy_cs, true);
288 list_for_each_entry(t, &msg->transfers, transfer_list) {
289 status = bcm63xx_hsspi_do_txrx(spi, t);
290 if (status)
291 break;
293 msg->actual_length += t->len;
295 spi_transfer_delay_exec(t);
297 if (t->cs_change)
298 bcm63xx_hsspi_set_cs(bs, spi->chip_select, false);
301 mutex_lock(&bs->bus_mutex);
302 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
303 reg &= ~GLOBAL_CTRL_CS_POLARITY_MASK;
304 reg |= bs->cs_polarity;
305 __raw_writel(reg, bs->regs + HSSPI_GLOBAL_CTRL_REG);
306 mutex_unlock(&bs->bus_mutex);
308 msg->status = status;
309 spi_finalize_current_message(master);
311 return 0;
314 static irqreturn_t bcm63xx_hsspi_interrupt(int irq, void *dev_id)
316 struct bcm63xx_hsspi *bs = (struct bcm63xx_hsspi *)dev_id;
318 if (__raw_readl(bs->regs + HSSPI_INT_STATUS_MASKED_REG) == 0)
319 return IRQ_NONE;
321 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
322 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
324 complete(&bs->done);
326 return IRQ_HANDLED;
329 static int bcm63xx_hsspi_probe(struct platform_device *pdev)
331 struct spi_master *master;
332 struct bcm63xx_hsspi *bs;
333 void __iomem *regs;
334 struct device *dev = &pdev->dev;
335 struct clk *clk, *pll_clk = NULL;
336 int irq, ret;
337 u32 reg, rate, num_cs = HSSPI_SPI_MAX_CS;
338 struct reset_control *reset;
340 irq = platform_get_irq(pdev, 0);
341 if (irq < 0)
342 return irq;
344 regs = devm_platform_ioremap_resource(pdev, 0);
345 if (IS_ERR(regs))
346 return PTR_ERR(regs);
348 clk = devm_clk_get(dev, "hsspi");
350 if (IS_ERR(clk))
351 return PTR_ERR(clk);
353 reset = devm_reset_control_get_optional_exclusive(dev, NULL);
354 if (IS_ERR(reset))
355 return PTR_ERR(reset);
357 ret = clk_prepare_enable(clk);
358 if (ret)
359 return ret;
361 ret = reset_control_reset(reset);
362 if (ret) {
363 dev_err(dev, "unable to reset device: %d\n", ret);
364 goto out_disable_clk;
367 rate = clk_get_rate(clk);
368 if (!rate) {
369 pll_clk = devm_clk_get(dev, "pll");
371 if (IS_ERR(pll_clk)) {
372 ret = PTR_ERR(pll_clk);
373 goto out_disable_clk;
376 ret = clk_prepare_enable(pll_clk);
377 if (ret)
378 goto out_disable_clk;
380 rate = clk_get_rate(pll_clk);
381 if (!rate) {
382 ret = -EINVAL;
383 goto out_disable_pll_clk;
387 master = spi_alloc_master(&pdev->dev, sizeof(*bs));
388 if (!master) {
389 ret = -ENOMEM;
390 goto out_disable_pll_clk;
393 bs = spi_master_get_devdata(master);
394 bs->pdev = pdev;
395 bs->clk = clk;
396 bs->pll_clk = pll_clk;
397 bs->regs = regs;
398 bs->speed_hz = rate;
399 bs->fifo = (u8 __iomem *)(bs->regs + HSSPI_FIFO_REG(0));
401 mutex_init(&bs->bus_mutex);
402 init_completion(&bs->done);
404 master->dev.of_node = dev->of_node;
405 if (!dev->of_node)
406 master->bus_num = HSSPI_BUS_NUM;
408 of_property_read_u32(dev->of_node, "num-cs", &num_cs);
409 if (num_cs > 8) {
410 dev_warn(dev, "unsupported number of cs (%i), reducing to 8\n",
411 num_cs);
412 num_cs = HSSPI_SPI_MAX_CS;
414 master->num_chipselect = num_cs;
415 master->setup = bcm63xx_hsspi_setup;
416 master->transfer_one_message = bcm63xx_hsspi_transfer_one;
417 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
418 SPI_RX_DUAL | SPI_TX_DUAL;
419 master->bits_per_word_mask = SPI_BPW_MASK(8);
420 master->auto_runtime_pm = true;
422 platform_set_drvdata(pdev, master);
424 /* Initialize the hardware */
425 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
427 /* clean up any pending interrupts */
428 __raw_writel(HSSPI_INT_CLEAR_ALL, bs->regs + HSSPI_INT_STATUS_REG);
430 /* read out default CS polarities */
431 reg = __raw_readl(bs->regs + HSSPI_GLOBAL_CTRL_REG);
432 bs->cs_polarity = reg & GLOBAL_CTRL_CS_POLARITY_MASK;
433 __raw_writel(reg | GLOBAL_CTRL_CLK_GATE_SSOFF,
434 bs->regs + HSSPI_GLOBAL_CTRL_REG);
436 ret = devm_request_irq(dev, irq, bcm63xx_hsspi_interrupt, IRQF_SHARED,
437 pdev->name, bs);
439 if (ret)
440 goto out_put_master;
442 /* register and we are done */
443 ret = devm_spi_register_master(dev, master);
444 if (ret)
445 goto out_put_master;
447 return 0;
449 out_put_master:
450 spi_master_put(master);
451 out_disable_pll_clk:
452 clk_disable_unprepare(pll_clk);
453 out_disable_clk:
454 clk_disable_unprepare(clk);
455 return ret;
459 static int bcm63xx_hsspi_remove(struct platform_device *pdev)
461 struct spi_master *master = platform_get_drvdata(pdev);
462 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
464 /* reset the hardware and block queue progress */
465 __raw_writel(0, bs->regs + HSSPI_INT_MASK_REG);
466 clk_disable_unprepare(bs->pll_clk);
467 clk_disable_unprepare(bs->clk);
469 return 0;
472 #ifdef CONFIG_PM_SLEEP
473 static int bcm63xx_hsspi_suspend(struct device *dev)
475 struct spi_master *master = dev_get_drvdata(dev);
476 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
478 spi_master_suspend(master);
479 clk_disable_unprepare(bs->pll_clk);
480 clk_disable_unprepare(bs->clk);
482 return 0;
485 static int bcm63xx_hsspi_resume(struct device *dev)
487 struct spi_master *master = dev_get_drvdata(dev);
488 struct bcm63xx_hsspi *bs = spi_master_get_devdata(master);
489 int ret;
491 ret = clk_prepare_enable(bs->clk);
492 if (ret)
493 return ret;
495 if (bs->pll_clk) {
496 ret = clk_prepare_enable(bs->pll_clk);
497 if (ret) {
498 clk_disable_unprepare(bs->clk);
499 return ret;
503 spi_master_resume(master);
505 return 0;
507 #endif
509 static SIMPLE_DEV_PM_OPS(bcm63xx_hsspi_pm_ops, bcm63xx_hsspi_suspend,
510 bcm63xx_hsspi_resume);
512 static const struct of_device_id bcm63xx_hsspi_of_match[] = {
513 { .compatible = "brcm,bcm6328-hsspi", },
514 { },
516 MODULE_DEVICE_TABLE(of, bcm63xx_hsspi_of_match);
518 static struct platform_driver bcm63xx_hsspi_driver = {
519 .driver = {
520 .name = "bcm63xx-hsspi",
521 .pm = &bcm63xx_hsspi_pm_ops,
522 .of_match_table = bcm63xx_hsspi_of_match,
524 .probe = bcm63xx_hsspi_probe,
525 .remove = bcm63xx_hsspi_remove,
528 module_platform_driver(bcm63xx_hsspi_driver);
530 MODULE_ALIAS("platform:bcm63xx_hsspi");
531 MODULE_DESCRIPTION("Broadcom BCM63xx High Speed SPI Controller driver");
532 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
533 MODULE_LICENSE("GPL");