sp5100_tco.c 12 KB

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