Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>. |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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 |
Michael Brown | c3b4860 | 2012-07-20 19:55:45 +0100 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301, USA. |
Michael Brown | b6ee89f | 2015-03-02 11:54:40 +0000 | [diff] [blame] | 18 | * |
| 19 | * You can also choose to distribute this program under the terms of |
| 20 | * the Unmodified Binary Distribution Licence (as given in the file |
| 21 | * COPYING.UBDL), provided that you have satisfied its requirements. |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Michael Brown | b6ee89f | 2015-03-02 11:54:40 +0000 | [diff] [blame] | 24 | FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); |
Michael Brown | c44a193 | 2009-05-01 15:41:06 +0100 | [diff] [blame] | 25 | |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 26 | #include <stddef.h> |
| 27 | #include <errno.h> |
Alexey Zaytsev | a1572e0 | 2008-03-02 03:41:10 +0300 | [diff] [blame] | 28 | #include <unistd.h> |
Michael Brown | 8406115 | 2010-04-19 20:16:01 +0100 | [diff] [blame] | 29 | #include <ipxe/spi.h> |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 30 | |
| 31 | /** @file |
| 32 | * |
| 33 | * SPI devices |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Munge SPI device address into command |
| 39 | * |
| 40 | * @v command SPI command |
| 41 | * @v address Address |
| 42 | * @v munge_address Device requires address munging |
| 43 | * @ret command Actual SPI command to use |
| 44 | * |
| 45 | * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3 |
| 46 | * of the command byte as address bit A8, rather than having a |
| 47 | * two-byte address. This function takes care of generating the |
| 48 | * appropriate command. |
| 49 | */ |
| 50 | static inline unsigned int spi_command ( unsigned int command, |
| 51 | unsigned int address, |
| 52 | int munge_address ) { |
| 53 | return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Wait for SPI device to complete operation |
| 58 | * |
| 59 | * @v device SPI device |
| 60 | * @ret rc Return status code |
| 61 | */ |
| 62 | static int spi_wait ( struct spi_device *device ) { |
| 63 | struct spi_bus *bus = device->bus; |
| 64 | uint8_t status; |
| 65 | int i; |
| 66 | int rc; |
| 67 | |
| 68 | for ( i = 0 ; i < 50 ; i++ ) { |
| 69 | udelay ( 20 ); |
| 70 | if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL, |
| 71 | &status, sizeof ( status ) ) ) != 0 ) |
| 72 | return rc; |
| 73 | if ( ! ( status & SPI_STATUS_NRDY ) ) |
| 74 | return 0; |
| 75 | } |
| 76 | DBG ( "SPI %p timed out\n", device ); |
| 77 | return -ETIMEDOUT; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Read data from SPI device |
| 82 | * |
| 83 | * @v nvs NVS device |
| 84 | * @v address Address from which to read |
| 85 | * @v data Data buffer |
| 86 | * @v len Length of data buffer |
| 87 | * @ret rc Return status code |
| 88 | */ |
| 89 | int spi_read ( struct nvs_device *nvs, unsigned int address, |
Michael Brown | 64787ba | 2007-12-06 23:35:37 +0000 | [diff] [blame] | 90 | void *data, size_t len ) { |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 91 | struct spi_device *device = nvs_to_spi ( nvs ); |
| 92 | struct spi_bus *bus = device->bus; |
| 93 | unsigned int command = spi_command ( SPI_READ, address, |
| 94 | device->munge_address ); |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 95 | int rc; |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 96 | |
Michael Brown | 1949641 | 2007-12-06 14:16:46 -0600 | [diff] [blame] | 97 | DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address ); |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 98 | if ( ( rc = bus->rw ( bus, device, command, address, |
| 99 | NULL, data, len ) ) != 0 ) { |
| 100 | DBG ( "SPI %p failed to read data from device\n", device ); |
| 101 | return rc; |
| 102 | } |
| 103 | |
| 104 | return 0; |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Write data to SPI device |
| 109 | * |
| 110 | * @v nvs NVS device |
| 111 | * @v address Address from which to read |
| 112 | * @v data Data buffer |
| 113 | * @v len Length of data buffer |
| 114 | * @ret rc Return status code |
| 115 | */ |
| 116 | int spi_write ( struct nvs_device *nvs, unsigned int address, |
Michael Brown | 64787ba | 2007-12-06 23:35:37 +0000 | [diff] [blame] | 117 | const void *data, size_t len ) { |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 118 | struct spi_device *device = nvs_to_spi ( nvs ); |
| 119 | struct spi_bus *bus = device->bus; |
| 120 | unsigned int command = spi_command ( SPI_WRITE, address, |
| 121 | device->munge_address ); |
| 122 | int rc; |
| 123 | |
Michael Brown | 1949641 | 2007-12-06 14:16:46 -0600 | [diff] [blame] | 124 | DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address ); |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 125 | |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 126 | if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1, |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 127 | NULL, NULL, 0 ) ) != 0 ) { |
| 128 | DBG ( "SPI %p failed to write-enable device\n", device ); |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 129 | return rc; |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 130 | } |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 131 | |
| 132 | if ( ( rc = bus->rw ( bus, device, command, address, |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 133 | data, NULL, len ) ) != 0 ) { |
| 134 | DBG ( "SPI %p failed to write data to device\n", device ); |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 135 | return rc; |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 136 | } |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 137 | |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 138 | if ( ( rc = spi_wait ( device ) ) != 0 ) { |
| 139 | DBG ( "SPI %p failed to complete write operation\n", device ); |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 140 | return rc; |
Michael Brown | 6b45947 | 2006-12-04 23:20:56 +0000 | [diff] [blame] | 141 | } |
Michael Brown | 1961ba4 | 2006-12-04 18:32:55 +0000 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |