pflash_cfi02.c 22 KB

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