2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
25 #include <gpxe/threewire.h>
29 * Three-wire serial devices
34 * Read data from three-wire device
37 * @v address Address from which to read
39 * @v len Length of data buffer
40 * @ret rc Return status code
42 int threewire_read ( struct nvs_device
*nvs
, unsigned int address
,
43 void *data
, size_t len
) {
44 struct spi_device
*device
= nvs_to_spi ( nvs
);
45 struct spi_bus
*bus
= device
->bus
;
48 assert ( bus
->mode
== SPI_MODE_THREEWIRE
);
50 DBGC ( device
, "3wire %p reading %zd bytes at %04x\n",
51 device
, len
, address
);
53 if ( ( rc
= bus
->rw ( bus
, device
, THREEWIRE_READ
, address
,
54 NULL
, data
, len
) ) != 0 ) {
55 DBGC ( device
, "3wire %p could not read: %s\n",
56 device
, strerror ( rc
) );
64 * Write data to three-wire device
67 * @v address Address from which to read
69 * @v len Length of data buffer
70 * @ret rc Return status code
72 int threewire_write ( struct nvs_device
*nvs
, unsigned int address
,
73 const void *data
, size_t len
) {
74 struct spi_device
*device
= nvs_to_spi ( nvs
);
75 struct spi_bus
*bus
= device
->bus
;
78 assert ( bus
->mode
== SPI_MODE_THREEWIRE
);
80 DBGC ( device
, "3wire %p writing %zd bytes at %04x\n",
81 device
, len
, address
);
83 /* Enable device for writing */
84 if ( ( rc
= bus
->rw ( bus
, device
, THREEWIRE_EWEN
,
85 THREEWIRE_EWEN_ADDRESS
, NULL
, NULL
, 0 ) ) != 0 ){
86 DBGC ( device
, "3wire %p could not enable writing: %s\n",
87 device
, strerror ( rc
) );
92 if ( ( rc
= bus
->rw ( bus
, device
, THREEWIRE_WRITE
, address
,
93 data
, NULL
, len
) ) != 0 ) {
94 DBGC ( device
, "3wire %p could not write: %s\n",
95 device
, strerror ( rc
) );
99 /* Our model of an SPI bus doesn't provide a mechanism for
100 * "assert CS, wait for MISO to become high, so just wait for
101 * long enough to ensure that the write has completed.
103 mdelay ( THREEWIRE_WRITE_MDELAY
);
109 * Autodetect device address length
111 * @v device SPI device
112 * @ret rc Return status code
114 int threewire_detect_address_len ( struct spi_device
*device
) {
115 struct nvs_device
*nvs
= &device
->nvs
;
118 DBGC ( device
, "3wire %p autodetecting address length\n", device
);
120 device
->address_len
= SPI_AUTODETECT_ADDRESS_LEN
;
121 if ( ( rc
= threewire_read ( nvs
, 0, NULL
,
122 ( 1 << nvs
->word_len_log2
) ) ) != 0 ) {
123 DBGC ( device
, "3wire %p could not autodetect address "
124 "length: %s\n", device
, strerror ( rc
) );
128 DBGC ( device
, "3wire %p autodetected address length %d\n",
129 device
, device
->address_len
);