rm9k_wdt.c 10 KB

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