added mtd driver
[linux-2.4.git] / arch / alpha / kernel / sys_marvel.c
1 /*
2  * linux/arch/alpha/kernel/sys_marvel.c
3  *
4  * Marvel / IO7 support
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/mm.h>
10 #include <linux/sched.h>
11 #include <linux/pci.h>
12 #include <linux/init.h>
13
14 #include <asm/ptrace.h>
15 #include <asm/system.h>
16 #include <asm/dma.h>
17 #include <asm/irq.h>
18 #include <asm/bitops.h>
19 #include <asm/mmu_context.h>
20 #include <asm/io.h>
21 #include <asm/pgtable.h>
22 #include <asm/core_marvel.h>
23 #include <asm/hwrpb.h>
24
25 #include "proto.h"
26 #include "err_impl.h"
27 #include "irq_impl.h"
28 #include "pci_impl.h"
29 #include "machvec_impl.h"
30
31 #if NR_IRQS < MARVEL_NR_IRQS
32 # error NR_IRQS < MARVEL_NR_IRQS !!!
33 #endif
34
35 \f
36 /*
37  * Interrupt handling.
38  */
39 static void 
40 io7_device_interrupt(unsigned long vector, struct pt_regs * regs)
41 {
42         unsigned int pid;
43         unsigned int irq;
44
45         /*
46          * Vector is 0x800 + (interrupt)
47          *
48          * where (interrupt) is:
49          *
50          *      ...16|15 14|13     4|3 0
51          *      -----+-----+--------+---
52          *        PE |  0  |   irq  | 0
53          *
54          * where (irq) is 
55          *
56          *       0x0800 - 0x0ff0         - 0x0800 + (LSI id << 4)
57          *       0x1000 - 0x2ff0         - 0x1000 + (MSI_DAT<8:0> << 4)
58          */
59         pid = vector >> 16;
60         irq = ((vector & 0xffff) - 0x800) >> 4;
61
62         irq += 16;                              /* offset for legacy */
63         irq &= MARVEL_IRQ_VEC_IRQ_MASK;         /* not too many bits */
64         irq |= pid << MARVEL_IRQ_VEC_PE_SHIFT;  /* merge the pid     */
65
66         handle_irq(irq, regs);
67 }
68
69 static volatile unsigned long *
70 io7_get_irq_ctl(unsigned int irq, struct io7 **pio7)
71 {
72         volatile unsigned long *ctl;
73         unsigned int pid;
74         struct io7 *io7;
75
76         pid = irq >> MARVEL_IRQ_VEC_PE_SHIFT;
77
78         if (!(io7 = marvel_find_io7(pid))) {
79                 printk(KERN_ERR 
80                        "%s for nonexistent io7 -- vec %x, pid %d\n",
81                        __FUNCTION__, irq, pid);
82                 return NULL;
83         }
84
85         irq &= MARVEL_IRQ_VEC_IRQ_MASK; /* isolate the vector    */
86         irq -= 16;                      /* subtract legacy bias  */
87
88         if (irq >= 0x180) {
89                 printk(KERN_ERR 
90                        "%s for invalid irq -- pid %d adjusted irq %x\n",
91                        __FUNCTION__, pid, irq);
92                 return NULL;
93         }
94
95         ctl = &io7->csrs->PO7_LSI_CTL[irq & 0xff].csr; /* assume LSI */
96         if (irq >= 0x80)                /* MSI */
97                 ctl = &io7->csrs->PO7_MSI_CTL[((irq - 0x80) >> 5) & 0x0f].csr;
98
99         if (pio7) *pio7 = io7;
100         return ctl;
101 }
102
103 static void
104 io7_enable_irq(unsigned int irq)
105 {
106         volatile unsigned long *ctl;
107         struct io7 *io7;
108
109         ctl = io7_get_irq_ctl(irq, &io7);
110         if (!ctl || !io7) {
111                 printk(KERN_ERR "%s: get_ctl failed for irq %x\n", 
112                        __FUNCTION__, irq);
113                 return;
114         }
115                 
116         spin_lock(&io7->irq_lock);
117         *ctl |= 1UL << 24;
118         mb();
119         *ctl;
120         spin_unlock(&io7->irq_lock);
121 }
122
123 static void
124 io7_disable_irq(unsigned int irq)
125 {
126         volatile unsigned long *ctl;
127         struct io7 *io7;
128
129         ctl = io7_get_irq_ctl(irq, &io7);
130         if (!ctl || !io7) {
131                 printk(KERN_ERR "%s: get_ctl failed for irq %x\n", 
132                        __FUNCTION__, irq);
133                 return;
134         }
135                 
136         spin_lock(&io7->irq_lock);
137         *ctl &= ~(1UL << 24);
138         mb();
139         *ctl;
140         spin_unlock(&io7->irq_lock);
141 }
142
143 static unsigned int
144 io7_startup_irq(unsigned int irq)
145 {
146         io7_enable_irq(irq);
147         return 0;       /* never anything pending */
148 }
149
150 static void
151 io7_end_irq(unsigned int irq)
152 {
153         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
154                 io7_enable_irq(irq);
155 }
156
157 static void
158 marvel_irq_noop(unsigned int irq) 
159
160         return; 
161 }
162
163 static unsigned int
164 marvel_irq_noop_return(unsigned int irq) 
165
166         return 0; 
167 }
168
169 static struct hw_interrupt_type marvel_legacy_irq_type = {
170         typename:       "LEGACY",
171         startup:        marvel_irq_noop_return,
172         shutdown:       marvel_irq_noop,
173         enable:         marvel_irq_noop,
174         disable:        marvel_irq_noop,
175         ack:            marvel_irq_noop,
176         end:            marvel_irq_noop,
177 };
178
179 static struct hw_interrupt_type io7_lsi_irq_type = {
180         typename:       "LSI",
181         startup:        io7_startup_irq,
182         shutdown:       io7_disable_irq,
183         enable:         io7_enable_irq,
184         disable:        io7_disable_irq,
185         ack:            io7_disable_irq,
186         end:            io7_end_irq,
187 };
188
189 static struct hw_interrupt_type io7_msi_irq_type = {
190         typename:       "MSI",
191         startup:        io7_startup_irq,
192         shutdown:       io7_disable_irq,
193         enable:         io7_enable_irq,
194         disable:        io7_disable_irq,
195         ack:            marvel_irq_noop,
196         end:            io7_end_irq,
197 };
198
199 static void
200 io7_redirect_irq(struct io7 *io7, 
201                  volatile unsigned long *csr, 
202                  unsigned int where)
203 {
204         unsigned long val;
205         
206         val = *csr;
207         val &= ~(0x1ffUL << 24);                /* clear the target pid   */
208         val |= ((unsigned long)where << 24);    /* set the new target pid */
209         
210         *csr = val;
211         mb();
212         *csr;
213 }
214
215 static void 
216 io7_redirect_one_lsi(struct io7 *io7, unsigned int which, unsigned int where)
217 {
218         unsigned long val;
219
220         /*
221          * LSI_CTL has target PID @ 14
222          */
223         val = io7->csrs->PO7_LSI_CTL[which].csr;
224         val &= ~(0x1ffUL << 14);                /* clear the target pid */
225         val |= ((unsigned long)where << 14);    /* set teh new target pid */
226
227         io7->csrs->PO7_LSI_CTL[which].csr = val;
228         mb();
229         io7->csrs->PO7_LSI_CTL[which].csr;
230 }
231
232 static void 
233 io7_redirect_one_msi(struct io7 *io7, unsigned int which, unsigned int where)
234 {
235         unsigned long val;
236
237         /*
238          * MSI_CTL has target PID @ 14
239          */
240         val = io7->csrs->PO7_MSI_CTL[which].csr;
241         val &= ~(0x1ffUL << 14);                /* clear the target pid */
242         val |= ((unsigned long)where << 14);    /* set teh new target pid */
243
244         io7->csrs->PO7_MSI_CTL[which].csr = val;
245         mb();
246         io7->csrs->PO7_MSI_CTL[which].csr;
247 }
248
249 static void __init
250 init_one_io7_lsi(struct io7 *io7, unsigned int which, unsigned int where)
251 {
252         /*
253          * LSI_CTL has target PID @ 14
254          */
255         io7->csrs->PO7_LSI_CTL[which].csr = ((unsigned long)where << 14);
256         mb();
257         io7->csrs->PO7_LSI_CTL[which].csr;
258 }
259
260 static void __init
261 init_one_io7_msi(struct io7 *io7, unsigned int which, unsigned int where)
262 {
263         /*
264          * MSI_CTL has target PID @ 14
265          */
266         io7->csrs->PO7_MSI_CTL[which].csr = ((unsigned long)where << 14);
267         mb();
268         io7->csrs->PO7_MSI_CTL[which].csr;
269 }
270
271 static void __init
272 init_io7_irqs(struct io7 *io7, 
273               struct hw_interrupt_type *lsi_ops,
274               struct hw_interrupt_type *msi_ops)
275 {
276         long base = (io7->pe << MARVEL_IRQ_VEC_PE_SHIFT) + 16;
277         long i;
278
279         printk("Initializing interrupts for IO7 at PE %u - base %lx\n",
280                 io7->pe, base);
281
282         /*
283          * Where should interrupts from this IO7 go?
284          *
285          * They really should be sent to the local CPU to avoid having to
286          * traverse the mesh, but if it's not an SMP kernel, they have to
287          * go to the boot CPU. Send them all to the boot CPU for now,
288          * as each secondary starts, it can redirect it's local device
289          * interrupts.
290          */
291         printk("  Interrupts reported to CPU at PE %u\n", boot_cpuid);
292
293         spin_lock(&io7->irq_lock);
294
295         /* set up the error irqs */
296         io7_redirect_irq(io7, &io7->csrs->HLT_CTL.csr, boot_cpuid);
297         io7_redirect_irq(io7, &io7->csrs->HPI_CTL.csr, boot_cpuid);
298         io7_redirect_irq(io7, &io7->csrs->CRD_CTL.csr, boot_cpuid);
299         io7_redirect_irq(io7, &io7->csrs->STV_CTL.csr, boot_cpuid);
300         io7_redirect_irq(io7, &io7->csrs->HEI_CTL.csr, boot_cpuid);
301
302         /* Set up the lsi irqs.  */
303         for (i = 0; i < 128; ++i) {
304                 irq_desc[base + i].status = IRQ_DISABLED | IRQ_LEVEL;
305                 irq_desc[base + i].handler = lsi_ops;
306         }
307
308         /* Disable the implemented irqs in hardware.  */
309         for (i = 0; i < 0x60; ++i) 
310                 init_one_io7_lsi(io7, i, boot_cpuid);
311
312         init_one_io7_lsi(io7, 0x74, boot_cpuid);
313         init_one_io7_lsi(io7, 0x75, boot_cpuid);
314
315
316         /* Set up the msi irqs.  */
317         for (i = 128; i < (128 + 512); ++i) {
318                 irq_desc[base + i].status = IRQ_DISABLED | IRQ_LEVEL;
319                 irq_desc[base + i].handler = msi_ops;
320         }
321
322         for (i = 0; i < 16; ++i)
323                 init_one_io7_msi(io7, i, boot_cpuid);
324
325         spin_unlock(&io7->irq_lock);
326 }
327
328 static void __init
329 marvel_init_irq(void)
330 {
331         int i;
332         struct io7 *io7 = NULL;
333
334         /* Reserve the legacy irqs.  */
335         for (i = 0; i < 16; ++i) {
336                 irq_desc[i].status = IRQ_DISABLED;
337                 irq_desc[i].handler = &marvel_legacy_irq_type;
338         }
339
340         /* Init the io7 irqs.  */
341         for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; )
342                 init_io7_irqs(io7, &io7_lsi_irq_type, &io7_msi_irq_type);
343 }
344
345 static int 
346 marvel_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
347 {
348         struct pci_controller *hose = dev->sysdata;
349         struct io7_port *io7_port = hose->sysdata;
350         struct io7 *io7 = io7_port->io7;
351         int msi_loc, msi_data_off;
352         u16 msg_ctl;
353         u16 msg_dat;
354         u8 intline; 
355         int irq;
356
357         pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &intline);
358         irq = intline;
359
360         msi_loc = pci_find_capability(dev, PCI_CAP_ID_MSI);
361         msg_ctl = 0;
362         if (msi_loc) 
363                 pci_read_config_word(dev, msi_loc + PCI_MSI_FLAGS, &msg_ctl);
364
365         if (msg_ctl & PCI_MSI_FLAGS_ENABLE) {
366                 msi_data_off = PCI_MSI_DATA_32;
367                 if (msg_ctl & PCI_MSI_FLAGS_64BIT) 
368                         msi_data_off = PCI_MSI_DATA_64;
369                 pci_read_config_word(dev, msi_loc + msi_data_off, &msg_dat);
370
371                 irq = msg_dat & 0x1ff;          /* we use msg_data<8:0> */
372                 irq += 0x80;                    /* offset for lsi       */
373
374 #if 1
375                 printk("PCI:%d:%d:%d (hose %d) [%s] is using MSI\n",
376                        dev->bus->number, 
377                        PCI_SLOT(dev->devfn), 
378                        PCI_FUNC(dev->devfn),
379                        hose->index,
380                        dev->name);
381                 printk("  %d message(s) from 0x%04x\n", 
382                        1 << ((msg_ctl & PCI_MSI_FLAGS_QSIZE) >> 4),
383                        msg_dat);
384                 printk("  reporting on %d IRQ(s) from %d (0x%x)\n", 
385                        1 << ((msg_ctl & PCI_MSI_FLAGS_QSIZE) >> 4),
386                        (irq + 16) | (io7->pe << MARVEL_IRQ_VEC_PE_SHIFT),
387                        (irq + 16) | (io7->pe << MARVEL_IRQ_VEC_PE_SHIFT));
388 #endif
389
390 #if 0
391                 pci_write_config_word(dev, msi_loc + PCI_MSI_FLAGS,
392                                       msg_ctl & ~PCI_MSI_FLAGS_ENABLE);
393                 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &intline);
394                 irq = intline;
395
396                 printk("  forcing LSI interrupt on irq %d [0x%x]\n", irq, irq);
397 #endif
398         }
399
400         irq += 16;                                      /* offset for legacy */
401         irq |= io7->pe << MARVEL_IRQ_VEC_PE_SHIFT;      /* merge the pid     */
402
403         return irq; 
404 }
405
406 static void __init
407 marvel_init_pci(void)
408 {
409         struct io7 *io7;
410
411         marvel_register_error_handlers();
412
413         pci_probe_only = 1;
414         common_init_pci();
415
416 #ifdef CONFIG_VGA_HOSE
417         locate_and_init_vga(NULL);
418 #endif
419
420         /* Clear any io7 errors.  */
421         for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) 
422                 io7_clear_errors(io7);
423 }
424
425 static void
426 marvel_init_rtc(void)
427 {
428         init_rtc_irq();
429 }
430
431 static void
432 marvel_smp_callin(void)
433 {
434         int cpuid = hard_smp_processor_id();
435         struct io7 *io7 = marvel_find_io7(cpuid);
436         unsigned int i;
437
438         if (!io7)
439                 return;
440
441         /* 
442          * There is a local IO7 - redirect all of it's interrupts here.
443          */
444         printk("Redirecting IO7 interrupts to local CPU at PE %u\n", cpuid);
445
446         /* Redirect the error IRQS here.  */
447         io7_redirect_irq(io7, &io7->csrs->HLT_CTL.csr, cpuid);
448         io7_redirect_irq(io7, &io7->csrs->HPI_CTL.csr, cpuid);
449         io7_redirect_irq(io7, &io7->csrs->CRD_CTL.csr, cpuid);
450         io7_redirect_irq(io7, &io7->csrs->STV_CTL.csr, cpuid);
451         io7_redirect_irq(io7, &io7->csrs->HEI_CTL.csr, cpuid);
452
453         /* Redirect the implemented LSIs here.  */
454         for (i = 0; i < 0x60; ++i) 
455                 io7_redirect_one_lsi(io7, i, cpuid);
456
457         io7_redirect_one_lsi(io7, 0x74, cpuid);
458         io7_redirect_one_lsi(io7, 0x75, cpuid);
459
460         /* Redirect the MSIs here.  */
461         for (i = 0; i < 16; ++i)
462                 io7_redirect_one_msi(io7, i, cpuid);
463 }
464 \f
465 /*
466  * System Vectors
467  */
468 struct alpha_machine_vector marvel_ev7_mv __initmv = {
469         vector_name:            "MARVEL/EV7",
470         DO_EV7_MMU,
471         DO_DEFAULT_RTC,
472         DO_MARVEL_IO,
473         DO_MARVEL_BUS,
474         machine_check:          marvel_machine_check,
475         max_dma_address:        ALPHA_MAX_DMA_ADDRESS,
476         min_io_address:         DEFAULT_IO_BASE,
477         min_mem_address:        DEFAULT_MEM_BASE,
478         pci_dac_offset:         IO7_DAC_OFFSET,
479
480         nr_irqs:                MARVEL_NR_IRQS,
481         device_interrupt:       io7_device_interrupt,
482
483         agp_info:               marvel_agp_info,
484
485         smp_callin:             marvel_smp_callin,
486         init_arch:              marvel_init_arch,
487         init_irq:               marvel_init_irq,
488         init_rtc:               marvel_init_rtc,
489         init_pci:               marvel_init_pci,
490         kill_arch:              marvel_kill_arch,
491         pci_map_irq:            marvel_map_irq,
492         pci_swizzle:            common_swizzle,
493
494         pa_to_nid:              marvel_pa_to_nid,
495         cpuid_to_nid:           marvel_cpuid_to_nid,
496         node_mem_start:         marvel_node_mem_start,
497         node_mem_size:          marvel_node_mem_size,
498 };
499 ALIAS_MV(marvel_ev7)