3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Copyright (C) 2004-2009 Freescale Semiconductor, Inc.
7 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <asm/immap.h>
34 struct spi_slave slave;
39 int cfspi_xfer(struct spi_slave *slave, uint bitlen, const void *dout,
40 void *din, ulong flags);
41 struct spi_slave *cfspi_setup_slave(struct cf_spi_slave *cfslave, uint mode);
42 void cfspi_init(void);
43 void cfspi_tx(u32 ctrl, u16 data);
46 extern void cfspi_port_conf(void);
47 extern int cfspi_claim_bus(uint bus, uint cs);
48 extern void cfspi_release_bus(uint bus, uint cs);
50 DECLARE_GLOBAL_DATA_PTR;
52 #if defined(CONFIG_CF_DSPI)
53 /* DSPI specific mode */
54 #define SPI_MODE_MOD 0x00200000
55 #define SPI_DBLRATE 0x00100000
59 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
61 cfspi_port_conf(); /* port configuration */
63 dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
64 DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
65 DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
66 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
68 /* Default setting in platform configuration */
69 #ifdef CONFIG_SYS_DSPI_CTAR0
70 dspi->ctar[0] = CONFIG_SYS_DSPI_CTAR0;
72 #ifdef CONFIG_SYS_DSPI_CTAR1
73 dspi->ctar[1] = CONFIG_SYS_DSPI_CTAR1;
75 #ifdef CONFIG_SYS_DSPI_CTAR2
76 dspi->ctar[2] = CONFIG_SYS_DSPI_CTAR2;
78 #ifdef CONFIG_SYS_DSPI_CTAR3
79 dspi->ctar[3] = CONFIG_SYS_DSPI_CTAR3;
81 #ifdef CONFIG_SYS_DSPI_CTAR4
82 dspi->ctar[4] = CONFIG_SYS_DSPI_CTAR4;
84 #ifdef CONFIG_SYS_DSPI_CTAR5
85 dspi->ctar[5] = CONFIG_SYS_DSPI_CTAR5;
87 #ifdef CONFIG_SYS_DSPI_CTAR6
88 dspi->ctar[6] = CONFIG_SYS_DSPI_CTAR6;
90 #ifdef CONFIG_SYS_DSPI_CTAR7
91 dspi->ctar[7] = CONFIG_SYS_DSPI_CTAR7;
95 void cfspi_tx(u32 ctrl, u16 data)
97 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
99 while ((dspi->sr & 0x0000F000) >= 4) ;
101 dspi->tfr = (ctrl | data);
106 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
108 while ((dspi->sr & 0x000000F0) == 0) ;
110 return (dspi->rfr & 0xFFFF);
113 int cfspi_xfer(struct spi_slave *slave, uint bitlen, const void *dout,
114 void *din, ulong flags)
116 struct cf_spi_slave *cfslave = (struct cf_spi_slave *)slave;
117 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
118 u8 *spi_rd = NULL, *spi_wr = NULL;
120 uint len = bitlen >> 3;
122 if (cfslave->charbit == 16) {
124 spi_wr16 = (u16 *) dout;
125 spi_rd16 = (u16 *) din;
127 spi_wr = (u8 *) dout;
131 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
132 ctrl |= DSPI_TFR_CONT;
134 ctrl = (ctrl & 0xFF000000) | ((1 << slave->cs) << 16);
137 int tmp_len = len - 1;
140 if (cfslave->charbit == 16)
141 cfspi_tx(ctrl, *spi_wr16++);
143 cfspi_tx(ctrl, *spi_wr++);
149 if (cfslave->charbit == 16)
150 *spi_rd16++ = cfspi_rx();
152 *spi_rd++ = cfspi_rx();
156 len = 1; /* remaining byte */
159 if ((flags & SPI_XFER_END) == SPI_XFER_END)
160 ctrl &= ~DSPI_TFR_CONT;
164 if (cfslave->charbit == 16)
165 cfspi_tx(ctrl, *spi_wr16);
167 cfspi_tx(ctrl, *spi_wr);
173 if (cfslave->charbit == 16)
174 *spi_rd16 = cfspi_rx();
176 *spi_rd = cfspi_rx();
187 struct spi_slave *cfspi_setup_slave(struct cf_spi_slave *cfslave, uint mode)
190 * bit definition for mode:
191 * bit 31 - 28: Transfer size 3 to 16 bits
192 * 27 - 26: PCS to SCK delay prescaler
193 * 25 - 24: After SCK delay prescaler
194 * 23 - 22: Delay after transfer prescaler
195 * 21 : Allow overwrite for bit 31-22 and bit 20-8
196 * 20 : Double baud rate
197 * 19 - 16: PCS to SCK delay scaler
198 * 15 - 12: After SCK delay scaler
199 * 11 - 8: Delay after transfer scaler
200 * 7 - 0: SPI_CPHA, SPI_CPOL, SPI_LSB_FIRST
202 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
203 int prescaler[] = { 2, 3, 5, 7 };
207 256, 512, 1024, 2048,
208 4096, 8192, 16384, 32768
210 int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
211 int best_i, best_j, bestmatch = 0x7FFFFFFF, baud_speed;
214 tmp = (prescaler[3] * scaler[15]);
215 /* Maximum and minimum baudrate it can handle */
216 if ((cfslave->baudrate > (gd->bus_clk >> 1)) ||
217 (cfslave->baudrate < (gd->bus_clk / tmp))) {
218 printf("Exceed baudrate limitation: Max %d - Min %d\n",
219 (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
223 /* Activate Double Baud when it exceed 1/4 the bus clk */
224 if ((CONFIG_SYS_DSPI_CTAR0 & DSPI_CTAR_DBR) ||
225 (cfslave->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
226 bus_setup |= DSPI_CTAR_DBR;
231 bus_setup |= DSPI_CTAR_CPOL;
233 bus_setup |= DSPI_CTAR_CPHA;
234 if (mode & SPI_LSB_FIRST)
235 bus_setup |= DSPI_CTAR_LSBFE;
237 /* Overwrite default value set in platform configuration file */
238 if (mode & SPI_MODE_MOD) {
240 if ((mode & 0xF0000000) == 0)
242 dspi->ctar[cfslave->slave.bus] & 0x78000000;
244 bus_setup |= ((mode & 0xF0000000) >> 1);
247 * Check to see if it is enabled by default in platform
248 * config, or manual setting passed by mode parameter
250 if (mode & SPI_DBLRATE) {
251 bus_setup |= DSPI_CTAR_DBR;
254 bus_setup |= (mode & 0x0FC00000) >> 4; /* PSCSCK, PASC, PDT */
255 bus_setup |= (mode & 0x000FFF00) >> 4; /* CSSCK, ASC, DT */
257 bus_setup |= (dspi->ctar[cfslave->slave.bus] & 0x78FCFFF0);
260 ((dspi->ctar[cfslave->slave.bus] & 0x78000000) ==
261 0x78000000) ? 16 : 8;
263 pbrcnt = sizeof(prescaler) / sizeof(int);
264 brcnt = sizeof(scaler) / sizeof(int);
266 /* baudrate calculation - to closer value, may not be exact match */
267 for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
268 baud_speed = gd->bus_clk / prescaler[i];
269 for (j = 0; j < brcnt; j++) {
270 tmp = (baud_speed / scaler[j]) * (1 + dbr);
272 if (tmp > cfslave->baudrate)
273 diff = tmp - cfslave->baudrate;
275 diff = cfslave->baudrate - tmp;
277 if (diff < bestmatch) {
284 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
285 dspi->ctar[cfslave->slave.bus] = bus_setup;
287 return &cfslave->slave;
289 #endif /* CONFIG_CF_DSPI */
291 #ifdef CONFIG_CF_QSPI
293 #endif /* CONFIG_CF_QSPI */
295 #ifdef CONFIG_CMD_SPI
296 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
298 if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
304 void spi_init_f(void)
308 void spi_init_r(void)
317 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
318 unsigned int max_hz, unsigned int mode)
320 struct cf_spi_slave *cfslave;
322 if (!spi_cs_is_valid(bus, cs))
325 cfslave = malloc(sizeof(struct cf_spi_slave));
329 cfslave->slave.bus = bus;
330 cfslave->slave.cs = cs;
331 cfslave->baudrate = max_hz;
334 return cfspi_setup_slave(cfslave, mode);
337 void spi_free_slave(struct spi_slave *slave)
342 int spi_claim_bus(struct spi_slave *slave)
344 return cfspi_claim_bus(slave->bus, slave->cs);
347 void spi_release_bus(struct spi_slave *slave)
349 cfspi_release_bus(slave->bus, slave->cs);
352 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
353 void *din, unsigned long flags)
355 return cfspi_xfer(slave, bitlen, dout, din, flags);
357 #endif /* CONFIG_CMD_SPI */