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
20 FILE_LICENCE ( GPL2_OR_LATER );
34 * Munge SPI device address into command
36 * @v command SPI command
38 * @v munge_address Device requires address munging
39 * @ret command Actual SPI command to use
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.
46 static inline unsigned int spi_command ( unsigned int command,
49 return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) );
53 * Wait for SPI device to complete operation
55 * @v device SPI device
56 * @ret rc Return status code
58 static int spi_wait ( struct spi_device *device ) {
59 struct spi_bus *bus = device->bus;
64 for ( i = 0 ; i < 50 ; i++ ) {
66 if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL,
67 &status, sizeof ( status ) ) ) != 0 )
69 if ( ! ( status & SPI_STATUS_NRDY ) )
72 DBG ( "SPI %p timed out\n", device );
77 * Read data from SPI device
80 * @v address Address from which to read
82 * @v len Length of data buffer
83 * @ret rc Return status code
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 );
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 );
104 * Write data to SPI 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
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 );
120 DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
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 );
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 );
134 if ( ( rc = spi_wait ( device ) ) != 0 ) {
135 DBG ( "SPI %p failed to complete write operation\n", device );