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