William Zhang | 7ff5855 | 2023-06-07 16:37:05 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com> |
| 4 | * |
| 5 | * Derived from linux/drivers/spi/spi-bcm63xx-hsspi.c: |
| 6 | * Copyright (C) 2000-2010 Broadcom Corporation |
| 7 | * Copyright (C) 2012-2013 Jonas Gorski <jogo@openwrt.org> |
| 8 | * Copyright (C) 2021 Broadcom Ltd |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <clk.h> |
| 14 | #include <spi.h> |
| 15 | #include <reset.h> |
| 16 | #include <wait_bit.h> |
| 17 | #include <dm.h> |
| 18 | #include <dm/device_compat.h> |
| 19 | |
| 20 | #define HSSPI_PP 0 |
| 21 | |
| 22 | #define SPI_MAX_SYNC_CLOCK 30000000 |
| 23 | |
| 24 | /* SPI Control register */ |
| 25 | #define SPI_CTL_REG 0x000 |
| 26 | #define SPI_CTL_CS_POL_SHIFT 0 |
| 27 | #define SPI_CTL_CS_POL_MASK (0xff << SPI_CTL_CS_POL_SHIFT) |
| 28 | #define SPI_CTL_CLK_GATE_SHIFT 16 |
| 29 | #define SPI_CTL_CLK_GATE_MASK BIT(SPI_CTL_CLK_GATE_SHIFT) |
| 30 | #define SPI_CTL_CLK_POL_SHIFT 17 |
| 31 | #define SPI_CTL_CLK_POL_MASK BIT(SPI_CTL_CLK_POL_SHIFT) |
| 32 | |
| 33 | /* SPI Interrupts registers */ |
| 34 | #define SPI_IR_STAT_REG 0x008 |
| 35 | #define SPI_IR_ST_MASK_REG 0x00c |
| 36 | #define SPI_IR_MASK_REG 0x010 |
| 37 | |
| 38 | #define SPI_IR_CLEAR_ALL 0xff001f1f |
| 39 | |
| 40 | /* SPI Ping-Pong Command registers */ |
| 41 | #define SPI_CMD_REG (0x080 + (0x40 * (HSSPI_PP)) + 0x00) |
| 42 | #define SPI_CMD_OP_SHIFT 0 |
| 43 | #define SPI_CMD_OP_START BIT(SPI_CMD_OP_SHIFT) |
| 44 | #define SPI_CMD_PFL_SHIFT 8 |
| 45 | #define SPI_CMD_PFL_MASK (0x7 << SPI_CMD_PFL_SHIFT) |
| 46 | #define SPI_CMD_SLAVE_SHIFT 12 |
| 47 | #define SPI_CMD_SLAVE_MASK (0x7 << SPI_CMD_SLAVE_SHIFT) |
| 48 | |
| 49 | /* SPI Ping-Pong Status registers */ |
| 50 | #define SPI_STAT_REG (0x080 + (0x40 * (HSSPI_PP)) + 0x04) |
| 51 | #define SPI_STAT_SRCBUSY_SHIFT 1 |
| 52 | #define SPI_STAT_SRCBUSY_MASK BIT(SPI_STAT_SRCBUSY_SHIFT) |
| 53 | |
| 54 | /* SPI Profile Clock registers */ |
| 55 | #define SPI_PFL_CLK_REG(x) (0x100 + (0x20 * (x)) + 0x00) |
| 56 | #define SPI_PFL_CLK_FREQ_SHIFT 0 |
| 57 | #define SPI_PFL_CLK_FREQ_MASK (0x3fff << SPI_PFL_CLK_FREQ_SHIFT) |
| 58 | #define SPI_PFL_CLK_RSTLOOP_SHIFT 15 |
| 59 | #define SPI_PFL_CLK_RSTLOOP_MASK BIT(SPI_PFL_CLK_RSTLOOP_SHIFT) |
| 60 | |
| 61 | /* SPI Profile Signal registers */ |
| 62 | #define SPI_PFL_SIG_REG(x) (0x100 + (0x20 * (x)) + 0x04) |
| 63 | #define SPI_PFL_SIG_LATCHRIS_SHIFT 12 |
| 64 | #define SPI_PFL_SIG_LATCHRIS_MASK BIT(SPI_PFL_SIG_LATCHRIS_SHIFT) |
| 65 | #define SPI_PFL_SIG_LAUNCHRIS_SHIFT 13 |
| 66 | #define SPI_PFL_SIG_LAUNCHRIS_MASK BIT(SPI_PFL_SIG_LAUNCHRIS_SHIFT) |
| 67 | #define SPI_PFL_SIG_ASYNCIN_SHIFT 16 |
| 68 | #define SPI_PFL_SIG_ASYNCIN_MASK BIT(SPI_PFL_SIG_ASYNCIN_SHIFT) |
| 69 | |
| 70 | /* SPI Profile Mode registers */ |
| 71 | #define SPI_PFL_MODE_REG(x) (0x100 + (0x20 * (x)) + 0x08) |
| 72 | #define SPI_PFL_MODE_FILL_SHIFT 0 |
| 73 | #define SPI_PFL_MODE_FILL_MASK (0xff << SPI_PFL_MODE_FILL_SHIFT) |
| 74 | #define SPI_PFL_MODE_MDRDSZ_SHIFT 16 |
| 75 | #define SPI_PFL_MODE_MDRDSZ_MASK BIT(SPI_PFL_MODE_MDRDSZ_SHIFT) |
| 76 | #define SPI_PFL_MODE_MDWRSZ_SHIFT 18 |
| 77 | #define SPI_PFL_MODE_MDWRSZ_MASK BIT(SPI_PFL_MODE_MDWRSZ_SHIFT) |
| 78 | #define SPI_PFL_MODE_3WIRE_SHIFT 20 |
| 79 | #define SPI_PFL_MODE_3WIRE_MASK BIT(SPI_PFL_MODE_3WIRE_SHIFT) |
| 80 | |
| 81 | /* SPI Ping-Pong FIFO registers */ |
| 82 | #define HSSPI_FIFO_SIZE 0x200 |
| 83 | #define HSSPI_FIFO_BASE (0x200 + \ |
| 84 | (HSSPI_FIFO_SIZE * HSSPI_PP)) |
| 85 | |
| 86 | /* SPI Ping-Pong FIFO OP register */ |
| 87 | #define HSSPI_FIFO_OP_SIZE 0x2 |
| 88 | #define HSSPI_FIFO_OP_REG (HSSPI_FIFO_BASE + 0x00) |
| 89 | #define HSSPI_FIFO_OP_BYTES_SHIFT 0 |
| 90 | #define HSSPI_FIFO_OP_BYTES_MASK (0x3ff << HSSPI_FIFO_OP_BYTES_SHIFT) |
| 91 | #define HSSPI_FIFO_OP_MBIT_SHIFT 11 |
| 92 | #define HSSPI_FIFO_OP_MBIT_MASK BIT(HSSPI_FIFO_OP_MBIT_SHIFT) |
| 93 | #define HSSPI_FIFO_OP_CODE_SHIFT 13 |
| 94 | #define HSSPI_FIFO_OP_READ_WRITE (1 << HSSPI_FIFO_OP_CODE_SHIFT) |
| 95 | #define HSSPI_FIFO_OP_CODE_W (2 << HSSPI_FIFO_OP_CODE_SHIFT) |
| 96 | #define HSSPI_FIFO_OP_CODE_R (3 << HSSPI_FIFO_OP_CODE_SHIFT) |
| 97 | |
| 98 | #define HSSPI_MAX_DATA_SIZE (HSSPI_FIFO_SIZE - HSSPI_FIFO_OP_SIZE) |
| 99 | |
| 100 | #define SPIM_CTRL_CS_OVERRIDE_SEL_SHIFT 0 |
| 101 | #define SPIM_CTRL_CS_OVERRIDE_SEL_MASK 0xff |
| 102 | #define SPIM_CTRL_CS_OVERRIDE_VAL_SHIFT 8 |
| 103 | #define SPIM_CTRL_CS_OVERRIDE_VAL_MASK 0xff |
| 104 | |
| 105 | struct bcmbca_hsspi_priv { |
| 106 | void __iomem *regs; |
| 107 | void __iomem *spim_ctrl; |
| 108 | u32 clk_rate; |
| 109 | u8 num_cs; |
| 110 | u8 cs_pols; |
| 111 | u32 speed; |
| 112 | }; |
| 113 | |
| 114 | static int bcmbca_hsspi_cs_info(struct udevice *bus, uint cs, |
| 115 | struct spi_cs_info *info) |
| 116 | { |
| 117 | struct bcmbca_hsspi_priv *priv = dev_get_priv(bus); |
| 118 | |
| 119 | if (cs >= priv->num_cs) { |
| 120 | dev_err(bus, "no cs %u\n", cs); |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int bcmbca_hsspi_set_mode(struct udevice *bus, uint mode) |
| 128 | { |
| 129 | struct bcmbca_hsspi_priv *priv = dev_get_priv(bus); |
| 130 | |
| 131 | /* clock polarity */ |
| 132 | if (mode & SPI_CPOL) |
| 133 | setbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_POL_MASK); |
| 134 | else |
| 135 | clrbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_POL_MASK); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int bcmbca_hsspi_set_speed(struct udevice *bus, uint speed) |
| 141 | { |
| 142 | struct bcmbca_hsspi_priv *priv = dev_get_priv(bus); |
| 143 | |
| 144 | priv->speed = speed; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void bcmbca_hsspi_setup_clock(struct bcmbca_hsspi_priv *priv, |
| 150 | struct dm_spi_slave_plat *plat) |
| 151 | { |
| 152 | u32 clr, set; |
| 153 | |
| 154 | /* profile clock */ |
| 155 | set = DIV_ROUND_UP(priv->clk_rate, priv->speed); |
| 156 | set = DIV_ROUND_UP(2048, set); |
| 157 | set &= SPI_PFL_CLK_FREQ_MASK; |
| 158 | set |= SPI_PFL_CLK_RSTLOOP_MASK; |
| 159 | writel(set, priv->regs + SPI_PFL_CLK_REG(plat->cs)); |
| 160 | |
| 161 | /* profile signal */ |
| 162 | set = 0; |
| 163 | clr = SPI_PFL_SIG_LAUNCHRIS_MASK | |
| 164 | SPI_PFL_SIG_LATCHRIS_MASK | |
| 165 | SPI_PFL_SIG_ASYNCIN_MASK; |
| 166 | |
| 167 | /* latch/launch config */ |
| 168 | if (plat->mode & SPI_CPHA) |
| 169 | set |= SPI_PFL_SIG_LAUNCHRIS_MASK; |
| 170 | else |
| 171 | set |= SPI_PFL_SIG_LATCHRIS_MASK; |
| 172 | |
| 173 | /* async clk */ |
| 174 | if (priv->speed > SPI_MAX_SYNC_CLOCK) |
| 175 | set |= SPI_PFL_SIG_ASYNCIN_MASK; |
| 176 | |
| 177 | clrsetbits_32(priv->regs + SPI_PFL_SIG_REG(plat->cs), clr, set); |
| 178 | |
| 179 | /* global control */ |
| 180 | set = 0; |
| 181 | clr = 0; |
| 182 | |
| 183 | if (priv->cs_pols & BIT(plat->cs)) |
| 184 | set |= BIT(plat->cs); |
| 185 | else |
| 186 | clr |= BIT(plat->cs); |
| 187 | |
| 188 | clrsetbits_32(priv->regs + SPI_CTL_REG, clr, set); |
| 189 | } |
| 190 | |
| 191 | static void bcmbca_hsspi_activate_cs(struct bcmbca_hsspi_priv *priv, |
| 192 | struct dm_spi_slave_plat *plat) |
| 193 | { |
| 194 | u32 val; |
| 195 | |
| 196 | /* set the override bit */ |
| 197 | val = readl(priv->spim_ctrl); |
| 198 | val |= BIT(plat->cs + SPIM_CTRL_CS_OVERRIDE_SEL_SHIFT); |
| 199 | writel(val, priv->spim_ctrl); |
| 200 | } |
| 201 | |
| 202 | static void bcmbca_hsspi_deactivate_cs(struct bcmbca_hsspi_priv *priv, |
| 203 | struct dm_spi_slave_plat *plat) |
| 204 | { |
| 205 | u32 val; |
| 206 | |
| 207 | /* clear the cs override bit */ |
| 208 | val = readl(priv->spim_ctrl); |
| 209 | val &= ~BIT(plat->cs + SPIM_CTRL_CS_OVERRIDE_SEL_SHIFT); |
| 210 | writel(val, priv->spim_ctrl); |
| 211 | } |
| 212 | |
| 213 | static int bcmbca_hsspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 214 | const void *dout, void *din, unsigned long flags) |
| 215 | { |
| 216 | struct bcmbca_hsspi_priv *priv = dev_get_priv(dev->parent); |
| 217 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
| 218 | size_t data_bytes = bitlen / 8; |
| 219 | size_t step_size = HSSPI_FIFO_SIZE; |
| 220 | u16 opcode = 0; |
| 221 | u32 val = SPI_PFL_MODE_FILL_MASK; |
| 222 | const u8 *tx = dout; |
| 223 | u8 *rx = din; |
| 224 | u32 cs_act = 0; |
| 225 | |
| 226 | if (flags & SPI_XFER_BEGIN) |
| 227 | bcmbca_hsspi_setup_clock(priv, plat); |
| 228 | |
| 229 | /* fifo operation */ |
| 230 | if (tx && rx) |
| 231 | opcode = HSSPI_FIFO_OP_READ_WRITE; |
| 232 | else if (rx) |
| 233 | opcode = HSSPI_FIFO_OP_CODE_R; |
| 234 | else if (tx) |
| 235 | opcode = HSSPI_FIFO_OP_CODE_W; |
| 236 | |
| 237 | if (opcode != HSSPI_FIFO_OP_CODE_R) |
| 238 | step_size -= HSSPI_FIFO_OP_SIZE; |
| 239 | |
| 240 | /* dual mode */ |
| 241 | if ((opcode == HSSPI_FIFO_OP_CODE_R && (plat->mode & SPI_RX_DUAL)) || |
| 242 | (opcode == HSSPI_FIFO_OP_CODE_W && (plat->mode & SPI_TX_DUAL))) { |
| 243 | opcode |= HSSPI_FIFO_OP_MBIT_MASK; |
| 244 | |
| 245 | /* profile mode */ |
| 246 | if (plat->mode & SPI_RX_DUAL) |
| 247 | val |= SPI_PFL_MODE_MDRDSZ_MASK; |
| 248 | if (plat->mode & SPI_TX_DUAL) |
| 249 | val |= SPI_PFL_MODE_MDWRSZ_MASK; |
| 250 | } |
| 251 | |
| 252 | if (plat->mode & SPI_3WIRE) |
| 253 | val |= SPI_PFL_MODE_3WIRE_MASK; |
| 254 | writel(val, priv->regs + SPI_PFL_MODE_REG(plat->cs)); |
| 255 | |
| 256 | /* transfer loop */ |
| 257 | while (data_bytes > 0) { |
| 258 | size_t curr_step = min(step_size, data_bytes); |
| 259 | int ret; |
| 260 | |
| 261 | /* copy tx data */ |
| 262 | if (tx) { |
| 263 | memcpy_toio(priv->regs + HSSPI_FIFO_BASE + |
| 264 | HSSPI_FIFO_OP_SIZE, tx, curr_step); |
| 265 | tx += curr_step; |
| 266 | } |
| 267 | |
| 268 | /* set fifo operation */ |
| 269 | writew(cpu_to_be16(opcode | (curr_step & HSSPI_FIFO_OP_BYTES_MASK)), |
| 270 | priv->regs + HSSPI_FIFO_OP_REG); |
| 271 | |
| 272 | /* make sure we keep cs active until spi transfer is done */ |
| 273 | if (!cs_act) { |
| 274 | bcmbca_hsspi_activate_cs(priv, plat); |
| 275 | cs_act = 1; |
| 276 | } |
| 277 | |
| 278 | /* issue the transfer */ |
| 279 | val = SPI_CMD_OP_START; |
| 280 | val |= (plat->cs << SPI_CMD_PFL_SHIFT) & |
| 281 | SPI_CMD_PFL_MASK; |
| 282 | val |= (plat->cs << SPI_CMD_SLAVE_SHIFT) & |
| 283 | SPI_CMD_SLAVE_MASK; |
| 284 | writel(val, priv->regs + SPI_CMD_REG); |
| 285 | |
| 286 | /* wait for completion */ |
| 287 | ret = wait_for_bit_32(priv->regs + SPI_STAT_REG, |
| 288 | SPI_STAT_SRCBUSY_MASK, false, |
| 289 | 1000, false); |
| 290 | if (ret) { |
| 291 | bcmbca_hsspi_deactivate_cs(priv, plat); |
| 292 | dev_err(dev, "interrupt timeout\n"); |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | data_bytes -= curr_step; |
| 297 | if ((flags & SPI_XFER_END) && !data_bytes) |
| 298 | bcmbca_hsspi_deactivate_cs(priv, plat); |
| 299 | |
| 300 | /* copy rx data */ |
| 301 | if (rx) { |
| 302 | memcpy_fromio(rx, priv->regs + HSSPI_FIFO_BASE, |
| 303 | curr_step); |
| 304 | rx += curr_step; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static const struct dm_spi_ops bcmbca_hsspi_ops = { |
| 312 | .cs_info = bcmbca_hsspi_cs_info, |
| 313 | .set_mode = bcmbca_hsspi_set_mode, |
| 314 | .set_speed = bcmbca_hsspi_set_speed, |
| 315 | .xfer = bcmbca_hsspi_xfer, |
| 316 | }; |
| 317 | |
| 318 | static const struct udevice_id bcmbca_hsspi_ids[] = { |
| 319 | { .compatible = "brcm,bcmbca-hsspi-v1.1", }, |
| 320 | { /* sentinel */ } |
| 321 | }; |
| 322 | |
| 323 | static int bcmbca_hsspi_child_pre_probe(struct udevice *dev) |
| 324 | { |
| 325 | struct bcmbca_hsspi_priv *priv = dev_get_priv(dev->parent); |
| 326 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
| 327 | u32 val; |
| 328 | |
| 329 | /* check cs */ |
| 330 | if (plat->cs >= priv->num_cs) { |
| 331 | dev_err(dev, "no cs %u\n", plat->cs); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
| 335 | /* cs polarity */ |
| 336 | if (plat->mode & SPI_CS_HIGH) |
| 337 | priv->cs_pols |= BIT(plat->cs); |
| 338 | else |
| 339 | priv->cs_pols &= ~BIT(plat->cs); |
| 340 | |
| 341 | /* set the polarity to spim cs register */ |
| 342 | val = readl(priv->spim_ctrl); |
| 343 | val &= ~BIT(plat->cs + SPIM_CTRL_CS_OVERRIDE_VAL_SHIFT); |
| 344 | if (priv->cs_pols & BIT(plat->cs)) |
| 345 | val |= BIT(plat->cs + SPIM_CTRL_CS_OVERRIDE_VAL_SHIFT); |
| 346 | writel(val, priv->spim_ctrl); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int bcmbca_hsspi_probe(struct udevice *dev) |
| 352 | { |
| 353 | struct bcmbca_hsspi_priv *priv = dev_get_priv(dev); |
| 354 | struct clk clk; |
| 355 | int ret; |
| 356 | |
| 357 | priv->regs = dev_remap_addr_name(dev, "hsspi"); |
| 358 | if (!priv->regs) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | priv->spim_ctrl = dev_remap_addr_name(dev, "spim-ctrl"); |
| 362 | if (!priv->spim_ctrl) { |
| 363 | dev_err(dev, "misc spim ctrl register not defined in dts!\n"); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | priv->num_cs = dev_read_u32_default(dev, "num-cs", 8); |
| 368 | |
| 369 | /* enable clock */ |
| 370 | ret = clk_get_by_name(dev, "hsspi", &clk); |
| 371 | if (ret < 0) |
| 372 | return ret; |
| 373 | |
| 374 | ret = clk_enable(&clk); |
| 375 | if (ret < 0 && ret != -ENOSYS) |
| 376 | return ret; |
| 377 | |
| 378 | clk_free(&clk); |
| 379 | |
| 380 | /* get clock rate */ |
| 381 | ret = clk_get_by_name(dev, "pll", &clk); |
| 382 | if (ret < 0 && ret != -ENOSYS) |
| 383 | return ret; |
| 384 | |
| 385 | priv->clk_rate = clk_get_rate(&clk); |
| 386 | |
| 387 | clk_free(&clk); |
| 388 | |
| 389 | /* initialize hardware */ |
| 390 | writel(0, priv->regs + SPI_IR_MASK_REG); |
| 391 | |
| 392 | /* clear pending interrupts */ |
| 393 | writel(SPI_IR_CLEAR_ALL, priv->regs + SPI_IR_STAT_REG); |
| 394 | |
| 395 | /* enable clk gate */ |
| 396 | setbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_GATE_MASK); |
| 397 | |
| 398 | /* read default cs polarities */ |
| 399 | priv->cs_pols = readl(priv->regs + SPI_CTL_REG) & |
| 400 | SPI_CTL_CS_POL_MASK; |
| 401 | |
| 402 | dev_info(dev, "Broadcom BCMBCA HS SPI bus driver\n"); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | U_BOOT_DRIVER(bcmbca_hsspi) = { |
| 407 | .name = "bcmbca_hsspi", |
| 408 | .id = UCLASS_SPI, |
| 409 | .of_match = bcmbca_hsspi_ids, |
| 410 | .ops = &bcmbca_hsspi_ops, |
| 411 | .priv_auto = sizeof(struct bcmbca_hsspi_priv), |
| 412 | .child_pre_probe = bcmbca_hsspi_child_pre_probe, |
| 413 | .probe = bcmbca_hsspi_probe, |
| 414 | }; |