12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f6d57916SPaul Mackerras /*
3f6d57916SPaul Mackerras * Copyright (C) 2002 Benjamin Herrenschmidt (benh@kernel.crashing.org)
4f6d57916SPaul Mackerras *
5f6d57916SPaul Mackerras * Todo: - add support for the OF persistent properties
6f6d57916SPaul Mackerras */
74b16f8e2SPaul Gortmaker #include <linux/export.h>
8f6d57916SPaul Mackerras #include <linux/kernel.h>
9f6d57916SPaul Mackerras #include <linux/stddef.h>
10f6d57916SPaul Mackerras #include <linux/string.h>
11f6d57916SPaul Mackerras #include <linux/nvram.h>
12f6d57916SPaul Mackerras #include <linux/init.h>
13f6d57916SPaul Mackerras #include <linux/delay.h>
14f6d57916SPaul Mackerras #include <linux/errno.h>
15f6d57916SPaul Mackerras #include <linux/adb.h>
16f6d57916SPaul Mackerras #include <linux/pmu.h>
1757c8a661SMike Rapoport #include <linux/memblock.h>
18f6d57916SPaul Mackerras #include <linux/completion.h>
19f6d57916SPaul Mackerras #include <linux/spinlock.h>
20*e6f6390aSChristophe Leroy #include <linux/of_address.h>
21f6d57916SPaul Mackerras #include <asm/sections.h>
22f6d57916SPaul Mackerras #include <asm/io.h>
23f6d57916SPaul Mackerras #include <asm/machdep.h>
24f6d57916SPaul Mackerras #include <asm/nvram.h>
25f6d57916SPaul Mackerras
260ebfff14SBenjamin Herrenschmidt #include "pmac.h"
270ebfff14SBenjamin Herrenschmidt
28f6d57916SPaul Mackerras #define DEBUG
29f6d57916SPaul Mackerras
30f6d57916SPaul Mackerras #ifdef DEBUG
31f6d57916SPaul Mackerras #define DBG(x...) printk(x)
32f6d57916SPaul Mackerras #else
33f6d57916SPaul Mackerras #define DBG(x...)
34f6d57916SPaul Mackerras #endif
35f6d57916SPaul Mackerras
36f6d57916SPaul Mackerras #define NVRAM_SIZE 0x2000 /* 8kB of non-volatile RAM */
37f6d57916SPaul Mackerras
38f6d57916SPaul Mackerras #define CORE99_SIGNATURE 0x5a
39f6d57916SPaul Mackerras #define CORE99_ADLER_START 0x14
40f6d57916SPaul Mackerras
41f6d57916SPaul Mackerras /* On Core99, nvram is either a sharp, a micron or an AMD flash */
42f6d57916SPaul Mackerras #define SM_FLASH_STATUS_DONE 0x80
43f6d57916SPaul Mackerras #define SM_FLASH_STATUS_ERR 0x38
4435499c01SPaul Mackerras
45f6d57916SPaul Mackerras #define SM_FLASH_CMD_ERASE_CONFIRM 0xd0
46f6d57916SPaul Mackerras #define SM_FLASH_CMD_ERASE_SETUP 0x20
47f6d57916SPaul Mackerras #define SM_FLASH_CMD_RESET 0xff
48f6d57916SPaul Mackerras #define SM_FLASH_CMD_WRITE_SETUP 0x40
49f6d57916SPaul Mackerras #define SM_FLASH_CMD_CLEAR_STATUS 0x50
50f6d57916SPaul Mackerras #define SM_FLASH_CMD_READ_STATUS 0x70
51f6d57916SPaul Mackerras
52f6d57916SPaul Mackerras /* CHRP NVRAM header */
53f6d57916SPaul Mackerras struct chrp_header {
54f6d57916SPaul Mackerras u8 signature;
55f6d57916SPaul Mackerras u8 cksum;
56f6d57916SPaul Mackerras u16 len;
57f6d57916SPaul Mackerras char name[12];
580f6be41cSGustavo A. R. Silva u8 data[];
59f6d57916SPaul Mackerras };
60f6d57916SPaul Mackerras
61f6d57916SPaul Mackerras struct core99_header {
62f6d57916SPaul Mackerras struct chrp_header hdr;
63f6d57916SPaul Mackerras u32 adler;
64f6d57916SPaul Mackerras u32 generation;
65f6d57916SPaul Mackerras u32 reserved[2];
66f6d57916SPaul Mackerras };
67f6d57916SPaul Mackerras
68f6d57916SPaul Mackerras /*
69f6d57916SPaul Mackerras * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
70f6d57916SPaul Mackerras */
71f6d57916SPaul Mackerras static int nvram_naddrs;
72af308377SStephen Rothwell static volatile unsigned char __iomem *nvram_data;
7335499c01SPaul Mackerras static int is_core_99;
749d021a21SXiang wangx static int core99_bank;
75f6d57916SPaul Mackerras static int nvram_partitions[3];
7635499c01SPaul Mackerras // XXX Turn that into a sem
777d725bdcSThomas Gleixner static DEFINE_RAW_SPINLOCK(nv_lock);
78f6d57916SPaul Mackerras
79f6d57916SPaul Mackerras static int (*core99_write_bank)(int bank, u8* datas);
80f6d57916SPaul Mackerras static int (*core99_erase_bank)(int bank);
81f6d57916SPaul Mackerras
82f6d57916SPaul Mackerras static char *nvram_image;
83f6d57916SPaul Mackerras
84f6d57916SPaul Mackerras
core99_nvram_read_byte(int addr)85f6d57916SPaul Mackerras static unsigned char core99_nvram_read_byte(int addr)
86f6d57916SPaul Mackerras {
87f6d57916SPaul Mackerras if (nvram_image == NULL)
88f6d57916SPaul Mackerras return 0xff;
89f6d57916SPaul Mackerras return nvram_image[addr];
90f6d57916SPaul Mackerras }
91f6d57916SPaul Mackerras
core99_nvram_write_byte(int addr,unsigned char val)92f6d57916SPaul Mackerras static void core99_nvram_write_byte(int addr, unsigned char val)
93f6d57916SPaul Mackerras {
94f6d57916SPaul Mackerras if (nvram_image == NULL)
95f6d57916SPaul Mackerras return;
96f6d57916SPaul Mackerras nvram_image[addr] = val;
97f6d57916SPaul Mackerras }
98f6d57916SPaul Mackerras
core99_nvram_read(char * buf,size_t count,loff_t * index)9935499c01SPaul Mackerras static ssize_t core99_nvram_read(char *buf, size_t count, loff_t *index)
10035499c01SPaul Mackerras {
10135499c01SPaul Mackerras int i;
10235499c01SPaul Mackerras
10335499c01SPaul Mackerras if (nvram_image == NULL)
10435499c01SPaul Mackerras return -ENODEV;
10535499c01SPaul Mackerras if (*index > NVRAM_SIZE)
10635499c01SPaul Mackerras return 0;
10735499c01SPaul Mackerras
10835499c01SPaul Mackerras i = *index;
10935499c01SPaul Mackerras if (i + count > NVRAM_SIZE)
11035499c01SPaul Mackerras count = NVRAM_SIZE - i;
11135499c01SPaul Mackerras
11235499c01SPaul Mackerras memcpy(buf, &nvram_image[i], count);
11335499c01SPaul Mackerras *index = i + count;
11435499c01SPaul Mackerras return count;
11535499c01SPaul Mackerras }
11635499c01SPaul Mackerras
core99_nvram_write(char * buf,size_t count,loff_t * index)11735499c01SPaul Mackerras static ssize_t core99_nvram_write(char *buf, size_t count, loff_t *index)
11835499c01SPaul Mackerras {
11935499c01SPaul Mackerras int i;
12035499c01SPaul Mackerras
12135499c01SPaul Mackerras if (nvram_image == NULL)
12235499c01SPaul Mackerras return -ENODEV;
12335499c01SPaul Mackerras if (*index > NVRAM_SIZE)
12435499c01SPaul Mackerras return 0;
12535499c01SPaul Mackerras
12635499c01SPaul Mackerras i = *index;
12735499c01SPaul Mackerras if (i + count > NVRAM_SIZE)
12835499c01SPaul Mackerras count = NVRAM_SIZE - i;
12935499c01SPaul Mackerras
13035499c01SPaul Mackerras memcpy(&nvram_image[i], buf, count);
13135499c01SPaul Mackerras *index = i + count;
13235499c01SPaul Mackerras return count;
13335499c01SPaul Mackerras }
13435499c01SPaul Mackerras
core99_nvram_size(void)13535499c01SPaul Mackerras static ssize_t core99_nvram_size(void)
13635499c01SPaul Mackerras {
13735499c01SPaul Mackerras if (nvram_image == NULL)
13835499c01SPaul Mackerras return -ENODEV;
13935499c01SPaul Mackerras return NVRAM_SIZE;
14035499c01SPaul Mackerras }
14135499c01SPaul Mackerras
14235499c01SPaul Mackerras #ifdef CONFIG_PPC32
143af308377SStephen Rothwell static volatile unsigned char __iomem *nvram_addr;
14435499c01SPaul Mackerras static int nvram_mult;
145f6d57916SPaul Mackerras
ppc32_nvram_size(void)146ebcebc7fSFinn Thain static ssize_t ppc32_nvram_size(void)
147ebcebc7fSFinn Thain {
148ebcebc7fSFinn Thain return NVRAM_SIZE;
149ebcebc7fSFinn Thain }
150ebcebc7fSFinn Thain
direct_nvram_read_byte(int addr)151f6d57916SPaul Mackerras static unsigned char direct_nvram_read_byte(int addr)
152f6d57916SPaul Mackerras {
153f6d57916SPaul Mackerras return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);
154f6d57916SPaul Mackerras }
155f6d57916SPaul Mackerras
direct_nvram_write_byte(int addr,unsigned char val)156f6d57916SPaul Mackerras static void direct_nvram_write_byte(int addr, unsigned char val)
157f6d57916SPaul Mackerras {
158f6d57916SPaul Mackerras out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);
159f6d57916SPaul Mackerras }
160f6d57916SPaul Mackerras
161f6d57916SPaul Mackerras
indirect_nvram_read_byte(int addr)162f6d57916SPaul Mackerras static unsigned char indirect_nvram_read_byte(int addr)
163f6d57916SPaul Mackerras {
164f6d57916SPaul Mackerras unsigned char val;
165f6d57916SPaul Mackerras unsigned long flags;
166f6d57916SPaul Mackerras
1677d725bdcSThomas Gleixner raw_spin_lock_irqsave(&nv_lock, flags);
168f6d57916SPaul Mackerras out_8(nvram_addr, addr >> 5);
169f6d57916SPaul Mackerras val = in_8(&nvram_data[(addr & 0x1f) << 4]);
1707d725bdcSThomas Gleixner raw_spin_unlock_irqrestore(&nv_lock, flags);
171f6d57916SPaul Mackerras
172f6d57916SPaul Mackerras return val;
173f6d57916SPaul Mackerras }
174f6d57916SPaul Mackerras
indirect_nvram_write_byte(int addr,unsigned char val)175f6d57916SPaul Mackerras static void indirect_nvram_write_byte(int addr, unsigned char val)
176f6d57916SPaul Mackerras {
177f6d57916SPaul Mackerras unsigned long flags;
178f6d57916SPaul Mackerras
1797d725bdcSThomas Gleixner raw_spin_lock_irqsave(&nv_lock, flags);
180f6d57916SPaul Mackerras out_8(nvram_addr, addr >> 5);
181f6d57916SPaul Mackerras out_8(&nvram_data[(addr & 0x1f) << 4], val);
1827d725bdcSThomas Gleixner raw_spin_unlock_irqrestore(&nv_lock, flags);
183f6d57916SPaul Mackerras }
184f6d57916SPaul Mackerras
185f6d57916SPaul Mackerras
186f6d57916SPaul Mackerras #ifdef CONFIG_ADB_PMU
187f6d57916SPaul Mackerras
pmu_nvram_complete(struct adb_request * req)188f6d57916SPaul Mackerras static void pmu_nvram_complete(struct adb_request *req)
189f6d57916SPaul Mackerras {
190f6d57916SPaul Mackerras if (req->arg)
191f6d57916SPaul Mackerras complete((struct completion *)req->arg);
192f6d57916SPaul Mackerras }
193f6d57916SPaul Mackerras
pmu_nvram_read_byte(int addr)194f6d57916SPaul Mackerras static unsigned char pmu_nvram_read_byte(int addr)
195f6d57916SPaul Mackerras {
196f6d57916SPaul Mackerras struct adb_request req;
1976e9a4738SPeter Zijlstra DECLARE_COMPLETION_ONSTACK(req_complete);
198f6d57916SPaul Mackerras
199f6d57916SPaul Mackerras req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
200f6d57916SPaul Mackerras if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,
201f6d57916SPaul Mackerras (addr >> 8) & 0xff, addr & 0xff))
202f6d57916SPaul Mackerras return 0xff;
203f6d57916SPaul Mackerras if (system_state == SYSTEM_RUNNING)
204f6d57916SPaul Mackerras wait_for_completion(&req_complete);
205f6d57916SPaul Mackerras while (!req.complete)
206f6d57916SPaul Mackerras pmu_poll();
207f6d57916SPaul Mackerras return req.reply[0];
208f6d57916SPaul Mackerras }
209f6d57916SPaul Mackerras
pmu_nvram_write_byte(int addr,unsigned char val)210f6d57916SPaul Mackerras static void pmu_nvram_write_byte(int addr, unsigned char val)
211f6d57916SPaul Mackerras {
212f6d57916SPaul Mackerras struct adb_request req;
2136e9a4738SPeter Zijlstra DECLARE_COMPLETION_ONSTACK(req_complete);
214f6d57916SPaul Mackerras
215f6d57916SPaul Mackerras req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;
216f6d57916SPaul Mackerras if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,
217f6d57916SPaul Mackerras (addr >> 8) & 0xff, addr & 0xff, val))
218f6d57916SPaul Mackerras return;
219f6d57916SPaul Mackerras if (system_state == SYSTEM_RUNNING)
220f6d57916SPaul Mackerras wait_for_completion(&req_complete);
221f6d57916SPaul Mackerras while (!req.complete)
222f6d57916SPaul Mackerras pmu_poll();
223f6d57916SPaul Mackerras }
224f6d57916SPaul Mackerras
225f6d57916SPaul Mackerras #endif /* CONFIG_ADB_PMU */
22635499c01SPaul Mackerras #endif /* CONFIG_PPC32 */
227f6d57916SPaul Mackerras
chrp_checksum(struct chrp_header * hdr)228f6d57916SPaul Mackerras static u8 chrp_checksum(struct chrp_header* hdr)
229f6d57916SPaul Mackerras {
230f6d57916SPaul Mackerras u8 *ptr;
231f6d57916SPaul Mackerras u16 sum = hdr->signature;
232f6d57916SPaul Mackerras for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)
233f6d57916SPaul Mackerras sum += *ptr;
234f6d57916SPaul Mackerras while (sum > 0xFF)
235f6d57916SPaul Mackerras sum = (sum & 0xFF) + (sum>>8);
236f6d57916SPaul Mackerras return sum;
237f6d57916SPaul Mackerras }
238f6d57916SPaul Mackerras
core99_calc_adler(u8 * buffer)239f6d57916SPaul Mackerras static u32 core99_calc_adler(u8 *buffer)
240f6d57916SPaul Mackerras {
241f6d57916SPaul Mackerras int cnt;
242f6d57916SPaul Mackerras u32 low, high;
243f6d57916SPaul Mackerras
244f6d57916SPaul Mackerras buffer += CORE99_ADLER_START;
245f6d57916SPaul Mackerras low = 1;
246f6d57916SPaul Mackerras high = 0;
247f6d57916SPaul Mackerras for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {
248f6d57916SPaul Mackerras if ((cnt % 5000) == 0) {
249f6d57916SPaul Mackerras high %= 65521UL;
250f6d57916SPaul Mackerras high %= 65521UL;
251f6d57916SPaul Mackerras }
252f6d57916SPaul Mackerras low += buffer[cnt];
253f6d57916SPaul Mackerras high += low;
254f6d57916SPaul Mackerras }
255f6d57916SPaul Mackerras low %= 65521UL;
256f6d57916SPaul Mackerras high %= 65521UL;
257f6d57916SPaul Mackerras
258f6d57916SPaul Mackerras return (high << 16) | low;
259f6d57916SPaul Mackerras }
260f6d57916SPaul Mackerras
core99_check(u8 * datas)261b346f571SNick Child static u32 __init core99_check(u8 *datas)
262f6d57916SPaul Mackerras {
263f6d57916SPaul Mackerras struct core99_header* hdr99 = (struct core99_header*)datas;
264f6d57916SPaul Mackerras
265f6d57916SPaul Mackerras if (hdr99->hdr.signature != CORE99_SIGNATURE) {
266f6d57916SPaul Mackerras DBG("Invalid signature\n");
267f6d57916SPaul Mackerras return 0;
268f6d57916SPaul Mackerras }
269f6d57916SPaul Mackerras if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {
270f6d57916SPaul Mackerras DBG("Invalid checksum\n");
271f6d57916SPaul Mackerras return 0;
272f6d57916SPaul Mackerras }
273f6d57916SPaul Mackerras if (hdr99->adler != core99_calc_adler(datas)) {
274f6d57916SPaul Mackerras DBG("Invalid adler\n");
275f6d57916SPaul Mackerras return 0;
276f6d57916SPaul Mackerras }
277f6d57916SPaul Mackerras return hdr99->generation;
278f6d57916SPaul Mackerras }
279f6d57916SPaul Mackerras
sm_erase_bank(int bank)280f6d57916SPaul Mackerras static int sm_erase_bank(int bank)
281f6d57916SPaul Mackerras {
2822d4b9712SAkinobu Mita int stat;
283f6d57916SPaul Mackerras unsigned long timeout;
284f6d57916SPaul Mackerras
285af308377SStephen Rothwell u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
286f6d57916SPaul Mackerras
287f6d57916SPaul Mackerras DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);
288f6d57916SPaul Mackerras
289f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_ERASE_SETUP);
290f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);
291f6d57916SPaul Mackerras timeout = 0;
292f6d57916SPaul Mackerras do {
293f6d57916SPaul Mackerras if (++timeout > 1000000) {
29435499c01SPaul Mackerras printk(KERN_ERR "nvram: Sharp/Micron flash erase timeout !\n");
295f6d57916SPaul Mackerras break;
296f6d57916SPaul Mackerras }
297f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_READ_STATUS);
298f6d57916SPaul Mackerras stat = in_8(base);
299f6d57916SPaul Mackerras } while (!(stat & SM_FLASH_STATUS_DONE));
300f6d57916SPaul Mackerras
301f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
302f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_RESET);
303f6d57916SPaul Mackerras
3042d4b9712SAkinobu Mita if (memchr_inv(base, 0xff, NVRAM_SIZE)) {
305f6d57916SPaul Mackerras printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
306f6d57916SPaul Mackerras return -ENXIO;
307f6d57916SPaul Mackerras }
308f6d57916SPaul Mackerras return 0;
309f6d57916SPaul Mackerras }
310f6d57916SPaul Mackerras
sm_write_bank(int bank,u8 * datas)311f6d57916SPaul Mackerras static int sm_write_bank(int bank, u8* datas)
312f6d57916SPaul Mackerras {
313f6d57916SPaul Mackerras int i, stat = 0;
314f6d57916SPaul Mackerras unsigned long timeout;
315f6d57916SPaul Mackerras
316af308377SStephen Rothwell u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
317f6d57916SPaul Mackerras
318f6d57916SPaul Mackerras DBG("nvram: Sharp/Micron Writing bank %d...\n", bank);
319f6d57916SPaul Mackerras
320f6d57916SPaul Mackerras for (i=0; i<NVRAM_SIZE; i++) {
321f6d57916SPaul Mackerras out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);
322f6d57916SPaul Mackerras udelay(1);
323f6d57916SPaul Mackerras out_8(base+i, datas[i]);
324f6d57916SPaul Mackerras timeout = 0;
325f6d57916SPaul Mackerras do {
326f6d57916SPaul Mackerras if (++timeout > 1000000) {
327f6d57916SPaul Mackerras printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");
328f6d57916SPaul Mackerras break;
329f6d57916SPaul Mackerras }
330f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_READ_STATUS);
331f6d57916SPaul Mackerras stat = in_8(base);
332f6d57916SPaul Mackerras } while (!(stat & SM_FLASH_STATUS_DONE));
333f6d57916SPaul Mackerras if (!(stat & SM_FLASH_STATUS_DONE))
334f6d57916SPaul Mackerras break;
335f6d57916SPaul Mackerras }
336f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
337f6d57916SPaul Mackerras out_8(base, SM_FLASH_CMD_RESET);
3382d4b9712SAkinobu Mita if (memcmp(base, datas, NVRAM_SIZE)) {
339f6d57916SPaul Mackerras printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
340f6d57916SPaul Mackerras return -ENXIO;
341f6d57916SPaul Mackerras }
342f6d57916SPaul Mackerras return 0;
343f6d57916SPaul Mackerras }
344f6d57916SPaul Mackerras
amd_erase_bank(int bank)345f6d57916SPaul Mackerras static int amd_erase_bank(int bank)
346f6d57916SPaul Mackerras {
3472d4b9712SAkinobu Mita int stat = 0;
348f6d57916SPaul Mackerras unsigned long timeout;
349f6d57916SPaul Mackerras
350af308377SStephen Rothwell u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
351f6d57916SPaul Mackerras
352f6d57916SPaul Mackerras DBG("nvram: AMD Erasing bank %d...\n", bank);
353f6d57916SPaul Mackerras
354f6d57916SPaul Mackerras /* Unlock 1 */
355f6d57916SPaul Mackerras out_8(base+0x555, 0xaa);
356f6d57916SPaul Mackerras udelay(1);
357f6d57916SPaul Mackerras /* Unlock 2 */
358f6d57916SPaul Mackerras out_8(base+0x2aa, 0x55);
359f6d57916SPaul Mackerras udelay(1);
360f6d57916SPaul Mackerras
361f6d57916SPaul Mackerras /* Sector-Erase */
362f6d57916SPaul Mackerras out_8(base+0x555, 0x80);
363f6d57916SPaul Mackerras udelay(1);
364f6d57916SPaul Mackerras out_8(base+0x555, 0xaa);
365f6d57916SPaul Mackerras udelay(1);
366f6d57916SPaul Mackerras out_8(base+0x2aa, 0x55);
367f6d57916SPaul Mackerras udelay(1);
368f6d57916SPaul Mackerras out_8(base, 0x30);
369f6d57916SPaul Mackerras udelay(1);
370f6d57916SPaul Mackerras
371f6d57916SPaul Mackerras timeout = 0;
372f6d57916SPaul Mackerras do {
373f6d57916SPaul Mackerras if (++timeout > 1000000) {
374f6d57916SPaul Mackerras printk(KERN_ERR "nvram: AMD flash erase timeout !\n");
375f6d57916SPaul Mackerras break;
376f6d57916SPaul Mackerras }
377f6d57916SPaul Mackerras stat = in_8(base) ^ in_8(base);
378f6d57916SPaul Mackerras } while (stat != 0);
379f6d57916SPaul Mackerras
380f6d57916SPaul Mackerras /* Reset */
381f6d57916SPaul Mackerras out_8(base, 0xf0);
382f6d57916SPaul Mackerras udelay(1);
383f6d57916SPaul Mackerras
3842d4b9712SAkinobu Mita if (memchr_inv(base, 0xff, NVRAM_SIZE)) {
385f6d57916SPaul Mackerras printk(KERN_ERR "nvram: AMD flash erase failed !\n");
386f6d57916SPaul Mackerras return -ENXIO;
387f6d57916SPaul Mackerras }
388f6d57916SPaul Mackerras return 0;
389f6d57916SPaul Mackerras }
390f6d57916SPaul Mackerras
amd_write_bank(int bank,u8 * datas)391f6d57916SPaul Mackerras static int amd_write_bank(int bank, u8* datas)
392f6d57916SPaul Mackerras {
393f6d57916SPaul Mackerras int i, stat = 0;
394f6d57916SPaul Mackerras unsigned long timeout;
395f6d57916SPaul Mackerras
396af308377SStephen Rothwell u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
397f6d57916SPaul Mackerras
398f6d57916SPaul Mackerras DBG("nvram: AMD Writing bank %d...\n", bank);
399f6d57916SPaul Mackerras
400f6d57916SPaul Mackerras for (i=0; i<NVRAM_SIZE; i++) {
401f6d57916SPaul Mackerras /* Unlock 1 */
402f6d57916SPaul Mackerras out_8(base+0x555, 0xaa);
403f6d57916SPaul Mackerras udelay(1);
404f6d57916SPaul Mackerras /* Unlock 2 */
405f6d57916SPaul Mackerras out_8(base+0x2aa, 0x55);
406f6d57916SPaul Mackerras udelay(1);
407f6d57916SPaul Mackerras
408f6d57916SPaul Mackerras /* Write single word */
409f6d57916SPaul Mackerras out_8(base+0x555, 0xa0);
410f6d57916SPaul Mackerras udelay(1);
411f6d57916SPaul Mackerras out_8(base+i, datas[i]);
412f6d57916SPaul Mackerras
413f6d57916SPaul Mackerras timeout = 0;
414f6d57916SPaul Mackerras do {
415f6d57916SPaul Mackerras if (++timeout > 1000000) {
416f6d57916SPaul Mackerras printk(KERN_ERR "nvram: AMD flash write timeout !\n");
417f6d57916SPaul Mackerras break;
418f6d57916SPaul Mackerras }
419f6d57916SPaul Mackerras stat = in_8(base) ^ in_8(base);
420f6d57916SPaul Mackerras } while (stat != 0);
421f6d57916SPaul Mackerras if (stat != 0)
422f6d57916SPaul Mackerras break;
423f6d57916SPaul Mackerras }
424f6d57916SPaul Mackerras
425f6d57916SPaul Mackerras /* Reset */
426f6d57916SPaul Mackerras out_8(base, 0xf0);
427f6d57916SPaul Mackerras udelay(1);
428f6d57916SPaul Mackerras
4292d4b9712SAkinobu Mita if (memcmp(base, datas, NVRAM_SIZE)) {
430f6d57916SPaul Mackerras printk(KERN_ERR "nvram: AMD flash write failed !\n");
431f6d57916SPaul Mackerras return -ENXIO;
432f6d57916SPaul Mackerras }
433f6d57916SPaul Mackerras return 0;
434f6d57916SPaul Mackerras }
435f6d57916SPaul Mackerras
lookup_partitions(void)436f6d57916SPaul Mackerras static void __init lookup_partitions(void)
437f6d57916SPaul Mackerras {
438f6d57916SPaul Mackerras u8 buffer[17];
439f6d57916SPaul Mackerras int i, offset;
440f6d57916SPaul Mackerras struct chrp_header* hdr;
441f6d57916SPaul Mackerras
442f6d57916SPaul Mackerras if (pmac_newworld) {
443f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_OF] = -1;
444f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_XPRAM] = -1;
445f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_NR] = -1;
446f6d57916SPaul Mackerras hdr = (struct chrp_header *)buffer;
447f6d57916SPaul Mackerras
448f6d57916SPaul Mackerras offset = 0;
449f6d57916SPaul Mackerras buffer[16] = 0;
450f6d57916SPaul Mackerras do {
451f6d57916SPaul Mackerras for (i=0;i<16;i++)
45235499c01SPaul Mackerras buffer[i] = ppc_md.nvram_read_val(offset+i);
453f6d57916SPaul Mackerras if (!strcmp(hdr->name, "common"))
454f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_OF] = offset + 0x10;
455f6d57916SPaul Mackerras if (!strcmp(hdr->name, "APL,MacOS75")) {
456f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;
457f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_NR] = offset + 0x110;
458f6d57916SPaul Mackerras }
459f6d57916SPaul Mackerras offset += (hdr->len * 0x10);
460f6d57916SPaul Mackerras } while(offset < NVRAM_SIZE);
461f6d57916SPaul Mackerras } else {
462f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_OF] = 0x1800;
463f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_XPRAM] = 0x1300;
464f6d57916SPaul Mackerras nvram_partitions[pmac_nvram_NR] = 0x1400;
465f6d57916SPaul Mackerras }
466f6d57916SPaul Mackerras DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);
467f6d57916SPaul Mackerras DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);
468f6d57916SPaul Mackerras DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);
469f6d57916SPaul Mackerras }
470f6d57916SPaul Mackerras
core99_nvram_sync(void)471f6d57916SPaul Mackerras static void core99_nvram_sync(void)
472f6d57916SPaul Mackerras {
473f6d57916SPaul Mackerras struct core99_header* hdr99;
474f6d57916SPaul Mackerras unsigned long flags;
475f6d57916SPaul Mackerras
476f6d57916SPaul Mackerras if (!is_core_99 || !nvram_data || !nvram_image)
477f6d57916SPaul Mackerras return;
478f6d57916SPaul Mackerras
4797d725bdcSThomas Gleixner raw_spin_lock_irqsave(&nv_lock, flags);
480f6d57916SPaul Mackerras if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,
481f6d57916SPaul Mackerras NVRAM_SIZE))
482f6d57916SPaul Mackerras goto bail;
483f6d57916SPaul Mackerras
484f6d57916SPaul Mackerras DBG("Updating nvram...\n");
485f6d57916SPaul Mackerras
486f6d57916SPaul Mackerras hdr99 = (struct core99_header*)nvram_image;
487f6d57916SPaul Mackerras hdr99->generation++;
488f6d57916SPaul Mackerras hdr99->hdr.signature = CORE99_SIGNATURE;
489f6d57916SPaul Mackerras hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);
490f6d57916SPaul Mackerras hdr99->adler = core99_calc_adler(nvram_image);
491f6d57916SPaul Mackerras core99_bank = core99_bank ? 0 : 1;
492f6d57916SPaul Mackerras if (core99_erase_bank)
493f6d57916SPaul Mackerras if (core99_erase_bank(core99_bank)) {
494f6d57916SPaul Mackerras printk("nvram: Error erasing bank %d\n", core99_bank);
495f6d57916SPaul Mackerras goto bail;
496f6d57916SPaul Mackerras }
497f6d57916SPaul Mackerras if (core99_write_bank)
498f6d57916SPaul Mackerras if (core99_write_bank(core99_bank, nvram_image))
499f6d57916SPaul Mackerras printk("nvram: Error writing bank %d\n", core99_bank);
500f6d57916SPaul Mackerras bail:
5017d725bdcSThomas Gleixner raw_spin_unlock_irqrestore(&nv_lock, flags);
502f6d57916SPaul Mackerras
503f6d57916SPaul Mackerras #ifdef DEBUG
504f6d57916SPaul Mackerras mdelay(2000);
505f6d57916SPaul Mackerras #endif
506f6d57916SPaul Mackerras }
507f6d57916SPaul Mackerras
core99_nvram_setup(struct device_node * dp,unsigned long addr)508cc5d0189SBenjamin Herrenschmidt static int __init core99_nvram_setup(struct device_node *dp, unsigned long addr)
509f6d57916SPaul Mackerras {
510f6d57916SPaul Mackerras int i;
511f6d57916SPaul Mackerras u32 gen_bank0, gen_bank1;
512f6d57916SPaul Mackerras
513f6d57916SPaul Mackerras if (nvram_naddrs < 1) {
514f6d57916SPaul Mackerras printk(KERN_ERR "nvram: no address\n");
51535499c01SPaul Mackerras return -EINVAL;
516f6d57916SPaul Mackerras }
5177e1c4e27SMike Rapoport nvram_image = memblock_alloc(NVRAM_SIZE, SMP_CACHE_BYTES);
5188a7f97b9SMike Rapoport if (!nvram_image)
5198a7f97b9SMike Rapoport panic("%s: Failed to allocate %u bytes\n", __func__,
5208a7f97b9SMike Rapoport NVRAM_SIZE);
521cc5d0189SBenjamin Herrenschmidt nvram_data = ioremap(addr, NVRAM_SIZE*2);
522f6d57916SPaul Mackerras nvram_naddrs = 1; /* Make sure we get the correct case */
523f6d57916SPaul Mackerras
524f6d57916SPaul Mackerras DBG("nvram: Checking bank 0...\n");
525f6d57916SPaul Mackerras
526f6d57916SPaul Mackerras gen_bank0 = core99_check((u8 *)nvram_data);
527f6d57916SPaul Mackerras gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);
528f6d57916SPaul Mackerras core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;
529f6d57916SPaul Mackerras
530f6d57916SPaul Mackerras DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);
531f6d57916SPaul Mackerras DBG("nvram: Active bank is: %d\n", core99_bank);
532f6d57916SPaul Mackerras
533f6d57916SPaul Mackerras for (i=0; i<NVRAM_SIZE; i++)
534f6d57916SPaul Mackerras nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];
535f6d57916SPaul Mackerras
536f6d57916SPaul Mackerras ppc_md.nvram_read_val = core99_nvram_read_byte;
537f6d57916SPaul Mackerras ppc_md.nvram_write_val = core99_nvram_write_byte;
53835499c01SPaul Mackerras ppc_md.nvram_read = core99_nvram_read;
53935499c01SPaul Mackerras ppc_md.nvram_write = core99_nvram_write;
54035499c01SPaul Mackerras ppc_md.nvram_size = core99_nvram_size;
541f6d57916SPaul Mackerras ppc_md.nvram_sync = core99_nvram_sync;
5423d1229d6SMichael Ellerman ppc_md.machine_shutdown = core99_nvram_sync;
543f6d57916SPaul Mackerras /*
544f6d57916SPaul Mackerras * Maybe we could be smarter here though making an exclusive list
545f6d57916SPaul Mackerras * of known flash chips is a bit nasty as older OF didn't provide us
546f6d57916SPaul Mackerras * with a useful "compatible" entry. A solution would be to really
547f6d57916SPaul Mackerras * identify the chip using flash id commands and base ourselves on
548f6d57916SPaul Mackerras * a list of known chips IDs
549f6d57916SPaul Mackerras */
55055b61fecSStephen Rothwell if (of_device_is_compatible(dp, "amd-0137")) {
551f6d57916SPaul Mackerras core99_erase_bank = amd_erase_bank;
552f6d57916SPaul Mackerras core99_write_bank = amd_write_bank;
553f6d57916SPaul Mackerras } else {
554f6d57916SPaul Mackerras core99_erase_bank = sm_erase_bank;
555f6d57916SPaul Mackerras core99_write_bank = sm_write_bank;
556f6d57916SPaul Mackerras }
55735499c01SPaul Mackerras return 0;
55835499c01SPaul Mackerras }
55935499c01SPaul Mackerras
pmac_nvram_init(void)56035499c01SPaul Mackerras int __init pmac_nvram_init(void)
56135499c01SPaul Mackerras {
56235499c01SPaul Mackerras struct device_node *dp;
563cc5d0189SBenjamin Herrenschmidt struct resource r1, r2;
564cc5d0189SBenjamin Herrenschmidt unsigned int s1 = 0, s2 = 0;
56535499c01SPaul Mackerras int err = 0;
56635499c01SPaul Mackerras
56735499c01SPaul Mackerras nvram_naddrs = 0;
56835499c01SPaul Mackerras
569cc5d0189SBenjamin Herrenschmidt dp = of_find_node_by_name(NULL, "nvram");
57035499c01SPaul Mackerras if (dp == NULL) {
57135499c01SPaul Mackerras printk(KERN_ERR "Can't find NVRAM device\n");
57235499c01SPaul Mackerras return -ENODEV;
57335499c01SPaul Mackerras }
574cc5d0189SBenjamin Herrenschmidt
575cc5d0189SBenjamin Herrenschmidt /* Try to obtain an address */
576cc5d0189SBenjamin Herrenschmidt if (of_address_to_resource(dp, 0, &r1) == 0) {
577cc5d0189SBenjamin Herrenschmidt nvram_naddrs = 1;
57828f65c11SJoe Perches s1 = resource_size(&r1);
579cc5d0189SBenjamin Herrenschmidt if (of_address_to_resource(dp, 1, &r2) == 0) {
580cc5d0189SBenjamin Herrenschmidt nvram_naddrs = 2;
58128f65c11SJoe Perches s2 = resource_size(&r2);
582cc5d0189SBenjamin Herrenschmidt }
583cc5d0189SBenjamin Herrenschmidt }
584cc5d0189SBenjamin Herrenschmidt
58555b61fecSStephen Rothwell is_core_99 = of_device_is_compatible(dp, "nvram,flash");
586cc5d0189SBenjamin Herrenschmidt if (is_core_99) {
587cc5d0189SBenjamin Herrenschmidt err = core99_nvram_setup(dp, r1.start);
588cc5d0189SBenjamin Herrenschmidt goto bail;
589cc5d0189SBenjamin Herrenschmidt }
590cc5d0189SBenjamin Herrenschmidt
59135499c01SPaul Mackerras #ifdef CONFIG_PPC32
592e8222502SBenjamin Herrenschmidt if (machine_is(chrp) && nvram_naddrs == 1) {
593cc5d0189SBenjamin Herrenschmidt nvram_data = ioremap(r1.start, s1);
594f6d57916SPaul Mackerras nvram_mult = 1;
595f6d57916SPaul Mackerras ppc_md.nvram_read_val = direct_nvram_read_byte;
596f6d57916SPaul Mackerras ppc_md.nvram_write_val = direct_nvram_write_byte;
597ebcebc7fSFinn Thain ppc_md.nvram_size = ppc32_nvram_size;
598f6d57916SPaul Mackerras } else if (nvram_naddrs == 1) {
599cc5d0189SBenjamin Herrenschmidt nvram_data = ioremap(r1.start, s1);
600cc5d0189SBenjamin Herrenschmidt nvram_mult = (s1 + NVRAM_SIZE - 1) / NVRAM_SIZE;
601f6d57916SPaul Mackerras ppc_md.nvram_read_val = direct_nvram_read_byte;
602f6d57916SPaul Mackerras ppc_md.nvram_write_val = direct_nvram_write_byte;
603ebcebc7fSFinn Thain ppc_md.nvram_size = ppc32_nvram_size;
604f6d57916SPaul Mackerras } else if (nvram_naddrs == 2) {
605cc5d0189SBenjamin Herrenschmidt nvram_addr = ioremap(r1.start, s1);
606cc5d0189SBenjamin Herrenschmidt nvram_data = ioremap(r2.start, s2);
607f6d57916SPaul Mackerras ppc_md.nvram_read_val = indirect_nvram_read_byte;
608f6d57916SPaul Mackerras ppc_md.nvram_write_val = indirect_nvram_write_byte;
609ebcebc7fSFinn Thain ppc_md.nvram_size = ppc32_nvram_size;
610f6d57916SPaul Mackerras } else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {
611f6d57916SPaul Mackerras #ifdef CONFIG_ADB_PMU
612f6d57916SPaul Mackerras nvram_naddrs = -1;
613f6d57916SPaul Mackerras ppc_md.nvram_read_val = pmu_nvram_read_byte;
614f6d57916SPaul Mackerras ppc_md.nvram_write_val = pmu_nvram_write_byte;
615ebcebc7fSFinn Thain ppc_md.nvram_size = ppc32_nvram_size;
616f6d57916SPaul Mackerras #endif /* CONFIG_ADB_PMU */
617cc5d0189SBenjamin Herrenschmidt } else {
61835499c01SPaul Mackerras printk(KERN_ERR "Incompatible type of NVRAM\n");
619cc5d0189SBenjamin Herrenschmidt err = -ENXIO;
620f6d57916SPaul Mackerras }
621cc5d0189SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
622cc5d0189SBenjamin Herrenschmidt bail:
623cc5d0189SBenjamin Herrenschmidt of_node_put(dp);
624cc5d0189SBenjamin Herrenschmidt if (err == 0)
625f6d57916SPaul Mackerras lookup_partitions();
62635499c01SPaul Mackerras return err;
627f6d57916SPaul Mackerras }
628f6d57916SPaul Mackerras
pmac_get_partition(int partition)629f6d57916SPaul Mackerras int pmac_get_partition(int partition)
630f6d57916SPaul Mackerras {
631f6d57916SPaul Mackerras return nvram_partitions[partition];
632f6d57916SPaul Mackerras }
633f6d57916SPaul Mackerras
pmac_xpram_read(int xpaddr)634f6d57916SPaul Mackerras u8 pmac_xpram_read(int xpaddr)
635f6d57916SPaul Mackerras {
63635499c01SPaul Mackerras int offset = pmac_get_partition(pmac_nvram_XPRAM);
637f6d57916SPaul Mackerras
63835499c01SPaul Mackerras if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
639f6d57916SPaul Mackerras return 0xff;
640f6d57916SPaul Mackerras
641f6d57916SPaul Mackerras return ppc_md.nvram_read_val(xpaddr + offset);
642f6d57916SPaul Mackerras }
643f6d57916SPaul Mackerras
pmac_xpram_write(int xpaddr,u8 data)644f6d57916SPaul Mackerras void pmac_xpram_write(int xpaddr, u8 data)
645f6d57916SPaul Mackerras {
64635499c01SPaul Mackerras int offset = pmac_get_partition(pmac_nvram_XPRAM);
647f6d57916SPaul Mackerras
64835499c01SPaul Mackerras if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)
649f6d57916SPaul Mackerras return;
650f6d57916SPaul Mackerras
651f6d57916SPaul Mackerras ppc_md.nvram_write_val(xpaddr + offset, data);
652f6d57916SPaul Mackerras }
653f6d57916SPaul Mackerras
654f6d57916SPaul Mackerras EXPORT_SYMBOL(pmac_get_partition);
655f6d57916SPaul Mackerras EXPORT_SYMBOL(pmac_xpram_read);
656f6d57916SPaul Mackerras EXPORT_SYMBOL(pmac_xpram_write);
657