Flash memory provides games with 1 Mebibit (= 128 kibibyte) of non volatile memory to keep game progress saved after console shutdown. This is the biggest capacity offered among save types. Contrary to SRAM this kind of memory doesn't require an external battery to keep data saved. Similar to SRAM, it is accessed using the PI bus at address 0x0800_0000 (eg. with Domain 2 timings). '''Warning: The information contained in this page is still WIP and may not completely reflect actual hardware behavior, especially when "corner case" behavior differs between the various chips.''' '''Trivia:''' the Ultra SDK offered a way to access multiple Flash chips and mapped them at address 0x0800_0000 | (chip_num << 17). But no known game used more than 1 Flash chip. '''Note:''' The flash chip is a custom design (manufactured by Macronix and Matsushita), but it seems at least similar in part to the MX29L1611 model which has a public [https://pdf1.alldatasheet.com/datasheet-pdf/download/74490/MCNIX/MX29L1611.html datasheet]. === TLDR for programmer === * Flash chip PI base address is 0x08000000. It exposes a 32bit Command Internal Register (CIR) at offset 0x10000, that is at PI address 0x08010000. This CIR is write-only and determine in what mode the flash chip is and what commands to execute. * Depending on the mode it's in, it will expose different content at its base address. In ReadArray mode it will expose the data array (eg. 1Mib), in Status mode it will expose its 8bit status register, in SiliconID mode it will expose its 64bit SiliconID register, in LoadBytePage mode it will expose a 128 byte internal page buffer. * On poweron flash chip is in ReadArray mode. So reading its content can be done without touching the CIR and no write operation can corrupt its content (except maybe if you trigger a program page command). But otherwise, if you want to read flash data array content, you first have to write the ReadArray command (0xF0000000) to CIR (eg. at PI address 0x08010000). * There were different models of flash memory which have different behaviors regarding read operations. "Older" models are "word-indexed" and therefore interpret addresses as 16bit-word addresses instead of 8bit-byte addresses like "byte-indexed" model would. For "word-indexed" chip the offset should be divided by 2 to effectively read the expected data: to read at byte offset 16 you should use an offset of 16/2=8 eg. PI address 0x08000008 instead of 0x08000010. "byte-indexed" models don't need such "divide by 2" adjustment. To know if your model is word-indexed or byte-indexed you have to know which flash model you interact with (use SiliconID for that). * On top of that, for DMA transfers, there is a "256x128 byte" boundary that cannot be crossed by a single DMA: eg. to read the whole flash content you have to split into at least 4 DMA because DMA can't cross offsets 0x8000, 0x10000 and 0x18000 for "byte-indexed" chips (resp. 0x4000, 0x8000 and 0xC000 for "word-indexed" chips). Note that flash is not unique with this DMA crossing restriction, other PI devices have similar restrictions and that's why the PI controller can split automatically DMA using the PGS setting: ROM have a "512 byte" boundary (PGS=7 : 2^(2+7)=512), and SRAM have a "32768 byte" boundary (PGS=0xD 2^(2+13)=32768). Unfortunately for flash, due to the existence of "word-indexed" flash we can't just set PGS to 0xD because PI don't know about the "divide by 2" adjustment needed for them. Instead we set PGS to a big value (0xF) and manually split (and divide by 2 if required by the model of flash) all DMAs. * Regarding write operations, flash works with erase & program operations on a sector and page layout. This means that you first need to erase a full sector (or the whole chip if it makes more sense for your use case) and then program each page individually. If you need to "only" change some page and not the whole sector you have to manually read them first and program them back after the erase sector operation. * Flash chip has 8 sectors, each consisting of 128 pages, a page being 128 byte (or 64 word for word-indexed flash). * Erasing is done in 2 steps: 1. Erase Setup: write either the Sector Erase Setup (0x4B000XXX, with XXX being an index of a page in the sector you want to erase) or the Chip Erase Setup (0x3C000000) command to CIR 2. Erase: write Erase (0x78000000) command to CIR. This will start the erase operation and automatically switch the flash into status mode (eg. reading at base address will return the WSM register content) so that you can poll for ERASE_BUSY bit to know when this operation is done. When done, you should also check ERASE_OK bit to know if erasure was successful. The erase operation can take between 85ms and 300ms depending on model and if it's the whole chip or just a single sector. * Programming is done in 3 steps: 1. Load Byte Page: write LoadBytePage (0xB4000000) command into CIR so that the Internal Page buffer can be accessed at flash base address. 2. Write the content you want in that buffer. Usually this is done using a single 128-byte DMA at flash base address (0x08000000). If you write less than 128byte, default content will be 0xff. 3. Program Page: write Program (0xA5000XXX, with XXX being the index of the page you want to program). This will start the page programming operation and automatically switch the flash into status mode so you can poll for PROGRAM_BUSY bit to know when this operation is done. When done, you should also check PROGRAM_OK bit to know if programming was successful. The program operation can take between 300µs and 3.5ms depending on chip model. * To identify the flash model programmatically, first switch to SiliconID mode (eg. write SiliconID command (0xE1000000) to CIR [some models may need multiple consecutive writes to properly switch to SiliconID mode]) and then issue an 8 byte DMA at PI address 0x08000000 to read SiliconID register. It is important to use DMA here, and not IO otherwise you'll get only the first 32bit repeated twice. === Chip layout: Sectors, Pages, Byte & Words === The 1Mebibit Flash memory is organized as 8 sectors, each composed of 128 pages, with each pages being 128 x 8-bit bytes (for byte addressed chips) or 64 x 16-bit words (for word addressed chips). Erase operations (eg. resetting bits to '1') can only operate either on the full chip or on a full sector. Programming operations (eg. clearing bits to '0') can only operate on a full page. When reading chip array, the distinction between word addressed chip and byte addressed chip matters. For instance, reading at address "4" means for byte addressed chip reading starting at the 4th byte, while for a word addressed chip it means reading starting at the 4th 16bit word which would correspond to the 8th byte : converting from a byte address to a word address require dividing by 2 the byte address. The table below summarize the chip layout: {| class="wikitable" |+Chip layout !Sector !Pages !8-bit byte Addresses !16-bit word Addresses |- |0 |0x000 - 0x07F |0x0000_0000 - 0x0000_3FFF |0x0000_0000 - 0x0000_1FFF |- |1 |0x080 - 0x0FF |0x0000_4000 - 0x0000_7FFF |0x0000_2000 - 0x0000_3FFF |- |2 |0x100 - 0x17F |0x0000_8000 - 0x0000_BFFF |0x0000_4000 - 0x0000_5FFF |- |3 |0x180 - 0x1FF |0x0000_C000 - 0x0000_FFFF |0x0000_6000 - 0x0000_7FFF |- |4 |0x200 - 0x27F |0x0001_0000 - 0x0001_3FFF |0x0000_8000 - 0x0000_9FFF |- |5 |0x280 - 0x2FF |0x0001_4000 - 0x0001_7FFF |0x0000_A000 - 0x0000_BFFF |- |6 |0x300 - 0x37F |0x0001_8000 - 0x0001_BFFF |0x0000_C000 - 0x0000_DFFF |- |7 |0x380 - 0x3FF |0x0001_C000 - 0x0001_FFFF |0x0000_E000 - 0x0000_FFFF |} === Command Internal Register (CIR) === Flash chip exposes a Command Internal Register (32bits, write only), mapped at flash address 0x0001_0000 to serve as an interface between the CPU and internal Flash operations: to execute an operation or transition to a given mode, the corresponding command should be written to CIR. '''Quirks:''' Some flash chips may need multiple writes to CIR to fully transition to the new mode. For instance, flash chip MX29L1100 (and possibly other Macronix variants) may need 2 writes to CIR to effectively switch to Status or SiliconID mode. Other modes / commands don't seem to need multiple write to CIR under normal conditions. Flash chip MN63F8MPN (from Matsushita) is not affected by such quirks. Note that, CIR is write only and cannot be read. Otherwise its address would conflict with "regular" data addresses when reading. Commands recognized by CIR are given below. Further research is needed to determine if other command exists (like suspend, protect, unprotect, ...). '''Trivia''': For all known commands bits 31-28 are the inverse of bits 27-24. {| class="wikitable" |+ !Command !Value !Comment |- |Chip Erase Setup |0x3C00_0000 | |- |Sector Erase Setup |0x4B00_0XXX |XXX denotes a page number belonging to the sector to erase. example: XXX = 123, will erase sector 2 (eg. pages [0x100 - 0x17F]) Note: bits [23:10] of command are ignored (and maybe also bits [6:0] as they don't contribute to sector determination). |- |Erase |0x7800_0000 | |- |Program Page |0xA500_0XXX |XXX denotes the page to program Note : bits [23:10] of command are ignored. |- |Load Byte Page |0xB400_0000 | |- |Status Mode |0xD200_0000 | |- |Silicon ID Mode |0xE100_0000 | |- |Read Array Mode |0xF000_0000 | |} === Silicon ID Mode === When in Silicon ID Mode, the read-only SILICON_ID register is mapped into flash address space. In this mode, the lower 16 bits of addresses are ignored and only the size of the read burst matters : for read burst of length N <= 8, only the first N bytes of SILICON_ID are read (eg. bits 63 to 64-8*N). For burst length of N > 8, the behavior is chip dependent. For the MN63F8MPN, the last 16-bit word of SILICON_ID (eg. DEVICE_ID) is repeated after the first 8 byte of SILICON_ID. For the MX29L1100, the 8 byte of SILICON_ID is repeated. The behavior is chip dependent when bit 17 of address is set (eg. flash address range [0x10000 - 0x1FFFF]). For the MN63F8MPN, an "open bus" value is returned (eg. lowest 16 bits of address is repeated for the whole read burst). After such an open bus read all subsequent reads will also return an "open bus" value until a new command is written to CIR. For the MX29L1100, the SILICON_ID is repeated (same behavior as when bit 17 is not set). {| class="wikitable" |+ ! !SILICON_ID |- |63:32 |FLASH_TYPE_ID (0x1111_8001) |- |31:16 |MANUFACTURER_ID |- |15:0 |DEVICE_ID |} '''Extra Details:''' : FLASH_TYPE_ID is expected to be equal to 0x1111_8001, but further research is needed to determine its proper purpose. Known combination of Manufacturer / Device ID is given in the table below. Given that current understanding of byte / word addressing indicates that it depends on chip model we also summarize in this table if the given chip operates in byte mode or word mode. Further research is needed to verify this claim. {| class="wikitable" |+ !Name !Manufacturer ID !Device ID !Byte / Word addressing |- |MX29L0000 |0x00C2 (Macronix) |0x0000 |word |- |MX29L0001 |0x00C2 |0x0001 |word |- |MX29L1100 |0x00C2 |0x001E |word |- |MX29L1101_A |0x00C2 |0x001D |byte |- |MX29L1101_B |0x00C2 |0x0084 |byte |- |MX29L1101_C |0x00C2 |0x008E |byte |- |MN63F8MPN |0x0032 (Matsushita) |0x00F1 |byte |} === Status Mode === When in Status Mode, the STATUS register is mapped into flash address space. In this mode, the lower 16 bits of addresses are ignored and only the size of the read burst matters : the pattern 00 (with being the STATUS register bits) is repeated. '''Quirk:''' On MX29L1100 the upper 16bits of the first read come from the data of the previous mode. If bit 17 of address is set (eg. flash address range [0x10000 - 0x1FFFF]) an "open bus" value is returned (eg. lowest 16 bits of address is repeated for the whole read burst). After such an open bus read all subsequent reads will also return an "open bus" value until a new command is written to CIR. ==== Clear status ==== After an Erase or Program operation is finished, the software is responsible for clearing the Status by writing 0 at flash address 0 while in Status Mode. MN63F8MPN only: Writing any value anywhere while is Status Mode will clear the ERASE_OK or PROGRAM_OK bits if they were set. MX29L1100: ERASE_OK and PROGRAM_OK are by default set, and will be unset after corresponding operation in case of failure. To reset them to their default state after a failure, one should write 0 at flash address 0 while in Status mode. {| class="wikitable" |+ |} {| class="wikitable" |+ ! !STATUS |- |7 |WSM_READY |- |6 |unknown (maybe some kind of suspend) |- |5-4 |unknown (always 0) |- |3 |ERASE_OK |- |2 |PROGRAM_OK |- |1 |ERASE_BUSY |- |0 |PROGRAM_BUSY |} '''Extra Details:''' : WSM_READY : When set, Write State Machine is ready to accept Erase or Program commands. : ERASE_OK : After an erase operation, it will reflect the success of the operation 1: OK, 0: NOK. : PROGRAM_OK : After a program operation, it will reflect the success of the operation 1: OK, 0:NOK. : ERASE_BUSY: When set, an Erase operation is currently being executed. : PROGRAM_BUSY : When set, a Program operation is currently being executed. : Further research is needed to determine purpose of bits 4-6. They may be related to Suspend operation / Sector Protection. === Chip Erase Setup === First phase of erase operation. Should be followed by Erase command to perform full chip erasure. === Sector Erase Setup === First phase of sector erase operation. Should be followed by Erase command to perform sector erasure. === Erase === Second phase of sector/chip erase operation. This will start the erase operation (eg. reset bits to "1"). During operation, WSM_READY bit is cleared, ERASE_BUSY is set At the end of operation, WSM_READY is set, ERASE_BUSY is cleared and ERASE_OK reflects success of operation. Also, to ease status polling by application, the chip automatically transition to Status Mode at the beginning of operation and will stay in this mode until a new valid command is written to CIR. Sector and Chip Erase operation takes around <280ms and <300ms respectively for MN63F8MPN, and ~85ms for both sector and chip erase for MX29L1100. Writing an Erase command without first writing a Chip or Sector Erase Setup command will not work and do nothing : Erase command will be ignored. === Load Byte Page Mode === First phase of page programming operation. When in Load Byte Page mode, the Internal Page Buffer is mapped into flash address space at flash addresses 0x00 - 0x7F. Initial content of Internal Page is all bits set (eg. 0xFF x 128). Writing to Internal Page result in a chip dependent behavior: * MN63F8MPN can only clear bits : eg. assuming an initial value of 0xFF at a given location, writing 0xA5 and then 0x5A there will result in value (0xFF & 0xA5 & 0x5A) = 0x00 being programmed into Internal Page. * MX29L1100 writes behaves normally (they can set and clear bits). Internal Page can be read. After a Program operation, the content on Internal Page is reset to all bits set. The content of Internal Page is persisted until either a Page is programmed, or chip power off. (Tested only on MX9L1100) === Page Program === Second phase of page programming operation. This will start the programming operation (eg. clearing bits to "0") of specified page, using content of previously loaded bytes into Internal Page Buffer. During operation, WSM_READY bit is cleared, PROGRAM_BUSY is set At the end of operation, WSM_READY is set, PROGRAM_BUSY is cleared and PROGRAM_OK reflects success of operation. Also, to ease status polling by application, the chip automatically transition to Status Mode at the beginning of operation and will stay in this mode until a new valid command is written to CIR. Program Page operation takes around <300µs (MN63F8MPN) / ~3.5ms (MX29L1100) It is recommended to only program pages that have all bits set to "1" (eg. the page should be erased before being programmed). But further research is required to document behavior in such a case. === Read Array Mode === When in Read Array mode, the Array Data is mapped for reading into flash address space at flash addresses 0x00000 - 0x1FFFF (for byte addressed chip) and 0x00000 - 0x0FFFF (for word addressed chip). Array Data can be read using IO or DMA. Note however that for DMA, any read burst crossing a 256-page boundary (eg. end of an odd sector) will "wrap around" and return value from the beginning of the previous even sector. === PI Configuration === '''Note:''' In this section when we talk about "Page Size" we refer to the ''PI'' Page Size setting (eg. when to split DMA bursts and emit an address on the PI bus), not the ''Flash'' Page Size (eg. the minimum size of "programmable" data). PI BSD Dom2 should be configured with the following values to access Flash memory : {| class="wikitable" |+PI BSD Configuration ! !Value !Meaning |- |Latency |0x5 |(5+1)*16 = 96ns |- |Pulse |0xC |(12+1)*16 = 208ns |- |Page Size |0xF |2^(15+2) = 128KiB |- |Release |0x2 |(2+1)*16 = 48ns |} Regarding the choice of Page Size, we recommend using the maximum value so that all meaningful DMA will be done in a single burst (eg. send address only once at beginning of DMA) and manually split DMA. This is to accommodate the 3 main constrains : * to read all bits of SiliconID a burst must be at least 8 byte (eg. PGS >= 1) * a single burst can't cross a 256-page boundary without exhibiting a wrap around * word addressed chips can't directly use regular byte addresses If there is no intent of supporting word addressed chip, maybe using PGS = 5 (128B) or PGS = 0xD (32KiB) could work as they would properly split DMA at the page (resp. 256-page) boundary.