a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/*
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is free software; you can redistribute it and/or
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * modify it under the terms of the GNU General Public License as
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * published by the Free Software Foundation; either version 2 of the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * License, or any later version.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is distributed in the hope that it will be useful, but
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * WITHOUT ANY WARRANTY; without even the implied warranty of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * General Public License for more details.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * You should have received a copy of the GNU General Public License
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * along with this program; if not, write to the Free Software
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncFILE_LICENCE ( GPL2_OR_LATER );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <stddef.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <string.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <assert.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <unistd.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/threewire.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/** @file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Three-wire serial devices
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Read data from three-wire device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v nvs NVS device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v address Address from which to read
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v data Data buffer
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v len Length of data buffer
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncint threewire_read ( struct nvs_device *nvs, unsigned int address,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync void *data, size_t len ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct spi_device *device = nvs_to_spi ( nvs );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct spi_bus *bus = device->bus;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync int rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync assert ( bus->mode == SPI_MODE_THREEWIRE );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p reading %zd bytes at %04x\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device, len, address );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( rc = bus->rw ( bus, device, THREEWIRE_READ, address,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync NULL, data, len ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p could not read: %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device, strerror ( rc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Write data to three-wire device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v nvs NVS device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v address Address from which to read
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v data Data buffer
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v len Length of data buffer
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncint threewire_write ( struct nvs_device *nvs, unsigned int address,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync const void *data, size_t len ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct spi_device *device = nvs_to_spi ( nvs );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct spi_bus *bus = device->bus;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync int rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync assert ( bus->mode == SPI_MODE_THREEWIRE );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p writing %zd bytes at %04x\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device, len, address );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Enable device for writing */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( rc = bus->rw ( bus, device, THREEWIRE_EWEN,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync THREEWIRE_EWEN_ADDRESS, NULL, NULL, 0 ) ) != 0 ){
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p could not enable writing: %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device, strerror ( rc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Write data */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( rc = bus->rw ( bus, device, THREEWIRE_WRITE, address,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync data, NULL, len ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p could not write: %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device, strerror ( rc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Our model of an SPI bus doesn't provide a mechanism for
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * "assert CS, wait for MISO to become high, so just wait for
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * long enough to ensure that the write has completed.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync mdelay ( THREEWIRE_WRITE_MDELAY );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Autodetect device address length
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v device SPI device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncint threewire_detect_address_len ( struct spi_device *device ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct nvs_device *nvs = &device->nvs;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync int rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p autodetecting address length\n", device );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device->address_len = SPI_AUTODETECT_ADDRESS_LEN;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( rc = threewire_read ( nvs, 0, NULL,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ( 1 << nvs->word_len_log2 ) ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p could not autodetect address "
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync "length: %s\n", device, strerror ( rc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( device, "3wire %p autodetected address length %d\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync device, device->address_len );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}