Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / nvs / threewire.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <unistd.h>
26 #include <ipxe/threewire.h>
27
28 /** @file
29  *
30  * Three-wire serial devices
31  *
32  */
33
34 /**
35  * Read data from three-wire device
36  *
37  * @v nvs               NVS device
38  * @v address           Address from which to read
39  * @v data              Data buffer
40  * @v len               Length of data buffer
41  * @ret rc              Return status code
42  */
43 int threewire_read ( struct nvs_device *nvs, unsigned int address,
44                      void *data, size_t len ) {
45         struct spi_device *device = nvs_to_spi ( nvs );
46         struct spi_bus *bus = device->bus;
47         int rc;
48
49         assert ( bus->mode == SPI_MODE_THREEWIRE );
50
51         DBGC ( device, "3wire %p reading %zd bytes at %04x\n",
52                device, len, address );
53
54         if ( ( rc = bus->rw ( bus, device, THREEWIRE_READ, address,
55                               NULL, data, len ) ) != 0 ) {
56                 DBGC ( device, "3wire %p could not read: %s\n",
57                        device, strerror ( rc ) );
58                 return rc;
59         }
60
61         return 0;
62 }
63
64 /**
65  * Write data to three-wire device
66  *
67  * @v nvs               NVS device
68  * @v address           Address from which to read
69  * @v data              Data buffer
70  * @v len               Length of data buffer
71  * @ret rc              Return status code
72  */
73 int threewire_write ( struct nvs_device *nvs, unsigned int address,
74                       const void *data, size_t len ) {
75         struct spi_device *device = nvs_to_spi ( nvs );
76         struct spi_bus *bus = device->bus;
77         int rc;
78
79         assert ( bus->mode == SPI_MODE_THREEWIRE );
80
81         DBGC ( device, "3wire %p writing %zd bytes at %04x\n",
82                device, len, address );
83
84         /* Enable device for writing */
85         if ( ( rc = bus->rw ( bus, device, THREEWIRE_EWEN,
86                               THREEWIRE_EWEN_ADDRESS, NULL, NULL, 0 ) ) != 0 ){
87                 DBGC ( device, "3wire %p could not enable writing: %s\n",
88                        device, strerror ( rc ) );
89                 return rc;
90         }
91
92         /* Write data */
93         if ( ( rc = bus->rw ( bus, device, THREEWIRE_WRITE, address,
94                               data, NULL, len ) ) != 0 ) {
95                 DBGC ( device, "3wire %p could not write: %s\n",
96                        device, strerror ( rc ) );
97                 return rc;
98         }
99
100         /* Our model of an SPI bus doesn't provide a mechanism for
101          * "assert CS, wait for MISO to become high, so just wait for
102          * long enough to ensure that the write has completed.
103          */
104         mdelay ( THREEWIRE_WRITE_MDELAY );
105
106         return 0;
107 }
108
109 /**
110  * Autodetect device address length
111  *
112  * @v device            SPI device
113  * @ret rc              Return status code
114  */
115 int threewire_detect_address_len ( struct spi_device *device ) {
116         struct nvs_device *nvs = &device->nvs;
117         int rc;
118
119         DBGC ( device, "3wire %p autodetecting address length\n", device );
120
121         device->address_len = SPI_AUTODETECT_ADDRESS_LEN;
122         if ( ( rc = threewire_read ( nvs, 0, NULL,
123                                      ( 1 << nvs->word_len_log2 ) ) ) != 0 ) {
124                 DBGC ( device, "3wire %p could not autodetect address "
125                        "length: %s\n", device, strerror ( rc ) );
126                 return rc;
127         }
128
129         DBGC ( device, "3wire %p autodetected address length %d\n",
130                device, device->address_len );
131         return 0;
132 }