2 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
26 #ifdef CONFIG_PPC_DCR_MMIO
27 static struct device_node *find_dcr_parent(struct device_node *node)
29 struct device_node *par, *tmp;
32 for (par = of_node_get(node); par;) {
33 if (of_get_property(par, "dcr-controller", NULL))
35 p = of_get_property(par, "dcr-parent", NULL);
38 par = of_get_parent(par);
40 par = of_find_node_by_phandle(*p);
47 #if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO)
49 bool dcr_map_ok_generic(dcr_host_t host)
51 if (host.type == DCR_HOST_NATIVE)
52 return dcr_map_ok_native(host.host.native);
53 else if (host.type == DCR_HOST_MMIO)
54 return dcr_map_ok_mmio(host.host.mmio);
58 EXPORT_SYMBOL_GPL(dcr_map_ok_generic);
60 dcr_host_t dcr_map_generic(struct device_node *dev,
65 struct device_node *dp;
68 host.type = DCR_HOST_INVALID;
70 dp = find_dcr_parent(dev);
74 prop = of_get_property(dp, "dcr-access-method", NULL);
76 pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop);
78 if (!strcmp(prop, "native")) {
79 host.type = DCR_HOST_NATIVE;
80 host.host.native = dcr_map_native(dev, dcr_n, dcr_c);
81 } else if (!strcmp(prop, "mmio")) {
82 host.type = DCR_HOST_MMIO;
83 host.host.mmio = dcr_map_mmio(dev, dcr_n, dcr_c);
89 EXPORT_SYMBOL_GPL(dcr_map_generic);
91 void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c)
93 if (host.type == DCR_HOST_NATIVE)
94 dcr_unmap_native(host.host.native, dcr_c);
95 else if (host.type == DCR_HOST_MMIO)
96 dcr_unmap_mmio(host.host.mmio, dcr_c);
97 else /* host.type == DCR_HOST_INVALID */
100 EXPORT_SYMBOL_GPL(dcr_unmap_generic);
102 u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n)
104 if (host.type == DCR_HOST_NATIVE)
105 return dcr_read_native(host.host.native, dcr_n);
106 else if (host.type == DCR_HOST_MMIO)
107 return dcr_read_mmio(host.host.mmio, dcr_n);
108 else /* host.type == DCR_HOST_INVALID */
112 EXPORT_SYMBOL_GPL(dcr_read_generic);
114 void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value)
116 if (host.type == DCR_HOST_NATIVE)
117 dcr_write_native(host.host.native, dcr_n, value);
118 else if (host.type == DCR_HOST_MMIO)
119 dcr_write_mmio(host.host.mmio, dcr_n, value);
120 else /* host.type == DCR_HOST_INVALID */
123 EXPORT_SYMBOL_GPL(dcr_write_generic);
125 #endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */
127 unsigned int dcr_resource_start(struct device_node *np, unsigned int index)
130 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
132 if (dr == NULL || ds & 1 || index >= (ds / 8))
135 return dr[index * 2];
137 EXPORT_SYMBOL_GPL(dcr_resource_start);
139 unsigned int dcr_resource_len(struct device_node *np, unsigned int index)
142 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
144 if (dr == NULL || ds & 1 || index >= (ds / 8))
147 return dr[index * 2 + 1];
149 EXPORT_SYMBOL_GPL(dcr_resource_len);
151 #ifdef CONFIG_PPC_DCR_MMIO
153 u64 of_translate_dcr_address(struct device_node *dev,
155 unsigned int *out_stride)
157 struct device_node *dp;
160 u64 ret = OF_BAD_ADDR;
162 dp = find_dcr_parent(dev);
166 /* Stride is not properly defined yet, default to 0x10 for Axon */
167 p = of_get_property(dp, "dcr-mmio-stride", NULL);
168 stride = (p == NULL) ? 0x10 : *p;
170 /* XXX FIXME: Which property name is to use of the 2 following ? */
171 p = of_get_property(dp, "dcr-mmio-range", NULL);
173 p = of_get_property(dp, "dcr-mmio-space", NULL);
177 /* Maybe could do some better range checking here */
178 ret = of_translate_address(dp, p);
179 if (ret != OF_BAD_ADDR)
180 ret += (u64)(stride) * (u64)dcr_n;
182 *out_stride = stride;
189 dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
193 dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
196 pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
197 dev->full_name, dcr_n, dcr_c);
199 addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
200 pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
201 (unsigned long long) addr, ret.stride);
202 if (addr == OF_BAD_ADDR)
204 pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
205 ret.token = ioremap(addr, dcr_c * ret.stride);
206 if (ret.token == NULL)
208 pr_debug("mapped at 0x%p -> base is 0x%p\n",
209 ret.token, ret.token - dcr_n * ret.stride);
210 ret.token -= dcr_n * ret.stride;
213 EXPORT_SYMBOL_GPL(dcr_map_mmio);
215 void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
217 dcr_host_mmio_t h = host;
221 h.token += host.base * h.stride;
225 EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
227 #endif /* defined(CONFIG_PPC_DCR_MMIO) */
229 #ifdef CONFIG_PPC_DCR_NATIVE
230 DEFINE_SPINLOCK(dcr_ind_lock);
231 #endif /* defined(CONFIG_PPC_DCR_NATIVE) */