blob: 5f39a79b066013b3a94efc3e9b68ad5f0121f4ae [file] [log] [blame]
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +11001/*
2 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 *
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.
9 *
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.
14 *
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
18 */
19
20#undef DEBUG
21
22#include <linux/kernel.h>
23#include <asm/prom.h>
24#include <asm/dcr.h>
25
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100026static struct device_node *find_dcr_parent(struct device_node *node)
27{
28 struct device_node *par, *tmp;
29 const u32 *p;
30
31 for (par = of_node_get(node); par;) {
32 if (of_get_property(par, "dcr-controller", NULL))
33 break;
34 p = of_get_property(par, "dcr-parent", NULL);
35 tmp = par;
36 if (p == NULL)
37 par = of_get_parent(par);
38 else
39 par = of_find_node_by_phandle(*p);
40 of_node_put(tmp);
41 }
42 return par;
43}
44
45#if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO)
46
47bool dcr_map_ok_generic(dcr_host_t host)
48{
49 if (host.type == DCR_HOST_NATIVE)
50 return dcr_map_ok_native(host.host.native);
51 else if (host.type == DCR_HOST_MMIO)
52 return dcr_map_ok_mmio(host.host.mmio);
53 else
54 return 0;
55}
56EXPORT_SYMBOL_GPL(dcr_map_ok_generic);
57
58dcr_host_t dcr_map_generic(struct device_node *dev,
59 unsigned int dcr_n,
60 unsigned int dcr_c)
61{
62 dcr_host_t host;
63 struct device_node *dp;
64 const char *prop;
65
66 host.type = DCR_HOST_INVALID;
67
68 dp = find_dcr_parent(dev);
69 if (dp == NULL)
70 return host;
71
72 prop = of_get_property(dp, "dcr-access-method", NULL);
73
74 pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop);
75
76 if (!strcmp(prop, "native")) {
77 host.type = DCR_HOST_NATIVE;
78 host.host.native = dcr_map_native(dev, dcr_n, dcr_c);
79 } else if (!strcmp(prop, "mmio")) {
80 host.type = DCR_HOST_MMIO;
81 host.host.mmio = dcr_map_mmio(dev, dcr_n, dcr_c);
82 }
83
84 of_node_put(dp);
85 return host;
86}
87EXPORT_SYMBOL_GPL(dcr_map_generic);
88
89void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c)
90{
91 if (host.type == DCR_HOST_NATIVE)
92 dcr_unmap_native(host.host.native, dcr_c);
93 else if (host.type == DCR_HOST_MMIO)
94 dcr_unmap_mmio(host.host.mmio, dcr_c);
95 else /* host.type == DCR_HOST_INVALID */
96 WARN_ON(true);
97}
98EXPORT_SYMBOL_GPL(dcr_unmap_generic);
99
100u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n)
101{
102 if (host.type == DCR_HOST_NATIVE)
103 return dcr_read_native(host.host.native, dcr_n);
104 else if (host.type == DCR_HOST_MMIO)
105 return dcr_read_mmio(host.host.mmio, dcr_n);
106 else /* host.type == DCR_HOST_INVALID */
107 WARN_ON(true);
108 return 0;
109}
110EXPORT_SYMBOL_GPL(dcr_read_generic);
111
112void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value)
113{
114 if (host.type == DCR_HOST_NATIVE)
115 dcr_write_native(host.host.native, dcr_n, value);
116 else if (host.type == DCR_HOST_MMIO)
117 dcr_write_mmio(host.host.mmio, dcr_n, value);
118 else /* host.type == DCR_HOST_INVALID */
119 WARN_ON(true);
120}
121EXPORT_SYMBOL_GPL(dcr_write_generic);
122
123#endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */
124
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100125unsigned int dcr_resource_start(struct device_node *np, unsigned int index)
126{
127 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000128 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100129
130 if (dr == NULL || ds & 1 || index >= (ds / 8))
131 return 0;
132
133 return dr[index * 2];
134}
Murali Iyer96b952d2007-08-09 07:46:49 +1000135EXPORT_SYMBOL_GPL(dcr_resource_start);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100136
137unsigned int dcr_resource_len(struct device_node *np, unsigned int index)
138{
139 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000140 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100141
142 if (dr == NULL || ds & 1 || index >= (ds / 8))
143 return 0;
144
145 return dr[index * 2 + 1];
146}
Murali Iyer96b952d2007-08-09 07:46:49 +1000147EXPORT_SYMBOL_GPL(dcr_resource_len);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100148
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000149#ifdef CONFIG_PPC_DCR_MMIO
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100150
151u64 of_translate_dcr_address(struct device_node *dev,
152 unsigned int dcr_n,
153 unsigned int *out_stride)
154{
155 struct device_node *dp;
156 const u32 *p;
157 unsigned int stride;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000158 u64 ret = OF_BAD_ADDR;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100159
160 dp = find_dcr_parent(dev);
161 if (dp == NULL)
162 return OF_BAD_ADDR;
163
164 /* Stride is not properly defined yet, default to 0x10 for Axon */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000165 p = of_get_property(dp, "dcr-mmio-stride", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100166 stride = (p == NULL) ? 0x10 : *p;
167
168 /* XXX FIXME: Which property name is to use of the 2 following ? */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000169 p = of_get_property(dp, "dcr-mmio-range", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100170 if (p == NULL)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000171 p = of_get_property(dp, "dcr-mmio-space", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100172 if (p == NULL)
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000173 goto done;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100174
175 /* Maybe could do some better range checking here */
176 ret = of_translate_address(dp, p);
177 if (ret != OF_BAD_ADDR)
178 ret += (u64)(stride) * (u64)dcr_n;
179 if (out_stride)
180 *out_stride = stride;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000181
182 done:
183 of_node_put(dp);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100184 return ret;
185}
186
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000187dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
188 unsigned int dcr_n,
189 unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100190{
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000191 dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100192 u64 addr;
193
194 pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
195 dev->full_name, dcr_n, dcr_c);
196
197 addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000198 pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
199 (unsigned long long) addr, ret.stride);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100200 if (addr == OF_BAD_ADDR)
201 return ret;
202 pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
203 ret.token = ioremap(addr, dcr_c * ret.stride);
204 if (ret.token == NULL)
205 return ret;
206 pr_debug("mapped at 0x%p -> base is 0x%p\n",
207 ret.token, ret.token - dcr_n * ret.stride);
208 ret.token -= dcr_n * ret.stride;
209 return ret;
210}
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000211EXPORT_SYMBOL_GPL(dcr_map_mmio);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100212
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000213void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100214{
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000215 dcr_host_mmio_t h = host;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100216
217 if (h.token == NULL)
218 return;
Michael Ellermancdbd3862007-10-15 19:34:37 +1000219 h.token += host.base * h.stride;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100220 iounmap(h.token);
221 h.token = NULL;
222}
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000223EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
224
225#endif /* defined(CONFIG_PPC_DCR_MMIO) */
226
227#ifdef CONFIG_PPC_DCR_NATIVE
Valentine Barshak853265e2008-02-05 01:57:55 +1100228DEFINE_SPINLOCK(dcr_ind_lock);
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000229#endif /* defined(CONFIG_PPC_DCR_NATIVE) */
230