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.
31 * Munge SPI device address into command
33 * @v command SPI command
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
,
46 return ( command
| ( ( ( address
>> 8 ) & munge_address
) << 3 ) );
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
;
61 for ( i
= 0 ; i
< 50 ; i
++ ) {
63 if ( ( rc
= bus
->rw ( bus
, device
, SPI_RDSR
, -1, NULL
,
64 &status
, sizeof ( status
) ) ) != 0 )
66 if ( ! ( status
& SPI_STATUS_NRDY
) )
69 DBG ( "SPI %p timed out\n", device
);
74 * Read data from SPI device
77 * @v address Address from which to read
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
);
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
);
101 * Write data to SPI 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
);
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
);
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
);
131 if ( ( rc
= spi_wait ( device
) ) != 0 ) {
132 DBG ( "SPI %p failed to complete write operation\n", device
);