2 * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
4 * Authors: Carsten Langgaard <carstenl@mips.com>
5 * Maciej W. Rozycki <macro@mips.com>
7 * Copyright (C) 2009 Lemote Inc.
8 * Author: Wu Zhangjin <wuzhangjin@gmail.com>
10 * This program is free software; you can distribute it and/or modify it
11 * under the terms of the GNU General Public License (Version 2) as
12 * published by the Free Software Foundation.
14 #include <linux/types.h>
15 #include <linux/pci.h>
16 #include <linux/kernel.h>
17 #include <linux/export.h>
22 #include <cs5536/cs5536_pci.h>
23 #include <cs5536/cs5536.h>
26 #define PCI_ACCESS_READ 0
27 #define PCI_ACCESS_WRITE 1
29 #define CFG_SPACE_REG(offset) \
30 (void *)CKSEG1ADDR(LOONGSON_PCICFG_BASE | (offset))
31 #define ID_SEL_BEGIN 11
32 #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
35 static int loongson_pcibios_config_access(unsigned char access_type
,
37 unsigned int devfn
, int where
,
40 u32 busnum
= bus
->number
;
44 int device
= PCI_SLOT(devfn
);
45 int function
= PCI_FUNC(devfn
);
49 /* board-specific part,currently,only fuloong2f,yeeloong2f
50 * use CS5536, fuloong2e use via686b, gdium has no
54 /* cs5536_pci_conf_read4/write4() will call _rdmsr/_wrmsr() to
55 * access the regsters PCI_MSR_ADDR, PCI_MSR_DATA_LO,
56 * PCI_MSR_DATA_HI, which is bigger than PCI_MSR_CTRL, so, it
57 * will not go this branch, but the others. so, no calling dead
60 if ((PCI_IDSEL_CS5536
== device
) && (reg
< PCI_MSR_CTRL
)) {
61 switch (access_type
) {
63 *data
= cs5536_pci_conf_read4(function
, reg
);
65 case PCI_ACCESS_WRITE
:
66 cs5536_pci_conf_write4(function
, reg
, *data
);
72 /* Type 0 configuration for onboard PCI bus */
73 if (device
> MAX_DEV_NUM
)
76 addr
= (1 << (device
+ ID_SEL_BEGIN
)) | (function
<< 8) | reg
;
79 /* Type 1 configuration for offboard PCI bus */
80 addr
= (busnum
<< 16) | (device
<< 11) | (function
<< 8) | reg
;
85 LOONGSON_PCICMD
|= LOONGSON_PCICMD_MABORT_CLR
| \
86 LOONGSON_PCICMD_MTABORT_CLR
;
88 LOONGSON_PCIMAP_CFG
= (addr
>> 16) | type
;
90 /* Flush Bonito register block */
91 dummy
= LOONGSON_PCIMAP_CFG
;
94 addrp
= CFG_SPACE_REG(addr
& 0xffff);
95 if (access_type
== PCI_ACCESS_WRITE
)
96 writel(cpu_to_le32(*data
), addrp
);
98 *data
= le32_to_cpu(readl(addrp
));
100 /* Detect Master/Target abort */
101 if (LOONGSON_PCICMD
& (LOONGSON_PCICMD_MABORT_CLR
|
102 LOONGSON_PCICMD_MTABORT_CLR
)) {
106 LOONGSON_PCICMD
|= (LOONGSON_PCICMD_MABORT_CLR
|
107 LOONGSON_PCICMD_MTABORT_CLR
);
118 * We can't address 8 and 16 bit words directly. Instead we have to
119 * read/write a 32bit word and mask/modify the data we actually want.
121 static int loongson_pcibios_read(struct pci_bus
*bus
, unsigned int devfn
,
122 int where
, int size
, u32
*val
)
126 if ((size
== 2) && (where
& 1))
127 return PCIBIOS_BAD_REGISTER_NUMBER
;
128 else if ((size
== 4) && (where
& 3))
129 return PCIBIOS_BAD_REGISTER_NUMBER
;
131 if (loongson_pcibios_config_access(PCI_ACCESS_READ
, bus
, devfn
, where
,
136 *val
= (data
>> ((where
& 3) << 3)) & 0xff;
138 *val
= (data
>> ((where
& 3) << 3)) & 0xffff;
142 return PCIBIOS_SUCCESSFUL
;
145 static int loongson_pcibios_write(struct pci_bus
*bus
, unsigned int devfn
,
146 int where
, int size
, u32 val
)
150 if ((size
== 2) && (where
& 1))
151 return PCIBIOS_BAD_REGISTER_NUMBER
;
152 else if ((size
== 4) && (where
& 3))
153 return PCIBIOS_BAD_REGISTER_NUMBER
;
158 if (loongson_pcibios_config_access(PCI_ACCESS_READ
, bus
, devfn
,
163 data
= (data
& ~(0xff << ((where
& 3) << 3))) |
164 (val
<< ((where
& 3) << 3));
166 data
= (data
& ~(0xffff << ((where
& 3) << 3))) |
167 (val
<< ((where
& 3) << 3));
170 if (loongson_pcibios_config_access(PCI_ACCESS_WRITE
, bus
, devfn
, where
,
174 return PCIBIOS_SUCCESSFUL
;
177 struct pci_ops loongson_pci_ops
= {
178 .read
= loongson_pcibios_read
,
179 .write
= loongson_pcibios_write
183 DEFINE_RAW_SPINLOCK(msr_lock
);
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);
193 raw_spin_lock_irqsave(&msr_lock
, flags
);
194 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_ADDR
, 4, msr
);
195 loongson_pcibios_read(&bus
, devfn
, PCI_MSR_DATA_LO
, 4, lo
);
196 loongson_pcibios_read(&bus
, devfn
, PCI_MSR_DATA_HI
, 4, hi
);
197 raw_spin_unlock_irqrestore(&msr_lock
, flags
);
199 EXPORT_SYMBOL(_rdmsr
);
201 void _wrmsr(u32 msr
, u32 hi
, u32 lo
)
203 struct pci_bus bus
= {
204 .number
= PCI_BUS_CS5536
206 u32 devfn
= PCI_DEVFN(PCI_IDSEL_CS5536
, 0);
209 raw_spin_lock_irqsave(&msr_lock
, flags
);
210 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_ADDR
, 4, msr
);
211 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_DATA_LO
, 4, lo
);
212 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_DATA_HI
, 4, hi
);
213 raw_spin_unlock_irqrestore(&msr_lock
, flags
);
215 EXPORT_SYMBOL(_wrmsr
);