blob: fae15d880758cc197ed0bfe8e095daca74f01bd5 [file] [log] [blame]
Pawel Moll3ecbf052012-09-24 14:55:40 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright (C) 2012 ARM Limited
12 */
13
14#define pr_fmt(fmt) "vexpress-config: " fmt
15
16#include <linux/bitops.h>
17#include <linux/completion.h>
18#include <linux/export.h>
19#include <linux/init.h>
20#include <linux/list.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/slab.h>
24#include <linux/string.h>
25#include <linux/vexpress.h>
26
27
28#define VEXPRESS_CONFIG_MAX_BRIDGES 2
29
30struct vexpress_config_bridge {
31 struct device_node *node;
32 struct vexpress_config_bridge_info *info;
33 struct list_head transactions;
34 spinlock_t transactions_lock;
35} vexpress_config_bridges[VEXPRESS_CONFIG_MAX_BRIDGES];
36
37static DECLARE_BITMAP(vexpress_config_bridges_map,
38 ARRAY_SIZE(vexpress_config_bridges));
39static DEFINE_MUTEX(vexpress_config_bridges_mutex);
40
41struct vexpress_config_bridge *vexpress_config_bridge_register(
42 struct device_node *node,
43 struct vexpress_config_bridge_info *info)
44{
45 struct vexpress_config_bridge *bridge;
46 int i;
47
48 pr_debug("Registering bridge '%s'\n", info->name);
49
50 mutex_lock(&vexpress_config_bridges_mutex);
51 i = find_first_zero_bit(vexpress_config_bridges_map,
52 ARRAY_SIZE(vexpress_config_bridges));
53 if (i >= ARRAY_SIZE(vexpress_config_bridges)) {
54 pr_err("Can't register more bridges!\n");
55 mutex_unlock(&vexpress_config_bridges_mutex);
56 return NULL;
57 }
58 __set_bit(i, vexpress_config_bridges_map);
59 bridge = &vexpress_config_bridges[i];
60
61 bridge->node = node;
62 bridge->info = info;
63 INIT_LIST_HEAD(&bridge->transactions);
64 spin_lock_init(&bridge->transactions_lock);
65
66 mutex_unlock(&vexpress_config_bridges_mutex);
67
68 return bridge;
69}
70
71void vexpress_config_bridge_unregister(struct vexpress_config_bridge *bridge)
72{
73 struct vexpress_config_bridge __bridge = *bridge;
74 int i;
75
76 mutex_lock(&vexpress_config_bridges_mutex);
77 for (i = 0; i < ARRAY_SIZE(vexpress_config_bridges); i++)
78 if (&vexpress_config_bridges[i] == bridge)
79 __clear_bit(i, vexpress_config_bridges_map);
80 mutex_unlock(&vexpress_config_bridges_mutex);
81
82 WARN_ON(!list_empty(&__bridge.transactions));
83 while (!list_empty(&__bridge.transactions))
84 cpu_relax();
85}
86
87
88struct vexpress_config_func {
89 struct vexpress_config_bridge *bridge;
90 void *func;
91};
92
93struct vexpress_config_func *__vexpress_config_func_get(struct device *dev,
94 struct device_node *node)
95{
96 struct device_node *bridge_node;
97 struct vexpress_config_func *func;
98 int i;
99
100 if (WARN_ON(dev && node && dev->of_node != node))
101 return NULL;
102 if (dev && !node)
103 node = dev->of_node;
104
105 func = kzalloc(sizeof(*func), GFP_KERNEL);
106 if (!func)
107 return NULL;
108
109 bridge_node = of_node_get(node);
110 while (bridge_node) {
111 const __be32 *prop = of_get_property(bridge_node,
112 "arm,vexpress,config-bridge", NULL);
113
114 if (prop) {
115 bridge_node = of_find_node_by_phandle(
116 be32_to_cpup(prop));
117 break;
118 }
119
120 bridge_node = of_get_next_parent(bridge_node);
121 }
122
123 mutex_lock(&vexpress_config_bridges_mutex);
124 for (i = 0; i < ARRAY_SIZE(vexpress_config_bridges); i++) {
125 struct vexpress_config_bridge *bridge =
126 &vexpress_config_bridges[i];
127
128 if (test_bit(i, vexpress_config_bridges_map) &&
129 bridge->node == bridge_node) {
130 func->bridge = bridge;
131 func->func = bridge->info->func_get(dev, node);
132 break;
133 }
134 }
135 mutex_unlock(&vexpress_config_bridges_mutex);
136
137 if (!func->func) {
138 of_node_put(node);
139 kfree(func);
140 return NULL;
141 }
142
143 return func;
144}
145
146void vexpress_config_func_put(struct vexpress_config_func *func)
147{
148 func->bridge->info->func_put(func->func);
149 of_node_put(func->bridge->node);
150 kfree(func);
151}
152
153
154struct vexpress_config_trans {
155 struct vexpress_config_func *func;
156 int offset;
157 bool write;
158 u32 *data;
159 int status;
160 struct completion completion;
161 struct list_head list;
162};
163
164static void vexpress_config_dump_trans(const char *what,
165 struct vexpress_config_trans *trans)
166{
167 pr_debug("%s %s trans %p func 0x%p offset %d data 0x%x status %d\n",
168 what, trans->write ? "write" : "read", trans,
169 trans->func->func, trans->offset,
170 trans->data ? *trans->data : 0, trans->status);
171}
172
173static int vexpress_config_schedule(struct vexpress_config_trans *trans)
174{
175 int status;
176 struct vexpress_config_bridge *bridge = trans->func->bridge;
177 unsigned long flags;
178
179 init_completion(&trans->completion);
180 trans->status = -EFAULT;
181
182 spin_lock_irqsave(&bridge->transactions_lock, flags);
183
184 vexpress_config_dump_trans("Executing", trans);
185
186 if (list_empty(&bridge->transactions))
187 status = bridge->info->func_exec(trans->func->func,
188 trans->offset, trans->write, trans->data);
189 else
190 status = VEXPRESS_CONFIG_STATUS_WAIT;
191
192 switch (status) {
193 case VEXPRESS_CONFIG_STATUS_DONE:
194 vexpress_config_dump_trans("Finished", trans);
195 trans->status = status;
196 break;
197 case VEXPRESS_CONFIG_STATUS_WAIT:
198 list_add_tail(&trans->list, &bridge->transactions);
199 break;
200 }
201
202 spin_unlock_irqrestore(&bridge->transactions_lock, flags);
203
204 return status;
205}
206
207void vexpress_config_complete(struct vexpress_config_bridge *bridge,
208 int status)
209{
210 struct vexpress_config_trans *trans;
211 unsigned long flags;
212
213 spin_lock_irqsave(&bridge->transactions_lock, flags);
214
215 trans = list_first_entry(&bridge->transactions,
216 struct vexpress_config_trans, list);
217 vexpress_config_dump_trans("Completed", trans);
218
219 trans->status = status;
220 list_del(&trans->list);
221
222 if (!list_empty(&bridge->transactions)) {
223 vexpress_config_dump_trans("Pending", trans);
224
225 bridge->info->func_exec(trans->func->func, trans->offset,
226 trans->write, trans->data);
227 }
228 spin_unlock_irqrestore(&bridge->transactions_lock, flags);
229
230 complete(&trans->completion);
231}
232
233int vexpress_config_wait(struct vexpress_config_trans *trans)
234{
235 wait_for_completion(&trans->completion);
236
237 return trans->status;
238}
239
240
241int vexpress_config_read(struct vexpress_config_func *func, int offset,
242 u32 *data)
243{
244 struct vexpress_config_trans trans = {
245 .func = func,
246 .offset = offset,
247 .write = false,
248 .data = data,
249 .status = 0,
250 };
251 int status = vexpress_config_schedule(&trans);
252
253 if (status == VEXPRESS_CONFIG_STATUS_WAIT)
254 status = vexpress_config_wait(&trans);
255
256 return status;
257}
258EXPORT_SYMBOL(vexpress_config_read);
259
260int vexpress_config_write(struct vexpress_config_func *func, int offset,
261 u32 data)
262{
263 struct vexpress_config_trans trans = {
264 .func = func,
265 .offset = offset,
266 .write = true,
267 .data = &data,
268 .status = 0,
269 };
270 int status = vexpress_config_schedule(&trans);
271
272 if (status == VEXPRESS_CONFIG_STATUS_WAIT)
273 status = vexpress_config_wait(&trans);
274
275 return status;
276}
277EXPORT_SYMBOL(vexpress_config_write);