sp5100_tco.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. * AMD Publication 48751 "BIOS and Kernel Developer’s Guide (BKDG)
  20. * for AMD Family 16h Models 00h-0Fh Processors"
  21. * AMD Publication 51192 "AMD Bolton FCH Register Reference Guide"
  22. * AMD Publication 52740 "BIOS and Kernel Developer’s Guide (BKDG)
  23. * for AMD Family 16h Models 30h-3Fh Processors"
  24. */
  25. /*
  26. * Includes, defines, variables, module parameters, ...
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/init.h>
  30. #include <linux/io.h>
  31. #include <linux/ioport.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/pci.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/types.h>
  37. #include <linux/watchdog.h>
  38. #include "sp5100_tco.h"
  39. #define TCO_DRIVER_NAME "sp5100-tco"
  40. /* internal variables */
  41. enum tco_reg_layout {
  42. sp5100, sb800, efch
  43. };
  44. struct sp5100_tco {
  45. struct watchdog_device wdd;
  46. void __iomem *tcobase;
  47. enum tco_reg_layout tco_reg_layout;
  48. };
  49. /* the watchdog platform device */
  50. static struct platform_device *sp5100_tco_platform_device;
  51. /* the associated PCI device */
  52. static struct pci_dev *sp5100_tco_pci;
  53. /* module parameters */
  54. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
  55. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  56. module_param(heartbeat, int, 0);
  57. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
  58. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  59. static bool nowayout = WATCHDOG_NOWAYOUT;
  60. module_param(nowayout, bool, 0);
  61. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started."
  62. " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  63. /*
  64. * Some TCO specific functions
  65. */
  66. static enum tco_reg_layout tco_reg_layout(struct pci_dev *dev)
  67. {
  68. if (dev->vendor == PCI_VENDOR_ID_ATI &&
  69. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
  70. dev->revision < 0x40) {
  71. return sp5100;
  72. } else if (dev->vendor == PCI_VENDOR_ID_AMD &&
  73. ((dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
  74. dev->revision >= 0x41) ||
  75. (dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
  76. dev->revision >= 0x49))) {
  77. return efch;
  78. }
  79. return sb800;
  80. }
  81. static int tco_timer_start(struct watchdog_device *wdd)
  82. {
  83. struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
  84. u32 val;
  85. val = readl(SP5100_WDT_CONTROL(tco->tcobase));
  86. val |= SP5100_WDT_START_STOP_BIT;
  87. writel(val, SP5100_WDT_CONTROL(tco->tcobase));
  88. return 0;
  89. }
  90. static int tco_timer_stop(struct watchdog_device *wdd)
  91. {
  92. struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
  93. u32 val;
  94. val = readl(SP5100_WDT_CONTROL(tco->tcobase));
  95. val &= ~SP5100_WDT_START_STOP_BIT;
  96. writel(val, SP5100_WDT_CONTROL(tco->tcobase));
  97. return 0;
  98. }
  99. static int tco_timer_ping(struct watchdog_device *wdd)
  100. {
  101. struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
  102. u32 val;
  103. val = readl(SP5100_WDT_CONTROL(tco->tcobase));
  104. val |= SP5100_WDT_TRIGGER_BIT;
  105. writel(val, SP5100_WDT_CONTROL(tco->tcobase));
  106. return 0;
  107. }
  108. static int tco_timer_set_timeout(struct watchdog_device *wdd,
  109. unsigned int t)
  110. {
  111. struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
  112. /* Write new heartbeat to watchdog */
  113. writel(t, SP5100_WDT_COUNT(tco->tcobase));
  114. wdd->timeout = t;
  115. return 0;
  116. }
  117. static u8 sp5100_tco_read_pm_reg8(u8 index)
  118. {
  119. outb(index, SP5100_IO_PM_INDEX_REG);
  120. return inb(SP5100_IO_PM_DATA_REG);
  121. }
  122. static void sp5100_tco_update_pm_reg8(u8 index, u8 reset, u8 set)
  123. {
  124. u8 val;
  125. outb(index, SP5100_IO_PM_INDEX_REG);
  126. val = inb(SP5100_IO_PM_DATA_REG);
  127. val &= reset;
  128. val |= set;
  129. outb(val, SP5100_IO_PM_DATA_REG);
  130. }
  131. static void tco_timer_enable(struct sp5100_tco *tco)
  132. {
  133. u32 val;
  134. switch (tco->tco_reg_layout) {
  135. case sb800:
  136. /* For SB800 or later */
  137. /* Set the Watchdog timer resolution to 1 sec */
  138. sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG,
  139. 0xff, SB800_PM_WATCHDOG_SECOND_RES);
  140. /* Enable watchdog decode bit and watchdog timer */
  141. sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL,
  142. ~SB800_PM_WATCHDOG_DISABLE,
  143. SB800_PCI_WATCHDOG_DECODE_EN);
  144. break;
  145. case sp5100:
  146. /* For SP5100 or SB7x0 */
  147. /* Enable watchdog decode bit */
  148. pci_read_config_dword(sp5100_tco_pci,
  149. SP5100_PCI_WATCHDOG_MISC_REG,
  150. &val);
  151. val |= SP5100_PCI_WATCHDOG_DECODE_EN;
  152. pci_write_config_dword(sp5100_tco_pci,
  153. SP5100_PCI_WATCHDOG_MISC_REG,
  154. val);
  155. /* Enable Watchdog timer and set the resolution to 1 sec */
  156. sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL,
  157. ~SP5100_PM_WATCHDOG_DISABLE,
  158. SP5100_PM_WATCHDOG_SECOND_RES);
  159. break;
  160. case efch:
  161. /* Set the Watchdog timer resolution to 1 sec and enable */
  162. sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN3,
  163. ~EFCH_PM_WATCHDOG_DISABLE,
  164. EFCH_PM_DECODEEN_SECOND_RES);
  165. break;
  166. }
  167. }
  168. static u32 sp5100_tco_read_pm_reg32(u8 index)
  169. {
  170. u32 val = 0;
  171. int i;
  172. for (i = 3; i >= 0; i--)
  173. val = (val << 8) + sp5100_tco_read_pm_reg8(index + i);
  174. return val;
  175. }
  176. static int sp5100_tco_setupdevice(struct device *dev,
  177. struct watchdog_device *wdd)
  178. {
  179. struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
  180. const char *dev_name;
  181. u32 mmio_addr = 0, val;
  182. int ret;
  183. /* Request the IO ports used by this driver */
  184. if (!request_muxed_region(SP5100_IO_PM_INDEX_REG,
  185. SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) {
  186. dev_err(dev, "I/O address 0x%04x already in use\n",
  187. SP5100_IO_PM_INDEX_REG);
  188. return -EBUSY;
  189. }
  190. /*
  191. * Determine type of southbridge chipset.
  192. */
  193. switch (tco->tco_reg_layout) {
  194. case sp5100:
  195. dev_name = SP5100_DEVNAME;
  196. mmio_addr = sp5100_tco_read_pm_reg32(SP5100_PM_WATCHDOG_BASE) &
  197. 0xfffffff8;
  198. break;
  199. case sb800:
  200. dev_name = SB800_DEVNAME;
  201. mmio_addr = sp5100_tco_read_pm_reg32(SB800_PM_WATCHDOG_BASE) &
  202. 0xfffffff8;
  203. break;
  204. case efch:
  205. dev_name = SB800_DEVNAME;
  206. val = sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN);
  207. if (val & EFCH_PM_DECODEEN_WDT_TMREN)
  208. mmio_addr = EFCH_PM_WDT_ADDR;
  209. break;
  210. default:
  211. return -ENODEV;
  212. }
  213. /* Check MMIO address conflict */
  214. if (!mmio_addr ||
  215. !devm_request_mem_region(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE,
  216. dev_name)) {
  217. if (mmio_addr)
  218. dev_dbg(dev, "MMIO address 0x%08x already in use\n",
  219. mmio_addr);
  220. switch (tco->tco_reg_layout) {
  221. case sp5100:
  222. /*
  223. * Secondly, Find the watchdog timer MMIO address
  224. * from SBResource_MMIO register.
  225. */
  226. /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */
  227. pci_read_config_dword(sp5100_tco_pci,
  228. SP5100_SB_RESOURCE_MMIO_BASE,
  229. &mmio_addr);
  230. if ((mmio_addr & (SB800_ACPI_MMIO_DECODE_EN |
  231. SB800_ACPI_MMIO_SEL)) !=
  232. SB800_ACPI_MMIO_DECODE_EN) {
  233. ret = -ENODEV;
  234. goto unreg_region;
  235. }
  236. mmio_addr &= ~0xFFF;
  237. mmio_addr += SB800_PM_WDT_MMIO_OFFSET;
  238. break;
  239. case sb800:
  240. /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */
  241. mmio_addr =
  242. sp5100_tco_read_pm_reg32(SB800_PM_ACPI_MMIO_EN);
  243. if ((mmio_addr & (SB800_ACPI_MMIO_DECODE_EN |
  244. SB800_ACPI_MMIO_SEL)) !=
  245. SB800_ACPI_MMIO_DECODE_EN) {
  246. ret = -ENODEV;
  247. goto unreg_region;
  248. }
  249. mmio_addr &= ~0xFFF;
  250. mmio_addr += SB800_PM_WDT_MMIO_OFFSET;
  251. break;
  252. case efch:
  253. val = sp5100_tco_read_pm_reg8(EFCH_PM_ISACONTROL);
  254. if (!(val & EFCH_PM_ISACONTROL_MMIOEN)) {
  255. ret = -ENODEV;
  256. goto unreg_region;
  257. }
  258. mmio_addr = EFCH_PM_ACPI_MMIO_ADDR +
  259. EFCH_PM_ACPI_MMIO_WDT_OFFSET;
  260. break;
  261. }
  262. dev_dbg(dev, "Got 0x%08x from SBResource_MMIO register\n",
  263. mmio_addr);
  264. if (!devm_request_mem_region(dev, mmio_addr,
  265. SP5100_WDT_MEM_MAP_SIZE,
  266. dev_name)) {
  267. dev_dbg(dev, "MMIO address 0x%08x already in use\n",
  268. mmio_addr);
  269. ret = -EBUSY;
  270. goto unreg_region;
  271. }
  272. }
  273. tco->tcobase = devm_ioremap(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE);
  274. if (!tco->tcobase) {
  275. dev_err(dev, "failed to get tcobase address\n");
  276. ret = -ENOMEM;
  277. goto unreg_region;
  278. }
  279. dev_info(dev, "Using 0x%08x for watchdog MMIO address\n", mmio_addr);
  280. /* Setup the watchdog timer */
  281. tco_timer_enable(tco);
  282. val = readl(SP5100_WDT_CONTROL(tco->tcobase));
  283. if (val & SP5100_WDT_DISABLED) {
  284. dev_err(dev, "Watchdog hardware is disabled\n");
  285. ret = -ENODEV;
  286. goto unreg_region;
  287. }
  288. /*
  289. * Save WatchDogFired status, because WatchDogFired flag is
  290. * cleared here.
  291. */
  292. if (val & SP5100_WDT_FIRED)
  293. wdd->bootstatus = WDIOF_CARDRESET;
  294. /* Set watchdog action to reset the system */
  295. val &= ~SP5100_WDT_ACTION_RESET;
  296. writel(val, SP5100_WDT_CONTROL(tco->tcobase));
  297. /* Set a reasonable heartbeat before we stop the timer */
  298. tco_timer_set_timeout(wdd, wdd->timeout);
  299. /*
  300. * Stop the TCO before we change anything so we don't race with
  301. * a zeroed timer.
  302. */
  303. tco_timer_stop(wdd);
  304. release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
  305. return 0;
  306. unreg_region:
  307. release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
  308. return ret;
  309. }
  310. static struct watchdog_info sp5100_tco_wdt_info = {
  311. .identity = "SP5100 TCO timer",
  312. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  313. };
  314. static const struct watchdog_ops sp5100_tco_wdt_ops = {
  315. .owner = THIS_MODULE,
  316. .start = tco_timer_start,
  317. .stop = tco_timer_stop,
  318. .ping = tco_timer_ping,
  319. .set_timeout = tco_timer_set_timeout,
  320. };
  321. static int sp5100_tco_probe(struct platform_device *pdev)
  322. {
  323. struct device *dev = &pdev->dev;
  324. struct watchdog_device *wdd;
  325. struct sp5100_tco *tco;
  326. int ret;
  327. tco = devm_kzalloc(dev, sizeof(*tco), GFP_KERNEL);
  328. if (!tco)
  329. return -ENOMEM;
  330. tco->tco_reg_layout = tco_reg_layout(sp5100_tco_pci);
  331. wdd = &tco->wdd;
  332. wdd->parent = dev;
  333. wdd->info = &sp5100_tco_wdt_info;
  334. wdd->ops = &sp5100_tco_wdt_ops;
  335. wdd->timeout = WATCHDOG_HEARTBEAT;
  336. wdd->min_timeout = 1;
  337. wdd->max_timeout = 0xffff;
  338. if (watchdog_init_timeout(wdd, heartbeat, NULL))
  339. dev_info(dev, "timeout value invalid, using %d\n",
  340. wdd->timeout);
  341. watchdog_set_nowayout(wdd, nowayout);
  342. watchdog_stop_on_reboot(wdd);
  343. watchdog_stop_on_unregister(wdd);
  344. watchdog_set_drvdata(wdd, tco);
  345. ret = sp5100_tco_setupdevice(dev, wdd);
  346. if (ret)
  347. return ret;
  348. ret = devm_watchdog_register_device(dev, wdd);
  349. if (ret) {
  350. dev_err(dev, "cannot register watchdog device (err=%d)\n", ret);
  351. return ret;
  352. }
  353. /* Show module parameters */
  354. dev_info(dev, "initialized. heartbeat=%d sec (nowayout=%d)\n",
  355. wdd->timeout, nowayout);
  356. return 0;
  357. }
  358. static struct platform_driver sp5100_tco_driver = {
  359. .probe = sp5100_tco_probe,
  360. .driver = {
  361. .name = TCO_DRIVER_NAME,
  362. },
  363. };
  364. /*
  365. * Data for PCI driver interface
  366. *
  367. * This data only exists for exporting the supported
  368. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  369. * register a pci_driver, because someone else might
  370. * want to register another driver on the same PCI id.
  371. */
  372. static const struct pci_device_id sp5100_tco_pci_tbl[] = {
  373. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
  374. PCI_ANY_ID, },
  375. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID,
  376. PCI_ANY_ID, },
  377. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, PCI_ANY_ID,
  378. PCI_ANY_ID, },
  379. { 0, }, /* End of list */
  380. };
  381. MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
  382. static int __init sp5100_tco_init(void)
  383. {
  384. struct pci_dev *dev = NULL;
  385. int err;
  386. /* Match the PCI device */
  387. for_each_pci_dev(dev) {
  388. if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
  389. sp5100_tco_pci = dev;
  390. break;
  391. }
  392. }
  393. if (!sp5100_tco_pci)
  394. return -ENODEV;
  395. pr_info("SP5100/SB800 TCO WatchDog Timer Driver\n");
  396. err = platform_driver_register(&sp5100_tco_driver);
  397. if (err)
  398. return err;
  399. sp5100_tco_platform_device =
  400. platform_device_register_simple(TCO_DRIVER_NAME, -1, NULL, 0);
  401. if (IS_ERR(sp5100_tco_platform_device)) {
  402. err = PTR_ERR(sp5100_tco_platform_device);
  403. goto unreg_platform_driver;
  404. }
  405. return 0;
  406. unreg_platform_driver:
  407. platform_driver_unregister(&sp5100_tco_driver);
  408. return err;
  409. }
  410. static void __exit sp5100_tco_exit(void)
  411. {
  412. platform_device_unregister(sp5100_tco_platform_device);
  413. platform_driver_unregister(&sp5100_tco_driver);
  414. }
  415. module_init(sp5100_tco_init);
  416. module_exit(sp5100_tco_exit);
  417. MODULE_AUTHOR("Priyanka Gupta");
  418. MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
  419. MODULE_LICENSE("GPL");