1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) Freescale Semiconductor, Inc. 2007
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Author: Scott Wood <scottwood@freescale.com>,
5*4882a593Smuzhiyun * with some bits from older board-specific PCI initialization.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <pci.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #if defined(CONFIG_OF_LIBFDT)
14*4882a593Smuzhiyun #include <linux/libfdt.h>
15*4882a593Smuzhiyun #include <fdt_support.h>
16*4882a593Smuzhiyun #endif
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <asm/mpc8349_pci.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define MAX_BUSES 2
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun static struct pci_controller pci_hose[MAX_BUSES];
25*4882a593Smuzhiyun static int pci_num_buses;
26*4882a593Smuzhiyun
pci_init_bus(int bus,struct pci_region * reg)27*4882a593Smuzhiyun static void pci_init_bus(int bus, struct pci_region *reg)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
30*4882a593Smuzhiyun volatile pot83xx_t *pot = immr->ios.pot;
31*4882a593Smuzhiyun volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus];
32*4882a593Smuzhiyun struct pci_controller *hose = &pci_hose[bus];
33*4882a593Smuzhiyun u32 dev;
34*4882a593Smuzhiyun u16 reg16;
35*4882a593Smuzhiyun int i;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun if (bus == 1)
38*4882a593Smuzhiyun pot += 3;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Setup outbound translation windows */
41*4882a593Smuzhiyun for (i = 0; i < 3; i++, reg++, pot++) {
42*4882a593Smuzhiyun if (reg->size == 0)
43*4882a593Smuzhiyun break;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun hose->regions[i] = *reg;
46*4882a593Smuzhiyun hose->region_count++;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun pot->potar = reg->bus_start >> 12;
49*4882a593Smuzhiyun pot->pobar = reg->phys_start >> 12;
50*4882a593Smuzhiyun pot->pocmr = ~(reg->size - 1) >> 12;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (reg->flags & PCI_REGION_IO)
53*4882a593Smuzhiyun pot->pocmr |= POCMR_IO;
54*4882a593Smuzhiyun #ifdef CONFIG_83XX_PCI_STREAMING
55*4882a593Smuzhiyun else if (reg->flags & PCI_REGION_PREFETCH)
56*4882a593Smuzhiyun pot->pocmr |= POCMR_SE;
57*4882a593Smuzhiyun #endif
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (bus == 1)
60*4882a593Smuzhiyun pot->pocmr |= POCMR_DST;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun pot->pocmr |= POCMR_EN;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* Point inbound translation at RAM */
66*4882a593Smuzhiyun pci_ctrl->pitar1 = 0;
67*4882a593Smuzhiyun pci_ctrl->pibar1 = 0;
68*4882a593Smuzhiyun pci_ctrl->piebar1 = 0;
69*4882a593Smuzhiyun pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
70*4882a593Smuzhiyun PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1));
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun i = hose->region_count++;
73*4882a593Smuzhiyun hose->regions[i].bus_start = 0;
74*4882a593Smuzhiyun hose->regions[i].phys_start = 0;
75*4882a593Smuzhiyun hose->regions[i].size = gd->ram_size;
76*4882a593Smuzhiyun hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun hose->first_busno = pci_last_busno() + 1;
79*4882a593Smuzhiyun hose->last_busno = 0xff;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80,
82*4882a593Smuzhiyun CONFIG_SYS_IMMR + 0x8304 + bus * 0x80);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun pci_register_hose(hose);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * Write to Command register
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun reg16 = 0xff;
90*4882a593Smuzhiyun dev = PCI_BDF(hose->first_busno, 0, 0);
91*4882a593Smuzhiyun pci_hose_read_config_word(hose, dev, PCI_COMMAND, ®16);
92*4882a593Smuzhiyun reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
93*4882a593Smuzhiyun pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * Clear non-reserved bits in status register.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
99*4882a593Smuzhiyun pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
100*4882a593Smuzhiyun pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun #ifdef CONFIG_PCI_SCAN_SHOW
103*4882a593Smuzhiyun printf("PCI: Bus Dev VenId DevId Class Int\n");
104*4882a593Smuzhiyun #endif
105*4882a593Smuzhiyun #ifndef CONFIG_PCISLAVE
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun * Hose scan.
108*4882a593Smuzhiyun */
109*4882a593Smuzhiyun hose->last_busno = pci_hose_scan(hose);
110*4882a593Smuzhiyun #endif
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * The caller must have already set OCCR, and the PCI_LAW BARs
115*4882a593Smuzhiyun * must have been set to cover all of the requested regions.
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * If fewer than three regions are requested, then the region
118*4882a593Smuzhiyun * list is terminated with a region of size 0.
119*4882a593Smuzhiyun */
mpc83xx_pci_init(int num_buses,struct pci_region ** reg)120*4882a593Smuzhiyun void mpc83xx_pci_init(int num_buses, struct pci_region **reg)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
123*4882a593Smuzhiyun int i;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (num_buses > MAX_BUSES) {
126*4882a593Smuzhiyun printf("%d PCI buses requested, %d supported\n",
127*4882a593Smuzhiyun num_buses, MAX_BUSES);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun num_buses = MAX_BUSES;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun pci_num_buses = num_buses;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * Release PCI RST Output signal.
136*4882a593Smuzhiyun * Power on to RST high must be at least 100 ms as per PCI spec.
137*4882a593Smuzhiyun * On warm boots only 1 ms is required, but we play it safe.
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun udelay(100000);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun for (i = 0; i < num_buses; i++)
142*4882a593Smuzhiyun immr->pci_ctrl[i].gcr = 1;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * RST high to first config access must be at least 2^25 cycles
146*4882a593Smuzhiyun * as per PCI spec. This could be cut in half if we know we're
147*4882a593Smuzhiyun * running at 66MHz. This could be insufficiently long if we're
148*4882a593Smuzhiyun * running the PCI bus at significantly less than 33MHz.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun udelay(1020000);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun for (i = 0; i < num_buses; i++)
153*4882a593Smuzhiyun pci_init_bus(i, reg[i]);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun #ifdef CONFIG_PCISLAVE
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun #define PCI_FUNCTION_CONFIG 0x44
159*4882a593Smuzhiyun #define PCI_FUNCTION_CFG_LOCK 0x20
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Unlock the configuration bit so that the host system can begin booting
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * This should be used after you have:
165*4882a593Smuzhiyun * 1) Called mpc83xx_pci_init()
166*4882a593Smuzhiyun * 2) Set up your inbound translation windows to the appropriate size
167*4882a593Smuzhiyun */
mpc83xx_pcislave_unlock(int bus)168*4882a593Smuzhiyun void mpc83xx_pcislave_unlock(int bus)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun struct pci_controller *hose = &pci_hose[bus];
171*4882a593Smuzhiyun u32 dev;
172*4882a593Smuzhiyun u16 reg16;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* Unlock configuration lock in PCI function configuration register */
175*4882a593Smuzhiyun dev = PCI_BDF(hose->first_busno, 0, 0);
176*4882a593Smuzhiyun pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, ®16);
177*4882a593Smuzhiyun reg16 &= ~(PCI_FUNCTION_CFG_LOCK);
178*4882a593Smuzhiyun pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* The configuration bit is now unlocked, so we can scan the bus */
181*4882a593Smuzhiyun hose->last_busno = pci_hose_scan(hose);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun #endif
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun #if defined(CONFIG_OF_LIBFDT)
ft_pci_setup(void * blob,bd_t * bd)186*4882a593Smuzhiyun void ft_pci_setup(void *blob, bd_t *bd)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun int nodeoffset;
189*4882a593Smuzhiyun int tmp[2];
190*4882a593Smuzhiyun const char *path;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun if (pci_num_buses < 1)
193*4882a593Smuzhiyun return;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun nodeoffset = fdt_path_offset(blob, "/aliases");
196*4882a593Smuzhiyun if (nodeoffset >= 0) {
197*4882a593Smuzhiyun path = fdt_getprop(blob, nodeoffset, "pci0", NULL);
198*4882a593Smuzhiyun if (path) {
199*4882a593Smuzhiyun tmp[0] = cpu_to_be32(pci_hose[0].first_busno);
200*4882a593Smuzhiyun tmp[1] = cpu_to_be32(pci_hose[0].last_busno);
201*4882a593Smuzhiyun do_fixup_by_path(blob, path, "bus-range",
202*4882a593Smuzhiyun &tmp, sizeof(tmp), 1);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun tmp[0] = cpu_to_be32(gd->pci_clk);
205*4882a593Smuzhiyun do_fixup_by_path(blob, path, "clock-frequency",
206*4882a593Smuzhiyun &tmp, sizeof(tmp[0]), 1);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (pci_num_buses < 2)
210*4882a593Smuzhiyun return;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun path = fdt_getprop(blob, nodeoffset, "pci1", NULL);
213*4882a593Smuzhiyun if (path) {
214*4882a593Smuzhiyun tmp[0] = cpu_to_be32(pci_hose[1].first_busno);
215*4882a593Smuzhiyun tmp[1] = cpu_to_be32(pci_hose[1].last_busno);
216*4882a593Smuzhiyun do_fixup_by_path(blob, path, "bus-range",
217*4882a593Smuzhiyun &tmp, sizeof(tmp), 1);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun tmp[0] = cpu_to_be32(gd->pci_clk);
220*4882a593Smuzhiyun do_fixup_by_path(blob, path, "clock-frequency",
221*4882a593Smuzhiyun &tmp, sizeof(tmp[0]), 1);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun #endif /* CONFIG_OF_LIBFDT */
226