blob: 33c1cfff56ca15d18ad396187fd9c51cdf21f65e [file] [log] [blame]
wdenka1191902005-01-09 17:12:27 +00001/*
2 * IXP PCI Init
3 * (C) Copyright 2004 eslab.whut.edu.cn
4 * Yue Hu(huyue_whut@yahoo.com.cn), Ligong Xue(lgxue@hotmail.com)
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25
26#include <common.h>
27
28#ifdef CONFIG_PCI
29
30#include <asm/processor.h>
31#include <asm/io.h>
32#include <pci.h>
33#include <asm/arch/ixp425.h>
34#include <asm/arch/ixp425pci.h>
35
36static void non_prefetch_read (unsigned int addr, unsigned int cmd,
37 unsigned int *data);
38static void non_prefetch_write (unsigned int addr, unsigned int cmd,
39 unsigned int data);
40static void configure_pins (void);
41static void sys_pci_gpio_clock_config (void);
42static void pci_bus_scan (void);
43static int pci_device_exists (unsigned int deviceNo);
44static void sys_pci_bar_info_get (unsigned int devnum, unsigned int bus,
45 unsigned int dev, unsigned int func);
46static void sys_pci_device_bars_write (void);
47static void calc_bars (PciBar * Bars[], unsigned int nBars,
48 unsigned int startAddr);
49
50#define PCI_MEMORY_BUS 0x00000000
51#define PCI_MEMORY_PHY 0x48000000
52#define PCI_MEMORY_SIZE 0x04000000
53
54#define PCI_MEM_BUS 0x40000000
55#define PCI_MEM_PHY 0x00000000
56#define PCI_MEM_SIZE 0x04000000
57
58#define PCI_IO_BUS 0x40000000
59#define PCI_IO_PHY 0x50000000
60#define PCI_IO_SIZE 0x10000000
61
62struct pci_controller hose;
63
64unsigned int nDevices;
65unsigned int nMBars;
66unsigned int nIOBars;
67PciBar *memBars[IXP425_PCI_MAX_BAR];
68PciBar *ioBars[IXP425_PCI_MAX_BAR];
69PciDevice devices[IXP425_PCI_MAX_FUNC_ON_BUS];
70
wdenk289f9322005-01-12 00:15:14 +000071void out_8 (volatile unsigned *addr, char val)
wdenka1191902005-01-09 17:12:27 +000072{
73 *addr = val;
74}
wdenk289f9322005-01-12 00:15:14 +000075
76void out_le16 (volatile unsigned *addr, unsigned short val)
wdenka1191902005-01-09 17:12:27 +000077{
78 *addr = cpu_to_le16 (val);
79}
wdenk289f9322005-01-12 00:15:14 +000080
81void out_le32 (volatile unsigned *addr, unsigned int val)
wdenka1191902005-01-09 17:12:27 +000082{
83 *addr = cpu_to_le32 (val);
84}
85
wdenk289f9322005-01-12 00:15:14 +000086unsigned char in_8 (volatile unsigned *addr)
wdenka1191902005-01-09 17:12:27 +000087{
88 unsigned char val;
89
90 val = *addr;
91 return val;
92}
wdenk289f9322005-01-12 00:15:14 +000093
94unsigned short in_le16 (volatile unsigned *addr)
wdenka1191902005-01-09 17:12:27 +000095{
96 unsigned short val;
97
98 val = *addr;
99 val = le16_to_cpu (val);
100 return val;
101}
wdenk289f9322005-01-12 00:15:14 +0000102
103unsigned in_le32 (volatile unsigned *addr)
wdenka1191902005-01-09 17:12:27 +0000104{
105 unsigned int val;
106
107 val = *addr;
108 val = le32_to_cpu (val);
109 return val;
110}
111
112int pci_read_config_dword (pci_dev_t dev, int where, unsigned int *val)
113{
114 unsigned int retval;
115 unsigned int addr;
116
117 /*address bits 31:28 specify the device 10:8 specify the function */
118 /*Set the address to be read */
119 addr = BIT ((31 - dev)) | (where & ~3);
120 non_prefetch_read (addr, NP_CMD_CONFIGREAD, &retval);
121
122 *val = retval;
123
124 return (OK);
125}
126
127int pci_read_config_word (pci_dev_t dev, int where, unsigned short *val)
128{
129 unsigned int n;
130 unsigned int retval;
131 unsigned int addr;
132 unsigned int byteEnables;
133
134 n = where % 4;
135 /*byte enables are 4 bits active low, the position of each
136 bit maps to the byte that it enables */
137 byteEnables =
138 (~(BIT (n) | BIT ((n + 1)))) &
139 IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
140 byteEnables = byteEnables << PCI_NP_CBE_BESL;
141 /*address bits 31:28 specify the device 10:8 specify the function */
142 /*Set the address to be read */
143 addr = BIT ((31 - dev)) | (where & ~3);
144 non_prefetch_read (addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
145
146 /*Pick out the word we are interested in */
147 *val = (retval >> (8 * n));
148
149 return (OK);
150}
151
152int pci_read_config_byte (pci_dev_t dev, int where, unsigned char *val)
153{
154 unsigned int retval;
155 unsigned int n;
156 unsigned int byteEnables;
157 unsigned int addr;
158
159 n = where % 4;
160 /*byte enables are 4 bits, active low, the position of each
161 bit maps to the byte that it enables */
162 byteEnables = (~BIT (n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
163 byteEnables = byteEnables << PCI_NP_CBE_BESL;
164
165 /*address bits 31:28 specify the device, 10:8 specify the function */
166 /*Set the address to be read */
167 addr = BIT ((31 - dev)) | (where & ~3);
168 non_prefetch_read (addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
169 /*Pick out the byte we are interested in */
170 *val = (retval >> (8 * n));
171
172 return (OK);
173}
174
175int pci_write_config_byte (pci_dev_t dev, int where, unsigned char val)
176{
177 unsigned int addr;
178 unsigned int byteEnables;
179 unsigned int n;
180 unsigned int ldata;
181
182 n = where % 4;
183 /*byte enables are 4 bits active low, the position of each
184 bit maps to the byte that it enables */
185 byteEnables = (~BIT (n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
186 byteEnables = byteEnables << PCI_NP_CBE_BESL;
187 ldata = val << (8 * n);
188 /*address bits 31:28 specify the device 10:8 specify the function */
189 /*Set the address to be written */
190 addr = BIT ((31 - dev)) | (where & ~3);
191 non_prefetch_write (addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
192
193 return (OK);
194}
195
196int pci_write_config_word (pci_dev_t dev, int where, unsigned short val)
197{
198 unsigned int addr;
199 unsigned int byteEnables;
200 unsigned int n;
201 unsigned int ldata;
202
203 n = where % 4;
204 /*byte enables are 4 bits active low, the position of each
205 bit maps to the byte that it enables */
206 byteEnables =
207 (~(BIT (n) | BIT ((n + 1)))) &
208 IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
209 byteEnables = byteEnables << PCI_NP_CBE_BESL;
210 ldata = val << (8 * n);
211 /*address bits 31:28 specify the device 10:8 specify the function */
212 /*Set the address to be written */
213 addr = BIT (31 - dev) | (where & ~3);
214 non_prefetch_write (addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
215
216 return (OK);
217}
218
219int pci_write_config_dword (pci_dev_t dev, int where, unsigned int val)
220{
221 unsigned int addr;
222
223 /*address bits 31:28 specify the device 10:8 specify the function */
224 /*Set the address to be written */
225 addr = BIT (31 - dev) | (where & ~3);
226 non_prefetch_write (addr, NP_CMD_CONFIGWRITE, val);
227
228 return (OK);
229}
230
231void non_prefetch_read (unsigned int addr,
232 unsigned int cmd, unsigned int *data)
233{
234 REG_WRITE (PCI_CSR_BASE, PCI_NP_AD_OFFSET, addr);
235
236 /*set up and execute the read */
237 REG_WRITE (PCI_CSR_BASE, PCI_NP_CBE_OFFSET, cmd);
238
239 /*The result of the read is now in np_rdata */
240 REG_READ (PCI_CSR_BASE, PCI_NP_RDATA_OFFSET, *data);
241
242 return;
243}
244
245void non_prefetch_write (unsigned int addr,
246 unsigned int cmd, unsigned int data)
247{
248
249 REG_WRITE (PCI_CSR_BASE, PCI_NP_AD_OFFSET, addr);
250 /*set up the write */
251 REG_WRITE (PCI_CSR_BASE, PCI_NP_CBE_OFFSET, cmd);
252 /*Execute the write by writing to NP_WDATA */
253 REG_WRITE (PCI_CSR_BASE, PCI_NP_WDATA_OFFSET, data);
254
255 return;
256}
257
258/*
259 * PCI controller config registers are accessed through these functions
260 * i.e. these allow us to set up our own BARs etc.
261 */
262void crp_read (unsigned int offset, unsigned int *data)
263{
264 REG_WRITE (PCI_CSR_BASE, PCI_CRP_AD_CBE_OFFSET, offset);
265 REG_READ (PCI_CSR_BASE, PCI_CRP_RDATA_OFFSET, *data);
266}
267
268void crp_write (unsigned int offset, unsigned int data)
269{
270 /*The CRP address register bit 16 indicates that we want to do a write */
271 REG_WRITE (PCI_CSR_BASE, PCI_CRP_AD_CBE_OFFSET,
272 PCI_CRP_WRITE | offset);
273 REG_WRITE (PCI_CSR_BASE, PCI_CRP_WDATA_OFFSET, data);
274}
275
276/*struct pci_controller *hose*/
277void pci_ixp_init (struct pci_controller *hose)
278{
279 unsigned int regval;
280
281 hose->first_busno = 0;
282 hose->last_busno = 0x00;
283
284 /* System memory space */
285 pci_set_region (hose->regions + 0,
286 PCI_MEMORY_BUS,
287 PCI_MEMORY_PHY, PCI_MEMORY_SIZE, PCI_REGION_MEMORY);
288
289 /* PCI memory space */
290 pci_set_region (hose->regions + 1,
291 PCI_MEM_BUS,
292 PCI_MEM_PHY, PCI_MEM_SIZE, PCI_REGION_MEM);
293 /* PCI I/O space */
294 pci_set_region (hose->regions + 2,
295 PCI_IO_BUS, PCI_IO_PHY, PCI_IO_SIZE, PCI_REGION_IO);
296
297 hose->region_count = 3;
298
299 pci_register_hose (hose);
300
301/*
302 ==========================================================
303 Init IXP PCI
304 ==========================================================
305*/
306 REG_READ (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
307 regval |= 1 << 2;
308 REG_WRITE (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
309
310 configure_pins ();
311
312 READ_GPIO_REG (IXP425_GPIO_GPOUTR, regval);
313 WRITE_GPIO_REG (IXP425_GPIO_GPOUTR, regval & (~(1 << 13)));
314 udelay (533);
315 sys_pci_gpio_clock_config ();
316 REG_WRITE (PCI_CSR_BASE, PCI_INTEN_OFFSET, 0);
317 udelay (100);
318 READ_GPIO_REG (IXP425_GPIO_GPOUTR, regval);
319 WRITE_GPIO_REG (IXP425_GPIO_GPOUTR, regval | (1 << 13));
320 udelay (533);
321 crp_write (PCI_CFG_BASE_ADDRESS_0, IXP425_PCI_BAR_0_DEFAULT);
322 crp_write (PCI_CFG_BASE_ADDRESS_1, IXP425_PCI_BAR_1_DEFAULT);
323 crp_write (PCI_CFG_BASE_ADDRESS_2, IXP425_PCI_BAR_2_DEFAULT);
324 crp_write (PCI_CFG_BASE_ADDRESS_3, IXP425_PCI_BAR_3_DEFAULT);
325 crp_write (PCI_CFG_BASE_ADDRESS_4, IXP425_PCI_BAR_4_DEFAULT);
326 crp_write (PCI_CFG_BASE_ADDRESS_5, IXP425_PCI_BAR_5_DEFAULT);
327 /*Setup PCI-AHB and AHB-PCI address mappings */
328 REG_WRITE (PCI_CSR_BASE, PCI_AHBMEMBASE_OFFSET,
329 IXP425_PCI_AHBMEMBASE_DEFAULT);
330
331 REG_WRITE (PCI_CSR_BASE, PCI_AHBIOBASE_OFFSET,
332 IXP425_PCI_AHBIOBASE_DEFAULT);
333
334 REG_WRITE (PCI_CSR_BASE, PCI_PCIMEMBASE_OFFSET,
335 IXP425_PCI_PCIMEMBASE_DEFAULT);
336
337 crp_write (PCI_CFG_SUB_VENDOR_ID, IXP425_PCI_SUB_VENDOR_SYSTEM);
338
339 REG_READ (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
340 regval |= PCI_CSR_IC | PCI_CSR_ABE | PCI_CSR_PDS;
341 REG_WRITE (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
342 crp_write (PCI_CFG_COMMAND, PCI_CFG_CMD_MAE | PCI_CFG_CMD_BME);
343 udelay (1000);
344
345 pci_write_config_word (0, PCI_CFG_COMMAND, INITIAL_PCI_CMD);
346 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PSE
347 | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE);
348#ifdef CONFIG_PCI_SCAN_SHOW
349 printf ("Device bus dev func deviceID vendorID \n");
350#endif
351 pci_bus_scan ();
352}
353
354void configure_pins (void)
355{
356 unsigned int regval;
357
358 /* Disable clock on GPIO PIN 14 */
359 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
360 WRITE_GPIO_REG (IXP425_GPIO_GPCLKR, regval & (~(1 << 8)));
361 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
362
363 READ_GPIO_REG (IXP425_GPIO_GPOER, regval);
364 WRITE_GPIO_REG (IXP425_GPIO_GPOER,
365 (((~(3 << 13)) & regval) | (0xf << 8)));
366 READ_GPIO_REG (IXP425_GPIO_GPOER, regval);
367
368 READ_GPIO_REG (IXP425_GPIO_GPIT2R, regval);
369 WRITE_GPIO_REG (IXP425_GPIO_GPIT2R,
370 (regval &
371 ((0x1 << 9) | (0x1 << 6) | (0x1 << 3) | 0x1)));
372 READ_GPIO_REG (IXP425_GPIO_GPIT2R, regval);
373
374 READ_GPIO_REG (IXP425_GPIO_GPISR, regval);
375 WRITE_GPIO_REG (IXP425_GPIO_GPISR, (regval | (0xf << 8)));
376 READ_GPIO_REG (IXP425_GPIO_GPISR, regval);
377}
378
379void sys_pci_gpio_clock_config (void)
380{
381 unsigned int regval;
382
383 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
384 regval |= 0x1 << 4;
385 WRITE_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
386 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
387 regval |= 0x1 << 8;
388 WRITE_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
389}
390
391void pci_bus_scan (void)
392{
393 unsigned int bus = 0, dev, func = 0;
394 unsigned short data16;
395 unsigned int data32;
396 unsigned char intPin;
397
398 /* Assign first device to ourselves */
399 devices[0].bus = 0;
400 devices[0].device = 0;
401 devices[0].func = 0;
402
403 crp_read (PCI_CFG_VENDOR_ID, &data32);
404
405 devices[0].vendor_id = data32 & IXP425_PCI_BOTTOM_WORD_OF_LONG_MASK;
406 devices[0].device_id = data32 >> 16;
407 devices[0].error = FALSE;
408 devices[0].bar[NO_BAR].size = 0; /*dummy - required */
409
410 nDevices = 1;
411
412 nMBars = 0;
413 nIOBars = 0;
414
415 for (dev = 0; dev < IXP425_PCI_MAX_DEV; dev++) {
416
417 /*Check whether a device is present */
418 if (pci_device_exists (dev) != TRUE) {
419
420 /*Clear error bits in ISR, write 1 to clear */
421 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PSE
422 | PCI_ISR_PFE | PCI_ISR_PPE |
423 PCI_ISR_AHBE);
424 continue;
425 }
426
427 /*A device is present, add an entry to the array */
428 devices[nDevices].bus = bus;
429 devices[nDevices].device = dev;
430 devices[nDevices].func = func;
431
432 pci_read_config_word (dev, PCI_CFG_VENDOR_ID, &data16);
433
434 devices[nDevices].vendor_id = data16;
435
436 pci_read_config_word (dev, PCI_CFG_DEVICE_ID, &data16);
437 devices[nDevices].device_id = data16;
438
439 /*The device is functioning correctly, set error to FALSE */
440 devices[nDevices].error = FALSE;
441
442 /*Figure out what BARs are on this device */
443 sys_pci_bar_info_get (nDevices, bus, dev, func);
444 /*Figure out what INTX# line the card uses */
445 pci_read_config_byte (dev, PCI_CFG_DEV_INT_PIN, &intPin);
446
447 /*assign the appropriate irq line */
448 if (intPin > PCI_IRQ_LINES) {
449 devices[nDevices].error = TRUE;
450 } else if (intPin != 0) {
451 /*This device uses an interrupt line */
452 /*devices[nDevices].irq = ixp425PciIntTranslate[dev][intPin-1]; */
453 devices[nDevices].irq = intPin;
454 }
455#ifdef CONFIG_PCI_SCAN_SHOW
456 printf ("%06d %03d %03d %04d %08d %08x\n", nDevices,
457 devices[nDevices].vendor_id);
458#endif
459 nDevices++;
460
461 }
462
463 calc_bars (memBars, nMBars, IXP425_PCI_BAR_MEM_BASE);
464 sys_pci_device_bars_write ();
465
466 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PSE
467 | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE);
468}
469
470void sys_pci_bar_info_get (unsigned int devnum,
471 unsigned int bus,
472 unsigned int dev, unsigned int func)
473{
474 unsigned int data32;
475 unsigned int tmp;
476 unsigned int size;
477
478 pci_write_config_dword (devnum,
479 PCI_CFG_BASE_ADDRESS_0, IXP425_PCI_BAR_QUERY);
480 pci_read_config_dword (devnum, PCI_CFG_BASE_ADDRESS_0, &data32);
481
482 devices[devnum].bar[0].address = (data32 & 1);
483
484 if (data32 & 1) {
485 /* IO space */
486 tmp = data32 & ~0x3;
487 size = ~(tmp - 1);
488 devices[devnum].bar[0].size = size;
489
490 if (nIOBars < IXP425_PCI_MAX_BAR) {
491 ioBars[nIOBars++] = &devices[devnum].bar[0];
492 }
493 } else {
494 /* Mem space */
495 tmp = data32 & ~IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
496 size = ~(tmp - 1);
497 devices[devnum].bar[0].size = size;
498
499 if (nMBars < IXP425_PCI_MAX_BAR) {
500 memBars[nMBars++] = &devices[devnum].bar[0];
501 } else {
502 devices[devnum].error = TRUE;
503 }
504
505 }
506
507 devices[devnum].bar[1].size = 0;
508}
509
510void sortBars (PciBar * Bars[], unsigned int nBars)
511{
512 unsigned int i, j;
513 PciBar *tmp;
514
515 if (nBars == 0) {
516 return;
517 }
518
519 /* Sort biggest to smallest */
520 for (i = 0; i < nBars - 1; i++) {
521 for (j = i + 1; j < nBars; j++) {
522 if (Bars[j]->size > Bars[i]->size) {
523 /* swap them */
524 tmp = Bars[i];
525 Bars[i] = Bars[j];
526 Bars[j] = tmp;
527 }
528 }
529 }
530}
531
532void calc_bars (PciBar * Bars[], unsigned int nBars, unsigned int startAddr)
533{
534 unsigned int i;
535
536 if (nBars == 0) {
537 return;
538 }
539
540 for (i = 0; i < nBars; i++) {
541 Bars[i]->address |= startAddr;
542 startAddr += Bars[i]->size;
543 }
544}
545
546void sys_pci_device_bars_write (void)
547{
548 unsigned int i;
549 int addr;
550
551 for (i = 1; i < nDevices; i++) {
552 if (devices[i].error) {
553 continue;
554 }
555
556 pci_write_config_dword (devices[i].device,
557 PCI_CFG_BASE_ADDRESS_0,
558 devices[i].bar[0].address);
wdenk289f9322005-01-12 00:15:14 +0000559 addr = (BIT (31 - devices[i].device) |
560 (0 << PCI_NP_AD_FUNCSL) |
561 (PCI_CFG_BASE_ADDRESS_0) ) & ~3;
wdenka1191902005-01-09 17:12:27 +0000562 pci_write_config_dword (devices[i].device,
563 PCI_CFG_DEV_INT_LINE, devices[i].irq);
564
565 pci_write_config_word (devices[i].device,
566 PCI_CFG_COMMAND, INITIAL_PCI_CMD);
567
568 }
569}
570
571
572int pci_device_exists (unsigned int deviceNo)
573{
574 unsigned int vendorId;
575 unsigned int regval;
576
577 pci_read_config_dword (deviceNo, PCI_CFG_VENDOR_ID, &vendorId);
578
579 /* There are two ways to find out an empty device.
580 * 1. check Master Abort bit after the access.
581 * 2. check whether the vendor id read back is 0x0.
582 */
583 REG_READ (PCI_CSR_BASE, PCI_ISR_OFFSET, regval);
584 if ((vendorId != 0x0) && ((regval & PCI_ISR_PFE) == 0)) {
585 return TRUE;
586 }
587 /*no device present, make sure that the master abort bit is reset */
588
589 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PFE);
590 return FALSE;
591}
592
593pci_dev_t pci_find_devices (struct pci_device_id * ids, int devNo)
594{
595 unsigned int i;
596 unsigned int devdidvid;
597 unsigned int didvid;
598 unsigned int vendorId, deviceId;
599
600 vendorId = ids->vendor;
601 deviceId = ids->device;
602 didvid = ((deviceId << 16) & IXP425_PCI_TOP_WORD_OF_LONG_MASK) |
603 (vendorId & IXP425_PCI_BOTTOM_WORD_OF_LONG_MASK);
604
605 for (i = devNo + 1; i < nDevices; i++) {
606
607 pci_read_config_dword (devices[i].device, PCI_CFG_VENDOR_ID,
608 &devdidvid);
609
610 if (devdidvid == didvid) {
611 return devices[i].device;
612 }
613 }
614 return -1;
615}
616#endif /* CONFIG_PCI */