cpwd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* cpwd.c - driver implementation for hardware watchdog
  3. * timers found on Sun Microsystems CP1400 and CP1500 boards.
  4. *
  5. * This device supports both the generic Linux watchdog
  6. * interface and Solaris-compatible ioctls as best it is
  7. * able.
  8. *
  9. * NOTE: CP1400 systems appear to have a defective intr_mask
  10. * register on the PLD, preventing the disabling of
  11. * timer interrupts. We use a timer to periodically
  12. * reset 'stopped' watchdogs on affected platforms.
  13. *
  14. * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
  15. * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/errno.h>
  22. #include <linux/major.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/ioport.h>
  26. #include <linux/timer.h>
  27. #include <linux/compat.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <linux/io.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/irq.h>
  35. #include <asm/watchdog.h>
  36. #define DRIVER_NAME "cpwd"
  37. #define WD_OBPNAME "watchdog"
  38. #define WD_BADMODEL "SUNW,501-5336"
  39. #define WD_BTIMEOUT (jiffies + (HZ * 1000))
  40. #define WD_BLIMIT 0xFFFF
  41. #define WD0_MINOR 212
  42. #define WD1_MINOR 213
  43. #define WD2_MINOR 214
  44. /* Internal driver definitions. */
  45. #define WD0_ID 0
  46. #define WD1_ID 1
  47. #define WD2_ID 2
  48. #define WD_NUMDEVS 3
  49. #define WD_INTR_OFF 0
  50. #define WD_INTR_ON 1
  51. #define WD_STAT_INIT 0x01 /* Watchdog timer is initialized */
  52. #define WD_STAT_BSTOP 0x02 /* Watchdog timer is brokenstopped */
  53. #define WD_STAT_SVCD 0x04 /* Watchdog interrupt occurred */
  54. /* Register value definitions
  55. */
  56. #define WD0_INTR_MASK 0x01 /* Watchdog device interrupt masks */
  57. #define WD1_INTR_MASK 0x02
  58. #define WD2_INTR_MASK 0x04
  59. #define WD_S_RUNNING 0x01 /* Watchdog device status running */
  60. #define WD_S_EXPIRED 0x02 /* Watchdog device status expired */
  61. struct cpwd {
  62. void __iomem *regs;
  63. spinlock_t lock;
  64. unsigned int irq;
  65. unsigned long timeout;
  66. bool enabled;
  67. bool reboot;
  68. bool broken;
  69. bool initialized;
  70. struct {
  71. struct miscdevice misc;
  72. void __iomem *regs;
  73. u8 intr_mask;
  74. u8 runstatus;
  75. u16 timeout;
  76. } devs[WD_NUMDEVS];
  77. };
  78. static DEFINE_MUTEX(cpwd_mutex);
  79. static struct cpwd *cpwd_device;
  80. /* Sun uses Altera PLD EPF8820ATC144-4
  81. * providing three hardware watchdogs:
  82. *
  83. * 1) RIC - sends an interrupt when triggered
  84. * 2) XIR - asserts XIR_B_RESET when triggered, resets CPU
  85. * 3) POR - asserts POR_B_RESET when triggered, resets CPU, backplane, board
  86. *
  87. *** Timer register block definition (struct wd_timer_regblk)
  88. *
  89. * dcntr and limit registers (halfword access):
  90. * -------------------
  91. * | 15 | ...| 1 | 0 |
  92. * -------------------
  93. * |- counter val -|
  94. * -------------------
  95. * dcntr - Current 16-bit downcounter value.
  96. * When downcounter reaches '0' watchdog expires.
  97. * Reading this register resets downcounter with
  98. * 'limit' value.
  99. * limit - 16-bit countdown value in 1/10th second increments.
  100. * Writing this register begins countdown with input value.
  101. * Reading from this register does not affect counter.
  102. * NOTES: After watchdog reset, dcntr and limit contain '1'
  103. *
  104. * status register (byte access):
  105. * ---------------------------
  106. * | 7 | ... | 2 | 1 | 0 |
  107. * --------------+------------
  108. * |- UNUSED -| EXP | RUN |
  109. * ---------------------------
  110. * status- Bit 0 - Watchdog is running
  111. * Bit 1 - Watchdog has expired
  112. *
  113. *** PLD register block definition (struct wd_pld_regblk)
  114. *
  115. * intr_mask register (byte access):
  116. * ---------------------------------
  117. * | 7 | ... | 3 | 2 | 1 | 0 |
  118. * +-------------+------------------
  119. * |- UNUSED -| WD3 | WD2 | WD1 |
  120. * ---------------------------------
  121. * WD3 - 1 == Interrupt disabled for watchdog 3
  122. * WD2 - 1 == Interrupt disabled for watchdog 2
  123. * WD1 - 1 == Interrupt disabled for watchdog 1
  124. *
  125. * pld_status register (byte access):
  126. * UNKNOWN, MAGICAL MYSTERY REGISTER
  127. *
  128. */
  129. #define WD_TIMER_REGSZ 16
  130. #define WD0_OFF 0
  131. #define WD1_OFF (WD_TIMER_REGSZ * 1)
  132. #define WD2_OFF (WD_TIMER_REGSZ * 2)
  133. #define PLD_OFF (WD_TIMER_REGSZ * 3)
  134. #define WD_DCNTR 0x00
  135. #define WD_LIMIT 0x04
  136. #define WD_STATUS 0x08
  137. #define PLD_IMASK (PLD_OFF + 0x00)
  138. #define PLD_STATUS (PLD_OFF + 0x04)
  139. static struct timer_list cpwd_timer;
  140. static int wd0_timeout;
  141. static int wd1_timeout;
  142. static int wd2_timeout;
  143. module_param(wd0_timeout, int, 0);
  144. MODULE_PARM_DESC(wd0_timeout, "Default watchdog0 timeout in 1/10secs");
  145. module_param(wd1_timeout, int, 0);
  146. MODULE_PARM_DESC(wd1_timeout, "Default watchdog1 timeout in 1/10secs");
  147. module_param(wd2_timeout, int, 0);
  148. MODULE_PARM_DESC(wd2_timeout, "Default watchdog2 timeout in 1/10secs");
  149. MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
  150. MODULE_DESCRIPTION("Hardware watchdog driver for Sun Microsystems CP1400/1500");
  151. MODULE_LICENSE("GPL");
  152. MODULE_SUPPORTED_DEVICE("watchdog");
  153. static void cpwd_writew(u16 val, void __iomem *addr)
  154. {
  155. writew(cpu_to_le16(val), addr);
  156. }
  157. static u16 cpwd_readw(void __iomem *addr)
  158. {
  159. u16 val = readw(addr);
  160. return le16_to_cpu(val);
  161. }
  162. static void cpwd_writeb(u8 val, void __iomem *addr)
  163. {
  164. writeb(val, addr);
  165. }
  166. static u8 cpwd_readb(void __iomem *addr)
  167. {
  168. return readb(addr);
  169. }
  170. /* Enable or disable watchdog interrupts
  171. * Because of the CP1400 defect this should only be
  172. * called during initialzation or by wd_[start|stop]timer()
  173. *
  174. * index - sub-device index, or -1 for 'all'
  175. * enable - non-zero to enable interrupts, zero to disable
  176. */
  177. static void cpwd_toggleintr(struct cpwd *p, int index, int enable)
  178. {
  179. unsigned char curregs = cpwd_readb(p->regs + PLD_IMASK);
  180. unsigned char setregs =
  181. (index == -1) ?
  182. (WD0_INTR_MASK | WD1_INTR_MASK | WD2_INTR_MASK) :
  183. (p->devs[index].intr_mask);
  184. if (enable == WD_INTR_ON)
  185. curregs &= ~setregs;
  186. else
  187. curregs |= setregs;
  188. cpwd_writeb(curregs, p->regs + PLD_IMASK);
  189. }
  190. /* Restarts timer with maximum limit value and
  191. * does not unset 'brokenstop' value.
  192. */
  193. static void cpwd_resetbrokentimer(struct cpwd *p, int index)
  194. {
  195. cpwd_toggleintr(p, index, WD_INTR_ON);
  196. cpwd_writew(WD_BLIMIT, p->devs[index].regs + WD_LIMIT);
  197. }
  198. /* Timer method called to reset stopped watchdogs--
  199. * because of the PLD bug on CP1400, we cannot mask
  200. * interrupts within the PLD so me must continually
  201. * reset the timers ad infinitum.
  202. */
  203. static void cpwd_brokentimer(struct timer_list *unused)
  204. {
  205. struct cpwd *p = cpwd_device;
  206. int id, tripped = 0;
  207. /* kill a running timer instance, in case we
  208. * were called directly instead of by kernel timer
  209. */
  210. if (timer_pending(&cpwd_timer))
  211. del_timer(&cpwd_timer);
  212. for (id = 0; id < WD_NUMDEVS; id++) {
  213. if (p->devs[id].runstatus & WD_STAT_BSTOP) {
  214. ++tripped;
  215. cpwd_resetbrokentimer(p, id);
  216. }
  217. }
  218. if (tripped) {
  219. /* there is at least one timer brokenstopped-- reschedule */
  220. cpwd_timer.expires = WD_BTIMEOUT;
  221. add_timer(&cpwd_timer);
  222. }
  223. }
  224. /* Reset countdown timer with 'limit' value and continue countdown.
  225. * This will not start a stopped timer.
  226. */
  227. static void cpwd_pingtimer(struct cpwd *p, int index)
  228. {
  229. if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING)
  230. cpwd_readw(p->devs[index].regs + WD_DCNTR);
  231. }
  232. /* Stop a running watchdog timer-- the timer actually keeps
  233. * running, but the interrupt is masked so that no action is
  234. * taken upon expiration.
  235. */
  236. static void cpwd_stoptimer(struct cpwd *p, int index)
  237. {
  238. if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING) {
  239. cpwd_toggleintr(p, index, WD_INTR_OFF);
  240. if (p->broken) {
  241. p->devs[index].runstatus |= WD_STAT_BSTOP;
  242. cpwd_brokentimer(NULL);
  243. }
  244. }
  245. }
  246. /* Start a watchdog timer with the specified limit value
  247. * If the watchdog is running, it will be restarted with
  248. * the provided limit value.
  249. *
  250. * This function will enable interrupts on the specified
  251. * watchdog.
  252. */
  253. static void cpwd_starttimer(struct cpwd *p, int index)
  254. {
  255. if (p->broken)
  256. p->devs[index].runstatus &= ~WD_STAT_BSTOP;
  257. p->devs[index].runstatus &= ~WD_STAT_SVCD;
  258. cpwd_writew(p->devs[index].timeout, p->devs[index].regs + WD_LIMIT);
  259. cpwd_toggleintr(p, index, WD_INTR_ON);
  260. }
  261. static int cpwd_getstatus(struct cpwd *p, int index)
  262. {
  263. unsigned char stat = cpwd_readb(p->devs[index].regs + WD_STATUS);
  264. unsigned char intr = cpwd_readb(p->devs[index].regs + PLD_IMASK);
  265. unsigned char ret = WD_STOPPED;
  266. /* determine STOPPED */
  267. if (!stat)
  268. return ret;
  269. /* determine EXPIRED vs FREERUN vs RUNNING */
  270. else if (WD_S_EXPIRED & stat) {
  271. ret = WD_EXPIRED;
  272. } else if (WD_S_RUNNING & stat) {
  273. if (intr & p->devs[index].intr_mask) {
  274. ret = WD_FREERUN;
  275. } else {
  276. /* Fudge WD_EXPIRED status for defective CP1400--
  277. * IF timer is running
  278. * AND brokenstop is set
  279. * AND an interrupt has been serviced
  280. * we are WD_EXPIRED.
  281. *
  282. * IF timer is running
  283. * AND brokenstop is set
  284. * AND no interrupt has been serviced
  285. * we are WD_FREERUN.
  286. */
  287. if (p->broken &&
  288. (p->devs[index].runstatus & WD_STAT_BSTOP)) {
  289. if (p->devs[index].runstatus & WD_STAT_SVCD) {
  290. ret = WD_EXPIRED;
  291. } else {
  292. /* we could as well pretend
  293. * we are expired */
  294. ret = WD_FREERUN;
  295. }
  296. } else {
  297. ret = WD_RUNNING;
  298. }
  299. }
  300. }
  301. /* determine SERVICED */
  302. if (p->devs[index].runstatus & WD_STAT_SVCD)
  303. ret |= WD_SERVICED;
  304. return ret;
  305. }
  306. static irqreturn_t cpwd_interrupt(int irq, void *dev_id)
  307. {
  308. struct cpwd *p = dev_id;
  309. /* Only WD0 will interrupt-- others are NMI and we won't
  310. * see them here....
  311. */
  312. spin_lock_irq(&p->lock);
  313. cpwd_stoptimer(p, WD0_ID);
  314. p->devs[WD0_ID].runstatus |= WD_STAT_SVCD;
  315. spin_unlock_irq(&p->lock);
  316. return IRQ_HANDLED;
  317. }
  318. static int cpwd_open(struct inode *inode, struct file *f)
  319. {
  320. struct cpwd *p = cpwd_device;
  321. mutex_lock(&cpwd_mutex);
  322. switch (iminor(inode)) {
  323. case WD0_MINOR:
  324. case WD1_MINOR:
  325. case WD2_MINOR:
  326. break;
  327. default:
  328. mutex_unlock(&cpwd_mutex);
  329. return -ENODEV;
  330. }
  331. /* Register IRQ on first open of device */
  332. if (!p->initialized) {
  333. if (request_irq(p->irq, &cpwd_interrupt,
  334. IRQF_SHARED, DRIVER_NAME, p)) {
  335. pr_err("Cannot register IRQ %d\n", p->irq);
  336. mutex_unlock(&cpwd_mutex);
  337. return -EBUSY;
  338. }
  339. p->initialized = true;
  340. }
  341. mutex_unlock(&cpwd_mutex);
  342. return stream_open(inode, f);
  343. }
  344. static int cpwd_release(struct inode *inode, struct file *file)
  345. {
  346. return 0;
  347. }
  348. static long cpwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  349. {
  350. static const struct watchdog_info info = {
  351. .options = WDIOF_SETTIMEOUT,
  352. .firmware_version = 1,
  353. .identity = DRIVER_NAME,
  354. };
  355. void __user *argp = (void __user *)arg;
  356. struct inode *inode = file_inode(file);
  357. int index = iminor(inode) - WD0_MINOR;
  358. struct cpwd *p = cpwd_device;
  359. int setopt = 0;
  360. switch (cmd) {
  361. /* Generic Linux IOCTLs */
  362. case WDIOC_GETSUPPORT:
  363. if (copy_to_user(argp, &info, sizeof(struct watchdog_info)))
  364. return -EFAULT;
  365. break;
  366. case WDIOC_GETSTATUS:
  367. case WDIOC_GETBOOTSTATUS:
  368. if (put_user(0, (int __user *)argp))
  369. return -EFAULT;
  370. break;
  371. case WDIOC_KEEPALIVE:
  372. cpwd_pingtimer(p, index);
  373. break;
  374. case WDIOC_SETOPTIONS:
  375. if (copy_from_user(&setopt, argp, sizeof(unsigned int)))
  376. return -EFAULT;
  377. if (setopt & WDIOS_DISABLECARD) {
  378. if (p->enabled)
  379. return -EINVAL;
  380. cpwd_stoptimer(p, index);
  381. } else if (setopt & WDIOS_ENABLECARD) {
  382. cpwd_starttimer(p, index);
  383. } else {
  384. return -EINVAL;
  385. }
  386. break;
  387. /* Solaris-compatible IOCTLs */
  388. case WIOCGSTAT:
  389. setopt = cpwd_getstatus(p, index);
  390. if (copy_to_user(argp, &setopt, sizeof(unsigned int)))
  391. return -EFAULT;
  392. break;
  393. case WIOCSTART:
  394. cpwd_starttimer(p, index);
  395. break;
  396. case WIOCSTOP:
  397. if (p->enabled)
  398. return -EINVAL;
  399. cpwd_stoptimer(p, index);
  400. break;
  401. default:
  402. return -EINVAL;
  403. }
  404. return 0;
  405. }
  406. static long cpwd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  407. {
  408. return cpwd_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  409. }
  410. static ssize_t cpwd_write(struct file *file, const char __user *buf,
  411. size_t count, loff_t *ppos)
  412. {
  413. struct inode *inode = file_inode(file);
  414. struct cpwd *p = cpwd_device;
  415. int index = iminor(inode);
  416. if (count) {
  417. cpwd_pingtimer(p, index);
  418. return 1;
  419. }
  420. return 0;
  421. }
  422. static ssize_t cpwd_read(struct file *file, char __user *buffer,
  423. size_t count, loff_t *ppos)
  424. {
  425. return -EINVAL;
  426. }
  427. static const struct file_operations cpwd_fops = {
  428. .owner = THIS_MODULE,
  429. .unlocked_ioctl = cpwd_ioctl,
  430. .compat_ioctl = cpwd_compat_ioctl,
  431. .open = cpwd_open,
  432. .write = cpwd_write,
  433. .read = cpwd_read,
  434. .release = cpwd_release,
  435. .llseek = no_llseek,
  436. };
  437. static int cpwd_probe(struct platform_device *op)
  438. {
  439. struct device_node *options;
  440. const char *str_prop;
  441. const void *prop_val;
  442. int i, err = -EINVAL;
  443. struct cpwd *p;
  444. if (cpwd_device)
  445. return -EINVAL;
  446. p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
  447. if (!p)
  448. return -ENOMEM;
  449. p->irq = op->archdata.irqs[0];
  450. spin_lock_init(&p->lock);
  451. p->regs = of_ioremap(&op->resource[0], 0,
  452. 4 * WD_TIMER_REGSZ, DRIVER_NAME);
  453. if (!p->regs) {
  454. pr_err("Unable to map registers\n");
  455. return -ENOMEM;
  456. }
  457. options = of_find_node_by_path("/options");
  458. if (!options) {
  459. err = -ENODEV;
  460. pr_err("Unable to find /options node\n");
  461. goto out_iounmap;
  462. }
  463. prop_val = of_get_property(options, "watchdog-enable?", NULL);
  464. p->enabled = (prop_val ? true : false);
  465. prop_val = of_get_property(options, "watchdog-reboot?", NULL);
  466. p->reboot = (prop_val ? true : false);
  467. str_prop = of_get_property(options, "watchdog-timeout", NULL);
  468. if (str_prop)
  469. p->timeout = simple_strtoul(str_prop, NULL, 10);
  470. of_node_put(options);
  471. /* CP1400s seem to have broken PLD implementations-- the
  472. * interrupt_mask register cannot be written, so no timer
  473. * interrupts can be masked within the PLD.
  474. */
  475. str_prop = of_get_property(op->dev.of_node, "model", NULL);
  476. p->broken = (str_prop && !strcmp(str_prop, WD_BADMODEL));
  477. if (!p->enabled)
  478. cpwd_toggleintr(p, -1, WD_INTR_OFF);
  479. for (i = 0; i < WD_NUMDEVS; i++) {
  480. static const char *cpwd_names[] = { "RIC", "XIR", "POR" };
  481. static int *parms[] = { &wd0_timeout,
  482. &wd1_timeout,
  483. &wd2_timeout };
  484. struct miscdevice *mp = &p->devs[i].misc;
  485. mp->minor = WD0_MINOR + i;
  486. mp->name = cpwd_names[i];
  487. mp->fops = &cpwd_fops;
  488. p->devs[i].regs = p->regs + (i * WD_TIMER_REGSZ);
  489. p->devs[i].intr_mask = (WD0_INTR_MASK << i);
  490. p->devs[i].runstatus &= ~WD_STAT_BSTOP;
  491. p->devs[i].runstatus |= WD_STAT_INIT;
  492. p->devs[i].timeout = p->timeout;
  493. if (*parms[i])
  494. p->devs[i].timeout = *parms[i];
  495. err = misc_register(&p->devs[i].misc);
  496. if (err) {
  497. pr_err("Could not register misc device for dev %d\n",
  498. i);
  499. goto out_unregister;
  500. }
  501. }
  502. if (p->broken) {
  503. timer_setup(&cpwd_timer, cpwd_brokentimer, 0);
  504. cpwd_timer.expires = WD_BTIMEOUT;
  505. pr_info("PLD defect workaround enabled for model %s\n",
  506. WD_BADMODEL);
  507. }
  508. platform_set_drvdata(op, p);
  509. cpwd_device = p;
  510. return 0;
  511. out_unregister:
  512. for (i--; i >= 0; i--)
  513. misc_deregister(&p->devs[i].misc);
  514. out_iounmap:
  515. of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
  516. return err;
  517. }
  518. static int cpwd_remove(struct platform_device *op)
  519. {
  520. struct cpwd *p = platform_get_drvdata(op);
  521. int i;
  522. for (i = 0; i < WD_NUMDEVS; i++) {
  523. misc_deregister(&p->devs[i].misc);
  524. if (!p->enabled) {
  525. cpwd_stoptimer(p, i);
  526. if (p->devs[i].runstatus & WD_STAT_BSTOP)
  527. cpwd_resetbrokentimer(p, i);
  528. }
  529. }
  530. if (p->broken)
  531. del_timer_sync(&cpwd_timer);
  532. if (p->initialized)
  533. free_irq(p->irq, p);
  534. of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
  535. cpwd_device = NULL;
  536. return 0;
  537. }
  538. static const struct of_device_id cpwd_match[] = {
  539. {
  540. .name = "watchdog",
  541. },
  542. {},
  543. };
  544. MODULE_DEVICE_TABLE(of, cpwd_match);
  545. static struct platform_driver cpwd_driver = {
  546. .driver = {
  547. .name = DRIVER_NAME,
  548. .of_match_table = cpwd_match,
  549. },
  550. .probe = cpwd_probe,
  551. .remove = cpwd_remove,
  552. };
  553. module_platform_driver(cpwd_driver);