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