menz69_wdt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Watchdog driver for the MEN z069 IP-Core
  4. *
  5. * Copyright (C) 2018 Johannes Thumshirn <jth@kernel.org>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mcb.h>
  10. #include <linux/module.h>
  11. #include <linux/watchdog.h>
  12. struct men_z069_drv {
  13. struct watchdog_device wdt;
  14. void __iomem *base;
  15. struct resource *mem;
  16. };
  17. #define MEN_Z069_WTR 0x10
  18. #define MEN_Z069_WTR_WDEN BIT(15)
  19. #define MEN_Z069_WTR_WDET_MASK 0x7fff
  20. #define MEN_Z069_WVR 0x14
  21. #define MEN_Z069_TIMER_FREQ 500 /* 500 Hz */
  22. #define MEN_Z069_WDT_COUNTER_MIN 1
  23. #define MEN_Z069_WDT_COUNTER_MAX 0x7fff
  24. #define MEN_Z069_DEFAULT_TIMEOUT 30
  25. static bool nowayout = WATCHDOG_NOWAYOUT;
  26. module_param(nowayout, bool, 0);
  27. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  28. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  29. static int men_z069_wdt_start(struct watchdog_device *wdt)
  30. {
  31. struct men_z069_drv *drv = watchdog_get_drvdata(wdt);
  32. u16 val;
  33. val = readw(drv->base + MEN_Z069_WTR);
  34. val |= MEN_Z069_WTR_WDEN;
  35. writew(val, drv->base + MEN_Z069_WTR);
  36. return 0;
  37. }
  38. static int men_z069_wdt_stop(struct watchdog_device *wdt)
  39. {
  40. struct men_z069_drv *drv = watchdog_get_drvdata(wdt);
  41. u16 val;
  42. val = readw(drv->base + MEN_Z069_WTR);
  43. val &= ~MEN_Z069_WTR_WDEN;
  44. writew(val, drv->base + MEN_Z069_WTR);
  45. return 0;
  46. }
  47. static int men_z069_wdt_ping(struct watchdog_device *wdt)
  48. {
  49. struct men_z069_drv *drv = watchdog_get_drvdata(wdt);
  50. u16 val;
  51. /* The watchdog trigger value toggles between 0x5555 and 0xaaaa */
  52. val = readw(drv->base + MEN_Z069_WVR);
  53. val ^= 0xffff;
  54. writew(val, drv->base + MEN_Z069_WVR);
  55. return 0;
  56. }
  57. static int men_z069_wdt_set_timeout(struct watchdog_device *wdt,
  58. unsigned int timeout)
  59. {
  60. struct men_z069_drv *drv = watchdog_get_drvdata(wdt);
  61. u16 reg, val, ena;
  62. wdt->timeout = timeout;
  63. val = timeout * MEN_Z069_TIMER_FREQ;
  64. reg = readw(drv->base + MEN_Z069_WVR);
  65. ena = reg & MEN_Z069_WTR_WDEN;
  66. reg = ena | val;
  67. writew(reg, drv->base + MEN_Z069_WTR);
  68. return 0;
  69. }
  70. static const struct watchdog_info men_z069_info = {
  71. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  72. .identity = "MEN z069 Watchdog",
  73. };
  74. static const struct watchdog_ops men_z069_ops = {
  75. .owner = THIS_MODULE,
  76. .start = men_z069_wdt_start,
  77. .stop = men_z069_wdt_stop,
  78. .ping = men_z069_wdt_ping,
  79. .set_timeout = men_z069_wdt_set_timeout,
  80. };
  81. static struct watchdog_device men_z069_wdt = {
  82. .info = &men_z069_info,
  83. .ops = &men_z069_ops,
  84. .timeout = MEN_Z069_DEFAULT_TIMEOUT,
  85. .min_timeout = 1,
  86. .max_timeout = MEN_Z069_WDT_COUNTER_MAX / MEN_Z069_TIMER_FREQ,
  87. };
  88. static int men_z069_probe(struct mcb_device *dev,
  89. const struct mcb_device_id *id)
  90. {
  91. struct men_z069_drv *drv;
  92. struct resource *mem;
  93. drv = devm_kzalloc(&dev->dev, sizeof(struct men_z069_drv), GFP_KERNEL);
  94. if (!drv)
  95. return -ENOMEM;
  96. mem = mcb_request_mem(dev, "z069-wdt");
  97. if (IS_ERR(mem))
  98. return PTR_ERR(mem);
  99. drv->base = devm_ioremap(&dev->dev, mem->start, resource_size(mem));
  100. if (drv->base == NULL)
  101. goto release_mem;
  102. drv->mem = mem;
  103. drv->wdt = men_z069_wdt;
  104. watchdog_init_timeout(&drv->wdt, 0, &dev->dev);
  105. watchdog_set_nowayout(&drv->wdt, nowayout);
  106. watchdog_set_drvdata(&drv->wdt, drv);
  107. drv->wdt.parent = &dev->dev;
  108. mcb_set_drvdata(dev, drv);
  109. return watchdog_register_device(&men_z069_wdt);
  110. release_mem:
  111. mcb_release_mem(mem);
  112. return -ENOMEM;
  113. }
  114. static void men_z069_remove(struct mcb_device *dev)
  115. {
  116. struct men_z069_drv *drv = mcb_get_drvdata(dev);
  117. watchdog_unregister_device(&drv->wdt);
  118. mcb_release_mem(drv->mem);
  119. }
  120. static const struct mcb_device_id men_z069_ids[] = {
  121. { .device = 0x45 },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(mcb, men_z069_ids);
  125. static struct mcb_driver men_z069_driver = {
  126. .driver = {
  127. .name = "z069-wdt",
  128. .owner = THIS_MODULE,
  129. },
  130. .probe = men_z069_probe,
  131. .remove = men_z069_remove,
  132. .id_table = men_z069_ids,
  133. };
  134. module_mcb_driver(men_z069_driver);
  135. MODULE_AUTHOR("Johannes Thumshirn <jth@kernel.org>");
  136. MODULE_LICENSE("GPL v2");
  137. MODULE_ALIAS("mcb:16z069");