Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / bitbash / spi_bit.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <byteswap.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <unistd.h>
29 #include <ipxe/bitbash.h>
30 #include <ipxe/spi_bit.h>
31
32 /** @file
33  *
34  * SPI bit-bashing interface
35  *
36  */
37
38 /** Delay between SCLK changes and around SS changes */
39 static void spi_bit_delay ( void ) {
40         udelay ( SPI_BIT_UDELAY );
41 }
42
43 /** Chip select line will be asserted */
44 #define SELECT_SLAVE 0
45
46 /** Chip select line will be deasserted */
47 #define DESELECT_SLAVE SPI_MODE_SSPOL
48
49 /**
50  * Select/deselect slave
51  *
52  * @v spibit            SPI bit-bashing interface
53  * @v slave             Slave number
54  * @v state             Slave select state
55  *
56  * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
57  */
58 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
59                                        unsigned int slave,
60                                        unsigned int state ) {
61         struct bit_basher *basher = &spibit->basher;
62
63         state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
64         DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
65                 spibit, slave, ( state ? "high" : "low" ) );
66
67         spi_bit_delay();
68         write_bit ( basher, SPI_BIT_SS ( slave ), state );
69         spi_bit_delay();
70 }
71
72 /**
73  * Transfer bits over SPI bit-bashing bus
74  *
75  * @v bus               SPI bus
76  * @v data_out          TX data buffer (or NULL)
77  * @v data_in           RX data buffer (or NULL)
78  * @v len               Length of transfer (in @b bits)
79  * @v endianness        Endianness of this data transfer
80  *
81  * This issues @c len clock cycles on the SPI bus, shifting out data
82  * from the @c data_out buffer to the MOSI line and shifting in data
83  * from the MISO line to the @c data_in buffer.  If @c data_out is
84  * NULL, then the data sent will be all zeroes.  If @c data_in is
85  * NULL, then the incoming data will be discarded.
86  */
87 static void spi_bit_transfer ( struct spi_bit_basher *spibit,
88                                const void *data_out, void *data_in,
89                                unsigned int len, int endianness ) {
90         struct spi_bus *bus = &spibit->bus;
91         struct bit_basher *basher = &spibit->basher;
92         unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
93         unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
94         unsigned int bit_offset;
95         unsigned int byte_offset;
96         unsigned int byte_mask;
97         unsigned int bit;
98         unsigned int step;
99
100         DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
101                 spibit, len, bus->mode );
102
103         for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
104                 /* Calculate byte offset and byte mask */
105                 bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
106                                ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
107                 byte_offset = ( bit_offset / 8 );
108                 byte_mask = ( 1 << ( bit_offset % 8 ) );
109
110                 /* Shift data in or out */
111                 if ( sclk == cpha ) {
112                         const uint8_t *byte;
113
114                         /* Shift data out */
115                         if ( data_out ) {
116                                 byte = ( data_out + byte_offset );
117                                 bit = ( *byte & byte_mask );
118                                 DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
119                                         spibit, ( bit ? 1 : 0 ) );
120                         } else {
121                                 bit = 0;
122                         }
123                         write_bit ( basher, SPI_BIT_MOSI, bit );
124                 } else {
125                         uint8_t *byte;
126
127                         /* Shift data in */
128                         bit = read_bit ( basher, SPI_BIT_MISO );
129                         if ( data_in ) {
130                                 DBGCP ( spibit, "SPIBIT %p read bit %d\n",
131                                         spibit, ( bit ? 1 : 0 ) );
132                                 byte = ( data_in + byte_offset );
133                                 *byte &= ~byte_mask;
134                                 *byte |= ( bit & byte_mask );
135                         }
136                 }
137
138                 /* Toggle clock line */
139                 spi_bit_delay();
140                 sclk ^= 1;
141                 write_bit ( basher, SPI_BIT_SCLK, sclk );
142         }
143 }
144
145 /**
146  * Read/write data via SPI bit-bashing bus
147  *
148  * @v bus               SPI bus
149  * @v device            SPI device
150  * @v command           Command
151  * @v address           Address to read/write (<0 for no address)
152  * @v data_out          TX data buffer (or NULL)
153  * @v data_in           RX data buffer (or NULL)
154  * @v len               Length of transfer
155  * @ret rc              Return status code
156  */
157 static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
158                         unsigned int command, int address,
159                         const void *data_out, void *data_in, size_t len ) {
160         struct spi_bit_basher *spibit
161                 = container_of ( bus, struct spi_bit_basher, bus );
162         uint32_t tmp_command;
163         uint32_t tmp_address;
164         uint32_t tmp_address_detect;
165
166         /* Open bit-bashing interface */
167         open_bit ( &spibit->basher );
168
169         /* Deassert chip select to reset specified slave */
170         spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
171
172         /* Set clock line to idle state */
173         write_bit ( &spibit->basher, SPI_BIT_SCLK, 
174                     ( bus->mode & SPI_MODE_CPOL ) );
175
176         /* Assert chip select on specified slave */
177         spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
178
179         /* Transmit command */
180         assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
181         tmp_command = cpu_to_le32 ( command );
182         spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
183                            SPI_BIT_BIG_ENDIAN );
184
185         /* Transmit address, if present */
186         if ( address >= 0 ) {
187                 assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
188                 tmp_address = cpu_to_le32 ( address );
189                 if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
190                         /* Autodetect address length.  This relies on
191                          * the device responding with a dummy zero
192                          * data bit before the first real data bit.
193                          */
194                         DBGC ( spibit, "SPIBIT %p autodetecting device "
195                                "address length\n", spibit );
196                         assert ( address == 0 );
197                         device->address_len = 0;
198                         do {
199                                 spi_bit_transfer ( spibit, &tmp_address,
200                                                    &tmp_address_detect, 1,
201                                                    SPI_BIT_BIG_ENDIAN );
202                                 device->address_len++;
203                         } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
204                         DBGC ( spibit, "SPIBIT %p autodetected device address "
205                                "length %d\n", spibit, device->address_len );
206                 } else {
207                         spi_bit_transfer ( spibit, &tmp_address, NULL,
208                                            device->address_len,
209                                            SPI_BIT_BIG_ENDIAN );
210                 }
211         }
212
213         /* Transmit/receive data */
214         spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
215                            spibit->endianness );
216
217         /* Deassert chip select on specified slave */
218         spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
219
220         /* Close bit-bashing interface */
221         close_bit ( &spibit->basher );
222
223         return 0;
224 }
225
226 /**
227  * Initialise SPI bit-bashing interface
228  *
229  * @v spibit            SPI bit-bashing interface
230  */
231 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
232         assert ( &spibit->basher.op->read != NULL );
233         assert ( &spibit->basher.op->write != NULL );
234         spibit->bus.rw = spi_bit_rw;
235 }