Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Broadcom Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation version 2. |
| 7 | * |
| 8 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 9 | * kind, whether express or implied; without even the implied warranty |
| 10 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/jiffies.h> |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 19 | #include <linux/notifier.h> |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_irq.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/printk.h> |
| 25 | #include <linux/reboot.h> |
| 26 | #include <linux/regmap.h> |
| 27 | #include <linux/smp.h> |
| 28 | #include <linux/mfd/syscon.h> |
| 29 | |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 30 | #define RESET_SOURCE_ENABLE_REG 1 |
| 31 | #define SW_MASTER_RESET_REG 2 |
| 32 | |
| 33 | static struct regmap *regmap; |
| 34 | static u32 rst_src_en; |
| 35 | static u32 sw_mstr_rst; |
| 36 | |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 37 | static int brcmstb_restart_handler(struct notifier_block *this, |
| 38 | unsigned long mode, void *cmd) |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 39 | { |
| 40 | int rc; |
| 41 | u32 tmp; |
| 42 | |
| 43 | rc = regmap_write(regmap, rst_src_en, 1); |
| 44 | if (rc) { |
| 45 | pr_err("failed to write rst_src_en (%d)\n", rc); |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 46 | return NOTIFY_DONE; |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | rc = regmap_read(regmap, rst_src_en, &tmp); |
| 50 | if (rc) { |
| 51 | pr_err("failed to read rst_src_en (%d)\n", rc); |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 52 | return NOTIFY_DONE; |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | rc = regmap_write(regmap, sw_mstr_rst, 1); |
| 56 | if (rc) { |
| 57 | pr_err("failed to write sw_mstr_rst (%d)\n", rc); |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 58 | return NOTIFY_DONE; |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | rc = regmap_read(regmap, sw_mstr_rst, &tmp); |
| 62 | if (rc) { |
| 63 | pr_err("failed to read sw_mstr_rst (%d)\n", rc); |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 64 | return NOTIFY_DONE; |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | while (1) |
| 68 | ; |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 69 | |
| 70 | return NOTIFY_DONE; |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 73 | static struct notifier_block brcmstb_restart_nb = { |
| 74 | .notifier_call = brcmstb_restart_handler, |
| 75 | .priority = 128, |
| 76 | }; |
| 77 | |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 78 | static int brcmstb_reboot_probe(struct platform_device *pdev) |
| 79 | { |
| 80 | int rc; |
| 81 | struct device_node *np = pdev->dev.of_node; |
| 82 | |
| 83 | regmap = syscon_regmap_lookup_by_phandle(np, "syscon"); |
| 84 | if (IS_ERR(regmap)) { |
| 85 | pr_err("failed to get syscon phandle\n"); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
| 89 | rc = of_property_read_u32_index(np, "syscon", RESET_SOURCE_ENABLE_REG, |
| 90 | &rst_src_en); |
| 91 | if (rc) { |
| 92 | pr_err("can't get rst_src_en offset (%d)\n", rc); |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | rc = of_property_read_u32_index(np, "syscon", SW_MASTER_RESET_REG, |
| 97 | &sw_mstr_rst); |
| 98 | if (rc) { |
| 99 | pr_err("can't get sw_mstr_rst offset (%d)\n", rc); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 103 | rc = register_restart_handler(&brcmstb_restart_nb); |
| 104 | if (rc) |
| 105 | dev_err(&pdev->dev, |
| 106 | "cannot register restart handler (err=%d)\n", rc); |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 107 | |
Guenter Roeck | 6136c41 | 2014-09-30 10:48:36 -0700 | [diff] [blame^] | 108 | return rc; |
Marc Carino | 030494e | 2014-07-22 16:48:03 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static const struct of_device_id of_match[] = { |
| 112 | { .compatible = "brcm,brcmstb-reboot", }, |
| 113 | {}, |
| 114 | }; |
| 115 | |
| 116 | static struct platform_driver brcmstb_reboot_driver = { |
| 117 | .probe = brcmstb_reboot_probe, |
| 118 | .driver = { |
| 119 | .name = "brcmstb-reboot", |
| 120 | .owner = THIS_MODULE, |
| 121 | .of_match_table = of_match, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | static int __init brcmstb_reboot_init(void) |
| 126 | { |
| 127 | return platform_driver_probe(&brcmstb_reboot_driver, |
| 128 | brcmstb_reboot_probe); |
| 129 | } |
| 130 | subsys_initcall(brcmstb_reboot_init); |