11 #include <linux/export.h>
12 #include <linux/kernel.h>
13 #include <linux/stddef.h>
14 #include <linux/string.h>
15 #include <linux/nvram.h>
18 #include <linux/errno.h>
19 #include <linux/adb.h>
20 #include <linux/pmu.h>
24 #include <asm/sections.h>
27 #include <asm/machdep.h>
28 #include <asm/nvram.h>
35 #define DBG(x...) printk(x)
40 #define NVRAM_SIZE 0x2000
42 #define CORE99_SIGNATURE 0x5a
43 #define CORE99_ADLER_START 0x14
46 #define SM_FLASH_STATUS_DONE 0x80
47 #define SM_FLASH_STATUS_ERR 0x38
49 #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
50 #define SM_FLASH_CMD_ERASE_SETUP 0x20
51 #define SM_FLASH_CMD_RESET 0xff
52 #define SM_FLASH_CMD_WRITE_SETUP 0x40
53 #define SM_FLASH_CMD_CLEAR_STATUS 0x50
54 #define SM_FLASH_CMD_READ_STATUS 0x70
75 static int nvram_naddrs;
76 static volatile unsigned char __iomem *nvram_data;
77 static int is_core_99;
78 static int core99_bank = 0;
79 static int nvram_partitions[3];
83 static int (*core99_write_bank)(
int bank,
u8* datas);
84 static int (*core99_erase_bank)(
int bank);
86 static char *nvram_image;
89 static unsigned char core99_nvram_read_byte(
int addr)
91 if (nvram_image ==
NULL)
93 return nvram_image[
addr];
96 static void core99_nvram_write_byte(
int addr,
unsigned char val)
98 if (nvram_image ==
NULL)
107 if (nvram_image ==
NULL)
116 memcpy(buf, &nvram_image[i], count);
121 static ssize_t core99_nvram_write(
char *buf,
size_t count, loff_t *index)
125 if (nvram_image ==
NULL)
134 memcpy(&nvram_image[i], buf, count);
139 static ssize_t core99_nvram_size(
void)
141 if (nvram_image ==
NULL)
147 static volatile unsigned char __iomem *nvram_addr;
148 static int nvram_mult;
150 static unsigned char direct_nvram_read_byte(
int addr)
155 static void direct_nvram_write_byte(
int addr,
unsigned char val)
161 static unsigned char indirect_nvram_read_byte(
int addr)
167 out_8(nvram_addr, addr >> 5);
168 val =
in_8(&nvram_data[(addr & 0x1f) << 4]);
174 static void indirect_nvram_write_byte(
int addr,
unsigned char val)
179 out_8(nvram_addr, addr >> 5);
180 out_8(&nvram_data[(addr & 0x1f) << 4], val);
185 #ifdef CONFIG_ADB_PMU
193 static unsigned char pmu_nvram_read_byte(
int addr)
200 (addr >> 8) & 0xff, addr & 0xff))
209 static void pmu_nvram_write_byte(
int addr,
unsigned char val)
216 (addr >> 8) & 0xff, addr & 0xff, val))
231 for (ptr = (
u8 *)&hdr->
len; ptr < hdr->
data; ptr++)
234 sum = (sum & 0xFF) + (sum>>8);
247 if ((cnt % 5000) == 0) {
257 return (high << 16) |
low;
260 static u32 core99_check(
u8* datas)
265 DBG(
"Invalid signature\n");
268 if (hdr99->
hdr.cksum != chrp_checksum(&hdr99->
hdr)) {
269 DBG(
"Invalid checksum\n");
272 if (hdr99->
adler != core99_calc_adler(datas)) {
273 DBG(
"Invalid adler\n");
279 static int sm_erase_bank(
int bank)
286 DBG(
"nvram: Sharp/Micron Erasing bank %d...\n", bank);
292 if (++timeout > 1000000) {
310 static int sm_write_bank(
int bank,
u8* datas)
313 unsigned long timeout;
317 DBG(
"nvram: Sharp/Micron Writing bank %d...\n", bank);
322 out_8(base+i, datas[i]);
325 if (++timeout > 1000000) {
331 }
while (!(stat & SM_FLASH_STATUS_DONE));
332 if (!(stat & SM_FLASH_STATUS_DONE))
337 if (
memcmp(base, datas, NVRAM_SIZE)) {
344 static int amd_erase_bank(
int bank)
347 unsigned long timeout;
351 DBG(
"nvram: AMD Erasing bank %d...\n", bank);
354 out_8(base+0x555, 0xaa);
357 out_8(base+0x2aa, 0x55);
361 out_8(base+0x555, 0x80);
363 out_8(base+0x555, 0xaa);
365 out_8(base+0x2aa, 0x55);
372 if (++timeout > 1000000) {
390 static int amd_write_bank(
int bank,
u8* datas)
393 unsigned long timeout;
397 DBG(
"nvram: AMD Writing bank %d...\n", bank);
401 out_8(base+0x555, 0xaa);
404 out_8(base+0x2aa, 0x55);
408 out_8(base+0x555, 0xa0);
410 out_8(base+i, datas[i]);
414 if (++timeout > 1000000) {
428 if (
memcmp(base, datas, NVRAM_SIZE)) {
435 static void __init lookup_partitions(
void)
451 buffer[i] =
ppc_md.nvram_read_val(offset+i);
458 offset += (hdr->
len * 0x10);
459 }
while(offset < NVRAM_SIZE);
470 static void core99_nvram_sync(
void)
475 if (!is_core_99 || !nvram_data || !nvram_image)
479 if (!
memcmp(nvram_image, (
u8*)nvram_data + core99_bank*NVRAM_SIZE,
483 DBG(
"Updating nvram...\n");
488 hdr99->
hdr.cksum = chrp_checksum(&hdr99->
hdr);
489 hdr99->
adler = core99_calc_adler(nvram_image);
490 core99_bank = core99_bank ? 0 : 1;
491 if (core99_erase_bank)
492 if (core99_erase_bank(core99_bank)) {
493 printk(
"nvram: Error erasing bank %d\n", core99_bank);
496 if (core99_write_bank)
497 if (core99_write_bank(core99_bank, nvram_image))
498 printk(
"nvram: Error writing bank %d\n", core99_bank);
510 u32 gen_bank0, gen_bank1;
512 if (nvram_naddrs < 1) {
517 if (nvram_image ==
NULL) {
521 nvram_data =
ioremap(addr, NVRAM_SIZE*2);
524 DBG(
"nvram: Checking bank 0...\n");
526 gen_bank0 = core99_check((
u8 *)nvram_data);
527 gen_bank1 = core99_check((
u8 *)nvram_data + NVRAM_SIZE);
528 core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
530 DBG(
"nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
531 DBG(
"nvram: Active bank is: %d\n", core99_bank);
534 nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
536 ppc_md.nvram_read_val = core99_nvram_read_byte;
537 ppc_md.nvram_write_val = core99_nvram_write_byte;
538 ppc_md.nvram_read = core99_nvram_read;
539 ppc_md.nvram_write = core99_nvram_write;
540 ppc_md.nvram_size = core99_nvram_size;
541 ppc_md.nvram_sync = core99_nvram_sync;
542 ppc_md.machine_shutdown = core99_nvram_sync;
551 core99_erase_bank = amd_erase_bank;
552 core99_write_bank = amd_write_bank;
554 core99_erase_bank = sm_erase_bank;
555 core99_write_bank = sm_write_bank;
564 unsigned int s1 = 0,
s2 = 0;
578 s1 = resource_size(&r1);
581 s2 = resource_size(&r2);
587 err = core99_nvram_setup(dp, r1.
start);
592 if (machine_is(chrp) && nvram_naddrs == 1) {
595 ppc_md.nvram_read_val = direct_nvram_read_byte;
596 ppc_md.nvram_write_val = direct_nvram_write_byte;
597 }
else if (nvram_naddrs == 1) {
599 nvram_mult = (s1 + NVRAM_SIZE - 1) / NVRAM_SIZE;
600 ppc_md.nvram_read_val = direct_nvram_read_byte;
601 ppc_md.nvram_write_val = direct_nvram_write_byte;
602 }
else if (nvram_naddrs == 2) {
605 ppc_md.nvram_read_val = indirect_nvram_read_byte;
606 ppc_md.nvram_write_val = indirect_nvram_write_byte;
607 }
else if (nvram_naddrs == 0 &&
sys_ctrler == SYS_CTRLER_PMU) {
608 #ifdef CONFIG_ADB_PMU
610 ppc_md.nvram_read_val = pmu_nvram_read_byte;
611 ppc_md.nvram_write_val = pmu_nvram_write_byte;
627 return nvram_partitions[partition];
637 return ppc_md.nvram_read_val(xpaddr + offset);
647 ppc_md.nvram_write_val(xpaddr + offset, data);