Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / nvs / spi.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 <errno.h>
24 #include <unistd.h>
25 #include <ipxe/spi.h>
26
27 /** @file
28  *
29  * SPI devices
30  *
31  */
32
33 /**
34  * Munge SPI device address into command
35  *
36  * @v command           SPI command
37  * @v address           Address
38  * @v munge_address     Device requires address munging
39  * @ret command         Actual SPI command to use
40  *
41  * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3
42  * of the command byte as address bit A8, rather than having a
43  * two-byte address.  This function takes care of generating the
44  * appropriate command.
45  */
46 static inline unsigned int spi_command ( unsigned int command,
47                                          unsigned int address,
48                                          int munge_address ) {
49         return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) );
50 }
51
52 /**
53  * Wait for SPI device to complete operation
54  *
55  * @v device            SPI device
56  * @ret rc              Return status code
57  */
58 static int spi_wait ( struct spi_device *device ) {
59         struct spi_bus *bus = device->bus;
60         uint8_t status;
61         int i;
62         int rc;
63
64         for ( i = 0 ; i < 50 ; i++ ) {
65                 udelay ( 20 );
66                 if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL,
67                                       &status, sizeof ( status ) ) ) != 0 )
68                         return rc;
69                 if ( ! ( status & SPI_STATUS_NRDY ) )
70                         return 0;
71         }
72         DBG ( "SPI %p timed out\n", device );
73         return -ETIMEDOUT;
74 }
75
76 /**
77  * Read data from SPI device
78  *
79  * @v nvs               NVS device
80  * @v address           Address from which to read
81  * @v data              Data buffer
82  * @v len               Length of data buffer
83  * @ret rc              Return status code
84  */
85 int spi_read ( struct nvs_device *nvs, unsigned int address,
86                void *data, size_t len ) {
87         struct spi_device *device = nvs_to_spi ( nvs );
88         struct spi_bus *bus = device->bus;
89         unsigned int command = spi_command ( SPI_READ, address,
90                                              device->munge_address );
91         int rc;
92
93         DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address );
94         if ( ( rc = bus->rw ( bus, device, command, address,
95                               NULL, data, len ) ) != 0 ) {
96                 DBG ( "SPI %p failed to read data from device\n", device );
97                 return rc;
98         }
99
100         return 0;
101 }
102
103 /**
104  * Write data to SPI device
105  *
106  * @v nvs               NVS device
107  * @v address           Address from which to read
108  * @v data              Data buffer
109  * @v len               Length of data buffer
110  * @ret rc              Return status code
111  */
112 int spi_write ( struct nvs_device *nvs, unsigned int address,
113                 const void *data, size_t len ) {
114         struct spi_device *device = nvs_to_spi ( nvs );
115         struct spi_bus *bus = device->bus;
116         unsigned int command = spi_command ( SPI_WRITE, address,
117                                              device->munge_address );
118         int rc;
119
120         DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
121
122         if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
123                               NULL, NULL, 0 ) ) != 0 ) {
124                 DBG ( "SPI %p failed to write-enable device\n", device );
125                 return rc;
126         }
127
128         if ( ( rc = bus->rw ( bus, device, command, address,
129                               data, NULL, len ) ) != 0 ) {
130                 DBG ( "SPI %p failed to write data to device\n", device );
131                 return rc;
132         }
133         
134         if ( ( rc = spi_wait ( device ) ) != 0 ) {
135                 DBG ( "SPI %p failed to complete write operation\n", device );
136                 return rc;
137         }
138
139         return 0;
140 }
141