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)  * Copyright (C) 2013 Broadcom Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define SECWDOG_CTRL_REG		0x00000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define SECWDOG_COUNT_REG		0x00000004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define SECWDOG_RESERVED_MASK		0x1dffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define SECWDOG_WD_LOAD_FLAG		0x10000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define SECWDOG_EN_MASK			0x08000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define SECWDOG_SRSTEN_MASK		0x04000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define SECWDOG_RES_MASK		0x00f00000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define SECWDOG_COUNT_MASK		0x000fffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define SECWDOG_MAX_COUNT		SECWDOG_COUNT_MASK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define SECWDOG_CLKS_SHIFT		20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define SECWDOG_MAX_RES			15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define SECWDOG_DEFAULT_RESOLUTION	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define SECWDOG_MAX_TRY			1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define SECS_TO_TICKS(x, w)		((x) << (w)->resolution)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define TICKS_TO_SECS(x, w)		((x) >> (w)->resolution)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define BCM_KONA_WDT_NAME		"bcm_kona_wdt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct bcm_kona_wdt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	 * One watchdog tick is 1/(2^resolution) seconds. Resolution can take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 * the values 0-15, meaning one tick can be 1s to 30.52us. Our default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	 * resolution of 4 means one tick is 62.5ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 * The watchdog counter is 20 bits. Depending on resolution, the maximum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	 * counter value of 0xfffff expires after about 12 days (resolution 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 * down to only 32s (resolution 15). The default resolution of 4 gives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	 * us a maximum of about 18 hours and 12 minutes before the watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 * times out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #ifdef CONFIG_BCM_KONA_WDT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unsigned long busy_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct dentry *debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int secure_register_read(struct bcm_kona_wdt *wdt, uint32_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	uint32_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	unsigned count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 * If the WD_LOAD_FLAG is set, the watchdog counter field is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * updated in hardware. Once the WD timer is updated in hardware, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * gets cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		if (unlikely(count > 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			udelay(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		val = readl_relaxed(wdt->base + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	} while ((val & SECWDOG_WD_LOAD_FLAG) && count < SECWDOG_MAX_TRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #ifdef CONFIG_BCM_KONA_WDT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/* Remember the maximum number iterations due to WD_LOAD_FLAG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (count > wdt->busy_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		wdt->busy_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* This is the only place we return a negative value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (val & SECWDOG_WD_LOAD_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* We always mask out reserved bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	val &= SECWDOG_RESERVED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #ifdef CONFIG_BCM_KONA_WDT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static int bcm_kona_show(struct seq_file *s, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int ctl_val, cur_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct bcm_kona_wdt *wdt = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (!wdt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		seq_puts(s, "No device pointer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	spin_lock_irqsave(&wdt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	ctl_val = secure_register_read(wdt, SECWDOG_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	cur_val = secure_register_read(wdt, SECWDOG_COUNT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	spin_unlock_irqrestore(&wdt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (ctl_val < 0 || cur_val < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		seq_puts(s, "Error accessing hardware\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		int ctl, cur, ctl_sec, cur_sec, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		ctl = ctl_val & SECWDOG_COUNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		res = (ctl_val & SECWDOG_RES_MASK) >> SECWDOG_CLKS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		cur = cur_val & SECWDOG_COUNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ctl_sec = TICKS_TO_SECS(ctl, wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		cur_sec = TICKS_TO_SECS(cur, wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		seq_printf(s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			   "Resolution: %d / %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			   "Control: %d s / %d (%#x) ticks\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			   "Current: %d s / %d (%#x) ticks\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			   "Busy count: %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			   res, wdt->resolution,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			   ctl_sec, ctl, ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			   cur_sec, cur, cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			   wdt->busy_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) DEFINE_SHOW_ATTRIBUTE(bcm_kona);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void bcm_kona_wdt_debug_init(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (!wdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	wdt->debugfs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	dir = debugfs_create_dir(BCM_KONA_WDT_NAME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	debugfs_create_file("info", S_IFREG | S_IRUGO, dir, wdt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			    &bcm_kona_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	wdt->debugfs = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void bcm_kona_wdt_debug_exit(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (wdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		debugfs_remove_recursive(wdt->debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void bcm_kona_wdt_debug_init(struct platform_device *pdev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void bcm_kona_wdt_debug_exit(struct platform_device *pdev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #endif /* CONFIG_BCM_KONA_WDT_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int bcm_kona_wdt_ctrl_reg_modify(struct bcm_kona_wdt *wdt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					unsigned mask, unsigned newval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	spin_lock_irqsave(&wdt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	val = secure_register_read(wdt, SECWDOG_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (val < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		ret = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		val &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		val |= newval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		writel_relaxed(val, wdt->base + SECWDOG_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	spin_unlock_irqrestore(&wdt->lock, flags);
^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 bcm_kona_wdt_set_resolution_reg(struct bcm_kona_wdt *wdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (wdt->resolution > SECWDOG_MAX_RES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_RES_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 					wdt->resolution << SECWDOG_CLKS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int bcm_kona_wdt_set_timeout_reg(struct watchdog_device *wdog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 					unsigned watchdog_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_COUNT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					SECS_TO_TICKS(wdog->timeout, wdt) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 					watchdog_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int bcm_kona_wdt_set_timeout(struct watchdog_device *wdog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	unsigned int t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	wdog->timeout = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static unsigned int bcm_kona_wdt_get_timeleft(struct watchdog_device *wdog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	spin_lock_irqsave(&wdt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	val = secure_register_read(wdt, SECWDOG_COUNT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	spin_unlock_irqrestore(&wdt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return TICKS_TO_SECS(val & SECWDOG_COUNT_MASK, wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int bcm_kona_wdt_start(struct watchdog_device *wdog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return bcm_kona_wdt_set_timeout_reg(wdog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 					SECWDOG_EN_MASK | SECWDOG_SRSTEN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int bcm_kona_wdt_stop(struct watchdog_device *wdog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_EN_MASK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 					    SECWDOG_SRSTEN_MASK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static const struct watchdog_ops bcm_kona_wdt_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	.owner =	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	.start =	bcm_kona_wdt_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	.stop =		bcm_kona_wdt_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	.set_timeout =	bcm_kona_wdt_set_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	.get_timeleft =	bcm_kona_wdt_get_timeleft,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static const struct watchdog_info bcm_kona_wdt_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	.options =	WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			WDIOF_KEEPALIVEPING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	.identity =	"Broadcom Kona Watchdog Timer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static struct watchdog_device bcm_kona_wdt_wdd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	.info =		&bcm_kona_wdt_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	.ops =		&bcm_kona_wdt_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	.min_timeout =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	.max_timeout =	SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	.timeout =	SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int bcm_kona_wdt_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct bcm_kona_wdt *wdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!wdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	spin_lock_init(&wdt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	wdt->base = devm_platform_ioremap_resource(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (IS_ERR(wdt->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return PTR_ERR(wdt->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	wdt->resolution = SECWDOG_DEFAULT_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	ret = bcm_kona_wdt_set_resolution_reg(wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		dev_err(dev, "Failed to set resolution (error: %d)", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	platform_set_drvdata(pdev, wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	watchdog_set_drvdata(&bcm_kona_wdt_wdd, wdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	bcm_kona_wdt_wdd.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	ret = bcm_kona_wdt_set_timeout_reg(&bcm_kona_wdt_wdd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		dev_err(dev, "Failed set watchdog timeout");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return ret;
^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) 	watchdog_stop_on_reboot(&bcm_kona_wdt_wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	watchdog_stop_on_unregister(&bcm_kona_wdt_wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	ret = devm_watchdog_register_device(dev, &bcm_kona_wdt_wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	bcm_kona_wdt_debug_init(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	dev_dbg(dev, "Broadcom Kona Watchdog Timer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int bcm_kona_wdt_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	bcm_kona_wdt_debug_exit(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	dev_dbg(&pdev->dev, "Watchdog driver disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static const struct of_device_id bcm_kona_wdt_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	{ .compatible = "brcm,kona-wdt", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) MODULE_DEVICE_TABLE(of, bcm_kona_wdt_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static struct platform_driver bcm_kona_wdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			.name = BCM_KONA_WDT_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			.of_match_table = bcm_kona_wdt_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		  },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	.probe = bcm_kona_wdt_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	.remove = bcm_kona_wdt_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) module_platform_driver(bcm_kona_wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) MODULE_LICENSE("GPL v2");