Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / arch / i386 / kernel / mca.c
1 /*
2  *  linux/arch/i386/kernel/mca.c
3  *  Written by Martin Kolinek, February 1996
4  *
5  * Changes:
6  *
7  *      Chris Beauregard July 28th, 1996
8  *      - Fixed up integrated SCSI detection
9  *
10  *      Chris Beauregard August 3rd, 1996
11  *      - Made mca_info local
12  *      - Made integrated registers accessible through standard function calls
13  *      - Added name field
14  *      - More sanity checking
15  *
16  *      Chris Beauregard August 9th, 1996
17  *      - Rewrote /proc/mca
18  *
19  *      Chris Beauregard January 7th, 1997
20  *      - Added basic NMI-processing
21  *      - Added more information to mca_info structure
22  *
23  *      David Weinehall October 12th, 1998
24  *      - Made a lot of cleaning up in the source
25  *      - Added use of save_flags / restore_flags
26  *      - Added the 'driver_loaded' flag in MCA_adapter
27  *      - Added an alternative implemention of ZP Gu's mca_find_unused_adapter
28  *
29  *      David Weinehall March 24th, 1999
30  *      - Fixed the output of 'Driver Installed' in /proc/mca/pos
31  *      - Made the Integrated Video & SCSI show up even if they have id 0000
32  *
33  *      Alexander Viro November 9th, 1999
34  *      - Switched to regular procfs methods
35  *
36  *      Alfred Arnold & David Weinehall August 23rd, 2000
37  *      - Added support for Planar POS-registers
38  */
39
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/mca.h>
45 #include <asm/system.h>
46 #include <asm/io.h>
47 #include <linux/proc_fs.h>
48 #include <linux/mman.h>
49 #include <linux/config.h>
50 #include <linux/mm.h>
51 #include <linux/pagemap.h>
52 #include <linux/ioport.h>
53 #include <asm/uaccess.h>
54 #include <linux/init.h>
55 #include <asm/arch_hooks.h>
56
57 static unsigned char which_scsi = 0;
58
59 int MCA_bus = 0;
60 EXPORT_SYMBOL(MCA_bus);
61
62 /*
63  * Motherboard register spinlock. Untested on SMP at the moment, but
64  * are there any MCA SMP boxes?
65  *
66  * Yes - Alan
67  */
68 static DEFINE_SPINLOCK(mca_lock);
69
70 /* Build the status info for the adapter */
71
72 static void mca_configure_adapter_status(struct mca_device *mca_dev) {
73         mca_dev->status = MCA_ADAPTER_NONE;
74
75         mca_dev->pos_id = mca_dev->pos[0]
76                 + (mca_dev->pos[1] << 8);
77
78         if(!mca_dev->pos_id && mca_dev->slot < MCA_MAX_SLOT_NR) {
79
80                 /* id = 0x0000 usually indicates hardware failure,
81                  * however, ZP Gu (zpg@castle.net> reports that his 9556
82                  * has 0x0000 as id and everything still works. There
83                  * also seem to be an adapter with id = 0x0000; the
84                  * NCR Parallel Bus Memory Card. Until this is confirmed,
85                  * however, this code will stay.
86                  */
87
88                 mca_dev->status = MCA_ADAPTER_ERROR;
89
90                 return;
91         } else if(mca_dev->pos_id != 0xffff) {
92
93                 /* 0xffff usually indicates that there's no adapter,
94                  * however, some integrated adapters may have 0xffff as
95                  * their id and still be valid. Examples are on-board
96                  * VGA of the 55sx, the integrated SCSI of the 56 & 57,
97                  * and possibly also the 95 ULTIMEDIA.
98                  */
99
100                 mca_dev->status = MCA_ADAPTER_NORMAL;
101         }
102
103         if((mca_dev->pos_id == 0xffff ||
104             mca_dev->pos_id == 0x0000) && mca_dev->slot >= MCA_MAX_SLOT_NR) {
105                 int j;
106
107                 for(j = 2; j < 8; j++) {
108                         if(mca_dev->pos[j] != 0xff) {
109                                 mca_dev->status = MCA_ADAPTER_NORMAL;
110                                 break;
111                         }
112                 }
113         }
114
115         if(!(mca_dev->pos[2] & MCA_ENABLED)) {
116
117                 /* enabled bit is in POS 2 */
118
119                 mca_dev->status = MCA_ADAPTER_DISABLED;
120         }
121 } /* mca_configure_adapter_status */
122
123 /*--------------------------------------------------------------------*/
124
125 static struct resource mca_standard_resources[] = {
126         { .start = 0x60, .end = 0x60, .name = "system control port B (MCA)" },
127         { .start = 0x90, .end = 0x90, .name = "arbitration (MCA)" },
128         { .start = 0x91, .end = 0x91, .name = "card Select Feedback (MCA)" },
129         { .start = 0x92, .end = 0x92, .name = "system Control port A (MCA)" },
130         { .start = 0x94, .end = 0x94, .name = "system board setup (MCA)" },
131         { .start = 0x96, .end = 0x97, .name = "POS (MCA)" },
132         { .start = 0x100, .end = 0x107, .name = "POS (MCA)" }
133 };
134
135 #define MCA_STANDARD_RESOURCES  (sizeof(mca_standard_resources)/sizeof(struct resource))
136
137 /**
138  *      mca_read_and_store_pos - read the POS registers into a memory buffer
139  *      @pos: a char pointer to 8 bytes, contains the POS register value on
140  *            successful return
141  *
142  *      Returns 1 if a card actually exists (i.e. the pos isn't
143  *      all 0xff) or 0 otherwise
144  */
145 static int mca_read_and_store_pos(unsigned char *pos) {
146         int j;
147         int found = 0;
148
149         for(j=0; j<8; j++) {
150                 if((pos[j] = inb_p(MCA_POS_REG(j))) != 0xff) {
151                         /* 0xff all across means no device. 0x00 means
152                          * something's broken, but a device is
153                          * probably there.  However, if you get 0x00
154                          * from a motherboard register it won't matter
155                          * what we find.  For the record, on the
156                          * 57SLC, the integrated SCSI adapter has
157                          * 0xffff for the adapter ID, but nonzero for
158                          * other registers.  */
159
160                         found = 1;
161                 }
162         }
163         return found;
164 }
165
166 static unsigned char mca_pc_read_pos(struct mca_device *mca_dev, int reg)
167 {
168         unsigned char byte;
169         unsigned long flags;
170
171         if(reg < 0 || reg >= 8)
172                 return 0;
173
174         spin_lock_irqsave(&mca_lock, flags);
175         if(mca_dev->pos_register) {
176                 /* Disable adapter setup, enable motherboard setup */
177
178                 outb_p(0, MCA_ADAPTER_SETUP_REG);
179                 outb_p(mca_dev->pos_register, MCA_MOTHERBOARD_SETUP_REG);
180
181                 byte = inb_p(MCA_POS_REG(reg));
182                 outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
183         } else {
184
185                 /* Make sure motherboard setup is off */
186
187                 outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
188
189                 /* Read the appropriate register */
190
191                 outb_p(0x8|(mca_dev->slot & 0xf), MCA_ADAPTER_SETUP_REG);
192                 byte = inb_p(MCA_POS_REG(reg));
193                 outb_p(0, MCA_ADAPTER_SETUP_REG);
194         }
195         spin_unlock_irqrestore(&mca_lock, flags);
196
197         mca_dev->pos[reg] = byte;
198
199         return byte;
200 }
201
202 static void mca_pc_write_pos(struct mca_device *mca_dev, int reg,
203                              unsigned char byte)
204 {
205         unsigned long flags;
206
207         if(reg < 0 || reg >= 8)
208                 return;
209
210         spin_lock_irqsave(&mca_lock, flags);
211
212         /* Make sure motherboard setup is off */
213
214         outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
215
216         /* Read in the appropriate register */
217
218         outb_p(0x8|(mca_dev->slot&0xf), MCA_ADAPTER_SETUP_REG);
219         outb_p(byte, MCA_POS_REG(reg));
220         outb_p(0, MCA_ADAPTER_SETUP_REG);
221
222         spin_unlock_irqrestore(&mca_lock, flags);
223
224         /* Update the global register list, while we have the byte */
225
226         mca_dev->pos[reg] = byte;
227
228 }
229
230 /* for the primary MCA bus, we have identity transforms */
231 static int mca_dummy_transform_irq(struct mca_device * mca_dev, int irq)
232 {
233         return irq;
234 }
235
236 static int mca_dummy_transform_ioport(struct mca_device * mca_dev, int port)
237 {
238         return port;
239 }
240
241 static void *mca_dummy_transform_memory(struct mca_device * mca_dev, void *mem)
242 {
243         return mem;
244 }
245
246
247 static int __init mca_init(void)
248 {
249         unsigned int i, j;
250         struct mca_device *mca_dev;
251         unsigned char pos[8];
252         short mca_builtin_scsi_ports[] = {0xf7, 0xfd, 0x00};
253         struct mca_bus *bus;
254
255         /* WARNING: Be careful when making changes here. Putting an adapter
256          * and the motherboard simultaneously into setup mode may result in
257          * damage to chips (according to The Indispensible PC Hardware Book
258          * by Hans-Peter Messmer). Also, we disable system interrupts (so
259          * that we are not disturbed in the middle of this).
260          */
261
262         /* Make sure the MCA bus is present */
263
264         if (mca_system_init()) {
265                 printk(KERN_ERR "MCA bus system initialisation failed\n");
266                 return -ENODEV;
267         }
268
269         if (!MCA_bus)
270                 return -ENODEV;
271
272         printk(KERN_INFO "Micro Channel bus detected.\n");
273
274         /* All MCA systems have at least a primary bus */
275         bus = mca_attach_bus(MCA_PRIMARY_BUS);
276         if (!bus)
277                 goto out_nomem;
278         bus->default_dma_mask = 0xffffffffLL;
279         bus->f.mca_write_pos = mca_pc_write_pos;
280         bus->f.mca_read_pos = mca_pc_read_pos;
281         bus->f.mca_transform_irq = mca_dummy_transform_irq;
282         bus->f.mca_transform_ioport = mca_dummy_transform_ioport;
283         bus->f.mca_transform_memory = mca_dummy_transform_memory;
284
285         /* get the motherboard device */
286         mca_dev = kmalloc(sizeof(struct mca_device), GFP_KERNEL);
287         if(unlikely(!mca_dev))
288                 goto out_nomem;
289         memset(mca_dev, 0, sizeof(struct mca_device));
290
291         /*
292          * We do not expect many MCA interrupts during initialization,
293          * but let us be safe:
294          */
295         spin_lock_irq(&mca_lock);
296
297         /* Make sure adapter setup is off */
298
299         outb_p(0, MCA_ADAPTER_SETUP_REG);
300
301         /* Read motherboard POS registers */
302
303         mca_dev->pos_register = 0x7f;
304         outb_p(mca_dev->pos_register, MCA_MOTHERBOARD_SETUP_REG);
305         mca_dev->name[0] = 0;
306         mca_read_and_store_pos(mca_dev->pos);
307         mca_configure_adapter_status(mca_dev);
308         /* fake POS and slot for a motherboard */
309         mca_dev->pos_id = MCA_MOTHERBOARD_POS;
310         mca_dev->slot = MCA_MOTHERBOARD;
311         mca_register_device(MCA_PRIMARY_BUS, mca_dev);
312
313         mca_dev = kmalloc(sizeof(struct mca_device), GFP_ATOMIC);
314         if(unlikely(!mca_dev))
315                 goto out_unlock_nomem;
316         memset(mca_dev, 0, sizeof(struct mca_device));
317
318
319         /* Put motherboard into video setup mode, read integrated video
320          * POS registers, and turn motherboard setup off.
321          */
322
323         mca_dev->pos_register = 0xdf;
324         outb_p(mca_dev->pos_register, MCA_MOTHERBOARD_SETUP_REG);
325         mca_dev->name[0] = 0;
326         mca_read_and_store_pos(mca_dev->pos);
327         mca_configure_adapter_status(mca_dev);
328         /* fake POS and slot for the integrated video */
329         mca_dev->pos_id = MCA_INTEGVIDEO_POS;
330         mca_dev->slot = MCA_INTEGVIDEO;
331         mca_register_device(MCA_PRIMARY_BUS, mca_dev);
332
333         /* Put motherboard into scsi setup mode, read integrated scsi
334          * POS registers, and turn motherboard setup off.
335          *
336          * It seems there are two possible SCSI registers. Martin says that
337          * for the 56,57, 0xf7 is the one, but fails on the 76.
338          * Alfredo (apena@vnet.ibm.com) says
339          * 0xfd works on his machine. We'll try both of them. I figure it's
340          * a good bet that only one could be valid at a time. This could
341          * screw up though if one is used for something else on the other
342          * machine.
343          */
344
345         for(i = 0; (which_scsi = mca_builtin_scsi_ports[i]) != 0; i++) {
346                 outb_p(which_scsi, MCA_MOTHERBOARD_SETUP_REG);
347                 if(mca_read_and_store_pos(pos))
348                         break;
349         }
350         if(which_scsi) {
351                 /* found a scsi card */
352                 mca_dev = kmalloc(sizeof(struct mca_device), GFP_ATOMIC);
353                 if(unlikely(!mca_dev))
354                         goto out_unlock_nomem;
355                 memset(mca_dev, 0, sizeof(struct mca_device));
356
357                 for(j = 0; j < 8; j++)
358                         mca_dev->pos[j] = pos[j];
359
360                 mca_configure_adapter_status(mca_dev);
361                 /* fake POS and slot for integrated SCSI controller */
362                 mca_dev->pos_id = MCA_INTEGSCSI_POS;
363                 mca_dev->slot = MCA_INTEGSCSI;
364                 mca_dev->pos_register = which_scsi;
365                 mca_register_device(MCA_PRIMARY_BUS, mca_dev);
366         }
367
368         /* Turn off motherboard setup */
369
370         outb_p(0xff, MCA_MOTHERBOARD_SETUP_REG);
371
372         /* Now loop over MCA slots: put each adapter into setup mode, and
373          * read its POS registers. Then put adapter setup off.
374          */
375
376         for(i=0; i<MCA_MAX_SLOT_NR; i++) {
377                 outb_p(0x8|(i&0xf), MCA_ADAPTER_SETUP_REG);
378                 if(!mca_read_and_store_pos(pos))
379                         continue;
380
381                 mca_dev = kmalloc(sizeof(struct mca_device), GFP_ATOMIC);
382                 if(unlikely(!mca_dev))
383                         goto out_unlock_nomem;
384                 memset(mca_dev, 0, sizeof(struct mca_device));
385
386                 for(j=0; j<8; j++)
387                         mca_dev->pos[j]=pos[j];
388
389                 mca_dev->driver_loaded = 0;
390                 mca_dev->slot = i;
391                 mca_dev->pos_register = 0;
392                 mca_configure_adapter_status(mca_dev);
393                 mca_register_device(MCA_PRIMARY_BUS, mca_dev);
394         }
395         outb_p(0, MCA_ADAPTER_SETUP_REG);
396
397         /* Enable interrupts and return memory start */
398         spin_unlock_irq(&mca_lock);
399
400         for (i = 0; i < MCA_STANDARD_RESOURCES; i++)
401                 request_resource(&ioport_resource, mca_standard_resources + i);
402
403         mca_do_proc_init();
404
405         return 0;
406
407  out_unlock_nomem:
408         spin_unlock_irq(&mca_lock);
409  out_nomem:
410         printk(KERN_EMERG "Failed memory allocation in MCA setup!\n");
411         return -ENOMEM;
412 }
413
414 subsys_initcall(mca_init);
415
416 /*--------------------------------------------------------------------*/
417
418 static void mca_handle_nmi_device(struct mca_device *mca_dev, int check_flag)
419 {
420         int slot = mca_dev->slot;
421
422         if(slot == MCA_INTEGSCSI) {
423                 printk(KERN_CRIT "NMI: caused by MCA integrated SCSI adapter (%s)\n",
424                         mca_dev->name);
425         } else if(slot == MCA_INTEGVIDEO) {
426                 printk(KERN_CRIT "NMI: caused by MCA integrated video adapter (%s)\n",
427                         mca_dev->name);
428         } else if(slot == MCA_MOTHERBOARD) {
429                 printk(KERN_CRIT "NMI: caused by motherboard (%s)\n",
430                         mca_dev->name);
431         }
432
433         /* More info available in POS 6 and 7? */
434
435         if(check_flag) {
436                 unsigned char pos6, pos7;
437
438                 pos6 = mca_device_read_pos(mca_dev, 6);
439                 pos7 = mca_device_read_pos(mca_dev, 7);
440
441                 printk(KERN_CRIT "NMI: POS 6 = 0x%x, POS 7 = 0x%x\n", pos6, pos7);
442         }
443
444 } /* mca_handle_nmi_slot */
445
446 /*--------------------------------------------------------------------*/
447
448 static int mca_handle_nmi_callback(struct device *dev, void *data)
449 {
450         struct mca_device *mca_dev = to_mca_device(dev);
451         unsigned char pos5;
452
453         pos5 = mca_device_read_pos(mca_dev, 5);
454
455         if(!(pos5 & 0x80)) {
456                 /* Bit 7 of POS 5 is reset when this adapter has a hardware
457                  * error. Bit 7 it reset if there's error information
458                  * available in POS 6 and 7.
459                  */
460                 mca_handle_nmi_device(mca_dev, !(pos5 & 0x40));
461                 return 1;
462         }
463         return 0;
464 }
465
466 void mca_handle_nmi(void)
467 {
468         /* First try - scan the various adapters and see if a specific
469          * adapter was responsible for the error.
470          */
471         bus_for_each_dev(&mca_bus_type, NULL, NULL, mca_handle_nmi_callback);
472
473         mca_nmi_hook();
474 } /* mca_handle_nmi */