pflash_cfi01.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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.1 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 "qemu/osdep.h"
  38. #include "hw/block/block.h"
  39. #include "hw/block/flash.h"
  40. #include "hw/qdev-properties.h"
  41. #include "hw/qdev-properties-system.h"
  42. #include "sysemu/block-backend.h"
  43. #include "qapi/error.h"
  44. #include "qemu/error-report.h"
  45. #include "qemu/bitops.h"
  46. #include "qemu/host-utils.h"
  47. #include "qemu/log.h"
  48. #include "qemu/option.h"
  49. #include "hw/sysbus.h"
  50. #include "migration/vmstate.h"
  51. #include "sysemu/blockdev.h"
  52. #include "sysemu/runstate.h"
  53. #include "trace.h"
  54. #define PFLASH_BE 0
  55. #define PFLASH_SECURE 1
  56. struct PFlashCFI01 {
  57. /*< private >*/
  58. SysBusDevice parent_obj;
  59. /*< public >*/
  60. BlockBackend *blk;
  61. uint32_t nb_blocs;
  62. uint64_t sector_len;
  63. uint8_t bank_width;
  64. uint8_t device_width; /* If 0, device width not specified. */
  65. uint8_t max_device_width; /* max device width in bytes */
  66. uint32_t features;
  67. uint8_t wcycle; /* if 0, the flash is read normally */
  68. bool ro;
  69. uint8_t cmd;
  70. uint8_t status;
  71. uint16_t ident0;
  72. uint16_t ident1;
  73. uint16_t ident2;
  74. uint16_t ident3;
  75. uint8_t cfi_table[0x52];
  76. uint64_t counter;
  77. uint32_t writeblock_size;
  78. MemoryRegion mem;
  79. char *name;
  80. void *storage;
  81. VMChangeStateEntry *vmstate;
  82. bool old_multiple_chip_handling;
  83. /* block update buffer */
  84. unsigned char *blk_bytes;
  85. uint32_t blk_offset;
  86. };
  87. static int pflash_post_load(void *opaque, int version_id);
  88. static bool pflash_blk_write_state_needed(void *opaque)
  89. {
  90. PFlashCFI01 *pfl = opaque;
  91. return (pfl->blk_offset != -1);
  92. }
  93. static const VMStateDescription vmstate_pflash_blk_write = {
  94. .name = "pflash_cfi01_blk_write",
  95. .version_id = 1,
  96. .minimum_version_id = 1,
  97. .needed = pflash_blk_write_state_needed,
  98. .fields = (const VMStateField[]) {
  99. VMSTATE_VBUFFER_UINT32(blk_bytes, PFlashCFI01, 0, NULL, writeblock_size),
  100. VMSTATE_UINT32(blk_offset, PFlashCFI01),
  101. VMSTATE_END_OF_LIST()
  102. }
  103. };
  104. static const VMStateDescription vmstate_pflash = {
  105. .name = "pflash_cfi01",
  106. .version_id = 1,
  107. .minimum_version_id = 1,
  108. .post_load = pflash_post_load,
  109. .fields = (const VMStateField[]) {
  110. VMSTATE_UINT8(wcycle, PFlashCFI01),
  111. VMSTATE_UINT8(cmd, PFlashCFI01),
  112. VMSTATE_UINT8(status, PFlashCFI01),
  113. VMSTATE_UINT64(counter, PFlashCFI01),
  114. VMSTATE_END_OF_LIST()
  115. },
  116. .subsections = (const VMStateDescription * const []) {
  117. &vmstate_pflash_blk_write,
  118. NULL
  119. }
  120. };
  121. /*
  122. * Perform a CFI query based on the bank width of the flash.
  123. * If this code is called we know we have a device_width set for
  124. * this flash.
  125. */
  126. static uint32_t pflash_cfi_query(PFlashCFI01 *pfl, hwaddr offset)
  127. {
  128. int i;
  129. uint32_t resp = 0;
  130. hwaddr boff;
  131. /*
  132. * Adjust incoming offset to match expected device-width
  133. * addressing. CFI query addresses are always specified in terms of
  134. * the maximum supported width of the device. This means that x8
  135. * devices and x8/x16 devices in x8 mode behave differently. For
  136. * devices that are not used at their max width, we will be
  137. * provided with addresses that use higher address bits than
  138. * expected (based on the max width), so we will shift them lower
  139. * so that they will match the addresses used when
  140. * device_width==max_device_width.
  141. */
  142. boff = offset >> (ctz32(pfl->bank_width) +
  143. ctz32(pfl->max_device_width) - ctz32(pfl->device_width));
  144. if (boff >= sizeof(pfl->cfi_table)) {
  145. return 0;
  146. }
  147. /*
  148. * Now we will construct the CFI response generated by a single
  149. * device, then replicate that for all devices that make up the
  150. * bus. For wide parts used in x8 mode, CFI query responses
  151. * are different than native byte-wide parts.
  152. */
  153. resp = pfl->cfi_table[boff];
  154. if (pfl->device_width != pfl->max_device_width) {
  155. /* The only case currently supported is x8 mode for a
  156. * wider part.
  157. */
  158. if (pfl->device_width != 1 || pfl->bank_width > 4) {
  159. trace_pflash_unsupported_device_configuration(pfl->name,
  160. pfl->device_width, pfl->max_device_width);
  161. return 0;
  162. }
  163. /* CFI query data is repeated, rather than zero padded for
  164. * wide devices used in x8 mode.
  165. */
  166. for (i = 1; i < pfl->max_device_width; i++) {
  167. resp = deposit32(resp, 8 * i, 8, pfl->cfi_table[boff]);
  168. }
  169. }
  170. /* Replicate responses for each device in bank. */
  171. if (pfl->device_width < pfl->bank_width) {
  172. for (i = pfl->device_width;
  173. i < pfl->bank_width; i += pfl->device_width) {
  174. resp = deposit32(resp, 8 * i, 8 * pfl->device_width, resp);
  175. }
  176. }
  177. return resp;
  178. }
  179. /* Perform a device id query based on the bank width of the flash. */
  180. static uint32_t pflash_devid_query(PFlashCFI01 *pfl, hwaddr offset)
  181. {
  182. int i;
  183. uint32_t resp;
  184. hwaddr boff;
  185. /*
  186. * Adjust incoming offset to match expected device-width
  187. * addressing. Device ID read addresses are always specified in
  188. * terms of the maximum supported width of the device. This means
  189. * that x8 devices and x8/x16 devices in x8 mode behave
  190. * differently. For devices that are not used at their max width,
  191. * we will be provided with addresses that use higher address bits
  192. * than expected (based on the max width), so we will shift them
  193. * lower so that they will match the addresses used when
  194. * device_width==max_device_width.
  195. */
  196. boff = offset >> (ctz32(pfl->bank_width) +
  197. ctz32(pfl->max_device_width) - ctz32(pfl->device_width));
  198. /*
  199. * Mask off upper bits which may be used in to query block
  200. * or sector lock status at other addresses.
  201. * Offsets 2/3 are block lock status, is not emulated.
  202. */
  203. switch (boff & 0xFF) {
  204. case 0:
  205. resp = pfl->ident0;
  206. trace_pflash_manufacturer_id(pfl->name, resp);
  207. break;
  208. case 1:
  209. resp = pfl->ident1;
  210. trace_pflash_device_id(pfl->name, resp);
  211. break;
  212. default:
  213. trace_pflash_device_info(pfl->name, offset);
  214. return 0;
  215. }
  216. /* Replicate responses for each device in bank. */
  217. if (pfl->device_width < pfl->bank_width) {
  218. for (i = pfl->device_width;
  219. i < pfl->bank_width; i += pfl->device_width) {
  220. resp = deposit32(resp, 8 * i, 8 * pfl->device_width, resp);
  221. }
  222. }
  223. return resp;
  224. }
  225. static uint32_t pflash_data_read(PFlashCFI01 *pfl, hwaddr offset,
  226. int width, int be)
  227. {
  228. uint8_t *p;
  229. uint32_t ret;
  230. p = pfl->storage;
  231. if (be) {
  232. ret = ldn_be_p(p + offset, width);
  233. } else {
  234. ret = ldn_le_p(p + offset, width);
  235. }
  236. trace_pflash_data_read(pfl->name, offset, width, ret);
  237. return ret;
  238. }
  239. static uint32_t pflash_read(PFlashCFI01 *pfl, hwaddr offset,
  240. int width, int be)
  241. {
  242. hwaddr boff;
  243. uint32_t ret;
  244. ret = -1;
  245. switch (pfl->cmd) {
  246. default:
  247. /* This should never happen : reset state & treat it as a read */
  248. trace_pflash_read_unknown_state(pfl->name, pfl->cmd);
  249. pfl->wcycle = 0;
  250. /*
  251. * The command 0x00 is not assigned by the CFI open standard,
  252. * but QEMU historically uses it for the READ_ARRAY command (0xff).
  253. */
  254. pfl->cmd = 0x00;
  255. /* fall through to read code */
  256. case 0x00: /* This model reset value for READ_ARRAY (not CFI compliant) */
  257. /* Flash area read */
  258. ret = pflash_data_read(pfl, offset, width, be);
  259. break;
  260. case 0x10: /* Single byte program */
  261. case 0x20: /* Block erase */
  262. case 0x28: /* Block erase */
  263. case 0x40: /* single byte program */
  264. case 0x50: /* Clear status register */
  265. case 0x60: /* Block /un)lock */
  266. case 0x70: /* Status Register */
  267. case 0xe8: /* Write block */
  268. /*
  269. * Status register read. Return status from each device in
  270. * bank.
  271. */
  272. ret = pfl->status;
  273. if (pfl->device_width && width > pfl->device_width) {
  274. int shift = pfl->device_width * 8;
  275. while (shift + pfl->device_width * 8 <= width * 8) {
  276. ret |= pfl->status << shift;
  277. shift += pfl->device_width * 8;
  278. }
  279. } else if (!pfl->device_width && width > 2) {
  280. /*
  281. * Handle 32 bit flash cases where device width is not
  282. * set. (Existing behavior before device width added.)
  283. */
  284. ret |= pfl->status << 16;
  285. }
  286. trace_pflash_read_status(pfl->name, ret);
  287. break;
  288. case 0x90:
  289. if (!pfl->device_width) {
  290. /* Preserve old behavior if device width not specified */
  291. boff = offset & 0xFF;
  292. if (pfl->bank_width == 2) {
  293. boff = boff >> 1;
  294. } else if (pfl->bank_width == 4) {
  295. boff = boff >> 2;
  296. }
  297. switch (boff) {
  298. case 0:
  299. ret = pfl->ident0 << 8 | pfl->ident1;
  300. trace_pflash_manufacturer_id(pfl->name, ret);
  301. break;
  302. case 1:
  303. ret = pfl->ident2 << 8 | pfl->ident3;
  304. trace_pflash_device_id(pfl->name, ret);
  305. break;
  306. default:
  307. trace_pflash_device_info(pfl->name, boff);
  308. ret = 0;
  309. break;
  310. }
  311. } else {
  312. /*
  313. * If we have a read larger than the bank_width, combine multiple
  314. * manufacturer/device ID queries into a single response.
  315. */
  316. int i;
  317. for (i = 0; i < width; i += pfl->bank_width) {
  318. ret = deposit32(ret, i * 8, pfl->bank_width * 8,
  319. pflash_devid_query(pfl,
  320. offset + i * pfl->bank_width));
  321. }
  322. }
  323. break;
  324. case 0x98: /* Query mode */
  325. if (!pfl->device_width) {
  326. /* Preserve old behavior if device width not specified */
  327. boff = offset & 0xFF;
  328. if (pfl->bank_width == 2) {
  329. boff = boff >> 1;
  330. } else if (pfl->bank_width == 4) {
  331. boff = boff >> 2;
  332. }
  333. if (boff < sizeof(pfl->cfi_table)) {
  334. ret = pfl->cfi_table[boff];
  335. } else {
  336. ret = 0;
  337. }
  338. } else {
  339. /*
  340. * If we have a read larger than the bank_width, combine multiple
  341. * CFI queries into a single response.
  342. */
  343. int i;
  344. for (i = 0; i < width; i += pfl->bank_width) {
  345. ret = deposit32(ret, i * 8, pfl->bank_width * 8,
  346. pflash_cfi_query(pfl,
  347. offset + i * pfl->bank_width));
  348. }
  349. }
  350. break;
  351. }
  352. trace_pflash_io_read(pfl->name, offset, width, ret, pfl->cmd, pfl->wcycle);
  353. return ret;
  354. }
  355. /* update flash content on disk */
  356. static void pflash_update(PFlashCFI01 *pfl, int offset,
  357. int size)
  358. {
  359. int offset_end;
  360. int ret;
  361. if (pfl->blk) {
  362. offset_end = offset + size;
  363. /* widen to sector boundaries */
  364. offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
  365. offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
  366. ret = blk_pwrite(pfl->blk, offset, offset_end - offset,
  367. pfl->storage + offset, 0);
  368. if (ret < 0) {
  369. /* TODO set error bit in status */
  370. error_report("Could not update PFLASH: %s", strerror(-ret));
  371. }
  372. }
  373. }
  374. /* copy current flash content to block update buffer */
  375. static void pflash_blk_write_start(PFlashCFI01 *pfl, hwaddr offset)
  376. {
  377. hwaddr mask = ~(pfl->writeblock_size - 1);
  378. trace_pflash_write_block_start(pfl->name, pfl->counter);
  379. pfl->blk_offset = offset & mask;
  380. memcpy(pfl->blk_bytes, pfl->storage + pfl->blk_offset,
  381. pfl->writeblock_size);
  382. }
  383. /* commit block update buffer changes */
  384. static void pflash_blk_write_flush(PFlashCFI01 *pfl)
  385. {
  386. g_assert(pfl->blk_offset != -1);
  387. trace_pflash_write_block_flush(pfl->name);
  388. memcpy(pfl->storage + pfl->blk_offset, pfl->blk_bytes,
  389. pfl->writeblock_size);
  390. pflash_update(pfl, pfl->blk_offset, pfl->writeblock_size);
  391. pfl->blk_offset = -1;
  392. }
  393. /* discard block update buffer changes */
  394. static void pflash_blk_write_abort(PFlashCFI01 *pfl)
  395. {
  396. trace_pflash_write_block_abort(pfl->name);
  397. pfl->blk_offset = -1;
  398. }
  399. static inline void pflash_data_write(PFlashCFI01 *pfl, hwaddr offset,
  400. uint32_t value, int width, int be)
  401. {
  402. uint8_t *p;
  403. if (pfl->blk_offset != -1) {
  404. /* block write: redirect writes to block update buffer */
  405. if ((offset < pfl->blk_offset) ||
  406. (offset + width > pfl->blk_offset + pfl->writeblock_size)) {
  407. pfl->status |= 0x10; /* Programming error */
  408. return;
  409. }
  410. trace_pflash_data_write_block(pfl->name, offset, width, value,
  411. pfl->counter);
  412. p = pfl->blk_bytes + (offset - pfl->blk_offset);
  413. } else {
  414. /* write directly to storage */
  415. trace_pflash_data_write(pfl->name, offset, width, value);
  416. p = pfl->storage + offset;
  417. }
  418. if (be) {
  419. stn_be_p(p, width, value);
  420. } else {
  421. stn_le_p(p, width, value);
  422. }
  423. }
  424. static void pflash_write(PFlashCFI01 *pfl, hwaddr offset,
  425. uint32_t value, int width, int be)
  426. {
  427. uint8_t *p;
  428. uint8_t cmd;
  429. cmd = value;
  430. trace_pflash_io_write(pfl->name, offset, width, value, pfl->wcycle);
  431. if (!pfl->wcycle) {
  432. /* Set the device in I/O access mode */
  433. memory_region_rom_device_set_romd(&pfl->mem, false);
  434. }
  435. switch (pfl->wcycle) {
  436. case 0:
  437. /* read mode */
  438. switch (cmd) {
  439. case 0x00: /* This model reset value for READ_ARRAY (not CFI) */
  440. goto mode_read_array;
  441. case 0x10: /* Single Byte Program */
  442. case 0x40: /* Single Byte Program */
  443. trace_pflash_write(pfl->name, "single byte program (0)");
  444. break;
  445. case 0x20: /* Block erase */
  446. p = pfl->storage;
  447. offset &= ~(pfl->sector_len - 1);
  448. trace_pflash_write_block_erase(pfl->name, offset, pfl->sector_len);
  449. if (!pfl->ro) {
  450. memset(p + offset, 0xff, pfl->sector_len);
  451. pflash_update(pfl, offset, pfl->sector_len);
  452. } else {
  453. pfl->status |= 0x20; /* Block erase error */
  454. }
  455. pfl->status |= 0x80; /* Ready! */
  456. break;
  457. case 0x50: /* Clear status bits */
  458. trace_pflash_write(pfl->name, "clear status bits");
  459. pfl->status = 0x0;
  460. goto mode_read_array;
  461. case 0x60: /* Block (un)lock */
  462. trace_pflash_write(pfl->name, "block unlock");
  463. break;
  464. case 0x70: /* Status Register */
  465. trace_pflash_write(pfl->name, "read status register");
  466. pfl->cmd = cmd;
  467. return;
  468. case 0x90: /* Read Device ID */
  469. trace_pflash_write(pfl->name, "read device information");
  470. pfl->cmd = cmd;
  471. return;
  472. case 0x98: /* CFI query */
  473. trace_pflash_write(pfl->name, "CFI query");
  474. break;
  475. case 0xe8: /* Write to buffer */
  476. trace_pflash_write(pfl->name, "write to buffer");
  477. pfl->status |= 0x80; /* Ready! */
  478. break;
  479. case 0xf0: /* Probe for AMD flash */
  480. trace_pflash_write(pfl->name, "probe for AMD flash");
  481. goto mode_read_array;
  482. case 0xff: /* Read Array */
  483. trace_pflash_write(pfl->name, "read array mode");
  484. goto mode_read_array;
  485. default:
  486. goto error_flash;
  487. }
  488. pfl->wcycle++;
  489. pfl->cmd = cmd;
  490. break;
  491. case 1:
  492. switch (pfl->cmd) {
  493. case 0x10: /* Single Byte Program */
  494. case 0x40: /* Single Byte Program */
  495. trace_pflash_write(pfl->name, "single byte program (1)");
  496. if (!pfl->ro) {
  497. pflash_data_write(pfl, offset, value, width, be);
  498. pflash_update(pfl, offset, width);
  499. } else {
  500. pfl->status |= 0x10; /* Programming error */
  501. }
  502. pfl->status |= 0x80; /* Ready! */
  503. pfl->wcycle = 0;
  504. break;
  505. case 0x20: /* Block erase */
  506. case 0x28:
  507. if (cmd == 0xd0) { /* confirm */
  508. pfl->wcycle = 0;
  509. pfl->status |= 0x80;
  510. } else if (cmd == 0xff) { /* Read Array */
  511. goto mode_read_array;
  512. } else
  513. goto error_flash;
  514. break;
  515. case 0xe8:
  516. /*
  517. * Mask writeblock size based on device width, or bank width if
  518. * device width not specified.
  519. */
  520. /* FIXME check @offset, @width */
  521. if (pfl->device_width) {
  522. value = extract32(value, 0, pfl->device_width * 8);
  523. } else {
  524. value = extract32(value, 0, pfl->bank_width * 8);
  525. }
  526. pfl->counter = value;
  527. pfl->wcycle++;
  528. break;
  529. case 0x60:
  530. if (cmd == 0xd0) {
  531. pfl->wcycle = 0;
  532. pfl->status |= 0x80;
  533. } else if (cmd == 0x01) {
  534. pfl->wcycle = 0;
  535. pfl->status |= 0x80;
  536. } else if (cmd == 0xff) { /* Read Array */
  537. goto mode_read_array;
  538. } else {
  539. trace_pflash_write(pfl->name, "unknown (un)locking command");
  540. goto mode_read_array;
  541. }
  542. break;
  543. case 0x98:
  544. if (cmd == 0xff) { /* Read Array */
  545. goto mode_read_array;
  546. } else {
  547. trace_pflash_write(pfl->name, "leaving query mode");
  548. }
  549. break;
  550. default:
  551. goto error_flash;
  552. }
  553. break;
  554. case 2:
  555. switch (pfl->cmd) {
  556. case 0xe8: /* Block write */
  557. /* FIXME check @offset, @width */
  558. if (pfl->blk_offset == -1 && pfl->counter) {
  559. pflash_blk_write_start(pfl, offset);
  560. }
  561. if (!pfl->ro && (pfl->blk_offset != -1)) {
  562. pflash_data_write(pfl, offset, value, width, be);
  563. } else {
  564. pfl->status |= 0x10; /* Programming error */
  565. }
  566. pfl->status |= 0x80;
  567. if (!pfl->counter) {
  568. trace_pflash_write(pfl->name, "block write finished");
  569. pfl->wcycle++;
  570. break;
  571. }
  572. pfl->counter--;
  573. break;
  574. default:
  575. goto error_flash;
  576. }
  577. break;
  578. case 3: /* Confirm mode */
  579. switch (pfl->cmd) {
  580. case 0xe8: /* Block write */
  581. if ((cmd == 0xd0) && !(pfl->status & 0x10)) {
  582. pflash_blk_write_flush(pfl);
  583. pfl->wcycle = 0;
  584. pfl->status |= 0x80;
  585. } else {
  586. pflash_blk_write_abort(pfl);
  587. goto mode_read_array;
  588. }
  589. break;
  590. default:
  591. pflash_blk_write_abort(pfl);
  592. goto error_flash;
  593. }
  594. break;
  595. default:
  596. /* Should never happen */
  597. trace_pflash_write(pfl->name, "invalid write state");
  598. goto mode_read_array;
  599. }
  600. return;
  601. error_flash:
  602. qemu_log_mask(LOG_UNIMP, "%s: Unimplemented flash cmd sequence "
  603. "(offset " HWADDR_FMT_plx ", wcycle 0x%x cmd 0x%x value 0x%x)"
  604. "\n", __func__, offset, pfl->wcycle, pfl->cmd, value);
  605. mode_read_array:
  606. trace_pflash_mode_read_array(pfl->name);
  607. memory_region_rom_device_set_romd(&pfl->mem, true);
  608. pfl->wcycle = 0;
  609. pfl->cmd = 0x00; /* This model reset value for READ_ARRAY (not CFI) */
  610. }
  611. static MemTxResult pflash_mem_read_with_attrs(void *opaque, hwaddr addr, uint64_t *value,
  612. unsigned len, MemTxAttrs attrs)
  613. {
  614. PFlashCFI01 *pfl = opaque;
  615. bool be = !!(pfl->features & (1 << PFLASH_BE));
  616. if ((pfl->features & (1 << PFLASH_SECURE)) && !attrs.secure) {
  617. *value = pflash_data_read(opaque, addr, len, be);
  618. } else {
  619. *value = pflash_read(opaque, addr, len, be);
  620. }
  621. return MEMTX_OK;
  622. }
  623. static MemTxResult pflash_mem_write_with_attrs(void *opaque, hwaddr addr, uint64_t value,
  624. unsigned len, MemTxAttrs attrs)
  625. {
  626. PFlashCFI01 *pfl = opaque;
  627. bool be = !!(pfl->features & (1 << PFLASH_BE));
  628. if ((pfl->features & (1 << PFLASH_SECURE)) && !attrs.secure) {
  629. return MEMTX_ERROR;
  630. } else {
  631. pflash_write(opaque, addr, value, len, be);
  632. return MEMTX_OK;
  633. }
  634. }
  635. static const MemoryRegionOps pflash_cfi01_ops = {
  636. .read_with_attrs = pflash_mem_read_with_attrs,
  637. .write_with_attrs = pflash_mem_write_with_attrs,
  638. .endianness = DEVICE_NATIVE_ENDIAN,
  639. };
  640. static void pflash_cfi01_fill_cfi_table(PFlashCFI01 *pfl)
  641. {
  642. uint64_t blocks_per_device, sector_len_per_device, device_len;
  643. int num_devices;
  644. /*
  645. * These are only used to expose the parameters of each device
  646. * in the cfi_table[].
  647. */
  648. num_devices = pfl->device_width ? (pfl->bank_width / pfl->device_width) : 1;
  649. if (pfl->old_multiple_chip_handling) {
  650. blocks_per_device = pfl->nb_blocs / num_devices;
  651. sector_len_per_device = pfl->sector_len;
  652. } else {
  653. blocks_per_device = pfl->nb_blocs;
  654. sector_len_per_device = pfl->sector_len / num_devices;
  655. }
  656. device_len = sector_len_per_device * blocks_per_device;
  657. /* Hardcoded CFI table */
  658. /* Standard "QRY" string */
  659. pfl->cfi_table[0x10] = 'Q';
  660. pfl->cfi_table[0x11] = 'R';
  661. pfl->cfi_table[0x12] = 'Y';
  662. /* Command set (Intel) */
  663. pfl->cfi_table[0x13] = 0x01;
  664. pfl->cfi_table[0x14] = 0x00;
  665. /* Primary extended table address (none) */
  666. pfl->cfi_table[0x15] = 0x31;
  667. pfl->cfi_table[0x16] = 0x00;
  668. /* Alternate command set (none) */
  669. pfl->cfi_table[0x17] = 0x00;
  670. pfl->cfi_table[0x18] = 0x00;
  671. /* Alternate extended table (none) */
  672. pfl->cfi_table[0x19] = 0x00;
  673. pfl->cfi_table[0x1A] = 0x00;
  674. /* Vcc min */
  675. pfl->cfi_table[0x1B] = 0x45;
  676. /* Vcc max */
  677. pfl->cfi_table[0x1C] = 0x55;
  678. /* Vpp min (no Vpp pin) */
  679. pfl->cfi_table[0x1D] = 0x00;
  680. /* Vpp max (no Vpp pin) */
  681. pfl->cfi_table[0x1E] = 0x00;
  682. /* Reserved */
  683. pfl->cfi_table[0x1F] = 0x07;
  684. /* Timeout for min size buffer write */
  685. pfl->cfi_table[0x20] = 0x07;
  686. /* Typical timeout for block erase */
  687. pfl->cfi_table[0x21] = 0x0a;
  688. /* Typical timeout for full chip erase (4096 ms) */
  689. pfl->cfi_table[0x22] = 0x00;
  690. /* Reserved */
  691. pfl->cfi_table[0x23] = 0x04;
  692. /* Max timeout for buffer write */
  693. pfl->cfi_table[0x24] = 0x04;
  694. /* Max timeout for block erase */
  695. pfl->cfi_table[0x25] = 0x04;
  696. /* Max timeout for chip erase */
  697. pfl->cfi_table[0x26] = 0x00;
  698. /* Device size */
  699. pfl->cfi_table[0x27] = ctz32(device_len); /* + 1; */
  700. /* Flash device interface (8 & 16 bits) */
  701. pfl->cfi_table[0x28] = 0x02;
  702. pfl->cfi_table[0x29] = 0x00;
  703. /* Max number of bytes in multi-bytes write */
  704. if (pfl->bank_width == 1) {
  705. pfl->cfi_table[0x2A] = 0x08;
  706. } else {
  707. pfl->cfi_table[0x2A] = 0x0B;
  708. }
  709. pfl->writeblock_size = 1 << pfl->cfi_table[0x2A];
  710. if (!pfl->old_multiple_chip_handling && num_devices > 1) {
  711. pfl->writeblock_size *= num_devices;
  712. }
  713. pfl->cfi_table[0x2B] = 0x00;
  714. /* Number of erase block regions (uniform) */
  715. pfl->cfi_table[0x2C] = 0x01;
  716. /* Erase block region 1 */
  717. pfl->cfi_table[0x2D] = blocks_per_device - 1;
  718. pfl->cfi_table[0x2E] = (blocks_per_device - 1) >> 8;
  719. pfl->cfi_table[0x2F] = sector_len_per_device >> 8;
  720. pfl->cfi_table[0x30] = sector_len_per_device >> 16;
  721. /* Extended */
  722. pfl->cfi_table[0x31] = 'P';
  723. pfl->cfi_table[0x32] = 'R';
  724. pfl->cfi_table[0x33] = 'I';
  725. pfl->cfi_table[0x34] = '1';
  726. pfl->cfi_table[0x35] = '0';
  727. pfl->cfi_table[0x36] = 0x00;
  728. pfl->cfi_table[0x37] = 0x00;
  729. pfl->cfi_table[0x38] = 0x00;
  730. pfl->cfi_table[0x39] = 0x00;
  731. pfl->cfi_table[0x3a] = 0x00;
  732. pfl->cfi_table[0x3b] = 0x00;
  733. pfl->cfi_table[0x3c] = 0x00;
  734. pfl->cfi_table[0x3f] = 0x01; /* Number of protection fields */
  735. }
  736. static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
  737. {
  738. ERRP_GUARD();
  739. PFlashCFI01 *pfl = PFLASH_CFI01(dev);
  740. uint64_t total_len;
  741. int ret;
  742. if (pfl->sector_len == 0) {
  743. error_setg(errp, "attribute \"sector-length\" not specified or zero.");
  744. return;
  745. }
  746. if (pfl->nb_blocs == 0) {
  747. error_setg(errp, "attribute \"num-blocks\" not specified or zero.");
  748. return;
  749. }
  750. if (pfl->name == NULL) {
  751. error_setg(errp, "attribute \"name\" not specified.");
  752. return;
  753. }
  754. total_len = pfl->sector_len * pfl->nb_blocs;
  755. memory_region_init_rom_device(
  756. &pfl->mem, OBJECT(dev),
  757. &pflash_cfi01_ops,
  758. pfl,
  759. pfl->name, total_len, errp);
  760. if (*errp) {
  761. return;
  762. }
  763. pfl->storage = memory_region_get_ram_ptr(&pfl->mem);
  764. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
  765. if (pfl->blk) {
  766. uint64_t perm;
  767. pfl->ro = !blk_supports_write_perm(pfl->blk);
  768. perm = BLK_PERM_CONSISTENT_READ | (pfl->ro ? 0 : BLK_PERM_WRITE);
  769. ret = blk_set_perm(pfl->blk, perm, BLK_PERM_ALL, errp);
  770. if (ret < 0) {
  771. return;
  772. }
  773. } else {
  774. pfl->ro = false;
  775. }
  776. if (pfl->blk) {
  777. if (!blk_check_size_and_read_all(pfl->blk, dev, pfl->storage,
  778. total_len, errp)) {
  779. vmstate_unregister_ram(&pfl->mem, DEVICE(pfl));
  780. return;
  781. }
  782. }
  783. /*
  784. * Default to devices being used at their maximum device width. This was
  785. * assumed before the device_width support was added.
  786. */
  787. if (!pfl->max_device_width) {
  788. pfl->max_device_width = pfl->device_width;
  789. }
  790. pfl->wcycle = 0;
  791. /*
  792. * The command 0x00 is not assigned by the CFI open standard,
  793. * but QEMU historically uses it for the READ_ARRAY command (0xff).
  794. */
  795. pfl->cmd = 0x00;
  796. pfl->status = 0x80; /* WSM ready */
  797. pflash_cfi01_fill_cfi_table(pfl);
  798. pfl->blk_bytes = g_malloc(pfl->writeblock_size);
  799. pfl->blk_offset = -1;
  800. }
  801. static void pflash_cfi01_system_reset(DeviceState *dev)
  802. {
  803. PFlashCFI01 *pfl = PFLASH_CFI01(dev);
  804. trace_pflash_reset(pfl->name);
  805. /*
  806. * The command 0x00 is not assigned by the CFI open standard,
  807. * but QEMU historically uses it for the READ_ARRAY command (0xff).
  808. */
  809. pfl->cmd = 0x00;
  810. pfl->wcycle = 0;
  811. memory_region_rom_device_set_romd(&pfl->mem, true);
  812. /*
  813. * The WSM ready timer occurs at most 150ns after system reset.
  814. * This model deliberately ignores this delay.
  815. */
  816. pfl->status = 0x80;
  817. pfl->blk_offset = -1;
  818. }
  819. static const Property pflash_cfi01_properties[] = {
  820. DEFINE_PROP_DRIVE("drive", PFlashCFI01, blk),
  821. /* num-blocks is the number of blocks actually visible to the guest,
  822. * ie the total size of the device divided by the sector length.
  823. * If we're emulating flash devices wired in parallel the actual
  824. * number of blocks per individual device will differ.
  825. */
  826. DEFINE_PROP_UINT32("num-blocks", PFlashCFI01, nb_blocs, 0),
  827. DEFINE_PROP_UINT64("sector-length", PFlashCFI01, sector_len, 0),
  828. /* width here is the overall width of this QEMU device in bytes.
  829. * The QEMU device may be emulating a number of flash devices
  830. * wired up in parallel; the width of each individual flash
  831. * device should be specified via device-width. If the individual
  832. * devices have a maximum width which is greater than the width
  833. * they are being used for, this maximum width should be set via
  834. * max-device-width (which otherwise defaults to device-width).
  835. * So for instance a 32-bit wide QEMU flash device made from four
  836. * 16-bit flash devices used in 8-bit wide mode would be configured
  837. * with width = 4, device-width = 1, max-device-width = 2.
  838. *
  839. * If device-width is not specified we default to backwards
  840. * compatible behaviour which is a bad emulation of two
  841. * 16 bit devices making up a 32 bit wide QEMU device. This
  842. * is deprecated for new uses of this device.
  843. */
  844. DEFINE_PROP_UINT8("width", PFlashCFI01, bank_width, 0),
  845. DEFINE_PROP_UINT8("device-width", PFlashCFI01, device_width, 0),
  846. DEFINE_PROP_UINT8("max-device-width", PFlashCFI01, max_device_width, 0),
  847. DEFINE_PROP_BIT("big-endian", PFlashCFI01, features, PFLASH_BE, 0),
  848. DEFINE_PROP_BIT("secure", PFlashCFI01, features, PFLASH_SECURE, 0),
  849. DEFINE_PROP_UINT16("id0", PFlashCFI01, ident0, 0),
  850. DEFINE_PROP_UINT16("id1", PFlashCFI01, ident1, 0),
  851. DEFINE_PROP_UINT16("id2", PFlashCFI01, ident2, 0),
  852. DEFINE_PROP_UINT16("id3", PFlashCFI01, ident3, 0),
  853. DEFINE_PROP_STRING("name", PFlashCFI01, name),
  854. DEFINE_PROP_BOOL("old-multiple-chip-handling", PFlashCFI01,
  855. old_multiple_chip_handling, false),
  856. };
  857. static void pflash_cfi01_class_init(ObjectClass *klass, void *data)
  858. {
  859. DeviceClass *dc = DEVICE_CLASS(klass);
  860. device_class_set_legacy_reset(dc, pflash_cfi01_system_reset);
  861. dc->realize = pflash_cfi01_realize;
  862. device_class_set_props(dc, pflash_cfi01_properties);
  863. dc->vmsd = &vmstate_pflash;
  864. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  865. }
  866. static const TypeInfo pflash_cfi01_types[] = {
  867. {
  868. .name = TYPE_PFLASH_CFI01,
  869. .parent = TYPE_SYS_BUS_DEVICE,
  870. .instance_size = sizeof(PFlashCFI01),
  871. .class_init = pflash_cfi01_class_init,
  872. },
  873. };
  874. DEFINE_TYPES(pflash_cfi01_types)
  875. PFlashCFI01 *pflash_cfi01_register(hwaddr base,
  876. const char *name,
  877. hwaddr size,
  878. BlockBackend *blk,
  879. uint32_t sector_len,
  880. int bank_width,
  881. uint16_t id0, uint16_t id1,
  882. uint16_t id2, uint16_t id3,
  883. int be)
  884. {
  885. DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
  886. if (blk) {
  887. qdev_prop_set_drive(dev, "drive", blk);
  888. }
  889. assert(QEMU_IS_ALIGNED(size, sector_len));
  890. qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
  891. qdev_prop_set_uint64(dev, "sector-length", sector_len);
  892. qdev_prop_set_uint8(dev, "width", bank_width);
  893. qdev_prop_set_bit(dev, "big-endian", !!be);
  894. qdev_prop_set_uint16(dev, "id0", id0);
  895. qdev_prop_set_uint16(dev, "id1", id1);
  896. qdev_prop_set_uint16(dev, "id2", id2);
  897. qdev_prop_set_uint16(dev, "id3", id3);
  898. qdev_prop_set_string(dev, "name", name);
  899. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  900. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  901. return PFLASH_CFI01(dev);
  902. }
  903. BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl)
  904. {
  905. return fl->blk;
  906. }
  907. MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl)
  908. {
  909. return &fl->mem;
  910. }
  911. /*
  912. * Handle -drive if=pflash for machines that use properties.
  913. * If @dinfo is null, do nothing.
  914. * Else if @fl's property "drive" is already set, fatal error.
  915. * Else set it to the BlockBackend with @dinfo.
  916. */
  917. void pflash_cfi01_legacy_drive(PFlashCFI01 *fl, DriveInfo *dinfo)
  918. {
  919. Location loc;
  920. if (!dinfo) {
  921. return;
  922. }
  923. loc_push_none(&loc);
  924. qemu_opts_loc_restore(dinfo->opts);
  925. if (fl->blk) {
  926. error_report("clashes with -machine");
  927. exit(1);
  928. }
  929. qdev_prop_set_drive_err(DEVICE(fl), "drive", blk_by_legacy_dinfo(dinfo),
  930. &error_fatal);
  931. loc_pop(&loc);
  932. }
  933. static void postload_update_cb(void *opaque, bool running, RunState state)
  934. {
  935. PFlashCFI01 *pfl = opaque;
  936. /* This is called after bdrv_activate_all. */
  937. qemu_del_vm_change_state_handler(pfl->vmstate);
  938. pfl->vmstate = NULL;
  939. trace_pflash_postload_cb(pfl->name);
  940. pflash_update(pfl, 0, pfl->sector_len * pfl->nb_blocs);
  941. }
  942. static int pflash_post_load(void *opaque, int version_id)
  943. {
  944. PFlashCFI01 *pfl = opaque;
  945. if (!pfl->ro) {
  946. pfl->vmstate = qemu_add_vm_change_state_handler(postload_update_cb,
  947. pfl);
  948. }
  949. return 0;
  950. }