make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / alpha / kernel / core_lca.c
1 /*
2  *      linux/arch/alpha/kernel/core_lca.c
3  *
4  * Written by David Mosberger (davidm@cs.arizona.edu) with some code
5  * taken from Dave Rusling's (david.rusling@reo.mts.dec.com) 32-bit
6  * bios code.
7  *
8  * Code common to all LCA core logic chips.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/tty.h>
16
17 #include <asm/ptrace.h>
18 #include <asm/system.h>
19 #include <asm/smp.h>
20
21 #define __EXTERN_INLINE inline
22 #include <asm/io.h>
23 #include <asm/core_lca.h>
24 #undef __EXTERN_INLINE
25
26 #include "proto.h"
27 #include "pci_impl.h"
28
29
30 /*
31  * BIOS32-style PCI interface:
32  */
33
34 /*
35  * Machine check reasons.  Defined according to PALcode sources
36  * (osf.h and platform.h).
37  */
38 #define MCHK_K_TPERR            0x0080
39 #define MCHK_K_TCPERR           0x0082
40 #define MCHK_K_HERR             0x0084
41 #define MCHK_K_ECC_C            0x0086
42 #define MCHK_K_ECC_NC           0x0088
43 #define MCHK_K_UNKNOWN          0x008A
44 #define MCHK_K_CACKSOFT         0x008C
45 #define MCHK_K_BUGCHECK         0x008E
46 #define MCHK_K_OS_BUGCHECK      0x0090
47 #define MCHK_K_DCPERR           0x0092
48 #define MCHK_K_ICPERR           0x0094
49
50
51 /*
52  * Platform-specific machine-check reasons:
53  */
54 #define MCHK_K_SIO_SERR         0x204   /* all platforms so far */
55 #define MCHK_K_SIO_IOCHK        0x206   /* all platforms so far */
56 #define MCHK_K_DCSR             0x208   /* all but Noname */
57
58
59 /*
60  * Given a bus, device, and function number, compute resulting
61  * configuration space address and setup the LCA_IOC_CONF register
62  * accordingly.  It is therefore not safe to have concurrent
63  * invocations to configuration space access routines, but there
64  * really shouldn't be any need for this.
65  *
66  * Type 0:
67  *
68  *  3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1 
69  *  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
70  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71  * | | | | | | | | | | | | | | | | | | | | | | | |F|F|F|R|R|R|R|R|R|0|0|
72  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73  *
74  *      31:11   Device select bit.
75  *      10:8    Function number
76  *       7:2    Register number
77  *
78  * Type 1:
79  *
80  *  3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1 
81  *  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
82  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
84  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85  *
86  *      31:24   reserved
87  *      23:16   bus number (8 bits = 128 possible buses)
88  *      15:11   Device number (5 bits)
89  *      10:8    function number
90  *       7:2    register number
91  *  
92  * Notes:
93  *      The function number selects which function of a multi-function device 
94  *      (e.g., SCSI and Ethernet).
95  * 
96  *      The register selects a DWORD (32 bit) register offset.  Hence it
97  *      doesn't get shifted by 2 bits as we want to "drop" the bottom two
98  *      bits.
99  */
100
101 static int
102 mk_conf_addr(struct pci_dev *dev, int where, unsigned long *pci_addr)
103 {
104         unsigned long addr;
105         u8 bus = dev->bus->number;
106         u8 device_fn = dev->devfn;
107
108         if (bus == 0) {
109                 int device = device_fn >> 3;
110                 int func = device_fn & 0x7;
111
112                 /* Type 0 configuration cycle.  */
113
114                 if (device > 12) {
115                         return -1;
116                 }
117
118                 *(vulp)LCA_IOC_CONF = 0;
119                 addr = (1 << (11 + device)) | (func << 8) | where;
120         } else {
121                 /* Type 1 configuration cycle.  */
122                 *(vulp)LCA_IOC_CONF = 1;
123                 addr = (bus << 16) | (device_fn << 8) | where;
124         }
125         *pci_addr = addr;
126         return 0;
127 }
128
129 static unsigned int
130 conf_read(unsigned long addr)
131 {
132         unsigned long flags, code, stat0;
133         unsigned int value;
134
135         __save_and_cli(flags);
136
137         /* Reset status register to avoid loosing errors.  */
138         stat0 = *(vulp)LCA_IOC_STAT0;
139         *(vulp)LCA_IOC_STAT0 = stat0;
140         mb();
141
142         /* Access configuration space.  */
143         value = *(vuip)addr;
144         draina();
145
146         stat0 = *(vulp)LCA_IOC_STAT0;
147         if (stat0 & LCA_IOC_STAT0_ERR) {
148                 code = ((stat0 >> LCA_IOC_STAT0_CODE_SHIFT)
149                         & LCA_IOC_STAT0_CODE_MASK);
150                 if (code != 1) {
151                         printk("lca.c:conf_read: got stat0=%lx\n", stat0);
152                 }
153
154                 /* Reset error status.  */
155                 *(vulp)LCA_IOC_STAT0 = stat0;
156                 mb();
157
158                 /* Reset machine check.  */
159                 wrmces(0x7);
160
161                 value = 0xffffffff;
162         }
163         __restore_flags(flags);
164         return value;
165 }
166
167 static void
168 conf_write(unsigned long addr, unsigned int value)
169 {
170         unsigned long flags, code, stat0;
171
172         __save_and_cli(flags);  /* avoid getting hit by machine check */
173
174         /* Reset status register to avoid loosing errors.  */
175         stat0 = *(vulp)LCA_IOC_STAT0;
176         *(vulp)LCA_IOC_STAT0 = stat0;
177         mb();
178
179         /* Access configuration space.  */
180         *(vuip)addr = value;
181         draina();
182
183         stat0 = *(vulp)LCA_IOC_STAT0;
184         if (stat0 & LCA_IOC_STAT0_ERR) {
185                 code = ((stat0 >> LCA_IOC_STAT0_CODE_SHIFT)
186                         & LCA_IOC_STAT0_CODE_MASK);
187                 if (code != 1) {
188                         printk("lca.c:conf_write: got stat0=%lx\n", stat0);
189                 }
190
191                 /* Reset error status.  */
192                 *(vulp)LCA_IOC_STAT0 = stat0;
193                 mb();
194
195                 /* Reset machine check. */
196                 wrmces(0x7);
197         }
198         __restore_flags(flags);
199 }
200
201 static int
202 lca_read_config_byte(struct pci_dev *dev, int where, u8 *value)
203 {
204         unsigned long addr, pci_addr;
205
206         if (mk_conf_addr(dev, where, &pci_addr))
207                 return PCIBIOS_DEVICE_NOT_FOUND;
208
209         addr = (pci_addr << 5) + 0x00 + LCA_CONF;
210         *value = conf_read(addr) >> ((where & 3) * 8);
211         return PCIBIOS_SUCCESSFUL;
212 }
213
214 static int 
215 lca_read_config_word(struct pci_dev *dev, int where, u16 *value)
216 {
217         unsigned long addr, pci_addr;
218
219         if (mk_conf_addr(dev, where, &pci_addr))
220                 return PCIBIOS_DEVICE_NOT_FOUND;
221
222         addr = (pci_addr << 5) + 0x08 + LCA_CONF;
223         *value = conf_read(addr) >> ((where & 3) * 8);
224         return PCIBIOS_SUCCESSFUL;
225 }
226
227 static int
228 lca_read_config_dword(struct pci_dev *dev, int where, u32 *value)
229 {
230         unsigned long addr, pci_addr;
231
232         if (mk_conf_addr(dev, where, &pci_addr))
233                 return PCIBIOS_DEVICE_NOT_FOUND;
234
235         addr = (pci_addr << 5) + 0x18 + LCA_CONF;
236         *value = conf_read(addr);
237         return PCIBIOS_SUCCESSFUL;
238 }
239
240 static int 
241 lca_write_config(struct pci_dev *dev, int where, u32 value, long mask)
242 {
243         unsigned long addr, pci_addr;
244
245         if (mk_conf_addr(dev, where, &pci_addr))
246                 return PCIBIOS_DEVICE_NOT_FOUND;
247
248         addr = (pci_addr << 5) + mask + LCA_CONF;
249         conf_write(addr, value << ((where & 3) * 8));
250         return PCIBIOS_SUCCESSFUL;
251 }
252
253 static int
254 lca_write_config_byte(struct pci_dev *dev, int where, u8 value)
255 {
256         return lca_write_config(dev, where, value, 0x00);
257 }
258
259 static int
260 lca_write_config_word(struct pci_dev *dev, int where, u16 value)
261 {
262         return lca_write_config(dev, where, value, 0x08);
263 }
264
265 static int
266 lca_write_config_dword(struct pci_dev *dev, int where, u32 value)
267 {
268         return lca_write_config(dev, where, value, 0x18);
269 }
270
271 struct pci_ops lca_pci_ops = 
272 {
273         read_byte:      lca_read_config_byte,
274         read_word:      lca_read_config_word,
275         read_dword:     lca_read_config_dword,
276         write_byte:     lca_write_config_byte,
277         write_word:     lca_write_config_word,
278         write_dword:    lca_write_config_dword
279 };
280 \f
281 void
282 lca_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
283 {
284         wmb();
285         *(vip)LCA_IOC_TBIA = 0;
286         mb();
287 }
288 \f
289 void __init
290 lca_init_arch(void)
291 {
292         struct pci_controller *hose;
293
294         /*
295          * Create our single hose.
296          */
297
298         pci_isa_hose = hose = alloc_pci_controller();
299         hose->io_space = &ioport_resource;
300         hose->mem_space = &iomem_resource;
301         hose->index = 0;
302
303         hose->sparse_mem_base = LCA_SPARSE_MEM - IDENT_ADDR;
304         hose->dense_mem_base = LCA_DENSE_MEM - IDENT_ADDR;
305         hose->sparse_io_base = LCA_IO - IDENT_ADDR;
306         hose->dense_io_base = 0;
307
308         /*
309          * Set up the PCI to main memory translation windows.
310          *
311          * Window 0 is direct access 1GB at 1GB
312          * Window 1 is scatter-gather 8MB at 8MB (for isa)
313          */
314         hose->sg_isa = iommu_arena_new(hose, 0x00800000, 0x00800000, 0);
315         hose->sg_pci = NULL;
316         __direct_map_base = 0x40000000;
317         __direct_map_size = 0x40000000;
318
319         *(vulp)LCA_IOC_W_BASE0 = __direct_map_base | (2UL << 32);
320         *(vulp)LCA_IOC_W_MASK0 = (__direct_map_size - 1) & 0xfff00000;
321         *(vulp)LCA_IOC_T_BASE0 = 0;
322
323         *(vulp)LCA_IOC_W_BASE1 = hose->sg_isa->dma_base | (3UL << 32);
324         *(vulp)LCA_IOC_W_MASK1 = (hose->sg_isa->size - 1) & 0xfff00000;
325         *(vulp)LCA_IOC_T_BASE1 = virt_to_phys(hose->sg_isa->ptes);
326
327         *(vulp)LCA_IOC_TB_ENA = 0x80;
328
329         lca_pci_tbi(hose, 0, -1);
330
331         /*
332          * Disable PCI parity for now.  The NCR53c810 chip has
333          * troubles meeting the PCI spec which results in
334          * data parity errors.
335          */
336         *(vulp)LCA_IOC_PAR_DIS = 1UL<<5;
337 }
338
339 /*
340  * Constants used during machine-check handling.  I suppose these
341  * could be moved into lca.h but I don't see much reason why anybody
342  * else would want to use them.
343  */
344
345 #define ESR_EAV         (1UL<< 0)       /* error address valid */
346 #define ESR_CEE         (1UL<< 1)       /* correctable error */
347 #define ESR_UEE         (1UL<< 2)       /* uncorrectable error */
348 #define ESR_WRE         (1UL<< 3)       /* write-error */
349 #define ESR_SOR         (1UL<< 4)       /* error source */
350 #define ESR_CTE         (1UL<< 7)       /* cache-tag error */
351 #define ESR_MSE         (1UL<< 9)       /* multiple soft errors */
352 #define ESR_MHE         (1UL<<10)       /* multiple hard errors */
353 #define ESR_NXM         (1UL<<12)       /* non-existent memory */
354
355 #define IOC_ERR         (  1<<4)        /* ioc logs an error */
356 #define IOC_CMD_SHIFT   0
357 #define IOC_CMD         (0xf<<IOC_CMD_SHIFT)
358 #define IOC_CODE_SHIFT  8
359 #define IOC_CODE        (0xf<<IOC_CODE_SHIFT)
360 #define IOC_LOST        (  1<<5)
361 #define IOC_P_NBR       ((__u32) ~((1<<13) - 1))
362
363 static void
364 mem_error(unsigned long esr, unsigned long ear)
365 {
366         printk("    %s %s error to %s occurred at address %x\n",
367                ((esr & ESR_CEE) ? "Correctable" :
368                 (esr & ESR_UEE) ? "Uncorrectable" : "A"),
369                (esr & ESR_WRE) ? "write" : "read",
370                (esr & ESR_SOR) ? "memory" : "b-cache",
371                (unsigned) (ear & 0x1ffffff8));
372         if (esr & ESR_CTE) {
373                 printk("    A b-cache tag parity error was detected.\n");
374         }
375         if (esr & ESR_MSE) {
376                 printk("    Several other correctable errors occurred.\n");
377         }
378         if (esr & ESR_MHE) {
379                 printk("    Several other uncorrectable errors occurred.\n");
380         }
381         if (esr & ESR_NXM) {
382                 printk("    Attempted to access non-existent memory.\n");
383         }
384 }
385
386 static void
387 ioc_error(__u32 stat0, __u32 stat1)
388 {
389         static const char * const pci_cmd[] = {
390                 "Interrupt Acknowledge", "Special", "I/O Read", "I/O Write",
391                 "Rsvd 1", "Rsvd 2", "Memory Read", "Memory Write", "Rsvd3",
392                 "Rsvd4", "Configuration Read", "Configuration Write",
393                 "Memory Read Multiple", "Dual Address", "Memory Read Line",
394                 "Memory Write and Invalidate"
395         };
396         static const char * const err_name[] = {
397                 "exceeded retry limit", "no device", "bad data parity",
398                 "target abort", "bad address parity", "page table read error",
399                 "invalid page", "data error"
400         };
401         unsigned code = (stat0 & IOC_CODE) >> IOC_CODE_SHIFT;
402         unsigned cmd  = (stat0 & IOC_CMD)  >> IOC_CMD_SHIFT;
403
404         printk("    %s initiated PCI %s cycle to address %x"
405                " failed due to %s.\n",
406                code > 3 ? "PCI" : "CPU", pci_cmd[cmd], stat1, err_name[code]);
407
408         if (code == 5 || code == 6) {
409                 printk("    (Error occurred at PCI memory address %x.)\n",
410                        (stat0 & ~IOC_P_NBR));
411         }
412         if (stat0 & IOC_LOST) {
413                 printk("    Other PCI errors occurred simultaneously.\n");
414         }
415 }
416
417 void
418 lca_machine_check(unsigned long vector, unsigned long la_ptr,
419                   struct pt_regs *regs)
420 {
421         const char * reason;
422         union el_lca el;
423
424         el.c = (struct el_common *) la_ptr;
425
426         wrmces(rdmces());       /* reset machine check pending flag */
427
428         printk(KERN_CRIT "LCA machine check: vector=%#lx pc=%#lx code=%#x\n",
429                vector, regs->pc, (unsigned int) el.c->code);
430
431         /*
432          * The first quadword after the common header always seems to
433          * be the machine check reason---don't know why this isn't
434          * part of the common header instead.  In the case of a long
435          * logout frame, the upper 32 bits is the machine check
436          * revision level, which we ignore for now.
437          */
438         switch ((unsigned int) el.c->code) {
439         case MCHK_K_TPERR:      reason = "tag parity error"; break;
440         case MCHK_K_TCPERR:     reason = "tag control parity error"; break;
441         case MCHK_K_HERR:       reason = "access to non-existent memory"; break;
442         case MCHK_K_ECC_C:      reason = "correctable ECC error"; break;
443         case MCHK_K_ECC_NC:     reason = "non-correctable ECC error"; break;
444         case MCHK_K_CACKSOFT:   reason = "MCHK_K_CACKSOFT"; break;
445         case MCHK_K_BUGCHECK:   reason = "illegal exception in PAL mode"; break;
446         case MCHK_K_OS_BUGCHECK: reason = "callsys in kernel mode"; break;
447         case MCHK_K_DCPERR:     reason = "d-cache parity error"; break;
448         case MCHK_K_ICPERR:     reason = "i-cache parity error"; break;
449         case MCHK_K_SIO_SERR:   reason = "SIO SERR occurred on PCI bus"; break;
450         case MCHK_K_SIO_IOCHK:  reason = "SIO IOCHK occurred on ISA bus"; break;
451         case MCHK_K_DCSR:       reason = "MCHK_K_DCSR"; break;
452         case MCHK_K_UNKNOWN:
453         default:                reason = "unknown"; break;
454         }
455
456         switch (el.c->size) {
457         case sizeof(struct el_lca_mcheck_short):
458                 printk(KERN_CRIT
459                        "  Reason: %s (short frame%s, dc_stat=%#lx):\n",
460                        reason, el.c->retry ? ", retryable" : "",
461                        el.s->dc_stat);
462                 if (el.s->esr & ESR_EAV) {
463                         mem_error(el.s->esr, el.s->ear);
464                 }
465                 if (el.s->ioc_stat0 & IOC_ERR) {
466                         ioc_error(el.s->ioc_stat0, el.s->ioc_stat1);
467                 }
468                 break;
469
470         case sizeof(struct el_lca_mcheck_long):
471                 printk(KERN_CRIT "  Reason: %s (long frame%s):\n",
472                        reason, el.c->retry ? ", retryable" : "");
473                 printk(KERN_CRIT
474                        "    reason: %#lx  exc_addr: %#lx  dc_stat: %#lx\n", 
475                        el.l->pt[0], el.l->exc_addr, el.l->dc_stat);
476                 printk(KERN_CRIT "    car: %#lx\n", el.l->car);
477                 if (el.l->esr & ESR_EAV) {
478                         mem_error(el.l->esr, el.l->ear);
479                 }
480                 if (el.l->ioc_stat0 & IOC_ERR) {
481                         ioc_error(el.l->ioc_stat0, el.l->ioc_stat1);
482                 }
483                 break;
484
485         default:
486                 printk(KERN_CRIT "  Unknown errorlog size %d\n", el.c->size);
487         }
488
489         /* Dump the logout area to give all info.  */
490 #if DEBUG_MCHECK > 1
491         {
492                 unsigned long * ptr = (unsigned long *) la_ptr;
493                 long i;
494                 for (i = 0; i < el.c->size / sizeof(long); i += 2) {
495                         printk(KERN_CRIT " +%8lx %016lx %016lx\n",
496                                i*sizeof(long), ptr[i], ptr[i+1]);
497                 }
498         }
499 #endif
500 }
501
502 /*
503  * The following routines are needed to support the SPEED changing
504  * necessary to successfully manage the thermal problem on the AlphaBook1.
505  */
506
507 void
508 lca_clock_print(void)
509 {
510         long    pmr_reg;
511
512         pmr_reg = LCA_READ_PMR;
513
514         printk("Status of clock control:\n");
515         printk("\tPrimary clock divisor\t0x%lx\n", LCA_GET_PRIMARY(pmr_reg));
516         printk("\tOverride clock divisor\t0x%lx\n", LCA_GET_OVERRIDE(pmr_reg));
517         printk("\tInterrupt override is %s\n",
518                (pmr_reg & LCA_PMR_INTO) ? "on" : "off"); 
519         printk("\tDMA override is %s\n",
520                (pmr_reg & LCA_PMR_DMAO) ? "on" : "off"); 
521
522 }
523
524 int
525 lca_get_clock(void)
526 {
527         long    pmr_reg;
528
529         pmr_reg = LCA_READ_PMR;
530         return(LCA_GET_PRIMARY(pmr_reg));
531
532 }
533
534 void
535 lca_clock_fiddle(int divisor)
536 {
537         long    pmr_reg;
538
539         pmr_reg = LCA_READ_PMR;
540         LCA_SET_PRIMARY_CLOCK(pmr_reg, divisor);
541         /* lca_norm_clock = divisor; */
542         LCA_WRITE_PMR(pmr_reg);
543         mb();
544 }