blob: 22991e1128e3cba208adb73d974df7c79239d55d [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +11002/*
3 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +11005 */
6
7#undef DEBUG
8
9#include <linux/kernel.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040010#include <linux/export.h>
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +110011#include <asm/prom.h>
12#include <asm/dcr.h>
13
Josh Boyer0723abd2008-05-20 07:59:23 -050014#ifdef CONFIG_PPC_DCR_MMIO
Stephen Neuendorfferb786af112008-05-07 04:29:17 +100015static struct device_node *find_dcr_parent(struct device_node *node)
16{
17 struct device_node *par, *tmp;
18 const u32 *p;
19
20 for (par = of_node_get(node); par;) {
21 if (of_get_property(par, "dcr-controller", NULL))
22 break;
23 p = of_get_property(par, "dcr-parent", NULL);
24 tmp = par;
25 if (p == NULL)
26 par = of_get_parent(par);
27 else
28 par = of_find_node_by_phandle(*p);
29 of_node_put(tmp);
30 }
31 return par;
32}
Josh Boyer0723abd2008-05-20 07:59:23 -050033#endif
Stephen Neuendorfferb786af112008-05-07 04:29:17 +100034
35#if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO)
36
37bool dcr_map_ok_generic(dcr_host_t host)
38{
39 if (host.type == DCR_HOST_NATIVE)
40 return dcr_map_ok_native(host.host.native);
41 else if (host.type == DCR_HOST_MMIO)
42 return dcr_map_ok_mmio(host.host.mmio);
43 else
Joe Perchesacdb6682015-03-30 16:46:04 -070044 return false;
Stephen Neuendorfferb786af112008-05-07 04:29:17 +100045}
46EXPORT_SYMBOL_GPL(dcr_map_ok_generic);
47
48dcr_host_t dcr_map_generic(struct device_node *dev,
49 unsigned int dcr_n,
50 unsigned int dcr_c)
51{
52 dcr_host_t host;
53 struct device_node *dp;
54 const char *prop;
55
56 host.type = DCR_HOST_INVALID;
57
58 dp = find_dcr_parent(dev);
59 if (dp == NULL)
60 return host;
61
62 prop = of_get_property(dp, "dcr-access-method", NULL);
63
64 pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop);
65
66 if (!strcmp(prop, "native")) {
67 host.type = DCR_HOST_NATIVE;
68 host.host.native = dcr_map_native(dev, dcr_n, dcr_c);
69 } else if (!strcmp(prop, "mmio")) {
70 host.type = DCR_HOST_MMIO;
71 host.host.mmio = dcr_map_mmio(dev, dcr_n, dcr_c);
72 }
73
74 of_node_put(dp);
75 return host;
76}
77EXPORT_SYMBOL_GPL(dcr_map_generic);
78
79void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c)
80{
81 if (host.type == DCR_HOST_NATIVE)
82 dcr_unmap_native(host.host.native, dcr_c);
83 else if (host.type == DCR_HOST_MMIO)
84 dcr_unmap_mmio(host.host.mmio, dcr_c);
85 else /* host.type == DCR_HOST_INVALID */
86 WARN_ON(true);
87}
88EXPORT_SYMBOL_GPL(dcr_unmap_generic);
89
90u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n)
91{
92 if (host.type == DCR_HOST_NATIVE)
93 return dcr_read_native(host.host.native, dcr_n);
94 else if (host.type == DCR_HOST_MMIO)
95 return dcr_read_mmio(host.host.mmio, dcr_n);
96 else /* host.type == DCR_HOST_INVALID */
97 WARN_ON(true);
98 return 0;
99}
100EXPORT_SYMBOL_GPL(dcr_read_generic);
101
102void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value)
103{
104 if (host.type == DCR_HOST_NATIVE)
105 dcr_write_native(host.host.native, dcr_n, value);
106 else if (host.type == DCR_HOST_MMIO)
107 dcr_write_mmio(host.host.mmio, dcr_n, value);
108 else /* host.type == DCR_HOST_INVALID */
109 WARN_ON(true);
110}
111EXPORT_SYMBOL_GPL(dcr_write_generic);
112
113#endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */
114
Grant Ericksone14d7742008-12-19 08:17:54 +0000115unsigned int dcr_resource_start(const struct device_node *np,
116 unsigned int index)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100117{
118 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000119 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100120
121 if (dr == NULL || ds & 1 || index >= (ds / 8))
122 return 0;
123
124 return dr[index * 2];
125}
Murali Iyer96b952d2007-08-09 07:46:49 +1000126EXPORT_SYMBOL_GPL(dcr_resource_start);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100127
Grant Ericksone14d7742008-12-19 08:17:54 +0000128unsigned int dcr_resource_len(const struct device_node *np, unsigned int index)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100129{
130 unsigned int ds;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000131 const u32 *dr = of_get_property(np, "dcr-reg", &ds);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100132
133 if (dr == NULL || ds & 1 || index >= (ds / 8))
134 return 0;
135
136 return dr[index * 2 + 1];
137}
Murali Iyer96b952d2007-08-09 07:46:49 +1000138EXPORT_SYMBOL_GPL(dcr_resource_len);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100139
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000140#ifdef CONFIG_PPC_DCR_MMIO
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100141
Grant Likelyba524642014-05-23 07:50:50 +0900142static u64 of_translate_dcr_address(struct device_node *dev,
143 unsigned int dcr_n,
144 unsigned int *out_stride)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100145{
146 struct device_node *dp;
147 const u32 *p;
148 unsigned int stride;
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000149 u64 ret = OF_BAD_ADDR;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100150
151 dp = find_dcr_parent(dev);
152 if (dp == NULL)
153 return OF_BAD_ADDR;
154
155 /* Stride is not properly defined yet, default to 0x10 for Axon */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000156 p = of_get_property(dp, "dcr-mmio-stride", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100157 stride = (p == NULL) ? 0x10 : *p;
158
159 /* XXX FIXME: Which property name is to use of the 2 following ? */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000160 p = of_get_property(dp, "dcr-mmio-range", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100161 if (p == NULL)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000162 p = of_get_property(dp, "dcr-mmio-space", NULL);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100163 if (p == NULL)
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000164 goto done;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100165
166 /* Maybe could do some better range checking here */
167 ret = of_translate_address(dp, p);
168 if (ret != OF_BAD_ADDR)
169 ret += (u64)(stride) * (u64)dcr_n;
170 if (out_stride)
171 *out_stride = stride;
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000172
173 done:
174 of_node_put(dp);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100175 return ret;
176}
177
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000178dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
179 unsigned int dcr_n,
180 unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100181{
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000182 dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100183 u64 addr;
184
Rob Herringb7c670d2017-08-21 10:16:47 -0500185 pr_debug("dcr_map(%pOF, 0x%x, 0x%x)\n",
186 dev, dcr_n, dcr_c);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100187
188 addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000189 pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
190 (unsigned long long) addr, ret.stride);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100191 if (addr == OF_BAD_ADDR)
192 return ret;
193 pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
194 ret.token = ioremap(addr, dcr_c * ret.stride);
195 if (ret.token == NULL)
196 return ret;
197 pr_debug("mapped at 0x%p -> base is 0x%p\n",
198 ret.token, ret.token - dcr_n * ret.stride);
199 ret.token -= dcr_n * ret.stride;
200 return ret;
201}
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000202EXPORT_SYMBOL_GPL(dcr_map_mmio);
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100203
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000204void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100205{
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000206 dcr_host_mmio_t h = host;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100207
208 if (h.token == NULL)
209 return;
Michael Ellermancdbd3862007-10-15 19:34:37 +1000210 h.token += host.base * h.stride;
Benjamin Herrenschmidt4c75a6f2006-11-11 17:24:53 +1100211 iounmap(h.token);
212 h.token = NULL;
213}
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000214EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
215
216#endif /* defined(CONFIG_PPC_DCR_MMIO) */
217
218#ifdef CONFIG_PPC_DCR_NATIVE
Valentine Barshak853265e2008-02-05 01:57:55 +1100219DEFINE_SPINLOCK(dcr_ind_lock);
Pranith Kumar22e55fc2014-08-19 23:24:15 -0400220EXPORT_SYMBOL_GPL(dcr_ind_lock);
Stephen Neuendorfferb786af112008-05-07 04:29:17 +1000221#endif /* defined(CONFIG_PPC_DCR_NATIVE) */
222