xref: /openbmc/linux/arch/sh/drivers/pci/pci-sh7780.c (revision bcf39352eb9e9026f7a1028d4bce3707b65f104b)
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 
4485b59f5bSPaul Mundt static void __init sh7780_pci66_init(struct pci_channel *hose)
4585b59f5bSPaul Mundt {
4685b59f5bSPaul Mundt 	unsigned int tmp;
4785b59f5bSPaul Mundt 
4885b59f5bSPaul Mundt 	if (!pci_is_66mhz_capable(hose, 0, 0))
4985b59f5bSPaul Mundt 		return;
5085b59f5bSPaul Mundt 
5185b59f5bSPaul Mundt 	/* Enable register access */
5285b59f5bSPaul Mundt 	tmp = __raw_readl(hose->reg_base + SH4_PCICR);
5385b59f5bSPaul Mundt 	tmp |= SH4_PCICR_PREFIX;
5485b59f5bSPaul Mundt 	__raw_writel(tmp, hose->reg_base + SH4_PCICR);
5585b59f5bSPaul Mundt 
5685b59f5bSPaul Mundt 	/* Enable 66MHz operation */
5785b59f5bSPaul Mundt 	tmp = __raw_readw(hose->reg_base + PCI_STATUS);
5885b59f5bSPaul Mundt 	tmp |= PCI_STATUS_66MHZ;
5985b59f5bSPaul Mundt 	__raw_writew(tmp, hose->reg_base + PCI_STATUS);
6085b59f5bSPaul Mundt 
6185b59f5bSPaul Mundt 	/* Done */
6285b59f5bSPaul Mundt 	tmp = __raw_readl(hose->reg_base + SH4_PCICR);
6385b59f5bSPaul Mundt 	tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
6485b59f5bSPaul Mundt 	__raw_writel(tmp, hose->reg_base + SH4_PCICR);
6585b59f5bSPaul Mundt }
6685b59f5bSPaul 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;
74*bcf39352SPaul Mundt 	int ret;
755283ecb5SPaul Mundt 
764e7b7fdbSPaul Mundt 	printk(KERN_NOTICE "PCI: Starting intialization.\n");
775283ecb5SPaul Mundt 
78e4c6a360SMagnus Damm 	chan->reg_base = 0xfe040000;
79e4c6a360SMagnus Damm 
804e7b7fdbSPaul Mundt 	/* Enable CPU access to the PCIC registers. */
814e7b7fdbSPaul Mundt 	__raw_writel(PCIECR_ENBL, PCIECR);
82959f85f8SPaul Mundt 
83a45635dfSPaul Mundt 	/* Reset */
84a45635dfSPaul Mundt 	__raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST,
85a45635dfSPaul Mundt 		     chan->reg_base + SH4_PCICR);
86a45635dfSPaul Mundt 
87aee4467bSPaul Mundt 	/*
88aee4467bSPaul Mundt 	 * Wait for it to come back up. The spec says to allow for up to
89aee4467bSPaul Mundt 	 * 1 second after toggling the reset pin, but in practice 100ms
90aee4467bSPaul Mundt 	 * is more than enough.
91aee4467bSPaul Mundt 	 */
92a45635dfSPaul Mundt 	mdelay(100);
93a45635dfSPaul Mundt 
94a45635dfSPaul Mundt 	id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
95a45635dfSPaul Mundt 	if (id != PCI_VENDOR_ID_RENESAS) {
964e7b7fdbSPaul Mundt 		printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
97959f85f8SPaul Mundt 		return -ENODEV;
98959f85f8SPaul Mundt 	}
99959f85f8SPaul Mundt 
100a45635dfSPaul Mundt 	id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
101a45635dfSPaul Mundt 	type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
102a45635dfSPaul Mundt 	       (id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
103a45635dfSPaul Mundt 	       (id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
104a45635dfSPaul Mundt 	       (id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
1054e7b7fdbSPaul Mundt 					  NULL;
1064e7b7fdbSPaul Mundt 	if (unlikely(!type)) {
1074e7b7fdbSPaul Mundt 		printk(KERN_ERR "PCI: Found an unsupported Renesas host "
1084e7b7fdbSPaul Mundt 		       "controller, device id 0x%04x.\n", id);
1094e7b7fdbSPaul Mundt 		return -EINVAL;
1104e7b7fdbSPaul Mundt 	}
1114e7b7fdbSPaul Mundt 
1124e7b7fdbSPaul Mundt 	printk(KERN_NOTICE "PCI: Found a Renesas %s host "
1134e7b7fdbSPaul Mundt 	       "controller, revision %d.\n", type,
114a45635dfSPaul Mundt 	       __raw_readb(chan->reg_base + PCI_REVISION_ID));
1154e7b7fdbSPaul Mundt 
116c66c1d79SPaul Mundt 	/*
117a45635dfSPaul Mundt 	 * Now throw it in to register initialization mode and
118a45635dfSPaul Mundt 	 * start the real work.
119c66c1d79SPaul Mundt 	 */
120a45635dfSPaul Mundt 	__raw_writel(SH4_PCICR_PREFIX, chan->reg_base + SH4_PCICR);
121a45635dfSPaul Mundt 
122a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
1235283ecb5SPaul Mundt 
124aee4467bSPaul Mundt 	memphys = __pa(memory_start);
125aee4467bSPaul Mundt 	memsize = roundup_pow_of_two(memory_end - memory_start);
126aee4467bSPaul Mundt 
127aee4467bSPaul Mundt 	/*
128aee4467bSPaul Mundt 	 * If there's more than 512MB of memory, we need to roll over to
129aee4467bSPaul Mundt 	 * LAR1/LSR1.
130aee4467bSPaul Mundt 	 */
131aee4467bSPaul Mundt 	if (memsize > SZ_512M) {
132aee4467bSPaul Mundt 		__raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
133aee4467bSPaul Mundt 		__raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
134aee4467bSPaul Mundt 			     chan->reg_base + SH4_PCILSR1);
135aee4467bSPaul Mundt 		memsize = SZ_512M;
136aee4467bSPaul Mundt 	} else {
137aee4467bSPaul Mundt 		/*
138aee4467bSPaul Mundt 		 * Otherwise just zero it out and disable it.
139aee4467bSPaul Mundt 		 */
140aee4467bSPaul Mundt 		__raw_writel(0, chan->reg_base + SH4_PCILAR1);
141aee4467bSPaul Mundt 		__raw_writel(0, chan->reg_base + SH4_PCILSR1);
142aee4467bSPaul Mundt 	}
143aee4467bSPaul Mundt 
144aee4467bSPaul Mundt 	/*
145aee4467bSPaul Mundt 	 * LAR0/LSR0 covers up to the first 512MB, which is enough to
146aee4467bSPaul Mundt 	 * cover all of lowmem on most platforms.
147aee4467bSPaul Mundt 	 */
148a45635dfSPaul Mundt 	__raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
149aee4467bSPaul Mundt 	__raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
150a45635dfSPaul Mundt 		     chan->reg_base + SH4_PCILSR0);
15162c7ae87SPaul Mundt 
152a45635dfSPaul Mundt 	/* Clear out PCI arbiter IRQs */
153a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH4_PCIAINT);
15462c7ae87SPaul Mundt 
155a45635dfSPaul Mundt 	/* Unmask all of the arbiter IRQs. */
156a45635dfSPaul Mundt 	__raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
157a45635dfSPaul Mundt 		     SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
158a45635dfSPaul Mundt 		     SH4_PCIAINT_WDPE, chan->reg_base + SH4_PCIAINTM);
15962c7ae87SPaul Mundt 
160a45635dfSPaul Mundt 	/* Clear all error conditions */
161a45635dfSPaul Mundt 	__raw_writew(PCI_STATUS_DETECTED_PARITY  | \
162a45635dfSPaul Mundt 		     PCI_STATUS_SIG_SYSTEM_ERROR | \
163a45635dfSPaul Mundt 		     PCI_STATUS_REC_MASTER_ABORT | \
164a45635dfSPaul Mundt 		     PCI_STATUS_REC_TARGET_ABORT | \
165a45635dfSPaul Mundt 		     PCI_STATUS_SIG_TARGET_ABORT | \
166a45635dfSPaul Mundt 		     PCI_STATUS_PARITY, chan->reg_base + PCI_STATUS);
1675283ecb5SPaul Mundt 
168a45635dfSPaul Mundt 	__raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
169a45635dfSPaul Mundt 		     PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
170a45635dfSPaul Mundt 		     PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
17162c7ae87SPaul Mundt 
172a45635dfSPaul Mundt 	/* Unmask all of the PCI IRQs */
173a45635dfSPaul Mundt 	__raw_writel(SH4_PCIINTM_TTADIM  | SH4_PCIINTM_TMTOIM  | \
174a45635dfSPaul Mundt 		     SH4_PCIINTM_MDEIM   | SH4_PCIINTM_APEDIM  | \
175a45635dfSPaul Mundt 		     SH4_PCIINTM_SDIM    | SH4_PCIINTM_DPEITWM | \
176a45635dfSPaul Mundt 		     SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM  | \
177a45635dfSPaul Mundt 		     SH4_PCIINTM_MADIMM  | SH4_PCIINTM_MWPDIM  | \
178a45635dfSPaul Mundt 		     SH4_PCIINTM_MRDPEIM, chan->reg_base + SH4_PCIINTM);
17962c7ae87SPaul Mundt 
180a45635dfSPaul Mundt 	/*
181a45635dfSPaul Mundt 	 * Disable the cache snoop controller for non-coherent DMA.
182a45635dfSPaul Mundt 	 */
183a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
184a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
185a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
186a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
18762c7ae87SPaul Mundt 
188a45635dfSPaul Mundt 	__raw_writel(0xfd000000, chan->reg_base + SH7780_PCIMBR0);
189a45635dfSPaul Mundt 	__raw_writel(0x00fc0000, chan->reg_base + SH7780_PCIMBMR0);
190a45635dfSPaul Mundt 
191a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
192a45635dfSPaul Mundt 	__raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
193a45635dfSPaul Mundt 
194a45635dfSPaul Mundt 	/*
195a45635dfSPaul Mundt 	 * Initialization mode complete, release the control register and
196a45635dfSPaul Mundt 	 * enable round robin mode to stop device overruns/starvation.
197a45635dfSPaul Mundt 	 */
198a45635dfSPaul Mundt 	__raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO,
199a45635dfSPaul Mundt 		     chan->reg_base + SH4_PCICR);
2005283ecb5SPaul Mundt 
201*bcf39352SPaul Mundt 	ret = register_pci_controller(chan);
202*bcf39352SPaul Mundt 	if (unlikely(ret))
203*bcf39352SPaul Mundt 		return ret;
204e79066a6SPaul Mundt 
20585b59f5bSPaul Mundt 	sh7780_pci66_init(chan);
20685b59f5bSPaul Mundt 
20785b59f5bSPaul Mundt 	printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
20885b59f5bSPaul Mundt 	       (__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
20985b59f5bSPaul Mundt 	       66 : 33);
21085b59f5bSPaul Mundt 
211d0e3db40SMagnus Damm 	return 0;
2125283ecb5SPaul Mundt }
213e79066a6SPaul Mundt arch_initcall(sh7780_pci_init);
214