Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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