blob: efa30c6833893088d2c8af045b9940fb6227cb07 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Anton Vorontsov1a8206c2009-01-08 04:26:12 +03002/*
3 * Copyright (C) 2007-2009 Freescale Semiconductor, Inc.
4 * Copyright (C) 2008-2009 MontaVista Software, Inc.
5 *
6 * Authors: Tony Li <tony.li@freescale.com>
7 * Anton Vorontsov <avorontsov@ru.mvista.com>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +03008 */
9
Tom Rini8c70baa2021-12-14 13:36:40 -050010#include <clock_legacy.h>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030011#include <pci.h>
12#include <mpc83xx.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030014#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030016
17DECLARE_GLOBAL_DATA_PTR;
18
19#define PCIE_MAX_BUSES 2
20
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020021static struct {
22 u32 base;
23 u32 size;
24} mpc83xx_pcie_cfg_space[] = {
25 {
Tom Rini56af6592022-11-16 13:10:33 -050026 .base = CFG_SYS_PCIE1_CFG_BASE,
27 .size = CFG_SYS_PCIE1_CFG_SIZE,
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020028 },
Tom Rini56af6592022-11-16 13:10:33 -050029#if defined(CFG_SYS_PCIE2_CFG_BASE) && defined(CFG_SYS_PCIE2_CFG_SIZE)
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020030 {
Tom Rini56af6592022-11-16 13:10:33 -050031 .base = CFG_SYS_PCIE2_CFG_BASE,
32 .size = CFG_SYS_PCIE2_CFG_SIZE,
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020033 },
34#endif
35};
36
Mario Sixde09afa2019-01-21 09:17:55 +010037int get_pcie_clk(int index)
38{
39 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
40 u32 pci_sync_in;
41 u8 spmf;
42 u8 clkin_div;
43 u32 sccr;
44 u32 csb_clk;
45 u32 testval;
46
47 clkin_div = ((im->clk.spmr & SPMR_CKID) >> SPMR_CKID_SHIFT);
48 sccr = im->clk.sccr;
Tom Rini8c70baa2021-12-14 13:36:40 -050049 pci_sync_in = get_board_sys_clk() / (1 + clkin_div);
Mario Sixde09afa2019-01-21 09:17:55 +010050 spmf = (im->clk.spmr & SPMR_SPMF) >> SPMR_SPMF_SHIFT;
51 csb_clk = pci_sync_in * (1 + clkin_div) * spmf;
52
53 if (index)
54 testval = (sccr & SCCR_PCIEXP2CM) >> SCCR_PCIEXP2CM_SHIFT;
55 else
56 testval = (sccr & SCCR_PCIEXP1CM) >> SCCR_PCIEXP1CM_SHIFT;
57
58 switch (testval) {
59 case 0:
60 return 0;
61 case 1:
62 return csb_clk;
63 case 2:
64 return csb_clk / 2;
65 case 3:
66 return csb_clk / 3;
67 }
68
69 return 0;
70}
71
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030072static void mpc83xx_pcie_init_bus(int bus, struct pci_region *reg)
73{
74 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
75 pex83xx_t *pex = &immr->pciexp[bus];
76 struct pex_outbound_window *out_win;
77 struct pex_inbound_window *in_win;
78 void *hose_cfg_base;
79 unsigned int ram_sz;
80 unsigned int barl;
81 unsigned int tar;
82 u16 reg16;
83 int i;
84
85 /* Enable pex csb bridge inbound & outbound transactions */
86 out_le32(&pex->bridge.pex_csb_ctrl,
87 in_le32(&pex->bridge.pex_csb_ctrl) | PEX_CSB_CTRL_OBPIOE |
88 PEX_CSB_CTRL_IBPIOE);
89
90 /* Enable bridge outbound */
91 out_le32(&pex->bridge.pex_csb_obctrl, PEX_CSB_OBCTRL_PIOE |
92 PEX_CSB_OBCTRL_MEMWE | PEX_CSB_OBCTRL_IOWE |
93 PEX_CSB_OBCTRL_CFGWE);
94
95 out_win = &pex->bridge.pex_outbound_win[0];
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020096 out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG |
97 mpc83xx_pcie_cfg_space[bus].size);
98 out_le32(&out_win->bar, mpc83xx_pcie_cfg_space[bus].base);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030099 out_le32(&out_win->tarl, 0);
100 out_le32(&out_win->tarh, 0);
101
Baidu Boy1388f222010-11-29 21:10:45 +0800102 for (i = 0; i < 2; i++) {
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300103 u32 ar;
104
Baidu Boy1388f222010-11-29 21:10:45 +0800105 if (reg[i].size == 0)
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300106 break;
107
108 out_win = &pex->bridge.pex_outbound_win[i + 1];
Baidu Boy1388f222010-11-29 21:10:45 +0800109 out_le32(&out_win->bar, reg[i].phys_start);
110 out_le32(&out_win->tarl, reg[i].bus_start);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300111 out_le32(&out_win->tarh, 0);
Baidu Boy1388f222010-11-29 21:10:45 +0800112 ar = PEX_OWAR_EN | (reg[i].size & PEX_OWAR_SIZE);
113 if (reg[i].flags & PCI_REGION_IO)
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300114 ar |= PEX_OWAR_TYPE_IO;
115 else
116 ar |= PEX_OWAR_TYPE_MEM;
117 out_le32(&out_win->ar, ar);
118 }
119
120 out_le32(&pex->bridge.pex_csb_ibctrl, PEX_CSB_IBCTRL_PIOE);
121
122 ram_sz = gd->ram_size;
123 barl = 0;
124 tar = 0;
125 i = 0;
126 while (ram_sz > 0) {
127 in_win = &pex->bridge.pex_inbound_win[i];
128 out_le32(&in_win->barl, barl);
129 out_le32(&in_win->barh, 0x0);
130 out_le32(&in_win->tar, tar);
131 if (ram_sz >= 0x10000000) {
132 /* The maxium windows size is 256M */
133 out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
134 PEX_IWAR_TYPE_PF | 0x0FFFF000);
135 barl += 0x10000000;
136 tar += 0x10000000;
137 ram_sz -= 0x10000000;
138 } else {
139 /* The UM is not clear here.
140 * So, round up to even Mb boundary */
141
142 ram_sz = ram_sz >> (20 +
143 ((ram_sz & 0xFFFFF) ? 1 : 0));
144 if (!(ram_sz % 2))
145 ram_sz -= 1;
146 out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
147 PEX_IWAR_TYPE_PF | (ram_sz << 20) | 0xFF000);
148 ram_sz = 0;
149 }
150 i++;
151 }
152
153 in_win = &pex->bridge.pex_inbound_win[i];
154 out_le32(&in_win->barl, CONFIG_SYS_IMMR);
155 out_le32(&in_win->barh, 0);
156 out_le32(&in_win->tar, CONFIG_SYS_IMMR);
157 out_le32(&in_win->ar, PEX_IWAR_EN |
158 PEX_IWAR_TYPE_NO_PF | PEX_IWAR_SIZE_1M);
159
160 /* Enable the host virtual INTX interrupts */
161 out_le32(&pex->bridge.pex_int_axi_misc_enb,
162 in_le32(&pex->bridge.pex_int_axi_misc_enb) | 0x1E0);
163
164 /* Hose configure header is memory-mapped */
165 hose_cfg_base = (void *)pex;
166
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300167 /* Configure the PCIE controller core clock ratio */
168 out_le32(hose_cfg_base + PEX_GCLK_RATIO,
Mario Sixde09afa2019-01-21 09:17:55 +0100169 ((get_pcie_clk(bus) / 1000000) * 16) / 333);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300170 udelay(1000000);
171
172 /* Do Type 1 bridge configuration */
173 out_8(hose_cfg_base + PCI_PRIMARY_BUS, 0);
174 out_8(hose_cfg_base + PCI_SECONDARY_BUS, 1);
175 out_8(hose_cfg_base + PCI_SUBORDINATE_BUS, 255);
176
177 /*
178 * Write to Command register
179 */
180 reg16 = in_le16(hose_cfg_base + PCI_COMMAND);
181 reg16 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO |
182 PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
183 out_le16(hose_cfg_base + PCI_COMMAND, reg16);
184
185 /*
186 * Clear non-reserved bits in status register.
187 */
188 out_le16(hose_cfg_base + PCI_STATUS, 0xffff);
189 out_8(hose_cfg_base + PCI_LATENCY_TIMER, 0x80);
190 out_8(hose_cfg_base + PCI_CACHE_LINE_SIZE, 0x08);
191
192 printf("PCIE%d: ", bus);
193
Roy Zang06fd4dd2012-12-10 19:02:59 +0800194#define PCI_LTSSM 0x404 /* PCIe Link Training, Status State Machine */
195#define PCI_LTSSM_L0 0x16 /* L0 state */
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300196 reg16 = in_le16(hose_cfg_base + PCI_LTSSM);
197 if (reg16 >= PCI_LTSSM_L0)
198 printf("link\n");
199 else
200 printf("No link\n");
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300201}
202
203/*
204 * The caller must have already set SCCR, SERDES and the PCIE_LAW BARs
205 * must have been set to cover all of the requested regions.
206 */
Peter Tysere2283322010-09-14 19:13:50 -0500207void mpc83xx_pcie_init(int num_buses, struct pci_region **reg)
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300208{
209 int i;
210
211 /*
212 * Release PCI RST Output signal.
213 * Power on to RST high must be at least 100 ms as per PCI spec.
Peter Tysere2283322010-09-14 19:13:50 -0500214 * On warm boots only 1 ms is required, but we play it safe.
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300215 */
Peter Tysere2283322010-09-14 19:13:50 -0500216 udelay(100000);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300217
Ilya Yanokf0b1fc52010-09-17 23:41:46 +0200218 if (num_buses > ARRAY_SIZE(mpc83xx_pcie_cfg_space)) {
219 printf("Second PCIE host contoller not configured!\n");
220 num_buses = ARRAY_SIZE(mpc83xx_pcie_cfg_space);
221 }
222
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300223 for (i = 0; i < num_buses; i++)
224 mpc83xx_pcie_init_bus(i, reg[i]);
225}