2 * linux/arch/alpha/kernel/core_titan.c
4 * Code common to all TITAN core logic chips.
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/pci.h>
11 #include <linux/sched.h>
12 #include <linux/init.h>
13 #include <linux/vmalloc.h>
15 #include <asm/hwrpb.h>
16 #include <asm/ptrace.h>
17 #include <asm/system.h>
19 #include <asm/pgalloc.h>
21 #define __EXTERN_INLINE inline
23 #include <asm/core_titan.h>
24 #undef __EXTERN_INLINE
26 #include <linux/bootmem.h>
31 /* Save Titan configuration data as the console had it set up. */
35 unsigned long wsba[4];
38 } saved_config[4] __attribute__((common));
41 * BIOS32-style PCI interface:
44 #define DEBUG_MCHECK 0 /* 0 = minimum, 1 = debug, 2 = dump+dump */
45 #define DEBUG_CONFIG 0
48 # define DBG_CFG(args) printk args
50 # define DBG_CFG(args)
55 * Routines to access TIG registers.
57 static inline volatile unsigned long *
58 mk_tig_addr(int offset)
60 return (volatile unsigned long *)(TITAN_TIG_SPACE + (offset << 6));
64 titan_read_tig(int offset, u8 value)
66 volatile unsigned long *tig_addr = mk_tig_addr(offset);
67 return (u8)(*tig_addr & 0xff);
71 titan_write_tig(int offset, u8 value)
73 volatile unsigned long *tig_addr = mk_tig_addr(offset);
74 *tig_addr = (unsigned long)value;
79 * Given a bus, device, and function number, compute resulting
80 * configuration space address
81 * accordingly. It is therefore not safe to have concurrent
82 * invocations to configuration space access routines, but there
83 * really shouldn't be any need for this.
85 * Note that all config space accesses use Type 1 address format.
87 * Note also that type 1 is determined by non-zero bus number.
91 * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
92 * 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
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 * 23:16 bus number (8 bits = 128 possible buses)
99 * 15:11 Device number (5 bits)
100 * 10:8 function number
101 * 7:2 register number
104 * The function number selects which function of a multi-function device
105 * (e.g., SCSI and Ethernet).
107 * The register selects a DWORD (32 bit) register offset. Hence it
108 * doesn't get shifted by 2 bits as we want to "drop" the bottom two
113 mk_conf_addr(struct pci_dev *dev, int where, unsigned long *pci_addr,
114 unsigned char *type1)
116 struct pci_controller *hose = dev->sysdata;
118 u8 bus = dev->bus->number;
119 u8 device_fn = dev->devfn;
121 DBG_CFG(("mk_conf_addr(bus=%d ,device_fn=0x%x, where=0x%x, "
122 "pci_addr=0x%p, type1=0x%p)\n",
123 bus, device_fn, where, pci_addr, type1));
125 if (hose->first_busno == bus)
129 addr = (bus << 16) | (device_fn << 8) | where;
130 addr |= hose->config_space_base;
133 DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
138 titan_read_config_byte(struct pci_dev *dev, int where, u8 *value)
143 if (mk_conf_addr(dev, where, &addr, &type1))
144 return PCIBIOS_DEVICE_NOT_FOUND;
146 *value = __kernel_ldbu(*(vucp)addr);
147 return PCIBIOS_SUCCESSFUL;
151 titan_read_config_word(struct pci_dev *dev, int where, u16 *value)
156 if (mk_conf_addr(dev, where, &addr, &type1))
157 return PCIBIOS_DEVICE_NOT_FOUND;
159 *value = __kernel_ldwu(*(vusp)addr);
160 return PCIBIOS_SUCCESSFUL;
164 titan_read_config_dword(struct pci_dev *dev, int where, u32 *value)
169 if (mk_conf_addr(dev, where, &addr, &type1))
170 return PCIBIOS_DEVICE_NOT_FOUND;
172 *value = *(vuip)addr;
173 return PCIBIOS_SUCCESSFUL;
177 titan_write_config_byte(struct pci_dev *dev, int where, u8 value)
182 if (mk_conf_addr(dev, where, &addr, &type1))
183 return PCIBIOS_DEVICE_NOT_FOUND;
185 __kernel_stb(value, *(vucp)addr);
187 __kernel_ldbu(*(vucp)addr);
188 return PCIBIOS_SUCCESSFUL;
192 titan_write_config_word(struct pci_dev *dev, int where, u16 value)
197 if (mk_conf_addr(dev, where, &addr, &type1))
198 return PCIBIOS_DEVICE_NOT_FOUND;
200 __kernel_stw(value, *(vusp)addr);
202 __kernel_ldwu(*(vusp)addr);
203 return PCIBIOS_SUCCESSFUL;
207 titan_write_config_dword(struct pci_dev *dev, int where, u32 value)
212 if (mk_conf_addr(dev, where, &addr, &type1))
213 return PCIBIOS_DEVICE_NOT_FOUND;
218 return PCIBIOS_SUCCESSFUL;
221 struct pci_ops titan_pci_ops =
223 read_byte: titan_read_config_byte,
224 read_word: titan_read_config_word,
225 read_dword: titan_read_config_dword,
226 write_byte: titan_write_config_byte,
227 write_word: titan_write_config_word,
228 write_dword: titan_write_config_dword
233 titan_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
235 titan_pachip *pachip =
236 (hose->index & 1) ? TITAN_pachip1 : TITAN_pachip0;
237 titan_pachip_port *port;
238 volatile unsigned long *csr;
241 /* Get the right hose. */
242 port = &pachip->g_port;
244 port = &pachip->a_port;
246 /* We can invalidate up to 8 tlb entries in a go. The flush
247 matches against <31:16> in the pci address.
248 Note that gtlbi* and atlbi* are in the same place in the g_port
249 and a_port, respectively, so the g_port offset can be used
250 even if hose is an a_port */
251 csr = &port->port_specific.g.gtlbia.csr;
252 if (((start ^ end) & 0xffff0000) == 0)
253 csr = &port->port_specific.g.gtlbiv.csr;
255 /* For TBIA, it doesn't matter what value we write. For TBI,
256 it's the shifted tag bits. */
257 value = (start & 0xffff0000) >> 12;
266 titan_query_agp(titan_pachip_port *port)
268 union TPAchipPCTL pctl;
271 pctl.pctl_q_whole = port->pctl.csr;
273 return pctl.pctl_r_bits.apctl_v_agp_present;
278 titan_init_one_pachip_port(titan_pachip_port *port, int index)
280 struct pci_controller *hose;
282 hose = alloc_pci_controller();
285 hose->io_space = alloc_resource();
286 hose->mem_space = alloc_resource();
289 * This is for userland consumption. The 40-bit PIO bias that we
290 * use in the kernel through KSEG doesn't work in the page table
291 * based user mappings. (43-bit KSEG sign extends the physical
292 * address from bit 40 to hit the I/O bit - mapped addresses don't).
293 * So make sure we get the 43-bit PIO bias.
295 hose->sparse_mem_base = 0;
296 hose->sparse_io_base = 0;
298 = (TITAN_MEM(index) & 0xffffffffff) | 0x80000000000;
300 = (TITAN_IO(index) & 0xffffffffff) | 0x80000000000;
302 hose->config_space_base = TITAN_CONF(index);
305 hose->io_space->start = TITAN_IO(index) - TITAN_IO_BIAS;
306 hose->io_space->end = hose->io_space->start + TITAN_IO_SPACE - 1;
307 hose->io_space->name = pci_io_names[index];
308 hose->io_space->flags = IORESOURCE_IO;
310 hose->mem_space->start = TITAN_MEM(index) - TITAN_MEM_BIAS;
311 hose->mem_space->end = hose->mem_space->start + 0xffffffff;
312 hose->mem_space->name = pci_mem_names[index];
313 hose->mem_space->flags = IORESOURCE_MEM;
315 if (request_resource(&ioport_resource, hose->io_space) < 0)
316 printk(KERN_ERR "Failed to request IO on hose %d\n", index);
317 if (request_resource(&iomem_resource, hose->mem_space) < 0)
318 printk(KERN_ERR "Failed to request MEM on hose %d\n", index);
321 * Save the existing PCI window translations. SRM will
322 * need them when we go to reboot.
324 saved_config[index].wsba[0] = port->wsba[0].csr;
325 saved_config[index].wsm[0] = port->wsm[0].csr;
326 saved_config[index].tba[0] = port->tba[0].csr;
328 saved_config[index].wsba[1] = port->wsba[1].csr;
329 saved_config[index].wsm[1] = port->wsm[1].csr;
330 saved_config[index].tba[1] = port->tba[1].csr;
332 saved_config[index].wsba[2] = port->wsba[2].csr;
333 saved_config[index].wsm[2] = port->wsm[2].csr;
334 saved_config[index].tba[2] = port->tba[2].csr;
336 saved_config[index].wsba[3] = port->wsba[3].csr;
337 saved_config[index].wsm[3] = port->wsm[3].csr;
338 saved_config[index].tba[3] = port->tba[3].csr;
341 * Set up the PCI to main memory translation windows.
343 * Note: Window 3 on Titan is Scatter-Gather ONLY.
345 * Window 0 is scatter-gather 8MB at 8MB (for isa)
346 * Window 1 is direct access 1GB at 2GB
347 * Window 2 is scatter-gather 1GB at 3GB
349 hose->sg_isa = iommu_arena_new(hose, 0x00800000, 0x00800000, 0);
350 hose->sg_isa->align_entry = 8; /* 64KB for ISA */
352 hose->sg_pci = iommu_arena_new(hose, 0xc0000000, 0x40000000, 0);
353 hose->sg_pci->align_entry = 4; /* Titan caches 4 PTEs at a time */
355 port->wsba[0].csr = hose->sg_isa->dma_base | 3;
356 port->wsm[0].csr = (hose->sg_isa->size - 1) & 0xfff00000;
357 port->tba[0].csr = virt_to_phys(hose->sg_isa->ptes);
359 port->wsba[1].csr = __direct_map_base | 1;
360 port->wsm[1].csr = (__direct_map_size - 1) & 0xfff00000;
361 port->tba[1].csr = 0;
363 port->wsba[2].csr = hose->sg_pci->dma_base | 3;
364 port->wsm[2].csr = (hose->sg_pci->size - 1) & 0xfff00000;
365 port->tba[2].csr = virt_to_phys(hose->sg_pci->ptes);
367 port->wsba[3].csr = 0;
369 /* Enable the Monster Window to make DAC pci64 possible. */
370 port->pctl.csr |= pctl_m_mwin;
373 * If it's an AGP port, initialize agplastwr.
375 if (titan_query_agp(port))
376 port->port_specific.a.agplastwr.csr = __direct_map_base;
378 titan_pci_tbi(hose, 0, -1);
382 titan_init_pachips(titan_pachip *pachip0, titan_pachip *pachip1)
384 int pchip1_present = TITAN_cchip->csc.csr & 1L<<14;
386 /* Init the ports in hose order... */
387 titan_init_one_pachip_port(&pachip0->g_port, 0); /* hose 0 */
389 titan_init_one_pachip_port(&pachip1->g_port, 1);/* hose 1 */
390 titan_init_one_pachip_port(&pachip0->a_port, 2); /* hose 2 */
392 titan_init_one_pachip_port(&pachip1->a_port, 3);/* hose 3 */
396 titan_init_vga_hose(void)
398 #ifdef CONFIG_VGA_HOSE
399 u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
401 if (pu64[7] == 3) { /* TERM_TYPE == graphics */
402 struct pci_controller *hose;
403 int h = (pu64[30] >> 24) & 0xff; /* console hose # */
406 * Our hose numbering matches the console's, so just find
409 for (hose = hose_head; hose; hose = hose->next) {
410 if (hose->index == h) break;
414 printk("Console graphics on hose %d\n", hose->index);
418 #endif /* CONFIG_VGA_HOSE */
422 titan_init_arch(void)
425 printk("%s: titan_init_arch()\n", __FUNCTION__);
426 printk("%s: CChip registers:\n", __FUNCTION__);
427 printk("%s: CSR_CSC 0x%lx\n", __FUNCTION__, TITAN_cchip->csc.csr);
428 printk("%s: CSR_MTR 0x%lx\n", __FUNCTION__, TITAN_cchip->mtr.csr);
429 printk("%s: CSR_MISC 0x%lx\n", __FUNCTION__, TITAN_cchip->misc.csr);
430 printk("%s: CSR_DIM0 0x%lx\n", __FUNCTION__, TITAN_cchip->dim0.csr);
431 printk("%s: CSR_DIM1 0x%lx\n", __FUNCTION__, TITAN_cchip->dim1.csr);
432 printk("%s: CSR_DIR0 0x%lx\n", __FUNCTION__, TITAN_cchip->dir0.csr);
433 printk("%s: CSR_DIR1 0x%lx\n", __FUNCTION__, TITAN_cchip->dir1.csr);
434 printk("%s: CSR_DRIR 0x%lx\n", __FUNCTION__, TITAN_cchip->drir.csr);
436 printk("%s: DChip registers:\n", __FUNCTION__);
437 printk("%s: CSR_DSC 0x%lx\n", __FUNCTION__, TITAN_dchip->dsc.csr);
438 printk("%s: CSR_STR 0x%lx\n", __FUNCTION__, TITAN_dchip->str.csr);
439 printk("%s: CSR_DREV 0x%lx\n", __FUNCTION__, TITAN_dchip->drev.csr);
442 boot_cpuid = __hard_smp_processor_id();
444 /* With multiple PCI busses, we play with I/O as physical addrs. */
445 ioport_resource.end = ~0UL;
446 iomem_resource.end = ~0UL;
448 /* PCI DMA Direct Mapping is 1GB at 2GB. */
449 __direct_map_base = 0x80000000;
450 __direct_map_size = 0x40000000;
452 /* Init the PA chip(s). */
453 titan_init_pachips(TITAN_pachip0, TITAN_pachip1);
455 /* Check for graphic console location (if any). */
456 titan_init_vga_hose();
460 titan_kill_one_pachip_port(titan_pachip_port *port, int index)
462 port->wsba[0].csr = saved_config[index].wsba[0];
463 port->wsm[0].csr = saved_config[index].wsm[0];
464 port->tba[0].csr = saved_config[index].tba[0];
466 port->wsba[1].csr = saved_config[index].wsba[1];
467 port->wsm[1].csr = saved_config[index].wsm[1];
468 port->tba[1].csr = saved_config[index].tba[1];
470 port->wsba[2].csr = saved_config[index].wsba[2];
471 port->wsm[2].csr = saved_config[index].wsm[2];
472 port->tba[2].csr = saved_config[index].tba[2];
474 port->wsba[3].csr = saved_config[index].wsba[3];
475 port->wsm[3].csr = saved_config[index].wsm[3];
476 port->tba[3].csr = saved_config[index].tba[3];
480 titan_kill_pachips(titan_pachip *pachip0, titan_pachip *pachip1)
482 int pchip1_present = TITAN_cchip->csc.csr & 1L<<14;
484 if (pchip1_present) {
485 titan_kill_one_pachip_port(&pachip1->g_port, 1);
486 titan_kill_one_pachip_port(&pachip1->a_port, 3);
488 titan_kill_one_pachip_port(&pachip0->g_port, 0);
489 titan_kill_one_pachip_port(&pachip0->a_port, 2);
493 titan_kill_arch(int mode)
495 titan_kill_pachips(TITAN_pachip0, TITAN_pachip1);
503 titan_ioremap(unsigned long addr, unsigned long size)
505 int h = (addr & TITAN_HOSE_MASK) >> TITAN_HOSE_SHIFT;
506 unsigned long baddr = addr & ~TITAN_HOSE_MASK;
507 unsigned long last = baddr + size - 1;
508 struct pci_controller *hose;
509 struct vm_struct *area;
517 #ifdef CONFIG_VGA_HOSE
518 if (pci_vga_hose && __titan_is_mem_vga(addr)) {
519 h = pci_vga_hose->index;
520 addr += pci_vga_hose->mem_space->start;
527 for (hose = hose_head; hose; hose = hose->next)
528 if (hose->index == h) break;
529 if (!hose) return (unsigned long)NULL;
532 * Is it direct-mapped?
534 if ((baddr >= __direct_map_base) &&
535 ((baddr + size - 1) < __direct_map_base + __direct_map_size))
536 return addr - __direct_map_base + TITAN_MEM_BIAS;
539 * Check the scatter-gather arena.
542 baddr >= (unsigned long)hose->sg_pci->dma_base &&
543 last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size){
546 * Adjust the limits (mappings must be page aligned)
548 baddr -= hose->sg_pci->dma_base;
549 last -= hose->sg_pci->dma_base;
551 size = PAGE_ALIGN(last) - baddr;
556 area = get_vm_area(size, VM_IOREMAP);
557 if (!area) return (unsigned long)NULL;
558 ptes = hose->sg_pci->ptes;
559 for (vaddr = (unsigned long)area->addr;
561 baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
562 pfn = ptes[baddr >> PAGE_SHIFT];
564 printk("ioremap failed... pte not valid...\n");
566 return (unsigned long)NULL;
568 pfn >>= 1; /* make it a true pfn */
570 if (__alpha_remap_area_pages(VMALLOC_VMADDR(vaddr),
573 printk("FAILED to map...\n");
575 return (unsigned long)NULL;
581 vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
586 * Not found - assume legacy ioremap.
588 return addr + TITAN_MEM_BIAS;
593 titan_iounmap(unsigned long addr)
595 if (((long)addr >> 41) == -2)
596 return; /* kseg map, nothing to do */
598 vfree((void *)(PAGE_MASK & addr));
605 #include <linux/agp_backend.h>
606 #include <asm/agp_backend.h>
607 #include <linux/slab.h>
608 #include <linux/delay.h>
610 struct titan_agp_aperture {
611 struct pci_iommu_arena *arena;
617 titan_agp_setup(alpha_agp_info *agp)
619 struct titan_agp_aperture *aper;
621 if (!alpha_agpgart_size)
624 aper = kmalloc(sizeof(struct titan_agp_aperture), GFP_KERNEL);
628 aper->arena = agp->hose->sg_pci;
629 aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
630 aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
632 if (aper->pg_start < 0) {
633 printk(KERN_ERR "Failed to reserve AGP memory\n");
638 agp->aperture.bus_base =
639 aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
640 agp->aperture.size = aper->pg_count * PAGE_SIZE;
641 agp->aperture.sysdata = aper;
647 titan_agp_cleanup(alpha_agp_info *agp)
649 struct titan_agp_aperture *aper = agp->aperture.sysdata;
652 status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
653 if (status == -EBUSY) {
655 "Attempted to release bound AGP memory - unbinding\n");
656 iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
657 status = iommu_release(aper->arena, aper->pg_start,
661 printk(KERN_ERR "Failed to release AGP memory\n");
668 titan_agp_configure(alpha_agp_info *agp)
670 union TPAchipPCTL pctl;
671 titan_pachip_port *port = agp->private;
672 pctl.pctl_q_whole = port->pctl.csr;
674 /* Side-Band Addressing? */
675 pctl.pctl_r_bits.apctl_v_agp_sba_en = agp->mode.bits.sba;
678 pctl.pctl_r_bits.apctl_v_agp_rate = 0; /* 1x */
679 if (agp->mode.bits.rate & 2)
680 pctl.pctl_r_bits.apctl_v_agp_rate = 1; /* 2x */
682 if (agp->mode.bits.rate & 4)
683 pctl.pctl_r_bits.apctl_v_agp_rate = 2; /* 4x */
687 pctl.pctl_r_bits.apctl_v_agp_hp_rd = 2;
688 pctl.pctl_r_bits.apctl_v_agp_lp_rd = 7;
693 pctl.pctl_r_bits.apctl_v_agp_en = agp->mode.bits.enable;
696 printk("Enabling AGP: %dX%s\n",
697 1 << pctl.pctl_r_bits.apctl_v_agp_rate,
698 pctl.pctl_r_bits.apctl_v_agp_sba_en ? " - SBA" : "");
701 port->pctl.csr = pctl.pctl_q_whole;
703 /* And wait at least 5000 66MHz cycles (per Titan spec). */
710 titan_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, agp_memory *mem)
712 struct titan_agp_aperture *aper = agp->aperture.sysdata;
713 return iommu_bind(aper->arena, aper->pg_start + pg_start,
714 mem->page_count, mem->memory);
718 titan_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, agp_memory *mem)
720 struct titan_agp_aperture *aper = agp->aperture.sysdata;
721 return iommu_unbind(aper->arena, aper->pg_start + pg_start,
726 titan_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
728 struct titan_agp_aperture *aper = agp->aperture.sysdata;
729 unsigned long baddr = addr - aper->arena->dma_base;
732 if (addr < agp->aperture.bus_base ||
733 addr >= agp->aperture.bus_base + agp->aperture.size) {
734 printk("%s: addr out of range\n", __FUNCTION__);
738 pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
740 printk("%s: pte not valid\n", __FUNCTION__);
744 return (pte >> 1) << PAGE_SHIFT;
747 struct alpha_agp_ops titan_agp_ops =
749 setup: titan_agp_setup,
750 cleanup: titan_agp_cleanup,
751 configure: titan_agp_configure,
752 bind: titan_agp_bind_memory,
753 unbind: titan_agp_unbind_memory,
754 translate: titan_agp_translate
761 struct pci_controller *hose;
762 titan_pachip_port *port;
764 union TPAchipPCTL pctl;
769 port = &TITAN_pachip0->a_port;
770 if (titan_query_agp(port))
773 titan_query_agp(port = &TITAN_pachip1->a_port))
777 * Find the hose the port is on.
779 for (hose = hose_head; hose; hose = hose->next)
780 if (hose->index == hosenum)
783 if (!hose || !hose->sg_pci)
787 * Allocate the info structure.
789 agp = kmalloc(sizeof(*agp), GFP_KERNEL);
794 agp->type = 0 /* FIXME: ALPHA_CORE_AGP */;
797 agp->ops = &titan_agp_ops;
800 * Aperture - not configured until ops.setup().
802 * FIXME - should we go ahead and allocate it here?
804 agp->aperture.bus_base = 0;
805 agp->aperture.size = 0;
806 agp->aperture.sysdata = NULL;
811 agp->capability.lw = 0;
812 agp->capability.bits.rate = 3; /* 2x, 1x */
813 agp->capability.bits.sba = 1;
814 agp->capability.bits.rq = 7; /* 8 - 1 */
819 pctl.pctl_q_whole = port->pctl.csr;
821 agp->mode.bits.rate = 1 << pctl.pctl_r_bits.apctl_v_agp_rate;
822 agp->mode.bits.sba = pctl.pctl_r_bits.apctl_v_agp_sba_en;
823 agp->mode.bits.rq = 7; /* RQ Depth? */
824 agp->mode.bits.enable = pctl.pctl_r_bits.apctl_v_agp_en;