blob: 74f824d2bca425c65817a19e48882bc1c2e1c8cb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott Wood49ea3b62007-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 Wood49ea3b62007-04-16 14:34:21 -05007 */
8
9#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Scott Wood49ea3b62007-04-16 14:34:21 -050011#include <pci.h>
Simon Glassc05ed002020-05-10 11:40:11 -060012#include <linux/delay.h>
Kim Phillips343d9102007-07-25 19:25:28 -050013
14#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Kim Phillips5b8bc602007-12-20 14:09:22 -060016#include <fdt_support.h>
Kim Phillips343d9102007-07-25 19:25:28 -050017#endif
18
Scott Wood49ea3b62007-04-16 14:34:21 -050019#include <asm/mpc8349_pci.h>
20
Scott Wood49ea3b62007-04-16 14:34:21 -050021#define MAX_BUSES 2
22
23DECLARE_GLOBAL_DATA_PTR;
24
25static struct pci_controller pci_hose[MAX_BUSES];
26static int pci_num_buses;
27
28static void pci_init_bus(int bus, struct pci_region *reg)
29{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood49ea3b62007-04-16 14:34:21 -050031 volatile pot83xx_t *pot = immr->ios.pot;
32 volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus];
33 struct pci_controller *hose = &pci_hose[bus];
34 u32 dev;
35 u16 reg16;
36 int i;
37
38 if (bus == 1)
39 pot += 3;
40
41 /* Setup outbound translation windows */
42 for (i = 0; i < 3; i++, reg++, pot++) {
43 if (reg->size == 0)
44 break;
45
46 hose->regions[i] = *reg;
47 hose->region_count++;
48
49 pot->potar = reg->bus_start >> 12;
50 pot->pobar = reg->phys_start >> 12;
51 pot->pocmr = ~(reg->size - 1) >> 12;
52
53 if (reg->flags & PCI_REGION_IO)
54 pot->pocmr |= POCMR_IO;
55#ifdef CONFIG_83XX_PCI_STREAMING
56 else if (reg->flags & PCI_REGION_PREFETCH)
57 pot->pocmr |= POCMR_SE;
58#endif
59
60 if (bus == 1)
61 pot->pocmr |= POCMR_DST;
62
63 pot->pocmr |= POCMR_EN;
64 }
65
66 /* Point inbound translation at RAM */
67 pci_ctrl->pitar1 = 0;
68 pci_ctrl->pibar1 = 0;
69 pci_ctrl->piebar1 = 0;
70 pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
Wolfgang Denk93e14592013-10-04 17:43:24 +020071 PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1));
Scott Wood49ea3b62007-04-16 14:34:21 -050072
73 i = hose->region_count++;
74 hose->regions[i].bus_start = 0;
75 hose->regions[i].phys_start = 0;
76 hose->regions[i].size = gd->ram_size;
Kumar Galaff4e66e2009-02-06 09:49:31 -060077 hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
Scott Wood49ea3b62007-04-16 14:34:21 -050078
Anton Vorontsov50a4d082009-02-19 18:20:50 +030079 hose->first_busno = pci_last_busno() + 1;
Scott Wood49ea3b62007-04-16 14:34:21 -050080 hose->last_busno = 0xff;
81
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020082 pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80,
Wolfgang Denk93e14592013-10-04 17:43:24 +020083 CONFIG_SYS_IMMR + 0x8304 + bus * 0x80);
Scott Wood49ea3b62007-04-16 14:34:21 -050084
85 pci_register_hose(hose);
86
87 /*
88 * Write to Command register
89 */
90 reg16 = 0xff;
91 dev = PCI_BDF(hose->first_busno, 0, 0);
92 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &reg16);
93 reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
94 pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
95
96 /*
97 * Clear non-reserved bits in status register.
98 */
99 pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
100 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
101 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
102
103#ifdef CONFIG_PCI_SCAN_SHOW
104 printf("PCI: Bus Dev VenId DevId Class Int\n");
105#endif
Ira Snyder75f35202009-01-12 13:32:26 -0800106#ifndef CONFIG_PCISLAVE
Scott Wood49ea3b62007-04-16 14:34:21 -0500107 /*
108 * Hose scan.
109 */
110 hose->last_busno = pci_hose_scan(hose);
Ira Snyder75f35202009-01-12 13:32:26 -0800111#endif
Scott Wood49ea3b62007-04-16 14:34:21 -0500112}
113
114/*
115 * The caller must have already set OCCR, and the PCI_LAW BARs
116 * must have been set to cover all of the requested regions.
117 *
118 * If fewer than three regions are requested, then the region
119 * list is terminated with a region of size 0.
120 */
Peter Tyser6aa3d3b2010-09-14 19:13:50 -0500121void mpc83xx_pci_init(int num_buses, struct pci_region **reg)
Scott Wood49ea3b62007-04-16 14:34:21 -0500122{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200123 volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood49ea3b62007-04-16 14:34:21 -0500124 int i;
125
126 if (num_buses > MAX_BUSES) {
Robert P. J. Dayd7b4ca22015-12-16 12:25:42 -0500127 printf("%d PCI buses requested, %d supported\n",
Scott Wood49ea3b62007-04-16 14:34:21 -0500128 num_buses, MAX_BUSES);
129
130 num_buses = MAX_BUSES;
131 }
132
133 pci_num_buses = num_buses;
134
135 /*
136 * Release PCI RST Output signal.
137 * Power on to RST high must be at least 100 ms as per PCI spec.
Peter Tyser6aa3d3b2010-09-14 19:13:50 -0500138 * On warm boots only 1 ms is required, but we play it safe.
Scott Wood49ea3b62007-04-16 14:34:21 -0500139 */
Peter Tyser6aa3d3b2010-09-14 19:13:50 -0500140 udelay(100000);
Scott Wood49ea3b62007-04-16 14:34:21 -0500141
142 for (i = 0; i < num_buses; i++)
143 immr->pci_ctrl[i].gcr = 1;
144
145 /*
146 * RST high to first config access must be at least 2^25 cycles
147 * as per PCI spec. This could be cut in half if we know we're
148 * running at 66MHz. This could be insufficiently long if we're
149 * running the PCI bus at significantly less than 33MHz.
150 */
151 udelay(1020000);
152
153 for (i = 0; i < num_buses; i++)
154 pci_init_bus(i, reg[i]);
155}
156
Ira W. Snyder4ff9aea2008-08-22 11:00:14 -0700157#ifdef CONFIG_PCISLAVE
158
159#define PCI_FUNCTION_CONFIG 0x44
160#define PCI_FUNCTION_CFG_LOCK 0x20
161
162/*
163 * Unlock the configuration bit so that the host system can begin booting
164 *
165 * This should be used after you have:
166 * 1) Called mpc83xx_pci_init()
167 * 2) Set up your inbound translation windows to the appropriate size
168 */
169void mpc83xx_pcislave_unlock(int bus)
170{
171 struct pci_controller *hose = &pci_hose[bus];
172 u32 dev;
173 u16 reg16;
174
175 /* Unlock configuration lock in PCI function configuration register */
176 dev = PCI_BDF(hose->first_busno, 0, 0);
177 pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, &reg16);
178 reg16 &= ~(PCI_FUNCTION_CFG_LOCK);
179 pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16);
Ira Snyder75f35202009-01-12 13:32:26 -0800180
181 /* The configuration bit is now unlocked, so we can scan the bus */
182 hose->last_busno = pci_hose_scan(hose);
Ira W. Snyder4ff9aea2008-08-22 11:00:14 -0700183}
184#endif
185
Kim Phillips343d9102007-07-25 19:25:28 -0500186#if defined(CONFIG_OF_LIBFDT)
187void ft_pci_setup(void *blob, bd_t *bd)
188{
189 int nodeoffset;
Kim Phillips343d9102007-07-25 19:25:28 -0500190 int tmp[2];
Kim Phillips5b8bc602007-12-20 14:09:22 -0600191 const char *path;
Kim Phillips343d9102007-07-25 19:25:28 -0500192
193 if (pci_num_buses < 1)
194 return;
195
Kim Phillips5b8bc602007-12-20 14:09:22 -0600196 nodeoffset = fdt_path_offset(blob, "/aliases");
Kim Phillips343d9102007-07-25 19:25:28 -0500197 if (nodeoffset >= 0) {
Kim Phillips5b8bc602007-12-20 14:09:22 -0600198 path = fdt_getprop(blob, nodeoffset, "pci0", NULL);
199 if (path) {
200 tmp[0] = cpu_to_be32(pci_hose[0].first_busno);
201 tmp[1] = cpu_to_be32(pci_hose[0].last_busno);
202 do_fixup_by_path(blob, path, "bus-range",
203 &tmp, sizeof(tmp), 1);
Kim Phillips3fde9e82007-08-15 22:30:33 -0500204
Kim Phillips5b8bc602007-12-20 14:09:22 -0600205 tmp[0] = cpu_to_be32(gd->pci_clk);
206 do_fixup_by_path(blob, path, "clock-frequency",
207 &tmp, sizeof(tmp[0]), 1);
208 }
Kim Phillips343d9102007-07-25 19:25:28 -0500209
Kim Phillips5b8bc602007-12-20 14:09:22 -0600210 if (pci_num_buses < 2)
211 return;
Kim Phillips343d9102007-07-25 19:25:28 -0500212
Kim Phillips5b8bc602007-12-20 14:09:22 -0600213 path = fdt_getprop(blob, nodeoffset, "pci1", NULL);
214 if (path) {
Anton Vorontsova5878d42009-02-19 18:20:46 +0300215 tmp[0] = cpu_to_be32(pci_hose[1].first_busno);
216 tmp[1] = cpu_to_be32(pci_hose[1].last_busno);
Kim Phillips5b8bc602007-12-20 14:09:22 -0600217 do_fixup_by_path(blob, path, "bus-range",
218 &tmp, sizeof(tmp), 1);
Kim Phillips3fde9e82007-08-15 22:30:33 -0500219
Kim Phillips5b8bc602007-12-20 14:09:22 -0600220 tmp[0] = cpu_to_be32(gd->pci_clk);
221 do_fixup_by_path(blob, path, "clock-frequency",
222 &tmp, sizeof(tmp[0]), 1);
223 }
Kim Phillips343d9102007-07-25 19:25:28 -0500224 }
225}
Kim Phillips5b8bc602007-12-20 14:09:22 -0600226#endif /* CONFIG_OF_LIBFDT */