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
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stddef.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <ipxe/spi.h>
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  */
spi_command(unsigned int command,unsigned int address,int munge_address)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  */
spi_wait(struct spi_device * device)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  */
spi_read(struct nvs_device * nvs,unsigned int address,void * data,size_t len)89 int spi_read ( struct nvs_device *nvs, unsigned int address,
90 	       void *data, size_t len ) {
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 );
95 	int rc;
96 
97 	DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address );
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;
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  */
spi_write(struct nvs_device * nvs,unsigned int address,const void * data,size_t len)116 int spi_write ( struct nvs_device *nvs, unsigned int address,
117 		const void *data, size_t len ) {
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 
124 	DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
125 
126 	if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
127 			      NULL, NULL, 0 ) ) != 0 ) {
128 		DBG ( "SPI %p failed to write-enable device\n", device );
129 		return rc;
130 	}
131 
132 	if ( ( rc = bus->rw ( bus, device, command, address,
133 			      data, NULL, len ) ) != 0 ) {
134 		DBG ( "SPI %p failed to write data to device\n", device );
135 		return rc;
136 	}
137 
138 	if ( ( rc = spi_wait ( device ) ) != 0 ) {
139 		DBG ( "SPI %p failed to complete write operation\n", device );
140 		return rc;
141 	}
142 
143 	return 0;
144 }
145 
146