blob: 5c289d0022605b0b6a9e4953d22442ca4638cd98 [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
30static void pci_init_bus(int bus, struct pci_region *reg)
31{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020032 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood2fa13912007-04-16 14:34:21 -050033 volatile pot83xx_t *pot = immr->ios.pot;
34 volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus];
35 struct pci_controller *hose = &pci_hose[bus];
36 u32 dev;
37 u16 reg16;
38 int i;
39
40 if (bus == 1)
41 pot += 3;
42
43 /* Setup outbound translation windows */
44 for (i = 0; i < 3; i++, reg++, pot++) {
45 if (reg->size == 0)
46 break;
47
48 hose->regions[i] = *reg;
49 hose->region_count++;
50
51 pot->potar = reg->bus_start >> 12;
52 pot->pobar = reg->phys_start >> 12;
53 pot->pocmr = ~(reg->size - 1) >> 12;
54
55 if (reg->flags & PCI_REGION_IO)
56 pot->pocmr |= POCMR_IO;
57#ifdef CONFIG_83XX_PCI_STREAMING
58 else if (reg->flags & PCI_REGION_PREFETCH)
59 pot->pocmr |= POCMR_SE;
60#endif
61
62 if (bus == 1)
63 pot->pocmr |= POCMR_DST;
64
65 pot->pocmr |= POCMR_EN;
66 }
67
68 /* Point inbound translation at RAM */
69 pci_ctrl->pitar1 = 0;
70 pci_ctrl->pibar1 = 0;
71 pci_ctrl->piebar1 = 0;
72 pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020073 PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1));
Scott Wood2fa13912007-04-16 14:34:21 -050074
75 i = hose->region_count++;
76 hose->regions[i].bus_start = 0;
77 hose->regions[i].phys_start = 0;
78 hose->regions[i].size = gd->ram_size;
Kumar Galaefa1f1d2009-02-06 09:49:31 -060079 hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
Scott Wood2fa13912007-04-16 14:34:21 -050080
Anton Vorontsov41d6cf82009-02-19 18:20:50 +030081 hose->first_busno = pci_last_busno() + 1;
Scott Wood2fa13912007-04-16 14:34:21 -050082 hose->last_busno = 0xff;
83
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084 pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80,
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020085 CONFIG_SYS_IMMR + 0x8304 + bus * 0x80);
Scott Wood2fa13912007-04-16 14:34:21 -050086
87 pci_register_hose(hose);
88
89 /*
90 * Write to Command register
91 */
92 reg16 = 0xff;
93 dev = PCI_BDF(hose->first_busno, 0, 0);
94 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &reg16);
95 reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
96 pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
97
98 /*
99 * Clear non-reserved bits in status register.
100 */
101 pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
102 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
103 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
104
105#ifdef CONFIG_PCI_SCAN_SHOW
106 printf("PCI: Bus Dev VenId DevId Class Int\n");
107#endif
Ira Snydere2e29ec2009-01-12 13:32:26 -0800108#ifndef CONFIG_PCISLAVE
Scott Wood2fa13912007-04-16 14:34:21 -0500109 /*
110 * Hose scan.
111 */
112 hose->last_busno = pci_hose_scan(hose);
Ira Snydere2e29ec2009-01-12 13:32:26 -0800113#endif
Scott Wood2fa13912007-04-16 14:34:21 -0500114}
115
116/*
117 * The caller must have already set OCCR, and the PCI_LAW BARs
118 * must have been set to cover all of the requested regions.
119 *
120 * If fewer than three regions are requested, then the region
121 * list is terminated with a region of size 0.
122 */
Peter Tysere2283322010-09-14 19:13:50 -0500123void mpc83xx_pci_init(int num_buses, struct pci_region **reg)
Scott Wood2fa13912007-04-16 14:34:21 -0500124{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200125 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood2fa13912007-04-16 14:34:21 -0500126 int i;
127
128 if (num_buses > MAX_BUSES) {
Robert P. J. Daycbd618f2015-12-16 12:25:42 -0500129 printf("%d PCI buses requested, %d supported\n",
Scott Wood2fa13912007-04-16 14:34:21 -0500130 num_buses, MAX_BUSES);
131
132 num_buses = MAX_BUSES;
133 }
134
135 pci_num_buses = num_buses;
136
137 /*
138 * Release PCI RST Output signal.
139 * Power on to RST high must be at least 100 ms as per PCI spec.
Peter Tysere2283322010-09-14 19:13:50 -0500140 * On warm boots only 1 ms is required, but we play it safe.
Scott Wood2fa13912007-04-16 14:34:21 -0500141 */
Peter Tysere2283322010-09-14 19:13:50 -0500142 udelay(100000);
Scott Wood2fa13912007-04-16 14:34:21 -0500143
144 for (i = 0; i < num_buses; i++)
145 immr->pci_ctrl[i].gcr = 1;
146
147 /*
148 * RST high to first config access must be at least 2^25 cycles
149 * as per PCI spec. This could be cut in half if we know we're
150 * running at 66MHz. This could be insufficiently long if we're
151 * running the PCI bus at significantly less than 33MHz.
152 */
153 udelay(1020000);
154
155 for (i = 0; i < num_buses; i++)
156 pci_init_bus(i, reg[i]);
157}
158
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700159#ifdef CONFIG_PCISLAVE
160
161#define PCI_FUNCTION_CONFIG 0x44
162#define PCI_FUNCTION_CFG_LOCK 0x20
163
164/*
165 * Unlock the configuration bit so that the host system can begin booting
166 *
167 * This should be used after you have:
168 * 1) Called mpc83xx_pci_init()
169 * 2) Set up your inbound translation windows to the appropriate size
170 */
171void mpc83xx_pcislave_unlock(int bus)
172{
173 struct pci_controller *hose = &pci_hose[bus];
174 u32 dev;
175 u16 reg16;
176
177 /* Unlock configuration lock in PCI function configuration register */
178 dev = PCI_BDF(hose->first_busno, 0, 0);
179 pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, &reg16);
180 reg16 &= ~(PCI_FUNCTION_CFG_LOCK);
181 pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16);
Ira Snydere2e29ec2009-01-12 13:32:26 -0800182
183 /* The configuration bit is now unlocked, so we can scan the bus */
184 hose->last_busno = pci_hose_scan(hose);
Ira W. Snyder51dc5e32008-08-22 11:00:14 -0700185}
186#endif
187
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500188#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900189void ft_pci_setup(void *blob, struct bd_info *bd)
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500190{
191 int nodeoffset;
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500192 int tmp[2];
Kim Phillipsfd47a742007-12-20 14:09:22 -0600193 const char *path;
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500194
195 if (pci_num_buses < 1)
196 return;
197
Kim Phillipsfd47a742007-12-20 14:09:22 -0600198 nodeoffset = fdt_path_offset(blob, "/aliases");
Kim Phillips3c6f3b72007-07-25 19:25:28 -0500199 if (nodeoffset >= 0) {
Kim Phillipsfd47a742007-12-20 14:09:22 -0600200 path = fdt_getprop(blob, nodeoffset, "pci0", NULL);
201 if (path) {
202 tmp[0] = cpu_to_be32(pci_hose[0].first_busno);
203 tmp[1] = cpu_to_be32(pci_hose[0].last_busno);
204 do_fixup_by_path(blob, path, "bus-range",
205 &tmp, sizeof(tmp), 1);
Kim Phillips21416812007-08-15 22:30:33 -0500206
Kim Phillipsfd47a742007-12-20 14:09:22 -0600207 tmp[0] = cpu_to_be32(gd->pci_clk);
208 do_fixup_by_path(blob, path, "clock-frequency",
209 &tmp, sizeof(tmp[0]), 1);
210 }
Scott Wood2fa13912007-04-16 14:34:21 -0500211
Kim Phillipsfd47a742007-12-20 14:09:22 -0600212 if (pci_num_buses < 2)
213 return;
Scott Wood2fa13912007-04-16 14:34:21 -0500214
Kim Phillipsfd47a742007-12-20 14:09:22 -0600215 path = fdt_getprop(blob, nodeoffset, "pci1", NULL);
216 if (path) {
Anton Vorontsov7c785472009-02-19 18:20:46 +0300217 tmp[0] = cpu_to_be32(pci_hose[1].first_busno);
218 tmp[1] = cpu_to_be32(pci_hose[1].last_busno);
Kim Phillipsfd47a742007-12-20 14:09:22 -0600219 do_fixup_by_path(blob, path, "bus-range",
220 &tmp, sizeof(tmp), 1);
Scott Wood2fa13912007-04-16 14:34:21 -0500221
Kim Phillipsfd47a742007-12-20 14:09:22 -0600222 tmp[0] = cpu_to_be32(gd->pci_clk);
223 do_fixup_by_path(blob, path, "clock-frequency",
224 &tmp, sizeof(tmp[0]), 1);
225 }
Scott Wood2fa13912007-04-16 14:34:21 -0500226 }
227}
Kim Phillipsfd47a742007-12-20 14:09:22 -0600228#endif /* CONFIG_OF_LIBFDT */