xref: /rk3399_rockchip-uboot/board/armltd/integrator/pci.c (revision 3765b3e7bd0f8e46914d417f29cbcb0c72b1acf7)
1576afd4fSJean-Christophe PLAGNIOL-VILLARD /*
2576afd4fSJean-Christophe PLAGNIOL-VILLARD  * (C) Copyright 2002
3576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Marius Groeger <mgroeger@sysgo.de>
5576afd4fSJean-Christophe PLAGNIOL-VILLARD  *
6576afd4fSJean-Christophe PLAGNIOL-VILLARD  * (C) Copyright 2002
7576afd4fSJean-Christophe PLAGNIOL-VILLARD  * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
8576afd4fSJean-Christophe PLAGNIOL-VILLARD  *
9576afd4fSJean-Christophe PLAGNIOL-VILLARD  * (C) Copyright 2003
10576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Texas Instruments, <www.ti.com>
11576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Kshitij Gupta <Kshitij@ti.com>
12576afd4fSJean-Christophe PLAGNIOL-VILLARD  *
13576afd4fSJean-Christophe PLAGNIOL-VILLARD  * (C) Copyright 2004
14576afd4fSJean-Christophe PLAGNIOL-VILLARD  * ARM Ltd.
15576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Philippe Robin, <philippe.robin@arm.com>
16576afd4fSJean-Christophe PLAGNIOL-VILLARD  *
172458716aSLinus Walleij  * (C) Copyright 2011
182458716aSLinus Walleij  * Linaro
192458716aSLinus Walleij  * Linus Walleij <linus.walleij@linaro.org>
202458716aSLinus Walleij  *
21*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
22576afd4fSJean-Christophe PLAGNIOL-VILLARD  */
23576afd4fSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
24576afd4fSJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
252458716aSLinus Walleij #include <asm/io.h>
262458716aSLinus Walleij #include "integrator-sc.h"
272458716aSLinus Walleij #include "pci_v3.h"
282458716aSLinus Walleij 
292458716aSLinus Walleij #define INTEGRATOR_BOOT_ROM_BASE	0x20000000
302458716aSLinus Walleij #define INTEGRATOR_HDR0_SDRAM_BASE	0x80000000
312458716aSLinus Walleij 
322458716aSLinus Walleij /*
332458716aSLinus Walleij  * These are in the physical addresses on the CPU side, i.e.
342458716aSLinus Walleij  * where we read and write stuff - you don't want to try to
352458716aSLinus Walleij  * move these around
362458716aSLinus Walleij  */
372458716aSLinus Walleij #define PHYS_PCI_MEM_BASE	0x40000000
382458716aSLinus Walleij #define PHYS_PCI_IO_BASE	0x60000000	/* PCI I/O space base */
392458716aSLinus Walleij #define PHYS_PCI_CONFIG_BASE	0x61000000
402458716aSLinus Walleij #define PHYS_PCI_V3_BASE	0x62000000	/* V360EPC registers */
412458716aSLinus Walleij #define SZ_256M			0x10000000
422458716aSLinus Walleij 
432458716aSLinus Walleij /*
442458716aSLinus Walleij  * These are in the PCI BUS address space
452458716aSLinus Walleij  * Set to 0x00000000 in the Linux kernel, 0x40000000 in Boot monitor
462458716aSLinus Walleij  * we follow the example of the kernel, because that is the address
472458716aSLinus Walleij  * range that devices actually use - what would they be doing at
482458716aSLinus Walleij  * 0x40000000?
492458716aSLinus Walleij  */
502458716aSLinus Walleij #define PCI_BUS_NONMEM_START	0x00000000
512458716aSLinus Walleij #define PCI_BUS_NONMEM_SIZE	SZ_256M
522458716aSLinus Walleij 
532458716aSLinus Walleij #define PCI_BUS_PREMEM_START	(PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE)
542458716aSLinus Walleij #define PCI_BUS_PREMEM_SIZE	SZ_256M
552458716aSLinus Walleij 
562458716aSLinus Walleij #if PCI_BUS_NONMEM_START & 0x000fffff
572458716aSLinus Walleij #error PCI_BUS_NONMEM_START must be megabyte aligned
582458716aSLinus Walleij #endif
592458716aSLinus Walleij #if PCI_BUS_PREMEM_START & 0x000fffff
602458716aSLinus Walleij #error PCI_BUS_PREMEM_START must be megabyte aligned
612458716aSLinus Walleij #endif
62576afd4fSJean-Christophe PLAGNIOL-VILLARD 
63576afd4fSJean-Christophe PLAGNIOL-VILLARD /*
64576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Initialize PCI Devices, report devices found.
65576afd4fSJean-Christophe PLAGNIOL-VILLARD  */
66576afd4fSJean-Christophe PLAGNIOL-VILLARD 
67576afd4fSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_PCI_PNP
682458716aSLinus Walleij #define PCI_ENET0_IOADDR	0x60000000 /* First card in PCI I/O space */
692458716aSLinus Walleij #define PCI_ENET0_MEMADDR	0x40000000 /* First card in PCI memory space */
70576afd4fSJean-Christophe PLAGNIOL-VILLARD static struct pci_config_table pci_integrator_config_table[] = {
71576afd4fSJean-Christophe PLAGNIOL-VILLARD 	{ PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x0f, PCI_ANY_ID,
72576afd4fSJean-Christophe PLAGNIOL-VILLARD 	  pci_cfgfunc_config_device, { PCI_ENET0_IOADDR,
73576afd4fSJean-Christophe PLAGNIOL-VILLARD 				       PCI_ENET0_MEMADDR,
74576afd4fSJean-Christophe PLAGNIOL-VILLARD 				       PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER }},
75576afd4fSJean-Christophe PLAGNIOL-VILLARD 	{ }
76576afd4fSJean-Christophe PLAGNIOL-VILLARD };
77576afd4fSJean-Christophe PLAGNIOL-VILLARD #endif /* CONFIG_PCI_PNP */
78576afd4fSJean-Christophe PLAGNIOL-VILLARD 
79576afd4fSJean-Christophe PLAGNIOL-VILLARD /* V3 access routines */
802458716aSLinus Walleij #define v3_writeb(o, v) __raw_writeb(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
812458716aSLinus Walleij #define v3_readb(o)    (__raw_readb(PHYS_PCI_V3_BASE + (unsigned int)(o)))
82576afd4fSJean-Christophe PLAGNIOL-VILLARD 
832458716aSLinus Walleij #define v3_writew(o, v) __raw_writew(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
842458716aSLinus Walleij #define v3_readw(o)    (__raw_readw(PHYS_PCI_V3_BASE + (unsigned int)(o)))
85576afd4fSJean-Christophe PLAGNIOL-VILLARD 
862458716aSLinus Walleij #define v3_writel(o, v) __raw_writel(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
872458716aSLinus Walleij #define v3_readl(o)    (__raw_readl(PHYS_PCI_V3_BASE + (unsigned int)(o)))
88576afd4fSJean-Christophe PLAGNIOL-VILLARD 
v3_open_config_window(pci_dev_t bdf,int offset)892458716aSLinus Walleij static unsigned long v3_open_config_window(pci_dev_t bdf, int offset)
902458716aSLinus Walleij {
912458716aSLinus Walleij 	unsigned int address, mapaddress;
922458716aSLinus Walleij 	unsigned int busnr = PCI_BUS(bdf);
932458716aSLinus Walleij 	unsigned int devfn = PCI_FUNC(bdf);
942458716aSLinus Walleij 
952458716aSLinus Walleij 	/*
962458716aSLinus Walleij 	 * Trap out illegal values
972458716aSLinus Walleij 	 */
982458716aSLinus Walleij 	if (offset > 255)
992458716aSLinus Walleij 		BUG();
1002458716aSLinus Walleij 	if (busnr > 255)
1012458716aSLinus Walleij 		BUG();
1022458716aSLinus Walleij 	if (devfn > 255)
1032458716aSLinus Walleij 		BUG();
1042458716aSLinus Walleij 
1052458716aSLinus Walleij 	if (busnr == 0) {
1062458716aSLinus Walleij 		/*
1072458716aSLinus Walleij 		 * Linux calls the thing U-Boot calls "DEV" "SLOT"
1082458716aSLinus Walleij 		 * instead, but it's the same 5 bits
1092458716aSLinus Walleij 		 */
1102458716aSLinus Walleij 		int slot = PCI_DEV(bdf);
1112458716aSLinus Walleij 
1122458716aSLinus Walleij 		/*
1132458716aSLinus Walleij 		 * local bus segment so need a type 0 config cycle
1142458716aSLinus Walleij 		 *
1152458716aSLinus Walleij 		 * build the PCI configuration "address" with one-hot in
1162458716aSLinus Walleij 		 * A31-A11
1172458716aSLinus Walleij 		 *
1182458716aSLinus Walleij 		 * mapaddress:
1192458716aSLinus Walleij 		 *  3:1 = config cycle (101)
1202458716aSLinus Walleij 		 *  0   = PCI A1 & A0 are 0 (0)
1212458716aSLinus Walleij 		 */
1222458716aSLinus Walleij 		address = PCI_FUNC(bdf) << 8;
1232458716aSLinus Walleij 		mapaddress = V3_LB_MAP_TYPE_CONFIG;
1242458716aSLinus Walleij 
1252458716aSLinus Walleij 		if (slot > 12)
1262458716aSLinus Walleij 			/*
1272458716aSLinus Walleij 			 * high order bits are handled by the MAP register
1282458716aSLinus Walleij 			 */
1292458716aSLinus Walleij 			mapaddress |= 1 << (slot - 5);
1302458716aSLinus Walleij 		else
1312458716aSLinus Walleij 			/*
1322458716aSLinus Walleij 			 * low order bits handled directly in the address
1332458716aSLinus Walleij 			 */
1342458716aSLinus Walleij 			address |= 1 << (slot + 11);
1352458716aSLinus Walleij 	} else {
1362458716aSLinus Walleij 		/*
1372458716aSLinus Walleij 		 * not the local bus segment so need a type 1 config cycle
1382458716aSLinus Walleij 		 *
1392458716aSLinus Walleij 		 * address:
1402458716aSLinus Walleij 		 *  23:16 = bus number
1412458716aSLinus Walleij 		 *  15:11 = slot number (7:3 of devfn)
1422458716aSLinus Walleij 		 *  10:8  = func number (2:0 of devfn)
1432458716aSLinus Walleij 		 *
1442458716aSLinus Walleij 		 * mapaddress:
1452458716aSLinus Walleij 		 *  3:1 = config cycle (101)
1462458716aSLinus Walleij 		 *  0   = PCI A1 & A0 from host bus (1)
1472458716aSLinus Walleij 		 */
1482458716aSLinus Walleij 		mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
1492458716aSLinus Walleij 		address = (busnr << 16) | (devfn << 8);
150576afd4fSJean-Christophe PLAGNIOL-VILLARD 	}
151576afd4fSJean-Christophe PLAGNIOL-VILLARD 
1522458716aSLinus Walleij 	/*
1532458716aSLinus Walleij 	 * Set up base0 to see all 512Mbytes of memory space (not
1542458716aSLinus Walleij 	 * prefetchable), this frees up base1 for re-use by
1552458716aSLinus Walleij 	 * configuration memory
1562458716aSLinus Walleij 	 */
1572458716aSLinus Walleij 	v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
1582458716aSLinus Walleij 			V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE);
1592458716aSLinus Walleij 
1602458716aSLinus Walleij 	/*
1612458716aSLinus Walleij 	 * Set up base1/map1 to point into configuration space.
1622458716aSLinus Walleij 	 */
1632458716aSLinus Walleij 	v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_CONFIG_BASE) |
1642458716aSLinus Walleij 			V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE);
1652458716aSLinus Walleij 	v3_writew(V3_LB_MAP1, mapaddress);
1662458716aSLinus Walleij 
1672458716aSLinus Walleij 	return PHYS_PCI_CONFIG_BASE + address + offset;
168576afd4fSJean-Christophe PLAGNIOL-VILLARD }
169576afd4fSJean-Christophe PLAGNIOL-VILLARD 
v3_close_config_window(void)1702458716aSLinus Walleij static void v3_close_config_window(void)
1712458716aSLinus Walleij {
1722458716aSLinus Walleij 	/*
1732458716aSLinus Walleij 	 * Reassign base1 for use by prefetchable PCI memory
1742458716aSLinus Walleij 	 */
1752458716aSLinus Walleij 	v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
1762458716aSLinus Walleij 			V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
1772458716aSLinus Walleij 			V3_LB_BASE_ENABLE);
1782458716aSLinus Walleij 	v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
1792458716aSLinus Walleij 			V3_LB_MAP_TYPE_MEM_MULTIPLE);
1802458716aSLinus Walleij 
1812458716aSLinus Walleij 	/*
1822458716aSLinus Walleij 	 * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
1832458716aSLinus Walleij 	 */
1842458716aSLinus Walleij 	v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
1852458716aSLinus Walleij 			V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
1862458716aSLinus Walleij }
1872458716aSLinus Walleij 
pci_integrator_read_byte(struct pci_controller * hose,pci_dev_t bdf,int offset,unsigned char * val)1882458716aSLinus Walleij static int pci_integrator_read_byte(struct pci_controller *hose, pci_dev_t bdf,
189576afd4fSJean-Christophe PLAGNIOL-VILLARD 				    int offset, unsigned char *val)
190576afd4fSJean-Christophe PLAGNIOL-VILLARD {
1912458716aSLinus Walleij 	unsigned long addr;
192576afd4fSJean-Christophe PLAGNIOL-VILLARD 
1932458716aSLinus Walleij 	addr = v3_open_config_window(bdf, offset);
1942458716aSLinus Walleij 	*val = __raw_readb(addr);
1952458716aSLinus Walleij 	v3_close_config_window();
196576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return 0;
197576afd4fSJean-Christophe PLAGNIOL-VILLARD }
198576afd4fSJean-Christophe PLAGNIOL-VILLARD 
pci_integrator_read__word(struct pci_controller * hose,pci_dev_t bdf,int offset,unsigned short * val)199576afd4fSJean-Christophe PLAGNIOL-VILLARD static int pci_integrator_read__word(struct pci_controller *hose,
2002458716aSLinus Walleij 				     pci_dev_t bdf, int offset,
201576afd4fSJean-Christophe PLAGNIOL-VILLARD 				     unsigned short *val)
202576afd4fSJean-Christophe PLAGNIOL-VILLARD {
2032458716aSLinus Walleij 	unsigned long addr;
204576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2052458716aSLinus Walleij 	addr = v3_open_config_window(bdf, offset);
2062458716aSLinus Walleij 	*val = __raw_readw(addr);
2072458716aSLinus Walleij 	v3_close_config_window();
208576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return 0;
209576afd4fSJean-Christophe PLAGNIOL-VILLARD }
210576afd4fSJean-Christophe PLAGNIOL-VILLARD 
pci_integrator_read_dword(struct pci_controller * hose,pci_dev_t bdf,int offset,unsigned int * val)211576afd4fSJean-Christophe PLAGNIOL-VILLARD static int pci_integrator_read_dword(struct pci_controller *hose,
2122458716aSLinus Walleij 				     pci_dev_t bdf, int offset,
213576afd4fSJean-Christophe PLAGNIOL-VILLARD 				     unsigned int *val)
214576afd4fSJean-Christophe PLAGNIOL-VILLARD {
2152458716aSLinus Walleij 	unsigned long addr;
216576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2172458716aSLinus Walleij 	addr = v3_open_config_window(bdf, offset);
2182458716aSLinus Walleij 	*val = __raw_readl(addr);
2192458716aSLinus Walleij 	v3_close_config_window();
220576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return 0;
221576afd4fSJean-Christophe PLAGNIOL-VILLARD }
222576afd4fSJean-Christophe PLAGNIOL-VILLARD 
pci_integrator_write_byte(struct pci_controller * hose,pci_dev_t bdf,int offset,unsigned char val)223576afd4fSJean-Christophe PLAGNIOL-VILLARD static int pci_integrator_write_byte(struct pci_controller *hose,
2242458716aSLinus Walleij 				     pci_dev_t bdf, int offset,
225576afd4fSJean-Christophe PLAGNIOL-VILLARD 				     unsigned char val)
226576afd4fSJean-Christophe PLAGNIOL-VILLARD {
2272458716aSLinus Walleij 	unsigned long addr;
228576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2292458716aSLinus Walleij 	addr = v3_open_config_window(bdf, offset);
2302458716aSLinus Walleij 	__raw_writeb((u8)val, addr);
2312458716aSLinus Walleij 	__raw_readb(addr);
2322458716aSLinus Walleij 	v3_close_config_window();
233576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return 0;
234576afd4fSJean-Christophe PLAGNIOL-VILLARD }
235576afd4fSJean-Christophe PLAGNIOL-VILLARD 
pci_integrator_write_word(struct pci_controller * hose,pci_dev_t bdf,int offset,unsigned short val)236576afd4fSJean-Christophe PLAGNIOL-VILLARD static int pci_integrator_write_word(struct pci_controller *hose,
2372458716aSLinus Walleij 				     pci_dev_t bdf, int offset,
238576afd4fSJean-Christophe PLAGNIOL-VILLARD 				     unsigned short val)
239576afd4fSJean-Christophe PLAGNIOL-VILLARD {
2402458716aSLinus Walleij 	unsigned long addr;
241576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2422458716aSLinus Walleij 	addr = v3_open_config_window(bdf, offset);
2432458716aSLinus Walleij 	__raw_writew((u8)val, addr);
2442458716aSLinus Walleij 	__raw_readw(addr);
2452458716aSLinus Walleij 	v3_close_config_window();
246576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return 0;
247576afd4fSJean-Christophe PLAGNIOL-VILLARD }
248576afd4fSJean-Christophe PLAGNIOL-VILLARD 
pci_integrator_write_dword(struct pci_controller * hose,pci_dev_t bdf,int offset,unsigned int val)249576afd4fSJean-Christophe PLAGNIOL-VILLARD static int pci_integrator_write_dword(struct pci_controller *hose,
2502458716aSLinus Walleij 				      pci_dev_t bdf, int offset,
251576afd4fSJean-Christophe PLAGNIOL-VILLARD 				      unsigned int val)
252576afd4fSJean-Christophe PLAGNIOL-VILLARD {
2532458716aSLinus Walleij 	unsigned long addr;
254576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2552458716aSLinus Walleij 	addr = v3_open_config_window(bdf, offset);
2562458716aSLinus Walleij 	__raw_writel((u8)val, addr);
2572458716aSLinus Walleij 	__raw_readl(addr);
2582458716aSLinus Walleij 	v3_close_config_window();
259576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return 0;
260576afd4fSJean-Christophe PLAGNIOL-VILLARD }
261576afd4fSJean-Christophe PLAGNIOL-VILLARD 
262576afd4fSJean-Christophe PLAGNIOL-VILLARD struct pci_controller integrator_hose = {
263576afd4fSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_PCI_PNP
264576afd4fSJean-Christophe PLAGNIOL-VILLARD 	config_table: pci_integrator_config_table,
265576afd4fSJean-Christophe PLAGNIOL-VILLARD #endif
266576afd4fSJean-Christophe PLAGNIOL-VILLARD };
267576afd4fSJean-Christophe PLAGNIOL-VILLARD 
pci_init_board(void)268576afd4fSJean-Christophe PLAGNIOL-VILLARD void pci_init_board(void)
269576afd4fSJean-Christophe PLAGNIOL-VILLARD {
270576afd4fSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose = &integrator_hose;
2712458716aSLinus Walleij 	u16 val;
272576afd4fSJean-Christophe PLAGNIOL-VILLARD 
273576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* setting this register will take the V3 out of reset */
2742458716aSLinus Walleij 	__raw_writel(SC_PCI_PCIEN, SC_PCI);
275576afd4fSJean-Christophe PLAGNIOL-VILLARD 
276fca94c3fSLinus Walleij 	/* Wait for 230 ms (from spec) before accessing any V3 registers */
277fca94c3fSLinus Walleij 	mdelay(230);
278576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2792458716aSLinus Walleij 	/* Now write the Base I/O Address Word to PHYS_PCI_V3_BASE + 0x6E */
2802458716aSLinus Walleij 	v3_writew(V3_LB_IO_BASE, (PHYS_PCI_V3_BASE >> 16));
281576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2822458716aSLinus Walleij 	/* Wait for the mailbox to settle */
283576afd4fSJean-Christophe PLAGNIOL-VILLARD 	do {
2842458716aSLinus Walleij 		v3_writeb(V3_MAIL_DATA, 0xAA);
2852458716aSLinus Walleij 		v3_writeb(V3_MAIL_DATA + 4, 0x55);
2862458716aSLinus Walleij 	} while (v3_readb(V3_MAIL_DATA) != 0xAA ||
2872458716aSLinus Walleij 		 v3_readb(V3_MAIL_DATA + 4) != 0x55);
288576afd4fSJean-Christophe PLAGNIOL-VILLARD 
289576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* Make sure that V3 register access is not locked, if it is, unlock it */
2902458716aSLinus Walleij 	if (v3_readw(V3_SYSTEM) & V3_SYSTEM_M_LOCK)
2912458716aSLinus Walleij 		v3_writew(V3_SYSTEM, 0xA05F);
292576afd4fSJean-Christophe PLAGNIOL-VILLARD 
2932458716aSLinus Walleij 	/*
2942458716aSLinus Walleij 	 * Ensure that the slave accesses from PCI are disabled while we
2952458716aSLinus Walleij 	 * setup memory windows
2962458716aSLinus Walleij 	 */
2972458716aSLinus Walleij 	val = v3_readw(V3_PCI_CMD);
2982458716aSLinus Walleij 	val &= ~(V3_COMMAND_M_MEM_EN | V3_COMMAND_M_IO_EN);
2992458716aSLinus Walleij 	v3_writew(V3_PCI_CMD, val);
300576afd4fSJean-Christophe PLAGNIOL-VILLARD 
301576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* Clear RST_OUT to 0; keep the PCI bus in reset until we've finished */
3022458716aSLinus Walleij 	val = v3_readw(V3_SYSTEM);
3032458716aSLinus Walleij 	val &= ~V3_SYSTEM_M_RST_OUT;
3042458716aSLinus Walleij 	v3_writew(V3_SYSTEM, val);
305576afd4fSJean-Christophe PLAGNIOL-VILLARD 
306576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* Make all accesses from PCI space retry until we're ready for them */
3072458716aSLinus Walleij 	val = v3_readw(V3_PCI_CFG);
3082458716aSLinus Walleij 	val |= V3_PCI_CFG_M_RETRY_EN;
3092458716aSLinus Walleij 	v3_writew(V3_PCI_CFG, val);
310576afd4fSJean-Christophe PLAGNIOL-VILLARD 
3112458716aSLinus Walleij 	/*
3122458716aSLinus Walleij 	 * Set up any V3 PCI Configuration Registers that we absolutely have to.
3132458716aSLinus Walleij 	 * LB_CFG controls Local Bus protocol.
3142458716aSLinus Walleij 	 * Enable LocalBus byte strobes for READ accesses too.
3152458716aSLinus Walleij 	 * set bit 7 BE_IMODE and bit 6 BE_OMODE
3162458716aSLinus Walleij 	 */
3172458716aSLinus Walleij 	val = v3_readw(V3_LB_CFG);
3182458716aSLinus Walleij 	val |= 0x0C0;
3192458716aSLinus Walleij 	v3_writew(V3_LB_CFG, val);
320576afd4fSJean-Christophe PLAGNIOL-VILLARD 
3212458716aSLinus Walleij 	/* PCI_CMD controls overall PCI operation. Enable PCI bus master. */
3222458716aSLinus Walleij 	val = v3_readw(V3_PCI_CMD);
3232458716aSLinus Walleij 	val |= V3_COMMAND_M_MASTER_EN;
3242458716aSLinus Walleij 	v3_writew(V3_PCI_CMD, val);
325576afd4fSJean-Christophe PLAGNIOL-VILLARD 
3262458716aSLinus Walleij 	/*
3272458716aSLinus Walleij 	 * PCI_MAP0 controls where the PCI to CPU memory window is on
3282458716aSLinus Walleij 	 * Local Bus
3292458716aSLinus Walleij 	 */
3302458716aSLinus Walleij 	v3_writel(V3_PCI_MAP0,
3312458716aSLinus Walleij 		  (INTEGRATOR_BOOT_ROM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_512MB |
332576afd4fSJean-Christophe PLAGNIOL-VILLARD 						V3_PCI_MAP_M_REG_EN |
3332458716aSLinus Walleij 						V3_PCI_MAP_M_ENABLE));
334576afd4fSJean-Christophe PLAGNIOL-VILLARD 
335576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* PCI_BASE0 is the PCI address of the start of the window */
3362458716aSLinus Walleij 	v3_writel(V3_PCI_BASE0, INTEGRATOR_BOOT_ROM_BASE);
337576afd4fSJean-Christophe PLAGNIOL-VILLARD 
338576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* PCI_MAP1 is LOCAL address of the start of the window */
3392458716aSLinus Walleij 	v3_writel(V3_PCI_MAP1,
3402458716aSLinus Walleij 		  (INTEGRATOR_HDR0_SDRAM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_1GB |
341576afd4fSJean-Christophe PLAGNIOL-VILLARD 						  V3_PCI_MAP_M_REG_EN |
3422458716aSLinus Walleij 						  V3_PCI_MAP_M_ENABLE));
343576afd4fSJean-Christophe PLAGNIOL-VILLARD 
344576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* PCI_BASE1 is the PCI address of the start of the window */
3452458716aSLinus Walleij 	v3_writel(V3_PCI_BASE1, INTEGRATOR_HDR0_SDRAM_BASE);
346576afd4fSJean-Christophe PLAGNIOL-VILLARD 
3472458716aSLinus Walleij 	/*
3482458716aSLinus Walleij 	 * Set up memory the windows from local bus memory into PCI
3492458716aSLinus Walleij 	 * configuration, I/O and Memory regions.
3502458716aSLinus Walleij 	 * PCI I/O, LB_BASE2 and LB_MAP2 are used exclusively for this.
3512458716aSLinus Walleij 	 */
3522458716aSLinus Walleij 	v3_writew(V3_LB_BASE2,
3532458716aSLinus Walleij 		  v3_addr_to_lb_map(PHYS_PCI_IO_BASE) | V3_LB_BASE_ENABLE);
3542458716aSLinus Walleij 	v3_writew(V3_LB_MAP2, 0);
355576afd4fSJean-Christophe PLAGNIOL-VILLARD 
356576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* PCI Configuration, use LB_BASE1/LB_MAP1. */
357576afd4fSJean-Christophe PLAGNIOL-VILLARD 
3582458716aSLinus Walleij 	/*
3592458716aSLinus Walleij 	 * PCI Memory use LB_BASE0/LB_MAP0 and LB_BASE1/LB_MAP1
3602458716aSLinus Walleij 	 * Map first 256Mbytes as non-prefetchable via BASE0/MAP0
3612458716aSLinus Walleij 	 */
3622458716aSLinus Walleij 	v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
3632458716aSLinus Walleij 			V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
3642458716aSLinus Walleij 	v3_writew(V3_LB_MAP0,
3652458716aSLinus Walleij 		  v3_addr_to_lb_map(PCI_BUS_NONMEM_START) | V3_LB_MAP_TYPE_MEM);
366576afd4fSJean-Christophe PLAGNIOL-VILLARD 
367576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* Map second 256 Mbytes as prefetchable via BASE1/MAP1 */
3682458716aSLinus Walleij 	v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
3692458716aSLinus Walleij 			V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
3702458716aSLinus Walleij 			V3_LB_BASE_ENABLE);
3712458716aSLinus Walleij 	v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
3722458716aSLinus Walleij 			V3_LB_MAP_TYPE_MEM_MULTIPLE);
373576afd4fSJean-Christophe PLAGNIOL-VILLARD 
3742458716aSLinus Walleij 	/* Dump PCI to local address space mappings */
3752458716aSLinus Walleij 	debug("LB_BASE0 = %08x\n", v3_readl(V3_LB_BASE0));
3762458716aSLinus Walleij 	debug("LB_MAP0 = %04x\n", v3_readw(V3_LB_MAP0));
3772458716aSLinus Walleij 	debug("LB_BASE1 = %08x\n", v3_readl(V3_LB_BASE1));
3782458716aSLinus Walleij 	debug("LB_MAP1 = %04x\n", v3_readw(V3_LB_MAP1));
3792458716aSLinus Walleij 	debug("LB_BASE2 = %04x\n", v3_readw(V3_LB_BASE2));
3802458716aSLinus Walleij 	debug("LB_MAP2 = %04x\n", v3_readw(V3_LB_MAP2));
3812458716aSLinus Walleij 	debug("LB_IO_BASE = %04x\n", v3_readw(V3_LB_IO_BASE));
382576afd4fSJean-Christophe PLAGNIOL-VILLARD 
383576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/*
3842458716aSLinus Walleij 	 * Allow accesses to PCI Configuration space and set up A1, A0 for
3852458716aSLinus Walleij 	 * type 1 config cycles
3862458716aSLinus Walleij 	 */
3872458716aSLinus Walleij 	val = v3_readw(V3_PCI_CFG);
3882458716aSLinus Walleij 	val &= ~(V3_PCI_CFG_M_RETRY_EN | V3_PCI_CFG_M_AD_LOW1);
3892458716aSLinus Walleij 	val |= V3_PCI_CFG_M_AD_LOW0;
3902458716aSLinus Walleij 	v3_writew(V3_PCI_CFG, val);
3912458716aSLinus Walleij 
3922458716aSLinus Walleij 	/* now we can allow incoming PCI MEMORY accesses */
3932458716aSLinus Walleij 	val = v3_readw(V3_PCI_CMD);
3942458716aSLinus Walleij 	val |= V3_COMMAND_M_MEM_EN;
3952458716aSLinus Walleij 	v3_writew(V3_PCI_CMD, val);
3962458716aSLinus Walleij 
3972458716aSLinus Walleij 	/*
3982458716aSLinus Walleij 	 * Set RST_OUT to take the PCI bus is out of reset, PCI devices can
3992458716aSLinus Walleij 	 * now initialise.
4002458716aSLinus Walleij 	 */
4012458716aSLinus Walleij 	val = v3_readw(V3_SYSTEM);
4022458716aSLinus Walleij 	val |= V3_SYSTEM_M_RST_OUT;
4032458716aSLinus Walleij 	v3_writew(V3_SYSTEM, val);
4042458716aSLinus Walleij 
4052458716aSLinus Walleij 	/*  Lock the V3 system register so that no one else can play with it */
4062458716aSLinus Walleij 	val = v3_readw(V3_SYSTEM);
4072458716aSLinus Walleij 	val |= V3_SYSTEM_M_LOCK;
4082458716aSLinus Walleij 	v3_writew(V3_SYSTEM, val);
4092458716aSLinus Walleij 
4102458716aSLinus Walleij 	/*
4112458716aSLinus Walleij 	 * Configure and register the PCI hose
412576afd4fSJean-Christophe PLAGNIOL-VILLARD 	 */
413576afd4fSJean-Christophe PLAGNIOL-VILLARD 	hose->first_busno = 0;
414576afd4fSJean-Christophe PLAGNIOL-VILLARD 	hose->last_busno = 0xff;
415576afd4fSJean-Christophe PLAGNIOL-VILLARD 
4162458716aSLinus Walleij 	/* System memory space, window 0 256 MB non-prefetchable */
417576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pci_set_region(hose->regions + 0,
4182458716aSLinus Walleij 		       PCI_BUS_NONMEM_START, PHYS_PCI_MEM_BASE,
4192458716aSLinus Walleij 		       SZ_256M,
4202458716aSLinus Walleij 		       PCI_REGION_MEM);
421576afd4fSJean-Christophe PLAGNIOL-VILLARD 
4222458716aSLinus Walleij 	/* System memory space, window 1 256 MB prefetchable */
423576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pci_set_region(hose->regions + 1,
4242458716aSLinus Walleij 		       PCI_BUS_PREMEM_START, PHYS_PCI_MEM_BASE + SZ_256M,
4252458716aSLinus Walleij 		       SZ_256M,
4262458716aSLinus Walleij 		       PCI_REGION_MEM |
4272458716aSLinus Walleij 		       PCI_REGION_PREFETCH);
428576afd4fSJean-Christophe PLAGNIOL-VILLARD 
429576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* PCI I/O space */
4302458716aSLinus Walleij 	pci_set_region(hose->regions + 2,
4312458716aSLinus Walleij 		       0x00000000, PHYS_PCI_IO_BASE, 0x01000000,
4322458716aSLinus Walleij 		       PCI_REGION_IO);
4332458716aSLinus Walleij 
4342458716aSLinus Walleij 	/* PCI Memory - config space */
435576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pci_set_region(hose->regions + 3,
4362458716aSLinus Walleij 		       0x00000000, PHYS_PCI_CONFIG_BASE, 0x01000000,
4372458716aSLinus Walleij 		       PCI_REGION_MEM);
4382458716aSLinus Walleij 	/* PCI V3 regs */
4392458716aSLinus Walleij 	pci_set_region(hose->regions + 4,
4402458716aSLinus Walleij 		       0x00000000, PHYS_PCI_V3_BASE, 0x01000000,
4412458716aSLinus Walleij 		       PCI_REGION_MEM);
4422458716aSLinus Walleij 
4432458716aSLinus Walleij 	hose->region_count = 5;
444576afd4fSJean-Christophe PLAGNIOL-VILLARD 
445576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pci_set_ops(hose,
446576afd4fSJean-Christophe PLAGNIOL-VILLARD 		    pci_integrator_read_byte,
447576afd4fSJean-Christophe PLAGNIOL-VILLARD 		    pci_integrator_read__word,
448576afd4fSJean-Christophe PLAGNIOL-VILLARD 		    pci_integrator_read_dword,
449576afd4fSJean-Christophe PLAGNIOL-VILLARD 		    pci_integrator_write_byte,
4502458716aSLinus Walleij 		    pci_integrator_write_word,
4512458716aSLinus Walleij 		    pci_integrator_write_dword);
452576afd4fSJean-Christophe PLAGNIOL-VILLARD 
453576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pci_register_hose(hose);
454576afd4fSJean-Christophe PLAGNIOL-VILLARD 
455576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_init(hose);
456576afd4fSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_device(hose, 0);
457576afd4fSJean-Christophe PLAGNIOL-VILLARD 
458576afd4fSJean-Christophe PLAGNIOL-VILLARD 	hose->last_busno = pci_hose_scan(hose);
459576afd4fSJean-Christophe PLAGNIOL-VILLARD }
460