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/export.h>
12 #include <linux/kernel.h>
13 #include <linux/stddef.h>
14 #include <linux/string.h>
15 #include <linux/nvram.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/adb.h>
20 #include <linux/pmu.h>
21 #include <linux/memblock.h>
22 #include <linux/completion.h>
23 #include <linux/spinlock.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 /* 8kB of non-volatile RAM */
42 #define CORE99_SIGNATURE 0x5a
43 #define CORE99_ADLER_START 0x14
45 /* On Core99, nvram is either a sharp, a micron or an AMD flash */
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
56 /* CHRP NVRAM header */
65 struct core99_header {
66 struct chrp_header hdr;
73 * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
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];
80 // XXX Turn that into a sem
81 static DEFINE_RAW_SPINLOCK(nv_lock);
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)
100 nvram_image[addr] = val;
103 static ssize_t core99_nvram_read(char *buf, size_t count, loff_t *index)
107 if (nvram_image == NULL)
109 if (*index > NVRAM_SIZE)
113 if (i + count > NVRAM_SIZE)
114 count = NVRAM_SIZE - i;
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)
127 if (*index > NVRAM_SIZE)
131 if (i + count > NVRAM_SIZE)
132 count = NVRAM_SIZE - i;
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)
152 return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);
155 static void direct_nvram_write_byte(int addr, unsigned char val)
157 out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);
161 static unsigned char indirect_nvram_read_byte(int addr)
166 raw_spin_lock_irqsave(&nv_lock, flags);
167 out_8(nvram_addr, addr >> 5);
168 val = in_8(&nvram_data[(addr & 0x1f) << 4]);
169 raw_spin_unlock_irqrestore(&nv_lock, flags);
174 static void indirect_nvram_write_byte(int addr, unsigned char val)
178 raw_spin_lock_irqsave(&nv_lock, flags);
179 out_8(nvram_addr, addr >> 5);
180 out_8(&nvram_data[(addr & 0x1f) << 4], val);
181 raw_spin_unlock_irqrestore(&nv_lock, flags);
185 #ifdef CONFIG_ADB_PMU
187 static void pmu_nvram_complete(struct adb_request *req)
190 complete((struct completion *)req->arg);
193 static unsigned char pmu_nvram_read_byte(int addr)
195 struct adb_request req;
196 DECLARE_COMPLETION_ONSTACK(req_complete);
198 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
199 if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,
200 (addr >> 8) & 0xff, addr & 0xff))
202 if (system_state == SYSTEM_RUNNING)
203 wait_for_completion(&req_complete);
204 while (!req.complete)
209 static void pmu_nvram_write_byte(int addr, unsigned char val)
211 struct adb_request req;
212 DECLARE_COMPLETION_ONSTACK(req_complete);
214 req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
215 if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,
216 (addr >> 8) & 0xff, addr & 0xff, val))
218 if (system_state == SYSTEM_RUNNING)
219 wait_for_completion(&req_complete);
220 while (!req.complete)
224 #endif /* CONFIG_ADB_PMU */
225 #endif /* CONFIG_PPC32 */
227 static u8 chrp_checksum(struct chrp_header* hdr)
230 u16 sum = hdr->signature;
231 for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
234 sum = (sum & 0xFF) + (sum>>8);
238 static u32 core99_calc_adler(u8 *buffer)
243 buffer += CORE99_ADLER_START;
246 for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
247 if ((cnt % 5000) == 0) {
257 return (high << 16) | low;
260 static u32 core99_check(u8* datas)
262 struct core99_header* hdr99 = (struct core99_header*)datas;
264 if (hdr99->hdr.signature != CORE99_SIGNATURE) {
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");
276 return hdr99->generation;
279 static int sm_erase_bank(int bank)
282 unsigned long timeout;
284 u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
286 DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
288 out_8(base, SM_FLASH_CMD_ERASE_SETUP);
289 out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
292 if (++timeout > 1000000) {
293 printk(KERN_ERR "nvram: Sharp/Micron flash erase timeout !\n");
296 out_8(base, SM_FLASH_CMD_READ_STATUS);
298 } while (!(stat & SM_FLASH_STATUS_DONE));
300 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
301 out_8(base, SM_FLASH_CMD_RESET);
303 if (memchr_inv(base, 0xff, NVRAM_SIZE)) {
304 printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
310 static int sm_write_bank(int bank, u8* datas)
313 unsigned long timeout;
315 u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
317 DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
319 for (i=0; i<NVRAM_SIZE; i++) {
320 out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
322 out_8(base+i, datas[i]);
325 if (++timeout > 1000000) {
326 printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
329 out_8(base, SM_FLASH_CMD_READ_STATUS);
331 } while (!(stat & SM_FLASH_STATUS_DONE));
332 if (!(stat & SM_FLASH_STATUS_DONE))
335 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
336 out_8(base, SM_FLASH_CMD_RESET);
337 if (memcmp(base, datas, NVRAM_SIZE)) {
338 printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
344 static int amd_erase_bank(int bank)
347 unsigned long timeout;
349 u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
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) {
373 printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
376 stat = in_8(base) ^ in_8(base);
383 if (memchr_inv(base, 0xff, NVRAM_SIZE)) {
384 printk(KERN_ERR "nvram: AMD flash erase failed !\n");
390 static int amd_write_bank(int bank, u8* datas)
393 unsigned long timeout;
395 u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
397 DBG("nvram: AMD Writing bank %d...\n", bank);
399 for (i=0; i<NVRAM_SIZE; i++) {
401 out_8(base+0x555, 0xaa);
404 out_8(base+0x2aa, 0x55);
407 /* Write single word */
408 out_8(base+0x555, 0xa0);
410 out_8(base+i, datas[i]);
414 if (++timeout > 1000000) {
415 printk(KERN_ERR "nvram: AMD flash write timeout !\n");
418 stat = in_8(base) ^ in_8(base);
428 if (memcmp(base, datas, NVRAM_SIZE)) {
429 printk(KERN_ERR "nvram: AMD flash write failed !\n");
435 static void __init lookup_partitions(void)
439 struct chrp_header* hdr;
442 nvram_partitions[pmac_nvram_OF] = -1;
443 nvram_partitions[pmac_nvram_XPRAM] = -1;
444 nvram_partitions[pmac_nvram_NR] = -1;
445 hdr = (struct chrp_header *)buffer;
451 buffer[i] = ppc_md.nvram_read_val(offset+i);
452 if (!strcmp(hdr->name, "common"))
453 nvram_partitions[pmac_nvram_OF] = offset + 0x10;
454 if (!strcmp(hdr->name, "APL,MacOS75")) {
455 nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;
456 nvram_partitions[pmac_nvram_NR] = offset + 0x110;
458 offset += (hdr->len * 0x10);
459 } while(offset < NVRAM_SIZE);
461 nvram_partitions[pmac_nvram_OF] = 0x1800;
462 nvram_partitions[pmac_nvram_XPRAM] = 0x1300;
463 nvram_partitions[pmac_nvram_NR] = 0x1400;
465 DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);
466 DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);
467 DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);
470 static void core99_nvram_sync(void)
472 struct core99_header* hdr99;
475 if (!is_core_99 || !nvram_data || !nvram_image)
478 raw_spin_lock_irqsave(&nv_lock, flags);
479 if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
483 DBG("Updating nvram...\n");
485 hdr99 = (struct core99_header*)nvram_image;
487 hdr99->hdr.signature = CORE99_SIGNATURE;
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);
500 raw_spin_unlock_irqrestore(&nv_lock, flags);
507 static int __init core99_nvram_setup(struct device_node *dp, unsigned long addr)
510 u32 gen_bank0, gen_bank1;
512 if (nvram_naddrs < 1) {
513 printk(KERN_ERR "nvram: no address\n");
516 nvram_image = memblock_alloc(NVRAM_SIZE, SMP_CACHE_BYTES);
517 nvram_data = ioremap(addr, NVRAM_SIZE*2);
518 nvram_naddrs = 1; /* Make sure we get the correct case */
520 DBG("nvram: Checking bank 0...\n");
522 gen_bank0 = core99_check((u8 *)nvram_data);
523 gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
524 core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
526 DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
527 DBG("nvram: Active bank is: %d\n", core99_bank);
529 for (i=0; i<NVRAM_SIZE; i++)
530 nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
532 ppc_md.nvram_read_val = core99_nvram_read_byte;
533 ppc_md.nvram_write_val = core99_nvram_write_byte;
534 ppc_md.nvram_read = core99_nvram_read;
535 ppc_md.nvram_write = core99_nvram_write;
536 ppc_md.nvram_size = core99_nvram_size;
537 ppc_md.nvram_sync = core99_nvram_sync;
538 ppc_md.machine_shutdown = core99_nvram_sync;
540 * Maybe we could be smarter here though making an exclusive list
541 * of known flash chips is a bit nasty as older OF didn't provide us
542 * with a useful "compatible" entry. A solution would be to really
543 * identify the chip using flash id commands and base ourselves on
544 * a list of known chips IDs
546 if (of_device_is_compatible(dp, "amd-0137")) {
547 core99_erase_bank = amd_erase_bank;
548 core99_write_bank = amd_write_bank;
550 core99_erase_bank = sm_erase_bank;
551 core99_write_bank = sm_write_bank;
556 int __init pmac_nvram_init(void)
558 struct device_node *dp;
559 struct resource r1, r2;
560 unsigned int s1 = 0, s2 = 0;
565 dp = of_find_node_by_name(NULL, "nvram");
567 printk(KERN_ERR "Can't find NVRAM device\n");
571 /* Try to obtain an address */
572 if (of_address_to_resource(dp, 0, &r1) == 0) {
574 s1 = resource_size(&r1);
575 if (of_address_to_resource(dp, 1, &r2) == 0) {
577 s2 = resource_size(&r2);
581 is_core_99 = of_device_is_compatible(dp, "nvram,flash");
583 err = core99_nvram_setup(dp, r1.start);
588 if (machine_is(chrp) && nvram_naddrs == 1) {
589 nvram_data = ioremap(r1.start, s1);
591 ppc_md.nvram_read_val = direct_nvram_read_byte;
592 ppc_md.nvram_write_val = direct_nvram_write_byte;
593 } else if (nvram_naddrs == 1) {
594 nvram_data = ioremap(r1.start, s1);
595 nvram_mult = (s1 + NVRAM_SIZE - 1) / NVRAM_SIZE;
596 ppc_md.nvram_read_val = direct_nvram_read_byte;
597 ppc_md.nvram_write_val = direct_nvram_write_byte;
598 } else if (nvram_naddrs == 2) {
599 nvram_addr = ioremap(r1.start, s1);
600 nvram_data = ioremap(r2.start, s2);
601 ppc_md.nvram_read_val = indirect_nvram_read_byte;
602 ppc_md.nvram_write_val = indirect_nvram_write_byte;
603 } else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {
604 #ifdef CONFIG_ADB_PMU
606 ppc_md.nvram_read_val = pmu_nvram_read_byte;
607 ppc_md.nvram_write_val = pmu_nvram_write_byte;
608 #endif /* CONFIG_ADB_PMU */
610 printk(KERN_ERR "Incompatible type of NVRAM\n");
613 #endif /* CONFIG_PPC32 */
621 int pmac_get_partition(int partition)
623 return nvram_partitions[partition];
626 u8 pmac_xpram_read(int xpaddr)
628 int offset = pmac_get_partition(pmac_nvram_XPRAM);
630 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
633 return ppc_md.nvram_read_val(xpaddr + offset);
636 void pmac_xpram_write(int xpaddr, u8 data)
638 int offset = pmac_get_partition(pmac_nvram_XPRAM);
640 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
643 ppc_md.nvram_write_val(xpaddr + offset, data);
646 EXPORT_SYMBOL(pmac_get_partition);
647 EXPORT_SYMBOL(pmac_xpram_read);
648 EXPORT_SYMBOL(pmac_xpram_write);