blob: 47ca74c5c35686101bf6ec262b5f0a3960a4d746 [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
10#include <common.h>
Tom Rini8c70baa2021-12-14 13:36:40 -050011#include <clock_legacy.h>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030012#include <pci.h>
13#include <mpc83xx.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030015#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030017
18DECLARE_GLOBAL_DATA_PTR;
19
20#define PCIE_MAX_BUSES 2
21
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020022static struct {
23 u32 base;
24 u32 size;
25} mpc83xx_pcie_cfg_space[] = {
26 {
Tom Rini56af6592022-11-16 13:10:33 -050027 .base = CFG_SYS_PCIE1_CFG_BASE,
28 .size = CFG_SYS_PCIE1_CFG_SIZE,
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020029 },
Tom Rini56af6592022-11-16 13:10:33 -050030#if defined(CFG_SYS_PCIE2_CFG_BASE) && defined(CFG_SYS_PCIE2_CFG_SIZE)
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020031 {
Tom Rini56af6592022-11-16 13:10:33 -050032 .base = CFG_SYS_PCIE2_CFG_BASE,
33 .size = CFG_SYS_PCIE2_CFG_SIZE,
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020034 },
35#endif
36};
37
Mario Sixde09afa2019-01-21 09:17:55 +010038int get_pcie_clk(int index)
39{
40 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
41 u32 pci_sync_in;
42 u8 spmf;
43 u8 clkin_div;
44 u32 sccr;
45 u32 csb_clk;
46 u32 testval;
47
48 clkin_div = ((im->clk.spmr & SPMR_CKID) >> SPMR_CKID_SHIFT);
49 sccr = im->clk.sccr;
Tom Rini8c70baa2021-12-14 13:36:40 -050050 pci_sync_in = get_board_sys_clk() / (1 + clkin_div);
Mario Sixde09afa2019-01-21 09:17:55 +010051 spmf = (im->clk.spmr & SPMR_SPMF) >> SPMR_SPMF_SHIFT;
52 csb_clk = pci_sync_in * (1 + clkin_div) * spmf;
53
54 if (index)
55 testval = (sccr & SCCR_PCIEXP2CM) >> SCCR_PCIEXP2CM_SHIFT;
56 else
57 testval = (sccr & SCCR_PCIEXP1CM) >> SCCR_PCIEXP1CM_SHIFT;
58
59 switch (testval) {
60 case 0:
61 return 0;
62 case 1:
63 return csb_clk;
64 case 2:
65 return csb_clk / 2;
66 case 3:
67 return csb_clk / 3;
68 }
69
70 return 0;
71}
72
Anton Vorontsov1a8206c2009-01-08 04:26:12 +030073static void mpc83xx_pcie_init_bus(int bus, struct pci_region *reg)
74{
75 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
76 pex83xx_t *pex = &immr->pciexp[bus];
77 struct pex_outbound_window *out_win;
78 struct pex_inbound_window *in_win;
79 void *hose_cfg_base;
80 unsigned int ram_sz;
81 unsigned int barl;
82 unsigned int tar;
83 u16 reg16;
84 int i;
85
86 /* Enable pex csb bridge inbound & outbound transactions */
87 out_le32(&pex->bridge.pex_csb_ctrl,
88 in_le32(&pex->bridge.pex_csb_ctrl) | PEX_CSB_CTRL_OBPIOE |
89 PEX_CSB_CTRL_IBPIOE);
90
91 /* Enable bridge outbound */
92 out_le32(&pex->bridge.pex_csb_obctrl, PEX_CSB_OBCTRL_PIOE |
93 PEX_CSB_OBCTRL_MEMWE | PEX_CSB_OBCTRL_IOWE |
94 PEX_CSB_OBCTRL_CFGWE);
95
96 out_win = &pex->bridge.pex_outbound_win[0];
Ilya Yanokf0b1fc52010-09-17 23:41:46 +020097 out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG |
98 mpc83xx_pcie_cfg_space[bus].size);
99 out_le32(&out_win->bar, mpc83xx_pcie_cfg_space[bus].base);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300100 out_le32(&out_win->tarl, 0);
101 out_le32(&out_win->tarh, 0);
102
Baidu Boy1388f222010-11-29 21:10:45 +0800103 for (i = 0; i < 2; i++) {
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300104 u32 ar;
105
Baidu Boy1388f222010-11-29 21:10:45 +0800106 if (reg[i].size == 0)
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300107 break;
108
109 out_win = &pex->bridge.pex_outbound_win[i + 1];
Baidu Boy1388f222010-11-29 21:10:45 +0800110 out_le32(&out_win->bar, reg[i].phys_start);
111 out_le32(&out_win->tarl, reg[i].bus_start);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300112 out_le32(&out_win->tarh, 0);
Baidu Boy1388f222010-11-29 21:10:45 +0800113 ar = PEX_OWAR_EN | (reg[i].size & PEX_OWAR_SIZE);
114 if (reg[i].flags & PCI_REGION_IO)
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300115 ar |= PEX_OWAR_TYPE_IO;
116 else
117 ar |= PEX_OWAR_TYPE_MEM;
118 out_le32(&out_win->ar, ar);
119 }
120
121 out_le32(&pex->bridge.pex_csb_ibctrl, PEX_CSB_IBCTRL_PIOE);
122
123 ram_sz = gd->ram_size;
124 barl = 0;
125 tar = 0;
126 i = 0;
127 while (ram_sz > 0) {
128 in_win = &pex->bridge.pex_inbound_win[i];
129 out_le32(&in_win->barl, barl);
130 out_le32(&in_win->barh, 0x0);
131 out_le32(&in_win->tar, tar);
132 if (ram_sz >= 0x10000000) {
133 /* The maxium windows size is 256M */
134 out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
135 PEX_IWAR_TYPE_PF | 0x0FFFF000);
136 barl += 0x10000000;
137 tar += 0x10000000;
138 ram_sz -= 0x10000000;
139 } else {
140 /* The UM is not clear here.
141 * So, round up to even Mb boundary */
142
143 ram_sz = ram_sz >> (20 +
144 ((ram_sz & 0xFFFFF) ? 1 : 0));
145 if (!(ram_sz % 2))
146 ram_sz -= 1;
147 out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
148 PEX_IWAR_TYPE_PF | (ram_sz << 20) | 0xFF000);
149 ram_sz = 0;
150 }
151 i++;
152 }
153
154 in_win = &pex->bridge.pex_inbound_win[i];
155 out_le32(&in_win->barl, CONFIG_SYS_IMMR);
156 out_le32(&in_win->barh, 0);
157 out_le32(&in_win->tar, CONFIG_SYS_IMMR);
158 out_le32(&in_win->ar, PEX_IWAR_EN |
159 PEX_IWAR_TYPE_NO_PF | PEX_IWAR_SIZE_1M);
160
161 /* Enable the host virtual INTX interrupts */
162 out_le32(&pex->bridge.pex_int_axi_misc_enb,
163 in_le32(&pex->bridge.pex_int_axi_misc_enb) | 0x1E0);
164
165 /* Hose configure header is memory-mapped */
166 hose_cfg_base = (void *)pex;
167
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300168 /* Configure the PCIE controller core clock ratio */
169 out_le32(hose_cfg_base + PEX_GCLK_RATIO,
Mario Sixde09afa2019-01-21 09:17:55 +0100170 ((get_pcie_clk(bus) / 1000000) * 16) / 333);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300171 udelay(1000000);
172
173 /* Do Type 1 bridge configuration */
174 out_8(hose_cfg_base + PCI_PRIMARY_BUS, 0);
175 out_8(hose_cfg_base + PCI_SECONDARY_BUS, 1);
176 out_8(hose_cfg_base + PCI_SUBORDINATE_BUS, 255);
177
178 /*
179 * Write to Command register
180 */
181 reg16 = in_le16(hose_cfg_base + PCI_COMMAND);
182 reg16 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO |
183 PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
184 out_le16(hose_cfg_base + PCI_COMMAND, reg16);
185
186 /*
187 * Clear non-reserved bits in status register.
188 */
189 out_le16(hose_cfg_base + PCI_STATUS, 0xffff);
190 out_8(hose_cfg_base + PCI_LATENCY_TIMER, 0x80);
191 out_8(hose_cfg_base + PCI_CACHE_LINE_SIZE, 0x08);
192
193 printf("PCIE%d: ", bus);
194
Roy Zang06fd4dd2012-12-10 19:02:59 +0800195#define PCI_LTSSM 0x404 /* PCIe Link Training, Status State Machine */
196#define PCI_LTSSM_L0 0x16 /* L0 state */
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300197 reg16 = in_le16(hose_cfg_base + PCI_LTSSM);
198 if (reg16 >= PCI_LTSSM_L0)
199 printf("link\n");
200 else
201 printf("No link\n");
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300202}
203
204/*
205 * The caller must have already set SCCR, SERDES and the PCIE_LAW BARs
206 * must have been set to cover all of the requested regions.
207 */
Peter Tysere2283322010-09-14 19:13:50 -0500208void mpc83xx_pcie_init(int num_buses, struct pci_region **reg)
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300209{
210 int i;
211
212 /*
213 * Release PCI RST Output signal.
214 * Power on to RST high must be at least 100 ms as per PCI spec.
Peter Tysere2283322010-09-14 19:13:50 -0500215 * On warm boots only 1 ms is required, but we play it safe.
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300216 */
Peter Tysere2283322010-09-14 19:13:50 -0500217 udelay(100000);
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300218
Ilya Yanokf0b1fc52010-09-17 23:41:46 +0200219 if (num_buses > ARRAY_SIZE(mpc83xx_pcie_cfg_space)) {
220 printf("Second PCIE host contoller not configured!\n");
221 num_buses = ARRAY_SIZE(mpc83xx_pcie_cfg_space);
222 }
223
Anton Vorontsov1a8206c2009-01-08 04:26:12 +0300224 for (i = 0; i < num_buses; i++)
225 mpc83xx_pcie_init_bus(i, reg[i]);
226}