2
0

pflash_cfi02.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * CFI parallel flash with AMD command set emulation
  3. *
  4. * Copyright (c) 2005 Jocelyn Mayer
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
  21. * Supported commands/modes are:
  22. * - flash read
  23. * - flash write
  24. * - flash ID read
  25. * - sector erase
  26. * - chip erase
  27. * - unlock bypass command
  28. * - CFI queries
  29. *
  30. * It does not support flash interleaving.
  31. * It does not implement boot blocs with reduced size
  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. #include "hw.h"
  37. #include "flash.h"
  38. #include "qemu-timer.h"
  39. #include "block.h"
  40. //#define PFLASH_DEBUG
  41. #ifdef PFLASH_DEBUG
  42. #define DPRINTF(fmt, ...) \
  43. do { \
  44. printf("PFLASH: " fmt , ## __VA_ARGS__); \
  45. } while (0)
  46. #else
  47. #define DPRINTF(fmt, ...) do { } while (0)
  48. #endif
  49. #define PFLASH_LAZY_ROMD_THRESHOLD 42
  50. struct pflash_t {
  51. BlockDriverState *bs;
  52. target_phys_addr_t base;
  53. uint32_t sector_len;
  54. uint32_t chip_len;
  55. int mappings;
  56. int width;
  57. int wcycle; /* if 0, the flash is read normally */
  58. int bypass;
  59. int ro;
  60. uint8_t cmd;
  61. uint8_t status;
  62. uint16_t ident[4];
  63. uint16_t unlock_addr[2];
  64. uint8_t cfi_len;
  65. uint8_t cfi_table[0x52];
  66. QEMUTimer *timer;
  67. ram_addr_t off;
  68. int fl_mem;
  69. int rom_mode;
  70. int read_counter; /* used for lazy switch-back to rom mode */
  71. void *storage;
  72. };
  73. static void pflash_register_memory(pflash_t *pfl, int rom_mode)
  74. {
  75. unsigned long phys_offset = pfl->fl_mem;
  76. int i;
  77. if (rom_mode)
  78. phys_offset |= pfl->off | IO_MEM_ROMD;
  79. pfl->rom_mode = rom_mode;
  80. for (i = 0; i < pfl->mappings; i++)
  81. cpu_register_physical_memory(pfl->base + i * pfl->chip_len,
  82. pfl->chip_len, phys_offset);
  83. }
  84. static void pflash_timer (void *opaque)
  85. {
  86. pflash_t *pfl = opaque;
  87. DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
  88. /* Reset flash */
  89. pfl->status ^= 0x80;
  90. if (pfl->bypass) {
  91. pfl->wcycle = 2;
  92. } else {
  93. pflash_register_memory(pfl, 1);
  94. pfl->wcycle = 0;
  95. }
  96. pfl->cmd = 0;
  97. }
  98. static uint32_t pflash_read (pflash_t *pfl, target_phys_addr_t offset,
  99. int width, int be)
  100. {
  101. target_phys_addr_t boff;
  102. uint32_t ret;
  103. uint8_t *p;
  104. DPRINTF("%s: offset " TARGET_FMT_plx "\n", __func__, offset);
  105. ret = -1;
  106. /* Lazy reset to ROMD mode after a certain amount of read accesses */
  107. if (!pfl->rom_mode && pfl->wcycle == 0 &&
  108. ++pfl->read_counter > PFLASH_LAZY_ROMD_THRESHOLD) {
  109. pflash_register_memory(pfl, 1);
  110. }
  111. offset &= pfl->chip_len - 1;
  112. boff = offset & 0xFF;
  113. if (pfl->width == 2)
  114. boff = boff >> 1;
  115. else if (pfl->width == 4)
  116. boff = boff >> 2;
  117. switch (pfl->cmd) {
  118. default:
  119. /* This should never happen : reset state & treat it as a read*/
  120. DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
  121. pfl->wcycle = 0;
  122. pfl->cmd = 0;
  123. case 0x80:
  124. /* We accept reads during second unlock sequence... */
  125. case 0x00:
  126. flash_read:
  127. /* Flash area read */
  128. p = pfl->storage;
  129. switch (width) {
  130. case 1:
  131. ret = p[offset];
  132. // DPRINTF("%s: data offset %08x %02x\n", __func__, offset, ret);
  133. break;
  134. case 2:
  135. if (be) {
  136. ret = p[offset] << 8;
  137. ret |= p[offset + 1];
  138. } else {
  139. ret = p[offset];
  140. ret |= p[offset + 1] << 8;
  141. }
  142. // DPRINTF("%s: data offset %08x %04x\n", __func__, offset, ret);
  143. break;
  144. case 4:
  145. if (be) {
  146. ret = p[offset] << 24;
  147. ret |= p[offset + 1] << 16;
  148. ret |= p[offset + 2] << 8;
  149. ret |= p[offset + 3];
  150. } else {
  151. ret = p[offset];
  152. ret |= p[offset + 1] << 8;
  153. ret |= p[offset + 2] << 16;
  154. ret |= p[offset + 3] << 24;
  155. }
  156. // DPRINTF("%s: data offset %08x %08x\n", __func__, offset, ret);
  157. break;
  158. }
  159. break;
  160. case 0x90:
  161. /* flash ID read */
  162. switch (boff) {
  163. case 0x00:
  164. case 0x01:
  165. ret = pfl->ident[boff & 0x01];
  166. break;
  167. case 0x02:
  168. ret = 0x00; /* Pretend all sectors are unprotected */
  169. break;
  170. case 0x0E:
  171. case 0x0F:
  172. if (pfl->ident[2 + (boff & 0x01)] == (uint8_t)-1)
  173. goto flash_read;
  174. ret = pfl->ident[2 + (boff & 0x01)];
  175. break;
  176. default:
  177. goto flash_read;
  178. }
  179. DPRINTF("%s: ID " TARGET_FMT_plx " %x\n", __func__, boff, ret);
  180. break;
  181. case 0xA0:
  182. case 0x10:
  183. case 0x30:
  184. /* Status register read */
  185. ret = pfl->status;
  186. DPRINTF("%s: status %x\n", __func__, ret);
  187. /* Toggle bit 6 */
  188. pfl->status ^= 0x40;
  189. break;
  190. case 0x98:
  191. /* CFI query mode */
  192. if (boff > pfl->cfi_len)
  193. ret = 0;
  194. else
  195. ret = pfl->cfi_table[boff];
  196. break;
  197. }
  198. return ret;
  199. }
  200. /* update flash content on disk */
  201. static void pflash_update(pflash_t *pfl, int offset,
  202. int size)
  203. {
  204. int offset_end;
  205. if (pfl->bs) {
  206. offset_end = offset + size;
  207. /* round to sectors */
  208. offset = offset >> 9;
  209. offset_end = (offset_end + 511) >> 9;
  210. bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
  211. offset_end - offset);
  212. }
  213. }
  214. static void pflash_write (pflash_t *pfl, target_phys_addr_t offset,
  215. uint32_t value, int width, int be)
  216. {
  217. target_phys_addr_t boff;
  218. uint8_t *p;
  219. uint8_t cmd;
  220. cmd = value;
  221. if (pfl->cmd != 0xA0 && cmd == 0xF0) {
  222. #if 0
  223. DPRINTF("%s: flash reset asked (%02x %02x)\n",
  224. __func__, pfl->cmd, cmd);
  225. #endif
  226. goto reset_flash;
  227. }
  228. DPRINTF("%s: offset " TARGET_FMT_plx " %08x %d %d\n", __func__,
  229. offset, value, width, pfl->wcycle);
  230. offset &= pfl->chip_len - 1;
  231. DPRINTF("%s: offset " TARGET_FMT_plx " %08x %d\n", __func__,
  232. offset, value, width);
  233. boff = offset & (pfl->sector_len - 1);
  234. if (pfl->width == 2)
  235. boff = boff >> 1;
  236. else if (pfl->width == 4)
  237. boff = boff >> 2;
  238. switch (pfl->wcycle) {
  239. case 0:
  240. /* Set the device in I/O access mode if required */
  241. if (pfl->rom_mode)
  242. pflash_register_memory(pfl, 0);
  243. pfl->read_counter = 0;
  244. /* We're in read mode */
  245. check_unlock0:
  246. if (boff == 0x55 && cmd == 0x98) {
  247. enter_CFI_mode:
  248. /* Enter CFI query mode */
  249. pfl->wcycle = 7;
  250. pfl->cmd = 0x98;
  251. return;
  252. }
  253. if (boff != pfl->unlock_addr[0] || cmd != 0xAA) {
  254. DPRINTF("%s: unlock0 failed " TARGET_FMT_plx " %02x %04x\n",
  255. __func__, boff, cmd, pfl->unlock_addr[0]);
  256. goto reset_flash;
  257. }
  258. DPRINTF("%s: unlock sequence started\n", __func__);
  259. break;
  260. case 1:
  261. /* We started an unlock sequence */
  262. check_unlock1:
  263. if (boff != pfl->unlock_addr[1] || cmd != 0x55) {
  264. DPRINTF("%s: unlock1 failed " TARGET_FMT_plx " %02x\n", __func__,
  265. boff, cmd);
  266. goto reset_flash;
  267. }
  268. DPRINTF("%s: unlock sequence done\n", __func__);
  269. break;
  270. case 2:
  271. /* We finished an unlock sequence */
  272. if (!pfl->bypass && boff != pfl->unlock_addr[0]) {
  273. DPRINTF("%s: command failed " TARGET_FMT_plx " %02x\n", __func__,
  274. boff, cmd);
  275. goto reset_flash;
  276. }
  277. switch (cmd) {
  278. case 0x20:
  279. pfl->bypass = 1;
  280. goto do_bypass;
  281. case 0x80:
  282. case 0x90:
  283. case 0xA0:
  284. pfl->cmd = cmd;
  285. DPRINTF("%s: starting command %02x\n", __func__, cmd);
  286. break;
  287. default:
  288. DPRINTF("%s: unknown command %02x\n", __func__, cmd);
  289. goto reset_flash;
  290. }
  291. break;
  292. case 3:
  293. switch (pfl->cmd) {
  294. case 0x80:
  295. /* We need another unlock sequence */
  296. goto check_unlock0;
  297. case 0xA0:
  298. DPRINTF("%s: write data offset " TARGET_FMT_plx " %08x %d\n",
  299. __func__, offset, value, width);
  300. p = pfl->storage;
  301. switch (width) {
  302. case 1:
  303. p[offset] &= value;
  304. pflash_update(pfl, offset, 1);
  305. break;
  306. case 2:
  307. if (be) {
  308. p[offset] &= value >> 8;
  309. p[offset + 1] &= value;
  310. } else {
  311. p[offset] &= value;
  312. p[offset + 1] &= value >> 8;
  313. }
  314. pflash_update(pfl, offset, 2);
  315. break;
  316. case 4:
  317. if (be) {
  318. p[offset] &= value >> 24;
  319. p[offset + 1] &= value >> 16;
  320. p[offset + 2] &= value >> 8;
  321. p[offset + 3] &= value;
  322. } else {
  323. p[offset] &= value;
  324. p[offset + 1] &= value >> 8;
  325. p[offset + 2] &= value >> 16;
  326. p[offset + 3] &= value >> 24;
  327. }
  328. pflash_update(pfl, offset, 4);
  329. break;
  330. }
  331. pfl->status = 0x00 | ~(value & 0x80);
  332. /* Let's pretend write is immediate */
  333. if (pfl->bypass)
  334. goto do_bypass;
  335. goto reset_flash;
  336. case 0x90:
  337. if (pfl->bypass && cmd == 0x00) {
  338. /* Unlock bypass reset */
  339. goto reset_flash;
  340. }
  341. /* We can enter CFI query mode from autoselect mode */
  342. if (boff == 0x55 && cmd == 0x98)
  343. goto enter_CFI_mode;
  344. /* No break here */
  345. default:
  346. DPRINTF("%s: invalid write for command %02x\n",
  347. __func__, pfl->cmd);
  348. goto reset_flash;
  349. }
  350. case 4:
  351. switch (pfl->cmd) {
  352. case 0xA0:
  353. /* Ignore writes while flash data write is occurring */
  354. /* As we suppose write is immediate, this should never happen */
  355. return;
  356. case 0x80:
  357. goto check_unlock1;
  358. default:
  359. /* Should never happen */
  360. DPRINTF("%s: invalid command state %02x (wc 4)\n",
  361. __func__, pfl->cmd);
  362. goto reset_flash;
  363. }
  364. break;
  365. case 5:
  366. switch (cmd) {
  367. case 0x10:
  368. if (boff != pfl->unlock_addr[0]) {
  369. DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx "\n",
  370. __func__, offset);
  371. goto reset_flash;
  372. }
  373. /* Chip erase */
  374. DPRINTF("%s: start chip erase\n", __func__);
  375. memset(pfl->storage, 0xFF, pfl->chip_len);
  376. pfl->status = 0x00;
  377. pflash_update(pfl, 0, pfl->chip_len);
  378. /* Let's wait 5 seconds before chip erase is done */
  379. qemu_mod_timer(pfl->timer,
  380. qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() * 5));
  381. break;
  382. case 0x30:
  383. /* Sector erase */
  384. p = pfl->storage;
  385. offset &= ~(pfl->sector_len - 1);
  386. DPRINTF("%s: start sector erase at " TARGET_FMT_plx "\n", __func__,
  387. offset);
  388. memset(p + offset, 0xFF, pfl->sector_len);
  389. pflash_update(pfl, offset, pfl->sector_len);
  390. pfl->status = 0x00;
  391. /* Let's wait 1/2 second before sector erase is done */
  392. qemu_mod_timer(pfl->timer,
  393. qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() / 2));
  394. break;
  395. default:
  396. DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd);
  397. goto reset_flash;
  398. }
  399. pfl->cmd = cmd;
  400. break;
  401. case 6:
  402. switch (pfl->cmd) {
  403. case 0x10:
  404. /* Ignore writes during chip erase */
  405. return;
  406. case 0x30:
  407. /* Ignore writes during sector erase */
  408. return;
  409. default:
  410. /* Should never happen */
  411. DPRINTF("%s: invalid command state %02x (wc 6)\n",
  412. __func__, pfl->cmd);
  413. goto reset_flash;
  414. }
  415. break;
  416. case 7: /* Special value for CFI queries */
  417. DPRINTF("%s: invalid write in CFI query mode\n", __func__);
  418. goto reset_flash;
  419. default:
  420. /* Should never happen */
  421. DPRINTF("%s: invalid write state (wc 7)\n", __func__);
  422. goto reset_flash;
  423. }
  424. pfl->wcycle++;
  425. return;
  426. /* Reset flash */
  427. reset_flash:
  428. pfl->bypass = 0;
  429. pfl->wcycle = 0;
  430. pfl->cmd = 0;
  431. return;
  432. do_bypass:
  433. pfl->wcycle = 2;
  434. pfl->cmd = 0;
  435. return;
  436. }
  437. static uint32_t pflash_readb_be(void *opaque, target_phys_addr_t addr)
  438. {
  439. return pflash_read(opaque, addr, 1, 1);
  440. }
  441. static uint32_t pflash_readb_le(void *opaque, target_phys_addr_t addr)
  442. {
  443. return pflash_read(opaque, addr, 1, 0);
  444. }
  445. static uint32_t pflash_readw_be(void *opaque, target_phys_addr_t addr)
  446. {
  447. pflash_t *pfl = opaque;
  448. return pflash_read(pfl, addr, 2, 1);
  449. }
  450. static uint32_t pflash_readw_le(void *opaque, target_phys_addr_t addr)
  451. {
  452. pflash_t *pfl = opaque;
  453. return pflash_read(pfl, addr, 2, 0);
  454. }
  455. static uint32_t pflash_readl_be(void *opaque, target_phys_addr_t addr)
  456. {
  457. pflash_t *pfl = opaque;
  458. return pflash_read(pfl, addr, 4, 1);
  459. }
  460. static uint32_t pflash_readl_le(void *opaque, target_phys_addr_t addr)
  461. {
  462. pflash_t *pfl = opaque;
  463. return pflash_read(pfl, addr, 4, 0);
  464. }
  465. static void pflash_writeb_be(void *opaque, target_phys_addr_t addr,
  466. uint32_t value)
  467. {
  468. pflash_write(opaque, addr, value, 1, 1);
  469. }
  470. static void pflash_writeb_le(void *opaque, target_phys_addr_t addr,
  471. uint32_t value)
  472. {
  473. pflash_write(opaque, addr, value, 1, 0);
  474. }
  475. static void pflash_writew_be(void *opaque, target_phys_addr_t addr,
  476. uint32_t value)
  477. {
  478. pflash_t *pfl = opaque;
  479. pflash_write(pfl, addr, value, 2, 1);
  480. }
  481. static void pflash_writew_le(void *opaque, target_phys_addr_t addr,
  482. uint32_t value)
  483. {
  484. pflash_t *pfl = opaque;
  485. pflash_write(pfl, addr, value, 2, 0);
  486. }
  487. static void pflash_writel_be(void *opaque, target_phys_addr_t addr,
  488. uint32_t value)
  489. {
  490. pflash_t *pfl = opaque;
  491. pflash_write(pfl, addr, value, 4, 1);
  492. }
  493. static void pflash_writel_le(void *opaque, target_phys_addr_t addr,
  494. uint32_t value)
  495. {
  496. pflash_t *pfl = opaque;
  497. pflash_write(pfl, addr, value, 4, 0);
  498. }
  499. static CPUWriteMemoryFunc * const pflash_write_ops_be[] = {
  500. &pflash_writeb_be,
  501. &pflash_writew_be,
  502. &pflash_writel_be,
  503. };
  504. static CPUReadMemoryFunc * const pflash_read_ops_be[] = {
  505. &pflash_readb_be,
  506. &pflash_readw_be,
  507. &pflash_readl_be,
  508. };
  509. static CPUWriteMemoryFunc * const pflash_write_ops_le[] = {
  510. &pflash_writeb_le,
  511. &pflash_writew_le,
  512. &pflash_writel_le,
  513. };
  514. static CPUReadMemoryFunc * const pflash_read_ops_le[] = {
  515. &pflash_readb_le,
  516. &pflash_readw_le,
  517. &pflash_readl_le,
  518. };
  519. /* Count trailing zeroes of a 32 bits quantity */
  520. static int ctz32 (uint32_t n)
  521. {
  522. int ret;
  523. ret = 0;
  524. if (!(n & 0xFFFF)) {
  525. ret += 16;
  526. n = n >> 16;
  527. }
  528. if (!(n & 0xFF)) {
  529. ret += 8;
  530. n = n >> 8;
  531. }
  532. if (!(n & 0xF)) {
  533. ret += 4;
  534. n = n >> 4;
  535. }
  536. if (!(n & 0x3)) {
  537. ret += 2;
  538. n = n >> 2;
  539. }
  540. if (!(n & 0x1)) {
  541. ret++;
  542. #if 0 /* This is not necessary as n is never 0 */
  543. n = n >> 1;
  544. #endif
  545. }
  546. #if 0 /* This is not necessary as n is never 0 */
  547. if (!n)
  548. ret++;
  549. #endif
  550. return ret;
  551. }
  552. pflash_t *pflash_cfi02_register(target_phys_addr_t base, ram_addr_t off,
  553. BlockDriverState *bs, uint32_t sector_len,
  554. int nb_blocs, int nb_mappings, int width,
  555. uint16_t id0, uint16_t id1,
  556. uint16_t id2, uint16_t id3,
  557. uint16_t unlock_addr0, uint16_t unlock_addr1,
  558. int be)
  559. {
  560. pflash_t *pfl;
  561. int32_t chip_len;
  562. int ret;
  563. chip_len = sector_len * nb_blocs;
  564. /* XXX: to be fixed */
  565. #if 0
  566. if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
  567. total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
  568. return NULL;
  569. #endif
  570. pfl = qemu_mallocz(sizeof(pflash_t));
  571. /* FIXME: Allocate ram ourselves. */
  572. pfl->storage = qemu_get_ram_ptr(off);
  573. if (be) {
  574. pfl->fl_mem = cpu_register_io_memory(pflash_read_ops_be,
  575. pflash_write_ops_be,
  576. pfl, DEVICE_NATIVE_ENDIAN);
  577. } else {
  578. pfl->fl_mem = cpu_register_io_memory(pflash_read_ops_le,
  579. pflash_write_ops_le,
  580. pfl, DEVICE_NATIVE_ENDIAN);
  581. }
  582. pfl->off = off;
  583. pfl->base = base;
  584. pfl->chip_len = chip_len;
  585. pfl->mappings = nb_mappings;
  586. pflash_register_memory(pfl, 1);
  587. pfl->bs = bs;
  588. if (pfl->bs) {
  589. /* read the initial flash content */
  590. ret = bdrv_read(pfl->bs, 0, pfl->storage, chip_len >> 9);
  591. if (ret < 0) {
  592. cpu_unregister_io_memory(pfl->fl_mem);
  593. qemu_free(pfl);
  594. return NULL;
  595. }
  596. }
  597. #if 0 /* XXX: there should be a bit to set up read-only,
  598. * the same way the hardware does (with WP pin).
  599. */
  600. pfl->ro = 1;
  601. #else
  602. pfl->ro = 0;
  603. #endif
  604. pfl->timer = qemu_new_timer_ns(vm_clock, pflash_timer, pfl);
  605. pfl->sector_len = sector_len;
  606. pfl->width = width;
  607. pfl->wcycle = 0;
  608. pfl->cmd = 0;
  609. pfl->status = 0;
  610. pfl->ident[0] = id0;
  611. pfl->ident[1] = id1;
  612. pfl->ident[2] = id2;
  613. pfl->ident[3] = id3;
  614. pfl->unlock_addr[0] = unlock_addr0;
  615. pfl->unlock_addr[1] = unlock_addr1;
  616. /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
  617. pfl->cfi_len = 0x52;
  618. /* Standard "QRY" string */
  619. pfl->cfi_table[0x10] = 'Q';
  620. pfl->cfi_table[0x11] = 'R';
  621. pfl->cfi_table[0x12] = 'Y';
  622. /* Command set (AMD/Fujitsu) */
  623. pfl->cfi_table[0x13] = 0x02;
  624. pfl->cfi_table[0x14] = 0x00;
  625. /* Primary extended table address */
  626. pfl->cfi_table[0x15] = 0x31;
  627. pfl->cfi_table[0x16] = 0x00;
  628. /* Alternate command set (none) */
  629. pfl->cfi_table[0x17] = 0x00;
  630. pfl->cfi_table[0x18] = 0x00;
  631. /* Alternate extended table (none) */
  632. pfl->cfi_table[0x19] = 0x00;
  633. pfl->cfi_table[0x1A] = 0x00;
  634. /* Vcc min */
  635. pfl->cfi_table[0x1B] = 0x27;
  636. /* Vcc max */
  637. pfl->cfi_table[0x1C] = 0x36;
  638. /* Vpp min (no Vpp pin) */
  639. pfl->cfi_table[0x1D] = 0x00;
  640. /* Vpp max (no Vpp pin) */
  641. pfl->cfi_table[0x1E] = 0x00;
  642. /* Reserved */
  643. pfl->cfi_table[0x1F] = 0x07;
  644. /* Timeout for min size buffer write (NA) */
  645. pfl->cfi_table[0x20] = 0x00;
  646. /* Typical timeout for block erase (512 ms) */
  647. pfl->cfi_table[0x21] = 0x09;
  648. /* Typical timeout for full chip erase (4096 ms) */
  649. pfl->cfi_table[0x22] = 0x0C;
  650. /* Reserved */
  651. pfl->cfi_table[0x23] = 0x01;
  652. /* Max timeout for buffer write (NA) */
  653. pfl->cfi_table[0x24] = 0x00;
  654. /* Max timeout for block erase */
  655. pfl->cfi_table[0x25] = 0x0A;
  656. /* Max timeout for chip erase */
  657. pfl->cfi_table[0x26] = 0x0D;
  658. /* Device size */
  659. pfl->cfi_table[0x27] = ctz32(chip_len);
  660. /* Flash device interface (8 & 16 bits) */
  661. pfl->cfi_table[0x28] = 0x02;
  662. pfl->cfi_table[0x29] = 0x00;
  663. /* Max number of bytes in multi-bytes write */
  664. /* XXX: disable buffered write as it's not supported */
  665. // pfl->cfi_table[0x2A] = 0x05;
  666. pfl->cfi_table[0x2A] = 0x00;
  667. pfl->cfi_table[0x2B] = 0x00;
  668. /* Number of erase block regions (uniform) */
  669. pfl->cfi_table[0x2C] = 0x01;
  670. /* Erase block region 1 */
  671. pfl->cfi_table[0x2D] = nb_blocs - 1;
  672. pfl->cfi_table[0x2E] = (nb_blocs - 1) >> 8;
  673. pfl->cfi_table[0x2F] = sector_len >> 8;
  674. pfl->cfi_table[0x30] = sector_len >> 16;
  675. /* Extended */
  676. pfl->cfi_table[0x31] = 'P';
  677. pfl->cfi_table[0x32] = 'R';
  678. pfl->cfi_table[0x33] = 'I';
  679. pfl->cfi_table[0x34] = '1';
  680. pfl->cfi_table[0x35] = '0';
  681. pfl->cfi_table[0x36] = 0x00;
  682. pfl->cfi_table[0x37] = 0x00;
  683. pfl->cfi_table[0x38] = 0x00;
  684. pfl->cfi_table[0x39] = 0x00;
  685. pfl->cfi_table[0x3a] = 0x00;
  686. pfl->cfi_table[0x3b] = 0x00;
  687. pfl->cfi_table[0x3c] = 0x00;
  688. return pfl;
  689. }