Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Driver for watchdog aspect of for Zodiac Inflight Innovations RAVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Supervisory Processor(SP) MCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2017 Zodiac Inflight Innovation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mfd/rave-sp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/nvmem-consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	RAVE_SP_RESET_BYTE = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	RAVE_SP_RESET_REASON_NORMAL = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	RAVE_SP_RESET_DELAY_MS = 500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * struct rave_sp_wdt_variant - RAVE SP watchdog variant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * @max_timeout:	Largest possible watchdog timeout setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * @min_timeout:	Smallest possible watchdog timeout setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * @configure:		Function to send configuration command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * @restart:		Function to send "restart" command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct rave_sp_wdt_variant {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned int max_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	unsigned int min_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int (*configure)(struct watchdog_device *, bool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int (*restart)(struct watchdog_device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * struct rave_sp_wdt - RAVE SP watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * @wdd:		Underlying watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * @sp:			Pointer to parent RAVE SP device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * @variant:		Device specific variant information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * @reboot_notifier:	Reboot notifier implementing machine reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) struct rave_sp_wdt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct watchdog_device wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct rave_sp *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	const struct rave_sp_wdt_variant *variant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct notifier_block reboot_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static struct rave_sp_wdt *to_rave_sp_wdt(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return container_of(wdd, struct rave_sp_wdt, wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int rave_sp_wdt_exec(struct watchdog_device *wdd, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			    size_t data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return rave_sp_exec(to_rave_sp_wdt(wdd)->sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			    data, data_size, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int rave_sp_wdt_legacy_configure(struct watchdog_device *wdd, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		[0] = RAVE_SP_CMD_SW_WDT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		[1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		[2] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		[3] = on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		[4] = on ? wdd->timeout : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static int rave_sp_wdt_rdu_configure(struct watchdog_device *wdd, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		[0] = RAVE_SP_CMD_SW_WDT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		[1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		[2] = on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		[3] = (u8)wdd->timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		[4] = (u8)(wdd->timeout >> 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * rave_sp_wdt_configure - Configure watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * @wdd:	Device to configure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * @on:		Desired state of the watchdog timer (ON/OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * This function configures two aspects of the watchdog timer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  *  - Wheither it is ON or OFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  *  - Its timeout duration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * with first aspect specified via function argument and second via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * the value of 'wdd->timeout'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int rave_sp_wdt_configure(struct watchdog_device *wdd, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return to_rave_sp_wdt(wdd)->variant->configure(wdd, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int rave_sp_wdt_legacy_restart(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		[0] = RAVE_SP_CMD_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		[1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		[2] = RAVE_SP_RESET_BYTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int rave_sp_wdt_rdu_restart(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		[0] = RAVE_SP_CMD_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		[1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		[2] = RAVE_SP_RESET_BYTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		[3] = RAVE_SP_RESET_REASON_NORMAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int rave_sp_wdt_reboot_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				       unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	 * Restart handler is called in atomic context which means we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * can't communicate to SP via UART. Luckily for use SP will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 * wait 500ms before actually resetting us, so we ask it to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	 * so here and let the rest of the system go on wrapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	 * things up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (action == SYS_DOWN || action == SYS_HALT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		struct rave_sp_wdt *sp_wd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			container_of(nb, struct rave_sp_wdt, reboot_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		const int ret = sp_wd->variant->restart(&sp_wd->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			dev_err(sp_wd->wdd.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				"Failed to issue restart command (%d)", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int rave_sp_wdt_restart(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			       unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 * The actual work was done by reboot notifier above. SP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 * firmware waits 500 ms before issuing reset, so let's hang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * here for twice that delay and hopefuly we'd never reach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * the return statement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	mdelay(2 * RAVE_SP_RESET_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int rave_sp_wdt_start(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	ret = rave_sp_wdt_configure(wdd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		set_bit(WDOG_HW_RUNNING, &wdd->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int rave_sp_wdt_stop(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return rave_sp_wdt_configure(wdd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int rave_sp_wdt_set_timeout(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				   unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	wdd->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return rave_sp_wdt_configure(wdd, watchdog_active(wdd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int rave_sp_wdt_ping(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	u8 cmd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		[0] = RAVE_SP_CMD_PET_WDT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		[1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return rave_sp_wdt_exec(wdd, cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static const struct watchdog_info rave_sp_wdt_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	.identity = "RAVE SP Watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static const struct watchdog_ops rave_sp_wdt_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	.start = rave_sp_wdt_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	.stop = rave_sp_wdt_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	.ping = rave_sp_wdt_ping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	.set_timeout = rave_sp_wdt_set_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	.restart = rave_sp_wdt_restart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static const struct rave_sp_wdt_variant rave_sp_wdt_legacy = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	.max_timeout = 255,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	.min_timeout = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	.configure = rave_sp_wdt_legacy_configure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.restart   = rave_sp_wdt_legacy_restart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static const struct rave_sp_wdt_variant rave_sp_wdt_rdu = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	.max_timeout = 180,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.min_timeout = 60,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	.configure = rave_sp_wdt_rdu_configure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	.restart   = rave_sp_wdt_rdu_restart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static const struct of_device_id rave_sp_wdt_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		.compatible = "zii,rave-sp-watchdog-legacy",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		.data = &rave_sp_wdt_legacy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		.compatible = "zii,rave-sp-watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		.data = &rave_sp_wdt_rdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	{ /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int rave_sp_wdt_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct watchdog_device *wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct rave_sp_wdt *sp_wd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct nvmem_cell *cell;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	__le16 timeout = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	sp_wd = devm_kzalloc(dev, sizeof(*sp_wd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (!sp_wd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	sp_wd->variant = of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	sp_wd->sp      = dev_get_drvdata(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	wdd              = &sp_wd->wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	wdd->parent      = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	wdd->info        = &rave_sp_wdt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	wdd->ops         = &rave_sp_wdt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	wdd->min_timeout = sp_wd->variant->min_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	wdd->max_timeout = sp_wd->variant->max_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	wdd->status      = WATCHDOG_NOWAYOUT_INIT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	wdd->timeout     = 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	cell = nvmem_cell_get(dev, "wdt-timeout");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (!IS_ERR(cell)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		void *value = nvmem_cell_read(cell, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (!IS_ERR(value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			memcpy(&timeout, value, min(len, sizeof(timeout)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		nvmem_cell_put(cell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	watchdog_init_timeout(wdd, le16_to_cpu(timeout), dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	watchdog_set_restart_priority(wdd, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	watchdog_stop_on_unregister(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	sp_wd->reboot_notifier.notifier_call = rave_sp_wdt_reboot_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	ret = devm_register_reboot_notifier(dev, &sp_wd->reboot_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		dev_err(dev, "Failed to register reboot notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 * We don't know if watchdog is running now. To be sure, let's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * start it and depend on watchdog core to ping it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	wdd->max_hw_heartbeat_ms = wdd->max_timeout * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	ret = rave_sp_wdt_start(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		dev_err(dev, "Watchdog didn't start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	ret = devm_watchdog_register_device(dev, wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		rave_sp_wdt_stop(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static struct platform_driver rave_sp_wdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	.probe = rave_sp_wdt_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		.name = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		.of_match_table = rave_sp_wdt_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) module_platform_driver(rave_sp_wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_DEVICE_TABLE(of, rave_sp_wdt_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) MODULE_DESCRIPTION("RAVE SP Watchdog driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) MODULE_ALIAS("platform:rave-sp-watchdog");