Merge branch 'timer/cleanup' into late/mvebu2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kernel / io-workarounds.c
CommitLineData
014da7ff 1/*
7cfb62a2
IK
2 * Support PCI IO workaround
3 *
014da7ff
BH
4 * Copyright (C) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5 * IBM, Corp.
7cfb62a2 6 * (C) Copyright 2007-2008 TOSHIBA CORPORATION
014da7ff
BH
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#undef DEBUG
13
14#include <linux/kernel.h>
333a1518 15#include <linux/sched.h> /* for init_mm */
7cfb62a2 16
014da7ff
BH
17#include <asm/io.h>
18#include <asm/machdep.h>
7cfb62a2 19#include <asm/pgtable.h>
014da7ff 20#include <asm/ppc-pci.h>
3cc30d07 21#include <asm/io-workarounds.h>
014da7ff 22
7cfb62a2 23#define IOWA_MAX_BUS 8
014da7ff 24
7cfb62a2
IK
25static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
26static unsigned int iowa_bus_count;
014da7ff 27
7cfb62a2
IK
28static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
29{
30 int i, j;
31 struct resource *res;
32 unsigned long vstart, vend;
014da7ff 33
7cfb62a2
IK
34 for (i = 0; i < iowa_bus_count; i++) {
35 struct iowa_bus *bus = &iowa_busses[i];
36 struct pci_controller *phb = bus->phb;
014da7ff 37
7cfb62a2
IK
38 if (vaddr) {
39 vstart = (unsigned long)phb->io_base_virt;
40 vend = vstart + phb->pci_io_size - 1;
41 if ((vaddr >= vstart) && (vaddr <= vend))
42 return bus;
43 }
44
45 if (paddr)
46 for (j = 0; j < 3; j++) {
47 res = &phb->mem_resources[j];
48 if (paddr >= res->start && paddr <= res->end)
49 return bus;
50 }
014da7ff 51 }
7cfb62a2 52
014da7ff
BH
53 return NULL;
54}
55
7cfb62a2 56struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
014da7ff 57{
7cfb62a2 58 struct iowa_bus *bus;
014da7ff
BH
59 int token;
60
014da7ff
BH
61 token = PCI_GET_ADDR_TOKEN(addr);
62
7cfb62a2
IK
63 if (token && token <= iowa_bus_count)
64 bus = &iowa_busses[token - 1];
014da7ff
BH
65 else {
66 unsigned long vaddr, paddr;
67 pte_t *ptep;
68
014da7ff 69 vaddr = (unsigned long)PCI_FIX_ADDR(addr);
7cfb62a2
IK
70 if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
71 return NULL;
014da7ff 72
014da7ff
BH
73 ptep = find_linux_pte(init_mm.pgd, vaddr);
74 if (ptep == NULL)
75 paddr = 0;
76 else
77 paddr = pte_pfn(*ptep) << PAGE_SHIFT;
7cfb62a2 78 bus = iowa_pci_find(vaddr, paddr);
014da7ff 79
014da7ff 80 if (bus == NULL)
7cfb62a2 81 return NULL;
014da7ff
BH
82 }
83
7cfb62a2 84 return bus;
014da7ff
BH
85}
86
7cfb62a2 87struct iowa_bus *iowa_pio_find_bus(unsigned long port)
014da7ff 88{
7cfb62a2
IK
89 unsigned long vaddr = (unsigned long)pci_io_base + port;
90 return iowa_pci_find(vaddr, 0);
014da7ff
BH
91}
92
014da7ff 93
7cfb62a2
IK
94#define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \
95static ret iowa_##name at \
96{ \
97 struct iowa_bus *bus; \
98 bus = iowa_##space##_find_bus(aa); \
99 if (bus && bus->ops && bus->ops->name) \
100 return bus->ops->name al; \
101 return __do_##name al; \
014da7ff
BH
102}
103
7cfb62a2
IK
104#define DEF_PCI_AC_NORET(name, at, al, space, aa) \
105static void iowa_##name at \
106{ \
107 struct iowa_bus *bus; \
108 bus = iowa_##space##_find_bus(aa); \
109 if (bus && bus->ops && bus->ops->name) { \
110 bus->ops->name al; \
111 return; \
112 } \
113 __do_##name al; \
014da7ff
BH
114}
115
7cfb62a2 116#include <asm/io-defs.h>
014da7ff 117
7cfb62a2
IK
118#undef DEF_PCI_AC_RET
119#undef DEF_PCI_AC_NORET
014da7ff 120
cad5cef6 121static const struct ppc_pci_io iowa_pci_io = {
014da7ff 122
7cfb62a2
IK
123#define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name,
124#define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name,
014da7ff 125
7cfb62a2 126#include <asm/io-defs.h>
014da7ff 127
7cfb62a2
IK
128#undef DEF_PCI_AC_RET
129#undef DEF_PCI_AC_NORET
014da7ff 130
7cfb62a2 131};
014da7ff 132
b36ac9e8 133static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
1cdab55d 134 unsigned long flags, void *caller)
014da7ff 135{
7cfb62a2 136 struct iowa_bus *bus;
1cdab55d 137 void __iomem *res = __ioremap_caller(addr, size, flags, caller);
014da7ff
BH
138 int busno;
139
b36ac9e8 140 bus = iowa_pci_find(0, (unsigned long)addr);
014da7ff 141 if (bus != NULL) {
7cfb62a2 142 busno = bus - iowa_busses;
014da7ff
BH
143 PCI_SET_ADDR_TOKEN(res, busno + 1);
144 }
014da7ff
BH
145 return res;
146}
147
d1109b75 148/* Enable IO workaround */
cad5cef6 149static void io_workaround_init(void)
d1109b75
ME
150{
151 static int io_workaround_inited;
152
153 if (io_workaround_inited)
154 return;
155 ppc_pci_io = iowa_pci_io;
156 ppc_md.ioremap = iowa_ioremap;
157 io_workaround_inited = 1;
158}
159
160/* Register new bus to support workaround */
cad5cef6
GKH
161void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
162 int (*initfunc)(struct iowa_bus *, void *), void *data)
014da7ff 163{
7cfb62a2 164 struct iowa_bus *bus;
44ef3390 165 struct device_node *np = phb->dn;
014da7ff 166
d1109b75
ME
167 io_workaround_init();
168
7cfb62a2
IK
169 if (iowa_bus_count >= IOWA_MAX_BUS) {
170 pr_err("IOWA:Too many pci bridges, "
171 "workarounds disabled for %s\n", np->full_name);
014da7ff
BH
172 return;
173 }
174
7cfb62a2
IK
175 bus = &iowa_busses[iowa_bus_count];
176 bus->phb = phb;
177 bus->ops = ops;
69b12368 178 bus->private = data;
014da7ff 179
7cfb62a2
IK
180 if (initfunc)
181 if ((*initfunc)(bus, data))
182 return;
014da7ff 183
7cfb62a2 184 iowa_bus_count++;
014da7ff 185
7cfb62a2 186 pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name);
014da7ff
BH
187}
188