pflash_cfi01.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
  22. * Supported commands/modes are:
  23. * - flash read
  24. * - flash write
  25. * - flash ID read
  26. * - sector erase
  27. * - CFI queries
  28. *
  29. * It does not support timings
  30. * It does not support flash interleaving
  31. * It does not implement software data protection as found in many real chips
  32. * It does not implement erase suspend/resume commands
  33. * It does not implement multiple sectors erase
  34. *
  35. * It does not implement much more ...
  36. */
  37. #include "hw.h"
  38. #include "flash.h"
  39. #include "block.h"
  40. #include "qemu-timer.h"
  41. #include "exec-memory.h"
  42. #define PFLASH_BUG(fmt, ...) \
  43. do { \
  44. printf("PFLASH: Possible BUG - " fmt, ## __VA_ARGS__); \
  45. exit(1); \
  46. } while(0)
  47. /* #define PFLASH_DEBUG */
  48. #ifdef PFLASH_DEBUG
  49. #define DPRINTF(fmt, ...) \
  50. do { \
  51. printf("PFLASH: " fmt , ## __VA_ARGS__); \
  52. } while (0)
  53. #else
  54. #define DPRINTF(fmt, ...) do { } while (0)
  55. #endif
  56. struct pflash_t {
  57. BlockDriverState *bs;
  58. target_phys_addr_t base;
  59. target_phys_addr_t sector_len;
  60. target_phys_addr_t 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_phys_addr_t counter;
  71. unsigned int writeblock_size;
  72. QEMUTimer *timer;
  73. MemoryRegion 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. memory_region_rom_device_set_readable(&pfl->mem, true);
  86. pfl->wcycle = 0;
  87. }
  88. pfl->cmd = 0;
  89. }
  90. static uint32_t pflash_read (pflash_t *pfl, target_phys_addr_t offset,
  91. int width, int be)
  92. {
  93. target_phys_addr_t 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. #if 0
  103. DPRINTF("%s: reading offset " TARGET_FMT_plx " under cmd %02x width %d\n",
  104. __func__, offset, pfl->cmd, width);
  105. #endif
  106. switch (pfl->cmd) {
  107. case 0x00:
  108. /* Flash area read */
  109. p = pfl->storage;
  110. switch (width) {
  111. case 1:
  112. ret = p[offset];
  113. DPRINTF("%s: data offset " TARGET_FMT_plx " %02x\n",
  114. __func__, offset, ret);
  115. break;
  116. case 2:
  117. if (be) {
  118. ret = p[offset] << 8;
  119. ret |= p[offset + 1];
  120. } else {
  121. ret = p[offset];
  122. ret |= p[offset + 1] << 8;
  123. }
  124. DPRINTF("%s: data offset " TARGET_FMT_plx " %04x\n",
  125. __func__, offset, ret);
  126. break;
  127. case 4:
  128. if (be) {
  129. ret = p[offset] << 24;
  130. ret |= p[offset + 1] << 16;
  131. ret |= p[offset + 2] << 8;
  132. ret |= p[offset + 3];
  133. } else {
  134. ret = p[offset];
  135. ret |= p[offset + 1] << 8;
  136. ret |= p[offset + 1] << 8;
  137. ret |= p[offset + 2] << 16;
  138. ret |= p[offset + 3] << 24;
  139. }
  140. DPRINTF("%s: data offset " TARGET_FMT_plx " %08x\n",
  141. __func__, offset, ret);
  142. break;
  143. default:
  144. DPRINTF("BUG in %s\n", __func__);
  145. }
  146. break;
  147. case 0x20: /* Block erase */
  148. case 0x50: /* Clear status register */
  149. case 0x60: /* Block /un)lock */
  150. case 0x70: /* Status Register */
  151. case 0xe8: /* Write block */
  152. /* Status register read */
  153. ret = pfl->status;
  154. DPRINTF("%s: status %x\n", __func__, ret);
  155. break;
  156. case 0x90:
  157. switch (boff) {
  158. case 0:
  159. ret = pfl->ident[0] << 8 | pfl->ident[1];
  160. DPRINTF("%s: Manufacturer Code %04x\n", __func__, ret);
  161. break;
  162. case 1:
  163. ret = pfl->ident[2] << 8 | pfl->ident[3];
  164. DPRINTF("%s: Device ID Code %04x\n", __func__, ret);
  165. break;
  166. default:
  167. DPRINTF("%s: Read Device Information boff=%x\n", __func__, boff);
  168. ret = 0;
  169. break;
  170. }
  171. break;
  172. case 0x98: /* Query mode */
  173. if (boff > pfl->cfi_len)
  174. ret = 0;
  175. else
  176. ret = pfl->cfi_table[boff];
  177. break;
  178. default:
  179. /* This should never happen : reset state & treat it as a read */
  180. DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
  181. pfl->wcycle = 0;
  182. pfl->cmd = 0;
  183. }
  184. return ret;
  185. }
  186. /* update flash content on disk */
  187. static void pflash_update(pflash_t *pfl, int offset,
  188. int size)
  189. {
  190. int offset_end;
  191. if (pfl->bs) {
  192. offset_end = offset + size;
  193. /* round to sectors */
  194. offset = offset >> 9;
  195. offset_end = (offset_end + 511) >> 9;
  196. bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
  197. offset_end - offset);
  198. }
  199. }
  200. static inline void pflash_data_write(pflash_t *pfl, target_phys_addr_t offset,
  201. uint32_t value, int width, int be)
  202. {
  203. uint8_t *p = pfl->storage;
  204. DPRINTF("%s: block write offset " TARGET_FMT_plx
  205. " value %x counter " TARGET_FMT_plx "\n",
  206. __func__, offset, value, pfl->counter);
  207. switch (width) {
  208. case 1:
  209. p[offset] = value;
  210. break;
  211. case 2:
  212. if (be) {
  213. p[offset] = value >> 8;
  214. p[offset + 1] = value;
  215. } else {
  216. p[offset] = value;
  217. p[offset + 1] = value >> 8;
  218. }
  219. break;
  220. case 4:
  221. if (be) {
  222. p[offset] = value >> 24;
  223. p[offset + 1] = value >> 16;
  224. p[offset + 2] = value >> 8;
  225. p[offset + 3] = value;
  226. } else {
  227. p[offset] = value;
  228. p[offset + 1] = value >> 8;
  229. p[offset + 2] = value >> 16;
  230. p[offset + 3] = value >> 24;
  231. }
  232. break;
  233. }
  234. }
  235. static void pflash_write(pflash_t *pfl, target_phys_addr_t offset,
  236. uint32_t value, int width, int be)
  237. {
  238. uint8_t *p;
  239. uint8_t cmd;
  240. cmd = value;
  241. DPRINTF("%s: writing offset " TARGET_FMT_plx " value %08x width %d wcycle 0x%x\n",
  242. __func__, offset, value, width, pfl->wcycle);
  243. if (!pfl->wcycle) {
  244. /* Set the device in I/O access mode */
  245. memory_region_rom_device_set_readable(&pfl->mem, false);
  246. }
  247. switch (pfl->wcycle) {
  248. case 0:
  249. /* read mode */
  250. switch (cmd) {
  251. case 0x00: /* ??? */
  252. goto reset_flash;
  253. case 0x10: /* Single Byte Program */
  254. case 0x40: /* Single Byte Program */
  255. DPRINTF("%s: Single Byte Program\n", __func__);
  256. break;
  257. case 0x20: /* Block erase */
  258. p = pfl->storage;
  259. offset &= ~(pfl->sector_len - 1);
  260. DPRINTF("%s: block erase at " TARGET_FMT_plx " bytes "
  261. TARGET_FMT_plx "\n",
  262. __func__, offset, pfl->sector_len);
  263. memset(p + offset, 0xff, pfl->sector_len);
  264. pflash_update(pfl, offset, pfl->sector_len);
  265. pfl->status |= 0x80; /* Ready! */
  266. break;
  267. case 0x50: /* Clear status bits */
  268. DPRINTF("%s: Clear status bits\n", __func__);
  269. pfl->status = 0x0;
  270. goto reset_flash;
  271. case 0x60: /* Block (un)lock */
  272. DPRINTF("%s: Block unlock\n", __func__);
  273. break;
  274. case 0x70: /* Status Register */
  275. DPRINTF("%s: Read status register\n", __func__);
  276. pfl->cmd = cmd;
  277. return;
  278. case 0x90: /* Read Device ID */
  279. DPRINTF("%s: Read Device information\n", __func__);
  280. pfl->cmd = cmd;
  281. return;
  282. case 0x98: /* CFI query */
  283. DPRINTF("%s: CFI query\n", __func__);
  284. break;
  285. case 0xe8: /* Write to buffer */
  286. DPRINTF("%s: Write to buffer\n", __func__);
  287. pfl->status |= 0x80; /* Ready! */
  288. break;
  289. case 0xff: /* Read array mode */
  290. DPRINTF("%s: Read array mode\n", __func__);
  291. goto reset_flash;
  292. default:
  293. goto error_flash;
  294. }
  295. pfl->wcycle++;
  296. pfl->cmd = cmd;
  297. return;
  298. case 1:
  299. switch (pfl->cmd) {
  300. case 0x10: /* Single Byte Program */
  301. case 0x40: /* Single Byte Program */
  302. DPRINTF("%s: Single Byte Program\n", __func__);
  303. pflash_data_write(pfl, offset, value, width, be);
  304. pflash_update(pfl, offset, width);
  305. pfl->status |= 0x80; /* Ready! */
  306. pfl->wcycle = 0;
  307. break;
  308. case 0x20: /* Block erase */
  309. case 0x28:
  310. if (cmd == 0xd0) { /* confirm */
  311. pfl->wcycle = 0;
  312. pfl->status |= 0x80;
  313. } else if (cmd == 0xff) { /* read array mode */
  314. goto reset_flash;
  315. } else
  316. goto error_flash;
  317. break;
  318. case 0xe8:
  319. DPRINTF("%s: block write of %x bytes\n", __func__, value);
  320. pfl->counter = value;
  321. pfl->wcycle++;
  322. break;
  323. case 0x60:
  324. if (cmd == 0xd0) {
  325. pfl->wcycle = 0;
  326. pfl->status |= 0x80;
  327. } else if (cmd == 0x01) {
  328. pfl->wcycle = 0;
  329. pfl->status |= 0x80;
  330. } else if (cmd == 0xff) {
  331. goto reset_flash;
  332. } else {
  333. DPRINTF("%s: Unknown (un)locking command\n", __func__);
  334. goto reset_flash;
  335. }
  336. break;
  337. case 0x98:
  338. if (cmd == 0xff) {
  339. goto reset_flash;
  340. } else {
  341. DPRINTF("%s: leaving query mode\n", __func__);
  342. }
  343. break;
  344. default:
  345. goto error_flash;
  346. }
  347. return;
  348. case 2:
  349. switch (pfl->cmd) {
  350. case 0xe8: /* Block write */
  351. pflash_data_write(pfl, offset, value, width, be);
  352. pfl->status |= 0x80;
  353. if (!pfl->counter) {
  354. target_phys_addr_t mask = pfl->writeblock_size - 1;
  355. mask = ~mask;
  356. DPRINTF("%s: block write finished\n", __func__);
  357. pfl->wcycle++;
  358. /* Flush the entire write buffer onto backing storage. */
  359. pflash_update(pfl, offset & mask, pfl->writeblock_size);
  360. }
  361. pfl->counter--;
  362. break;
  363. default:
  364. goto error_flash;
  365. }
  366. return;
  367. case 3: /* Confirm mode */
  368. switch (pfl->cmd) {
  369. case 0xe8: /* Block write */
  370. if (cmd == 0xd0) {
  371. pfl->wcycle = 0;
  372. pfl->status |= 0x80;
  373. } else {
  374. DPRINTF("%s: unknown command for \"write block\"\n", __func__);
  375. PFLASH_BUG("Write block confirm");
  376. goto reset_flash;
  377. }
  378. break;
  379. default:
  380. goto error_flash;
  381. }
  382. return;
  383. default:
  384. /* Should never happen */
  385. DPRINTF("%s: invalid write state\n", __func__);
  386. goto reset_flash;
  387. }
  388. return;
  389. error_flash:
  390. printf("%s: Unimplemented flash cmd sequence "
  391. "(offset " TARGET_FMT_plx ", wcycle 0x%x cmd 0x%x value 0x%x)\n",
  392. __func__, offset, pfl->wcycle, pfl->cmd, value);
  393. reset_flash:
  394. memory_region_rom_device_set_readable(&pfl->mem, true);
  395. pfl->bypass = 0;
  396. pfl->wcycle = 0;
  397. pfl->cmd = 0;
  398. return;
  399. }
  400. static uint32_t pflash_readb_be(void *opaque, target_phys_addr_t addr)
  401. {
  402. return pflash_read(opaque, addr, 1, 1);
  403. }
  404. static uint32_t pflash_readb_le(void *opaque, target_phys_addr_t addr)
  405. {
  406. return pflash_read(opaque, addr, 1, 0);
  407. }
  408. static uint32_t pflash_readw_be(void *opaque, target_phys_addr_t addr)
  409. {
  410. pflash_t *pfl = opaque;
  411. return pflash_read(pfl, addr, 2, 1);
  412. }
  413. static uint32_t pflash_readw_le(void *opaque, target_phys_addr_t addr)
  414. {
  415. pflash_t *pfl = opaque;
  416. return pflash_read(pfl, addr, 2, 0);
  417. }
  418. static uint32_t pflash_readl_be(void *opaque, target_phys_addr_t addr)
  419. {
  420. pflash_t *pfl = opaque;
  421. return pflash_read(pfl, addr, 4, 1);
  422. }
  423. static uint32_t pflash_readl_le(void *opaque, target_phys_addr_t addr)
  424. {
  425. pflash_t *pfl = opaque;
  426. return pflash_read(pfl, addr, 4, 0);
  427. }
  428. static void pflash_writeb_be(void *opaque, target_phys_addr_t addr,
  429. uint32_t value)
  430. {
  431. pflash_write(opaque, addr, value, 1, 1);
  432. }
  433. static void pflash_writeb_le(void *opaque, target_phys_addr_t addr,
  434. uint32_t value)
  435. {
  436. pflash_write(opaque, addr, value, 1, 0);
  437. }
  438. static void pflash_writew_be(void *opaque, target_phys_addr_t addr,
  439. uint32_t value)
  440. {
  441. pflash_t *pfl = opaque;
  442. pflash_write(pfl, addr, value, 2, 1);
  443. }
  444. static void pflash_writew_le(void *opaque, target_phys_addr_t addr,
  445. uint32_t value)
  446. {
  447. pflash_t *pfl = opaque;
  448. pflash_write(pfl, addr, value, 2, 0);
  449. }
  450. static void pflash_writel_be(void *opaque, target_phys_addr_t addr,
  451. uint32_t value)
  452. {
  453. pflash_t *pfl = opaque;
  454. pflash_write(pfl, addr, value, 4, 1);
  455. }
  456. static void pflash_writel_le(void *opaque, target_phys_addr_t addr,
  457. uint32_t value)
  458. {
  459. pflash_t *pfl = opaque;
  460. pflash_write(pfl, addr, value, 4, 0);
  461. }
  462. static const MemoryRegionOps pflash_cfi01_ops_be = {
  463. .old_mmio = {
  464. .read = { pflash_readb_be, pflash_readw_be, pflash_readl_be, },
  465. .write = { pflash_writeb_be, pflash_writew_be, pflash_writel_be, },
  466. },
  467. .endianness = DEVICE_NATIVE_ENDIAN,
  468. };
  469. static const MemoryRegionOps pflash_cfi01_ops_le = {
  470. .old_mmio = {
  471. .read = { pflash_readb_le, pflash_readw_le, pflash_readl_le, },
  472. .write = { pflash_writeb_le, pflash_writew_le, pflash_writel_le, },
  473. },
  474. .endianness = DEVICE_NATIVE_ENDIAN,
  475. };
  476. /* Count trailing zeroes of a 32 bits quantity */
  477. static int ctz32 (uint32_t n)
  478. {
  479. int ret;
  480. ret = 0;
  481. if (!(n & 0xFFFF)) {
  482. ret += 16;
  483. n = n >> 16;
  484. }
  485. if (!(n & 0xFF)) {
  486. ret += 8;
  487. n = n >> 8;
  488. }
  489. if (!(n & 0xF)) {
  490. ret += 4;
  491. n = n >> 4;
  492. }
  493. if (!(n & 0x3)) {
  494. ret += 2;
  495. n = n >> 2;
  496. }
  497. if (!(n & 0x1)) {
  498. ret++;
  499. #if 0 /* This is not necessary as n is never 0 */
  500. n = n >> 1;
  501. #endif
  502. }
  503. #if 0 /* This is not necessary as n is never 0 */
  504. if (!n)
  505. ret++;
  506. #endif
  507. return ret;
  508. }
  509. pflash_t *pflash_cfi01_register(target_phys_addr_t base,
  510. DeviceState *qdev, const char *name,
  511. target_phys_addr_t size,
  512. BlockDriverState *bs, uint32_t sector_len,
  513. int nb_blocs, int width,
  514. uint16_t id0, uint16_t id1,
  515. uint16_t id2, uint16_t id3, int be)
  516. {
  517. pflash_t *pfl;
  518. target_phys_addr_t total_len;
  519. int ret;
  520. total_len = sector_len * nb_blocs;
  521. /* XXX: to be fixed */
  522. #if 0
  523. if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
  524. total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
  525. return NULL;
  526. #endif
  527. pfl = g_malloc0(sizeof(pflash_t));
  528. memory_region_init_rom_device(
  529. &pfl->mem, be ? &pflash_cfi01_ops_be : &pflash_cfi01_ops_le, pfl,
  530. qdev, name, size);
  531. pfl->storage = memory_region_get_ram_ptr(&pfl->mem);
  532. memory_region_add_subregion(get_system_memory(), base, &pfl->mem);
  533. pfl->bs = bs;
  534. if (pfl->bs) {
  535. /* read the initial flash content */
  536. ret = bdrv_read(pfl->bs, 0, pfl->storage, total_len >> 9);
  537. if (ret < 0) {
  538. memory_region_del_subregion(get_system_memory(), &pfl->mem);
  539. memory_region_destroy(&pfl->mem);
  540. g_free(pfl);
  541. return NULL;
  542. }
  543. bdrv_attach_dev_nofail(pfl->bs, pfl);
  544. }
  545. #if 0 /* XXX: there should be a bit to set up read-only,
  546. * the same way the hardware does (with WP pin).
  547. */
  548. pfl->ro = 1;
  549. #else
  550. pfl->ro = 0;
  551. #endif
  552. pfl->timer = qemu_new_timer_ns(vm_clock, pflash_timer, pfl);
  553. pfl->base = base;
  554. pfl->sector_len = sector_len;
  555. pfl->total_len = total_len;
  556. pfl->width = width;
  557. pfl->wcycle = 0;
  558. pfl->cmd = 0;
  559. pfl->status = 0;
  560. pfl->ident[0] = id0;
  561. pfl->ident[1] = id1;
  562. pfl->ident[2] = id2;
  563. pfl->ident[3] = id3;
  564. /* Hardcoded CFI table */
  565. pfl->cfi_len = 0x52;
  566. /* Standard "QRY" string */
  567. pfl->cfi_table[0x10] = 'Q';
  568. pfl->cfi_table[0x11] = 'R';
  569. pfl->cfi_table[0x12] = 'Y';
  570. /* Command set (Intel) */
  571. pfl->cfi_table[0x13] = 0x01;
  572. pfl->cfi_table[0x14] = 0x00;
  573. /* Primary extended table address (none) */
  574. pfl->cfi_table[0x15] = 0x31;
  575. pfl->cfi_table[0x16] = 0x00;
  576. /* Alternate command set (none) */
  577. pfl->cfi_table[0x17] = 0x00;
  578. pfl->cfi_table[0x18] = 0x00;
  579. /* Alternate extended table (none) */
  580. pfl->cfi_table[0x19] = 0x00;
  581. pfl->cfi_table[0x1A] = 0x00;
  582. /* Vcc min */
  583. pfl->cfi_table[0x1B] = 0x45;
  584. /* Vcc max */
  585. pfl->cfi_table[0x1C] = 0x55;
  586. /* Vpp min (no Vpp pin) */
  587. pfl->cfi_table[0x1D] = 0x00;
  588. /* Vpp max (no Vpp pin) */
  589. pfl->cfi_table[0x1E] = 0x00;
  590. /* Reserved */
  591. pfl->cfi_table[0x1F] = 0x07;
  592. /* Timeout for min size buffer write */
  593. pfl->cfi_table[0x20] = 0x07;
  594. /* Typical timeout for block erase */
  595. pfl->cfi_table[0x21] = 0x0a;
  596. /* Typical timeout for full chip erase (4096 ms) */
  597. pfl->cfi_table[0x22] = 0x00;
  598. /* Reserved */
  599. pfl->cfi_table[0x23] = 0x04;
  600. /* Max timeout for buffer write */
  601. pfl->cfi_table[0x24] = 0x04;
  602. /* Max timeout for block erase */
  603. pfl->cfi_table[0x25] = 0x04;
  604. /* Max timeout for chip erase */
  605. pfl->cfi_table[0x26] = 0x00;
  606. /* Device size */
  607. pfl->cfi_table[0x27] = ctz32(total_len); // + 1;
  608. /* Flash device interface (8 & 16 bits) */
  609. pfl->cfi_table[0x28] = 0x02;
  610. pfl->cfi_table[0x29] = 0x00;
  611. /* Max number of bytes in multi-bytes write */
  612. if (width == 1) {
  613. pfl->cfi_table[0x2A] = 0x08;
  614. } else {
  615. pfl->cfi_table[0x2A] = 0x0B;
  616. }
  617. pfl->writeblock_size = 1 << pfl->cfi_table[0x2A];
  618. pfl->cfi_table[0x2B] = 0x00;
  619. /* Number of erase block regions (uniform) */
  620. pfl->cfi_table[0x2C] = 0x01;
  621. /* Erase block region 1 */
  622. pfl->cfi_table[0x2D] = nb_blocs - 1;
  623. pfl->cfi_table[0x2E] = (nb_blocs - 1) >> 8;
  624. pfl->cfi_table[0x2F] = sector_len >> 8;
  625. pfl->cfi_table[0x30] = sector_len >> 16;
  626. /* Extended */
  627. pfl->cfi_table[0x31] = 'P';
  628. pfl->cfi_table[0x32] = 'R';
  629. pfl->cfi_table[0x33] = 'I';
  630. pfl->cfi_table[0x34] = '1';
  631. pfl->cfi_table[0x35] = '1';
  632. pfl->cfi_table[0x36] = 0x00;
  633. pfl->cfi_table[0x37] = 0x00;
  634. pfl->cfi_table[0x38] = 0x00;
  635. pfl->cfi_table[0x39] = 0x00;
  636. pfl->cfi_table[0x3a] = 0x00;
  637. pfl->cfi_table[0x3b] = 0x00;
  638. pfl->cfi_table[0x3c] = 0x00;
  639. return pfl;
  640. }
  641. MemoryRegion *pflash_cfi01_get_memory(pflash_t *fl)
  642. {
  643. return &fl->mem;
  644. }