sp805_wdt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * drivers/char/watchdog/sp805-wdt.c
  3. *
  4. * Watchdog driver for ARM SP805 watchdog module
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Viresh Kumar<viresh.kumar@st.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2 or later. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/resource.h>
  15. #include <linux/amba/bus.h>
  16. #include <linux/bitops.h>
  17. #include <linux/clk.h>
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/ioport.h>
  22. #include <linux/kernel.h>
  23. #include <linux/math64.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/pm.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/types.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/watchdog.h>
  33. /* default timeout in seconds */
  34. #define DEFAULT_TIMEOUT 60
  35. #define MODULE_NAME "sp805-wdt"
  36. /* watchdog register offsets and masks */
  37. #define WDTLOAD 0x000
  38. #define LOAD_MIN 0x00000001
  39. #define LOAD_MAX 0xFFFFFFFF
  40. #define WDTVALUE 0x004
  41. #define WDTCONTROL 0x008
  42. /* control register masks */
  43. #define INT_ENABLE (1 << 0)
  44. #define RESET_ENABLE (1 << 1)
  45. #define WDTINTCLR 0x00C
  46. #define WDTRIS 0x010
  47. #define WDTMIS 0x014
  48. #define INT_MASK (1 << 0)
  49. #define WDTLOCK 0xC00
  50. #define UNLOCK 0x1ACCE551
  51. #define LOCK 0x00000001
  52. /**
  53. * struct sp805_wdt: sp805 wdt device structure
  54. * @lock: spin lock protecting dev structure and io access
  55. * @base: base address of wdt
  56. * @clk: clock structure of wdt
  57. * @adev: amba device structure of wdt
  58. * @status: current status of wdt
  59. * @load_val: load value to be set for current timeout
  60. */
  61. struct sp805_wdt {
  62. spinlock_t lock;
  63. void __iomem *base;
  64. struct clk *clk;
  65. struct amba_device *adev;
  66. unsigned long status;
  67. #define WDT_BUSY 0
  68. #define WDT_CAN_BE_CLOSED 1
  69. unsigned int load_val;
  70. };
  71. /* local variables */
  72. static struct sp805_wdt *wdt;
  73. static bool nowayout = WATCHDOG_NOWAYOUT;
  74. /* This routine finds load value that will reset system in required timout */
  75. static void wdt_setload(unsigned int timeout)
  76. {
  77. u64 load, rate;
  78. rate = clk_get_rate(wdt->clk);
  79. /*
  80. * sp805 runs counter with given value twice, after the end of first
  81. * counter it gives an interrupt and then starts counter again. If
  82. * interrupt already occurred then it resets the system. This is why
  83. * load is half of what should be required.
  84. */
  85. load = div_u64(rate, 2) * timeout - 1;
  86. load = (load > LOAD_MAX) ? LOAD_MAX : load;
  87. load = (load < LOAD_MIN) ? LOAD_MIN : load;
  88. spin_lock(&wdt->lock);
  89. wdt->load_val = load;
  90. /* roundup timeout to closest positive integer value */
  91. wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
  92. spin_unlock(&wdt->lock);
  93. }
  94. /* returns number of seconds left for reset to occur */
  95. static u32 wdt_timeleft(void)
  96. {
  97. u64 load, rate;
  98. rate = clk_get_rate(wdt->clk);
  99. spin_lock(&wdt->lock);
  100. load = readl_relaxed(wdt->base + WDTVALUE);
  101. /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
  102. if (!(readl_relaxed(wdt->base + WDTRIS) & INT_MASK))
  103. load += wdt->load_val + 1;
  104. spin_unlock(&wdt->lock);
  105. return div_u64(load, rate);
  106. }
  107. /* enables watchdog timers reset */
  108. static void wdt_enable(void)
  109. {
  110. spin_lock(&wdt->lock);
  111. writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
  112. writel_relaxed(wdt->load_val, wdt->base + WDTLOAD);
  113. writel_relaxed(INT_MASK, wdt->base + WDTINTCLR);
  114. writel_relaxed(INT_ENABLE | RESET_ENABLE, wdt->base + WDTCONTROL);
  115. writel_relaxed(LOCK, wdt->base + WDTLOCK);
  116. /* Flush posted writes. */
  117. readl_relaxed(wdt->base + WDTLOCK);
  118. spin_unlock(&wdt->lock);
  119. }
  120. /* disables watchdog timers reset */
  121. static void wdt_disable(void)
  122. {
  123. spin_lock(&wdt->lock);
  124. writel_relaxed(UNLOCK, wdt->base + WDTLOCK);
  125. writel_relaxed(0, wdt->base + WDTCONTROL);
  126. writel_relaxed(LOCK, wdt->base + WDTLOCK);
  127. /* Flush posted writes. */
  128. readl_relaxed(wdt->base + WDTLOCK);
  129. spin_unlock(&wdt->lock);
  130. }
  131. static ssize_t sp805_wdt_write(struct file *file, const char *data,
  132. size_t len, loff_t *ppos)
  133. {
  134. if (len) {
  135. if (!nowayout) {
  136. size_t i;
  137. clear_bit(WDT_CAN_BE_CLOSED, &wdt->status);
  138. for (i = 0; i != len; i++) {
  139. char c;
  140. if (get_user(c, data + i))
  141. return -EFAULT;
  142. /* Check for Magic Close character */
  143. if (c == 'V') {
  144. set_bit(WDT_CAN_BE_CLOSED,
  145. &wdt->status);
  146. break;
  147. }
  148. }
  149. }
  150. wdt_enable();
  151. }
  152. return len;
  153. }
  154. static const struct watchdog_info ident = {
  155. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  156. .identity = MODULE_NAME,
  157. };
  158. static long sp805_wdt_ioctl(struct file *file, unsigned int cmd,
  159. unsigned long arg)
  160. {
  161. int ret = -ENOTTY;
  162. unsigned int timeout;
  163. switch (cmd) {
  164. case WDIOC_GETSUPPORT:
  165. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  166. sizeof(ident)) ? -EFAULT : 0;
  167. break;
  168. case WDIOC_GETSTATUS:
  169. ret = put_user(0, (int *)arg);
  170. break;
  171. case WDIOC_KEEPALIVE:
  172. wdt_enable();
  173. ret = 0;
  174. break;
  175. case WDIOC_SETTIMEOUT:
  176. ret = get_user(timeout, (unsigned int *)arg);
  177. if (ret)
  178. break;
  179. wdt_setload(timeout);
  180. wdt_enable();
  181. /* Fall through */
  182. case WDIOC_GETTIMEOUT:
  183. ret = put_user(wdt->timeout, (unsigned int *)arg);
  184. break;
  185. case WDIOC_GETTIMELEFT:
  186. ret = put_user(wdt_timeleft(), (unsigned int *)arg);
  187. break;
  188. }
  189. return ret;
  190. }
  191. static int sp805_wdt_open(struct inode *inode, struct file *file)
  192. {
  193. int ret = 0;
  194. if (test_and_set_bit(WDT_BUSY, &wdt->status))
  195. return -EBUSY;
  196. ret = clk_enable(wdt->clk);
  197. if (ret) {
  198. dev_err(&wdt->adev->dev, "clock enable fail");
  199. goto err;
  200. }
  201. wdt_enable();
  202. /* can not be closed, once enabled */
  203. clear_bit(WDT_CAN_BE_CLOSED, &wdt->status);
  204. return nonseekable_open(inode, file);
  205. err:
  206. clear_bit(WDT_BUSY, &wdt->status);
  207. return ret;
  208. }
  209. static int sp805_wdt_release(struct inode *inode, struct file *file)
  210. {
  211. if (!test_bit(WDT_CAN_BE_CLOSED, &wdt->status)) {
  212. clear_bit(WDT_BUSY, &wdt->status);
  213. dev_warn(&wdt->adev->dev, "Device closed unexpectedly\n");
  214. return 0;
  215. }
  216. wdt_disable();
  217. clk_disable(wdt->clk);
  218. clear_bit(WDT_BUSY, &wdt->status);
  219. return 0;
  220. }
  221. static const struct file_operations sp805_wdt_fops = {
  222. .owner = THIS_MODULE,
  223. .llseek = no_llseek,
  224. .write = sp805_wdt_write,
  225. .unlocked_ioctl = sp805_wdt_ioctl,
  226. .open = sp805_wdt_open,
  227. .release = sp805_wdt_release,
  228. };
  229. static struct miscdevice sp805_wdt_miscdev = {
  230. .minor = WATCHDOG_MINOR,
  231. .name = "watchdog",
  232. .fops = &sp805_wdt_fops,
  233. };
  234. static int __devinit
  235. sp805_wdt_probe(struct amba_device *adev, const struct amba_id *id)
  236. {
  237. int ret = 0;
  238. if (!devm_request_mem_region(&adev->dev, adev->res.start,
  239. resource_size(&adev->res), "sp805_wdt")) {
  240. dev_warn(&adev->dev, "Failed to get memory region resource\n");
  241. ret = -ENOENT;
  242. goto err;
  243. }
  244. wdt = devm_kzalloc(&adev->dev, sizeof(*wdt), GFP_KERNEL);
  245. if (!wdt) {
  246. dev_warn(&adev->dev, "Kzalloc failed\n");
  247. ret = -ENOMEM;
  248. goto err;
  249. }
  250. wdt->base = devm_ioremap(&adev->dev, adev->res.start,
  251. resource_size(&adev->res));
  252. if (!wdt->base) {
  253. ret = -ENOMEM;
  254. dev_warn(&adev->dev, "ioremap fail\n");
  255. goto err;
  256. }
  257. wdt->clk = clk_get(&adev->dev, NULL);
  258. if (IS_ERR(wdt->clk)) {
  259. dev_warn(&adev->dev, "Clock not found\n");
  260. ret = PTR_ERR(wdt->clk);
  261. goto err;
  262. }
  263. wdt->adev = adev;
  264. spin_lock_init(&wdt->lock);
  265. wdt_setload(DEFAULT_TIMEOUT);
  266. ret = misc_register(&sp805_wdt_miscdev);
  267. if (ret < 0) {
  268. dev_warn(&adev->dev, "cannot register misc device\n");
  269. goto err_misc_register;
  270. }
  271. dev_info(&adev->dev, "registration successful\n");
  272. return 0;
  273. err_misc_register:
  274. clk_put(wdt->clk);
  275. err:
  276. dev_err(&adev->dev, "Probe Failed!!!\n");
  277. return ret;
  278. }
  279. static int __devexit sp805_wdt_remove(struct amba_device *adev)
  280. {
  281. misc_deregister(&sp805_wdt_miscdev);
  282. clk_put(wdt->clk);
  283. return 0;
  284. }
  285. #ifdef CONFIG_PM
  286. static int sp805_wdt_suspend(struct device *dev)
  287. {
  288. if (test_bit(WDT_BUSY, &wdt->status)) {
  289. wdt_disable();
  290. clk_disable(wdt->clk);
  291. }
  292. return 0;
  293. }
  294. static int sp805_wdt_resume(struct device *dev)
  295. {
  296. int ret = 0;
  297. if (test_bit(WDT_BUSY, &wdt->status)) {
  298. ret = clk_enable(wdt->clk);
  299. if (ret) {
  300. dev_err(dev, "clock enable fail");
  301. return ret;
  302. }
  303. wdt_enable();
  304. }
  305. return ret;
  306. }
  307. #endif /* CONFIG_PM */
  308. static SIMPLE_DEV_PM_OPS(sp805_wdt_dev_pm_ops, sp805_wdt_suspend,
  309. sp805_wdt_resume);
  310. static struct amba_id sp805_wdt_ids[] = {
  311. {
  312. .id = 0x00141805,
  313. .mask = 0x00ffffff,
  314. },
  315. { 0, 0 },
  316. };
  317. MODULE_DEVICE_TABLE(amba, sp805_wdt_ids);
  318. static struct amba_driver sp805_wdt_driver = {
  319. .drv = {
  320. .name = MODULE_NAME,
  321. .pm = &sp805_wdt_dev_pm_ops,
  322. },
  323. .id_table = sp805_wdt_ids,
  324. .probe = sp805_wdt_probe,
  325. .remove = __devexit_p(sp805_wdt_remove),
  326. };
  327. module_amba_driver(sp805_wdt_driver);
  328. module_param(nowayout, bool, 0);
  329. MODULE_PARM_DESC(nowayout,
  330. "Set to 1 to keep watchdog running after device release");
  331. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
  332. MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
  333. MODULE_LICENSE("GPL");
  334. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);