it85spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
  5. * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl>
  6. * Copyright (C) 2008 coresystems GmbH
  7. * Copyright (C) 2010 Google Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /*
  23. * Contains the ITE IT85* SPI specific routines
  24. *
  25. * FIXME: EC firmware updates on this chip can be interrupted due to factors
  26. * such as SMBus traffic. YOU MUST DISABLE any services, such as power
  27. * management daemons, which can interact with the EC during firmware update.
  28. */
  29. #if defined(__i386__) || defined(__x86_64__)
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "flash.h"
  34. #include "spi.h"
  35. #include "programmer.h"
  36. /* Supported ECs, ITE_LAST should always be LAST member */
  37. enum ite_chip_id {
  38. ITE_IT85XX,
  39. ITE_IT8518,
  40. ITE_LAST
  41. };
  42. /* chip-specific parameters */
  43. typedef struct {
  44. enum ite_chip_id chip_id;
  45. uint8_t port_data;
  46. uint8_t port_cmd;
  47. uint8_t copy_to_sram_cmd;
  48. uint8_t exit_sram_cmd;
  49. uint32_t exit_sram_delay;
  50. } ite_chip;
  51. /* table of supported chips + parameters, order by ite_chip_id index */
  52. static ite_chip ite_chips[] = {
  53. { ITE_IT85XX,
  54. 0x60,
  55. 0x64,
  56. 0xB4,
  57. 0xFE,
  58. 0,
  59. },
  60. { ITE_IT8518,
  61. 0x62,
  62. 0x66,
  63. 0xDD, /* default value, see note in it85xx_spi_send_command */
  64. 0xFF,
  65. 500000,
  66. }
  67. };
  68. /* pointer to table entry of identified chip */
  69. static ite_chip *found_chip;
  70. #define MAX_TIMEOUT 100000
  71. #define MAX_TRY 5
  72. /* Constants for I/O ports */
  73. #define ITE_SUPERIO_PORT1 0x2e
  74. #define ITE_SUPERIO_PORT2 0x4e
  75. /* Constants for Logical Device registers */
  76. #define LDNSEL 0x07
  77. /* These are standard Super I/O 16-bit base address registers */
  78. #define SHM_IO_BAR0 0x60 /* big-endian, this is high bits */
  79. #define SHM_IO_BAR1 0x61
  80. /* The 8042 keyboard controller uses an input buffer and an output buffer to
  81. * communicate with the host CPU. Both buffers are 1-byte depth. That means
  82. * IBF is set to 1 when the host CPU sends a command to the input buffer
  83. * of the EC. IBF is cleared to 0 once the command is read by the EC.
  84. */
  85. #define KB_IBF (1 << 1) /* Input Buffer Full */
  86. #define KB_OBF (1 << 0) /* Output Buffer Full */
  87. /* IT8502 supports two access modes:
  88. * LPC_MEMORY: through the memory window in 0xFFFFFxxx (follow mode)
  89. * LPC_IO: through I/O port (so called indirect memory)
  90. */
  91. #undef LPC_MEMORY
  92. #define LPC_IO
  93. #ifdef LPC_IO
  94. /* macro to fill in indirect-access registers. */
  95. #define INDIRECT_A0(base, value) OUTB(value, (base) + 0) /* little-endian */
  96. #define INDIRECT_A1(base, value) OUTB(value, (base) + 1)
  97. #define INDIRECT_A2(base, value) OUTB(value, (base) + 2)
  98. #define INDIRECT_A3(base, value) OUTB(value, (base) + 3)
  99. #define INDIRECT_READ(base) INB((base) + 4)
  100. #define INDIRECT_WRITE(base, value) OUTB(value, (base) + 4)
  101. #endif /* LPC_IO */
  102. #ifdef LPC_IO
  103. unsigned int shm_io_base;
  104. #endif
  105. unsigned char *ce_high, *ce_low;
  106. static int it85xx_scratch_rom_reenter = 0;
  107. /* This function will poll the keyboard status register until either
  108. * an expected value shows up, or the timeout is reached.
  109. * timeout is in usec.
  110. *
  111. * Returns: 0 -- the expected value showed up.
  112. * 1 -- timeout.
  113. */
  114. static int wait_for(const unsigned int mask, const unsigned int expected_value,
  115. const int timeout, const char * error_message,
  116. const char * function_name, const int lineno)
  117. {
  118. int time_passed;
  119. for (time_passed = 0;; ++time_passed) {
  120. if ((INB(found_chip->port_cmd) & mask) == expected_value)
  121. return 0;
  122. if (time_passed >= timeout)
  123. break;
  124. programmer_delay(1);
  125. }
  126. if (error_message)
  127. msg_perr("%s():%d %s", function_name, lineno, error_message);
  128. return 1;
  129. }
  130. /* IT8502 employs a scratch RAM when flash is being updated. Call the following
  131. * two functions before/after flash erase/program. */
  132. void it85xx_enter_scratch_rom(void)
  133. {
  134. int ret, tries;
  135. if (it85xx_scratch_rom_reenter > 0)
  136. return;
  137. msg_pdbg("%s: entering scratch rom mode\n", __func__);
  138. for (tries = 0; tries < MAX_TRY; ++tries) {
  139. /* Wait until IBF (input buffer) is not full. */
  140. if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
  141. "* timeout at waiting for IBF==0.\n",
  142. __func__, __LINE__))
  143. continue;
  144. /* Copy EC firmware to SRAM. */
  145. OUTB(found_chip->copy_to_sram_cmd, found_chip->port_cmd);
  146. /* Confirm EC has taken away the command. */
  147. if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
  148. "* timeout at taking command.\n",
  149. __func__, __LINE__))
  150. continue;
  151. /* Waiting for OBF (output buffer) has data.
  152. * Note sometimes the replied command might be stolen by kernel
  153. * ISR so that it is okay as long as the command is 0xFA. */
  154. if (wait_for(KB_OBF, KB_OBF, MAX_TIMEOUT, NULL, NULL, 0))
  155. msg_pdbg("%s():%d * timeout at waiting for OBF.\n",
  156. __func__, __LINE__);
  157. if ((ret = INB(found_chip->port_data)) == 0xFA) {
  158. break;
  159. } else {
  160. msg_perr("%s():%d * not run on SRAM ret=%d\n",
  161. __func__, __LINE__, ret);
  162. continue;
  163. }
  164. }
  165. if (tries < MAX_TRY) {
  166. /* EC already runs on SRAM */
  167. it85xx_scratch_rom_reenter++;
  168. msg_pdbg("%s():%d * SUCCESS.\n", __func__, __LINE__);
  169. } else {
  170. msg_perr("%s():%d * Max try reached.\n", __func__, __LINE__);
  171. }
  172. }
  173. void it85xx_exit_scratch_rom(void)
  174. {
  175. int tries;
  176. msg_pdbg("%s():%d was called ...\n", __func__, __LINE__);
  177. if (it85xx_scratch_rom_reenter <= 0)
  178. return;
  179. for (tries = 0; tries < MAX_TRY; ++tries) {
  180. /* Wait until IBF (input buffer) is not full. */
  181. if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
  182. "* timeout at waiting for IBF==0.\n",
  183. __func__, __LINE__))
  184. continue;
  185. /* Exit SRAM. Run on flash. */
  186. OUTB(found_chip->exit_sram_cmd, found_chip->port_cmd);
  187. /* Confirm EC has taken away the command. */
  188. if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
  189. "* timeout at taking command.\n",
  190. __func__, __LINE__)) {
  191. /* We cannot ensure if EC has exited update mode.
  192. * If EC is in normal mode already, a further 0xFE
  193. * command will reboot system. So, exit loop here. */
  194. tries = MAX_TRY;
  195. break;
  196. }
  197. break;
  198. }
  199. if (tries < MAX_TRY) {
  200. it85xx_scratch_rom_reenter = 0;
  201. msg_pdbg("%s():%d * SUCCESS.\n", __func__, __LINE__);
  202. } else {
  203. msg_perr("%s():%d * Max try reached.\n", __func__, __LINE__);
  204. }
  205. programmer_delay(found_chip->exit_sram_delay);
  206. }
  207. static int it85xx_shutdown(void *data)
  208. {
  209. msg_pdbg("%s():%d\n", __func__, __LINE__);
  210. it85xx_exit_scratch_rom();
  211. return 0; /* FIXME: Should probably return something meaningful */
  212. }
  213. static int it85xx_spi_common_init(struct superio s)
  214. {
  215. chipaddr base;
  216. msg_pdbg("%s():%d superio.vendor=0x%02x\n", __func__, __LINE__,
  217. s.vendor);
  218. if (register_shutdown(it85xx_shutdown, NULL))
  219. return 1;
  220. #ifdef LPC_IO
  221. /* Get LPCPNP of SHM. That's big-endian. */
  222. sio_write(s.port, LDNSEL, 0x0F); /* Set LDN to SHM (0x0F) */
  223. shm_io_base = (sio_read(s.port, SHM_IO_BAR0) << 8) +
  224. sio_read(s.port, SHM_IO_BAR1);
  225. msg_pdbg("%s():%d shm_io_base=0x%04x\n", __func__, __LINE__,
  226. shm_io_base);
  227. /* These pointers are not used directly. They will be send to EC's
  228. * register for indirect access. */
  229. base = 0xFFFFF000;
  230. ce_high = ((unsigned char *)base) + 0xE00; /* 0xFFFFFE00 */
  231. ce_low = ((unsigned char *)base) + 0xD00; /* 0xFFFFFD00 */
  232. /* pre-set indirect-access registers since in most of cases they are
  233. * 0xFFFFxx00. */
  234. INDIRECT_A0(shm_io_base, base & 0xFF);
  235. INDIRECT_A2(shm_io_base, (base >> 16) & 0xFF);
  236. INDIRECT_A3(shm_io_base, (base >> 24));
  237. #endif
  238. #ifdef LPC_MEMORY
  239. /* FIXME: We should block accessing that region for anything else.
  240. * Major TODO here, and it will be a lot of work.
  241. */
  242. base = (chipaddr)physmap("it85 communication", 0xFFFFF000, 0x1000);
  243. msg_pdbg("%s():%d base=0x%08x\n", __func__, __LINE__,
  244. (unsigned int)base);
  245. ce_high = (unsigned char *)(base + 0xE00); /* 0xFFFFFE00 */
  246. ce_low = (unsigned char *)(base + 0xD00); /* 0xFFFFFD00 */
  247. #endif
  248. return 0;
  249. }
  250. /* According to ITE 8502 document, the procedure to follow mode is following:
  251. * 1. write 0x00 to LPC/FWH address 0xffff_fexxh (drive CE# high)
  252. * 2. write data to LPC/FWH address 0xffff_fdxxh (drive CE# low and MOSI
  253. * with data)
  254. * 3. read date from LPC/FWH address 0xffff_fdxxh (drive CE# low and get
  255. * data from MISO)
  256. */
  257. static int it85xx_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  258. const unsigned char *writearr, unsigned char *readarr)
  259. {
  260. int i;
  261. static int wdt_reset_flag_set = 0;
  262. if (found_chip->chip_id == ITE_IT8518) {
  263. /*
  264. * 0xd8 - Sets WDT reset flag to reboot EC after exiting from
  265. * scratch mode. This flag will be be checked when
  266. * command 0x8c is received. Use this when changing
  267. * ROM content (erase / write commands).
  268. * 0xdd - Same as d8, but without setting the WDT reset flag.
  269. * Use this for commands that do not change EC code.
  270. *
  271. * If opcode will cause ROM content to change and scratch
  272. * mode has been previously entered, then we need to re-enter
  273. * scratch mode using 0xd8.
  274. *
  275. * FIXME(dhendrix): This is specific to Stout. We should use
  276. * better method to apply board hacks rather than using the
  277. * EC's chip ID as the condition.
  278. */
  279. switch (writearr[0]) {
  280. case JEDEC_BYTE_PROGRAM:
  281. case JEDEC_BE_52:
  282. case JEDEC_BE_D7:
  283. case JEDEC_BE_D8:
  284. case JEDEC_CE_60:
  285. case JEDEC_CE_C7:
  286. case JEDEC_SE:
  287. if (!wdt_reset_flag_set) {
  288. msg_pdbg("%s: changing copy_to_sram_cmd\n",
  289. __func__);
  290. found_chip->copy_to_sram_cmd = 0xd8;
  291. it85xx_exit_scratch_rom();
  292. wdt_reset_flag_set = 1;
  293. }
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. it85xx_enter_scratch_rom();
  300. /* Exit scratch ROM ONLY when programmer shuts down. Otherwise, the
  301. * temporary flash state may halt the EC.
  302. */
  303. #ifdef LPC_IO
  304. INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff);
  305. INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/
  306. INDIRECT_A1(shm_io_base, (((unsigned long int)ce_low) >> 8) & 0xff);
  307. #endif
  308. #ifdef LPC_MEMORY
  309. mmio_writeb(0, ce_high);
  310. #endif
  311. for (i = 0; i < writecnt; ++i) {
  312. #ifdef LPC_IO
  313. INDIRECT_WRITE(shm_io_base, writearr[i]);
  314. #endif
  315. #ifdef LPC_MEMORY
  316. mmio_writeb(writearr[i], ce_low);
  317. #endif
  318. }
  319. for (i = 0; i < readcnt; ++i) {
  320. #ifdef LPC_IO
  321. readarr[i] = INDIRECT_READ(shm_io_base);
  322. #endif
  323. #ifdef LPC_MEMORY
  324. readarr[i] = mmio_readb(ce_low);
  325. #endif
  326. }
  327. #ifdef LPC_IO
  328. INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff);
  329. INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/
  330. #endif
  331. #ifdef LPC_MEMORY
  332. mmio_writeb(0, ce_high);
  333. #endif
  334. return 0;
  335. }
  336. static const struct spi_programmer spi_programmer_it8518 = {
  337. .type = SPI_CONTROLLER_IT85XX,
  338. .max_data_read = 256,
  339. .max_data_write = 256,
  340. .command = it85xx_spi_send_command,
  341. .multicommand = default_spi_send_multicommand,
  342. .read = default_spi_read,
  343. .write_256 = default_spi_write_256,
  344. };
  345. static const struct spi_programmer spi_programmer_it85xx = {
  346. .type = SPI_CONTROLLER_IT85XX,
  347. .max_data_read = 1,
  348. .max_data_write = 1,
  349. .command = it85xx_spi_send_command,
  350. .multicommand = default_spi_send_multicommand,
  351. .read = default_spi_read,
  352. .write_256 = default_spi_write_256,
  353. };
  354. /* it8518-specific i/o initialization */
  355. void setup_it8518_io_base()
  356. {
  357. OUTB(0x07, 0x2e); /* Set LDN to SHM */
  358. OUTB(0x0f, 0x2f);
  359. OUTB(0x60, 0x2e); /* Set IO space to 0x3F0 */
  360. OUTB(0x03, 0x2f);
  361. OUTB(0x61, 0x2e);
  362. OUTB(0xf0, 0x2f);
  363. OUTB(0x30, 0x2e); /* Enable this Logical Device */
  364. OUTB(0x01, 0x2f);
  365. }
  366. static int check_params(void)
  367. {
  368. int ret = 0;
  369. char *p = NULL;
  370. p = extract_programmer_param("type");
  371. if (p && strcmp(p, "ec")) {
  372. msg_pdbg("it85xx only supports \"ec\" type devices\n");
  373. ret = 1;
  374. }
  375. free(p);
  376. return ret;
  377. }
  378. int it8518_spi_init(struct superio s)
  379. {
  380. int ret;
  381. if (check_params())
  382. return 1;
  383. if (!(internal_buses_supported & BUS_FWH)) {
  384. msg_pdbg("%s():%d buses not support FWH\n", __func__, __LINE__);
  385. return 1;
  386. }
  387. found_chip = &ite_chips[ITE_IT8518];
  388. #ifdef LPC_IO
  389. setup_it8518_io_base();
  390. #endif
  391. ret = it85xx_spi_common_init(s);
  392. if (!ret) {
  393. msg_pdbg("%s: internal_buses_supported=0x%x\n", __func__,
  394. internal_buses_supported);
  395. /* Check for FWH because IT85 listens to FWH cycles.
  396. * FIXME: The big question is whether FWH cycles are necessary
  397. * for communication even if LPC_IO is defined.
  398. */
  399. if (internal_buses_supported & BUS_FWH)
  400. msg_pdbg("Registering IT85 SPI.\n");
  401. /* FIXME: Really leave FWH enabled? We can't use this region
  402. * anymore since accessing it would mess up IT85 communication.
  403. * If we decide to disable FWH for this region, we should print
  404. * a debug message about it.
  405. */
  406. /* Set this as SPI controller and add FWH | LPC to
  407. * supported buses. */
  408. buses_supported |= BUS_LPC | BUS_FWH;
  409. register_spi_programmer(&spi_programmer_it8518);
  410. }
  411. return ret;
  412. }
  413. int it85xx_spi_init(struct superio s)
  414. {
  415. int ret;
  416. if (alias && alias->type != ALIAS_EC)
  417. return 1;
  418. if (check_params())
  419. return 1;
  420. found_chip = &ite_chips[ITE_IT85XX];
  421. /*
  422. * FIXME: This is necessary to ensure that access to the shared access
  423. * window region is sent on the LPC bus. The old CLI syntax
  424. * (-p internal:bus=lpc) would cause the chipset enable code to set the
  425. * target bus appropriately before this function gets run, but the new
  426. * syntax ("-p ec") does not cause that to happen.
  427. */
  428. target_bus = BUS_LPC;
  429. msg_pdbg("%s: forcing target bus: 0x%08x\n", __func__, target_bus);
  430. chipset_flash_enable();
  431. ret = it85xx_spi_common_init(s);
  432. msg_pdbg("FWH: %s():%d ret=%d\n", __func__, __LINE__, ret);
  433. if (!ret) {
  434. msg_pdbg("%s: internal_buses_supported=0x%x\n", __func__,
  435. internal_buses_supported);
  436. /* Check for FWH because IT85 listens to FWH cycles.
  437. * FIXME: The big question is whether FWH cycles are necessary
  438. * for communication even if LPC_IO is defined.
  439. */
  440. if (internal_buses_supported & BUS_FWH)
  441. msg_pdbg("Registering IT85 SPI.\n");
  442. /* FIXME: Really leave FWH enabled? We can't use this region
  443. * anymore since accessing it would mess up IT85 communication.
  444. * If we decide to disable FWH for this region, we should print
  445. * a debug message about it.
  446. */
  447. /* Set this as SPI controller and add FWH | LPC to
  448. * supported buses. */
  449. buses_supported |= BUS_LPC | BUS_FWH;
  450. register_spi_programmer(&spi_programmer_it85xx);
  451. }
  452. return ret;
  453. }
  454. #endif