blob: 059ac4e8a67e7365445d599b10a934a2d7088c29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-integrator/pci_v3.c
3 *
4 * PCI functions for V3 host PCI bridge
5 *
6 * Copyright (C) 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/ioport.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010029#include <linux/io.h>
Linus Walleij86adc392013-02-02 23:16:57 +010030#include <linux/platform_device.h>
Linus Walleij01ef3102013-03-16 22:03:41 +010031#include <video/vga.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/hardware.h>
Russell Kinga285edc2010-01-14 19:59:37 +000034#include <mach/platform.h>
Linus Walleij695436e2012-02-26 10:46:48 +010035#include <mach/irqs.h>
36
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040037#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/mach/pci.h>
Russell Kingc6af66b2007-05-17 10:16:55 +010039#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Walleij207bcf42013-02-03 00:20:44 +010041/*
42 * V3 Local Bus to PCI Bridge definitions
43 *
44 * Registers (these are taken from page 129 of the EPC User's Manual Rev 1.04
45 * All V3 register names are prefaced by V3_ to avoid clashing with any other
46 * PCI definitions. Their names match the user's manual.
47 *
48 * I'm assuming that I20 is disabled.
49 *
50 */
51#define V3_PCI_VENDOR 0x00000000
52#define V3_PCI_DEVICE 0x00000002
53#define V3_PCI_CMD 0x00000004
54#define V3_PCI_STAT 0x00000006
55#define V3_PCI_CC_REV 0x00000008
56#define V3_PCI_HDR_CFG 0x0000000C
57#define V3_PCI_IO_BASE 0x00000010
58#define V3_PCI_BASE0 0x00000014
59#define V3_PCI_BASE1 0x00000018
60#define V3_PCI_SUB_VENDOR 0x0000002C
61#define V3_PCI_SUB_ID 0x0000002E
62#define V3_PCI_ROM 0x00000030
63#define V3_PCI_BPARAM 0x0000003C
64#define V3_PCI_MAP0 0x00000040
65#define V3_PCI_MAP1 0x00000044
66#define V3_PCI_INT_STAT 0x00000048
67#define V3_PCI_INT_CFG 0x0000004C
68#define V3_LB_BASE0 0x00000054
69#define V3_LB_BASE1 0x00000058
70#define V3_LB_MAP0 0x0000005E
71#define V3_LB_MAP1 0x00000062
72#define V3_LB_BASE2 0x00000064
73#define V3_LB_MAP2 0x00000066
74#define V3_LB_SIZE 0x00000068
75#define V3_LB_IO_BASE 0x0000006E
76#define V3_FIFO_CFG 0x00000070
77#define V3_FIFO_PRIORITY 0x00000072
78#define V3_FIFO_STAT 0x00000074
79#define V3_LB_ISTAT 0x00000076
80#define V3_LB_IMASK 0x00000077
81#define V3_SYSTEM 0x00000078
82#define V3_LB_CFG 0x0000007A
83#define V3_PCI_CFG 0x0000007C
84#define V3_DMA_PCI_ADR0 0x00000080
85#define V3_DMA_PCI_ADR1 0x00000090
86#define V3_DMA_LOCAL_ADR0 0x00000084
87#define V3_DMA_LOCAL_ADR1 0x00000094
88#define V3_DMA_LENGTH0 0x00000088
89#define V3_DMA_LENGTH1 0x00000098
90#define V3_DMA_CSR0 0x0000008B
91#define V3_DMA_CSR1 0x0000009B
92#define V3_DMA_CTLB_ADR0 0x0000008C
93#define V3_DMA_CTLB_ADR1 0x0000009C
94#define V3_DMA_DELAY 0x000000E0
95#define V3_MAIL_DATA 0x000000C0
96#define V3_PCI_MAIL_IEWR 0x000000D0
97#define V3_PCI_MAIL_IERD 0x000000D2
98#define V3_LB_MAIL_IEWR 0x000000D4
99#define V3_LB_MAIL_IERD 0x000000D6
100#define V3_MAIL_WR_STAT 0x000000D8
101#define V3_MAIL_RD_STAT 0x000000DA
102#define V3_QBA_MAP 0x000000DC
103
104/* PCI COMMAND REGISTER bits
105 */
106#define V3_COMMAND_M_FBB_EN (1 << 9)
107#define V3_COMMAND_M_SERR_EN (1 << 8)
108#define V3_COMMAND_M_PAR_EN (1 << 6)
109#define V3_COMMAND_M_MASTER_EN (1 << 2)
110#define V3_COMMAND_M_MEM_EN (1 << 1)
111#define V3_COMMAND_M_IO_EN (1 << 0)
112
113/* SYSTEM REGISTER bits
114 */
115#define V3_SYSTEM_M_RST_OUT (1 << 15)
116#define V3_SYSTEM_M_LOCK (1 << 14)
117
118/* PCI_CFG bits
119 */
120#define V3_PCI_CFG_M_I2O_EN (1 << 15)
121#define V3_PCI_CFG_M_IO_REG_DIS (1 << 14)
122#define V3_PCI_CFG_M_IO_DIS (1 << 13)
123#define V3_PCI_CFG_M_EN3V (1 << 12)
124#define V3_PCI_CFG_M_RETRY_EN (1 << 10)
125#define V3_PCI_CFG_M_AD_LOW1 (1 << 9)
126#define V3_PCI_CFG_M_AD_LOW0 (1 << 8)
127
128/* PCI_BASE register bits (PCI -> Local Bus)
129 */
130#define V3_PCI_BASE_M_ADR_BASE 0xFFF00000
131#define V3_PCI_BASE_M_ADR_BASEL 0x000FFF00
132#define V3_PCI_BASE_M_PREFETCH (1 << 3)
133#define V3_PCI_BASE_M_TYPE (3 << 1)
134#define V3_PCI_BASE_M_IO (1 << 0)
135
136/* PCI MAP register bits (PCI -> Local bus)
137 */
138#define V3_PCI_MAP_M_MAP_ADR 0xFFF00000
139#define V3_PCI_MAP_M_RD_POST_INH (1 << 15)
140#define V3_PCI_MAP_M_ROM_SIZE (3 << 10)
141#define V3_PCI_MAP_M_SWAP (3 << 8)
142#define V3_PCI_MAP_M_ADR_SIZE 0x000000F0
143#define V3_PCI_MAP_M_REG_EN (1 << 1)
144#define V3_PCI_MAP_M_ENABLE (1 << 0)
145
146/*
147 * LB_BASE0,1 register bits (Local bus -> PCI)
148 */
149#define V3_LB_BASE_ADR_BASE 0xfff00000
150#define V3_LB_BASE_SWAP (3 << 8)
151#define V3_LB_BASE_ADR_SIZE (15 << 4)
152#define V3_LB_BASE_PREFETCH (1 << 3)
153#define V3_LB_BASE_ENABLE (1 << 0)
154
155#define V3_LB_BASE_ADR_SIZE_1MB (0 << 4)
156#define V3_LB_BASE_ADR_SIZE_2MB (1 << 4)
157#define V3_LB_BASE_ADR_SIZE_4MB (2 << 4)
158#define V3_LB_BASE_ADR_SIZE_8MB (3 << 4)
159#define V3_LB_BASE_ADR_SIZE_16MB (4 << 4)
160#define V3_LB_BASE_ADR_SIZE_32MB (5 << 4)
161#define V3_LB_BASE_ADR_SIZE_64MB (6 << 4)
162#define V3_LB_BASE_ADR_SIZE_128MB (7 << 4)
163#define V3_LB_BASE_ADR_SIZE_256MB (8 << 4)
164#define V3_LB_BASE_ADR_SIZE_512MB (9 << 4)
165#define V3_LB_BASE_ADR_SIZE_1GB (10 << 4)
166#define V3_LB_BASE_ADR_SIZE_2GB (11 << 4)
167
168#define v3_addr_to_lb_base(a) ((a) & V3_LB_BASE_ADR_BASE)
169
170/*
171 * LB_MAP0,1 register bits (Local bus -> PCI)
172 */
173#define V3_LB_MAP_MAP_ADR 0xfff0
174#define V3_LB_MAP_TYPE (7 << 1)
175#define V3_LB_MAP_AD_LOW_EN (1 << 0)
176
177#define V3_LB_MAP_TYPE_IACK (0 << 1)
178#define V3_LB_MAP_TYPE_IO (1 << 1)
179#define V3_LB_MAP_TYPE_MEM (3 << 1)
180#define V3_LB_MAP_TYPE_CONFIG (5 << 1)
181#define V3_LB_MAP_TYPE_MEM_MULTIPLE (6 << 1)
182
183#define v3_addr_to_lb_map(a) (((a) >> 16) & V3_LB_MAP_MAP_ADR)
184
185/*
186 * LB_BASE2 register bits (Local bus -> PCI IO)
187 */
188#define V3_LB_BASE2_ADR_BASE 0xff00
189#define V3_LB_BASE2_SWAP (3 << 6)
190#define V3_LB_BASE2_ENABLE (1 << 0)
191
192#define v3_addr_to_lb_base2(a) (((a) >> 16) & V3_LB_BASE2_ADR_BASE)
193
194/*
195 * LB_MAP2 register bits (Local bus -> PCI IO)
196 */
197#define V3_LB_MAP2_MAP_ADR 0xff00
198
199#define v3_addr_to_lb_map2(a) (((a) >> 16) & V3_LB_MAP2_MAP_ADR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/*
202 * The V3 PCI interface chip in Integrator provides several windows from
203 * local bus memory into the PCI memory areas. Unfortunately, there
Rob Herring29d39602012-07-13 16:27:43 -0500204 * are not really enough windows for our usage, therefore we reuse
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * one of the windows for access to PCI configuration space. The
206 * memory map is as follows:
Rob Herring29d39602012-07-13 16:27:43 -0500207 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * Local Bus Memory Usage
Rob Herring29d39602012-07-13 16:27:43 -0500209 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * 40000000 - 4FFFFFFF PCI memory. 256M non-prefetchable
211 * 50000000 - 5FFFFFFF PCI memory. 256M prefetchable
212 * 60000000 - 60FFFFFF PCI IO. 16M
213 * 61000000 - 61FFFFFF PCI Configuration. 16M
Rob Herring29d39602012-07-13 16:27:43 -0500214 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * There are three V3 windows, each described by a pair of V3 registers.
216 * These are LB_BASE0/LB_MAP0, LB_BASE1/LB_MAP1 and LB_BASE2/LB_MAP2.
217 * Base0 and Base1 can be used for any type of PCI memory access. Base2
218 * can be used either for PCI I/O or for I20 accesses. By default, uHAL
219 * uses this only for PCI IO space.
Rob Herring29d39602012-07-13 16:27:43 -0500220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Normally these spaces are mapped using the following base registers:
Rob Herring29d39602012-07-13 16:27:43 -0500222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Usage Local Bus Memory Base/Map registers used
Rob Herring29d39602012-07-13 16:27:43 -0500224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
226 * Mem 50000000 - 5FFFFFFF LB_BASE1/LB_MAP1
227 * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
228 * Cfg 61000000 - 61FFFFFF
Rob Herring29d39602012-07-13 16:27:43 -0500229 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * This means that I20 and PCI configuration space accesses will fail.
Rob Herring29d39602012-07-13 16:27:43 -0500231 * When PCI configuration accesses are needed (via the uHAL PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * configuration space primitives) we must remap the spaces as follows:
Rob Herring29d39602012-07-13 16:27:43 -0500233 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Usage Local Bus Memory Base/Map registers used
Rob Herring29d39602012-07-13 16:27:43 -0500235 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
237 * Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP0
238 * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
239 * Cfg 61000000 - 61FFFFFF LB_BASE1/LB_MAP1
Rob Herring29d39602012-07-13 16:27:43 -0500240 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * To make this work, the code depends on overlapping windows working.
Rob Herring29d39602012-07-13 16:27:43 -0500242 * The V3 chip translates an address by checking its range within
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * each of the BASE/MAP pairs in turn (in ascending register number
244 * order). It will use the first matching pair. So, for example,
245 * if the same address is mapped by both LB_BASE0/LB_MAP0 and
Rob Herring29d39602012-07-13 16:27:43 -0500246 * LB_BASE1/LB_MAP1, the V3 will use the translation from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * LB_BASE0/LB_MAP0.
Rob Herring29d39602012-07-13 16:27:43 -0500248 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * To allow PCI Configuration space access, the code enlarges the
250 * window mapped by LB_BASE0/LB_MAP0 from 256M to 512M. This occludes
251 * the windows currently mapped by LB_BASE1/LB_MAP1 so that it can
252 * be remapped for use by configuration cycles.
Rob Herring29d39602012-07-13 16:27:43 -0500253 *
254 * At the end of the PCI Configuration space accesses,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * LB_BASE1/LB_MAP1 is reset to map PCI Memory. Finally the window
256 * mapped by LB_BASE0/LB_MAP0 is reduced in size from 512M to 256M to
257 * reveal the now restored LB_BASE1/LB_MAP1 window.
Rob Herring29d39602012-07-13 16:27:43 -0500258 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * NOTE: We do not set up I2O mapping. I suspect that this is only
260 * for an intelligent (target) device. Using I2O disables most of
261 * the mappings into PCI memory.
262 */
263
Linus Walleija5ecbab2013-03-16 21:51:02 +0100264static void __iomem *pci_v3_base;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266// V3 access routines
Linus Walleija5ecbab2013-03-16 21:51:02 +0100267#define v3_writeb(o,v) __raw_writeb(v, pci_v3_base + (unsigned int)(o))
268#define v3_readb(o) (__raw_readb(pci_v3_base + (unsigned int)(o)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Walleija5ecbab2013-03-16 21:51:02 +0100270#define v3_writew(o,v) __raw_writew(v, pci_v3_base + (unsigned int)(o))
271#define v3_readw(o) (__raw_readw(pci_v3_base + (unsigned int)(o)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Walleija5ecbab2013-03-16 21:51:02 +0100273#define v3_writel(o,v) __raw_writel(v, pci_v3_base + (unsigned int)(o))
274#define v3_readl(o) (__raw_readl(pci_v3_base + (unsigned int)(o)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/*============================================================================
277 *
278 * routine: uHALir_PCIMakeConfigAddress()
279 *
280 * parameters: bus = which bus
281 * device = which device
282 * function = which function
283 * offset = configuration space register we are interested in
284 *
285 * description: this routine will generate a platform dependent config
286 * address.
287 *
288 * calls: none
289 *
290 * returns: configuration address to play on the PCI bus
291 *
Rob Herring29d39602012-07-13 16:27:43 -0500292 * To generate the appropriate PCI configuration cycles in the PCI
293 * configuration address space, you present the V3 with the following pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * (which is very nearly a type 1 (except that the lower two bits are 00 and
295 * not 01). In order for this mapping to work you need to set up one of
296 * the local to PCI aperatures to 16Mbytes in length translating to
297 * PCI configuration space starting at 0x0000.0000.
298 *
299 * PCI configuration cycles look like this:
300 *
301 * Type 0:
302 *
Rob Herring29d39602012-07-13 16:27:43 -0500303 * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
305 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
306 * | | |D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|0|
307 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 *
309 * 31:11 Device select bit.
310 * 10:8 Function number
311 * 7:2 Register number
312 *
313 * Type 1:
314 *
Rob Herring29d39602012-07-13 16:27:43 -0500315 * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
317 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
318 * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320 *
321 * 31:24 reserved
322 * 23:16 bus number (8 bits = 128 possible buses)
323 * 15:11 Device number (5 bits)
324 * 10:8 function number
325 * 7:2 register number
Rob Herring29d39602012-07-13 16:27:43 -0500326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500328static DEFINE_RAW_SPINLOCK(v3_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330#define PCI_BUS_NONMEM_START 0x00000000
331#define PCI_BUS_NONMEM_SIZE SZ_256M
332
333#define PCI_BUS_PREMEM_START PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE
334#define PCI_BUS_PREMEM_SIZE SZ_256M
335
336#if PCI_BUS_NONMEM_START & 0x000fffff
337#error PCI_BUS_NONMEM_START must be megabyte aligned
338#endif
339#if PCI_BUS_PREMEM_START & 0x000fffff
340#error PCI_BUS_PREMEM_START must be megabyte aligned
341#endif
342
343#undef V3_LB_BASE_PREFETCH
344#define V3_LB_BASE_PREFETCH 0
345
Arnd Bergmannb7a3f8d2012-09-14 20:16:39 +0000346static void __iomem *v3_open_config_window(struct pci_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 unsigned int devfn, int offset)
348{
349 unsigned int address, mapaddress, busnr;
350
351 busnr = bus->number;
352
353 /*
354 * Trap out illegal values
355 */
Sasha Levinf7a9b362012-11-08 15:23:08 -0500356 BUG_ON(offset > 255);
357 BUG_ON(busnr > 255);
358 BUG_ON(devfn > 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (busnr == 0) {
361 int slot = PCI_SLOT(devfn);
362
363 /*
364 * local bus segment so need a type 0 config cycle
365 *
366 * build the PCI configuration "address" with one-hot in
367 * A31-A11
368 *
369 * mapaddress:
370 * 3:1 = config cycle (101)
371 * 0 = PCI A1 & A0 are 0 (0)
372 */
373 address = PCI_FUNC(devfn) << 8;
374 mapaddress = V3_LB_MAP_TYPE_CONFIG;
375
376 if (slot > 12)
377 /*
378 * high order bits are handled by the MAP register
379 */
380 mapaddress |= 1 << (slot - 5);
381 else
382 /*
383 * low order bits handled directly in the address
384 */
385 address |= 1 << (slot + 11);
386 } else {
387 /*
388 * not the local bus segment so need a type 1 config cycle
389 *
390 * address:
391 * 23:16 = bus number
392 * 15:11 = slot number (7:3 of devfn)
393 * 10:8 = func number (2:0 of devfn)
394 *
395 * mapaddress:
396 * 3:1 = config cycle (101)
397 * 0 = PCI A1 & A0 from host bus (1)
398 */
399 mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
400 address = (busnr << 16) | (devfn << 8);
401 }
402
403 /*
404 * Set up base0 to see all 512Mbytes of memory space (not
405 * prefetchable), this frees up base1 for re-use by
406 * configuration memory
407 */
408 v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
409 V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE);
410
411 /*
412 * Set up base1/map1 to point into configuration space.
413 */
414 v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_CONFIG_BASE) |
415 V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE);
416 v3_writew(V3_LB_MAP1, mapaddress);
417
418 return PCI_CONFIG_VADDR + address + offset;
419}
420
421static void v3_close_config_window(void)
422{
423 /*
424 * Reassign base1 for use by prefetchable PCI memory
425 */
426 v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
427 V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
428 V3_LB_BASE_ENABLE);
429 v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
430 V3_LB_MAP_TYPE_MEM_MULTIPLE);
431
432 /*
433 * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
434 */
435 v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
436 V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
437}
438
439static int v3_read_config(struct pci_bus *bus, unsigned int devfn, int where,
440 int size, u32 *val)
441{
Arnd Bergmannb7a3f8d2012-09-14 20:16:39 +0000442 void __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned long flags;
444 u32 v;
445
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500446 raw_spin_lock_irqsave(&v3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 addr = v3_open_config_window(bus, devfn, where);
448
449 switch (size) {
450 case 1:
451 v = __raw_readb(addr);
452 break;
453
454 case 2:
455 v = __raw_readw(addr);
456 break;
457
458 default:
459 v = __raw_readl(addr);
460 break;
461 }
462
463 v3_close_config_window();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500464 raw_spin_unlock_irqrestore(&v3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 *val = v;
467 return PCIBIOS_SUCCESSFUL;
468}
469
470static int v3_write_config(struct pci_bus *bus, unsigned int devfn, int where,
471 int size, u32 val)
472{
Arnd Bergmannb7a3f8d2012-09-14 20:16:39 +0000473 void __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 unsigned long flags;
475
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500476 raw_spin_lock_irqsave(&v3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 addr = v3_open_config_window(bus, devfn, where);
478
479 switch (size) {
480 case 1:
481 __raw_writeb((u8)val, addr);
482 __raw_readb(addr);
483 break;
484
485 case 2:
486 __raw_writew((u16)val, addr);
487 __raw_readw(addr);
488 break;
489
490 case 4:
491 __raw_writel(val, addr);
492 __raw_readl(addr);
493 break;
494 }
495
496 v3_close_config_window();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500497 raw_spin_unlock_irqrestore(&v3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 return PCIBIOS_SUCCESSFUL;
500}
501
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100502static struct pci_ops pci_v3_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .read = v3_read_config,
504 .write = v3_write_config,
505};
506
507static struct resource non_mem = {
508 .name = "PCI non-prefetchable",
509 .start = PHYS_PCI_MEM_BASE + PCI_BUS_NONMEM_START,
510 .end = PHYS_PCI_MEM_BASE + PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE - 1,
511 .flags = IORESOURCE_MEM,
512};
513
514static struct resource pre_mem = {
515 .name = "PCI prefetchable",
516 .start = PHYS_PCI_MEM_BASE + PCI_BUS_PREMEM_START,
517 .end = PHYS_PCI_MEM_BASE + PCI_BUS_PREMEM_START + PCI_BUS_PREMEM_SIZE - 1,
518 .flags = IORESOURCE_MEM | IORESOURCE_PREFETCH,
519};
520
Bjorn Helgaas37d15902011-10-28 16:26:16 -0600521static int __init pci_v3_setup_resources(struct pci_sys_data *sys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 if (request_resource(&iomem_resource, &non_mem)) {
524 printk(KERN_ERR "PCI: unable to allocate non-prefetchable "
525 "memory region\n");
526 return -EBUSY;
527 }
528 if (request_resource(&iomem_resource, &pre_mem)) {
529 release_resource(&non_mem);
530 printk(KERN_ERR "PCI: unable to allocate prefetchable "
531 "memory region\n");
532 return -EBUSY;
533 }
534
535 /*
Bjorn Helgaas37d15902011-10-28 16:26:16 -0600536 * the mem resource for this bus
537 * the prefetch mem resource for this bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Bjorn Helgaas9f786d02012-02-23 20:19:01 -0700539 pci_add_resource_offset(&sys->resources, &non_mem, sys->mem_offset);
540 pci_add_resource_offset(&sys->resources, &pre_mem, sys->mem_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 return 1;
543}
544
545/*
546 * These don't seem to be implemented on the Integrator I have, which
547 * means I can't get additional information on the reason for the pm2fb
548 * problems. I suppose I'll just have to mind-meld with the machine. ;)
549 */
Linus Walleij379df272012-11-17 19:24:23 +0100550static void __iomem *ap_syscon_base;
551#define INTEGRATOR_SC_PCIENABLE_OFFSET 0x18
552#define INTEGRATOR_SC_LBFADDR_OFFSET 0x20
553#define INTEGRATOR_SC_LBFCODE_OFFSET 0x24
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555static int
556v3_pci_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
557{
558 unsigned long pc = instruction_pointer(regs);
559 unsigned long instr = *(unsigned long *)pc;
560#if 0
561 char buf[128];
562
563 sprintf(buf, "V3 fault: addr 0x%08lx, FSR 0x%03x, PC 0x%08lx [%08lx] LBFADDR=%08x LBFCODE=%02x ISTAT=%02x\n",
Linus Walleij379df272012-11-17 19:24:23 +0100564 addr, fsr, pc, instr, __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFADDR_OFFSET), __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFCODE_OFFSET) & 255,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 v3_readb(V3_LB_ISTAT));
566 printk(KERN_DEBUG "%s", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#endif
568
569 v3_writeb(V3_LB_ISTAT, 0);
Linus Walleij379df272012-11-17 19:24:23 +0100570 __raw_writel(3, ap_syscon_base + INTEGRATOR_SC_PCIENABLE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
573 * If the instruction being executed was a read,
574 * make it look like it read all-ones.
575 */
576 if ((instr & 0x0c100000) == 0x04100000) {
577 int reg = (instr >> 12) & 15;
578 unsigned long val;
579
580 if (instr & 0x00400000)
581 val = 255;
582 else
583 val = -1;
584
585 regs->uregs[reg] = val;
586 regs->ARM_pc += 4;
587 return 0;
588 }
589
590 if ((instr & 0x0e100090) == 0x00100090) {
591 int reg = (instr >> 12) & 15;
592
593 regs->uregs[reg] = -1;
594 regs->ARM_pc += 4;
595 return 0;
596 }
597
598 return 1;
599}
600
Jeff Garzike8f2af12007-10-26 05:40:25 -0400601static irqreturn_t v3_irq(int dummy, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603#ifdef CONFIG_DEBUG_LL
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700604 struct pt_regs *regs = get_irq_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 unsigned long pc = instruction_pointer(regs);
606 unsigned long instr = *(unsigned long *)pc;
607 char buf[128];
Russell King7c284722008-05-23 19:35:52 +0100608 extern void printascii(const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jeff Garzike8f2af12007-10-26 05:40:25 -0400610 sprintf(buf, "V3 int %d: pc=0x%08lx [%08lx] LBFADDR=%08x LBFCODE=%02x "
611 "ISTAT=%02x\n", IRQ_AP_V3INT, pc, instr,
Linus Walleij379df272012-11-17 19:24:23 +0100612 __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFADDR_OFFSET),
613 __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFCODE_OFFSET) & 255,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 v3_readb(V3_LB_ISTAT));
615 printascii(buf);
616#endif
617
618 v3_writew(V3_PCI_STAT, 0xf000);
619 v3_writeb(V3_LB_ISTAT, 0);
Linus Walleij379df272012-11-17 19:24:23 +0100620 __raw_writel(3, ap_syscon_base + INTEGRATOR_SC_PCIENABLE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622#ifdef CONFIG_DEBUG_LL
623 /*
624 * If the instruction being executed was a read,
625 * make it look like it read all-ones.
626 */
627 if ((instr & 0x0c100000) == 0x04100000) {
628 int reg = (instr >> 16) & 15;
629 sprintf(buf, " reg%d = %08lx\n", reg, regs->uregs[reg]);
630 printascii(buf);
631 }
632#endif
633 return IRQ_HANDLED;
634}
635
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100636static int __init pci_v3_setup(int nr, struct pci_sys_data *sys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int ret = 0;
639
Linus Walleij67c6b2e2013-01-10 10:18:49 +0100640 if (!ap_syscon_base)
641 return -EINVAL;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (nr == 0) {
644 sys->mem_offset = PHYS_PCI_MEM_BASE;
Bjorn Helgaas37d15902011-10-28 16:26:16 -0600645 ret = pci_v3_setup_resources(sys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
648 return ret;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/*
652 * V3_LB_BASE? - local bus address
653 * V3_LB_MAP? - pci bus address
654 */
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100655static void __init pci_v3_preinit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 unsigned long flags;
658 unsigned int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Rob Herringc9d95fb2011-06-28 21:16:13 -0500660 pcibios_min_mem = 0x00100000;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Hook in our fault handler for PCI errors
664 */
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100665 hook_fault_code(4, v3_pci_fault, SIGBUS, 0, "external abort on linefetch");
666 hook_fault_code(6, v3_pci_fault, SIGBUS, 0, "external abort on linefetch");
667 hook_fault_code(8, v3_pci_fault, SIGBUS, 0, "external abort on non-linefetch");
668 hook_fault_code(10, v3_pci_fault, SIGBUS, 0, "external abort on non-linefetch");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500670 raw_spin_lock_irqsave(&v3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /*
673 * Unlock V3 registers, but only if they were previously locked.
674 */
675 if (v3_readw(V3_SYSTEM) & V3_SYSTEM_M_LOCK)
676 v3_writew(V3_SYSTEM, 0xa05f);
677
678 /*
679 * Setup window 0 - PCI non-prefetchable memory
680 * Local: 0x40000000 Bus: 0x00000000 Size: 256MB
681 */
682 v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
683 V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
684 v3_writew(V3_LB_MAP0, v3_addr_to_lb_map(PCI_BUS_NONMEM_START) |
685 V3_LB_MAP_TYPE_MEM);
686
687 /*
688 * Setup window 1 - PCI prefetchable memory
689 * Local: 0x50000000 Bus: 0x10000000 Size: 256MB
690 */
691 v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
692 V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
693 V3_LB_BASE_ENABLE);
694 v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
695 V3_LB_MAP_TYPE_MEM_MULTIPLE);
696
697 /*
698 * Setup window 2 - PCI IO
699 */
700 v3_writel(V3_LB_BASE2, v3_addr_to_lb_base2(PHYS_PCI_IO_BASE) |
701 V3_LB_BASE_ENABLE);
702 v3_writew(V3_LB_MAP2, v3_addr_to_lb_map2(0));
703
704 /*
705 * Disable PCI to host IO cycles
706 */
707 temp = v3_readw(V3_PCI_CFG) & ~V3_PCI_CFG_M_I2O_EN;
708 temp |= V3_PCI_CFG_M_IO_REG_DIS | V3_PCI_CFG_M_IO_DIS;
709 v3_writew(V3_PCI_CFG, temp);
710
711 printk(KERN_DEBUG "FIFO_CFG: %04x FIFO_PRIO: %04x\n",
712 v3_readw(V3_FIFO_CFG), v3_readw(V3_FIFO_PRIORITY));
713
714 /*
715 * Set the V3 FIFO such that writes have higher priority than
716 * reads, and local bus write causes local bus read fifo flush.
717 * Same for PCI.
718 */
719 v3_writew(V3_FIFO_PRIORITY, 0x0a0a);
720
721 /*
722 * Re-lock the system register.
723 */
724 temp = v3_readw(V3_SYSTEM) | V3_SYSTEM_M_LOCK;
725 v3_writew(V3_SYSTEM, temp);
726
727 /*
728 * Clear any error conditions, and enable write errors.
729 */
730 v3_writeb(V3_LB_ISTAT, 0);
731 v3_writew(V3_LB_CFG, v3_readw(V3_LB_CFG) | (1 << 10));
732 v3_writeb(V3_LB_IMASK, 0x28);
Linus Walleij379df272012-11-17 19:24:23 +0100733 __raw_writel(3, ap_syscon_base + INTEGRATOR_SC_PCIENABLE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500735 raw_spin_unlock_irqrestore(&v3_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100738static void __init pci_v3_postinit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 unsigned int pci_cmd;
741
742 pci_cmd = PCI_COMMAND_MEMORY |
743 PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
744
745 v3_writew(V3_PCI_CMD, pci_cmd);
746
747 v3_writeb(V3_LB_ISTAT, ~0x40);
748 v3_writeb(V3_LB_IMASK, 0x68);
749
750#if 0
751 ret = request_irq(IRQ_AP_LBUSTIMEOUT, lb_timeout, 0, "bus timeout", NULL);
752 if (ret)
753 printk(KERN_ERR "PCI: unable to grab local bus timeout "
754 "interrupt: %d\n", ret);
755#endif
Russell King863dab42006-08-28 12:47:05 +0100756
757 register_isa_ports(PHYS_PCI_MEM_BASE, PHYS_PCI_IO_BASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100759
760/*
761 * A small note about bridges and interrupts. The DECchip 21050 (and
762 * later) adheres to the PCI-PCI bridge specification. This says that
763 * the interrupts on the other side of a bridge are swizzled in the
764 * following manner:
765 *
766 * Dev Interrupt Interrupt
767 * Pin on Pin on
768 * Device Connector
769 *
770 * 4 A A
771 * B B
772 * C C
773 * D D
774 *
775 * 5 A B
776 * B C
777 * C D
778 * D A
779 *
780 * 6 A C
781 * B D
782 * C A
783 * D B
784 *
785 * 7 A D
786 * B A
787 * C B
788 * D C
789 *
790 * Where A = pin 1, B = pin 2 and so on and pin=0 = default = A.
791 * Thus, each swizzle is ((pin-1) + (device#-4)) % 4
792 */
793
794/*
795 * This routine handles multiple bridges.
796 */
Linus Walleij86adc392013-02-02 23:16:57 +0100797static u8 __init pci_v3_swizzle(struct pci_dev *dev, u8 *pinp)
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100798{
799 if (*pinp == 0)
800 *pinp = 1;
801
802 return pci_common_swizzle(dev, pinp);
803}
804
805static int irq_tab[4] __initdata = {
806 IRQ_AP_PCIINT0, IRQ_AP_PCIINT1, IRQ_AP_PCIINT2, IRQ_AP_PCIINT3
807};
808
809/*
810 * map the specified device/slot/pin to an IRQ. This works out such
811 * that slot 9 pin 1 is INT0, pin 2 is INT1, and slot 10 pin 1 is INT1.
812 */
Linus Walleij86adc392013-02-02 23:16:57 +0100813static int __init pci_v3_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100814{
815 int intnr = ((slot - 9) + (pin - 1)) & 3;
816
817 return irq_tab[intnr];
818}
819
Linus Walleij86adc392013-02-02 23:16:57 +0100820static struct hw_pci pci_v3 __initdata = {
821 .swizzle = pci_v3_swizzle,
822 .map_irq = pci_v3_map_irq,
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100823 .setup = pci_v3_setup,
824 .nr_controllers = 1,
825 .ops = &pci_v3_ops,
826 .preinit = pci_v3_preinit,
827 .postinit = pci_v3_postinit,
828};
829
Linus Walleij86adc392013-02-02 23:16:57 +0100830static int __init pci_v3_probe(struct platform_device *pdev)
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100831{
Linus Walleij52834562013-04-04 14:02:57 +0200832 int ret;
833
Linus Walleij03884f42013-02-03 00:06:04 +0100834 /* Remap the Integrator system controller */
835 ap_syscon_base = ioremap(INTEGRATOR_SC_BASE, 0x100);
836 if (!ap_syscon_base) {
837 dev_err(&pdev->dev, "unable to remap the AP syscon for PCIv3\n");
838 return -ENODEV;
839 }
840
Linus Walleija5ecbab2013-03-16 21:51:02 +0100841 pci_v3_base = devm_ioremap(&pdev->dev, PHYS_PCI_V3_BASE, SZ_64K);
842 if (!pci_v3_base) {
843 dev_err(&pdev->dev, "unable to remap PCIv3 base\n");
844 return -ENODEV;
845 }
846
Linus Walleij52834562013-04-04 14:02:57 +0200847 ret = devm_request_irq(&pdev->dev, IRQ_AP_V3INT, v3_irq, 0, "V3", NULL);
848 if (ret) {
849 dev_err(&pdev->dev, "unable to grab PCI error interrupt: %d\n",
850 ret);
851 return -ENODEV;
852 }
853
Linus Walleij01ef3102013-03-16 22:03:41 +0100854 vga_base = (unsigned long)PCI_MEMORY_VADDR;
Linus Walleij86adc392013-02-02 23:16:57 +0100855 pci_common_init(&pci_v3);
Linus Walleij52834562013-04-04 14:02:57 +0200856
Linus Walleijf4bc4f02013-01-29 17:14:18 +0100857 return 0;
858}
859
Linus Walleij86adc392013-02-02 23:16:57 +0100860static struct platform_driver pci_v3_driver = {
861 .driver = {
862 .name = "pci-v3",
863 },
864};
865
866static int __init pci_v3_init(void)
867{
868 return platform_driver_probe(&pci_v3_driver, pci_v3_probe);
869}
870
871subsys_initcall(pci_v3_init);