Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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