rm9k_wdt.c 10 KB

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