3 * (C) Copyright 2004 eslab.whut.edu.cn
4 * Yue Hu(huyue_whut@yahoo.com.cn), Ligong Xue(lgxue@hotmail.com)
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #include <asm/processor.h>
33 #include <asm/arch/ixp425.h>
34 #include <asm/arch/ixp425pci.h>
36 static void non_prefetch_read (unsigned int addr, unsigned int cmd,
38 static void non_prefetch_write (unsigned int addr, unsigned int cmd,
40 static void configure_pins (void);
41 static void sys_pci_gpio_clock_config (void);
42 static void pci_bus_scan (void);
43 static int pci_device_exists (unsigned int deviceNo);
44 static void sys_pci_bar_info_get (unsigned int devnum, unsigned int bus,
45 unsigned int dev, unsigned int func);
46 static void sys_pci_device_bars_write (void);
47 static void calc_bars (PciBar * Bars[], unsigned int nBars,
48 unsigned int startAddr);
50 #define PCI_MEMORY_BUS 0x00000000
51 #define PCI_MEMORY_PHY 0x48000000
52 #define PCI_MEMORY_SIZE 0x04000000
54 #define PCI_MEM_BUS 0x40000000
55 #define PCI_MEM_PHY 0x00000000
56 #define PCI_MEM_SIZE 0x04000000
58 #define PCI_IO_BUS 0x40000000
59 #define PCI_IO_PHY 0x50000000
60 #define PCI_IO_SIZE 0x10000000
62 struct pci_controller hose;
64 unsigned int nDevices;
67 PciBar *memBars[IXP425_PCI_MAX_BAR];
68 PciBar *ioBars[IXP425_PCI_MAX_BAR];
69 PciDevice devices[IXP425_PCI_MAX_FUNC_ON_BUS];
71 int pci_read_config_dword (pci_dev_t dev, int where, unsigned int *val)
76 /*address bits 31:28 specify the device 10:8 specify the function */
77 /*Set the address to be read */
78 addr = BIT ((31 - dev)) | (where & ~3);
79 non_prefetch_read (addr, NP_CMD_CONFIGREAD, &retval);
86 int pci_read_config_word (pci_dev_t dev, int where, unsigned short *val)
91 unsigned int byteEnables;
94 /*byte enables are 4 bits active low, the position of each
95 bit maps to the byte that it enables */
97 (~(BIT (n) | BIT ((n + 1)))) &
98 IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
99 byteEnables = byteEnables << PCI_NP_CBE_BESL;
100 /*address bits 31:28 specify the device 10:8 specify the function */
101 /*Set the address to be read */
102 addr = BIT ((31 - dev)) | (where & ~3);
103 non_prefetch_read (addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
105 /*Pick out the word we are interested in */
106 *val = (retval >> (8 * n));
111 int pci_read_config_byte (pci_dev_t dev, int where, unsigned char *val)
115 unsigned int byteEnables;
119 /*byte enables are 4 bits, active low, the position of each
120 bit maps to the byte that it enables */
121 byteEnables = (~BIT (n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
122 byteEnables = byteEnables << PCI_NP_CBE_BESL;
124 /*address bits 31:28 specify the device, 10:8 specify the function */
125 /*Set the address to be read */
126 addr = BIT ((31 - dev)) | (where & ~3);
127 non_prefetch_read (addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
128 /*Pick out the byte we are interested in */
129 *val = (retval >> (8 * n));
134 int pci_write_config_byte (pci_dev_t dev, int where, unsigned char val)
137 unsigned int byteEnables;
142 /*byte enables are 4 bits active low, the position of each
143 bit maps to the byte that it enables */
144 byteEnables = (~BIT (n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
145 byteEnables = byteEnables << PCI_NP_CBE_BESL;
146 ldata = val << (8 * n);
147 /*address bits 31:28 specify the device 10:8 specify the function */
148 /*Set the address to be written */
149 addr = BIT ((31 - dev)) | (where & ~3);
150 non_prefetch_write (addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
155 int pci_write_config_word (pci_dev_t dev, int where, unsigned short val)
158 unsigned int byteEnables;
163 /*byte enables are 4 bits active low, the position of each
164 bit maps to the byte that it enables */
166 (~(BIT (n) | BIT ((n + 1)))) &
167 IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
168 byteEnables = byteEnables << PCI_NP_CBE_BESL;
169 ldata = val << (8 * n);
170 /*address bits 31:28 specify the device 10:8 specify the function */
171 /*Set the address to be written */
172 addr = BIT (31 - dev) | (where & ~3);
173 non_prefetch_write (addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
178 int pci_write_config_dword (pci_dev_t dev, int where, unsigned int val)
182 /*address bits 31:28 specify the device 10:8 specify the function */
183 /*Set the address to be written */
184 addr = BIT (31 - dev) | (where & ~3);
185 non_prefetch_write (addr, NP_CMD_CONFIGWRITE, val);
190 void non_prefetch_read (unsigned int addr,
191 unsigned int cmd, unsigned int *data)
193 REG_WRITE (PCI_CSR_BASE, PCI_NP_AD_OFFSET, addr);
195 /*set up and execute the read */
196 REG_WRITE (PCI_CSR_BASE, PCI_NP_CBE_OFFSET, cmd);
198 /*The result of the read is now in np_rdata */
199 REG_READ (PCI_CSR_BASE, PCI_NP_RDATA_OFFSET, *data);
204 void non_prefetch_write (unsigned int addr,
205 unsigned int cmd, unsigned int data)
208 REG_WRITE (PCI_CSR_BASE, PCI_NP_AD_OFFSET, addr);
209 /*set up the write */
210 REG_WRITE (PCI_CSR_BASE, PCI_NP_CBE_OFFSET, cmd);
211 /*Execute the write by writing to NP_WDATA */
212 REG_WRITE (PCI_CSR_BASE, PCI_NP_WDATA_OFFSET, data);
218 * PCI controller config registers are accessed through these functions
219 * i.e. these allow us to set up our own BARs etc.
221 void crp_read (unsigned int offset, unsigned int *data)
223 REG_WRITE (PCI_CSR_BASE, PCI_CRP_AD_CBE_OFFSET, offset);
224 REG_READ (PCI_CSR_BASE, PCI_CRP_RDATA_OFFSET, *data);
227 void crp_write (unsigned int offset, unsigned int data)
229 /*The CRP address register bit 16 indicates that we want to do a write */
230 REG_WRITE (PCI_CSR_BASE, PCI_CRP_AD_CBE_OFFSET,
231 PCI_CRP_WRITE | offset);
232 REG_WRITE (PCI_CSR_BASE, PCI_CRP_WDATA_OFFSET, data);
235 /*struct pci_controller *hose*/
236 void pci_ixp_init (struct pci_controller *hose)
240 hose->first_busno = 0;
241 hose->last_busno = 0x00;
243 /* System memory space */
244 pci_set_region (hose->regions + 0,
246 PCI_MEMORY_PHY, PCI_MEMORY_SIZE, PCI_REGION_MEMORY);
248 /* PCI memory space */
249 pci_set_region (hose->regions + 1,
251 PCI_MEM_PHY, PCI_MEM_SIZE, PCI_REGION_MEM);
253 pci_set_region (hose->regions + 2,
254 PCI_IO_BUS, PCI_IO_PHY, PCI_IO_SIZE, PCI_REGION_IO);
256 hose->region_count = 3;
258 pci_register_hose (hose);
261 ==========================================================
263 ==========================================================
265 REG_READ (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
267 REG_WRITE (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
271 READ_GPIO_REG (IXP425_GPIO_GPOUTR, regval);
272 WRITE_GPIO_REG (IXP425_GPIO_GPOUTR, regval & (~(1 << 13)));
274 sys_pci_gpio_clock_config ();
275 REG_WRITE (PCI_CSR_BASE, PCI_INTEN_OFFSET, 0);
277 READ_GPIO_REG (IXP425_GPIO_GPOUTR, regval);
278 WRITE_GPIO_REG (IXP425_GPIO_GPOUTR, regval | (1 << 13));
280 crp_write (PCI_CFG_BASE_ADDRESS_0, IXP425_PCI_BAR_0_DEFAULT);
281 crp_write (PCI_CFG_BASE_ADDRESS_1, IXP425_PCI_BAR_1_DEFAULT);
282 crp_write (PCI_CFG_BASE_ADDRESS_2, IXP425_PCI_BAR_2_DEFAULT);
283 crp_write (PCI_CFG_BASE_ADDRESS_3, IXP425_PCI_BAR_3_DEFAULT);
284 crp_write (PCI_CFG_BASE_ADDRESS_4, IXP425_PCI_BAR_4_DEFAULT);
285 crp_write (PCI_CFG_BASE_ADDRESS_5, IXP425_PCI_BAR_5_DEFAULT);
286 /*Setup PCI-AHB and AHB-PCI address mappings */
287 REG_WRITE (PCI_CSR_BASE, PCI_AHBMEMBASE_OFFSET,
288 IXP425_PCI_AHBMEMBASE_DEFAULT);
290 REG_WRITE (PCI_CSR_BASE, PCI_AHBIOBASE_OFFSET,
291 IXP425_PCI_AHBIOBASE_DEFAULT);
293 REG_WRITE (PCI_CSR_BASE, PCI_PCIMEMBASE_OFFSET,
294 IXP425_PCI_PCIMEMBASE_DEFAULT);
296 crp_write (PCI_CFG_SUB_VENDOR_ID, IXP425_PCI_SUB_VENDOR_SYSTEM);
298 REG_READ (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
299 regval |= PCI_CSR_IC | PCI_CSR_ABE | PCI_CSR_PDS;
300 REG_WRITE (PCI_CSR_BASE, PCI_CSR_OFFSET, regval);
301 crp_write (PCI_CFG_COMMAND, PCI_CFG_CMD_MAE | PCI_CFG_CMD_BME);
304 pci_write_config_word (0, PCI_CFG_COMMAND, INITIAL_PCI_CMD);
305 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PSE
306 | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE);
307 #ifdef CONFIG_PCI_SCAN_SHOW
308 printf ("Device bus dev func deviceID vendorID \n");
313 void configure_pins (void)
317 /* Disable clock on GPIO PIN 14 */
318 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
319 WRITE_GPIO_REG (IXP425_GPIO_GPCLKR, regval & (~(1 << 8)));
320 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
322 READ_GPIO_REG (IXP425_GPIO_GPOER, regval);
323 WRITE_GPIO_REG (IXP425_GPIO_GPOER,
324 (((~(3 << 13)) & regval) | (0xf << 8)));
325 READ_GPIO_REG (IXP425_GPIO_GPOER, regval);
327 READ_GPIO_REG (IXP425_GPIO_GPIT2R, regval);
328 WRITE_GPIO_REG (IXP425_GPIO_GPIT2R,
330 ((0x1 << 9) | (0x1 << 6) | (0x1 << 3) | 0x1)));
331 READ_GPIO_REG (IXP425_GPIO_GPIT2R, regval);
333 READ_GPIO_REG (IXP425_GPIO_GPISR, regval);
334 WRITE_GPIO_REG (IXP425_GPIO_GPISR, (regval | (0xf << 8)));
335 READ_GPIO_REG (IXP425_GPIO_GPISR, regval);
338 void sys_pci_gpio_clock_config (void)
342 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
344 WRITE_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
345 READ_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
347 WRITE_GPIO_REG (IXP425_GPIO_GPCLKR, regval);
350 void pci_bus_scan (void)
352 unsigned int bus = 0, dev, func = 0;
353 unsigned short data16;
355 unsigned char intPin;
357 /* Assign first device to ourselves */
359 devices[0].device = 0;
362 crp_read (PCI_CFG_VENDOR_ID, &data32);
364 devices[0].vendor_id = data32 & IXP425_PCI_BOTTOM_WORD_OF_LONG_MASK;
365 devices[0].device_id = data32 >> 16;
366 devices[0].error = FALSE;
367 devices[0].bar[NO_BAR].size = 0; /*dummy - required */
374 for (dev = 0; dev < IXP425_PCI_MAX_DEV; dev++) {
376 /*Check whether a device is present */
377 if (pci_device_exists (dev) != TRUE) {
379 /*Clear error bits in ISR, write 1 to clear */
380 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PSE
381 | PCI_ISR_PFE | PCI_ISR_PPE |
386 /*A device is present, add an entry to the array */
387 devices[nDevices].bus = bus;
388 devices[nDevices].device = dev;
389 devices[nDevices].func = func;
391 pci_read_config_word (dev, PCI_CFG_VENDOR_ID, &data16);
393 devices[nDevices].vendor_id = data16;
395 pci_read_config_word (dev, PCI_CFG_DEVICE_ID, &data16);
396 devices[nDevices].device_id = data16;
398 /*The device is functioning correctly, set error to FALSE */
399 devices[nDevices].error = FALSE;
401 /*Figure out what BARs are on this device */
402 sys_pci_bar_info_get (nDevices, bus, dev, func);
403 /*Figure out what INTX# line the card uses */
404 pci_read_config_byte (dev, PCI_CFG_DEV_INT_PIN, &intPin);
406 /*assign the appropriate irq line */
407 if (intPin > PCI_IRQ_LINES) {
408 devices[nDevices].error = TRUE;
409 } else if (intPin != 0) {
410 /*This device uses an interrupt line */
411 /*devices[nDevices].irq = ixp425PciIntTranslate[dev][intPin-1]; */
412 devices[nDevices].irq = intPin;
414 #ifdef CONFIG_PCI_SCAN_SHOW
415 printf ("%06d %03d %03d %04d %08d %08x\n", nDevices,
416 devices[nDevices].vendor_id);
422 calc_bars (memBars, nMBars, IXP425_PCI_BAR_MEM_BASE);
423 sys_pci_device_bars_write ();
425 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PSE
426 | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE);
429 void sys_pci_bar_info_get (unsigned int devnum,
431 unsigned int dev, unsigned int func)
437 pci_write_config_dword (devnum,
438 PCI_CFG_BASE_ADDRESS_0, IXP425_PCI_BAR_QUERY);
439 pci_read_config_dword (devnum, PCI_CFG_BASE_ADDRESS_0, &data32);
441 devices[devnum].bar[0].address = (data32 & 1);
447 devices[devnum].bar[0].size = size;
449 if (nIOBars < IXP425_PCI_MAX_BAR) {
450 ioBars[nIOBars++] = &devices[devnum].bar[0];
454 tmp = data32 & ~IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
456 devices[devnum].bar[0].size = size;
458 if (nMBars < IXP425_PCI_MAX_BAR) {
459 memBars[nMBars++] = &devices[devnum].bar[0];
461 devices[devnum].error = TRUE;
466 devices[devnum].bar[1].size = 0;
469 void sortBars (PciBar * Bars[], unsigned int nBars)
478 /* Sort biggest to smallest */
479 for (i = 0; i < nBars - 1; i++) {
480 for (j = i + 1; j < nBars; j++) {
481 if (Bars[j]->size > Bars[i]->size) {
491 void calc_bars (PciBar * Bars[], unsigned int nBars, unsigned int startAddr)
499 for (i = 0; i < nBars; i++) {
500 Bars[i]->address |= startAddr;
501 startAddr += Bars[i]->size;
505 void sys_pci_device_bars_write (void)
510 for (i = 1; i < nDevices; i++) {
511 if (devices[i].error) {
515 pci_write_config_dword (devices[i].device,
516 PCI_CFG_BASE_ADDRESS_0,
517 devices[i].bar[0].address);
518 addr = BIT (31 - devices[i].device) |
519 (0 << PCI_NP_AD_FUNCSL) |
520 (PCI_CFG_BASE_ADDRESS_0 & ~3);
521 pci_write_config_dword (devices[i].device,
522 PCI_CFG_DEV_INT_LINE, devices[i].irq);
524 pci_write_config_word (devices[i].device,
525 PCI_CFG_COMMAND, INITIAL_PCI_CMD);
531 int pci_device_exists (unsigned int deviceNo)
533 unsigned int vendorId;
536 pci_read_config_dword (deviceNo, PCI_CFG_VENDOR_ID, &vendorId);
538 /* There are two ways to find out an empty device.
539 * 1. check Master Abort bit after the access.
540 * 2. check whether the vendor id read back is 0x0.
542 REG_READ (PCI_CSR_BASE, PCI_ISR_OFFSET, regval);
543 if ((vendorId != 0x0) && ((regval & PCI_ISR_PFE) == 0)) {
546 /*no device present, make sure that the master abort bit is reset */
548 REG_WRITE (PCI_CSR_BASE, PCI_ISR_OFFSET, PCI_ISR_PFE);
552 pci_dev_t pci_find_devices (struct pci_device_id * ids, int devNo)
555 unsigned int devdidvid;
557 unsigned int vendorId, deviceId;
559 vendorId = ids->vendor;
560 deviceId = ids->device;
561 didvid = ((deviceId << 16) & IXP425_PCI_TOP_WORD_OF_LONG_MASK) |
562 (vendorId & IXP425_PCI_BOTTOM_WORD_OF_LONG_MASK);
564 for (i = devNo + 1; i < nDevices; i++) {
566 pci_read_config_dword (devices[i].device, PCI_CFG_VENDOR_ID,
569 if (devdidvid == didvid) {
570 return devices[i].device;
575 #endif /* CONFIG_PCI */