pflash_cfi01.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * CFI parallel flash with Intel command set emulation
  3. *
  4. * Copyright (c) 2006 Thorsten Zitterell
  5. * Copyright (c) 2005 Jocelyn Mayer
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  20. */
  21. /*
  22. * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
  23. * Supported commands/modes are:
  24. * - flash read
  25. * - flash write
  26. * - flash ID read
  27. * - sector erase
  28. * - CFI queries
  29. *
  30. * It does not support timings
  31. * It does not support flash interleaving
  32. * It does not implement software data protection as found in many real chips
  33. * It does not implement erase suspend/resume commands
  34. * It does not implement multiple sectors erase
  35. *
  36. * It does not implement much more ...
  37. */
  38. #include "hw.h"
  39. #include "flash.h"
  40. #include "block.h"
  41. #include "qemu-timer.h"
  42. #define PFLASH_BUG(fmt, args...) \
  43. do { \
  44. printf("PFLASH: Possible BUG - " fmt, ##args); \
  45. exit(1); \
  46. } while(0)
  47. /* #define PFLASH_DEBUG */
  48. #ifdef PFLASH_DEBUG
  49. #define DPRINTF(fmt, args...) \
  50. do { \
  51. printf("PFLASH: " fmt , ##args); \
  52. } while (0)
  53. #else
  54. #define DPRINTF(fmt, args...) do { } while (0)
  55. #endif
  56. struct pflash_t {
  57. BlockDriverState *bs;
  58. target_ulong base;
  59. target_ulong sector_len;
  60. target_ulong total_len;
  61. int width;
  62. int wcycle; /* if 0, the flash is read normally */
  63. int bypass;
  64. int ro;
  65. uint8_t cmd;
  66. uint8_t status;
  67. uint16_t ident[4];
  68. uint8_t cfi_len;
  69. uint8_t cfi_table[0x52];
  70. target_ulong counter;
  71. QEMUTimer *timer;
  72. ram_addr_t off;
  73. int fl_mem;
  74. void *storage;
  75. };
  76. static void pflash_timer (void *opaque)
  77. {
  78. pflash_t *pfl = opaque;
  79. DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
  80. /* Reset flash */
  81. pfl->status ^= 0x80;
  82. if (pfl->bypass) {
  83. pfl->wcycle = 2;
  84. } else {
  85. cpu_register_physical_memory(pfl->base, pfl->total_len,
  86. pfl->off | IO_MEM_ROMD | pfl->fl_mem);
  87. pfl->wcycle = 0;
  88. }
  89. pfl->cmd = 0;
  90. }
  91. static uint32_t pflash_read (pflash_t *pfl, target_ulong offset, int width)
  92. {
  93. target_ulong boff;
  94. uint32_t ret;
  95. uint8_t *p;
  96. ret = -1;
  97. boff = offset & 0xFF; /* why this here ?? */
  98. if (pfl->width == 2)
  99. boff = boff >> 1;
  100. else if (pfl->width == 4)
  101. boff = boff >> 2;
  102. DPRINTF("%s: reading offset " TARGET_FMT_lx " under cmd %02x width %d\n",
  103. __func__, offset, pfl->cmd, width);
  104. switch (pfl->cmd) {
  105. case 0x00:
  106. /* Flash area read */
  107. p = pfl->storage;
  108. switch (width) {
  109. case 1:
  110. ret = p[offset];
  111. DPRINTF("%s: data offset " TARGET_FMT_lx " %02x\n",
  112. __func__, offset, ret);
  113. break;
  114. case 2:
  115. #if defined(TARGET_WORDS_BIGENDIAN)
  116. ret = p[offset] << 8;
  117. ret |= p[offset + 1];
  118. #else
  119. ret = p[offset];
  120. ret |= p[offset + 1] << 8;
  121. #endif
  122. DPRINTF("%s: data offset " TARGET_FMT_lx " %04x\n",
  123. __func__, offset, ret);
  124. break;
  125. case 4:
  126. #if defined(TARGET_WORDS_BIGENDIAN)
  127. ret = p[offset] << 24;
  128. ret |= p[offset + 1] << 16;
  129. ret |= p[offset + 2] << 8;
  130. ret |= p[offset + 3];
  131. #else
  132. ret = p[offset];
  133. ret |= p[offset + 1] << 8;
  134. ret |= p[offset + 1] << 8;
  135. ret |= p[offset + 2] << 16;
  136. ret |= p[offset + 3] << 24;
  137. #endif
  138. DPRINTF("%s: data offset " TARGET_FMT_lx " %08x\n",
  139. __func__, offset, ret);
  140. break;
  141. default:
  142. DPRINTF("BUG in %s\n", __func__);
  143. }
  144. break;
  145. case 0x20: /* Block erase */
  146. case 0x50: /* Clear status register */
  147. case 0x60: /* Block /un)lock */
  148. case 0x70: /* Status Register */
  149. case 0xe8: /* Write block */
  150. /* Status register read */
  151. ret = pfl->status;
  152. DPRINTF("%s: status %x\n", __func__, ret);
  153. break;
  154. case 0x98: /* Query mode */
  155. if (boff > pfl->cfi_len)
  156. ret = 0;
  157. else
  158. ret = pfl->cfi_table[boff];
  159. break;
  160. default:
  161. /* This should never happen : reset state & treat it as a read */
  162. DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
  163. pfl->wcycle = 0;
  164. pfl->cmd = 0;
  165. }
  166. return ret;
  167. }
  168. /* update flash content on disk */
  169. static void pflash_update(pflash_t *pfl, int offset,
  170. int size)
  171. {
  172. int offset_end;
  173. if (pfl->bs) {
  174. offset_end = offset + size;
  175. /* round to sectors */
  176. offset = offset >> 9;
  177. offset_end = (offset_end + 511) >> 9;
  178. bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
  179. offset_end - offset);
  180. }
  181. }
  182. static void inline pflash_data_write(pflash_t *pfl, target_ulong offset,
  183. uint32_t value, int width)
  184. {
  185. uint8_t *p = pfl->storage;
  186. DPRINTF("%s: block write offset " TARGET_FMT_lx
  187. " value %x counter " TARGET_FMT_lx "\n",
  188. __func__, offset, value, pfl->counter);
  189. switch (width) {
  190. case 1:
  191. p[offset] = value;
  192. pflash_update(pfl, offset, 1);
  193. break;
  194. case 2:
  195. #if defined(TARGET_WORDS_BIGENDIAN)
  196. p[offset] = value >> 8;
  197. p[offset + 1] = value;
  198. #else
  199. p[offset] = value;
  200. p[offset + 1] = value >> 8;
  201. #endif
  202. pflash_update(pfl, offset, 2);
  203. break;
  204. case 4:
  205. #if defined(TARGET_WORDS_BIGENDIAN)
  206. p[offset] = value >> 24;
  207. p[offset + 1] = value >> 16;
  208. p[offset + 2] = value >> 8;
  209. p[offset + 3] = value;
  210. #else
  211. p[offset] = value;
  212. p[offset + 1] = value >> 8;
  213. p[offset + 2] = value >> 16;
  214. p[offset + 3] = value >> 24;
  215. #endif
  216. pflash_update(pfl, offset, 4);
  217. break;
  218. }
  219. }
  220. static void pflash_write (pflash_t *pfl, target_ulong offset, uint32_t value,
  221. int width)
  222. {
  223. target_ulong boff;
  224. uint8_t *p;
  225. uint8_t cmd;
  226. cmd = value;
  227. DPRINTF("%s: writing offset " TARGET_FMT_lx " value %08x width %d wcycle 0x%x\n",
  228. __func__, offset, value, width, pfl->wcycle);
  229. /* Set the device in I/O access mode */
  230. cpu_register_physical_memory(pfl->base, pfl->total_len, pfl->fl_mem);
  231. boff = offset & (pfl->sector_len - 1);
  232. if (pfl->width == 2)
  233. boff = boff >> 1;
  234. else if (pfl->width == 4)
  235. boff = boff >> 2;
  236. switch (pfl->wcycle) {
  237. case 0:
  238. /* read mode */
  239. switch (cmd) {
  240. case 0x00: /* ??? */
  241. goto reset_flash;
  242. case 0x10: /* Single Byte Program */
  243. case 0x40: /* Single Byte Program */
  244. DPRINTF(stderr, "%s: Single Byte Program\n", __func__);
  245. break;
  246. case 0x20: /* Block erase */
  247. p = pfl->storage;
  248. offset &= ~(pfl->sector_len - 1);
  249. DPRINTF("%s: block erase at " TARGET_FMT_lx " bytes "
  250. TARGET_FMT_lx "\n",
  251. __func__, offset, pfl->sector_len);
  252. memset(p + offset, 0xff, pfl->sector_len);
  253. pflash_update(pfl, offset, pfl->sector_len);
  254. pfl->status |= 0x80; /* Ready! */
  255. break;
  256. case 0x50: /* Clear status bits */
  257. DPRINTF("%s: Clear status bits\n", __func__);
  258. pfl->status = 0x0;
  259. goto reset_flash;
  260. case 0x60: /* Block (un)lock */
  261. DPRINTF("%s: Block unlock\n", __func__);
  262. break;
  263. case 0x70: /* Status Register */
  264. DPRINTF("%s: Read status register\n", __func__);
  265. pfl->cmd = cmd;
  266. return;
  267. case 0x98: /* CFI query */
  268. DPRINTF("%s: CFI query\n", __func__);
  269. break;
  270. case 0xe8: /* Write to buffer */
  271. DPRINTF("%s: Write to buffer\n", __func__);
  272. pfl->status |= 0x80; /* Ready! */
  273. break;
  274. case 0xff: /* Read array mode */
  275. DPRINTF("%s: Read array mode\n", __func__);
  276. goto reset_flash;
  277. default:
  278. goto error_flash;
  279. }
  280. pfl->wcycle++;
  281. pfl->cmd = cmd;
  282. return;
  283. case 1:
  284. switch (pfl->cmd) {
  285. case 0x10: /* Single Byte Program */
  286. case 0x40: /* Single Byte Program */
  287. DPRINTF("%s: Single Byte Program\n", __func__);
  288. pflash_data_write(pfl, offset, value, width);
  289. pfl->status |= 0x80; /* Ready! */
  290. pfl->wcycle = 0;
  291. break;
  292. case 0x20: /* Block erase */
  293. case 0x28:
  294. if (cmd == 0xd0) { /* confirm */
  295. pfl->wcycle = 0;
  296. pfl->status |= 0x80;
  297. } else if (cmd == 0xff) { /* read array mode */
  298. goto reset_flash;
  299. } else
  300. goto error_flash;
  301. break;
  302. case 0xe8:
  303. DPRINTF("%s: block write of %x bytes\n", __func__, value);
  304. pfl->counter = value;
  305. pfl->wcycle++;
  306. break;
  307. case 0x60:
  308. if (cmd == 0xd0) {
  309. pfl->wcycle = 0;
  310. pfl->status |= 0x80;
  311. } else if (cmd == 0x01) {
  312. pfl->wcycle = 0;
  313. pfl->status |= 0x80;
  314. } else if (cmd == 0xff) {
  315. goto reset_flash;
  316. } else {
  317. DPRINTF("%s: Unknown (un)locking command\n", __func__);
  318. goto reset_flash;
  319. }
  320. break;
  321. case 0x98:
  322. if (cmd == 0xff) {
  323. goto reset_flash;
  324. } else {
  325. DPRINTF("%s: leaving query mode\n", __func__);
  326. }
  327. break;
  328. default:
  329. goto error_flash;
  330. }
  331. return;
  332. case 2:
  333. switch (pfl->cmd) {
  334. case 0xe8: /* Block write */
  335. pflash_data_write(pfl, offset, value, width);
  336. pfl->status |= 0x80;
  337. if (!pfl->counter) {
  338. DPRINTF("%s: block write finished\n", __func__);
  339. pfl->wcycle++;
  340. }
  341. pfl->counter--;
  342. break;
  343. default:
  344. goto error_flash;
  345. }
  346. return;
  347. case 3: /* Confirm mode */
  348. switch (pfl->cmd) {
  349. case 0xe8: /* Block write */
  350. if (cmd == 0xd0) {
  351. pfl->wcycle = 0;
  352. pfl->status |= 0x80;
  353. } else {
  354. DPRINTF("%s: unknown command for \"write block\"\n", __func__);
  355. PFLASH_BUG("Write block confirm");
  356. goto reset_flash;
  357. }
  358. break;
  359. default:
  360. goto error_flash;
  361. }
  362. return;
  363. default:
  364. /* Should never happen */
  365. DPRINTF("%s: invalid write state\n", __func__);
  366. goto reset_flash;
  367. }
  368. return;
  369. error_flash:
  370. printf("%s: Unimplemented flash cmd sequence "
  371. "(offset " TARGET_FMT_lx ", wcycle 0x%x cmd 0x%x value 0x%x)\n",
  372. __func__, offset, pfl->wcycle, pfl->cmd, value);
  373. reset_flash:
  374. cpu_register_physical_memory(pfl->base, pfl->total_len,
  375. pfl->off | IO_MEM_ROMD | pfl->fl_mem);
  376. pfl->bypass = 0;
  377. pfl->wcycle = 0;
  378. pfl->cmd = 0;
  379. return;
  380. }
  381. static uint32_t pflash_readb (void *opaque, target_phys_addr_t addr)
  382. {
  383. return pflash_read(opaque, addr, 1);
  384. }
  385. static uint32_t pflash_readw (void *opaque, target_phys_addr_t addr)
  386. {
  387. pflash_t *pfl = opaque;
  388. return pflash_read(pfl, addr, 2);
  389. }
  390. static uint32_t pflash_readl (void *opaque, target_phys_addr_t addr)
  391. {
  392. pflash_t *pfl = opaque;
  393. return pflash_read(pfl, addr, 4);
  394. }
  395. static void pflash_writeb (void *opaque, target_phys_addr_t addr,
  396. uint32_t value)
  397. {
  398. pflash_write(opaque, addr, value, 1);
  399. }
  400. static void pflash_writew (void *opaque, target_phys_addr_t addr,
  401. uint32_t value)
  402. {
  403. pflash_t *pfl = opaque;
  404. pflash_write(pfl, addr, value, 2);
  405. }
  406. static void pflash_writel (void *opaque, target_phys_addr_t addr,
  407. uint32_t value)
  408. {
  409. pflash_t *pfl = opaque;
  410. pflash_write(pfl, addr, value, 4);
  411. }
  412. static CPUWriteMemoryFunc *pflash_write_ops[] = {
  413. &pflash_writeb,
  414. &pflash_writew,
  415. &pflash_writel,
  416. };
  417. static CPUReadMemoryFunc *pflash_read_ops[] = {
  418. &pflash_readb,
  419. &pflash_readw,
  420. &pflash_readl,
  421. };
  422. /* Count trailing zeroes of a 32 bits quantity */
  423. static int ctz32 (uint32_t n)
  424. {
  425. int ret;
  426. ret = 0;
  427. if (!(n & 0xFFFF)) {
  428. ret += 16;
  429. n = n >> 16;
  430. }
  431. if (!(n & 0xFF)) {
  432. ret += 8;
  433. n = n >> 8;
  434. }
  435. if (!(n & 0xF)) {
  436. ret += 4;
  437. n = n >> 4;
  438. }
  439. if (!(n & 0x3)) {
  440. ret += 2;
  441. n = n >> 2;
  442. }
  443. if (!(n & 0x1)) {
  444. ret++;
  445. n = n >> 1;
  446. }
  447. #if 0 /* This is not necessary as n is never 0 */
  448. if (!n)
  449. ret++;
  450. #endif
  451. return ret;
  452. }
  453. pflash_t *pflash_cfi01_register(target_phys_addr_t base, ram_addr_t off,
  454. BlockDriverState *bs, uint32_t sector_len,
  455. int nb_blocs, int width,
  456. uint16_t id0, uint16_t id1,
  457. uint16_t id2, uint16_t id3)
  458. {
  459. pflash_t *pfl;
  460. target_long total_len;
  461. total_len = sector_len * nb_blocs;
  462. /* XXX: to be fixed */
  463. #if 0
  464. if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
  465. total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
  466. return NULL;
  467. #endif
  468. pfl = qemu_mallocz(sizeof(pflash_t));
  469. pfl->storage = phys_ram_base + off;
  470. pfl->fl_mem = cpu_register_io_memory(0,
  471. pflash_read_ops, pflash_write_ops, pfl);
  472. pfl->off = off;
  473. cpu_register_physical_memory(base, total_len,
  474. off | pfl->fl_mem | IO_MEM_ROMD);
  475. pfl->bs = bs;
  476. if (pfl->bs) {
  477. /* read the initial flash content */
  478. bdrv_read(pfl->bs, 0, pfl->storage, total_len >> 9);
  479. }
  480. #if 0 /* XXX: there should be a bit to set up read-only,
  481. * the same way the hardware does (with WP pin).
  482. */
  483. pfl->ro = 1;
  484. #else
  485. pfl->ro = 0;
  486. #endif
  487. pfl->timer = qemu_new_timer(vm_clock, pflash_timer, pfl);
  488. pfl->base = base;
  489. pfl->sector_len = sector_len;
  490. pfl->total_len = total_len;
  491. pfl->width = width;
  492. pfl->wcycle = 0;
  493. pfl->cmd = 0;
  494. pfl->status = 0;
  495. pfl->ident[0] = id0;
  496. pfl->ident[1] = id1;
  497. pfl->ident[2] = id2;
  498. pfl->ident[3] = id3;
  499. /* Hardcoded CFI table */
  500. pfl->cfi_len = 0x52;
  501. /* Standard "QRY" string */
  502. pfl->cfi_table[0x10] = 'Q';
  503. pfl->cfi_table[0x11] = 'R';
  504. pfl->cfi_table[0x12] = 'Y';
  505. /* Command set (Intel) */
  506. pfl->cfi_table[0x13] = 0x01;
  507. pfl->cfi_table[0x14] = 0x00;
  508. /* Primary extended table address (none) */
  509. pfl->cfi_table[0x15] = 0x31;
  510. pfl->cfi_table[0x16] = 0x00;
  511. /* Alternate command set (none) */
  512. pfl->cfi_table[0x17] = 0x00;
  513. pfl->cfi_table[0x18] = 0x00;
  514. /* Alternate extended table (none) */
  515. pfl->cfi_table[0x19] = 0x00;
  516. pfl->cfi_table[0x1A] = 0x00;
  517. /* Vcc min */
  518. pfl->cfi_table[0x1B] = 0x45;
  519. /* Vcc max */
  520. pfl->cfi_table[0x1C] = 0x55;
  521. /* Vpp min (no Vpp pin) */
  522. pfl->cfi_table[0x1D] = 0x00;
  523. /* Vpp max (no Vpp pin) */
  524. pfl->cfi_table[0x1E] = 0x00;
  525. /* Reserved */
  526. pfl->cfi_table[0x1F] = 0x07;
  527. /* Timeout for min size buffer write */
  528. pfl->cfi_table[0x20] = 0x07;
  529. /* Typical timeout for block erase */
  530. pfl->cfi_table[0x21] = 0x0a;
  531. /* Typical timeout for full chip erase (4096 ms) */
  532. pfl->cfi_table[0x22] = 0x00;
  533. /* Reserved */
  534. pfl->cfi_table[0x23] = 0x04;
  535. /* Max timeout for buffer write */
  536. pfl->cfi_table[0x24] = 0x04;
  537. /* Max timeout for block erase */
  538. pfl->cfi_table[0x25] = 0x04;
  539. /* Max timeout for chip erase */
  540. pfl->cfi_table[0x26] = 0x00;
  541. /* Device size */
  542. pfl->cfi_table[0x27] = ctz32(total_len); // + 1;
  543. /* Flash device interface (8 & 16 bits) */
  544. pfl->cfi_table[0x28] = 0x02;
  545. pfl->cfi_table[0x29] = 0x00;
  546. /* Max number of bytes in multi-bytes write */
  547. pfl->cfi_table[0x2A] = 0x0B;
  548. pfl->cfi_table[0x2B] = 0x00;
  549. /* Number of erase block regions (uniform) */
  550. pfl->cfi_table[0x2C] = 0x01;
  551. /* Erase block region 1 */
  552. pfl->cfi_table[0x2D] = nb_blocs - 1;
  553. pfl->cfi_table[0x2E] = (nb_blocs - 1) >> 8;
  554. pfl->cfi_table[0x2F] = sector_len >> 8;
  555. pfl->cfi_table[0x30] = sector_len >> 16;
  556. /* Extended */
  557. pfl->cfi_table[0x31] = 'P';
  558. pfl->cfi_table[0x32] = 'R';
  559. pfl->cfi_table[0x33] = 'I';
  560. pfl->cfi_table[0x34] = '1';
  561. pfl->cfi_table[0x35] = '1';
  562. pfl->cfi_table[0x36] = 0x00;
  563. pfl->cfi_table[0x37] = 0x00;
  564. pfl->cfi_table[0x38] = 0x00;
  565. pfl->cfi_table[0x39] = 0x00;
  566. pfl->cfi_table[0x3a] = 0x00;
  567. pfl->cfi_table[0x3b] = 0x00;
  568. pfl->cfi_table[0x3c] = 0x00;
  569. return pfl;
  570. }