blob: 5b5c91548de2db7dd38ca02fb8954b88854bb540 [file] [log] [blame]
wdenka1191902005-01-09 17:12:27 +00001/*
2 * IXP PCI Init
Michael Schwingen29161f42011-05-23 00:00:12 +02003 *
4 * (C) Copyright 2011
5 * Michael Schwingen, michael@schwingen.org
wdenka1191902005-01-09 17:12:27 +00006 * (C) Copyright 2004 eslab.whut.edu.cn
7 * Yue Hu(huyue_whut@yahoo.com.cn), Ligong Xue(lgxue@hotmail.com)
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
wdenka1191902005-01-09 17:12:27 +000028#include <common.h>
wdenka1191902005-01-09 17:12:27 +000029#include <asm/processor.h>
30#include <asm/io.h>
31#include <pci.h>
32#include <asm/arch/ixp425.h>
33#include <asm/arch/ixp425pci.h>
34
Michael Schwingen29161f42011-05-23 00:00:12 +020035DECLARE_GLOBAL_DATA_PTR;
36
37static void non_prefetch_read(unsigned int addr, unsigned int cmd,
38 unsigned int *data);
39static void non_prefetch_write(unsigned int addr, unsigned int cmd,
40 unsigned int data);
41
42/*define the sub vendor and subsystem to be used */
43#define IXP425_PCI_SUB_VENDOR_SYSTEM 0x00000000
wdenka1191902005-01-09 17:12:27 +000044
45#define PCI_MEMORY_BUS 0x00000000
Michael Schwingen29161f42011-05-23 00:00:12 +020046#define PCI_MEMORY_PHY 0x00000000
wdenka1191902005-01-09 17:12:27 +000047#define PCI_MEMORY_SIZE 0x04000000
48
Michael Schwingen29161f42011-05-23 00:00:12 +020049#define PCI_MEM_BUS 0x48000000
wdenka1191902005-01-09 17:12:27 +000050#define PCI_MEM_PHY 0x00000000
51#define PCI_MEM_SIZE 0x04000000
52
Michael Schwingen29161f42011-05-23 00:00:12 +020053#define PCI_IO_BUS 0x00000000
54#define PCI_IO_PHY 0x00000000
55#define PCI_IO_SIZE 0x00010000
wdenka1191902005-01-09 17:12:27 +000056
Michael Schwingen29161f42011-05-23 00:00:12 +020057/* build address value for config sycle */
58static unsigned int pci_config_addr(pci_dev_t bdf, unsigned int reg)
59{
60 unsigned int bus = PCI_BUS(bdf);
61 unsigned int dev = PCI_DEV(bdf);
62 unsigned int func = PCI_FUNC(bdf);
63 unsigned int addr;
wdenka1191902005-01-09 17:12:27 +000064
Michael Schwingen29161f42011-05-23 00:00:12 +020065 if (bus) { /* secondary bus, use type 1 config cycle */
66 addr = bdf | (reg & ~3) | 1;
67 } else {
68 /*
69 primary bus, type 0 config cycle. address bits 31:28
70 specify the device 10:8 specify the function
71 */
72 addr = BIT((31 - dev)) | (func << 8) | (reg & ~3);
73 }
wdenka1191902005-01-09 17:12:27 +000074
Michael Schwingen29161f42011-05-23 00:00:12 +020075 return addr;
76}
77
78static int pci_config_status(void)
79{
80 unsigned int regval;
81
82 regval = readl(PCI_CSR_BASE + PCI_ISR_OFFSET);
83 if ((regval & PCI_ISR_PFE) == 0)
84 return OK;
85
86 /* no device present, make sure that the master abort bit is reset */
87 writel(PCI_ISR_PFE, PCI_CSR_BASE + PCI_ISR_OFFSET);
88 return ERROR;
89}
90
91static int pci_ixp_hose_read_config_dword(struct pci_controller *hose,
92 pci_dev_t bdf, int where, unsigned int *val)
wdenka1191902005-01-09 17:12:27 +000093{
94 unsigned int retval;
95 unsigned int addr;
Michael Schwingen29161f42011-05-23 00:00:12 +020096 int stat;
wdenka1191902005-01-09 17:12:27 +000097
Michael Schwingen29161f42011-05-23 00:00:12 +020098 debug("pci_ixp_hose_read_config_dword: bdf %x, reg %x", bdf, where);
wdenka1191902005-01-09 17:12:27 +000099 /*Set the address to be read */
Michael Schwingen29161f42011-05-23 00:00:12 +0200100 addr = pci_config_addr(bdf, where);
101 non_prefetch_read(addr, NP_CMD_CONFIGREAD, &retval);
wdenka1191902005-01-09 17:12:27 +0000102 *val = retval;
103
Michael Schwingen29161f42011-05-23 00:00:12 +0200104 stat = pci_config_status();
105 if (stat < 0)
106 *val = -1;
107 debug("-> val %x, status %x\n", *val, stat);
108 return stat;
wdenka1191902005-01-09 17:12:27 +0000109}
110
Michael Schwingen29161f42011-05-23 00:00:12 +0200111static int pci_ixp_hose_read_config_word(struct pci_controller *hose,
112 pci_dev_t bdf, int where, unsigned short *val)
wdenka1191902005-01-09 17:12:27 +0000113{
114 unsigned int n;
115 unsigned int retval;
116 unsigned int addr;
117 unsigned int byteEnables;
Michael Schwingen29161f42011-05-23 00:00:12 +0200118 int stat;
wdenka1191902005-01-09 17:12:27 +0000119
Michael Schwingen29161f42011-05-23 00:00:12 +0200120 debug("pci_ixp_hose_read_config_word: bdf %x, reg %x", bdf, where);
wdenka1191902005-01-09 17:12:27 +0000121 n = where % 4;
122 /*byte enables are 4 bits active low, the position of each
123 bit maps to the byte that it enables */
124 byteEnables =
Michael Schwingen29161f42011-05-23 00:00:12 +0200125 (~(BIT(n) | BIT((n + 1)))) &
wdenka1191902005-01-09 17:12:27 +0000126 IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
127 byteEnables = byteEnables << PCI_NP_CBE_BESL;
wdenka1191902005-01-09 17:12:27 +0000128 /*Set the address to be read */
Michael Schwingen29161f42011-05-23 00:00:12 +0200129 addr = pci_config_addr(bdf, where);
130 non_prefetch_read(addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
wdenka1191902005-01-09 17:12:27 +0000131
132 /*Pick out the word we are interested in */
Michael Schwingen29161f42011-05-23 00:00:12 +0200133 *val = retval >> (8 * n);
wdenka1191902005-01-09 17:12:27 +0000134
Michael Schwingen29161f42011-05-23 00:00:12 +0200135 stat = pci_config_status();
136 if (stat < 0)
137 *val = -1;
138 debug("-> val %x, status %x\n", *val, stat);
139 return stat;
wdenka1191902005-01-09 17:12:27 +0000140}
141
Michael Schwingen29161f42011-05-23 00:00:12 +0200142static int pci_ixp_hose_read_config_byte(struct pci_controller *hose,
143 pci_dev_t bdf, int where, unsigned char *val)
wdenka1191902005-01-09 17:12:27 +0000144{
145 unsigned int retval;
146 unsigned int n;
147 unsigned int byteEnables;
148 unsigned int addr;
Michael Schwingen29161f42011-05-23 00:00:12 +0200149 int stat;
wdenka1191902005-01-09 17:12:27 +0000150
Michael Schwingen29161f42011-05-23 00:00:12 +0200151 debug("pci_ixp_hose_read_config_byte: bdf %x, reg %x", bdf, where);
wdenka1191902005-01-09 17:12:27 +0000152 n = where % 4;
153 /*byte enables are 4 bits, active low, the position of each
154 bit maps to the byte that it enables */
Michael Schwingen29161f42011-05-23 00:00:12 +0200155 byteEnables = (~BIT(n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
wdenka1191902005-01-09 17:12:27 +0000156 byteEnables = byteEnables << PCI_NP_CBE_BESL;
157
wdenka1191902005-01-09 17:12:27 +0000158 /*Set the address to be read */
Michael Schwingen29161f42011-05-23 00:00:12 +0200159 addr = pci_config_addr(bdf, where);
160 non_prefetch_read(addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
wdenka1191902005-01-09 17:12:27 +0000161 /*Pick out the byte we are interested in */
Michael Schwingen29161f42011-05-23 00:00:12 +0200162 *val = retval >> (8 * n);
wdenka1191902005-01-09 17:12:27 +0000163
Michael Schwingen29161f42011-05-23 00:00:12 +0200164 stat = pci_config_status();
165 if (stat < 0)
166 *val = -1;
167 debug("-> val %x, status %x\n", *val, stat);
168 return stat;
wdenka1191902005-01-09 17:12:27 +0000169}
170
Michael Schwingen29161f42011-05-23 00:00:12 +0200171static int pci_ixp_hose_write_config_byte(struct pci_controller *hose,
172 pci_dev_t bdf, int where, unsigned char val)
wdenka1191902005-01-09 17:12:27 +0000173{
174 unsigned int addr;
175 unsigned int byteEnables;
176 unsigned int n;
177 unsigned int ldata;
Michael Schwingen29161f42011-05-23 00:00:12 +0200178 int stat;
wdenka1191902005-01-09 17:12:27 +0000179
Michael Schwingen29161f42011-05-23 00:00:12 +0200180 debug("pci_ixp_hose_write_config_byte: bdf %x, reg %x, val %x",
181 bdf, where, val);
wdenka1191902005-01-09 17:12:27 +0000182 n = where % 4;
183 /*byte enables are 4 bits active low, the position of each
184 bit maps to the byte that it enables */
Michael Schwingen29161f42011-05-23 00:00:12 +0200185 byteEnables = (~BIT(n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
wdenka1191902005-01-09 17:12:27 +0000186 byteEnables = byteEnables << PCI_NP_CBE_BESL;
187 ldata = val << (8 * n);
wdenka1191902005-01-09 17:12:27 +0000188 /*Set the address to be written */
Michael Schwingen29161f42011-05-23 00:00:12 +0200189 addr = pci_config_addr(bdf, where);
190 non_prefetch_write(addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
wdenka1191902005-01-09 17:12:27 +0000191
Michael Schwingen29161f42011-05-23 00:00:12 +0200192 stat = pci_config_status();
193 debug("-> status %x\n", stat);
194 return stat;
wdenka1191902005-01-09 17:12:27 +0000195}
196
Michael Schwingen29161f42011-05-23 00:00:12 +0200197static int pci_ixp_hose_write_config_word(struct pci_controller *hose,
198 pci_dev_t bdf, int where, unsigned short val)
wdenka1191902005-01-09 17:12:27 +0000199{
200 unsigned int addr;
201 unsigned int byteEnables;
202 unsigned int n;
203 unsigned int ldata;
Michael Schwingen29161f42011-05-23 00:00:12 +0200204 int stat;
wdenka1191902005-01-09 17:12:27 +0000205
Michael Schwingen29161f42011-05-23 00:00:12 +0200206 debug("pci_ixp_hose_write_config_word: bdf %x, reg %x, val %x",
207 bdf, where, val);
wdenka1191902005-01-09 17:12:27 +0000208 n = where % 4;
209 /*byte enables are 4 bits active low, the position of each
210 bit maps to the byte that it enables */
211 byteEnables =
Michael Schwingen29161f42011-05-23 00:00:12 +0200212 (~(BIT(n) | BIT((n + 1)))) &
wdenka1191902005-01-09 17:12:27 +0000213 IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
214 byteEnables = byteEnables << PCI_NP_CBE_BESL;
215 ldata = val << (8 * n);
wdenka1191902005-01-09 17:12:27 +0000216 /*Set the address to be written */
Michael Schwingen29161f42011-05-23 00:00:12 +0200217 addr = pci_config_addr(bdf, where);
218 non_prefetch_write(addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
wdenka1191902005-01-09 17:12:27 +0000219
Michael Schwingen29161f42011-05-23 00:00:12 +0200220 stat = pci_config_status();
221 debug("-> status %x\n", stat);
222 return stat;
wdenka1191902005-01-09 17:12:27 +0000223}
224
Michael Schwingen29161f42011-05-23 00:00:12 +0200225static int pci_ixp_hose_write_config_dword(struct pci_controller *hose,
226 pci_dev_t bdf, int where, unsigned int val)
wdenka1191902005-01-09 17:12:27 +0000227{
228 unsigned int addr;
Michael Schwingen29161f42011-05-23 00:00:12 +0200229 int stat;
wdenka1191902005-01-09 17:12:27 +0000230
Michael Schwingen29161f42011-05-23 00:00:12 +0200231 debug("pci_ixp_hose_write_config_dword: bdf %x, reg %x, val %x",
232 bdf, where, val);
wdenka1191902005-01-09 17:12:27 +0000233 /*Set the address to be written */
Michael Schwingen29161f42011-05-23 00:00:12 +0200234 addr = pci_config_addr(bdf, where);
235 non_prefetch_write(addr, NP_CMD_CONFIGWRITE, val);
wdenka1191902005-01-09 17:12:27 +0000236
Michael Schwingen29161f42011-05-23 00:00:12 +0200237 stat = pci_config_status();
238 debug("-> status %x\n", stat);
239 return stat;
wdenka1191902005-01-09 17:12:27 +0000240}
241
Michael Schwingen29161f42011-05-23 00:00:12 +0200242static void non_prefetch_read(unsigned int addr,
243 unsigned int cmd, unsigned int *data)
wdenka1191902005-01-09 17:12:27 +0000244{
Michael Schwingen29161f42011-05-23 00:00:12 +0200245 writel(addr, PCI_CSR_BASE + PCI_NP_AD_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000246
247 /*set up and execute the read */
Michael Schwingen29161f42011-05-23 00:00:12 +0200248 writel(cmd, PCI_CSR_BASE + PCI_NP_CBE_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000249
250 /*The result of the read is now in np_rdata */
Michael Schwingen29161f42011-05-23 00:00:12 +0200251 *data = readl(PCI_CSR_BASE + PCI_NP_RDATA_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000252
253 return;
254}
255
Michael Schwingen29161f42011-05-23 00:00:12 +0200256static void non_prefetch_write(unsigned int addr,
257 unsigned int cmd, unsigned int data)
wdenka1191902005-01-09 17:12:27 +0000258{
259
Michael Schwingen29161f42011-05-23 00:00:12 +0200260 writel(addr, PCI_CSR_BASE + PCI_NP_AD_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000261 /*set up the write */
Michael Schwingen29161f42011-05-23 00:00:12 +0200262 writel(cmd, PCI_CSR_BASE + PCI_NP_CBE_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000263 /*Execute the write by writing to NP_WDATA */
Michael Schwingen29161f42011-05-23 00:00:12 +0200264 writel(data, PCI_CSR_BASE + PCI_NP_WDATA_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000265
266 return;
267}
268
Michael Schwingen29161f42011-05-23 00:00:12 +0200269static void crp_write(unsigned int offset, unsigned int data)
wdenka1191902005-01-09 17:12:27 +0000270{
Michael Schwingen29161f42011-05-23 00:00:12 +0200271 /*
272 * The CRP address register bit 16 indicates that we want to do a
273 * write
274 */
275 writel(PCI_CRP_WRITE | offset, PCI_CSR_BASE + PCI_CRP_AD_CBE_OFFSET);
276 writel(data, PCI_CSR_BASE + PCI_CRP_WDATA_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000277}
278
Michael Schwingen29161f42011-05-23 00:00:12 +0200279void pci_ixp_init(struct pci_controller *hose)
wdenka1191902005-01-09 17:12:27 +0000280{
Michael Schwingen29161f42011-05-23 00:00:12 +0200281 unsigned int csr;
wdenka1191902005-01-09 17:12:27 +0000282
Michael Schwingen29161f42011-05-23 00:00:12 +0200283 /*
284 * Specify that the AHB bus is operating in big endian mode. Set up
285 * byte lane swapping between little-endian PCI and the big-endian
286 * AHB bus
287 */
288#ifdef __ARMEB__
289 csr = PCI_CSR_ABE | PCI_CSR_PDS | PCI_CSR_ADS;
290#else
291 csr = PCI_CSR_ABE;
292#endif
293 writel(csr, PCI_CSR_BASE + PCI_CSR_OFFSET);
294
295 writel(0, PCI_CSR_BASE + PCI_INTEN_OFFSET);
296
297 /*
298 * We configure the PCI inbound memory windows to be
299 * 1:1 mapped to SDRAM
300 */
301 crp_write(PCI_CFG_BASE_ADDRESS_0, 0x00000000);
302 crp_write(PCI_CFG_BASE_ADDRESS_1, 0x01000000);
303 crp_write(PCI_CFG_BASE_ADDRESS_2, 0x02000000);
304 crp_write(PCI_CFG_BASE_ADDRESS_3, 0x03000000);
305
306 /*
307 * Enable CSR window at 64 MiB to allow PCI masters
308 * to continue prefetching past 64 MiB boundary.
309 */
310 crp_write(PCI_CFG_BASE_ADDRESS_4, 0x04000000);
311 /*
312 * Enable the IO window to be way up high, at 0xfffffc00
313 */
314 crp_write(PCI_CFG_BASE_ADDRESS_5, 0xfffffc01);
315
316 /*Setup PCI-AHB and AHB-PCI address mappings */
317 writel(0x00010203, PCI_CSR_BASE + PCI_AHBMEMBASE_OFFSET);
318
319 writel(0x00000000, PCI_CSR_BASE + PCI_AHBIOBASE_OFFSET);
320
321 writel(0x48494a4b, PCI_CSR_BASE + PCI_PCIMEMBASE_OFFSET);
322
323 crp_write(PCI_CFG_SUB_VENDOR_ID, IXP425_PCI_SUB_VENDOR_SYSTEM);
324
325 crp_write(PCI_CFG_COMMAND, PCI_CFG_CMD_MAE | PCI_CFG_CMD_BME);
326 udelay(1000);
327
328 /* clear error bits in status register */
329 writel(PCI_ISR_PSE | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE,
330 PCI_CSR_BASE + PCI_ISR_OFFSET);
331
332 /*
333 * Set Initialize Complete in PCI Control Register: allow IXP4XX to
334 * respond to PCI configuration cycles.
335 */
336 csr |= PCI_CSR_IC;
337 writel(csr, PCI_CSR_BASE + PCI_CSR_OFFSET);
wdenka1191902005-01-09 17:12:27 +0000338
339 hose->first_busno = 0;
Michael Schwingen29161f42011-05-23 00:00:12 +0200340 hose->last_busno = 0;
wdenka1191902005-01-09 17:12:27 +0000341
342 /* System memory space */
Michael Schwingen29161f42011-05-23 00:00:12 +0200343 pci_set_region(hose->regions + 0,
344 PCI_MEMORY_BUS,
345 PCI_MEMORY_PHY, PCI_MEMORY_SIZE, PCI_REGION_SYS_MEMORY);
wdenka1191902005-01-09 17:12:27 +0000346
347 /* PCI memory space */
Michael Schwingen29161f42011-05-23 00:00:12 +0200348 pci_set_region(hose->regions + 1,
349 PCI_MEM_BUS,
350 PCI_MEM_PHY, PCI_MEM_SIZE, PCI_REGION_MEM);
wdenka1191902005-01-09 17:12:27 +0000351 /* PCI I/O space */
Michael Schwingen29161f42011-05-23 00:00:12 +0200352 pci_set_region(hose->regions + 2,
353 PCI_IO_BUS, PCI_IO_PHY, PCI_IO_SIZE, PCI_REGION_IO);
wdenka1191902005-01-09 17:12:27 +0000354
355 hose->region_count = 3;
356
Michael Schwingen29161f42011-05-23 00:00:12 +0200357 pci_set_ops(hose,
358 pci_ixp_hose_read_config_byte,
359 pci_ixp_hose_read_config_word,
360 pci_ixp_hose_read_config_dword,
361 pci_ixp_hose_write_config_byte,
362 pci_ixp_hose_write_config_word,
363 pci_ixp_hose_write_config_dword);
wdenka1191902005-01-09 17:12:27 +0000364
Michael Schwingen29161f42011-05-23 00:00:12 +0200365 pci_register_hose(hose);
366 hose->last_busno = pci_hose_scan(hose);
wdenka1191902005-01-09 17:12:27 +0000367}