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