rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
flash_int.cpp
Go to the documentation of this file.
1/**
2 *
3 * @file flash.c
4 * @brief Lower-level code for Cypress related to internal flash memory
5 * @author andreika <prometheus.pcb@gmail.com>
6 */
7
8#include "pch.h"
9
10#if EFI_STORAGE_INT_FLASH
11
12#include "flash_int.h"
13#include <string.h>
14
15
16// todo: add DualFlash support
17
18//#define CYPRESS_FLASH_DEBUG
19
20typedef uint32_t flashdata_t;
21
22static volatile uint32_t mainFlashMap[] = {
23 0x00000000, 0x00002000, 0x00004000, 0x00006000, 0x00008000,
24 0x00010000, 0x00020000, 0x00030000, 0x00040000, 0x00050000,
25 0x00060000, 0x00070000, 0x00080000, 0x00090000, 0x000A0000,
26 0x000B0000, 0x000C0000, 0x000D0000, 0x000E0000, 0x000F0000,
27 0x00100000, 0x00102000, 0x00104000, 0x00106000, 0x00108000,
28 0x00110000, 0x00120000, 0x00130000, 0x00140000, 0x00150000,
29 0x00160000, 0x00170000, 0x00180000,
30 // todo: add upper 40k flash area
31};
32
33bool flashUnlock(void) {
34 return true;
35}
36
37bool flashLock(void) {
38 return true;
39}
40
41#define CYPRESS_FLASH_WORD_ALIGNMENT 2
42
43static int alignToWord(int v) {
44 return (v + CYPRESS_FLASH_WORD_ALIGNMENT - 1) & ~(CYPRESS_FLASH_WORD_ALIGNMENT - 1);
45}
46
47static __attribute__((optimize("O0"))) int flashSectorEraseAtAddress(volatile uint32_t sectorStart) {
48 return MFlash_SectorErase((uint16_t*)sectorStart) != Ok ? FLASH_RETURN_BAD_FLASH : FLASH_RETURN_SUCCESS;
49}
50
51int __attribute__((optimize("O0"))) intFlashErase(flashaddr_t address, size_t size) {
52 // todo: this is a temporary hack
53 // todo: why the code below doesn't work with -O2?!
54 if (flashSectorEraseAtAddress(address) != FLASH_RETURN_SUCCESS) {
55 return FLASH_RETURN_BAD_FLASH;
56 }
57#if 0
58 volatile int i;
60
61 volatile int numSectors = (sizeof(mainFlashMap) / sizeof(mainFlashMap[0])) - 1;
62 // list through all sectors and erase those inside the given memory area
63 for (i = 0; i < numSectors; i++) {
64 volatile uint32_t sectorStart = mainFlashMap[i];
65 volatile uint32_t sectorEnd = mainFlashMap[i + 1] - 1;
66 // if the sector overlaps the address range
67 if (sectorStart < (address + size) && sectorEnd >= address) {
68 if (flashSectorEraseAtAddress(sectorStart) != FLASH_RETURN_SUCCESS) {
69 return FLASH_RETURN_BAD_FLASH;
70 }
71 // check if erased
72 size_t sectorSize = sectorEnd - sectorStart + 1;
73 if (flashIsErased(sectorStart, sectorSize) == FALSE)
74 return FLASH_RETURN_BAD_FLASH; /* Sector is not empty despite the erase cycle! */
75
76 }
77 }
78#endif
79 /* Successfully deleted sector */
81}
82
83int intFlashWrite(flashaddr_t address, const char* buffer, size_t size) {
84 uint32_t sizeInWords = alignToWord(size) >> 1;
85 return MFlash_WriteData16Bit((uint16_t*)address, (uint16_t*)buffer, sizeInWords) == Ok ? FLASH_RETURN_SUCCESS : FLASH_RETURN_BAD_FLASH;
86 //return MFlash_WriteData16Bit_Fm0Type3CrSecureArea((uint16_t*)address, (uint16_t*)buffer, sizeInWords) == Ok ? 0 : -1;
87}
88
89bool intFlashIsErased(flashaddr_t address, size_t size) {
90 /* Check for default set bits in the flash memory
91 * For efficiency, compare flashdata_t values as much as possible,
92 * then, fallback to byte per byte comparison. */
93 while (size >= sizeof(flashdata_t)) {
94 if (*(volatile flashdata_t*) address != (flashdata_t) (-1)) // flashdata_t being unsigned, -1 is 0xFF..FF
95 return false;
96 address += sizeof(flashdata_t);
97 size -= sizeof(flashdata_t);
98 }
99 while (size > 0) {
100 if (*(char*) address != 0xFF)
101 return false;
102 ++address;
103 --size;
104 }
105
106 return TRUE;
107}
108
109bool intFlashCompare(flashaddr_t address, const char* buffer, size_t size) {
110 /* For efficiency, compare flashdata_t values as much as possible,
111 * then, fallback to byte per byte comparison. */
112 while (size >= sizeof(flashdata_t)) {
113 if (*(volatile flashdata_t*) address != *(flashdata_t*) buffer)
114 return FALSE;
115 address += sizeof(flashdata_t);
116 buffer += sizeof(flashdata_t);
117 size -= sizeof(flashdata_t);
118 }
119 while (size > 0) {
120 if (*(volatile char*) address != *buffer)
121 return FALSE;
122 ++address;
123 ++buffer;
124 --size;
125 }
126
127 return TRUE;
128}
129
130int intFlashRead(flashaddr_t source, char* destination, size_t size) {
131 memcpy(destination, (char*) source, size);
133}
134
135#endif /* EFI_STORAGE_INT_FLASH */
typedef __attribute__
Ignition Mode.
bool flashUnlock(void)
Definition flash_int.cpp:33
bool flashLock(void)
Definition flash_int.cpp:37
return FLASH_RETURN_SUCCESS
Definition flash_int.cpp:80
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
Definition flash_int.cpp:83
static int alignToWord(int v)
Definition flash_int.cpp:43
volatile int numSectors
Definition flash_int.cpp:61
bool intFlashIsErased(flashaddr_t address, size_t size)
Check if the size bytes of flash memory starting at address are erased.
Definition flash_int.cpp:89
int intFlashRead(flashaddr_t source, char *destination, size_t size)
Copy data from the flash memory to a destination.
int size_t size
Definition flash_int.cpp:51
bool intFlashCompare(flashaddr_t address, const char *buffer, size_t size)
Check if the data in buffer are identical to the one in flash memory.
uint32_t flashdata_t
Definition flash_int.cpp:20
static volatile uint32_t mainFlashMap[]
Definition flash_int.cpp:22
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.
uintptr_t flashaddr_t
Address in the flash memory.
Definition flash_int.h:86
uint32_t flashdata_t
Definition flash_int.h:54
static BigBufferHandle buffer