blob: 0e0ddd67e6e13944b9536818becffeba7cca5aeb [file] [log] [blame]
Paul Mundt5283ecb2006-09-27 15:59:17 +09001/*
Paul Mundt62c7ae82009-04-17 20:37:16 +09002 * Low-Level PCI Support for the SH7780
Paul Mundt5283ecb2006-09-27 15:59:17 +09003 *
Paul Mundta45635d2010-01-29 22:19:04 +09004 * Copyright (C) 2005 - 2010 Paul Mundt
Paul Mundt5283ecb2006-09-27 15:59:17 +09005 *
Paul Mundt62c7ae82009-04-17 20:37:16 +09006 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
Paul Mundt5283ecb2006-09-27 15:59:17 +09009 */
Paul Mundt5283ecb2006-09-27 15:59:17 +090010#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/pci.h>
Paul Mundt5283ecb2006-09-27 15:59:17 +090014#include <linux/errno.h>
Paul Mundt5283ecb2006-09-27 15:59:17 +090015#include <linux/delay.h>
Paul Mundtaee44672010-02-01 11:33:22 +090016#include <linux/log2.h>
Paul Mundt959f85f2006-09-27 16:43:28 +090017#include "pci-sh4.h"
Paul Mundta45635d2010-01-29 22:19:04 +090018#include <asm/mmu.h>
19#include <asm/sizes.h>
Paul Mundt5283ecb2006-09-27 15:59:17 +090020
Paul Mundte79066a2009-04-20 18:29:22 +090021static struct resource sh7785_io_resource = {
22 .name = "SH7785_IO",
Paul Mundta45635d2010-01-29 22:19:04 +090023 .start = 0x1000,
24 .end = SH7780_PCI_IO_SIZE - 1,
Paul Mundte79066a2009-04-20 18:29:22 +090025 .flags = IORESOURCE_IO
26};
27
28static struct resource sh7785_mem_resource = {
29 .name = "SH7785_mem",
30 .start = SH7780_PCI_MEMORY_BASE,
31 .end = SH7780_PCI_MEMORY_BASE + SH7780_PCI_MEM_SIZE - 1,
32 .flags = IORESOURCE_MEM
33};
34
35static struct pci_channel sh7780_pci_controller = {
36 .pci_ops = &sh4_pci_ops,
37 .mem_resource = &sh7785_mem_resource,
Paul Mundt09cfeb12009-04-20 18:42:00 +090038 .mem_offset = 0x00000000,
Paul Mundte79066a2009-04-20 18:29:22 +090039 .io_resource = &sh7785_io_resource,
Paul Mundt09cfeb12009-04-20 18:42:00 +090040 .io_offset = 0x00000000,
Paul Mundt5582b062009-05-27 00:12:58 +090041 .io_map_base = SH7780_PCI_IO_BASE,
Paul Mundte79066a2009-04-20 18:29:22 +090042};
43
Paul Mundt85b59f52010-02-01 13:01:42 +090044static void __init sh7780_pci66_init(struct pci_channel *hose)
45{
46 unsigned int tmp;
47
48 if (!pci_is_66mhz_capable(hose, 0, 0))
49 return;
50
51 /* Enable register access */
52 tmp = __raw_readl(hose->reg_base + SH4_PCICR);
53 tmp |= SH4_PCICR_PREFIX;
54 __raw_writel(tmp, hose->reg_base + SH4_PCICR);
55
56 /* Enable 66MHz operation */
57 tmp = __raw_readw(hose->reg_base + PCI_STATUS);
58 tmp |= PCI_STATUS_66MHZ;
59 __raw_writew(tmp, hose->reg_base + PCI_STATUS);
60
61 /* Done */
62 tmp = __raw_readl(hose->reg_base + SH4_PCICR);
63 tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
64 __raw_writel(tmp, hose->reg_base + SH4_PCICR);
65}
66
Paul Mundte79066a2009-04-20 18:29:22 +090067static int __init sh7780_pci_init(void)
Paul Mundt5283ecb2006-09-27 15:59:17 +090068{
Paul Mundte79066a2009-04-20 18:29:22 +090069 struct pci_channel *chan = &sh7780_pci_controller;
Paul Mundta45635d2010-01-29 22:19:04 +090070 phys_addr_t memphys;
71 size_t memsize;
Paul Mundt959f85f2006-09-27 16:43:28 +090072 unsigned int id;
Paul Mundta45635d2010-01-29 22:19:04 +090073 const char *type;
Paul Mundtbcf39352010-02-01 13:11:25 +090074 int ret;
Paul Mundt5283ecb2006-09-27 15:59:17 +090075
Paul Mundt4e7b7fd2009-04-17 15:05:19 +090076 printk(KERN_NOTICE "PCI: Starting intialization.\n");
Paul Mundt5283ecb2006-09-27 15:59:17 +090077
Magnus Damme4c6a362008-02-19 21:35:04 +090078 chan->reg_base = 0xfe040000;
79
Paul Mundt4e7b7fd2009-04-17 15:05:19 +090080 /* Enable CPU access to the PCIC registers. */
81 __raw_writel(PCIECR_ENBL, PCIECR);
Paul Mundt959f85f2006-09-27 16:43:28 +090082
Paul Mundta45635d2010-01-29 22:19:04 +090083 /* Reset */
84 __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST,
85 chan->reg_base + SH4_PCICR);
86
Paul Mundtaee44672010-02-01 11:33:22 +090087 /*
88 * Wait for it to come back up. The spec says to allow for up to
89 * 1 second after toggling the reset pin, but in practice 100ms
90 * is more than enough.
91 */
Paul Mundta45635d2010-01-29 22:19:04 +090092 mdelay(100);
93
94 id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
95 if (id != PCI_VENDOR_ID_RENESAS) {
Paul Mundt4e7b7fd2009-04-17 15:05:19 +090096 printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
Paul Mundt959f85f2006-09-27 16:43:28 +090097 return -ENODEV;
98 }
99
Paul Mundta45635d2010-01-29 22:19:04 +0900100 id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
101 type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
102 (id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
103 (id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
104 (id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900105 NULL;
106 if (unlikely(!type)) {
107 printk(KERN_ERR "PCI: Found an unsupported Renesas host "
108 "controller, device id 0x%04x.\n", id);
109 return -EINVAL;
110 }
111
112 printk(KERN_NOTICE "PCI: Found a Renesas %s host "
113 "controller, revision %d.\n", type,
Paul Mundta45635d2010-01-29 22:19:04 +0900114 __raw_readb(chan->reg_base + PCI_REVISION_ID));
Paul Mundt4e7b7fd2009-04-17 15:05:19 +0900115
Paul Mundtc66c1d72009-04-17 16:38:00 +0900116 /*
Paul Mundta45635d2010-01-29 22:19:04 +0900117 * Now throw it in to register initialization mode and
118 * start the real work.
Paul Mundtc66c1d72009-04-17 16:38:00 +0900119 */
Paul Mundta45635d2010-01-29 22:19:04 +0900120 __raw_writel(SH4_PCICR_PREFIX, chan->reg_base + SH4_PCICR);
121
Paul Mundta45635d2010-01-29 22:19:04 +0900122 __raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
Paul Mundt5283ecb2006-09-27 15:59:17 +0900123
Paul Mundtaee44672010-02-01 11:33:22 +0900124 memphys = __pa(memory_start);
125 memsize = roundup_pow_of_two(memory_end - memory_start);
126
127 /*
128 * If there's more than 512MB of memory, we need to roll over to
129 * LAR1/LSR1.
130 */
131 if (memsize > SZ_512M) {
132 __raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
133 __raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
134 chan->reg_base + SH4_PCILSR1);
135 memsize = SZ_512M;
136 } else {
137 /*
138 * Otherwise just zero it out and disable it.
139 */
140 __raw_writel(0, chan->reg_base + SH4_PCILAR1);
141 __raw_writel(0, chan->reg_base + SH4_PCILSR1);
142 }
143
144 /*
145 * LAR0/LSR0 covers up to the first 512MB, which is enough to
146 * cover all of lowmem on most platforms.
147 */
Paul Mundta45635d2010-01-29 22:19:04 +0900148 __raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
Paul Mundtaee44672010-02-01 11:33:22 +0900149 __raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
Paul Mundta45635d2010-01-29 22:19:04 +0900150 chan->reg_base + SH4_PCILSR0);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900151
Paul Mundta45635d2010-01-29 22:19:04 +0900152 /* Clear out PCI arbiter IRQs */
153 __raw_writel(0, chan->reg_base + SH4_PCIAINT);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900154
Paul Mundta45635d2010-01-29 22:19:04 +0900155 /* Unmask all of the arbiter IRQs. */
156 __raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
157 SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
158 SH4_PCIAINT_WDPE, chan->reg_base + SH4_PCIAINTM);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900159
Paul Mundta45635d2010-01-29 22:19:04 +0900160 /* Clear all error conditions */
161 __raw_writew(PCI_STATUS_DETECTED_PARITY | \
162 PCI_STATUS_SIG_SYSTEM_ERROR | \
163 PCI_STATUS_REC_MASTER_ABORT | \
164 PCI_STATUS_REC_TARGET_ABORT | \
165 PCI_STATUS_SIG_TARGET_ABORT | \
166 PCI_STATUS_PARITY, chan->reg_base + PCI_STATUS);
Paul Mundt5283ecb2006-09-27 15:59:17 +0900167
Paul Mundta45635d2010-01-29 22:19:04 +0900168 __raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
169 PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
170 PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900171
Paul Mundta45635d2010-01-29 22:19:04 +0900172 /* Unmask all of the PCI IRQs */
173 __raw_writel(SH4_PCIINTM_TTADIM | SH4_PCIINTM_TMTOIM | \
174 SH4_PCIINTM_MDEIM | SH4_PCIINTM_APEDIM | \
175 SH4_PCIINTM_SDIM | SH4_PCIINTM_DPEITWM | \
176 SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM | \
177 SH4_PCIINTM_MADIMM | SH4_PCIINTM_MWPDIM | \
178 SH4_PCIINTM_MRDPEIM, chan->reg_base + SH4_PCIINTM);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900179
Paul Mundta45635d2010-01-29 22:19:04 +0900180 /*
181 * Disable the cache snoop controller for non-coherent DMA.
182 */
183 __raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
184 __raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
185 __raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
186 __raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
Paul Mundt62c7ae82009-04-17 20:37:16 +0900187
Paul Mundta45635d2010-01-29 22:19:04 +0900188 __raw_writel(0xfd000000, chan->reg_base + SH7780_PCIMBR0);
189 __raw_writel(0x00fc0000, chan->reg_base + SH7780_PCIMBMR0);
190
191 __raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
192 __raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
193
194 /*
195 * Initialization mode complete, release the control register and
196 * enable round robin mode to stop device overruns/starvation.
197 */
198 __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO,
199 chan->reg_base + SH4_PCICR);
Paul Mundt5283ecb2006-09-27 15:59:17 +0900200
Paul Mundtbcf39352010-02-01 13:11:25 +0900201 ret = register_pci_controller(chan);
202 if (unlikely(ret))
203 return ret;
Paul Mundte79066a2009-04-20 18:29:22 +0900204
Paul Mundt85b59f52010-02-01 13:01:42 +0900205 sh7780_pci66_init(chan);
206
207 printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
208 (__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
209 66 : 33);
210
Magnus Dammd0e3db42009-03-11 15:46:14 +0900211 return 0;
Paul Mundt5283ecb2006-09-27 15:59:17 +0900212}
Paul Mundte79066a2009-04-20 18:29:22 +0900213arch_initcall(sh7780_pci_init);