Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

threewire.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <string.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <gpxe/threewire.h>
  24. /** @file
  25. *
  26. * Three-wire serial devices
  27. *
  28. */
  29. /**
  30. * Read data from three-wire device
  31. *
  32. * @v nvs NVS device
  33. * @v address Address from which to read
  34. * @v data Data buffer
  35. * @v len Length of data buffer
  36. * @ret rc Return status code
  37. */
  38. int threewire_read ( struct nvs_device *nvs, unsigned int address,
  39. void *data, size_t len ) {
  40. struct spi_device *device = nvs_to_spi ( nvs );
  41. struct spi_bus *bus = device->bus;
  42. int rc;
  43. assert ( bus->mode == SPI_MODE_THREEWIRE );
  44. DBGC ( device, "3wire %p reading %zd bytes at %04x\n",
  45. device, len, address );
  46. if ( ( rc = bus->rw ( bus, device, THREEWIRE_READ, address,
  47. NULL, data, len ) ) != 0 ) {
  48. DBGC ( device, "3wire %p could not read: %s\n",
  49. device, strerror ( rc ) );
  50. return rc;
  51. }
  52. return 0;
  53. }
  54. /**
  55. * Write data to three-wire device
  56. *
  57. * @v nvs NVS device
  58. * @v address Address from which to read
  59. * @v data Data buffer
  60. * @v len Length of data buffer
  61. * @ret rc Return status code
  62. */
  63. int threewire_write ( struct nvs_device *nvs, unsigned int address,
  64. const void *data, size_t len ) {
  65. struct spi_device *device = nvs_to_spi ( nvs );
  66. struct spi_bus *bus = device->bus;
  67. int rc;
  68. assert ( bus->mode == SPI_MODE_THREEWIRE );
  69. DBGC ( device, "3wire %p writing %zd bytes at %04x\n",
  70. device, len, address );
  71. /* Enable device for writing */
  72. if ( ( rc = bus->rw ( bus, device, THREEWIRE_EWEN,
  73. THREEWIRE_EWEN_ADDRESS, NULL, NULL, 0 ) ) != 0 ){
  74. DBGC ( device, "3wire %p could not enable writing: %s\n",
  75. device, strerror ( rc ) );
  76. return rc;
  77. }
  78. /* Write data */
  79. if ( ( rc = bus->rw ( bus, device, THREEWIRE_WRITE, address,
  80. data, NULL, len ) ) != 0 ) {
  81. DBGC ( device, "3wire %p could not write: %s\n",
  82. device, strerror ( rc ) );
  83. return rc;
  84. }
  85. /* Our model of an SPI bus doesn't provide a mechanism for
  86. * "assert CS, wait for MISO to become high, so just wait for
  87. * long enough to ensure that the write has completed.
  88. */
  89. mdelay ( THREEWIRE_WRITE_MDELAY );
  90. return 0;
  91. }
  92. /**
  93. * Autodetect device address length
  94. *
  95. * @v device SPI device
  96. * @ret rc Return status code
  97. */
  98. int threewire_detect_address_len ( struct spi_device *device ) {
  99. struct nvs_device *nvs = &device->nvs;
  100. int rc;
  101. DBGC ( device, "3wire %p autodetecting address length\n", device );
  102. device->address_len = SPI_AUTODETECT_ADDRESS_LEN;
  103. if ( ( rc = threewire_read ( nvs, 0, NULL,
  104. ( 1 << nvs->word_len_log2 ) ) ) != 0 ) {
  105. DBGC ( device, "3wire %p could not autodetect address "
  106. "length: %s\n", device, strerror ( rc ) );
  107. return rc;
  108. }
  109. DBGC ( device, "3wire %p autodetected address length %d\n",
  110. device, device->address_len );
  111. return 0;
  112. }