kernel: move CONFIG_NET_IP_TUNNEL to generic
[openwrt.git] / target / linux / ramips / patches-3.9 / 0154-reset-Add-reset-controller-API.patch
1 From d6cfbdfa001891894efe078a49ad82ac8a932dbb Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 20 May 2013 15:42:01 +0200
4 Subject: [PATCH 154/164] reset: Add reset controller API
5
6 backport from v3.10-rc1
7 61fc41317666be400802ac793f47de816ef7bd57
8 6034bb22d8387708075c083385e5d2e1072a4f33
9 4e11f848c65b1c87782cb232a6e3b47a9d4c1f98
10
11 This adds a simple API for devices to request being reset
12 by separate reset controller hardware and implements the
13 reset signal device tree binding.
14
15 Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
16 Reviewed-by: Stephen Warren <swarren@nvidia.com>
17 Reviewed-by: Shawn Guo <shawn.guo@linaro.org>
18 Reviewed-by: Marek Vasut <marex@denx.de>
19 Reviewed-by: Pavel Machek <pavel@ucw.cz>
20 ---
21  Documentation/devicetree/bindings/reset/reset.txt |   75 ++++++
22  drivers/Kconfig                                   |    2 +
23  drivers/Makefile                                  |    3 +
24  drivers/reset/Kconfig                             |   13 +
25  drivers/reset/Makefile                            |    1 +
26  drivers/reset/core.c                              |  297 +++++++++++++++++++++
27  include/linux/reset-controller.h                  |   51 ++++
28  include/linux/reset.h                             |   17 ++
29  8 files changed, 459 insertions(+)
30  create mode 100644 Documentation/devicetree/bindings/reset/reset.txt
31  create mode 100644 drivers/reset/Kconfig
32  create mode 100644 drivers/reset/Makefile
33  create mode 100644 drivers/reset/core.c
34  create mode 100644 include/linux/reset-controller.h
35  create mode 100644 include/linux/reset.h
36
37 --- /dev/null
38 +++ b/Documentation/devicetree/bindings/reset/reset.txt
39 @@ -0,0 +1,75 @@
40 += Reset Signal Device Tree Bindings =
41 +
42 +This binding is intended to represent the hardware reset signals present
43 +internally in most IC (SoC, FPGA, ...) designs. Reset signals for whole
44 +standalone chips are most likely better represented as GPIOs, although there
45 +are likely to be exceptions to this rule.
46 +
47 +Hardware blocks typically receive a reset signal. This signal is generated by
48 +a reset provider (e.g. power management or clock module) and received by a
49 +reset consumer (the module being reset, or a module managing when a sub-
50 +ordinate module is reset). This binding exists to represent the provider and
51 +consumer, and provide a way to couple the two together.
52 +
53 +A reset signal is represented by the phandle of the provider, plus a reset
54 +specifier - a list of DT cells that represents the reset signal within the
55 +provider. The length (number of cells) and semantics of the reset specifier
56 +are dictated by the binding of the reset provider, although common schemes
57 +are described below.
58 +
59 +A word on where to place reset signal consumers in device tree: It is possible
60 +in hardware for a reset signal to affect multiple logically separate HW blocks
61 +at once. In this case, it would be unwise to represent this reset signal in
62 +the DT node of each affected HW block, since if activated, an unrelated block
63 +may be reset. Instead, reset signals should be represented in the DT node
64 +where it makes most sense to control it; this may be a bus node if all
65 +children of the bus are affected by the reset signal, or an individual HW
66 +block node for dedicated reset signals. The intent of this binding is to give
67 +appropriate software access to the reset signals in order to manage the HW,
68 +rather than to slavishly enumerate the reset signal that affects each HW
69 +block.
70 +
71 += Reset providers =
72 +
73 +Required properties:
74 +#reset-cells:  Number of cells in a reset specifier; Typically 0 for nodes
75 +               with a single reset output and 1 for nodes with multiple
76 +               reset outputs.
77 +
78 +For example:
79 +
80 +       rst: reset-controller {
81 +               #reset-cells = <1>;
82 +       };
83 +
84 += Reset consumers =
85 +
86 +Required properties:
87 +resets:                List of phandle and reset specifier pairs, one pair
88 +               for each reset signal that affects the device, or that the
89 +               device manages. Note: if the reset provider specifies '0' for
90 +               #reset-cells, then only the phandle portion of the pair will
91 +               appear.
92 +
93 +Optional properties:
94 +reset-names:   List of reset signal name strings sorted in the same order as
95 +               the resets property. Consumers drivers will use reset-names to
96 +               match reset signal names with reset specifiers.
97 +
98 +For example:
99 +
100 +       device {
101 +               resets = <&rst 20>;
102 +               reset-names = "reset";
103 +       };
104 +
105 +This represents a device with a single reset signal named "reset".
106 +
107 +       bus {
108 +               resets = <&rst 10> <&rst 11> <&rst 12> <&rst 11>;
109 +               reset-names = "i2s1", "i2s2", "dma", "mixer";
110 +       };
111 +
112 +This represents a bus that controls the reset signal of each of four sub-
113 +ordinate devices. Consider for example a bus that fails to operate unless no
114 +child device has reset asserted.
115 --- a/drivers/Kconfig
116 +++ b/drivers/Kconfig
117 @@ -164,4 +164,6 @@ source "drivers/irqchip/Kconfig"
118  
119  source "drivers/ipack/Kconfig"
120  
121 +source "drivers/reset/Kconfig"
122 +
123  endmenu
124 --- a/drivers/Makefile
125 +++ b/drivers/Makefile
126 @@ -38,6 +38,9 @@ obj-$(CONFIG_XEN)             += xen/
127  # regulators early, since some subsystems rely on them to initialize
128  obj-$(CONFIG_REGULATOR)                += regulator/
129  
130 +# reset controllers early, since gpu drivers might rely on them to initialize
131 +obj-$(CONFIG_RESET_CONTROLLER) += reset/
132 +
133  # tty/ comes before char/ so that the VT console is the boot-time
134  # default.
135  obj-y                          += tty/
136 --- /dev/null
137 +++ b/drivers/reset/Kconfig
138 @@ -0,0 +1,13 @@
139 +config ARCH_HAS_RESET_CONTROLLER
140 +       bool
141 +
142 +menuconfig RESET_CONTROLLER
143 +       bool "Reset Controller Support"
144 +       default y if ARCH_HAS_RESET_CONTROLLER
145 +       help
146 +         Generic Reset Controller support.
147 +
148 +         This framework is designed to abstract reset handling of devices
149 +         via GPIOs or SoC-internal reset controller modules.
150 +
151 +         If unsure, say no.
152 --- /dev/null
153 +++ b/drivers/reset/Makefile
154 @@ -0,0 +1 @@
155 +obj-$(CONFIG_RESET_CONTROLLER) += core.o
156 --- /dev/null
157 +++ b/drivers/reset/core.c
158 @@ -0,0 +1,297 @@
159 +/*
160 + * Reset Controller framework
161 + *
162 + * Copyright 2013 Philipp Zabel, Pengutronix
163 + *
164 + * This program is free software; you can redistribute it and/or modify
165 + * it under the terms of the GNU General Public License as published by
166 + * the Free Software Foundation; either version 2 of the License, or
167 + * (at your option) any later version.
168 + */
169 +#include <linux/device.h>
170 +#include <linux/err.h>
171 +#include <linux/export.h>
172 +#include <linux/kernel.h>
173 +#include <linux/module.h>
174 +#include <linux/of.h>
175 +#include <linux/reset.h>
176 +#include <linux/reset-controller.h>
177 +#include <linux/slab.h>
178 +
179 +static DEFINE_MUTEX(reset_controller_list_mutex);
180 +static LIST_HEAD(reset_controller_list);
181 +
182 +/**
183 + * struct reset_control - a reset control
184 + * @rcdev: a pointer to the reset controller device
185 + *         this reset control belongs to
186 + * @id: ID of the reset controller in the reset
187 + *      controller device
188 + */
189 +struct reset_control {
190 +       struct reset_controller_dev *rcdev;
191 +       struct device *dev;
192 +       unsigned int id;
193 +};
194 +
195 +/**
196 + * of_reset_simple_xlate - translate reset_spec to the reset line number
197 + * @rcdev: a pointer to the reset controller device
198 + * @reset_spec: reset line specifier as found in the device tree
199 + * @flags: a flags pointer to fill in (optional)
200 + *
201 + * This simple translation function should be used for reset controllers
202 + * with 1:1 mapping, where reset lines can be indexed by number without gaps.
203 + */
204 +int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
205 +                         const struct of_phandle_args *reset_spec)
206 +{
207 +       if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
208 +               return -EINVAL;
209 +
210 +       if (reset_spec->args[0] >= rcdev->nr_resets)
211 +               return -EINVAL;
212 +
213 +       return reset_spec->args[0];
214 +}
215 +EXPORT_SYMBOL_GPL(of_reset_simple_xlate);
216 +
217 +/**
218 + * reset_controller_register - register a reset controller device
219 + * @rcdev: a pointer to the initialized reset controller device
220 + */
221 +int reset_controller_register(struct reset_controller_dev *rcdev)
222 +{
223 +       if (!rcdev->of_xlate) {
224 +               rcdev->of_reset_n_cells = 1;
225 +               rcdev->of_xlate = of_reset_simple_xlate;
226 +       }
227 +
228 +       mutex_lock(&reset_controller_list_mutex);
229 +       list_add(&rcdev->list, &reset_controller_list);
230 +       mutex_unlock(&reset_controller_list_mutex);
231 +
232 +       return 0;
233 +}
234 +EXPORT_SYMBOL_GPL(reset_controller_register);
235 +
236 +/**
237 + * reset_controller_unregister - unregister a reset controller device
238 + * @rcdev: a pointer to the reset controller device
239 + */
240 +void reset_controller_unregister(struct reset_controller_dev *rcdev)
241 +{
242 +       mutex_lock(&reset_controller_list_mutex);
243 +       list_del(&rcdev->list);
244 +       mutex_unlock(&reset_controller_list_mutex);
245 +}
246 +EXPORT_SYMBOL_GPL(reset_controller_unregister);
247 +
248 +/**
249 + * reset_control_reset - reset the controlled device
250 + * @rstc: reset controller
251 + */
252 +int reset_control_reset(struct reset_control *rstc)
253 +{
254 +       if (rstc->rcdev->ops->reset)
255 +               return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
256 +
257 +       return -ENOSYS;
258 +}
259 +EXPORT_SYMBOL_GPL(reset_control_reset);
260 +
261 +/**
262 + * reset_control_assert - asserts the reset line
263 + * @rstc: reset controller
264 + */
265 +int reset_control_assert(struct reset_control *rstc)
266 +{
267 +       if (rstc->rcdev->ops->assert)
268 +               return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
269 +
270 +       return -ENOSYS;
271 +}
272 +EXPORT_SYMBOL_GPL(reset_control_assert);
273 +
274 +/**
275 + * reset_control_deassert - deasserts the reset line
276 + * @rstc: reset controller
277 + */
278 +int reset_control_deassert(struct reset_control *rstc)
279 +{
280 +       if (rstc->rcdev->ops->deassert)
281 +               return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
282 +
283 +       return -ENOSYS;
284 +}
285 +EXPORT_SYMBOL_GPL(reset_control_deassert);
286 +
287 +/**
288 + * reset_control_get - Lookup and obtain a reference to a reset controller.
289 + * @dev: device to be reset by the controller
290 + * @id: reset line name
291 + *
292 + * Returns a struct reset_control or IS_ERR() condition containing errno.
293 + *
294 + * Use of id names is optional.
295 + */
296 +struct reset_control *reset_control_get(struct device *dev, const char *id)
297 +{
298 +       struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
299 +       struct reset_controller_dev *r, *rcdev;
300 +       struct of_phandle_args args;
301 +       int index = 0;
302 +       int rstc_id;
303 +       int ret;
304 +
305 +       if (!dev)
306 +               return ERR_PTR(-EINVAL);
307 +
308 +       if (id)
309 +               index = of_property_match_string(dev->of_node,
310 +                                                "reset-names", id);
311 +       ret = of_parse_phandle_with_args(dev->of_node, "resets", "#reset-cells",
312 +                                        index, &args);
313 +       if (ret)
314 +               return ERR_PTR(ret);
315 +
316 +       mutex_lock(&reset_controller_list_mutex);
317 +       rcdev = NULL;
318 +       list_for_each_entry(r, &reset_controller_list, list) {
319 +               if (args.np == r->of_node) {
320 +                       rcdev = r;
321 +                       break;
322 +               }
323 +       }
324 +       of_node_put(args.np);
325 +
326 +       if (!rcdev) {
327 +               mutex_unlock(&reset_controller_list_mutex);
328 +               return ERR_PTR(-ENODEV);
329 +       }
330 +
331 +       rstc_id = rcdev->of_xlate(rcdev, &args);
332 +       if (rstc_id < 0) {
333 +               mutex_unlock(&reset_controller_list_mutex);
334 +               return ERR_PTR(rstc_id);
335 +       }
336 +
337 +       try_module_get(rcdev->owner);
338 +       mutex_unlock(&reset_controller_list_mutex);
339 +
340 +       rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
341 +       if (!rstc) {
342 +               module_put(rcdev->owner);
343 +               return ERR_PTR(-ENOMEM);
344 +       }
345 +
346 +       rstc->dev = dev;
347 +       rstc->rcdev = rcdev;
348 +       rstc->id = rstc_id;
349 +
350 +       return rstc;
351 +}
352 +EXPORT_SYMBOL_GPL(reset_control_get);
353 +
354 +/**
355 + * reset_control_put - free the reset controller
356 + * @rstc: reset controller
357 + */
358 +
359 +void reset_control_put(struct reset_control *rstc)
360 +{
361 +       if (IS_ERR(rstc))
362 +               return;
363 +
364 +       module_put(rstc->rcdev->owner);
365 +       kfree(rstc);
366 +}
367 +EXPORT_SYMBOL_GPL(reset_control_put);
368 +
369 +static void devm_reset_control_release(struct device *dev, void *res)
370 +{
371 +       reset_control_put(*(struct reset_control **)res);
372 +}
373 +
374 +/**
375 + * devm_reset_control_get - resource managed reset_control_get()
376 + * @dev: device to be reset by the controller
377 + * @id: reset line name
378 + *
379 + * Managed reset_control_get(). For reset controllers returned from this
380 + * function, reset_control_put() is called automatically on driver detach.
381 + * See reset_control_get() for more information.
382 + */
383 +struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
384 +{
385 +       struct reset_control **ptr, *rstc;
386 +
387 +       ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
388 +                          GFP_KERNEL);
389 +       if (!ptr)
390 +               return ERR_PTR(-ENOMEM);
391 +
392 +       rstc = reset_control_get(dev, id);
393 +       if (!IS_ERR(rstc)) {
394 +               *ptr = rstc;
395 +               devres_add(dev, ptr);
396 +       } else {
397 +               devres_free(ptr);
398 +       }
399 +
400 +       return rstc;
401 +}
402 +EXPORT_SYMBOL_GPL(devm_reset_control_get);
403 +
404 +static int devm_reset_control_match(struct device *dev, void *res, void *data)
405 +{
406 +       struct reset_control **rstc = res;
407 +       if (WARN_ON(!rstc || !*rstc))
408 +               return 0;
409 +       return *rstc == data;
410 +}
411 +
412 +/**
413 + * devm_reset_control_put - resource managed reset_control_put()
414 + * @rstc: reset controller to free
415 + *
416 + * Deallocate a reset control allocated withd devm_reset_control_get().
417 + * This function will not need to be called normally, as devres will take
418 + * care of freeing the resource.
419 + */
420 +void devm_reset_control_put(struct reset_control *rstc)
421 +{
422 +       int ret;
423 +
424 +       ret = devres_release(rstc->dev, devm_reset_control_release,
425 +                            devm_reset_control_match, rstc);
426 +       if (ret)
427 +               WARN_ON(ret);
428 +}
429 +EXPORT_SYMBOL_GPL(devm_reset_control_put);
430 +
431 +/**
432 + * device_reset - find reset controller associated with the device
433 + *                and perform reset
434 + * @dev: device to be reset by the controller
435 + *
436 + * Convenience wrapper for reset_control_get() and reset_control_reset().
437 + * This is useful for the common case of devices with single, dedicated reset
438 + * lines.
439 + */
440 +int device_reset(struct device *dev)
441 +{
442 +       struct reset_control *rstc;
443 +       int ret;
444 +
445 +       rstc = reset_control_get(dev, NULL);
446 +       if (IS_ERR(rstc))
447 +               return PTR_ERR(rstc);
448 +
449 +       ret = reset_control_reset(rstc);
450 +
451 +       reset_control_put(rstc);
452 +
453 +       return ret;
454 +}
455 +EXPORT_SYMBOL_GPL(device_reset);
456 --- /dev/null
457 +++ b/include/linux/reset-controller.h
458 @@ -0,0 +1,51 @@
459 +#ifndef _LINUX_RESET_CONTROLLER_H_
460 +#define _LINUX_RESET_CONTROLLER_H_
461 +
462 +#include <linux/list.h>
463 +
464 +struct reset_controller_dev;
465 +
466 +/**
467 + * struct reset_control_ops
468 + *
469 + * @reset: for self-deasserting resets, does all necessary
470 + *         things to reset the device
471 + * @assert: manually assert the reset line, if supported
472 + * @deassert: manually deassert the reset line, if supported
473 + */
474 +struct reset_control_ops {
475 +       int (*reset)(struct reset_controller_dev *rcdev, unsigned long id);
476 +       int (*assert)(struct reset_controller_dev *rcdev, unsigned long id);
477 +       int (*deassert)(struct reset_controller_dev *rcdev, unsigned long id);
478 +};
479 +
480 +struct module;
481 +struct device_node;
482 +
483 +/**
484 + * struct reset_controller_dev - reset controller entity that might
485 + *                               provide multiple reset controls
486 + * @ops: a pointer to device specific struct reset_control_ops
487 + * @owner: kernel module of the reset controller driver
488 + * @list: internal list of reset controller devices
489 + * @of_node: corresponding device tree node as phandle target
490 + * @of_reset_n_cells: number of cells in reset line specifiers
491 + * @of_xlate: translation function to translate from specifier as found in the
492 + *            device tree to id as given to the reset control ops
493 + * @nr_resets: number of reset controls in this reset controller device
494 + */
495 +struct reset_controller_dev {
496 +       struct reset_control_ops *ops;
497 +       struct module *owner;
498 +       struct list_head list;
499 +       struct device_node *of_node;
500 +       int of_reset_n_cells;
501 +       int (*of_xlate)(struct reset_controller_dev *rcdev,
502 +                       const struct of_phandle_args *reset_spec);
503 +       unsigned int nr_resets;
504 +};
505 +
506 +int reset_controller_register(struct reset_controller_dev *rcdev);
507 +void reset_controller_unregister(struct reset_controller_dev *rcdev);
508 +
509 +#endif
510 --- /dev/null
511 +++ b/include/linux/reset.h
512 @@ -0,0 +1,17 @@
513 +#ifndef _LINUX_RESET_H_
514 +#define _LINUX_RESET_H_
515 +
516 +struct device;
517 +struct reset_control;
518 +
519 +int reset_control_reset(struct reset_control *rstc);
520 +int reset_control_assert(struct reset_control *rstc);
521 +int reset_control_deassert(struct reset_control *rstc);
522 +
523 +struct reset_control *reset_control_get(struct device *dev, const char *id);
524 +void reset_control_put(struct reset_control *rstc);
525 +struct reset_control *devm_reset_control_get(struct device *dev, const char *id);
526 +
527 +int device_reset(struct device *dev);
528 +
529 +#endif