blob: 121e26fffd50946e8df457b51c3c088b6c6fa528 [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>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040023#include <linux/export.h>
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110024#include <asm/prom.h>
25#include <asm/dcr.h>
26
Josh Boyer0723abd2008-05-20 07:59:23 -050027#ifdef CONFIG_PPC_DCR_MMIO
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100028static struct device_node *find_dcr_parent(struct device_node *node)
29{
30 struct device_node *par, *tmp;
31 const u32 *p;
32
33 for (par = of_node_get(node); par;) {
34 if (of_get_property(par, "dcr-controller", NULL))
35 break;
36 p = of_get_property(par, "dcr-parent", NULL);
37 tmp = par;
38 if (p == NULL)
39 par = of_get_parent(par);
40 else
41 par = of_find_node_by_phandle(*p);
42 of_node_put(tmp);
43 }
44 return par;
45}
Josh Boyer0723abd2008-05-20 07:59:23 -050046#endif
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100047
48#if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO)
49
50bool dcr_map_ok_generic(dcr_host_t host)
51{
52 if (host.type == DCR_HOST_NATIVE)
53 return dcr_map_ok_native(host.host.native);
54 else if (host.type == DCR_HOST_MMIO)
55 return dcr_map_ok_mmio(host.host.mmio);
56 else
Joe Perchesacdb6682015-03-30 16:46:04 -070057 return false;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +100058}
59EXPORT_SYMBOL_GPL(dcr_map_ok_generic);
60
61dcr_host_t dcr_map_generic(struct device_node *dev,
62 unsigned int dcr_n,
63 unsigned int dcr_c)
64{
65 dcr_host_t host;
66 struct device_node *dp;
67 const char *prop;
68
69 host.type = DCR_HOST_INVALID;
70
71 dp = find_dcr_parent(dev);
72 if (dp == NULL)
73 return host;
74
75 prop = of_get_property(dp, "dcr-access-method", NULL);
76
77 pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop);
78
79 if (!strcmp(prop, "native")) {
80 host.type = DCR_HOST_NATIVE;
81 host.host.native = dcr_map_native(dev, dcr_n, dcr_c);
82 } else if (!strcmp(prop, "mmio")) {
83 host.type = DCR_HOST_MMIO;
84 host.host.mmio = dcr_map_mmio(dev, dcr_n, dcr_c);
85 }
86
87 of_node_put(dp);
88 return host;
89}
90EXPORT_SYMBOL_GPL(dcr_map_generic);
91
92void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c)
93{
94 if (host.type == DCR_HOST_NATIVE)
95 dcr_unmap_native(host.host.native, dcr_c);
96 else if (host.type == DCR_HOST_MMIO)
97 dcr_unmap_mmio(host.host.mmio, dcr_c);
98 else /* host.type == DCR_HOST_INVALID */
99 WARN_ON(true);
100}
101EXPORT_SYMBOL_GPL(dcr_unmap_generic);
102
103u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n)
104{
105 if (host.type == DCR_HOST_NATIVE)
106 return dcr_read_native(host.host.native, dcr_n);
107 else if (host.type == DCR_HOST_MMIO)
108 return dcr_read_mmio(host.host.mmio, dcr_n);
109 else /* host.type == DCR_HOST_INVALID */
110 WARN_ON(true);
111 return 0;
112}
113EXPORT_SYMBOL_GPL(dcr_read_generic);
114
115void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value)
116{
117 if (host.type == DCR_HOST_NATIVE)
118 dcr_write_native(host.host.native, dcr_n, value);
119 else if (host.type == DCR_HOST_MMIO)
120 dcr_write_mmio(host.host.mmio, dcr_n, value);
121 else /* host.type == DCR_HOST_INVALID */
122 WARN_ON(true);
123}
124EXPORT_SYMBOL_GPL(dcr_write_generic);
125
126#endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */
127
Grant Ericksone14d7742008-12-19 08:17:54 +0000128unsigned int dcr_resource_start(const struct device_node *np,
129 unsigned int index)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100130{
131 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000132 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100133
134 if (dr == NULL || ds & 1 || index >= (ds / 8))
135 return 0;
136
137 return dr[index * 2];
138}
Murali Iyer96b952d2007-08-09 07:46:49 +1000139EXPORT_SYMBOL_GPL(dcr_resource_start);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100140
Grant Ericksone14d7742008-12-19 08:17:54 +0000141unsigned int dcr_resource_len(const struct device_node *np, unsigned int index)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100142{
143 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000144 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100145
146 if (dr == NULL || ds & 1 || index >= (ds / 8))
147 return 0;
148
149 return dr[index * 2 + 1];
150}
Murali Iyer96b952d2007-08-09 07:46:49 +1000151EXPORT_SYMBOL_GPL(dcr_resource_len);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100152
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000153#ifdef CONFIG_PPC_DCR_MMIO
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100154
Grant Likelyba524642014-05-23 07:50:50 +0900155static u64 of_translate_dcr_address(struct device_node *dev,
156 unsigned int dcr_n,
157 unsigned int *out_stride)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100158{
159 struct device_node *dp;
160 const u32 *p;
161 unsigned int stride;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000162 u64 ret = OF_BAD_ADDR;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100163
164 dp = find_dcr_parent(dev);
165 if (dp == NULL)
166 return OF_BAD_ADDR;
167
168 /* Stride is not properly defined yet, default to 0x10 for Axon */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000169 p = of_get_property(dp, "dcr-mmio-stride", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100170 stride = (p == NULL) ? 0x10 : *p;
171
172 /* XXX FIXME: Which property name is to use of the 2 following ? */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000173 p = of_get_property(dp, "dcr-mmio-range", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100174 if (p == NULL)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000175 p = of_get_property(dp, "dcr-mmio-space", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100176 if (p == NULL)
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000177 goto done;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100178
179 /* Maybe could do some better range checking here */
180 ret = of_translate_address(dp, p);
181 if (ret != OF_BAD_ADDR)
182 ret += (u64)(stride) * (u64)dcr_n;
183 if (out_stride)
184 *out_stride = stride;
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000185
186 done:
187 of_node_put(dp);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100188 return ret;
189}
190
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000191dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
192 unsigned int dcr_n,
193 unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100194{
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000195 dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100196 u64 addr;
197
198 pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
199 dev->full_name, dcr_n, dcr_c);
200
201 addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000202 pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
203 (unsigned long long) addr, ret.stride);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100204 if (addr == OF_BAD_ADDR)
205 return ret;
206 pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
207 ret.token = ioremap(addr, dcr_c * ret.stride);
208 if (ret.token == NULL)
209 return ret;
210 pr_debug("mapped at 0x%p -> base is 0x%p\n",
211 ret.token, ret.token - dcr_n * ret.stride);
212 ret.token -= dcr_n * ret.stride;
213 return ret;
214}
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000215EXPORT_SYMBOL_GPL(dcr_map_mmio);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100216
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000217void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100218{
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000219 dcr_host_mmio_t h = host;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100220
221 if (h.token == NULL)
222 return;
Michael Ellermancdbd3862007-10-15 19:34:37 +1000223 h.token += host.base * h.stride;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100224 iounmap(h.token);
225 h.token = NULL;
226}
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000227EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
228
229#endif /* defined(CONFIG_PPC_DCR_MMIO) */
230
231#ifdef CONFIG_PPC_DCR_NATIVE
Valentine Barshak853265e2008-02-05 01:57:55 +1100232DEFINE_SPINLOCK(dcr_ind_lock);
Pranith Kumar22e55fc2014-08-19 23:24:15 -0400233EXPORT_SYMBOL_GPL(dcr_ind_lock);
Stephen Neuendorfferb786af12008-05-07 04:29:17 +1000234#endif /* defined(CONFIG_PPC_DCR_NATIVE) */
235