2
0

pflash-cfi02-test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * QTest testcase for parallel flash with AMD command set
  3. *
  4. * Copyright (c) 2019 Stephen Checkoway
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "libqtest.h"
  11. /*
  12. * To test the pflash_cfi02 device, we run QEMU with the musicpal machine with
  13. * a pflash drive. This enables us to test some flash configurations, but not
  14. * all. In particular, we're limited to a 16-bit wide flash device.
  15. */
  16. #define MP_FLASH_SIZE_MAX (32 * 1024 * 1024)
  17. #define BASE_ADDR (0x100000000ULL - MP_FLASH_SIZE_MAX)
  18. #define UNIFORM_FLASH_SIZE (8 * 1024 * 1024)
  19. #define UNIFORM_FLASH_SECTOR_SIZE (64 * 1024)
  20. /* Use a newtype to keep flash addresses separate from byte addresses. */
  21. typedef struct {
  22. uint64_t addr;
  23. } faddr;
  24. #define FLASH_ADDR(x) ((faddr) { .addr = (x) })
  25. #define CFI_ADDR FLASH_ADDR(0x55)
  26. #define UNLOCK0_ADDR FLASH_ADDR(0x555)
  27. #define UNLOCK1_ADDR FLASH_ADDR(0x2AA)
  28. #define CFI_CMD 0x98
  29. #define UNLOCK0_CMD 0xAA
  30. #define UNLOCK1_CMD 0x55
  31. #define SECOND_UNLOCK_CMD 0x80
  32. #define AUTOSELECT_CMD 0x90
  33. #define RESET_CMD 0xF0
  34. #define PROGRAM_CMD 0xA0
  35. #define SECTOR_ERASE_CMD 0x30
  36. #define CHIP_ERASE_CMD 0x10
  37. #define UNLOCK_BYPASS_CMD 0x20
  38. #define UNLOCK_BYPASS_RESET_CMD 0x00
  39. #define ERASE_SUSPEND_CMD 0xB0
  40. #define ERASE_RESUME_CMD SECTOR_ERASE_CMD
  41. typedef struct {
  42. int bank_width;
  43. /* Nonuniform block size. */
  44. int nb_blocs[4];
  45. int sector_len[4];
  46. QTestState *qtest;
  47. } FlashConfig;
  48. static char image_path[] = "/tmp/qtest.XXXXXX";
  49. /*
  50. * The pflash implementation allows some parameters to be unspecified. We want
  51. * to test those configurations but we also need to know the real values in
  52. * our testing code. So after we launch qemu, we'll need a new FlashConfig
  53. * with the correct values filled in.
  54. */
  55. static FlashConfig expand_config_defaults(const FlashConfig *c)
  56. {
  57. FlashConfig ret = *c;
  58. if (ret.bank_width == 0) {
  59. ret.bank_width = 2;
  60. }
  61. if (ret.nb_blocs[0] == 0 && ret.sector_len[0] == 0) {
  62. ret.sector_len[0] = UNIFORM_FLASH_SECTOR_SIZE;
  63. ret.nb_blocs[0] = UNIFORM_FLASH_SIZE / UNIFORM_FLASH_SECTOR_SIZE;
  64. }
  65. /* XXX: Limitations of test harness. */
  66. assert(ret.bank_width == 2);
  67. return ret;
  68. }
  69. /*
  70. * Return a bit mask suitable for extracting the least significant
  71. * status/query response from an interleaved response.
  72. */
  73. static inline uint64_t device_mask(const FlashConfig *c)
  74. {
  75. return (uint64_t)-1;
  76. }
  77. /*
  78. * Return a bit mask exactly as long as the bank_width.
  79. */
  80. static inline uint64_t bank_mask(const FlashConfig *c)
  81. {
  82. if (c->bank_width == 8) {
  83. return (uint64_t)-1;
  84. }
  85. return (1ULL << (c->bank_width * 8)) - 1ULL;
  86. }
  87. static inline void flash_write(const FlashConfig *c, uint64_t byte_addr,
  88. uint64_t data)
  89. {
  90. /* Sanity check our tests. */
  91. assert((data & ~bank_mask(c)) == 0);
  92. uint64_t addr = BASE_ADDR + byte_addr;
  93. switch (c->bank_width) {
  94. case 1:
  95. qtest_writeb(c->qtest, addr, data);
  96. break;
  97. case 2:
  98. qtest_writew(c->qtest, addr, data);
  99. break;
  100. case 4:
  101. qtest_writel(c->qtest, addr, data);
  102. break;
  103. case 8:
  104. qtest_writeq(c->qtest, addr, data);
  105. break;
  106. default:
  107. abort();
  108. }
  109. }
  110. static inline uint64_t flash_read(const FlashConfig *c, uint64_t byte_addr)
  111. {
  112. uint64_t addr = BASE_ADDR + byte_addr;
  113. switch (c->bank_width) {
  114. case 1:
  115. return qtest_readb(c->qtest, addr);
  116. case 2:
  117. return qtest_readw(c->qtest, addr);
  118. case 4:
  119. return qtest_readl(c->qtest, addr);
  120. case 8:
  121. return qtest_readq(c->qtest, addr);
  122. default:
  123. abort();
  124. }
  125. }
  126. /*
  127. * Convert a flash address expressed in the maximum width of the device as a
  128. * byte address.
  129. */
  130. static inline uint64_t as_byte_addr(const FlashConfig *c, faddr flash_addr)
  131. {
  132. /*
  133. * Command addresses are always given as addresses in the maximum
  134. * supported bus size for the flash chip. So an x8/x16 chip in x8 mode
  135. * uses addresses 0xAAA and 0x555 to unlock because the least significant
  136. * bit is ignored. (0x555 rather than 0x554 is traditional.)
  137. *
  138. * In general we need to multiply by the maximum device width.
  139. */
  140. return flash_addr.addr * c->bank_width;
  141. }
  142. /*
  143. * Return the command value or expected status replicated across all devices.
  144. */
  145. static inline uint64_t replicate(const FlashConfig *c, uint64_t data)
  146. {
  147. /* Sanity check our tests. */
  148. assert((data & ~device_mask(c)) == 0);
  149. return data;
  150. }
  151. static inline void flash_cmd(const FlashConfig *c, faddr cmd_addr,
  152. uint8_t cmd)
  153. {
  154. flash_write(c, as_byte_addr(c, cmd_addr), replicate(c, cmd));
  155. }
  156. static inline uint64_t flash_query(const FlashConfig *c, faddr query_addr)
  157. {
  158. return flash_read(c, as_byte_addr(c, query_addr));
  159. }
  160. static inline uint64_t flash_query_1(const FlashConfig *c, faddr query_addr)
  161. {
  162. return flash_query(c, query_addr) & device_mask(c);
  163. }
  164. static void unlock(const FlashConfig *c)
  165. {
  166. flash_cmd(c, UNLOCK0_ADDR, UNLOCK0_CMD);
  167. flash_cmd(c, UNLOCK1_ADDR, UNLOCK1_CMD);
  168. }
  169. static void reset(const FlashConfig *c)
  170. {
  171. flash_cmd(c, FLASH_ADDR(0), RESET_CMD);
  172. }
  173. static void sector_erase(const FlashConfig *c, uint64_t byte_addr)
  174. {
  175. unlock(c);
  176. flash_cmd(c, UNLOCK0_ADDR, SECOND_UNLOCK_CMD);
  177. unlock(c);
  178. flash_write(c, byte_addr, replicate(c, SECTOR_ERASE_CMD));
  179. }
  180. static void wait_for_completion(const FlashConfig *c, uint64_t byte_addr)
  181. {
  182. /* If DQ6 is toggling, step the clock and ensure the toggle stops. */
  183. const uint64_t dq6 = replicate(c, 0x40);
  184. if ((flash_read(c, byte_addr) & dq6) ^ (flash_read(c, byte_addr) & dq6)) {
  185. /* Wait for erase or program to finish. */
  186. qtest_clock_step_next(c->qtest);
  187. /* Ensure that DQ6 has stopped toggling. */
  188. g_assert_cmphex(flash_read(c, byte_addr), ==, flash_read(c, byte_addr));
  189. }
  190. }
  191. static void bypass_program(const FlashConfig *c, uint64_t byte_addr,
  192. uint16_t data)
  193. {
  194. flash_cmd(c, UNLOCK0_ADDR, PROGRAM_CMD);
  195. flash_write(c, byte_addr, data);
  196. /*
  197. * Data isn't valid until DQ6 stops toggling. We don't model this as
  198. * writes are immediate, but if this changes in the future, we can wait
  199. * until the program is complete.
  200. */
  201. wait_for_completion(c, byte_addr);
  202. }
  203. static void program(const FlashConfig *c, uint64_t byte_addr, uint16_t data)
  204. {
  205. unlock(c);
  206. bypass_program(c, byte_addr, data);
  207. }
  208. static void chip_erase(const FlashConfig *c)
  209. {
  210. unlock(c);
  211. flash_cmd(c, UNLOCK0_ADDR, SECOND_UNLOCK_CMD);
  212. unlock(c);
  213. flash_cmd(c, UNLOCK0_ADDR, CHIP_ERASE_CMD);
  214. }
  215. static void erase_suspend(const FlashConfig *c)
  216. {
  217. flash_cmd(c, FLASH_ADDR(0), ERASE_SUSPEND_CMD);
  218. }
  219. static void erase_resume(const FlashConfig *c)
  220. {
  221. flash_cmd(c, FLASH_ADDR(0), ERASE_RESUME_CMD);
  222. }
  223. /*
  224. * Test flash commands with a variety of device geometry.
  225. */
  226. static void test_geometry(const void *opaque)
  227. {
  228. const FlashConfig *config = opaque;
  229. QTestState *qtest;
  230. qtest = qtest_initf("-M musicpal,accel=qtest"
  231. " -drive if=pflash,file=%s,format=raw,copy-on-read"
  232. /* Device geometry properties. */
  233. " -global driver=cfi.pflash02,"
  234. "property=num-blocks0,value=%d"
  235. " -global driver=cfi.pflash02,"
  236. "property=sector-length0,value=%d"
  237. " -global driver=cfi.pflash02,"
  238. "property=num-blocks1,value=%d"
  239. " -global driver=cfi.pflash02,"
  240. "property=sector-length1,value=%d"
  241. " -global driver=cfi.pflash02,"
  242. "property=num-blocks2,value=%d"
  243. " -global driver=cfi.pflash02,"
  244. "property=sector-length2,value=%d"
  245. " -global driver=cfi.pflash02,"
  246. "property=num-blocks3,value=%d"
  247. " -global driver=cfi.pflash02,"
  248. "property=sector-length3,value=%d",
  249. image_path,
  250. config->nb_blocs[0],
  251. config->sector_len[0],
  252. config->nb_blocs[1],
  253. config->sector_len[1],
  254. config->nb_blocs[2],
  255. config->sector_len[2],
  256. config->nb_blocs[3],
  257. config->sector_len[3]);
  258. FlashConfig explicit_config = expand_config_defaults(config);
  259. explicit_config.qtest = qtest;
  260. const FlashConfig *c = &explicit_config;
  261. /* Check the IDs. */
  262. unlock(c);
  263. flash_cmd(c, UNLOCK0_ADDR, AUTOSELECT_CMD);
  264. g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
  265. if (c->bank_width >= 2) {
  266. /*
  267. * XXX: The ID returned by the musicpal flash chip is 16 bits which
  268. * wouldn't happen with an 8-bit device. It would probably be best to
  269. * prohibit addresses larger than the device width in pflash_cfi02.c,
  270. * but then we couldn't test smaller device widths at all.
  271. */
  272. g_assert_cmphex(flash_query(c, FLASH_ADDR(1)), ==,
  273. replicate(c, 0x236D));
  274. }
  275. reset(c);
  276. /* Check the erase blocks. */
  277. flash_cmd(c, CFI_ADDR, CFI_CMD);
  278. g_assert_cmphex(flash_query(c, FLASH_ADDR(0x10)), ==, replicate(c, 'Q'));
  279. g_assert_cmphex(flash_query(c, FLASH_ADDR(0x11)), ==, replicate(c, 'R'));
  280. g_assert_cmphex(flash_query(c, FLASH_ADDR(0x12)), ==, replicate(c, 'Y'));
  281. /* Num erase regions. */
  282. int nb_erase_regions = flash_query_1(c, FLASH_ADDR(0x2C));
  283. g_assert_cmphex(nb_erase_regions, ==,
  284. !!c->nb_blocs[0] + !!c->nb_blocs[1] + !!c->nb_blocs[2] +
  285. !!c->nb_blocs[3]);
  286. /* Check device length. */
  287. uint32_t device_len = 1 << flash_query_1(c, FLASH_ADDR(0x27));
  288. g_assert_cmphex(device_len, ==, UNIFORM_FLASH_SIZE);
  289. /* Check that erase suspend to read/write is supported. */
  290. uint16_t pri = flash_query_1(c, FLASH_ADDR(0x15)) +
  291. (flash_query_1(c, FLASH_ADDR(0x16)) << 8);
  292. g_assert_cmpint(pri, >=, 0x2D + 4 * nb_erase_regions);
  293. g_assert_cmpint(flash_query(c, FLASH_ADDR(pri + 0)), ==, replicate(c, 'P'));
  294. g_assert_cmpint(flash_query(c, FLASH_ADDR(pri + 1)), ==, replicate(c, 'R'));
  295. g_assert_cmpint(flash_query(c, FLASH_ADDR(pri + 2)), ==, replicate(c, 'I'));
  296. g_assert_cmpint(flash_query_1(c, FLASH_ADDR(pri + 6)), ==, 2); /* R/W */
  297. reset(c);
  298. const uint64_t dq7 = replicate(c, 0x80);
  299. const uint64_t dq6 = replicate(c, 0x40);
  300. const uint64_t dq3 = replicate(c, 0x08);
  301. const uint64_t dq2 = replicate(c, 0x04);
  302. uint64_t byte_addr = 0;
  303. for (int region = 0; region < nb_erase_regions; ++region) {
  304. uint64_t base = 0x2D + 4 * region;
  305. flash_cmd(c, CFI_ADDR, CFI_CMD);
  306. uint32_t nb_sectors = flash_query_1(c, FLASH_ADDR(base + 0)) +
  307. (flash_query_1(c, FLASH_ADDR(base + 1)) << 8) + 1;
  308. uint32_t sector_len = (flash_query_1(c, FLASH_ADDR(base + 2)) << 8) +
  309. (flash_query_1(c, FLASH_ADDR(base + 3)) << 16);
  310. g_assert_cmphex(nb_sectors, ==, c->nb_blocs[region]);
  311. g_assert_cmphex(sector_len, ==, c->sector_len[region]);
  312. reset(c);
  313. /* Erase and program sector. */
  314. for (uint32_t i = 0; i < nb_sectors; ++i) {
  315. sector_erase(c, byte_addr);
  316. /* Check that DQ3 is 0. */
  317. g_assert_cmphex(flash_read(c, byte_addr) & dq3, ==, 0);
  318. qtest_clock_step_next(c->qtest); /* Step over the 50 us timeout. */
  319. /* Check that DQ3 is 1. */
  320. uint64_t status0 = flash_read(c, byte_addr);
  321. g_assert_cmphex(status0 & dq3, ==, dq3);
  322. /* DQ7 is 0 during an erase. */
  323. g_assert_cmphex(status0 & dq7, ==, 0);
  324. uint64_t status1 = flash_read(c, byte_addr);
  325. /* DQ6 toggles during an erase. */
  326. g_assert_cmphex(status0 & dq6, ==, ~status1 & dq6);
  327. /* Wait for erase to complete. */
  328. wait_for_completion(c, byte_addr);
  329. /* Ensure DQ6 has stopped toggling. */
  330. g_assert_cmphex(flash_read(c, byte_addr), ==,
  331. flash_read(c, byte_addr));
  332. /* Now the data should be valid. */
  333. g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
  334. /* Program a bit pattern. */
  335. program(c, byte_addr, 0x55);
  336. g_assert_cmphex(flash_read(c, byte_addr) & 0xFF, ==, 0x55);
  337. program(c, byte_addr, 0xA5);
  338. g_assert_cmphex(flash_read(c, byte_addr) & 0xFF, ==, 0x05);
  339. byte_addr += sector_len;
  340. }
  341. }
  342. /* Erase the chip. */
  343. chip_erase(c);
  344. /* Read toggle. */
  345. uint64_t status0 = flash_read(c, 0);
  346. /* DQ7 is 0 during an erase. */
  347. g_assert_cmphex(status0 & dq7, ==, 0);
  348. uint64_t status1 = flash_read(c, 0);
  349. /* DQ6 toggles during an erase. */
  350. g_assert_cmphex(status0 & dq6, ==, ~status1 & dq6);
  351. /* Wait for erase to complete. */
  352. qtest_clock_step_next(c->qtest);
  353. /* Ensure DQ6 has stopped toggling. */
  354. g_assert_cmphex(flash_read(c, 0), ==, flash_read(c, 0));
  355. /* Now the data should be valid. */
  356. for (int region = 0; region < nb_erase_regions; ++region) {
  357. for (uint32_t i = 0; i < c->nb_blocs[region]; ++i) {
  358. uint64_t byte_addr = i * c->sector_len[region];
  359. g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
  360. }
  361. }
  362. /* Unlock bypass */
  363. unlock(c);
  364. flash_cmd(c, UNLOCK0_ADDR, UNLOCK_BYPASS_CMD);
  365. bypass_program(c, 0 * c->bank_width, 0x01);
  366. bypass_program(c, 1 * c->bank_width, 0x23);
  367. bypass_program(c, 2 * c->bank_width, 0x45);
  368. /*
  369. * Test that bypass programming, unlike normal programming can use any
  370. * address for the PROGRAM_CMD.
  371. */
  372. flash_cmd(c, FLASH_ADDR(3 * c->bank_width), PROGRAM_CMD);
  373. flash_write(c, 3 * c->bank_width, 0x67);
  374. wait_for_completion(c, 3 * c->bank_width);
  375. flash_cmd(c, FLASH_ADDR(0), UNLOCK_BYPASS_RESET_CMD);
  376. bypass_program(c, 4 * c->bank_width, 0x89); /* Should fail. */
  377. g_assert_cmphex(flash_read(c, 0 * c->bank_width), ==, 0x01);
  378. g_assert_cmphex(flash_read(c, 1 * c->bank_width), ==, 0x23);
  379. g_assert_cmphex(flash_read(c, 2 * c->bank_width), ==, 0x45);
  380. g_assert_cmphex(flash_read(c, 3 * c->bank_width), ==, 0x67);
  381. g_assert_cmphex(flash_read(c, 4 * c->bank_width), ==, bank_mask(c));
  382. /* Test ignored high order bits of address. */
  383. flash_cmd(c, FLASH_ADDR(0x5555), UNLOCK0_CMD);
  384. flash_cmd(c, FLASH_ADDR(0x2AAA), UNLOCK1_CMD);
  385. flash_cmd(c, FLASH_ADDR(0x5555), AUTOSELECT_CMD);
  386. g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
  387. reset(c);
  388. /*
  389. * Program a word on each sector, erase one or two sectors per region, and
  390. * verify that all of those, and only those, are erased.
  391. */
  392. byte_addr = 0;
  393. for (int region = 0; region < nb_erase_regions; ++region) {
  394. for (int i = 0; i < config->nb_blocs[region]; ++i) {
  395. program(c, byte_addr, 0);
  396. byte_addr += config->sector_len[region];
  397. }
  398. }
  399. unlock(c);
  400. flash_cmd(c, UNLOCK0_ADDR, SECOND_UNLOCK_CMD);
  401. unlock(c);
  402. byte_addr = 0;
  403. const uint64_t erase_cmd = replicate(c, SECTOR_ERASE_CMD);
  404. for (int region = 0; region < nb_erase_regions; ++region) {
  405. flash_write(c, byte_addr, erase_cmd);
  406. if (c->nb_blocs[region] > 1) {
  407. flash_write(c, byte_addr + c->sector_len[region], erase_cmd);
  408. }
  409. byte_addr += c->sector_len[region] * c->nb_blocs[region];
  410. }
  411. qtest_clock_step_next(c->qtest); /* Step over the 50 us timeout. */
  412. wait_for_completion(c, 0);
  413. byte_addr = 0;
  414. for (int region = 0; region < nb_erase_regions; ++region) {
  415. for (int i = 0; i < config->nb_blocs[region]; ++i) {
  416. if (i < 2) {
  417. g_assert_cmphex(flash_read(c, byte_addr), ==, bank_mask(c));
  418. } else {
  419. g_assert_cmphex(flash_read(c, byte_addr), ==, 0);
  420. }
  421. byte_addr += config->sector_len[region];
  422. }
  423. }
  424. /* Test erase suspend/resume during erase timeout. */
  425. sector_erase(c, 0);
  426. /*
  427. * Check that DQ 3 is 0 and DQ6 and DQ2 are toggling in the sector being
  428. * erased as well as in a sector not being erased.
  429. */
  430. byte_addr = c->sector_len[0];
  431. status0 = flash_read(c, 0);
  432. status1 = flash_read(c, 0);
  433. g_assert_cmpint(status0 & dq3, ==, 0);
  434. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  435. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  436. status0 = flash_read(c, byte_addr);
  437. status1 = flash_read(c, byte_addr);
  438. g_assert_cmpint(status0 & dq3, ==, 0);
  439. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  440. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  441. /*
  442. * Check that after suspending, DQ6 does not toggle but DQ2 does toggle in
  443. * an erase suspended sector but that neither toggle (we should be
  444. * getting data) in a sector not being erased.
  445. */
  446. erase_suspend(c);
  447. status0 = flash_read(c, 0);
  448. status1 = flash_read(c, 0);
  449. g_assert_cmpint(status0 & dq6, ==, status1 & dq6);
  450. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  451. g_assert_cmpint(flash_read(c, byte_addr), ==, flash_read(c, byte_addr));
  452. /* Check that after resuming, DQ3 is 1 and DQ6 and DQ2 toggle. */
  453. erase_resume(c);
  454. status0 = flash_read(c, 0);
  455. status1 = flash_read(c, 0);
  456. g_assert_cmpint(status0 & dq3, ==, dq3);
  457. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  458. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  459. status0 = flash_read(c, byte_addr);
  460. status1 = flash_read(c, byte_addr);
  461. g_assert_cmpint(status0 & dq3, ==, dq3);
  462. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  463. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  464. wait_for_completion(c, 0);
  465. /* Repeat this process but this time suspend after the timeout. */
  466. sector_erase(c, 0);
  467. qtest_clock_step_next(c->qtest);
  468. /*
  469. * Check that DQ 3 is 1 and DQ6 and DQ2 are toggling in the sector being
  470. * erased as well as in a sector not being erased.
  471. */
  472. byte_addr = c->sector_len[0];
  473. status0 = flash_read(c, 0);
  474. status1 = flash_read(c, 0);
  475. g_assert_cmpint(status0 & dq3, ==, dq3);
  476. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  477. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  478. status0 = flash_read(c, byte_addr);
  479. status1 = flash_read(c, byte_addr);
  480. g_assert_cmpint(status0 & dq3, ==, dq3);
  481. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  482. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  483. /*
  484. * Check that after suspending, DQ6 does not toggle but DQ2 does toggle in
  485. * an erase suspended sector but that neither toggle (we should be
  486. * getting data) in a sector not being erased.
  487. */
  488. erase_suspend(c);
  489. status0 = flash_read(c, 0);
  490. status1 = flash_read(c, 0);
  491. g_assert_cmpint(status0 & dq6, ==, status1 & dq6);
  492. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  493. g_assert_cmpint(flash_read(c, byte_addr), ==, flash_read(c, byte_addr));
  494. /* Check that after resuming, DQ3 is 1 and DQ6 and DQ2 toggle. */
  495. erase_resume(c);
  496. status0 = flash_read(c, 0);
  497. status1 = flash_read(c, 0);
  498. g_assert_cmpint(status0 & dq3, ==, dq3);
  499. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  500. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  501. status0 = flash_read(c, byte_addr);
  502. status1 = flash_read(c, byte_addr);
  503. g_assert_cmpint(status0 & dq3, ==, dq3);
  504. g_assert_cmpint(status0 & dq6, ==, ~status1 & dq6);
  505. g_assert_cmpint(status0 & dq2, ==, ~status1 & dq2);
  506. wait_for_completion(c, 0);
  507. qtest_quit(qtest);
  508. }
  509. /*
  510. * Test that
  511. * 1. enter autoselect mode;
  512. * 2. enter CFI mode; and then
  513. * 3. exit CFI mode
  514. * leaves the flash device in autoselect mode.
  515. */
  516. static void test_cfi_in_autoselect(const void *opaque)
  517. {
  518. const FlashConfig *config = opaque;
  519. QTestState *qtest;
  520. qtest = qtest_initf("-M musicpal,accel=qtest"
  521. " -drive if=pflash,file=%s,format=raw,copy-on-read",
  522. image_path);
  523. FlashConfig explicit_config = expand_config_defaults(config);
  524. explicit_config.qtest = qtest;
  525. const FlashConfig *c = &explicit_config;
  526. /* 1. Enter autoselect. */
  527. unlock(c);
  528. flash_cmd(c, UNLOCK0_ADDR, AUTOSELECT_CMD);
  529. g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
  530. /* 2. Enter CFI. */
  531. flash_cmd(c, CFI_ADDR, CFI_CMD);
  532. g_assert_cmphex(flash_query(c, FLASH_ADDR(0x10)), ==, replicate(c, 'Q'));
  533. g_assert_cmphex(flash_query(c, FLASH_ADDR(0x11)), ==, replicate(c, 'R'));
  534. g_assert_cmphex(flash_query(c, FLASH_ADDR(0x12)), ==, replicate(c, 'Y'));
  535. /* 3. Exit CFI. */
  536. reset(c);
  537. g_assert_cmphex(flash_query(c, FLASH_ADDR(0)), ==, replicate(c, 0xBF));
  538. qtest_quit(qtest);
  539. }
  540. static void cleanup(void *opaque)
  541. {
  542. unlink(image_path);
  543. }
  544. /*
  545. * XXX: Tests are limited to bank_width = 2 for now because that's what
  546. * hw/arm/musicpal.c has.
  547. */
  548. static const FlashConfig configuration[] = {
  549. /* One x16 device. */
  550. {
  551. .bank_width = 2,
  552. },
  553. /* Nonuniform sectors (top boot). */
  554. {
  555. .bank_width = 2,
  556. .nb_blocs = { 127, 1, 2, 1 },
  557. .sector_len = { 0x10000, 0x08000, 0x02000, 0x04000 },
  558. },
  559. /* Nonuniform sectors (bottom boot). */
  560. {
  561. .bank_width = 2,
  562. .nb_blocs = { 1, 2, 1, 127 },
  563. .sector_len = { 0x04000, 0x02000, 0x08000, 0x10000 },
  564. },
  565. };
  566. int main(int argc, char **argv)
  567. {
  568. int fd = mkstemp(image_path);
  569. if (fd == -1) {
  570. g_printerr("Failed to create temporary file %s: %s\n", image_path,
  571. strerror(errno));
  572. exit(EXIT_FAILURE);
  573. }
  574. if (ftruncate(fd, UNIFORM_FLASH_SIZE) < 0) {
  575. int error_code = errno;
  576. close(fd);
  577. unlink(image_path);
  578. g_printerr("Failed to truncate file %s to %u MB: %s\n", image_path,
  579. UNIFORM_FLASH_SIZE, strerror(error_code));
  580. exit(EXIT_FAILURE);
  581. }
  582. close(fd);
  583. qtest_add_abrt_handler(cleanup, NULL);
  584. g_test_init(&argc, &argv, NULL);
  585. size_t nb_configurations = sizeof configuration / sizeof configuration[0];
  586. for (size_t i = 0; i < nb_configurations; ++i) {
  587. const FlashConfig *config = &configuration[i];
  588. char *path = g_strdup_printf("pflash-cfi02"
  589. "/geometry/%dx%x-%dx%x-%dx%x-%dx%x"
  590. "/%d",
  591. config->nb_blocs[0],
  592. config->sector_len[0],
  593. config->nb_blocs[1],
  594. config->sector_len[1],
  595. config->nb_blocs[2],
  596. config->sector_len[2],
  597. config->nb_blocs[3],
  598. config->sector_len[3],
  599. config->bank_width);
  600. qtest_add_data_func(path, config, test_geometry);
  601. g_free(path);
  602. }
  603. qtest_add_data_func("pflash-cfi02/cfi-in-autoselect", &configuration[0],
  604. test_cfi_in_autoselect);
  605. int result = g_test_run();
  606. cleanup(NULL);
  607. return result;
  608. }