| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2015 Paul Kocialkowski <contact@paulk.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include "flash.h" |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 19 | #include "programmer.h" |
| Nico Huber | 6bd4f10 | 2026-02-22 23:28:13 +0100 | [diff] [blame] | 20 | #include "chipdrivers/edi.h" |
| Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 21 | #include "spi_command.h" |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 22 | #include "ene.h" |
| 23 | #include "edi.h" |
| 24 | |
| 25 | static unsigned int edi_read_buffer_length = EDI_READ_BUFFER_LENGTH_DEFAULT; |
| 26 | |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 27 | static void edi_write_cmd(unsigned char *cmd, unsigned short address, unsigned char data) |
| 28 | { |
| 29 | cmd[0] = EDI_WRITE; /* EDI write command. */ |
| 30 | cmd[1] = 0x00; /* Address is only 2 bytes. */ |
| 31 | cmd[2] = (address >> 8) & 0xff; /* Address higher byte. */ |
| 32 | cmd[3] = (address >> 0) & 0xff; /* Address lower byte. */ |
| 33 | cmd[4] = data; /* Write data. */ |
| 34 | } |
| 35 | |
| 36 | static void edi_read_cmd(unsigned char *cmd, unsigned short address) |
| 37 | { |
| 38 | cmd[0] = EDI_READ; /* EDI read command. */ |
| 39 | cmd[1] = 0x00; /* Address is only 2 bytes. */ |
| 40 | cmd[2] = (address >> 8) & 0xff; /* Address higher byte. */ |
| 41 | cmd[3] = (address >> 0) & 0xff; /* Address lower byte. */ |
| 42 | } |
| 43 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 44 | static int edi_write(const struct spi_master *spi, unsigned short address, unsigned char data) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 45 | { |
| 46 | unsigned char cmd[5]; |
| 47 | int rc; |
| 48 | |
| 49 | edi_write_cmd(cmd, address, data); |
| 50 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 51 | rc = spi->command(spi, sizeof(cmd), 0, cmd, NULL); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 52 | if (rc) |
| 53 | return -1; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 58 | static int edi_read_byte(const struct spi_master *spi, unsigned short address, unsigned char *data) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 59 | { |
| 60 | unsigned char cmd[4]; |
| 61 | unsigned char buffer[edi_read_buffer_length]; |
| 62 | unsigned int index; |
| 63 | unsigned int i; |
| 64 | int rc; |
| 65 | |
| 66 | edi_read_cmd(cmd, address); |
| 67 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 68 | rc = spi->command(spi, sizeof(cmd), sizeof(buffer), cmd, buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 69 | if (rc) |
| 70 | return -1; |
| 71 | |
| 72 | index = 0; |
| 73 | |
| 74 | for (i = 0; i < sizeof(buffer); i++) { |
| 75 | index = i; |
| 76 | |
| 77 | if (buffer[i] == EDI_NOT_READY) |
| 78 | continue; |
| 79 | |
| 80 | if (buffer[i] == EDI_READY) { |
| 81 | if (i == (sizeof(buffer) - 1)) { |
| 82 | /* |
| 83 | * Buffer size was too small for receiving the value. |
| 84 | * This is as good as getting only EDI_NOT_READY. |
| 85 | */ |
| 86 | |
| 87 | buffer[i] = EDI_NOT_READY; |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | *data = buffer[i + 1]; |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (buffer[index] == EDI_NOT_READY) |
| 97 | return -EDI_NOT_READY; |
| 98 | |
| 99 | return -1; |
| 100 | } |
| 101 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 102 | static int edi_read(const struct spi_master *spi, unsigned short address, unsigned char *data) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 103 | { |
| 104 | int rc; |
| 105 | |
| 106 | do { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 107 | rc = edi_read_byte(spi, address, data); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 108 | if (rc == -EDI_NOT_READY) { |
| 109 | /* |
| 110 | * Buffer size is increased, one step at a time, |
| 111 | * to hold more data if we only catch EDI_NOT_READY. |
| 112 | * Once CS is deasserted, no more data will be sent by the EC, |
| 113 | * so we cannot keep reading afterwards and have to start a new |
| 114 | * transaction with a longer buffer, to be safe. |
| 115 | */ |
| 116 | |
| 117 | if (edi_read_buffer_length < EDI_READ_BUFFER_LENGTH_MAX) { |
| 118 | msg_pwarn("%s: Retrying read with greater buffer length!\n", __func__); |
| 119 | edi_read_buffer_length++; |
| 120 | } else { |
| 121 | msg_perr("%s: Maximum buffer length reached and data still not ready!\n", __func__); |
| 122 | return -1; |
| 123 | } |
| 124 | } else if (rc < 0) { |
| 125 | return -1; |
| 126 | } |
| 127 | } while (rc == -EDI_NOT_READY); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 132 | static int edi_disable(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 133 | { |
| 134 | unsigned char cmd = EDI_DISABLE; |
| 135 | int rc; |
| 136 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 137 | rc = spi->command(spi, sizeof(cmd), 0, &cmd, NULL); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 138 | if (rc) |
| 139 | return -1; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 144 | static struct found_id *edi_chip_probe(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 145 | { |
| 146 | unsigned char hwversion; |
| 147 | unsigned char ediid; |
| 148 | int rc; |
| 149 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 150 | rc = edi_read(spi, ENE_EC_HWVERSION, &hwversion); |
| Mike Banon | 3a82604 | 2018-01-15 01:09:16 +0300 | [diff] [blame] | 151 | if (rc < 0) { |
| 152 | msg_cdbg("%s: reading hwversion failed\n", __func__); |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 153 | return NULL; |
| Mike Banon | 3a82604 | 2018-01-15 01:09:16 +0300 | [diff] [blame] | 154 | } |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 155 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 156 | rc = edi_read(spi, ENE_EC_EDIID, &ediid); |
| Mike Banon | 3a82604 | 2018-01-15 01:09:16 +0300 | [diff] [blame] | 157 | if (rc < 0) { |
| 158 | msg_cdbg("%s: reading ediid failed\n", __func__); |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 159 | return NULL; |
| Mike Banon | 3a82604 | 2018-01-15 01:09:16 +0300 | [diff] [blame] | 160 | } |
| 161 | |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 162 | struct found_id *const found = calloc(1, sizeof(*found)); |
| 163 | if (!found) { |
| 164 | msg_cerr("Out of memory!\n"); |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | struct id_info *const id = &found->info.id; |
| 169 | |
| 170 | id->hwversion = hwversion; |
| 171 | id->model = ediid; |
| 172 | id->type = ID_EDI; |
| 173 | |
| Mike Banon | 3a82604 | 2018-01-15 01:09:16 +0300 | [diff] [blame] | 174 | msg_cdbg("%s: hwversion 0x%02x, ediid 0x%02x\n", __func__, hwversion, ediid); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 175 | |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 176 | return found; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 177 | } |
| 178 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 179 | static int edi_spi_enable(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 180 | { |
| 181 | unsigned char buffer; |
| 182 | int rc; |
| 183 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 184 | rc = edi_read(spi, ENE_XBI_EFCFG, &buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 185 | if (rc < 0) |
| 186 | return -1; |
| 187 | |
| 188 | buffer |= ENE_XBI_EFCFG_CMD_WE; |
| 189 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 190 | rc = edi_write(spi, ENE_XBI_EFCFG, buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 191 | if (rc < 0) |
| 192 | return -1; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 197 | static int edi_spi_disable(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 198 | { |
| 199 | unsigned char buffer; |
| 200 | int rc; |
| 201 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 202 | rc = edi_read(spi, ENE_XBI_EFCFG, &buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 203 | if (rc < 0) |
| 204 | return -1; |
| 205 | |
| 206 | buffer &= ~ENE_XBI_EFCFG_CMD_WE; |
| 207 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 208 | rc = edi_write(spi, ENE_XBI_EFCFG, buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 209 | if (rc < 0) |
| 210 | return -1; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 215 | static int edi_spi_busy(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 216 | { |
| 217 | unsigned char buffer; |
| 218 | int rc; |
| 219 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 220 | rc = edi_read(spi, ENE_XBI_EFCFG, &buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 221 | if (rc < 0) |
| 222 | return -1; |
| 223 | |
| 224 | return !!(buffer & ENE_XBI_EFCFG_BUSY); |
| 225 | } |
| 226 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 227 | static int edi_spi_address(const struct spi_master *spi, unsigned int start, unsigned int address) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 228 | { |
| 229 | int rc; |
| 230 | |
| 231 | if ((address == start) || (((address - 1) & 0xff) != (address & 0xff))) { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 232 | rc = edi_write(spi, ENE_XBI_EFA0, ((address & 0xff) >> 0)); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 233 | if (rc < 0) |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | if ((address == start) || (((address - 1) & 0xff00) != (address & 0xff00))) { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 238 | rc = edi_write(spi, ENE_XBI_EFA1, ((address & 0xff00) >> 8)); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 239 | if (rc < 0) |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | if ((address == start) || (((address - 1) & 0xff0000) != (address & 0xff0000))) { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 244 | rc = edi_write(spi, ENE_XBI_EFA2, ((address & 0xff0000) >> 16)); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 245 | if (rc < 0) |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 252 | static int edi_8051_reset(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 253 | { |
| 254 | unsigned char buffer; |
| 255 | int rc; |
| 256 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 257 | rc = edi_read(spi, ENE_EC_PXCFG, &buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 258 | if (rc < 0) |
| 259 | return -1; |
| 260 | |
| 261 | buffer |= ENE_EC_PXCFG_8051_RESET; |
| 262 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 263 | rc = edi_write(spi, ENE_EC_PXCFG, buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 264 | if (rc < 0) |
| 265 | return -1; |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 270 | static int edi_8051_execute(const struct spi_master *spi) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 271 | { |
| 272 | unsigned char buffer; |
| 273 | int rc; |
| 274 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 275 | rc = edi_read(spi, ENE_EC_PXCFG, &buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 276 | if (rc < 0) |
| 277 | return -1; |
| 278 | |
| 279 | buffer &= ~ENE_EC_PXCFG_8051_RESET; |
| 280 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 281 | rc = edi_write(spi, ENE_EC_PXCFG, buffer); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 282 | if (rc < 0) |
| 283 | return -1; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | int edi_chip_block_erase(struct flashctx *flash, unsigned int page, unsigned int size) |
| 289 | { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 290 | const struct spi_master *const spi = flash->mst.spi; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 291 | unsigned int timeout = 64; |
| 292 | int rc; |
| 293 | |
| 294 | if (size != flash->chip->page_size) { |
| 295 | msg_perr("%s: Block erase size is not page size!\n", __func__); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 299 | rc = edi_spi_enable(spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 300 | if (rc < 0) { |
| 301 | msg_perr("%s: Unable to enable SPI!\n", __func__); |
| 302 | return -1; |
| 303 | } |
| 304 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 305 | rc = edi_spi_address(spi, page, page); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 306 | if (rc < 0) |
| 307 | return -1; |
| 308 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 309 | rc = edi_write(spi, ENE_XBI_EFCMD, ENE_XBI_EFCMD_ERASE); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 310 | if (rc < 0) |
| 311 | return -1; |
| 312 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 313 | while (edi_spi_busy(spi) == 1 && timeout) { |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 314 | programmer_delay(10); |
| 315 | timeout--; |
| 316 | } |
| 317 | |
| 318 | if (!timeout) { |
| 319 | msg_perr("%s: Timed out waiting for SPI not busy!\n", __func__); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 323 | rc = edi_spi_disable(spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 324 | if (rc < 0) { |
| 325 | msg_perr("%s: Unable to disable SPI!\n", __func__); |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int edi_chip_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
| 333 | { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 334 | const struct spi_master *const spi = flash->mst.spi; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 335 | unsigned int address = start; |
| 336 | unsigned int pages; |
| 337 | unsigned int timeout; |
| 338 | unsigned int i, j; |
| 339 | int rc; |
| 340 | |
| 341 | if ((start % flash->chip->page_size) != 0) { |
| 342 | msg_perr("%s: Start address is not page-aligned!\n", __func__); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | if ((len % flash->chip->page_size) != 0) { |
| 347 | msg_perr("%s: Length is not page-aligned!\n", __func__); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | pages = len / flash->chip->page_size; |
| 352 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 353 | rc = edi_spi_enable(spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 354 | if (rc < 0) { |
| 355 | msg_perr("%s: Unable to enable SPI!\n", __func__); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | for (i = 0; i < pages; i++) { |
| 360 | timeout = 64; |
| 361 | |
| 362 | /* Clear page buffer. */ |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 363 | rc = edi_write(spi, ENE_XBI_EFCMD, ENE_XBI_EFCMD_HVPL_CLEAR); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 364 | if (rc < 0) |
| 365 | return -1; |
| 366 | |
| 367 | for (j = 0; j < flash->chip->page_size; j++) { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 368 | rc = edi_spi_address(spi, start, address); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 369 | if (rc < 0) |
| 370 | return -1; |
| 371 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 372 | rc = edi_write(spi, ENE_XBI_EFDAT, *buf); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 373 | if (rc < 0) |
| 374 | return -1; |
| 375 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 376 | rc = edi_write(spi, ENE_XBI_EFCMD, ENE_XBI_EFCMD_HVPL_LATCH); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 377 | if (rc < 0) |
| 378 | return -1; |
| 379 | |
| 380 | buf++; |
| 381 | address++; |
| 382 | } |
| 383 | |
| 384 | /* Program page buffer to flash. */ |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 385 | rc = edi_write(spi, ENE_XBI_EFCMD, ENE_XBI_EFCMD_PROGRAM); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 386 | if (rc < 0) |
| 387 | return -1; |
| 388 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 389 | while (edi_spi_busy(spi) == 1 && timeout) { |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 390 | programmer_delay(10); |
| 391 | timeout--; |
| 392 | } |
| 393 | |
| 394 | if (!timeout) { |
| 395 | msg_perr("%s: Timed out waiting for SPI not busy!\n", __func__); |
| 396 | return -1; |
| 397 | } |
| Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 398 | |
| 399 | flashprog_progress_add(flash, flash->chip->page_size); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 400 | } |
| 401 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 402 | rc = edi_spi_disable(spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 403 | if (rc < 0) { |
| 404 | msg_perr("%s: Unable to disable SPI!\n", __func__); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | int edi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 412 | { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 413 | const struct spi_master *const spi = flash->mst.spi; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 414 | unsigned int address = start; |
| 415 | unsigned int i; |
| 416 | unsigned int timeout; |
| 417 | int rc; |
| 418 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 419 | rc = edi_spi_enable(spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 420 | if (rc < 0) { |
| 421 | msg_perr("%s: Unable to enable SPI!\n", __func__); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * EDI brings such a drastic overhead that there is about no need to |
| 427 | * have any delay in between calls. The EDI protocol will handle wait |
| 428 | * I/O times on its own anyway. |
| 429 | */ |
| 430 | |
| 431 | for (i = 0; i < len; i++) { |
| 432 | timeout = 64; |
| 433 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 434 | rc = edi_spi_address(spi, start, address); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 435 | if (rc < 0) |
| 436 | return -1; |
| 437 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 438 | rc = edi_write(spi, ENE_XBI_EFCMD, ENE_XBI_EFCMD_READ); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 439 | if (rc < 0) |
| 440 | return -1; |
| 441 | |
| 442 | do { |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 443 | rc = edi_read(spi, ENE_XBI_EFDAT, buf); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 444 | if (rc == 0) |
| 445 | break; |
| 446 | |
| 447 | /* Just in case. */ |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 448 | while (edi_spi_busy(spi) == 1 && timeout) { |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 449 | programmer_delay(10); |
| 450 | timeout--; |
| 451 | } |
| 452 | |
| 453 | if (!timeout) { |
| 454 | msg_perr("%s: Timed out waiting for SPI not busy!\n", __func__); |
| 455 | return -1; |
| 456 | } |
| 457 | } while (1); |
| 458 | |
| 459 | buf++; |
| 460 | address++; |
| Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 461 | flashprog_progress_add(flash, 1); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 462 | } |
| 463 | |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 464 | rc = edi_spi_disable(spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 465 | if (rc < 0) { |
| 466 | msg_perr("%s: Unable to disable SPI!\n", __func__); |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 473 | static void edi_finish(struct flashctx *flash) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 474 | { |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 475 | const struct spi_master *const spi = flash->mst.spi; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 476 | |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 477 | if (edi_8051_execute(spi) < 0) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 478 | msg_perr("%s: Unable to execute 8051!\n", __func__); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 479 | |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 480 | if (edi_disable(spi) < 0) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 481 | msg_perr("%s: Unable to disable EDI!\n", __func__); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 482 | } |
| 483 | |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 484 | struct found_id *probe_edi(const struct bus_probe *probe, const struct master_common *mst) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 485 | { |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 486 | const struct spi_master *const spi = (const struct spi_master *)mst; |
| Paul Kocialkowski | a590f48 | 2018-01-15 01:08:39 +0300 | [diff] [blame] | 487 | unsigned char hwversion; |
| 488 | |
| 489 | /* |
| 490 | * ENE chips enable EDI by detecting a clock frequency between 1 MHz and |
| 491 | * 8 MHz. In many cases, the chip won't be able to both detect the clock |
| 492 | * signal and serve the associated request at the same time. |
| 493 | * |
| 494 | * Thus, a dummy read has to be added to ensure that EDI is enabled and |
| 495 | * operational starting from the next request. This dummy read below |
| 496 | * draws the chip's attention and as result the chip enables its EDI. |
| 497 | */ |
| Nico Huber | a94ce1c | 2026-02-19 19:24:44 +0100 | [diff] [blame] | 498 | edi_read(spi, ENE_EC_HWVERSION, &hwversion); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 499 | |
| Nico Huber | f3113ac | 2026-02-21 12:50:19 +0100 | [diff] [blame^] | 500 | return edi_chip_probe(spi); |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | int edi_prepare(struct flashctx *flash, enum preparation_steps step) |
| 504 | { |
| 505 | int rc; |
| 506 | |
| 507 | if (step < PREPARE_FULL) |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 508 | return 0; |
| 509 | |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 510 | rc = edi_8051_reset(flash->mst.spi); |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 511 | if (rc < 0) { |
| 512 | msg_perr("%s: Unable to reset 8051!\n", __func__); |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 513 | return rc; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 514 | } |
| 515 | |
| Nico Huber | 4af02fe | 2026-02-21 12:29:26 +0100 | [diff] [blame] | 516 | flash->chip->finish_access = edi_finish; |
| 517 | return 0; |
| Paul Kocialkowski | 80ae14e | 2018-01-15 01:07:46 +0300 | [diff] [blame] | 518 | } |