3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Copyright (C) 2004-2008 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,
32 #if defined(CONFIG_CF_DSPI)
33 #include <asm/immap.h>
37 volatile gpio_t *gpio = (gpio_t *) MMAP_GPIO;
38 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
40 gpio->par_dspi = GPIO_PAR_DSPI_PCS5_PCS5 | GPIO_PAR_DSPI_PCS2_PCS2 |
41 GPIO_PAR_DSPI_PCS1_PCS1 | GPIO_PAR_DSPI_PCS0_PCS0 |
42 GPIO_PAR_DSPI_SIN_SIN | GPIO_PAR_DSPI_SOUT_SOUT |
43 GPIO_PAR_DSPI_SCK_SCK;
45 dspi->dmcr = DSPI_DMCR_MSTR | DSPI_DMCR_CSIS7 | DSPI_DMCR_CSIS6 |
46 DSPI_DMCR_CSIS5 | DSPI_DMCR_CSIS4 | DSPI_DMCR_CSIS3 |
47 DSPI_DMCR_CSIS2 | DSPI_DMCR_CSIS1 | DSPI_DMCR_CSIS0 |
48 DSPI_DMCR_CRXF | DSPI_DMCR_CTXF;
50 #ifdef CFG_DSPI_DCTAR0
51 dspi->dctar0 = CFG_DSPI_DCTAR0;
53 #ifdef CFG_DSPI_DCTAR1
54 dspi->dctar1 = CFG_DSPI_DCTAR1;
56 #ifdef CFG_DSPI_DCTAR2
57 dspi->dctar2 = CFG_DSPI_DCTAR2;
59 #ifdef CFG_DSPI_DCTAR3
60 dspi->dctar3 = CFG_DSPI_DCTAR3;
62 #ifdef CFG_DSPI_DCTAR4
63 dspi->dctar4 = CFG_DSPI_DCTAR4;
65 #ifdef CFG_DSPI_DCTAR5
66 dspi->dctar5 = CFG_DSPI_DCTAR5;
68 #ifdef CFG_DSPI_DCTAR6
69 dspi->dctar6 = CFG_DSPI_DCTAR6;
71 #ifdef CFG_DSPI_DCTAR7
72 dspi->dctar7 = CFG_DSPI_DCTAR7;
76 void dspi_tx(int chipsel, u8 attrib, u16 data)
78 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
80 while ((dspi->dsr & 0x0000F000) >= 4) ;
82 dspi->dtfr = (attrib << 24) | ((1 << chipsel) << 16) | data;
87 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
89 while ((dspi->dsr & 0x000000F0) == 0) ;
91 return (dspi->drfr & 0xFFFF);
94 #if defined(CONFIG_CMD_SPI)
108 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
109 unsigned int max_hz, unsigned int mode)
111 struct spi_slave *slave;
113 slave = malloc(sizeof(struct spi_slave));
123 void spi_free_slave(struct spi_slave *slave)
128 int spi_claim_bus(struct spi_slave *slave)
133 void spi_release_bus(struct spi_slave *slave)
137 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
138 void *din, unsigned long flags)
140 static int bWrite = 0;
142 int len = bitlen >> 3;
145 spi_wr = (u8 *) dout;
147 /* command handling */
148 if (((len == 4) || (len == 1) || (len == 5)) && (dout != NULL)) {
150 case 0x02: /* Page Prog */
152 dspi_tx(slave->cs, 0x80, spi_wr[0]);
154 dspi_tx(slave->cs, 0x80, spi_wr[1]);
156 dspi_tx(slave->cs, 0x80, spi_wr[2]);
158 dspi_tx(slave->cs, 0x80, spi_wr[3]);
161 case 0x05: /* Read Status */
163 if ((spi_wr[1] == 0xFF) && (spi_wr[2] == 0xFF)
164 && (spi_wr[3] == 0xFF)) {
165 dspi_tx(slave->cs, 0x80, *spi_wr);
169 case 0x06: /* WREN */
170 dspi_tx(slave->cs, 0x00, *spi_wr);
173 case 0x0B: /* Fast read */
174 if ((len == 5) && (spi_wr[4] == 0)) {
175 dspi_tx(slave->cs, 0x80, spi_wr[0]);
177 dspi_tx(slave->cs, 0x80, spi_wr[1]);
179 dspi_tx(slave->cs, 0x80, spi_wr[2]);
181 dspi_tx(slave->cs, 0x80, spi_wr[3]);
183 dspi_tx(slave->cs, 0x80, spi_wr[4]);
187 case 0x9F: /* RDID */
188 dspi_tx(slave->cs, 0x80, *spi_wr);
191 case 0xD8: /* Sector erase */
193 if ((spi_wr[2] == 0) && (spi_wr[3] == 0)) {
194 dspi_tx(slave->cs, 0x80, spi_wr[0]);
196 dspi_tx(slave->cs, 0x80, spi_wr[1]);
198 dspi_tx(slave->cs, 0x80, spi_wr[2]);
200 dspi_tx(slave->cs, 0x00, spi_wr[3]);
212 dspi_tx(slave->cs, 0x80, *spi_wr);
218 dspi_tx(slave->cs, 0x80, 0);
224 if (flags == SPI_XFER_END) {
226 dspi_tx(slave->cs, 0x00, *spi_wr);
230 dspi_tx(slave->cs, 0x00, 0);
237 #endif /* CONFIG_CMD_SPI */
239 #endif /* CONFIG_CF_DSPI */