blob: dcfe1af91b39b15b46abf5a56e1a442b538373af [file] [log] [blame]
Michael Brown1961ba42006-12-04 18:32:55 +00001/*
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 Brownc3b48602012-07-20 19:55:45 +010016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
Michael Brownb6ee89f2015-03-02 11:54:40 +000018 *
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 Brown1961ba42006-12-04 18:32:55 +000022 */
23
Michael Brownb6ee89f2015-03-02 11:54:40 +000024FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
Michael Brownc44a1932009-05-01 15:41:06 +010025
Michael Brown1961ba42006-12-04 18:32:55 +000026#include <stddef.h>
27#include <errno.h>
Alexey Zaytseva1572e02008-03-02 03:41:10 +030028#include <unistd.h>
Michael Brown84061152010-04-19 20:16:01 +010029#include <ipxe/spi.h>
Michael Brown1961ba42006-12-04 18:32:55 +000030
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 */
50static 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 */
62static 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 */
89int spi_read ( struct nvs_device *nvs, unsigned int address,
Michael Brown64787ba2007-12-06 23:35:37 +000090 void *data, size_t len ) {
Michael Brown1961ba42006-12-04 18:32:55 +000091 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 Brown6b459472006-12-04 23:20:56 +000095 int rc;
Michael Brown1961ba42006-12-04 18:32:55 +000096
Michael Brown19496412007-12-06 14:16:46 -060097 DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address );
Michael Brown6b459472006-12-04 23:20:56 +000098 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 Brown1961ba42006-12-04 18:32:55 +0000105}
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 */
116int spi_write ( struct nvs_device *nvs, unsigned int address,
Michael Brown64787ba2007-12-06 23:35:37 +0000117 const void *data, size_t len ) {
Michael Brown1961ba42006-12-04 18:32:55 +0000118 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 Brown19496412007-12-06 14:16:46 -0600124 DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
Michael Brown6b459472006-12-04 23:20:56 +0000125
Michael Brown1961ba42006-12-04 18:32:55 +0000126 if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
Michael Brown6b459472006-12-04 23:20:56 +0000127 NULL, NULL, 0 ) ) != 0 ) {
128 DBG ( "SPI %p failed to write-enable device\n", device );
Michael Brown1961ba42006-12-04 18:32:55 +0000129 return rc;
Michael Brown6b459472006-12-04 23:20:56 +0000130 }
Michael Brown1961ba42006-12-04 18:32:55 +0000131
132 if ( ( rc = bus->rw ( bus, device, command, address,
Michael Brown6b459472006-12-04 23:20:56 +0000133 data, NULL, len ) ) != 0 ) {
134 DBG ( "SPI %p failed to write data to device\n", device );
Michael Brown1961ba42006-12-04 18:32:55 +0000135 return rc;
Michael Brown6b459472006-12-04 23:20:56 +0000136 }
Michael Brown1961ba42006-12-04 18:32:55 +0000137
Michael Brown6b459472006-12-04 23:20:56 +0000138 if ( ( rc = spi_wait ( device ) ) != 0 ) {
139 DBG ( "SPI %p failed to complete write operation\n", device );
Michael Brown1961ba42006-12-04 18:32:55 +0000140 return rc;
Michael Brown6b459472006-12-04 23:20:56 +0000141 }
Michael Brown1961ba42006-12-04 18:32:55 +0000142
143 return 0;
144}
145