2 * Copyright (C) 2002 Benjamin Herrenschmidt (benh@kernel.crashing.org)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Todo: - add support for the OF persistent properties
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/stddef.h>
15 #include <linux/string.h>
16 #include <linux/nvram.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/adb.h>
22 #include <linux/pmu.h>
23 #include <linux/bootmem.h>
24 #include <linux/completion.h>
25 #include <linux/spinlock.h>
26 #include <asm/sections.h>
28 #include <asm/system.h>
30 #include <asm/machdep.h>
31 #include <asm/nvram.h>
36 #define DBG(x...) printk(x)
41 #define NVRAM_SIZE 0x2000 /* 8kB of non-volatile RAM */
43 #define CORE99_SIGNATURE 0x5a
44 #define CORE99_ADLER_START 0x14
46 /* On Core99, nvram is either a sharp, a micron or an AMD flash */
47 #define SM_FLASH_STATUS_DONE 0x80
48 #define SM_FLASH_STATUS_ERR 0x38
50 #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
51 #define SM_FLASH_CMD_ERASE_SETUP 0x20
52 #define SM_FLASH_CMD_RESET 0xff
53 #define SM_FLASH_CMD_WRITE_SETUP 0x40
54 #define SM_FLASH_CMD_CLEAR_STATUS 0x50
55 #define SM_FLASH_CMD_READ_STATUS 0x70
57 /* CHRP NVRAM header */
66 struct core99_header {
67 struct chrp_header hdr;
74 * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
76 static int nvram_naddrs;
77 static volatile unsigned char *nvram_data;
78 static int is_core_99;
79 static int core99_bank = 0;
80 static int nvram_partitions[3];
81 // XXX Turn that into a sem
82 static DEFINE_SPINLOCK(nv_lock);
84 extern int pmac_newworld;
85 extern int system_running;
87 static int (*core99_write_bank)(int bank, u8* datas);
88 static int (*core99_erase_bank)(int bank);
90 static char *nvram_image;
93 static unsigned char core99_nvram_read_byte(int addr)
95 if (nvram_image == NULL)
97 return nvram_image[addr];
100 static void core99_nvram_write_byte(int addr, unsigned char val)
102 if (nvram_image == NULL)
104 nvram_image[addr] = val;
107 static ssize_t core99_nvram_read(char *buf, size_t count, loff_t *index)
111 if (nvram_image == NULL)
113 if (*index > NVRAM_SIZE)
117 if (i + count > NVRAM_SIZE)
118 count = NVRAM_SIZE - i;
120 memcpy(buf, &nvram_image[i], count);
125 static ssize_t core99_nvram_write(char *buf, size_t count, loff_t *index)
129 if (nvram_image == NULL)
131 if (*index > NVRAM_SIZE)
135 if (i + count > NVRAM_SIZE)
136 count = NVRAM_SIZE - i;
138 memcpy(&nvram_image[i], buf, count);
143 static ssize_t core99_nvram_size(void)
145 if (nvram_image == NULL)
151 static volatile unsigned char *nvram_addr;
152 static int nvram_mult;
154 static unsigned char direct_nvram_read_byte(int addr)
156 return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);
159 static void direct_nvram_write_byte(int addr, unsigned char val)
161 out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);
165 static unsigned char indirect_nvram_read_byte(int addr)
170 spin_lock_irqsave(&nv_lock, flags);
171 out_8(nvram_addr, addr >> 5);
172 val = in_8(&nvram_data[(addr & 0x1f) << 4]);
173 spin_unlock_irqrestore(&nv_lock, flags);
178 static void indirect_nvram_write_byte(int addr, unsigned char val)
182 spin_lock_irqsave(&nv_lock, flags);
183 out_8(nvram_addr, addr >> 5);
184 out_8(&nvram_data[(addr & 0x1f) << 4], val);
185 spin_unlock_irqrestore(&nv_lock, flags);
189 #ifdef CONFIG_ADB_PMU
191 static void pmu_nvram_complete(struct adb_request *req)
194 complete((struct completion *)req->arg);
197 static unsigned char pmu_nvram_read_byte(int addr)
199 struct adb_request req;
200 DECLARE_COMPLETION(req_complete);
202 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
203 if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,
204 (addr >> 8) & 0xff, addr & 0xff))
206 if (system_state == SYSTEM_RUNNING)
207 wait_for_completion(&req_complete);
208 while (!req.complete)
213 static void pmu_nvram_write_byte(int addr, unsigned char val)
215 struct adb_request req;
216 DECLARE_COMPLETION(req_complete);
218 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
219 if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,
220 (addr >> 8) & 0xff, addr & 0xff, val))
222 if (system_state == SYSTEM_RUNNING)
223 wait_for_completion(&req_complete);
224 while (!req.complete)
228 #endif /* CONFIG_ADB_PMU */
229 #endif /* CONFIG_PPC32 */
231 static u8 chrp_checksum(struct chrp_header* hdr)
234 u16 sum = hdr->signature;
235 for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
238 sum = (sum & 0xFF) + (sum>>8);
242 static u32 core99_calc_adler(u8 *buffer)
247 buffer += CORE99_ADLER_START;
250 for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
251 if ((cnt % 5000) == 0) {
261 return (high << 16) | low;
264 static u32 core99_check(u8* datas)
266 struct core99_header* hdr99 = (struct core99_header*)datas;
268 if (hdr99->hdr.signature != CORE99_SIGNATURE) {
269 DBG("Invalid signature\n");
272 if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
273 DBG("Invalid checksum\n");
276 if (hdr99->adler != core99_calc_adler(datas)) {
277 DBG("Invalid adler\n");
280 return hdr99->generation;
283 static int sm_erase_bank(int bank)
286 unsigned long timeout;
288 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
290 DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
292 out_8(base, SM_FLASH_CMD_ERASE_SETUP);
293 out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
296 if (++timeout > 1000000) {
297 printk(KERN_ERR "nvram: Sharp/Micron flash erase timeout !\n");
300 out_8(base, SM_FLASH_CMD_READ_STATUS);
302 } while (!(stat & SM_FLASH_STATUS_DONE));
304 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
305 out_8(base, SM_FLASH_CMD_RESET);
307 for (i=0; i<NVRAM_SIZE; i++)
308 if (base[i] != 0xff) {
309 printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
315 static int sm_write_bank(int bank, u8* datas)
318 unsigned long timeout;
320 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
322 DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
324 for (i=0; i<NVRAM_SIZE; i++) {
325 out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
327 out_8(base+i, datas[i]);
330 if (++timeout > 1000000) {
331 printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
334 out_8(base, SM_FLASH_CMD_READ_STATUS);
336 } while (!(stat & SM_FLASH_STATUS_DONE));
337 if (!(stat & SM_FLASH_STATUS_DONE))
340 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
341 out_8(base, SM_FLASH_CMD_RESET);
342 for (i=0; i<NVRAM_SIZE; i++)
343 if (base[i] != datas[i]) {
344 printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
350 static int amd_erase_bank(int bank)
353 unsigned long timeout;
355 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
357 DBG("nvram: AMD Erasing bank %d...\n", bank);
360 out_8(base+0x555, 0xaa);
363 out_8(base+0x2aa, 0x55);
367 out_8(base+0x555, 0x80);
369 out_8(base+0x555, 0xaa);
371 out_8(base+0x2aa, 0x55);
378 if (++timeout > 1000000) {
379 printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
382 stat = in_8(base) ^ in_8(base);
389 for (i=0; i<NVRAM_SIZE; i++)
390 if (base[i] != 0xff) {
391 printk(KERN_ERR "nvram: AMD flash erase failed !\n");
397 static int amd_write_bank(int bank, u8* datas)
400 unsigned long timeout;
402 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
404 DBG("nvram: AMD Writing bank %d...\n", bank);
406 for (i=0; i<NVRAM_SIZE; i++) {
408 out_8(base+0x555, 0xaa);
411 out_8(base+0x2aa, 0x55);
414 /* Write single word */
415 out_8(base+0x555, 0xa0);
417 out_8(base+i, datas[i]);
421 if (++timeout > 1000000) {
422 printk(KERN_ERR "nvram: AMD flash write timeout !\n");
425 stat = in_8(base) ^ in_8(base);
435 for (i=0; i<NVRAM_SIZE; i++)
436 if (base[i] != datas[i]) {
437 printk(KERN_ERR "nvram: AMD flash write failed !\n");
443 static void __init lookup_partitions(void)
447 struct chrp_header* hdr;
450 nvram_partitions[pmac_nvram_OF] = -1;
451 nvram_partitions[pmac_nvram_XPRAM] = -1;
452 nvram_partitions[pmac_nvram_NR] = -1;
453 hdr = (struct chrp_header *)buffer;
459 buffer[i] = ppc_md.nvram_read_val(offset+i);
460 if (!strcmp(hdr->name, "common"))
461 nvram_partitions[pmac_nvram_OF] = offset + 0x10;
462 if (!strcmp(hdr->name, "APL,MacOS75")) {
463 nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;
464 nvram_partitions[pmac_nvram_NR] = offset + 0x110;
466 offset += (hdr->len * 0x10);
467 } while(offset < NVRAM_SIZE);
469 nvram_partitions[pmac_nvram_OF] = 0x1800;
470 nvram_partitions[pmac_nvram_XPRAM] = 0x1300;
471 nvram_partitions[pmac_nvram_NR] = 0x1400;
473 DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);
474 DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);
475 DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);
478 static void core99_nvram_sync(void)
480 struct core99_header* hdr99;
483 if (!is_core_99 || !nvram_data || !nvram_image)
486 spin_lock_irqsave(&nv_lock, flags);
487 if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
491 DBG("Updating nvram...\n");
493 hdr99 = (struct core99_header*)nvram_image;
495 hdr99->hdr.signature = CORE99_SIGNATURE;
496 hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
497 hdr99->adler = core99_calc_adler(nvram_image);
498 core99_bank = core99_bank ? 0 : 1;
499 if (core99_erase_bank)
500 if (core99_erase_bank(core99_bank)) {
501 printk("nvram: Error erasing bank %d\n", core99_bank);
504 if (core99_write_bank)
505 if (core99_write_bank(core99_bank, nvram_image))
506 printk("nvram: Error writing bank %d\n", core99_bank);
508 spin_unlock_irqrestore(&nv_lock, flags);
515 static int __init core99_nvram_setup(struct device_node *dp, unsigned long addr)
518 u32 gen_bank0, gen_bank1;
520 if (nvram_naddrs < 1) {
521 printk(KERN_ERR "nvram: no address\n");
524 nvram_image = alloc_bootmem(NVRAM_SIZE);
525 if (nvram_image == NULL) {
526 printk(KERN_ERR "nvram: can't allocate ram image\n");
529 nvram_data = ioremap(addr, NVRAM_SIZE*2);
530 nvram_naddrs = 1; /* Make sure we get the correct case */
532 DBG("nvram: Checking bank 0...\n");
534 gen_bank0 = core99_check((u8 *)nvram_data);
535 gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
536 core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
538 DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
539 DBG("nvram: Active bank is: %d\n", core99_bank);
541 for (i=0; i<NVRAM_SIZE; i++)
542 nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
544 ppc_md.nvram_read_val = core99_nvram_read_byte;
545 ppc_md.nvram_write_val = core99_nvram_write_byte;
546 ppc_md.nvram_read = core99_nvram_read;
547 ppc_md.nvram_write = core99_nvram_write;
548 ppc_md.nvram_size = core99_nvram_size;
549 ppc_md.nvram_sync = core99_nvram_sync;
550 ppc_md.machine_shutdown = core99_nvram_sync;
552 * Maybe we could be smarter here though making an exclusive list
553 * of known flash chips is a bit nasty as older OF didn't provide us
554 * with a useful "compatible" entry. A solution would be to really
555 * identify the chip using flash id commands and base ourselves on
556 * a list of known chips IDs
558 if (device_is_compatible(dp, "amd-0137")) {
559 core99_erase_bank = amd_erase_bank;
560 core99_write_bank = amd_write_bank;
562 core99_erase_bank = sm_erase_bank;
563 core99_write_bank = sm_write_bank;
568 int __init pmac_nvram_init(void)
570 struct device_node *dp;
571 struct resource r1, r2;
572 unsigned int s1 = 0, s2 = 0;
577 dp = of_find_node_by_name(NULL, "nvram");
579 printk(KERN_ERR "Can't find NVRAM device\n");
583 /* Try to obtain an address */
584 if (of_address_to_resource(dp, 0, &r1) == 0) {
586 s1 = (r1.end - r1.start) + 1;
587 if (of_address_to_resource(dp, 1, &r2) == 0) {
589 s2 = (r2.end - r2.start) + 1;
593 is_core_99 = device_is_compatible(dp, "nvram,flash");
595 err = core99_nvram_setup(dp, r1.start);
600 if (_machine == _MACH_chrp && nvram_naddrs == 1) {
601 nvram_data = ioremap(r1.start, s1);
603 ppc_md.nvram_read_val = direct_nvram_read_byte;
604 ppc_md.nvram_write_val = direct_nvram_write_byte;
605 } else if (nvram_naddrs == 1) {
606 nvram_data = ioremap(r1.start, s1);
607 nvram_mult = (s1 + NVRAM_SIZE - 1) / NVRAM_SIZE;
608 ppc_md.nvram_read_val = direct_nvram_read_byte;
609 ppc_md.nvram_write_val = direct_nvram_write_byte;
610 } else if (nvram_naddrs == 2) {
611 nvram_addr = ioremap(r1.start, s1);
612 nvram_data = ioremap(r2.start, s2);
613 ppc_md.nvram_read_val = indirect_nvram_read_byte;
614 ppc_md.nvram_write_val = indirect_nvram_write_byte;
615 } else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {
616 #ifdef CONFIG_ADB_PMU
618 ppc_md.nvram_read_val = pmu_nvram_read_byte;
619 ppc_md.nvram_write_val = pmu_nvram_write_byte;
620 #endif /* CONFIG_ADB_PMU */
622 printk(KERN_ERR "Incompatible type of NVRAM\n");
625 #endif /* CONFIG_PPC32 */
633 int pmac_get_partition(int partition)
635 return nvram_partitions[partition];
638 u8 pmac_xpram_read(int xpaddr)
640 int offset = pmac_get_partition(pmac_nvram_XPRAM);
642 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
645 return ppc_md.nvram_read_val(xpaddr + offset);
648 void pmac_xpram_write(int xpaddr, u8 data)
650 int offset = pmac_get_partition(pmac_nvram_XPRAM);
652 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
655 ppc_md.nvram_write_val(xpaddr + offset, data);
658 EXPORT_SYMBOL(pmac_get_partition);
659 EXPORT_SYMBOL(pmac_xpram_read);
660 EXPORT_SYMBOL(pmac_xpram_write);