2 * fuloong2e specific PCI support.
4 * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
6 * Authors: Carsten Langgaard <carstenl@mips.com>
7 * Maciej W. Rozycki <macro@mips.com>
9 * Copyright (C) 2009 Lemote Inc.
10 * Author: Wu Zhangjin <wuzj@lemote.com>
12 * This program is free software; you can distribute it and/or modify it
13 * under the terms of the GNU General Public License (Version 2) as
14 * published by the Free Software Foundation.
16 #include <linux/types.h>
17 #include <linux/pci.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
24 #include <cs5536/cs5536_pci.h>
25 #include <cs5536/cs5536.h>
28 #define PCI_ACCESS_READ 0
29 #define PCI_ACCESS_WRITE 1
31 #define CFG_SPACE_REG(offset) \
32 (void *)CKSEG1ADDR(LOONGSON_PCICFG_BASE | (offset))
33 #define ID_SEL_BEGIN 11
34 #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
37 static int loongson_pcibios_config_access(unsigned char access_type
,
39 unsigned int devfn
, int where
,
42 u32 busnum
= bus
->number
;
46 int device
= PCI_SLOT(devfn
);
47 int function
= PCI_FUNC(devfn
);
51 /* board-specific part,currently,only fuloong2f,yeeloong2f
52 * use CS5536, fuloong2e use via686b, gdium has no
56 /* cs5536_pci_conf_read4/write4() will call _rdmsr/_wrmsr() to
57 * access the regsters PCI_MSR_ADDR, PCI_MSR_DATA_LO,
58 * PCI_MSR_DATA_HI, which is bigger than PCI_MSR_CTRL, so, it
59 * will not go this branch, but the others. so, no calling dead
62 if ((PCI_IDSEL_CS5536
== device
) && (reg
< PCI_MSR_CTRL
)) {
63 switch (access_type
) {
65 *data
= cs5536_pci_conf_read4(function
, reg
);
67 case PCI_ACCESS_WRITE
:
68 cs5536_pci_conf_write4(function
, reg
, *data
);
74 /* Type 0 configuration for onboard PCI bus */
75 if (device
> MAX_DEV_NUM
)
78 addr
= (1 << (device
+ ID_SEL_BEGIN
)) | (function
<< 8) | reg
;
81 /* Type 1 configuration for offboard PCI bus */
82 addr
= (busnum
<< 16) | (device
<< 11) | (function
<< 8) | reg
;
87 LOONGSON_PCICMD
|= LOONGSON_PCICMD_MABORT_CLR
| \
88 LOONGSON_PCICMD_MTABORT_CLR
;
90 LOONGSON_PCIMAP_CFG
= (addr
>> 16) | type
;
92 /* Flush Bonito register block */
93 dummy
= LOONGSON_PCIMAP_CFG
;
96 addrp
= CFG_SPACE_REG(addr
& 0xffff);
97 if (access_type
== PCI_ACCESS_WRITE
)
98 writel(cpu_to_le32(*data
), addrp
);
100 *data
= le32_to_cpu(readl(addrp
));
102 /* Detect Master/Target abort */
103 if (LOONGSON_PCICMD
& (LOONGSON_PCICMD_MABORT_CLR
|
104 LOONGSON_PCICMD_MTABORT_CLR
)) {
108 LOONGSON_PCICMD
|= (LOONGSON_PCICMD_MABORT_CLR
|
109 LOONGSON_PCICMD_MTABORT_CLR
);
120 * We can't address 8 and 16 bit words directly. Instead we have to
121 * read/write a 32bit word and mask/modify the data we actually want.
123 static int loongson_pcibios_read(struct pci_bus
*bus
, unsigned int devfn
,
124 int where
, int size
, u32
*val
)
128 if ((size
== 2) && (where
& 1))
129 return PCIBIOS_BAD_REGISTER_NUMBER
;
130 else if ((size
== 4) && (where
& 3))
131 return PCIBIOS_BAD_REGISTER_NUMBER
;
133 if (loongson_pcibios_config_access(PCI_ACCESS_READ
, bus
, devfn
, where
,
138 *val
= (data
>> ((where
& 3) << 3)) & 0xff;
140 *val
= (data
>> ((where
& 3) << 3)) & 0xffff;
144 return PCIBIOS_SUCCESSFUL
;
147 static int loongson_pcibios_write(struct pci_bus
*bus
, unsigned int devfn
,
148 int where
, int size
, u32 val
)
152 if ((size
== 2) && (where
& 1))
153 return PCIBIOS_BAD_REGISTER_NUMBER
;
154 else if ((size
== 4) && (where
& 3))
155 return PCIBIOS_BAD_REGISTER_NUMBER
;
160 if (loongson_pcibios_config_access(PCI_ACCESS_READ
, bus
, devfn
,
165 data
= (data
& ~(0xff << ((where
& 3) << 3))) |
166 (val
<< ((where
& 3) << 3));
168 data
= (data
& ~(0xffff << ((where
& 3) << 3))) |
169 (val
<< ((where
& 3) << 3));
172 if (loongson_pcibios_config_access(PCI_ACCESS_WRITE
, bus
, devfn
, where
,
176 return PCIBIOS_SUCCESSFUL
;
179 struct pci_ops loongson_pci_ops
= {
180 .read
= loongson_pcibios_read
,
181 .write
= loongson_pcibios_write
185 void _rdmsr(u32 msr
, u32
*hi
, u32
*lo
)
187 struct pci_bus bus
= {
188 .number
= PCI_BUS_CS5536
190 u32 devfn
= PCI_DEVFN(PCI_IDSEL_CS5536
, 0);
191 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_ADDR
, 4, msr
);
192 loongson_pcibios_read(&bus
, devfn
, PCI_MSR_DATA_LO
, 4, lo
);
193 loongson_pcibios_read(&bus
, devfn
, PCI_MSR_DATA_HI
, 4, hi
);
195 EXPORT_SYMBOL(_rdmsr
);
197 void _wrmsr(u32 msr
, u32 hi
, u32 lo
)
199 struct pci_bus bus
= {
200 .number
= PCI_BUS_CS5536
202 u32 devfn
= PCI_DEVFN(PCI_IDSEL_CS5536
, 0);
203 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_ADDR
, 4, msr
);
204 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_DATA_LO
, 4, lo
);
205 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_DATA_HI
, 4, hi
);
207 EXPORT_SYMBOL(_wrmsr
);