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