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

📄 nvram.c

📁 linux内核源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (C) 2002 Benjamin Herrenschmidt (benh@kernel.crashing.org) * *  This program is free software; you can redistribute it and/or *  modify it under the terms of the GNU General Public License *  as published by the Free Software Foundation; either version *  2 of the License, or (at your option) any later version. * *  Todo: - add support for the OF persistent properties */#include <linux/module.h>#include <linux/kernel.h>#include <linux/stddef.h>#include <linux/string.h>#include <linux/nvram.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/errno.h>#include <linux/adb.h>#include <linux/pmu.h>#include <linux/bootmem.h>#include <linux/completion.h>#include <linux/spinlock.h>#include <asm/sections.h>#include <asm/io.h>#include <asm/system.h>#include <asm/prom.h>#include <asm/machdep.h>#include <asm/nvram.h>#include "pmac.h"#define DEBUG#ifdef DEBUG#define DBG(x...) printk(x)#else#define DBG(x...)#endif#define NVRAM_SIZE		0x2000	/* 8kB of non-volatile RAM */#define CORE99_SIGNATURE	0x5a#define CORE99_ADLER_START	0x14/* On Core99, nvram is either a sharp, a micron or an AMD flash */#define SM_FLASH_STATUS_DONE	0x80#define SM_FLASH_STATUS_ERR	0x38#define SM_FLASH_CMD_ERASE_CONFIRM	0xd0#define SM_FLASH_CMD_ERASE_SETUP	0x20#define SM_FLASH_CMD_RESET		0xff#define SM_FLASH_CMD_WRITE_SETUP	0x40#define SM_FLASH_CMD_CLEAR_STATUS	0x50#define SM_FLASH_CMD_READ_STATUS	0x70/* CHRP NVRAM header */struct chrp_header {  u8		signature;  u8		cksum;  u16		len;  char          name[12];  u8		data[0];};struct core99_header {  struct chrp_header	hdr;  u32			adler;  u32			generation;  u32			reserved[2];};/* * Read and write the non-volatile RAM on PowerMacs and CHRP machines. */static int nvram_naddrs;static volatile unsigned char __iomem *nvram_data;static int is_core_99;static int core99_bank = 0;static int nvram_partitions[3];// XXX Turn that into a semstatic DEFINE_SPINLOCK(nv_lock);static int (*core99_write_bank)(int bank, u8* datas);static int (*core99_erase_bank)(int bank);static char *nvram_image;static unsigned char core99_nvram_read_byte(int addr){	if (nvram_image == NULL)		return 0xff;	return nvram_image[addr];}static void core99_nvram_write_byte(int addr, unsigned char val){	if (nvram_image == NULL)		return;	nvram_image[addr] = val;}static ssize_t core99_nvram_read(char *buf, size_t count, loff_t *index){	int i;	if (nvram_image == NULL)		return -ENODEV;	if (*index > NVRAM_SIZE)		return 0;	i = *index;	if (i + count > NVRAM_SIZE)		count = NVRAM_SIZE - i;	memcpy(buf, &nvram_image[i], count);	*index = i + count;	return count;}static ssize_t core99_nvram_write(char *buf, size_t count, loff_t *index){	int i;	if (nvram_image == NULL)		return -ENODEV;	if (*index > NVRAM_SIZE)		return 0;	i = *index;	if (i + count > NVRAM_SIZE)		count = NVRAM_SIZE - i;	memcpy(&nvram_image[i], buf, count);	*index = i + count;	return count;}static ssize_t core99_nvram_size(void){	if (nvram_image == NULL)		return -ENODEV;	return NVRAM_SIZE;}#ifdef CONFIG_PPC32static volatile unsigned char __iomem *nvram_addr;static int nvram_mult;static unsigned char direct_nvram_read_byte(int addr){	return in_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult]);}static void direct_nvram_write_byte(int addr, unsigned char val){	out_8(&nvram_data[(addr & (NVRAM_SIZE - 1)) * nvram_mult], val);}static unsigned char indirect_nvram_read_byte(int addr){	unsigned char val;	unsigned long flags;	spin_lock_irqsave(&nv_lock, flags);	out_8(nvram_addr, addr >> 5);	val = in_8(&nvram_data[(addr & 0x1f) << 4]);	spin_unlock_irqrestore(&nv_lock, flags);	return val;}static void indirect_nvram_write_byte(int addr, unsigned char val){	unsigned long flags;	spin_lock_irqsave(&nv_lock, flags);	out_8(nvram_addr, addr >> 5);	out_8(&nvram_data[(addr & 0x1f) << 4], val);	spin_unlock_irqrestore(&nv_lock, flags);}#ifdef CONFIG_ADB_PMUstatic void pmu_nvram_complete(struct adb_request *req){	if (req->arg)		complete((struct completion *)req->arg);}static unsigned char pmu_nvram_read_byte(int addr){	struct adb_request req;	DECLARE_COMPLETION_ONSTACK(req_complete);		req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;	if (pmu_request(&req, pmu_nvram_complete, 3, PMU_READ_NVRAM,			(addr >> 8) & 0xff, addr & 0xff))		return 0xff;	if (system_state == SYSTEM_RUNNING)		wait_for_completion(&req_complete);	while (!req.complete)		pmu_poll();	return req.reply[0];}static void pmu_nvram_write_byte(int addr, unsigned char val){	struct adb_request req;	DECLARE_COMPLETION_ONSTACK(req_complete);		req.arg = system_state == SYSTEM_RUNNING ? &req_complete : NULL;	if (pmu_request(&req, pmu_nvram_complete, 4, PMU_WRITE_NVRAM,			(addr >> 8) & 0xff, addr & 0xff, val))		return;	if (system_state == SYSTEM_RUNNING)		wait_for_completion(&req_complete);	while (!req.complete)		pmu_poll();}#endif /* CONFIG_ADB_PMU */#endif /* CONFIG_PPC32 */static u8 chrp_checksum(struct chrp_header* hdr){	u8 *ptr;	u16 sum = hdr->signature;	for (ptr = (u8 *)&hdr->len; ptr < hdr->data; ptr++)		sum += *ptr;	while (sum > 0xFF)		sum = (sum & 0xFF) + (sum>>8);	return sum;}static u32 core99_calc_adler(u8 *buffer){	int cnt;	u32 low, high;   	buffer += CORE99_ADLER_START;	low = 1;	high = 0;	for (cnt=0; cnt<(NVRAM_SIZE-CORE99_ADLER_START); cnt++) {		if ((cnt % 5000) == 0) {			high  %= 65521UL;			high %= 65521UL;		}		low += buffer[cnt];		high += low;	}	low  %= 65521UL;	high %= 65521UL;	return (high << 16) | low;}static u32 core99_check(u8* datas){	struct core99_header* hdr99 = (struct core99_header*)datas;	if (hdr99->hdr.signature != CORE99_SIGNATURE) {		DBG("Invalid signature\n");		return 0;	}	if (hdr99->hdr.cksum != chrp_checksum(&hdr99->hdr)) {		DBG("Invalid checksum\n");		return 0;	}	if (hdr99->adler != core99_calc_adler(datas)) {		DBG("Invalid adler\n");		return 0;	}	return hdr99->generation;}static int sm_erase_bank(int bank){	int stat, i;	unsigned long timeout;	u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;       	DBG("nvram: Sharp/Micron Erasing bank %d...\n", bank);	out_8(base, SM_FLASH_CMD_ERASE_SETUP);	out_8(base, SM_FLASH_CMD_ERASE_CONFIRM);	timeout = 0;	do {		if (++timeout > 1000000) {			printk(KERN_ERR "nvram: Sharp/Micron flash erase timeout !\n");			break;		}		out_8(base, SM_FLASH_CMD_READ_STATUS);		stat = in_8(base);	} while (!(stat & SM_FLASH_STATUS_DONE));	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] != 0xff) {			printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");			return -ENXIO;		}	return 0;}static int sm_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: Sharp/Micron Writing bank %d...\n", bank);	for (i=0; i<NVRAM_SIZE; i++) {		out_8(base+i, SM_FLASH_CMD_WRITE_SETUP);		udelay(1);		out_8(base+i, datas[i]);		timeout = 0;		do {			if (++timeout > 1000000) {				printk(KERN_ERR "nvram: Sharp/Micron flash write timeout !\n");				break;

⌨️ 快捷键说明

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