Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Simple Reset Controller Driver |
| 4 | * |
| 5 | * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de> |
| 6 | * |
| 7 | * Based on Allwinner SoCs Reset Controller driver |
| 8 | * |
| 9 | * Copyright 2013 Maxime Ripard |
| 10 | * |
| 11 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/reset-controller.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | |
| 23 | #include "reset-simple.h" |
| 24 | |
| 25 | static inline struct reset_simple_data * |
| 26 | to_reset_simple_data(struct reset_controller_dev *rcdev) |
| 27 | { |
| 28 | return container_of(rcdev, struct reset_simple_data, rcdev); |
| 29 | } |
| 30 | |
| 31 | static int reset_simple_update(struct reset_controller_dev *rcdev, |
| 32 | unsigned long id, bool assert) |
| 33 | { |
| 34 | struct reset_simple_data *data = to_reset_simple_data(rcdev); |
| 35 | int reg_width = sizeof(u32); |
| 36 | int bank = id / (reg_width * BITS_PER_BYTE); |
| 37 | int offset = id % (reg_width * BITS_PER_BYTE); |
| 38 | unsigned long flags; |
| 39 | u32 reg; |
| 40 | |
| 41 | spin_lock_irqsave(&data->lock, flags); |
| 42 | |
| 43 | reg = readl(data->membase + (bank * reg_width)); |
| 44 | if (assert ^ data->active_low) |
| 45 | reg |= BIT(offset); |
| 46 | else |
| 47 | reg &= ~BIT(offset); |
| 48 | writel(reg, data->membase + (bank * reg_width)); |
| 49 | |
| 50 | spin_unlock_irqrestore(&data->lock, flags); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int reset_simple_assert(struct reset_controller_dev *rcdev, |
| 56 | unsigned long id) |
| 57 | { |
| 58 | return reset_simple_update(rcdev, id, true); |
| 59 | } |
| 60 | |
| 61 | static int reset_simple_deassert(struct reset_controller_dev *rcdev, |
| 62 | unsigned long id) |
| 63 | { |
| 64 | return reset_simple_update(rcdev, id, false); |
| 65 | } |
| 66 | |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 67 | static int reset_simple_status(struct reset_controller_dev *rcdev, |
| 68 | unsigned long id) |
| 69 | { |
| 70 | struct reset_simple_data *data = to_reset_simple_data(rcdev); |
| 71 | int reg_width = sizeof(u32); |
| 72 | int bank = id / (reg_width * BITS_PER_BYTE); |
| 73 | int offset = id % (reg_width * BITS_PER_BYTE); |
| 74 | u32 reg; |
| 75 | |
| 76 | reg = readl(data->membase + (bank * reg_width)); |
| 77 | |
| 78 | return !(reg & BIT(offset)) ^ !data->status_active_low; |
| 79 | } |
| 80 | |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 81 | const struct reset_control_ops reset_simple_ops = { |
| 82 | .assert = reset_simple_assert, |
| 83 | .deassert = reset_simple_deassert, |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 84 | .status = reset_simple_status, |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 85 | }; |
Kunihiko Hayashi | 9ad39ab | 2018-07-04 19:13:56 +0900 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(reset_simple_ops); |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * struct reset_simple_devdata - simple reset controller properties |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 90 | * @reg_offset: offset between base address and first reset register. |
| 91 | * @nr_resets: number of resets. If not set, default to resource size in bits. |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 92 | * @active_low: if true, bits are cleared to assert the reset. Otherwise, bits |
| 93 | * are set to assert the reset. |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 94 | * @status_active_low: if true, bits read back as cleared while the reset is |
| 95 | * asserted. Otherwise, bits read back as set while the |
| 96 | * reset is asserted. |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 97 | */ |
| 98 | struct reset_simple_devdata { |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 99 | u32 reg_offset; |
| 100 | u32 nr_resets; |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 101 | bool active_low; |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 102 | bool status_active_low; |
| 103 | }; |
| 104 | |
| 105 | #define SOCFPGA_NR_BANKS 8 |
| 106 | |
| 107 | static const struct reset_simple_devdata reset_simple_socfpga = { |
Dinh Nguyen | b3ca988 | 2018-11-13 12:50:48 -0600 | [diff] [blame] | 108 | .reg_offset = 0x20, |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 109 | .nr_resets = SOCFPGA_NR_BANKS * 32, |
| 110 | .status_active_low = true, |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static const struct reset_simple_devdata reset_simple_active_low = { |
| 114 | .active_low = true, |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 115 | .status_active_low = true, |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static const struct of_device_id reset_simple_dt_ids[] = { |
Dinh Nguyen | b3ca988 | 2018-11-13 12:50:48 -0600 | [diff] [blame] | 119 | { .compatible = "altr,stratix10-rst-mgr", |
| 120 | .data = &reset_simple_socfpga }, |
Philipp Zabel | 0af8a13 | 2017-08-11 13:02:47 +0200 | [diff] [blame] | 121 | { .compatible = "st,stm32-rcc", }, |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 122 | { .compatible = "allwinner,sun6i-a31-clock-reset", |
| 123 | .data = &reset_simple_active_low }, |
Philipp Zabel | f0e0ada | 2017-08-11 13:03:05 +0200 | [diff] [blame] | 124 | { .compatible = "zte,zx296718-reset", |
| 125 | .data = &reset_simple_active_low }, |
Joel Stanley | 1d7592f | 2018-02-20 12:13:29 +1030 | [diff] [blame] | 126 | { .compatible = "aspeed,ast2400-lpc-reset" }, |
| 127 | { .compatible = "aspeed,ast2500-lpc-reset" }, |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 128 | { /* sentinel */ }, |
| 129 | }; |
| 130 | |
| 131 | static int reset_simple_probe(struct platform_device *pdev) |
| 132 | { |
| 133 | struct device *dev = &pdev->dev; |
| 134 | const struct reset_simple_devdata *devdata; |
| 135 | struct reset_simple_data *data; |
| 136 | void __iomem *membase; |
| 137 | struct resource *res; |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 138 | u32 reg_offset = 0; |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 139 | |
| 140 | devdata = of_device_get_match_data(dev); |
| 141 | |
| 142 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 143 | if (!data) |
| 144 | return -ENOMEM; |
| 145 | |
| 146 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 147 | membase = devm_ioremap_resource(dev, res); |
| 148 | if (IS_ERR(membase)) |
| 149 | return PTR_ERR(membase); |
| 150 | |
| 151 | spin_lock_init(&data->lock); |
| 152 | data->membase = membase; |
| 153 | data->rcdev.owner = THIS_MODULE; |
| 154 | data->rcdev.nr_resets = resource_size(res) * BITS_PER_BYTE; |
| 155 | data->rcdev.ops = &reset_simple_ops; |
| 156 | data->rcdev.of_node = dev->of_node; |
| 157 | |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 158 | if (devdata) { |
| 159 | reg_offset = devdata->reg_offset; |
| 160 | if (devdata->nr_resets) |
| 161 | data->rcdev.nr_resets = devdata->nr_resets; |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 162 | data->active_low = devdata->active_low; |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 163 | data->status_active_low = devdata->status_active_low; |
| 164 | } |
| 165 | |
Philipp Zabel | adf20d7 | 2017-08-11 13:02:19 +0200 | [diff] [blame] | 166 | data->membase += reg_offset; |
Philipp Zabel | 81c22ad | 2017-08-11 12:58:43 +0200 | [diff] [blame] | 167 | |
| 168 | return devm_reset_controller_register(dev, &data->rcdev); |
| 169 | } |
| 170 | |
| 171 | static struct platform_driver reset_simple_driver = { |
| 172 | .probe = reset_simple_probe, |
| 173 | .driver = { |
| 174 | .name = "simple-reset", |
| 175 | .of_match_table = reset_simple_dt_ids, |
| 176 | }, |
| 177 | }; |
| 178 | builtin_platform_driver(reset_simple_driver); |