Start a new LOG file in preparation for release
[gpxe.git] / src / drivers / nvs / spi.c
bloba5bd46a10467bf9294bfc836339ff7b6a25781dc
1 /*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stddef.h>
20 #include <errno.h>
21 #include <timer.h>
22 #include <gpxe/spi.h>
24 /** @file
26 * SPI devices
30 /**
31 * Munge SPI device address into command
33 * @v command SPI command
34 * @v address Address
35 * @v munge_address Device requires address munging
36 * @ret command Actual SPI command to use
38 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3
39 * of the command byte as address bit A8, rather than having a
40 * two-byte address. This function takes care of generating the
41 * appropriate command.
43 static inline unsigned int spi_command ( unsigned int command,
44 unsigned int address,
45 int munge_address ) {
46 return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) );
49 /**
50 * Wait for SPI device to complete operation
52 * @v device SPI device
53 * @ret rc Return status code
55 static int spi_wait ( struct spi_device *device ) {
56 struct spi_bus *bus = device->bus;
57 uint8_t status;
58 int i;
59 int rc;
61 for ( i = 0 ; i < 50 ; i++ ) {
62 udelay ( 20 );
63 if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL,
64 &status, sizeof ( status ) ) ) != 0 )
65 return rc;
66 if ( ! ( status & SPI_STATUS_NRDY ) )
67 return 0;
69 DBG ( "SPI %p timed out\n", device );
70 return -ETIMEDOUT;
73 /**
74 * Read data from SPI device
76 * @v nvs NVS device
77 * @v address Address from which to read
78 * @v data Data buffer
79 * @v len Length of data buffer
80 * @ret rc Return status code
82 int spi_read ( struct nvs_device *nvs, unsigned int address,
83 void *data, unsigned int len ) {
84 struct spi_device *device = nvs_to_spi ( nvs );
85 struct spi_bus *bus = device->bus;
86 unsigned int command = spi_command ( SPI_READ, address,
87 device->munge_address );
88 int rc;
90 DBG ( "SPI %p reading %d bytes from %#04x\n", device, len, address );
91 if ( ( rc = bus->rw ( bus, device, command, address,
92 NULL, data, len ) ) != 0 ) {
93 DBG ( "SPI %p failed to read data from device\n", device );
94 return rc;
97 return 0;
101 * Write data to SPI device
103 * @v nvs NVS device
104 * @v address Address from which to read
105 * @v data Data buffer
106 * @v len Length of data buffer
107 * @ret rc Return status code
109 int spi_write ( struct nvs_device *nvs, unsigned int address,
110 const void *data, unsigned int len ) {
111 struct spi_device *device = nvs_to_spi ( nvs );
112 struct spi_bus *bus = device->bus;
113 unsigned int command = spi_command ( SPI_WRITE, address,
114 device->munge_address );
115 int rc;
117 DBG ( "SPI %p writing %d bytes to %#04x\n", device, len, address );
119 if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
120 NULL, NULL, 0 ) ) != 0 ) {
121 DBG ( "SPI %p failed to write-enable device\n", device );
122 return rc;
125 if ( ( rc = bus->rw ( bus, device, command, address,
126 data, NULL, len ) ) != 0 ) {
127 DBG ( "SPI %p failed to write data to device\n", device );
128 return rc;
131 if ( ( rc = spi_wait ( device ) ) != 0 ) {
132 DBG ( "SPI %p failed to complete write operation\n", device );
133 return rc;
136 return 0;