Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Broadcom BCM2835 SPI Controllers |
| 3 | * |
| 4 | * Copyright (C) 2012 Chris Boot |
| 5 | * Copyright (C) 2013 Stephen Warren |
| 6 | * |
| 7 | * This driver is inspired by: |
| 8 | * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org> |
| 9 | * spi-atmel.c, Copyright (C) 2006 Atmel Corporation |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/completion.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/of.h> |
| 31 | #include <linux/of_irq.h> |
| 32 | #include <linux/of_device.h> |
| 33 | #include <linux/spi/spi.h> |
| 34 | |
| 35 | /* SPI register offsets */ |
| 36 | #define BCM2835_SPI_CS 0x00 |
| 37 | #define BCM2835_SPI_FIFO 0x04 |
| 38 | #define BCM2835_SPI_CLK 0x08 |
| 39 | #define BCM2835_SPI_DLEN 0x0c |
| 40 | #define BCM2835_SPI_LTOH 0x10 |
| 41 | #define BCM2835_SPI_DC 0x14 |
| 42 | |
| 43 | /* Bitfields in CS */ |
| 44 | #define BCM2835_SPI_CS_LEN_LONG 0x02000000 |
| 45 | #define BCM2835_SPI_CS_DMA_LEN 0x01000000 |
| 46 | #define BCM2835_SPI_CS_CSPOL2 0x00800000 |
| 47 | #define BCM2835_SPI_CS_CSPOL1 0x00400000 |
| 48 | #define BCM2835_SPI_CS_CSPOL0 0x00200000 |
| 49 | #define BCM2835_SPI_CS_RXF 0x00100000 |
| 50 | #define BCM2835_SPI_CS_RXR 0x00080000 |
| 51 | #define BCM2835_SPI_CS_TXD 0x00040000 |
| 52 | #define BCM2835_SPI_CS_RXD 0x00020000 |
| 53 | #define BCM2835_SPI_CS_DONE 0x00010000 |
| 54 | #define BCM2835_SPI_CS_LEN 0x00002000 |
| 55 | #define BCM2835_SPI_CS_REN 0x00001000 |
| 56 | #define BCM2835_SPI_CS_ADCS 0x00000800 |
| 57 | #define BCM2835_SPI_CS_INTR 0x00000400 |
| 58 | #define BCM2835_SPI_CS_INTD 0x00000200 |
| 59 | #define BCM2835_SPI_CS_DMAEN 0x00000100 |
| 60 | #define BCM2835_SPI_CS_TA 0x00000080 |
| 61 | #define BCM2835_SPI_CS_CSPOL 0x00000040 |
| 62 | #define BCM2835_SPI_CS_CLEAR_RX 0x00000020 |
| 63 | #define BCM2835_SPI_CS_CLEAR_TX 0x00000010 |
| 64 | #define BCM2835_SPI_CS_CPOL 0x00000008 |
| 65 | #define BCM2835_SPI_CS_CPHA 0x00000004 |
| 66 | #define BCM2835_SPI_CS_CS_10 0x00000002 |
| 67 | #define BCM2835_SPI_CS_CS_01 0x00000001 |
| 68 | |
| 69 | #define BCM2835_SPI_TIMEOUT_MS 30000 |
| 70 | #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS) |
| 71 | |
| 72 | #define DRV_NAME "spi-bcm2835" |
| 73 | |
| 74 | struct bcm2835_spi { |
| 75 | void __iomem *regs; |
| 76 | struct clk *clk; |
| 77 | int irq; |
| 78 | struct completion done; |
| 79 | const u8 *tx_buf; |
| 80 | u8 *rx_buf; |
| 81 | int len; |
| 82 | }; |
| 83 | |
| 84 | static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg) |
| 85 | { |
| 86 | return readl(bs->regs + reg); |
| 87 | } |
| 88 | |
| 89 | static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val) |
| 90 | { |
| 91 | writel(val, bs->regs + reg); |
| 92 | } |
| 93 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 94 | static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 95 | { |
| 96 | u8 byte; |
| 97 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 98 | while (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 99 | byte = bcm2835_rd(bs, BCM2835_SPI_FIFO); |
| 100 | if (bs->rx_buf) |
| 101 | *bs->rx_buf++ = byte; |
| 102 | } |
| 103 | } |
| 104 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 105 | static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 106 | { |
| 107 | u8 byte; |
| 108 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 109 | while ((bs->len) && |
| 110 | (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 111 | byte = bs->tx_buf ? *bs->tx_buf++ : 0; |
| 112 | bcm2835_wr(bs, BCM2835_SPI_FIFO, byte); |
| 113 | bs->len--; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id) |
| 118 | { |
| 119 | struct spi_master *master = dev_id; |
| 120 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 121 | u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS); |
| 122 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 123 | /* Read as many bytes as possible from FIFO */ |
| 124 | bcm2835_rd_fifo(bs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 125 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 126 | if (bs->len) { /* there is more data to transmit */ |
| 127 | bcm2835_wr_fifo(bs); |
| 128 | } else { /* Transfer complete */ |
| 129 | /* Disable SPI interrupts */ |
| 130 | cs &= ~(BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD); |
| 131 | bcm2835_wr(bs, BCM2835_SPI_CS, cs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 132 | |
| 133 | /* |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 134 | * Wake up bcm2835_spi_transfer_one(), which will call |
| 135 | * bcm2835_spi_finish_transfer(), to drain the RX FIFO. |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 136 | */ |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 137 | complete(&bs->done); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 138 | } |
| 139 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame^] | 140 | return IRQ_HANDLED; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 141 | } |
| 142 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 143 | static int bcm2835_spi_start_transfer(struct spi_device *spi, |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 144 | struct spi_transfer *tfr) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 145 | { |
| 146 | struct bcm2835_spi *bs = spi_master_get_devdata(spi->master); |
| 147 | unsigned long spi_hz, clk_hz, cdiv; |
| 148 | u32 cs = BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA; |
| 149 | |
| 150 | spi_hz = tfr->speed_hz; |
| 151 | clk_hz = clk_get_rate(bs->clk); |
| 152 | |
| 153 | if (spi_hz >= clk_hz / 2) { |
| 154 | cdiv = 2; /* clk_hz/2 is the fastest we can go */ |
| 155 | } else if (spi_hz) { |
| 156 | /* CDIV must be a power of two */ |
| 157 | cdiv = roundup_pow_of_two(DIV_ROUND_UP(clk_hz, spi_hz)); |
| 158 | |
| 159 | if (cdiv >= 65536) |
| 160 | cdiv = 0; /* 0 is the slowest we can go */ |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 161 | } else { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 162 | cdiv = 0; /* 0 is the slowest we can go */ |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 163 | } |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 164 | |
| 165 | if (spi->mode & SPI_CPOL) |
| 166 | cs |= BCM2835_SPI_CS_CPOL; |
| 167 | if (spi->mode & SPI_CPHA) |
| 168 | cs |= BCM2835_SPI_CS_CPHA; |
| 169 | |
| 170 | if (!(spi->mode & SPI_NO_CS)) { |
| 171 | if (spi->mode & SPI_CS_HIGH) { |
| 172 | cs |= BCM2835_SPI_CS_CSPOL; |
| 173 | cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select; |
| 174 | } |
| 175 | |
| 176 | cs |= spi->chip_select; |
| 177 | } |
| 178 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 179 | reinit_completion(&bs->done); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 180 | bs->tx_buf = tfr->tx_buf; |
| 181 | bs->rx_buf = tfr->rx_buf; |
| 182 | bs->len = tfr->len; |
| 183 | |
| 184 | bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv); |
| 185 | /* |
| 186 | * Enable the HW block. This will immediately trigger a DONE (TX |
| 187 | * empty) interrupt, upon which we will fill the TX FIFO with the |
| 188 | * first TX bytes. Pre-filling the TX FIFO here to avoid the |
| 189 | * interrupt doesn't work:-( |
| 190 | */ |
| 191 | bcm2835_wr(bs, BCM2835_SPI_CS, cs); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int bcm2835_spi_finish_transfer(struct spi_device *spi, |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 197 | struct spi_transfer *tfr, |
| 198 | bool cs_change) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 199 | { |
| 200 | struct bcm2835_spi *bs = spi_master_get_devdata(spi->master); |
| 201 | u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS); |
| 202 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 203 | if (tfr->delay_usecs) |
| 204 | udelay(tfr->delay_usecs); |
| 205 | |
| 206 | if (cs_change) |
| 207 | /* Clear TA flag */ |
| 208 | bcm2835_wr(bs, BCM2835_SPI_CS, cs & ~BCM2835_SPI_CS_TA); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 213 | static int bcm2835_spi_transfer_one(struct spi_master *master, |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 214 | struct spi_message *mesg) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 215 | { |
| 216 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 217 | struct spi_transfer *tfr; |
| 218 | struct spi_device *spi = mesg->spi; |
| 219 | int err = 0; |
| 220 | unsigned int timeout; |
| 221 | bool cs_change; |
| 222 | |
| 223 | list_for_each_entry(tfr, &mesg->transfers, transfer_list) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 224 | err = bcm2835_spi_start_transfer(spi, tfr); |
| 225 | if (err) |
| 226 | goto out; |
| 227 | |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 228 | timeout = wait_for_completion_timeout( |
| 229 | &bs->done, |
| 230 | msecs_to_jiffies(BCM2835_SPI_TIMEOUT_MS) |
| 231 | ); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 232 | if (!timeout) { |
| 233 | err = -ETIMEDOUT; |
| 234 | goto out; |
| 235 | } |
| 236 | |
| 237 | cs_change = tfr->cs_change || |
| 238 | list_is_last(&tfr->transfer_list, &mesg->transfers); |
| 239 | |
| 240 | err = bcm2835_spi_finish_transfer(spi, tfr, cs_change); |
| 241 | if (err) |
| 242 | goto out; |
| 243 | |
| 244 | mesg->actual_length += (tfr->len - bs->len); |
| 245 | } |
| 246 | |
| 247 | out: |
| 248 | /* Clear FIFOs, and disable the HW block */ |
| 249 | bcm2835_wr(bs, BCM2835_SPI_CS, |
| 250 | BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX); |
| 251 | mesg->status = err; |
| 252 | spi_finalize_current_message(master); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int bcm2835_spi_probe(struct platform_device *pdev) |
| 258 | { |
| 259 | struct spi_master *master; |
| 260 | struct bcm2835_spi *bs; |
| 261 | struct resource *res; |
| 262 | int err; |
| 263 | |
| 264 | master = spi_alloc_master(&pdev->dev, sizeof(*bs)); |
| 265 | if (!master) { |
| 266 | dev_err(&pdev->dev, "spi_alloc_master() failed\n"); |
| 267 | return -ENOMEM; |
| 268 | } |
| 269 | |
| 270 | platform_set_drvdata(pdev, master); |
| 271 | |
| 272 | master->mode_bits = BCM2835_SPI_MODE_BITS; |
Axel Lin | c2b6a3a | 2013-08-05 08:43:02 +0800 | [diff] [blame] | 273 | master->bits_per_word_mask = SPI_BPW_MASK(8); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 274 | master->num_chipselect = 3; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 275 | master->transfer_one_message = bcm2835_spi_transfer_one; |
| 276 | master->dev.of_node = pdev->dev.of_node; |
| 277 | |
| 278 | bs = spi_master_get_devdata(master); |
| 279 | |
| 280 | init_completion(&bs->done); |
| 281 | |
| 282 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Laurent Navet | 2d6e75e | 2013-05-02 14:13:30 +0200 | [diff] [blame] | 283 | bs->regs = devm_ioremap_resource(&pdev->dev, res); |
| 284 | if (IS_ERR(bs->regs)) { |
| 285 | err = PTR_ERR(bs->regs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 286 | goto out_master_put; |
| 287 | } |
| 288 | |
| 289 | bs->clk = devm_clk_get(&pdev->dev, NULL); |
| 290 | if (IS_ERR(bs->clk)) { |
| 291 | err = PTR_ERR(bs->clk); |
| 292 | dev_err(&pdev->dev, "could not get clk: %d\n", err); |
| 293 | goto out_master_put; |
| 294 | } |
| 295 | |
| 296 | bs->irq = irq_of_parse_and_map(pdev->dev.of_node, 0); |
| 297 | if (bs->irq <= 0) { |
| 298 | dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq); |
| 299 | err = bs->irq ? bs->irq : -ENODEV; |
| 300 | goto out_master_put; |
| 301 | } |
| 302 | |
| 303 | clk_prepare_enable(bs->clk); |
| 304 | |
Jingoo Han | 08bc054 | 2013-12-09 19:25:00 +0900 | [diff] [blame] | 305 | err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0, |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 306 | dev_name(&pdev->dev), master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 307 | if (err) { |
| 308 | dev_err(&pdev->dev, "could not request IRQ: %d\n", err); |
| 309 | goto out_clk_disable; |
| 310 | } |
| 311 | |
| 312 | /* initialise the hardware */ |
| 313 | bcm2835_wr(bs, BCM2835_SPI_CS, |
| 314 | BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX); |
| 315 | |
Jingoo Han | 247263d | 2013-09-24 13:23:00 +0900 | [diff] [blame] | 316 | err = devm_spi_register_master(&pdev->dev, master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 317 | if (err) { |
| 318 | dev_err(&pdev->dev, "could not register SPI master: %d\n", err); |
Jingoo Han | 08bc054 | 2013-12-09 19:25:00 +0900 | [diff] [blame] | 319 | goto out_clk_disable; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 324 | out_clk_disable: |
| 325 | clk_disable_unprepare(bs->clk); |
| 326 | out_master_put: |
| 327 | spi_master_put(master); |
| 328 | return err; |
| 329 | } |
| 330 | |
| 331 | static int bcm2835_spi_remove(struct platform_device *pdev) |
| 332 | { |
Wei Yongjun | e0b35b8 | 2013-11-15 15:43:27 +0800 | [diff] [blame] | 333 | struct spi_master *master = platform_get_drvdata(pdev); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 334 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 335 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 336 | /* Clear FIFOs, and disable the HW block */ |
| 337 | bcm2835_wr(bs, BCM2835_SPI_CS, |
| 338 | BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX); |
| 339 | |
| 340 | clk_disable_unprepare(bs->clk); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static const struct of_device_id bcm2835_spi_match[] = { |
| 346 | { .compatible = "brcm,bcm2835-spi", }, |
| 347 | {} |
| 348 | }; |
| 349 | MODULE_DEVICE_TABLE(of, bcm2835_spi_match); |
| 350 | |
| 351 | static struct platform_driver bcm2835_spi_driver = { |
| 352 | .driver = { |
| 353 | .name = DRV_NAME, |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 354 | .of_match_table = bcm2835_spi_match, |
| 355 | }, |
| 356 | .probe = bcm2835_spi_probe, |
| 357 | .remove = bcm2835_spi_remove, |
| 358 | }; |
| 359 | module_platform_driver(bcm2835_spi_driver); |
| 360 | |
| 361 | MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835"); |
| 362 | MODULE_AUTHOR("Chris Boot <bootc@bootc.net>"); |
| 363 | MODULE_LICENSE("GPL v2"); |