blob: 507ab3417b3f5a19e19065b44894a516a245be41 [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>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Scott Wood2fa13912007-04-16 14:34:21 -050011#include <pci.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <asm/bitops.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Kim Phillips3c6f3b72007-07-25 19:25:28 -050015
16#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Kim Phillipsfd47a742007-12-20 14:09:22 -060018#include <fdt_support.h>
Kim Phillips3c6f3b72007-07-25 19:25:28 -050019#endif
20
Scott Wood2fa13912007-04-16 14:34:21 -050021#include <asm/mpc8349_pci.h>
22
Scott Wood2fa13912007-04-16 14:34:21 -050023#define MAX_BUSES 2
24
25DECLARE_GLOBAL_DATA_PTR;
26
27static struct pci_controller pci_hose[MAX_BUSES];
28static int pci_num_buses;
29
Sinan Akman8d09f912021-05-11 14:18:02 -040030#if !defined(CONFIG_DM_PCI)
Scott Wood2fa13912007-04-16 14:34:21 -050031static void pci_init_bus(int bus, struct pci_region *reg)
32{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020033 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood2fa13912007-04-16 14:34:21 -050034 volatile pot83xx_t *pot = immr->ios.pot;
35 volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus];
36 struct pci_controller *hose = &pci_hose[bus];
37 u32 dev;
38 u16 reg16;
39 int i;
40
41 if (bus == 1)
42 pot += 3;
43
44 /* Setup outbound translation windows */
45 for (i = 0; i < 3; i++, reg++, pot++) {
46 if (reg->size == 0)
47 break;
48
49 hose->regions[i] = *reg;
50 hose->region_count++;
51
52 pot->potar = reg->bus_start >> 12;
53 pot->pobar = reg->phys_start >> 12;
54 pot->pocmr = ~(reg->size - 1) >> 12;
55
56 if (reg->flags & PCI_REGION_IO)
57 pot->pocmr |= POCMR_IO;
58#ifdef CONFIG_83XX_PCI_STREAMING
59 else if (reg->flags & PCI_REGION_PREFETCH)
60 pot->pocmr |= POCMR_SE;
61#endif
62
63 if (bus == 1)
64 pot->pocmr |= POCMR_DST;
65
66 pot->pocmr |= POCMR_EN;
67 }
68
69 /* Point inbound translation at RAM */
70 pci_ctrl->pitar1 = 0;
71 pci_ctrl->pibar1 = 0;
72 pci_ctrl->piebar1 = 0;
73 pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020074 PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1));
Scott Wood2fa13912007-04-16 14:34:21 -050075
76 i = hose->region_count++;
77 hose->regions[i].bus_start = 0;
78 hose->regions[i].phys_start = 0;
79 hose->regions[i].size = gd->ram_size;
Kumar Galaefa1f1d2009-02-06 09:49:31 -060080 hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
Scott Wood2fa13912007-04-16 14:34:21 -050081
Anton Vorontsov41d6cf82009-02-19 18:20:50 +030082 hose->first_busno = pci_last_busno() + 1;
Scott Wood2fa13912007-04-16 14:34:21 -050083 hose->last_busno = 0xff;
84
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020085 pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80,
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020086 CONFIG_SYS_IMMR + 0x8304 + bus * 0x80);
Scott Wood2fa13912007-04-16 14:34:21 -050087
88 pci_register_hose(hose);
89
90 /*
91 * Write to Command register
92 */
93 reg16 = 0xff;
94 dev = PCI_BDF(hose->first_busno, 0, 0);
95 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &reg16);
96 reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
97 pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
98
99 /*
100 * Clear non-reserved bits in status register.
101 */
102 pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
103 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
104 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
105
106#ifdef CONFIG_PCI_SCAN_SHOW
107 printf("PCI: Bus Dev VenId DevId Class Int\n");
108#endif
Ira Snydere2e29ec2009-01-12 13:32:26 -0800109#ifndef CONFIG_PCISLAVE
Scott Wood2fa13912007-04-16 14:34:21 -0500110 /*
111 * Hose scan.
112 */
113 hose->last_busno = pci_hose_scan(hose);
Ira Snydere2e29ec2009-01-12 13:32:26 -0800114#endif
Scott Wood2fa13912007-04-16 14:34:21 -0500115}
116
117/*
118 * The caller must have already set OCCR, and the PCI_LAW BARs
119 * must have been set to cover all of the requested regions.
120 *
121 * If fewer than three regions are requested, then the region
122 * list is terminated with a region of size 0.
123 */
Peter Tysere2283322010-09-14 19:13:50 -0500124void mpc83xx_pci_init(int num_buses, struct pci_region **reg)
Scott Wood2fa13912007-04-16 14:34:21 -0500125{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200126 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood2fa13912007-04-16 14:34:21 -0500127 int i;
128
129 if (num_buses > MAX_BUSES) {
Robert P. J. Daycbd618f2015-12-16 12:25:42 -0500130 printf("%d PCI buses requested, %d supported\n",
Scott Wood2fa13912007-04-16 14:34:21 -0500131 num_buses, MAX_BUSES);
132
133 num_buses = MAX_BUSES;
134 }
135
136 pci_num_buses = num_buses;
137
138 /*
139 * Release PCI RST Output signal.
140 * Power on to RST high must be at least 100 ms as per PCI spec.
Peter Tysere2283322010-09-14 19:13:50 -0500141 * On warm boots only 1 ms is required, but we play it safe.
Scott Wood2fa13912007-04-16 14:34:21 -0500142 */
Peter Tysere2283322010-09-14 19:13:50 -0500143 udelay(100000);
Scott Wood2fa13912007-04-16 14:34:21 -0500144
145 for (i = 0; i < num_buses; i++)
146 immr->pci_ctrl[i].gcr = 1;
147
148 /*
149 * RST high to first config access must be at least 2^25 cycles
150 * as per PCI spec. This could be cut in half if we know we're
151 * running at 66MHz. This could be insufficiently long if we're
152 * running the PCI bus at significantly less than 33MHz.
153 */
154 udelay(1020000);
155
156 for (i = 0; i < num_buses; i++)
157 pci_init_bus(i, reg[i]);
158}
159
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700160#ifdef CONFIG_PCISLAVE
161
162#define PCI_FUNCTION_CONFIG 0x44
163#define PCI_FUNCTION_CFG_LOCK 0x20
164
165/*
166 * Unlock the configuration bit so that the host system can begin booting
167 *
168 * This should be used after you have:
169 * 1) Called mpc83xx_pci_init()
170 * 2) Set up your inbound translation windows to the appropriate size
171 */
172void mpc83xx_pcislave_unlock(int bus)
173{
174 struct pci_controller *hose = &pci_hose[bus];
175 u32 dev;
176 u16 reg16;
177
178 /* Unlock configuration lock in PCI function configuration register */
179 dev = PCI_BDF(hose->first_busno, 0, 0);
180 pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, &reg16);
181 reg16 &= ~(PCI_FUNCTION_CFG_LOCK);
182 pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16);
Ira Snydere2e29ec2009-01-12 13:32:26 -0800183
184 /* The configuration bit is now unlocked, so we can scan the bus */
185 hose->last_busno = pci_hose_scan(hose);
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700186}
187#endif
Sinan Akman8d09f912021-05-11 14:18:02 -0400188#endif /* CONFIG_DM_PCI */
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700189
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500190#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900191void ft_pci_setup(void *blob, struct bd_info *bd)
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500192{
193 int nodeoffset;
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500194 int tmp[2];
Kim Phillipsfd47a742007-12-20 14:09:22 -0600195 const char *path;
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500196
197 if (pci_num_buses < 1)
198 return;
199
Kim Phillipsfd47a742007-12-20 14:09:22 -0600200 nodeoffset = fdt_path_offset(blob, "/aliases");
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500201 if (nodeoffset >= 0) {
Kim Phillipsfd47a742007-12-20 14:09:22 -0600202 path = fdt_getprop(blob, nodeoffset, "pci0", NULL);
203 if (path) {
204 tmp[0] = cpu_to_be32(pci_hose[0].first_busno);
205 tmp[1] = cpu_to_be32(pci_hose[0].last_busno);
206 do_fixup_by_path(blob, path, "bus-range",
207 &tmp, sizeof(tmp), 1);
Kim Phillips21416812007-08-15 22:30:33 -0500208
Kim Phillipsfd47a742007-12-20 14:09:22 -0600209 tmp[0] = cpu_to_be32(gd->pci_clk);
210 do_fixup_by_path(blob, path, "clock-frequency",
211 &tmp, sizeof(tmp[0]), 1);
212 }
Scott Wood2fa13912007-04-16 14:34:21 -0500213
Kim Phillipsfd47a742007-12-20 14:09:22 -0600214 if (pci_num_buses < 2)
215 return;
Scott Wood2fa13912007-04-16 14:34:21 -0500216
Kim Phillipsfd47a742007-12-20 14:09:22 -0600217 path = fdt_getprop(blob, nodeoffset, "pci1", NULL);
218 if (path) {
Anton Vorontsov7c785472009-02-19 18:20:46 +0300219 tmp[0] = cpu_to_be32(pci_hose[1].first_busno);
220 tmp[1] = cpu_to_be32(pci_hose[1].last_busno);
Kim Phillipsfd47a742007-12-20 14:09:22 -0600221 do_fixup_by_path(blob, path, "bus-range",
222 &tmp, sizeof(tmp), 1);
Scott Wood2fa13912007-04-16 14:34:21 -0500223
Kim Phillipsfd47a742007-12-20 14:09:22 -0600224 tmp[0] = cpu_to_be32(gd->pci_clk);
225 do_fixup_by_path(blob, path, "clock-frequency",
226 &tmp, sizeof(tmp[0]), 1);
227 }
Scott Wood2fa13912007-04-16 14:34:21 -0500228 }
229}
Kim Phillipsfd47a742007-12-20 14:09:22 -0600230#endif /* CONFIG_OF_LIBFDT */