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 FILE_LICENCE ( GPL2_OR_LATER
);
33 * Munge SPI device address into command
35 * @v command SPI command
37 * @v munge_address Device requires address munging
38 * @ret command Actual SPI command to use
40 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3
41 * of the command byte as address bit A8, rather than having a
42 * two-byte address. This function takes care of generating the
43 * appropriate command.
45 static inline unsigned int spi_command ( unsigned int command
,
48 return ( command
| ( ( ( address
>> 8 ) & munge_address
) << 3 ) );
52 * Wait for SPI device to complete operation
54 * @v device SPI device
55 * @ret rc Return status code
57 static int spi_wait ( struct spi_device
*device
) {
58 struct spi_bus
*bus
= device
->bus
;
63 for ( i
= 0 ; i
< 50 ; i
++ ) {
65 if ( ( rc
= bus
->rw ( bus
, device
, SPI_RDSR
, -1, NULL
,
66 &status
, sizeof ( status
) ) ) != 0 )
68 if ( ! ( status
& SPI_STATUS_NRDY
) )
71 DBG ( "SPI %p timed out\n", device
);
76 * Read data from SPI device
79 * @v address Address from which to read
81 * @v len Length of data buffer
82 * @ret rc Return status code
84 int spi_read ( struct nvs_device
*nvs
, unsigned int address
,
85 void *data
, size_t len
) {
86 struct spi_device
*device
= nvs_to_spi ( nvs
);
87 struct spi_bus
*bus
= device
->bus
;
88 unsigned int command
= spi_command ( SPI_READ
, address
,
89 device
->munge_address
);
92 DBG ( "SPI %p reading %zd bytes from %#04x\n", device
, len
, address
);
93 if ( ( rc
= bus
->rw ( bus
, device
, command
, address
,
94 NULL
, data
, len
) ) != 0 ) {
95 DBG ( "SPI %p failed to read data from device\n", device
);
103 * Write data to SPI device
106 * @v address Address from which to read
107 * @v data Data buffer
108 * @v len Length of data buffer
109 * @ret rc Return status code
111 int spi_write ( struct nvs_device
*nvs
, unsigned int address
,
112 const void *data
, size_t len
) {
113 struct spi_device
*device
= nvs_to_spi ( nvs
);
114 struct spi_bus
*bus
= device
->bus
;
115 unsigned int command
= spi_command ( SPI_WRITE
, address
,
116 device
->munge_address
);
119 DBG ( "SPI %p writing %zd bytes to %#04x\n", device
, len
, address
);
121 if ( ( rc
= bus
->rw ( bus
, device
, SPI_WREN
, -1,
122 NULL
, NULL
, 0 ) ) != 0 ) {
123 DBG ( "SPI %p failed to write-enable device\n", device
);
127 if ( ( rc
= bus
->rw ( bus
, device
, command
, address
,
128 data
, NULL
, len
) ) != 0 ) {
129 DBG ( "SPI %p failed to write data to device\n", device
);
133 if ( ( rc
= spi_wait ( device
) ) != 0 ) {
134 DBG ( "SPI %p failed to complete write operation\n", device
);