rm9k_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Watchdog implementation for GPI h/w found on PMC-Sierra RM9xxx
  3. * chips.
  4. *
  5. * Copyright (C) 2004 by Basler Vision Technologies AG
  6. * Author: Thomas Koeller <thomas.koeller@baslerweb.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/fs.h>
  27. #include <linux/reboot.h>
  28. #include <linux/notifier.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/io.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/atomic.h>
  34. #include <asm/processor.h>
  35. #include <asm/system.h>
  36. #include <asm/rm9k-ocd.h>
  37. #include <rm9k_wdt.h>
  38. #define CLOCK 125000000
  39. #define MAX_TIMEOUT_SECONDS 32
  40. #define CPCCR 0x0080
  41. #define CPGIG1SR 0x0044
  42. #define CPGIG1ER 0x0054
  43. /* Function prototypes */
  44. static irqreturn_t wdt_gpi_irqhdl(int, void *);
  45. static void wdt_gpi_start(void);
  46. static void wdt_gpi_stop(void);
  47. static void wdt_gpi_set_timeout(unsigned int);
  48. static int wdt_gpi_open(struct inode *, struct file *);
  49. static int wdt_gpi_release(struct inode *, struct file *);
  50. static ssize_t wdt_gpi_write(struct file *, const char __user *, size_t,
  51. loff_t *);
  52. static long wdt_gpi_ioctl(struct file *, unsigned int, unsigned long);
  53. static int wdt_gpi_notify(struct notifier_block *, unsigned long, void *);
  54. static const struct resource *wdt_gpi_get_resource(struct platform_device *,
  55. const char *, unsigned int);
  56. static int __init wdt_gpi_probe(struct device *);
  57. static int __exit wdt_gpi_remove(struct device *);
  58. static const char wdt_gpi_name[] = "wdt_gpi";
  59. static atomic_t opencnt;
  60. static int expect_close;
  61. static int locked;
  62. /* These are set from device resources */
  63. static void __iomem *wd_regs;
  64. static unsigned int wd_irq, wd_ctr;
  65. /* Module arguments */
  66. static int timeout = MAX_TIMEOUT_SECONDS;
  67. module_param(timeout, int, 0444);
  68. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
  69. static unsigned long resetaddr = 0xbffdc200;
  70. module_param(resetaddr, ulong, 0444);
  71. MODULE_PARM_DESC(resetaddr, "Address to write to to force a reset");
  72. static unsigned long flagaddr = 0xbffdc104;
  73. module_param(flagaddr, ulong, 0444);
  74. MODULE_PARM_DESC(flagaddr, "Address to write to boot flags to");
  75. static int powercycle;
  76. module_param(powercycle, bool, 0444);
  77. MODULE_PARM_DESC(powercycle, "Cycle power if watchdog expires");
  78. static int nowayout = WATCHDOG_NOWAYOUT;
  79. module_param(nowayout, bool, 0444);
  80. MODULE_PARM_DESC(nowayout, "Watchdog cannot be disabled once started");
  81. /* Kernel interfaces */
  82. static const struct file_operations fops = {
  83. .owner = THIS_MODULE,
  84. .open = wdt_gpi_open,
  85. .release = wdt_gpi_release,
  86. .write = wdt_gpi_write,
  87. .unlocked_ioctl = wdt_gpi_ioctl,
  88. };
  89. static struct miscdevice miscdev = {
  90. .minor = WATCHDOG_MINOR,
  91. .name = wdt_gpi_name,
  92. .fops = &fops,
  93. };
  94. static struct notifier_block wdt_gpi_shutdown = {
  95. .notifier_call = wdt_gpi_notify,
  96. };
  97. /* Interrupt handler */
  98. static irqreturn_t wdt_gpi_irqhdl(int irq, void *ctxt)
  99. {
  100. if (!unlikely(__raw_readl(wd_regs + 0x0008) & 0x1))
  101. return IRQ_NONE;
  102. __raw_writel(0x1, wd_regs + 0x0008);
  103. printk(KERN_CRIT "%s: watchdog expired - resetting system\n",
  104. wdt_gpi_name);
  105. *(volatile char *) flagaddr |= 0x01;
  106. *(volatile char *) resetaddr = powercycle ? 0x01 : 0x2;
  107. iob();
  108. while (1)
  109. cpu_relax();
  110. }
  111. /* Watchdog functions */
  112. static void wdt_gpi_start(void)
  113. {
  114. u32 reg;
  115. lock_titan_regs();
  116. reg = titan_readl(CPGIG1ER);
  117. titan_writel(reg | (0x100 << wd_ctr), CPGIG1ER);
  118. iob();
  119. unlock_titan_regs();
  120. }
  121. static void wdt_gpi_stop(void)
  122. {
  123. u32 reg;
  124. lock_titan_regs();
  125. reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
  126. titan_writel(reg, CPCCR);
  127. reg = titan_readl(CPGIG1ER);
  128. titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
  129. iob();
  130. unlock_titan_regs();
  131. }
  132. static void wdt_gpi_set_timeout(unsigned int to)
  133. {
  134. u32 reg;
  135. const u32 wdval = (to * CLOCK) & ~0x0000000f;
  136. lock_titan_regs();
  137. reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
  138. titan_writel(reg, CPCCR);
  139. wmb();
  140. __raw_writel(wdval, wd_regs + 0x0000);
  141. wmb();
  142. titan_writel(reg | (0x2 << (wd_ctr * 4)), CPCCR);
  143. wmb();
  144. titan_writel(reg | (0x5 << (wd_ctr * 4)), CPCCR);
  145. iob();
  146. unlock_titan_regs();
  147. }
  148. /* /dev/watchdog operations */
  149. static int wdt_gpi_open(struct inode *inode, struct file *file)
  150. {
  151. int res;
  152. if (unlikely(atomic_dec_if_positive(&opencnt) < 0))
  153. return -EBUSY;
  154. expect_close = 0;
  155. if (locked) {
  156. module_put(THIS_MODULE);
  157. free_irq(wd_irq, &miscdev);
  158. locked = 0;
  159. }
  160. res = request_irq(wd_irq, wdt_gpi_irqhdl, IRQF_SHARED | IRQF_DISABLED,
  161. wdt_gpi_name, &miscdev);
  162. if (unlikely(res))
  163. return res;
  164. wdt_gpi_set_timeout(timeout);
  165. wdt_gpi_start();
  166. printk(KERN_INFO "%s: watchdog started, timeout = %u seconds\n",
  167. wdt_gpi_name, timeout);
  168. return nonseekable_open(inode, file);
  169. }
  170. static int wdt_gpi_release(struct inode *inode, struct file *file)
  171. {
  172. if (nowayout) {
  173. printk(KERN_INFO "%s: no way out - watchdog left running\n",
  174. wdt_gpi_name);
  175. __module_get(THIS_MODULE);
  176. locked = 1;
  177. } else {
  178. if (expect_close) {
  179. wdt_gpi_stop();
  180. free_irq(wd_irq, &miscdev);
  181. printk(KERN_INFO "%s: watchdog stopped\n",
  182. wdt_gpi_name);
  183. } else {
  184. printk(KERN_CRIT "%s: unexpected close() -"
  185. " watchdog left running\n",
  186. wdt_gpi_name);
  187. wdt_gpi_set_timeout(timeout);
  188. __module_get(THIS_MODULE);
  189. locked = 1;
  190. }
  191. }
  192. atomic_inc(&opencnt);
  193. return 0;
  194. }
  195. static ssize_t wdt_gpi_write(struct file *f, const char __user *d, size_t s,
  196. loff_t *o)
  197. {
  198. char val;
  199. wdt_gpi_set_timeout(timeout);
  200. expect_close = (s > 0) && !get_user(val, d) && (val == 'V');
  201. return s ? 1 : 0;
  202. }
  203. static long wdt_gpi_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  204. {
  205. long res = -ENOTTY;
  206. const long size = _IOC_SIZE(cmd);
  207. int stat;
  208. void __user *argp = (void __user *)arg;
  209. static struct watchdog_info wdinfo = {
  210. .identity = "RM9xxx/GPI watchdog",
  211. .firmware_version = 0,
  212. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
  213. };
  214. if (unlikely(_IOC_TYPE(cmd) != WATCHDOG_IOCTL_BASE))
  215. return -ENOTTY;
  216. if ((_IOC_DIR(cmd) & _IOC_READ)
  217. && !access_ok(VERIFY_WRITE, arg, size))
  218. return -EFAULT;
  219. if ((_IOC_DIR(cmd) & _IOC_WRITE)
  220. && !access_ok(VERIFY_READ, arg, size))
  221. return -EFAULT;
  222. expect_close = 0;
  223. switch (cmd) {
  224. case WDIOC_GETSUPPORT:
  225. wdinfo.options = nowayout ?
  226. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING :
  227. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  228. WDIOF_MAGICCLOSE;
  229. res = __copy_to_user(argp, &wdinfo, size) ? -EFAULT : size;
  230. break;
  231. case WDIOC_GETSTATUS:
  232. break;
  233. case WDIOC_GETBOOTSTATUS:
  234. stat = (*(volatile char *) flagaddr & 0x01)
  235. ? WDIOF_CARDRESET : 0;
  236. res = __copy_to_user(argp, &stat, size) ?
  237. -EFAULT : size;
  238. break;
  239. case WDIOC_SETOPTIONS:
  240. break;
  241. case WDIOC_KEEPALIVE:
  242. wdt_gpi_set_timeout(timeout);
  243. res = size;
  244. break;
  245. case WDIOC_SETTIMEOUT:
  246. {
  247. int val;
  248. if (unlikely(__copy_from_user(&val, argp, size))) {
  249. res = -EFAULT;
  250. break;
  251. }
  252. if (val > MAX_TIMEOUT_SECONDS)
  253. val = MAX_TIMEOUT_SECONDS;
  254. timeout = val;
  255. wdt_gpi_set_timeout(val);
  256. res = size;
  257. printk(KERN_INFO "%s: timeout set to %u seconds\n",
  258. wdt_gpi_name, timeout);
  259. }
  260. break;
  261. case WDIOC_GETTIMEOUT:
  262. res = __copy_to_user(argp, &timeout, size) ?
  263. -EFAULT : size;
  264. break;
  265. }
  266. return res;
  267. }
  268. /* Shutdown notifier */
  269. static int wdt_gpi_notify(struct notifier_block *this, unsigned long code,
  270. void *unused)
  271. {
  272. if (code == SYS_DOWN || code == SYS_HALT)
  273. wdt_gpi_stop();
  274. return NOTIFY_DONE;
  275. }
  276. /* Init & exit procedures */
  277. static const struct resource *wdt_gpi_get_resource(struct platform_device *pdv,
  278. const char *name, unsigned int type)
  279. {
  280. char buf[80];
  281. if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
  282. return NULL;
  283. return platform_get_resource_byname(pdv, type, buf);
  284. }
  285. /* No hotplugging on the platform bus - use __init */
  286. static int __init wdt_gpi_probe(struct device *dev)
  287. {
  288. int res;
  289. struct platform_device * const pdv = to_platform_device(dev);
  290. const struct resource
  291. * const rr = wdt_gpi_get_resource(pdv, WDT_RESOURCE_REGS,
  292. IORESOURCE_MEM),
  293. * const ri = wdt_gpi_get_resource(pdv, WDT_RESOURCE_IRQ,
  294. IORESOURCE_IRQ),
  295. * const rc = wdt_gpi_get_resource(pdv, WDT_RESOURCE_COUNTER,
  296. 0);
  297. if (unlikely(!rr || !ri || !rc))
  298. return -ENXIO;
  299. wd_regs = ioremap_nocache(rr->start, rr->end + 1 - rr->start);
  300. if (unlikely(!wd_regs))
  301. return -ENOMEM;
  302. wd_irq = ri->start;
  303. wd_ctr = rc->start;
  304. res = misc_register(&miscdev);
  305. if (res)
  306. iounmap(wd_regs);
  307. else
  308. register_reboot_notifier(&wdt_gpi_shutdown);
  309. return res;
  310. }
  311. static int __exit wdt_gpi_remove(struct device *dev)
  312. {
  313. int res;
  314. unregister_reboot_notifier(&wdt_gpi_shutdown);
  315. res = misc_deregister(&miscdev);
  316. iounmap(wd_regs);
  317. wd_regs = NULL;
  318. return res;
  319. }
  320. /* Device driver init & exit */
  321. static struct device_driver wdt_gpi_driver = {
  322. .name = (char *) wdt_gpi_name,
  323. .bus = &platform_bus_type,
  324. .owner = THIS_MODULE,
  325. .probe = wdt_gpi_probe,
  326. .remove = __exit_p(wdt_gpi_remove),
  327. .shutdown = NULL,
  328. .suspend = NULL,
  329. .resume = NULL,
  330. };
  331. static int __init wdt_gpi_init_module(void)
  332. {
  333. atomic_set(&opencnt, 1);
  334. if (timeout > MAX_TIMEOUT_SECONDS)
  335. timeout = MAX_TIMEOUT_SECONDS;
  336. return driver_register(&wdt_gpi_driver);
  337. }
  338. static void __exit wdt_gpi_cleanup_module(void)
  339. {
  340. driver_unregister(&wdt_gpi_driver);
  341. }
  342. module_init(wdt_gpi_init_module);
  343. module_exit(wdt_gpi_cleanup_module);
  344. MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
  345. MODULE_DESCRIPTION("Basler eXcite watchdog driver for gpi devices");
  346. MODULE_VERSION("0.1");
  347. MODULE_LICENSE("GPL");
  348. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);