pci-octeon.c 22 KB

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