xref: /openbmc/linux/arch/sh/drivers/pci/pci-sh7780.c (revision 85b59f5bb24aeca1a987cbb206e228bf630c8327)
15283ecb5SPaul Mundt /*
25283ecb5SPaul Mundt  * Low-Level PCI Support for the SH7780
35283ecb5SPaul Mundt  *
4a45635dfSPaul Mundt  *  Copyright (C) 2005 - 2010  Paul Mundt
55283ecb5SPaul Mundt  *
662c7ae87SPaul Mundt  * This file is subject to the terms and conditions of the GNU General Public
762c7ae87SPaul Mundt  * License.  See the file "COPYING" in the main directory of this archive
862c7ae87SPaul Mundt  * for more details.
95283ecb5SPaul Mundt  */
105283ecb5SPaul Mundt #include <linux/types.h>
115283ecb5SPaul Mundt #include <linux/kernel.h>
125283ecb5SPaul Mundt #include <linux/init.h>
135283ecb5SPaul Mundt #include <linux/pci.h>
145283ecb5SPaul Mundt #include <linux/errno.h>
155283ecb5SPaul Mundt #include <linux/delay.h>
16aee4467bSPaul Mundt #include <linux/log2.h>
17959f85f8SPaul Mundt #include "pci-sh4.h"
18a45635dfSPaul Mundt #include <asm/mmu.h>
19a45635dfSPaul Mundt #include <asm/sizes.h>
205283ecb5SPaul Mundt 
21e79066a6SPaul Mundt static struct resource sh7785_io_resource = {
22e79066a6SPaul Mundt 	.name	= "SH7785_IO",
23a45635dfSPaul Mundt 	.start	= 0x1000,
24a45635dfSPaul Mundt 	.end	= SH7780_PCI_IO_SIZE - 1,
25e79066a6SPaul Mundt 	.flags	= IORESOURCE_IO
26e79066a6SPaul Mundt };
27e79066a6SPaul Mundt 
28e79066a6SPaul Mundt static struct resource sh7785_mem_resource = {
29e79066a6SPaul Mundt 	.name	= "SH7785_mem",
30e79066a6SPaul Mundt 	.start	= SH7780_PCI_MEMORY_BASE,
31e79066a6SPaul Mundt 	.end	= SH7780_PCI_MEMORY_BASE + SH7780_PCI_MEM_SIZE - 1,
32e79066a6SPaul Mundt 	.flags	= IORESOURCE_MEM
33e79066a6SPaul Mundt };
34e79066a6SPaul Mundt 
35e79066a6SPaul Mundt static struct pci_channel sh7780_pci_controller = {
36e79066a6SPaul Mundt 	.pci_ops	= &sh4_pci_ops,
37e79066a6SPaul Mundt 	.mem_resource	= &sh7785_mem_resource,
3809cfeb13SPaul Mundt 	.mem_offset	= 0x00000000,
39e79066a6SPaul Mundt 	.io_resource	= &sh7785_io_resource,
4009cfeb13SPaul Mundt 	.io_offset	= 0x00000000,
415582b064SPaul Mundt 	.io_map_base	= SH7780_PCI_IO_BASE,
42e79066a6SPaul Mundt };
43e79066a6SPaul Mundt 
44*85b59f5bSPaul Mundt static void __init sh7780_pci66_init(struct pci_channel *hose)
45*85b59f5bSPaul Mundt {
46*85b59f5bSPaul Mundt 	unsigned int tmp;
47*85b59f5bSPaul Mundt 
48*85b59f5bSPaul Mundt 	if (!pci_is_66mhz_capable(hose, 0, 0))
49*85b59f5bSPaul Mundt 		return;
50*85b59f5bSPaul Mundt 
51*85b59f5bSPaul Mundt 	/* Enable register access */
52*85b59f5bSPaul Mundt 	tmp = __raw_readl(hose->reg_base + SH4_PCICR);
53*85b59f5bSPaul Mundt 	tmp |= SH4_PCICR_PREFIX;
54*85b59f5bSPaul Mundt 	__raw_writel(tmp, hose->reg_base + SH4_PCICR);
55*85b59f5bSPaul Mundt 
56*85b59f5bSPaul Mundt 	/* Enable 66MHz operation */
57*85b59f5bSPaul Mundt 	tmp = __raw_readw(hose->reg_base + PCI_STATUS);
58*85b59f5bSPaul Mundt 	tmp |= PCI_STATUS_66MHZ;
59*85b59f5bSPaul Mundt 	__raw_writew(tmp, hose->reg_base + PCI_STATUS);
60*85b59f5bSPaul Mundt 
61*85b59f5bSPaul Mundt 	/* Done */
62*85b59f5bSPaul Mundt 	tmp = __raw_readl(hose->reg_base + SH4_PCICR);
63*85b59f5bSPaul Mundt 	tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
64*85b59f5bSPaul Mundt 	__raw_writel(tmp, hose->reg_base + SH4_PCICR);
65*85b59f5bSPaul Mundt }
66*85b59f5bSPaul Mundt 
67e79066a6SPaul Mundt static int __init sh7780_pci_init(void)
685283ecb5SPaul Mundt {
69e79066a6SPaul Mundt 	struct pci_channel *chan = &sh7780_pci_controller;
70a45635dfSPaul Mundt 	phys_addr_t memphys;
71a45635dfSPaul Mundt 	size_t memsize;
72959f85f8SPaul Mundt 	unsigned int id;
73a45635dfSPaul Mundt 	const char *type;
745283ecb5SPaul Mundt 
754e7b7fdbSPaul Mundt 	printk(KERN_NOTICE "PCI: Starting intialization.\n");
765283ecb5SPaul Mundt 
77e4c6a360SMagnus Damm 	chan->reg_base = 0xfe040000;
78e4c6a360SMagnus Damm 
794e7b7fdbSPaul Mundt 	/* Enable CPU access to the PCIC registers. */
804e7b7fdbSPaul Mundt 	__raw_writel(PCIECR_ENBL, PCIECR);
81959f85f8SPaul Mundt 
82a45635dfSPaul Mundt 	/* Reset */
83a45635dfSPaul Mundt 	__raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST,
84a45635dfSPaul Mundt 		     chan->reg_base + SH4_PCICR);
85a45635dfSPaul Mundt 
86aee4467bSPaul Mundt 	/*
87aee4467bSPaul Mundt 	 * Wait for it to come back up. The spec says to allow for up to
88aee4467bSPaul Mundt 	 * 1 second after toggling the reset pin, but in practice 100ms
89aee4467bSPaul Mundt 	 * is more than enough.
90aee4467bSPaul Mundt 	 */
91a45635dfSPaul Mundt 	mdelay(100);
92a45635dfSPaul Mundt 
93a45635dfSPaul Mundt 	id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
94a45635dfSPaul Mundt 	if (id != PCI_VENDOR_ID_RENESAS) {
954e7b7fdbSPaul Mundt 		printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
96959f85f8SPaul Mundt 		return -ENODEV;
97959f85f8SPaul Mundt 	}
98959f85f8SPaul Mundt 
99a45635dfSPaul Mundt 	id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
100a45635dfSPaul Mundt 	type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
101a45635dfSPaul Mundt 	       (id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
102a45635dfSPaul Mundt 	       (id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
103a45635dfSPaul Mundt 	       (id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
1044e7b7fdbSPaul Mundt 					  NULL;
1054e7b7fdbSPaul Mundt 	if (unlikely(!type)) {
1064e7b7fdbSPaul Mundt 		printk(KERN_ERR "PCI: Found an unsupported Renesas host "
1074e7b7fdbSPaul Mundt 		       "controller, device id 0x%04x.\n", id);
1084e7b7fdbSPaul Mundt 		return -EINVAL;
1094e7b7fdbSPaul Mundt 	}
1104e7b7fdbSPaul Mundt 
1114e7b7fdbSPaul Mundt 	printk(KERN_NOTICE "PCI: Found a Renesas %s host "
1124e7b7fdbSPaul Mundt 	       "controller, revision %d.\n", type,
113a45635dfSPaul Mundt 	       __raw_readb(chan->reg_base + PCI_REVISION_ID));
1144e7b7fdbSPaul Mundt 
115c66c1d79SPaul Mundt 	/*
116a45635dfSPaul Mundt 	 * Now throw it in to register initialization mode and
117a45635dfSPaul Mundt 	 * start the real work.
118c66c1d79SPaul Mundt 	 */
119a45635dfSPaul Mundt 	__raw_writel(SH4_PCICR_PREFIX, chan->reg_base + SH4_PCICR);
120a45635dfSPaul Mundt 
121a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
1225283ecb5SPaul Mundt 
123aee4467bSPaul Mundt 	memphys = __pa(memory_start);
124aee4467bSPaul Mundt 	memsize = roundup_pow_of_two(memory_end - memory_start);
125aee4467bSPaul Mundt 
126aee4467bSPaul Mundt 	/*
127aee4467bSPaul Mundt 	 * If there's more than 512MB of memory, we need to roll over to
128aee4467bSPaul Mundt 	 * LAR1/LSR1.
129aee4467bSPaul Mundt 	 */
130aee4467bSPaul Mundt 	if (memsize > SZ_512M) {
131aee4467bSPaul Mundt 		__raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
132aee4467bSPaul Mundt 		__raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
133aee4467bSPaul Mundt 			     chan->reg_base + SH4_PCILSR1);
134aee4467bSPaul Mundt 		memsize = SZ_512M;
135aee4467bSPaul Mundt 	} else {
136aee4467bSPaul Mundt 		/*
137aee4467bSPaul Mundt 		 * Otherwise just zero it out and disable it.
138aee4467bSPaul Mundt 		 */
139aee4467bSPaul Mundt 		__raw_writel(0, chan->reg_base + SH4_PCILAR1);
140aee4467bSPaul Mundt 		__raw_writel(0, chan->reg_base + SH4_PCILSR1);
141aee4467bSPaul Mundt 	}
142aee4467bSPaul Mundt 
143aee4467bSPaul Mundt 	/*
144aee4467bSPaul Mundt 	 * LAR0/LSR0 covers up to the first 512MB, which is enough to
145aee4467bSPaul Mundt 	 * cover all of lowmem on most platforms.
146aee4467bSPaul Mundt 	 */
147a45635dfSPaul Mundt 	__raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
148aee4467bSPaul Mundt 	__raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
149a45635dfSPaul Mundt 		     chan->reg_base + SH4_PCILSR0);
15062c7ae87SPaul Mundt 
151a45635dfSPaul Mundt 	/* Clear out PCI arbiter IRQs */
152a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH4_PCIAINT);
15362c7ae87SPaul Mundt 
154a45635dfSPaul Mundt 	/* Unmask all of the arbiter IRQs. */
155a45635dfSPaul Mundt 	__raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
156a45635dfSPaul Mundt 		     SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
157a45635dfSPaul Mundt 		     SH4_PCIAINT_WDPE, chan->reg_base + SH4_PCIAINTM);
15862c7ae87SPaul Mundt 
159a45635dfSPaul Mundt 	/* Clear all error conditions */
160a45635dfSPaul Mundt 	__raw_writew(PCI_STATUS_DETECTED_PARITY  | \
161a45635dfSPaul Mundt 		     PCI_STATUS_SIG_SYSTEM_ERROR | \
162a45635dfSPaul Mundt 		     PCI_STATUS_REC_MASTER_ABORT | \
163a45635dfSPaul Mundt 		     PCI_STATUS_REC_TARGET_ABORT | \
164a45635dfSPaul Mundt 		     PCI_STATUS_SIG_TARGET_ABORT | \
165a45635dfSPaul Mundt 		     PCI_STATUS_PARITY, chan->reg_base + PCI_STATUS);
1665283ecb5SPaul Mundt 
167a45635dfSPaul Mundt 	__raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
168a45635dfSPaul Mundt 		     PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
169a45635dfSPaul Mundt 		     PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
17062c7ae87SPaul Mundt 
171a45635dfSPaul Mundt 	/* Unmask all of the PCI IRQs */
172a45635dfSPaul Mundt 	__raw_writel(SH4_PCIINTM_TTADIM  | SH4_PCIINTM_TMTOIM  | \
173a45635dfSPaul Mundt 		     SH4_PCIINTM_MDEIM   | SH4_PCIINTM_APEDIM  | \
174a45635dfSPaul Mundt 		     SH4_PCIINTM_SDIM    | SH4_PCIINTM_DPEITWM | \
175a45635dfSPaul Mundt 		     SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM  | \
176a45635dfSPaul Mundt 		     SH4_PCIINTM_MADIMM  | SH4_PCIINTM_MWPDIM  | \
177a45635dfSPaul Mundt 		     SH4_PCIINTM_MRDPEIM, chan->reg_base + SH4_PCIINTM);
17862c7ae87SPaul Mundt 
179a45635dfSPaul Mundt 	/*
180a45635dfSPaul Mundt 	 * Disable the cache snoop controller for non-coherent DMA.
181a45635dfSPaul Mundt 	 */
182a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
183a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
184a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
185a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
18662c7ae87SPaul Mundt 
187a45635dfSPaul Mundt 	__raw_writel(0xfd000000, chan->reg_base + SH7780_PCIMBR0);
188a45635dfSPaul Mundt 	__raw_writel(0x00fc0000, chan->reg_base + SH7780_PCIMBMR0);
189a45635dfSPaul Mundt 
190a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
191a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
192a45635dfSPaul Mundt 
193a45635dfSPaul Mundt 	/*
194a45635dfSPaul Mundt 	 * Initialization mode complete, release the control register and
195a45635dfSPaul Mundt 	 * enable round robin mode to stop device overruns/starvation.
196a45635dfSPaul Mundt 	 */
197a45635dfSPaul Mundt 	__raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO,
198a45635dfSPaul Mundt 		     chan->reg_base + SH4_PCICR);
1995283ecb5SPaul Mundt 
200e79066a6SPaul Mundt 	register_pci_controller(chan);
201e79066a6SPaul Mundt 
202*85b59f5bSPaul Mundt 	sh7780_pci66_init(chan);
203*85b59f5bSPaul Mundt 
204*85b59f5bSPaul Mundt 	printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
205*85b59f5bSPaul Mundt 	       (__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
206*85b59f5bSPaul Mundt 	       66 : 33);
207*85b59f5bSPaul Mundt 
208d0e3db40SMagnus Damm 	return 0;
2095283ecb5SPaul Mundt }
210e79066a6SPaul Mundt arch_initcall(sh7780_pci_init);
211