Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2003 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc. |
| 7 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 8 | * Chao Fu (B44548@freescale.com) |
| 9 | * Haikun Wang (B53464@freescale.com) |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 10 | */ |
Simon Glass | 51a3ec3 | 2017-05-17 17:18:07 -0600 | [diff] [blame] | 11 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Vladimir Oltean | 2926f57 | 2020-05-04 11:24:26 +0300 | [diff] [blame] | 13 | #include <linux/math64.h> |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 17 | #include <spi.h> |
| 18 | #include <malloc.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <fdtdec.h> |
| 21 | #ifndef CONFIG_M68K |
| 22 | #include <asm/arch/clock.h> |
| 23 | #endif |
| 24 | #include <fsl_dspi.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 26 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 27 | #include <linux/printk.h> |
Igor Prusov | c3421ea | 2023-11-09 20:10:04 +0300 | [diff] [blame] | 28 | #include <linux/time.h> |
Vladimir Oltean | 2926f57 | 2020-05-04 11:24:26 +0300 | [diff] [blame] | 29 | |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 32 | /* fsl_dspi_plat flags */ |
Jagan Teki | c97ca92 | 2015-10-23 01:37:18 +0530 | [diff] [blame] | 33 | #define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0) |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 34 | |
| 35 | /* idle data value */ |
| 36 | #define DSPI_IDLE_VAL 0x0 |
| 37 | |
| 38 | /* max chipselect signals number */ |
| 39 | #define FSL_DSPI_MAX_CHIPSELECT 6 |
| 40 | |
| 41 | /* default SCK frequency, unit: HZ */ |
| 42 | #define FSL_DSPI_DEFAULT_SCK_FREQ 10000000 |
| 43 | |
| 44 | /* tx/rx data wait timeout value, unit: us */ |
| 45 | #define DSPI_TXRX_WAIT_TIMEOUT 1000000 |
| 46 | |
| 47 | /* CTAR register pre-configure value */ |
| 48 | #define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \ |
| 49 | DSPI_CTAR_PCSSCK_1CLK | \ |
| 50 | DSPI_CTAR_PASC(0) | \ |
| 51 | DSPI_CTAR_PDT(0) | \ |
| 52 | DSPI_CTAR_CSSCK(0) | \ |
| 53 | DSPI_CTAR_ASC(0) | \ |
| 54 | DSPI_CTAR_DT(0)) |
| 55 | |
| 56 | /* CTAR register pre-configure mask */ |
| 57 | #define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \ |
| 58 | DSPI_CTAR_PCSSCK(3) | \ |
| 59 | DSPI_CTAR_PASC(3) | \ |
| 60 | DSPI_CTAR_PDT(3) | \ |
| 61 | DSPI_CTAR_CSSCK(15) | \ |
| 62 | DSPI_CTAR_ASC(15) | \ |
| 63 | DSPI_CTAR_DT(15)) |
| 64 | |
| 65 | /** |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 66 | * struct fsl_dspi_plat - platform data for Freescale DSPI |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 67 | * |
| 68 | * @flags: Flags for DSPI DSPI_FLAG_... |
| 69 | * @speed_hz: Default SCK frequency |
| 70 | * @num_chipselect: Number of DSPI chipselect signals |
| 71 | * @regs_addr: Base address of DSPI registers |
| 72 | */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 73 | struct fsl_dspi_plat { |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 74 | uint flags; |
| 75 | uint speed_hz; |
| 76 | uint num_chipselect; |
| 77 | fdt_addr_t regs_addr; |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * struct fsl_dspi_priv - private data for Freescale DSPI |
| 82 | * |
| 83 | * @flags: Flags for DSPI DSPI_FLAG_... |
| 84 | * @mode: SPI mode to use for slave device (see SPI mode flags) |
| 85 | * @mcr_val: MCR register configure value |
| 86 | * @bus_clk: DSPI input clk frequency |
| 87 | * @speed_hz: Default SCK frequency |
| 88 | * @charbit: How many bits in every transfer |
| 89 | * @num_chipselect: Number of DSPI chipselect signals |
| 90 | * @ctar_val: CTAR register configure value of per chipselect slave device |
| 91 | * @regs: Point to DSPI register structure for I/O access |
| 92 | */ |
| 93 | struct fsl_dspi_priv { |
| 94 | uint flags; |
| 95 | uint mode; |
| 96 | uint mcr_val; |
| 97 | uint bus_clk; |
| 98 | uint speed_hz; |
| 99 | uint charbit; |
| 100 | uint num_chipselect; |
| 101 | uint ctar_val[FSL_DSPI_MAX_CHIPSELECT]; |
| 102 | struct dspi *regs; |
| 103 | }; |
| 104 | |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 105 | __weak void cpu_dspi_port_conf(void) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | __weak int cpu_dspi_claim_bus(uint bus, uint cs) |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | __weak void cpu_dspi_release_bus(uint bus, uint cs) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | static uint dspi_read32(uint flags, uint *addr) |
| 119 | { |
| 120 | return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? |
| 121 | in_be32(addr) : in_le32(addr); |
| 122 | } |
| 123 | |
| 124 | static void dspi_write32(uint flags, uint *addr, uint val) |
| 125 | { |
| 126 | flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? |
| 127 | out_be32(addr, val) : out_le32(addr, val); |
| 128 | } |
| 129 | |
| 130 | static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt) |
| 131 | { |
| 132 | uint mcr_val; |
| 133 | |
| 134 | mcr_val = dspi_read32(priv->flags, &priv->regs->mcr); |
| 135 | |
| 136 | if (halt) |
| 137 | mcr_val |= DSPI_MCR_HALT; |
| 138 | else |
| 139 | mcr_val &= ~DSPI_MCR_HALT; |
| 140 | |
| 141 | dspi_write32(priv->flags, &priv->regs->mcr, mcr_val); |
| 142 | } |
| 143 | |
| 144 | static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val) |
| 145 | { |
| 146 | /* halt DSPI module */ |
| 147 | dspi_halt(priv, 1); |
| 148 | |
| 149 | dspi_write32(priv->flags, &priv->regs->mcr, cfg_val); |
| 150 | |
| 151 | /* resume module */ |
| 152 | dspi_halt(priv, 0); |
| 153 | |
| 154 | priv->mcr_val = cfg_val; |
| 155 | } |
| 156 | |
| 157 | static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv, |
| 158 | uint cs, uint state) |
| 159 | { |
| 160 | uint mcr_val; |
| 161 | |
| 162 | dspi_halt(priv, 1); |
| 163 | |
| 164 | mcr_val = dspi_read32(priv->flags, &priv->regs->mcr); |
| 165 | if (state & SPI_CS_HIGH) |
| 166 | /* CSx inactive state is low */ |
| 167 | mcr_val &= ~DSPI_MCR_PCSIS(cs); |
| 168 | else |
| 169 | /* CSx inactive state is high */ |
| 170 | mcr_val |= DSPI_MCR_PCSIS(cs); |
| 171 | dspi_write32(priv->flags, &priv->regs->mcr, mcr_val); |
| 172 | |
| 173 | dspi_halt(priv, 0); |
| 174 | } |
| 175 | |
| 176 | static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv, |
| 177 | uint cs, uint mode) |
| 178 | { |
| 179 | uint bus_setup; |
| 180 | |
| 181 | bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]); |
| 182 | |
| 183 | bus_setup &= ~DSPI_CTAR_SET_MODE_MASK; |
| 184 | bus_setup |= priv->ctar_val[cs]; |
| 185 | bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE); |
| 186 | |
| 187 | if (mode & SPI_CPOL) |
| 188 | bus_setup |= DSPI_CTAR_CPOL; |
| 189 | if (mode & SPI_CPHA) |
| 190 | bus_setup |= DSPI_CTAR_CPHA; |
| 191 | if (mode & SPI_LSB_FIRST) |
| 192 | bus_setup |= DSPI_CTAR_LSBFE; |
| 193 | |
| 194 | dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup); |
| 195 | |
| 196 | priv->charbit = |
| 197 | ((dspi_read32(priv->flags, &priv->regs->ctar[0]) & |
| 198 | DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv) |
| 204 | { |
| 205 | uint mcr_val; |
| 206 | |
| 207 | dspi_halt(priv, 1); |
| 208 | mcr_val = dspi_read32(priv->flags, &priv->regs->mcr); |
| 209 | /* flush RX and TX FIFO */ |
| 210 | mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF); |
| 211 | dspi_write32(priv->flags, &priv->regs->mcr, mcr_val); |
| 212 | dspi_halt(priv, 0); |
| 213 | } |
| 214 | |
| 215 | static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data) |
| 216 | { |
| 217 | int timeout = DSPI_TXRX_WAIT_TIMEOUT; |
| 218 | |
| 219 | /* wait for empty entries in TXFIFO or timeout */ |
| 220 | while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 && |
| 221 | timeout--) |
| 222 | udelay(1); |
| 223 | |
| 224 | if (timeout >= 0) |
| 225 | dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data)); |
| 226 | else |
| 227 | debug("dspi_tx: waiting timeout!\n"); |
| 228 | } |
| 229 | |
| 230 | static u16 dspi_rx(struct fsl_dspi_priv *priv) |
| 231 | { |
| 232 | int timeout = DSPI_TXRX_WAIT_TIMEOUT; |
| 233 | |
| 234 | /* wait for valid entries in RXFIFO or timeout */ |
| 235 | while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 && |
| 236 | timeout--) |
| 237 | udelay(1); |
| 238 | |
| 239 | if (timeout >= 0) |
| 240 | return (u16)DSPI_RFR_RXDATA( |
| 241 | dspi_read32(priv->flags, &priv->regs->rfr)); |
| 242 | else { |
| 243 | debug("dspi_rx: waiting timeout!\n"); |
| 244 | return (u16)(~0); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen, |
| 249 | const void *dout, void *din, unsigned long flags) |
| 250 | { |
| 251 | u16 *spi_rd16 = NULL, *spi_wr16 = NULL; |
| 252 | u8 *spi_rd = NULL, *spi_wr = NULL; |
| 253 | static u32 ctrl; |
| 254 | uint len = bitlen >> 3; |
| 255 | |
| 256 | if (priv->charbit == 16) { |
| 257 | bitlen >>= 1; |
| 258 | spi_wr16 = (u16 *)dout; |
| 259 | spi_rd16 = (u16 *)din; |
| 260 | } else { |
| 261 | spi_wr = (u8 *)dout; |
| 262 | spi_rd = (u8 *)din; |
| 263 | } |
| 264 | |
| 265 | if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN) |
| 266 | ctrl |= DSPI_TFR_CONT; |
| 267 | |
| 268 | ctrl = ctrl & DSPI_TFR_CONT; |
| 269 | ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs); |
| 270 | |
| 271 | if (len > 1) { |
| 272 | int tmp_len = len - 1; |
| 273 | while (tmp_len--) { |
Jared Bents | 0d8cdcb | 2019-03-22 09:46:52 -0500 | [diff] [blame] | 274 | if ((dout != NULL) && (din != NULL)) { |
| 275 | if (priv->charbit == 16) { |
| 276 | dspi_tx(priv, ctrl, *spi_wr16++); |
| 277 | *spi_rd16++ = dspi_rx(priv); |
| 278 | } |
| 279 | else { |
| 280 | dspi_tx(priv, ctrl, *spi_wr++); |
| 281 | *spi_rd++ = dspi_rx(priv); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | else if (dout != NULL) { |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 286 | if (priv->charbit == 16) |
| 287 | dspi_tx(priv, ctrl, *spi_wr16++); |
| 288 | else |
| 289 | dspi_tx(priv, ctrl, *spi_wr++); |
| 290 | dspi_rx(priv); |
| 291 | } |
| 292 | |
Jared Bents | 0d8cdcb | 2019-03-22 09:46:52 -0500 | [diff] [blame] | 293 | else if (din != NULL) { |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 294 | dspi_tx(priv, ctrl, DSPI_IDLE_VAL); |
| 295 | if (priv->charbit == 16) |
| 296 | *spi_rd16++ = dspi_rx(priv); |
| 297 | else |
| 298 | *spi_rd++ = dspi_rx(priv); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | len = 1; /* remaining byte */ |
| 303 | } |
| 304 | |
| 305 | if ((flags & SPI_XFER_END) == SPI_XFER_END) |
| 306 | ctrl &= ~DSPI_TFR_CONT; |
| 307 | |
| 308 | if (len) { |
Jared Bents | 0d8cdcb | 2019-03-22 09:46:52 -0500 | [diff] [blame] | 309 | if ((dout != NULL) && (din != NULL)) { |
| 310 | if (priv->charbit == 16) { |
| 311 | dspi_tx(priv, ctrl, *spi_wr16++); |
| 312 | *spi_rd16++ = dspi_rx(priv); |
| 313 | } |
| 314 | else { |
| 315 | dspi_tx(priv, ctrl, *spi_wr++); |
| 316 | *spi_rd++ = dspi_rx(priv); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | else if (dout != NULL) { |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 321 | if (priv->charbit == 16) |
| 322 | dspi_tx(priv, ctrl, *spi_wr16); |
| 323 | else |
| 324 | dspi_tx(priv, ctrl, *spi_wr); |
| 325 | dspi_rx(priv); |
| 326 | } |
| 327 | |
Jared Bents | 0d8cdcb | 2019-03-22 09:46:52 -0500 | [diff] [blame] | 328 | else if (din != NULL) { |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 329 | dspi_tx(priv, ctrl, DSPI_IDLE_VAL); |
| 330 | if (priv->charbit == 16) |
| 331 | *spi_rd16 = dspi_rx(priv); |
| 332 | else |
| 333 | *spi_rd = dspi_rx(priv); |
| 334 | } |
| 335 | } else { |
| 336 | /* dummy read */ |
| 337 | dspi_tx(priv, ctrl, DSPI_IDLE_VAL); |
| 338 | dspi_rx(priv); |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Calculate the divide value between input clk frequency and expected SCK frequency |
| 346 | * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br) |
| 347 | * Dbr: use default value 0 |
| 348 | * |
| 349 | * @pbr: return Baud Rate Prescaler value |
| 350 | * @br: return Baud Rate Scaler value |
| 351 | * @speed_hz: expected SCK frequency |
| 352 | * @clkrate: input clk frequency |
| 353 | */ |
| 354 | static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br, |
| 355 | int speed_hz, uint clkrate) |
| 356 | { |
| 357 | /* Valid baud rate pre-scaler values */ |
| 358 | int pbr_tbl[4] = {2, 3, 5, 7}; |
| 359 | int brs[16] = {2, 4, 6, 8, |
| 360 | 16, 32, 64, 128, |
| 361 | 256, 512, 1024, 2048, |
| 362 | 4096, 8192, 16384, 32768}; |
| 363 | int temp, i = 0, j = 0; |
| 364 | |
| 365 | temp = clkrate / speed_hz; |
| 366 | |
| 367 | for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++) |
| 368 | for (j = 0; j < ARRAY_SIZE(brs); j++) { |
| 369 | if (pbr_tbl[i] * brs[j] >= temp) { |
| 370 | *pbr = i; |
| 371 | *br = j; |
| 372 | return 0; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz); |
| 377 | debug("clkrate is %d, we use the max prescaler value.\n", clkrate); |
| 378 | |
| 379 | *pbr = ARRAY_SIZE(pbr_tbl) - 1; |
| 380 | *br = ARRAY_SIZE(brs) - 1; |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | |
Vladimir Oltean | 2926f57 | 2020-05-04 11:24:26 +0300 | [diff] [blame] | 384 | static void ns_delay_scale(unsigned char *psc, unsigned char *sc, int delay_ns, |
| 385 | unsigned long clkrate) |
| 386 | { |
| 387 | int scale_needed, scale, minscale = INT_MAX; |
| 388 | int pscale_tbl[4] = {1, 3, 5, 7}; |
| 389 | u32 remainder; |
| 390 | int i, j; |
| 391 | |
| 392 | scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC, |
| 393 | &remainder); |
| 394 | if (remainder) |
| 395 | scale_needed++; |
| 396 | |
| 397 | for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++) |
| 398 | for (j = 0; j <= DSPI_CTAR_SCALE_BITS; j++) { |
| 399 | scale = pscale_tbl[i] * (2 << j); |
| 400 | if (scale >= scale_needed) { |
| 401 | if (scale < minscale) { |
| 402 | minscale = scale; |
| 403 | *psc = i; |
| 404 | *sc = j; |
| 405 | } |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (minscale == INT_MAX) { |
| 411 | pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value", |
| 412 | delay_ns, clkrate); |
| 413 | *psc = ARRAY_SIZE(pscale_tbl) - 1; |
| 414 | *sc = DSPI_CTAR_SCALE_BITS; |
| 415 | } |
| 416 | } |
| 417 | |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 418 | static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed) |
| 419 | { |
| 420 | int ret; |
| 421 | uint bus_setup; |
| 422 | int best_i, best_j, bus_clk; |
| 423 | |
| 424 | bus_clk = priv->bus_clk; |
| 425 | |
| 426 | debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n", |
| 427 | speed, bus_clk); |
| 428 | |
| 429 | bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]); |
| 430 | bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf)); |
| 431 | |
| 432 | ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk); |
| 433 | if (ret) { |
| 434 | speed = priv->speed_hz; |
| 435 | debug("DSPI set_speed use default SCK rate %u.\n", speed); |
| 436 | fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk); |
| 437 | } |
| 438 | |
| 439 | bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j)); |
| 440 | dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup); |
| 441 | |
| 442 | priv->speed_hz = speed; |
| 443 | |
| 444 | return 0; |
| 445 | } |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 446 | |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 447 | static int fsl_dspi_child_pre_probe(struct udevice *dev) |
| 448 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 449 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 450 | struct fsl_dspi_priv *priv = dev_get_priv(dev->parent); |
Vladimir Oltean | 2926f57 | 2020-05-04 11:24:26 +0300 | [diff] [blame] | 451 | u32 cs_sck_delay = 0, sck_cs_delay = 0; |
| 452 | unsigned char pcssck = 0, cssck = 0; |
| 453 | unsigned char pasc = 0, asc = 0; |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 454 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 455 | if (slave_plat->cs[0] >= priv->num_chipselect) { |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 456 | debug("DSPI invalid chipselect number %d(max %d)!\n", |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 457 | slave_plat->cs[0], priv->num_chipselect - 1); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 458 | return -EINVAL; |
| 459 | } |
| 460 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 461 | ofnode_read_u32(dev_ofnode(dev), "fsl,spi-cs-sck-delay", |
| 462 | &cs_sck_delay); |
| 463 | ofnode_read_u32(dev_ofnode(dev), "fsl,spi-sck-cs-delay", |
| 464 | &sck_cs_delay); |
Vladimir Oltean | 2926f57 | 2020-05-04 11:24:26 +0300 | [diff] [blame] | 465 | |
| 466 | /* Set PCS to SCK delay scale values */ |
| 467 | ns_delay_scale(&pcssck, &cssck, cs_sck_delay, priv->bus_clk); |
| 468 | |
| 469 | /* Set After SCK delay scale values */ |
| 470 | ns_delay_scale(&pasc, &asc, sck_cs_delay, priv->bus_clk); |
| 471 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 472 | priv->ctar_val[slave_plat->cs[0]] = DSPI_CTAR_DEFAULT_VALUE | |
Vladimir Oltean | 2926f57 | 2020-05-04 11:24:26 +0300 | [diff] [blame] | 473 | DSPI_CTAR_PCSSCK(pcssck) | |
| 474 | DSPI_CTAR_PASC(pasc); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 475 | |
| 476 | debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n", |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 477 | slave_plat->cs[0], slave_plat->max_hz, slave_plat->mode); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int fsl_dspi_probe(struct udevice *bus) |
| 483 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 484 | struct fsl_dspi_plat *plat = dev_get_plat(bus); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 485 | struct fsl_dspi_priv *priv = dev_get_priv(bus); |
| 486 | struct dm_spi_bus *dm_spi_bus; |
| 487 | uint mcr_cfg_val; |
| 488 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 489 | dm_spi_bus = dev_get_uclass_priv(bus); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 490 | |
Pengfei Fan | 746271d | 2022-12-09 09:39:50 +0800 | [diff] [blame] | 491 | /* cpu special pin muxing configure */ |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 492 | cpu_dspi_port_conf(); |
| 493 | |
| 494 | /* get input clk frequency */ |
| 495 | priv->regs = (struct dspi *)plat->regs_addr; |
| 496 | priv->flags = plat->flags; |
| 497 | #ifdef CONFIG_M68K |
| 498 | priv->bus_clk = gd->bus_clk; |
| 499 | #else |
| 500 | priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK); |
| 501 | #endif |
| 502 | priv->num_chipselect = plat->num_chipselect; |
| 503 | priv->speed_hz = plat->speed_hz; |
| 504 | /* frame data length in bits, default 8bits */ |
| 505 | priv->charbit = 8; |
| 506 | |
| 507 | dm_spi_bus->max_hz = plat->speed_hz; |
| 508 | |
| 509 | /* default: all CS signals inactive state is high */ |
| 510 | mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK | |
| 511 | DSPI_MCR_CRXF | DSPI_MCR_CTXF; |
| 512 | fsl_dspi_init_mcr(priv, mcr_cfg_val); |
| 513 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 514 | debug("%s probe done, bus-num %d.\n", bus->name, dev_seq(bus)); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int fsl_dspi_claim_bus(struct udevice *dev) |
| 520 | { |
| 521 | uint sr_val; |
| 522 | struct fsl_dspi_priv *priv; |
| 523 | struct udevice *bus = dev->parent; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 524 | struct dm_spi_slave_plat *slave_plat = |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 525 | dev_get_parent_plat(dev); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 526 | |
| 527 | priv = dev_get_priv(bus); |
| 528 | |
Robert P. J. Day | c5b1e5d | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 529 | /* processor special preparation work */ |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 530 | cpu_dspi_claim_bus(dev_seq(bus), slave_plat->cs[0]); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 531 | |
| 532 | /* configure transfer mode */ |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 533 | fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs[0], priv->mode); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 534 | |
| 535 | /* configure active state of CSX */ |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 536 | fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs[0], |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 537 | priv->mode); |
| 538 | |
| 539 | fsl_dspi_clr_fifo(priv); |
| 540 | |
| 541 | /* check module TX and RX status */ |
| 542 | sr_val = dspi_read32(priv->flags, &priv->regs->sr); |
| 543 | if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) { |
| 544 | debug("DSPI RX/TX not ready!\n"); |
| 545 | return -EIO; |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static int fsl_dspi_release_bus(struct udevice *dev) |
| 552 | { |
| 553 | struct udevice *bus = dev->parent; |
| 554 | struct fsl_dspi_priv *priv = dev_get_priv(bus); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 555 | struct dm_spi_slave_plat *slave_plat = |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 556 | dev_get_parent_plat(dev); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 557 | |
| 558 | /* halt module */ |
| 559 | dspi_halt(priv, 1); |
| 560 | |
| 561 | /* processor special release work */ |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 562 | cpu_dspi_release_bus(dev_seq(bus), slave_plat->cs[0]); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * This function doesn't do anything except help with debugging |
| 569 | */ |
| 570 | static int fsl_dspi_bind(struct udevice *bus) |
| 571 | { |
Simon Glass | c4222cd | 2020-12-16 21:20:19 -0700 | [diff] [blame] | 572 | debug("%s assigned seq %d.\n", bus->name, dev_seq(bus)); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 576 | static int fsl_dspi_of_to_plat(struct udevice *bus) |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 577 | { |
| 578 | fdt_addr_t addr; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 579 | struct fsl_dspi_plat *plat = dev_get_plat(bus); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 580 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 581 | int node = dev_of_offset(bus); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 582 | |
| 583 | if (fdtdec_get_bool(blob, node, "big-endian")) |
| 584 | plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG; |
| 585 | |
Michael Walle | 2de392c | 2021-10-13 18:14:18 +0200 | [diff] [blame] | 586 | plat->num_chipselect = fdtdec_get_int(blob, node, |
| 587 | "spi-num-chipselects", |
| 588 | FSL_DSPI_MAX_CHIPSELECT); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 589 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 590 | addr = dev_read_addr(bus); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 591 | if (addr == FDT_ADDR_T_NONE) { |
| 592 | debug("DSPI: Can't get base address or size\n"); |
| 593 | return -ENOMEM; |
| 594 | } |
| 595 | plat->regs_addr = addr; |
| 596 | |
| 597 | plat->speed_hz = fdtdec_get_int(blob, |
| 598 | node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ); |
| 599 | |
Pengfei Fan | 746271d | 2022-12-09 09:39:50 +0800 | [diff] [blame] | 600 | debug("DSPI: regs=%pa, max-frequency=%d, endianness=%s, num-cs=%d\n", |
York Sun | aa5b66c | 2015-08-03 12:02:05 -0700 | [diff] [blame] | 601 | &plat->regs_addr, plat->speed_hz, |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 602 | plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le", |
| 603 | plat->num_chipselect); |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 609 | const void *dout, void *din, unsigned long flags) |
| 610 | { |
| 611 | struct fsl_dspi_priv *priv; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 612 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 613 | struct udevice *bus; |
| 614 | |
| 615 | bus = dev->parent; |
| 616 | priv = dev_get_priv(bus); |
| 617 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 618 | return dspi_xfer(priv, slave_plat->cs[0], bitlen, dout, din, flags); |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static int fsl_dspi_set_speed(struct udevice *bus, uint speed) |
| 622 | { |
| 623 | struct fsl_dspi_priv *priv = dev_get_priv(bus); |
| 624 | |
| 625 | return fsl_dspi_cfg_speed(priv, speed); |
| 626 | } |
| 627 | |
| 628 | static int fsl_dspi_set_mode(struct udevice *bus, uint mode) |
| 629 | { |
| 630 | struct fsl_dspi_priv *priv = dev_get_priv(bus); |
| 631 | |
| 632 | debug("DSPI set_mode: mode 0x%x.\n", mode); |
| 633 | |
| 634 | /* |
| 635 | * We store some chipselect special configure value in priv->ctar_val, |
| 636 | * and we can't get the correct chipselect number here, |
| 637 | * so just store mode value. |
| 638 | * Do really configuration when claim_bus. |
| 639 | */ |
| 640 | priv->mode = mode; |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | static const struct dm_spi_ops fsl_dspi_ops = { |
| 646 | .claim_bus = fsl_dspi_claim_bus, |
| 647 | .release_bus = fsl_dspi_release_bus, |
| 648 | .xfer = fsl_dspi_xfer, |
| 649 | .set_speed = fsl_dspi_set_speed, |
| 650 | .set_mode = fsl_dspi_set_mode, |
| 651 | }; |
| 652 | |
| 653 | static const struct udevice_id fsl_dspi_ids[] = { |
| 654 | { .compatible = "fsl,vf610-dspi" }, |
Michael Walle | 78692a7 | 2021-10-13 18:14:17 +0200 | [diff] [blame] | 655 | { .compatible = "fsl,ls1021a-v1.0-dspi" }, |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 656 | { } |
| 657 | }; |
| 658 | |
| 659 | U_BOOT_DRIVER(fsl_dspi) = { |
| 660 | .name = "fsl_dspi", |
| 661 | .id = UCLASS_SPI, |
| 662 | .of_match = fsl_dspi_ids, |
| 663 | .ops = &fsl_dspi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 664 | .of_to_plat = fsl_dspi_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 665 | .plat_auto = sizeof(struct fsl_dspi_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 666 | .priv_auto = sizeof(struct fsl_dspi_priv), |
Haikun.Wang@freescale.com | a28e291 | 2015-03-24 22:03:58 +0800 | [diff] [blame] | 667 | .probe = fsl_dspi_probe, |
| 668 | .child_pre_probe = fsl_dspi_child_pre_probe, |
| 669 | .bind = fsl_dspi_bind, |
| 670 | }; |