Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 1 | /* |
Paul Gortmaker | 90ce95a | 2017-02-07 19:18:44 -0500 | [diff] [blame] | 2 | * AR71xx Reset Controller Driver |
| 3 | * Author: Alban Bedel |
| 4 | * |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 5 | * Copyright (C) 2015 Alban Bedel <albeu@free.fr> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
Philipp Zabel | 9e9ba09 | 2016-08-09 11:17:22 +0200 | [diff] [blame] | 18 | #include <linux/io.h> |
Paul Gortmaker | 90ce95a | 2017-02-07 19:18:44 -0500 | [diff] [blame] | 19 | #include <linux/init.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 20 | #include <linux/mod_devicetable.h> |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/reset-controller.h> |
Alban Bedel | ba64e27 | 2015-11-24 01:00:33 +0100 | [diff] [blame] | 23 | #include <linux/reboot.h> |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 24 | |
| 25 | struct ath79_reset { |
| 26 | struct reset_controller_dev rcdev; |
Alban Bedel | ba64e27 | 2015-11-24 01:00:33 +0100 | [diff] [blame] | 27 | struct notifier_block restart_nb; |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 28 | void __iomem *base; |
| 29 | spinlock_t lock; |
| 30 | }; |
| 31 | |
Alban Bedel | ba64e27 | 2015-11-24 01:00:33 +0100 | [diff] [blame] | 32 | #define FULL_CHIP_RESET 24 |
| 33 | |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 34 | static int ath79_reset_update(struct reset_controller_dev *rcdev, |
| 35 | unsigned long id, bool assert) |
| 36 | { |
| 37 | struct ath79_reset *ath79_reset = |
| 38 | container_of(rcdev, struct ath79_reset, rcdev); |
| 39 | unsigned long flags; |
| 40 | u32 val; |
| 41 | |
| 42 | spin_lock_irqsave(&ath79_reset->lock, flags); |
| 43 | val = readl(ath79_reset->base); |
| 44 | if (assert) |
| 45 | val |= BIT(id); |
| 46 | else |
| 47 | val &= ~BIT(id); |
| 48 | writel(val, ath79_reset->base); |
| 49 | spin_unlock_irqrestore(&ath79_reset->lock, flags); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int ath79_reset_assert(struct reset_controller_dev *rcdev, |
| 55 | unsigned long id) |
| 56 | { |
| 57 | return ath79_reset_update(rcdev, id, true); |
| 58 | } |
| 59 | |
| 60 | static int ath79_reset_deassert(struct reset_controller_dev *rcdev, |
| 61 | unsigned long id) |
| 62 | { |
| 63 | return ath79_reset_update(rcdev, id, false); |
| 64 | } |
| 65 | |
| 66 | static int ath79_reset_status(struct reset_controller_dev *rcdev, |
| 67 | unsigned long id) |
| 68 | { |
| 69 | struct ath79_reset *ath79_reset = |
| 70 | container_of(rcdev, struct ath79_reset, rcdev); |
| 71 | u32 val; |
| 72 | |
| 73 | val = readl(ath79_reset->base); |
| 74 | |
| 75 | return !!(val & BIT(id)); |
| 76 | } |
| 77 | |
Philipp Zabel | d2f79f2 | 2016-01-17 15:12:23 +0100 | [diff] [blame] | 78 | static const struct reset_control_ops ath79_reset_ops = { |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 79 | .assert = ath79_reset_assert, |
| 80 | .deassert = ath79_reset_deassert, |
| 81 | .status = ath79_reset_status, |
| 82 | }; |
| 83 | |
Alban Bedel | ba64e27 | 2015-11-24 01:00:33 +0100 | [diff] [blame] | 84 | static int ath79_reset_restart_handler(struct notifier_block *nb, |
| 85 | unsigned long action, void *data) |
| 86 | { |
| 87 | struct ath79_reset *ath79_reset = |
| 88 | container_of(nb, struct ath79_reset, restart_nb); |
| 89 | |
| 90 | ath79_reset_assert(&ath79_reset->rcdev, FULL_CHIP_RESET); |
| 91 | |
| 92 | return NOTIFY_DONE; |
| 93 | } |
| 94 | |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 95 | static int ath79_reset_probe(struct platform_device *pdev) |
| 96 | { |
| 97 | struct ath79_reset *ath79_reset; |
| 98 | struct resource *res; |
Alban Bedel | ba64e27 | 2015-11-24 01:00:33 +0100 | [diff] [blame] | 99 | int err; |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 100 | |
| 101 | ath79_reset = devm_kzalloc(&pdev->dev, |
| 102 | sizeof(*ath79_reset), GFP_KERNEL); |
| 103 | if (!ath79_reset) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | platform_set_drvdata(pdev, ath79_reset); |
| 107 | |
| 108 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 109 | ath79_reset->base = devm_ioremap_resource(&pdev->dev, res); |
| 110 | if (IS_ERR(ath79_reset->base)) |
| 111 | return PTR_ERR(ath79_reset->base); |
| 112 | |
Axel Lin | f319cb8 | 2015-09-01 20:15:10 +0800 | [diff] [blame] | 113 | spin_lock_init(&ath79_reset->lock); |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 114 | ath79_reset->rcdev.ops = &ath79_reset_ops; |
| 115 | ath79_reset->rcdev.owner = THIS_MODULE; |
| 116 | ath79_reset->rcdev.of_node = pdev->dev.of_node; |
| 117 | ath79_reset->rcdev.of_reset_n_cells = 1; |
| 118 | ath79_reset->rcdev.nr_resets = 32; |
| 119 | |
Masahiro Yamada | 56865f4 | 2016-05-01 19:36:58 +0900 | [diff] [blame] | 120 | err = devm_reset_controller_register(&pdev->dev, &ath79_reset->rcdev); |
Alban Bedel | ba64e27 | 2015-11-24 01:00:33 +0100 | [diff] [blame] | 121 | if (err) |
| 122 | return err; |
| 123 | |
| 124 | ath79_reset->restart_nb.notifier_call = ath79_reset_restart_handler; |
| 125 | ath79_reset->restart_nb.priority = 128; |
| 126 | |
| 127 | err = register_restart_handler(&ath79_reset->restart_nb); |
| 128 | if (err) |
| 129 | dev_warn(&pdev->dev, "Failed to register restart handler\n"); |
| 130 | |
| 131 | return 0; |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 134 | static const struct of_device_id ath79_reset_dt_ids[] = { |
| 135 | { .compatible = "qca,ar7100-reset", }, |
| 136 | { }, |
| 137 | }; |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 138 | |
| 139 | static struct platform_driver ath79_reset_driver = { |
| 140 | .probe = ath79_reset_probe, |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 141 | .driver = { |
Paul Gortmaker | 90ce95a | 2017-02-07 19:18:44 -0500 | [diff] [blame] | 142 | .name = "ath79-reset", |
| 143 | .of_match_table = ath79_reset_dt_ids, |
| 144 | .suppress_bind_attrs = true, |
Alban Bedel | ff591a9 | 2015-08-03 19:23:52 +0200 | [diff] [blame] | 145 | }, |
| 146 | }; |
Paul Gortmaker | 90ce95a | 2017-02-07 19:18:44 -0500 | [diff] [blame] | 147 | builtin_platform_driver(ath79_reset_driver); |