1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Broadcom BCM2835 auxiliary SPI Controllers
5 * the driver does not rely on the native chipselects at all
6 * but only uses the gpio type chipselects
8 * Based on: spi-bcm2835.c
10 * Copyright (C) 2015 Martin Sperl
13 #include <linux/clk.h>
14 #include <linux/completion.h>
15 #include <linux/debugfs.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/of_irq.h>
26 #include <linux/regmap.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spinlock.h>
30 /* define polling limits */
31 static unsigned int polling_limit_us = 30;
32 module_param(polling_limit_us, uint, 0664);
33 MODULE_PARM_DESC(polling_limit_us,
34 "time in us to run a transfer in polling mode - if zero no polling is used\n");
37 * spi register defines
39 * note there is garbage in the "official" documentation,
40 * so some data is taken from the file:
41 * brcm_usrlib/dag/vmcsx/vcinclude/bcm2708_chip/aux_io.h
43 * http://www.broadcom.com/docs/support/videocore/Brcm_Android_ICS_Graphics_Stack.tar.gz
46 /* SPI register offsets */
47 #define BCM2835_AUX_SPI_CNTL0 0x00
48 #define BCM2835_AUX_SPI_CNTL1 0x04
49 #define BCM2835_AUX_SPI_STAT 0x08
50 #define BCM2835_AUX_SPI_PEEK 0x0C
51 #define BCM2835_AUX_SPI_IO 0x20
52 #define BCM2835_AUX_SPI_TXHOLD 0x30
54 /* Bitfields in CNTL0 */
55 #define BCM2835_AUX_SPI_CNTL0_SPEED 0xFFF00000
56 #define BCM2835_AUX_SPI_CNTL0_SPEED_MAX 0xFFF
57 #define BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT 20
58 #define BCM2835_AUX_SPI_CNTL0_CS 0x000E0000
59 #define BCM2835_AUX_SPI_CNTL0_POSTINPUT 0x00010000
60 #define BCM2835_AUX_SPI_CNTL0_VAR_CS 0x00008000
61 #define BCM2835_AUX_SPI_CNTL0_VAR_WIDTH 0x00004000
62 #define BCM2835_AUX_SPI_CNTL0_DOUTHOLD 0x00003000
63 #define BCM2835_AUX_SPI_CNTL0_ENABLE 0x00000800
64 #define BCM2835_AUX_SPI_CNTL0_IN_RISING 0x00000400
65 #define BCM2835_AUX_SPI_CNTL0_CLEARFIFO 0x00000200
66 #define BCM2835_AUX_SPI_CNTL0_OUT_RISING 0x00000100
67 #define BCM2835_AUX_SPI_CNTL0_CPOL 0x00000080
68 #define BCM2835_AUX_SPI_CNTL0_MSBF_OUT 0x00000040
69 #define BCM2835_AUX_SPI_CNTL0_SHIFTLEN 0x0000003F
71 /* Bitfields in CNTL1 */
72 #define BCM2835_AUX_SPI_CNTL1_CSHIGH 0x00000700
73 #define BCM2835_AUX_SPI_CNTL1_TXEMPTY 0x00000080
74 #define BCM2835_AUX_SPI_CNTL1_IDLE 0x00000040
75 #define BCM2835_AUX_SPI_CNTL1_MSBF_IN 0x00000002
76 #define BCM2835_AUX_SPI_CNTL1_KEEP_IN 0x00000001
78 /* Bitfields in STAT */
79 #define BCM2835_AUX_SPI_STAT_TX_LVL 0xFF000000
80 #define BCM2835_AUX_SPI_STAT_RX_LVL 0x00FF0000
81 #define BCM2835_AUX_SPI_STAT_TX_FULL 0x00000400
82 #define BCM2835_AUX_SPI_STAT_TX_EMPTY 0x00000200
83 #define BCM2835_AUX_SPI_STAT_RX_FULL 0x00000100
84 #define BCM2835_AUX_SPI_STAT_RX_EMPTY 0x00000080
85 #define BCM2835_AUX_SPI_STAT_BUSY 0x00000040
86 #define BCM2835_AUX_SPI_STAT_BITCOUNT 0x0000003F
88 struct bcm2835aux_spi {
99 u64 count_transfer_polling;
100 u64 count_transfer_irq;
101 u64 count_transfer_irq_after_poll;
103 struct dentry *debugfs_dir;
106 #if defined(CONFIG_DEBUG_FS)
107 static void bcm2835aux_debugfs_create(struct bcm2835aux_spi *bs,
114 snprintf(name, sizeof(name), "spi-bcm2835aux-%s", dname);
116 /* the base directory */
117 dir = debugfs_create_dir(name, NULL);
118 bs->debugfs_dir = dir;
121 debugfs_create_u64("count_transfer_polling", 0444, dir,
122 &bs->count_transfer_polling);
123 debugfs_create_u64("count_transfer_irq", 0444, dir,
124 &bs->count_transfer_irq);
125 debugfs_create_u64("count_transfer_irq_after_poll", 0444, dir,
126 &bs->count_transfer_irq_after_poll);
129 static void bcm2835aux_debugfs_remove(struct bcm2835aux_spi *bs)
131 debugfs_remove_recursive(bs->debugfs_dir);
132 bs->debugfs_dir = NULL;
135 static void bcm2835aux_debugfs_create(struct bcm2835aux_spi *bs,
140 static void bcm2835aux_debugfs_remove(struct bcm2835aux_spi *bs)
143 #endif /* CONFIG_DEBUG_FS */
145 static inline u32 bcm2835aux_rd(struct bcm2835aux_spi *bs, unsigned int reg)
147 return readl(bs->regs + reg);
150 static inline void bcm2835aux_wr(struct bcm2835aux_spi *bs, unsigned int reg,
153 writel(val, bs->regs + reg);
156 static inline void bcm2835aux_rd_fifo(struct bcm2835aux_spi *bs)
159 int count = min(bs->rx_len, 3);
161 data = bcm2835aux_rd(bs, BCM2835_AUX_SPI_IO);
165 *bs->rx_buf++ = (data >> 16) & 0xff;
168 *bs->rx_buf++ = (data >> 8) & 0xff;
171 *bs->rx_buf++ = (data >> 0) & 0xff;
172 /* fallthrough - no default */
176 bs->pending -= count;
179 static inline void bcm2835aux_wr_fifo(struct bcm2835aux_spi *bs)
186 /* gather up to 3 bytes to write to the FIFO */
187 count = min(bs->tx_len, 3);
189 for (i = 0; i < count; i++) {
190 byte = bs->tx_buf ? *bs->tx_buf++ : 0;
191 data |= byte << (8 * (2 - i));
194 /* and set the variable bit-length */
195 data |= (count * 8) << 24;
197 /* and decrement length */
199 bs->pending += count;
201 /* write to the correct TX-register */
203 bcm2835aux_wr(bs, BCM2835_AUX_SPI_TXHOLD, data);
205 bcm2835aux_wr(bs, BCM2835_AUX_SPI_IO, data);
208 static void bcm2835aux_spi_reset_hw(struct bcm2835aux_spi *bs)
210 /* disable spi clearing fifo and interrupts */
211 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, 0);
212 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0,
213 BCM2835_AUX_SPI_CNTL0_CLEARFIFO);
216 static void bcm2835aux_spi_transfer_helper(struct bcm2835aux_spi *bs)
218 u32 stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT);
220 /* check if we have data to read */
221 for (; bs->rx_len && (stat & BCM2835_AUX_SPI_STAT_RX_LVL);
222 stat = bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT))
223 bcm2835aux_rd_fifo(bs);
225 /* check if we have data to write */
227 (bs->pending < 12) &&
228 (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
229 BCM2835_AUX_SPI_STAT_TX_FULL))) {
230 bcm2835aux_wr_fifo(bs);
234 static irqreturn_t bcm2835aux_spi_interrupt(int irq, void *dev_id)
236 struct spi_master *master = dev_id;
237 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
239 /* IRQ may be shared, so return if our interrupts are disabled */
240 if (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_CNTL1) &
241 (BCM2835_AUX_SPI_CNTL1_TXEMPTY | BCM2835_AUX_SPI_CNTL1_IDLE)))
244 /* do common fifo handling */
245 bcm2835aux_spi_transfer_helper(bs);
248 /* disable tx fifo empty interrupt */
249 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
250 BCM2835_AUX_SPI_CNTL1_IDLE);
253 /* and if rx_len is 0 then disable interrupts and wake up completion */
255 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
256 spi_finalize_current_transfer(master);
262 static int __bcm2835aux_spi_transfer_one_irq(struct spi_master *master,
263 struct spi_device *spi,
264 struct spi_transfer *tfr)
266 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
268 /* enable interrupts */
269 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1] |
270 BCM2835_AUX_SPI_CNTL1_TXEMPTY |
271 BCM2835_AUX_SPI_CNTL1_IDLE);
273 /* and wait for finish... */
277 static int bcm2835aux_spi_transfer_one_irq(struct spi_master *master,
278 struct spi_device *spi,
279 struct spi_transfer *tfr)
281 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
283 /* update statistics */
284 bs->count_transfer_irq++;
286 /* fill in registers and fifos before enabling interrupts */
287 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
288 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
290 /* fill in tx fifo with data before enabling interrupts */
291 while ((bs->tx_len) &&
292 (bs->pending < 12) &&
293 (!(bcm2835aux_rd(bs, BCM2835_AUX_SPI_STAT) &
294 BCM2835_AUX_SPI_STAT_TX_FULL))) {
295 bcm2835aux_wr_fifo(bs);
298 /* now run the interrupt mode */
299 return __bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
302 static int bcm2835aux_spi_transfer_one_poll(struct spi_master *master,
303 struct spi_device *spi,
304 struct spi_transfer *tfr)
306 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
307 unsigned long timeout;
309 /* update statistics */
310 bs->count_transfer_polling++;
313 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
314 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
316 /* set the timeout to at least 2 jiffies */
317 timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;
319 /* loop until finished the transfer */
322 /* do common fifo handling */
323 bcm2835aux_spi_transfer_helper(bs);
325 /* there is still data pending to read check the timeout */
326 if (bs->rx_len && time_after(jiffies, timeout)) {
327 dev_dbg_ratelimited(&spi->dev,
328 "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
330 bs->tx_len, bs->rx_len);
331 /* forward to interrupt handler */
332 bs->count_transfer_irq_after_poll++;
333 return __bcm2835aux_spi_transfer_one_irq(master,
338 /* and return without waiting for completion */
342 static int bcm2835aux_spi_transfer_one(struct spi_master *master,
343 struct spi_device *spi,
344 struct spi_transfer *tfr)
346 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
347 unsigned long spi_hz, clk_hz, speed;
348 unsigned long hz_per_byte, byte_limit;
350 /* calculate the registers to handle
352 * note that we use the variable data mode, which
353 * is not optimal for longer transfers as we waste registers
354 * resulting (potentially) in more interrupts when transferring
359 spi_hz = tfr->speed_hz;
360 clk_hz = clk_get_rate(bs->clk);
362 if (spi_hz >= clk_hz / 2) {
365 speed = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
366 if (speed > BCM2835_AUX_SPI_CNTL0_SPEED_MAX)
367 speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
368 } else { /* the slowest we can go */
369 speed = BCM2835_AUX_SPI_CNTL0_SPEED_MAX;
371 /* mask out old speed from previous spi_transfer */
372 bs->cntl[0] &= ~(BCM2835_AUX_SPI_CNTL0_SPEED);
373 /* set the new speed */
374 bs->cntl[0] |= speed << BCM2835_AUX_SPI_CNTL0_SPEED_SHIFT;
376 tfr->effective_speed_hz = clk_hz / (2 * (speed + 1));
378 /* set transmit buffers and length */
379 bs->tx_buf = tfr->tx_buf;
380 bs->rx_buf = tfr->rx_buf;
381 bs->tx_len = tfr->len;
382 bs->rx_len = tfr->len;
385 /* Calculate the estimated time in us the transfer runs. Note that
386 * there are 2 idle clocks cycles after each chunk getting
387 * transferred - in our case the chunk size is 3 bytes, so we
388 * approximate this by 9 cycles/byte. This is used to find the number
389 * of Hz per byte per polling limit. E.g., we can transfer 1 byte in
390 * 30 µs per 300,000 Hz of bus clock.
392 hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;
393 byte_limit = hz_per_byte ? tfr->effective_speed_hz / hz_per_byte : 1;
395 /* run in polling mode for short transfers */
396 if (tfr->len < byte_limit)
397 return bcm2835aux_spi_transfer_one_poll(master, spi, tfr);
399 /* run in interrupt mode for all others */
400 return bcm2835aux_spi_transfer_one_irq(master, spi, tfr);
403 static int bcm2835aux_spi_prepare_message(struct spi_master *master,
404 struct spi_message *msg)
406 struct spi_device *spi = msg->spi;
407 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
409 bs->cntl[0] = BCM2835_AUX_SPI_CNTL0_ENABLE |
410 BCM2835_AUX_SPI_CNTL0_VAR_WIDTH |
411 BCM2835_AUX_SPI_CNTL0_MSBF_OUT;
412 bs->cntl[1] = BCM2835_AUX_SPI_CNTL1_MSBF_IN;
414 /* handle all the modes */
415 if (spi->mode & SPI_CPOL) {
416 bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_CPOL;
417 bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_OUT_RISING;
419 bs->cntl[0] |= BCM2835_AUX_SPI_CNTL0_IN_RISING;
421 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL1, bs->cntl[1]);
422 bcm2835aux_wr(bs, BCM2835_AUX_SPI_CNTL0, bs->cntl[0]);
427 static int bcm2835aux_spi_unprepare_message(struct spi_master *master,
428 struct spi_message *msg)
430 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
432 bcm2835aux_spi_reset_hw(bs);
437 static void bcm2835aux_spi_handle_err(struct spi_master *master,
438 struct spi_message *msg)
440 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
442 bcm2835aux_spi_reset_hw(bs);
445 static int bcm2835aux_spi_setup(struct spi_device *spi)
447 /* sanity check for native cs */
448 if (spi->mode & SPI_NO_CS)
454 /* for dt-backwards compatibility: only support native on CS0
455 * known things not supported with broken native CS:
456 * * multiple chip-selects: cs0-cs2 are all
457 * simultaniously asserted whenever there is a transfer
458 * this even includes SPI_NO_CS
459 * * SPI_CS_HIGH: cs are always asserted low
460 * * cs_change: cs is deasserted after each spi_transfer
461 * * cs_delay_usec: cs is always deasserted one SCK cycle
462 * after the last transfer
466 "Native CS is not supported - please configure cs-gpio in device-tree\n");
468 if (spi->chip_select == 0)
471 dev_warn(&spi->dev, "Native CS is not working for cs > 0\n");
476 static int bcm2835aux_spi_probe(struct platform_device *pdev)
478 struct spi_master *master;
479 struct bcm2835aux_spi *bs;
480 unsigned long clk_hz;
483 master = devm_spi_alloc_master(&pdev->dev, sizeof(*bs));
487 platform_set_drvdata(pdev, master);
488 master->mode_bits = (SPI_CPOL | SPI_CS_HIGH | SPI_NO_CS);
489 master->bits_per_word_mask = SPI_BPW_MASK(8);
490 /* even though the driver never officially supported native CS
491 * allow a single native CS for legacy DT support purposes when
492 * no cs-gpio is configured.
493 * Known limitations for native cs are:
494 * * multiple chip-selects: cs0-cs2 are all simultaniously asserted
495 * whenever there is a transfer - this even includes SPI_NO_CS
496 * * SPI_CS_HIGH: is ignores - cs are always asserted low
497 * * cs_change: cs is deasserted after each spi_transfer
498 * * cs_delay_usec: cs is always deasserted one SCK cycle after
501 master->num_chipselect = 1;
502 master->setup = bcm2835aux_spi_setup;
503 master->transfer_one = bcm2835aux_spi_transfer_one;
504 master->handle_err = bcm2835aux_spi_handle_err;
505 master->prepare_message = bcm2835aux_spi_prepare_message;
506 master->unprepare_message = bcm2835aux_spi_unprepare_message;
507 master->dev.of_node = pdev->dev.of_node;
508 master->use_gpio_descriptors = true;
510 bs = spi_master_get_devdata(master);
513 bs->regs = devm_platform_ioremap_resource(pdev, 0);
514 if (IS_ERR(bs->regs))
515 return PTR_ERR(bs->regs);
517 bs->clk = devm_clk_get(&pdev->dev, NULL);
518 if (IS_ERR(bs->clk)) {
519 err = PTR_ERR(bs->clk);
520 dev_err(&pdev->dev, "could not get clk: %d\n", err);
524 bs->irq = platform_get_irq(pdev, 0);
526 return bs->irq ? bs->irq : -ENODEV;
528 /* this also enables the HW block */
529 err = clk_prepare_enable(bs->clk);
531 dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
535 /* just checking if the clock returns a sane value */
536 clk_hz = clk_get_rate(bs->clk);
538 dev_err(&pdev->dev, "clock returns 0 Hz\n");
540 goto out_clk_disable;
543 /* reset SPI-HW block */
544 bcm2835aux_spi_reset_hw(bs);
546 err = devm_request_irq(&pdev->dev, bs->irq,
547 bcm2835aux_spi_interrupt,
549 dev_name(&pdev->dev), master);
551 dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
552 goto out_clk_disable;
555 err = spi_register_master(master);
557 dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
558 goto out_clk_disable;
561 bcm2835aux_debugfs_create(bs, dev_name(&pdev->dev));
566 clk_disable_unprepare(bs->clk);
570 static int bcm2835aux_spi_remove(struct platform_device *pdev)
572 struct spi_master *master = platform_get_drvdata(pdev);
573 struct bcm2835aux_spi *bs = spi_master_get_devdata(master);
575 bcm2835aux_debugfs_remove(bs);
577 spi_unregister_master(master);
579 bcm2835aux_spi_reset_hw(bs);
581 /* disable the HW block by releasing the clock */
582 clk_disable_unprepare(bs->clk);
587 static const struct of_device_id bcm2835aux_spi_match[] = {
588 { .compatible = "brcm,bcm2835-aux-spi", },
591 MODULE_DEVICE_TABLE(of, bcm2835aux_spi_match);
593 static struct platform_driver bcm2835aux_spi_driver = {
595 .name = "spi-bcm2835aux",
596 .of_match_table = bcm2835aux_spi_match,
598 .probe = bcm2835aux_spi_probe,
599 .remove = bcm2835aux_spi_remove,
601 module_platform_driver(bcm2835aux_spi_driver);
603 MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835 aux");
604 MODULE_AUTHOR("Martin Sperl <kernel@martin.sperl.org>");
605 MODULE_LICENSE("GPL");