1 // SPDX-License-Identifier: GPL-2.0+
4 * (C) Copyright 2000-2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Copyright (C) 2004-2009 Freescale Semiconductor, Inc.
8 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
14 #include <asm/immap.h>
17 struct spi_slave slave;
22 extern void cfspi_port_conf(void);
23 extern int cfspi_claim_bus(uint bus, uint cs);
24 extern void cfspi_release_bus(uint bus, uint cs);
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifndef CONFIG_SPI_IDLE_VAL
29 #if defined(CONFIG_SPI_MMC)
30 #define CONFIG_SPI_IDLE_VAL 0xFFFF
32 #define CONFIG_SPI_IDLE_VAL 0x0
36 #if defined(CONFIG_CF_DSPI)
37 /* DSPI specific mode */
38 #define SPI_MODE_MOD 0x00200000
39 #define SPI_DBLRATE 0x00100000
41 static inline struct cf_spi_slave *to_cf_spi_slave(struct spi_slave *slave)
43 return container_of(slave, struct cf_spi_slave, slave);
46 static void cfspi_init(void)
48 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
50 cfspi_port_conf(); /* port configuration */
52 dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
53 DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
54 DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
55 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
57 /* Default setting in platform configuration */
58 #ifdef CONFIG_SYS_DSPI_CTAR0
59 dspi->ctar[0] = CONFIG_SYS_DSPI_CTAR0;
61 #ifdef CONFIG_SYS_DSPI_CTAR1
62 dspi->ctar[1] = CONFIG_SYS_DSPI_CTAR1;
64 #ifdef CONFIG_SYS_DSPI_CTAR2
65 dspi->ctar[2] = CONFIG_SYS_DSPI_CTAR2;
67 #ifdef CONFIG_SYS_DSPI_CTAR3
68 dspi->ctar[3] = CONFIG_SYS_DSPI_CTAR3;
70 #ifdef CONFIG_SYS_DSPI_CTAR4
71 dspi->ctar[4] = CONFIG_SYS_DSPI_CTAR4;
73 #ifdef CONFIG_SYS_DSPI_CTAR5
74 dspi->ctar[5] = CONFIG_SYS_DSPI_CTAR5;
76 #ifdef CONFIG_SYS_DSPI_CTAR6
77 dspi->ctar[6] = CONFIG_SYS_DSPI_CTAR6;
79 #ifdef CONFIG_SYS_DSPI_CTAR7
80 dspi->ctar[7] = CONFIG_SYS_DSPI_CTAR7;
84 static void cfspi_tx(u32 ctrl, u16 data)
86 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
88 while ((dspi->sr & 0x0000F000) >= 4) ;
90 dspi->tfr = (ctrl | data);
93 static u16 cfspi_rx(void)
95 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
97 while ((dspi->sr & 0x000000F0) == 0) ;
99 return (dspi->rfr & 0xFFFF);
102 static int cfspi_xfer(struct spi_slave *slave, uint bitlen, const void *dout,
103 void *din, ulong flags)
105 struct cf_spi_slave *cfslave = to_cf_spi_slave(slave);
106 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
107 u8 *spi_rd = NULL, *spi_wr = NULL;
109 uint len = bitlen >> 3;
111 if (cfslave->charbit == 16) {
113 spi_wr16 = (u16 *) dout;
114 spi_rd16 = (u16 *) din;
116 spi_wr = (u8 *) dout;
120 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
121 ctrl |= DSPI_TFR_CONT;
123 ctrl = (ctrl & 0xFF000000) | ((1 << slave->cs) << 16);
126 int tmp_len = len - 1;
129 if (cfslave->charbit == 16)
130 cfspi_tx(ctrl, *spi_wr16++);
132 cfspi_tx(ctrl, *spi_wr++);
137 cfspi_tx(ctrl, CONFIG_SPI_IDLE_VAL);
138 if (cfslave->charbit == 16)
139 *spi_rd16++ = cfspi_rx();
141 *spi_rd++ = cfspi_rx();
145 len = 1; /* remaining byte */
148 if ((flags & SPI_XFER_END) == SPI_XFER_END)
149 ctrl &= ~DSPI_TFR_CONT;
153 if (cfslave->charbit == 16)
154 cfspi_tx(ctrl, *spi_wr16);
156 cfspi_tx(ctrl, *spi_wr);
161 cfspi_tx(ctrl, CONFIG_SPI_IDLE_VAL);
162 if (cfslave->charbit == 16)
163 *spi_rd16 = cfspi_rx();
165 *spi_rd = cfspi_rx();
169 cfspi_tx(ctrl, CONFIG_SPI_IDLE_VAL);
176 static struct spi_slave *cfspi_setup_slave(struct cf_spi_slave *cfslave,
180 * bit definition for mode:
181 * bit 31 - 28: Transfer size 3 to 16 bits
182 * 27 - 26: PCS to SCK delay prescaler
183 * 25 - 24: After SCK delay prescaler
184 * 23 - 22: Delay after transfer prescaler
185 * 21 : Allow overwrite for bit 31-22 and bit 20-8
186 * 20 : Double baud rate
187 * 19 - 16: PCS to SCK delay scaler
188 * 15 - 12: After SCK delay scaler
189 * 11 - 8: Delay after transfer scaler
190 * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST
192 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
193 int prescaler[] = { 2, 3, 5, 7 };
197 256, 512, 1024, 2048,
198 4096, 8192, 16384, 32768
200 int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
201 int best_i, best_j, bestmatch = 0x7FFFFFFF, baud_speed;
204 tmp = (prescaler[3] * scaler[15]);
205 /* Maximum and minimum baudrate it can handle */
206 if ((cfslave->baudrate > (gd->bus_clk >> 1)) ||
207 (cfslave->baudrate < (gd->bus_clk / tmp))) {
208 printf("Exceed baudrate limitation: Max %d - Min %d\n",
209 (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
213 /* Activate Double Baud when it exceed 1/4 the bus clk */
214 if ((CONFIG_SYS_DSPI_CTAR0 & DSPI_CTAR_DBR) ||
215 (cfslave->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
216 bus_setup |= DSPI_CTAR_DBR;
221 bus_setup |= DSPI_CTAR_CPOL;
223 bus_setup |= DSPI_CTAR_CPHA;
224 if (mode & SPI_LSB_FIRST)
225 bus_setup |= DSPI_CTAR_LSBFE;
227 /* Overwrite default value set in platform configuration file */
228 if (mode & SPI_MODE_MOD) {
230 if ((mode & 0xF0000000) == 0)
232 dspi->ctar[cfslave->slave.bus] & 0x78000000;
234 bus_setup |= ((mode & 0xF0000000) >> 1);
237 * Check to see if it is enabled by default in platform
238 * config, or manual setting passed by mode parameter
240 if (mode & SPI_DBLRATE) {
241 bus_setup |= DSPI_CTAR_DBR;
244 bus_setup |= (mode & 0x0FC00000) >> 4; /* PSCSCK, PASC, PDT */
245 bus_setup |= (mode & 0x000FFF00) >> 4; /* CSSCK, ASC, DT */
247 bus_setup |= (dspi->ctar[cfslave->slave.bus] & 0x78FCFFF0);
250 ((dspi->ctar[cfslave->slave.bus] & 0x78000000) ==
251 0x78000000) ? 16 : 8;
253 pbrcnt = sizeof(prescaler) / sizeof(int);
254 brcnt = sizeof(scaler) / sizeof(int);
256 /* baudrate calculation - to closer value, may not be exact match */
257 for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
258 baud_speed = gd->bus_clk / prescaler[i];
259 for (j = 0; j < brcnt; j++) {
260 tmp = (baud_speed / scaler[j]) * (1 + dbr);
262 if (tmp > cfslave->baudrate)
263 diff = tmp - cfslave->baudrate;
265 diff = cfslave->baudrate - tmp;
267 if (diff < bestmatch) {
274 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
275 dspi->ctar[cfslave->slave.bus] = bus_setup;
277 return &cfslave->slave;
279 #endif /* CONFIG_CF_DSPI */
281 #ifdef CONFIG_CMD_SPI
282 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
284 if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
295 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
296 unsigned int max_hz, unsigned int mode)
298 struct cf_spi_slave *cfslave;
300 if (!spi_cs_is_valid(bus, cs))
303 cfslave = spi_alloc_slave(struct cf_spi_slave, bus, cs);
307 cfslave->baudrate = max_hz;
310 return cfspi_setup_slave(cfslave, mode);
313 void spi_free_slave(struct spi_slave *slave)
315 struct cf_spi_slave *cfslave = to_cf_spi_slave(slave);
320 int spi_claim_bus(struct spi_slave *slave)
322 return cfspi_claim_bus(slave->bus, slave->cs);
325 void spi_release_bus(struct spi_slave *slave)
327 cfspi_release_bus(slave->bus, slave->cs);
330 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
331 void *din, unsigned long flags)
333 return cfspi_xfer(slave, bitlen, dout, din, flags);
335 #endif /* CONFIG_CMD_SPI */