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