1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc.
5 * Authors: Carsten Langgaard <carstenl@mips.com>
6 * Maciej W. Rozycki <macro@mips.com>
8 * Copyright (C) 2009 Lemote Inc.
9 * Author: Wu Zhangjin <wuzhangjin@gmail.com>
11 #include <linux/types.h>
12 #include <linux/pci.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
19 #include <cs5536/cs5536_pci.h>
20 #include <cs5536/cs5536.h>
23 #define PCI_ACCESS_READ 0
24 #define PCI_ACCESS_WRITE 1
26 #define CFG_SPACE_REG(offset) \
27 (void *)CKSEG1ADDR(LOONGSON_PCICFG_BASE | (offset))
28 #define ID_SEL_BEGIN 11
29 #define MAX_DEV_NUM (31 - ID_SEL_BEGIN)
32 static int loongson_pcibios_config_access(unsigned char access_type
,
34 unsigned int devfn
, int where
,
37 u32 busnum
= bus
->number
;
41 int device
= PCI_SLOT(devfn
);
42 int function
= PCI_FUNC(devfn
);
46 /* board-specific part,currently,only fuloong2f,yeeloong2f
47 * use CS5536, fuloong2e use via686b, gdium has no
51 /* cs5536_pci_conf_read4/write4() will call _rdmsr/_wrmsr() to
52 * access the regsters PCI_MSR_ADDR, PCI_MSR_DATA_LO,
53 * PCI_MSR_DATA_HI, which is bigger than PCI_MSR_CTRL, so, it
54 * will not go this branch, but the others. so, no calling dead
57 if ((PCI_IDSEL_CS5536
== device
) && (reg
< PCI_MSR_CTRL
)) {
58 switch (access_type
) {
60 *data
= cs5536_pci_conf_read4(function
, reg
);
62 case PCI_ACCESS_WRITE
:
63 cs5536_pci_conf_write4(function
, reg
, *data
);
69 /* Type 0 configuration for onboard PCI bus */
70 if (device
> MAX_DEV_NUM
)
73 addr
= (1 << (device
+ ID_SEL_BEGIN
)) | (function
<< 8) | reg
;
76 /* Type 1 configuration for offboard PCI bus */
77 addr
= (busnum
<< 16) | (device
<< 11) | (function
<< 8) | reg
;
82 LOONGSON_PCICMD
|= LOONGSON_PCICMD_MABORT_CLR
| \
83 LOONGSON_PCICMD_MTABORT_CLR
;
85 LOONGSON_PCIMAP_CFG
= (addr
>> 16) | type
;
87 /* Flush Bonito register block */
88 dummy
= LOONGSON_PCIMAP_CFG
;
91 addrp
= CFG_SPACE_REG(addr
& 0xffff);
92 if (access_type
== PCI_ACCESS_WRITE
)
93 writel(cpu_to_le32(*data
), addrp
);
95 *data
= le32_to_cpu(readl(addrp
));
97 /* Detect Master/Target abort */
98 if (LOONGSON_PCICMD
& (LOONGSON_PCICMD_MABORT_CLR
|
99 LOONGSON_PCICMD_MTABORT_CLR
)) {
103 LOONGSON_PCICMD
|= (LOONGSON_PCICMD_MABORT_CLR
|
104 LOONGSON_PCICMD_MTABORT_CLR
);
115 * We can't address 8 and 16 bit words directly. Instead we have to
116 * read/write a 32bit word and mask/modify the data we actually want.
118 static int loongson_pcibios_read(struct pci_bus
*bus
, unsigned int devfn
,
119 int where
, int size
, u32
*val
)
123 if ((size
== 2) && (where
& 1))
124 return PCIBIOS_BAD_REGISTER_NUMBER
;
125 else if ((size
== 4) && (where
& 3))
126 return PCIBIOS_BAD_REGISTER_NUMBER
;
128 if (loongson_pcibios_config_access(PCI_ACCESS_READ
, bus
, devfn
, where
,
133 *val
= (data
>> ((where
& 3) << 3)) & 0xff;
135 *val
= (data
>> ((where
& 3) << 3)) & 0xffff;
139 return PCIBIOS_SUCCESSFUL
;
142 static int loongson_pcibios_write(struct pci_bus
*bus
, unsigned int devfn
,
143 int where
, int size
, u32 val
)
147 if ((size
== 2) && (where
& 1))
148 return PCIBIOS_BAD_REGISTER_NUMBER
;
149 else if ((size
== 4) && (where
& 3))
150 return PCIBIOS_BAD_REGISTER_NUMBER
;
155 if (loongson_pcibios_config_access(PCI_ACCESS_READ
, bus
, devfn
,
160 data
= (data
& ~(0xff << ((where
& 3) << 3))) |
161 (val
<< ((where
& 3) << 3));
163 data
= (data
& ~(0xffff << ((where
& 3) << 3))) |
164 (val
<< ((where
& 3) << 3));
167 if (loongson_pcibios_config_access(PCI_ACCESS_WRITE
, bus
, devfn
, where
,
171 return PCIBIOS_SUCCESSFUL
;
174 struct pci_ops loongson_pci_ops
= {
175 .read
= loongson_pcibios_read
,
176 .write
= loongson_pcibios_write
180 DEFINE_RAW_SPINLOCK(msr_lock
);
182 void _rdmsr(u32 msr
, u32
*hi
, u32
*lo
)
184 struct pci_bus bus
= {
185 .number
= PCI_BUS_CS5536
187 u32 devfn
= PCI_DEVFN(PCI_IDSEL_CS5536
, 0);
190 raw_spin_lock_irqsave(&msr_lock
, flags
);
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
);
194 raw_spin_unlock_irqrestore(&msr_lock
, flags
);
196 EXPORT_SYMBOL(_rdmsr
);
198 void _wrmsr(u32 msr
, u32 hi
, u32 lo
)
200 struct pci_bus bus
= {
201 .number
= PCI_BUS_CS5536
203 u32 devfn
= PCI_DEVFN(PCI_IDSEL_CS5536
, 0);
206 raw_spin_lock_irqsave(&msr_lock
, flags
);
207 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_ADDR
, 4, msr
);
208 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_DATA_LO
, 4, lo
);
209 loongson_pcibios_write(&bus
, devfn
, PCI_MSR_DATA_HI
, 4, hi
);
210 raw_spin_unlock_irqrestore(&msr_lock
, flags
);
212 EXPORT_SYMBOL(_wrmsr
);