blob: 01c604a5ac36821c0186e50a095301cb15ac9886 [file] [log] [blame]
David Daneye8635b42009-04-23 17:44:38 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
David Daney01a62212009-06-29 17:18:51 -07006 * Copyright (C) 2005-2009 Cavium Networks
David Daneye8635b42009-04-23 17:44:38 -07007 */
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/pci.h>
11#include <linux/interrupt.h>
12#include <linux/time.h>
13#include <linux/delay.h>
Ralf Baechlef65aad42012-10-17 00:39:09 +020014#include <linux/platform_device.h>
David Daneyb93b2ab2010-10-01 13:27:34 -070015#include <linux/swiotlb.h>
David Daneye8635b42009-04-23 17:44:38 -070016
17#include <asm/time.h>
18
19#include <asm/octeon/octeon.h>
20#include <asm/octeon/cvmx-npi-defs.h>
21#include <asm/octeon/cvmx-pci-defs.h>
David Daney01a62212009-06-29 17:18:51 -070022#include <asm/octeon/pci-octeon.h>
David Daneye8635b42009-04-23 17:44:38 -070023
David Daneyb93b2ab2010-10-01 13:27:34 -070024#include <dma-coherence.h>
25
David Daneye8635b42009-04-23 17:44:38 -070026#define USE_OCTEON_INTERNAL_ARBITER
27
28/*
29 * Octeon's PCI controller uses did=3, subdid=2 for PCI IO
30 * addresses. Use PCI endian swapping 1 so no address swapping is
31 * necessary. The Linux io routines will endian swap the data.
32 */
Ralf Baechle70342282013-01-22 12:59:30 +010033#define OCTEON_PCI_IOSPACE_BASE 0x80011a0400000000ull
34#define OCTEON_PCI_IOSPACE_SIZE (1ull<<32)
David Daneye8635b42009-04-23 17:44:38 -070035
36/* Octeon't PCI controller uses did=3, subdid=3 for PCI memory. */
37#define OCTEON_PCI_MEMSPACE_OFFSET (0x00011b0000000000ull)
38
David Daneyb93b2ab2010-10-01 13:27:34 -070039u64 octeon_bar1_pci_phys;
40
David Daneye8635b42009-04-23 17:44:38 -070041/**
42 * This is the bit decoding used for the Octeon PCI controller addresses
43 */
44union octeon_pci_address {
45 uint64_t u64;
46 struct {
47 uint64_t upper:2;
48 uint64_t reserved:13;
49 uint64_t io:1;
50 uint64_t did:5;
51 uint64_t subdid:3;
52 uint64_t reserved2:4;
53 uint64_t endian_swap:2;
54 uint64_t reserved3:10;
55 uint64_t bus:8;
56 uint64_t dev:5;
57 uint64_t func:3;
58 uint64_t reg:8;
59 } s;
60};
61
Andi Kleen3cf5ae62012-10-04 17:11:35 -070062int __initconst (*octeon_pcibios_map_irq)(const struct pci_dev *dev,
David Daney01a62212009-06-29 17:18:51 -070063 u8 slot, u8 pin);
64enum octeon_dma_bar_type octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_INVALID;
65
66/**
67 * Map a PCI device to the appropriate interrupt line
68 *
69 * @dev: The Linux PCI device structure for the device to map
70 * @slot: The slot number for this device on __BUS 0__. Linux
Ralf Baechle70342282013-01-22 12:59:30 +010071 * enumerates through all the bridges and figures out the
72 * slot on Bus 0 where this device eventually hooks to.
David Daney01a62212009-06-29 17:18:51 -070073 * @pin: The PCI interrupt pin read from the device, then swizzled
Ralf Baechle70342282013-01-22 12:59:30 +010074 * as it goes through each bridge.
David Daney01a62212009-06-29 17:18:51 -070075 * Returns Interrupt number for the device
76 */
77int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
78{
79 if (octeon_pcibios_map_irq)
80 return octeon_pcibios_map_irq(dev, slot, pin);
81 else
82 panic("octeon_pcibios_map_irq not set.");
83}
84
85
86/*
87 * Called to perform platform specific PCI setup
88 */
89int pcibios_plat_dev_init(struct pci_dev *dev)
90{
91 uint16_t config;
92 uint32_t dconfig;
93 int pos;
94 /*
95 * Force the Cache line setting to 64 bytes. The standard
96 * Linux bus scan doesn't seem to set it. Octeon really has
97 * 128 byte lines, but Intel bridges get really upset if you
98 * try and set values above 64 bytes. Value is specified in
99 * 32bit words.
100 */
101 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 64 / 4);
102 /* Set latency timers for all devices */
David Daney53efc982011-12-08 12:26:28 +0000103 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
David Daney01a62212009-06-29 17:18:51 -0700104
105 /* Enable reporting System errors and parity errors on all devices */
106 /* Enable parity checking and error reporting */
107 pci_read_config_word(dev, PCI_COMMAND, &config);
108 config |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
109 pci_write_config_word(dev, PCI_COMMAND, config);
110
111 if (dev->subordinate) {
112 /* Set latency timers on sub bridges */
David Daney53efc982011-12-08 12:26:28 +0000113 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 64);
David Daney01a62212009-06-29 17:18:51 -0700114 /* More bridge error detection */
115 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &config);
116 config |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
117 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, config);
118 }
119
120 /* Enable the PCIe normal error reporting */
Jiang Liu39a36122012-07-24 17:20:14 +0800121 config = PCI_EXP_DEVCTL_CERE; /* Correctable Error Reporting */
122 config |= PCI_EXP_DEVCTL_NFERE; /* Non-Fatal Error Reporting */
Ralf Baechle70342282013-01-22 12:59:30 +0100123 config |= PCI_EXP_DEVCTL_FERE; /* Fatal Error Reporting */
124 config |= PCI_EXP_DEVCTL_URRE; /* Unsupported Request */
Jiang Liu39a36122012-07-24 17:20:14 +0800125 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, config);
David Daney01a62212009-06-29 17:18:51 -0700126
127 /* Find the Advanced Error Reporting capability */
128 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
129 if (pos) {
130 /* Clear Uncorrectable Error Status */
131 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
132 &dconfig);
133 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
134 dconfig);
135 /* Enable reporting of all uncorrectable errors */
136 /* Uncorrectable Error Mask - turned on bits disable errors */
137 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, 0);
138 /*
139 * Leave severity at HW default. This only controls if
140 * errors are reported as uncorrectable or
141 * correctable, not if the error is reported.
142 */
143 /* PCI_ERR_UNCOR_SEVER - Uncorrectable Error Severity */
144 /* Clear Correctable Error Status */
145 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &dconfig);
146 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, dconfig);
147 /* Enable reporting of all correctable errors */
148 /* Correctable Error Mask - turned on bits disable errors */
149 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, 0);
150 /* Advanced Error Capabilities */
151 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &dconfig);
152 /* ECRC Generation Enable */
153 if (config & PCI_ERR_CAP_ECRC_GENC)
154 config |= PCI_ERR_CAP_ECRC_GENE;
155 /* ECRC Check Enable */
156 if (config & PCI_ERR_CAP_ECRC_CHKC)
157 config |= PCI_ERR_CAP_ECRC_CHKE;
158 pci_write_config_dword(dev, pos + PCI_ERR_CAP, dconfig);
159 /* PCI_ERR_HEADER_LOG - Header Log Register (16 bytes) */
160 /* Report all errors to the root complex */
161 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND,
162 PCI_ERR_ROOT_CMD_COR_EN |
163 PCI_ERR_ROOT_CMD_NONFATAL_EN |
164 PCI_ERR_ROOT_CMD_FATAL_EN);
165 /* Clear the Root status register */
166 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &dconfig);
167 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, dconfig);
168 }
169
David Daneyb93b2ab2010-10-01 13:27:34 -0700170 dev->dev.archdata.dma_ops = octeon_pci_dma_map_ops;
171
David Daney01a62212009-06-29 17:18:51 -0700172 return 0;
173}
174
David Daneye8635b42009-04-23 17:44:38 -0700175/**
176 * Return the mapping of PCI device number to IRQ line. Each
177 * character in the return string represents the interrupt
178 * line for the device at that position. Device 1 maps to the
179 * first character, etc. The characters A-D are used for PCI
180 * interrupts.
181 *
182 * Returns PCI interrupt mapping
183 */
184const char *octeon_get_pci_interrupts(void)
185{
186 /*
187 * Returning an empty string causes the interrupts to be
188 * routed based on the PCI specification. From the PCI spec:
189 *
190 * INTA# of Device Number 0 is connected to IRQW on the system
191 * board. (Device Number has no significance regarding being
192 * located on the system board or in a connector.) INTA# of
193 * Device Number 1 is connected to IRQX on the system
194 * board. INTA# of Device Number 2 is connected to IRQY on the
195 * system board. INTA# of Device Number 3 is connected to IRQZ
196 * on the system board. The table below describes how each
197 * agent's INTx# lines are connected to the system board
198 * interrupt lines. The following equation can be used to
199 * determine to which INTx# signal on the system board a given
200 * device's INTx# line(s) is connected.
201 *
202 * MB = (D + I) MOD 4 MB = System board Interrupt (IRQW = 0,
203 * IRQX = 1, IRQY = 2, and IRQZ = 3) D = Device Number I =
204 * Interrupt Number (INTA# = 0, INTB# = 1, INTC# = 2, and
205 * INTD# = 3)
206 */
207 switch (octeon_bootinfo->board_type) {
208 case CVMX_BOARD_TYPE_NAO38:
209 /* This is really the NAC38 */
210 return "AAAAADABAAAAAAAAAAAAAAAAAAAAAAAA";
David Daneye8635b42009-04-23 17:44:38 -0700211 case CVMX_BOARD_TYPE_EBH3100:
212 case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
213 case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
214 return "AAABAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
215 case CVMX_BOARD_TYPE_BBGW_REF:
216 return "AABCD";
Roel Kluin2fe06262010-01-20 00:59:27 +0100217 case CVMX_BOARD_TYPE_THUNDER:
218 case CVMX_BOARD_TYPE_EBH3000:
David Daneye8635b42009-04-23 17:44:38 -0700219 default:
220 return "";
221 }
222}
223
224/**
225 * Map a PCI device to the appropriate interrupt line
226 *
227 * @dev: The Linux PCI device structure for the device to map
228 * @slot: The slot number for this device on __BUS 0__. Linux
Ralf Baechle70342282013-01-22 12:59:30 +0100229 * enumerates through all the bridges and figures out the
230 * slot on Bus 0 where this device eventually hooks to.
David Daneye8635b42009-04-23 17:44:38 -0700231 * @pin: The PCI interrupt pin read from the device, then swizzled
Ralf Baechle70342282013-01-22 12:59:30 +0100232 * as it goes through each bridge.
David Daneye8635b42009-04-23 17:44:38 -0700233 * Returns Interrupt number for the device
234 */
235int __init octeon_pci_pcibios_map_irq(const struct pci_dev *dev,
236 u8 slot, u8 pin)
237{
238 int irq_num;
239 const char *interrupts;
240 int dev_num;
241
242 /* Get the board specific interrupt mapping */
243 interrupts = octeon_get_pci_interrupts();
244
245 dev_num = dev->devfn >> 3;
246 if (dev_num < strlen(interrupts))
247 irq_num = ((interrupts[dev_num] - 'A' + pin - 1) & 3) +
248 OCTEON_IRQ_PCI_INT0;
249 else
250 irq_num = ((slot + pin - 3) & 3) + OCTEON_IRQ_PCI_INT0;
251 return irq_num;
252}
253
254
David Daney01a62212009-06-29 17:18:51 -0700255/*
David Daneye8635b42009-04-23 17:44:38 -0700256 * Read a value from configuration space
David Daneye8635b42009-04-23 17:44:38 -0700257 */
258static int octeon_read_config(struct pci_bus *bus, unsigned int devfn,
259 int reg, int size, u32 *val)
260{
261 union octeon_pci_address pci_addr;
262
263 pci_addr.u64 = 0;
264 pci_addr.s.upper = 2;
265 pci_addr.s.io = 1;
266 pci_addr.s.did = 3;
267 pci_addr.s.subdid = 1;
268 pci_addr.s.endian_swap = 1;
269 pci_addr.s.bus = bus->number;
270 pci_addr.s.dev = devfn >> 3;
271 pci_addr.s.func = devfn & 0x7;
272 pci_addr.s.reg = reg;
273
David Daneye8635b42009-04-23 17:44:38 -0700274 switch (size) {
275 case 4:
276 *val = le32_to_cpu(cvmx_read64_uint32(pci_addr.u64));
277 return PCIBIOS_SUCCESSFUL;
278 case 2:
279 *val = le16_to_cpu(cvmx_read64_uint16(pci_addr.u64));
280 return PCIBIOS_SUCCESSFUL;
281 case 1:
282 *val = cvmx_read64_uint8(pci_addr.u64);
283 return PCIBIOS_SUCCESSFUL;
284 }
285 return PCIBIOS_FUNC_NOT_SUPPORTED;
286}
287
288
David Daney01a62212009-06-29 17:18:51 -0700289/*
David Daneye8635b42009-04-23 17:44:38 -0700290 * Write a value to PCI configuration space
David Daneye8635b42009-04-23 17:44:38 -0700291 */
292static int octeon_write_config(struct pci_bus *bus, unsigned int devfn,
293 int reg, int size, u32 val)
294{
295 union octeon_pci_address pci_addr;
296
297 pci_addr.u64 = 0;
298 pci_addr.s.upper = 2;
299 pci_addr.s.io = 1;
300 pci_addr.s.did = 3;
301 pci_addr.s.subdid = 1;
302 pci_addr.s.endian_swap = 1;
303 pci_addr.s.bus = bus->number;
304 pci_addr.s.dev = devfn >> 3;
305 pci_addr.s.func = devfn & 0x7;
306 pci_addr.s.reg = reg;
307
David Daneye8635b42009-04-23 17:44:38 -0700308 switch (size) {
309 case 4:
310 cvmx_write64_uint32(pci_addr.u64, cpu_to_le32(val));
311 return PCIBIOS_SUCCESSFUL;
312 case 2:
313 cvmx_write64_uint16(pci_addr.u64, cpu_to_le16(val));
314 return PCIBIOS_SUCCESSFUL;
315 case 1:
316 cvmx_write64_uint8(pci_addr.u64, val);
317 return PCIBIOS_SUCCESSFUL;
318 }
319 return PCIBIOS_FUNC_NOT_SUPPORTED;
320}
321
322
323static struct pci_ops octeon_pci_ops = {
Rob Herring7b097772015-01-09 20:34:36 -0600324 .read = octeon_read_config,
325 .write = octeon_write_config,
David Daneye8635b42009-04-23 17:44:38 -0700326};
327
328static struct resource octeon_pci_mem_resource = {
329 .start = 0,
330 .end = 0,
331 .name = "Octeon PCI MEM",
332 .flags = IORESOURCE_MEM,
333};
334
335/*
336 * PCI ports must be above 16KB so the ISA bus filtering in the PCI-X to PCI
337 * bridge
338 */
339static struct resource octeon_pci_io_resource = {
340 .start = 0x4000,
341 .end = OCTEON_PCI_IOSPACE_SIZE - 1,
342 .name = "Octeon PCI IO",
343 .flags = IORESOURCE_IO,
344};
345
346static struct pci_controller octeon_pci_controller = {
347 .pci_ops = &octeon_pci_ops,
348 .mem_resource = &octeon_pci_mem_resource,
349 .mem_offset = OCTEON_PCI_MEMSPACE_OFFSET,
350 .io_resource = &octeon_pci_io_resource,
351 .io_offset = 0,
352 .io_map_base = OCTEON_PCI_IOSPACE_BASE,
353};
354
355
David Daney01a62212009-06-29 17:18:51 -0700356/*
David Daneye8635b42009-04-23 17:44:38 -0700357 * Low level initialize the Octeon PCI controller
David Daneye8635b42009-04-23 17:44:38 -0700358 */
359static void octeon_pci_initialize(void)
360{
361 union cvmx_pci_cfg01 cfg01;
362 union cvmx_npi_ctl_status ctl_status;
363 union cvmx_pci_ctl_status_2 ctl_status_2;
364 union cvmx_pci_cfg19 cfg19;
365 union cvmx_pci_cfg16 cfg16;
366 union cvmx_pci_cfg22 cfg22;
367 union cvmx_pci_cfg56 cfg56;
368
369 /* Reset the PCI Bus */
370 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x1);
371 cvmx_read_csr(CVMX_CIU_SOFT_PRST);
372
373 udelay(2000); /* Hold PCI reset for 2 ms */
374
375 ctl_status.u64 = 0; /* cvmx_read_csr(CVMX_NPI_CTL_STATUS); */
376 ctl_status.s.max_word = 1;
377 ctl_status.s.timer = 1;
378 cvmx_write_csr(CVMX_NPI_CTL_STATUS, ctl_status.u64);
379
380 /* Deassert PCI reset and advertize PCX Host Mode Device Capability
381 (64b) */
382 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x4);
383 cvmx_read_csr(CVMX_CIU_SOFT_PRST);
384
385 udelay(2000); /* Wait 2 ms after deasserting PCI reset */
386
387 ctl_status_2.u32 = 0;
388 ctl_status_2.s.tsr_hwm = 1; /* Initializes to 0. Must be set
389 before any PCI reads. */
390 ctl_status_2.s.bar2pres = 1; /* Enable BAR2 */
391 ctl_status_2.s.bar2_enb = 1;
392 ctl_status_2.s.bar2_cax = 1; /* Don't use L2 */
393 ctl_status_2.s.bar2_esx = 1;
394 ctl_status_2.s.pmo_amod = 1; /* Round robin priority */
395 if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG) {
396 /* BAR1 hole */
397 ctl_status_2.s.bb1_hole = OCTEON_PCI_BAR1_HOLE_BITS;
398 ctl_status_2.s.bb1_siz = 1; /* BAR1 is 2GB */
399 ctl_status_2.s.bb_ca = 1; /* Don't use L2 with big bars */
400 ctl_status_2.s.bb_es = 1; /* Big bar in byte swap mode */
Ralf Baechle70342282013-01-22 12:59:30 +0100401 ctl_status_2.s.bb1 = 1; /* BAR1 is big */
402 ctl_status_2.s.bb0 = 1; /* BAR0 is big */
David Daneye8635b42009-04-23 17:44:38 -0700403 }
404
405 octeon_npi_write32(CVMX_NPI_PCI_CTL_STATUS_2, ctl_status_2.u32);
406 udelay(2000); /* Wait 2 ms before doing PCI reads */
407
408 ctl_status_2.u32 = octeon_npi_read32(CVMX_NPI_PCI_CTL_STATUS_2);
409 pr_notice("PCI Status: %s %s-bit\n",
410 ctl_status_2.s.ap_pcix ? "PCI-X" : "PCI",
411 ctl_status_2.s.ap_64ad ? "64" : "32");
412
413 if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
414 union cvmx_pci_cnt_reg cnt_reg_start;
415 union cvmx_pci_cnt_reg cnt_reg_end;
416 unsigned long cycles, pci_clock;
417
418 cnt_reg_start.u64 = cvmx_read_csr(CVMX_NPI_PCI_CNT_REG);
419 cycles = read_c0_cvmcount();
420 udelay(1000);
421 cnt_reg_end.u64 = cvmx_read_csr(CVMX_NPI_PCI_CNT_REG);
422 cycles = read_c0_cvmcount() - cycles;
423 pci_clock = (cnt_reg_end.s.pcicnt - cnt_reg_start.s.pcicnt) /
424 (cycles / (mips_hpt_frequency / 1000000));
425 pr_notice("PCI Clock: %lu MHz\n", pci_clock);
426 }
427
428 /*
429 * TDOMC must be set to one in PCI mode. TDOMC should be set to 4
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300430 * in PCI-X mode to allow four outstanding splits. Otherwise,
David Daneye8635b42009-04-23 17:44:38 -0700431 * should not change from its reset value. Don't write PCI_CFG19
432 * in PCI mode (0x82000001 reset value), write it to 0x82000004
433 * after PCI-X mode is known. MRBCI,MDWE,MDRE -> must be zero.
434 * MRBCM -> must be one.
435 */
436 if (ctl_status_2.s.ap_pcix) {
437 cfg19.u32 = 0;
438 /*
439 * Target Delayed/Split request outstanding maximum
440 * count. [1..31] and 0=32. NOTE: If the user
441 * programs these bits beyond the Designed Maximum
442 * outstanding count, then the designed maximum table
Ralf Baechle70342282013-01-22 12:59:30 +0100443 * depth will be used instead. No additional
David Daneye8635b42009-04-23 17:44:38 -0700444 * Deferred/Split transactions will be accepted if
445 * this outstanding maximum count is
446 * reached. Furthermore, no additional deferred/split
447 * transactions will be accepted if the I/O delay/ I/O
448 * Split Request outstanding maximum is reached.
449 */
450 cfg19.s.tdomc = 4;
451 /*
452 * Master Deferred Read Request Outstanding Max Count
Ralf Baechle70342282013-01-22 12:59:30 +0100453 * (PCI only). CR4C[26:24] Max SAC cycles MAX DAC
David Daneye8635b42009-04-23 17:44:38 -0700454 * cycles 000 8 4 001 1 0 010 2 1 011 3 1 100 4 2 101
455 * 5 2 110 6 3 111 7 3 For example, if these bits are
456 * programmed to 100, the core can support 2 DAC
457 * cycles, 4 SAC cycles or a combination of 1 DAC and
458 * 2 SAC cycles. NOTE: For the PCI-X maximum
459 * outstanding split transactions, refer to
460 * CRE0[22:20].
461 */
462 cfg19.s.mdrrmc = 2;
463 /*
464 * Master Request (Memory Read) Byte Count/Byte Enable
465 * select. 0 = Byte Enables valid. In PCI mode, a
466 * burst transaction cannot be performed using Memory
467 * Read command=4?h6. 1 = DWORD Byte Count valid
468 * (default). In PCI Mode, the memory read byte
469 * enables are automatically generated by the
470 * core. Note: N3 Master Request transaction sizes are
471 * always determined through the
472 * am_attr[<35:32>|<7:0>] field.
473 */
474 cfg19.s.mrbcm = 1;
475 octeon_npi_write32(CVMX_NPI_PCI_CFG19, cfg19.u32);
476 }
477
478
479 cfg01.u32 = 0;
480 cfg01.s.msae = 1; /* Memory Space Access Enable */
481 cfg01.s.me = 1; /* Master Enable */
482 cfg01.s.pee = 1; /* PERR# Enable */
483 cfg01.s.see = 1; /* System Error Enable */
484 cfg01.s.fbbe = 1; /* Fast Back to Back Transaction Enable */
485
486 octeon_npi_write32(CVMX_NPI_PCI_CFG01, cfg01.u32);
487
488#ifdef USE_OCTEON_INTERNAL_ARBITER
489 /*
490 * When OCTEON is a PCI host, most systems will use OCTEON's
491 * internal arbiter, so must enable it before any PCI/PCI-X
492 * traffic can occur.
493 */
494 {
495 union cvmx_npi_pci_int_arb_cfg pci_int_arb_cfg;
496
497 pci_int_arb_cfg.u64 = 0;
498 pci_int_arb_cfg.s.en = 1; /* Internal arbiter enable */
499 cvmx_write_csr(CVMX_NPI_PCI_INT_ARB_CFG, pci_int_arb_cfg.u64);
500 }
David Daney01a62212009-06-29 17:18:51 -0700501#endif /* USE_OCTEON_INTERNAL_ARBITER */
David Daneye8635b42009-04-23 17:44:38 -0700502
503 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300504 * Preferably written to 1 to set MLTD. [RDSATI,TRTAE,
David Daneye8635b42009-04-23 17:44:38 -0700505 * TWTAE,TMAE,DPPMR -> must be zero. TILT -> must not be set to
506 * 1..7.
507 */
508 cfg16.u32 = 0;
509 cfg16.s.mltd = 1; /* Master Latency Timer Disable */
510 octeon_npi_write32(CVMX_NPI_PCI_CFG16, cfg16.u32);
511
512 /*
513 * Should be written to 0x4ff00. MTTV -> must be zero.
514 * FLUSH -> must be 1. MRV -> should be 0xFF.
515 */
516 cfg22.u32 = 0;
517 /* Master Retry Value [1..255] and 0=infinite */
518 cfg22.s.mrv = 0xff;
519 /*
520 * AM_DO_FLUSH_I control NOTE: This bit MUST BE ONE for proper
521 * N3K operation.
522 */
523 cfg22.s.flush = 1;
524 octeon_npi_write32(CVMX_NPI_PCI_CFG22, cfg22.u32);
525
526 /*
527 * MOST Indicates the maximum number of outstanding splits (in -1
528 * notation) when OCTEON is in PCI-X mode. PCI-X performance is
529 * affected by the MOST selection. Should generally be written
530 * with one of 0x3be807, 0x2be807, 0x1be807, or 0x0be807,
531 * depending on the desired MOST of 3, 2, 1, or 0, respectively.
532 */
533 cfg56.u32 = 0;
534 cfg56.s.pxcid = 7; /* RO - PCI-X Capability ID */
535 cfg56.s.ncp = 0xe8; /* RO - Next Capability Pointer */
536 cfg56.s.dpere = 1; /* Data Parity Error Recovery Enable */
537 cfg56.s.roe = 1; /* Relaxed Ordering Enable */
538 cfg56.s.mmbc = 1; /* Maximum Memory Byte Count
539 [0=512B,1=1024B,2=2048B,3=4096B] */
540 cfg56.s.most = 3; /* Maximum outstanding Split transactions [0=1
541 .. 7=32] */
542
543 octeon_npi_write32(CVMX_NPI_PCI_CFG56, cfg56.u32);
544
545 /*
546 * Affects PCI performance when OCTEON services reads to its
Ralf Baechle70342282013-01-22 12:59:30 +0100547 * BAR1/BAR2. Refer to Section 10.6.1. The recommended values are
David Daneye8635b42009-04-23 17:44:38 -0700548 * 0x22, 0x33, and 0x33 for PCI_READ_CMD_6, PCI_READ_CMD_C, and
549 * PCI_READ_CMD_E, respectively. Unfortunately due to errata DDR-700,
550 * these values need to be changed so they won't possibly prefetch off
551 * of the end of memory if PCI is DMAing a buffer at the end of
552 * memory. Note that these values differ from their reset values.
553 */
554 octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_6, 0x21);
555 octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_C, 0x31);
556 octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_E, 0x31);
557}
558
559
David Daney01a62212009-06-29 17:18:51 -0700560/*
David Daneye8635b42009-04-23 17:44:38 -0700561 * Initialize the Octeon PCI controller
David Daneye8635b42009-04-23 17:44:38 -0700562 */
563static int __init octeon_pci_setup(void)
564{
565 union cvmx_npi_mem_access_subidx mem_access;
566 int index;
567
568 /* Only these chips have PCI */
569 if (octeon_has_feature(OCTEON_FEATURE_PCIE))
570 return 0;
571
572 /* Point pcibios_map_irq() to the PCI version of it */
573 octeon_pcibios_map_irq = octeon_pci_pcibios_map_irq;
574
575 /* Only use the big bars on chips that support it */
576 if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||
577 OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2) ||
578 OCTEON_IS_MODEL(OCTEON_CN38XX_PASS1))
579 octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_SMALL;
580 else
581 octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_BIG;
582
David Daneye8635b42009-04-23 17:44:38 -0700583 if (!octeon_is_pci_host()) {
584 pr_notice("Not in host mode, PCI Controller not initialized\n");
585 return 0;
586 }
587
Aaro Koskinend8b74272013-07-25 20:26:48 +0300588 /* PCI I/O and PCI MEM values */
589 set_io_port_base(OCTEON_PCI_IOSPACE_BASE);
590 ioport_resource.start = 0;
591 ioport_resource.end = OCTEON_PCI_IOSPACE_SIZE - 1;
592
David Daneye8635b42009-04-23 17:44:38 -0700593 pr_notice("%s Octeon big bar support\n",
594 (octeon_dma_bar_type ==
595 OCTEON_DMA_BAR_TYPE_BIG) ? "Enabling" : "Disabling");
596
597 octeon_pci_initialize();
598
599 mem_access.u64 = 0;
600 mem_access.s.esr = 1; /* Endian-Swap on read. */
601 mem_access.s.esw = 1; /* Endian-Swap on write. */
602 mem_access.s.nsr = 0; /* No-Snoop on read. */
603 mem_access.s.nsw = 0; /* No-Snoop on write. */
604 mem_access.s.ror = 0; /* Relax Read on read. */
605 mem_access.s.row = 0; /* Relax Order on write. */
606 mem_access.s.ba = 0; /* PCI Address bits [63:36]. */
607 cvmx_write_csr(CVMX_NPI_MEM_ACCESS_SUBID3, mem_access.u64);
608
609 /*
610 * Remap the Octeon BAR 2 above all 32 bit devices
611 * (0x8000000000ul). This is done here so it is remapped
612 * before the readl()'s below. We don't want BAR2 overlapping
613 * with BAR0/BAR1 during these reads.
614 */
David Daneyb93b2ab2010-10-01 13:27:34 -0700615 octeon_npi_write32(CVMX_NPI_PCI_CFG08,
616 (u32)(OCTEON_BAR2_PCI_ADDRESS & 0xffffffffull));
617 octeon_npi_write32(CVMX_NPI_PCI_CFG09,
618 (u32)(OCTEON_BAR2_PCI_ADDRESS >> 32));
David Daneye8635b42009-04-23 17:44:38 -0700619
620 if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG) {
621 /* Remap the Octeon BAR 0 to 0-2GB */
622 octeon_npi_write32(CVMX_NPI_PCI_CFG04, 0);
623 octeon_npi_write32(CVMX_NPI_PCI_CFG05, 0);
624
625 /*
626 * Remap the Octeon BAR 1 to map 2GB-4GB (minus the
627 * BAR 1 hole).
628 */
629 octeon_npi_write32(CVMX_NPI_PCI_CFG06, 2ul << 30);
630 octeon_npi_write32(CVMX_NPI_PCI_CFG07, 0);
631
David Daneyb93b2ab2010-10-01 13:27:34 -0700632 /* BAR1 movable mappings set for identity mapping */
633 octeon_bar1_pci_phys = 0x80000000ull;
634 for (index = 0; index < 32; index++) {
635 union cvmx_pci_bar1_indexx bar1_index;
636
637 bar1_index.u32 = 0;
638 /* Address bits[35:22] sent to L2C */
639 bar1_index.s.addr_idx =
640 (octeon_bar1_pci_phys >> 22) + index;
641 /* Don't put PCI accesses in L2. */
642 bar1_index.s.ca = 1;
643 /* Endian Swap Mode */
644 bar1_index.s.end_swp = 1;
645 /* Set '1' when the selected address range is valid. */
646 bar1_index.s.addr_v = 1;
647 octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
648 bar1_index.u32);
649 }
650
David Daneye8635b42009-04-23 17:44:38 -0700651 /* Devices go after BAR1 */
652 octeon_pci_mem_resource.start =
653 OCTEON_PCI_MEMSPACE_OFFSET + (4ul << 30) -
654 (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
655 octeon_pci_mem_resource.end =
656 octeon_pci_mem_resource.start + (1ul << 30);
657 } else {
658 /* Remap the Octeon BAR 0 to map 128MB-(128MB+4KB) */
659 octeon_npi_write32(CVMX_NPI_PCI_CFG04, 128ul << 20);
660 octeon_npi_write32(CVMX_NPI_PCI_CFG05, 0);
661
662 /* Remap the Octeon BAR 1 to map 0-128MB */
663 octeon_npi_write32(CVMX_NPI_PCI_CFG06, 0);
664 octeon_npi_write32(CVMX_NPI_PCI_CFG07, 0);
665
David Daneyb93b2ab2010-10-01 13:27:34 -0700666 /* BAR1 movable regions contiguous to cover the swiotlb */
667 octeon_bar1_pci_phys =
668 virt_to_phys(octeon_swiotlb) & ~((1ull << 22) - 1);
669
670 for (index = 0; index < 32; index++) {
671 union cvmx_pci_bar1_indexx bar1_index;
672
673 bar1_index.u32 = 0;
674 /* Address bits[35:22] sent to L2C */
675 bar1_index.s.addr_idx =
676 (octeon_bar1_pci_phys >> 22) + index;
677 /* Don't put PCI accesses in L2. */
678 bar1_index.s.ca = 1;
679 /* Endian Swap Mode */
680 bar1_index.s.end_swp = 1;
681 /* Set '1' when the selected address range is valid. */
682 bar1_index.s.addr_v = 1;
683 octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
684 bar1_index.u32);
685 }
686
David Daneye8635b42009-04-23 17:44:38 -0700687 /* Devices go after BAR0 */
688 octeon_pci_mem_resource.start =
689 OCTEON_PCI_MEMSPACE_OFFSET + (128ul << 20) +
690 (4ul << 10);
691 octeon_pci_mem_resource.end =
692 octeon_pci_mem_resource.start + (1ul << 30);
693 }
694
695 register_pci_controller(&octeon_pci_controller);
696
697 /*
698 * Clear any errors that might be pending from before the bus
699 * was setup properly.
700 */
701 cvmx_write_csr(CVMX_NPI_PCI_INT_SUM2, -1);
David Daneyb93b2ab2010-10-01 13:27:34 -0700702
David Daneye1ced092012-11-15 13:58:59 -0800703 if (IS_ERR(platform_device_register_simple("octeon_pci_edac",
704 -1, NULL, 0)))
Masanari Iida6774def2014-11-05 22:26:48 +0900705 pr_err("Registration of co_pci_edac failed!\n");
Ralf Baechlef65aad42012-10-17 00:39:09 +0200706
David Daneyb93b2ab2010-10-01 13:27:34 -0700707 octeon_pci_dma_init();
708
David Daneye8635b42009-04-23 17:44:38 -0700709 return 0;
710}
711
712arch_initcall(octeon_pci_setup);