1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2002
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 *
6 * Influenced by code from:
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 */
9
10 #include <common.h>
11 #include <spi.h>
12
13 #include <malloc.h>
14
15 /*-----------------------------------------------------------------------
16 * Definitions
17 */
18
19 #ifdef DEBUG_SPI
20 #define PRINTD(fmt,args...) printf (fmt ,##args)
21 #else
22 #define PRINTD(fmt,args...)
23 #endif
24
25 struct soft_spi_slave {
26 struct spi_slave slave;
27 unsigned int mode;
28 };
29
to_soft_spi(struct spi_slave * slave)30 static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
31 {
32 return container_of(slave, struct soft_spi_slave, slave);
33 }
34
35 /*=====================================================================*/
36 /* Public Functions */
37 /*=====================================================================*/
38
39 /*-----------------------------------------------------------------------
40 * Initialization
41 */
spi_init(void)42 void spi_init (void)
43 {
44 }
45
spi_setup_slave(unsigned int bus,unsigned int cs,unsigned int max_hz,unsigned int mode)46 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
47 unsigned int max_hz, unsigned int mode)
48 {
49 struct soft_spi_slave *ss;
50
51 if (!spi_cs_is_valid(bus, cs))
52 return NULL;
53
54 ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
55 if (!ss)
56 return NULL;
57
58 ss->mode = mode;
59
60 /* TODO: Use max_hz to limit the SCK rate */
61
62 return &ss->slave;
63 }
64
spi_free_slave(struct spi_slave * slave)65 void spi_free_slave(struct spi_slave *slave)
66 {
67 struct soft_spi_slave *ss = to_soft_spi(slave);
68
69 free(ss);
70 }
71
spi_claim_bus(struct spi_slave * slave)72 int spi_claim_bus(struct spi_slave *slave)
73 {
74 #ifdef CONFIG_SYS_IMMR
75 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
76 #endif
77 struct soft_spi_slave *ss = to_soft_spi(slave);
78
79 /*
80 * Make sure the SPI clock is in idle state as defined for
81 * this slave.
82 */
83 if (ss->mode & SPI_CPOL)
84 SPI_SCL(1);
85 else
86 SPI_SCL(0);
87
88 return 0;
89 }
90
spi_release_bus(struct spi_slave * slave)91 void spi_release_bus(struct spi_slave *slave)
92 {
93 /* Nothing to do */
94 }
95
96 /*-----------------------------------------------------------------------
97 * SPI transfer
98 *
99 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
100 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
101 *
102 * The source of the outgoing bits is the "dout" parameter and the
103 * destination of the input bits is the "din" parameter. Note that "dout"
104 * and "din" can point to the same memory location, in which case the
105 * input data overwrites the output data (since both are buffered by
106 * temporary variables, this is OK).
107 */
spi_xfer(struct spi_slave * slave,unsigned int bitlen,const void * dout,void * din,unsigned long flags)108 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
109 const void *dout, void *din, unsigned long flags)
110 {
111 #ifdef CONFIG_SYS_IMMR
112 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
113 #endif
114 struct soft_spi_slave *ss = to_soft_spi(slave);
115 uchar tmpdin = 0;
116 uchar tmpdout = 0;
117 const u8 *txd = dout;
118 u8 *rxd = din;
119 int cpol = ss->mode & SPI_CPOL;
120 int cpha = ss->mode & SPI_CPHA;
121 unsigned int j;
122
123 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
124 slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
125
126 if (flags & SPI_XFER_BEGIN)
127 spi_cs_activate(slave);
128
129 for(j = 0; j < bitlen; j++) {
130 /*
131 * Check if it is time to work on a new byte.
132 */
133 if ((j % 8) == 0) {
134 if (txd)
135 tmpdout = *txd++;
136 else
137 tmpdout = 0;
138 if(j != 0) {
139 if (rxd)
140 *rxd++ = tmpdin;
141 }
142 tmpdin = 0;
143 }
144
145 if (!cpha)
146 SPI_SCL(!cpol);
147 SPI_SDA(tmpdout & 0x80);
148 SPI_DELAY;
149 if (cpha)
150 SPI_SCL(!cpol);
151 else
152 SPI_SCL(cpol);
153 tmpdin <<= 1;
154 tmpdin |= SPI_READ;
155 tmpdout <<= 1;
156 SPI_DELAY;
157 if (cpha)
158 SPI_SCL(cpol);
159 }
160 /*
161 * If the number of bits isn't a multiple of 8, shift the last
162 * bits over to left-justify them. Then store the last byte
163 * read in.
164 */
165 if (rxd) {
166 if ((bitlen % 8) != 0)
167 tmpdin <<= 8 - (bitlen % 8);
168 *rxd++ = tmpdin;
169 }
170
171 if (flags & SPI_XFER_END)
172 spi_cs_deactivate(slave);
173
174 return(0);
175 }
176