uapi/if_ether.h: move __UAPI_DEF_ETHHDR libc define
[linux/fpc-iii.git] / drivers / reset / reset-sunxi.c
blob2c7dd1fd08df626c28e78744441804dfef6ce123
1 /*
2 * Allwinner SoCs Reset Controller driver
4 * Copyright 2013 Maxime Ripard
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/init.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/reset-controller.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/types.h>
25 struct sunxi_reset_data {
26 spinlock_t lock;
27 void __iomem *membase;
28 struct reset_controller_dev rcdev;
31 static int sunxi_reset_assert(struct reset_controller_dev *rcdev,
32 unsigned long id)
34 struct sunxi_reset_data *data = container_of(rcdev,
35 struct sunxi_reset_data,
36 rcdev);
37 int reg_width = sizeof(u32);
38 int bank = id / (reg_width * BITS_PER_BYTE);
39 int offset = id % (reg_width * BITS_PER_BYTE);
40 unsigned long flags;
41 u32 reg;
43 spin_lock_irqsave(&data->lock, flags);
45 reg = readl(data->membase + (bank * reg_width));
46 writel(reg & ~BIT(offset), data->membase + (bank * reg_width));
48 spin_unlock_irqrestore(&data->lock, flags);
50 return 0;
53 static int sunxi_reset_deassert(struct reset_controller_dev *rcdev,
54 unsigned long id)
56 struct sunxi_reset_data *data = container_of(rcdev,
57 struct sunxi_reset_data,
58 rcdev);
59 int reg_width = sizeof(u32);
60 int bank = id / (reg_width * BITS_PER_BYTE);
61 int offset = id % (reg_width * BITS_PER_BYTE);
62 unsigned long flags;
63 u32 reg;
65 spin_lock_irqsave(&data->lock, flags);
67 reg = readl(data->membase + (bank * reg_width));
68 writel(reg | BIT(offset), data->membase + (bank * reg_width));
70 spin_unlock_irqrestore(&data->lock, flags);
72 return 0;
75 static const struct reset_control_ops sunxi_reset_ops = {
76 .assert = sunxi_reset_assert,
77 .deassert = sunxi_reset_deassert,
80 static int sunxi_reset_init(struct device_node *np)
82 struct sunxi_reset_data *data;
83 struct resource res;
84 resource_size_t size;
85 int ret;
87 data = kzalloc(sizeof(*data), GFP_KERNEL);
88 if (!data)
89 return -ENOMEM;
91 ret = of_address_to_resource(np, 0, &res);
92 if (ret)
93 goto err_alloc;
95 size = resource_size(&res);
96 if (!request_mem_region(res.start, size, np->name)) {
97 ret = -EBUSY;
98 goto err_alloc;
101 data->membase = ioremap(res.start, size);
102 if (!data->membase) {
103 ret = -ENOMEM;
104 goto err_alloc;
107 spin_lock_init(&data->lock);
109 data->rcdev.owner = THIS_MODULE;
110 data->rcdev.nr_resets = size * 8;
111 data->rcdev.ops = &sunxi_reset_ops;
112 data->rcdev.of_node = np;
114 return reset_controller_register(&data->rcdev);
116 err_alloc:
117 kfree(data);
118 return ret;
122 * These are the reset controller we need to initialize early on in
123 * our system, before we can even think of using a regular device
124 * driver for it.
126 static const struct of_device_id sunxi_early_reset_dt_ids[] __initconst = {
127 { .compatible = "allwinner,sun6i-a31-ahb1-reset", },
128 { /* sentinel */ },
131 void __init sun6i_reset_init(void)
133 struct device_node *np;
135 for_each_matching_node(np, sunxi_early_reset_dt_ids)
136 sunxi_reset_init(np);
140 * And these are the controllers we can register through the regular
141 * device model.
143 static const struct of_device_id sunxi_reset_dt_ids[] = {
144 { .compatible = "allwinner,sun6i-a31-clock-reset", },
145 { /* sentinel */ },
148 static int sunxi_reset_probe(struct platform_device *pdev)
150 struct sunxi_reset_data *data;
151 struct resource *res;
153 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
157 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
158 data->membase = devm_ioremap_resource(&pdev->dev, res);
159 if (IS_ERR(data->membase))
160 return PTR_ERR(data->membase);
162 spin_lock_init(&data->lock);
164 data->rcdev.owner = THIS_MODULE;
165 data->rcdev.nr_resets = resource_size(res) * 8;
166 data->rcdev.ops = &sunxi_reset_ops;
167 data->rcdev.of_node = pdev->dev.of_node;
169 return devm_reset_controller_register(&pdev->dev, &data->rcdev);
172 static struct platform_driver sunxi_reset_driver = {
173 .probe = sunxi_reset_probe,
174 .driver = {
175 .name = "sunxi-reset",
176 .of_match_table = sunxi_reset_dt_ids,
179 builtin_platform_driver(sunxi_reset_driver);