009-v6.3-01-watchdog-mt7621-wdt-avoid-static-global-declarations.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. From 783c7cb4659b53b5e1b809dac5e8cdf250145919 Mon Sep 17 00:00:00 2001
  2. From: Sergio Paracuellos <[email protected]>
  3. Date: Tue, 14 Feb 2023 11:39:35 +0100
  4. Subject: [PATCH 1/2] watchdog: mt7621-wdt: avoid static global declarations
  5. Instead of using static global definitions in driver code, refactor code
  6. introducing a new watchdog driver data structure and use it along the
  7. code.
  8. Reviewed-by: Guenter Roeck <[email protected]>
  9. Signed-off-by: Sergio Paracuellos <[email protected]>
  10. Link: https://lore.kernel.org/r/[email protected]
  11. [groeck: unsigned -> unsigned int]
  12. Signed-off-by: Guenter Roeck <[email protected]>
  13. Signed-off-by: Wim Van Sebroeck <[email protected]>
  14. ---
  15. drivers/watchdog/mt7621_wdt.c | 102 +++++++++++++++++++++++++++---------------
  16. 1 file changed, 65 insertions(+), 37 deletions(-)
  17. --- a/drivers/watchdog/mt7621_wdt.c
  18. +++ b/drivers/watchdog/mt7621_wdt.c
  19. @@ -31,8 +31,11 @@
  20. #define TMR1CTL_RESTART BIT(9)
  21. #define TMR1CTL_PRESCALE_SHIFT 16
  22. -static void __iomem *mt7621_wdt_base;
  23. -static struct reset_control *mt7621_wdt_reset;
  24. +struct mt7621_wdt_data {
  25. + void __iomem *base;
  26. + struct reset_control *rst;
  27. + struct watchdog_device wdt;
  28. +};
  29. static bool nowayout = WATCHDOG_NOWAYOUT;
  30. module_param(nowayout, bool, 0);
  31. @@ -40,27 +43,31 @@ MODULE_PARM_DESC(nowayout,
  32. "Watchdog cannot be stopped once started (default="
  33. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  34. -static inline void rt_wdt_w32(unsigned reg, u32 val)
  35. +static inline void rt_wdt_w32(void __iomem *base, unsigned int reg, u32 val)
  36. {
  37. - iowrite32(val, mt7621_wdt_base + reg);
  38. + iowrite32(val, base + reg);
  39. }
  40. -static inline u32 rt_wdt_r32(unsigned reg)
  41. +static inline u32 rt_wdt_r32(void __iomem *base, unsigned int reg)
  42. {
  43. - return ioread32(mt7621_wdt_base + reg);
  44. + return ioread32(base + reg);
  45. }
  46. static int mt7621_wdt_ping(struct watchdog_device *w)
  47. {
  48. - rt_wdt_w32(TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
  49. + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  50. +
  51. + rt_wdt_w32(drvdata->base, TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
  52. return 0;
  53. }
  54. static int mt7621_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
  55. {
  56. + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  57. +
  58. w->timeout = t;
  59. - rt_wdt_w32(TIMER_REG_TMR1LOAD, t * 1000);
  60. + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1LOAD, t * 1000);
  61. mt7621_wdt_ping(w);
  62. return 0;
  63. @@ -68,29 +75,31 @@ static int mt7621_wdt_set_timeout(struct
  64. static int mt7621_wdt_start(struct watchdog_device *w)
  65. {
  66. + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  67. u32 t;
  68. /* set the prescaler to 1ms == 1000us */
  69. - rt_wdt_w32(TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
  70. + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
  71. mt7621_wdt_set_timeout(w, w->timeout);
  72. - t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  73. + t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
  74. t |= TMR1CTL_ENABLE;
  75. - rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  76. + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
  77. return 0;
  78. }
  79. static int mt7621_wdt_stop(struct watchdog_device *w)
  80. {
  81. + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  82. u32 t;
  83. mt7621_wdt_ping(w);
  84. - t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  85. + t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
  86. t &= ~TMR1CTL_ENABLE;
  87. - rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  88. + rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
  89. return 0;
  90. }
  91. @@ -105,7 +114,9 @@ static int mt7621_wdt_bootcause(void)
  92. static int mt7621_wdt_is_running(struct watchdog_device *w)
  93. {
  94. - return !!(rt_wdt_r32(TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
  95. + struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  96. +
  97. + return !!(rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
  98. }
  99. static const struct watchdog_info mt7621_wdt_info = {
  100. @@ -121,30 +132,39 @@ static const struct watchdog_ops mt7621_
  101. .set_timeout = mt7621_wdt_set_timeout,
  102. };
  103. -static struct watchdog_device mt7621_wdt_dev = {
  104. - .info = &mt7621_wdt_info,
  105. - .ops = &mt7621_wdt_ops,
  106. - .min_timeout = 1,
  107. - .max_timeout = 0xfffful / 1000,
  108. -};
  109. -
  110. static int mt7621_wdt_probe(struct platform_device *pdev)
  111. {
  112. struct device *dev = &pdev->dev;
  113. - mt7621_wdt_base = devm_platform_ioremap_resource(pdev, 0);
  114. - if (IS_ERR(mt7621_wdt_base))
  115. - return PTR_ERR(mt7621_wdt_base);
  116. -
  117. - mt7621_wdt_reset = devm_reset_control_get_exclusive(dev, NULL);
  118. - if (!IS_ERR(mt7621_wdt_reset))
  119. - reset_control_deassert(mt7621_wdt_reset);
  120. -
  121. - mt7621_wdt_dev.bootstatus = mt7621_wdt_bootcause();
  122. -
  123. - watchdog_init_timeout(&mt7621_wdt_dev, mt7621_wdt_dev.max_timeout,
  124. - dev);
  125. - watchdog_set_nowayout(&mt7621_wdt_dev, nowayout);
  126. - if (mt7621_wdt_is_running(&mt7621_wdt_dev)) {
  127. + struct watchdog_device *mt7621_wdt;
  128. + struct mt7621_wdt_data *drvdata;
  129. + int err;
  130. +
  131. + drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  132. + if (!drvdata)
  133. + return -ENOMEM;
  134. +
  135. + drvdata->base = devm_platform_ioremap_resource(pdev, 0);
  136. + if (IS_ERR(drvdata->base))
  137. + return PTR_ERR(drvdata->base);
  138. +
  139. + drvdata->rst = devm_reset_control_get_exclusive(dev, NULL);
  140. + if (!IS_ERR(drvdata->rst))
  141. + reset_control_deassert(drvdata->rst);
  142. +
  143. + mt7621_wdt = &drvdata->wdt;
  144. + mt7621_wdt->info = &mt7621_wdt_info;
  145. + mt7621_wdt->ops = &mt7621_wdt_ops;
  146. + mt7621_wdt->min_timeout = 1;
  147. + mt7621_wdt->max_timeout = 0xfffful / 1000;
  148. + mt7621_wdt->parent = dev;
  149. +
  150. + mt7621_wdt->bootstatus = mt7621_wdt_bootcause();
  151. +
  152. + watchdog_init_timeout(mt7621_wdt, mt7621_wdt->max_timeout, dev);
  153. + watchdog_set_nowayout(mt7621_wdt, nowayout);
  154. + watchdog_set_drvdata(mt7621_wdt, drvdata);
  155. +
  156. + if (mt7621_wdt_is_running(mt7621_wdt)) {
  157. /*
  158. * Make sure to apply timeout from watchdog core, taking
  159. * the prescaler of this driver here into account (the
  160. @@ -154,17 +174,25 @@ static int mt7621_wdt_probe(struct platf
  161. * we first disable the watchdog, set the new prescaler
  162. * and timeout, and then re-enable the watchdog.
  163. */
  164. - mt7621_wdt_stop(&mt7621_wdt_dev);
  165. - mt7621_wdt_start(&mt7621_wdt_dev);
  166. - set_bit(WDOG_HW_RUNNING, &mt7621_wdt_dev.status);
  167. + mt7621_wdt_stop(mt7621_wdt);
  168. + mt7621_wdt_start(mt7621_wdt);
  169. + set_bit(WDOG_HW_RUNNING, &mt7621_wdt->status);
  170. }
  171. - return devm_watchdog_register_device(dev, &mt7621_wdt_dev);
  172. + err = devm_watchdog_register_device(dev, &drvdata->wdt);
  173. + if (err)
  174. + return err;
  175. +
  176. + platform_set_drvdata(pdev, drvdata);
  177. +
  178. + return 0;
  179. }
  180. static void mt7621_wdt_shutdown(struct platform_device *pdev)
  181. {
  182. - mt7621_wdt_stop(&mt7621_wdt_dev);
  183. + struct mt7621_wdt_data *drvdata = platform_get_drvdata(pdev);
  184. +
  185. + mt7621_wdt_stop(&drvdata->wdt);
  186. }
  187. static const struct of_device_id mt7621_wdt_match[] = {