make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / alpha / kernel / core_apecs.c
1 /*
2  *      linux/arch/alpha/kernel/core_apecs.c
3  *
4  * Rewritten for Apecs from the lca.c from:
5  *
6  * Written by David Mosberger (davidm@cs.arizona.edu) with some code
7  * taken from Dave Rusling's (david.rusling@reo.mts.dec.com) 32-bit
8  * bios code.
9  *
10  * Code common to all APECS core logic chips.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/pci.h>
16 #include <linux/init.h>
17
18 #include <asm/system.h>
19 #include <asm/ptrace.h>
20 #include <asm/smp.h>
21
22 #define __EXTERN_INLINE inline
23 #include <asm/io.h>
24 #include <asm/core_apecs.h>
25 #undef __EXTERN_INLINE
26
27 #include "proto.h"
28 #include "pci_impl.h"
29
30 /*
31  * NOTE: Herein lie back-to-back mb instructions.  They are magic. 
32  * One plausible explanation is that the i/o controller does not properly
33  * handle the system transaction.  Another involves timing.  Ho hum.
34  */
35
36 /*
37  * BIOS32-style PCI interface:
38  */
39
40 #define DEBUG_CONFIG 0
41
42 #if DEBUG_CONFIG
43 # define DBGC(args)     printk args
44 #else
45 # define DBGC(args)
46 #endif
47
48 #define vuip    volatile unsigned int  *
49
50 /*
51  * Given a bus, device, and function number, compute resulting
52  * configuration space address and setup the APECS_HAXR2 register
53  * accordingly.  It is therefore not safe to have concurrent
54  * invocations to configuration space access routines, but there
55  * really shouldn't be any need for this.
56  *
57  * Type 0:
58  *
59  *  3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1 
60  *  3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
61  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62  * | | | | | | | | | | | | | | | | | | | | | | | |F|F|F|R|R|R|R|R|R|0|0|
63  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64  *
65  *      31:11   Device select bit.
66  *      10:8    Function number
67  *       7:2    Register number
68  *
69  * Type 1:
70  *
71  *  3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1 
72  *  3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
73  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74  * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
75  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76  *
77  *      31:24   reserved
78  *      23:16   bus number (8 bits = 128 possible buses)
79  *      15:11   Device number (5 bits)
80  *      10:8    function number
81  *       7:2    register number
82  *  
83  * Notes:
84  *      The function number selects which function of a multi-function device 
85  *      (e.g., SCSI and Ethernet).
86  * 
87  *      The register selects a DWORD (32 bit) register offset.  Hence it
88  *      doesn't get shifted by 2 bits as we want to "drop" the bottom two
89  *      bits.
90  */
91
92 static int
93 mk_conf_addr(struct pci_dev *dev, int where, unsigned long *pci_addr,
94              unsigned char *type1)
95 {
96         unsigned long addr;
97         u8 bus = dev->bus->number;
98         u8 device_fn = dev->devfn;
99
100         DBGC(("mk_conf_addr(bus=%d ,device_fn=0x%x, where=0x%x,"
101               " pci_addr=0x%p, type1=0x%p)\n",
102               bus, device_fn, where, pci_addr, type1));
103
104         if (bus == 0) {
105                 int device = device_fn >> 3;
106
107                 /* type 0 configuration cycle: */
108
109                 if (device > 20) {
110                         DBGC(("mk_conf_addr: device (%d) > 20, returning -1\n",
111                               device));
112                         return -1;
113                 }
114
115                 *type1 = 0;
116                 addr = (device_fn << 8) | (where);
117         } else {
118                 /* type 1 configuration cycle: */
119                 *type1 = 1;
120                 addr = (bus << 16) | (device_fn << 8) | (where);
121         }
122         *pci_addr = addr;
123         DBGC(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
124         return 0;
125 }
126
127 static unsigned int
128 conf_read(unsigned long addr, unsigned char type1)
129 {
130         unsigned long flags;
131         unsigned int stat0, value;
132         unsigned int haxr2 = 0;
133
134         __save_and_cli(flags);  /* avoid getting hit by machine check */
135
136         DBGC(("conf_read(addr=0x%lx, type1=%d)\n", addr, type1));
137
138         /* Reset status register to avoid losing errors.  */
139         stat0 = *(vuip)APECS_IOC_DCSR;
140         *(vuip)APECS_IOC_DCSR = stat0;
141         mb();
142         DBGC(("conf_read: APECS DCSR was 0x%x\n", stat0));
143
144         /* If Type1 access, must set HAE #2. */
145         if (type1) {
146                 haxr2 = *(vuip)APECS_IOC_HAXR2;
147                 mb();
148                 *(vuip)APECS_IOC_HAXR2 = haxr2 | 1;
149                 DBGC(("conf_read: TYPE1 access\n"));
150         }
151
152         draina();
153         mcheck_expected(0) = 1;
154         mcheck_taken(0) = 0;
155         mb();
156
157         /* Access configuration space.  */
158
159         /* Some SRMs step on these registers during a machine check.  */
160         asm volatile("ldl %0,%1; mb; mb" : "=r"(value) : "m"(*(vuip)addr)
161                      : "$9", "$10", "$11", "$12", "$13", "$14", "memory");
162
163         if (mcheck_taken(0)) {
164                 mcheck_taken(0) = 0;
165                 value = 0xffffffffU;
166                 mb();
167         }
168         mcheck_expected(0) = 0;
169         mb();
170
171 #if 1
172         /*
173          * david.rusling@reo.mts.dec.com.  This code is needed for the
174          * EB64+ as it does not generate a machine check (why I don't
175          * know).  When we build kernels for one particular platform
176          * then we can make this conditional on the type.
177          */
178         draina();
179
180         /* Now look for any errors.  */
181         stat0 = *(vuip)APECS_IOC_DCSR;
182         DBGC(("conf_read: APECS DCSR after read 0x%x\n", stat0));
183
184         /* Is any error bit set? */
185         if (stat0 & 0xffe0U) {
186                 /* If not NDEV, print status.  */
187                 if (!(stat0 & 0x0800)) {
188                         printk("apecs.c:conf_read: got stat0=%x\n", stat0);
189                 }
190
191                 /* Reset error status.  */
192                 *(vuip)APECS_IOC_DCSR = stat0;
193                 mb();
194                 wrmces(0x7);                    /* reset machine check */
195                 value = 0xffffffff;
196         }
197 #endif
198
199         /* If Type1 access, must reset HAE #2 so normal IO space ops work.  */
200         if (type1) {
201                 *(vuip)APECS_IOC_HAXR2 = haxr2 & ~1;
202                 mb();
203         }
204         __restore_flags(flags);
205
206         return value;
207 }
208
209 static void
210 conf_write(unsigned long addr, unsigned int value, unsigned char type1)
211 {
212         unsigned long flags;
213         unsigned int stat0;
214         unsigned int haxr2 = 0;
215
216         __save_and_cli(flags);  /* avoid getting hit by machine check */
217
218         /* Reset status register to avoid losing errors.  */
219         stat0 = *(vuip)APECS_IOC_DCSR;
220         *(vuip)APECS_IOC_DCSR = stat0;
221         mb();
222
223         /* If Type1 access, must set HAE #2. */
224         if (type1) {
225                 haxr2 = *(vuip)APECS_IOC_HAXR2;
226                 mb();
227                 *(vuip)APECS_IOC_HAXR2 = haxr2 | 1;
228         }
229
230         draina();
231         mcheck_expected(0) = 1;
232         mb();
233
234         /* Access configuration space.  */
235         *(vuip)addr = value;
236         mb();
237         mb();  /* magic */
238         mcheck_expected(0) = 0;
239         mb();
240
241 #if 1
242         /*
243          * david.rusling@reo.mts.dec.com.  This code is needed for the
244          * EB64+ as it does not generate a machine check (why I don't
245          * know).  When we build kernels for one particular platform
246          * then we can make this conditional on the type.
247          */
248         draina();
249
250         /* Now look for any errors.  */
251         stat0 = *(vuip)APECS_IOC_DCSR;
252
253         /* Is any error bit set? */
254         if (stat0 & 0xffe0U) {
255                 /* If not NDEV, print status.  */
256                 if (!(stat0 & 0x0800)) {
257                         printk("apecs.c:conf_write: got stat0=%x\n", stat0);
258                 }
259
260                 /* Reset error status.  */
261                 *(vuip)APECS_IOC_DCSR = stat0;
262                 mb();
263                 wrmces(0x7);                    /* reset machine check */
264         }
265 #endif
266
267         /* If Type1 access, must reset HAE #2 so normal IO space ops work.  */
268         if (type1) {
269                 *(vuip)APECS_IOC_HAXR2 = haxr2 & ~1;
270                 mb();
271         }
272         __restore_flags(flags);
273 }
274
275 static int
276 apecs_read_config_byte(struct pci_dev *dev, int where, u8 *value)
277 {
278         unsigned long addr, pci_addr;
279         unsigned char type1;
280
281         if (mk_conf_addr(dev, where, &pci_addr, &type1))
282                 return PCIBIOS_DEVICE_NOT_FOUND;
283
284         addr = (pci_addr << 5) + 0x00 + APECS_CONF;
285         *value = conf_read(addr, type1) >> ((where & 3) * 8);
286         return PCIBIOS_SUCCESSFUL;
287 }
288
289 static int 
290 apecs_read_config_word(struct pci_dev *dev, int where, u16 *value)
291 {
292         unsigned long addr, pci_addr;
293         unsigned char type1;
294
295         if (mk_conf_addr(dev, where, &pci_addr, &type1))
296                 return PCIBIOS_DEVICE_NOT_FOUND;
297
298         addr = (pci_addr << 5) + 0x08 + APECS_CONF;
299         *value = conf_read(addr, type1) >> ((where & 3) * 8);
300         return PCIBIOS_SUCCESSFUL;
301 }
302
303 static int
304 apecs_read_config_dword(struct pci_dev *dev, int where, u32 *value)
305 {
306         unsigned long addr, pci_addr;
307         unsigned char type1;
308
309         if (mk_conf_addr(dev, where, &pci_addr, &type1))
310                 return PCIBIOS_DEVICE_NOT_FOUND;
311
312         addr = (pci_addr << 5) + 0x18 + APECS_CONF;
313         *value = conf_read(addr, type1);
314         return PCIBIOS_SUCCESSFUL;
315 }
316
317 static int
318 apecs_write_config(struct pci_dev *dev, int where, u32 value, long mask)
319 {
320         unsigned long addr, pci_addr;
321         unsigned char type1;
322
323         if (mk_conf_addr(dev, where, &pci_addr, &type1))
324                 return PCIBIOS_DEVICE_NOT_FOUND;
325
326         addr = (pci_addr << 5) + mask + APECS_CONF;
327         conf_write(addr, value << ((where & 3) * 8), type1);
328         return PCIBIOS_SUCCESSFUL;
329 }
330
331 static int
332 apecs_write_config_byte(struct pci_dev *dev, int where, u8 value)
333 {
334         return apecs_write_config(dev, where, value, 0x00);
335 }
336
337 static int
338 apecs_write_config_word(struct pci_dev *dev, int where, u16 value)
339 {
340         return apecs_write_config(dev, where, value, 0x08);
341 }
342
343 static int
344 apecs_write_config_dword(struct pci_dev *dev, int where, u32 value)
345 {
346         return apecs_write_config(dev, where, value, 0x18);
347 }
348
349 struct pci_ops apecs_pci_ops = 
350 {
351         read_byte:      apecs_read_config_byte,
352         read_word:      apecs_read_config_word,
353         read_dword:     apecs_read_config_dword,
354         write_byte:     apecs_write_config_byte,
355         write_word:     apecs_write_config_word,
356         write_dword:    apecs_write_config_dword
357 };
358 \f
359 void
360 apecs_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
361 {
362         wmb();
363         *(vip)APECS_IOC_TBIA = 0;
364         mb();
365 }
366 \f
367 void __init
368 apecs_init_arch(void)
369 {
370         struct pci_controller *hose;
371
372         /*
373          * Create our single hose.
374          */
375
376         pci_isa_hose = hose = alloc_pci_controller();
377         hose->io_space = &ioport_resource;
378         hose->mem_space = &iomem_resource;
379         hose->index = 0;
380
381         hose->sparse_mem_base = APECS_SPARSE_MEM - IDENT_ADDR;
382         hose->dense_mem_base = APECS_DENSE_MEM - IDENT_ADDR;
383         hose->sparse_io_base = APECS_IO - IDENT_ADDR;
384         hose->dense_io_base = 0;
385
386         /*
387          * Set up the PCI to main memory translation windows.
388          *
389          * Window 1 is direct access 1GB at 1GB
390          * Window 2 is scatter-gather 8MB at 8MB (for isa)
391          */
392         hose->sg_isa = iommu_arena_new(hose, 0x00800000, 0x00800000, 0);
393         hose->sg_pci = NULL;
394         __direct_map_base = 0x40000000;
395         __direct_map_size = 0x40000000;
396
397         *(vuip)APECS_IOC_PB1R = __direct_map_base | 0x00080000;
398         *(vuip)APECS_IOC_PM1R = (__direct_map_size - 1) & 0xfff00000U;
399         *(vuip)APECS_IOC_TB1R = 0;
400
401         *(vuip)APECS_IOC_PB2R = hose->sg_isa->dma_base | 0x000c0000;
402         *(vuip)APECS_IOC_PM2R = (hose->sg_isa->size - 1) & 0xfff00000;
403         *(vuip)APECS_IOC_TB2R = virt_to_phys(hose->sg_isa->ptes) >> 1;
404
405         apecs_pci_tbi(hose, 0, -1);
406
407         /*
408          * Finally, clear the HAXR2 register, which gets used
409          * for PCI Config Space accesses. That is the way
410          * we want to use it, and we do not want to depend on
411          * what ARC or SRM might have left behind...
412          */
413         *(vuip)APECS_IOC_HAXR2 = 0;
414         mb();
415 }
416
417 void
418 apecs_pci_clr_err(void)
419 {
420         unsigned int jd;
421
422         jd = *(vuip)APECS_IOC_DCSR;
423         if (jd & 0xffe0L) {
424                 *(vuip)APECS_IOC_SEAR;
425                 *(vuip)APECS_IOC_DCSR = jd | 0xffe1L;
426                 mb();
427                 *(vuip)APECS_IOC_DCSR;
428         }
429         *(vuip)APECS_IOC_TBIA = (unsigned int)APECS_IOC_TBIA;
430         mb();
431         *(vuip)APECS_IOC_TBIA;
432 }
433
434 void
435 apecs_machine_check(unsigned long vector, unsigned long la_ptr,
436                     struct pt_regs * regs)
437 {
438         struct el_common *mchk_header;
439         struct el_apecs_procdata *mchk_procdata;
440         struct el_apecs_sysdata_mcheck *mchk_sysdata;
441
442         mchk_header = (struct el_common *)la_ptr;
443
444         mchk_procdata = (struct el_apecs_procdata *)
445                 (la_ptr + mchk_header->proc_offset
446                  - sizeof(mchk_procdata->paltemp));
447
448         mchk_sysdata = (struct el_apecs_sysdata_mcheck *)
449                 (la_ptr + mchk_header->sys_offset);
450
451
452         /* Clear the error before any reporting.  */
453         mb();
454         mb(); /* magic */
455         draina();
456         apecs_pci_clr_err();
457         wrmces(0x7);            /* reset machine check pending flag */
458         mb();
459
460         process_mcheck_info(vector, la_ptr, regs, "APECS",
461                             (mcheck_expected(0)
462                              && (mchk_sysdata->epic_dcsr & 0x0c00UL)));
463 }