18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2013 Broadcom Corporation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
58c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as
68c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any
98c2ecf20Sopenharmony_ci * kind, whether express or implied; without even the implied warranty
108c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
118c2ecf20Sopenharmony_ci * GNU General Public License for more details.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/bitops.h>
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include <linux/errno.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
208c2ecf20Sopenharmony_ci#include <linux/notifier.h>
218c2ecf20Sopenharmony_ci#include <linux/of_address.h>
228c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
238c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
248c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
258c2ecf20Sopenharmony_ci#include <linux/printk.h>
268c2ecf20Sopenharmony_ci#include <linux/reboot.h>
278c2ecf20Sopenharmony_ci#include <linux/regmap.h>
288c2ecf20Sopenharmony_ci#include <linux/smp.h>
298c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define RESET_SOURCE_ENABLE_REG 1
328c2ecf20Sopenharmony_ci#define SW_MASTER_RESET_REG 2
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic struct regmap *regmap;
358c2ecf20Sopenharmony_cistatic u32 rst_src_en;
368c2ecf20Sopenharmony_cistatic u32 sw_mstr_rst;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistruct reset_reg_mask {
398c2ecf20Sopenharmony_ci	u32 rst_src_en_mask;
408c2ecf20Sopenharmony_ci	u32 sw_mstr_rst_mask;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic const struct reset_reg_mask *reset_masks;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int brcmstb_restart_handler(struct notifier_block *this,
468c2ecf20Sopenharmony_ci				   unsigned long mode, void *cmd)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	int rc;
498c2ecf20Sopenharmony_ci	u32 tmp;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	rc = regmap_write(regmap, rst_src_en, reset_masks->rst_src_en_mask);
528c2ecf20Sopenharmony_ci	if (rc) {
538c2ecf20Sopenharmony_ci		pr_err("failed to write rst_src_en (%d)\n", rc);
548c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	rc = regmap_read(regmap, rst_src_en, &tmp);
588c2ecf20Sopenharmony_ci	if (rc) {
598c2ecf20Sopenharmony_ci		pr_err("failed to read rst_src_en (%d)\n", rc);
608c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
618c2ecf20Sopenharmony_ci	}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	rc = regmap_write(regmap, sw_mstr_rst, reset_masks->sw_mstr_rst_mask);
648c2ecf20Sopenharmony_ci	if (rc) {
658c2ecf20Sopenharmony_ci		pr_err("failed to write sw_mstr_rst (%d)\n", rc);
668c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	rc = regmap_read(regmap, sw_mstr_rst, &tmp);
708c2ecf20Sopenharmony_ci	if (rc) {
718c2ecf20Sopenharmony_ci		pr_err("failed to read sw_mstr_rst (%d)\n", rc);
728c2ecf20Sopenharmony_ci		return NOTIFY_DONE;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	while (1)
768c2ecf20Sopenharmony_ci		;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic struct notifier_block brcmstb_restart_nb = {
828c2ecf20Sopenharmony_ci	.notifier_call = brcmstb_restart_handler,
838c2ecf20Sopenharmony_ci	.priority = 128,
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic const struct reset_reg_mask reset_bits_40nm = {
878c2ecf20Sopenharmony_ci	.rst_src_en_mask = BIT(0),
888c2ecf20Sopenharmony_ci	.sw_mstr_rst_mask = BIT(0),
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic const struct reset_reg_mask reset_bits_65nm = {
928c2ecf20Sopenharmony_ci	.rst_src_en_mask = BIT(3),
938c2ecf20Sopenharmony_ci	.sw_mstr_rst_mask = BIT(31),
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic const struct of_device_id of_match[] = {
978c2ecf20Sopenharmony_ci	{ .compatible = "brcm,brcmstb-reboot", .data = &reset_bits_40nm },
988c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7038-reboot", .data = &reset_bits_65nm },
998c2ecf20Sopenharmony_ci	{},
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int brcmstb_reboot_probe(struct platform_device *pdev)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	int rc;
1058c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
1068c2ecf20Sopenharmony_ci	const struct of_device_id *of_id;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	of_id = of_match_node(of_match, np);
1098c2ecf20Sopenharmony_ci	if (!of_id) {
1108c2ecf20Sopenharmony_ci		pr_err("failed to look up compatible string\n");
1118c2ecf20Sopenharmony_ci		return -EINVAL;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci	reset_masks = of_id->data;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	regmap = syscon_regmap_lookup_by_phandle(np, "syscon");
1168c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
1178c2ecf20Sopenharmony_ci		pr_err("failed to get syscon phandle\n");
1188c2ecf20Sopenharmony_ci		return -EINVAL;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	rc = of_property_read_u32_index(np, "syscon", RESET_SOURCE_ENABLE_REG,
1228c2ecf20Sopenharmony_ci					&rst_src_en);
1238c2ecf20Sopenharmony_ci	if (rc) {
1248c2ecf20Sopenharmony_ci		pr_err("can't get rst_src_en offset (%d)\n", rc);
1258c2ecf20Sopenharmony_ci		return -EINVAL;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	rc = of_property_read_u32_index(np, "syscon", SW_MASTER_RESET_REG,
1298c2ecf20Sopenharmony_ci					&sw_mstr_rst);
1308c2ecf20Sopenharmony_ci	if (rc) {
1318c2ecf20Sopenharmony_ci		pr_err("can't get sw_mstr_rst offset (%d)\n", rc);
1328c2ecf20Sopenharmony_ci		return -EINVAL;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	rc = register_restart_handler(&brcmstb_restart_nb);
1368c2ecf20Sopenharmony_ci	if (rc)
1378c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
1388c2ecf20Sopenharmony_ci			"cannot register restart handler (err=%d)\n", rc);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return rc;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic struct platform_driver brcmstb_reboot_driver = {
1448c2ecf20Sopenharmony_ci	.probe = brcmstb_reboot_probe,
1458c2ecf20Sopenharmony_ci	.driver = {
1468c2ecf20Sopenharmony_ci		.name = "brcmstb-reboot",
1478c2ecf20Sopenharmony_ci		.of_match_table = of_match,
1488c2ecf20Sopenharmony_ci	},
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int __init brcmstb_reboot_init(void)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	return platform_driver_probe(&brcmstb_reboot_driver,
1548c2ecf20Sopenharmony_ci					brcmstb_reboot_probe);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_cisubsys_initcall(brcmstb_reboot_init);
157