rm9k_wdt.c 10 KB

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