diff options
| author | Yash Shah <yash.shah@sifive.com> | 2019-02-19 06:40:07 -0500 |
|---|---|---|
| committer | Mark Brown <broonie@kernel.org> | 2019-02-19 10:28:53 -0500 |
| commit | 484a9a68d669f899657a97dbb369cb3e3be7e7f5 (patch) | |
| tree | 0b88bce0cee2cac0c0d28144e82b2a6b574edc35 /drivers/spi | |
| parent | 3b155e873a38c3b28e419da759cfe86c74b1c870 (diff) | |
spi: sifive: Add driver for the SiFive SPI controller
Add driver for the SiFive SPI controller
on the HiFive Unleashed board.
Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
Signed-off-by: Yash Shah <yash.shah@sifive.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'drivers/spi')
| -rw-r--r-- | drivers/spi/Kconfig | 6 | ||||
| -rw-r--r-- | drivers/spi/Makefile | 1 | ||||
| -rw-r--r-- | drivers/spi/spi-sifive.c | 450 |
3 files changed, 457 insertions, 0 deletions
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 17d79b91d7a8..f761655e2a36 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig | |||
| @@ -651,6 +651,12 @@ config SPI_SH_HSPI | |||
| 651 | help | 651 | help |
| 652 | SPI driver for SuperH HSPI blocks. | 652 | SPI driver for SuperH HSPI blocks. |
| 653 | 653 | ||
| 654 | config SPI_SIFIVE | ||
| 655 | tristate "SiFive SPI controller" | ||
| 656 | depends on HAS_IOMEM | ||
| 657 | help | ||
| 658 | This exposes the SPI controller IP from SiFive. | ||
| 659 | |||
| 654 | config SPI_SIRF | 660 | config SPI_SIRF |
| 655 | tristate "CSR SiRFprimaII SPI controller" | 661 | tristate "CSR SiRFprimaII SPI controller" |
| 656 | depends on SIRF_DMA | 662 | depends on SIRF_DMA |
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 5c5af4676279..d8fc03c9faa2 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile | |||
| @@ -95,6 +95,7 @@ obj-$(CONFIG_SPI_SH) += spi-sh.o | |||
| 95 | obj-$(CONFIG_SPI_SH_HSPI) += spi-sh-hspi.o | 95 | obj-$(CONFIG_SPI_SH_HSPI) += spi-sh-hspi.o |
| 96 | obj-$(CONFIG_SPI_SH_MSIOF) += spi-sh-msiof.o | 96 | obj-$(CONFIG_SPI_SH_MSIOF) += spi-sh-msiof.o |
| 97 | obj-$(CONFIG_SPI_SH_SCI) += spi-sh-sci.o | 97 | obj-$(CONFIG_SPI_SH_SCI) += spi-sh-sci.o |
| 98 | obj-$(CONFIG_SPI_SIFIVE) += spi-sifive.o | ||
| 98 | obj-$(CONFIG_SPI_SIRF) += spi-sirf.o | 99 | obj-$(CONFIG_SPI_SIRF) += spi-sirf.o |
| 99 | obj-$(CONFIG_SPI_SLAVE_MT27XX) += spi-slave-mt27xx.o | 100 | obj-$(CONFIG_SPI_SLAVE_MT27XX) += spi-slave-mt27xx.o |
| 100 | obj-$(CONFIG_SPI_SPRD) += spi-sprd.o | 101 | obj-$(CONFIG_SPI_SPRD) += spi-sprd.o |
diff --git a/drivers/spi/spi-sifive.c b/drivers/spi/spi-sifive.c new file mode 100644 index 000000000000..961307c4a613 --- /dev/null +++ b/drivers/spi/spi-sifive.c | |||
| @@ -0,0 +1,450 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0 | ||
| 2 | // | ||
| 3 | // Copyright 2018 SiFive, Inc. | ||
| 4 | // | ||
| 5 | // SiFive SPI controller driver (master mode only) | ||
| 6 | // | ||
| 7 | // Author: SiFive, Inc. | ||
| 8 | // sifive@sifive.com | ||
| 9 | |||
| 10 | #include <linux/clk.h> | ||
| 11 | #include <linux/module.h> | ||
| 12 | #include <linux/interrupt.h> | ||
| 13 | #include <linux/of.h> | ||
| 14 | #include <linux/platform_device.h> | ||
| 15 | #include <linux/spi/spi.h> | ||
| 16 | #include <linux/io.h> | ||
| 17 | #include <linux/log2.h> | ||
| 18 | |||
| 19 | #define SIFIVE_SPI_DRIVER_NAME "sifive_spi" | ||
| 20 | |||
| 21 | #define SIFIVE_SPI_MAX_CS 32 | ||
| 22 | #define SIFIVE_SPI_DEFAULT_DEPTH 8 | ||
| 23 | #define SIFIVE_SPI_DEFAULT_MAX_BITS 8 | ||
| 24 | |||
| 25 | /* register offsets */ | ||
| 26 | #define SIFIVE_SPI_REG_SCKDIV 0x00 /* Serial clock divisor */ | ||
| 27 | #define SIFIVE_SPI_REG_SCKMODE 0x04 /* Serial clock mode */ | ||
| 28 | #define SIFIVE_SPI_REG_CSID 0x10 /* Chip select ID */ | ||
| 29 | #define SIFIVE_SPI_REG_CSDEF 0x14 /* Chip select default */ | ||
| 30 | #define SIFIVE_SPI_REG_CSMODE 0x18 /* Chip select mode */ | ||
| 31 | #define SIFIVE_SPI_REG_DELAY0 0x28 /* Delay control 0 */ | ||
| 32 | #define SIFIVE_SPI_REG_DELAY1 0x2c /* Delay control 1 */ | ||
| 33 | #define SIFIVE_SPI_REG_FMT 0x40 /* Frame format */ | ||
| 34 | #define SIFIVE_SPI_REG_TXDATA 0x48 /* Tx FIFO data */ | ||
| 35 | #define SIFIVE_SPI_REG_RXDATA 0x4c /* Rx FIFO data */ | ||
| 36 | #define SIFIVE_SPI_REG_TXMARK 0x50 /* Tx FIFO watermark */ | ||
| 37 | #define SIFIVE_SPI_REG_RXMARK 0x54 /* Rx FIFO watermark */ | ||
| 38 | #define SIFIVE_SPI_REG_FCTRL 0x60 /* SPI flash interface control */ | ||
| 39 | #define SIFIVE_SPI_REG_FFMT 0x64 /* SPI flash instruction format */ | ||
| 40 | #define SIFIVE_SPI_REG_IE 0x70 /* Interrupt Enable Register */ | ||
| 41 | #define SIFIVE_SPI_REG_IP 0x74 /* Interrupt Pendings Register */ | ||
| 42 | |||
| 43 | /* sckdiv bits */ | ||
| 44 | #define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU | ||
| 45 | |||
| 46 | /* sckmode bits */ | ||
| 47 | #define SIFIVE_SPI_SCKMODE_PHA BIT(0) | ||
| 48 | #define SIFIVE_SPI_SCKMODE_POL BIT(1) | ||
| 49 | #define SIFIVE_SPI_SCKMODE_MODE_MASK (SIFIVE_SPI_SCKMODE_PHA | \ | ||
| 50 | SIFIVE_SPI_SCKMODE_POL) | ||
| 51 | |||
| 52 | /* csmode bits */ | ||
| 53 | #define SIFIVE_SPI_CSMODE_MODE_AUTO 0U | ||
| 54 | #define SIFIVE_SPI_CSMODE_MODE_HOLD 2U | ||
| 55 | #define SIFIVE_SPI_CSMODE_MODE_OFF 3U | ||
| 56 | |||
| 57 | /* delay0 bits */ | ||
| 58 | #define SIFIVE_SPI_DELAY0_CSSCK(x) ((u32)(x)) | ||
| 59 | #define SIFIVE_SPI_DELAY0_CSSCK_MASK 0xffU | ||
| 60 | #define SIFIVE_SPI_DELAY0_SCKCS(x) ((u32)(x) << 16) | ||
| 61 | #define SIFIVE_SPI_DELAY0_SCKCS_MASK (0xffU << 16) | ||
| 62 | |||
| 63 | /* delay1 bits */ | ||
| 64 | #define SIFIVE_SPI_DELAY1_INTERCS(x) ((u32)(x)) | ||
| 65 | #define SIFIVE_SPI_DELAY1_INTERCS_MASK 0xffU | ||
| 66 | #define SIFIVE_SPI_DELAY1_INTERXFR(x) ((u32)(x) << 16) | ||
| 67 | #define SIFIVE_SPI_DELAY1_INTERXFR_MASK (0xffU << 16) | ||
| 68 | |||
| 69 | /* fmt bits */ | ||
| 70 | #define SIFIVE_SPI_FMT_PROTO_SINGLE 0U | ||
| 71 | #define SIFIVE_SPI_FMT_PROTO_DUAL 1U | ||
| 72 | #define SIFIVE_SPI_FMT_PROTO_QUAD 2U | ||
| 73 | #define SIFIVE_SPI_FMT_PROTO_MASK 3U | ||
| 74 | #define SIFIVE_SPI_FMT_ENDIAN BIT(2) | ||
| 75 | #define SIFIVE_SPI_FMT_DIR BIT(3) | ||
| 76 | #define SIFIVE_SPI_FMT_LEN(x) ((u32)(x) << 16) | ||
| 77 | #define SIFIVE_SPI_FMT_LEN_MASK (0xfU << 16) | ||
| 78 | |||
| 79 | /* txdata bits */ | ||
| 80 | #define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU | ||
| 81 | #define SIFIVE_SPI_TXDATA_FULL BIT(31) | ||
| 82 | |||
| 83 | /* rxdata bits */ | ||
| 84 | #define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU | ||
| 85 | #define SIFIVE_SPI_RXDATA_EMPTY BIT(31) | ||
| 86 | |||
| 87 | /* ie and ip bits */ | ||
| 88 | #define SIFIVE_SPI_IP_TXWM BIT(0) | ||
| 89 | #define SIFIVE_SPI_IP_RXWM BIT(1) | ||
| 90 | |||
| 91 | struct sifive_spi { | ||
| 92 | void __iomem *regs; /* virt. address of control registers */ | ||
| 93 | struct clk *clk; /* bus clock */ | ||
| 94 | unsigned int fifo_depth; /* fifo depth in words */ | ||
| 95 | u32 cs_inactive; /* level of the CS pins when inactive */ | ||
| 96 | struct completion done; /* wake-up from interrupt */ | ||
| 97 | }; | ||
| 98 | |||
| 99 | static void sifive_spi_write(struct sifive_spi *spi, int offset, u32 value) | ||
| 100 | { | ||
| 101 | iowrite32(value, spi->regs + offset); | ||
| 102 | } | ||
| 103 | |||
| 104 | static u32 sifive_spi_read(struct sifive_spi *spi, int offset) | ||
| 105 | { | ||
| 106 | return ioread32(spi->regs + offset); | ||
| 107 | } | ||
| 108 | |||
| 109 | static void sifive_spi_init(struct sifive_spi *spi) | ||
| 110 | { | ||
| 111 | /* Watermark interrupts are disabled by default */ | ||
| 112 | sifive_spi_write(spi, SIFIVE_SPI_REG_IE, 0); | ||
| 113 | |||
| 114 | /* Default watermark FIFO threshold values */ | ||
| 115 | sifive_spi_write(spi, SIFIVE_SPI_REG_TXMARK, 1); | ||
| 116 | sifive_spi_write(spi, SIFIVE_SPI_REG_RXMARK, 0); | ||
| 117 | |||
| 118 | /* Set CS/SCK Delays and Inactive Time to defaults */ | ||
| 119 | sifive_spi_write(spi, SIFIVE_SPI_REG_DELAY0, | ||
| 120 | SIFIVE_SPI_DELAY0_CSSCK(1) | | ||
| 121 | SIFIVE_SPI_DELAY0_SCKCS(1)); | ||
| 122 | sifive_spi_write(spi, SIFIVE_SPI_REG_DELAY1, | ||
| 123 | SIFIVE_SPI_DELAY1_INTERCS(1) | | ||
| 124 | SIFIVE_SPI_DELAY1_INTERXFR(0)); | ||
| 125 | |||
| 126 | /* Exit specialized memory-mapped SPI flash mode */ | ||
| 127 | sifive_spi_write(spi, SIFIVE_SPI_REG_FCTRL, 0); | ||
| 128 | } | ||
| 129 | |||
| 130 | static int | ||
| 131 | sifive_spi_prepare_message(struct spi_master *master, struct spi_message *msg) | ||
| 132 | { | ||
| 133 | struct sifive_spi *spi = spi_master_get_devdata(master); | ||
| 134 | struct spi_device *device = msg->spi; | ||
| 135 | |||
| 136 | /* Update the chip select polarity */ | ||
| 137 | if (device->mode & SPI_CS_HIGH) | ||
| 138 | spi->cs_inactive &= ~BIT(device->chip_select); | ||
| 139 | else | ||
| 140 | spi->cs_inactive |= BIT(device->chip_select); | ||
| 141 | sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, spi->cs_inactive); | ||
| 142 | |||
| 143 | /* Select the correct device */ | ||
| 144 | sifive_spi_write(spi, SIFIVE_SPI_REG_CSID, device->chip_select); | ||
| 145 | |||
| 146 | /* Set clock mode */ | ||
| 147 | sifive_spi_write(spi, SIFIVE_SPI_REG_SCKMODE, | ||
| 148 | device->mode & SIFIVE_SPI_SCKMODE_MODE_MASK); | ||
| 149 | |||
| 150 | return 0; | ||
| 151 | } | ||
| 152 | |||
| 153 | static void sifive_spi_set_cs(struct spi_device *device, bool is_high) | ||
| 154 | { | ||
| 155 | struct sifive_spi *spi = spi_master_get_devdata(device->master); | ||
| 156 | |||
| 157 | /* Reverse polarity is handled by SCMR/CPOL. Not inverted CS. */ | ||
| 158 | if (device->mode & SPI_CS_HIGH) | ||
| 159 | is_high = !is_high; | ||
| 160 | |||
| 161 | sifive_spi_write(spi, SIFIVE_SPI_REG_CSMODE, is_high ? | ||
| 162 | SIFIVE_SPI_CSMODE_MODE_AUTO : | ||
| 163 | SIFIVE_SPI_CSMODE_MODE_HOLD); | ||
| 164 | } | ||
| 165 | |||
| 166 | static int | ||
| 167 | sifive_spi_prep_transfer(struct sifive_spi *spi, struct spi_device *device, | ||
| 168 | struct spi_transfer *t) | ||
| 169 | { | ||
| 170 | u32 cr; | ||
| 171 | unsigned int mode; | ||
| 172 | |||
| 173 | /* Calculate and program the clock rate */ | ||
| 174 | cr = DIV_ROUND_UP(clk_get_rate(spi->clk) >> 1, t->speed_hz) - 1; | ||
| 175 | cr &= SIFIVE_SPI_SCKDIV_DIV_MASK; | ||
| 176 | sifive_spi_write(spi, SIFIVE_SPI_REG_SCKDIV, cr); | ||
| 177 | |||
| 178 | mode = max_t(unsigned int, t->rx_nbits, t->tx_nbits); | ||
| 179 | |||
| 180 | /* Set frame format */ | ||
| 181 | cr = SIFIVE_SPI_FMT_LEN(t->bits_per_word); | ||
| 182 | switch (mode) { | ||
| 183 | case SPI_NBITS_QUAD: | ||
| 184 | cr |= SIFIVE_SPI_FMT_PROTO_QUAD; | ||
| 185 | break; | ||
| 186 | case SPI_NBITS_DUAL: | ||
| 187 | cr |= SIFIVE_SPI_FMT_PROTO_DUAL; | ||
| 188 | break; | ||
| 189 | default: | ||
| 190 | cr |= SIFIVE_SPI_FMT_PROTO_SINGLE; | ||
| 191 | break; | ||
| 192 | } | ||
| 193 | if (device->mode & SPI_LSB_FIRST) | ||
| 194 | cr |= SIFIVE_SPI_FMT_ENDIAN; | ||
| 195 | if (!t->rx_buf) | ||
| 196 | cr |= SIFIVE_SPI_FMT_DIR; | ||
| 197 | sifive_spi_write(spi, SIFIVE_SPI_REG_FMT, cr); | ||
| 198 | |||
| 199 | /* We will want to poll if the time we need to wait is | ||
| 200 | * less than the context switching time. | ||
| 201 | * Let's call that threshold 5us. The operation will take: | ||
| 202 | * (8/mode) * fifo_depth / hz <= 5 * 10^-6 | ||
| 203 | * 1600000 * fifo_depth <= hz * mode | ||
| 204 | */ | ||
| 205 | return 1600000 * spi->fifo_depth <= t->speed_hz * mode; | ||
| 206 | } | ||
| 207 | |||
| 208 | static irqreturn_t sifive_spi_irq(int irq, void *dev_id) | ||
| 209 | { | ||
| 210 | struct sifive_spi *spi = dev_id; | ||
| 211 | u32 ip = sifive_spi_read(spi, SIFIVE_SPI_REG_IP); | ||
| 212 | |||
| 213 | if (ip & (SIFIVE_SPI_IP_TXWM | SIFIVE_SPI_IP_RXWM)) { | ||
| 214 | /* Disable interrupts until next transfer */ | ||
| 215 | sifive_spi_write(spi, SIFIVE_SPI_REG_IE, 0); | ||
| 216 | complete(&spi->done); | ||
| 217 | return IRQ_HANDLED; | ||
| 218 | } | ||
| 219 | |||
| 220 | return IRQ_NONE; | ||
| 221 | } | ||
| 222 | |||
| 223 | static void sifive_spi_wait(struct sifive_spi *spi, u32 bit, int poll) | ||
| 224 | { | ||
| 225 | if (poll) { | ||
| 226 | u32 cr; | ||
| 227 | |||
| 228 | do { | ||
| 229 | cr = sifive_spi_read(spi, SIFIVE_SPI_REG_IP); | ||
| 230 | } while (!(cr & bit)); | ||
| 231 | } else { | ||
| 232 | reinit_completion(&spi->done); | ||
| 233 | sifive_spi_write(spi, SIFIVE_SPI_REG_IE, bit); | ||
| 234 | wait_for_completion(&spi->done); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | static void sifive_spi_tx(struct sifive_spi *spi, const u8 *tx_ptr) | ||
| 239 | { | ||
| 240 | WARN_ON_ONCE((sifive_spi_read(spi, SIFIVE_SPI_REG_TXDATA) | ||
| 241 | & SIFIVE_SPI_TXDATA_FULL) != 0); | ||
| 242 | sifive_spi_write(spi, SIFIVE_SPI_REG_TXDATA, | ||
| 243 | *tx_ptr & SIFIVE_SPI_TXDATA_DATA_MASK); | ||
| 244 | } | ||
| 245 | |||
| 246 | static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr) | ||
| 247 | { | ||
| 248 | u32 data = sifive_spi_read(spi, SIFIVE_SPI_REG_RXDATA); | ||
| 249 | |||
| 250 | WARN_ON_ONCE((data & SIFIVE_SPI_RXDATA_EMPTY) != 0); | ||
| 251 | *rx_ptr = data & SIFIVE_SPI_RXDATA_DATA_MASK; | ||
| 252 | } | ||
| 253 | |||
| 254 | static int | ||
| 255 | sifive_spi_transfer_one(struct spi_master *master, struct spi_device *device, | ||
| 256 | struct spi_transfer *t) | ||
| 257 | { | ||
| 258 | struct sifive_spi *spi = spi_master_get_devdata(master); | ||
| 259 | int poll = sifive_spi_prep_transfer(spi, device, t); | ||
| 260 | const u8 *tx_ptr = t->tx_buf; | ||
| 261 | u8 *rx_ptr = t->rx_buf; | ||
| 262 | unsigned int remaining_words = t->len; | ||
| 263 | |||
| 264 | while (remaining_words) { | ||
| 265 | unsigned int n_words = min(remaining_words, spi->fifo_depth); | ||
| 266 | unsigned int i; | ||
| 267 | |||
| 268 | /* Enqueue n_words for transmission */ | ||
| 269 | for (i = 0; i < n_words; i++) | ||
| 270 | sifive_spi_tx(spi, tx_ptr++); | ||
| 271 | |||
| 272 | if (rx_ptr) { | ||
| 273 | /* Wait for transmission + reception to complete */ | ||
| 274 | sifive_spi_write(spi, SIFIVE_SPI_REG_RXMARK, | ||
| 275 | n_words - 1); | ||
| 276 | sifive_spi_wait(spi, SIFIVE_SPI_IP_RXWM, poll); | ||
| 277 | |||
| 278 | /* Read out all the data from the RX FIFO */ | ||
| 279 | for (i = 0; i < n_words; i++) | ||
| 280 | sifive_spi_rx(spi, rx_ptr++); | ||
| 281 | } else { | ||
| 282 | /* Wait for transmission to complete */ | ||
| 283 | sifive_spi_wait(spi, SIFIVE_SPI_IP_TXWM, poll); | ||
| 284 | } | ||
| 285 | |||
| 286 | remaining_words -= n_words; | ||
| 287 | } | ||
| 288 | |||
| 289 | return 0; | ||
| 290 | } | ||
| 291 | |||
| 292 | static int sifive_spi_probe(struct platform_device *pdev) | ||
| 293 | { | ||
| 294 | struct sifive_spi *spi; | ||
| 295 | struct resource *res; | ||
| 296 | int ret, irq, num_cs; | ||
| 297 | u32 cs_bits, max_bits_per_word; | ||
| 298 | struct spi_master *master; | ||
| 299 | |||
| 300 | master = spi_alloc_master(&pdev->dev, sizeof(struct sifive_spi)); | ||
| 301 | if (!master) { | ||
| 302 | dev_err(&pdev->dev, "out of memory\n"); | ||
| 303 | return -ENOMEM; | ||
| 304 | } | ||
| 305 | |||
| 306 | spi = spi_master_get_devdata(master); | ||
| 307 | init_completion(&spi->done); | ||
| 308 | platform_set_drvdata(pdev, master); | ||
| 309 | |||
| 310 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
| 311 | spi->regs = devm_ioremap_resource(&pdev->dev, res); | ||
| 312 | if (IS_ERR(spi->regs)) { | ||
| 313 | dev_err(&pdev->dev, "Unable to map IO resources\n"); | ||
| 314 | ret = PTR_ERR(spi->regs); | ||
| 315 | goto put_master; | ||
| 316 | } | ||
| 317 | |||
| 318 | spi->clk = devm_clk_get(&pdev->dev, NULL); | ||
| 319 | if (IS_ERR(spi->clk)) { | ||
| 320 | dev_err(&pdev->dev, "Unable to find bus clock\n"); | ||
| 321 | ret = PTR_ERR(spi->clk); | ||
| 322 | goto put_master; | ||
| 323 | } | ||
| 324 | |||
| 325 | irq = platform_get_irq(pdev, 0); | ||
| 326 | if (irq < 0) { | ||
| 327 | dev_err(&pdev->dev, "Unable to find interrupt\n"); | ||
| 328 | ret = irq; | ||
| 329 | goto put_master; | ||
| 330 | } | ||
| 331 | |||
| 332 | /* Optional parameters */ | ||
| 333 | ret = | ||
| 334 | of_property_read_u32(pdev->dev.of_node, "sifive,fifo-depth", | ||
| 335 | &spi->fifo_depth); | ||
| 336 | if (ret < 0) | ||
| 337 | spi->fifo_depth = SIFIVE_SPI_DEFAULT_DEPTH; | ||
| 338 | |||
| 339 | ret = | ||
| 340 | of_property_read_u32(pdev->dev.of_node, "sifive,max-bits-per-word", | ||
| 341 | &max_bits_per_word); | ||
| 342 | |||
| 343 | if (!ret && max_bits_per_word < 8) { | ||
| 344 | dev_err(&pdev->dev, "Only 8bit SPI words supported by the driver\n"); | ||
| 345 | ret = -EINVAL; | ||
| 346 | goto put_master; | ||
| 347 | } | ||
| 348 | |||
| 349 | /* Spin up the bus clock before hitting registers */ | ||
| 350 | ret = clk_prepare_enable(spi->clk); | ||
| 351 | if (ret) { | ||
| 352 | dev_err(&pdev->dev, "Unable to enable bus clock\n"); | ||
| 353 | goto put_master; | ||
| 354 | } | ||
| 355 | |||
| 356 | /* probe the number of CS lines */ | ||
| 357 | spi->cs_inactive = sifive_spi_read(spi, SIFIVE_SPI_REG_CSDEF); | ||
| 358 | sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, 0xffffffffU); | ||
| 359 | cs_bits = sifive_spi_read(spi, SIFIVE_SPI_REG_CSDEF); | ||
| 360 | sifive_spi_write(spi, SIFIVE_SPI_REG_CSDEF, spi->cs_inactive); | ||
| 361 | if (!cs_bits) { | ||
| 362 | dev_err(&pdev->dev, "Could not auto probe CS lines\n"); | ||
| 363 | ret = -EINVAL; | ||
| 364 | goto put_master; | ||
| 365 | } | ||
| 366 | |||
| 367 | num_cs = ilog2(cs_bits) + 1; | ||
| 368 | if (num_cs > SIFIVE_SPI_MAX_CS) { | ||
| 369 | dev_err(&pdev->dev, "Invalid number of spi slaves\n"); | ||
| 370 | ret = -EINVAL; | ||
| 371 | goto put_master; | ||
| 372 | } | ||
| 373 | |||
| 374 | /* Define our master */ | ||
| 375 | master->dev.of_node = pdev->dev.of_node; | ||
| 376 | master->bus_num = pdev->id; | ||
| 377 | master->num_chipselect = num_cs; | ||
| 378 | master->mode_bits = SPI_CPHA | SPI_CPOL | ||
| 379 | | SPI_CS_HIGH | SPI_LSB_FIRST | ||
| 380 | | SPI_TX_DUAL | SPI_TX_QUAD | ||
| 381 | | SPI_RX_DUAL | SPI_RX_QUAD; | ||
| 382 | /* TODO: add driver support for bits_per_word < 8 | ||
| 383 | * we need to "left-align" the bits (unless SPI_LSB_FIRST) | ||
| 384 | */ | ||
| 385 | master->bits_per_word_mask = SPI_BPW_MASK(8); | ||
| 386 | master->flags = SPI_CONTROLLER_MUST_TX | SPI_MASTER_GPIO_SS; | ||
| 387 | master->prepare_message = sifive_spi_prepare_message; | ||
| 388 | master->set_cs = sifive_spi_set_cs; | ||
| 389 | master->transfer_one = sifive_spi_transfer_one; | ||
| 390 | |||
| 391 | pdev->dev.dma_mask = NULL; | ||
| 392 | /* Configure the SPI master hardware */ | ||
| 393 | sifive_spi_init(spi); | ||
| 394 | |||
| 395 | /* Register for SPI Interrupt */ | ||
| 396 | ret = devm_request_irq(&pdev->dev, irq, sifive_spi_irq, 0, | ||
| 397 | dev_name(&pdev->dev), spi); | ||
| 398 | if (ret) { | ||
| 399 | dev_err(&pdev->dev, "Unable to bind to interrupt\n"); | ||
| 400 | goto put_master; | ||
| 401 | } | ||
| 402 | |||
| 403 | dev_info(&pdev->dev, "mapped; irq=%d, cs=%d\n", | ||
| 404 | irq, master->num_chipselect); | ||
| 405 | |||
| 406 | ret = devm_spi_register_master(&pdev->dev, master); | ||
| 407 | if (ret < 0) { | ||
| 408 | dev_err(&pdev->dev, "spi_register_master failed\n"); | ||
| 409 | goto put_master; | ||
| 410 | } | ||
| 411 | |||
| 412 | return 0; | ||
| 413 | |||
| 414 | put_master: | ||
| 415 | spi_master_put(master); | ||
| 416 | |||
| 417 | return ret; | ||
| 418 | } | ||
| 419 | |||
| 420 | static int sifive_spi_remove(struct platform_device *pdev) | ||
| 421 | { | ||
| 422 | struct spi_master *master = platform_get_drvdata(pdev); | ||
| 423 | struct sifive_spi *spi = spi_master_get_devdata(master); | ||
| 424 | |||
| 425 | /* Disable all the interrupts just in case */ | ||
| 426 | sifive_spi_write(spi, SIFIVE_SPI_REG_IE, 0); | ||
| 427 | spi_master_put(master); | ||
| 428 | |||
| 429 | return 0; | ||
| 430 | } | ||
| 431 | |||
| 432 | static const struct of_device_id sifive_spi_of_match[] = { | ||
| 433 | { .compatible = "sifive,spi0", }, | ||
| 434 | {} | ||
| 435 | }; | ||
| 436 | MODULE_DEVICE_TABLE(of, sifive_spi_of_match); | ||
| 437 | |||
| 438 | static struct platform_driver sifive_spi_driver = { | ||
| 439 | .probe = sifive_spi_probe, | ||
| 440 | .remove = sifive_spi_remove, | ||
| 441 | .driver = { | ||
| 442 | .name = SIFIVE_SPI_DRIVER_NAME, | ||
| 443 | .of_match_table = sifive_spi_of_match, | ||
| 444 | }, | ||
| 445 | }; | ||
| 446 | module_platform_driver(sifive_spi_driver); | ||
| 447 | |||
| 448 | MODULE_AUTHOR("SiFive, Inc. <sifive@sifive.com>"); | ||
| 449 | MODULE_DESCRIPTION("SiFive SPI driver"); | ||
| 450 | MODULE_LICENSE("GPL"); | ||
