|
| 1 | +// Copyright 2023 ETH Zurich and University of Bologna. |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Paul Scheffler <paulsc@iis.ee.ethz.ch> |
| 6 | +// |
| 7 | +// Boot disk flasher for Cheshire; writes a contiguous disk segment to a boot target disk. |
| 8 | +// This program can be preloaded and invoked repeatedly to write multiple segments. |
| 9 | + |
| 10 | +#include <stdint.h> |
| 11 | +#include "util.h" |
| 12 | +#include "params.h" |
| 13 | +#include "regs/cheshire.h" |
| 14 | +#include "spi_host_regs.h" |
| 15 | +#include "dif/clint.h" |
| 16 | +#include "hal/i2c_24fc1025.h" |
| 17 | +#include "hal/spi_s25fs512s.h" |
| 18 | +#include "hal/spi_sdcard.h" |
| 19 | +#include "hal/uart_debug.h" |
| 20 | +#include "gpt.h" |
| 21 | +#include "printf.h" |
| 22 | + |
| 23 | +int flash_spi_sdcard(uint64_t core_freq, uint64_t rtc_freq, void *img_base, uint64_t sector, |
| 24 | + uint64_t len) { |
| 25 | + // Initialize device handle |
| 26 | + spi_sdcard_t device = { |
| 27 | + .spi_freq = 24 * 1000 * 1000, // 24MHz (maximum is 25MHz) |
| 28 | + .csid = 0, |
| 29 | + .csid_dummy = SPI_HOST_PARAM_NUM_C_S - 1 // Last physical CS is designated dummy |
| 30 | + }; |
| 31 | + CHECK_CALL(spi_sdcard_init(&device, core_freq)) |
| 32 | + // Wait for device to be initialized (1ms, round up extra tick to be sure) |
| 33 | + clint_spin_until((1000 * rtc_freq) / (1000 * 1000) + 1); |
| 34 | + // Write sectors: we have 512 512B blocks per 256KiB sector, so a 9b left shift |
| 35 | + return spi_sdcard_write_blocks(&device, img_base, sector << 9, len << 9, 1); |
| 36 | +} |
| 37 | + |
| 38 | +int flash_spi_s25fs512s(uint64_t core_freq, uint64_t rtc_freq, void *img_base, uint64_t sector, |
| 39 | + uint64_t len) { |
| 40 | + // Initialize device handle |
| 41 | + spi_s25fs512s_t device = { |
| 42 | + .spi_freq = MIN(40 * 1000 * 1000, core_freq / 4), // Up to quarter core freq or 40MHz |
| 43 | + .csid = 1}; |
| 44 | + CHECK_CALL(spi_s25fs512s_init(&device, core_freq)) |
| 45 | + // Wait for device to be initialized (t_PU = 300us, round up extra tick to be sure) |
| 46 | + clint_spin_until((350 * rtc_freq) / (1000 * 1000) + 1); |
| 47 | + // Write sectors of 256 KiB directly |
| 48 | + return spi_s25fs512s_single_flash(&device, img_base, sector, len); |
| 49 | +} |
| 50 | + |
| 51 | +int flash_i2c_24fc1025(uint64_t core_freq, void *img_base) { |
| 52 | + // Initialize device handle |
| 53 | + dif_i2c_t i2c; |
| 54 | + CHECK_CALL(i2c_24fc1025_init(&i2c, core_freq)) |
| 55 | + // Write half of a single 256 KiB sector (entire capacity) |
| 56 | + return i2c_24fc1025_write(&i2c, img_base, 0, 128 * 1024); |
| 57 | +} |
| 58 | + |
| 59 | +int main() { |
| 60 | + int ret; |
| 61 | + // Read reference frequency and compute core frequency |
| 62 | + uint32_t rtc_freq = *reg32(&__base_regs, CHESHIRE_RTC_FREQ_REG_OFFSET); |
| 63 | + uint64_t core_freq = clint_get_core_freq(rtc_freq, 2500); |
| 64 | + // Get arguments from scratch registers |
| 65 | + volatile uint32_t *scratch = reg32(&__base_regs, CHESHIRE_SCRATCH_0_REG_OFFSET); |
| 66 | + uint64_t target = scratch[0]; |
| 67 | + void *img_base = (void *)(uintptr_t)scratch[1]; |
| 68 | + uint64_t sector = scratch[2]; |
| 69 | + uint64_t len = scratch[3]; |
| 70 | + // Flash chosen disk |
| 71 | + printf("[FLASH] Write buffer at 0x%x of length %d to target %d, sector %d ... ", img_base, len, |
| 72 | + target, sector); |
| 73 | + switch (target) { |
| 74 | + case 1: { |
| 75 | + ret = flash_spi_sdcard(core_freq, rtc_freq, img_base, sector, len); |
| 76 | + break; |
| 77 | + } |
| 78 | + case 2: { |
| 79 | + ret = flash_spi_s25fs512s(core_freq, rtc_freq, img_base, sector, len); |
| 80 | + break; |
| 81 | + } |
| 82 | + case 3: { |
| 83 | + ret = flash_i2c_24fc1025(core_freq, img_base); |
| 84 | + break; |
| 85 | + } |
| 86 | + default: { |
| 87 | + ret = -1; |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + if (ret) |
| 92 | + printf("ERROR (%d)\r\n", ret); |
| 93 | + else |
| 94 | + printf("OK\r\n"); |
| 95 | + return ret; |
| 96 | +} |
0 commit comments