ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / mach-voyager / voyager_basic.c
1 /* Copyright (C) 1999,2001 
2  *
3  * Author: J.E.J.Bottomley@HansenPartnership.com
4  *
5  * linux/arch/i386/kernel/voyager.c
6  *
7  * This file contains all the voyager specific routines for getting
8  * initialisation of the architecture to function.  For additional
9  * features see:
10  *
11  *      voyager_cat.c - Voyager CAT bus interface
12  *      voyager_smp.c - Voyager SMP hal (emulates linux smp.c)
13  */
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/sched.h>
19 #include <linux/ptrace.h>
20 #include <linux/ioport.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/reboot.h>
25 #include <linux/sysrq.h>
26 #include <asm/io.h>
27 #include <asm/pgalloc.h>
28 #include <asm/voyager.h>
29 #include <asm/vic.h>
30 #include <linux/pm.h>
31 #include <linux/irq.h>
32 #include <asm/tlbflush.h>
33 #include <asm/arch_hooks.h>
34
35 /*
36  * Power off function, if any
37  */
38 void (*pm_power_off)(void);
39
40 int reboot_thru_bios;
41
42 int voyager_level = 0;
43
44 struct voyager_SUS *voyager_SUS = NULL;
45
46 #ifdef CONFIG_SMP
47 static void
48 voyager_dump(int dummy1, struct pt_regs *dummy2, struct tty_struct *dummy3)
49 {
50         /* get here via a sysrq */
51         voyager_smp_dump();
52 }
53
54 static struct sysrq_key_op sysrq_voyager_dump_op = {
55         .handler        = voyager_dump,
56         .help_msg       = "Voyager",
57         .action_msg     = "Dump Voyager Status\n",
58 };
59 #endif
60
61 void
62 voyager_detect(struct voyager_bios_info *bios)
63 {
64         if(bios->len != 0xff) {
65                 int class = (bios->class_1 << 8) 
66                         | (bios->class_2 & 0xff);
67
68                 printk("Voyager System detected.\n"
69                        "        Class %x, Revision %d.%d\n",
70                        class, bios->major, bios->minor);
71                 if(class == VOYAGER_LEVEL4) 
72                         voyager_level = 4;
73                 else if(class < VOYAGER_LEVEL5_AND_ABOVE)
74                         voyager_level = 3;
75                 else
76                         voyager_level = 5;
77                 printk("        Architecture Level %d\n", voyager_level);
78                 if(voyager_level < 4)
79                         printk("\n**WARNING**: Voyager HAL only supports Levels 4 and 5 Architectures at the moment\n\n");
80                 /* install the power off handler */
81                 pm_power_off = voyager_power_off;
82 #ifdef CONFIG_SMP
83                 register_sysrq_key('v', &sysrq_voyager_dump_op);
84 #endif
85         } else {
86                 printk("\n\n**WARNING**: No Voyager Subsystem Found\n");
87         }
88 }
89
90 void
91 voyager_system_interrupt(int cpl, void *dev_id, struct pt_regs *regs)
92 {
93         printk("Voyager: detected system interrupt\n");
94 }
95
96 /* Routine to read information from the extended CMOS area */
97 __u8
98 voyager_extended_cmos_read(__u16 addr)
99 {
100         outb(addr & 0xff, 0x74);
101         outb((addr >> 8) & 0xff, 0x75);
102         return inb(0x76);
103 }
104
105 /* internal definitions for the SUS Click Map of memory */
106
107 #define CLICK_ENTRIES   16
108 #define CLICK_SIZE      4096    /* click to byte conversion for Length */
109
110 typedef struct ClickMap {
111         struct Entry {
112                 __u32   Address;
113                 __u32   Length;
114         } Entry[CLICK_ENTRIES];
115 } ClickMap_t;
116
117
118 /* This routine is pretty much an awful hack to read the bios clickmap by
119  * mapping it into page 0.  There are usually three regions in the map:
120  *      Base Memory
121  *      Extended Memory
122  *      zero length marker for end of map
123  *
124  * Returns are 0 for failure and 1 for success on extracting region.
125  */
126 int __init
127 voyager_memory_detect(int region, __u32 *start, __u32 *length)
128 {
129         int i;
130         int retval = 0;
131         __u8 cmos[4];
132         ClickMap_t *map;
133         unsigned long map_addr;
134         unsigned long old;
135
136         if(region >= CLICK_ENTRIES) {
137                 printk("Voyager: Illegal ClickMap region %d\n", region);
138                 return 0;
139         }
140
141         for(i = 0; i < sizeof(cmos); i++)
142                 cmos[i] = voyager_extended_cmos_read(VOYAGER_MEMORY_CLICKMAP + i);
143
144         map_addr = *(unsigned long *)cmos;
145
146         /* steal page 0 for this */
147         old = pg0[0];
148         pg0[0] = ((map_addr & PAGE_MASK) | _PAGE_RW | _PAGE_PRESENT);
149         local_flush_tlb();
150         /* now clear everything out but page 0 */
151         map = (ClickMap_t *)(map_addr & (~PAGE_MASK));
152
153         /* zero length is the end of the clickmap */
154         if(map->Entry[region].Length != 0) {
155                 *length = map->Entry[region].Length * CLICK_SIZE;
156                 *start = map->Entry[region].Address;
157                 retval = 1;
158         }
159
160         /* replace the mapping */
161         pg0[0] = old;
162         local_flush_tlb();
163         return retval;
164 }
165
166 /* voyager specific handling code for timer interrupts.  Used to hand
167  * off the timer tick to the SMP code, since the VIC doesn't have an
168  * internal timer (The QIC does, but that's another story). */
169 void
170 voyager_timer_interrupt(struct pt_regs *regs)
171 {
172         if((jiffies & 0x3ff) == 0) {
173
174                 /* There seems to be something flaky in either
175                  * hardware or software that is resetting the timer 0
176                  * count to something much higher than it should be
177                  * This seems to occur in the boot sequence, just
178                  * before root is mounted.  Therefore, every 10
179                  * seconds or so, we sanity check the timer zero count
180                  * and kick it back to where it should be.
181                  *
182                  * FIXME: This is the most awful hack yet seen.  I
183                  * should work out exactly what is interfering with
184                  * the timer count settings early in the boot sequence
185                  * and swiftly introduce it to something sharp and
186                  * pointy.  */
187                 __u16 val;
188                 extern spinlock_t i8253_lock;
189
190                 spin_lock(&i8253_lock);
191                 
192                 outb_p(0x00, 0x43);
193                 val = inb_p(0x40);
194                 val |= inb(0x40) << 8;
195                 spin_unlock(&i8253_lock);
196
197                 if(val > LATCH) {
198                         printk("\nVOYAGER: countdown timer value too high (%d), resetting\n\n", val);
199                         spin_lock(&i8253_lock);
200                         outb(0x34,0x43);
201                         outb_p(LATCH & 0xff , 0x40);    /* LSB */
202                         outb(LATCH >> 8 , 0x40);        /* MSB */
203                         spin_unlock(&i8253_lock);
204                 }
205         }
206 #ifdef CONFIG_SMP
207         smp_vic_timer_interrupt(regs);
208 #endif
209 }
210
211 void
212 voyager_power_off(void)
213 {
214         printk("VOYAGER Power Off\n");
215
216         if(voyager_level == 5) {
217                 voyager_cat_power_off();
218         } else if(voyager_level == 4) {
219                 /* This doesn't apparently work on most L4 machines,
220                  * but the specs say to do this to get automatic power
221                  * off.  Unfortunately, if it doesn't power off the
222                  * machine, it ends up doing a cold restart, which
223                  * isn't really intended, so comment out the code */
224 #if 0
225                 int port;
226
227           
228                 /* enable the voyager Configuration Space */
229                 outb((inb(VOYAGER_MC_SETUP) & 0xf0) | 0x8, 
230                      VOYAGER_MC_SETUP);
231                 /* the port for the power off flag is an offset from the
232                    floating base */
233                 port = (inb(VOYAGER_SSPB_RELOCATION_PORT) << 8) + 0x21;
234                 /* set the power off flag */
235                 outb(inb(port) | 0x1, port);
236 #endif
237         }
238         /* and wait for it to happen */
239         for(;;) {
240                 __asm("cli");
241                 __asm("hlt");
242         }
243 }
244
245 /* copied from process.c */
246 static inline void
247 kb_wait(void)
248 {
249         int i;
250
251         for (i=0; i<0x10000; i++)
252                 if ((inb_p(0x64) & 0x02) == 0)
253                         break;
254 }
255
256 void
257 machine_restart(char *cmd)
258 {
259         printk("Voyager Warm Restart\n");
260         kb_wait();
261
262         if(voyager_level == 5) {
263                 /* write magic values to the RTC to inform system that
264                  * shutdown is beginning */
265                 outb(0x8f, 0x70);
266                 outb(0x5 , 0x71);
267                 
268                 udelay(50);
269                 outb(0xfe,0x64);         /* pull reset low */
270         } else if(voyager_level == 4) {
271                 __u16 catbase = inb(VOYAGER_SSPB_RELOCATION_PORT)<<8;
272                 __u8 basebd = inb(VOYAGER_MC_SETUP);
273                 
274                 outb(basebd | 0x08, VOYAGER_MC_SETUP);
275                 outb(0x02, catbase + 0x21);
276         }
277         for(;;) {
278                 asm("cli");
279                 asm("hlt");
280         }
281 }
282
283 EXPORT_SYMBOL(machine_restart);
284
285 void
286 mca_nmi_hook(void)
287 {
288         __u8 dumpval __attribute__((unused)) = inb(0xf823);
289         __u8 swnmi __attribute__((unused)) = inb(0xf813);
290
291         /* FIXME: assume dump switch pressed */
292         /* check to see if the dump switch was pressed */
293         VDEBUG(("VOYAGER: dumpval = 0x%x, swnmi = 0x%x\n", dumpval, swnmi));
294         /* clear swnmi */
295         outb(0xff, 0xf813);
296         /* tell SUS to ignore dump */
297         if(voyager_level == 5 && voyager_SUS != NULL) {
298                 if(voyager_SUS->SUS_mbox == VOYAGER_DUMP_BUTTON_NMI) {
299                         voyager_SUS->kernel_mbox = VOYAGER_NO_COMMAND;
300                         voyager_SUS->kernel_flags |= VOYAGER_OS_IN_PROGRESS;
301                         udelay(1000);
302                         voyager_SUS->kernel_mbox = VOYAGER_IGNORE_DUMP;
303                         voyager_SUS->kernel_flags &= ~VOYAGER_OS_IN_PROGRESS;
304                 }
305         }
306         printk(KERN_ERR "VOYAGER: Dump switch pressed, printing CPU%d tracebacks\n", smp_processor_id());
307         show_stack(NULL, NULL);
308         show_state();
309 }
310
311
312
313 void
314 machine_halt(void)
315 {
316         /* treat a halt like a power off */
317         machine_power_off();
318 }
319
320 EXPORT_SYMBOL(machine_halt);
321
322 void machine_power_off(void)
323 {
324         if (pm_power_off)
325                 pm_power_off();
326 }
327
328 EXPORT_SYMBOL(machine_power_off);