exar_wdt.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * exar_wdt.c - Driver for the watchdog present in some
  4. * Exar/MaxLinear UART chips like the XR28V38x.
  5. *
  6. * (c) Copyright 2022 D. Müller <[email protected]>.
  7. *
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/io.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/watchdog.h>
  16. #define DRV_NAME "exar_wdt"
  17. static const unsigned short sio_config_ports[] = { 0x2e, 0x4e };
  18. static const unsigned char sio_enter_keys[] = { 0x67, 0x77, 0x87, 0xA0 };
  19. #define EXAR_EXIT_KEY 0xAA
  20. #define EXAR_LDN 0x07
  21. #define EXAR_DID 0x20
  22. #define EXAR_VID 0x23
  23. #define EXAR_WDT 0x26
  24. #define EXAR_ACT 0x30
  25. #define EXAR_RTBASE 0x60
  26. #define EXAR_WDT_LDEV 0x08
  27. #define EXAR_VEN_ID 0x13A8
  28. #define EXAR_DEV_382 0x0382
  29. #define EXAR_DEV_384 0x0384
  30. /* WDT runtime registers */
  31. #define WDT_CTRL 0x00
  32. #define WDT_VAL 0x01
  33. #define WDT_UNITS_10MS 0x0 /* the 10 millisec unit of the HW is not used */
  34. #define WDT_UNITS_SEC 0x2
  35. #define WDT_UNITS_MIN 0x4
  36. /* default WDT control for WDTOUT signal activ / rearm by read */
  37. #define EXAR_WDT_DEF_CONF 0
  38. struct wdt_pdev_node {
  39. struct list_head list;
  40. struct platform_device *pdev;
  41. const char name[16];
  42. };
  43. struct wdt_priv {
  44. /* the lock for WDT io operations */
  45. spinlock_t io_lock;
  46. struct resource wdt_res;
  47. struct watchdog_device wdt_dev;
  48. unsigned short did;
  49. unsigned short config_port;
  50. unsigned char enter_key;
  51. unsigned char unit;
  52. unsigned char timeout;
  53. };
  54. #define WATCHDOG_TIMEOUT 60
  55. static int timeout = WATCHDOG_TIMEOUT;
  56. module_param(timeout, int, 0);
  57. MODULE_PARM_DESC(timeout,
  58. "Watchdog timeout in seconds. 1<=timeout<=15300, default="
  59. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  60. static bool nowayout = WATCHDOG_NOWAYOUT;
  61. module_param(nowayout, bool, 0);
  62. MODULE_PARM_DESC(nowayout,
  63. "Watchdog cannot be stopped once started (default="
  64. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  65. static int exar_sio_enter(const unsigned short config_port,
  66. const unsigned char key)
  67. {
  68. if (!request_muxed_region(config_port, 2, DRV_NAME))
  69. return -EBUSY;
  70. /* write the ENTER-KEY twice */
  71. outb(key, config_port);
  72. outb(key, config_port);
  73. return 0;
  74. }
  75. static void exar_sio_exit(const unsigned short config_port)
  76. {
  77. outb(EXAR_EXIT_KEY, config_port);
  78. release_region(config_port, 2);
  79. }
  80. static unsigned char exar_sio_read(const unsigned short config_port,
  81. const unsigned char reg)
  82. {
  83. outb(reg, config_port);
  84. return inb(config_port + 1);
  85. }
  86. static void exar_sio_write(const unsigned short config_port,
  87. const unsigned char reg, const unsigned char val)
  88. {
  89. outb(reg, config_port);
  90. outb(val, config_port + 1);
  91. }
  92. static unsigned short exar_sio_read16(const unsigned short config_port,
  93. const unsigned char reg)
  94. {
  95. unsigned char msb, lsb;
  96. msb = exar_sio_read(config_port, reg);
  97. lsb = exar_sio_read(config_port, reg + 1);
  98. return (msb << 8) | lsb;
  99. }
  100. static void exar_sio_select_wdt(const unsigned short config_port)
  101. {
  102. exar_sio_write(config_port, EXAR_LDN, EXAR_WDT_LDEV);
  103. }
  104. static void exar_wdt_arm(const struct wdt_priv *priv)
  105. {
  106. unsigned short rt_base = priv->wdt_res.start;
  107. /* write timeout value twice to arm watchdog */
  108. outb(priv->timeout, rt_base + WDT_VAL);
  109. outb(priv->timeout, rt_base + WDT_VAL);
  110. }
  111. static void exar_wdt_disarm(const struct wdt_priv *priv)
  112. {
  113. unsigned short rt_base = priv->wdt_res.start;
  114. /*
  115. * use two accesses with different values to make sure
  116. * that a combination of a previous single access and
  117. * the ones below with the same value are not falsely
  118. * interpreted as "arm watchdog"
  119. */
  120. outb(0xFF, rt_base + WDT_VAL);
  121. outb(0, rt_base + WDT_VAL);
  122. }
  123. static int exar_wdt_start(struct watchdog_device *wdog)
  124. {
  125. struct wdt_priv *priv = watchdog_get_drvdata(wdog);
  126. unsigned short rt_base = priv->wdt_res.start;
  127. spin_lock(&priv->io_lock);
  128. exar_wdt_disarm(priv);
  129. outb(priv->unit, rt_base + WDT_CTRL);
  130. exar_wdt_arm(priv);
  131. spin_unlock(&priv->io_lock);
  132. return 0;
  133. }
  134. static int exar_wdt_stop(struct watchdog_device *wdog)
  135. {
  136. struct wdt_priv *priv = watchdog_get_drvdata(wdog);
  137. spin_lock(&priv->io_lock);
  138. exar_wdt_disarm(priv);
  139. spin_unlock(&priv->io_lock);
  140. return 0;
  141. }
  142. static int exar_wdt_keepalive(struct watchdog_device *wdog)
  143. {
  144. struct wdt_priv *priv = watchdog_get_drvdata(wdog);
  145. unsigned short rt_base = priv->wdt_res.start;
  146. spin_lock(&priv->io_lock);
  147. /* reading the WDT_VAL reg will feed the watchdog */
  148. inb(rt_base + WDT_VAL);
  149. spin_unlock(&priv->io_lock);
  150. return 0;
  151. }
  152. static int exar_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
  153. {
  154. struct wdt_priv *priv = watchdog_get_drvdata(wdog);
  155. bool unit_min = false;
  156. /*
  157. * if new timeout is bigger then 255 seconds, change the
  158. * unit to minutes and round the timeout up to the next whole minute
  159. */
  160. if (t > 255) {
  161. unit_min = true;
  162. t = DIV_ROUND_UP(t, 60);
  163. }
  164. /* save for later use in exar_wdt_start() */
  165. priv->unit = unit_min ? WDT_UNITS_MIN : WDT_UNITS_SEC;
  166. priv->timeout = t;
  167. wdog->timeout = unit_min ? t * 60 : t;
  168. if (watchdog_hw_running(wdog))
  169. exar_wdt_start(wdog);
  170. return 0;
  171. }
  172. static const struct watchdog_info exar_wdt_info = {
  173. .options = WDIOF_KEEPALIVEPING |
  174. WDIOF_SETTIMEOUT |
  175. WDIOF_MAGICCLOSE,
  176. .identity = "Exar/MaxLinear XR28V38x Watchdog",
  177. };
  178. static const struct watchdog_ops exar_wdt_ops = {
  179. .owner = THIS_MODULE,
  180. .start = exar_wdt_start,
  181. .stop = exar_wdt_stop,
  182. .ping = exar_wdt_keepalive,
  183. .set_timeout = exar_wdt_set_timeout,
  184. };
  185. static int exar_wdt_config(struct watchdog_device *wdog,
  186. const unsigned char conf)
  187. {
  188. struct wdt_priv *priv = watchdog_get_drvdata(wdog);
  189. int ret;
  190. ret = exar_sio_enter(priv->config_port, priv->enter_key);
  191. if (ret)
  192. return ret;
  193. exar_sio_select_wdt(priv->config_port);
  194. exar_sio_write(priv->config_port, EXAR_WDT, conf);
  195. exar_sio_exit(priv->config_port);
  196. return 0;
  197. }
  198. static int __init exar_wdt_probe(struct platform_device *pdev)
  199. {
  200. struct device *dev = &pdev->dev;
  201. struct wdt_priv *priv = dev->platform_data;
  202. struct watchdog_device *wdt_dev = &priv->wdt_dev;
  203. struct resource *res;
  204. int ret;
  205. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  206. if (!res)
  207. return -ENXIO;
  208. spin_lock_init(&priv->io_lock);
  209. wdt_dev->info = &exar_wdt_info;
  210. wdt_dev->ops = &exar_wdt_ops;
  211. wdt_dev->min_timeout = 1;
  212. wdt_dev->max_timeout = 255 * 60;
  213. watchdog_init_timeout(wdt_dev, timeout, NULL);
  214. watchdog_set_nowayout(wdt_dev, nowayout);
  215. watchdog_stop_on_reboot(wdt_dev);
  216. watchdog_stop_on_unregister(wdt_dev);
  217. watchdog_set_drvdata(wdt_dev, priv);
  218. ret = exar_wdt_config(wdt_dev, EXAR_WDT_DEF_CONF);
  219. if (ret)
  220. return ret;
  221. exar_wdt_set_timeout(wdt_dev, timeout);
  222. /* Make sure that the watchdog is not running */
  223. exar_wdt_stop(wdt_dev);
  224. ret = devm_watchdog_register_device(dev, wdt_dev);
  225. if (ret)
  226. return ret;
  227. dev_info(dev, "XR28V%X WDT initialized. timeout=%d sec (nowayout=%d)\n",
  228. priv->did, timeout, nowayout);
  229. return 0;
  230. }
  231. static unsigned short __init exar_detect(const unsigned short config_port,
  232. const unsigned char key,
  233. unsigned short *rt_base)
  234. {
  235. int ret;
  236. unsigned short base = 0;
  237. unsigned short vid, did;
  238. ret = exar_sio_enter(config_port, key);
  239. if (ret)
  240. return 0;
  241. vid = exar_sio_read16(config_port, EXAR_VID);
  242. did = exar_sio_read16(config_port, EXAR_DID);
  243. /* check for the vendor and device IDs we currently know about */
  244. if (vid == EXAR_VEN_ID &&
  245. (did == EXAR_DEV_382 ||
  246. did == EXAR_DEV_384)) {
  247. exar_sio_select_wdt(config_port);
  248. /* is device active? */
  249. if (exar_sio_read(config_port, EXAR_ACT) == 0x01)
  250. base = exar_sio_read16(config_port, EXAR_RTBASE);
  251. }
  252. exar_sio_exit(config_port);
  253. if (base) {
  254. pr_debug("Found a XR28V%X WDT (conf: 0x%x / rt: 0x%04x)\n",
  255. did, config_port, base);
  256. *rt_base = base;
  257. return did;
  258. }
  259. return 0;
  260. }
  261. static struct platform_driver exar_wdt_driver = {
  262. .driver = {
  263. .name = DRV_NAME,
  264. },
  265. };
  266. static LIST_HEAD(pdev_list);
  267. static int __init exar_wdt_register(struct wdt_priv *priv, const int idx)
  268. {
  269. struct wdt_pdev_node *n;
  270. n = kzalloc(sizeof(*n), GFP_KERNEL);
  271. if (!n)
  272. return -ENOMEM;
  273. INIT_LIST_HEAD(&n->list);
  274. scnprintf((char *)n->name, sizeof(n->name), DRV_NAME ".%d", idx);
  275. priv->wdt_res.name = n->name;
  276. n->pdev = platform_device_register_resndata(NULL, DRV_NAME, idx,
  277. &priv->wdt_res, 1,
  278. priv, sizeof(*priv));
  279. if (IS_ERR(n->pdev)) {
  280. int err = PTR_ERR(n->pdev);
  281. kfree(n);
  282. return err;
  283. }
  284. list_add_tail(&n->list, &pdev_list);
  285. return 0;
  286. }
  287. static void exar_wdt_unregister(void)
  288. {
  289. struct wdt_pdev_node *n, *t;
  290. list_for_each_entry_safe(n, t, &pdev_list, list) {
  291. platform_device_unregister(n->pdev);
  292. list_del(&n->list);
  293. kfree(n);
  294. }
  295. }
  296. static int __init exar_wdt_init(void)
  297. {
  298. int ret, i, j, idx = 0;
  299. /* search for active Exar watchdogs on all possible locations */
  300. for (i = 0; i < ARRAY_SIZE(sio_config_ports); i++) {
  301. for (j = 0; j < ARRAY_SIZE(sio_enter_keys); j++) {
  302. unsigned short did, rt_base = 0;
  303. did = exar_detect(sio_config_ports[i],
  304. sio_enter_keys[j],
  305. &rt_base);
  306. if (did) {
  307. struct wdt_priv priv = {
  308. .wdt_res = DEFINE_RES_IO(rt_base, 2),
  309. .did = did,
  310. .config_port = sio_config_ports[i],
  311. .enter_key = sio_enter_keys[j],
  312. };
  313. ret = exar_wdt_register(&priv, idx);
  314. if (!ret)
  315. idx++;
  316. }
  317. }
  318. }
  319. if (!idx)
  320. return -ENODEV;
  321. ret = platform_driver_probe(&exar_wdt_driver, exar_wdt_probe);
  322. if (ret)
  323. exar_wdt_unregister();
  324. return ret;
  325. }
  326. static void __exit exar_wdt_exit(void)
  327. {
  328. exar_wdt_unregister();
  329. platform_driver_unregister(&exar_wdt_driver);
  330. }
  331. module_init(exar_wdt_init);
  332. module_exit(exar_wdt_exit);
  333. MODULE_AUTHOR("David Müller <[email protected]>");
  334. MODULE_DESCRIPTION("Exar/MaxLinear Watchdog Driver");
  335. MODULE_LICENSE("GPL");