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>
27 #include <asm/system.h>
29 #include <asm/machdep.h>
30 #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 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
)
92 if (nvram_image
== NULL
)
94 if (*index
> NVRAM_SIZE
)
98 if (i
+ count
> NVRAM_SIZE
)
99 count
= NVRAM_SIZE
- i
;
101 memcpy(buf
, &nvram_image
[i
], count
);
106 static ssize_t __pmac
core99_nvram_write(char *buf
, size_t count
, loff_t
*index
)
110 if (nvram_image
== NULL
)
112 if (*index
> NVRAM_SIZE
)
116 if (i
+ count
> NVRAM_SIZE
)
117 count
= NVRAM_SIZE
- i
;
119 memcpy(&nvram_image
[i
], buf
, count
);
124 static ssize_t __pmac
core99_nvram_size(void)
126 if (nvram_image
== NULL
)
131 static u8 __pmac
chrp_checksum(struct chrp_header
* hdr
)
134 u16 sum
= hdr
->signature
;
135 for (ptr
= (u8
*)&hdr
->len
; ptr
< hdr
->data
; ptr
++)
138 sum
= (sum
& 0xFF) + (sum
>>8);
142 static u32 __pmac
core99_calc_adler(u8
*buffer
)
147 buffer
+= CORE99_ADLER_START
;
150 for (cnt
=0; cnt
<(NVRAM_SIZE
-CORE99_ADLER_START
); cnt
++) {
151 if ((cnt
% 5000) == 0) {
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");
172 if (hdr99
->hdr
.cksum
!= chrp_checksum(&hdr99
->hdr
)) {
173 DBG("Invalid checksum\n");
176 if (hdr99
->adler
!= core99_calc_adler(datas
)) {
177 DBG("Invalid adler\n");
180 return hdr99
->generation
;
183 static int __pmac
sm_erase_bank(int bank
)
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
);
196 if (++timeout
> 1000000) {
197 printk(KERN_ERR
"nvram: Sharp/Miron flash erase timeout !\n");
200 out_8(base
, SM_FLASH_CMD_READ_STATUS
);
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");
215 static int __pmac
sm_write_bank(int bank
, u8
* datas
)
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
);
227 out_8(base
+i
, datas
[i
]);
230 if (++timeout
> 1000000) {
231 printk(KERN_ERR
"nvram: Sharp/Micron flash write timeout !\n");
234 out_8(base
, SM_FLASH_CMD_READ_STATUS
);
236 } while (!(stat
& SM_FLASH_STATUS_DONE
));
237 if (!(stat
& SM_FLASH_STATUS_DONE
))
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");
250 static int __pmac
amd_erase_bank(int bank
)
253 unsigned long timeout
;
255 u8
* base
= (u8
*)nvram_data
+ core99_bank
*NVRAM_SIZE
;
257 DBG("nvram: AMD Erasing bank %d...\n", bank
);
260 out_8(base
+0x555, 0xaa);
263 out_8(base
+0x2aa, 0x55);
267 out_8(base
+0x555, 0x80);
269 out_8(base
+0x555, 0xaa);
271 out_8(base
+0x2aa, 0x55);
278 if (++timeout
> 1000000) {
279 printk(KERN_ERR
"nvram: AMD flash erase timeout !\n");
282 stat
= in_8(base
) ^ in_8(base
);
289 for (i
=0; i
<NVRAM_SIZE
; i
++)
290 if (base
[i
] != 0xff) {
291 printk(KERN_ERR
"nvram: AMD flash erase failed !\n");
297 static int __pmac
amd_write_bank(int bank
, u8
* datas
)
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
++) {
308 out_8(base
+0x555, 0xaa);
311 out_8(base
+0x2aa, 0x55);
314 /* Write single word */
315 out_8(base
+0x555, 0xa0);
317 out_8(base
+i
, datas
[i
]);
321 if (++timeout
> 1000000) {
322 printk(KERN_ERR
"nvram: AMD flash write timeout !\n");
325 stat
= in_8(base
) ^ in_8(base
);
335 for (i
=0; i
<NVRAM_SIZE
; i
++)
336 if (base
[i
] != datas
[i
]) {
337 printk(KERN_ERR
"nvram: AMD flash write failed !\n");
344 static int __pmac
core99_nvram_sync(void)
346 struct core99_header
* hdr99
;
349 spin_lock_irqsave(&nv_lock
, flags
);
350 if (!memcmp(nvram_image
, (u8
*)nvram_data
+ core99_bank
*NVRAM_SIZE
,
354 DBG("Updating nvram...\n");
356 hdr99
= (struct core99_header
*)nvram_image
;
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
);
367 if (core99_write_bank
)
368 if (core99_write_bank(core99_bank
, nvram_image
))
369 printk("nvram: Error writing bank %d\n", core99_bank
);
371 spin_unlock_irqrestore(&nv_lock
, flags
);
376 int __init
pmac_nvram_init(void)
378 struct device_node
*dp
;
379 u32 gen_bank0
, gen_bank1
;
382 dp
= find_devices("nvram");
384 printk(KERN_ERR
"Can't find NVRAM device\n");
387 if (!device_is_compatible(dp
, "nvram,flash")) {
388 printk(KERN_ERR
"Incompatible type of NVRAM\n");
392 nvram_image
= alloc_bootmem(NVRAM_SIZE
);
393 if (nvram_image
== NULL
) {
394 printk(KERN_ERR
"nvram: can't allocate ram image\n");
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
;
427 core99_erase_bank
= sm_erase_bank
;
428 core99_write_bank
= sm_write_bank
;
434 int __pmac
pmac_get_partition(int partition
)
436 struct nvram_partition
*part
;
445 case pmac_nvram_XPRAM
:
446 name
= "APL,MacOS75";
455 part
= nvram_find_partition(sig
, name
);
462 u8 __pmac
pmac_xpram_read(int xpaddr
)
464 int offset
= pmac_get_partition(pmac_nvram_XPRAM
);
469 if (offset
< 0 || xpaddr
< 0 || xpaddr
> 0x100)
471 index
= offset
+ xpaddr
;
473 count
= ppc_md
.nvram_read(&buf
, 1, &index
);
479 void __pmac
pmac_xpram_write(int xpaddr
, u8 data
)
481 int offset
= pmac_get_partition(pmac_nvram_XPRAM
);
485 if (offset
< 0 || xpaddr
< 0 || xpaddr
> 0x100)
487 index
= offset
+ xpaddr
;
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
);