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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <errno.h>
  20. #include <timer.h>
  21. #include <gpxe/spi.h>
  22. /** @file
  23. *
  24. * SPI devices
  25. *
  26. */
  27. /**
  28. * Munge SPI device address into command
  29. *
  30. * @v command SPI command
  31. * @v address Address
  32. * @v munge_address Device requires address munging
  33. * @ret command Actual SPI command to use
  34. *
  35. * Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3
  36. * of the command byte as address bit A8, rather than having a
  37. * two-byte address. This function takes care of generating the
  38. * appropriate command.
  39. */
  40. static inline unsigned int spi_command ( unsigned int command,
  41. unsigned int address,
  42. int munge_address ) {
  43. return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) );
  44. }
  45. /**
  46. * Wait for SPI device to complete operation
  47. *
  48. * @v device SPI device
  49. * @ret rc Return status code
  50. */
  51. static int spi_wait ( struct spi_device *device ) {
  52. struct spi_bus *bus = device->bus;
  53. uint8_t status;
  54. int i;
  55. int rc;
  56. for ( i = 0 ; i < 50 ; i++ ) {
  57. udelay ( 20 );
  58. if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL,
  59. &status, sizeof ( status ) ) ) != 0 )
  60. return rc;
  61. if ( ! ( status & SPI_STATUS_NRDY ) )
  62. return 0;
  63. }
  64. DBG ( "SPI %p timed out\n", device );
  65. return -ETIMEDOUT;
  66. }
  67. /**
  68. * Read data from SPI device
  69. *
  70. * @v nvs NVS device
  71. * @v address Address from which to read
  72. * @v data Data buffer
  73. * @v len Length of data buffer
  74. * @ret rc Return status code
  75. */
  76. int spi_read ( struct nvs_device *nvs, unsigned int address,
  77. void *data, size_t len ) {
  78. struct spi_device *device = nvs_to_spi ( nvs );
  79. struct spi_bus *bus = device->bus;
  80. unsigned int command = spi_command ( SPI_READ, address,
  81. device->munge_address );
  82. int rc;
  83. DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address );
  84. if ( ( rc = bus->rw ( bus, device, command, address,
  85. NULL, data, len ) ) != 0 ) {
  86. DBG ( "SPI %p failed to read data from device\n", device );
  87. return rc;
  88. }
  89. return 0;
  90. }
  91. /**
  92. * Write data to SPI device
  93. *
  94. * @v nvs NVS device
  95. * @v address Address from which to read
  96. * @v data Data buffer
  97. * @v len Length of data buffer
  98. * @ret rc Return status code
  99. */
  100. int spi_write ( struct nvs_device *nvs, unsigned int address,
  101. const void *data, size_t len ) {
  102. struct spi_device *device = nvs_to_spi ( nvs );
  103. struct spi_bus *bus = device->bus;
  104. unsigned int command = spi_command ( SPI_WRITE, address,
  105. device->munge_address );
  106. int rc;
  107. DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
  108. if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
  109. NULL, NULL, 0 ) ) != 0 ) {
  110. DBG ( "SPI %p failed to write-enable device\n", device );
  111. return rc;
  112. }
  113. if ( ( rc = bus->rw ( bus, device, command, address,
  114. data, NULL, len ) ) != 0 ) {
  115. DBG ( "SPI %p failed to write data to device\n", device );
  116. return rc;
  117. }
  118. if ( ( rc = spi_wait ( device ) ) != 0 ) {
  119. DBG ( "SPI %p failed to complete write operation\n", device );
  120. return rc;
  121. }
  122. return 0;
  123. }