iPXE
|
00001 /* 00002 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>. 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License as 00006 * published by the Free Software Foundation; either version 2 of the 00007 * License, or any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 * 02110-1301, USA. 00018 * 00019 * You can also choose to distribute this program under the terms of 00020 * the Unmodified Binary Distribution Licence (as given in the file 00021 * COPYING.UBDL), provided that you have satisfied its requirements. 00022 */ 00023 00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); 00025 00026 #include <stddef.h> 00027 #include <errno.h> 00028 #include <unistd.h> 00029 #include <ipxe/spi.h> 00030 00031 /** @file 00032 * 00033 * SPI devices 00034 * 00035 */ 00036 00037 /** 00038 * Munge SPI device address into command 00039 * 00040 * @v command SPI command 00041 * @v address Address 00042 * @v munge_address Device requires address munging 00043 * @ret command Actual SPI command to use 00044 * 00045 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3 00046 * of the command byte as address bit A8, rather than having a 00047 * two-byte address. This function takes care of generating the 00048 * appropriate command. 00049 */ 00050 static inline unsigned int spi_command ( unsigned int command, 00051 unsigned int address, 00052 int munge_address ) { 00053 return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) ); 00054 } 00055 00056 /** 00057 * Wait for SPI device to complete operation 00058 * 00059 * @v device SPI device 00060 * @ret rc Return status code 00061 */ 00062 static int spi_wait ( struct spi_device *device ) { 00063 struct spi_bus *bus = device->bus; 00064 uint8_t status; 00065 int i; 00066 int rc; 00067 00068 for ( i = 0 ; i < 50 ; i++ ) { 00069 udelay ( 20 ); 00070 if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL, 00071 &status, sizeof ( status ) ) ) != 0 ) 00072 return rc; 00073 if ( ! ( status & SPI_STATUS_NRDY ) ) 00074 return 0; 00075 } 00076 DBG ( "SPI %p timed out\n", device ); 00077 return -ETIMEDOUT; 00078 } 00079 00080 /** 00081 * Read data from SPI device 00082 * 00083 * @v nvs NVS device 00084 * @v address Address from which to read 00085 * @v data Data buffer 00086 * @v len Length of data buffer 00087 * @ret rc Return status code 00088 */ 00089 int spi_read ( struct nvs_device *nvs, unsigned int address, 00090 void *data, size_t len ) { 00091 struct spi_device *device = nvs_to_spi ( nvs ); 00092 struct spi_bus *bus = device->bus; 00093 unsigned int command = spi_command ( SPI_READ, address, 00094 device->munge_address ); 00095 int rc; 00096 00097 DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address ); 00098 if ( ( rc = bus->rw ( bus, device, command, address, 00099 NULL, data, len ) ) != 0 ) { 00100 DBG ( "SPI %p failed to read data from device\n", device ); 00101 return rc; 00102 } 00103 00104 return 0; 00105 } 00106 00107 /** 00108 * Write data to SPI device 00109 * 00110 * @v nvs NVS device 00111 * @v address Address from which to read 00112 * @v data Data buffer 00113 * @v len Length of data buffer 00114 * @ret rc Return status code 00115 */ 00116 int spi_write ( struct nvs_device *nvs, unsigned int address, 00117 const void *data, size_t len ) { 00118 struct spi_device *device = nvs_to_spi ( nvs ); 00119 struct spi_bus *bus = device->bus; 00120 unsigned int command = spi_command ( SPI_WRITE, address, 00121 device->munge_address ); 00122 int rc; 00123 00124 DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address ); 00125 00126 if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1, 00127 NULL, NULL, 0 ) ) != 0 ) { 00128 DBG ( "SPI %p failed to write-enable device\n", device ); 00129 return rc; 00130 } 00131 00132 if ( ( rc = bus->rw ( bus, device, command, address, 00133 data, NULL, len ) ) != 0 ) { 00134 DBG ( "SPI %p failed to write data to device\n", device ); 00135 return rc; 00136 } 00137 00138 if ( ( rc = spi_wait ( device ) ) != 0 ) { 00139 DBG ( "SPI %p failed to complete write operation\n", device ); 00140 return rc; 00141 } 00142 00143 return 0; 00144 } 00145