| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <arch/mmio.h> |
| 6 |
#include <boot_device.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <commonlib/helpers.h> |
| 9 |
#include <commonlib/region.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#define WRITE_BYTE_CMD 0x10 |
| 13 |
#define BLOCK_ERASE_CMD 0x20 |
| 14 |
#define CLEAR_STATUS_CMD 0x50 |
| 15 |
#define READ_STATUS_CMD 0x70 |
| 16 |
#define BLOCK_ERASE_CONFIRM_CMD 0xD0 |
| 17 |
#define READ_ARRAY_CMD 0xFF |
| 18 |
|
| 19 |
#define CLEARED_ARRAY_STATUS 0x00 |
| 20 |
|
| 21 |
#define QEMU_FLASH_BLOCK_SIZE 0x1000 |
| 22 |
|
| 23 |
#if CONFIG(ELOG) |
| 24 |
#include <southbridge/intel/common/pmutil.h> |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
void pch_log_state(void) {} |
| 36 |
#endif |
| 37 |
|
| 38 |
static ssize_t qemu_writeat(const struct region_device *rd, const void *b, |
| 39 |
size_t offset, size_t size) |
| 40 |
{ |
| 41 |
const struct mem_region_device *mdev; |
| 42 |
size_t i; |
| 43 |
volatile char *ptr; |
| 44 |
const char *buf = b; |
| 45 |
|
| 46 |
mdev = container_of(rd, __typeof__(*mdev), rdev); |
| 47 |
ptr = &mdev->base[offset]; |
| 48 |
|
| 49 |
for (i = 0; i < size; i++) { |
| 50 |
write8(ptr, WRITE_BYTE_CMD); |
| 51 |
write8(ptr, buf[i]); |
| 52 |
ptr++; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
if (size > 0) { |
| 57 |
write8(ptr - 1, READ_ARRAY_CMD); |
| 58 |
} |
| 59 |
|
| 60 |
return size; |
| 61 |
} |
| 62 |
|
| 63 |
static ssize_t qemu_eraseat(const struct region_device *rd, size_t offset, |
| 64 |
size_t size) |
| 65 |
{ |
| 66 |
const struct mem_region_device *mdev; |
| 67 |
size_t i; |
| 68 |
volatile char *ptr; |
| 69 |
|
| 70 |
mdev = container_of(rd, __typeof__(*mdev), rdev); |
| 71 |
ptr = &mdev->base[offset]; |
| 72 |
|
| 73 |
if (!IS_ALIGNED(offset, QEMU_FLASH_BLOCK_SIZE)) { |
| 74 |
printk(BIOS_ERR, "%s: erased offset isn't multiple of block size\n", |
| 75 |
__func__); |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
if (!IS_ALIGNED(size, QEMU_FLASH_BLOCK_SIZE)) { |
| 80 |
printk(BIOS_ERR, "%s: erased size isn't multiple of block size\n", |
| 81 |
__func__); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
|
| 85 |
for (i = 0; i < size; i += QEMU_FLASH_BLOCK_SIZE) { |
| 86 |
write8(ptr, BLOCK_ERASE_CMD); |
| 87 |
write8(ptr, BLOCK_ERASE_CONFIRM_CMD); |
| 88 |
ptr += QEMU_FLASH_BLOCK_SIZE; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (size > 0) { |
| 93 |
write8(ptr - QEMU_FLASH_BLOCK_SIZE, READ_ARRAY_CMD); |
| 94 |
} |
| 95 |
|
| 96 |
return size; |
| 97 |
} |
| 98 |
|
| 99 |
static struct region_device_ops flash_ops; |
| 100 |
static const struct mem_region_device boot_dev = |
| 101 |
MEM_REGION_DEV_INIT(0x100000000ULL - CONFIG_ROM_SIZE, CONFIG_ROM_SIZE, &flash_ops); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
void boot_device_init(void) |
| 123 |
{ |
| 124 |
volatile char *ptr; |
| 125 |
char original, readback; |
| 126 |
static bool initialized = false; |
| 127 |
|
| 128 |
if (initialized) |
| 129 |
return; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
flash_ops = mem_rdev_rw_ops; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
ptr = (volatile char *)boot_dev.base; |
| 149 |
original = read8(ptr); |
| 150 |
while (original == (char)0xFF || (original & 0x0F) == 0) |
| 151 |
original = read8(++ptr); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
write8(ptr, CLEAR_STATUS_CMD); |
| 159 |
readback = read8(ptr); |
| 160 |
if (readback == CLEAR_STATUS_CMD) { |
| 161 |
printk(BIOS_DEBUG, "QEMU flash behaves as RAM\n"); |
| 162 |
|
| 163 |
write8(ptr, original); |
| 164 |
} else { |
| 165 |
|
| 166 |
write8(ptr, READ_STATUS_CMD); |
| 167 |
readback = read8(ptr); |
| 168 |
if (readback == original) { |
| 169 |
printk(BIOS_DEBUG, "QEMU flash behaves as ROM\n"); |
| 170 |
|
| 171 |
flash_ops.writeat = NULL; |
| 172 |
flash_ops.eraseat = NULL; |
| 173 |
} else if (readback == CLEARED_ARRAY_STATUS) { |
| 174 |
|
| 175 |
write8(ptr, WRITE_BYTE_CMD); |
| 176 |
write8(ptr, original); |
| 177 |
write8(ptr, READ_STATUS_CMD); |
| 178 |
readback = read8(ptr); |
| 179 |
if (readback & 0x10 ) { |
| 180 |
printk(BIOS_DEBUG, |
| 181 |
"QEMU flash behaves as write-protected flash\n"); |
| 182 |
flash_ops.writeat = NULL; |
| 183 |
flash_ops.eraseat = NULL; |
| 184 |
} else { |
| 185 |
printk(BIOS_DEBUG, "QEMU flash behaves as writable flash\n"); |
| 186 |
flash_ops.writeat = qemu_writeat; |
| 187 |
flash_ops.eraseat = qemu_eraseat; |
| 188 |
} |
| 189 |
|
| 190 |
write8(ptr, READ_ARRAY_CMD); |
| 191 |
} else { |
| 192 |
printk(BIOS_ERR, "Unexpected QEMU flash behavior, assuming ROM\n"); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
flash_ops.writeat = NULL; |
| 198 |
flash_ops.eraseat = NULL; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
initialized = true; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
const struct region_device *boot_device_rw(void) |
| 207 |
{ |
| 208 |
return &boot_dev.rdev; |
| 209 |
} |