orion_wdt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * drivers/watchdog/orion_wdt.c
  3. *
  4. * Watchdog driver for Orion/Kirkwood processors
  5. *
  6. * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. /* RSTOUT mask register physical address for Orion5x, Kirkwood and Dove */
  26. #define ORION_RSTOUT_MASK_OFFSET 0x20108
  27. /* Internal registers can be configured at any 1 MiB aligned address */
  28. #define INTERNAL_REGS_MASK ~(SZ_1M - 1)
  29. /*
  30. * Watchdog timer block registers.
  31. */
  32. #define TIMER_CTRL 0x0000
  33. #define TIMER_A370_STATUS 0x04
  34. #define WDT_MAX_CYCLE_COUNT 0xffffffff
  35. #define WDT_A370_RATIO_MASK(v) ((v) << 16)
  36. #define WDT_A370_RATIO_SHIFT 5
  37. #define WDT_A370_RATIO (1 << WDT_A370_RATIO_SHIFT)
  38. #define WDT_AXP_FIXED_ENABLE_BIT BIT(10)
  39. #define WDT_A370_EXPIRED BIT(31)
  40. static bool nowayout = WATCHDOG_NOWAYOUT;
  41. static int heartbeat = -1; /* module parameter (seconds) */
  42. struct orion_watchdog;
  43. struct orion_watchdog_data {
  44. int wdt_counter_offset;
  45. int wdt_enable_bit;
  46. int rstout_enable_bit;
  47. int rstout_mask_bit;
  48. int (*clock_init)(struct platform_device *,
  49. struct orion_watchdog *);
  50. int (*enabled)(struct orion_watchdog *);
  51. int (*start)(struct watchdog_device *);
  52. int (*stop)(struct watchdog_device *);
  53. };
  54. struct orion_watchdog {
  55. struct watchdog_device wdt;
  56. void __iomem *reg;
  57. void __iomem *rstout;
  58. void __iomem *rstout_mask;
  59. unsigned long clk_rate;
  60. struct clk *clk;
  61. const struct orion_watchdog_data *data;
  62. };
  63. static int orion_wdt_clock_init(struct platform_device *pdev,
  64. struct orion_watchdog *dev)
  65. {
  66. int ret;
  67. dev->clk = clk_get(&pdev->dev, NULL);
  68. if (IS_ERR(dev->clk))
  69. return PTR_ERR(dev->clk);
  70. ret = clk_prepare_enable(dev->clk);
  71. if (ret) {
  72. clk_put(dev->clk);
  73. return ret;
  74. }
  75. dev->clk_rate = clk_get_rate(dev->clk);
  76. return 0;
  77. }
  78. static int armada370_wdt_clock_init(struct platform_device *pdev,
  79. struct orion_watchdog *dev)
  80. {
  81. int ret;
  82. dev->clk = clk_get(&pdev->dev, NULL);
  83. if (IS_ERR(dev->clk))
  84. return PTR_ERR(dev->clk);
  85. ret = clk_prepare_enable(dev->clk);
  86. if (ret) {
  87. clk_put(dev->clk);
  88. return ret;
  89. }
  90. /* Setup watchdog input clock */
  91. atomic_io_modify(dev->reg + TIMER_CTRL,
  92. WDT_A370_RATIO_MASK(WDT_A370_RATIO_SHIFT),
  93. WDT_A370_RATIO_MASK(WDT_A370_RATIO_SHIFT));
  94. dev->clk_rate = clk_get_rate(dev->clk) / WDT_A370_RATIO;
  95. return 0;
  96. }
  97. static int armadaxp_wdt_clock_init(struct platform_device *pdev,
  98. struct orion_watchdog *dev)
  99. {
  100. int ret;
  101. dev->clk = of_clk_get_by_name(pdev->dev.of_node, "fixed");
  102. if (IS_ERR(dev->clk))
  103. return PTR_ERR(dev->clk);
  104. ret = clk_prepare_enable(dev->clk);
  105. if (ret) {
  106. clk_put(dev->clk);
  107. return ret;
  108. }
  109. /* Enable the fixed watchdog clock input */
  110. atomic_io_modify(dev->reg + TIMER_CTRL,
  111. WDT_AXP_FIXED_ENABLE_BIT,
  112. WDT_AXP_FIXED_ENABLE_BIT);
  113. dev->clk_rate = clk_get_rate(dev->clk);
  114. return 0;
  115. }
  116. static int orion_wdt_ping(struct watchdog_device *wdt_dev)
  117. {
  118. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  119. /* Reload watchdog duration */
  120. writel(dev->clk_rate * wdt_dev->timeout,
  121. dev->reg + dev->data->wdt_counter_offset);
  122. return 0;
  123. }
  124. static int armada375_start(struct watchdog_device *wdt_dev)
  125. {
  126. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  127. u32 reg;
  128. /* Set watchdog duration */
  129. writel(dev->clk_rate * wdt_dev->timeout,
  130. dev->reg + dev->data->wdt_counter_offset);
  131. /* Clear the watchdog expiration bit */
  132. atomic_io_modify(dev->reg + TIMER_A370_STATUS, WDT_A370_EXPIRED, 0);
  133. /* Enable watchdog timer */
  134. atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
  135. dev->data->wdt_enable_bit);
  136. /* Enable reset on watchdog */
  137. reg = readl(dev->rstout);
  138. reg |= dev->data->rstout_enable_bit;
  139. writel(reg, dev->rstout);
  140. atomic_io_modify(dev->rstout_mask, dev->data->rstout_mask_bit, 0);
  141. return 0;
  142. }
  143. static int armada370_start(struct watchdog_device *wdt_dev)
  144. {
  145. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  146. u32 reg;
  147. /* Set watchdog duration */
  148. writel(dev->clk_rate * wdt_dev->timeout,
  149. dev->reg + dev->data->wdt_counter_offset);
  150. /* Clear the watchdog expiration bit */
  151. atomic_io_modify(dev->reg + TIMER_A370_STATUS, WDT_A370_EXPIRED, 0);
  152. /* Enable watchdog timer */
  153. atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
  154. dev->data->wdt_enable_bit);
  155. /* Enable reset on watchdog */
  156. reg = readl(dev->rstout);
  157. reg |= dev->data->rstout_enable_bit;
  158. writel(reg, dev->rstout);
  159. return 0;
  160. }
  161. static int orion_start(struct watchdog_device *wdt_dev)
  162. {
  163. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  164. /* Set watchdog duration */
  165. writel(dev->clk_rate * wdt_dev->timeout,
  166. dev->reg + dev->data->wdt_counter_offset);
  167. /* Enable watchdog timer */
  168. atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
  169. dev->data->wdt_enable_bit);
  170. /* Enable reset on watchdog */
  171. atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit,
  172. dev->data->rstout_enable_bit);
  173. return 0;
  174. }
  175. static int orion_wdt_start(struct watchdog_device *wdt_dev)
  176. {
  177. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  178. /* There are some per-SoC quirks to handle */
  179. return dev->data->start(wdt_dev);
  180. }
  181. static int orion_stop(struct watchdog_device *wdt_dev)
  182. {
  183. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  184. /* Disable reset on watchdog */
  185. atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit, 0);
  186. /* Disable watchdog timer */
  187. atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
  188. return 0;
  189. }
  190. static int armada375_stop(struct watchdog_device *wdt_dev)
  191. {
  192. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  193. u32 reg;
  194. /* Disable reset on watchdog */
  195. atomic_io_modify(dev->rstout_mask, dev->data->rstout_mask_bit,
  196. dev->data->rstout_mask_bit);
  197. reg = readl(dev->rstout);
  198. reg &= ~dev->data->rstout_enable_bit;
  199. writel(reg, dev->rstout);
  200. /* Disable watchdog timer */
  201. atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
  202. return 0;
  203. }
  204. static int armada370_stop(struct watchdog_device *wdt_dev)
  205. {
  206. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  207. u32 reg;
  208. /* Disable reset on watchdog */
  209. reg = readl(dev->rstout);
  210. reg &= ~dev->data->rstout_enable_bit;
  211. writel(reg, dev->rstout);
  212. /* Disable watchdog timer */
  213. atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
  214. return 0;
  215. }
  216. static int orion_wdt_stop(struct watchdog_device *wdt_dev)
  217. {
  218. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  219. return dev->data->stop(wdt_dev);
  220. }
  221. static int orion_enabled(struct orion_watchdog *dev)
  222. {
  223. bool enabled, running;
  224. enabled = readl(dev->rstout) & dev->data->rstout_enable_bit;
  225. running = readl(dev->reg + TIMER_CTRL) & dev->data->wdt_enable_bit;
  226. return enabled && running;
  227. }
  228. static int armada375_enabled(struct orion_watchdog *dev)
  229. {
  230. bool masked, enabled, running;
  231. masked = readl(dev->rstout_mask) & dev->data->rstout_mask_bit;
  232. enabled = readl(dev->rstout) & dev->data->rstout_enable_bit;
  233. running = readl(dev->reg + TIMER_CTRL) & dev->data->wdt_enable_bit;
  234. return !masked && enabled && running;
  235. }
  236. static int orion_wdt_enabled(struct watchdog_device *wdt_dev)
  237. {
  238. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  239. return dev->data->enabled(dev);
  240. }
  241. static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
  242. {
  243. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  244. return readl(dev->reg + dev->data->wdt_counter_offset) / dev->clk_rate;
  245. }
  246. static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
  247. unsigned int timeout)
  248. {
  249. wdt_dev->timeout = timeout;
  250. return 0;
  251. }
  252. static const struct watchdog_info orion_wdt_info = {
  253. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  254. .identity = "Orion Watchdog",
  255. };
  256. static const struct watchdog_ops orion_wdt_ops = {
  257. .owner = THIS_MODULE,
  258. .start = orion_wdt_start,
  259. .stop = orion_wdt_stop,
  260. .ping = orion_wdt_ping,
  261. .set_timeout = orion_wdt_set_timeout,
  262. .get_timeleft = orion_wdt_get_timeleft,
  263. };
  264. static irqreturn_t orion_wdt_irq(int irq, void *devid)
  265. {
  266. panic("Watchdog Timeout");
  267. return IRQ_HANDLED;
  268. }
  269. /*
  270. * The original devicetree binding for this driver specified only
  271. * one memory resource, so in order to keep DT backwards compatibility
  272. * we try to fallback to a hardcoded register address, if the resource
  273. * is missing from the devicetree.
  274. */
  275. static void __iomem *orion_wdt_ioremap_rstout(struct platform_device *pdev,
  276. phys_addr_t internal_regs)
  277. {
  278. struct resource *res;
  279. phys_addr_t rstout;
  280. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  281. if (res)
  282. return devm_ioremap(&pdev->dev, res->start,
  283. resource_size(res));
  284. rstout = internal_regs + ORION_RSTOUT_MASK_OFFSET;
  285. WARN(1, FW_BUG "falling back to harcoded RSTOUT reg %pa\n", &rstout);
  286. return devm_ioremap(&pdev->dev, rstout, 0x4);
  287. }
  288. static const struct orion_watchdog_data orion_data = {
  289. .rstout_enable_bit = BIT(1),
  290. .wdt_enable_bit = BIT(4),
  291. .wdt_counter_offset = 0x24,
  292. .clock_init = orion_wdt_clock_init,
  293. .enabled = orion_enabled,
  294. .start = orion_start,
  295. .stop = orion_stop,
  296. };
  297. static const struct orion_watchdog_data armada370_data = {
  298. .rstout_enable_bit = BIT(8),
  299. .wdt_enable_bit = BIT(8),
  300. .wdt_counter_offset = 0x34,
  301. .clock_init = armada370_wdt_clock_init,
  302. .enabled = orion_enabled,
  303. .start = armada370_start,
  304. .stop = armada370_stop,
  305. };
  306. static const struct orion_watchdog_data armadaxp_data = {
  307. .rstout_enable_bit = BIT(8),
  308. .wdt_enable_bit = BIT(8),
  309. .wdt_counter_offset = 0x34,
  310. .clock_init = armadaxp_wdt_clock_init,
  311. .enabled = orion_enabled,
  312. .start = armada370_start,
  313. .stop = armada370_stop,
  314. };
  315. static const struct orion_watchdog_data armada375_data = {
  316. .rstout_enable_bit = BIT(8),
  317. .rstout_mask_bit = BIT(10),
  318. .wdt_enable_bit = BIT(8),
  319. .wdt_counter_offset = 0x34,
  320. .clock_init = armada370_wdt_clock_init,
  321. .enabled = armada375_enabled,
  322. .start = armada375_start,
  323. .stop = armada375_stop,
  324. };
  325. static const struct orion_watchdog_data armada380_data = {
  326. .rstout_enable_bit = BIT(8),
  327. .rstout_mask_bit = BIT(10),
  328. .wdt_enable_bit = BIT(8),
  329. .wdt_counter_offset = 0x34,
  330. .clock_init = armadaxp_wdt_clock_init,
  331. .enabled = armada375_enabled,
  332. .start = armada375_start,
  333. .stop = armada375_stop,
  334. };
  335. static const struct of_device_id orion_wdt_of_match_table[] = {
  336. {
  337. .compatible = "marvell,orion-wdt",
  338. .data = &orion_data,
  339. },
  340. {
  341. .compatible = "marvell,armada-370-wdt",
  342. .data = &armada370_data,
  343. },
  344. {
  345. .compatible = "marvell,armada-xp-wdt",
  346. .data = &armadaxp_data,
  347. },
  348. {
  349. .compatible = "marvell,armada-375-wdt",
  350. .data = &armada375_data,
  351. },
  352. {
  353. .compatible = "marvell,armada-380-wdt",
  354. .data = &armada380_data,
  355. },
  356. {},
  357. };
  358. MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
  359. static int orion_wdt_get_regs(struct platform_device *pdev,
  360. struct orion_watchdog *dev)
  361. {
  362. struct device_node *node = pdev->dev.of_node;
  363. struct resource *res;
  364. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  365. if (!res)
  366. return -ENODEV;
  367. dev->reg = devm_ioremap(&pdev->dev, res->start,
  368. resource_size(res));
  369. if (!dev->reg)
  370. return -ENOMEM;
  371. /* Each supported compatible has some RSTOUT register quirk */
  372. if (of_device_is_compatible(node, "marvell,orion-wdt")) {
  373. dev->rstout = orion_wdt_ioremap_rstout(pdev, res->start &
  374. INTERNAL_REGS_MASK);
  375. if (!dev->rstout)
  376. return -ENODEV;
  377. } else if (of_device_is_compatible(node, "marvell,armada-370-wdt") ||
  378. of_device_is_compatible(node, "marvell,armada-xp-wdt")) {
  379. /* Dedicated RSTOUT register, can be requested. */
  380. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  381. dev->rstout = devm_ioremap_resource(&pdev->dev, res);
  382. if (IS_ERR(dev->rstout))
  383. return PTR_ERR(dev->rstout);
  384. } else if (of_device_is_compatible(node, "marvell,armada-375-wdt") ||
  385. of_device_is_compatible(node, "marvell,armada-380-wdt")) {
  386. /* Dedicated RSTOUT register, can be requested. */
  387. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  388. dev->rstout = devm_ioremap_resource(&pdev->dev, res);
  389. if (IS_ERR(dev->rstout))
  390. return PTR_ERR(dev->rstout);
  391. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  392. if (!res)
  393. return -ENODEV;
  394. dev->rstout_mask = devm_ioremap(&pdev->dev, res->start,
  395. resource_size(res));
  396. if (!dev->rstout_mask)
  397. return -ENOMEM;
  398. } else {
  399. return -ENODEV;
  400. }
  401. return 0;
  402. }
  403. static int orion_wdt_probe(struct platform_device *pdev)
  404. {
  405. struct orion_watchdog *dev;
  406. const struct of_device_id *match;
  407. unsigned int wdt_max_duration; /* (seconds) */
  408. int ret, irq;
  409. dev = devm_kzalloc(&pdev->dev, sizeof(struct orion_watchdog),
  410. GFP_KERNEL);
  411. if (!dev)
  412. return -ENOMEM;
  413. match = of_match_device(orion_wdt_of_match_table, &pdev->dev);
  414. if (!match)
  415. /* Default legacy match */
  416. match = &orion_wdt_of_match_table[0];
  417. dev->wdt.info = &orion_wdt_info;
  418. dev->wdt.ops = &orion_wdt_ops;
  419. dev->wdt.min_timeout = 1;
  420. dev->data = match->data;
  421. ret = orion_wdt_get_regs(pdev, dev);
  422. if (ret)
  423. return ret;
  424. ret = dev->data->clock_init(pdev, dev);
  425. if (ret) {
  426. dev_err(&pdev->dev, "cannot initialize clock\n");
  427. return ret;
  428. }
  429. wdt_max_duration = WDT_MAX_CYCLE_COUNT / dev->clk_rate;
  430. dev->wdt.timeout = wdt_max_duration;
  431. dev->wdt.max_timeout = wdt_max_duration;
  432. watchdog_init_timeout(&dev->wdt, heartbeat, &pdev->dev);
  433. platform_set_drvdata(pdev, &dev->wdt);
  434. watchdog_set_drvdata(&dev->wdt, dev);
  435. /*
  436. * Let's make sure the watchdog is fully stopped, unless it's
  437. * explicitly enabled. This may be the case if the module was
  438. * removed and re-insterted, or if the bootloader explicitly
  439. * set a running watchdog before booting the kernel.
  440. */
  441. if (!orion_wdt_enabled(&dev->wdt))
  442. orion_wdt_stop(&dev->wdt);
  443. /* Request the IRQ only after the watchdog is disabled */
  444. irq = platform_get_irq(pdev, 0);
  445. if (irq > 0) {
  446. /*
  447. * Not all supported platforms specify an interrupt for the
  448. * watchdog, so let's make it optional.
  449. */
  450. ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
  451. pdev->name, dev);
  452. if (ret < 0) {
  453. dev_err(&pdev->dev, "failed to request IRQ\n");
  454. goto disable_clk;
  455. }
  456. }
  457. watchdog_set_nowayout(&dev->wdt, nowayout);
  458. ret = watchdog_register_device(&dev->wdt);
  459. if (ret)
  460. goto disable_clk;
  461. pr_info("Initial timeout %d sec%s\n",
  462. dev->wdt.timeout, nowayout ? ", nowayout" : "");
  463. return 0;
  464. disable_clk:
  465. clk_disable_unprepare(dev->clk);
  466. clk_put(dev->clk);
  467. return ret;
  468. }
  469. static int orion_wdt_remove(struct platform_device *pdev)
  470. {
  471. struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
  472. struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
  473. watchdog_unregister_device(wdt_dev);
  474. clk_disable_unprepare(dev->clk);
  475. clk_put(dev->clk);
  476. return 0;
  477. }
  478. static void orion_wdt_shutdown(struct platform_device *pdev)
  479. {
  480. struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
  481. orion_wdt_stop(wdt_dev);
  482. }
  483. static struct platform_driver orion_wdt_driver = {
  484. .probe = orion_wdt_probe,
  485. .remove = orion_wdt_remove,
  486. .shutdown = orion_wdt_shutdown,
  487. .driver = {
  488. .owner = THIS_MODULE,
  489. .name = "orion_wdt",
  490. .of_match_table = orion_wdt_of_match_table,
  491. },
  492. };
  493. module_platform_driver(orion_wdt_driver);
  494. MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
  495. MODULE_DESCRIPTION("Orion Processor Watchdog");
  496. module_param(heartbeat, int, 0);
  497. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  498. module_param(nowayout, bool, 0);
  499. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  500. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  501. MODULE_LICENSE("GPL");
  502. MODULE_ALIAS("platform:orion_wdt");