Linux-2.6.12-rc2
[linux-2.6/next.git] / arch / ppc64 / kernel / pmac_nvram.c
blobe32a902236e3eeaef4801fc9088d0648330c1ec7
1 /*
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/init.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/bootmem.h>
23 #include <linux/completion.h>
24 #include <linux/spinlock.h>
25 #include <asm/sections.h>
26 #include <asm/io.h>
27 #include <asm/system.h>
28 #include <asm/prom.h>
29 #include <asm/machdep.h>
30 #include <asm/nvram.h>
32 #define DEBUG
34 #ifdef DEBUG
35 #define DBG(x...) printk(x)
36 #else
37 #define DBG(x...)
38 #endif
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 */
57 struct chrp_header {
58 u8 signature;
59 u8 cksum;
60 u16 len;
61 char name[12];
62 u8 data[0];
65 struct core99_header {
66 struct chrp_header hdr;
67 u32 adler;
68 u32 generation;
69 u32 reserved[2];
73 * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
75 static volatile unsigned char *nvram_data;
76 static int core99_bank = 0;
77 // XXX Turn that into a sem
78 static DEFINE_SPINLOCK(nv_lock);
80 extern int system_running;
82 static int (*core99_write_bank)(int bank, u8* datas);
83 static int (*core99_erase_bank)(int bank);
85 static char *nvram_image __pmacdata;
88 static ssize_t __pmac core99_nvram_read(char *buf, size_t count, loff_t *index)
90 int i;
92 if (nvram_image == NULL)
93 return -ENODEV;
94 if (*index > NVRAM_SIZE)
95 return 0;
97 i = *index;
98 if (i + count > NVRAM_SIZE)
99 count = NVRAM_SIZE - i;
101 memcpy(buf, &nvram_image[i], count);
102 *index = i + count;
103 return count;
106 static ssize_t __pmac core99_nvram_write(char *buf, size_t count, loff_t *index)
108 int i;
110 if (nvram_image == NULL)
111 return -ENODEV;
112 if (*index > NVRAM_SIZE)
113 return 0;
115 i = *index;
116 if (i + count > NVRAM_SIZE)
117 count = NVRAM_SIZE - i;
119 memcpy(&nvram_image[i], buf, count);
120 *index = i + count;
121 return count;
124 static ssize_t __pmac core99_nvram_size(void)
126 if (nvram_image == NULL)
127 return -ENODEV;
128 return NVRAM_SIZE;
131 static u8 __pmac chrp_checksum(struct chrp_header* hdr)
133 u8 *ptr;
134 u16 sum = hdr->signature;
135 for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
136 sum += *ptr;
137 while (sum > 0xFF)
138 sum = (sum & 0xFF) + (sum>>8);
139 return sum;
142 static u32 __pmac core99_calc_adler(u8 *buffer)
144 int cnt;
145 u32 low, high;
147 buffer += CORE99_ADLER_START;
148 low = 1;
149 high = 0;
150 for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
151 if ((cnt % 5000) == 0) {
152 high %= 65521UL;
153 high %= 65521UL;
155 low += buffer[cnt];
156 high += low;
158 low %= 65521UL;
159 high %= 65521UL;
161 return (high << 16) | low;
164 static u32 __pmac core99_check(u8* datas)
166 struct core99_header* hdr99 = (struct core99_header*)datas;
168 if (hdr99->hdr.signature != CORE99_SIGNATURE) {
169 DBG("Invalid signature\n");
170 return 0;
172 if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
173 DBG("Invalid checksum\n");
174 return 0;
176 if (hdr99->adler != core99_calc_adler(datas)) {
177 DBG("Invalid adler\n");
178 return 0;
180 return hdr99->generation;
183 static int __pmac sm_erase_bank(int bank)
185 int stat, i;
186 unsigned long timeout;
188 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
190 DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
192 out_8(base, SM_FLASH_CMD_ERASE_SETUP);
193 out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
194 timeout = 0;
195 do {
196 if (++timeout > 1000000) {
197 printk(KERN_ERR "nvram: Sharp/Miron flash erase timeout !\n");
198 break;
200 out_8(base, SM_FLASH_CMD_READ_STATUS);
201 stat = in_8(base);
202 } while (!(stat & SM_FLASH_STATUS_DONE));
204 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
205 out_8(base, SM_FLASH_CMD_RESET);
207 for (i=0; i<NVRAM_SIZE; i++)
208 if (base[i] != 0xff) {
209 printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
210 return -ENXIO;
212 return 0;
215 static int __pmac sm_write_bank(int bank, u8* datas)
217 int i, stat = 0;
218 unsigned long timeout;
220 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
222 DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
224 for (i=0; i<NVRAM_SIZE; i++) {
225 out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
226 udelay(1);
227 out_8(base+i, datas[i]);
228 timeout = 0;
229 do {
230 if (++timeout > 1000000) {
231 printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
232 break;
234 out_8(base, SM_FLASH_CMD_READ_STATUS);
235 stat = in_8(base);
236 } while (!(stat & SM_FLASH_STATUS_DONE));
237 if (!(stat & SM_FLASH_STATUS_DONE))
238 break;
240 out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
241 out_8(base, SM_FLASH_CMD_RESET);
242 for (i=0; i<NVRAM_SIZE; i++)
243 if (base[i] != datas[i]) {
244 printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
245 return -ENXIO;
247 return 0;
250 static int __pmac amd_erase_bank(int bank)
252 int i, stat = 0;
253 unsigned long timeout;
255 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
257 DBG("nvram: AMD Erasing bank %d...\n", bank);
259 /* Unlock 1 */
260 out_8(base+0x555, 0xaa);
261 udelay(1);
262 /* Unlock 2 */
263 out_8(base+0x2aa, 0x55);
264 udelay(1);
266 /* Sector-Erase */
267 out_8(base+0x555, 0x80);
268 udelay(1);
269 out_8(base+0x555, 0xaa);
270 udelay(1);
271 out_8(base+0x2aa, 0x55);
272 udelay(1);
273 out_8(base, 0x30);
274 udelay(1);
276 timeout = 0;
277 do {
278 if (++timeout > 1000000) {
279 printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
280 break;
282 stat = in_8(base) ^ in_8(base);
283 } while (stat != 0);
285 /* Reset */
286 out_8(base, 0xf0);
287 udelay(1);
289 for (i=0; i<NVRAM_SIZE; i++)
290 if (base[i] != 0xff) {
291 printk(KERN_ERR "nvram: AMD flash erase failed !\n");
292 return -ENXIO;
294 return 0;
297 static int __pmac amd_write_bank(int bank, u8* datas)
299 int i, stat = 0;
300 unsigned long timeout;
302 u8* base = (u8 *)nvram_data + core99_bank*NVRAM_SIZE;
304 DBG("nvram: AMD Writing bank %d...\n", bank);
306 for (i=0; i<NVRAM_SIZE; i++) {
307 /* Unlock 1 */
308 out_8(base+0x555, 0xaa);
309 udelay(1);
310 /* Unlock 2 */
311 out_8(base+0x2aa, 0x55);
312 udelay(1);
314 /* Write single word */
315 out_8(base+0x555, 0xa0);
316 udelay(1);
317 out_8(base+i, datas[i]);
319 timeout = 0;
320 do {
321 if (++timeout > 1000000) {
322 printk(KERN_ERR "nvram: AMD flash write timeout !\n");
323 break;
325 stat = in_8(base) ^ in_8(base);
326 } while (stat != 0);
327 if (stat != 0)
328 break;
331 /* Reset */
332 out_8(base, 0xf0);
333 udelay(1);
335 for (i=0; i<NVRAM_SIZE; i++)
336 if (base[i] != datas[i]) {
337 printk(KERN_ERR "nvram: AMD flash write failed !\n");
338 return -ENXIO;
340 return 0;
344 static int __pmac core99_nvram_sync(void)
346 struct core99_header* hdr99;
347 unsigned long flags;
349 spin_lock_irqsave(&nv_lock, flags);
350 if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
351 NVRAM_SIZE))
352 goto bail;
354 DBG("Updating nvram...\n");
356 hdr99 = (struct core99_header*)nvram_image;
357 hdr99->generation++;
358 hdr99->hdr.signature = CORE99_SIGNATURE;
359 hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
360 hdr99->adler = core99_calc_adler(nvram_image);
361 core99_bank = core99_bank ? 0 : 1;
362 if (core99_erase_bank)
363 if (core99_erase_bank(core99_bank)) {
364 printk("nvram: Error erasing bank %d\n", core99_bank);
365 goto bail;
367 if (core99_write_bank)
368 if (core99_write_bank(core99_bank, nvram_image))
369 printk("nvram: Error writing bank %d\n", core99_bank);
370 bail:
371 spin_unlock_irqrestore(&nv_lock, flags);
373 return 0;
376 int __init pmac_nvram_init(void)
378 struct device_node *dp;
379 u32 gen_bank0, gen_bank1;
380 int i;
382 dp = find_devices("nvram");
383 if (dp == NULL) {
384 printk(KERN_ERR "Can't find NVRAM device\n");
385 return -ENODEV;
387 if (!device_is_compatible(dp, "nvram,flash")) {
388 printk(KERN_ERR "Incompatible type of NVRAM\n");
389 return -ENXIO;
392 nvram_image = alloc_bootmem(NVRAM_SIZE);
393 if (nvram_image == NULL) {
394 printk(KERN_ERR "nvram: can't allocate ram image\n");
395 return -ENOMEM;
397 nvram_data = ioremap(dp->addrs[0].address, NVRAM_SIZE*2);
399 DBG("nvram: Checking bank 0...\n");
401 gen_bank0 = core99_check((u8 *)nvram_data);
402 gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
403 core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
405 DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
406 DBG("nvram: Active bank is: %d\n", core99_bank);
408 for (i=0; i<NVRAM_SIZE; i++)
409 nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
411 ppc_md.nvram_read = core99_nvram_read;
412 ppc_md.nvram_write = core99_nvram_write;
413 ppc_md.nvram_size = core99_nvram_size;
414 ppc_md.nvram_sync = core99_nvram_sync;
417 * Maybe we could be smarter here though making an exclusive list
418 * of known flash chips is a bit nasty as older OF didn't provide us
419 * with a useful "compatible" entry. A solution would be to really
420 * identify the chip using flash id commands and base ourselves on
421 * a list of known chips IDs
423 if (device_is_compatible(dp, "amd-0137")) {
424 core99_erase_bank = amd_erase_bank;
425 core99_write_bank = amd_write_bank;
426 } else {
427 core99_erase_bank = sm_erase_bank;
428 core99_write_bank = sm_write_bank;
431 return 0;
434 int __pmac pmac_get_partition(int partition)
436 struct nvram_partition *part;
437 const char *name;
438 int sig;
440 switch(partition) {
441 case pmac_nvram_OF:
442 name = "common";
443 sig = NVRAM_SIG_SYS;
444 break;
445 case pmac_nvram_XPRAM:
446 name = "APL,MacOS75";
447 sig = NVRAM_SIG_OS;
448 break;
449 case pmac_nvram_NR:
450 default:
451 /* Oldworld stuff */
452 return -ENODEV;
455 part = nvram_find_partition(sig, name);
456 if (part == NULL)
457 return 0;
459 return part->index;
462 u8 __pmac pmac_xpram_read(int xpaddr)
464 int offset = pmac_get_partition(pmac_nvram_XPRAM);
465 loff_t index;
466 u8 buf;
467 ssize_t count;
469 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
470 return 0xff;
471 index = offset + xpaddr;
473 count = ppc_md.nvram_read(&buf, 1, &index);
474 if (count != 1)
475 return 0xff;
476 return buf;
479 void __pmac pmac_xpram_write(int xpaddr, u8 data)
481 int offset = pmac_get_partition(pmac_nvram_XPRAM);
482 loff_t index;
483 u8 buf;
485 if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
486 return;
487 index = offset + xpaddr;
488 buf = data;
490 ppc_md.nvram_write(&buf, 1, &index);
493 EXPORT_SYMBOL(pmac_get_partition);
494 EXPORT_SYMBOL(pmac_xpram_read);
495 EXPORT_SYMBOL(pmac_xpram_write);