blob: 43e4a9f39b9b8f9be75a52695c9adec73a981667 [file] [log] [blame]
Steffen Trumtrara39a4932014-04-15 17:06:44 -05001/*
Paul Gortmaker02163192016-06-13 14:03:35 -04002 * Socfpga Reset Controller Driver
3 *
Steffen Trumtrara39a4932014-04-15 17:06:44 -05004 * Copyright 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 *
6 * based on
7 * Allwinner SoCs Reset Controller driver
8 *
9 * Copyright 2013 Maxime Ripard
10 *
11 * Maxime Ripard <maxime.ripard@free-electrons.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 */
18
19#include <linux/err.h>
20#include <linux/io.h>
Paul Gortmaker02163192016-06-13 14:03:35 -040021#include <linux/init.h>
Steffen Trumtrara39a4932014-04-15 17:06:44 -050022#include <linux/of.h>
23#include <linux/platform_device.h>
24#include <linux/reset-controller.h>
25#include <linux/spinlock.h>
26#include <linux/types.h>
27
28#define NR_BANKS 4
Steffen Trumtrara39a4932014-04-15 17:06:44 -050029
30struct socfpga_reset_data {
31 spinlock_t lock;
32 void __iomem *membase;
33 struct reset_controller_dev rcdev;
34};
35
36static int socfpga_reset_assert(struct reset_controller_dev *rcdev,
37 unsigned long id)
38{
39 struct socfpga_reset_data *data = container_of(rcdev,
40 struct socfpga_reset_data,
41 rcdev);
42 int bank = id / BITS_PER_LONG;
43 int offset = id % BITS_PER_LONG;
44 unsigned long flags;
45 u32 reg;
46
47 spin_lock_irqsave(&data->lock, flags);
48
Philipp Zabel6b37d3e2016-07-04 19:47:56 +020049 reg = readl(data->membase + (bank * NR_BANKS));
50 writel(reg | BIT(offset), data->membase + (bank * NR_BANKS));
Steffen Trumtrara39a4932014-04-15 17:06:44 -050051 spin_unlock_irqrestore(&data->lock, flags);
52
53 return 0;
54}
55
56static int socfpga_reset_deassert(struct reset_controller_dev *rcdev,
57 unsigned long id)
58{
59 struct socfpga_reset_data *data = container_of(rcdev,
60 struct socfpga_reset_data,
61 rcdev);
62
63 int bank = id / BITS_PER_LONG;
64 int offset = id % BITS_PER_LONG;
65 unsigned long flags;
66 u32 reg;
67
68 spin_lock_irqsave(&data->lock, flags);
69
Philipp Zabel6b37d3e2016-07-04 19:47:56 +020070 reg = readl(data->membase + (bank * NR_BANKS));
71 writel(reg & ~BIT(offset), data->membase + (bank * NR_BANKS));
Steffen Trumtrara39a4932014-04-15 17:06:44 -050072
73 spin_unlock_irqrestore(&data->lock, flags);
74
75 return 0;
76}
77
Dinh Nguyenf2008902014-11-03 16:33:05 -060078static int socfpga_reset_status(struct reset_controller_dev *rcdev,
79 unsigned long id)
80{
81 struct socfpga_reset_data *data = container_of(rcdev,
82 struct socfpga_reset_data, rcdev);
83 int bank = id / BITS_PER_LONG;
84 int offset = id % BITS_PER_LONG;
85 u32 reg;
86
Philipp Zabel6b37d3e2016-07-04 19:47:56 +020087 reg = readl(data->membase + (bank * NR_BANKS));
Dinh Nguyenf2008902014-11-03 16:33:05 -060088
89 return !(reg & BIT(offset));
90}
91
Philipp Zabel387eb3f2016-01-17 15:13:41 +010092static const struct reset_control_ops socfpga_reset_ops = {
Steffen Trumtrara39a4932014-04-15 17:06:44 -050093 .assert = socfpga_reset_assert,
94 .deassert = socfpga_reset_deassert,
Dinh Nguyenf2008902014-11-03 16:33:05 -060095 .status = socfpga_reset_status,
Steffen Trumtrara39a4932014-04-15 17:06:44 -050096};
97
98static int socfpga_reset_probe(struct platform_device *pdev)
99{
100 struct socfpga_reset_data *data;
101 struct resource *res;
Dinh Nguyen27e44642015-07-31 16:03:10 -0500102 struct device *dev = &pdev->dev;
103 struct device_node *np = dev->of_node;
Philipp Zabel6b37d3e2016-07-04 19:47:56 +0200104 u32 modrst_offset;
Steffen Trumtrara39a4932014-04-15 17:06:44 -0500105
106 /*
107 * The binding was mainlined without the required property.
108 * Do not continue, when we encounter an old DT.
109 */
110 if (!of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) {
111 dev_err(&pdev->dev, "%s missing #reset-cells property\n",
112 pdev->dev.of_node->full_name);
113 return -EINVAL;
114 }
115
116 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
117 if (!data)
118 return -ENOMEM;
119
120 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
121 data->membase = devm_ioremap_resource(&pdev->dev, res);
122 if (IS_ERR(data->membase))
123 return PTR_ERR(data->membase);
124
Philipp Zabel6b37d3e2016-07-04 19:47:56 +0200125 if (of_property_read_u32(np, "altr,modrst-offset", &modrst_offset)) {
Dinh Nguyen27e44642015-07-31 16:03:10 -0500126 dev_warn(dev, "missing altr,modrst-offset property, assuming 0x10!\n");
Philipp Zabel6b37d3e2016-07-04 19:47:56 +0200127 modrst_offset = 0x10;
Dinh Nguyen27e44642015-07-31 16:03:10 -0500128 }
Philipp Zabel6b37d3e2016-07-04 19:47:56 +0200129 data->membase += modrst_offset;
Dinh Nguyen27e44642015-07-31 16:03:10 -0500130
Steffen Trumtrara39a4932014-04-15 17:06:44 -0500131 spin_lock_init(&data->lock);
132
133 data->rcdev.owner = THIS_MODULE;
134 data->rcdev.nr_resets = NR_BANKS * BITS_PER_LONG;
135 data->rcdev.ops = &socfpga_reset_ops;
136 data->rcdev.of_node = pdev->dev.of_node;
Steffen Trumtrara39a4932014-04-15 17:06:44 -0500137
Masahiro Yamadadc22e082016-05-01 19:37:02 +0900138 return devm_reset_controller_register(dev, &data->rcdev);
Steffen Trumtrara39a4932014-04-15 17:06:44 -0500139}
140
141static const struct of_device_id socfpga_reset_dt_ids[] = {
142 { .compatible = "altr,rst-mgr", },
143 { /* sentinel */ },
144};
145
146static struct platform_driver socfpga_reset_driver = {
147 .probe = socfpga_reset_probe,
Steffen Trumtrara39a4932014-04-15 17:06:44 -0500148 .driver = {
149 .name = "socfpga-reset",
Steffen Trumtrara39a4932014-04-15 17:06:44 -0500150 .of_match_table = socfpga_reset_dt_ids,
151 },
152};
Paul Gortmaker02163192016-06-13 14:03:35 -0400153builtin_platform_driver(socfpga_reset_driver);