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., 51 Franklin Street, Fifth Floor, Boston, MA
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
30 #include <ipxe/threewire.h>
34 * Three-wire serial devices
39 * Read data from three-wire device
42 * @v address Address from which to read
44 * @v len Length of data buffer
45 * @ret rc Return status code
47 int threewire_read ( struct nvs_device
*nvs
, unsigned int address
,
48 void *data
, size_t len
) {
49 struct spi_device
*device
= nvs_to_spi ( nvs
);
50 struct spi_bus
*bus
= device
->bus
;
53 assert ( bus
->mode
== SPI_MODE_THREEWIRE
);
55 DBGC ( device
, "3wire %p reading %zd bytes at %04x\n",
56 device
, len
, address
);
58 if ( ( rc
= bus
->rw ( bus
, device
, THREEWIRE_READ
, address
,
59 NULL
, data
, len
) ) != 0 ) {
60 DBGC ( device
, "3wire %p could not read: %s\n",
61 device
, strerror ( rc
) );
69 * Write data to three-wire device
72 * @v address Address from which to read
74 * @v len Length of data buffer
75 * @ret rc Return status code
77 int threewire_write ( struct nvs_device
*nvs
, unsigned int address
,
78 const void *data
, size_t len
) {
79 struct spi_device
*device
= nvs_to_spi ( nvs
);
80 struct spi_bus
*bus
= device
->bus
;
83 assert ( bus
->mode
== SPI_MODE_THREEWIRE
);
85 DBGC ( device
, "3wire %p writing %zd bytes at %04x\n",
86 device
, len
, address
);
88 /* Enable device for writing */
89 if ( ( rc
= bus
->rw ( bus
, device
, THREEWIRE_EWEN
,
90 THREEWIRE_EWEN_ADDRESS
, NULL
, NULL
, 0 ) ) != 0 ){
91 DBGC ( device
, "3wire %p could not enable writing: %s\n",
92 device
, strerror ( rc
) );
97 if ( ( rc
= bus
->rw ( bus
, device
, THREEWIRE_WRITE
, address
,
98 data
, NULL
, len
) ) != 0 ) {
99 DBGC ( device
, "3wire %p could not write: %s\n",
100 device
, strerror ( rc
) );
104 /* Our model of an SPI bus doesn't provide a mechanism for
105 * "assert CS, wait for MISO to become high, so just wait for
106 * long enough to ensure that the write has completed.
108 mdelay ( THREEWIRE_WRITE_MDELAY
);
114 * Autodetect device address length
116 * @v device SPI device
117 * @ret rc Return status code
119 int threewire_detect_address_len ( struct spi_device
*device
) {
120 struct nvs_device
*nvs
= &device
->nvs
;
123 DBGC ( device
, "3wire %p autodetecting address length\n", device
);
125 device
->address_len
= SPI_AUTODETECT_ADDRESS_LEN
;
126 if ( ( rc
= threewire_read ( nvs
, 0, NULL
,
127 ( 1 << nvs
->word_len_log2
) ) ) != 0 ) {
128 DBGC ( device
, "3wire %p could not autodetect address "
129 "length: %s\n", device
, strerror ( rc
) );
133 DBGC ( device
, "3wire %p autodetected address length %d\n",
134 device
, device
->address_len
);