3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * Influenced by code from:
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #if defined(CONFIG_SOFT_SPI)
34 /*-----------------------------------------------------------------------
39 #define PRINTD(fmt,args...) printf (fmt ,##args)
41 #define PRINTD(fmt,args...)
44 struct soft_spi_slave {
45 struct spi_slave slave;
49 static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
51 return container_of(slave, struct soft_spi_slave, slave);
54 /*=====================================================================*/
55 /* Public Functions */
56 /*=====================================================================*/
58 /*-----------------------------------------------------------------------
64 volatile immap_t *immr = (immap_t *)CFG_IMMR;
70 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
71 unsigned int max_hz, unsigned int mode)
73 struct soft_spi_slave *ss;
75 if (!spi_cs_is_valid(bus, cs))
78 ss = malloc(sizeof(struct soft_spi_slave));
86 /* TODO: Use max_hz to limit the SCK rate */
91 void spi_free_slave(struct spi_slave *slave)
93 struct soft_spi_slave *ss = to_soft_spi(slave);
98 int spi_claim_bus(struct spi_slave *slave)
101 volatile immap_t *immr = (immap_t *)CFG_IMMR;
103 struct soft_spi_slave *ss = to_soft_spi(slave);
106 * Make sure the SPI clock is in idle state as defined for
109 if (ss->mode & SPI_CPOL)
117 void spi_release_bus(struct spi_slave *slave)
122 /*-----------------------------------------------------------------------
125 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
126 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
128 * The source of the outgoing bits is the "dout" parameter and the
129 * destination of the input bits is the "din" parameter. Note that "dout"
130 * and "din" can point to the same memory location, in which case the
131 * input data overwrites the output data (since both are buffered by
132 * temporary variables, this is OK).
134 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
135 const void *dout, void *din, unsigned long flags)
138 volatile immap_t *immr = (immap_t *)CFG_IMMR;
140 struct soft_spi_slave *ss = to_soft_spi(slave);
143 const u8 *txd = dout;
145 int cpol = ss->mode & SPI_CPOL;
146 int cpha = ss->mode & SPI_CPHA;
149 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
150 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
152 if (flags & SPI_XFER_BEGIN)
153 spi_cs_activate(slave);
155 for(j = 0; j < bitlen; j++) {
157 * Check if it is time to work on a new byte.
169 SPI_SDA(tmpdout & 0x80);
183 * If the number of bits isn't a multiple of 8, shift the last
184 * bits over to left-justify them. Then store the last byte
187 if((bitlen % 8) != 0)
188 tmpdin <<= 8 - (bitlen % 8);
191 if (flags & SPI_XFER_END)
192 spi_cs_deactivate(slave);
197 #endif /* CONFIG_SOFT_SPI */