blob: a8ba9983dd5a8ab0ccc8e04bd82816796e02282b [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
Josh Boyer0723abd2008-05-20 07:59:23 -050026#ifdef CONFIG_PPC_DCR_MMIO
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100027static struct device_node *find_dcr_parent(struct device_node *node)
28{
29 struct device_node *par, *tmp;
30 const u32 *p;
31
32 for (par = of_node_get(node); par;) {
33 if (of_get_property(par, "dcr-controller", NULL))
34 break;
35 p = of_get_property(par, "dcr-parent", NULL);
36 tmp = par;
37 if (p == NULL)
38 par = of_get_parent(par);
39 else
40 par = of_find_node_by_phandle(*p);
41 of_node_put(tmp);
42 }
43 return par;
44}
Josh Boyer0723abd2008-05-20 07:59:23 -050045#endif
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100046
47#if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO)
48
49bool dcr_map_ok_generic(dcr_host_t host)
50{
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);
55 else
56 return 0;
57}
58EXPORT_SYMBOL_GPL(dcr_map_ok_generic);
59
60dcr_host_t dcr_map_generic(struct device_node *dev,
61 unsigned int dcr_n,
62 unsigned int dcr_c)
63{
64 dcr_host_t host;
65 struct device_node *dp;
66 const char *prop;
67
68 host.type = DCR_HOST_INVALID;
69
70 dp = find_dcr_parent(dev);
71 if (dp == NULL)
72 return host;
73
74 prop = of_get_property(dp, "dcr-access-method", NULL);
75
76 pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop);
77
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);
84 }
85
86 of_node_put(dp);
87 return host;
88}
89EXPORT_SYMBOL_GPL(dcr_map_generic);
90
91void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c)
92{
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 */
98 WARN_ON(true);
99}
100EXPORT_SYMBOL_GPL(dcr_unmap_generic);
101
102u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n)
103{
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 */
109 WARN_ON(true);
110 return 0;
111}
112EXPORT_SYMBOL_GPL(dcr_read_generic);
113
114void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value)
115{
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 */
121 WARN_ON(true);
122}
123EXPORT_SYMBOL_GPL(dcr_write_generic);
124
125#endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */
126
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100127unsigned int dcr_resource_start(struct device_node *np, unsigned int index)
128{
129 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000130 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100131
132 if (dr == NULL || ds & 1 || index >= (ds / 8))
133 return 0;
134
135 return dr[index * 2];
136}
Murali Iyer96b952d2007-08-09 07:46:49 +1000137EXPORT_SYMBOL_GPL(dcr_resource_start);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100138
139unsigned int dcr_resource_len(struct device_node *np, unsigned int index)
140{
141 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000142 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100143
144 if (dr == NULL || ds & 1 || index >= (ds / 8))
145 return 0;
146
147 return dr[index * 2 + 1];
148}
Murali Iyer96b952d2007-08-09 07:46:49 +1000149EXPORT_SYMBOL_GPL(dcr_resource_len);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100150
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000151#ifdef CONFIG_PPC_DCR_MMIO
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100152
153u64 of_translate_dcr_address(struct device_node *dev,
154 unsigned int dcr_n,
155 unsigned int *out_stride)
156{
157 struct device_node *dp;
158 const u32 *p;
159 unsigned int stride;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000160 u64 ret = OF_BAD_ADDR;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100161
162 dp = find_dcr_parent(dev);
163 if (dp == NULL)
164 return OF_BAD_ADDR;
165
166 /* Stride is not properly defined yet, default to 0x10 for Axon */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000167 p = of_get_property(dp, "dcr-mmio-stride", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100168 stride = (p == NULL) ? 0x10 : *p;
169
170 /* XXX FIXME: Which property name is to use of the 2 following ? */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000171 p = of_get_property(dp, "dcr-mmio-range", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100172 if (p == NULL)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000173 p = of_get_property(dp, "dcr-mmio-space", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100174 if (p == NULL)
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000175 goto done;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100176
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;
181 if (out_stride)
182 *out_stride = stride;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000183
184 done:
185 of_node_put(dp);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100186 return ret;
187}
188
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000189dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
190 unsigned int dcr_n,
191 unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100192{
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000193 dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100194 u64 addr;
195
196 pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
197 dev->full_name, dcr_n, dcr_c);
198
199 addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000200 pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
201 (unsigned long long) addr, ret.stride);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100202 if (addr == OF_BAD_ADDR)
203 return ret;
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)
207 return ret;
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;
211 return ret;
212}
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000213EXPORT_SYMBOL_GPL(dcr_map_mmio);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100214
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000215void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100216{
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000217 dcr_host_mmio_t h = host;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100218
219 if (h.token == NULL)
220 return;
Michael Ellermancdbd3862007-10-15 19:34:37 +1000221 h.token += host.base * h.stride;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100222 iounmap(h.token);
223 h.token = NULL;
224}
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000225EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
226
227#endif /* defined(CONFIG_PPC_DCR_MMIO) */
228
229#ifdef CONFIG_PPC_DCR_NATIVE
Valentine Barshak853265e2008-02-05 01:57:55 +1100230DEFINE_SPINLOCK(dcr_ind_lock);
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000231#endif /* defined(CONFIG_PPC_DCR_NATIVE) */
232