3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * Influenced by code from:
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
16 /*-----------------------------------------------------------------------
21 #define PRINTD(fmt,args...) printf (fmt ,##args)
23 #define PRINTD(fmt,args...)
26 struct soft_spi_slave {
27 struct spi_slave slave;
31 static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
33 return container_of(slave, struct soft_spi_slave, slave);
36 /*=====================================================================*/
37 /* Public Functions */
38 /*=====================================================================*/
40 /*-----------------------------------------------------------------------
46 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
52 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
53 unsigned int max_hz, unsigned int mode)
55 struct soft_spi_slave *ss;
57 if (!spi_cs_is_valid(bus, cs))
60 ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
66 /* TODO: Use max_hz to limit the SCK rate */
71 void spi_free_slave(struct spi_slave *slave)
73 struct soft_spi_slave *ss = to_soft_spi(slave);
78 int spi_claim_bus(struct spi_slave *slave)
80 #ifdef CONFIG_SYS_IMMR
81 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
83 struct soft_spi_slave *ss = to_soft_spi(slave);
86 * Make sure the SPI clock is in idle state as defined for
89 if (ss->mode & SPI_CPOL)
97 void spi_release_bus(struct spi_slave *slave)
102 /*-----------------------------------------------------------------------
105 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
106 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
108 * The source of the outgoing bits is the "dout" parameter and the
109 * destination of the input bits is the "din" parameter. Note that "dout"
110 * and "din" can point to the same memory location, in which case the
111 * input data overwrites the output data (since both are buffered by
112 * temporary variables, this is OK).
114 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
115 const void *dout, void *din, unsigned long flags)
117 #ifdef CONFIG_SYS_IMMR
118 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
120 struct soft_spi_slave *ss = to_soft_spi(slave);
123 const u8 *txd = dout;
125 int cpol = ss->mode & SPI_CPOL;
126 int cpha = ss->mode & SPI_CPHA;
129 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
130 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
132 if (flags & SPI_XFER_BEGIN)
133 spi_cs_activate(slave);
135 for(j = 0; j < bitlen; j++) {
137 * Check if it is time to work on a new byte.
149 SPI_SDA(tmpdout & 0x80);
163 * If the number of bits isn't a multiple of 8, shift the last
164 * bits over to left-justify them. Then store the last byte
167 if((bitlen % 8) != 0)
168 tmpdin <<= 8 - (bitlen % 8);
171 if (flags & SPI_XFER_END)
172 spi_cs_deactivate(slave);