Subject: [PATCH] nvram driver as module (was Re: nvram driver not possible as a module)

From: Joerg Dorchain <[email-masked]>
Date: Sat, 22 Jan 2005 22:20:32 +0100

On Thu, Jan 20, 2005 at 11:50:26AM +0100, Joerg Dorchain wrote:
> > Does this help:
> [...]
> 
> Most likely, if there wasn't a specific reason to use bootmem.
> 
> I'll try tonight.

Well, it did not crash. The following does even work ;-)

Signed-off-by: Joerg Dorchain <[email-masked]>

--- ./include/asm-ppc/system.h.orig	2005-01-22 21:30:19.000000000 +0100
+++ ./include/asm-ppc/system.h	2005-01-22 21:28:48.335018120 +0100
@@ -69,7 +69,7 @@
 #define _set_L3CR(val)	do { } while(0)
 #endif
 extern void via_cuda_init(void);
-extern void pmac_nvram_init(void);
+extern int pmac_nvram_init(void);
 extern void read_rtc_time(void);
 extern void pmac_find_display(void);
 extern void giveup_fpu(struct task_struct *);
--- arch/ppc/platforms/pmac_nvram.c.orig	2005-01-22 20:46:21.000000000 +0100
+++ arch/ppc/platforms/pmac_nvram.c	2005-01-22 22:15:10.000000000 +0100
@@ -75,8 +75,8 @@
  * Read and write the non-volatile RAM on PowerMacs and CHRP machines.
  */
 static int nvram_naddrs;
-static volatile unsigned char *nvram_addr;
-static volatile unsigned char *nvram_data;
+static volatile unsigned char *nvram_addr = NULL;
+static volatile unsigned char *nvram_data = NULL;
 static int nvram_mult, is_core_99;
 static int core99_bank = 0;
 static int nvram_partitions[3];
@@ -88,7 +88,7 @@
 static int (*core99_write_bank)(int bank, u8* datas);
 static int (*core99_erase_bank)(int bank);
 
-static char *nvram_image __pmacdata;
+static char *nvram_image __pmacdata = NULL;

 static unsigned char __pmac core99_nvram_read_byte(int addr)
@@ -467,7 +467,7 @@
 #endif
 }
 
-void __init pmac_nvram_init(void)
+int __init pmac_nvram_init(void)
 {
 	struct device_node *dp;
 
@@ -476,7 +476,7 @@
 	dp = find_devices("nvram");
 	if (dp == NULL) {
 		printk(KERN_ERR "Can't find NVRAM device\n");
-		return;
+		return -ENODEV;
 	}
 	nvram_naddrs = dp->n_addrs;
 	is_core_99 = device_is_compatible(dp, "nvram,flash");
@@ -486,12 +486,16 @@
 
 		if (nvram_naddrs < 1) {
 			printk(KERN_ERR "nvram: no address\n");
-			return;
+			return -ENODEV;
 		}
+#ifdef MODULE
+		nvram_image = kmalloc(NVRAM_SIZE, GFP_KERNEL);
+#else
 		nvram_image = alloc_bootmem(NVRAM_SIZE);
+#endif
 		if (nvram_image == NULL) {
 			printk(KERN_ERR "nvram: can't allocate ram image\n");
-			return;
+			return -ENODEV;
 		}
 		nvram_data = ioremap(dp->addrs[0].address, NVRAM_SIZE*2);
 		nvram_naddrs = 1; /* Make sure we get the correct case */
@@ -550,8 +554,10 @@
 	} else {
 		printk(KERN_ERR "Don't know how to access NVRAM with %d addresses\n",
 		       nvram_naddrs);
+		return -ENODEV;
 	}
 	lookup_partitions();
+	return 0;
 }
 
 int __pmac pmac_get_partition(int partition)
@@ -579,6 +585,29 @@
 	ppc_md.nvram_write_val(xpaddr + offset, data);
 }
 
+static void pmac_nvram_exit(void)
+{
+#ifdef MODULE
+	if (ppc_md.nvram_sync)
+		ppc_md.nvram_sync();
+	if (nvram_addr)
+		iounmap(nvram_addr);
+	if (nvram_data)
+		iounmap(nvram_data);
+	if (nvram_image)
+		kfree(nvram_image);
+	nvram_addr=NULL;
+	nvram_data=NULL;
+	nvram_image=NULL;
+	ppc_md.nvram_read_val	= NULL;
+	ppc_md.nvram_write_val	= NULL;
+	ppc_md.nvram_sync	= NULL;
+#endif
+}
+
+MODULE_LICENSE("GPL");
+module_exit(pmac_nvram_exit);
+module_init(pmac_nvram_init);
 EXPORT_SYMBOL(pmac_get_partition);
 EXPORT_SYMBOL(pmac_xpram_read);
 EXPORT_SYMBOL(pmac_xpram_write);