18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Atmel AT91 SAM9 & SAMA5 SoCs reset code 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2007 Atmel Corporation. 58c2ecf20Sopenharmony_ci * Copyright (C) BitBox Ltd 2010 68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcosoft.com> 78c2ecf20Sopenharmony_ci * Copyright (C) 2014 Free Electrons 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public 108c2ecf20Sopenharmony_ci * License version 2. This program is licensed "as is" without any 118c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/clk.h> 158c2ecf20Sopenharmony_ci#include <linux/io.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/of_address.h> 188c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 198c2ecf20Sopenharmony_ci#include <linux/reboot.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include <soc/at91/at91sam9_ddrsdr.h> 228c2ecf20Sopenharmony_ci#include <soc/at91/at91sam9_sdramc.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define AT91_RSTC_CR 0x00 /* Reset Controller Control Register */ 258c2ecf20Sopenharmony_ci#define AT91_RSTC_PROCRST BIT(0) /* Processor Reset */ 268c2ecf20Sopenharmony_ci#define AT91_RSTC_PERRST BIT(2) /* Peripheral Reset */ 278c2ecf20Sopenharmony_ci#define AT91_RSTC_EXTRST BIT(3) /* External Reset */ 288c2ecf20Sopenharmony_ci#define AT91_RSTC_KEY (0xa5 << 24) /* KEY Password */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define AT91_RSTC_SR 0x04 /* Reset Controller Status Register */ 318c2ecf20Sopenharmony_ci#define AT91_RSTC_URSTS BIT(0) /* User Reset Status */ 328c2ecf20Sopenharmony_ci#define AT91_RSTC_RSTTYP GENMASK(10, 8) /* Reset Type */ 338c2ecf20Sopenharmony_ci#define AT91_RSTC_NRSTL BIT(16) /* NRST Pin Level */ 348c2ecf20Sopenharmony_ci#define AT91_RSTC_SRCMP BIT(17) /* Software Reset Command in Progress */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define AT91_RSTC_MR 0x08 /* Reset Controller Mode Register */ 378c2ecf20Sopenharmony_ci#define AT91_RSTC_URSTEN BIT(0) /* User Reset Enable */ 388c2ecf20Sopenharmony_ci#define AT91_RSTC_URSTASYNC BIT(2) /* User Reset Asynchronous Control */ 398c2ecf20Sopenharmony_ci#define AT91_RSTC_URSTIEN BIT(4) /* User Reset Interrupt Enable */ 408c2ecf20Sopenharmony_ci#define AT91_RSTC_ERSTL GENMASK(11, 8) /* External Reset Length */ 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cienum reset_type { 438c2ecf20Sopenharmony_ci RESET_TYPE_GENERAL = 0, 448c2ecf20Sopenharmony_ci RESET_TYPE_WAKEUP = 1, 458c2ecf20Sopenharmony_ci RESET_TYPE_WATCHDOG = 2, 468c2ecf20Sopenharmony_ci RESET_TYPE_SOFTWARE = 3, 478c2ecf20Sopenharmony_ci RESET_TYPE_USER = 4, 488c2ecf20Sopenharmony_ci RESET_TYPE_CPU_FAIL = 6, 498c2ecf20Sopenharmony_ci RESET_TYPE_XTAL_FAIL = 7, 508c2ecf20Sopenharmony_ci RESET_TYPE_ULP2 = 8, 518c2ecf20Sopenharmony_ci}; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistruct at91_reset { 548c2ecf20Sopenharmony_ci void __iomem *rstc_base; 558c2ecf20Sopenharmony_ci void __iomem *ramc_base[2]; 568c2ecf20Sopenharmony_ci struct clk *sclk; 578c2ecf20Sopenharmony_ci struct notifier_block nb; 588c2ecf20Sopenharmony_ci u32 args; 598c2ecf20Sopenharmony_ci u32 ramc_lpr; 608c2ecf20Sopenharmony_ci}; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* 638c2ecf20Sopenharmony_ci* unless the SDRAM is cleanly shutdown before we hit the 648c2ecf20Sopenharmony_ci* reset register it can be left driving the data bus and 658c2ecf20Sopenharmony_ci* killing the chance of a subsequent boot from NAND 668c2ecf20Sopenharmony_ci*/ 678c2ecf20Sopenharmony_cistatic int at91_reset(struct notifier_block *this, unsigned long mode, 688c2ecf20Sopenharmony_ci void *cmd) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct at91_reset *reset = container_of(this, struct at91_reset, nb); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci asm volatile( 738c2ecf20Sopenharmony_ci /* Align to cache lines */ 748c2ecf20Sopenharmony_ci ".balign 32\n\t" 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* Disable SDRAM0 accesses */ 778c2ecf20Sopenharmony_ci " tst %0, #0\n\t" 788c2ecf20Sopenharmony_ci " beq 1f\n\t" 798c2ecf20Sopenharmony_ci " str %3, [%0, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t" 808c2ecf20Sopenharmony_ci /* Power down SDRAM0 */ 818c2ecf20Sopenharmony_ci " str %4, [%0, %6]\n\t" 828c2ecf20Sopenharmony_ci /* Disable SDRAM1 accesses */ 838c2ecf20Sopenharmony_ci "1: tst %1, #0\n\t" 848c2ecf20Sopenharmony_ci " beq 2f\n\t" 858c2ecf20Sopenharmony_ci " strne %3, [%1, #" __stringify(AT91_DDRSDRC_RTR) "]\n\t" 868c2ecf20Sopenharmony_ci /* Power down SDRAM1 */ 878c2ecf20Sopenharmony_ci " strne %4, [%1, %6]\n\t" 888c2ecf20Sopenharmony_ci /* Reset CPU */ 898c2ecf20Sopenharmony_ci "2: str %5, [%2, #" __stringify(AT91_RSTC_CR) "]\n\t" 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci " b .\n\t" 928c2ecf20Sopenharmony_ci : 938c2ecf20Sopenharmony_ci : "r" (reset->ramc_base[0]), 948c2ecf20Sopenharmony_ci "r" (reset->ramc_base[1]), 958c2ecf20Sopenharmony_ci "r" (reset->rstc_base), 968c2ecf20Sopenharmony_ci "r" (1), 978c2ecf20Sopenharmony_ci "r" cpu_to_le32(AT91_DDRSDRC_LPCB_POWER_DOWN), 988c2ecf20Sopenharmony_ci "r" (reset->args), 998c2ecf20Sopenharmony_ci "r" (reset->ramc_lpr) 1008c2ecf20Sopenharmony_ci : "r4"); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci return NOTIFY_DONE; 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic void __init at91_reset_status(struct platform_device *pdev, 1068c2ecf20Sopenharmony_ci void __iomem *base) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci const char *reason; 1098c2ecf20Sopenharmony_ci u32 reg = readl(base + AT91_RSTC_SR); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci switch ((reg & AT91_RSTC_RSTTYP) >> 8) { 1128c2ecf20Sopenharmony_ci case RESET_TYPE_GENERAL: 1138c2ecf20Sopenharmony_ci reason = "general reset"; 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci case RESET_TYPE_WAKEUP: 1168c2ecf20Sopenharmony_ci reason = "wakeup"; 1178c2ecf20Sopenharmony_ci break; 1188c2ecf20Sopenharmony_ci case RESET_TYPE_WATCHDOG: 1198c2ecf20Sopenharmony_ci reason = "watchdog reset"; 1208c2ecf20Sopenharmony_ci break; 1218c2ecf20Sopenharmony_ci case RESET_TYPE_SOFTWARE: 1228c2ecf20Sopenharmony_ci reason = "software reset"; 1238c2ecf20Sopenharmony_ci break; 1248c2ecf20Sopenharmony_ci case RESET_TYPE_USER: 1258c2ecf20Sopenharmony_ci reason = "user reset"; 1268c2ecf20Sopenharmony_ci break; 1278c2ecf20Sopenharmony_ci case RESET_TYPE_CPU_FAIL: 1288c2ecf20Sopenharmony_ci reason = "CPU clock failure detection"; 1298c2ecf20Sopenharmony_ci break; 1308c2ecf20Sopenharmony_ci case RESET_TYPE_XTAL_FAIL: 1318c2ecf20Sopenharmony_ci reason = "32.768 kHz crystal failure detection"; 1328c2ecf20Sopenharmony_ci break; 1338c2ecf20Sopenharmony_ci case RESET_TYPE_ULP2: 1348c2ecf20Sopenharmony_ci reason = "ULP2 reset"; 1358c2ecf20Sopenharmony_ci break; 1368c2ecf20Sopenharmony_ci default: 1378c2ecf20Sopenharmony_ci reason = "unknown reset"; 1388c2ecf20Sopenharmony_ci break; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Starting after %s\n", reason); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic const struct of_device_id at91_ramc_of_match[] = { 1458c2ecf20Sopenharmony_ci { 1468c2ecf20Sopenharmony_ci .compatible = "atmel,at91sam9260-sdramc", 1478c2ecf20Sopenharmony_ci .data = (void *)AT91_SDRAMC_LPR, 1488c2ecf20Sopenharmony_ci }, 1498c2ecf20Sopenharmony_ci { 1508c2ecf20Sopenharmony_ci .compatible = "atmel,at91sam9g45-ddramc", 1518c2ecf20Sopenharmony_ci .data = (void *)AT91_DDRSDRC_LPR, 1528c2ecf20Sopenharmony_ci }, 1538c2ecf20Sopenharmony_ci { /* sentinel */ } 1548c2ecf20Sopenharmony_ci}; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic const struct of_device_id at91_reset_of_match[] = { 1578c2ecf20Sopenharmony_ci { 1588c2ecf20Sopenharmony_ci .compatible = "atmel,at91sam9260-rstc", 1598c2ecf20Sopenharmony_ci .data = (void *)(AT91_RSTC_KEY | AT91_RSTC_PERRST | 1608c2ecf20Sopenharmony_ci AT91_RSTC_PROCRST), 1618c2ecf20Sopenharmony_ci }, 1628c2ecf20Sopenharmony_ci { 1638c2ecf20Sopenharmony_ci .compatible = "atmel,at91sam9g45-rstc", 1648c2ecf20Sopenharmony_ci .data = (void *)(AT91_RSTC_KEY | AT91_RSTC_PERRST | 1658c2ecf20Sopenharmony_ci AT91_RSTC_PROCRST) 1668c2ecf20Sopenharmony_ci }, 1678c2ecf20Sopenharmony_ci { 1688c2ecf20Sopenharmony_ci .compatible = "atmel,sama5d3-rstc", 1698c2ecf20Sopenharmony_ci .data = (void *)(AT91_RSTC_KEY | AT91_RSTC_PERRST | 1708c2ecf20Sopenharmony_ci AT91_RSTC_PROCRST) 1718c2ecf20Sopenharmony_ci }, 1728c2ecf20Sopenharmony_ci { 1738c2ecf20Sopenharmony_ci .compatible = "atmel,samx7-rstc", 1748c2ecf20Sopenharmony_ci .data = (void *)(AT91_RSTC_KEY | AT91_RSTC_PROCRST) 1758c2ecf20Sopenharmony_ci }, 1768c2ecf20Sopenharmony_ci { 1778c2ecf20Sopenharmony_ci .compatible = "microchip,sam9x60-rstc", 1788c2ecf20Sopenharmony_ci .data = (void *)(AT91_RSTC_KEY | AT91_RSTC_PROCRST) 1798c2ecf20Sopenharmony_ci }, 1808c2ecf20Sopenharmony_ci { /* sentinel */ } 1818c2ecf20Sopenharmony_ci}; 1828c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, at91_reset_of_match); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic int __init at91_reset_probe(struct platform_device *pdev) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci const struct of_device_id *match; 1878c2ecf20Sopenharmony_ci struct at91_reset *reset; 1888c2ecf20Sopenharmony_ci struct device_node *np; 1898c2ecf20Sopenharmony_ci int ret, idx = 0; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci reset = devm_kzalloc(&pdev->dev, sizeof(*reset), GFP_KERNEL); 1928c2ecf20Sopenharmony_ci if (!reset) 1938c2ecf20Sopenharmony_ci return -ENOMEM; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci reset->rstc_base = of_iomap(pdev->dev.of_node, 0); 1968c2ecf20Sopenharmony_ci if (!reset->rstc_base) { 1978c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Could not map reset controller address\n"); 1988c2ecf20Sopenharmony_ci return -ENODEV; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci if (!of_device_is_compatible(pdev->dev.of_node, "atmel,sama5d3-rstc")) { 2028c2ecf20Sopenharmony_ci /* we need to shutdown the ddr controller, so get ramc base */ 2038c2ecf20Sopenharmony_ci for_each_matching_node_and_match(np, at91_ramc_of_match, &match) { 2048c2ecf20Sopenharmony_ci reset->ramc_lpr = (u32)match->data; 2058c2ecf20Sopenharmony_ci reset->ramc_base[idx] = of_iomap(np, 0); 2068c2ecf20Sopenharmony_ci if (!reset->ramc_base[idx]) { 2078c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Could not map ram controller address\n"); 2088c2ecf20Sopenharmony_ci of_node_put(np); 2098c2ecf20Sopenharmony_ci return -ENODEV; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci idx++; 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci } 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci match = of_match_node(at91_reset_of_match, pdev->dev.of_node); 2168c2ecf20Sopenharmony_ci reset->nb.notifier_call = at91_reset; 2178c2ecf20Sopenharmony_ci reset->nb.priority = 192; 2188c2ecf20Sopenharmony_ci reset->args = (u32)match->data; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci reset->sclk = devm_clk_get(&pdev->dev, NULL); 2218c2ecf20Sopenharmony_ci if (IS_ERR(reset->sclk)) 2228c2ecf20Sopenharmony_ci return PTR_ERR(reset->sclk); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci ret = clk_prepare_enable(reset->sclk); 2258c2ecf20Sopenharmony_ci if (ret) { 2268c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Could not enable slow clock\n"); 2278c2ecf20Sopenharmony_ci return ret; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, reset); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci if (of_device_is_compatible(pdev->dev.of_node, "microchip,sam9x60-rstc")) { 2338c2ecf20Sopenharmony_ci u32 val = readl(reset->rstc_base + AT91_RSTC_MR); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci writel(AT91_RSTC_KEY | AT91_RSTC_URSTASYNC | val, 2368c2ecf20Sopenharmony_ci reset->rstc_base + AT91_RSTC_MR); 2378c2ecf20Sopenharmony_ci } 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci ret = register_restart_handler(&reset->nb); 2408c2ecf20Sopenharmony_ci if (ret) { 2418c2ecf20Sopenharmony_ci clk_disable_unprepare(reset->sclk); 2428c2ecf20Sopenharmony_ci return ret; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci at91_reset_status(pdev, reset->rstc_base); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci return 0; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_cistatic int __exit at91_reset_remove(struct platform_device *pdev) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci struct at91_reset *reset = platform_get_drvdata(pdev); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci unregister_restart_handler(&reset->nb); 2558c2ecf20Sopenharmony_ci clk_disable_unprepare(reset->sclk); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci return 0; 2588c2ecf20Sopenharmony_ci} 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_cistatic struct platform_driver at91_reset_driver = { 2618c2ecf20Sopenharmony_ci .remove = __exit_p(at91_reset_remove), 2628c2ecf20Sopenharmony_ci .driver = { 2638c2ecf20Sopenharmony_ci .name = "at91-reset", 2648c2ecf20Sopenharmony_ci .of_match_table = at91_reset_of_match, 2658c2ecf20Sopenharmony_ci }, 2668c2ecf20Sopenharmony_ci}; 2678c2ecf20Sopenharmony_cimodule_platform_driver_probe(at91_reset_driver, at91_reset_probe); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ciMODULE_AUTHOR("Atmel Corporation"); 2708c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Reset driver for Atmel SoCs"); 2718c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 272