⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nvram.c

📁 linux内核源码
💻 C
📖 第 1 页 / 共 2 页
字号:
			}			out_8(base, SM_FLASH_CMD_READ_STATUS);			stat = in_8(base);		} while (!(stat & SM_FLASH_STATUS_DONE));		if (!(stat & SM_FLASH_STATUS_DONE))			break;	}	out_8(base, SM_FLASH_CMD_CLEAR_STATUS);	out_8(base, SM_FLASH_CMD_RESET);	for (i=0; i<NVRAM_SIZE; i++)		if (base[i] != datas[i]) {			printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");			return -ENXIO;		}	return 0;}static int amd_erase_bank(int bank){	int i, stat = 0;	unsigned long timeout;	u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;       	DBG("nvram: AMD Erasing bank %d...\n", bank);	/* Unlock 1 */	out_8(base+0x555, 0xaa);	udelay(1);	/* Unlock 2 */	out_8(base+0x2aa, 0x55);	udelay(1);	/* Sector-Erase */	out_8(base+0x555, 0x80);	udelay(1);	out_8(base+0x555, 0xaa);	udelay(1);	out_8(base+0x2aa, 0x55);	udelay(1);	out_8(base, 0x30);	udelay(1);	timeout = 0;	do {		if (++timeout > 1000000) {			printk(KERN_ERR "nvram: AMD flash erase timeout !\n");			break;		}		stat = in_8(base) ^ in_8(base);	} while (stat != 0);		/* Reset */	out_8(base, 0xf0);	udelay(1);		for (i=0; i<NVRAM_SIZE; i++)		if (base[i] != 0xff) {			printk(KERN_ERR "nvram: AMD flash erase failed !\n");			return -ENXIO;		}	return 0;}static int amd_write_bank(int bank, u8* datas){	int i, stat = 0;	unsigned long timeout;	u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;       	DBG("nvram: AMD Writing bank %d...\n", bank);	for (i=0; i<NVRAM_SIZE; i++) {		/* Unlock 1 */		out_8(base+0x555, 0xaa);		udelay(1);		/* Unlock 2 */		out_8(base+0x2aa, 0x55);		udelay(1);		/* Write single word */		out_8(base+0x555, 0xa0);		udelay(1);		out_8(base+i, datas[i]);				timeout = 0;		do {			if (++timeout > 1000000) {				printk(KERN_ERR "nvram: AMD flash write timeout !\n");				break;			}			stat = in_8(base) ^ in_8(base);		} while (stat != 0);		if (stat != 0)			break;	}	/* Reset */	out_8(base, 0xf0);	udelay(1);	for (i=0; i<NVRAM_SIZE; i++)		if (base[i] != datas[i]) {			printk(KERN_ERR "nvram: AMD flash write failed !\n");			return -ENXIO;		}	return 0;}static void __init lookup_partitions(void){	u8 buffer[17];	int i, offset;	struct chrp_header* hdr;	if (pmac_newworld) {		nvram_partitions[pmac_nvram_OF] = -1;		nvram_partitions[pmac_nvram_XPRAM] = -1;		nvram_partitions[pmac_nvram_NR] = -1;		hdr = (struct chrp_header *)buffer;		offset = 0;		buffer[16] = 0;		do {			for (i=0;i<16;i++)				buffer[i] = ppc_md.nvram_read_val(offset+i);			if (!strcmp(hdr->name, "common"))				nvram_partitions[pmac_nvram_OF] = offset + 0x10;			if (!strcmp(hdr->name, "APL,MacOS75")) {				nvram_partitions[pmac_nvram_XPRAM] = offset + 0x10;				nvram_partitions[pmac_nvram_NR] = offset + 0x110;			}			offset += (hdr->len * 0x10);		} while(offset < NVRAM_SIZE);	} else {		nvram_partitions[pmac_nvram_OF] = 0x1800;		nvram_partitions[pmac_nvram_XPRAM] = 0x1300;		nvram_partitions[pmac_nvram_NR] = 0x1400;	}	DBG("nvram: OF partition at 0x%x\n", nvram_partitions[pmac_nvram_OF]);	DBG("nvram: XP partition at 0x%x\n", nvram_partitions[pmac_nvram_XPRAM]);	DBG("nvram: NR partition at 0x%x\n", nvram_partitions[pmac_nvram_NR]);}static void core99_nvram_sync(void){	struct core99_header* hdr99;	unsigned long flags;	if (!is_core_99 || !nvram_data || !nvram_image)		return;	spin_lock_irqsave(&nv_lock, flags);	if (!memcmp(nvram_image, (u8*)nvram_data + core99_bank*NVRAM_SIZE,		NVRAM_SIZE))		goto bail;	DBG("Updating nvram...\n");	hdr99 = (struct core99_header*)nvram_image;	hdr99->generation++;	hdr99->hdr.signature = CORE99_SIGNATURE;	hdr99->hdr.cksum = chrp_checksum(&hdr99->hdr);	hdr99->adler = core99_calc_adler(nvram_image);	core99_bank = core99_bank ? 0 : 1;	if (core99_erase_bank)		if (core99_erase_bank(core99_bank)) {			printk("nvram: Error erasing bank %d\n", core99_bank);			goto bail;		}	if (core99_write_bank)		if (core99_write_bank(core99_bank, nvram_image))			printk("nvram: Error writing bank %d\n", core99_bank); bail:	spin_unlock_irqrestore(&nv_lock, flags);#ifdef DEBUG       	mdelay(2000);#endif}static int __init core99_nvram_setup(struct device_node *dp, unsigned long addr){	int i;	u32 gen_bank0, gen_bank1;	if (nvram_naddrs < 1) {		printk(KERN_ERR "nvram: no address\n");		return -EINVAL;	}	nvram_image = alloc_bootmem(NVRAM_SIZE);	if (nvram_image == NULL) {		printk(KERN_ERR "nvram: can't allocate ram image\n");		return -ENOMEM;	}	nvram_data = ioremap(addr, NVRAM_SIZE*2);	nvram_naddrs = 1; /* Make sure we get the correct case */	DBG("nvram: Checking bank 0...\n");	gen_bank0 = core99_check((u8 *)nvram_data);	gen_bank1 = core99_check((u8 *)nvram_data + NVRAM_SIZE);	core99_bank = (gen_bank0 < gen_bank1) ? 1 : 0;	DBG("nvram: gen0=%d, gen1=%d\n", gen_bank0, gen_bank1);	DBG("nvram: Active bank is: %d\n", core99_bank);	for (i=0; i<NVRAM_SIZE; i++)		nvram_image[i] = nvram_data[i + core99_bank*NVRAM_SIZE];	ppc_md.nvram_read_val	= core99_nvram_read_byte;	ppc_md.nvram_write_val	= core99_nvram_write_byte;	ppc_md.nvram_read	= core99_nvram_read;	ppc_md.nvram_write	= core99_nvram_write;	ppc_md.nvram_size	= core99_nvram_size;	ppc_md.nvram_sync	= core99_nvram_sync;	ppc_md.machine_shutdown	= core99_nvram_sync;	/* 	 * Maybe we could be smarter here though making an exclusive list	 * of known flash chips is a bit nasty as older OF didn't provide us	 * with a useful "compatible" entry. A solution would be to really	 * identify the chip using flash id commands and base ourselves on	 * a list of known chips IDs	 */	if (of_device_is_compatible(dp, "amd-0137")) {		core99_erase_bank = amd_erase_bank;		core99_write_bank = amd_write_bank;	} else {		core99_erase_bank = sm_erase_bank;		core99_write_bank = sm_write_bank;	}	return 0;}int __init pmac_nvram_init(void){	struct device_node *dp;	struct resource r1, r2;	unsigned int s1 = 0, s2 = 0;	int err = 0;	nvram_naddrs = 0;	dp = of_find_node_by_name(NULL, "nvram");	if (dp == NULL) {		printk(KERN_ERR "Can't find NVRAM device\n");		return -ENODEV;	}	/* Try to obtain an address */	if (of_address_to_resource(dp, 0, &r1) == 0) {		nvram_naddrs = 1;		s1 = (r1.end - r1.start) + 1;		if (of_address_to_resource(dp, 1, &r2) == 0) {			nvram_naddrs = 2;			s2 = (r2.end - r2.start) + 1;		}	}	is_core_99 = of_device_is_compatible(dp, "nvram,flash");	if (is_core_99) {		err = core99_nvram_setup(dp, r1.start);		goto bail;	}#ifdef CONFIG_PPC32	if (machine_is(chrp) && nvram_naddrs == 1) {		nvram_data = ioremap(r1.start, s1);		nvram_mult = 1;		ppc_md.nvram_read_val	= direct_nvram_read_byte;		ppc_md.nvram_write_val	= direct_nvram_write_byte;	} else if (nvram_naddrs == 1) {		nvram_data = ioremap(r1.start, s1);		nvram_mult = (s1 + NVRAM_SIZE - 1) / NVRAM_SIZE;		ppc_md.nvram_read_val	= direct_nvram_read_byte;		ppc_md.nvram_write_val	= direct_nvram_write_byte;	} else if (nvram_naddrs == 2) {		nvram_addr = ioremap(r1.start, s1);		nvram_data = ioremap(r2.start, s2);		ppc_md.nvram_read_val	= indirect_nvram_read_byte;		ppc_md.nvram_write_val	= indirect_nvram_write_byte;	} else if (nvram_naddrs == 0 && sys_ctrler == SYS_CTRLER_PMU) {#ifdef CONFIG_ADB_PMU		nvram_naddrs = -1;		ppc_md.nvram_read_val	= pmu_nvram_read_byte;		ppc_md.nvram_write_val	= pmu_nvram_write_byte;#endif /* CONFIG_ADB_PMU */	} else {		printk(KERN_ERR "Incompatible type of NVRAM\n");		err = -ENXIO;	}#endif /* CONFIG_PPC32 */bail:	of_node_put(dp);	if (err == 0)		lookup_partitions();	return err;}int pmac_get_partition(int partition){	return nvram_partitions[partition];}u8 pmac_xpram_read(int xpaddr){	int offset = pmac_get_partition(pmac_nvram_XPRAM);	if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)		return 0xff;	return ppc_md.nvram_read_val(xpaddr + offset);}void pmac_xpram_write(int xpaddr, u8 data){	int offset = pmac_get_partition(pmac_nvram_XPRAM);	if (offset < 0 || xpaddr < 0 || xpaddr > 0x100)		return;	ppc_md.nvram_write_val(xpaddr + offset, data);}EXPORT_SYMBOL(pmac_get_partition);EXPORT_SYMBOL(pmac_xpram_read);EXPORT_SYMBOL(pmac_xpram_write);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -