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