sb600spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2008 Wang Qingpei <Qingpei.Wang@amd.com>
  5. * Copyright (C) 2008 Joe Bao <Zheng.Bao@amd.com>
  6. * Copyright (C) 2008 Advanced Micro Devices, Inc.
  7. * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
  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; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #if defined(__i386__) || defined(__x86_64__)
  24. #include "flash.h"
  25. #include "programmer.h"
  26. #include "spi.h"
  27. /* This struct is unused, but helps visualize the SB600 SPI BAR layout.
  28. *struct sb600_spi_controller {
  29. * unsigned int spi_cntrl0; / * 00h * /
  30. * unsigned int restrictedcmd1; / * 04h * /
  31. * unsigned int restrictedcmd2; / * 08h * /
  32. * unsigned int spi_cntrl1; / * 0ch * /
  33. * unsigned int spi_cmdvalue0; / * 10h * /
  34. * unsigned int spi_cmdvalue1; / * 14h * /
  35. * unsigned int spi_cmdvalue2; / * 18h * /
  36. * unsigned int spi_fakeid; / * 1Ch * /
  37. *};
  38. */
  39. static uint8_t *sb600_spibar = NULL;
  40. static void reset_internal_fifo_pointer(void)
  41. {
  42. mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
  43. /* FIXME: This loop makes no sense at all. */
  44. while (mmio_readb(sb600_spibar + 0xD) & 0x7)
  45. msg_pspew("reset\n");
  46. }
  47. static int compare_internal_fifo_pointer(uint8_t want)
  48. {
  49. uint8_t tmp;
  50. tmp = mmio_readb(sb600_spibar + 0xd) & 0x07;
  51. want &= 0x7;
  52. if (want != tmp) {
  53. msg_perr("SB600 FIFO pointer corruption! Pointer is %d, wanted "
  54. "%d\n", tmp, want);
  55. msg_perr("Something else is accessing the flash chip and "
  56. "causes random corruption.\nPlease stop all "
  57. "applications and drivers and IPMI which access the "
  58. "flash chip.\n");
  59. return 1;
  60. } else {
  61. msg_pspew("SB600 FIFO pointer is %d, wanted %d\n", tmp, want);
  62. return 0;
  63. }
  64. }
  65. static int reset_compare_internal_fifo_pointer(uint8_t want)
  66. {
  67. int ret;
  68. ret = compare_internal_fifo_pointer(want);
  69. reset_internal_fifo_pointer();
  70. return ret;
  71. }
  72. static void execute_command(void)
  73. {
  74. mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2);
  75. while (mmio_readb(sb600_spibar + 2) & 1)
  76. ;
  77. }
  78. static int sb600_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  79. const unsigned char *writearr, unsigned char *readarr)
  80. {
  81. int count;
  82. /* First byte is cmd which can not being sent through FIFO. */
  83. unsigned char cmd = *writearr++;
  84. unsigned int readoffby1;
  85. unsigned char readwrite;
  86. writecnt--;
  87. msg_pspew("%s, cmd=%x, writecnt=%x, readcnt=%x\n",
  88. __func__, cmd, writecnt, readcnt);
  89. if (readcnt > 8) {
  90. msg_pinfo("%s, SB600 SPI controller can not receive %d bytes, "
  91. "it is limited to 8 bytes\n", __func__, readcnt);
  92. return SPI_INVALID_LENGTH;
  93. }
  94. if (writecnt > 8) {
  95. msg_pinfo("%s, SB600 SPI controller can not send %d bytes, "
  96. "it is limited to 8 bytes\n", __func__, writecnt);
  97. return SPI_INVALID_LENGTH;
  98. }
  99. /* This is a workaround for a bug in SB600 and SB700. If we only send
  100. * an opcode and no additional data/address, the SPI controller will
  101. * read one byte too few from the chip. Basically, the last byte of
  102. * the chip response is discarded and will not end up in the FIFO.
  103. * It is unclear if the CS# line is set high too early as well.
  104. */
  105. readoffby1 = (writecnt) ? 0 : 1;
  106. readwrite = (readcnt + readoffby1) << 4 | (writecnt);
  107. mmio_writeb(readwrite, sb600_spibar + 1);
  108. mmio_writeb(cmd, sb600_spibar + 0);
  109. /* Before we use the FIFO, reset it first. */
  110. reset_internal_fifo_pointer();
  111. /* Send the write byte to FIFO. */
  112. msg_pspew("Writing: ");
  113. for (count = 0; count < writecnt; count++, writearr++) {
  114. msg_pspew("[%02x]", *writearr);
  115. mmio_writeb(*writearr, sb600_spibar + 0xC);
  116. }
  117. msg_pspew("\n");
  118. /*
  119. * We should send the data by sequence, which means we need to reset
  120. * the FIFO pointer to the first byte we want to send.
  121. */
  122. if (reset_compare_internal_fifo_pointer(writecnt))
  123. return SPI_PROGRAMMER_ERROR;
  124. msg_pspew("Executing: \n");
  125. execute_command();
  126. /*
  127. * After the command executed, we should find out the index of the
  128. * received byte. Here we just reset the FIFO pointer and skip the
  129. * writecnt.
  130. * It would be possible to increase the FIFO pointer by one instead
  131. * of reading and discarding one byte from the FIFO.
  132. * The FIFO is implemented on top of an 8 byte ring buffer and the
  133. * buffer is never cleared. For every byte that is shifted out after
  134. * the opcode, the FIFO already stores the response from the chip.
  135. * Usually, the chip will respond with 0x00 or 0xff.
  136. */
  137. if (reset_compare_internal_fifo_pointer(writecnt + readcnt))
  138. return SPI_PROGRAMMER_ERROR;
  139. /* Skip the bytes we sent. */
  140. msg_pspew("Skipping: ");
  141. for (count = 0; count < writecnt; count++) {
  142. cmd = mmio_readb(sb600_spibar + 0xC);
  143. msg_pspew("[%02x]", cmd);
  144. }
  145. msg_pspew("\n");
  146. if (compare_internal_fifo_pointer(writecnt))
  147. return SPI_PROGRAMMER_ERROR;
  148. msg_pspew("Reading: ");
  149. for (count = 0; count < readcnt; count++, readarr++) {
  150. *readarr = mmio_readb(sb600_spibar + 0xC);
  151. msg_pspew("[%02x]", *readarr);
  152. }
  153. msg_pspew("\n");
  154. if (reset_compare_internal_fifo_pointer(readcnt + writecnt))
  155. return SPI_PROGRAMMER_ERROR;
  156. if (mmio_readb(sb600_spibar + 1) != readwrite) {
  157. msg_perr("Unexpected change in SB600 read/write count!\n");
  158. msg_perr("Something else is accessing the flash chip and "
  159. "causes random corruption.\nPlease stop all "
  160. "applications and drivers and IPMI which access the "
  161. "flash chip.\n");
  162. return SPI_PROGRAMMER_ERROR;
  163. }
  164. return 0;
  165. }
  166. static const struct spi_programmer spi_programmer_sb600 = {
  167. .type = SPI_CONTROLLER_SB600,
  168. .max_data_read = 8,
  169. .max_data_write = 5,
  170. .command = sb600_spi_send_command,
  171. .multicommand = default_spi_send_multicommand,
  172. .read = default_spi_read,
  173. .write_256 = default_spi_write_256,
  174. };
  175. int sb600_probe_spi(struct pci_dev *dev)
  176. {
  177. struct pci_dev *smbus_dev;
  178. uint32_t tmp;
  179. uint8_t reg;
  180. static const char *const speed_names[4] = {
  181. "Reserved", "33", "22", "16.5"
  182. };
  183. /* Read SPI_BaseAddr */
  184. tmp = pci_read_long(dev, 0xa0);
  185. tmp &= 0xffffffe0; /* remove bits 4-0 (reserved) */
  186. msg_pdbg("SPI base address is at 0x%x\n", tmp);
  187. /* If the BAR has address 0, it is unlikely SPI is used. */
  188. if (!tmp)
  189. return 0;
  190. /* Physical memory has to be mapped at page (4k) boundaries. */
  191. sb600_spibar = physmap("SB600 SPI registers", tmp & 0xfffff000,
  192. 0x1000);
  193. /* The low bits of the SPI base address are used as offset into
  194. * the mapped page.
  195. */
  196. sb600_spibar += tmp & 0xfff;
  197. tmp = pci_read_long(dev, 0xa0);
  198. msg_pdbg("AltSpiCSEnable=%i, SpiRomEnable=%i, "
  199. "AbortEnable=%i\n", tmp & 0x1, (tmp & 0x2) >> 1,
  200. (tmp & 0x4) >> 2);
  201. tmp = (pci_read_byte(dev, 0xba) & 0x4) >> 2;
  202. msg_pdbg("PrefetchEnSPIFromIMC=%i, ", tmp);
  203. tmp = pci_read_byte(dev, 0xbb);
  204. /* FIXME: Set bit 3,6,7 if not already set.
  205. * Set bit 5, otherwise SPI accesses are pointless in LPC mode.
  206. * See doc 42413 AMD SB700/710/750 RPR.
  207. */
  208. msg_pdbg("PrefetchEnSPIFromHost=%i, SpiOpEnInLpcMode=%i\n",
  209. tmp & 0x1, (tmp & 0x20) >> 5);
  210. tmp = mmio_readl(sb600_spibar);
  211. /* FIXME: If SpiAccessMacRomEn or SpiHostAccessRomEn are zero on
  212. * SB700 or later, reads and writes will be corrupted. Abort in this
  213. * case. Make sure to avoid this check on SB600.
  214. */
  215. msg_pdbg("SpiArbEnable=%i, SpiAccessMacRomEn=%i, "
  216. "SpiHostAccessRomEn=%i, ArbWaitCount=%i, "
  217. "SpiBridgeDisable=%i, DropOneClkOnRd=%i\n",
  218. (tmp >> 19) & 0x1, (tmp >> 22) & 0x1,
  219. (tmp >> 23) & 0x1, (tmp >> 24) & 0x7,
  220. (tmp >> 27) & 0x1, (tmp >> 28) & 0x1);
  221. tmp = (mmio_readb(sb600_spibar + 0xd) >> 4) & 0x3;
  222. msg_pdbg("NormSpeed is %s MHz\n", speed_names[tmp]);
  223. /* Look for the SMBus device. */
  224. smbus_dev = pci_dev_find(0x1002, 0x4385);
  225. if (!smbus_dev) {
  226. smbus_dev = pci_dev_find(0x1022, 0x780b); /* AMD Hudson */
  227. if (!smbus_dev) {
  228. msg_perr("ERROR: SMBus device not found. Not enabling SPI.\n");
  229. return ERROR_NONFATAL;
  230. }
  231. }
  232. /* Note about the bit tests below: If a bit is zero, the GPIO is SPI. */
  233. /* GPIO11/SPI_DO and GPIO12/SPI_DI status */
  234. reg = pci_read_byte(smbus_dev, 0xAB);
  235. reg &= 0xC0;
  236. msg_pdbg("GPIO11 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_DO");
  237. msg_pdbg("GPIO12 used for %s\n", (reg & (1 << 7)) ? "GPIO" : "SPI_DI");
  238. if (reg != 0x00) {
  239. msg_pdbg("Not enabling SPI");
  240. return 0;
  241. }
  242. /* GPIO31/SPI_HOLD and GPIO32/SPI_CS status */
  243. reg = pci_read_byte(smbus_dev, 0x83);
  244. reg &= 0xC0;
  245. msg_pdbg("GPIO31 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_HOLD");
  246. msg_pdbg("GPIO32 used for %s\n", (reg & (1 << 7)) ? "GPIO" : "SPI_CS");
  247. /* SPI_HOLD is not used on all boards, filter it out. */
  248. if ((reg & 0x80) != 0x00) {
  249. msg_pdbg("Not enabling SPI");
  250. return 0;
  251. }
  252. /* GPIO47/SPI_CLK status */
  253. reg = pci_read_byte(smbus_dev, 0xA7);
  254. reg &= 0x40;
  255. msg_pdbg("GPIO47 used for %s\n", (reg & (1 << 6)) ? "GPIO" : "SPI_CLK");
  256. if (reg != 0x00) {
  257. msg_pdbg("Not enabling SPI");
  258. return 0;
  259. }
  260. reg = pci_read_byte(dev, 0x40);
  261. msg_pdbg("SB700 IMC is %sactive.\n", (reg & (1 << 7)) ? "" : "not ");
  262. if (reg & (1 << 7)) {
  263. /* If we touch any region used by the IMC, the IMC and the SPI
  264. * interface will lock up, and the only way to recover is a
  265. * hard reset, but that is a bad choice for a half-erased or
  266. * half-written flash chip.
  267. * There appears to be an undocumented register which can freeze
  268. * or disable the IMC, but for now we want to play it safe.
  269. */
  270. msg_perr("The SB700 IMC is active and may interfere with SPI "
  271. "commands. Disabling write.\n");
  272. /* FIXME: Should we only disable SPI writes, or will the lockup
  273. * affect LPC/FWH chips as well?
  274. */
  275. programmer_may_write = 0;
  276. }
  277. /* Bring the FIFO to a clean state. */
  278. reset_internal_fifo_pointer();
  279. register_spi_programmer(&spi_programmer_sb600);
  280. return 0;
  281. }
  282. #endif