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.

threewire.c 3.8KB

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