patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / alpha / kernel / core_marvel.c
1 /*
2  *      linux/arch/alpha/kernel/core_marvel.c
3  *
4  * Code common to all Marvel based systems.
5  */
6
7 #define __EXTERN_INLINE inline
8 #include <asm/io.h>
9 #include <asm/core_marvel.h>
10 #undef __EXTERN_INLINE
11
12 #include <linux/types.h>
13 #include <linux/pci.h>
14 #include <linux/sched.h>
15 #include <linux/init.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mc146818rtc.h>
18 #include <linux/rtc.h>
19 #include <linux/module.h>
20 #include <linux/bootmem.h>
21
22 #include <asm/ptrace.h>
23 #include <asm/smp.h>
24 #include <asm/gct.h>
25 #include <asm/pgalloc.h>
26 #include <asm/tlbflush.h>
27 #include <asm/rtc.h>
28
29 #include "proto.h"
30 #include "pci_impl.h"
31
32 \f
33 /*
34  * Debug helpers
35  */
36 #define DEBUG_CONFIG 0
37
38 #if DEBUG_CONFIG
39 # define DBG_CFG(args) printk args
40 #else
41 # define DBG_CFG(args)
42 #endif
43
44 \f
45 /*
46  * Private data
47  */
48 static struct io7 *io7_head = NULL;
49
50 \f
51 /*
52  * Helper functions
53  */
54 static unsigned long __attribute__ ((unused))
55 read_ev7_csr(int pe, unsigned long offset)
56 {
57         ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
58         unsigned long q;
59
60         mb();
61         q = ev7csr->csr;
62         mb();
63
64         return q;
65 }
66
67 static void __attribute__ ((unused))
68 write_ev7_csr(int pe, unsigned long offset, unsigned long q)
69 {
70         ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
71
72         mb();
73         ev7csr->csr = q;
74         mb();
75 }
76
77 static char * __init
78 mk_resource_name(int pe, int port, char *str)
79 {
80         char tmp[80];
81         char *name;
82         
83         sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
84         name = alloc_bootmem(strlen(tmp) + 1);
85         strcpy(name, tmp);
86
87         return name;
88 }
89
90 inline struct io7 *
91 marvel_next_io7(struct io7 *prev)
92 {
93         return (prev ? prev->next : io7_head);
94 }
95
96 struct io7 *
97 marvel_find_io7(int pe)
98 {
99         struct io7 *io7;
100
101         for (io7 = io7_head; io7 && io7->pe != pe; io7 = io7->next)
102                 continue;
103
104         return io7;
105 }
106
107 static struct io7 * __init
108 alloc_io7(unsigned int pe)
109 {
110         struct io7 *io7;
111         struct io7 *insp;
112         int h;
113
114         if (marvel_find_io7(pe)) {
115                 printk(KERN_WARNING "IO7 at PE %d already allocated!\n", pe);
116                 return NULL;
117         }
118
119         io7 = alloc_bootmem(sizeof(*io7));
120         io7->pe = pe;
121         io7->irq_lock = SPIN_LOCK_UNLOCKED;
122
123         for (h = 0; h < 4; h++) {
124                 io7->ports[h].io7 = io7;
125                 io7->ports[h].port = h;
126                 io7->ports[h].enabled = 0; /* default to disabled */
127         }
128
129         /*
130          * Insert in pe sorted order.
131          */
132         if (NULL == io7_head)                   /* empty list */
133                 io7_head = io7; 
134         else if (io7_head->pe > io7->pe) {      /* insert at head */
135                 io7->next = io7_head;
136                 io7_head = io7;
137         } else {                                /* insert at position */
138                 for (insp = io7_head; insp; insp = insp->next) {
139                         if (insp->pe == io7->pe) {
140                                 printk(KERN_ERR "Too many IO7s at PE %d\n", 
141                                        io7->pe);
142                                 return NULL;
143                         }
144
145                         if (NULL == insp->next || 
146                             insp->next->pe > io7->pe) { /* insert here */
147                                 io7->next = insp->next;
148                                 insp->next = io7;
149                                 break;
150                         }
151                 }
152
153                 if (NULL == insp) { /* couldn't insert ?!? */
154                         printk(KERN_WARNING "Failed to insert IO7 at PE %d "
155                                " - adding at head of list\n", io7->pe);
156                         io7->next = io7_head;
157                         io7_head = io7;
158                 }
159         }
160         
161         return io7;
162 }
163
164 void
165 io7_clear_errors(struct io7 *io7)
166 {
167         io7_port7_csrs *p7csrs;
168         io7_ioport_csrs *csrs;
169         int port;
170
171
172         /*
173          * First the IO ports.
174          */
175         for (port = 0; port < 4; port++) {
176                 csrs = IO7_CSRS_KERN(io7->pe, port);
177
178                 csrs->POx_ERR_SUM.csr = -1UL;
179                 csrs->POx_TLB_ERR.csr = -1UL;
180                 csrs->POx_SPL_COMPLT.csr = -1UL;
181                 csrs->POx_TRANS_SUM.csr = -1UL;
182         }
183
184         /*
185          * Then the common ones.
186          */
187         p7csrs = IO7_PORT7_CSRS_KERN(io7->pe);
188
189         p7csrs->PO7_ERROR_SUM.csr = -1UL;
190         p7csrs->PO7_UNCRR_SYM.csr = -1UL;
191         p7csrs->PO7_CRRCT_SYM.csr = -1UL;
192 }
193
194 \f
195 /*
196  * IO7 PCI, PCI/X, AGP configuration.
197  */
198 static void __init
199 io7_init_hose(struct io7 *io7, int port)
200 {
201         static int hose_index = 0;
202
203         struct pci_controller *hose = alloc_pci_controller();
204         struct io7_port *io7_port = &io7->ports[port];
205         io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, port);
206         int i;
207
208         hose->index = hose_index++;     /* arbitrary */
209         
210         /*
211          * We don't have an isa or legacy hose, but glibc expects to be
212          * able to use the bus == 0 / dev == 0 form of the iobase syscall
213          * to determine information about the i/o system. Since XFree86 
214          * relies on glibc's determination to tell whether or not to use
215          * sparse access, we need to point the pci_isa_hose at a real hose
216          * so at least that determination is correct.
217          */
218         if (hose->index == 0)
219                 pci_isa_hose = hose;
220
221         io7_port->csrs = csrs;
222         io7_port->hose = hose;
223         hose->sysdata = io7_port;
224
225         hose->io_space = alloc_resource();
226         hose->mem_space = alloc_resource();
227
228         /*
229          * Base addresses for userland consumption. Since these are going
230          * to be mapped, they are pure physical addresses.
231          */
232         hose->sparse_mem_base = hose->sparse_io_base = 0;
233         hose->dense_mem_base = IO7_MEM_PHYS(io7->pe, port);
234         hose->dense_io_base = IO7_IO_PHYS(io7->pe, port);
235
236         /*
237          * Base addresses and resource ranges for kernel consumption.
238          */
239         hose->config_space_base = (unsigned long)IO7_CONF_KERN(io7->pe, port);
240
241         hose->io_space->start = (unsigned long)IO7_IO_KERN(io7->pe, port);
242         hose->io_space->end = hose->io_space->start + IO7_IO_SPACE - 1;
243         hose->io_space->name = mk_resource_name(io7->pe, port, "IO");
244         hose->io_space->flags = IORESOURCE_IO;
245
246         hose->mem_space->start = (unsigned long)IO7_MEM_KERN(io7->pe, port);
247         hose->mem_space->end = hose->mem_space->start + IO7_MEM_SPACE - 1;
248         hose->mem_space->name = mk_resource_name(io7->pe, port, "MEM");
249         hose->mem_space->flags = IORESOURCE_MEM;
250
251         if (request_resource(&ioport_resource, hose->io_space) < 0)
252                 printk(KERN_ERR "Failed to request IO on hose %d\n", 
253                        hose->index);
254         if (request_resource(&iomem_resource, hose->mem_space) < 0)
255                 printk(KERN_ERR "Failed to request MEM on hose %d\n", 
256                        hose->index);
257
258         /*
259          * Save the existing DMA window settings for later restoration.
260          */
261         for (i = 0; i < 4; i++) {
262                 io7_port->saved_wbase[i] = csrs->POx_WBASE[i].csr;
263                 io7_port->saved_wmask[i] = csrs->POx_WMASK[i].csr;
264                 io7_port->saved_tbase[i] = csrs->POx_TBASE[i].csr;
265         }
266
267         /*
268          * Set up the PCI to main memory translation windows.
269          *
270          * Window 0 is scatter-gather 8MB at 8MB
271          * Window 1 is direct access 1GB at 2GB
272          * Window 2 is scatter-gather (up-to) 1GB at 3GB
273          * Window 3 is disabled
274          */
275
276         /*
277          * TBIA before modifying windows.
278          */
279         marvel_pci_tbi(hose, 0, -1);
280
281         /*
282          * Set up window 0 for scatter-gather 8MB at 8MB.
283          */
284         hose->sg_isa = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
285                                             hose, 0x00800000, 0x00800000, 0);
286         hose->sg_isa->align_entry = 8;  /* cache line boundary */
287         csrs->POx_WBASE[0].csr = 
288                 hose->sg_isa->dma_base | wbase_m_ena | wbase_m_sg;
289         csrs->POx_WMASK[0].csr = (hose->sg_isa->size - 1) & wbase_m_addr;
290         csrs->POx_TBASE[0].csr = virt_to_phys(hose->sg_isa->ptes);
291
292         /*
293          * Set up window 1 for direct-mapped 1GB at 2GB.
294          */
295         csrs->POx_WBASE[1].csr = __direct_map_base | wbase_m_ena;
296         csrs->POx_WMASK[1].csr = (__direct_map_size - 1) & wbase_m_addr;
297         csrs->POx_TBASE[1].csr = 0;
298
299         /*
300          * Set up window 2 for scatter-gather (up-to) 1GB at 3GB.
301          */
302         hose->sg_pci = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
303                                             hose, 0xc0000000, 0x40000000, 0);
304         hose->sg_pci->align_entry = 8;  /* cache line boundary */
305         csrs->POx_WBASE[2].csr = 
306                 hose->sg_pci->dma_base | wbase_m_ena | wbase_m_sg;
307         csrs->POx_WMASK[2].csr = (hose->sg_pci->size - 1) & wbase_m_addr;
308         csrs->POx_TBASE[2].csr = virt_to_phys(hose->sg_pci->ptes);
309
310         /*
311          * Disable window 3.
312          */
313         csrs->POx_WBASE[3].csr = 0;
314
315         /*
316          * Make sure that the AGP Monster Window is disabled.
317          */
318         csrs->POx_CTRL.csr &= ~(1UL << 61);
319
320 #if 1
321         printk("FIXME: disabling master aborts\n");
322         csrs->POx_MSK_HEI.csr &= ~(3UL << 14);
323 #endif
324         /*
325          * TBIA after modifying windows.
326          */
327         marvel_pci_tbi(hose, 0, -1);
328 }
329
330 static void __init
331 marvel_init_io7(struct io7 *io7)
332 {
333         int i;
334
335         printk("Initializing IO7 at PID %d\n", io7->pe);
336
337         /*
338          * Get the Port 7 CSR pointer.
339          */
340         io7->csrs = IO7_PORT7_CSRS_KERN(io7->pe);
341
342         /*
343          * Init this IO7's hoses.
344          */
345         for (i = 0; i < IO7_NUM_PORTS; i++) {
346                 io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, i);
347                 if (csrs->POx_CACHE_CTL.csr == 8) {
348                         io7->ports[i].enabled = 1;
349                         io7_init_hose(io7, i);
350                 }
351         }
352 }
353
354 void
355 marvel_io7_present(gct6_node *node)
356 {
357         int pe;
358
359         if (node->type != GCT_TYPE_HOSE ||
360             node->subtype != GCT_SUBTYPE_IO_PORT_MODULE) 
361                 return;
362
363         pe = (node->id >> 8) & 0xff;
364         printk("Found an IO7 at PID %d\n", pe);
365
366         alloc_io7(pe);
367 }
368
369 static void __init
370 marvel_init_vga_hose(void)
371 {
372 #ifdef CONFIG_VGA_HOSE
373         u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
374
375         if (pu64[7] == 3) {     /* TERM_TYPE == graphics */
376                 struct pci_controller *hose = NULL;
377                 int h = (pu64[30] >> 24) & 0xff; /* TERM_OUT_LOC, hose # */
378                 struct io7 *io7;
379                 int pid, port;
380
381                 /* FIXME - encoding is going to have to change for Marvel
382                  *         since hose will be able to overflow a byte...
383                  *         need to fix this decode when the console 
384                  *         changes its encoding
385                  */
386                 printk("console graphics is on hose %d (console)\n", h);
387
388                 /*
389                  * The console's hose numbering is:
390                  *
391                  *      hose<n:2>: PID
392                  *      hose<1:0>: PORT
393                  *
394                  * We need to find the hose at that pid and port
395                  */
396                 pid = h >> 2;
397                 port = h & 3;
398                 if ((io7 = marvel_find_io7(pid)))
399                         hose = io7->ports[port].hose;
400
401                 if (hose) {
402                         printk("Console graphics on hose %d\n", hose->index);
403                         pci_vga_hose = hose;
404                 }
405         }
406 #endif /* CONFIG_VGA_HOSE */
407 }
408
409 gct6_search_struct gct_wanted_node_list[] = {
410         { GCT_TYPE_HOSE, GCT_SUBTYPE_IO_PORT_MODULE, marvel_io7_present },
411         { 0, 0, NULL }
412 };
413
414 /*
415  * In case the GCT is not complete, let the user specify PIDs with IO7s
416  * at boot time. Syntax is 'io7=a,b,c,...,n' where a-n are the PIDs (decimal)
417  * where IO7s are connected
418  */
419 static int __init
420 marvel_specify_io7(char *str)
421 {
422         unsigned long pid;
423         struct io7 *io7;
424         char *pchar;
425
426         do {
427                 pid = simple_strtoul(str, &pchar, 0);
428                 if (pchar != str) {
429                         printk("User-specified IO7 at PID %lu\n", pid);
430                         io7 = alloc_io7(pid);
431                         if (io7) marvel_init_io7(io7);
432                 }
433
434                 if (pchar == str) pchar++;
435                 str = pchar;
436         } while(*str);
437
438         return 0;
439 }
440 __setup("io7=", marvel_specify_io7);
441
442 void __init
443 marvel_init_arch(void)
444 {
445         struct io7 *io7;
446
447         /* With multiple PCI busses, we play with I/O as physical addrs.  */
448         ioport_resource.end = ~0UL;
449
450         /* PCI DMA Direct Mapping is 1GB at 2GB.  */
451         __direct_map_base = 0x80000000;
452         __direct_map_size = 0x40000000;
453
454         /* Parse the config tree.  */
455         gct6_find_nodes(GCT_NODE_PTR(0), gct_wanted_node_list);
456
457         /* Init the io7s.  */
458         for (io7 = NULL; NULL != (io7 = marvel_next_io7(io7)); ) 
459                 marvel_init_io7(io7);
460
461         /* Check for graphic console location (if any).  */
462         marvel_init_vga_hose();
463 }
464
465 void
466 marvel_kill_arch(int mode)
467 {
468 }
469
470 \f
471 /*
472  * PCI Configuration Space access functions
473  *
474  * Configuration space addresses have the following format:
475  *
476  *      |2 2 2 2|1 1 1 1|1 1 1 1|1 1 
477  *      |3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
478  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479  *      |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|R|R|
480  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
481  *
482  *       n:24   reserved for hose base
483  *      23:16   bus number (8 bits = 128 possible buses)
484  *      15:11   Device number (5 bits)
485  *      10:8    function number
486  *       7:2    register number
487  *  
488  * Notes:
489  *      IO7 determines whether to use a type 0 or type 1 config cycle
490  *      based on the bus number. Therefore the bus number must be set 
491  *      to 0 for the root bus on any hose.
492  *      
493  *      The function number selects which function of a multi-function device 
494  *      (e.g., SCSI and Ethernet).
495  * 
496  */
497
498 static inline unsigned long
499 build_conf_addr(struct pci_controller *hose, u8 bus, 
500                 unsigned int devfn, int where)
501 {
502         return (hose->config_space_base | (bus << 16) | (devfn << 8) | where);
503 }
504
505 static unsigned long
506 mk_conf_addr(struct pci_bus *pbus, unsigned int devfn, int where)
507 {
508         struct pci_controller *hose = pbus->sysdata;
509         struct io7_port *io7_port;
510         unsigned long addr = 0;
511         u8 bus = pbus->number;
512
513         if (!hose)
514                 return addr;
515
516         /* Check for enabled.  */
517         io7_port = hose->sysdata;
518         if (!io7_port->enabled)
519                 return addr;
520
521         if (!pbus->parent) { /* No parent means peer PCI bus. */
522                 /* Don't support idsel > 20 on primary bus.  */
523                 if (devfn >= PCI_DEVFN(21, 0))
524                         return addr;
525                 bus = 0;
526         }
527
528         addr = build_conf_addr(hose, bus, devfn, where);
529
530         DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
531         return addr;
532 }
533
534 static int
535 marvel_read_config(struct pci_bus *bus, unsigned int devfn, int where,
536                    int size, u32 *value)
537 {
538         unsigned long addr;
539         
540         if (0 == (addr = mk_conf_addr(bus, devfn, where)))
541                 return PCIBIOS_DEVICE_NOT_FOUND;
542
543         switch(size) {
544         case 1: 
545                 *value = __kernel_ldbu(*(vucp)addr);
546                 break;
547         case 2: 
548                 *value = __kernel_ldwu(*(vusp)addr);
549                 break;
550         case 4: 
551                 *value = *(vuip)addr;
552                 break;
553         default:
554                 return PCIBIOS_FUNC_NOT_SUPPORTED;
555         }
556
557         return PCIBIOS_SUCCESSFUL;
558 }
559
560 static int
561 marvel_write_config(struct pci_bus *bus, unsigned int devfn, int where,
562                     int size, u32 value)
563 {
564         unsigned long addr;
565         
566         if (0 == (addr = mk_conf_addr(bus, devfn, where)))
567                 return PCIBIOS_DEVICE_NOT_FOUND;
568
569         switch (size) {
570         case 1:
571                 __kernel_stb(value, *(vucp)addr);
572                 mb();
573                 __kernel_ldbu(*(vucp)addr);
574                 break;
575         case 2:
576                 __kernel_stw(value, *(vusp)addr);
577                 mb();
578                 __kernel_ldwu(*(vusp)addr);
579                 break;
580         case 4:
581                 *(vuip)addr = value;
582                 mb();
583                 *(vuip)addr;
584                 break;
585         default:
586                 return PCIBIOS_FUNC_NOT_SUPPORTED;
587         }
588
589         return PCIBIOS_SUCCESSFUL;
590 }
591
592 struct pci_ops marvel_pci_ops =
593 {
594         .read =         marvel_read_config,
595         .write =        marvel_write_config,
596 };
597
598 \f
599 /*
600  * Other PCI helper functions.
601  */
602 void
603 marvel_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
604 {
605         io7_ioport_csrs *csrs = ((struct io7_port *)hose->sysdata)->csrs;
606
607         wmb();
608         csrs->POx_SG_TBIA.csr = 0;
609         mb();
610         csrs->POx_SG_TBIA.csr;
611 }
612
613 \f
614 /*
615  * IO map support.
616  */
617 unsigned long
618 marvel_ioremap(unsigned long addr, unsigned long size)
619 {
620         struct pci_controller *hose;
621         unsigned long baddr, last;
622         struct vm_struct *area;
623         unsigned long vaddr;
624         unsigned long *ptes;
625         unsigned long pfn;
626
627         /*
628          * Adjust the addr.
629          */ 
630 #ifdef CONFIG_VGA_HOSE
631         if (pci_vga_hose && __marvel_is_mem_vga(addr)) {
632                 addr += pci_vga_hose->mem_space->start;
633         }
634 #endif
635
636         if (!marvel_is_ioaddr(addr)) return 0UL;
637
638         /*
639          * Find the hose.
640          */
641         for (hose = hose_head; hose; hose = hose->next) {
642                 if ((addr >> 32) == (hose->mem_space->start >> 32))
643                         break; 
644         }
645         if (!hose)
646                 return 0UL;
647
648         /*
649          * We have the hose - calculate the bus limits.
650          */
651         baddr = addr - hose->mem_space->start;
652         last = baddr + size - 1;
653
654         /*
655          * Is it direct-mapped?
656          */
657         if ((baddr >= __direct_map_base) && 
658             ((baddr + size - 1) < __direct_map_base + __direct_map_size)) 
659                 return IDENT_ADDR | (baddr - __direct_map_base);
660
661         /* 
662          * Check the scatter-gather arena.
663          */
664         if (hose->sg_pci &&
665             baddr >= (unsigned long)hose->sg_pci->dma_base &&
666             last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size){
667
668                 /*
669                  * Adjust the limits (mappings must be page aligned)
670                  */
671                 baddr -= hose->sg_pci->dma_base;
672                 last -= hose->sg_pci->dma_base;
673                 baddr &= PAGE_MASK;
674                 size = PAGE_ALIGN(last) - baddr;
675
676                 /*
677                  * Map it.
678                  */
679                 area = get_vm_area(size, VM_IOREMAP);
680                 if (!area) return (unsigned long)NULL;
681                 ptes = hose->sg_pci->ptes;
682                 for (vaddr = (unsigned long)area->addr; 
683                     baddr <= last; 
684                     baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
685                         pfn = ptes[baddr >> PAGE_SHIFT];
686                         if (!(pfn & 1)) {
687                                 printk("ioremap failed... pte not valid...\n");
688                                 vfree(area->addr);
689                                 return 0UL;
690                         }
691                         pfn >>= 1;      /* make it a true pfn */
692                         
693                         if (__alpha_remap_area_pages(vaddr,
694                                                      pfn << PAGE_SHIFT, 
695                                                      PAGE_SIZE, 0)) {
696                                 printk("FAILED to map...\n");
697                                 vfree(area->addr);
698                                 return 0UL;
699                         }
700                 }
701
702                 flush_tlb_all();
703
704                 vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
705
706                 return vaddr;
707         }
708
709         /*
710          * Not found - assume legacy ioremap.
711          */
712         return addr;
713 }
714
715 void
716 marvel_iounmap(unsigned long addr)
717 {
718         if (((long)addr >> 41) == -2)
719                 return; /* kseg map, nothing to do */
720         if (addr)
721                 vfree((void *)(PAGE_MASK & addr)); 
722 }
723
724 #ifndef CONFIG_ALPHA_GENERIC
725 EXPORT_SYMBOL(marvel_ioremap);
726 EXPORT_SYMBOL(marvel_iounmap);
727 #endif
728
729 \f
730 /*
731  * RTC Support
732  */
733 struct marvel_rtc_access_info {
734         unsigned long function;
735         unsigned long index;
736         unsigned long data;
737 };
738
739 static void
740 __marvel_access_rtc(void *info)
741 {
742         struct marvel_rtc_access_info *rtc_access = info;
743
744         register unsigned long __r0 __asm__("$0");
745         register unsigned long __r16 __asm__("$16") = rtc_access->function;
746         register unsigned long __r17 __asm__("$17") = rtc_access->index;
747         register unsigned long __r18 __asm__("$18") = rtc_access->data;
748         
749         __asm__ __volatile__(
750                 "call_pal %4 # cserve rtc"
751                 : "=r"(__r16), "=r"(__r17), "=r"(__r18), "=r"(__r0)
752                 : "i"(PAL_cserve), "0"(__r16), "1"(__r17), "2"(__r18)
753                 : "$1", "$22", "$23", "$24", "$25");
754
755         rtc_access->data = __r0;
756 }
757
758 u8
759 __marvel_rtc_io(int write, u8 b, unsigned long addr)
760 {
761         struct marvel_rtc_access_info rtc_access = {0, };
762         static u8 index = 0;
763         u8 ret = 0;
764
765         switch(addr) {
766         case 0x70:                                      /* RTC_PORT(0) */
767                 if (write) index = b;
768                 ret = index;
769                 break;
770
771         case 0x71:                                      /* RTC_PORT(1) */
772                 rtc_access.index = index;
773                 rtc_access.data = BCD_TO_BIN(b);
774                 rtc_access.function = 0x49;             /* GET_TOY */
775                 if (write) rtc_access.function = 0x48;  /* PUT_TOY */
776
777 #ifdef CONFIG_SMP
778                 if (smp_processor_id() != boot_cpuid)
779                         smp_call_function_on_cpu(__marvel_access_rtc,
780                                                  &rtc_access,
781                                                  1,     /* retry */
782                                                  1,     /* wait  */
783                                                  1UL << boot_cpuid);
784                 else
785                         __marvel_access_rtc(&rtc_access);
786 #else
787                 __marvel_access_rtc(&rtc_access);
788 #endif
789                 ret = BIN_TO_BCD(rtc_access.data);
790                 
791                 break;
792
793         default:
794                 printk(KERN_WARNING "Illegal RTC port %lx\n", addr);
795                 break;
796         }
797
798         return ret;
799 }
800
801 \f
802 /*
803  * NUMA Support
804  */
805 /**********
806  * FIXME - for now each cpu is a node by itself 
807  *              -- no real support for striped mode 
808  **********
809  */
810 int
811 marvel_pa_to_nid(unsigned long pa)
812 {
813         int cpuid;
814
815         if ((pa >> 43) & 1)     /* I/O */ 
816                 cpuid = (~(pa >> 35) & 0xff);
817         else                    /* mem */
818                 cpuid = ((pa >> 34) & 0x3) | ((pa >> (37 - 2)) & (0x1f << 2));
819
820         return marvel_cpuid_to_nid(cpuid);
821 }
822
823 int
824 marvel_cpuid_to_nid(int cpuid)
825 {
826         return cpuid;
827 }
828
829 unsigned long
830 marvel_node_mem_start(int nid)
831 {
832         unsigned long pa;
833
834         pa = (nid & 0x3) | ((nid & (0x1f << 2)) << 1);
835         pa <<= 34;
836
837         return pa;
838 }
839
840 unsigned long
841 marvel_node_mem_size(int nid)
842 {
843         return 16UL * 1024 * 1024 * 1024; /* 16GB */
844 }
845
846 \f
847 /* 
848  * AGP GART Support.
849  */
850 #include <linux/agp_backend.h>
851 #include <asm/agp_backend.h>
852 #include <linux/slab.h>
853 #include <linux/delay.h>
854
855 struct marvel_agp_aperture {
856         struct pci_iommu_arena *arena;
857         long pg_start;
858         long pg_count;
859 };
860
861 static int
862 marvel_agp_setup(alpha_agp_info *agp)
863 {
864         struct marvel_agp_aperture *aper;
865
866         if (!alpha_agpgart_size)
867                 return -ENOMEM;
868
869         aper = kmalloc(sizeof(*aper), GFP_KERNEL);
870         if (aper == NULL) return -ENOMEM;
871
872         aper->arena = agp->hose->sg_pci;
873         aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
874         aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
875                                        aper->pg_count - 1);
876
877         if (aper->pg_start < 0) {
878                 printk(KERN_ERR "Failed to reserve AGP memory\n");
879                 kfree(aper);
880                 return -ENOMEM;
881         }
882
883         agp->aperture.bus_base = 
884                 aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
885         agp->aperture.size = aper->pg_count * PAGE_SIZE;
886         agp->aperture.sysdata = aper;
887
888         return 0;
889 }
890
891 static void
892 marvel_agp_cleanup(alpha_agp_info *agp)
893 {
894         struct marvel_agp_aperture *aper = agp->aperture.sysdata;
895         int status;
896
897         status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
898         if (status == -EBUSY) {
899                 printk(KERN_WARNING
900                        "Attempted to release bound AGP memory - unbinding\n");
901                 iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
902                 status = iommu_release(aper->arena, aper->pg_start, 
903                                        aper->pg_count);
904         }
905         if (status < 0)
906                 printk(KERN_ERR "Failed to release AGP memory\n");
907
908         kfree(aper);
909         kfree(agp);
910 }
911
912 static int
913 marvel_agp_configure(alpha_agp_info *agp)
914 {
915         io7_ioport_csrs *csrs = ((struct io7_port *)agp->hose->sysdata)->csrs;
916         struct io7 *io7 = ((struct io7_port *)agp->hose->sysdata)->io7;
917         unsigned int new_rate = 0;
918         unsigned long agp_pll;
919
920         /*
921          * Check the requested mode against the PLL setting.
922          * The agpgart_be code has not programmed the card yet,
923          * so we can still tweak mode here.
924          */
925         agp_pll = io7->csrs->POx_RST[IO7_AGP_PORT].csr;
926         switch(IO7_PLL_RNGB(agp_pll)) {
927         case 0x4:                               /* 2x only */
928                 /* 
929                  * The PLL is only programmed for 2x, so adjust the
930                  * rate to 2x, if necessary.
931                  */
932                 if (agp->mode.bits.rate != 2) 
933                         new_rate = 2;
934                 break;
935
936         case 0x6:                               /* 1x / 4x */
937                 /*
938                  * The PLL is programmed for 1x or 4x.  Don't go faster
939                  * than requested, so if the requested rate is 2x, use 1x.
940                  */
941                 if (agp->mode.bits.rate == 2) 
942                         new_rate = 1;
943                 break;
944
945         default:                                /* ??????? */
946                 /*
947                  * Don't know what this PLL setting is, take the requested
948                  * rate, but warn the user.
949                  */
950                 printk("%s: unknown PLL setting RNGB=%lx (PLL6_CTL=%016lx)\n",
951                        __FUNCTION__, IO7_PLL_RNGB(agp_pll), agp_pll);
952                 break;
953         }
954
955         /*
956          * Set the new rate, if necessary.
957          */
958         if (new_rate) {
959                 printk("Requested AGP Rate %dX not compatible "
960                        "with PLL setting - using %dX\n",
961                        agp->mode.bits.rate,
962                        new_rate);
963
964                 agp->mode.bits.rate = new_rate;
965         }
966                 
967         printk("Enabling AGP on hose %d: %dX%s RQ %d\n", 
968                agp->hose->index, agp->mode.bits.rate, 
969                agp->mode.bits.sba ? " - SBA" : "", agp->mode.bits.rq);
970
971         csrs->AGP_CMD.csr = agp->mode.lw;
972
973         return 0;
974 }
975
976 static int 
977 marvel_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
978 {
979         struct marvel_agp_aperture *aper = agp->aperture.sysdata;
980         return iommu_bind(aper->arena, aper->pg_start + pg_start, 
981                           mem->page_count, mem->memory);
982 }
983
984 static int 
985 marvel_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
986 {
987         struct marvel_agp_aperture *aper = agp->aperture.sysdata;
988         return iommu_unbind(aper->arena, aper->pg_start + pg_start,
989                             mem->page_count);
990 }
991
992 static unsigned long
993 marvel_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
994 {
995         struct marvel_agp_aperture *aper = agp->aperture.sysdata;
996         unsigned long baddr = addr - aper->arena->dma_base;
997         unsigned long pte;
998
999         if (addr < agp->aperture.bus_base ||
1000             addr >= agp->aperture.bus_base + agp->aperture.size) {
1001                 printk("%s: addr out of range\n", __FUNCTION__);
1002                 return -EINVAL;
1003         }
1004
1005         pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
1006         if (!(pte & 1)) {
1007                 printk("%s: pte not valid\n", __FUNCTION__);
1008                 return -EINVAL;
1009         } 
1010         return (pte >> 1) << PAGE_SHIFT;
1011 }
1012
1013 struct alpha_agp_ops marvel_agp_ops =
1014 {
1015         .setup          = marvel_agp_setup,
1016         .cleanup        = marvel_agp_cleanup,
1017         .configure      = marvel_agp_configure,
1018         .bind           = marvel_agp_bind_memory,
1019         .unbind         = marvel_agp_unbind_memory,
1020         .translate      = marvel_agp_translate
1021 };
1022
1023 alpha_agp_info *
1024 marvel_agp_info(void)
1025 {
1026         struct pci_controller *hose;
1027         io7_ioport_csrs *csrs;
1028         alpha_agp_info *agp;
1029         struct io7 *io7;
1030
1031         /*
1032          * Find the first IO7 with an AGP card.
1033          *
1034          * FIXME -- there should be a better way (we want to be able to
1035          * specify and what if the agp card is not video???)
1036          */
1037         hose = NULL;
1038         for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) {
1039                 struct pci_controller *h;
1040                 vuip addr;
1041
1042                 if (!io7->ports[IO7_AGP_PORT].enabled)
1043                         continue;
1044
1045                 h = io7->ports[IO7_AGP_PORT].hose;
1046                 addr = (vuip)build_conf_addr(h, 0, PCI_DEVFN(5, 0), 0);
1047
1048                 if (*addr != 0xffffffffu) {
1049                         hose = h;
1050                         break;
1051                 }
1052         }
1053
1054         if (!hose || !hose->sg_pci)
1055                 return NULL;
1056
1057         printk("MARVEL - using hose %d as AGP\n", hose->index);
1058
1059         /* 
1060          * Get the csrs from the hose.
1061          */
1062         csrs = ((struct io7_port *)hose->sysdata)->csrs;
1063
1064         /*
1065          * Allocate the info structure.
1066          */
1067         agp = kmalloc(sizeof(*agp), GFP_KERNEL);
1068
1069         /*
1070          * Fill it in.
1071          */
1072         agp->hose = hose;
1073         agp->private = NULL;
1074         agp->ops = &marvel_agp_ops;
1075
1076         /*
1077          * Aperture - not configured until ops.setup().
1078          */
1079         agp->aperture.bus_base = 0;
1080         agp->aperture.size = 0;
1081         agp->aperture.sysdata = NULL;
1082
1083         /*
1084          * Capabilities.
1085          *
1086          * NOTE: IO7 reports through AGP_STAT that it can support a read queue
1087          *       depth of 17 (rq = 0x10). It actually only supports a depth of
1088          *       16 (rq = 0xf).
1089          */
1090         agp->capability.lw = csrs->AGP_STAT.csr;
1091         agp->capability.bits.rq = 0xf;
1092         
1093         /*
1094          * Mode.
1095          */
1096         agp->mode.lw = csrs->AGP_CMD.csr;
1097
1098         return agp;
1099 }