it87spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*
  22. * Contains the ITE IT87* SPI specific routines
  23. */
  24. #if defined(__i386__) || defined(__x86_64__)
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include "flash.h"
  28. #include "chipdrivers.h"
  29. #include "programmer.h"
  30. #include "spi.h"
  31. #define ITE_SUPERIO_PORT1 0x2e
  32. #define ITE_SUPERIO_PORT2 0x4e
  33. uint16_t it8716f_flashport = 0;
  34. /* use fast 33MHz SPI (<>0) or slow 16MHz (0) */
  35. static int fast_spi = 1;
  36. /* Helper functions for most recent ITE IT87xx Super I/O chips */
  37. #define CHIP_ID_BYTE1_REG 0x20
  38. #define CHIP_ID_BYTE2_REG 0x21
  39. #define CHIP_VER_REG 0x22
  40. void enter_conf_mode_ite(uint16_t port)
  41. {
  42. OUTB(0x87, port);
  43. OUTB(0x01, port);
  44. OUTB(0x55, port);
  45. if (port == ITE_SUPERIO_PORT1)
  46. OUTB(0x55, port);
  47. else
  48. OUTB(0xaa, port);
  49. }
  50. void exit_conf_mode_ite(uint16_t port)
  51. {
  52. sio_write(port, 0x02, 0x02);
  53. }
  54. uint16_t probe_id_ite(uint16_t port)
  55. {
  56. uint16_t id;
  57. enter_conf_mode_ite(port);
  58. id = sio_read(port, CHIP_ID_BYTE1_REG) << 8;
  59. id |= sio_read(port, CHIP_ID_BYTE2_REG);
  60. exit_conf_mode_ite(port);
  61. return id;
  62. }
  63. void probe_superio_ite(void)
  64. {
  65. struct superio s = {};
  66. uint16_t ite_ports[] = {ITE_SUPERIO_PORT1, ITE_SUPERIO_PORT2, 0};
  67. uint16_t *i = ite_ports;
  68. s.vendor = SUPERIO_VENDOR_ITE;
  69. for (; *i; i++) {
  70. s.port = *i;
  71. s.model = probe_id_ite(s.port);
  72. switch (s.model >> 8) {
  73. case 0x82:
  74. case 0x86:
  75. case 0x87:
  76. /* FIXME: Print revision for all models? */
  77. msg_pdbg("Found ITE Super I/O, ID 0x%04hx on port "
  78. "0x%x\n", s.model, s.port);
  79. register_superio(s);
  80. break;
  81. case 0x85:
  82. msg_pdbg("Found ITE EC, ID 0x%04hx,"
  83. "Rev 0x%02x on port 0x%x.\n",
  84. s.model, sio_read(s.port, CHIP_VER_REG),
  85. s.port);
  86. register_superio(s);
  87. break;
  88. }
  89. }
  90. return;
  91. }
  92. static int it8716f_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  93. const unsigned char *writearr, unsigned char *readarr);
  94. static int it8716f_spi_chip_read(struct flashctx *flash, uint8_t *buf,
  95. unsigned int start, unsigned int len);
  96. static int it8716f_spi_chip_write_256(struct flashctx *flash, uint8_t *buf,
  97. unsigned int start, unsigned int len);
  98. static const struct spi_programmer spi_programmer_it87xx = {
  99. .type = SPI_CONTROLLER_IT87XX,
  100. .max_data_read = MAX_DATA_UNSPECIFIED,
  101. .max_data_write = MAX_DATA_UNSPECIFIED,
  102. .command = it8716f_spi_send_command,
  103. .multicommand = default_spi_send_multicommand,
  104. .read = it8716f_spi_chip_read,
  105. .write_256 = it8716f_spi_chip_write_256,
  106. };
  107. static uint16_t it87spi_probe(uint16_t port)
  108. {
  109. uint8_t tmp = 0;
  110. char *portpos = NULL;
  111. uint16_t flashport = 0;
  112. enter_conf_mode_ite(port);
  113. /* NOLDN, reg 0x24, mask out lowest bit (suspend) */
  114. tmp = sio_read(port, 0x24) & 0xFE;
  115. /* Check if LPC->SPI translation is active. */
  116. if (!(tmp & 0x0e)) {
  117. msg_pdbg("No IT87* serial flash segment enabled.\n");
  118. exit_conf_mode_ite(port);
  119. /* Nothing to do. */
  120. return 1;
  121. }
  122. msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
  123. 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis");
  124. msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
  125. 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis");
  126. msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
  127. 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis");
  128. msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
  129. 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis");
  130. msg_pdbg("LPC write to serial flash %sabled\n",
  131. (tmp & 1 << 4) ? "en" : "dis");
  132. /* The LPC->SPI force write enable below only makes sense for
  133. * non-programmer mode.
  134. */
  135. /* If any serial flash segment is enabled, enable writing. */
  136. if ((tmp & 0xe) && (!(tmp & 1 << 4))) {
  137. msg_pdbg("Enabling LPC write to serial flash\n");
  138. tmp |= 1 << 4;
  139. sio_write(port, 0x24, tmp);
  140. }
  141. msg_pdbg("Serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29);
  142. /* LDN 0x7, reg 0x64/0x65 */
  143. sio_write(port, 0x07, 0x7);
  144. flashport = sio_read(port, 0x64) << 8;
  145. flashport |= sio_read(port, 0x65);
  146. msg_pdbg("Serial flash port 0x%04x\n", flashport);
  147. /* Non-default port requested? */
  148. portpos = extract_programmer_param("it87spiport");
  149. if (portpos) {
  150. char *endptr = NULL;
  151. unsigned long forced_flashport;
  152. forced_flashport = strtoul(portpos, &endptr, 0);
  153. /* Port 0, port >0x1000, unaligned ports and garbage strings
  154. * are rejected.
  155. */
  156. if (!forced_flashport || (forced_flashport >= 0x1000) ||
  157. (forced_flashport & 0x7) || (*endptr != '\0')) {
  158. /* Using ports below 0x100 is a really bad idea, and
  159. * should only be done if no port between 0x100 and
  160. * 0xff8 works due to routing issues.
  161. */
  162. msg_perr("Error: it87spiport specified, but no valid "
  163. "port specified.\nPort must be a multiple of "
  164. "0x8 and lie between 0x100 and 0xff8.\n");
  165. free(portpos);
  166. return 1;
  167. } else {
  168. flashport = (uint16_t)forced_flashport;
  169. msg_pinfo("Forcing serial flash port 0x%04x\n",
  170. flashport);
  171. sio_write(port, 0x64, (flashport >> 8));
  172. sio_write(port, 0x65, (flashport & 0xff));
  173. }
  174. }
  175. free(portpos);
  176. exit_conf_mode_ite(port);
  177. it8716f_flashport = flashport;
  178. if (internal_buses_supported & BUS_SPI)
  179. msg_pdbg("Overriding chipset SPI with IT87 SPI.\n");
  180. /* FIXME: Add the SPI bus or replace the other buses with it? */
  181. register_spi_programmer(&spi_programmer_it87xx);
  182. return 0;
  183. }
  184. int init_superio_ite(void)
  185. {
  186. int i, ret, chips_found = 0;
  187. for (i = 0; i < superio_count; i++) {
  188. if (superios[i].vendor != SUPERIO_VENDOR_ITE)
  189. continue;
  190. switch (superios[i].model) {
  191. case 0x8500:
  192. case 0x8502:
  193. case 0x8510:
  194. case 0x8511:
  195. case 0x8512:
  196. /* FIXME: This should be enabled, but we need a check
  197. * for laptop whitelisting due to the amount of things
  198. * which can go wrong if the EC firmware does not
  199. * implement the interface we want.
  200. */
  201. if (!it85xx_spi_init(superios[i]))
  202. chips_found++;
  203. break;
  204. case 0x8518:
  205. if (!it8518_spi_init(superios[i]))
  206. chips_found++;
  207. break;
  208. case 0x8705:
  209. if (!it8705f_write_enable(superios[i].port))
  210. chips_found++;
  211. break;
  212. case 0x8716:
  213. case 0x8718:
  214. case 0x8720:
  215. if (!it87spi_probe(superios[i].port))
  216. chips_found++;
  217. break;
  218. default:
  219. msg_pdbg("Super I/O ID 0x%04hx is not on the list of "
  220. "flash capable controllers.\n",
  221. superios[i].model);
  222. }
  223. }
  224. if (chips_found == 0) {
  225. ret = 1; /* failed to probe/initialize/enable chip */
  226. } else if (chips_found == 1) {
  227. ret = 0; /* success */
  228. } else {
  229. msg_pdbg("%s: Found %d programmable ECs/SuperIOs, aborting.\n",
  230. __func__, chips_found);
  231. ret = 1;
  232. }
  233. return ret;
  234. }
  235. /*
  236. * The IT8716F only supports commands with length 1,2,4,5 bytes including
  237. * command byte and can not read more than 3 bytes from the device.
  238. *
  239. * This function expects writearr[0] to be the first byte sent to the device,
  240. * whereas the IT8716F splits commands internally into address and non-address
  241. * commands with the address in inverse wire order. That's why the register
  242. * ordering in case 4 and 5 may seem strange.
  243. */
  244. static int it8716f_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  245. const unsigned char *writearr, unsigned char *readarr)
  246. {
  247. uint8_t busy, writeenc;
  248. int i;
  249. do {
  250. busy = INB(it8716f_flashport) & 0x80;
  251. } while (busy);
  252. if (readcnt > 3) {
  253. msg_pinfo("%s called with unsupported readcnt %i.\n",
  254. __func__, readcnt);
  255. return SPI_INVALID_LENGTH;
  256. }
  257. switch (writecnt) {
  258. case 1:
  259. OUTB(writearr[0], it8716f_flashport + 1);
  260. writeenc = 0x0;
  261. break;
  262. case 2:
  263. OUTB(writearr[0], it8716f_flashport + 1);
  264. OUTB(writearr[1], it8716f_flashport + 7);
  265. writeenc = 0x1;
  266. break;
  267. case 4:
  268. OUTB(writearr[0], it8716f_flashport + 1);
  269. OUTB(writearr[1], it8716f_flashport + 4);
  270. OUTB(writearr[2], it8716f_flashport + 3);
  271. OUTB(writearr[3], it8716f_flashport + 2);
  272. writeenc = 0x2;
  273. break;
  274. case 5:
  275. OUTB(writearr[0], it8716f_flashport + 1);
  276. OUTB(writearr[1], it8716f_flashport + 4);
  277. OUTB(writearr[2], it8716f_flashport + 3);
  278. OUTB(writearr[3], it8716f_flashport + 2);
  279. OUTB(writearr[4], it8716f_flashport + 7);
  280. writeenc = 0x3;
  281. break;
  282. default:
  283. msg_pinfo("%s called with unsupported writecnt %i.\n",
  284. __func__, writecnt);
  285. return SPI_INVALID_LENGTH;
  286. }
  287. /*
  288. * Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
  289. * Note:
  290. * We can't use writecnt directly, but have to use a strange encoding.
  291. */
  292. OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4)
  293. | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
  294. if (readcnt > 0) {
  295. do {
  296. busy = INB(it8716f_flashport) & 0x80;
  297. } while (busy);
  298. for (i = 0; i < readcnt; i++)
  299. readarr[i] = INB(it8716f_flashport + 5 + i);
  300. }
  301. return 0;
  302. }
  303. /* Page size is usually 256 bytes */
  304. static int it8716f_spi_page_program(struct flashctx *flash, uint8_t *buf,
  305. unsigned int start)
  306. {
  307. unsigned int i;
  308. int result;
  309. chipaddr bios = flash->virtual_memory;
  310. result = spi_write_enable(flash);
  311. if (result)
  312. return result;
  313. /* FIXME: The command below seems to be redundant or wrong. */
  314. OUTB(0x06, it8716f_flashport + 1);
  315. OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
  316. for (i = 0; i < flash->page_size; i++)
  317. chip_writeb(flash, buf[i], bios + start + i);
  318. OUTB(0, it8716f_flashport);
  319. /* Wait until the Write-In-Progress bit is cleared.
  320. * This usually takes 1-10 ms, so wait in 1 ms steps.
  321. */
  322. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  323. programmer_delay(1000);
  324. return 0;
  325. }
  326. /*
  327. * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
  328. * Need to read this big flash using firmware cycles 3 byte at a time.
  329. */
  330. static int it8716f_spi_chip_read(struct flashctx *flash, uint8_t *buf,
  331. unsigned int start, unsigned int len)
  332. {
  333. fast_spi = 0;
  334. /* FIXME: Check if someone explicitly requested to use IT87 SPI although
  335. * the mainboard does not use IT87 SPI translation. This should be done
  336. * via a programmer parameter for the internal programmer.
  337. */
  338. if ((flash->total_size * 1024 > 512 * 1024)) {
  339. spi_read_chunked(flash, buf, start, len, 3);
  340. } else {
  341. read_memmapped(flash, buf, start, len);
  342. }
  343. return 0;
  344. }
  345. static int it8716f_spi_chip_write_256(struct flashctx *flash, uint8_t *buf,
  346. unsigned int start, unsigned int len)
  347. {
  348. /*
  349. * IT8716F only allows maximum of 512 kb SPI chip size for memory
  350. * mapped access. It also can't write more than 1+3+256 bytes at once,
  351. * so page_size > 256 bytes needs a fallback.
  352. * FIXME: Split too big page writes into chunks IT87* can handle instead
  353. * of degrading to single-byte program.
  354. * FIXME: Check if someone explicitly requested to use IT87 SPI although
  355. * the mainboard does not use IT87 SPI translation. This should be done
  356. * via a programmer parameter for the internal programmer.
  357. */
  358. if ((flash->total_size * 1024 > 512 * 1024) ||
  359. (flash->page_size > 256)) {
  360. spi_chip_write_1(flash, buf, start, len);
  361. } else {
  362. unsigned int lenhere;
  363. if (start % flash->page_size) {
  364. /* start to the end of the page or to start + len,
  365. * whichever is smaller.
  366. */
  367. lenhere = min(len, flash->page_size - start % flash->page_size);
  368. spi_chip_write_1(flash, buf, start, lenhere);
  369. start += lenhere;
  370. len -= lenhere;
  371. buf += lenhere;
  372. }
  373. while (len >= flash->page_size) {
  374. it8716f_spi_page_program(flash, buf, start);
  375. start += flash->page_size;
  376. len -= flash->page_size;
  377. buf += flash->page_size;
  378. }
  379. if (len)
  380. spi_chip_write_1(flash, buf, start, len);
  381. }
  382. return 0;
  383. }
  384. #endif