2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
38 * Munge SPI device address into command
40 * @v command SPI command
42 * @v munge_address Device requires address munging
43 * @ret command Actual SPI command to use
45 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3
46 * of the command byte as address bit A8, rather than having a
47 * two-byte address. This function takes care of generating the
48 * appropriate command.
50 static inline unsigned int spi_command ( unsigned int command,
53 return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) );
57 * Wait for SPI device to complete operation
59 * @v device SPI device
60 * @ret rc Return status code
62 static int spi_wait ( struct spi_device *device ) {
63 struct spi_bus *bus = device->bus;
68 for ( i = 0 ; i < 50 ; i++ ) {
70 if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL,
71 &status, sizeof ( status ) ) ) != 0 )
73 if ( ! ( status & SPI_STATUS_NRDY ) )
76 DBG ( "SPI %p timed out\n", device );
81 * Read data from SPI device
84 * @v address Address from which to read
86 * @v len Length of data buffer
87 * @ret rc Return status code
89 int spi_read ( struct nvs_device *nvs, unsigned int address,
90 void *data, size_t len ) {
91 struct spi_device *device = nvs_to_spi ( nvs );
92 struct spi_bus *bus = device->bus;
93 unsigned int command = spi_command ( SPI_READ, address,
94 device->munge_address );
97 DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address );
98 if ( ( rc = bus->rw ( bus, device, command, address,
99 NULL, data, len ) ) != 0 ) {
100 DBG ( "SPI %p failed to read data from device\n", device );
108 * Write data to SPI device
111 * @v address Address from which to read
112 * @v data Data buffer
113 * @v len Length of data buffer
114 * @ret rc Return status code
116 int spi_write ( struct nvs_device *nvs, unsigned int address,
117 const void *data, size_t len ) {
118 struct spi_device *device = nvs_to_spi ( nvs );
119 struct spi_bus *bus = device->bus;
120 unsigned int command = spi_command ( SPI_WRITE, address,
121 device->munge_address );
124 DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
126 if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
127 NULL, NULL, 0 ) ) != 0 ) {
128 DBG ( "SPI %p failed to write-enable device\n", device );
132 if ( ( rc = bus->rw ( bus, device, command, address,
133 data, NULL, len ) ) != 0 ) {
134 DBG ( "SPI %p failed to write data to device\n", device );
138 if ( ( rc = spi_wait ( device ) ) != 0 ) {
139 DBG ( "SPI %p failed to complete write operation\n", device );