1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2021 SiFive
* Copyright (c) 2021 Western Digital Corporation or its affiliates.
*
* Authors:
* Green Wan <green.wan@sifive.com>
* Anup Patel <anup.patel@wdc.com>
*/
#include <libfdt.h>
#include <sbi/sbi_console.h>
#include <sbi/sbi_ecall_interface.h>
#include <sbi/sbi_hart.h>
#include <sbi/sbi_system.h>
#include <sbi/sbi_timer.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/gpio/fdt_gpio.h>
#include <sbi_utils/reset/fdt_reset.h>
struct gpio_reset {
struct gpio_pin pin;
u32 active_delay;
u32 inactive_delay;
};
static struct gpio_reset poweroff = {
.active_delay = 100,
.inactive_delay = 100
};
static struct gpio_reset restart = {
.active_delay = 100,
.inactive_delay = 100
};
static struct gpio_reset *gpio_reset_get(bool is_restart, u32 type)
{
struct gpio_reset *reset = NULL;
switch (type) {
case SBI_SRST_RESET_TYPE_SHUTDOWN:
if (!is_restart)
reset = &poweroff;
break;
case SBI_SRST_RESET_TYPE_COLD_REBOOT:
case SBI_SRST_RESET_TYPE_WARM_REBOOT:
if (is_restart)
reset = &restart;
break;
}
if (reset && !reset->pin.chip)
reset = NULL;
return reset;
}
static void gpio_reset_exec(struct gpio_reset *reset)
{
if (reset) {
/* drive it active, also inactive->active edge */
gpio_direction_output(&reset->pin, 1);
sbi_timer_mdelay(reset->active_delay);
/* drive inactive, also active->inactive edge */
gpio_set(&reset->pin, 0);
sbi_timer_mdelay(reset->inactive_delay);
/* drive it active, also inactive->active edge */
gpio_set(&reset->pin, 1);
}
/* hang !!! */
sbi_hart_hang();
}
static int gpio_system_poweroff_check(u32 type, u32 reason)
{
if (gpio_reset_get(FALSE, type))
return 128;
return 0;
}
static void gpio_system_poweroff(u32 type, u32 reason)
{
gpio_reset_exec(gpio_reset_get(FALSE, type));
}
static struct sbi_system_reset_device gpio_poweroff = {
.name = "gpio-poweroff",
.system_reset_check = gpio_system_poweroff_check,
.system_reset = gpio_system_poweroff
};
static int gpio_system_restart_check(u32 type, u32 reason)
{
if (gpio_reset_get(TRUE, type))
return 128;
return 0;
}
static void gpio_system_restart(u32 type, u32 reason)
{
gpio_reset_exec(gpio_reset_get(TRUE, type));
}
static struct sbi_system_reset_device gpio_restart = {
.name = "gpio-restart",
.system_reset_check = gpio_system_restart_check,
.system_reset = gpio_system_restart
};
static int gpio_reset_init(void *fdt, int nodeoff,
const struct fdt_match *match)
{
int rc, len;
const fdt32_t *val;
bool is_restart = (ulong)match->data;
const char *dir_prop = (is_restart) ? "open-source" : "input";
struct gpio_reset *reset = (is_restart) ? &restart : &poweroff;
rc = fdt_gpio_pin_get(fdt, nodeoff, 0, &reset->pin);
if (rc)
return rc;
if (fdt_getprop(fdt, nodeoff, dir_prop, &len)) {
rc = gpio_direction_input(&reset->pin);
if (rc)
return rc;
}
val = fdt_getprop(fdt, nodeoff, "active-delay-ms", &len);
if (len > 0)
reset->active_delay = fdt32_to_cpu(*val);
val = fdt_getprop(fdt, nodeoff, "inactive-delay-ms", &len);
if (len > 0)
reset->inactive_delay = fdt32_to_cpu(*val);
if (is_restart)
sbi_system_reset_add_device(&gpio_restart);
else
sbi_system_reset_add_device(&gpio_poweroff);
return 0;
}
static const struct fdt_match gpio_poweroff_match[] = {
{ .compatible = "gpio-poweroff", .data = (const void *)FALSE },
{ },
};
struct fdt_reset fdt_poweroff_gpio = {
.match_table = gpio_poweroff_match,
.init = gpio_reset_init,
};
static const struct fdt_match gpio_reset_match[] = {
{ .compatible = "gpio-restart", .data = (const void *)TRUE },
{ },
};
struct fdt_reset fdt_reset_gpio = {
.match_table = gpio_reset_match,
.init = gpio_reset_init,
};
|