1
2
3
4
5
6
7
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>
14#include <linux/platform_device.h>
15#include <linux/swiotlb.h>
16
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>
22#include <asm/octeon/pci-octeon.h>
23
24#define USE_OCTEON_INTERNAL_ARBITER
25
26
27
28
29
30
31#define OCTEON_PCI_IOSPACE_BASE 0x80011a0400000000ull
32#define OCTEON_PCI_IOSPACE_SIZE (1ull<<32)
33
34
35#define OCTEON_PCI_MEMSPACE_OFFSET (0x00011b0000000000ull)
36
37u64 octeon_bar1_pci_phys;
38
39
40
41
42union octeon_pci_address {
43 uint64_t u64;
44 struct {
45 uint64_t upper:2;
46 uint64_t reserved:13;
47 uint64_t io:1;
48 uint64_t did:5;
49 uint64_t subdid:3;
50 uint64_t reserved2:4;
51 uint64_t endian_swap:2;
52 uint64_t reserved3:10;
53 uint64_t bus:8;
54 uint64_t dev:5;
55 uint64_t func:3;
56 uint64_t reg:8;
57 } s;
58};
59
60int (*octeon_pcibios_map_irq)(const struct pci_dev *dev, u8 slot, u8 pin);
61enum octeon_dma_bar_type octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_INVALID;
62
63
64
65
66
67
68
69
70
71
72
73
74int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
75{
76 if (octeon_pcibios_map_irq)
77 return octeon_pcibios_map_irq(dev, slot, pin);
78 else
79 panic("octeon_pcibios_map_irq not set.");
80}
81
82
83
84
85
86int pcibios_plat_dev_init(struct pci_dev *dev)
87{
88 uint16_t config;
89 uint32_t dconfig;
90 int pos;
91
92
93
94
95
96
97
98 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 64 / 4);
99
100 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
101
102
103
104 pci_read_config_word(dev, PCI_COMMAND, &config);
105 config |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
106 pci_write_config_word(dev, PCI_COMMAND, config);
107
108 if (dev->subordinate) {
109
110 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 64);
111
112 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &config);
113 config |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
114 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, config);
115 }
116
117
118 config = PCI_EXP_DEVCTL_CERE;
119 config |= PCI_EXP_DEVCTL_NFERE;
120 config |= PCI_EXP_DEVCTL_FERE;
121 config |= PCI_EXP_DEVCTL_URRE;
122 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, config);
123
124
125 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
126 if (pos) {
127
128 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
129 &dconfig);
130 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
131 dconfig);
132
133
134 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, 0);
135
136
137
138
139
140
141
142 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &dconfig);
143 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, dconfig);
144
145
146 pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, 0);
147
148 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &dconfig);
149
150 if (config & PCI_ERR_CAP_ECRC_GENC)
151 config |= PCI_ERR_CAP_ECRC_GENE;
152
153 if (config & PCI_ERR_CAP_ECRC_CHKC)
154 config |= PCI_ERR_CAP_ECRC_CHKE;
155 pci_write_config_dword(dev, pos + PCI_ERR_CAP, dconfig);
156
157
158 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND,
159 PCI_ERR_ROOT_CMD_COR_EN |
160 PCI_ERR_ROOT_CMD_NONFATAL_EN |
161 PCI_ERR_ROOT_CMD_FATAL_EN);
162
163 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &dconfig);
164 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, dconfig);
165 }
166
167 return 0;
168}
169
170
171
172
173
174
175
176
177
178
179const char *octeon_get_pci_interrupts(void)
180{
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 if (of_machine_is_compatible("dlink,dsr-500n"))
203 return "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
204 switch (octeon_bootinfo->board_type) {
205 case CVMX_BOARD_TYPE_NAO38:
206
207 return "AAAAADABAAAAAAAAAAAAAAAAAAAAAAAA";
208 case CVMX_BOARD_TYPE_EBH3100:
209 case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
210 case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
211 return "AAABAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
212 case CVMX_BOARD_TYPE_BBGW_REF:
213 return "AABCD";
214 case CVMX_BOARD_TYPE_CUST_DSR1000N:
215 return "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
216 case CVMX_BOARD_TYPE_THUNDER:
217 case CVMX_BOARD_TYPE_EBH3000:
218 default:
219 return "";
220 }
221}
222
223
224
225
226
227
228
229
230
231
232
233
234int __init octeon_pci_pcibios_map_irq(const struct pci_dev *dev,
235 u8 slot, u8 pin)
236{
237 int irq_num;
238 const char *interrupts;
239 int dev_num;
240
241
242 interrupts = octeon_get_pci_interrupts();
243
244 dev_num = dev->devfn >> 3;
245 if (dev_num < strlen(interrupts))
246 irq_num = ((interrupts[dev_num] - 'A' + pin - 1) & 3) +
247 OCTEON_IRQ_PCI_INT0;
248 else
249 irq_num = ((slot + pin - 3) & 3) + OCTEON_IRQ_PCI_INT0;
250 return irq_num;
251}
252
253
254
255
256
257static int octeon_read_config(struct pci_bus *bus, unsigned int devfn,
258 int reg, int size, u32 *val)
259{
260 union octeon_pci_address pci_addr;
261
262 pci_addr.u64 = 0;
263 pci_addr.s.upper = 2;
264 pci_addr.s.io = 1;
265 pci_addr.s.did = 3;
266 pci_addr.s.subdid = 1;
267 pci_addr.s.endian_swap = 1;
268 pci_addr.s.bus = bus->number;
269 pci_addr.s.dev = devfn >> 3;
270 pci_addr.s.func = devfn & 0x7;
271 pci_addr.s.reg = reg;
272
273 switch (size) {
274 case 4:
275 *val = le32_to_cpu(cvmx_read64_uint32(pci_addr.u64));
276 return PCIBIOS_SUCCESSFUL;
277 case 2:
278 *val = le16_to_cpu(cvmx_read64_uint16(pci_addr.u64));
279 return PCIBIOS_SUCCESSFUL;
280 case 1:
281 *val = cvmx_read64_uint8(pci_addr.u64);
282 return PCIBIOS_SUCCESSFUL;
283 }
284 return PCIBIOS_FUNC_NOT_SUPPORTED;
285}
286
287
288
289
290
291static int octeon_write_config(struct pci_bus *bus, unsigned int devfn,
292 int reg, int size, u32 val)
293{
294 union octeon_pci_address pci_addr;
295
296 pci_addr.u64 = 0;
297 pci_addr.s.upper = 2;
298 pci_addr.s.io = 1;
299 pci_addr.s.did = 3;
300 pci_addr.s.subdid = 1;
301 pci_addr.s.endian_swap = 1;
302 pci_addr.s.bus = bus->number;
303 pci_addr.s.dev = devfn >> 3;
304 pci_addr.s.func = devfn & 0x7;
305 pci_addr.s.reg = reg;
306
307 switch (size) {
308 case 4:
309 cvmx_write64_uint32(pci_addr.u64, cpu_to_le32(val));
310 return PCIBIOS_SUCCESSFUL;
311 case 2:
312 cvmx_write64_uint16(pci_addr.u64, cpu_to_le16(val));
313 return PCIBIOS_SUCCESSFUL;
314 case 1:
315 cvmx_write64_uint8(pci_addr.u64, val);
316 return PCIBIOS_SUCCESSFUL;
317 }
318 return PCIBIOS_FUNC_NOT_SUPPORTED;
319}
320
321
322static struct pci_ops octeon_pci_ops = {
323 .read = octeon_read_config,
324 .write = octeon_write_config,
325};
326
327static struct resource octeon_pci_mem_resource = {
328 .start = 0,
329 .end = 0,
330 .name = "Octeon PCI MEM",
331 .flags = IORESOURCE_MEM,
332};
333
334
335
336
337
338static struct resource octeon_pci_io_resource = {
339 .start = 0x4000,
340 .end = OCTEON_PCI_IOSPACE_SIZE - 1,
341 .name = "Octeon PCI IO",
342 .flags = IORESOURCE_IO,
343};
344
345static struct pci_controller octeon_pci_controller = {
346 .pci_ops = &octeon_pci_ops,
347 .mem_resource = &octeon_pci_mem_resource,
348 .mem_offset = OCTEON_PCI_MEMSPACE_OFFSET,
349 .io_resource = &octeon_pci_io_resource,
350 .io_offset = 0,
351 .io_map_base = OCTEON_PCI_IOSPACE_BASE,
352};
353
354
355
356
357
358static void octeon_pci_initialize(void)
359{
360 union cvmx_pci_cfg01 cfg01;
361 union cvmx_npi_ctl_status ctl_status;
362 union cvmx_pci_ctl_status_2 ctl_status_2;
363 union cvmx_pci_cfg19 cfg19;
364 union cvmx_pci_cfg16 cfg16;
365 union cvmx_pci_cfg22 cfg22;
366 union cvmx_pci_cfg56 cfg56;
367
368
369 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x1);
370 cvmx_read_csr(CVMX_CIU_SOFT_PRST);
371
372 udelay(2000);
373
374 ctl_status.u64 = 0;
375 ctl_status.s.max_word = 1;
376 ctl_status.s.timer = 1;
377 cvmx_write_csr(CVMX_NPI_CTL_STATUS, ctl_status.u64);
378
379
380
381 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x4);
382 cvmx_read_csr(CVMX_CIU_SOFT_PRST);
383
384 udelay(2000);
385
386 ctl_status_2.u32 = 0;
387 ctl_status_2.s.tsr_hwm = 1;
388
389 ctl_status_2.s.bar2pres = 1;
390 ctl_status_2.s.bar2_enb = 1;
391 ctl_status_2.s.bar2_cax = 1;
392 ctl_status_2.s.bar2_esx = 1;
393 ctl_status_2.s.pmo_amod = 1;
394 if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG) {
395
396 ctl_status_2.s.bb1_hole = OCTEON_PCI_BAR1_HOLE_BITS;
397 ctl_status_2.s.bb1_siz = 1;
398 ctl_status_2.s.bb_ca = 1;
399 ctl_status_2.s.bb_es = 1;
400 ctl_status_2.s.bb1 = 1;
401 ctl_status_2.s.bb0 = 1;
402 }
403
404 octeon_npi_write32(CVMX_NPI_PCI_CTL_STATUS_2, ctl_status_2.u32);
405 udelay(2000);
406
407 ctl_status_2.u32 = octeon_npi_read32(CVMX_NPI_PCI_CTL_STATUS_2);
408 pr_notice("PCI Status: %s %s-bit\n",
409 ctl_status_2.s.ap_pcix ? "PCI-X" : "PCI",
410 ctl_status_2.s.ap_64ad ? "64" : "32");
411
412 if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
413 union cvmx_pci_cnt_reg cnt_reg_start;
414 union cvmx_pci_cnt_reg cnt_reg_end;
415 unsigned long cycles, pci_clock;
416
417 cnt_reg_start.u64 = cvmx_read_csr(CVMX_NPI_PCI_CNT_REG);
418 cycles = read_c0_cvmcount();
419 udelay(1000);
420 cnt_reg_end.u64 = cvmx_read_csr(CVMX_NPI_PCI_CNT_REG);
421 cycles = read_c0_cvmcount() - cycles;
422 pci_clock = (cnt_reg_end.s.pcicnt - cnt_reg_start.s.pcicnt) /
423 (cycles / (mips_hpt_frequency / 1000000));
424 pr_notice("PCI Clock: %lu MHz\n", pci_clock);
425 }
426
427
428
429
430
431
432
433
434
435 if (ctl_status_2.s.ap_pcix) {
436 cfg19.u32 = 0;
437
438
439
440
441
442
443
444
445
446
447
448
449 cfg19.s.tdomc = 4;
450
451
452
453
454
455
456
457
458
459
460
461 cfg19.s.mdrrmc = 2;
462
463
464
465
466
467
468
469
470
471
472
473 cfg19.s.mrbcm = 1;
474 octeon_npi_write32(CVMX_NPI_PCI_CFG19, cfg19.u32);
475 }
476
477
478 cfg01.u32 = 0;
479 cfg01.s.msae = 1;
480 cfg01.s.me = 1;
481 cfg01.s.pee = 1;
482 cfg01.s.see = 1;
483 cfg01.s.fbbe = 1;
484
485 octeon_npi_write32(CVMX_NPI_PCI_CFG01, cfg01.u32);
486
487#ifdef USE_OCTEON_INTERNAL_ARBITER
488
489
490
491
492
493 {
494 union cvmx_npi_pci_int_arb_cfg pci_int_arb_cfg;
495
496 pci_int_arb_cfg.u64 = 0;
497 pci_int_arb_cfg.s.en = 1;
498 cvmx_write_csr(CVMX_NPI_PCI_INT_ARB_CFG, pci_int_arb_cfg.u64);
499 }
500#endif
501
502
503
504
505
506
507 cfg16.u32 = 0;
508 cfg16.s.mltd = 1;
509 octeon_npi_write32(CVMX_NPI_PCI_CFG16, cfg16.u32);
510
511
512
513
514
515 cfg22.u32 = 0;
516
517 cfg22.s.mrv = 0xff;
518
519
520
521
522 cfg22.s.flush = 1;
523 octeon_npi_write32(CVMX_NPI_PCI_CFG22, cfg22.u32);
524
525
526
527
528
529
530
531
532 cfg56.u32 = 0;
533 cfg56.s.pxcid = 7;
534 cfg56.s.ncp = 0xe8;
535 cfg56.s.dpere = 1;
536 cfg56.s.roe = 1;
537 cfg56.s.mmbc = 1;
538
539 cfg56.s.most = 3;
540
541
542 octeon_npi_write32(CVMX_NPI_PCI_CFG56, cfg56.u32);
543
544
545
546
547
548
549
550
551
552
553 octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_6, 0x21);
554 octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_C, 0x31);
555 octeon_npi_write32(CVMX_NPI_PCI_READ_CMD_E, 0x31);
556}
557
558
559
560
561
562static int __init octeon_pci_setup(void)
563{
564 union cvmx_npi_mem_access_subidx mem_access;
565 int index;
566
567
568 if (octeon_has_feature(OCTEON_FEATURE_PCIE))
569 return 0;
570
571 if (!octeon_is_pci_host()) {
572 pr_notice("Not in host mode, PCI Controller not initialized\n");
573 return 0;
574 }
575
576
577 octeon_pcibios_map_irq = octeon_pci_pcibios_map_irq;
578
579
580 if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||
581 OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2) ||
582 OCTEON_IS_MODEL(OCTEON_CN38XX_PASS1))
583 octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_SMALL;
584 else
585 octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_BIG;
586
587
588 set_io_port_base(OCTEON_PCI_IOSPACE_BASE);
589 ioport_resource.start = 0;
590 ioport_resource.end = OCTEON_PCI_IOSPACE_SIZE - 1;
591
592 pr_notice("%s Octeon big bar support\n",
593 (octeon_dma_bar_type ==
594 OCTEON_DMA_BAR_TYPE_BIG) ? "Enabling" : "Disabling");
595
596 octeon_pci_initialize();
597
598 mem_access.u64 = 0;
599 mem_access.s.esr = 1;
600 mem_access.s.esw = 1;
601 mem_access.s.nsr = 0;
602 mem_access.s.nsw = 0;
603 mem_access.s.ror = 0;
604 mem_access.s.row = 0;
605 mem_access.s.ba = 0;
606 cvmx_write_csr(CVMX_NPI_MEM_ACCESS_SUBID3, mem_access.u64);
607
608
609
610
611
612
613
614 octeon_npi_write32(CVMX_NPI_PCI_CFG08,
615 (u32)(OCTEON_BAR2_PCI_ADDRESS & 0xffffffffull));
616 octeon_npi_write32(CVMX_NPI_PCI_CFG09,
617 (u32)(OCTEON_BAR2_PCI_ADDRESS >> 32));
618
619 if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG) {
620
621 octeon_npi_write32(CVMX_NPI_PCI_CFG04, 0);
622 octeon_npi_write32(CVMX_NPI_PCI_CFG05, 0);
623
624
625
626
627
628 octeon_npi_write32(CVMX_NPI_PCI_CFG06, 2ul << 30);
629 octeon_npi_write32(CVMX_NPI_PCI_CFG07, 0);
630
631
632 octeon_bar1_pci_phys = 0x80000000ull;
633 for (index = 0; index < 32; index++) {
634 union cvmx_pci_bar1_indexx bar1_index;
635
636 bar1_index.u32 = 0;
637
638 bar1_index.s.addr_idx =
639 (octeon_bar1_pci_phys >> 22) + index;
640
641 bar1_index.s.ca = 1;
642
643 bar1_index.s.end_swp = 1;
644
645 bar1_index.s.addr_v = 1;
646 octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
647 bar1_index.u32);
648 }
649
650
651 octeon_pci_mem_resource.start =
652 OCTEON_PCI_MEMSPACE_OFFSET + (4ul << 30) -
653 (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
654 octeon_pci_mem_resource.end =
655 octeon_pci_mem_resource.start + (1ul << 30);
656 } else {
657
658 octeon_npi_write32(CVMX_NPI_PCI_CFG04, 128ul << 20);
659 octeon_npi_write32(CVMX_NPI_PCI_CFG05, 0);
660
661
662 octeon_npi_write32(CVMX_NPI_PCI_CFG06, 0);
663 octeon_npi_write32(CVMX_NPI_PCI_CFG07, 0);
664
665
666 octeon_bar1_pci_phys =
667 virt_to_phys(octeon_swiotlb) & ~((1ull << 22) - 1);
668
669 for (index = 0; index < 32; index++) {
670 union cvmx_pci_bar1_indexx bar1_index;
671
672 bar1_index.u32 = 0;
673
674 bar1_index.s.addr_idx =
675 (octeon_bar1_pci_phys >> 22) + index;
676
677 bar1_index.s.ca = 1;
678
679 bar1_index.s.end_swp = 1;
680
681 bar1_index.s.addr_v = 1;
682 octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
683 bar1_index.u32);
684 }
685
686
687 octeon_pci_mem_resource.start =
688 OCTEON_PCI_MEMSPACE_OFFSET + (128ul << 20) +
689 (4ul << 10);
690 octeon_pci_mem_resource.end =
691 octeon_pci_mem_resource.start + (1ul << 30);
692 }
693
694 register_pci_controller(&octeon_pci_controller);
695
696
697
698
699
700 cvmx_write_csr(CVMX_NPI_PCI_INT_SUM2, -1);
701
702 if (IS_ERR(platform_device_register_simple("octeon_pci_edac",
703 -1, NULL, 0)))
704 pr_err("Registration of co_pci_edac failed!\n");
705
706 octeon_pci_dma_init();
707
708 return 0;
709}
710
711arch_initcall(octeon_pci_setup);
712