iPXE
threewire.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <string.h>
00028 #include <assert.h>
00029 #include <unistd.h>
00030 #include <ipxe/threewire.h>
00031 
00032 /** @file
00033  *
00034  * Three-wire serial devices
00035  *
00036  */
00037 
00038 /**
00039  * Read data from three-wire device
00040  *
00041  * @v nvs               NVS device
00042  * @v address           Address from which to read
00043  * @v data              Data buffer
00044  * @v len               Length of data buffer
00045  * @ret rc              Return status code
00046  */
00047 int threewire_read ( struct nvs_device *nvs, unsigned int address,
00048                      void *data, size_t len ) {
00049         struct spi_device *device = nvs_to_spi ( nvs );
00050         struct spi_bus *bus = device->bus;
00051         int rc;
00052 
00053         assert ( bus->mode == SPI_MODE_THREEWIRE );
00054 
00055         DBGC ( device, "3wire %p reading %zd bytes at %04x\n",
00056                device, len, address );
00057 
00058         if ( ( rc = bus->rw ( bus, device, THREEWIRE_READ, address,
00059                               NULL, data, len ) ) != 0 ) {
00060                 DBGC ( device, "3wire %p could not read: %s\n",
00061                        device, strerror ( rc ) );
00062                 return rc;
00063         }
00064 
00065         return 0;
00066 }
00067 
00068 /**
00069  * Write data to three-wire device
00070  *
00071  * @v nvs               NVS device
00072  * @v address           Address from which to read
00073  * @v data              Data buffer
00074  * @v len               Length of data buffer
00075  * @ret rc              Return status code
00076  */
00077 int threewire_write ( struct nvs_device *nvs, unsigned int address,
00078                       const void *data, size_t len ) {
00079         struct spi_device *device = nvs_to_spi ( nvs );
00080         struct spi_bus *bus = device->bus;
00081         int rc;
00082 
00083         assert ( bus->mode == SPI_MODE_THREEWIRE );
00084 
00085         DBGC ( device, "3wire %p writing %zd bytes at %04x\n",
00086                device, len, address );
00087 
00088         /* Enable device for writing */
00089         if ( ( rc = bus->rw ( bus, device, THREEWIRE_EWEN,
00090                               THREEWIRE_EWEN_ADDRESS, NULL, NULL, 0 ) ) != 0 ){
00091                 DBGC ( device, "3wire %p could not enable writing: %s\n",
00092                        device, strerror ( rc ) );
00093                 return rc;
00094         }
00095 
00096         /* Write data */
00097         if ( ( rc = bus->rw ( bus, device, THREEWIRE_WRITE, address,
00098                               data, NULL, len ) ) != 0 ) {
00099                 DBGC ( device, "3wire %p could not write: %s\n",
00100                        device, strerror ( rc ) );
00101                 return rc;
00102         }
00103 
00104         /* Our model of an SPI bus doesn't provide a mechanism for
00105          * "assert CS, wait for MISO to become high, so just wait for
00106          * long enough to ensure that the write has completed.
00107          */
00108         mdelay ( THREEWIRE_WRITE_MDELAY );
00109 
00110         return 0;
00111 }
00112 
00113 /**
00114  * Autodetect device address length
00115  *
00116  * @v device            SPI device
00117  * @ret rc              Return status code
00118  */
00119 int threewire_detect_address_len ( struct spi_device *device ) {
00120         struct nvs_device *nvs = &device->nvs;
00121         int rc;
00122 
00123         DBGC ( device, "3wire %p autodetecting address length\n", device );
00124 
00125         device->address_len = SPI_AUTODETECT_ADDRESS_LEN;
00126         if ( ( rc = threewire_read ( nvs, 0, NULL,
00127                                      ( 1 << nvs->word_len_log2 ) ) ) != 0 ) {
00128                 DBGC ( device, "3wire %p could not autodetect address "
00129                        "length: %s\n", device, strerror ( rc ) );
00130                 return rc;
00131         }
00132 
00133         DBGC ( device, "3wire %p autodetected address length %d\n",
00134                device, device->address_len );
00135         return 0;
00136 }