sp5100_tco.c 15 KB

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