Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

spi.c 3.7KB

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