at91-sama5d2_shdwc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Atmel SAMA5D2-Compatible Shutdown Controller (SHDWC) driver.
  3. * Found on some SoCs as the sama5d2 (obviously).
  4. *
  5. * Copyright (C) 2015 Atmel Corporation,
  6. * Nicolas Ferre <[email protected]>
  7. *
  8. * Evolved from driver at91-poweroff.c.
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. * TODO:
  15. * - addition to status of other wake-up inputs [1 - 15]
  16. * - Analog Comparator wake-up alarm
  17. * - Serial RX wake-up alarm
  18. * - low power debouncer
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/clk/at91_pmc.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/printk.h>
  28. #include <soc/at91/at91sam9_ddrsdr.h>
  29. #define SLOW_CLOCK_FREQ 32768
  30. #define AT91_SHDW_CR 0x00 /* Shut Down Control Register */
  31. #define AT91_SHDW_SHDW BIT(0) /* Shut Down command */
  32. #define AT91_SHDW_KEY (0xa5UL << 24) /* KEY Password */
  33. #define AT91_SHDW_MR 0x04 /* Shut Down Mode Register */
  34. #define AT91_SHDW_WKUPDBC_SHIFT 24
  35. #define AT91_SHDW_WKUPDBC_MASK GENMASK(26, 24)
  36. #define AT91_SHDW_WKUPDBC(x) (((x) << AT91_SHDW_WKUPDBC_SHIFT) \
  37. & AT91_SHDW_WKUPDBC_MASK)
  38. #define AT91_SHDW_SR 0x08 /* Shut Down Status Register */
  39. #define AT91_SHDW_WKUPIS_SHIFT 16
  40. #define AT91_SHDW_WKUPIS_MASK GENMASK(31, 16)
  41. #define AT91_SHDW_WKUPIS(x) ((1 << (x)) << AT91_SHDW_WKUPIS_SHIFT \
  42. & AT91_SHDW_WKUPIS_MASK)
  43. #define AT91_SHDW_WUIR 0x0c /* Shutdown Wake-up Inputs Register */
  44. #define AT91_SHDW_WKUPEN_MASK GENMASK(15, 0)
  45. #define AT91_SHDW_WKUPEN(x) ((1 << (x)) & AT91_SHDW_WKUPEN_MASK)
  46. #define AT91_SHDW_WKUPT_SHIFT 16
  47. #define AT91_SHDW_WKUPT_MASK GENMASK(31, 16)
  48. #define AT91_SHDW_WKUPT(x) ((1 << (x)) << AT91_SHDW_WKUPT_SHIFT \
  49. & AT91_SHDW_WKUPT_MASK)
  50. #define SHDW_WK_PIN(reg, cfg) ((reg) & AT91_SHDW_WKUPIS((cfg)->wkup_pin_input))
  51. #define SHDW_RTCWK(reg, cfg) (((reg) >> ((cfg)->sr_rtcwk_shift)) & 0x1)
  52. #define SHDW_RTTWK(reg, cfg) (((reg) >> ((cfg)->sr_rttwk_shift)) & 0x1)
  53. #define SHDW_RTCWKEN(cfg) (1 << ((cfg)->mr_rtcwk_shift))
  54. #define SHDW_RTTWKEN(cfg) (1 << ((cfg)->mr_rttwk_shift))
  55. #define DBC_PERIOD_US(x) DIV_ROUND_UP_ULL((1000000 * (x)), \
  56. SLOW_CLOCK_FREQ)
  57. #define SHDW_CFG_NOT_USED (32)
  58. struct shdwc_reg_config {
  59. u8 wkup_pin_input;
  60. u8 mr_rtcwk_shift;
  61. u8 mr_rttwk_shift;
  62. u8 sr_rtcwk_shift;
  63. u8 sr_rttwk_shift;
  64. };
  65. struct pmc_reg_config {
  66. u8 mckr;
  67. };
  68. struct ddrc_reg_config {
  69. u32 type_offset;
  70. u32 type_mask;
  71. };
  72. struct reg_config {
  73. struct shdwc_reg_config shdwc;
  74. struct pmc_reg_config pmc;
  75. struct ddrc_reg_config ddrc;
  76. };
  77. struct shdwc {
  78. const struct reg_config *rcfg;
  79. struct clk *sclk;
  80. void __iomem *shdwc_base;
  81. void __iomem *mpddrc_base;
  82. void __iomem *pmc_base;
  83. };
  84. /*
  85. * Hold configuration here, cannot be more than one instance of the driver
  86. * since pm_power_off itself is global.
  87. */
  88. static struct shdwc *at91_shdwc;
  89. static const unsigned long long sdwc_dbc_period[] = {
  90. 0, 3, 32, 512, 4096, 32768,
  91. };
  92. static void __init at91_wakeup_status(struct platform_device *pdev)
  93. {
  94. struct shdwc *shdw = platform_get_drvdata(pdev);
  95. const struct reg_config *rcfg = shdw->rcfg;
  96. u32 reg;
  97. char *reason = "unknown";
  98. reg = readl(shdw->shdwc_base + AT91_SHDW_SR);
  99. dev_dbg(&pdev->dev, "%s: status = %#x\n", __func__, reg);
  100. /* Simple power-on, just bail out */
  101. if (!reg)
  102. return;
  103. if (SHDW_WK_PIN(reg, &rcfg->shdwc))
  104. reason = "WKUP pin";
  105. else if (SHDW_RTCWK(reg, &rcfg->shdwc))
  106. reason = "RTC";
  107. else if (SHDW_RTTWK(reg, &rcfg->shdwc))
  108. reason = "RTT";
  109. pr_info("AT91: Wake-Up source: %s\n", reason);
  110. }
  111. static void at91_poweroff(void)
  112. {
  113. asm volatile(
  114. /* Align to cache lines */
  115. ".balign 32\n\t"
  116. /* Ensure AT91_SHDW_CR is in the TLB by reading it */
  117. " ldr r6, [%2, #" __stringify(AT91_SHDW_CR) "]\n\t"
  118. /* Power down SDRAM0 */
  119. " tst %0, #0\n\t"
  120. " beq 1f\n\t"
  121. " str %1, [%0, #" __stringify(AT91_DDRSDRC_LPR) "]\n\t"
  122. /* Switch the master clock source to slow clock. */
  123. "1: ldr r6, [%4, %5]\n\t"
  124. " bic r6, r6, #" __stringify(AT91_PMC_CSS) "\n\t"
  125. " str r6, [%4, %5]\n\t"
  126. /* Wait for clock switch. */
  127. "2: ldr r6, [%4, #" __stringify(AT91_PMC_SR) "]\n\t"
  128. " tst r6, #" __stringify(AT91_PMC_MCKRDY) "\n\t"
  129. " beq 2b\n\t"
  130. /* Shutdown CPU */
  131. " str %3, [%2, #" __stringify(AT91_SHDW_CR) "]\n\t"
  132. " b .\n\t"
  133. :
  134. : "r" (at91_shdwc->mpddrc_base),
  135. "r" cpu_to_le32(AT91_DDRSDRC_LPDDR2_PWOFF),
  136. "r" (at91_shdwc->shdwc_base),
  137. "r" cpu_to_le32(AT91_SHDW_KEY | AT91_SHDW_SHDW),
  138. "r" (at91_shdwc->pmc_base),
  139. "r" (at91_shdwc->rcfg->pmc.mckr)
  140. : "r6");
  141. }
  142. static u32 at91_shdwc_debouncer_value(struct platform_device *pdev,
  143. u32 in_period_us)
  144. {
  145. int i;
  146. int max_idx = ARRAY_SIZE(sdwc_dbc_period) - 1;
  147. unsigned long long period_us;
  148. unsigned long long max_period_us = DBC_PERIOD_US(sdwc_dbc_period[max_idx]);
  149. if (in_period_us > max_period_us) {
  150. dev_warn(&pdev->dev,
  151. "debouncer period %u too big, reduced to %llu us\n",
  152. in_period_us, max_period_us);
  153. return max_idx;
  154. }
  155. for (i = max_idx - 1; i > 0; i--) {
  156. period_us = DBC_PERIOD_US(sdwc_dbc_period[i]);
  157. dev_dbg(&pdev->dev, "%s: ref[%d] = %llu\n",
  158. __func__, i, period_us);
  159. if (in_period_us > period_us)
  160. break;
  161. }
  162. return i + 1;
  163. }
  164. static u32 at91_shdwc_get_wakeup_input(struct platform_device *pdev,
  165. struct device_node *np)
  166. {
  167. struct device_node *cnp;
  168. u32 wk_input_mask;
  169. u32 wuir = 0;
  170. u32 wk_input;
  171. for_each_child_of_node(np, cnp) {
  172. if (of_property_read_u32(cnp, "reg", &wk_input)) {
  173. dev_warn(&pdev->dev, "reg property is missing for %pOF\n",
  174. cnp);
  175. continue;
  176. }
  177. wk_input_mask = 1 << wk_input;
  178. if (!(wk_input_mask & AT91_SHDW_WKUPEN_MASK)) {
  179. dev_warn(&pdev->dev,
  180. "wake-up input %d out of bounds ignore\n",
  181. wk_input);
  182. continue;
  183. }
  184. wuir |= wk_input_mask;
  185. if (of_property_read_bool(cnp, "atmel,wakeup-active-high"))
  186. wuir |= AT91_SHDW_WKUPT(wk_input);
  187. dev_dbg(&pdev->dev, "%s: (child %d) wuir = %#x\n",
  188. __func__, wk_input, wuir);
  189. }
  190. return wuir;
  191. }
  192. static void at91_shdwc_dt_configure(struct platform_device *pdev)
  193. {
  194. struct shdwc *shdw = platform_get_drvdata(pdev);
  195. const struct reg_config *rcfg = shdw->rcfg;
  196. struct device_node *np = pdev->dev.of_node;
  197. u32 mode = 0, tmp, input;
  198. if (!np) {
  199. dev_err(&pdev->dev, "device node not found\n");
  200. return;
  201. }
  202. if (!of_property_read_u32(np, "debounce-delay-us", &tmp))
  203. mode |= AT91_SHDW_WKUPDBC(at91_shdwc_debouncer_value(pdev, tmp));
  204. if (of_property_read_bool(np, "atmel,wakeup-rtc-timer"))
  205. mode |= SHDW_RTCWKEN(&rcfg->shdwc);
  206. if (of_property_read_bool(np, "atmel,wakeup-rtt-timer"))
  207. mode |= SHDW_RTTWKEN(&rcfg->shdwc);
  208. dev_dbg(&pdev->dev, "%s: mode = %#x\n", __func__, mode);
  209. writel(mode, shdw->shdwc_base + AT91_SHDW_MR);
  210. input = at91_shdwc_get_wakeup_input(pdev, np);
  211. writel(input, shdw->shdwc_base + AT91_SHDW_WUIR);
  212. }
  213. static const struct reg_config sama5d2_reg_config = {
  214. .shdwc = {
  215. .wkup_pin_input = 0,
  216. .mr_rtcwk_shift = 17,
  217. .mr_rttwk_shift = SHDW_CFG_NOT_USED,
  218. .sr_rtcwk_shift = 5,
  219. .sr_rttwk_shift = SHDW_CFG_NOT_USED,
  220. },
  221. .pmc = {
  222. .mckr = 0x30,
  223. },
  224. .ddrc = {
  225. .type_offset = AT91_DDRSDRC_MDR,
  226. .type_mask = AT91_DDRSDRC_MD
  227. },
  228. };
  229. static const struct reg_config sam9x60_reg_config = {
  230. .shdwc = {
  231. .wkup_pin_input = 0,
  232. .mr_rtcwk_shift = 17,
  233. .mr_rttwk_shift = 16,
  234. .sr_rtcwk_shift = 5,
  235. .sr_rttwk_shift = 4,
  236. },
  237. .pmc = {
  238. .mckr = 0x28,
  239. },
  240. .ddrc = {
  241. .type_offset = AT91_DDRSDRC_MDR,
  242. .type_mask = AT91_DDRSDRC_MD
  243. },
  244. };
  245. static const struct reg_config sama7g5_reg_config = {
  246. .shdwc = {
  247. .wkup_pin_input = 0,
  248. .mr_rtcwk_shift = 17,
  249. .mr_rttwk_shift = 16,
  250. .sr_rtcwk_shift = 5,
  251. .sr_rttwk_shift = 4,
  252. },
  253. .pmc = {
  254. .mckr = 0x28,
  255. },
  256. };
  257. static const struct of_device_id at91_shdwc_of_match[] = {
  258. {
  259. .compatible = "atmel,sama5d2-shdwc",
  260. .data = &sama5d2_reg_config,
  261. },
  262. {
  263. .compatible = "microchip,sam9x60-shdwc",
  264. .data = &sam9x60_reg_config,
  265. },
  266. {
  267. .compatible = "microchip,sama7g5-shdwc",
  268. .data = &sama7g5_reg_config,
  269. }, {
  270. /*sentinel*/
  271. }
  272. };
  273. MODULE_DEVICE_TABLE(of, at91_shdwc_of_match);
  274. static const struct of_device_id at91_pmc_ids[] = {
  275. { .compatible = "atmel,sama5d2-pmc" },
  276. { .compatible = "microchip,sam9x60-pmc" },
  277. { .compatible = "microchip,sama7g5-pmc" },
  278. { /* Sentinel. */ }
  279. };
  280. static int __init at91_shdwc_probe(struct platform_device *pdev)
  281. {
  282. struct resource *res;
  283. const struct of_device_id *match;
  284. struct device_node *np;
  285. u32 ddr_type;
  286. int ret;
  287. if (!pdev->dev.of_node)
  288. return -ENODEV;
  289. if (at91_shdwc)
  290. return -EBUSY;
  291. at91_shdwc = devm_kzalloc(&pdev->dev, sizeof(*at91_shdwc), GFP_KERNEL);
  292. if (!at91_shdwc)
  293. return -ENOMEM;
  294. platform_set_drvdata(pdev, at91_shdwc);
  295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. at91_shdwc->shdwc_base = devm_ioremap_resource(&pdev->dev, res);
  297. if (IS_ERR(at91_shdwc->shdwc_base))
  298. return PTR_ERR(at91_shdwc->shdwc_base);
  299. match = of_match_node(at91_shdwc_of_match, pdev->dev.of_node);
  300. at91_shdwc->rcfg = match->data;
  301. at91_shdwc->sclk = devm_clk_get(&pdev->dev, NULL);
  302. if (IS_ERR(at91_shdwc->sclk))
  303. return PTR_ERR(at91_shdwc->sclk);
  304. ret = clk_prepare_enable(at91_shdwc->sclk);
  305. if (ret) {
  306. dev_err(&pdev->dev, "Could not enable slow clock\n");
  307. return ret;
  308. }
  309. at91_wakeup_status(pdev);
  310. at91_shdwc_dt_configure(pdev);
  311. np = of_find_matching_node(NULL, at91_pmc_ids);
  312. if (!np) {
  313. ret = -ENODEV;
  314. goto clk_disable;
  315. }
  316. at91_shdwc->pmc_base = of_iomap(np, 0);
  317. of_node_put(np);
  318. if (!at91_shdwc->pmc_base) {
  319. ret = -ENOMEM;
  320. goto clk_disable;
  321. }
  322. if (at91_shdwc->rcfg->ddrc.type_mask) {
  323. np = of_find_compatible_node(NULL, NULL,
  324. "atmel,sama5d3-ddramc");
  325. if (!np) {
  326. ret = -ENODEV;
  327. goto unmap;
  328. }
  329. at91_shdwc->mpddrc_base = of_iomap(np, 0);
  330. of_node_put(np);
  331. if (!at91_shdwc->mpddrc_base) {
  332. ret = -ENOMEM;
  333. goto unmap;
  334. }
  335. ddr_type = readl(at91_shdwc->mpddrc_base +
  336. at91_shdwc->rcfg->ddrc.type_offset) &
  337. at91_shdwc->rcfg->ddrc.type_mask;
  338. if (ddr_type != AT91_DDRSDRC_MD_LPDDR2 &&
  339. ddr_type != AT91_DDRSDRC_MD_LPDDR3) {
  340. iounmap(at91_shdwc->mpddrc_base);
  341. at91_shdwc->mpddrc_base = NULL;
  342. }
  343. }
  344. pm_power_off = at91_poweroff;
  345. return 0;
  346. unmap:
  347. iounmap(at91_shdwc->pmc_base);
  348. clk_disable:
  349. clk_disable_unprepare(at91_shdwc->sclk);
  350. return ret;
  351. }
  352. static int __exit at91_shdwc_remove(struct platform_device *pdev)
  353. {
  354. struct shdwc *shdw = platform_get_drvdata(pdev);
  355. if (pm_power_off == at91_poweroff)
  356. pm_power_off = NULL;
  357. /* Reset values to disable wake-up features */
  358. writel(0, shdw->shdwc_base + AT91_SHDW_MR);
  359. writel(0, shdw->shdwc_base + AT91_SHDW_WUIR);
  360. if (shdw->mpddrc_base)
  361. iounmap(shdw->mpddrc_base);
  362. iounmap(shdw->pmc_base);
  363. clk_disable_unprepare(shdw->sclk);
  364. return 0;
  365. }
  366. static struct platform_driver at91_shdwc_driver = {
  367. .remove = __exit_p(at91_shdwc_remove),
  368. .driver = {
  369. .name = "at91-shdwc",
  370. .of_match_table = at91_shdwc_of_match,
  371. },
  372. };
  373. module_platform_driver_probe(at91_shdwc_driver, at91_shdwc_probe);
  374. MODULE_AUTHOR("Nicolas Ferre <[email protected]>");
  375. MODULE_DESCRIPTION("Atmel shutdown controller driver");
  376. MODULE_LICENSE("GPL v2");