pm-arm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARM-specific support for Broadcom STB S2/S3/S5 power management
  4. *
  5. * S2: clock gate CPUs and as many peripherals as possible
  6. * S3: power off all of the chip except the Always ON (AON) island; keep DDR is
  7. * self-refresh
  8. * S5: (a.k.a. S3 cold boot) much like S3, except DDR is powered down, so we
  9. * treat this mode like a soft power-off, with wakeup allowed from AON
  10. *
  11. * Copyright © 2014-2017 Broadcom
  12. */
  13. #define pr_fmt(fmt) "brcmstb-pm: " fmt
  14. #include <linux/bitops.h>
  15. #include <linux/compiler.h>
  16. #include <linux/delay.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/ioport.h>
  22. #include <linux/kconfig.h>
  23. #include <linux/kernel.h>
  24. #include <linux/memblock.h>
  25. #include <linux/module.h>
  26. #include <linux/notifier.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm.h>
  31. #include <linux/printk.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/sizes.h>
  34. #include <linux/slab.h>
  35. #include <linux/sort.h>
  36. #include <linux/suspend.h>
  37. #include <linux/types.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/soc/brcmstb/brcmstb.h>
  40. #include <asm/fncpy.h>
  41. #include <asm/setup.h>
  42. #include <asm/suspend.h>
  43. #include "pm.h"
  44. #include "aon_defs.h"
  45. #define SHIMPHY_DDR_PAD_CNTRL 0x8c
  46. /* Method #0 */
  47. #define SHIMPHY_PAD_PLL_SEQUENCE BIT(8)
  48. #define SHIMPHY_PAD_GATE_PLL_S3 BIT(9)
  49. /* Method #1 */
  50. #define PWRDWN_SEQ_NO_SEQUENCING 0
  51. #define PWRDWN_SEQ_HOLD_CHANNEL 1
  52. #define PWRDWN_SEQ_RESET_PLL 2
  53. #define PWRDWN_SEQ_POWERDOWN_PLL 3
  54. #define SHIMPHY_PAD_S3_PWRDWN_SEQ_MASK 0x00f00000
  55. #define SHIMPHY_PAD_S3_PWRDWN_SEQ_SHIFT 20
  56. #define DDR_FORCE_CKE_RST_N BIT(3)
  57. #define DDR_PHY_RST_N BIT(2)
  58. #define DDR_PHY_CKE BIT(1)
  59. #define DDR_PHY_NO_CHANNEL 0xffffffff
  60. #define MAX_NUM_MEMC 3
  61. struct brcmstb_memc {
  62. void __iomem *ddr_phy_base;
  63. void __iomem *ddr_shimphy_base;
  64. void __iomem *ddr_ctrl;
  65. };
  66. struct brcmstb_pm_control {
  67. void __iomem *aon_ctrl_base;
  68. void __iomem *aon_sram;
  69. struct brcmstb_memc memcs[MAX_NUM_MEMC];
  70. void __iomem *boot_sram;
  71. size_t boot_sram_len;
  72. bool support_warm_boot;
  73. size_t pll_status_offset;
  74. int num_memc;
  75. struct brcmstb_s3_params *s3_params;
  76. dma_addr_t s3_params_pa;
  77. int s3entry_method;
  78. u32 warm_boot_offset;
  79. u32 phy_a_standby_ctrl_offs;
  80. u32 phy_b_standby_ctrl_offs;
  81. bool needs_ddr_pad;
  82. struct platform_device *pdev;
  83. };
  84. enum bsp_initiate_command {
  85. BSP_CLOCK_STOP = 0x00,
  86. BSP_GEN_RANDOM_KEY = 0x4A,
  87. BSP_RESTORE_RANDOM_KEY = 0x55,
  88. BSP_GEN_FIXED_KEY = 0x63,
  89. };
  90. #define PM_INITIATE 0x01
  91. #define PM_INITIATE_SUCCESS 0x00
  92. #define PM_INITIATE_FAIL 0xfe
  93. static struct brcmstb_pm_control ctrl;
  94. static int (*brcmstb_pm_do_s2_sram)(void __iomem *aon_ctrl_base,
  95. void __iomem *ddr_phy_pll_status);
  96. static int brcmstb_init_sram(struct device_node *dn)
  97. {
  98. void __iomem *sram;
  99. struct resource res;
  100. int ret;
  101. ret = of_address_to_resource(dn, 0, &res);
  102. if (ret)
  103. return ret;
  104. /* Uncached, executable remapping of SRAM */
  105. sram = __arm_ioremap_exec(res.start, resource_size(&res), false);
  106. if (!sram)
  107. return -ENOMEM;
  108. ctrl.boot_sram = sram;
  109. ctrl.boot_sram_len = resource_size(&res);
  110. return 0;
  111. }
  112. static const struct of_device_id sram_dt_ids[] = {
  113. { .compatible = "mmio-sram" },
  114. { /* sentinel */ }
  115. };
  116. static int do_bsp_initiate_command(enum bsp_initiate_command cmd)
  117. {
  118. void __iomem *base = ctrl.aon_ctrl_base;
  119. int ret;
  120. int timeo = 1000 * 1000; /* 1 second */
  121. writel_relaxed(0, base + AON_CTRL_PM_INITIATE);
  122. (void)readl_relaxed(base + AON_CTRL_PM_INITIATE);
  123. /* Go! */
  124. writel_relaxed((cmd << 1) | PM_INITIATE, base + AON_CTRL_PM_INITIATE);
  125. /*
  126. * If firmware doesn't support the 'ack', then just assume it's done
  127. * after 10ms. Note that this only works for command 0, BSP_CLOCK_STOP
  128. */
  129. if (of_machine_is_compatible("brcm,bcm74371a0")) {
  130. (void)readl_relaxed(base + AON_CTRL_PM_INITIATE);
  131. mdelay(10);
  132. return 0;
  133. }
  134. for (;;) {
  135. ret = readl_relaxed(base + AON_CTRL_PM_INITIATE);
  136. if (!(ret & PM_INITIATE))
  137. break;
  138. if (timeo <= 0) {
  139. pr_err("error: timeout waiting for BSP (%x)\n", ret);
  140. break;
  141. }
  142. timeo -= 50;
  143. udelay(50);
  144. }
  145. return (ret & 0xff) != PM_INITIATE_SUCCESS;
  146. }
  147. static int brcmstb_pm_handshake(void)
  148. {
  149. void __iomem *base = ctrl.aon_ctrl_base;
  150. u32 tmp;
  151. int ret;
  152. /* BSP power handshake, v1 */
  153. tmp = readl_relaxed(base + AON_CTRL_HOST_MISC_CMDS);
  154. tmp &= ~1UL;
  155. writel_relaxed(tmp, base + AON_CTRL_HOST_MISC_CMDS);
  156. (void)readl_relaxed(base + AON_CTRL_HOST_MISC_CMDS);
  157. ret = do_bsp_initiate_command(BSP_CLOCK_STOP);
  158. if (ret)
  159. pr_err("BSP handshake failed\n");
  160. /*
  161. * HACK: BSP may have internal race on the CLOCK_STOP command.
  162. * Avoid touching the BSP for a few milliseconds.
  163. */
  164. mdelay(3);
  165. return ret;
  166. }
  167. static inline void shimphy_set(u32 value, u32 mask)
  168. {
  169. int i;
  170. if (!ctrl.needs_ddr_pad)
  171. return;
  172. for (i = 0; i < ctrl.num_memc; i++) {
  173. u32 tmp;
  174. tmp = readl_relaxed(ctrl.memcs[i].ddr_shimphy_base +
  175. SHIMPHY_DDR_PAD_CNTRL);
  176. tmp = value | (tmp & mask);
  177. writel_relaxed(tmp, ctrl.memcs[i].ddr_shimphy_base +
  178. SHIMPHY_DDR_PAD_CNTRL);
  179. }
  180. wmb(); /* Complete sequence in order. */
  181. }
  182. static inline void ddr_ctrl_set(bool warmboot)
  183. {
  184. int i;
  185. for (i = 0; i < ctrl.num_memc; i++) {
  186. u32 tmp;
  187. tmp = readl_relaxed(ctrl.memcs[i].ddr_ctrl +
  188. ctrl.warm_boot_offset);
  189. if (warmboot)
  190. tmp |= 1;
  191. else
  192. tmp &= ~1; /* Cold boot */
  193. writel_relaxed(tmp, ctrl.memcs[i].ddr_ctrl +
  194. ctrl.warm_boot_offset);
  195. }
  196. /* Complete sequence in order */
  197. wmb();
  198. }
  199. static inline void s3entry_method0(void)
  200. {
  201. shimphy_set(SHIMPHY_PAD_GATE_PLL_S3 | SHIMPHY_PAD_PLL_SEQUENCE,
  202. 0xffffffff);
  203. }
  204. static inline void s3entry_method1(void)
  205. {
  206. /*
  207. * S3 Entry Sequence
  208. * -----------------
  209. * Step 1: SHIMPHY_ADDR_CNTL_0_DDR_PAD_CNTRL [ S3_PWRDWN_SEQ ] = 3
  210. * Step 2: MEMC_DDR_0_WARM_BOOT [ WARM_BOOT ] = 1
  211. */
  212. shimphy_set((PWRDWN_SEQ_POWERDOWN_PLL <<
  213. SHIMPHY_PAD_S3_PWRDWN_SEQ_SHIFT),
  214. ~SHIMPHY_PAD_S3_PWRDWN_SEQ_MASK);
  215. ddr_ctrl_set(true);
  216. }
  217. static inline void s5entry_method1(void)
  218. {
  219. int i;
  220. /*
  221. * S5 Entry Sequence
  222. * -----------------
  223. * Step 1: SHIMPHY_ADDR_CNTL_0_DDR_PAD_CNTRL [ S3_PWRDWN_SEQ ] = 3
  224. * Step 2: MEMC_DDR_0_WARM_BOOT [ WARM_BOOT ] = 0
  225. * Step 3: DDR_PHY_CONTROL_REGS_[AB]_0_STANDBY_CONTROL[ CKE ] = 0
  226. * DDR_PHY_CONTROL_REGS_[AB]_0_STANDBY_CONTROL[ RST_N ] = 0
  227. */
  228. shimphy_set((PWRDWN_SEQ_POWERDOWN_PLL <<
  229. SHIMPHY_PAD_S3_PWRDWN_SEQ_SHIFT),
  230. ~SHIMPHY_PAD_S3_PWRDWN_SEQ_MASK);
  231. ddr_ctrl_set(false);
  232. for (i = 0; i < ctrl.num_memc; i++) {
  233. u32 tmp;
  234. /* Step 3: Channel A (RST_N = CKE = 0) */
  235. tmp = readl_relaxed(ctrl.memcs[i].ddr_phy_base +
  236. ctrl.phy_a_standby_ctrl_offs);
  237. tmp &= ~(DDR_PHY_RST_N | DDR_PHY_RST_N);
  238. writel_relaxed(tmp, ctrl.memcs[i].ddr_phy_base +
  239. ctrl.phy_a_standby_ctrl_offs);
  240. /* Step 3: Channel B? */
  241. if (ctrl.phy_b_standby_ctrl_offs != DDR_PHY_NO_CHANNEL) {
  242. tmp = readl_relaxed(ctrl.memcs[i].ddr_phy_base +
  243. ctrl.phy_b_standby_ctrl_offs);
  244. tmp &= ~(DDR_PHY_RST_N | DDR_PHY_RST_N);
  245. writel_relaxed(tmp, ctrl.memcs[i].ddr_phy_base +
  246. ctrl.phy_b_standby_ctrl_offs);
  247. }
  248. }
  249. /* Must complete */
  250. wmb();
  251. }
  252. /*
  253. * Run a Power Management State Machine (PMSM) shutdown command and put the CPU
  254. * into a low-power mode
  255. */
  256. static void brcmstb_do_pmsm_power_down(unsigned long base_cmd, bool onewrite)
  257. {
  258. void __iomem *base = ctrl.aon_ctrl_base;
  259. if ((ctrl.s3entry_method == 1) && (base_cmd == PM_COLD_CONFIG))
  260. s5entry_method1();
  261. /* pm_start_pwrdn transition 0->1 */
  262. writel_relaxed(base_cmd, base + AON_CTRL_PM_CTRL);
  263. if (!onewrite) {
  264. (void)readl_relaxed(base + AON_CTRL_PM_CTRL);
  265. writel_relaxed(base_cmd | PM_PWR_DOWN, base + AON_CTRL_PM_CTRL);
  266. (void)readl_relaxed(base + AON_CTRL_PM_CTRL);
  267. }
  268. wfi();
  269. }
  270. /* Support S5 cold boot out of "poweroff" */
  271. static void brcmstb_pm_poweroff(void)
  272. {
  273. brcmstb_pm_handshake();
  274. /* Clear magic S3 warm-boot value */
  275. writel_relaxed(0, ctrl.aon_sram + AON_REG_MAGIC_FLAGS);
  276. (void)readl_relaxed(ctrl.aon_sram + AON_REG_MAGIC_FLAGS);
  277. /* Skip wait-for-interrupt signal; just use a countdown */
  278. writel_relaxed(0x10, ctrl.aon_ctrl_base + AON_CTRL_PM_CPU_WAIT_COUNT);
  279. (void)readl_relaxed(ctrl.aon_ctrl_base + AON_CTRL_PM_CPU_WAIT_COUNT);
  280. if (ctrl.s3entry_method == 1) {
  281. shimphy_set((PWRDWN_SEQ_POWERDOWN_PLL <<
  282. SHIMPHY_PAD_S3_PWRDWN_SEQ_SHIFT),
  283. ~SHIMPHY_PAD_S3_PWRDWN_SEQ_MASK);
  284. ddr_ctrl_set(false);
  285. brcmstb_do_pmsm_power_down(M1_PM_COLD_CONFIG, true);
  286. return; /* We should never actually get here */
  287. }
  288. brcmstb_do_pmsm_power_down(PM_COLD_CONFIG, false);
  289. }
  290. static void *brcmstb_pm_copy_to_sram(void *fn, size_t len)
  291. {
  292. unsigned int size = ALIGN(len, FNCPY_ALIGN);
  293. if (ctrl.boot_sram_len < size) {
  294. pr_err("standby code will not fit in SRAM\n");
  295. return NULL;
  296. }
  297. return fncpy(ctrl.boot_sram, fn, size);
  298. }
  299. /*
  300. * S2 suspend/resume picks up where we left off, so we must execute carefully
  301. * from SRAM, in order to allow DDR to come back up safely before we continue.
  302. */
  303. static int brcmstb_pm_s2(void)
  304. {
  305. /* A previous S3 can set a value hazardous to S2, so make sure. */
  306. if (ctrl.s3entry_method == 1) {
  307. shimphy_set((PWRDWN_SEQ_NO_SEQUENCING <<
  308. SHIMPHY_PAD_S3_PWRDWN_SEQ_SHIFT),
  309. ~SHIMPHY_PAD_S3_PWRDWN_SEQ_MASK);
  310. ddr_ctrl_set(false);
  311. }
  312. brcmstb_pm_do_s2_sram = brcmstb_pm_copy_to_sram(&brcmstb_pm_do_s2,
  313. brcmstb_pm_do_s2_sz);
  314. if (!brcmstb_pm_do_s2_sram)
  315. return -EINVAL;
  316. return brcmstb_pm_do_s2_sram(ctrl.aon_ctrl_base,
  317. ctrl.memcs[0].ddr_phy_base +
  318. ctrl.pll_status_offset);
  319. }
  320. /*
  321. * This function is called on a new stack, so don't allow inlining (which will
  322. * generate stack references on the old stack). It cannot be made static because
  323. * it is referenced from brcmstb_pm_s3()
  324. */
  325. noinline int brcmstb_pm_s3_finish(void)
  326. {
  327. struct brcmstb_s3_params *params = ctrl.s3_params;
  328. dma_addr_t params_pa = ctrl.s3_params_pa;
  329. phys_addr_t reentry = virt_to_phys(&cpu_resume_arm);
  330. enum bsp_initiate_command cmd;
  331. u32 flags;
  332. /*
  333. * Clear parameter structure, but not DTU area, which has already been
  334. * filled in. We know DTU is a the end, so we can just subtract its
  335. * size.
  336. */
  337. memset(params, 0, sizeof(*params) - sizeof(params->dtu));
  338. flags = readl_relaxed(ctrl.aon_sram + AON_REG_MAGIC_FLAGS);
  339. flags &= S3_BOOTLOADER_RESERVED;
  340. flags |= S3_FLAG_NO_MEM_VERIFY;
  341. flags |= S3_FLAG_LOAD_RANDKEY;
  342. /* Load random / fixed key */
  343. if (flags & S3_FLAG_LOAD_RANDKEY)
  344. cmd = BSP_GEN_RANDOM_KEY;
  345. else
  346. cmd = BSP_GEN_FIXED_KEY;
  347. if (do_bsp_initiate_command(cmd)) {
  348. pr_info("key loading failed\n");
  349. return -EIO;
  350. }
  351. params->magic = BRCMSTB_S3_MAGIC;
  352. params->reentry = reentry;
  353. /* No more writes to DRAM */
  354. flush_cache_all();
  355. flags |= BRCMSTB_S3_MAGIC_SHORT;
  356. writel_relaxed(flags, ctrl.aon_sram + AON_REG_MAGIC_FLAGS);
  357. writel_relaxed(lower_32_bits(params_pa),
  358. ctrl.aon_sram + AON_REG_CONTROL_LOW);
  359. writel_relaxed(upper_32_bits(params_pa),
  360. ctrl.aon_sram + AON_REG_CONTROL_HIGH);
  361. switch (ctrl.s3entry_method) {
  362. case 0:
  363. s3entry_method0();
  364. brcmstb_do_pmsm_power_down(PM_WARM_CONFIG, false);
  365. break;
  366. case 1:
  367. s3entry_method1();
  368. brcmstb_do_pmsm_power_down(M1_PM_WARM_CONFIG, true);
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. /* Must have been interrupted from wfi()? */
  374. return -EINTR;
  375. }
  376. static int brcmstb_pm_do_s3(unsigned long sp)
  377. {
  378. unsigned long save_sp;
  379. int ret;
  380. asm volatile (
  381. "mov %[save], sp\n"
  382. "mov sp, %[new]\n"
  383. "bl brcmstb_pm_s3_finish\n"
  384. "mov %[ret], r0\n"
  385. "mov %[new], sp\n"
  386. "mov sp, %[save]\n"
  387. : [save] "=&r" (save_sp), [ret] "=&r" (ret)
  388. : [new] "r" (sp)
  389. );
  390. return ret;
  391. }
  392. static int brcmstb_pm_s3(void)
  393. {
  394. void __iomem *sp = ctrl.boot_sram + ctrl.boot_sram_len;
  395. return cpu_suspend((unsigned long)sp, brcmstb_pm_do_s3);
  396. }
  397. static int brcmstb_pm_standby(bool deep_standby)
  398. {
  399. int ret;
  400. if (brcmstb_pm_handshake())
  401. return -EIO;
  402. if (deep_standby)
  403. ret = brcmstb_pm_s3();
  404. else
  405. ret = brcmstb_pm_s2();
  406. if (ret)
  407. pr_err("%s: standby failed\n", __func__);
  408. return ret;
  409. }
  410. static int brcmstb_pm_enter(suspend_state_t state)
  411. {
  412. int ret = -EINVAL;
  413. switch (state) {
  414. case PM_SUSPEND_STANDBY:
  415. ret = brcmstb_pm_standby(false);
  416. break;
  417. case PM_SUSPEND_MEM:
  418. ret = brcmstb_pm_standby(true);
  419. break;
  420. }
  421. return ret;
  422. }
  423. static int brcmstb_pm_valid(suspend_state_t state)
  424. {
  425. switch (state) {
  426. case PM_SUSPEND_STANDBY:
  427. return true;
  428. case PM_SUSPEND_MEM:
  429. return ctrl.support_warm_boot;
  430. default:
  431. return false;
  432. }
  433. }
  434. static const struct platform_suspend_ops brcmstb_pm_ops = {
  435. .enter = brcmstb_pm_enter,
  436. .valid = brcmstb_pm_valid,
  437. };
  438. static const struct of_device_id aon_ctrl_dt_ids[] = {
  439. { .compatible = "brcm,brcmstb-aon-ctrl" },
  440. {}
  441. };
  442. struct ddr_phy_ofdata {
  443. bool supports_warm_boot;
  444. size_t pll_status_offset;
  445. int s3entry_method;
  446. u32 warm_boot_offset;
  447. u32 phy_a_standby_ctrl_offs;
  448. u32 phy_b_standby_ctrl_offs;
  449. };
  450. static struct ddr_phy_ofdata ddr_phy_71_1 = {
  451. .supports_warm_boot = true,
  452. .pll_status_offset = 0x0c,
  453. .s3entry_method = 1,
  454. .warm_boot_offset = 0x2c,
  455. .phy_a_standby_ctrl_offs = 0x198,
  456. .phy_b_standby_ctrl_offs = DDR_PHY_NO_CHANNEL
  457. };
  458. static struct ddr_phy_ofdata ddr_phy_72_0 = {
  459. .supports_warm_boot = true,
  460. .pll_status_offset = 0x10,
  461. .s3entry_method = 1,
  462. .warm_boot_offset = 0x40,
  463. .phy_a_standby_ctrl_offs = 0x2a4,
  464. .phy_b_standby_ctrl_offs = 0x8a4
  465. };
  466. static struct ddr_phy_ofdata ddr_phy_225_1 = {
  467. .supports_warm_boot = false,
  468. .pll_status_offset = 0x4,
  469. .s3entry_method = 0
  470. };
  471. static struct ddr_phy_ofdata ddr_phy_240_1 = {
  472. .supports_warm_boot = true,
  473. .pll_status_offset = 0x4,
  474. .s3entry_method = 0
  475. };
  476. static const struct of_device_id ddr_phy_dt_ids[] = {
  477. {
  478. .compatible = "brcm,brcmstb-ddr-phy-v71.1",
  479. .data = &ddr_phy_71_1,
  480. },
  481. {
  482. .compatible = "brcm,brcmstb-ddr-phy-v72.0",
  483. .data = &ddr_phy_72_0,
  484. },
  485. {
  486. .compatible = "brcm,brcmstb-ddr-phy-v225.1",
  487. .data = &ddr_phy_225_1,
  488. },
  489. {
  490. .compatible = "brcm,brcmstb-ddr-phy-v240.1",
  491. .data = &ddr_phy_240_1,
  492. },
  493. {
  494. /* Same as v240.1, for the registers we care about */
  495. .compatible = "brcm,brcmstb-ddr-phy-v240.2",
  496. .data = &ddr_phy_240_1,
  497. },
  498. {}
  499. };
  500. struct ddr_seq_ofdata {
  501. bool needs_ddr_pad;
  502. u32 warm_boot_offset;
  503. };
  504. static const struct ddr_seq_ofdata ddr_seq_b22 = {
  505. .needs_ddr_pad = false,
  506. .warm_boot_offset = 0x2c,
  507. };
  508. static const struct ddr_seq_ofdata ddr_seq = {
  509. .needs_ddr_pad = true,
  510. };
  511. static const struct of_device_id ddr_shimphy_dt_ids[] = {
  512. { .compatible = "brcm,brcmstb-ddr-shimphy-v1.0" },
  513. {}
  514. };
  515. static const struct of_device_id brcmstb_memc_of_match[] = {
  516. {
  517. .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.1",
  518. .data = &ddr_seq,
  519. },
  520. {
  521. .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.2",
  522. .data = &ddr_seq_b22,
  523. },
  524. {
  525. .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.3",
  526. .data = &ddr_seq_b22,
  527. },
  528. {
  529. .compatible = "brcm,brcmstb-memc-ddr-rev-b.3.0",
  530. .data = &ddr_seq_b22,
  531. },
  532. {
  533. .compatible = "brcm,brcmstb-memc-ddr-rev-b.3.1",
  534. .data = &ddr_seq_b22,
  535. },
  536. {
  537. .compatible = "brcm,brcmstb-memc-ddr",
  538. .data = &ddr_seq,
  539. },
  540. {},
  541. };
  542. static void __iomem *brcmstb_ioremap_match(const struct of_device_id *matches,
  543. int index, const void **ofdata)
  544. {
  545. struct device_node *dn;
  546. const struct of_device_id *match;
  547. dn = of_find_matching_node_and_match(NULL, matches, &match);
  548. if (!dn)
  549. return ERR_PTR(-EINVAL);
  550. if (ofdata)
  551. *ofdata = match->data;
  552. return of_io_request_and_map(dn, index, dn->full_name);
  553. }
  554. static int brcmstb_pm_panic_notify(struct notifier_block *nb,
  555. unsigned long action, void *data)
  556. {
  557. writel_relaxed(BRCMSTB_PANIC_MAGIC, ctrl.aon_sram + AON_REG_PANIC);
  558. return NOTIFY_DONE;
  559. }
  560. static struct notifier_block brcmstb_pm_panic_nb = {
  561. .notifier_call = brcmstb_pm_panic_notify,
  562. };
  563. static int brcmstb_pm_probe(struct platform_device *pdev)
  564. {
  565. const struct ddr_phy_ofdata *ddr_phy_data;
  566. const struct ddr_seq_ofdata *ddr_seq_data;
  567. const struct of_device_id *of_id = NULL;
  568. struct device_node *dn;
  569. void __iomem *base;
  570. int ret, i;
  571. /* AON ctrl registers */
  572. base = brcmstb_ioremap_match(aon_ctrl_dt_ids, 0, NULL);
  573. if (IS_ERR(base)) {
  574. pr_err("error mapping AON_CTRL\n");
  575. return PTR_ERR(base);
  576. }
  577. ctrl.aon_ctrl_base = base;
  578. /* AON SRAM registers */
  579. base = brcmstb_ioremap_match(aon_ctrl_dt_ids, 1, NULL);
  580. if (IS_ERR(base)) {
  581. /* Assume standard offset */
  582. ctrl.aon_sram = ctrl.aon_ctrl_base +
  583. AON_CTRL_SYSTEM_DATA_RAM_OFS;
  584. } else {
  585. ctrl.aon_sram = base;
  586. }
  587. writel_relaxed(0, ctrl.aon_sram + AON_REG_PANIC);
  588. /* DDR PHY registers */
  589. base = brcmstb_ioremap_match(ddr_phy_dt_ids, 0,
  590. (const void **)&ddr_phy_data);
  591. if (IS_ERR(base)) {
  592. pr_err("error mapping DDR PHY\n");
  593. return PTR_ERR(base);
  594. }
  595. ctrl.support_warm_boot = ddr_phy_data->supports_warm_boot;
  596. ctrl.pll_status_offset = ddr_phy_data->pll_status_offset;
  597. /* Only need DDR PHY 0 for now? */
  598. ctrl.memcs[0].ddr_phy_base = base;
  599. ctrl.s3entry_method = ddr_phy_data->s3entry_method;
  600. ctrl.phy_a_standby_ctrl_offs = ddr_phy_data->phy_a_standby_ctrl_offs;
  601. ctrl.phy_b_standby_ctrl_offs = ddr_phy_data->phy_b_standby_ctrl_offs;
  602. /*
  603. * Slightly grosss to use the phy ver to get a memc,
  604. * offset but that is the only versioned things so far
  605. * we can test for.
  606. */
  607. ctrl.warm_boot_offset = ddr_phy_data->warm_boot_offset;
  608. /* DDR SHIM-PHY registers */
  609. for_each_matching_node(dn, ddr_shimphy_dt_ids) {
  610. i = ctrl.num_memc;
  611. if (i >= MAX_NUM_MEMC) {
  612. pr_warn("too many MEMCs (max %d)\n", MAX_NUM_MEMC);
  613. break;
  614. }
  615. base = of_io_request_and_map(dn, 0, dn->full_name);
  616. if (IS_ERR(base)) {
  617. if (!ctrl.support_warm_boot)
  618. break;
  619. pr_err("error mapping DDR SHIMPHY %d\n", i);
  620. return PTR_ERR(base);
  621. }
  622. ctrl.memcs[i].ddr_shimphy_base = base;
  623. ctrl.num_memc++;
  624. }
  625. /* Sequencer DRAM Param and Control Registers */
  626. i = 0;
  627. for_each_matching_node(dn, brcmstb_memc_of_match) {
  628. base = of_iomap(dn, 0);
  629. if (!base) {
  630. pr_err("error mapping DDR Sequencer %d\n", i);
  631. return -ENOMEM;
  632. }
  633. of_id = of_match_node(brcmstb_memc_of_match, dn);
  634. if (!of_id) {
  635. iounmap(base);
  636. return -EINVAL;
  637. }
  638. ddr_seq_data = of_id->data;
  639. ctrl.needs_ddr_pad = ddr_seq_data->needs_ddr_pad;
  640. /* Adjust warm boot offset based on the DDR sequencer */
  641. if (ddr_seq_data->warm_boot_offset)
  642. ctrl.warm_boot_offset = ddr_seq_data->warm_boot_offset;
  643. ctrl.memcs[i].ddr_ctrl = base;
  644. i++;
  645. }
  646. pr_debug("PM: supports warm boot:%d, method:%d, wboffs:%x\n",
  647. ctrl.support_warm_boot, ctrl.s3entry_method,
  648. ctrl.warm_boot_offset);
  649. dn = of_find_matching_node(NULL, sram_dt_ids);
  650. if (!dn) {
  651. pr_err("SRAM not found\n");
  652. return -EINVAL;
  653. }
  654. ret = brcmstb_init_sram(dn);
  655. if (ret) {
  656. pr_err("error setting up SRAM for PM\n");
  657. return ret;
  658. }
  659. ctrl.pdev = pdev;
  660. ctrl.s3_params = kmalloc(sizeof(*ctrl.s3_params), GFP_KERNEL);
  661. if (!ctrl.s3_params)
  662. return -ENOMEM;
  663. ctrl.s3_params_pa = dma_map_single(&pdev->dev, ctrl.s3_params,
  664. sizeof(*ctrl.s3_params),
  665. DMA_TO_DEVICE);
  666. if (dma_mapping_error(&pdev->dev, ctrl.s3_params_pa)) {
  667. pr_err("error mapping DMA memory\n");
  668. ret = -ENOMEM;
  669. goto out;
  670. }
  671. atomic_notifier_chain_register(&panic_notifier_list,
  672. &brcmstb_pm_panic_nb);
  673. pm_power_off = brcmstb_pm_poweroff;
  674. suspend_set_ops(&brcmstb_pm_ops);
  675. return 0;
  676. out:
  677. kfree(ctrl.s3_params);
  678. pr_warn("PM: initialization failed with code %d\n", ret);
  679. return ret;
  680. }
  681. static struct platform_driver brcmstb_pm_driver = {
  682. .driver = {
  683. .name = "brcmstb-pm",
  684. .of_match_table = aon_ctrl_dt_ids,
  685. },
  686. };
  687. static int __init brcmstb_pm_init(void)
  688. {
  689. return platform_driver_probe(&brcmstb_pm_driver,
  690. brcmstb_pm_probe);
  691. }
  692. module_init(brcmstb_pm_init);