| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <boot_device.h> |
| 8 |
#include <cbfs.h> |
| 9 |
#include <console/console.h> |
| 10 |
#include <device/mmio.h> |
| 11 |
#include <lib.h> |
| 12 |
#include <soc/addressmap.h> |
| 13 |
#include <soc/clock.h> |
| 14 |
#include <soc/spi.h> |
| 15 |
#include <spi_flash.h> |
| 16 |
#include <spi_sdcard.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <symbols.h> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define MSEL_SPI0x1(m) (((m) == 5) || ((m) == 14)) |
| 31 |
#define MSEL_SPI0x4(m) (((m) == 6) || ((m) == 10) || ((m) == 15)) |
| 32 |
#define MSEL_SPI1x1(m) ((m) == 12) |
| 33 |
#define MSEL_SPI1x4(m) (((m) == 7) || ((m) == 13)) |
| 34 |
#define MSEL_SPI1SD(m) ((m) == 8) |
| 35 |
#define MSEL_SPI2x1(m) ((m) == 9) |
| 36 |
#define MSEL_SPI2SD(m) ((m) == 11) |
| 37 |
|
| 38 |
#define MSEL_ACCESS_METHOD_MMAP(m) ((m == 5) || (m == 6) || (m == 7) || (m == 10) || (m == 13)) |
| 39 |
|
| 40 |
|
| 41 |
struct fu740_spi_config fu740_spi_configs[] = { |
| 42 |
{ 0 }, |
| 43 |
{ |
| 44 |
.freq = 10*1000*1000, |
| 45 |
}, |
| 46 |
{ 0 }, |
| 47 |
}; |
| 48 |
|
| 49 |
static struct spi_sdcard spi2_sdcard; |
| 50 |
|
| 51 |
|
| 52 |
static struct spi_flash spi_flash; |
| 53 |
static bool spi_flash_init_done; |
| 54 |
|
| 55 |
static ssize_t sd_readat(const struct region_device *rdev, void *dest, |
| 56 |
size_t offset, size_t count) |
| 57 |
{ |
| 58 |
spi_sdcard_read(&spi2_sdcard, dest, offset, count); |
| 59 |
return count; |
| 60 |
} |
| 61 |
|
| 62 |
static ssize_t spi_readat(const struct region_device *rd, void *b, size_t offset, size_t size) |
| 63 |
{ |
| 64 |
if (spi_flash_read(&spi_flash, offset, size, b)) |
| 65 |
return -1; |
| 66 |
return size; |
| 67 |
} |
| 68 |
|
| 69 |
static ssize_t spi_writeat(const struct region_device *rd, const void *b, |
| 70 |
size_t offset, size_t size) |
| 71 |
{ |
| 72 |
if (spi_flash_write(&spi_flash, offset, size, b)) |
| 73 |
return -1; |
| 74 |
return size; |
| 75 |
} |
| 76 |
|
| 77 |
static ssize_t spi_eraseat(const struct region_device *rd, size_t offset, size_t size) |
| 78 |
{ |
| 79 |
if (spi_flash_erase(&spi_flash, offset, size)) |
| 80 |
return -1; |
| 81 |
return size; |
| 82 |
} |
| 83 |
|
| 84 |
static const struct region_device_ops sd_ops = { |
| 85 |
.mmap = mmap_helper_rdev_mmap, |
| 86 |
.munmap = mmap_helper_rdev_munmap, |
| 87 |
.readat = sd_readat, |
| 88 |
}; |
| 89 |
|
| 90 |
static const struct region_device_ops spi_ops = { |
| 91 |
.mmap = mmap_helper_rdev_mmap, |
| 92 |
.munmap = mmap_helper_rdev_munmap, |
| 93 |
.readat = spi_readat, |
| 94 |
.writeat = spi_writeat, |
| 95 |
.eraseat = spi_eraseat, |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static struct mmap_helper_region_device spi_mdev = |
| 107 |
MMAP_HELPER_DEV_INIT(&spi_ops, 0, CONFIG_ROM_SIZE, &cbfs_cache); |
| 108 |
|
| 109 |
static struct mmap_helper_region_device sd_mdev = |
| 110 |
MMAP_HELPER_DEV_INIT(&sd_ops, 0, CONFIG_ROM_SIZE, &cbfs_cache); |
| 111 |
|
| 112 |
void boot_device_init(void) |
| 113 |
{ |
| 114 |
printk(BIOS_DEBUG, "%s 0\n", __func__); |
| 115 |
if (spi_flash_init_done == true) |
| 116 |
return; |
| 117 |
|
| 118 |
uint32_t m = read32((uint32_t *)FU740_MSEL) & 0xf; |
| 119 |
printk(BIOS_DEBUG, "%s MSEL %#x\n", __func__, m); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if (MSEL_SPI0x4(m)) { |
| 127 |
die("SPI0x4 is not supported yet"); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
if (MSEL_SPI1x4(m)) { |
| 134 |
printk(BIOS_DEBUG, "%s SPI1x4 1\n", __func__); |
| 135 |
fu740_spi_configs[1].ffmt_config.data_proto = FU740_SPI_PROTO_Q; |
| 136 |
fu740_spi_configs[1].ffmt_config.cmd_code = 0x6b; |
| 137 |
if (spi_flash_probe(1, 0, &spi_flash)) { |
| 138 |
printk(BIOS_EMERG, "SPI1x4 failed to init SPI flash\n"); |
| 139 |
return; |
| 140 |
} |
| 141 |
if (fu740_spi_setup(&spi_flash.spi) == -1) { |
| 142 |
printk(BIOS_EMERG, "SPI1x4 failed to configure mmap for SPI flash\n"); |
| 143 |
} |
| 144 |
printk(BIOS_DEBUG, "%s SPI1x4 2\n", __func__); |
| 145 |
} else if (MSEL_SPI1x1(m)) { |
| 146 |
printk(BIOS_DEBUG, "%s 1\n", __func__); |
| 147 |
fu740_spi_configs[1].ffmt_config.data_proto = FU740_SPI_PROTO_S; |
| 148 |
fu740_spi_configs[1].ffmt_config.cmd_code = 0x03; |
| 149 |
|
| 150 |
if (spi_flash_probe(1, 0, &spi_flash)) { |
| 151 |
printk(BIOS_EMERG, "failed to init SPI flash\n"); |
| 152 |
return; |
| 153 |
} |
| 154 |
printk(BIOS_DEBUG, "%s 2\n", __func__); |
| 155 |
} else if (MSEL_SPI2SD(m)) { |
| 156 |
spi_sdcard_init(&spi2_sdcard, 2, 0); |
| 157 |
} else { |
| 158 |
printk(BIOS_EMERG, "MSEL: %#02x: ", m); |
| 159 |
die("unsupported configuration of MSEL\n"); |
| 160 |
} |
| 161 |
|
| 162 |
spi_flash_init_done = true; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
const struct region_device *boot_device_ro(void) |
| 167 |
{ |
| 168 |
if (spi_flash_init_done != true) |
| 169 |
return NULL; |
| 170 |
|
| 171 |
uint32_t m = read32((uint32_t *)FU740_MSEL); |
| 172 |
|
| 173 |
if (MSEL_SPI2SD(m) || MSEL_SPI1SD(m)) |
| 174 |
return &sd_mdev.rdev; |
| 175 |
return &spi_mdev.rdev; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|