2 * arch/ppc/platforms/pmac_nvram.c
4 * Copyright (C) 2002 Benjamin Herrenschmidt (benh@kernel.crashing.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Todo: - add support for the OF persistent properties
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/stddef.h>
17 #include <linux/string.h>
18 #include <linux/nvram.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/adb.h>
24 #include <linux/pmu.h>
25 #include <linux/bootmem.h>
26 #include <linux/completion.h>
27 #include <linux/spinlock.h>
28 #include <asm/sections.h>
30 #include <asm/system.h>
32 #include <asm/machdep.h>
33 #include <asm/nvram.h>
38 #define DBG(x...) printk(x)
43 #define NVRAM_SIZE 0x2000 /* 8kB of non-volatile RAM */
45 #define CORE99_SIGNATURE 0x5a
46 #define CORE99_ADLER_START 0x14
48 /* On Core99, nvram is either a sharp, a micron or an AMD flash */
49 #define SM_FLASH_STATUS_DONE 0x80
50 #define SM_FLASH_STATUS_ERR 0x38
51 #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
52 #define SM_FLASH_CMD_ERASE_SETUP 0x20
53 #define SM_FLASH_CMD_RESET 0xff
54 #define SM_FLASH_CMD_WRITE_SETUP 0x40
55 #define SM_FLASH_CMD_CLEAR_STATUS 0x50
56 #define SM_FLASH_CMD_READ_STATUS 0x70
58 /* CHRP NVRAM header */
67 struct core99_header {
68 struct chrp_header hdr;
75 * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
77 static int nvram_naddrs;
78 static volatile unsigned char *nvram_addr;
79 static volatile unsigned char *nvram_data;
80 static int nvram_mult, is_core_99;
81 static int core99_bank = 0;
82 static int nvram_partitions[3];
83 static DEFINE_SPINLOCK(nv_lock);
85 extern int pmac_newworld;
86 extern int system_running;
88 static int (*core99_write_bank)(int bank, u8* datas);
89 static int (*core99_erase_bank)(int bank);
91 static char *nvram_image __pmacdata;
94 static unsigned char __pmac core99_nvram_read_byte(int addr)
96 if (nvram_image == NULL)
98 return nvram_image[addr];
101 static void __pmac core99_nvram_write_byte(int addr, unsigned char val)
103 if (nvram_image == NULL)
105 nvram_image[addr] = val;
109 static unsigned char __openfirmware direct_nvram_read_byte(int addr)
111 return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);
114 static void __openfirmware direct_nvram_write_byte(int addr, unsigned char val)
116 out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);
120 static unsigned char __pmac indirect_nvram_read_byte(int addr)
125 spin_lock_irqsave(&nv_lock, flags);
126 out_8(nvram_addr, addr >> 5);
127 val = in_8(&nvram_data[(addr & 0x1f) << 4]);
128 spin_unlock_irqrestore(&nv_lock, flags);
133 static void __pmac indirect_nvram_write_byte(int addr, unsigned char val)
137 spin_lock_irqsave(&nv_lock, flags);
138 out_8(nvram_addr, addr >> 5);
139 out_8(&nvram_data[(addr & 0x1f) << 4], val);
140 spin_unlock_irqrestore(&nv_lock, flags);
144 #ifdef CONFIG_ADB_PMU
146 static void __pmac pmu_nvram_complete(struct adb_request *req)
149 complete((struct completion *)req->arg);
152 static unsigned char __pmac pmu_nvram_read_byte(int addr)
154 struct adb_request req;
155 DECLARE_COMPLETION(req_complete);
157 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
158 if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,
159 (addr >> 8) & 0xff, addr & 0xff))
161 if (system_state == SYSTEM_RUNNING)
162 wait_for_completion(&req_complete);
163 while (!req.complete)
168 static void __pmac pmu_nvram_write_byte(int addr, unsigned char val)
170 struct adb_request req;
171 DECLARE_COMPLETION(req_complete);
173 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
174 if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,
175 (addr >> 8) & 0xff, addr & 0xff, val))
177 if (system_state == SYSTEM_RUNNING)
178 wait_for_completion(&req_complete);
179 while (!req.complete)
183 #endif /* CONFIG_ADB_PMU */
186 static u8 __pmac chrp_checksum(struct chrp_header* hdr)
189 u16 sum = hdr->signature;
190 for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
193 sum = (sum & 0xFF) + (sum>>8);
197 static u32 __pmac core99_calc_adler(u8 *buffer)
202 buffer += CORE99_ADLER_START;
205 for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
206 if ((cnt % 5000) == 0) {
216 return (high << 16) | low;
219 static u32 __pmac core99_check(u8* datas)
221 struct core99_header* hdr99 = (struct core99_header*)datas;
223 if (hdr99->hdr.signature != CORE99_SIGNATURE) {
224 DBG("Invalid signature\n");
227 if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
228 DBG("Invalid checksum\n");
231 if (hdr99->adler != core99_calc_adler(datas)) {
232 DBG("Invalid adler\n");
235 return hdr99->generation;
238 static int __pmac sm_erase_bank(int bank)
241 unsigned long timeout;
243 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
245 DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
247 out_8(base, SM_FLASH_CMD_ERASE_SETUP);
248 out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
251 if (++timeout > 1000000) {
252 printk(KERN_ERR "nvram: Sharp/Miron flash erase timeout !\n");
255 out_8(base, SM_FLASH_CMD_READ_STATUS);
257 } while (!(stat & SM_FLASH_STATUS_DONE));
259 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
260 out_8(base, SM_FLASH_CMD_RESET);
262 for (i=0; i<NVRAM_SIZE; i++)
263 if (base[i] != 0xff) {
264 printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
270 static int __pmac sm_write_bank(int bank, u8* datas)
273 unsigned long timeout;
275 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
277 DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
279 for (i=0; i<NVRAM_SIZE; i++) {
280 out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
282 out_8(base+i, datas[i]);
285 if (++timeout > 1000000) {
286 printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
289 out_8(base, SM_FLASH_CMD_READ_STATUS);
291 } while (!(stat & SM_FLASH_STATUS_DONE));
292 if (!(stat & SM_FLASH_STATUS_DONE))
295 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
296 out_8(base, SM_FLASH_CMD_RESET);
297 for (i=0; i<NVRAM_SIZE; i++)
298 if (base[i] != datas[i]) {
299 printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
305 static int __pmac amd_erase_bank(int bank)
308 unsigned long timeout;
310 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
312 DBG("nvram: AMD Erasing bank %d...\n", bank);
315 out_8(base+0x555, 0xaa);
318 out_8(base+0x2aa, 0x55);
322 out_8(base+0x555, 0x80);
324 out_8(base+0x555, 0xaa);
326 out_8(base+0x2aa, 0x55);
333 if (++timeout > 1000000) {
334 printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
337 stat = in_8(base) ^ in_8(base);
344 for (i=0; i<NVRAM_SIZE; i++)
345 if (base[i] != 0xff) {
346 printk(KERN_ERR "nvram: AMD flash erase failed !\n");
352 static int __pmac amd_write_bank(int bank, u8* datas)
355 unsigned long timeout;
357 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
359 DBG("nvram: AMD Writing bank %d...\n", bank);
361 for (i=0; i<NVRAM_SIZE; i++) {
363 out_8(base+0x555, 0xaa);
366 out_8(base+0x2aa, 0x55);
369 /* Write single word */
370 out_8(base+0x555, 0xa0);
372 out_8(base+i, datas[i]);
376 if (++timeout > 1000000) {
377 printk(KERN_ERR "nvram: AMD flash write timeout !\n");
380 stat = in_8(base) ^ in_8(base);
390 for (i=0; i<NVRAM_SIZE; i++)
391 if (base[i] != datas[i]) {
392 printk(KERN_ERR "nvram: AMD flash write failed !\n");
398 static void __init lookup_partitions(void)
402 struct chrp_header* hdr;
405 nvram_partitions[pmac_nvram_OF] = -1;
406 nvram_partitions[pmac_nvram_XPRAM] = -1;
407 nvram_partitions[pmac_nvram_NR] = -1;
408 hdr = (struct chrp_header *)buffer;
414 buffer[i] = nvram_read_byte(offset+i);
415 if (!strcmp(hdr->name, "common"))
416 nvram_partitions[pmac_nvram_OF] = offset + 0x10;
417 if (!strcmp(hdr->name, "APL,MacOS75")) {
418 nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;
419 nvram_partitions[pmac_nvram_NR] = offset + 0x110;
421 offset += (hdr->len * 0x10);
422 } while(offset < NVRAM_SIZE);
424 nvram_partitions[pmac_nvram_OF] = 0x1800;
425 nvram_partitions[pmac_nvram_XPRAM] = 0x1300;
426 nvram_partitions[pmac_nvram_NR] = 0x1400;
428 DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);
429 DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);
430 DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);
433 static void __pmac core99_nvram_sync(void)
435 struct core99_header* hdr99;
438 if (!is_core_99 || !nvram_data || !nvram_image)
441 spin_lock_irqsave(&nv_lock, flags);
442 if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
446 DBG("Updating nvram...\n");
448 hdr99 = (struct core99_header*)nvram_image;
450 hdr99->hdr.signature = CORE99_SIGNATURE;
451 hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
452 hdr99->adler = core99_calc_adler(nvram_image);
453 core99_bank = core99_bank ? 0 : 1;
454 if (core99_erase_bank)
455 if (core99_erase_bank(core99_bank)) {
456 printk("nvram: Error erasing bank %d\n", core99_bank);
459 if (core99_write_bank)
460 if (core99_write_bank(core99_bank, nvram_image))
461 printk("nvram: Error writing bank %d\n", core99_bank);
463 spin_unlock_irqrestore(&nv_lock, flags);
470 void __init pmac_nvram_init(void)
472 struct device_node *dp;
476 dp = find_devices("nvram");
478 printk(KERN_ERR "Can't find NVRAM device\n");
481 nvram_naddrs = dp->n_addrs;
482 is_core_99 = device_is_compatible(dp, "nvram,flash");
485 u32 gen_bank0, gen_bank1;
487 if (nvram_naddrs < 1) {
488 printk(KERN_ERR "nvram: no address\n");
491 nvram_image = alloc_bootmem(NVRAM_SIZE);
492 if (nvram_image == NULL) {
493 printk(KERN_ERR "nvram: can't allocate ram image\n");
496 nvram_data = ioremap(dp->addrs[0].address, NVRAM_SIZE*2);
497 nvram_naddrs = 1; /* Make sure we get the correct case */
499 DBG("nvram: Checking bank 0...\n");
501 gen_bank0 = core99_check((u8 *)nvram_data);
502 gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
503 core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
505 DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
506 DBG("nvram: Active bank is: %d\n", core99_bank);
508 for (i=0; i<NVRAM_SIZE; i++)
509 nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
511 ppc_md.nvram_read_val = core99_nvram_read_byte;
512 ppc_md.nvram_write_val = core99_nvram_write_byte;
513 ppc_md.nvram_sync = core99_nvram_sync;
515 * Maybe we could be smarter here though making an exclusive list
516 * of known flash chips is a bit nasty as older OF didn't provide us
517 * with a useful "compatible" entry. A solution would be to really
518 * identify the chip using flash id commands and base ourselves on
519 * a list of known chips IDs
521 if (device_is_compatible(dp, "amd-0137")) {
522 core99_erase_bank = amd_erase_bank;
523 core99_write_bank = amd_write_bank;
525 core99_erase_bank = sm_erase_bank;
526 core99_write_bank = sm_write_bank;
528 } else if (_machine == _MACH_chrp && nvram_naddrs == 1) {
529 nvram_data = ioremap(dp->addrs[0].address + isa_mem_base,
532 ppc_md.nvram_read_val = direct_nvram_read_byte;
533 ppc_md.nvram_write_val = direct_nvram_write_byte;
534 } else if (nvram_naddrs == 1) {
535 nvram_data = ioremap(dp->addrs[0].address, dp->addrs[0].size);
536 nvram_mult = (dp->addrs[0].size + NVRAM_SIZE - 1) / NVRAM_SIZE;
537 ppc_md.nvram_read_val = direct_nvram_read_byte;
538 ppc_md.nvram_write_val = direct_nvram_write_byte;
539 } else if (nvram_naddrs == 2) {
540 nvram_addr = ioremap(dp->addrs[0].address, dp->addrs[0].size);
541 nvram_data = ioremap(dp->addrs[1].address, dp->addrs[1].size);
542 ppc_md.nvram_read_val = indirect_nvram_read_byte;
543 ppc_md.nvram_write_val = indirect_nvram_write_byte;
544 } else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {
545 #ifdef CONFIG_ADB_PMU
547 ppc_md.nvram_read_val = pmu_nvram_read_byte;
548 ppc_md.nvram_write_val = pmu_nvram_write_byte;
549 #endif /* CONFIG_ADB_PMU */
551 printk(KERN_ERR "Don't know how to access NVRAM with %d addresses\n",
557 int __pmac pmac_get_partition(int partition)
559 return nvram_partitions[partition];
562 u8 __pmac pmac_xpram_read(int xpaddr)
564 int offset = nvram_partitions[pmac_nvram_XPRAM];
569 return ppc_md.nvram_read_val(xpaddr + offset);
572 void __pmac pmac_xpram_write(int xpaddr, u8 data)
574 int offset = nvram_partitions[pmac_nvram_XPRAM];
579 ppc_md.nvram_write_val(xpaddr + offset, data);
582 EXPORT_SYMBOL(pmac_get_partition);
583 EXPORT_SYMBOL(pmac_xpram_read);
584 EXPORT_SYMBOL(pmac_xpram_write);