1 /*
2  *                     OpenBIOS - free your system!
3  *              ( firmware/flash device driver for Linux )
4  *
5  *  bios_core.c - core skeleton
6  *
7  *  This program is part of a free implementation of the IEEE 1275-1994
8  *  Standard for Boot (Initialization Configuration) Firmware.
9  *
10  *  Copyright (C) 1998-2004  Stefan Reinauer
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; version 2 of the License.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
24  *
25  */
26 
27 #include <linux/config.h>
28 #include <linux/version.h>
29 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
30 #ifdef MODULE
31 #ifdef MODVERSIONS
32 #include <linux/modversions.h>
33 #endif
34 #endif
35 #include <linux/module.h>
36 #endif
37 #include <linux/pci.h>
38 #include <linux/errno.h>
39 #include <linux/vmalloc.h>
40 #include <linux/init.h>
41 
42 #include <asm/io.h>
43 
44 #include "bios.h"
45 #include "pcisets.h"
46 #include "flashchips.h"
47 #include "programming.h"
48 
49 extern struct file_operations bios_fops;
50 int bios_proc_register(void);
51 int bios_proc_unregister(void);
52 
53 int write = 0;
54 
55 spinlock_t bios_lock = SPIN_LOCK_UNLOCKED;
56 
57 /*
58  * ******************************************
59  *
60  *      Cleanup
61  *
62  * ******************************************
63  */
64 
free_iomaps(void)65 static void free_iomaps(void)
66 {
67 	unsigned long lastmapped=0;
68 	unsigned int i;
69 
70 	/* We remember the last mapped area to be sure that we only iounmap
71 	 * every mapped area once. If two flash devices are in the same
72 	 * area but do not occur sequentially during probing you have a
73 	 * seriously strange hardware
74 	 */
75 	for (i=0; i<flashcount; i++) {
76 		if (lastmapped==flashdevices[i].mapped)
77 			continue;
78 		iounmap((void *)flashdevices[i].mapped);
79 		lastmapped=flashdevices[i].mapped;
80 	}
81 }
82 
83 /*
84  * ******************************************
85  *
86  *	Initialization
87  *
88  * ******************************************
89  */
90 
probe_system(void)91 void probe_system(void)
92 {
93 #ifdef __alpha__
94 	probe_alphafw();
95 #endif
96 	/* This function checks all flash media attached to
97 	 * PCI devices in the system. This means NON-PCI systems
98 	 * don't work. This causes machine checks on my LX164 test
99 	 * machine, so leave it away until it's fixed. This is
100 	 * needed for Ruffians, so we check the machine type
101 	 * in probe_alphafw() and call probe_pcibus from there.
102 	 * This could use some cleanup
103 	 */
104 #ifndef __alpha__
105 	probe_pcibus();
106 #endif
107 }
108 
bios_init(void)109 static __init int bios_init(void)
110 {
111 	printk(KERN_INFO "BIOS driver v" BIOS_VERSION " (writing %s) for "
112 			UTS_RELEASE "\n", write?"enabled":"disabled");
113 
114 #if !defined(UTC_BIOS) && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
115 	if (!pci_present()) {
116 		printk(KERN_WARNING "BIOS: No PCI system.");
117 		return -EBUSY;
118 	}
119 #endif
120 
121 	/* Probe for flash devices */
122 	probe_system();
123 
124 	if (flashcount==0) {
125 		printk(KERN_WARNING "BIOS: No flash devices found.\n");
126 		return -EBUSY;
127 	}
128 
129 	if (register_chrdev(BIOS_MAJOR, "bios",  &bios_fops) == -EBUSY) {
130 		printk(KERN_WARNING "BIOS: Could not register bios device.\n");
131 		free_iomaps();
132 		return -EBUSY;
133 	}
134 
135 #ifdef CONFIG_PROC_FS
136 	bios_proc_register();
137 #endif
138 	return 0;
139 }
140 
141 /*
142  * ******************************************
143  *
144  *	module handling
145  *
146  * ******************************************
147  */
148 
149 #ifdef MODULE
150 MODULE_PARM(write,"i");
151 MODULE_AUTHOR("Stefan Reinauer <stefan.reinauer@coreboot.org>");
152 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,10)
153 MODULE_LICENSE("GPL");
154 #endif
155 
cleanup_bios_module(void)156 static __exit void cleanup_bios_module (void)
157 {
158 #ifdef CONFIG_PROC_FS
159 	bios_proc_unregister();
160 #endif
161 	free_iomaps();
162 
163 	unregister_chrdev(BIOS_MAJOR, "bios");
164 	printk(KERN_INFO "BIOS driver removed.\n");
165 }
166 
167 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
init_module(void)168 int init_module(void)
169 {
170 	return bios_init();
171 }
172 
cleanup_module(void)173 void cleanup_module(void)
174 {
175 	cleanup_bios_module();
176 }
177 #endif
178 
179 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,74)
180 module_init(bios_init);
181 module_exit(cleanup_bios_module);
182 #endif
183 
inc_mod(void)184 void inc_mod(void)
185 {
186 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
187 	MOD_INC_USE_COUNT;
188 #endif
189 }
190 
dec_mod(void)191 void dec_mod(void)
192 {
193 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
194 	MOD_DEC_USE_COUNT;
195 #endif
196 }
197 
198 #endif
199