blob: 665ac378c89ae9fd7a25a0cf07337239c7713464 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott Wood2fa13912007-04-16 14:34:21 -05002/*
3 * Copyright (C) Freescale Semiconductor, Inc. 2007
4 *
5 * Author: Scott Wood <scottwood@freescale.com>,
6 * with some bits from older board-specific PCI initialization.
Scott Wood2fa13912007-04-16 14:34:21 -05007 */
8
9#include <common.h>
10#include <pci.h>
Kim Phillips3c6f3b72007-07-25 19:25:28 -050011
12#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Kim Phillipsfd47a742007-12-20 14:09:22 -060014#include <fdt_support.h>
Kim Phillips3c6f3b72007-07-25 19:25:28 -050015#endif
16
Scott Wood2fa13912007-04-16 14:34:21 -050017#include <asm/mpc8349_pci.h>
18
Scott Wood2fa13912007-04-16 14:34:21 -050019#define MAX_BUSES 2
20
21DECLARE_GLOBAL_DATA_PTR;
22
23static struct pci_controller pci_hose[MAX_BUSES];
24static int pci_num_buses;
25
26static void pci_init_bus(int bus, struct pci_region *reg)
27{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020028 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood2fa13912007-04-16 14:34:21 -050029 volatile pot83xx_t *pot = immr->ios.pot;
30 volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus];
31 struct pci_controller *hose = &pci_hose[bus];
32 u32 dev;
33 u16 reg16;
34 int i;
35
36 if (bus == 1)
37 pot += 3;
38
39 /* Setup outbound translation windows */
40 for (i = 0; i < 3; i++, reg++, pot++) {
41 if (reg->size == 0)
42 break;
43
44 hose->regions[i] = *reg;
45 hose->region_count++;
46
47 pot->potar = reg->bus_start >> 12;
48 pot->pobar = reg->phys_start >> 12;
49 pot->pocmr = ~(reg->size - 1) >> 12;
50
51 if (reg->flags & PCI_REGION_IO)
52 pot->pocmr |= POCMR_IO;
53#ifdef CONFIG_83XX_PCI_STREAMING
54 else if (reg->flags & PCI_REGION_PREFETCH)
55 pot->pocmr |= POCMR_SE;
56#endif
57
58 if (bus == 1)
59 pot->pocmr |= POCMR_DST;
60
61 pot->pocmr |= POCMR_EN;
62 }
63
64 /* Point inbound translation at RAM */
65 pci_ctrl->pitar1 = 0;
66 pci_ctrl->pibar1 = 0;
67 pci_ctrl->piebar1 = 0;
68 pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020069 PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1));
Scott Wood2fa13912007-04-16 14:34:21 -050070
71 i = hose->region_count++;
72 hose->regions[i].bus_start = 0;
73 hose->regions[i].phys_start = 0;
74 hose->regions[i].size = gd->ram_size;
Kumar Galaefa1f1d2009-02-06 09:49:31 -060075 hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
Scott Wood2fa13912007-04-16 14:34:21 -050076
Anton Vorontsov41d6cf82009-02-19 18:20:50 +030077 hose->first_busno = pci_last_busno() + 1;
Scott Wood2fa13912007-04-16 14:34:21 -050078 hose->last_busno = 0xff;
79
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020080 pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80,
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020081 CONFIG_SYS_IMMR + 0x8304 + bus * 0x80);
Scott Wood2fa13912007-04-16 14:34:21 -050082
83 pci_register_hose(hose);
84
85 /*
86 * Write to Command register
87 */
88 reg16 = 0xff;
89 dev = PCI_BDF(hose->first_busno, 0, 0);
90 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &reg16);
91 reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
92 pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
93
94 /*
95 * Clear non-reserved bits in status register.
96 */
97 pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
98 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
99 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
100
101#ifdef CONFIG_PCI_SCAN_SHOW
102 printf("PCI: Bus Dev VenId DevId Class Int\n");
103#endif
Ira Snydere2e29ec2009-01-12 13:32:26 -0800104#ifndef CONFIG_PCISLAVE
Scott Wood2fa13912007-04-16 14:34:21 -0500105 /*
106 * Hose scan.
107 */
108 hose->last_busno = pci_hose_scan(hose);
Ira Snydere2e29ec2009-01-12 13:32:26 -0800109#endif
Scott Wood2fa13912007-04-16 14:34:21 -0500110}
111
112/*
113 * The caller must have already set OCCR, and the PCI_LAW BARs
114 * must have been set to cover all of the requested regions.
115 *
116 * If fewer than three regions are requested, then the region
117 * list is terminated with a region of size 0.
118 */
Peter Tysere2283322010-09-14 19:13:50 -0500119void mpc83xx_pci_init(int num_buses, struct pci_region **reg)
Scott Wood2fa13912007-04-16 14:34:21 -0500120{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200121 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood2fa13912007-04-16 14:34:21 -0500122 int i;
123
124 if (num_buses > MAX_BUSES) {
Robert P. J. Daycbd618f2015-12-16 12:25:42 -0500125 printf("%d PCI buses requested, %d supported\n",
Scott Wood2fa13912007-04-16 14:34:21 -0500126 num_buses, MAX_BUSES);
127
128 num_buses = MAX_BUSES;
129 }
130
131 pci_num_buses = num_buses;
132
133 /*
134 * Release PCI RST Output signal.
135 * Power on to RST high must be at least 100 ms as per PCI spec.
Peter Tysere2283322010-09-14 19:13:50 -0500136 * On warm boots only 1 ms is required, but we play it safe.
Scott Wood2fa13912007-04-16 14:34:21 -0500137 */
Peter Tysere2283322010-09-14 19:13:50 -0500138 udelay(100000);
Scott Wood2fa13912007-04-16 14:34:21 -0500139
140 for (i = 0; i < num_buses; i++)
141 immr->pci_ctrl[i].gcr = 1;
142
143 /*
144 * RST high to first config access must be at least 2^25 cycles
145 * as per PCI spec. This could be cut in half if we know we're
146 * running at 66MHz. This could be insufficiently long if we're
147 * running the PCI bus at significantly less than 33MHz.
148 */
149 udelay(1020000);
150
151 for (i = 0; i < num_buses; i++)
152 pci_init_bus(i, reg[i]);
153}
154
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700155#ifdef CONFIG_PCISLAVE
156
157#define PCI_FUNCTION_CONFIG 0x44
158#define PCI_FUNCTION_CFG_LOCK 0x20
159
160/*
161 * Unlock the configuration bit so that the host system can begin booting
162 *
163 * This should be used after you have:
164 * 1) Called mpc83xx_pci_init()
165 * 2) Set up your inbound translation windows to the appropriate size
166 */
167void mpc83xx_pcislave_unlock(int bus)
168{
169 struct pci_controller *hose = &pci_hose[bus];
170 u32 dev;
171 u16 reg16;
172
173 /* Unlock configuration lock in PCI function configuration register */
174 dev = PCI_BDF(hose->first_busno, 0, 0);
175 pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, &reg16);
176 reg16 &= ~(PCI_FUNCTION_CFG_LOCK);
177 pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16);
Ira Snydere2e29ec2009-01-12 13:32:26 -0800178
179 /* The configuration bit is now unlocked, so we can scan the bus */
180 hose->last_busno = pci_hose_scan(hose);
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700181}
182#endif
183
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500184#if defined(CONFIG_OF_LIBFDT)
185void ft_pci_setup(void *blob, bd_t *bd)
186{
187 int nodeoffset;
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500188 int tmp[2];
Kim Phillipsfd47a742007-12-20 14:09:22 -0600189 const char *path;
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500190
191 if (pci_num_buses < 1)
192 return;
193
Kim Phillipsfd47a742007-12-20 14:09:22 -0600194 nodeoffset = fdt_path_offset(blob, "/aliases");
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500195 if (nodeoffset >= 0) {
Kim Phillipsfd47a742007-12-20 14:09:22 -0600196 path = fdt_getprop(blob, nodeoffset, "pci0", NULL);
197 if (path) {
198 tmp[0] = cpu_to_be32(pci_hose[0].first_busno);
199 tmp[1] = cpu_to_be32(pci_hose[0].last_busno);
200 do_fixup_by_path(blob, path, "bus-range",
201 &tmp, sizeof(tmp), 1);
Kim Phillips21416812007-08-15 22:30:33 -0500202
Kim Phillipsfd47a742007-12-20 14:09:22 -0600203 tmp[0] = cpu_to_be32(gd->pci_clk);
204 do_fixup_by_path(blob, path, "clock-frequency",
205 &tmp, sizeof(tmp[0]), 1);
206 }
Scott Wood2fa13912007-04-16 14:34:21 -0500207
Kim Phillipsfd47a742007-12-20 14:09:22 -0600208 if (pci_num_buses < 2)
209 return;
Scott Wood2fa13912007-04-16 14:34:21 -0500210
Kim Phillipsfd47a742007-12-20 14:09:22 -0600211 path = fdt_getprop(blob, nodeoffset, "pci1", NULL);
212 if (path) {
Anton Vorontsov7c785472009-02-19 18:20:46 +0300213 tmp[0] = cpu_to_be32(pci_hose[1].first_busno);
214 tmp[1] = cpu_to_be32(pci_hose[1].last_busno);
Kim Phillipsfd47a742007-12-20 14:09:22 -0600215 do_fixup_by_path(blob, path, "bus-range",
216 &tmp, sizeof(tmp), 1);
Scott Wood2fa13912007-04-16 14:34:21 -0500217
Kim Phillipsfd47a742007-12-20 14:09:22 -0600218 tmp[0] = cpu_to_be32(gd->pci_clk);
219 do_fixup_by_path(blob, path, "clock-frequency",
220 &tmp, sizeof(tmp[0]), 1);
221 }
Scott Wood2fa13912007-04-16 14:34:21 -0500222 }
223}
Kim Phillipsfd47a742007-12-20 14:09:22 -0600224#endif /* CONFIG_OF_LIBFDT */