1/*
2 * Copyright (C) 2007 Lemote, Inc. & Institute of Computing Technology
3 * Author: Fuxin Zhang, zhangfx@lemote.com
4 *
5 *  This program is free software; you can redistribute	 it and/or modify it
6 *  under  the terms of	 the GNU General  Public License as published by the
7 *  Free Software Foundation;  either version 2 of the	License, or (at your
8 *  option) any later version.
9 */
10#include <linux/pci.h>
11
12#include <pci.h>
13#include <loongson.h>
14#include <boot_param.h>
15
16static struct resource loongson_pci_mem_resource = {
17	.name	= "pci memory space",
18	.start	= LOONGSON_PCI_MEM_START,
19	.end	= LOONGSON_PCI_MEM_END,
20	.flags	= IORESOURCE_MEM,
21};
22
23static struct resource loongson_pci_io_resource = {
24	.name	= "pci io space",
25	.start	= LOONGSON_PCI_IO_START,
26	.end	= IO_SPACE_LIMIT,
27	.flags	= IORESOURCE_IO,
28};
29
30static struct pci_controller  loongson_pci_controller = {
31	.pci_ops	= &loongson_pci_ops,
32	.io_resource	= &loongson_pci_io_resource,
33	.mem_resource	= &loongson_pci_mem_resource,
34	.mem_offset	= 0x00000000UL,
35	.io_offset	= 0x00000000UL,
36};
37
38static void __init setup_pcimap(void)
39{
40	/*
41	 * local to PCI mapping for CPU accessing PCI space
42	 * CPU address space [256M,448M] is window for accessing pci space
43	 * we set pcimap_lo[0,1,2] to map it to pci space[0M,64M], [320M,448M]
44	 *
45	 * pcimap: PCI_MAP2  PCI_Mem_Lo2 PCI_Mem_Lo1 PCI_Mem_Lo0
46	 *	     [<2G]   [384M,448M] [320M,384M] [0M,64M]
47	 */
48	LOONGSON_PCIMAP = LOONGSON_PCIMAP_PCIMAP_2 |
49		LOONGSON_PCIMAP_WIN(2, LOONGSON_PCILO2_BASE) |
50		LOONGSON_PCIMAP_WIN(1, LOONGSON_PCILO1_BASE) |
51		LOONGSON_PCIMAP_WIN(0, 0);
52
53	/*
54	 * PCI-DMA to local mapping: [2G,2G+256M] -> [0M,256M]
55	 */
56	LOONGSON_PCIBASE0 = 0x80000000ul;   /* base: 2G -> mmap: 0M */
57	/* size: 256M, burst transmission, pre-fetch enable, 64bit */
58	LOONGSON_PCI_HIT0_SEL_L = 0xc000000cul;
59	LOONGSON_PCI_HIT0_SEL_H = 0xfffffffful;
60	LOONGSON_PCI_HIT1_SEL_L = 0x00000006ul; /* set this BAR as invalid */
61	LOONGSON_PCI_HIT1_SEL_H = 0x00000000ul;
62	LOONGSON_PCI_HIT2_SEL_L = 0x00000006ul; /* set this BAR as invalid */
63	LOONGSON_PCI_HIT2_SEL_H = 0x00000000ul;
64
65	/* avoid deadlock of PCI reading/writing lock operation */
66	LOONGSON_PCI_ISR4C = 0xd2000001ul;
67
68	/* can not change gnt to break pci transfer when device's gnt not
69	deassert for some broken device */
70	LOONGSON_PXARB_CFG = 0x00fe0105ul;
71
72#ifdef CONFIG_CPU_SUPPORTS_ADDRWINCFG
73	/*
74	 * set cpu addr window2 to map CPU address space to PCI address space
75	 */
76	LOONGSON_ADDRWIN_CPUTOPCI(ADDRWIN_WIN2, LOONGSON_CPU_MEM_SRC,
77		LOONGSON_PCI_MEM_DST, MMAP_CPUTOPCI_SIZE);
78#endif
79}
80
81extern int sbx00_acpi_init(void);
82
83static int __init pcibios_init(void)
84{
85	setup_pcimap();
86
87	loongson_pci_controller.io_map_base = mips_io_port_base;
88#ifdef CONFIG_LEFI_FIRMWARE_INTERFACE
89	loongson_pci_mem_resource.start = loongson_sysconf.pci_mem_start_addr;
90	loongson_pci_mem_resource.end = loongson_sysconf.pci_mem_end_addr;
91#endif
92	register_pci_controller(&loongson_pci_controller);
93
94#ifdef CONFIG_CPU_LOONGSON3
95	sbx00_acpi_init();
96#endif
97
98	return 0;
99}
100
101arch_initcall(pcibios_init);
102