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