blob: 16f034d58f9ecab59ce604999951dd26748d7dcf [file] [log] [blame]
John Rigby5f91db72008-02-26 09:38:14 -07001/*
Detlev Zundel57ae8a52010-01-21 17:55:58 +01002 * Copyright (C) 2009-2010 DENX Software Engineering <wd@denx.de>
Kumar Gala4c2e3da2009-07-28 21:49:52 -05003 * Copyright (C) Freescale Semiconductor, Inc. 2006, 2007.
John Rigby5f91db72008-02-26 09:38:14 -07004 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25
Wolfgang Denk843efb12009-05-16 10:47:43 +020026#include <asm/io.h>
John Rigby5f91db72008-02-26 09:38:14 -070027#include <asm/mmu.h>
28#include <asm/global_data.h>
29#include <pci.h>
30#if defined(CONFIG_OF_LIBFDT)
31#include <libfdt.h>
32#include <fdt_support.h>
33#endif
34
35DECLARE_GLOBAL_DATA_PTR;
36
37/* System RAM mapped to PCI space */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#define CONFIG_PCI_SYS_MEM_BUS CONFIG_SYS_SDRAM_BASE
39#define CONFIG_PCI_SYS_MEM_PHYS CONFIG_SYS_SDRAM_BASE
John Rigby5f91db72008-02-26 09:38:14 -070040
41static struct pci_controller pci_hose;
42
43
44/**************************************************************************
45 * pci_init_board()
46 *
47 */
48void
49pci_init_board(void)
50{
Wolfgang Denk843efb12009-05-16 10:47:43 +020051 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
John Rigby5f91db72008-02-26 09:38:14 -070052 volatile law512x_t *pci_law;
53 volatile pot512x_t *pci_pot;
54 volatile pcictrl512x_t *pci_ctrl;
John Rigby5f91db72008-02-26 09:38:14 -070055 u16 reg16;
56 u32 reg32;
57 u32 dev;
Wolfgang Denk8b251262009-05-16 10:47:39 +020058 int i;
John Rigby5f91db72008-02-26 09:38:14 -070059 struct pci_controller *hose;
60
61 /* Set PCI divider for 33MHz */
Detlev Zundel57ae8a52010-01-21 17:55:58 +010062 reg32 = in_be32(&im->clk.scfr[0]);
John Rigby5f91db72008-02-26 09:38:14 -070063 reg32 &= ~(SCFR1_PCI_DIV_MASK);
64 reg32 |= SCFR1_PCI_DIV << SCFR1_PCI_DIV_SHIFT;
Detlev Zundel57ae8a52010-01-21 17:55:58 +010065 out_be32(&im->clk.scfr[0], reg32);
John Rigby5f91db72008-02-26 09:38:14 -070066
Wolfgang Denk843efb12009-05-16 10:47:43 +020067 clrsetbits_be32(&im->clk.scfr[0],
68 SCFR1_PCI_DIV_MASK,
69 SCFR1_PCI_DIV << SCFR1_PCI_DIV_SHIFT
70 );
71
72 pci_law = im->sysconf.pcilaw;
73 pci_pot = im->ios.pot;
74 pci_ctrl = &im->pci_ctrl;
John Rigby5f91db72008-02-26 09:38:14 -070075
76 hose = &pci_hose;
77
78 /*
79 * Release PCI RST Output signal
80 */
Wolfgang Denk843efb12009-05-16 10:47:43 +020081 out_be32(&pci_ctrl->gcr, 0);
John Rigby5f91db72008-02-26 09:38:14 -070082 udelay(2000);
Wolfgang Denk843efb12009-05-16 10:47:43 +020083 out_be32(&pci_ctrl->gcr, 1);
John Rigby5f91db72008-02-26 09:38:14 -070084
85 /* We need to wait at least a 1sec based on PCI specs */
Wolfgang Denk8b251262009-05-16 10:47:39 +020086 for (i = 0; i < 1000; i++)
87 udelay(1000);
John Rigby5f91db72008-02-26 09:38:14 -070088
89 /*
90 * Configure PCI Local Access Windows
91 */
Wolfgang Denk843efb12009-05-16 10:47:43 +020092 out_be32(&pci_law[0].bar, CONFIG_SYS_PCI_MEM_PHYS & LAWBAR_BAR);
93 out_be32(&pci_law[0].ar, LAWAR_EN | LAWAR_SIZE_512M);
John Rigby5f91db72008-02-26 09:38:14 -070094
Wolfgang Denk843efb12009-05-16 10:47:43 +020095 out_be32(&pci_law[1].bar, CONFIG_SYS_PCI_IO_PHYS & LAWBAR_BAR);
96 out_be32(&pci_law[1].ar, LAWAR_EN | LAWAR_SIZE_16M);
John Rigby5f91db72008-02-26 09:38:14 -070097
98 /*
99 * Configure PCI Outbound Translation Windows
100 */
101
102 /* PCI mem space - prefetch */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200103 out_be32(&pci_pot[0].potar,
104 (CONFIG_SYS_PCI_MEM_BASE >> 12) & POTAR_TA_MASK);
105 out_be32(&pci_pot[0].pobar,
106 (CONFIG_SYS_PCI_MEM_PHYS >> 12) & POBAR_BA_MASK);
107 out_be32(&pci_pot[0].pocmr,
108 POCMR_EN | POCMR_PRE | POCMR_CM_256M);
John Rigby5f91db72008-02-26 09:38:14 -0700109
110 /* PCI IO space */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200111 out_be32(&pci_pot[1].potar,
112 (CONFIG_SYS_PCI_IO_BASE >> 12) & POTAR_TA_MASK);
113 out_be32(&pci_pot[1].pobar,
114 (CONFIG_SYS_PCI_IO_PHYS >> 12) & POBAR_BA_MASK);
115 out_be32(&pci_pot[1].pocmr,
116 POCMR_EN | POCMR_IO | POCMR_CM_16M);
John Rigby5f91db72008-02-26 09:38:14 -0700117
118 /* PCI mmio - non-prefetch mem space */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200119 out_be32(&pci_pot[2].potar,
120 (CONFIG_SYS_PCI_MMIO_BASE >> 12) & POTAR_TA_MASK);
121 out_be32(&pci_pot[2].pobar,
122 (CONFIG_SYS_PCI_MMIO_PHYS >> 12) & POBAR_BA_MASK);
123 out_be32(&pci_pot[2].pocmr,
124 POCMR_EN | POCMR_CM_256M);
John Rigby5f91db72008-02-26 09:38:14 -0700125
126 /*
127 * Configure PCI Inbound Translation Windows
128 */
129
130 /* we need RAM mapped to PCI space for the devices to
131 * access main memory */
Wolfgang Denk843efb12009-05-16 10:47:43 +0200132 out_be32(&pci_ctrl[0].pitar1, 0x0);
133 out_be32(&pci_ctrl[0].pibar1, 0x0);
134 out_be32(&pci_ctrl[0].piebar1, 0x0);
135 out_be32(&pci_ctrl[0].piwar1,
136 PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
137 PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size) - 1));
John Rigby5f91db72008-02-26 09:38:14 -0700138
139 hose->first_busno = 0;
140 hose->last_busno = 0xff;
141
142 /* PCI memory prefetch space */
143 pci_set_region(hose->regions + 0,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200144 CONFIG_SYS_PCI_MEM_BASE,
145 CONFIG_SYS_PCI_MEM_PHYS,
146 CONFIG_SYS_PCI_MEM_SIZE,
John Rigby5f91db72008-02-26 09:38:14 -0700147 PCI_REGION_MEM|PCI_REGION_PREFETCH);
148
149 /* PCI memory space */
150 pci_set_region(hose->regions + 1,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200151 CONFIG_SYS_PCI_MMIO_BASE,
152 CONFIG_SYS_PCI_MMIO_PHYS,
153 CONFIG_SYS_PCI_MMIO_SIZE,
John Rigby5f91db72008-02-26 09:38:14 -0700154 PCI_REGION_MEM);
155
156 /* PCI IO space */
157 pci_set_region(hose->regions + 2,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200158 CONFIG_SYS_PCI_IO_BASE,
159 CONFIG_SYS_PCI_IO_PHYS,
160 CONFIG_SYS_PCI_IO_SIZE,
John Rigby5f91db72008-02-26 09:38:14 -0700161 PCI_REGION_IO);
162
163 /* System memory space */
164 pci_set_region(hose->regions + 3,
165 CONFIG_PCI_SYS_MEM_BUS,
166 CONFIG_PCI_SYS_MEM_PHYS,
167 gd->ram_size,
Kumar Galaff4e66e2009-02-06 09:49:31 -0600168 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
John Rigby5f91db72008-02-26 09:38:14 -0700169
170 hose->region_count = 4;
171
172 pci_setup_indirect(hose,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200173 (CONFIG_SYS_IMMR + 0x8300),
174 (CONFIG_SYS_IMMR + 0x8304));
John Rigby5f91db72008-02-26 09:38:14 -0700175
176 pci_register_hose(hose);
177
178 /*
179 * Write to Command register
180 */
181 reg16 = 0xff;
182 dev = PCI_BDF(hose->first_busno, 0, 0);
183 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &reg16);
184 reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
185 pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
186
187 /*
188 * Clear non-reserved bits in status register.
189 */
190 pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
191 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
192 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
193
194#ifdef CONFIG_PCI_SCAN_SHOW
195 printf("PCI: Bus Dev VenId DevId Class Int\n");
196#endif
197 /*
198 * Hose scan.
199 */
200 hose->last_busno = pci_hose_scan(hose);
201}
202
203#if defined(CONFIG_OF_LIBFDT)
204void ft_pci_setup(void *blob, bd_t *bd)
205{
206 int nodeoffset;
207 int tmp[2];
208 const char *path;
209
210 nodeoffset = fdt_path_offset(blob, "/aliases");
211 if (nodeoffset >= 0) {
212 path = fdt_getprop(blob, nodeoffset, "pci", NULL);
213 if (path) {
214 tmp[0] = cpu_to_be32(pci_hose.first_busno);
215 tmp[1] = cpu_to_be32(pci_hose.last_busno);
216 do_fixup_by_path(blob, path, "bus-range",
217 &tmp, sizeof(tmp), 1);
218
219 tmp[0] = cpu_to_be32(gd->pci_clk);
220 do_fixup_by_path(blob, path, "clock-frequency",
221 &tmp, sizeof(tmp[0]), 1);
222 }
223 }
224}
225#endif /* CONFIG_OF_LIBFDT */