OSDN Git Service

fd6548b27e12f4a43419b1a32bddff3ccf44d459
[uclinux-h8/linux.git] / drivers / pci / host / pci-sh7751.c
1 /*
2  * SH7751 PCI driver
3  * Copyright (C) 2016 Yoshinori Sato
4  *
5  */
6
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>
14 #include <linux/io.h>
15 #include "pci-host-common.h"
16 #include "pci-sh7751.h"
17
18 #define pcic_writel(val, reg) __raw_writel(val, pci_reg_base + (reg))
19 #define pcic_readl(reg) __raw_readl(pci_reg_base + (reg))
20
21 unsigned long PCIBIOS_MIN_IO;
22 unsigned long PCIBIOS_MIN_MEM;
23 DEFINE_RAW_SPINLOCK(pci_config_lock);
24
25 /*
26  * PCIC fixups
27  */
28
29 static __initconst const struct fixups {
30         char *compatible;
31         void (*fixup)(void __iomem *, void __iomem *);
32 } fixup_list[] = {
33 };
34
35 static __init void pcic_fixups(struct device_node *np,
36                        void __iomem *pcic, void __iomem *bcr)
37 {
38         int i;
39         const struct fixups *f = fixup_list;
40
41         for (i = 0; i < ARRAY_SIZE(fixup_list); i++) {
42                 if (of_device_is_compatible(np, f->compatible)) {
43                         f->fixup(pcic, bcr);
44                         break;
45                 }
46         }
47 }
48
49 /*
50  * Direct access to PCI hardware...
51  */
52 #define CONFIG_CMD(bus, devfn, where) \
53         (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
54
55 /*
56  * Functions for accessing PCI configuration space with type 1 accesses
57  */
58 static int sh4_pci_read(struct pci_bus *bus, unsigned int devfn,
59                            int where, int size, u32 *val)
60 {
61         struct gen_pci *pci = bus->sysdata;
62         void __iomem *pci_reg_base = (void __iomem *)pci->cfg.res.start;
63         unsigned long flags;
64         u32 data;
65
66         /*
67          * PCIPDR may only be accessed as 32 bit words,
68          * so we must do byte alignment by hand
69          */
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);
74
75         switch (size) {
76         case 1:
77                 *val = (data >> ((where & 3) << 3)) & 0xff;
78                 break;
79         case 2:
80                 *val = (data >> ((where & 2) << 3)) & 0xffff;
81                 break;
82         case 4:
83                 *val = data;
84                 break;
85         default:
86                 return PCIBIOS_FUNC_NOT_SUPPORTED;
87         }
88
89         return PCIBIOS_SUCCESSFUL;
90 }
91
92 /*
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.
96  */
97 static int sh4_pci_write(struct pci_bus *bus, unsigned int devfn,
98                          int where, int size, u32 val)
99 {
100         struct gen_pci *pci = bus->sysdata;
101         void __iomem *pci_reg_base = (void __iomem *)pci->cfg.res.start;
102         unsigned long flags;
103         int shift;
104         u32 data;
105
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);
110
111         switch (size) {
112         case 1:
113                 shift = (where & 3) << 3;
114                 data &= ~(0xff << shift);
115                 data |= ((val & 0xff) << shift);
116                 break;
117         case 2:
118                 shift = (where & 2) << 3;
119                 data &= ~(0xffff << shift);
120                 data |= ((val & 0xffff) << shift);
121                 break;
122         case 4:
123                 data = val;
124                 break;
125         default:
126                 return PCIBIOS_FUNC_NOT_SUPPORTED;
127         }
128
129         pcic_writel(data, SH4_PCIPDR);
130
131         return PCIBIOS_SUCCESSFUL;
132 }
133
134 static struct gen_pci_cfg_bus_ops pci_sh7751_ops = {
135         .ops = {
136                 .read   = sh4_pci_read,
137                 .write  = sh4_pci_write,
138         },
139 };
140
141 /*
142  *  Called after each bus is probed, but before its children
143  *  are examined.
144  */
145 void pcibios_fixup_bus(struct pci_bus *bus)
146 {
147 }
148
149 /*
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
153  * modulo 0x400.
154  */
155 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
156                                               resource_size_t size, resource_size_t align)
157 {
158         resource_size_t start = res->start;
159
160         return start;
161 }
162
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)
165 {
166         /*
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.
170          */
171         if (mmap_state == pci_mmap_io)
172                 return -EINVAL;
173
174         /*
175          * Ignore write-combine; for now only return uncached mappings.
176          */
177         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
178
179         return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
180                                vma->vm_end - vma->vm_start,
181                                vma->vm_page_prot);
182 }
183
184 static const struct of_device_id sh7751_pci_of_match[] = {
185         { .compatible = "renesas,sh7751-pci", },
186         { },
187 };
188 MODULE_DEVICE_TABLE(of, sh7751_pci_of_match);
189
190 static int __init area_sdram_check(void __iomem *pci_reg_base,
191                                    void __iomem *bcr,
192                                    unsigned int area)
193 {
194         unsigned long word;
195
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",
200                        area, word);
201                 return 0;
202         }
203         pcic_writel(word, SH4_PCIBCR1);
204
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",
209                        area, word);
210                 return 0;
211         }
212         pcic_writel(word, SH4_PCIBCR2);
213
214         return 1;
215 }
216
217 static __init int sh7751_pci_probe(struct platform_device *pdev)
218 {
219         struct resource *res, *wres;
220         u32 id;
221         u32 reg, word;
222         void __iomem *pci_reg_base;
223         void __iomem *bcr;
224         struct gen_pci *pci;
225
226         pci = devm_kzalloc(&pdev->dev, sizeof(*pci), GFP_KERNEL);
227         if (!pci)
228                 return -ENOMEM;
229
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);
234
235         wres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
236         if (IS_ERR(wres))
237                 return PTR_ERR(wres);
238
239         res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
240         bcr = devm_ioremap_resource(&pdev->dev, res);
241         if (IS_ERR(pci_reg_base))
242                 return PTR_ERR(bcr);
243
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");
249                 return -ENODEV;
250         }
251         dev_info(&pdev->dev, "PCI core found at %p\n",
252                 pci_reg_base);
253
254         /* Set the BCR's to enable PCI access */
255         reg = __raw_readl(bcr);
256         reg |= 0x80000;
257         __raw_writel(reg, bcr);
258
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);
264
265         /* set the command/status bits to:
266          * Wait Cycle Control + Parity Enable + Bus Master +
267          * Mem space enable
268          */
269         word = SH7751_PCICONF1_WCC | SH7751_PCICONF1_PER |
270                SH7751_PCICONF1_BUM | SH7751_PCICONF1_MES;
271         pcic_writel(word, SH7751_PCICONF1);
272
273         /* define this host as the host bridge */
274         word = PCI_BASE_CLASS_BRIDGE << 24;
275         pcic_writel(word, SH7751_PCICONF2);
276
277         /* Set IO and Mem windows to local address
278          * Make PCI and local address the same for easy 1 to 1 mapping
279          */
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);
286
287         /* check BCR for SDRAM in specified area */
288         area_sdram_check(pci_reg_base, bcr, (wres->start >> 27) & 0x07);
289
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);
299
300         pcic_fixups(pdev->dev.of_node, pci_reg_base, bcr);
301
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);
306
307         pci->cfg.ops = &pci_sh7751_ops;
308         return pci_host_common_probe(pdev, pci);
309 }
310
311 static __refdata struct platform_driver sh7751_pci_driver = {
312         .driver = {
313                 .name = "sh7751-pci",
314                 .of_match_table = sh7751_pci_of_match,
315         },
316         .probe = sh7751_pci_probe,
317 };
318 module_platform_driver(sh7751_pci_driver);
319
320 MODULE_DESCRIPTION("SH7751 PCI driver");
321 MODULE_LICENSE("GPL v2");