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