You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threewire.c 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <assert.h>
  20. #include <timer.h>
  21. #include <gpxe/threewire.h>
  22. /** @file
  23. *
  24. * Three-wire serial devices
  25. *
  26. */
  27. /**
  28. * Read data from three-wire device
  29. *
  30. * @v nvs NVS device
  31. * @v address Address from which to read
  32. * @v data Data buffer
  33. * @v len Length of data buffer
  34. * @ret rc Return status code
  35. */
  36. int threewire_read ( struct nvs_device *nvs, unsigned int address,
  37. void *data, size_t len ) {
  38. struct spi_device *device = nvs_to_spi ( nvs );
  39. struct spi_bus *bus = device->bus;
  40. assert ( bus->mode == SPI_MODE_THREEWIRE );
  41. DBG ( "3wire %p reading %zd bytes at %04x\n", device, len, address );
  42. return bus->rw ( bus, device, THREEWIRE_READ, address,
  43. NULL, data, len );
  44. }
  45. /**
  46. * Write data to three-wire device
  47. *
  48. * @v nvs NVS device
  49. * @v address Address from which to read
  50. * @v data Data buffer
  51. * @v len Length of data buffer
  52. * @ret rc Return status code
  53. */
  54. int threewire_write ( struct nvs_device *nvs, unsigned int address,
  55. const void *data, size_t len ) {
  56. struct spi_device *device = nvs_to_spi ( nvs );
  57. struct spi_bus *bus = device->bus;
  58. int rc;
  59. assert ( bus->mode == SPI_MODE_THREEWIRE );
  60. DBG ( "3wire %p writing %zd bytes at %04x\n", device, len, address );
  61. /* Enable device for writing */
  62. if ( ( rc = bus->rw ( bus, device, THREEWIRE_EWEN,
  63. THREEWIRE_EWEN_ADDRESS, NULL, NULL, 0 ) ) != 0 )
  64. return rc;
  65. /* Write data */
  66. if ( ( rc = bus->rw ( bus, device, THREEWIRE_WRITE, address,
  67. data, NULL, len ) ) != 0 )
  68. return rc;
  69. /* Our model of an SPI bus doesn't provide a mechanism for
  70. * "assert CS, wait for MISO to become high, so just wait for
  71. * long enough to ensure that the write has completed.
  72. */
  73. mdelay ( THREEWIRE_WRITE_MDELAY );
  74. return 0;
  75. }