Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Broadcom BCM63xx SPI controller support |
| 4 | * |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 5 | * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org> |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 6 | * Copyright (C) 2010 Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com> |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/spi/spi.h> |
| 17 | #include <linux/completion.h> |
| 18 | #include <linux/err.h> |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 20 | #include <linux/of.h> |
Álvaro Fernández Rojas | 38807ad | 2020-06-16 19:32:34 +0200 | [diff] [blame] | 21 | #include <linux/reset.h> |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 22 | |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 23 | /* BCM 6338/6348 SPI core */ |
| 24 | #define SPI_6348_RSET_SIZE 64 |
| 25 | #define SPI_6348_CMD 0x00 /* 16-bits register */ |
| 26 | #define SPI_6348_INT_STATUS 0x02 |
| 27 | #define SPI_6348_INT_MASK_ST 0x03 |
| 28 | #define SPI_6348_INT_MASK 0x04 |
| 29 | #define SPI_6348_ST 0x05 |
| 30 | #define SPI_6348_CLK_CFG 0x06 |
| 31 | #define SPI_6348_FILL_BYTE 0x07 |
| 32 | #define SPI_6348_MSG_TAIL 0x09 |
| 33 | #define SPI_6348_RX_TAIL 0x0b |
| 34 | #define SPI_6348_MSG_CTL 0x40 /* 8-bits register */ |
| 35 | #define SPI_6348_MSG_CTL_WIDTH 8 |
| 36 | #define SPI_6348_MSG_DATA 0x41 |
| 37 | #define SPI_6348_MSG_DATA_SIZE 0x3f |
| 38 | #define SPI_6348_RX_DATA 0x80 |
| 39 | #define SPI_6348_RX_DATA_SIZE 0x3f |
| 40 | |
| 41 | /* BCM 3368/6358/6262/6368 SPI core */ |
| 42 | #define SPI_6358_RSET_SIZE 1804 |
| 43 | #define SPI_6358_MSG_CTL 0x00 /* 16-bits register */ |
| 44 | #define SPI_6358_MSG_CTL_WIDTH 16 |
| 45 | #define SPI_6358_MSG_DATA 0x02 |
| 46 | #define SPI_6358_MSG_DATA_SIZE 0x21e |
| 47 | #define SPI_6358_RX_DATA 0x400 |
| 48 | #define SPI_6358_RX_DATA_SIZE 0x220 |
| 49 | #define SPI_6358_CMD 0x700 /* 16-bits register */ |
| 50 | #define SPI_6358_INT_STATUS 0x702 |
| 51 | #define SPI_6358_INT_MASK_ST 0x703 |
| 52 | #define SPI_6358_INT_MASK 0x704 |
| 53 | #define SPI_6358_ST 0x705 |
| 54 | #define SPI_6358_CLK_CFG 0x706 |
| 55 | #define SPI_6358_FILL_BYTE 0x707 |
| 56 | #define SPI_6358_MSG_TAIL 0x709 |
| 57 | #define SPI_6358_RX_TAIL 0x70B |
| 58 | |
| 59 | /* Shared SPI definitions */ |
| 60 | |
| 61 | /* Message configuration */ |
| 62 | #define SPI_FD_RW 0x00 |
| 63 | #define SPI_HD_W 0x01 |
| 64 | #define SPI_HD_R 0x02 |
| 65 | #define SPI_BYTE_CNT_SHIFT 0 |
| 66 | #define SPI_6348_MSG_TYPE_SHIFT 6 |
| 67 | #define SPI_6358_MSG_TYPE_SHIFT 14 |
| 68 | |
| 69 | /* Command */ |
| 70 | #define SPI_CMD_NOOP 0x00 |
| 71 | #define SPI_CMD_SOFT_RESET 0x01 |
| 72 | #define SPI_CMD_HARD_RESET 0x02 |
| 73 | #define SPI_CMD_START_IMMEDIATE 0x03 |
| 74 | #define SPI_CMD_COMMAND_SHIFT 0 |
| 75 | #define SPI_CMD_COMMAND_MASK 0x000f |
| 76 | #define SPI_CMD_DEVICE_ID_SHIFT 4 |
| 77 | #define SPI_CMD_PREPEND_BYTE_CNT_SHIFT 8 |
| 78 | #define SPI_CMD_ONE_BYTE_SHIFT 11 |
| 79 | #define SPI_CMD_ONE_WIRE_SHIFT 12 |
| 80 | #define SPI_DEV_ID_0 0 |
| 81 | #define SPI_DEV_ID_1 1 |
| 82 | #define SPI_DEV_ID_2 2 |
| 83 | #define SPI_DEV_ID_3 3 |
| 84 | |
| 85 | /* Interrupt mask */ |
| 86 | #define SPI_INTR_CMD_DONE 0x01 |
| 87 | #define SPI_INTR_RX_OVERFLOW 0x02 |
| 88 | #define SPI_INTR_TX_UNDERFLOW 0x04 |
| 89 | #define SPI_INTR_TX_OVERFLOW 0x08 |
| 90 | #define SPI_INTR_RX_UNDERFLOW 0x10 |
| 91 | #define SPI_INTR_CLEAR_ALL 0x1f |
| 92 | |
| 93 | /* Status */ |
| 94 | #define SPI_RX_EMPTY 0x02 |
| 95 | #define SPI_CMD_BUSY 0x04 |
| 96 | #define SPI_SERIAL_BUSY 0x08 |
| 97 | |
| 98 | /* Clock configuration */ |
| 99 | #define SPI_CLK_20MHZ 0x00 |
| 100 | #define SPI_CLK_0_391MHZ 0x01 |
| 101 | #define SPI_CLK_0_781MHZ 0x02 /* default */ |
| 102 | #define SPI_CLK_1_563MHZ 0x03 |
| 103 | #define SPI_CLK_3_125MHZ 0x04 |
| 104 | #define SPI_CLK_6_250MHZ 0x05 |
| 105 | #define SPI_CLK_12_50MHZ 0x06 |
| 106 | #define SPI_CLK_MASK 0x07 |
| 107 | #define SPI_SSOFFTIME_MASK 0x38 |
| 108 | #define SPI_SSOFFTIME_SHIFT 3 |
| 109 | #define SPI_BYTE_SWAP 0x80 |
| 110 | |
| 111 | enum bcm63xx_regs_spi { |
| 112 | SPI_CMD, |
| 113 | SPI_INT_STATUS, |
| 114 | SPI_INT_MASK_ST, |
| 115 | SPI_INT_MASK, |
| 116 | SPI_ST, |
| 117 | SPI_CLK_CFG, |
| 118 | SPI_FILL_BYTE, |
| 119 | SPI_MSG_TAIL, |
| 120 | SPI_RX_TAIL, |
| 121 | SPI_MSG_CTL, |
| 122 | SPI_MSG_DATA, |
| 123 | SPI_RX_DATA, |
| 124 | SPI_MSG_TYPE_SHIFT, |
| 125 | SPI_MSG_CTL_WIDTH, |
| 126 | SPI_MSG_DATA_SIZE, |
| 127 | }; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 128 | |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 129 | #define BCM63XX_SPI_MAX_PREPEND 15 |
| 130 | |
Jonas Gorski | 6505999 | 2015-09-10 16:11:40 +0200 | [diff] [blame] | 131 | #define BCM63XX_SPI_MAX_CS 8 |
Jonas Gorski | a45fcea | 2015-09-10 16:11:41 +0200 | [diff] [blame] | 132 | #define BCM63XX_SPI_BUS_NUM 0 |
Jonas Gorski | 6505999 | 2015-09-10 16:11:40 +0200 | [diff] [blame] | 133 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 134 | struct bcm63xx_spi { |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 135 | struct completion done; |
| 136 | |
| 137 | void __iomem *regs; |
| 138 | int irq; |
| 139 | |
| 140 | /* Platform data */ |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 141 | const unsigned long *reg_offsets; |
Aravind Thokala | b85d65d | 2017-06-19 23:47:48 +0530 | [diff] [blame] | 142 | unsigned int fifo_size; |
Florian Fainelli | 5a67044 | 2012-06-18 12:07:51 +0200 | [diff] [blame] | 143 | unsigned int msg_type_shift; |
| 144 | unsigned int msg_ctl_width; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 145 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 146 | /* data iomem */ |
| 147 | u8 __iomem *tx_io; |
| 148 | const u8 __iomem *rx_io; |
| 149 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 150 | struct clk *clk; |
| 151 | struct platform_device *pdev; |
| 152 | }; |
| 153 | |
| 154 | static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs, |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 155 | unsigned int offset) |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 156 | { |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 157 | return readb(bs->regs + bs->reg_offsets[offset]); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs, |
| 161 | unsigned int offset) |
| 162 | { |
Jonas Gorski | 682b528 | 2015-10-12 12:24:21 +0200 | [diff] [blame] | 163 | #ifdef CONFIG_CPU_BIG_ENDIAN |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 164 | return ioread16be(bs->regs + bs->reg_offsets[offset]); |
Jonas Gorski | 158fcc4 | 2015-09-10 16:11:42 +0200 | [diff] [blame] | 165 | #else |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 166 | return readw(bs->regs + bs->reg_offsets[offset]); |
Jonas Gorski | 158fcc4 | 2015-09-10 16:11:42 +0200 | [diff] [blame] | 167 | #endif |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static inline void bcm_spi_writeb(struct bcm63xx_spi *bs, |
| 171 | u8 value, unsigned int offset) |
| 172 | { |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 173 | writeb(value, bs->regs + bs->reg_offsets[offset]); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static inline void bcm_spi_writew(struct bcm63xx_spi *bs, |
| 177 | u16 value, unsigned int offset) |
| 178 | { |
Jonas Gorski | 682b528 | 2015-10-12 12:24:21 +0200 | [diff] [blame] | 179 | #ifdef CONFIG_CPU_BIG_ENDIAN |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 180 | iowrite16be(value, bs->regs + bs->reg_offsets[offset]); |
Jonas Gorski | 158fcc4 | 2015-09-10 16:11:42 +0200 | [diff] [blame] | 181 | #else |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 182 | writew(value, bs->regs + bs->reg_offsets[offset]); |
Jonas Gorski | 158fcc4 | 2015-09-10 16:11:42 +0200 | [diff] [blame] | 183 | #endif |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Aravind Thokala | b85d65d | 2017-06-19 23:47:48 +0530 | [diff] [blame] | 186 | static const unsigned int bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = { |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 187 | { 20000000, SPI_CLK_20MHZ }, |
| 188 | { 12500000, SPI_CLK_12_50MHZ }, |
| 189 | { 6250000, SPI_CLK_6_250MHZ }, |
| 190 | { 3125000, SPI_CLK_3_125MHZ }, |
| 191 | { 1563000, SPI_CLK_1_563MHZ }, |
| 192 | { 781000, SPI_CLK_0_781MHZ }, |
| 193 | { 391000, SPI_CLK_0_391MHZ } |
| 194 | }; |
| 195 | |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 196 | static void bcm63xx_spi_setup_transfer(struct spi_device *spi, |
| 197 | struct spi_transfer *t) |
| 198 | { |
| 199 | struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master); |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 200 | u8 clk_cfg, reg; |
| 201 | int i; |
| 202 | |
Geert Uytterhoeven | 4dde99b | 2015-11-09 10:28:50 +0100 | [diff] [blame] | 203 | /* Default to lowest clock configuration */ |
| 204 | clk_cfg = SPI_CLK_0_391MHZ; |
| 205 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 206 | /* Find the closest clock configuration */ |
| 207 | for (i = 0; i < SPI_CLK_MASK; i++) { |
Jonas Gorski | 68792e2 | 2013-03-12 00:13:46 +0100 | [diff] [blame] | 208 | if (t->speed_hz >= bcm63xx_spi_freq_table[i][0]) { |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 209 | clk_cfg = bcm63xx_spi_freq_table[i][1]; |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 214 | /* clear existing clock configuration bits of the register */ |
| 215 | reg = bcm_spi_readb(bs, SPI_CLK_CFG); |
| 216 | reg &= ~SPI_CLK_MASK; |
| 217 | reg |= clk_cfg; |
| 218 | |
| 219 | bcm_spi_writeb(bs, reg, SPI_CLK_CFG); |
| 220 | dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n", |
Jonas Gorski | 68792e2 | 2013-03-12 00:13:46 +0100 | [diff] [blame] | 221 | clk_cfg, t->speed_hz); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* the spi->mode bits understood by this driver: */ |
| 225 | #define MODEBITS (SPI_CPOL | SPI_CPHA) |
| 226 | |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 227 | static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *first, |
| 228 | unsigned int num_transfers) |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 229 | { |
| 230 | struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master); |
| 231 | u16 msg_ctl; |
| 232 | u16 cmd; |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 233 | unsigned int i, timeout = 0, prepend_len = 0, len = 0; |
| 234 | struct spi_transfer *t = first; |
| 235 | bool do_rx = false; |
| 236 | bool do_tx = false; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 237 | |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 238 | /* Disable the CMD_DONE interrupt */ |
| 239 | bcm_spi_writeb(bs, 0, SPI_INT_MASK); |
| 240 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 241 | dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n", |
| 242 | t->tx_buf, t->rx_buf, t->len); |
| 243 | |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 244 | if (num_transfers > 1 && t->tx_buf && t->len <= BCM63XX_SPI_MAX_PREPEND) |
| 245 | prepend_len = t->len; |
| 246 | |
| 247 | /* prepare the buffer */ |
| 248 | for (i = 0; i < num_transfers; i++) { |
| 249 | if (t->tx_buf) { |
| 250 | do_tx = true; |
| 251 | memcpy_toio(bs->tx_io + len, t->tx_buf, t->len); |
| 252 | |
| 253 | /* don't prepend more than one tx */ |
| 254 | if (t != first) |
| 255 | prepend_len = 0; |
| 256 | } |
| 257 | |
| 258 | if (t->rx_buf) { |
| 259 | do_rx = true; |
| 260 | /* prepend is half-duplex write only */ |
| 261 | if (t == first) |
| 262 | prepend_len = 0; |
| 263 | } |
| 264 | |
| 265 | len += t->len; |
| 266 | |
| 267 | t = list_entry(t->transfer_list.next, struct spi_transfer, |
| 268 | transfer_list); |
| 269 | } |
| 270 | |
Axel Lin | aa0fe82 | 2014-02-09 11:06:04 +0800 | [diff] [blame] | 271 | reinit_completion(&bs->done); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 272 | |
| 273 | /* Fill in the Message control register */ |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 274 | msg_ctl = (len << SPI_BYTE_CNT_SHIFT); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 275 | |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 276 | if (do_rx && do_tx && prepend_len == 0) |
Florian Fainelli | 5a67044 | 2012-06-18 12:07:51 +0200 | [diff] [blame] | 277 | msg_ctl |= (SPI_FD_RW << bs->msg_type_shift); |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 278 | else if (do_rx) |
Florian Fainelli | 5a67044 | 2012-06-18 12:07:51 +0200 | [diff] [blame] | 279 | msg_ctl |= (SPI_HD_R << bs->msg_type_shift); |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 280 | else if (do_tx) |
Florian Fainelli | 5a67044 | 2012-06-18 12:07:51 +0200 | [diff] [blame] | 281 | msg_ctl |= (SPI_HD_W << bs->msg_type_shift); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 282 | |
Florian Fainelli | 5a67044 | 2012-06-18 12:07:51 +0200 | [diff] [blame] | 283 | switch (bs->msg_ctl_width) { |
| 284 | case 8: |
| 285 | bcm_spi_writeb(bs, msg_ctl, SPI_MSG_CTL); |
| 286 | break; |
| 287 | case 16: |
| 288 | bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL); |
| 289 | break; |
| 290 | } |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 291 | |
| 292 | /* Issue the transfer */ |
| 293 | cmd = SPI_CMD_START_IMMEDIATE; |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 294 | cmd |= (prepend_len << SPI_CMD_PREPEND_BYTE_CNT_SHIFT); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 295 | cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT); |
| 296 | bcm_spi_writew(bs, cmd, SPI_CMD); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 297 | |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 298 | /* Enable the CMD_DONE interrupt */ |
| 299 | bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 300 | |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 301 | timeout = wait_for_completion_timeout(&bs->done, HZ); |
| 302 | if (!timeout) |
| 303 | return -ETIMEDOUT; |
| 304 | |
Jonas Gorski | 20e9e78 | 2013-12-17 21:42:08 +0100 | [diff] [blame] | 305 | if (!do_rx) |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 306 | return 0; |
| 307 | |
| 308 | len = 0; |
| 309 | t = first; |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 310 | /* Read out all the data */ |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 311 | for (i = 0; i < num_transfers; i++) { |
| 312 | if (t->rx_buf) |
| 313 | memcpy_fromio(t->rx_buf, bs->rx_io + len, t->len); |
| 314 | |
| 315 | if (t != first || prepend_len == 0) |
| 316 | len += t->len; |
| 317 | |
| 318 | t = list_entry(t->transfer_list.next, struct spi_transfer, |
| 319 | transfer_list); |
| 320 | } |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 321 | |
| 322 | return 0; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 325 | static int bcm63xx_spi_transfer_one(struct spi_master *master, |
| 326 | struct spi_message *m) |
| 327 | { |
| 328 | struct bcm63xx_spi *bs = spi_master_get_devdata(master); |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 329 | struct spi_transfer *t, *first = NULL; |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 330 | struct spi_device *spi = m->spi; |
| 331 | int status = 0; |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 332 | unsigned int n_transfers = 0, total_len = 0; |
| 333 | bool can_use_prepend = false; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 334 | |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 335 | /* |
| 336 | * This SPI controller does not support keeping CS active after a |
| 337 | * transfer. |
| 338 | * Work around this by merging as many transfers we can into one big |
| 339 | * full-duplex transfers. |
| 340 | */ |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 341 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 342 | if (!first) |
| 343 | first = t; |
| 344 | |
| 345 | n_transfers++; |
| 346 | total_len += t->len; |
| 347 | |
| 348 | if (n_transfers == 2 && !first->rx_buf && !t->tx_buf && |
| 349 | first->len <= BCM63XX_SPI_MAX_PREPEND) |
| 350 | can_use_prepend = true; |
| 351 | else if (can_use_prepend && t->tx_buf) |
| 352 | can_use_prepend = false; |
| 353 | |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 354 | /* we can only transfer one fifo worth of data */ |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 355 | if ((can_use_prepend && |
| 356 | total_len > (bs->fifo_size + BCM63XX_SPI_MAX_PREPEND)) || |
| 357 | (!can_use_prepend && total_len > bs->fifo_size)) { |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 358 | dev_err(&spi->dev, "unable to do transfers larger than FIFO size (%i > %i)\n", |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 359 | total_len, bs->fifo_size); |
| 360 | status = -EINVAL; |
| 361 | goto exit; |
| 362 | } |
| 363 | |
| 364 | /* all combined transfers have to have the same speed */ |
| 365 | if (t->speed_hz != first->speed_hz) { |
| 366 | dev_err(&spi->dev, "unable to change speed between transfers\n"); |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 367 | status = -EINVAL; |
| 368 | goto exit; |
| 369 | } |
| 370 | |
| 371 | /* CS will be deasserted directly after transfer */ |
Alexandru Ardelean | e7f2d4c | 2021-03-08 16:54:54 +0200 | [diff] [blame] | 372 | if (t->delay.value) { |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 373 | dev_err(&spi->dev, "unable to keep CS asserted after transfer\n"); |
| 374 | status = -EINVAL; |
| 375 | goto exit; |
| 376 | } |
| 377 | |
Jonas Gorski | b17de07 | 2013-02-03 15:15:13 +0100 | [diff] [blame] | 378 | if (t->cs_change || |
| 379 | list_is_last(&t->transfer_list, &m->transfers)) { |
| 380 | /* configure adapter for a new transfer */ |
| 381 | bcm63xx_spi_setup_transfer(spi, first); |
| 382 | |
| 383 | /* send the data */ |
| 384 | status = bcm63xx_txrx_bufs(spi, first, n_transfers); |
| 385 | if (status) |
| 386 | goto exit; |
| 387 | |
| 388 | m->actual_length += total_len; |
| 389 | |
| 390 | first = NULL; |
| 391 | n_transfers = 0; |
| 392 | total_len = 0; |
| 393 | can_use_prepend = false; |
Jonas Gorski | c0fde3b | 2013-02-03 15:15:12 +0100 | [diff] [blame] | 394 | } |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 395 | } |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 396 | exit: |
| 397 | m->status = status; |
| 398 | spi_finalize_current_message(master); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 399 | |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 400 | return 0; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* This driver supports single master mode only. Hence |
| 404 | * CMD_DONE is the only interrupt we care about |
| 405 | */ |
| 406 | static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id) |
| 407 | { |
| 408 | struct spi_master *master = (struct spi_master *)dev_id; |
| 409 | struct bcm63xx_spi *bs = spi_master_get_devdata(master); |
| 410 | u8 intr; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 411 | |
| 412 | /* Read interupts and clear them immediately */ |
| 413 | intr = bcm_spi_readb(bs, SPI_INT_STATUS); |
| 414 | bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS); |
| 415 | bcm_spi_writeb(bs, 0, SPI_INT_MASK); |
| 416 | |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 417 | /* A transfer completed */ |
| 418 | if (intr & SPI_INTR_CMD_DONE) |
| 419 | complete(&bs->done); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 420 | |
| 421 | return IRQ_HANDLED; |
| 422 | } |
| 423 | |
Jonas Gorski | ccd0657 | 2017-02-22 00:30:40 +0100 | [diff] [blame] | 424 | static size_t bcm63xx_spi_max_length(struct spi_device *spi) |
Jonas Gorski | 0135c03 | 2017-02-20 11:50:09 +0100 | [diff] [blame] | 425 | { |
| 426 | struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master); |
| 427 | |
| 428 | return bs->fifo_size; |
| 429 | } |
| 430 | |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 431 | static const unsigned long bcm6348_spi_reg_offsets[] = { |
| 432 | [SPI_CMD] = SPI_6348_CMD, |
| 433 | [SPI_INT_STATUS] = SPI_6348_INT_STATUS, |
| 434 | [SPI_INT_MASK_ST] = SPI_6348_INT_MASK_ST, |
| 435 | [SPI_INT_MASK] = SPI_6348_INT_MASK, |
| 436 | [SPI_ST] = SPI_6348_ST, |
| 437 | [SPI_CLK_CFG] = SPI_6348_CLK_CFG, |
| 438 | [SPI_FILL_BYTE] = SPI_6348_FILL_BYTE, |
| 439 | [SPI_MSG_TAIL] = SPI_6348_MSG_TAIL, |
| 440 | [SPI_RX_TAIL] = SPI_6348_RX_TAIL, |
| 441 | [SPI_MSG_CTL] = SPI_6348_MSG_CTL, |
| 442 | [SPI_MSG_DATA] = SPI_6348_MSG_DATA, |
| 443 | [SPI_RX_DATA] = SPI_6348_RX_DATA, |
| 444 | [SPI_MSG_TYPE_SHIFT] = SPI_6348_MSG_TYPE_SHIFT, |
| 445 | [SPI_MSG_CTL_WIDTH] = SPI_6348_MSG_CTL_WIDTH, |
| 446 | [SPI_MSG_DATA_SIZE] = SPI_6348_MSG_DATA_SIZE, |
| 447 | }; |
| 448 | |
| 449 | static const unsigned long bcm6358_spi_reg_offsets[] = { |
| 450 | [SPI_CMD] = SPI_6358_CMD, |
| 451 | [SPI_INT_STATUS] = SPI_6358_INT_STATUS, |
| 452 | [SPI_INT_MASK_ST] = SPI_6358_INT_MASK_ST, |
| 453 | [SPI_INT_MASK] = SPI_6358_INT_MASK, |
| 454 | [SPI_ST] = SPI_6358_ST, |
| 455 | [SPI_CLK_CFG] = SPI_6358_CLK_CFG, |
| 456 | [SPI_FILL_BYTE] = SPI_6358_FILL_BYTE, |
| 457 | [SPI_MSG_TAIL] = SPI_6358_MSG_TAIL, |
| 458 | [SPI_RX_TAIL] = SPI_6358_RX_TAIL, |
| 459 | [SPI_MSG_CTL] = SPI_6358_MSG_CTL, |
| 460 | [SPI_MSG_DATA] = SPI_6358_MSG_DATA, |
| 461 | [SPI_RX_DATA] = SPI_6358_RX_DATA, |
| 462 | [SPI_MSG_TYPE_SHIFT] = SPI_6358_MSG_TYPE_SHIFT, |
| 463 | [SPI_MSG_CTL_WIDTH] = SPI_6358_MSG_CTL_WIDTH, |
| 464 | [SPI_MSG_DATA_SIZE] = SPI_6358_MSG_DATA_SIZE, |
| 465 | }; |
| 466 | |
| 467 | static const struct platform_device_id bcm63xx_spi_dev_match[] = { |
| 468 | { |
| 469 | .name = "bcm6348-spi", |
| 470 | .driver_data = (unsigned long)bcm6348_spi_reg_offsets, |
| 471 | }, |
| 472 | { |
| 473 | .name = "bcm6358-spi", |
| 474 | .driver_data = (unsigned long)bcm6358_spi_reg_offsets, |
| 475 | }, |
| 476 | { |
| 477 | }, |
| 478 | }; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 479 | |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 480 | static const struct of_device_id bcm63xx_spi_of_match[] = { |
| 481 | { .compatible = "brcm,bcm6348-spi", .data = &bcm6348_spi_reg_offsets }, |
| 482 | { .compatible = "brcm,bcm6358-spi", .data = &bcm6358_spi_reg_offsets }, |
| 483 | { }, |
| 484 | }; |
| 485 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 486 | static int bcm63xx_spi_probe(struct platform_device *pdev) |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 487 | { |
| 488 | struct resource *r; |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 489 | const unsigned long *bcm63xx_spireg; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 490 | struct device *dev = &pdev->dev; |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 491 | int irq, bus_num; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 492 | struct spi_master *master; |
| 493 | struct clk *clk; |
| 494 | struct bcm63xx_spi *bs; |
| 495 | int ret; |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 496 | u32 num_cs = BCM63XX_SPI_MAX_CS; |
Álvaro Fernández Rojas | 38807ad | 2020-06-16 19:32:34 +0200 | [diff] [blame] | 497 | struct reset_control *reset; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 498 | |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 499 | if (dev->of_node) { |
| 500 | const struct of_device_id *match; |
| 501 | |
| 502 | match = of_match_node(bcm63xx_spi_of_match, dev->of_node); |
| 503 | if (!match) |
| 504 | return -EINVAL; |
| 505 | bcm63xx_spireg = match->data; |
| 506 | |
| 507 | of_property_read_u32(dev->of_node, "num-cs", &num_cs); |
| 508 | if (num_cs > BCM63XX_SPI_MAX_CS) { |
| 509 | dev_warn(dev, "unsupported number of cs (%i), reducing to 8\n", |
| 510 | num_cs); |
| 511 | num_cs = BCM63XX_SPI_MAX_CS; |
| 512 | } |
| 513 | |
| 514 | bus_num = -1; |
| 515 | } else if (pdev->id_entry->driver_data) { |
| 516 | const struct platform_device_id *match = pdev->id_entry; |
| 517 | |
| 518 | bcm63xx_spireg = (const unsigned long *)match->driver_data; |
| 519 | bus_num = BCM63XX_SPI_BUS_NUM; |
| 520 | } else { |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 521 | return -EINVAL; |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 522 | } |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 523 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 524 | irq = platform_get_irq(pdev, 0); |
Stephen Boyd | 6b8ac10 | 2019-07-30 11:15:41 -0700 | [diff] [blame] | 525 | if (irq < 0) |
Gustavo A. R. Silva | ba8afe9 | 2017-08-07 23:52:34 -0500 | [diff] [blame] | 526 | return irq; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 527 | |
Jingoo Han | acf4fc6 | 2013-12-09 19:20:15 +0900 | [diff] [blame] | 528 | clk = devm_clk_get(dev, "spi"); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 529 | if (IS_ERR(clk)) { |
| 530 | dev_err(dev, "no clock for device\n"); |
Jingoo Han | acf4fc6 | 2013-12-09 19:20:15 +0900 | [diff] [blame] | 531 | return PTR_ERR(clk); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Álvaro Fernández Rojas | 38807ad | 2020-06-16 19:32:34 +0200 | [diff] [blame] | 534 | reset = devm_reset_control_get_optional_exclusive(dev, NULL); |
| 535 | if (IS_ERR(reset)) |
| 536 | return PTR_ERR(reset); |
| 537 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 538 | master = spi_alloc_master(dev, sizeof(*bs)); |
| 539 | if (!master) { |
| 540 | dev_err(dev, "out of memory\n"); |
Jingoo Han | acf4fc6 | 2013-12-09 19:20:15 +0900 | [diff] [blame] | 541 | return -ENOMEM; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | bs = spi_master_get_devdata(master); |
Axel Lin | aa0fe82 | 2014-02-09 11:06:04 +0800 | [diff] [blame] | 545 | init_completion(&bs->done); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 546 | |
| 547 | platform_set_drvdata(pdev, master); |
| 548 | bs->pdev = pdev; |
| 549 | |
Julia Lawall | de0fa83 | 2013-08-14 11:11:09 +0200 | [diff] [blame] | 550 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jonas Gorski | b66c773 | 2013-03-12 00:13:47 +0100 | [diff] [blame] | 551 | bs->regs = devm_ioremap_resource(&pdev->dev, r); |
| 552 | if (IS_ERR(bs->regs)) { |
| 553 | ret = PTR_ERR(bs->regs); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 554 | goto out_err; |
| 555 | } |
| 556 | |
| 557 | bs->irq = irq; |
| 558 | bs->clk = clk; |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 559 | bs->reg_offsets = bcm63xx_spireg; |
| 560 | bs->fifo_size = bs->reg_offsets[SPI_MSG_DATA_SIZE]; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 561 | |
| 562 | ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0, |
| 563 | pdev->name, master); |
| 564 | if (ret) { |
| 565 | dev_err(dev, "unable to request irq\n"); |
| 566 | goto out_err; |
| 567 | } |
| 568 | |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 569 | master->dev.of_node = dev->of_node; |
| 570 | master->bus_num = bus_num; |
| 571 | master->num_chipselect = num_cs; |
Florian Fainelli | cde4384 | 2012-04-20 15:37:33 +0200 | [diff] [blame] | 572 | master->transfer_one_message = bcm63xx_spi_transfer_one; |
Florian Fainelli | 88a3a25 | 2012-04-20 15:37:35 +0200 | [diff] [blame] | 573 | master->mode_bits = MODEBITS; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 574 | master->bits_per_word_mask = SPI_BPW_MASK(8); |
Jonas Gorski | 0135c03 | 2017-02-20 11:50:09 +0100 | [diff] [blame] | 575 | master->max_transfer_size = bcm63xx_spi_max_length; |
| 576 | master->max_message_size = bcm63xx_spi_max_length; |
Mark Brown | 5355d96 | 2013-07-28 15:34:06 +0100 | [diff] [blame] | 577 | master->auto_runtime_pm = true; |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 578 | bs->msg_type_shift = bs->reg_offsets[SPI_MSG_TYPE_SHIFT]; |
| 579 | bs->msg_ctl_width = bs->reg_offsets[SPI_MSG_CTL_WIDTH]; |
| 580 | bs->tx_io = (u8 *)(bs->regs + bs->reg_offsets[SPI_MSG_DATA]); |
| 581 | bs->rx_io = (const u8 *)(bs->regs + bs->reg_offsets[SPI_RX_DATA]); |
Florian Fainelli | 5a67044 | 2012-06-18 12:07:51 +0200 | [diff] [blame] | 582 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 583 | /* Initialize hardware */ |
Jonas Gorski | ea01e8a | 2013-12-17 21:42:09 +0100 | [diff] [blame] | 584 | ret = clk_prepare_enable(bs->clk); |
| 585 | if (ret) |
| 586 | goto out_err; |
| 587 | |
Álvaro Fernández Rojas | 38807ad | 2020-06-16 19:32:34 +0200 | [diff] [blame] | 588 | ret = reset_control_reset(reset); |
| 589 | if (ret) { |
| 590 | dev_err(dev, "unable to reset device: %d\n", ret); |
| 591 | goto out_clk_disable; |
| 592 | } |
| 593 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 594 | bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS); |
| 595 | |
Álvaro Fernández Rojas | 2d13f2f | 2021-02-23 16:18:50 +0100 | [diff] [blame] | 596 | pm_runtime_enable(&pdev->dev); |
| 597 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 598 | /* register and we are done */ |
Jingoo Han | bca7693 | 2013-09-24 13:24:57 +0900 | [diff] [blame] | 599 | ret = devm_spi_register_master(dev, master); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 600 | if (ret) { |
| 601 | dev_err(dev, "spi register failed\n"); |
Álvaro Fernández Rojas | 2d13f2f | 2021-02-23 16:18:50 +0100 | [diff] [blame] | 602 | goto out_pm_disable; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Arnd Bergmann | 0ba2cf7 | 2015-11-12 17:44:34 +0100 | [diff] [blame] | 605 | dev_info(dev, "at %pr (irq %d, FIFOs size %d)\n", |
| 606 | r, irq, bs->fifo_size); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 607 | |
| 608 | return 0; |
| 609 | |
Álvaro Fernández Rojas | 2d13f2f | 2021-02-23 16:18:50 +0100 | [diff] [blame] | 610 | out_pm_disable: |
| 611 | pm_runtime_disable(&pdev->dev); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 612 | out_clk_disable: |
Jonas Gorski | 4fbb82a | 2013-03-12 00:13:38 +0100 | [diff] [blame] | 613 | clk_disable_unprepare(clk); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 614 | out_err: |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 615 | spi_master_put(master); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 616 | return ret; |
| 617 | } |
| 618 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 619 | static int bcm63xx_spi_remove(struct platform_device *pdev) |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 620 | { |
Wei Yongjun | 9637b86 | 2013-11-15 15:50:59 +0800 | [diff] [blame] | 621 | struct spi_master *master = platform_get_drvdata(pdev); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 622 | struct bcm63xx_spi *bs = spi_master_get_devdata(master); |
| 623 | |
| 624 | /* reset spi block */ |
| 625 | bcm_spi_writeb(bs, 0, SPI_INT_MASK); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 626 | |
| 627 | /* HW shutdown */ |
Jonas Gorski | 4fbb82a | 2013-03-12 00:13:38 +0100 | [diff] [blame] | 628 | clk_disable_unprepare(bs->clk); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 629 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
Jonas Gorski | 1bae202 | 2013-12-17 21:42:10 +0100 | [diff] [blame] | 633 | #ifdef CONFIG_PM_SLEEP |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 634 | static int bcm63xx_spi_suspend(struct device *dev) |
| 635 | { |
Axel Lin | a1216394 | 2013-08-09 15:35:16 +0800 | [diff] [blame] | 636 | struct spi_master *master = dev_get_drvdata(dev); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 637 | struct bcm63xx_spi *bs = spi_master_get_devdata(master); |
| 638 | |
Florian Fainelli | 9651995 | 2012-10-03 11:56:54 +0200 | [diff] [blame] | 639 | spi_master_suspend(master); |
| 640 | |
Jonas Gorski | 4fbb82a | 2013-03-12 00:13:38 +0100 | [diff] [blame] | 641 | clk_disable_unprepare(bs->clk); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static int bcm63xx_spi_resume(struct device *dev) |
| 647 | { |
Axel Lin | a1216394 | 2013-08-09 15:35:16 +0800 | [diff] [blame] | 648 | struct spi_master *master = dev_get_drvdata(dev); |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 649 | struct bcm63xx_spi *bs = spi_master_get_devdata(master); |
Jonas Gorski | ea01e8a | 2013-12-17 21:42:09 +0100 | [diff] [blame] | 650 | int ret; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 651 | |
Jonas Gorski | ea01e8a | 2013-12-17 21:42:09 +0100 | [diff] [blame] | 652 | ret = clk_prepare_enable(bs->clk); |
| 653 | if (ret) |
| 654 | return ret; |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 655 | |
Florian Fainelli | 9651995 | 2012-10-03 11:56:54 +0200 | [diff] [blame] | 656 | spi_master_resume(master); |
| 657 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 658 | return 0; |
| 659 | } |
Jonas Gorski | 1bae202 | 2013-12-17 21:42:10 +0100 | [diff] [blame] | 660 | #endif |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 661 | |
| 662 | static const struct dev_pm_ops bcm63xx_spi_pm_ops = { |
Jonas Gorski | 1bae202 | 2013-12-17 21:42:10 +0100 | [diff] [blame] | 663 | SET_SYSTEM_SLEEP_PM_OPS(bcm63xx_spi_suspend, bcm63xx_spi_resume) |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 664 | }; |
| 665 | |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 666 | static struct platform_driver bcm63xx_spi_driver = { |
| 667 | .driver = { |
| 668 | .name = "bcm63xx-spi", |
Jonas Gorski | 1bae202 | 2013-12-17 21:42:10 +0100 | [diff] [blame] | 669 | .pm = &bcm63xx_spi_pm_ops, |
Jonas Gorski | c29f088 | 2017-02-21 11:58:22 +0100 | [diff] [blame] | 670 | .of_match_table = bcm63xx_spi_of_match, |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 671 | }, |
Jonas Gorski | 44d8fb3 | 2015-10-12 12:24:23 +0200 | [diff] [blame] | 672 | .id_table = bcm63xx_spi_dev_match, |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 673 | .probe = bcm63xx_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 674 | .remove = bcm63xx_spi_remove, |
Florian Fainelli | b42dfed | 2012-02-01 11:14:09 +0100 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | module_platform_driver(bcm63xx_spi_driver); |
| 678 | |
| 679 | MODULE_ALIAS("platform:bcm63xx_spi"); |
| 680 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 681 | MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>"); |
| 682 | MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver"); |
| 683 | MODULE_LICENSE("GPL"); |