ZuluSCSI_platform.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. // Platform-specific definitions for ZuluSCSI RP2040 hardware.
  22. #pragma once
  23. #include <stdint.h>
  24. #include <Arduino.h>
  25. #include "ZuluSCSI_platform_gpio.h"
  26. #include "scsiHostPhy.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* These are used in debug output and default SCSI strings */
  31. extern const char *g_platform_name;
  32. #define PLATFORM_NAME "ZuluSCSI RP2040"
  33. #define PLATFORM_REVISION "2.0"
  34. #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_10
  35. #define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 32768
  36. #define PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE 65536
  37. #define PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE 8192
  38. #define SD_USE_SDIO 1
  39. #define PLATFORM_HAS_INITIATOR_MODE 1
  40. #define PLATFORM_HAS_PARITY_CHECK 1
  41. // NOTE: The driver supports synchronous speeds higher than 10MB/s, but this
  42. // has not been tested due to lack of fast enough SCSI adapter.
  43. // #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_TURBO
  44. // Debug logging function, can be used to print to e.g. serial port.
  45. // May get called from interrupt handlers.
  46. void platform_log(const char *s);
  47. void platform_emergency_log_save();
  48. // Timing and delay functions.
  49. // Arduino platform already provides these
  50. unsigned long millis(void);
  51. void delay(unsigned long ms);
  52. // Short delays, can be called from interrupt mode
  53. static inline void delay_ns(unsigned long ns)
  54. {
  55. delayMicroseconds((ns + 999) / 1000);
  56. }
  57. // Approximate fast delay
  58. static inline void delay_100ns()
  59. {
  60. asm volatile ("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
  61. }
  62. // Initialize SD card and GPIO configuration
  63. void platform_init();
  64. // Initialization for main application, not used for bootloader
  65. void platform_late_init();
  66. // Disable the status LED
  67. void platform_disable_led(void);
  68. // Query whether initiator mode is enabled on targets with PLATFORM_HAS_INITIATOR_MODE
  69. bool platform_is_initiator_mode_enabled();
  70. // Setup soft watchdog if supported
  71. void platform_reset_watchdog();
  72. // Set callback that will be called during data transfer to/from SD card.
  73. // This can be used to implement simultaneous transfer to SCSI bus.
  74. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  75. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  76. // Reprogram firmware in main program area.
  77. #ifndef RP2040_DISABLE_BOOTLOADER
  78. #define PLATFORM_BOOTLOADER_SIZE (128 * 1024)
  79. #define PLATFORM_FLASH_TOTAL_SIZE (1024 * 1024)
  80. #define PLATFORM_FLASH_PAGE_SIZE 4096
  81. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE]);
  82. void platform_boot_to_main_firmware();
  83. #endif
  84. // ROM drive in the unused external flash area
  85. #ifndef RP2040_DISABLE_ROMDRIVE
  86. #define PLATFORM_HAS_ROM_DRIVE 1
  87. // Check maximum available space for ROM drive in bytes
  88. uint32_t platform_get_romdrive_maxsize();
  89. // Read ROM drive area
  90. bool platform_read_romdrive(uint8_t *dest, uint32_t start, uint32_t count);
  91. // Reprogram ROM drive area
  92. #define PLATFORM_ROMDRIVE_PAGE_SIZE 4096
  93. bool platform_write_romdrive(const uint8_t *data, uint32_t start, uint32_t count);
  94. #endif
  95. // Parity lookup tables for write and read from SCSI bus.
  96. // These are used by macros below and the code in scsi_accel_rp2040.cpp
  97. extern const uint16_t g_scsi_parity_lookup[256];
  98. extern const uint16_t g_scsi_parity_check_lookup[512];
  99. // Below are GPIO access definitions that are used from scsiPhy.cpp.
  100. // Write a single SCSI pin.
  101. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  102. #define SCSI_OUT(pin, state) \
  103. *(state ? &sio_hw->gpio_clr : &sio_hw->gpio_set) = 1 << (SCSI_OUT_ ## pin)
  104. // Read a single SCSI pin.
  105. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  106. #define SCSI_IN(pin) \
  107. ((sio_hw->gpio_in & (1 << (SCSI_IN_ ## pin))) ? 0 : 1)
  108. // Set pin directions for initiator vs. target mode
  109. #define SCSI_ENABLE_INITIATOR() \
  110. (sio_hw->gpio_oe_set = (1 << SCSI_OUT_ACK) | \
  111. (1 << SCSI_OUT_ATN)), \
  112. (sio_hw->gpio_oe_clr = (1 << SCSI_IN_IO) | \
  113. (1 << SCSI_IN_CD) | \
  114. (1 << SCSI_IN_MSG) | \
  115. (1 << SCSI_IN_REQ))
  116. // Enable driving of shared control pins
  117. #define SCSI_ENABLE_CONTROL_OUT() \
  118. (sio_hw->gpio_oe_set = (1 << SCSI_OUT_CD) | \
  119. (1 << SCSI_OUT_MSG))
  120. // Set SCSI data bus to output
  121. #define SCSI_ENABLE_DATA_OUT() \
  122. (sio_hw->gpio_clr = (1 << SCSI_DATA_DIR), \
  123. sio_hw->gpio_oe_set = SCSI_IO_DATA_MASK)
  124. // Write SCSI data bus, also sets REQ to inactive.
  125. #define SCSI_OUT_DATA(data) \
  126. gpio_put_masked(SCSI_IO_DATA_MASK | (1 << SCSI_OUT_REQ), \
  127. g_scsi_parity_lookup[(uint8_t)(data)] | (1 << SCSI_OUT_REQ)), \
  128. SCSI_ENABLE_DATA_OUT()
  129. // Release SCSI data bus and REQ signal
  130. #define SCSI_RELEASE_DATA_REQ() \
  131. (sio_hw->gpio_oe_clr = SCSI_IO_DATA_MASK, \
  132. sio_hw->gpio_set = (1 << SCSI_DATA_DIR) | (1 << SCSI_OUT_REQ))
  133. // Release all SCSI outputs
  134. #define SCSI_RELEASE_OUTPUTS() \
  135. SCSI_RELEASE_DATA_REQ(), \
  136. sio_hw->gpio_oe_clr = (1 << SCSI_OUT_CD) | \
  137. (1 << SCSI_OUT_MSG), \
  138. sio_hw->gpio_set = (1 << SCSI_OUT_IO) | \
  139. (1 << SCSI_OUT_CD) | \
  140. (1 << SCSI_OUT_MSG) | \
  141. (1 << SCSI_OUT_RST) | \
  142. (1 << SCSI_OUT_BSY) | \
  143. (1 << SCSI_OUT_REQ) | \
  144. (1 << SCSI_OUT_SEL)
  145. // Read SCSI data bus
  146. #define SCSI_IN_DATA() \
  147. (~sio_hw->gpio_in & SCSI_IO_DATA_MASK) >> SCSI_IO_SHIFT
  148. #ifdef __cplusplus
  149. }
  150. // SD card driver for SdFat
  151. #ifdef SD_USE_SDIO
  152. class SdioConfig;
  153. extern SdioConfig g_sd_sdio_config;
  154. #define SD_CONFIG g_sd_sdio_config
  155. #define SD_CONFIG_CRASH g_sd_sdio_config
  156. #else
  157. class SdSpiConfig;
  158. extern SdSpiConfig g_sd_spi_config;
  159. #define SD_CONFIG g_sd_spi_config
  160. #define SD_CONFIG_CRASH g_sd_spi_config
  161. #endif
  162. #endif