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