pflash_cfi01.c 22 KB

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