pflash_cfi01.c 20 KB

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