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 /*-----------------------------------------------------------------------
47 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
48 unsigned int max_hz, unsigned int mode)
50 struct soft_spi_slave *ss;
52 if (!spi_cs_is_valid(bus, cs))
55 ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
61 /* TODO: Use max_hz to limit the SCK rate */
66 void spi_free_slave(struct spi_slave *slave)
68 struct soft_spi_slave *ss = to_soft_spi(slave);
73 int spi_claim_bus(struct spi_slave *slave)
75 #ifdef CONFIG_SYS_IMMR
76 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
78 struct soft_spi_slave *ss = to_soft_spi(slave);
81 * Make sure the SPI clock is in idle state as defined for
84 if (ss->mode & SPI_CPOL)
92 void spi_release_bus(struct spi_slave *slave)
97 /*-----------------------------------------------------------------------
100 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
101 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
103 * The source of the outgoing bits is the "dout" parameter and the
104 * destination of the input bits is the "din" parameter. Note that "dout"
105 * and "din" can point to the same memory location, in which case the
106 * input data overwrites the output data (since both are buffered by
107 * temporary variables, this is OK).
109 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
110 const void *dout, void *din, unsigned long flags)
112 #ifdef CONFIG_SYS_IMMR
113 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
115 struct soft_spi_slave *ss = to_soft_spi(slave);
118 const u8 *txd = dout;
120 int cpol = ss->mode & SPI_CPOL;
121 int cpha = ss->mode & SPI_CPHA;
124 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
125 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
127 if (flags & SPI_XFER_BEGIN)
128 spi_cs_activate(slave);
130 for(j = 0; j < bitlen; j++) {
132 * Check if it is time to work on a new byte.
148 SPI_SDA(tmpdout & 0x80);
162 * If the number of bits isn't a multiple of 8, shift the last
163 * bits over to left-justify them. Then store the last byte
167 if ((bitlen % 8) != 0)
168 tmpdin <<= 8 - (bitlen % 8);
172 if (flags & SPI_XFER_END)
173 spi_cs_deactivate(slave);