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