Use helper functions to access flash chips
Right now we perform direct pointer manipulation without any abstraction
to read from and write to memory mapped flash chips. That makes it
impossible to drive any flasher which does not mmap the whole chip.
Using helper functions readb() and writeb() allows a driver for external
flash programmers like Paraflasher to replace readb and writeb with
calls to its own chip access routines.
This patch has the additional advantage of removing lots of unnecessary
casts to volatile uint8_t * and now-superfluous parentheses which caused
poor readability.
I used the semantic patcher Coccinelle to create this patch. The
semantic patch follows:
@@
expression a;
typedef uint8_t;
volatile uint8_t *b;
@@
- *(b) = (a);
+ writeb(a, b);
@@
volatile uint8_t *b;
@@
- *(b)
+ readb(b)
@@
type T;
T b;
@@
(
readb
|
writeb
)
(...,
- (T)
- (b)
+ b
)
In contrast to a sed script, the semantic patch performs type checking
before converting anything.
Tested-by: Joe Julian
Corresponding to flashrom svn r418 and coreboot v2 svn r3971.
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: FENG Yu Ning <fengyuning1984@gmail.com>
diff --git a/stm50flw0x0x.c b/stm50flw0x0x.c
index c06bab9..edb76cc 100644
--- a/stm50flw0x0x.c
+++ b/stm50flw0x0x.c
@@ -33,9 +33,9 @@
void protect_stm50flw0x0x(volatile uint8_t *bios)
{
- *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
- *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
- *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
+ writeb(0xAA, bios + 0x5555);
+ writeb(0x55, bios + 0x2AAA);
+ writeb(0xA0, bios + 0x5555);
usleep(200);
}
@@ -47,37 +47,37 @@
uint32_t largeid1, largeid2;
/* Issue JEDEC Product ID Entry command */
- *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+ writeb(0xAA, bios + 0x5555);
myusec_delay(10);
- *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+ writeb(0x55, bios + 0x2AAA);
myusec_delay(10);
- *(volatile uint8_t *)(bios + 0x5555) = 0x90;
+ writeb(0x90, bios + 0x5555);
myusec_delay(40);
/* Read product ID */
- id1 = *(volatile uint8_t *)bios;
- id2 = *(volatile uint8_t *)(bios + 0x01);
+ id1 = readb(bios);
+ id2 = readb(bios + 0x01);
largeid1 = id1;
largeid2 = id2;
/* Check if it is a continuation ID, this should be a while loop. */
if (id1 == 0x7F) {
largeid1 <<= 8;
- id1 = *(volatile uint8_t *)(bios + 0x100);
+ id1 = readb(bios + 0x100);
largeid1 |= id1;
}
if (id2 == 0x7F) {
largeid2 <<= 8;
- id2 = *(volatile uint8_t *)(bios + 0x101);
+ id2 = readb(bios + 0x101);
largeid2 |= id2;
}
/* Issue JEDEC Product ID Exit command */
- *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+ writeb(0xAA, bios + 0x5555);
myusec_delay(10);
- *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+ writeb(0x55, bios + 0x2AAA);
myusec_delay(10);
- *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
+ writeb(0xF0, bios + 0x5555);
myusec_delay(40);
printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __FUNCTION__, largeid1,
@@ -96,21 +96,21 @@
uint8_t id1;
// id2;
- *bios = 0x70;
- if ((*bios & 0x80) == 0) { // it's busy
- while ((*bios & 0x80) == 0) ;
+ writeb(0x70, bios);
+ if ((readb(bios) & 0x80) == 0) { // it's busy
+ while ((readb(bios) & 0x80) == 0) ;
}
// put another command to get out of status register mode
- *bios = 0x90;
+ writeb(0x90, bios);
myusec_delay(10);
- id1 = *(volatile uint8_t *)bios;
+ id1 = readb(bios);
// this is needed to jam it out of "read id" mode
- *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
- *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
- *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
+ writeb(0xAA, bios + 0x5555);
+ writeb(0x55, bios + 0x2AAA);
+ writeb(0xF0, bios + 0x5555);
}
/*
@@ -142,8 +142,8 @@
// unlock each 4k-sector
for (j = 0; j < 0x10000; j += 0x1000) {
printf_debug("unlocking at 0x%x\n", offset + j);
- *(flash_addr + offset + j) = unlock_sector;
- if (*(flash_addr + offset + j) != unlock_sector) {
+ writeb(unlock_sector, flash_addr + offset + j);
+ if (readb(flash_addr + offset + j) != unlock_sector) {
printf("Cannot unlock sector @ 0x%x\n",
offset + j);
return -1;
@@ -151,8 +151,8 @@
}
} else {
printf_debug("unlocking at 0x%x\n", offset);
- *(flash_addr + offset) = unlock_sector;
- if (*(flash_addr + offset) != unlock_sector) {
+ writeb(unlock_sector, flash_addr + offset);
+ if (readb(flash_addr + offset) != unlock_sector) {
printf("Cannot unlock sector @ 0x%x\n", offset);
return -1;
}
@@ -167,17 +167,17 @@
int j;
// clear status register
- *bios = 0x50;
+ writeb(0x50, bios);
printf_debug("Erase at %p\n", bios);
// now start it
- *(volatile uint8_t *)(bios) = 0x20;
- *(volatile uint8_t *)(bios) = 0xd0;
+ writeb(0x20, bios);
+ writeb(0xd0, bios);
myusec_delay(10);
wait_stm50flw0x0x(flash->virtual_memory);
for (j = 0; j < flash->page_size; j++) {
- if (*(bios + j) != 0xFF) {
+ if (readb(bios + j) != 0xFF) {
printf("Erase failed at 0x%x\n", offset + j);
return -1;
}
@@ -197,8 +197,8 @@
/* transfer data from source to destination */
for (i = 0; i < page_size; i++) {
- *dst = 0x40;
- *dst++ = *src++;
+ writeb(0x40, dst);
+ writeb(*src++, dst++);
wait_stm50flw0x0x(bios);
}
@@ -210,7 +210,7 @@
dst = d;
src = s;
for (i = 0; i < page_size; i++) {
- if (*dst != *src) {
+ if (readb(dst) != *src) {
rc = -1;
break;
}