3 * Copyright (C) 2016 Yoshinori Sato
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/of_address.h>
10 #include <linux/of_pci.h>
11 #include <linux/of_platform.h>
12 #include <linux/pci.h>
13 #include <linux/platform_device.h>
15 #include "pci-host-common.h"
16 #include "pci-sh7751.h"
18 #define pcic_writel(val, reg) __raw_writel(val, pci_reg_base + (reg))
19 #define pcic_readl(reg) __raw_readl(pci_reg_base + (reg))
21 unsigned long PCIBIOS_MIN_IO;
22 unsigned long PCIBIOS_MIN_MEM;
23 DEFINE_RAW_SPINLOCK(pci_config_lock);
29 static __initconst const struct fixups {
31 void (*fixup)(void __iomem *, void __iomem *);
35 static __init void pcic_fixups(struct device_node *np,
36 void __iomem *pcic, void __iomem *bcr)
39 const struct fixups *f = fixup_list;
41 for (i = 0; i < ARRAY_SIZE(fixup_list); i++) {
42 if (of_device_is_compatible(np, f->compatible)) {
50 * Direct access to PCI hardware...
52 #define CONFIG_CMD(bus, devfn, where) \
53 (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
56 * Functions for accessing PCI configuration space with type 1 accesses
58 static int sh4_pci_read(struct pci_bus *bus, unsigned int devfn,
59 int where, int size, u32 *val)
61 struct gen_pci *pci = bus->sysdata;
62 void __iomem *pci_reg_base = (void __iomem *)pci->cfg.res.start;
67 * PCIPDR may only be accessed as 32 bit words,
68 * so we must do byte alignment by hand
70 raw_spin_lock_irqsave(&pci_config_lock, flags);
71 pcic_writel(CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
72 data = pcic_readl(SH4_PCIPDR);
73 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
77 *val = (data >> ((where & 3) << 3)) & 0xff;
80 *val = (data >> ((where & 2) << 3)) & 0xffff;
86 return PCIBIOS_FUNC_NOT_SUPPORTED;
89 return PCIBIOS_SUCCESSFUL;
93 * Since SH4 only does 32bit access we'll have to do a read,
94 * mask,write operation.
95 * We'll allow an odd byte offset, though it should be illegal.
97 static int sh4_pci_write(struct pci_bus *bus, unsigned int devfn,
98 int where, int size, u32 val)
100 struct gen_pci *pci = bus->sysdata;
101 void __iomem *pci_reg_base = (void __iomem *)pci->cfg.res.start;
106 raw_spin_lock_irqsave(&pci_config_lock, flags);
107 pcic_writel(CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
108 data = pcic_readl(SH4_PCIPDR);
109 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
113 shift = (where & 3) << 3;
114 data &= ~(0xff << shift);
115 data |= ((val & 0xff) << shift);
118 shift = (where & 2) << 3;
119 data &= ~(0xffff << shift);
120 data |= ((val & 0xffff) << shift);
126 return PCIBIOS_FUNC_NOT_SUPPORTED;
129 pcic_writel(data, SH4_PCIPDR);
131 return PCIBIOS_SUCCESSFUL;
134 static struct gen_pci_cfg_bus_ops pci_sh7751_ops = {
136 .read = sh4_pci_read,
137 .write = sh4_pci_write,
142 * Called after each bus is probed, but before its children
145 void pcibios_fixup_bus(struct pci_bus *bus)
150 * We need to avoid collisions with `mirrored' VGA ports
151 * and other strange ISA hardware, so we always want the
152 * addresses to be allocated in the 0x000-0x0ff region
155 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
156 resource_size_t size, resource_size_t align)
158 resource_size_t start = res->start;
163 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
164 enum pci_mmap_state mmap_state, int write_combine)
167 * I/O space can be accessed via normal processor loads and stores on
168 * this platform but for now we elect not to do this and portable
169 * drivers should not do this anyway.
171 if (mmap_state == pci_mmap_io)
175 * Ignore write-combine; for now only return uncached mappings.
177 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
179 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
180 vma->vm_end - vma->vm_start,
184 static const struct of_device_id sh7751_pci_of_match[] = {
185 { .compatible = "renesas,sh7751-pci", },
188 MODULE_DEVICE_TABLE(of, sh7751_pci_of_match);
190 static int __init area_sdram_check(void __iomem *pci_reg_base,
196 word = __raw_readl(bcr + SH7751_BCR1);
197 /* check BCR for SDRAM in area */
198 if (((word >> area) & 1) == 0) {
199 printk("PCI: Area %d is not configured for SDRAM. BCR1=0x%lx\n",
203 pcic_writel(word, SH4_PCIBCR1);
205 word = __raw_readw(bcr + SH7751_BCR2);
206 /* check BCR2 for 32bit SDRAM interface*/
207 if (((word >> (area << 1)) & 0x3) != 0x3) {
208 printk("PCI: Area %d is not 32 bit SDRAM. BCR2=0x%lx\n",
212 pcic_writel(word, SH4_PCIBCR2);
217 static __init int sh7751_pci_probe(struct platform_device *pdev)
219 struct resource *res, *wres;
222 void __iomem *pci_reg_base;
226 pci = devm_kzalloc(&pdev->dev, sizeof(*pci), GFP_KERNEL);
230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 pci_reg_base = (void __iomem *)res->start;
232 if (IS_ERR(pci_reg_base))
233 return PTR_ERR(pci_reg_base);
235 wres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
237 return PTR_ERR(wres);
239 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
240 bcr = devm_ioremap_resource(&pdev->dev, res);
241 if (IS_ERR(pci_reg_base))
244 /* check for SH7751/SH7751R hardware */
245 id = pcic_readl(SH7751_PCICONF0);
246 if (id != ((SH7751_DEVICE_ID << 16) | SH7751_VENDOR_ID) &&
247 id != ((SH7751R_DEVICE_ID << 16) | SH7751_VENDOR_ID)) {
248 pr_warn("PCI: This is not an SH7751(R)\n");
251 dev_info(&pdev->dev, "PCI core found at %p\n",
254 /* Set the BCR's to enable PCI access */
255 reg = __raw_readl(bcr);
257 __raw_writel(reg, bcr);
259 /* Turn the clocks back on (not done in reset)*/
260 pcic_writel(0, SH4_PCICLKR);
261 /* Clear Powerdown IRQ's (not done in reset) */
262 word = SH4_PCIPINT_D3 | SH4_PCIPINT_D0;
263 pcic_writel(word, SH4_PCIPINT);
265 /* set the command/status bits to:
266 * Wait Cycle Control + Parity Enable + Bus Master +
269 word = SH7751_PCICONF1_WCC | SH7751_PCICONF1_PER |
270 SH7751_PCICONF1_BUM | SH7751_PCICONF1_MES;
271 pcic_writel(word, SH7751_PCICONF1);
273 /* define this host as the host bridge */
274 word = PCI_BASE_CLASS_BRIDGE << 24;
275 pcic_writel(word, SH7751_PCICONF2);
277 /* Set IO and Mem windows to local address
278 * Make PCI and local address the same for easy 1 to 1 mapping
280 word = wres->end - wres->start - 1;
281 pcic_writel(word, SH4_PCILSR0);
282 /* Set the values on window 0 PCI config registers */
283 word = P2SEGADDR(wres->start);
284 pcic_writel(word, SH4_PCILAR0);
285 pcic_writel(word, SH7751_PCICONF5);
287 /* check BCR for SDRAM in specified area */
288 area_sdram_check(pci_reg_base, bcr, (wres->start >> 27) & 0x07);
290 /* configure the wait control registers */
291 word = __raw_readl(bcr + SH7751_WCR1);
292 pcic_writel(word, SH4_PCIWCR1);
293 word = __raw_readl(bcr + SH7751_WCR2);
294 pcic_writel(word, SH4_PCIWCR2);
295 word = __raw_readl(bcr + SH7751_WCR3);
296 pcic_writel(word, SH4_PCIWCR3);
297 word = __raw_readl(bcr + SH7751_MCR);
298 pcic_writel(word, SH4_PCIMCR);
300 pcic_fixups(pdev->dev.of_node, pci_reg_base, bcr);
302 /* SH7751 init done, set central function init complete */
303 /* use round robin mode to stop a device starving/overruning */
304 word = SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_ARBM;
305 pcic_writel(word, SH4_PCICR);
307 pci->cfg.ops = &pci_sh7751_ops;
308 return pci_host_common_probe(pdev, pci);
311 static __refdata struct platform_driver sh7751_pci_driver = {
313 .name = "sh7751-pci",
314 .of_match_table = sh7751_pci_of_match,
316 .probe = sh7751_pci_probe,
318 module_platform_driver(sh7751_pci_driver);
320 MODULE_DESCRIPTION("SH7751 PCI driver");
321 MODULE_LICENSE("GPL v2");