1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Low-level PCI config space access for OLPC systems who lack the VSA
4*4882a593Smuzhiyun * PCI virtualization software.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright © 2006 Advanced Micro Devices, Inc.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The AMD Geode chipset (ie: GX2 processor, cs5536 I/O companion device)
9*4882a593Smuzhiyun * has some I/O functions (display, southbridge, sound, USB HCIs, etc)
10*4882a593Smuzhiyun * that more or less behave like PCI devices, but the hardware doesn't
11*4882a593Smuzhiyun * directly implement the PCI configuration space headers. AMD provides
12*4882a593Smuzhiyun * "VSA" (Virtual System Architecture) software that emulates PCI config
13*4882a593Smuzhiyun * space for these devices, by trapping I/O accesses to PCI config register
14*4882a593Smuzhiyun * (CF8/CFC) and running some code in System Management Mode interrupt state.
15*4882a593Smuzhiyun * On the OLPC platform, we don't want to use that VSA code because
16*4882a593Smuzhiyun * (a) it slows down suspend/resume, and (b) recompiling it requires special
17*4882a593Smuzhiyun * compilers that are hard to get. So instead of letting the complex VSA
18*4882a593Smuzhiyun * code simulate the PCI config registers for the on-chip devices, we
19*4882a593Smuzhiyun * just simulate them the easy way, by inserting the code into the
20*4882a593Smuzhiyun * pci_write_config and pci_read_config path. Most of the config registers
21*4882a593Smuzhiyun * are read-only anyway, so the bulk of the simulation is just table lookup.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <linux/pci.h>
25*4882a593Smuzhiyun #include <linux/init.h>
26*4882a593Smuzhiyun #include <asm/olpc.h>
27*4882a593Smuzhiyun #include <asm/geode.h>
28*4882a593Smuzhiyun #include <asm/pci_x86.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * In the tables below, the first two line (8 longwords) are the
32*4882a593Smuzhiyun * size masks that are used when the higher level PCI code determines
33*4882a593Smuzhiyun * the size of the region by writing ~0 to a base address register
34*4882a593Smuzhiyun * and reading back the result.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * The following lines are the values that are read during normal
37*4882a593Smuzhiyun * PCI config access cycles, i.e. not after just having written
38*4882a593Smuzhiyun * ~0 to a base address register.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static const uint32_t lxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */
42*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
43*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun 0x281022, 0x2200005, 0x6000021, 0x80f808, /* AMD Vendor ID */
46*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0, /* No virtual registers, hence no BAR */
47*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x28100b,
48*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
49*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
50*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
51*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun static const uint32_t gxnb_hdr[] = { /* dev 1 function 0 - devfn = 8 */
55*4882a593Smuzhiyun 0xfffffffd, 0x0, 0x0, 0x0,
56*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun 0x28100b, 0x2200005, 0x6000021, 0x80f808, /* NSC Vendor ID */
59*4882a593Smuzhiyun 0xac1d, 0x0, 0x0, 0x0, /* I/O BAR - base of virtual registers */
60*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x28100b,
61*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
62*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
63*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
64*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun static const uint32_t lxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */
68*4882a593Smuzhiyun 0xff000008, 0xffffc000, 0xffffc000, 0xffffc000,
69*4882a593Smuzhiyun 0xffffc000, 0x0, 0x0, 0x0,
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun 0x20811022, 0x2200003, 0x3000000, 0x0, /* AMD Vendor ID */
72*4882a593Smuzhiyun 0xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */
73*4882a593Smuzhiyun 0xfe00c000, 0x0, 0x0, 0x30100b, /* VIP */
74*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x10e, /* INTA, IRQ14 for graphics accel */
75*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
76*4882a593Smuzhiyun 0x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */
77*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun static const uint32_t gxfb_hdr[] = { /* dev 1 function 1 - devfn = 9 */
81*4882a593Smuzhiyun 0xff800008, 0xffffc000, 0xffffc000, 0xffffc000,
82*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun 0x30100b, 0x2200003, 0x3000000, 0x0, /* NSC Vendor ID */
85*4882a593Smuzhiyun 0xfd000000, 0xfe000000, 0xfe004000, 0xfe008000, /* FB, GP, VG, DF */
86*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x30100b,
87*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
88*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
89*4882a593Smuzhiyun 0x3d0, 0x3c0, 0xa0000, 0x0, /* VG IO, VG IO, EGA FB, MONO FB */
90*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun static const uint32_t aes_hdr[] = { /* dev 1 function 2 - devfn = 0xa */
94*4882a593Smuzhiyun 0xffffc000, 0x0, 0x0, 0x0,
95*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun 0x20821022, 0x2a00006, 0x10100000, 0x8, /* NSC Vendor ID */
98*4882a593Smuzhiyun 0xfe010000, 0x0, 0x0, 0x0, /* AES registers */
99*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x20821022,
100*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
101*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
102*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
103*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun static const uint32_t isa_hdr[] = { /* dev f function 0 - devfn = 78 */
108*4882a593Smuzhiyun 0xfffffff9, 0xffffff01, 0xffffffc1, 0xffffffe1,
109*4882a593Smuzhiyun 0xffffff81, 0xffffffc1, 0x0, 0x0,
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun 0x20901022, 0x2a00049, 0x6010003, 0x802000,
112*4882a593Smuzhiyun 0x18b1, 0x1001, 0x1801, 0x1881, /* SMB-8 GPIO-256 MFGPT-64 IRQ-32 */
113*4882a593Smuzhiyun 0x1401, 0x1841, 0x0, 0x20901022, /* PMS-128 ACPI-64 */
114*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
115*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
116*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0xaa5b, /* IRQ steering */
117*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun static const uint32_t ac97_hdr[] = { /* dev f function 3 - devfn = 7b */
121*4882a593Smuzhiyun 0xffffff81, 0x0, 0x0, 0x0,
122*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun 0x20931022, 0x2a00041, 0x4010001, 0x0,
125*4882a593Smuzhiyun 0x1481, 0x0, 0x0, 0x0, /* I/O BAR-128 */
126*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x20931022,
127*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x205, /* IntB, IRQ5 */
128*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
129*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
130*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun static const uint32_t ohci_hdr[] = { /* dev f function 4 - devfn = 7c */
134*4882a593Smuzhiyun 0xfffff000, 0x0, 0x0, 0x0,
135*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun 0x20941022, 0x2300006, 0xc031002, 0x0,
138*4882a593Smuzhiyun 0xfe01a000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */
139*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x20941022,
140*4882a593Smuzhiyun 0x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */
141*4882a593Smuzhiyun 0xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O,
142*4882a593Smuzhiyun 44 is mask 8103 (power control) */
143*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
144*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun static const uint32_t ehci_hdr[] = { /* dev f function 4 - devfn = 7d */
148*4882a593Smuzhiyun 0xfffff000, 0x0, 0x0, 0x0,
149*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x0,
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun 0x20951022, 0x2300006, 0xc032002, 0x0,
152*4882a593Smuzhiyun 0xfe01b000, 0x0, 0x0, 0x0, /* MEMBAR-1000 */
153*4882a593Smuzhiyun 0x0, 0x0, 0x0, 0x20951022,
154*4882a593Smuzhiyun 0x0, 0x40, 0x0, 0x40a, /* CapPtr INT-D, IRQA */
155*4882a593Smuzhiyun 0xc8020001, 0x0, 0x0, 0x0, /* Capabilities - 40 is R/O, 44 is
156*4882a593Smuzhiyun mask 8103 (power control) */
157*4882a593Smuzhiyun #if 0
158*4882a593Smuzhiyun 0x1, 0x40080000, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun 0x01000001, 0x0, 0x0, 0x0, /* EECP - see EHCI spec section 2.1.7 */
161*4882a593Smuzhiyun 0x2020, 0x0, 0x0, 0x0, /* (EHCI page 8) 60 SBRN (R/O),
162*4882a593Smuzhiyun 61 FLADJ (R/W), PORTWAKECAP */
163*4882a593Smuzhiyun };
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun static uint32_t ff_loc = ~0;
166*4882a593Smuzhiyun static uint32_t zero_loc;
167*4882a593Smuzhiyun static int bar_probing; /* Set after a write of ~0 to a BAR */
168*4882a593Smuzhiyun static int is_lx;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun #define NB_SLOT 0x1 /* Northbridge - GX chip - Device 1 */
171*4882a593Smuzhiyun #define SB_SLOT 0xf /* Southbridge - CS5536 chip - Device F */
172*4882a593Smuzhiyun
is_simulated(unsigned int bus,unsigned int devfn)173*4882a593Smuzhiyun static int is_simulated(unsigned int bus, unsigned int devfn)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun return (!bus && ((PCI_SLOT(devfn) == NB_SLOT) ||
176*4882a593Smuzhiyun (PCI_SLOT(devfn) == SB_SLOT)));
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
hdr_addr(const uint32_t * hdr,int reg)179*4882a593Smuzhiyun static uint32_t *hdr_addr(const uint32_t *hdr, int reg)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun uint32_t addr;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * This is a little bit tricky. The header maps consist of
185*4882a593Smuzhiyun * 0x20 bytes of size masks, followed by 0x70 bytes of header data.
186*4882a593Smuzhiyun * In the normal case, when not probing a BAR's size, we want
187*4882a593Smuzhiyun * to access the header data, so we add 0x20 to the reg offset,
188*4882a593Smuzhiyun * thus skipping the size mask area.
189*4882a593Smuzhiyun * In the BAR probing case, we want to access the size mask for
190*4882a593Smuzhiyun * the BAR, so we subtract 0x10 (the config header offset for
191*4882a593Smuzhiyun * BAR0), and don't skip the size mask area.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun addr = (uint32_t)hdr + reg + (bar_probing ? -0x10 : 0x20);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun bar_probing = 0;
197*4882a593Smuzhiyun return (uint32_t *)addr;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
pci_olpc_read(unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,uint32_t * value)200*4882a593Smuzhiyun static int pci_olpc_read(unsigned int seg, unsigned int bus,
201*4882a593Smuzhiyun unsigned int devfn, int reg, int len, uint32_t *value)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun uint32_t *addr;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun WARN_ON(seg);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* Use the hardware mechanism for non-simulated devices */
208*4882a593Smuzhiyun if (!is_simulated(bus, devfn))
209*4882a593Smuzhiyun return pci_direct_conf1.read(seg, bus, devfn, reg, len, value);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * No device has config registers past 0x70, so we save table space
213*4882a593Smuzhiyun * by not storing entries for the nonexistent registers
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun if (reg >= 0x70)
216*4882a593Smuzhiyun addr = &zero_loc;
217*4882a593Smuzhiyun else {
218*4882a593Smuzhiyun switch (devfn) {
219*4882a593Smuzhiyun case 0x8:
220*4882a593Smuzhiyun addr = hdr_addr(is_lx ? lxnb_hdr : gxnb_hdr, reg);
221*4882a593Smuzhiyun break;
222*4882a593Smuzhiyun case 0x9:
223*4882a593Smuzhiyun addr = hdr_addr(is_lx ? lxfb_hdr : gxfb_hdr, reg);
224*4882a593Smuzhiyun break;
225*4882a593Smuzhiyun case 0xa:
226*4882a593Smuzhiyun addr = is_lx ? hdr_addr(aes_hdr, reg) : &ff_loc;
227*4882a593Smuzhiyun break;
228*4882a593Smuzhiyun case 0x78:
229*4882a593Smuzhiyun addr = hdr_addr(isa_hdr, reg);
230*4882a593Smuzhiyun break;
231*4882a593Smuzhiyun case 0x7b:
232*4882a593Smuzhiyun addr = hdr_addr(ac97_hdr, reg);
233*4882a593Smuzhiyun break;
234*4882a593Smuzhiyun case 0x7c:
235*4882a593Smuzhiyun addr = hdr_addr(ohci_hdr, reg);
236*4882a593Smuzhiyun break;
237*4882a593Smuzhiyun case 0x7d:
238*4882a593Smuzhiyun addr = hdr_addr(ehci_hdr, reg);
239*4882a593Smuzhiyun break;
240*4882a593Smuzhiyun default:
241*4882a593Smuzhiyun addr = &ff_loc;
242*4882a593Smuzhiyun break;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun switch (len) {
246*4882a593Smuzhiyun case 1:
247*4882a593Smuzhiyun *value = *(uint8_t *)addr;
248*4882a593Smuzhiyun break;
249*4882a593Smuzhiyun case 2:
250*4882a593Smuzhiyun *value = *(uint16_t *)addr;
251*4882a593Smuzhiyun break;
252*4882a593Smuzhiyun case 4:
253*4882a593Smuzhiyun *value = *addr;
254*4882a593Smuzhiyun break;
255*4882a593Smuzhiyun default:
256*4882a593Smuzhiyun BUG();
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun return 0;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
pci_olpc_write(unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,uint32_t value)262*4882a593Smuzhiyun static int pci_olpc_write(unsigned int seg, unsigned int bus,
263*4882a593Smuzhiyun unsigned int devfn, int reg, int len, uint32_t value)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun WARN_ON(seg);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* Use the hardware mechanism for non-simulated devices */
268*4882a593Smuzhiyun if (!is_simulated(bus, devfn))
269*4882a593Smuzhiyun return pci_direct_conf1.write(seg, bus, devfn, reg, len, value);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* XXX we may want to extend this to simulate EHCI power management */
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /*
274*4882a593Smuzhiyun * Mostly we just discard writes, but if the write is a size probe
275*4882a593Smuzhiyun * (i.e. writing ~0 to a BAR), we remember it and arrange to return
276*4882a593Smuzhiyun * the appropriate size mask on the next read. This is cheating
277*4882a593Smuzhiyun * to some extent, because it depends on the fact that the next
278*4882a593Smuzhiyun * access after such a write will always be a read to the same BAR.
279*4882a593Smuzhiyun */
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun if ((reg >= 0x10) && (reg < 0x2c)) {
282*4882a593Smuzhiyun /* write is to a BAR */
283*4882a593Smuzhiyun if (value == ~0)
284*4882a593Smuzhiyun bar_probing = 1;
285*4882a593Smuzhiyun } else {
286*4882a593Smuzhiyun /*
287*4882a593Smuzhiyun * No warning on writes to ROM BAR, CMD, LATENCY_TIMER,
288*4882a593Smuzhiyun * CACHE_LINE_SIZE, or PM registers.
289*4882a593Smuzhiyun */
290*4882a593Smuzhiyun if ((reg != PCI_ROM_ADDRESS) && (reg != PCI_COMMAND_MASTER) &&
291*4882a593Smuzhiyun (reg != PCI_LATENCY_TIMER) &&
292*4882a593Smuzhiyun (reg != PCI_CACHE_LINE_SIZE) && (reg != 0x44))
293*4882a593Smuzhiyun printk(KERN_WARNING "OLPC PCI: Config write to devfn"
294*4882a593Smuzhiyun " %x reg %x value %x\n", devfn, reg, value);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun static const struct pci_raw_ops pci_olpc_conf = {
301*4882a593Smuzhiyun .read = pci_olpc_read,
302*4882a593Smuzhiyun .write = pci_olpc_write,
303*4882a593Smuzhiyun };
304*4882a593Smuzhiyun
pci_olpc_init(void)305*4882a593Smuzhiyun int __init pci_olpc_init(void)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun printk(KERN_INFO "PCI: Using configuration type OLPC XO-1\n");
308*4882a593Smuzhiyun raw_pci_ops = &pci_olpc_conf;
309*4882a593Smuzhiyun is_lx = is_geode_lx();
310*4882a593Smuzhiyun return 0;
311*4882a593Smuzhiyun }
312