Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * (C) Copyright 2000-2003 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * Copyright (C) 2004-2009 Freescale Semiconductor, Inc. |
| 8 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 9 | * |
| 10 | * Support for DM and DT, non-DM code removed. |
| 11 | * Copyright (C) 2018 Angelo Dureghello <angelo@sysam.it> |
| 12 | * |
| 13 | * TODO: fsl_dspi.c should work as a driver for the DSPI module. |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 17 | #include <dm.h> |
| 18 | #include <dm/platform_data/spi_coldfire.h> |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 19 | #include <spi.h> |
| 20 | #include <malloc.h> |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 21 | #include <asm/coldfire/dspi.h> |
| 22 | #include <asm/io.h> |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 23 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 24 | struct coldfire_spi_priv { |
| 25 | struct dspi *regs; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 26 | uint baudrate; |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 27 | int mode; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 28 | int charbit; |
| 29 | }; |
| 30 | |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Wolfgang Wegner | b97e0cd | 2010-04-23 05:43:12 +0000 | [diff] [blame] | 33 | #ifndef CONFIG_SPI_IDLE_VAL |
| 34 | #if defined(CONFIG_SPI_MMC) |
| 35 | #define CONFIG_SPI_IDLE_VAL 0xFFFF |
| 36 | #else |
| 37 | #define CONFIG_SPI_IDLE_VAL 0x0 |
| 38 | #endif |
| 39 | #endif |
| 40 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 41 | /* |
| 42 | * DSPI specific mode |
| 43 | * |
| 44 | * bit 31 - 28: Transfer size 3 to 16 bits |
| 45 | * 27 - 26: PCS to SCK delay prescaler |
| 46 | * 25 - 24: After SCK delay prescaler |
| 47 | * 23 - 22: Delay after transfer prescaler |
| 48 | * 21 : Allow overwrite for bit 31-22 and bit 20-8 |
| 49 | * 20 : Double baud rate |
| 50 | * 19 - 16: PCS to SCK delay scaler |
| 51 | * 15 - 12: After SCK delay scaler |
| 52 | * 11 - 8: Delay after transfer scaler |
| 53 | * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST |
| 54 | */ |
| 55 | #define SPI_MODE_MOD 0x00200000 |
| 56 | #define SPI_MODE_DBLRATE 0x00100000 |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 57 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 58 | #define SPI_MODE_XFER_SZ_MASK 0xf0000000 |
| 59 | #define SPI_MODE_DLY_PRE_MASK 0x0fc00000 |
| 60 | #define SPI_MODE_DLY_SCA_MASK 0x000fff00 |
| 61 | |
| 62 | #define MCF_FRM_SZ_16BIT DSPI_CTAR_TRSZ(0xf) |
| 63 | #define MCF_DSPI_SPEED_BESTMATCH 0x7FFFFFFF |
| 64 | #define MCF_DSPI_MAX_CTAR_REGS 8 |
| 65 | |
| 66 | /* Default values */ |
| 67 | #define MCF_DSPI_DEFAULT_SCK_FREQ 10000000 |
| 68 | #define MCF_DSPI_DEFAULT_MAX_CS 4 |
| 69 | #define MCF_DSPI_DEFAULT_MODE 0 |
| 70 | |
| 71 | #define MCF_DSPI_DEFAULT_CTAR (DSPI_CTAR_TRSZ(7) | \ |
| 72 | DSPI_CTAR_PCSSCK_1CLK | \ |
| 73 | DSPI_CTAR_PASC(0) | \ |
| 74 | DSPI_CTAR_PDT(0) | \ |
| 75 | DSPI_CTAR_CSSCK(0) | \ |
| 76 | DSPI_CTAR_ASC(0) | \ |
| 77 | DSPI_CTAR_DT(1) | \ |
| 78 | DSPI_CTAR_BR(6)) |
| 79 | |
| 80 | #define MCF_CTAR_MODE_MASK (MCF_FRM_SZ_16BIT | \ |
| 81 | DSPI_CTAR_PCSSCK(3) | \ |
| 82 | DSPI_CTAR_PASC_7CLK | \ |
| 83 | DSPI_CTAR_PDT(3) | \ |
| 84 | DSPI_CTAR_CSSCK(0x0f) | \ |
| 85 | DSPI_CTAR_ASC(0x0f) | \ |
| 86 | DSPI_CTAR_DT(0x0f)) |
| 87 | |
| 88 | #define setup_ctrl(ctrl, cs) ((ctrl & 0xFF000000) | ((1 << cs) << 16)) |
| 89 | |
| 90 | static inline void cfspi_tx(struct coldfire_spi_priv *cfspi, |
| 91 | u32 ctrl, u16 data) |
Axel Lin | bb16627 | 2015-02-21 00:17:47 +0800 | [diff] [blame] | 92 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Need to check fifo level here |
| 95 | */ |
| 96 | while ((readl(&cfspi->regs->sr) & 0x0000F000) >= 0x4000) |
| 97 | ; |
| 98 | |
| 99 | writel(ctrl | data, &cfspi->regs->tfr); |
Axel Lin | bb16627 | 2015-02-21 00:17:47 +0800 | [diff] [blame] | 100 | } |
| 101 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 102 | static inline u16 cfspi_rx(struct coldfire_spi_priv *cfspi) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 103 | { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 104 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 105 | while ((readl(&cfspi->regs->sr) & 0x000000F0) == 0) |
| 106 | ; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 107 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 108 | return readw(&cfspi->regs->rfr); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 111 | static int coldfire_spi_claim_bus(struct udevice *dev) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 112 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 113 | struct udevice *bus = dev->parent; |
| 114 | struct coldfire_spi_priv *cfspi = dev_get_priv(bus); |
| 115 | struct dspi *dspi = cfspi->regs; |
| 116 | struct dm_spi_slave_platdata *slave_plat = |
| 117 | dev_get_parent_platdata(dev); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 118 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 119 | if ((in_be32(&dspi->sr) & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) |
| 120 | return -1; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 121 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 122 | /* Clear FIFO and resume transfer */ |
| 123 | clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF); |
| 124 | |
| 125 | dspi_chip_select(slave_plat->cs); |
| 126 | |
| 127 | return 0; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 130 | static int coldfire_spi_release_bus(struct udevice *dev) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 131 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 132 | struct udevice *bus = dev->parent; |
| 133 | struct coldfire_spi_priv *cfspi = dev_get_priv(bus); |
| 134 | struct dspi *dspi = cfspi->regs; |
| 135 | struct dm_spi_slave_platdata *slave_plat = |
| 136 | dev_get_parent_platdata(dev); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 137 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 138 | /* Clear FIFO */ |
| 139 | clrbits_be32(&dspi->mcr, DSPI_MCR_CTXF | DSPI_MCR_CRXF); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 140 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 141 | dspi_chip_unselect(slave_plat->cs); |
| 142 | |
| 143 | return 0; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 146 | static int coldfire_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 147 | const void *dout, void *din, |
| 148 | unsigned long flags) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 149 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 150 | struct udevice *bus = dev_get_parent(dev); |
| 151 | struct coldfire_spi_priv *cfspi = dev_get_priv(bus); |
| 152 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 153 | u16 *spi_rd16 = NULL, *spi_wr16 = NULL; |
| 154 | u8 *spi_rd = NULL, *spi_wr = NULL; |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 155 | static u32 ctrl; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 156 | uint len = bitlen >> 3; |
| 157 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 158 | if (cfspi->charbit == 16) { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 159 | bitlen >>= 1; |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 160 | spi_wr16 = (u16 *)dout; |
| 161 | spi_rd16 = (u16 *)din; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 162 | } else { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 163 | spi_wr = (u8 *)dout; |
| 164 | spi_rd = (u8 *)din; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN) |
| 168 | ctrl |= DSPI_TFR_CONT; |
| 169 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 170 | ctrl = setup_ctrl(ctrl, slave_plat->cs); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 171 | |
| 172 | if (len > 1) { |
| 173 | int tmp_len = len - 1; |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 174 | |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 175 | while (tmp_len--) { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 176 | if (dout) { |
| 177 | if (cfspi->charbit == 16) |
| 178 | cfspi_tx(cfspi, ctrl, *spi_wr16++); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 179 | else |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 180 | cfspi_tx(cfspi, ctrl, *spi_wr++); |
| 181 | cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 184 | if (din) { |
| 185 | cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL); |
| 186 | if (cfspi->charbit == 16) |
| 187 | *spi_rd16++ = cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 188 | else |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 189 | *spi_rd++ = cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | len = 1; /* remaining byte */ |
| 194 | } |
| 195 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 196 | if (flags & SPI_XFER_END) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 197 | ctrl &= ~DSPI_TFR_CONT; |
| 198 | |
| 199 | if (len) { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 200 | if (dout) { |
| 201 | if (cfspi->charbit == 16) |
| 202 | cfspi_tx(cfspi, ctrl, *spi_wr16); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 203 | else |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 204 | cfspi_tx(cfspi, ctrl, *spi_wr); |
| 205 | cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 208 | if (din) { |
| 209 | cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL); |
| 210 | if (cfspi->charbit == 16) |
| 211 | *spi_rd16 = cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 212 | else |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 213 | *spi_rd = cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 214 | } |
| 215 | } else { |
| 216 | /* dummy read */ |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 217 | cfspi_tx(cfspi, ctrl, CONFIG_SPI_IDLE_VAL); |
| 218 | cfspi_rx(cfspi); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 224 | static int coldfire_spi_set_speed(struct udevice *bus, uint max_hz) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 225 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 226 | struct coldfire_spi_priv *cfspi = dev_get_priv(bus); |
| 227 | struct dspi *dspi = cfspi->regs; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 228 | int prescaler[] = { 2, 3, 5, 7 }; |
| 229 | int scaler[] = { |
| 230 | 2, 4, 6, 8, |
| 231 | 16, 32, 64, 128, |
| 232 | 256, 512, 1024, 2048, |
| 233 | 4096, 8192, 16384, 32768 |
| 234 | }; |
| 235 | int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0; |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 236 | int best_i, best_j, bestmatch = MCF_DSPI_SPEED_BESTMATCH, baud_speed; |
| 237 | u32 bus_setup; |
| 238 | |
| 239 | cfspi->baudrate = max_hz; |
| 240 | |
| 241 | /* Read current setup */ |
| 242 | bus_setup = readl(&dspi->ctar[bus->seq]); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 243 | |
| 244 | tmp = (prescaler[3] * scaler[15]); |
| 245 | /* Maximum and minimum baudrate it can handle */ |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 246 | if ((cfspi->baudrate > (gd->bus_clk >> 1)) || |
| 247 | (cfspi->baudrate < (gd->bus_clk / tmp))) { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 248 | printf("Exceed baudrate limitation: Max %d - Min %d\n", |
| 249 | (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp)); |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 250 | return -1; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* Activate Double Baud when it exceed 1/4 the bus clk */ |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 254 | if ((bus_setup & DSPI_CTAR_DBR) || |
| 255 | (cfspi->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 256 | bus_setup |= DSPI_CTAR_DBR; |
| 257 | dbr = 1; |
| 258 | } |
| 259 | |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 260 | /* Overwrite default value set in platform configuration file */ |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 261 | if (cfspi->mode & SPI_MODE_MOD) { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 262 | /* |
| 263 | * Check to see if it is enabled by default in platform |
| 264 | * config, or manual setting passed by mode parameter |
| 265 | */ |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 266 | if (cfspi->mode & SPI_MODE_DBLRATE) { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 267 | bus_setup |= DSPI_CTAR_DBR; |
| 268 | dbr = 1; |
| 269 | } |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 270 | } |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 271 | |
| 272 | pbrcnt = sizeof(prescaler) / sizeof(int); |
| 273 | brcnt = sizeof(scaler) / sizeof(int); |
| 274 | |
| 275 | /* baudrate calculation - to closer value, may not be exact match */ |
| 276 | for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) { |
| 277 | baud_speed = gd->bus_clk / prescaler[i]; |
| 278 | for (j = 0; j < brcnt; j++) { |
| 279 | tmp = (baud_speed / scaler[j]) * (1 + dbr); |
| 280 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 281 | if (tmp > cfspi->baudrate) |
| 282 | diff = tmp - cfspi->baudrate; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 283 | else |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 284 | diff = cfspi->baudrate - tmp; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 285 | |
| 286 | if (diff < bestmatch) { |
| 287 | bestmatch = diff; |
| 288 | best_i = i; |
| 289 | best_j = j; |
| 290 | } |
| 291 | } |
| 292 | } |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 293 | |
| 294 | bus_setup &= ~(DSPI_CTAR_PBR(0x03) | DSPI_CTAR_BR(0x0f)); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 295 | bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j)); |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 296 | writel(bus_setup, &dspi->ctar[bus->seq]); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 297 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 298 | return 0; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 299 | } |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 300 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 301 | static int coldfire_spi_set_mode(struct udevice *bus, uint mode) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 302 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 303 | struct coldfire_spi_priv *cfspi = dev_get_priv(bus); |
| 304 | struct dspi *dspi = cfspi->regs; |
| 305 | u32 bus_setup = 0; |
| 306 | |
| 307 | cfspi->mode = mode; |
| 308 | |
| 309 | if (cfspi->mode & SPI_CPOL) |
| 310 | bus_setup |= DSPI_CTAR_CPOL; |
| 311 | if (cfspi->mode & SPI_CPHA) |
| 312 | bus_setup |= DSPI_CTAR_CPHA; |
| 313 | if (cfspi->mode & SPI_LSB_FIRST) |
| 314 | bus_setup |= DSPI_CTAR_LSBFE; |
| 315 | |
| 316 | /* Overwrite default value set in platform configuration file */ |
| 317 | if (cfspi->mode & SPI_MODE_MOD) { |
| 318 | if ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) == 0) |
| 319 | bus_setup |= |
| 320 | readl(&dspi->ctar[bus->seq]) & MCF_FRM_SZ_16BIT; |
| 321 | else |
| 322 | bus_setup |= |
| 323 | ((cfspi->mode & SPI_MODE_XFER_SZ_MASK) >> 1); |
| 324 | |
| 325 | /* PSCSCK, PASC, PDT */ |
| 326 | bus_setup |= (cfspi->mode & SPI_MODE_DLY_PRE_MASK) >> 4; |
| 327 | /* CSSCK, ASC, DT */ |
| 328 | bus_setup |= (cfspi->mode & SPI_MODE_DLY_SCA_MASK) >> 4; |
| 329 | } else { |
| 330 | bus_setup |= |
| 331 | (readl(&dspi->ctar[bus->seq]) & MCF_CTAR_MODE_MASK); |
| 332 | } |
| 333 | |
| 334 | cfspi->charbit = |
| 335 | ((readl(&dspi->ctar[bus->seq]) & MCF_FRM_SZ_16BIT) == |
| 336 | MCF_FRM_SZ_16BIT) ? 16 : 8; |
| 337 | |
| 338 | setbits_be32(&dspi->ctar[bus->seq], bus_setup); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int coldfire_spi_probe(struct udevice *bus) |
| 344 | { |
| 345 | struct coldfire_spi_platdata *plat = dev_get_platdata(bus); |
| 346 | struct coldfire_spi_priv *cfspi = dev_get_priv(bus); |
| 347 | struct dspi *dspi = cfspi->regs; |
| 348 | int i; |
| 349 | |
| 350 | cfspi->regs = (struct dspi *)plat->regs_addr; |
| 351 | |
| 352 | cfspi->baudrate = plat->speed_hz; |
| 353 | cfspi->mode = plat->mode; |
| 354 | |
| 355 | for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++) { |
| 356 | unsigned int ctar = 0; |
| 357 | |
| 358 | if (plat->ctar[i][0] == 0) |
| 359 | break; |
| 360 | |
| 361 | ctar = DSPI_CTAR_TRSZ(plat->ctar[i][0]) | |
| 362 | DSPI_CTAR_PCSSCK(plat->ctar[i][1]) | |
| 363 | DSPI_CTAR_PASC(plat->ctar[i][2]) | |
| 364 | DSPI_CTAR_PDT(plat->ctar[i][3]) | |
| 365 | DSPI_CTAR_CSSCK(plat->ctar[i][4]) | |
| 366 | DSPI_CTAR_ASC(plat->ctar[i][5]) | |
| 367 | DSPI_CTAR_DT(plat->ctar[i][6]) | |
| 368 | DSPI_CTAR_BR(plat->ctar[i][7]); |
| 369 | |
| 370 | writel(ctar, &cfspi->regs->ctar[i]); |
| 371 | } |
| 372 | |
| 373 | /* Default CTARs */ |
| 374 | for (i = 0; i < MCF_DSPI_MAX_CTAR_REGS; i++) |
| 375 | writel(MCF_DSPI_DEFAULT_CTAR, &dspi->ctar[i]); |
| 376 | |
| 377 | dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 | |
| 378 | DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 | |
| 379 | DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 | |
| 380 | DSPI_MCR_CRXF | DSPI_MCR_CTXF; |
| 381 | |
| 382 | return 0; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 383 | } |
| 384 | |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 385 | void spi_init(void) |
| 386 | { |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 389 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 390 | static int coldfire_dspi_ofdata_to_platdata(struct udevice *bus) |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 391 | { |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 392 | fdt_addr_t addr; |
| 393 | struct coldfire_spi_platdata *plat = bus->platdata; |
| 394 | const void *blob = gd->fdt_blob; |
| 395 | int node = dev_of_offset(bus); |
| 396 | int *ctar, len; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 397 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 398 | addr = devfdt_get_addr(bus); |
| 399 | if (addr == FDT_ADDR_T_NONE) |
| 400 | return -ENOMEM; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 401 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 402 | plat->regs_addr = addr; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 403 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 404 | plat->num_cs = fdtdec_get_int(blob, node, "num-cs", |
| 405 | MCF_DSPI_DEFAULT_MAX_CS); |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 406 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 407 | plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 408 | MCF_DSPI_DEFAULT_SCK_FREQ); |
| 409 | |
| 410 | plat->mode = fdtdec_get_int(blob, node, "spi-mode", |
| 411 | MCF_DSPI_DEFAULT_MODE); |
| 412 | |
| 413 | memset(plat->ctar, 0, sizeof(plat->ctar)); |
| 414 | |
| 415 | ctar = (int *)fdt_getprop(blob, node, "ctar-params", &len); |
| 416 | |
| 417 | if (ctar && len) { |
| 418 | int i, q, ctar_regs; |
| 419 | |
| 420 | ctar_regs = len / sizeof(unsigned int) / MAX_CTAR_FIELDS; |
| 421 | |
| 422 | if (ctar_regs > MAX_CTAR_REGS) |
| 423 | ctar_regs = MAX_CTAR_REGS; |
| 424 | |
| 425 | for (i = 0; i < ctar_regs; i++) { |
| 426 | for (q = 0; q < MAX_CTAR_FIELDS; q++) |
| 427 | plat->ctar[i][q] = *ctar++; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | debug("DSPI: regs=%pa, max-frequency=%d, num-cs=%d, mode=%d\n", |
| 432 | (void *)plat->regs_addr, |
| 433 | plat->speed_hz, plat->num_cs, plat->mode); |
| 434 | |
| 435 | return 0; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 438 | static const struct udevice_id coldfire_spi_ids[] = { |
| 439 | { .compatible = "fsl,mcf-dspi" }, |
| 440 | { } |
| 441 | }; |
| 442 | #endif |
Axel Lin | bb16627 | 2015-02-21 00:17:47 +0800 | [diff] [blame] | 443 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 444 | static const struct dm_spi_ops coldfire_spi_ops = { |
| 445 | .claim_bus = coldfire_spi_claim_bus, |
| 446 | .release_bus = coldfire_spi_release_bus, |
| 447 | .xfer = coldfire_spi_xfer, |
| 448 | .set_speed = coldfire_spi_set_speed, |
| 449 | .set_mode = coldfire_spi_set_mode, |
| 450 | }; |
TsiChung Liew | dec61c7 | 2009-06-30 14:09:47 +0000 | [diff] [blame] | 451 | |
Angelo Dureghello | 5ea3766 | 2019-03-13 21:46:47 +0100 | [diff] [blame] | 452 | U_BOOT_DRIVER(coldfire_spi) = { |
| 453 | .name = "spi_coldfire", |
| 454 | .id = UCLASS_SPI, |
| 455 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 456 | .of_match = coldfire_spi_ids, |
| 457 | .ofdata_to_platdata = coldfire_dspi_ofdata_to_platdata, |
| 458 | .platdata_auto_alloc_size = sizeof(struct coldfire_spi_platdata), |
| 459 | #endif |
| 460 | .probe = coldfire_spi_probe, |
| 461 | .ops = &coldfire_spi_ops, |
| 462 | .priv_auto_alloc_size = sizeof(struct coldfire_spi_priv), |
| 463 | }; |