ene_lpc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2012 Google Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * Neither the name of Google or the names of contributors or
  18. * licensors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * This software is provided "AS IS," without a warranty of any kind.
  22. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  23. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  24. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  25. * GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE
  26. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  27. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  28. * GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  29. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  30. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  31. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  32. * EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. */
  34. #if defined(__i386__) || defined(__x86_64__)
  35. #include <inttypes.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <sys/time.h>
  40. #include "chipdrivers.h"
  41. #include "flash.h"
  42. #include "programmer.h"
  43. #include "spi.h"
  44. /* Supported ENE ECs, ENE_LAST should always be LAST member */
  45. enum ene_chip_id {
  46. ENE_KB932 = 0,
  47. ENE_KB94X,
  48. ENE_LAST
  49. };
  50. /* EC state */
  51. enum ene_ec_state {
  52. EC_STATE_NORMAL,
  53. EC_STATE_IDLE,
  54. EC_STATE_RESET,
  55. EC_STATE_UNKNOWN
  56. };
  57. /* chip-specific parameters */
  58. typedef struct {
  59. enum ene_chip_id chip_id;
  60. uint8_t hwver;
  61. uint8_t ediid;
  62. uint32_t port_bios;
  63. uint32_t port_ec_command;
  64. uint32_t port_ec_data;
  65. uint8_t ec_reset_cmd;
  66. uint8_t ec_reset_data;
  67. uint8_t ec_restart_cmd;
  68. uint8_t ec_restart_data;
  69. uint8_t ec_pause_cmd;
  70. uint8_t ec_pause_data;
  71. uint16_t ec_status_buf;
  72. uint8_t ec_is_stopping;
  73. uint8_t ec_is_running;
  74. uint8_t ec_is_pausing;
  75. uint32_t port_io_base;
  76. } ene_chip;
  77. /* table of supported chips + parameters */
  78. static ene_chip ene_chips[] = {
  79. { ENE_KB932, /* chip_id */
  80. 0xa2, 0x02, /* hwver + ediid */
  81. 0x66, /* port_bios */
  82. 0x6c, 0x68, /* port_ec_{command,data} */
  83. 0x59, 0xf2, /* ec_reset_{cmd,data} */
  84. 0x59, 0xf9, /* ec_restart_{cmd,data} */
  85. 0x59, 0xf1, /* ec_pause_{cmd,data} */
  86. 0xf554, /* ec_status_buf */
  87. 0xa5, 0x00, /* ec_is_{stopping,running} masks */
  88. 0x33, /* ec_is_pausing mask */
  89. 0xfd60 }, /* port_io_base */
  90. { ENE_KB94X, /* chip_id */
  91. 0xa3, 0x05, /* hwver + ediid */
  92. 0x66, /* port_bios */
  93. 0x66, 0x68, /* port_ec_{command,data} */
  94. 0x7d, 0x10, /* ec_reset_{cmd,data} */
  95. 0x7f, 0x10, /* ec_restart_{cmd,data} */
  96. 0x7e, 0x10, /* ec_pause_{cmd,data} */
  97. 0xf710, /* ec_status_buf */
  98. 0x02, 0x00, /* ec_is_{stopping,running} masks */
  99. 0x01, /* ec_is_pausing mask */
  100. 0x0380 }, /* port_io_base */
  101. };
  102. /* pointer to table entry of identified chip */
  103. static ene_chip *found_chip;
  104. /* current ec state */
  105. static enum ene_ec_state ec_state = EC_STATE_NORMAL;
  106. #define REG_EC_HWVER 0xff00
  107. #define REG_EC_FWVER 0xff01
  108. #define REG_EC_EDIID 0xff24
  109. #define REG_8051_CTRL 0xff14
  110. #define REG_EC_EXTCMD 0xff10
  111. #define CPU_RESET 1
  112. /* Hwardware registers */
  113. #define REG_SPI_DATA 0xfeab
  114. #define REG_SPI_COMMAND 0xfeac
  115. #define REG_SPI_CONFIG 0xfead
  116. #define CFG_CSn_FORCE_LOW (1 << 4)
  117. #define CFG_COMMAND_WRITE_ENABLE (1 << 3)
  118. #define CFG_STATUS (1 << 1)
  119. #define CFG_ENABLE_BUSY_STATUS_CHECK (1 << 0)
  120. /* Timeout */
  121. #define EC_COMMAND_TIMEOUT 4
  122. #define EC_RESTART_TIMEOUT 10
  123. #define ENE_SPI_DELAY_CYCLE 4
  124. #define EC_PAUSE_TIMEOUT 12
  125. #define EC_RESET_TRIES 3
  126. #define ENE_KB94X_PAUSE_WAKEUP_PORT 0x64
  127. #define MASK_INPUT_BUFFER_FULL 2
  128. #define MASK_OUTPUT_BUFFER_FULL 1
  129. const int port_ene_bank = 1;
  130. const int port_ene_offset = 2;
  131. const int port_ene_data = 3;
  132. static struct timeval pause_begin, pause_now;
  133. static void ec_command(uint8_t cmd, uint8_t data)
  134. {
  135. struct timeval begin, now;
  136. /* Spin wait for EC input buffer empty */
  137. gettimeofday(&begin, NULL);
  138. while (INB(found_chip->port_ec_command) & MASK_INPUT_BUFFER_FULL) {
  139. gettimeofday(&now, NULL);
  140. if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) {
  141. msg_pdbg("%s: buf not empty\n", __func__);
  142. return;
  143. }
  144. }
  145. /* Write command */
  146. OUTB(cmd, found_chip->port_ec_command);
  147. if (found_chip->chip_id == ENE_KB932) {
  148. /* Spin wait for EC input buffer empty */
  149. gettimeofday(&begin, NULL);
  150. while (INB(found_chip->port_ec_command) &
  151. MASK_INPUT_BUFFER_FULL) {
  152. gettimeofday(&now, NULL);
  153. if ((now.tv_sec - begin.tv_sec) >=
  154. EC_COMMAND_TIMEOUT) {
  155. msg_pdbg("%s: buf not empty\n", __func__);
  156. return;
  157. }
  158. }
  159. /* Write data */
  160. OUTB(data, found_chip->port_ec_data);
  161. }
  162. }
  163. static uint8_t ene_read(uint16_t addr)
  164. {
  165. uint8_t bank;
  166. uint8_t offset;
  167. uint8_t data;
  168. uint32_t port_io_base;
  169. bank = addr >> 8;
  170. offset = addr & 0xff;
  171. port_io_base = found_chip->port_io_base;
  172. OUTB(bank, port_io_base + port_ene_bank);
  173. OUTB(offset, port_io_base + port_ene_offset);
  174. data = INB(port_io_base + port_ene_data);
  175. return data;
  176. }
  177. static void ene_write(uint16_t addr, uint8_t data)
  178. {
  179. uint8_t bank;
  180. uint8_t offset;
  181. uint32_t port_io_base;
  182. bank = addr >> 8;
  183. offset = addr & 0xff;
  184. port_io_base = found_chip->port_io_base;
  185. OUTB(bank, port_io_base + port_ene_bank);
  186. OUTB(offset, port_io_base + port_ene_offset);
  187. OUTB(data, port_io_base + port_ene_data);
  188. }
  189. /**
  190. * wait_cycles, wait for n LPC bus clock cycles
  191. *
  192. * @param n: number of LPC cycles to wait
  193. * @return void
  194. */
  195. void wait_cycles(int n)
  196. {
  197. while (n--)
  198. INB(found_chip->port_io_base + port_ene_bank);
  199. }
  200. static int is_spicmd_write(uint8_t cmd)
  201. {
  202. switch (cmd) {
  203. case JEDEC_WREN:
  204. /* Chip Write Enable */
  205. case JEDEC_EWSR:
  206. /* Write Status Enable */
  207. case JEDEC_CE_60:
  208. /* Chip Erase 0x60 */
  209. case JEDEC_CE_C7:
  210. /* Chip Erase 0xc7 */
  211. case JEDEC_BE_52:
  212. /* Block Erase 0x52 */
  213. case JEDEC_BE_D8:
  214. /* Block Erase 0xd8 */
  215. case JEDEC_BE_D7:
  216. /* Block Erase 0xd7 */
  217. case JEDEC_SE:
  218. /* Sector Erase */
  219. case JEDEC_BYTE_PROGRAM:
  220. /* Write memory byte */
  221. case JEDEC_AAI_WORD_PROGRAM:
  222. /* Write AAI word */
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. static void ene_spi_start(void)
  228. {
  229. int cfg;
  230. cfg = ene_read(REG_SPI_CONFIG);
  231. cfg |= CFG_CSn_FORCE_LOW;
  232. cfg |= CFG_COMMAND_WRITE_ENABLE;
  233. ene_write(REG_SPI_CONFIG, cfg);
  234. wait_cycles(ENE_SPI_DELAY_CYCLE);
  235. }
  236. static void ene_spi_end(void)
  237. {
  238. int cfg;
  239. cfg = ene_read(REG_SPI_CONFIG);
  240. cfg &= ~CFG_CSn_FORCE_LOW;
  241. cfg |= CFG_COMMAND_WRITE_ENABLE;
  242. ene_write(REG_SPI_CONFIG, cfg);
  243. wait_cycles(ENE_SPI_DELAY_CYCLE);
  244. }
  245. static int ene_spi_wait(void)
  246. {
  247. struct timeval begin, now;
  248. gettimeofday(&begin, NULL);
  249. while(ene_read(REG_SPI_CONFIG) & CFG_STATUS) {
  250. gettimeofday(&now, NULL);
  251. if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) {
  252. msg_pdbg("%s: spi busy\n", __func__);
  253. return 1;
  254. }
  255. }
  256. return 0;
  257. }
  258. static int ene_pause_ec(void)
  259. {
  260. struct timeval begin, now;
  261. if (!found_chip->ec_pause_cmd)
  262. return -1;
  263. /* EC prepare pause */
  264. ec_command(found_chip->ec_pause_cmd, found_chip->ec_pause_data);
  265. gettimeofday(&begin, NULL);
  266. /* Spin wait for EC ready */
  267. while (ene_read(found_chip->ec_status_buf) !=
  268. found_chip->ec_is_pausing) {
  269. gettimeofday(&now, NULL);
  270. if ((now.tv_sec - begin.tv_sec) >=
  271. EC_COMMAND_TIMEOUT) {
  272. msg_pdbg("%s: unable to pause ec\n", __func__);
  273. return -1;
  274. }
  275. }
  276. gettimeofday(&pause_begin, NULL);
  277. ec_state = EC_STATE_IDLE;
  278. return 0;
  279. }
  280. static int ene_resume_ec(void)
  281. {
  282. struct timeval begin, now;
  283. if (found_chip->chip_id == ENE_KB94X)
  284. OUTB(0xff, ENE_KB94X_PAUSE_WAKEUP_PORT);
  285. else
  286. /* Trigger 8051 interrupt to resume */
  287. ene_write(REG_EC_EXTCMD, 0xff);
  288. gettimeofday(&begin, NULL);
  289. while (ene_read(found_chip->ec_status_buf) !=
  290. found_chip->ec_is_running) {
  291. gettimeofday(&now, NULL);
  292. if ((now.tv_sec - begin.tv_sec) >=
  293. EC_COMMAND_TIMEOUT) {
  294. msg_pdbg("%s: unable to resume ec\n", __func__);
  295. return -1;
  296. }
  297. }
  298. ec_state = EC_STATE_NORMAL;
  299. return 0;
  300. }
  301. static int ene_pause_timeout_check(void)
  302. {
  303. gettimeofday(&pause_now, NULL);
  304. if ((pause_now.tv_sec - pause_begin.tv_sec) >=
  305. EC_PAUSE_TIMEOUT) {
  306. if(ene_resume_ec() == 0)
  307. ene_pause_ec();
  308. }
  309. return 0;
  310. }
  311. static int ene_reset_ec(void)
  312. {
  313. uint8_t reg;
  314. struct timeval begin, now;
  315. gettimeofday(&begin, NULL);
  316. /* EC prepare reset */
  317. ec_command(found_chip->ec_reset_cmd, found_chip->ec_reset_data);
  318. /* Spin wait for EC ready */
  319. while (ene_read(found_chip->ec_status_buf) !=
  320. found_chip->ec_is_stopping) {
  321. gettimeofday(&now, NULL);
  322. if ((now.tv_sec - begin.tv_sec) >=
  323. EC_COMMAND_TIMEOUT) {
  324. msg_pdbg("%s: unable to reset ec\n", __func__);
  325. return -1;
  326. }
  327. }
  328. /* Wait 1 second */
  329. sleep(1);
  330. /* Reset 8051 */
  331. reg = ene_read(REG_8051_CTRL);
  332. reg |= CPU_RESET;
  333. ene_write(REG_8051_CTRL, reg);
  334. ec_state = EC_STATE_RESET;
  335. return 0;
  336. }
  337. static int ene_enter_flash_mode(void)
  338. {
  339. if (ene_pause_ec())
  340. return ene_reset_ec();
  341. return 0;
  342. }
  343. static int ene_spi_send_command(const struct flashctx *flash,
  344. unsigned int writecnt,
  345. unsigned int readcnt,
  346. const unsigned char *writearr,
  347. unsigned char *readarr)
  348. {
  349. int i;
  350. int tries = EC_RESET_TRIES;
  351. if (ec_state == EC_STATE_IDLE && is_spicmd_write(writearr[0])) {
  352. do {
  353. /* Enter reset mode if we need to write/erase */
  354. if (ene_resume_ec())
  355. continue;
  356. if (!ene_reset_ec())
  357. break;
  358. } while (--tries > 0);
  359. if (!tries) {
  360. msg_perr("%s: EC failed reset, skipping write\n",
  361. __func__);
  362. ec_state = EC_STATE_IDLE;
  363. return 1;
  364. }
  365. }
  366. else if(found_chip->chip_id == ENE_KB94X && ec_state == EC_STATE_IDLE)
  367. ene_pause_timeout_check();
  368. ene_spi_start();
  369. for (i = 0; i < writecnt; i++) {
  370. ene_write(REG_SPI_COMMAND, writearr[i]);
  371. if (ene_spi_wait()) {
  372. msg_pdbg("%s: write count %d\n", __func__, i);
  373. return 1;
  374. }
  375. }
  376. for (i = 0; i < readcnt; i++) {
  377. /* Push data by clock the serial bus */
  378. ene_write(REG_SPI_COMMAND, 0);
  379. if (ene_spi_wait()) {
  380. msg_pdbg("%s: read count %d\n", __func__, i);
  381. return 1;
  382. }
  383. readarr[i] = ene_read(REG_SPI_DATA);
  384. if (ene_spi_wait()) {
  385. msg_pdbg("%s: read count %d\n", __func__, i);
  386. return 1;
  387. }
  388. }
  389. ene_spi_end();
  390. return 0;
  391. }
  392. static int ene_leave_flash_mode(void *data)
  393. {
  394. int rv = 0;
  395. uint8_t reg;
  396. struct timeval begin, now;
  397. if (ec_state == EC_STATE_RESET) {
  398. reg = ene_read(REG_8051_CTRL);
  399. reg &= ~CPU_RESET;
  400. ene_write(REG_8051_CTRL, reg);
  401. gettimeofday(&begin, NULL);
  402. /* EC restart */
  403. while (ene_read(found_chip->ec_status_buf) !=
  404. found_chip->ec_is_running) {
  405. gettimeofday(&now, NULL);
  406. if ((now.tv_sec - begin.tv_sec) >=
  407. EC_RESTART_TIMEOUT) {
  408. msg_pdbg("%s: ec restart busy\n", __func__);
  409. rv = 1;
  410. goto exit;
  411. }
  412. }
  413. msg_pdbg("%s: send ec restart\n", __func__);
  414. ec_command(found_chip->ec_restart_cmd,
  415. found_chip->ec_restart_data);
  416. ec_state = EC_STATE_NORMAL;
  417. rv = 0;
  418. goto exit;
  419. }
  420. rv = ene_resume_ec();
  421. exit:
  422. /*
  423. * Trigger ec interrupt after pause/reset by sending 0x80
  424. * to bios command port.
  425. */
  426. OUTB(0x80, found_chip->port_bios);
  427. return rv;
  428. }
  429. static const struct spi_programmer spi_programmer_ene = {
  430. .type = SPI_CONTROLLER_ENE,
  431. .max_data_read = 256,
  432. .max_data_write = 256,
  433. .command = ene_spi_send_command,
  434. .multicommand = default_spi_send_multicommand,
  435. .read = default_spi_read,
  436. .write_256 = default_spi_write_256,
  437. };
  438. int ene_probe_spi_flash(const char *name)
  439. {
  440. uint8_t hwver, ediid, i;
  441. int ret = 0;
  442. char *p = NULL;
  443. if (alias && alias->type != ALIAS_EC)
  444. return 1;
  445. msg_pdbg("%s\n", __func__);
  446. p = extract_programmer_param("type");
  447. if (p && strcmp(p, "ec")) {
  448. msg_pdbg("ene_lpc only supports \"ec\" type devices\n");
  449. ret = 1;
  450. goto ene_probe_spi_flash_exit;
  451. }
  452. for (i = 0; i < ENE_LAST; ++i) {
  453. found_chip = &ene_chips[i];
  454. hwver = ene_read(REG_EC_HWVER);
  455. ediid = ene_read(REG_EC_EDIID);
  456. if(hwver == ene_chips[i].hwver &&
  457. ediid == ene_chips[i].ediid) {
  458. break;
  459. }
  460. }
  461. if (i == ENE_LAST) {
  462. msg_pdbg("ENE EC not found (probe failed)\n");
  463. ret = 1;
  464. goto ene_probe_spi_flash_exit;
  465. }
  466. /* TODO: probe the EC stop protocol
  467. *
  468. * Compal - ec_command(0x41, 0xa1) returns 43 4f 4d 50 41 4c 9c
  469. */
  470. if (register_shutdown(ene_leave_flash_mode, NULL)) {
  471. ret = 1;
  472. goto ene_probe_spi_flash_exit;
  473. }
  474. ene_enter_flash_mode();
  475. buses_supported |= BUS_LPC;
  476. register_spi_programmer(&spi_programmer_ene);
  477. msg_pdbg("%s: successfully initialized ene\n", __func__);
  478. ene_probe_spi_flash_exit:
  479. free(p);
  480. return ret;
  481. }
  482. #endif /* __i386__ || __x86_64__ */