ast2400.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2016 - 2017 Raptor Engineering, LLC
  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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "flash.h"
  23. #include "programmer.h"
  24. #include "hwaccess.h"
  25. #define PCI_VENDOR_ID_ASPEED 0x1a03
  26. #define ASPEED_MEMMAP_SIZE (128 * 1024)
  27. #define ASPEED_P2A_OFFSET 0x10000
  28. #define AST2400_SCU_APB_ADDR 0x1e6e2000
  29. #define AST2400_SCU_APB_BRIDGE_OFFSET (AST2400_SCU_APB_ADDR & 0xffff)
  30. #define AST2400_SCU_PROT_KEY 0x00
  31. #define AST2400_SCU_MISC_CTL 0x2c
  32. #define AST2400_SCU_HW_STRAP 0x70
  33. #define AST2400_SCU_PASSWORD 0x1688a8a8
  34. #define AST2400_SCU_BOOT_SRC_MASK 0x3
  35. #define AST2400_SCU_BOOT_SPI 0x2
  36. #define AST2400_SCU_BOOT_NONE 0x3
  37. #define AST2400_SMC_APB_ADDR 0x1e620000
  38. #define AST2400_SMC_FMC00 0x00
  39. #define AST2400_SMC_CE_CTL(N) (0x10 + (N * 4))
  40. #define AST2400_SMC_CE_SEG(N) (0x30 + (N * 4))
  41. #define AST2400_SMC_FLASH_MMIO_ADDR 0x20000000
  42. #define AST2400_SPI_APB_ADDR 0x1e630000
  43. #define AST2400_SPI_CFG 0x00
  44. #define AST2400_SPI_CTL 0x04
  45. #define AST2400_SPI_CFG_WRITE_EN 0x1
  46. #define AST2400_SPI_CMD_FAST_R_MODE 0x1
  47. #define AST2400_SPI_CMD_USER_MODE 0x3
  48. #define AST2400_SPI_CMD_MASK 0x3
  49. #define AST2400_SPI_STOP_CE_ACTIVE (0x1 << 2)
  50. #define AST2400_SPI_CPOL_1 (0x1 << 4)
  51. #define AST2400_SPI_LSB_FIRST_CTRL (0x1 << 5)
  52. #define AST2400_SPI_SPEED_MASK (0xf << 8)
  53. #define AST2400_SPI_IO_MODE_MASK (0x3 << 28)
  54. #define AST2400_SPI_FLASH_MMIO_ADDR 0x30000000
  55. #define AST2400_WDT_APB_ADDR 0x1e785000
  56. #define AST2400_WDT_APB_BRIDGE_OFFSET (AST2400_WDT_APB_ADDR & 0xffff)
  57. #define AST2400_WDT1_CTL 0x0c
  58. #define AST2400_WDT_RESET_MODE_MASK (0x3 << 5)
  59. #define AST2400_WDT_RESET_CPU_ONLY (0x2 << 5)
  60. uint8_t *ast2400_device_bar = 0;
  61. uint8_t ast2400_device_spi_bus = 0;
  62. uint8_t ast2400_device_halt_cpu = 0;
  63. uint8_t ast2400_device_resume_cpu = 0;
  64. uint8_t ast2400_device_tickle_fw = 0;
  65. uint32_t ast2400_device_flash_mmio_offset = 0;
  66. uint32_t ast2400_device_host_mode = 0;
  67. uint32_t ast2400_original_wdt_conf = 0;
  68. const struct dev_entry bmc_aspeed_ast2400[] = {
  69. {PCI_VENDOR_ID_ASPEED, 0x2000, OK, "ASPEED", "AST2400" },
  70. {0},
  71. };
  72. static int ast2400_spi_send_command(struct flashctx *flash,
  73. unsigned int writecnt, unsigned int readcnt,
  74. const unsigned char *writearr,
  75. unsigned char *readarr);
  76. static const struct spi_master spi_master_ast2400 = {
  77. .type = SPI_CONTROLLER_AST2400,
  78. .max_data_read = 256,
  79. .max_data_write = 256,
  80. .command = ast2400_spi_send_command,
  81. .multicommand = default_spi_send_multicommand,
  82. .read = default_spi_read,
  83. .write_256 = default_spi_write_256,
  84. .write_aai = default_spi_write_aai,
  85. };
  86. static int ast2400_set_a2b_bridge_scu(void)
  87. {
  88. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  89. pci_mmio_writel(AST2400_SCU_APB_ADDR & 0xffff0000, ast2400_device_bar + 0xf004);
  90. pci_mmio_writel(0x1, ast2400_device_bar + 0xf000);
  91. return 0;
  92. }
  93. static int ast2400_set_a2b_bridge_wdt(void)
  94. {
  95. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  96. pci_mmio_writel(AST2400_WDT_APB_ADDR & 0xffff0000, ast2400_device_bar + 0xf004);
  97. pci_mmio_writel(0x1, ast2400_device_bar + 0xf000);
  98. return 0;
  99. }
  100. static int ast2400_set_a2b_bridge_smc(void)
  101. {
  102. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  103. pci_mmio_writel(AST2400_SMC_APB_ADDR, ast2400_device_bar + 0xf004);
  104. pci_mmio_writel(0x1, ast2400_device_bar + 0xf000);
  105. return 0;
  106. }
  107. static int ast2400_set_a2b_bridge_spi(void)
  108. {
  109. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  110. pci_mmio_writel(AST2400_SPI_APB_ADDR, ast2400_device_bar + 0xf004);
  111. pci_mmio_writel(0x1, ast2400_device_bar + 0xf000);
  112. return 0;
  113. }
  114. static int ast2400_set_a2b_bridge_smc_flash(void)
  115. {
  116. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  117. pci_mmio_writel(AST2400_SMC_FLASH_MMIO_ADDR + ast2400_device_flash_mmio_offset, ast2400_device_bar + 0xf004);
  118. pci_mmio_writel(0x1, ast2400_device_bar + 0xf000);
  119. return 0;
  120. }
  121. static int ast2400_set_a2b_bridge_spi_flash(void)
  122. {
  123. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  124. pci_mmio_writel(AST2400_SPI_FLASH_MMIO_ADDR, ast2400_device_bar + 0xf004);
  125. pci_mmio_writel(0x1, ast2400_device_bar + 0xf000);
  126. return 0;
  127. }
  128. static int ast2400_disable_cpu(void) {
  129. uint32_t dword;
  130. if (ast2400_device_halt_cpu) {
  131. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_HW_STRAP);
  132. if (((dword & AST2400_SCU_BOOT_SRC_MASK) != AST2400_SCU_BOOT_SPI)
  133. && ((dword & AST2400_SCU_BOOT_SRC_MASK) != AST2400_SCU_BOOT_NONE)) { /* NONE permitted to allow for BMC recovery after Ctrl+C or crash */
  134. msg_perr("CPU halt requested but CPU firmware source is not SPI.\n");
  135. pci_mmio_writel(0x0, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_PROT_KEY);
  136. ast2400_device_halt_cpu = 0;
  137. return 1;
  138. }
  139. /* Disable WDT from issuing full SoC reset
  140. * Without this, OpenPOWER systems will crash when the GPIO blocks are reset on WDT timeout
  141. */
  142. msg_pinfo("Configuring P2A bridge for WDT access\n");
  143. ast2400_set_a2b_bridge_wdt();
  144. ast2400_original_wdt_conf = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_WDT_APB_BRIDGE_OFFSET + AST2400_WDT1_CTL);
  145. pci_mmio_writel((ast2400_original_wdt_conf & ~AST2400_WDT_RESET_MODE_MASK) | AST2400_WDT_RESET_CPU_ONLY, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_WDT_APB_BRIDGE_OFFSET + AST2400_WDT1_CTL);
  146. /* Disable CPU */
  147. ast2400_set_a2b_bridge_scu();
  148. pci_mmio_writel((dword & ~AST2400_SCU_BOOT_SRC_MASK) | AST2400_SCU_BOOT_NONE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_HW_STRAP);
  149. }
  150. return 0;
  151. }
  152. static int ast2400_enable_cpu(void) {
  153. uint32_t dword;
  154. if (ast2400_device_halt_cpu && ast2400_device_resume_cpu) {
  155. /* Re-enable CPU */
  156. ast2400_set_a2b_bridge_scu();
  157. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_HW_STRAP);
  158. pci_mmio_writel((dword & ~AST2400_SCU_BOOT_SRC_MASK) | AST2400_SCU_BOOT_SPI, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_HW_STRAP);
  159. /* Reset WDT configuration */
  160. ast2400_set_a2b_bridge_wdt();
  161. pci_mmio_writel((ast2400_original_wdt_conf & ~AST2400_WDT_RESET_MODE_MASK) | AST2400_WDT_RESET_CPU_ONLY, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_WDT_APB_BRIDGE_OFFSET + AST2400_WDT1_CTL);
  162. }
  163. return 0;
  164. }
  165. static int ast2400_shutdown(void *data) {
  166. /* Reactivate CPU if previously deactivated */
  167. ast2400_enable_cpu();
  168. /* Disable backdoor APB access */
  169. pci_mmio_writel(0x0, ast2400_device_bar + 0xf000);
  170. return 0;
  171. }
  172. int ast2400_init(void)
  173. {
  174. struct pci_dev *dev = NULL;
  175. uint32_t dword;
  176. uint8_t divisor;
  177. char *arg;
  178. ast2400_device_spi_bus = 0;
  179. arg = extract_programmer_param("spibus");
  180. if (arg) {
  181. if (!strcmp(arg,"host"))
  182. ast2400_device_host_mode = 1;
  183. else
  184. ast2400_device_spi_bus = strtol(arg, NULL, 0);
  185. }
  186. free(arg);
  187. ast2400_device_halt_cpu = 0;
  188. arg = extract_programmer_param("cpu");
  189. if (arg && !strcmp(arg,"pause")) {
  190. ast2400_device_halt_cpu = 1;
  191. ast2400_device_resume_cpu = 1;
  192. }
  193. if (arg && !strcmp(arg,"halt")) {
  194. ast2400_device_halt_cpu = 1;
  195. ast2400_device_resume_cpu = 0;
  196. }
  197. arg = extract_programmer_param("tickle");
  198. if (arg && !strcmp(arg,"true"))
  199. ast2400_device_tickle_fw = 1;
  200. free(arg);
  201. if ((ast2400_device_host_mode == 0) && ((ast2400_device_spi_bus < 0) || (ast2400_device_spi_bus > 4))) {
  202. msg_perr("SPI bus number out of range! Valid values are 0 - 4.\n");
  203. return 1;
  204. }
  205. if (rget_io_perms())
  206. return 1;
  207. dev = pcidev_init(bmc_aspeed_ast2400, PCI_BASE_ADDRESS_1);
  208. if (!dev)
  209. return 1;
  210. uintptr_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_1);
  211. if (!io_base_addr)
  212. return 1;
  213. msg_pinfo("Detected ASPEED MMIO base address: %p.\n", (void*)io_base_addr);
  214. ast2400_device_bar = rphysmap("ASPEED", io_base_addr, ASPEED_MEMMAP_SIZE);
  215. if (ast2400_device_bar == ERROR_PTR)
  216. return 1;
  217. if (register_shutdown(ast2400_shutdown, dev))
  218. return 1;
  219. io_base_addr += ASPEED_P2A_OFFSET;
  220. msg_pinfo("ASPEED P2A base address: %p.\n", (void*)io_base_addr);
  221. msg_pinfo("Configuring P2A bridge for SCU access\n");
  222. ast2400_set_a2b_bridge_scu();
  223. pci_mmio_writel(AST2400_SCU_PASSWORD, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_PROT_KEY);
  224. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_MISC_CTL);
  225. pci_mmio_writel(dword & ~((0x1 << 24) | (0x2 << 22)), ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SCU_APB_BRIDGE_OFFSET + AST2400_SCU_MISC_CTL);
  226. /* Halt CPU if requested */
  227. if (ast2400_disable_cpu())
  228. return 1;
  229. msg_pinfo("Configuring P2A bridge for SMC access\n");
  230. ast2400_set_a2b_bridge_smc();
  231. if (ast2400_device_host_mode) {
  232. msg_pinfo("Configuring P2A bridge for SPI access\n");
  233. ast2400_set_a2b_bridge_spi();
  234. divisor = 0; /* Slowest speed for now */
  235. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CTL);
  236. dword &= ~AST2400_SPI_SPEED_MASK;
  237. dword |= (divisor << 8);
  238. dword &= ~AST2400_SPI_CPOL_1;
  239. dword &= ~AST2400_SPI_LSB_FIRST_CTRL; /* MSB first */
  240. dword &= ~AST2400_SPI_IO_MODE_MASK; /* Single bit I/O mode */
  241. pci_mmio_writel(dword, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CTL);
  242. }
  243. else {
  244. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_FMC00);
  245. if (((dword >> (ast2400_device_spi_bus * 2)) & 0x3) != 0x2) {
  246. msg_perr("CE%01x Flash type is not SPI!\n", ast2400_device_spi_bus);
  247. return 1;
  248. }
  249. msg_pinfo("Enabling CE%01x write\n", ast2400_device_spi_bus);
  250. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_FMC00);
  251. pci_mmio_writel(dword | (0x1 << (16 + ast2400_device_spi_bus)), ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_FMC00);
  252. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_SEG(ast2400_device_spi_bus));
  253. ast2400_device_flash_mmio_offset = ((dword >> 16) & 0x3f) * 0x800000;
  254. msg_pinfo("Using CE%01x offset 0x%08x\n", ast2400_device_spi_bus, ast2400_device_flash_mmio_offset);
  255. }
  256. register_spi_master(&spi_master_ast2400);
  257. return 0;
  258. }
  259. static void ast2400_spi_xfer_data(struct flashctx *flash,
  260. unsigned int writecnt, unsigned int readcnt,
  261. const unsigned char *writearr,
  262. unsigned char *readarr)
  263. {
  264. int i;
  265. uint32_t dword;
  266. for (i = 0; i < writecnt; i++)
  267. msg_pspew("[%02x]", writearr[i]);
  268. msg_pspew("\n");
  269. for (i = 0; i < writecnt; i=i+4) {
  270. if ((writecnt - i) < 4)
  271. break;
  272. dword = writearr[i];
  273. dword |= writearr[i + 1] << 8;
  274. dword |= writearr[i + 2] << 16;
  275. dword |= writearr[i + 3] << 24;
  276. pci_mmio_writel(dword, ast2400_device_bar + ASPEED_P2A_OFFSET);
  277. }
  278. for (; i < writecnt; i++)
  279. pci_mmio_writeb(writearr[i], ast2400_device_bar + ASPEED_P2A_OFFSET);
  280. programmer_delay(1);
  281. for (i = 0; i < readcnt;) {
  282. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET);
  283. if (i < readcnt)
  284. readarr[i] = dword & 0xff;
  285. i++;
  286. if (i < readcnt)
  287. readarr[i] = (dword >> 8) & 0xff;
  288. i++;
  289. if (i < readcnt)
  290. readarr[i] = (dword >> 16) & 0xff;
  291. i++;
  292. if (i < readcnt)
  293. readarr[i] = (dword >> 24) & 0xff;
  294. i++;
  295. }
  296. for (i = 0; i < readcnt; i++)
  297. msg_pspew("[%02x]", readarr[i]);
  298. msg_pspew("\n");
  299. }
  300. /* Returns 0 upon success, a negative number upon errors. */
  301. static int ast2400_spi_send_command(struct flashctx *flash,
  302. unsigned int writecnt, unsigned int readcnt,
  303. const unsigned char *writearr,
  304. unsigned char *readarr)
  305. {
  306. uint32_t dword;
  307. msg_pspew("%s, cmd=0x%02x, writecnt=%d, readcnt=%d\n", __func__, *writearr, writecnt, readcnt);
  308. if (ast2400_device_host_mode) {
  309. /* Set up user command mode */
  310. ast2400_set_a2b_bridge_spi();
  311. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CFG);
  312. pci_mmio_writel(dword | AST2400_SPI_CFG_WRITE_EN, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CFG);
  313. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CTL);
  314. pci_mmio_writel(dword | AST2400_SPI_CMD_USER_MODE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CTL);
  315. /* Transfer data */
  316. ast2400_set_a2b_bridge_spi_flash();
  317. ast2400_spi_xfer_data(flash, writecnt, readcnt, writearr, readarr);
  318. /* Tear down user command mode */
  319. ast2400_set_a2b_bridge_spi();
  320. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CTL);
  321. pci_mmio_writel((dword & ~AST2400_SPI_CMD_MASK) | AST2400_SPI_CMD_FAST_R_MODE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CTL);
  322. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CFG);
  323. pci_mmio_writel(dword & ~AST2400_SPI_CFG_WRITE_EN, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SPI_CFG);
  324. }
  325. else {
  326. /* Set up user command mode */
  327. ast2400_set_a2b_bridge_smc();
  328. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  329. pci_mmio_writel(dword | AST2400_SPI_CMD_USER_MODE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  330. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  331. pci_mmio_writel(dword & ~AST2400_SPI_STOP_CE_ACTIVE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  332. /* Transfer data */
  333. ast2400_set_a2b_bridge_smc_flash();
  334. ast2400_spi_xfer_data(flash, writecnt, readcnt, writearr, readarr);
  335. /* Tear down user command mode */
  336. ast2400_set_a2b_bridge_smc();
  337. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  338. pci_mmio_writel(dword | AST2400_SPI_STOP_CE_ACTIVE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  339. dword = pci_mmio_readl(ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  340. pci_mmio_writel((dword & ~AST2400_SPI_CMD_MASK) | AST2400_SPI_CMD_FAST_R_MODE, ast2400_device_bar + ASPEED_P2A_OFFSET + AST2400_SMC_CE_CTL(ast2400_device_spi_bus));
  341. }
  342. if (ast2400_device_tickle_fw) {
  343. ast2400_enable_cpu();
  344. programmer_delay(100);
  345. ast2400_disable_cpu();
  346. }
  347. return 0;
  348. }