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