sp5100_tco.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * sp5100_tco : TCO timer driver for sp5100 chipsets
  3. *
  4. * (c) Copyright 2009 Google Inc., All Rights Reserved.
  5. *
  6. * Based on i8xx_tco.c:
  7. * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
  8. * Reserved.
  9. * http://www.kernelconcepts.de
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide",
  17. * AMD Publication 45482 "AMD SB800-Series Southbridges Register
  18. * Reference Guide"
  19. */
  20. /*
  21. * Includes, defines, variables, module parameters, ...
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/types.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/init.h>
  30. #include <linux/fs.h>
  31. #include <linux/pci.h>
  32. #include <linux/ioport.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/io.h>
  36. #include "sp5100_tco.h"
  37. /* Module and version information */
  38. #define TCO_VERSION "0.05"
  39. #define TCO_MODULE_NAME "SP5100 TCO timer"
  40. #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
  41. /* internal variables */
  42. static u32 tcobase_phys;
  43. static u32 tco_wdt_fired;
  44. static void __iomem *tcobase;
  45. static unsigned int pm_iobase;
  46. static DEFINE_SPINLOCK(tco_lock); /* Guards the hardware */
  47. static unsigned long timer_alive;
  48. static char tco_expect_close;
  49. static struct pci_dev *sp5100_tco_pci;
  50. /* the watchdog platform device */
  51. static struct platform_device *sp5100_tco_platform_device;
  52. /* module parameters */
  53. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
  54. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  55. module_param(heartbeat, int, 0);
  56. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
  57. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  58. static bool nowayout = WATCHDOG_NOWAYOUT;
  59. module_param(nowayout, bool, 0);
  60. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started."
  61. " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  62. /*
  63. * Some TCO specific functions
  64. */
  65. static void tco_timer_start(void)
  66. {
  67. u32 val;
  68. unsigned long flags;
  69. spin_lock_irqsave(&tco_lock, flags);
  70. val = readl(SP5100_WDT_CONTROL(tcobase));
  71. val |= SP5100_WDT_START_STOP_BIT;
  72. writel(val, SP5100_WDT_CONTROL(tcobase));
  73. spin_unlock_irqrestore(&tco_lock, flags);
  74. }
  75. static void tco_timer_stop(void)
  76. {
  77. u32 val;
  78. unsigned long flags;
  79. spin_lock_irqsave(&tco_lock, flags);
  80. val = readl(SP5100_WDT_CONTROL(tcobase));
  81. val &= ~SP5100_WDT_START_STOP_BIT;
  82. writel(val, SP5100_WDT_CONTROL(tcobase));
  83. spin_unlock_irqrestore(&tco_lock, flags);
  84. }
  85. static void tco_timer_keepalive(void)
  86. {
  87. u32 val;
  88. unsigned long flags;
  89. spin_lock_irqsave(&tco_lock, flags);
  90. val = readl(SP5100_WDT_CONTROL(tcobase));
  91. val |= SP5100_WDT_TRIGGER_BIT;
  92. writel(val, SP5100_WDT_CONTROL(tcobase));
  93. spin_unlock_irqrestore(&tco_lock, flags);
  94. }
  95. static int tco_timer_set_heartbeat(int t)
  96. {
  97. unsigned long flags;
  98. if (t < 0 || t > 0xffff)
  99. return -EINVAL;
  100. /* Write new heartbeat to watchdog */
  101. spin_lock_irqsave(&tco_lock, flags);
  102. writel(t, SP5100_WDT_COUNT(tcobase));
  103. spin_unlock_irqrestore(&tco_lock, flags);
  104. heartbeat = t;
  105. return 0;
  106. }
  107. static void tco_timer_enable(void)
  108. {
  109. int val;
  110. if (sp5100_tco_pci->revision >= 0x40) {
  111. /* For SB800 or later */
  112. /* Set the Watchdog timer resolution to 1 sec */
  113. outb(SB800_PM_WATCHDOG_CONFIG, SB800_IO_PM_INDEX_REG);
  114. val = inb(SB800_IO_PM_DATA_REG);
  115. val |= SB800_PM_WATCHDOG_SECOND_RES;
  116. outb(val, SB800_IO_PM_DATA_REG);
  117. /* Enable watchdog decode bit and watchdog timer */
  118. outb(SB800_PM_WATCHDOG_CONTROL, SB800_IO_PM_INDEX_REG);
  119. val = inb(SB800_IO_PM_DATA_REG);
  120. val |= SB800_PCI_WATCHDOG_DECODE_EN;
  121. val &= ~SB800_PM_WATCHDOG_DISABLE;
  122. outb(val, SB800_IO_PM_DATA_REG);
  123. } else {
  124. /* For SP5100 or SB7x0 */
  125. /* Enable watchdog decode bit */
  126. pci_read_config_dword(sp5100_tco_pci,
  127. SP5100_PCI_WATCHDOG_MISC_REG,
  128. &val);
  129. val |= SP5100_PCI_WATCHDOG_DECODE_EN;
  130. pci_write_config_dword(sp5100_tco_pci,
  131. SP5100_PCI_WATCHDOG_MISC_REG,
  132. val);
  133. /* Enable Watchdog timer and set the resolution to 1 sec */
  134. outb(SP5100_PM_WATCHDOG_CONTROL, SP5100_IO_PM_INDEX_REG);
  135. val = inb(SP5100_IO_PM_DATA_REG);
  136. val |= SP5100_PM_WATCHDOG_SECOND_RES;
  137. val &= ~SP5100_PM_WATCHDOG_DISABLE;
  138. outb(val, SP5100_IO_PM_DATA_REG);
  139. }
  140. }
  141. /*
  142. * /dev/watchdog handling
  143. */
  144. static int sp5100_tco_open(struct inode *inode, struct file *file)
  145. {
  146. /* /dev/watchdog can only be opened once */
  147. if (test_and_set_bit(0, &timer_alive))
  148. return -EBUSY;
  149. /* Reload and activate timer */
  150. tco_timer_start();
  151. tco_timer_keepalive();
  152. return nonseekable_open(inode, file);
  153. }
  154. static int sp5100_tco_release(struct inode *inode, struct file *file)
  155. {
  156. /* Shut off the timer. */
  157. if (tco_expect_close == 42) {
  158. tco_timer_stop();
  159. } else {
  160. pr_crit("Unexpected close, not stopping watchdog!\n");
  161. tco_timer_keepalive();
  162. }
  163. clear_bit(0, &timer_alive);
  164. tco_expect_close = 0;
  165. return 0;
  166. }
  167. static ssize_t sp5100_tco_write(struct file *file, const char __user *data,
  168. size_t len, loff_t *ppos)
  169. {
  170. /* See if we got the magic character 'V' and reload the timer */
  171. if (len) {
  172. if (!nowayout) {
  173. size_t i;
  174. /* note: just in case someone wrote the magic character
  175. * five months ago... */
  176. tco_expect_close = 0;
  177. /* scan to see whether or not we got the magic character
  178. */
  179. for (i = 0; i != len; i++) {
  180. char c;
  181. if (get_user(c, data + i))
  182. return -EFAULT;
  183. if (c == 'V')
  184. tco_expect_close = 42;
  185. }
  186. }
  187. /* someone wrote to us, we should reload the timer */
  188. tco_timer_keepalive();
  189. }
  190. return len;
  191. }
  192. static long sp5100_tco_ioctl(struct file *file, unsigned int cmd,
  193. unsigned long arg)
  194. {
  195. int new_options, retval = -EINVAL;
  196. int new_heartbeat;
  197. void __user *argp = (void __user *)arg;
  198. int __user *p = argp;
  199. static const struct watchdog_info ident = {
  200. .options = WDIOF_SETTIMEOUT |
  201. WDIOF_KEEPALIVEPING |
  202. WDIOF_MAGICCLOSE,
  203. .firmware_version = 0,
  204. .identity = TCO_MODULE_NAME,
  205. };
  206. switch (cmd) {
  207. case WDIOC_GETSUPPORT:
  208. return copy_to_user(argp, &ident,
  209. sizeof(ident)) ? -EFAULT : 0;
  210. case WDIOC_GETSTATUS:
  211. case WDIOC_GETBOOTSTATUS:
  212. return put_user(0, p);
  213. case WDIOC_SETOPTIONS:
  214. if (get_user(new_options, p))
  215. return -EFAULT;
  216. if (new_options & WDIOS_DISABLECARD) {
  217. tco_timer_stop();
  218. retval = 0;
  219. }
  220. if (new_options & WDIOS_ENABLECARD) {
  221. tco_timer_start();
  222. tco_timer_keepalive();
  223. retval = 0;
  224. }
  225. return retval;
  226. case WDIOC_KEEPALIVE:
  227. tco_timer_keepalive();
  228. return 0;
  229. case WDIOC_SETTIMEOUT:
  230. if (get_user(new_heartbeat, p))
  231. return -EFAULT;
  232. if (tco_timer_set_heartbeat(new_heartbeat))
  233. return -EINVAL;
  234. tco_timer_keepalive();
  235. /* Fall through */
  236. case WDIOC_GETTIMEOUT:
  237. return put_user(heartbeat, p);
  238. default:
  239. return -ENOTTY;
  240. }
  241. }
  242. /*
  243. * Kernel Interfaces
  244. */
  245. static const struct file_operations sp5100_tco_fops = {
  246. .owner = THIS_MODULE,
  247. .llseek = no_llseek,
  248. .write = sp5100_tco_write,
  249. .unlocked_ioctl = sp5100_tco_ioctl,
  250. .open = sp5100_tco_open,
  251. .release = sp5100_tco_release,
  252. };
  253. static struct miscdevice sp5100_tco_miscdev = {
  254. .minor = WATCHDOG_MINOR,
  255. .name = "watchdog",
  256. .fops = &sp5100_tco_fops,
  257. };
  258. /*
  259. * Data for PCI driver interface
  260. *
  261. * This data only exists for exporting the supported
  262. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  263. * register a pci_driver, because someone else might
  264. * want to register another driver on the same PCI id.
  265. */
  266. static const struct pci_device_id sp5100_tco_pci_tbl[] = {
  267. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
  268. PCI_ANY_ID, },
  269. { 0, }, /* End of list */
  270. };
  271. MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
  272. /*
  273. * Init & exit routines
  274. */
  275. static unsigned char sp5100_tco_setupdevice(void)
  276. {
  277. struct pci_dev *dev = NULL;
  278. const char *dev_name = NULL;
  279. u32 val;
  280. u32 index_reg, data_reg, base_addr;
  281. /* Match the PCI device */
  282. for_each_pci_dev(dev) {
  283. if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
  284. sp5100_tco_pci = dev;
  285. break;
  286. }
  287. }
  288. if (!sp5100_tco_pci)
  289. return 0;
  290. pr_info("PCI Revision ID: 0x%x\n", sp5100_tco_pci->revision);
  291. /*
  292. * Determine type of southbridge chipset.
  293. */
  294. if (sp5100_tco_pci->revision >= 0x40) {
  295. dev_name = SB800_DEVNAME;
  296. index_reg = SB800_IO_PM_INDEX_REG;
  297. data_reg = SB800_IO_PM_DATA_REG;
  298. base_addr = SB800_PM_WATCHDOG_BASE;
  299. } else {
  300. dev_name = SP5100_DEVNAME;
  301. index_reg = SP5100_IO_PM_INDEX_REG;
  302. data_reg = SP5100_IO_PM_DATA_REG;
  303. base_addr = SP5100_PM_WATCHDOG_BASE;
  304. }
  305. /* Request the IO ports used by this driver */
  306. pm_iobase = SP5100_IO_PM_INDEX_REG;
  307. if (!request_region(pm_iobase, SP5100_PM_IOPORTS_SIZE, dev_name)) {
  308. pr_err("I/O address 0x%04x already in use\n", pm_iobase);
  309. goto exit;
  310. }
  311. /*
  312. * First, Find the watchdog timer MMIO address from indirect I/O.
  313. */
  314. outb(base_addr+3, index_reg);
  315. val = inb(data_reg);
  316. outb(base_addr+2, index_reg);
  317. val = val << 8 | inb(data_reg);
  318. outb(base_addr+1, index_reg);
  319. val = val << 8 | inb(data_reg);
  320. outb(base_addr+0, index_reg);
  321. /* Low three bits of BASE are reserved */
  322. val = val << 8 | (inb(data_reg) & 0xf8);
  323. pr_debug("Got 0x%04x from indirect I/O\n", val);
  324. /* Check MMIO address conflict */
  325. if (request_mem_region_exclusive(val, SP5100_WDT_MEM_MAP_SIZE,
  326. dev_name))
  327. goto setup_wdt;
  328. else
  329. pr_debug("MMIO address 0x%04x already in use\n", val);
  330. /*
  331. * Secondly, Find the watchdog timer MMIO address
  332. * from SBResource_MMIO register.
  333. */
  334. if (sp5100_tco_pci->revision >= 0x40) {
  335. /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */
  336. outb(SB800_PM_ACPI_MMIO_EN+3, SB800_IO_PM_INDEX_REG);
  337. val = inb(SB800_IO_PM_DATA_REG);
  338. outb(SB800_PM_ACPI_MMIO_EN+2, SB800_IO_PM_INDEX_REG);
  339. val = val << 8 | inb(SB800_IO_PM_DATA_REG);
  340. outb(SB800_PM_ACPI_MMIO_EN+1, SB800_IO_PM_INDEX_REG);
  341. val = val << 8 | inb(SB800_IO_PM_DATA_REG);
  342. outb(SB800_PM_ACPI_MMIO_EN+0, SB800_IO_PM_INDEX_REG);
  343. val = val << 8 | inb(SB800_IO_PM_DATA_REG);
  344. } else {
  345. /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */
  346. pci_read_config_dword(sp5100_tco_pci,
  347. SP5100_SB_RESOURCE_MMIO_BASE, &val);
  348. }
  349. /* The SBResource_MMIO is enabled and mapped memory space? */
  350. if ((val & (SB800_ACPI_MMIO_DECODE_EN | SB800_ACPI_MMIO_SEL)) ==
  351. SB800_ACPI_MMIO_DECODE_EN) {
  352. /* Clear unnecessary the low twelve bits */
  353. val &= ~0xFFF;
  354. /* Add the Watchdog Timer offset to base address. */
  355. val += SB800_PM_WDT_MMIO_OFFSET;
  356. /* Check MMIO address conflict */
  357. if (request_mem_region_exclusive(val, SP5100_WDT_MEM_MAP_SIZE,
  358. dev_name)) {
  359. pr_debug("Got 0x%04x from SBResource_MMIO register\n",
  360. val);
  361. goto setup_wdt;
  362. } else
  363. pr_debug("MMIO address 0x%04x already in use\n", val);
  364. } else
  365. pr_debug("SBResource_MMIO is disabled(0x%04x)\n", val);
  366. pr_notice("failed to find MMIO address, giving up.\n");
  367. goto unreg_region;
  368. setup_wdt:
  369. tcobase_phys = val;
  370. tcobase = ioremap(val, SP5100_WDT_MEM_MAP_SIZE);
  371. if (!tcobase) {
  372. pr_err("failed to get tcobase address\n");
  373. goto unreg_mem_region;
  374. }
  375. pr_info("Using 0x%04x for watchdog MMIO address\n", val);
  376. /* Setup the watchdog timer */
  377. tco_timer_enable();
  378. /* Check that the watchdog action is set to reset the system */
  379. val = readl(SP5100_WDT_CONTROL(tcobase));
  380. /*
  381. * Save WatchDogFired status, because WatchDogFired flag is
  382. * cleared here.
  383. */
  384. tco_wdt_fired = val & SP5100_PM_WATCHDOG_FIRED;
  385. val &= ~SP5100_PM_WATCHDOG_ACTION_RESET;
  386. writel(val, SP5100_WDT_CONTROL(tcobase));
  387. /* Set a reasonable heartbeat before we stop the timer */
  388. tco_timer_set_heartbeat(heartbeat);
  389. /*
  390. * Stop the TCO before we change anything so we don't race with
  391. * a zeroed timer.
  392. */
  393. tco_timer_stop();
  394. /* Done */
  395. return 1;
  396. unreg_mem_region:
  397. release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
  398. unreg_region:
  399. release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
  400. exit:
  401. return 0;
  402. }
  403. static int sp5100_tco_init(struct platform_device *dev)
  404. {
  405. int ret;
  406. /*
  407. * Check whether or not the hardware watchdog is there. If found, then
  408. * set it up.
  409. */
  410. if (!sp5100_tco_setupdevice())
  411. return -ENODEV;
  412. /* Check to see if last reboot was due to watchdog timeout */
  413. pr_info("Last reboot was %striggered by watchdog.\n",
  414. tco_wdt_fired ? "" : "not ");
  415. /*
  416. * Check that the heartbeat value is within it's range.
  417. * If not, reset to the default.
  418. */
  419. if (tco_timer_set_heartbeat(heartbeat)) {
  420. heartbeat = WATCHDOG_HEARTBEAT;
  421. tco_timer_set_heartbeat(heartbeat);
  422. }
  423. ret = misc_register(&sp5100_tco_miscdev);
  424. if (ret != 0) {
  425. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  426. WATCHDOG_MINOR, ret);
  427. goto exit;
  428. }
  429. clear_bit(0, &timer_alive);
  430. /* Show module parameters */
  431. pr_info("initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n",
  432. tcobase, heartbeat, nowayout);
  433. return 0;
  434. exit:
  435. iounmap(tcobase);
  436. release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
  437. release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
  438. return ret;
  439. }
  440. static void sp5100_tco_cleanup(void)
  441. {
  442. /* Stop the timer before we leave */
  443. if (!nowayout)
  444. tco_timer_stop();
  445. /* Deregister */
  446. misc_deregister(&sp5100_tco_miscdev);
  447. iounmap(tcobase);
  448. release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
  449. release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
  450. }
  451. static int sp5100_tco_remove(struct platform_device *dev)
  452. {
  453. if (tcobase)
  454. sp5100_tco_cleanup();
  455. return 0;
  456. }
  457. static void sp5100_tco_shutdown(struct platform_device *dev)
  458. {
  459. tco_timer_stop();
  460. }
  461. static struct platform_driver sp5100_tco_driver = {
  462. .probe = sp5100_tco_init,
  463. .remove = sp5100_tco_remove,
  464. .shutdown = sp5100_tco_shutdown,
  465. .driver = {
  466. .name = TCO_MODULE_NAME,
  467. },
  468. };
  469. static int __init sp5100_tco_init_module(void)
  470. {
  471. int err;
  472. pr_info("SP5100/SB800 TCO WatchDog Timer Driver v%s\n", TCO_VERSION);
  473. err = platform_driver_register(&sp5100_tco_driver);
  474. if (err)
  475. return err;
  476. sp5100_tco_platform_device = platform_device_register_simple(
  477. TCO_MODULE_NAME, -1, NULL, 0);
  478. if (IS_ERR(sp5100_tco_platform_device)) {
  479. err = PTR_ERR(sp5100_tco_platform_device);
  480. goto unreg_platform_driver;
  481. }
  482. return 0;
  483. unreg_platform_driver:
  484. platform_driver_unregister(&sp5100_tco_driver);
  485. return err;
  486. }
  487. static void __exit sp5100_tco_cleanup_module(void)
  488. {
  489. platform_device_unregister(sp5100_tco_platform_device);
  490. platform_driver_unregister(&sp5100_tco_driver);
  491. pr_info("SP5100/SB800 TCO Watchdog Module Unloaded\n");
  492. }
  493. module_init(sp5100_tco_init_module);
  494. module_exit(sp5100_tco_cleanup_module);
  495. MODULE_AUTHOR("Priyanka Gupta");
  496. MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
  497. MODULE_LICENSE("GPL");