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
52 #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
53 #define SM_FLASH_CMD_ERASE_SETUP 0x20
54 #define SM_FLASH_CMD_RESET 0xff
55 #define SM_FLASH_CMD_WRITE_SETUP 0x40
56 #define SM_FLASH_CMD_CLEAR_STATUS 0x50
57 #define SM_FLASH_CMD_READ_STATUS 0x70
59 /* CHRP NVRAM header */
68 struct core99_header {
69 struct chrp_header hdr;
76 * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
78 static int nvram_naddrs;
79 static volatile unsigned char *nvram_data;
80 static int is_core_99;
81 static int core99_bank = 0;
82 static int nvram_partitions[3];
83 // XXX Turn that into a sem
84 static DEFINE_SPINLOCK(nv_lock);
86 extern int pmac_newworld;
87 extern int system_running;
89 static int (*core99_write_bank)(int bank, u8* datas);
90 static int (*core99_erase_bank)(int bank);
92 static char *nvram_image;
95 static unsigned char core99_nvram_read_byte(int addr)
97 if (nvram_image == NULL)
99 return nvram_image[addr];
102 static void core99_nvram_write_byte(int addr, unsigned char val)
104 if (nvram_image == NULL)
106 nvram_image[addr] = val;
109 static ssize_t core99_nvram_read(char *buf, size_t count, loff_t *index)
113 if (nvram_image == NULL)
115 if (*index > NVRAM_SIZE)
119 if (i + count > NVRAM_SIZE)
120 count = NVRAM_SIZE - i;
122 memcpy(buf, &nvram_image[i], count);
127 static ssize_t core99_nvram_write(char *buf, size_t count, loff_t *index)
131 if (nvram_image == NULL)
133 if (*index > NVRAM_SIZE)
137 if (i + count > NVRAM_SIZE)
138 count = NVRAM_SIZE - i;
140 memcpy(&nvram_image[i], buf, count);
145 static ssize_t core99_nvram_size(void)
147 if (nvram_image == NULL)
153 static volatile unsigned char *nvram_addr;
154 static int nvram_mult;
156 static unsigned char direct_nvram_read_byte(int addr)
158 return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);
161 static void direct_nvram_write_byte(int addr, unsigned char val)
163 out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);
167 static unsigned char indirect_nvram_read_byte(int addr)
172 spin_lock_irqsave(&nv_lock, flags);
173 out_8(nvram_addr, addr >> 5);
174 val = in_8(&nvram_data[(addr & 0x1f) << 4]);
175 spin_unlock_irqrestore(&nv_lock, flags);
180 static void indirect_nvram_write_byte(int addr, unsigned char val)
184 spin_lock_irqsave(&nv_lock, flags);
185 out_8(nvram_addr, addr >> 5);
186 out_8(&nvram_data[(addr & 0x1f) << 4], val);
187 spin_unlock_irqrestore(&nv_lock, flags);
191 #ifdef CONFIG_ADB_PMU
193 static void pmu_nvram_complete(struct adb_request *req)
196 complete((struct completion *)req->arg);
199 static unsigned char pmu_nvram_read_byte(int addr)
201 struct adb_request req;
202 DECLARE_COMPLETION(req_complete);
204 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
205 if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,
206 (addr >> 8) & 0xff, addr & 0xff))
208 if (system_state == SYSTEM_RUNNING)
209 wait_for_completion(&req_complete);
210 while (!req.complete)
215 static void pmu_nvram_write_byte(int addr, unsigned char val)
217 struct adb_request req;
218 DECLARE_COMPLETION(req_complete);
220 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
221 if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,
222 (addr >> 8) & 0xff, addr & 0xff, val))
224 if (system_state == SYSTEM_RUNNING)
225 wait_for_completion(&req_complete);
226 while (!req.complete)
230 #endif /* CONFIG_ADB_PMU */
231 #endif /* CONFIG_PPC32 */
233 static u8 chrp_checksum(struct chrp_header* hdr)
236 u16 sum = hdr->signature;
237 for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
240 sum = (sum & 0xFF) + (sum>>8);
244 static u32 core99_calc_adler(u8 *buffer)
249 buffer += CORE99_ADLER_START;
252 for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
253 if ((cnt % 5000) == 0) {
263 return (high << 16) | low;
266 static u32 core99_check(u8* datas)
268 struct core99_header* hdr99 = (struct core99_header*)datas;
270 if (hdr99->hdr.signature != CORE99_SIGNATURE) {
271 DBG("Invalid signature\n");
274 if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
275 DBG("Invalid checksum\n");
278 if (hdr99->adler != core99_calc_adler(datas)) {
279 DBG("Invalid adler\n");
282 return hdr99->generation;
285 static int sm_erase_bank(int bank)
288 unsigned long timeout;
290 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
292 DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
294 out_8(base, SM_FLASH_CMD_ERASE_SETUP);
295 out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
298 if (++timeout > 1000000) {
299 printk(KERN_ERR "nvram: Sharp/Micron flash erase timeout !\n");
302 out_8(base, SM_FLASH_CMD_READ_STATUS);
304 } while (!(stat & SM_FLASH_STATUS_DONE));
306 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
307 out_8(base, SM_FLASH_CMD_RESET);
309 for (i=0; i<NVRAM_SIZE; i++)
310 if (base[i] != 0xff) {
311 printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
317 static int sm_write_bank(int bank, u8* datas)
320 unsigned long timeout;
322 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
324 DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
326 for (i=0; i<NVRAM_SIZE; i++) {
327 out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
329 out_8(base+i, datas[i]);
332 if (++timeout > 1000000) {
333 printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
336 out_8(base, SM_FLASH_CMD_READ_STATUS);
338 } while (!(stat & SM_FLASH_STATUS_DONE));
339 if (!(stat & SM_FLASH_STATUS_DONE))
342 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
343 out_8(base, SM_FLASH_CMD_RESET);
344 for (i=0; i<NVRAM_SIZE; i++)
345 if (base[i] != datas[i]) {
346 printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
352 static int amd_erase_bank(int bank)
355 unsigned long timeout;
357 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
359 DBG("nvram: AMD Erasing bank %d...\n", bank);
362 out_8(base+0x555, 0xaa);
365 out_8(base+0x2aa, 0x55);
369 out_8(base+0x555, 0x80);
371 out_8(base+0x555, 0xaa);
373 out_8(base+0x2aa, 0x55);
380 if (++timeout > 1000000) {
381 printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
384 stat = in_8(base) ^ in_8(base);
391 for (i=0; i<NVRAM_SIZE; i++)
392 if (base[i] != 0xff) {
393 printk(KERN_ERR "nvram: AMD flash erase failed !\n");
399 static int amd_write_bank(int bank, u8* datas)
402 unsigned long timeout;
404 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
406 DBG("nvram: AMD Writing bank %d...\n", bank);
408 for (i=0; i<NVRAM_SIZE; i++) {
410 out_8(base+0x555, 0xaa);
413 out_8(base+0x2aa, 0x55);
416 /* Write single word */
417 out_8(base+0x555, 0xa0);
419 out_8(base+i, datas[i]);
423 if (++timeout > 1000000) {
424 printk(KERN_ERR "nvram: AMD flash write timeout !\n");
427 stat = in_8(base) ^ in_8(base);
437 for (i=0; i<NVRAM_SIZE; i++)
438 if (base[i] != datas[i]) {
439 printk(KERN_ERR "nvram: AMD flash write failed !\n");
445 static void __init lookup_partitions(void)
449 struct chrp_header* hdr;
452 nvram_partitions[pmac_nvram_OF] = -1;
453 nvram_partitions[pmac_nvram_XPRAM] = -1;
454 nvram_partitions[pmac_nvram_NR] = -1;
455 hdr = (struct chrp_header *)buffer;
461 buffer[i] = ppc_md.nvram_read_val(offset+i);
462 if (!strcmp(hdr->name, "common"))
463 nvram_partitions[pmac_nvram_OF] = offset + 0x10;
464 if (!strcmp(hdr->name, "APL,MacOS75")) {
465 nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;
466 nvram_partitions[pmac_nvram_NR] = offset + 0x110;
468 offset += (hdr->len * 0x10);
469 } while(offset < NVRAM_SIZE);
471 nvram_partitions[pmac_nvram_OF] = 0x1800;
472 nvram_partitions[pmac_nvram_XPRAM] = 0x1300;
473 nvram_partitions[pmac_nvram_NR] = 0x1400;
475 DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);
476 DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);
477 DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);
480 static void core99_nvram_sync(void)
482 struct core99_header* hdr99;
485 if (!is_core_99 || !nvram_data || !nvram_image)
488 spin_lock_irqsave(&nv_lock, flags);
489 if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
493 DBG("Updating nvram...\n");
495 hdr99 = (struct core99_header*)nvram_image;
497 hdr99->hdr.signature = CORE99_SIGNATURE;
498 hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
499 hdr99->adler = core99_calc_adler(nvram_image);
500 core99_bank = core99_bank ? 0 : 1;
501 if (core99_erase_bank)
502 if (core99_erase_bank(core99_bank)) {
503 printk("nvram: Error erasing bank %d\n", core99_bank);
506 if (core99_write_bank)
507 if (core99_write_bank(core99_bank, nvram_image))
508 printk("nvram: Error writing bank %d\n", core99_bank);
510 spin_unlock_irqrestore(&nv_lock, flags);
517 static int __init core99_nvram_setup(struct device_node *dp)
520 u32 gen_bank0, gen_bank1;
522 if (nvram_naddrs < 1) {
523 printk(KERN_ERR "nvram: no address\n");
526 nvram_image = alloc_bootmem(NVRAM_SIZE);
527 if (nvram_image == NULL) {
528 printk(KERN_ERR "nvram: can't allocate ram image\n");
531 nvram_data = ioremap(dp->addrs[0].address, NVRAM_SIZE*2);
532 nvram_naddrs = 1; /* Make sure we get the correct case */
534 DBG("nvram: Checking bank 0...\n");
536 gen_bank0 = core99_check((u8 *)nvram_data);
537 gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
538 core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
540 DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
541 DBG("nvram: Active bank is: %d\n", core99_bank);
543 for (i=0; i<NVRAM_SIZE; i++)
544 nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
546 ppc_md.nvram_read_val = core99_nvram_read_byte;
547 ppc_md.nvram_write_val = core99_nvram_write_byte;
548 ppc_md.nvram_read = core99_nvram_read;
549 ppc_md.nvram_write = core99_nvram_write;
550 ppc_md.nvram_size = core99_nvram_size;
551 ppc_md.nvram_sync = core99_nvram_sync;
553 * Maybe we could be smarter here though making an exclusive list
554 * of known flash chips is a bit nasty as older OF didn't provide us
555 * with a useful "compatible" entry. A solution would be to really
556 * identify the chip using flash id commands and base ourselves on
557 * a list of known chips IDs
559 if (device_is_compatible(dp, "amd-0137")) {
560 core99_erase_bank = amd_erase_bank;
561 core99_write_bank = amd_write_bank;
563 core99_erase_bank = sm_erase_bank;
564 core99_write_bank = sm_write_bank;
569 int __init pmac_nvram_init(void)
571 struct device_node *dp;
576 dp = find_devices("nvram");
578 printk(KERN_ERR "Can't find NVRAM device\n");
581 nvram_naddrs = dp->n_addrs;
582 is_core_99 = device_is_compatible(dp, "nvram,flash");
584 err = core99_nvram_setup(dp);
586 else if (_machine == _MACH_chrp && nvram_naddrs == 1) {
587 nvram_data = ioremap(dp->addrs[0].address + isa_mem_base,
590 ppc_md.nvram_read_val = direct_nvram_read_byte;
591 ppc_md.nvram_write_val = direct_nvram_write_byte;
592 } else if (nvram_naddrs == 1) {
593 nvram_data = ioremap(dp->addrs[0].address, dp->addrs[0].size);
594 nvram_mult = (dp->addrs[0].size + NVRAM_SIZE - 1) / NVRAM_SIZE;
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 == 2) {
598 nvram_addr = ioremap(dp->addrs[0].address, dp->addrs[0].size);
599 nvram_data = ioremap(dp->addrs[1].address, dp->addrs[1].size);
600 ppc_md.nvram_read_val = indirect_nvram_read_byte;
601 ppc_md.nvram_write_val = indirect_nvram_write_byte;
602 } else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {
603 #ifdef CONFIG_ADB_PMU
605 ppc_md.nvram_read_val = pmu_nvram_read_byte;
606 ppc_md.nvram_write_val = pmu_nvram_write_byte;
607 #endif /* CONFIG_ADB_PMU */
611 printk(KERN_ERR "Incompatible type of NVRAM\n");
618 int pmac_get_partition(int partition)
620 return nvram_partitions[partition];
623 u8 pmac_xpram_read(int xpaddr)
625 int offset = pmac_get_partition(pmac_nvram_XPRAM);
627 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
630 return ppc_md.nvram_read_val(xpaddr + offset);
633 void pmac_xpram_write(int xpaddr, u8 data)
635 int offset = pmac_get_partition(pmac_nvram_XPRAM);
637 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
640 ppc_md.nvram_write_val(xpaddr + offset, data);
643 EXPORT_SYMBOL(pmac_get_partition);
644 EXPORT_SYMBOL(pmac_xpram_read);
645 EXPORT_SYMBOL(pmac_xpram_write);