1fcaf2036SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e95dddb3SAnson Huang /*
35739b919SAnson Huang * Copyright (C) 2013-2015 Freescale Semiconductor, Inc.
4261b3503SBai Ping * Copyright 2017-2018 NXP.
5e95dddb3SAnson Huang */
6e95dddb3SAnson Huang
7e95dddb3SAnson Huang #include <linux/err.h>
8e95dddb3SAnson Huang #include <linux/io.h>
9e95dddb3SAnson Huang #include <linux/of.h>
10e95dddb3SAnson Huang #include <linux/of_address.h>
11e95dddb3SAnson Huang #include <linux/mfd/syscon.h>
12e95dddb3SAnson Huang #include <linux/regmap.h>
13fcc4f9fcSFabio Estevam #include "common.h"
14f1c6f314SShawn Guo #include "hardware.h"
15e95dddb3SAnson Huang
16e95dddb3SAnson Huang #define REG_SET 0x4
17e95dddb3SAnson Huang #define REG_CLR 0x8
18e95dddb3SAnson Huang
19263475d4SAnson Huang #define ANADIG_REG_2P5 0x130
20e95dddb3SAnson Huang #define ANADIG_REG_CORE 0x140
21263475d4SAnson Huang #define ANADIG_ANA_MISC0 0x150
22e95dddb3SAnson Huang #define ANADIG_DIGPROG 0x260
23d8ce823fSShawn Guo #define ANADIG_DIGPROG_IMX6SL 0x280
245739b919SAnson Huang #define ANADIG_DIGPROG_IMX7D 0x800
25e95dddb3SAnson Huang
26c90dec00SAnson Huang #define SRC_SBMR2 0x1c
27c90dec00SAnson Huang
28263475d4SAnson Huang #define BM_ANADIG_REG_2P5_ENABLE_WEAK_LINREG 0x40000
29bc4abc3eSAnson Huang #define BM_ANADIG_REG_2P5_ENABLE_PULLDOWN 0x8
30e95dddb3SAnson Huang #define BM_ANADIG_REG_CORE_FET_ODRIVE 0x20000000
31263475d4SAnson Huang #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG 0x1000
32bc4abc3eSAnson Huang /* Below MISC0_DISCON_HIGH_SNVS is only for i.MX6SL */
33bc4abc3eSAnson Huang #define BM_ANADIG_ANA_MISC0_DISCON_HIGH_SNVS 0x2000
34e95dddb3SAnson Huang
35e95dddb3SAnson Huang static struct regmap *anatop;
36e95dddb3SAnson Huang
imx_anatop_enable_weak2p5(bool enable)37263475d4SAnson Huang static void imx_anatop_enable_weak2p5(bool enable)
38263475d4SAnson Huang {
39263475d4SAnson Huang u32 reg, val;
40263475d4SAnson Huang
41263475d4SAnson Huang regmap_read(anatop, ANADIG_ANA_MISC0, &val);
42263475d4SAnson Huang
43263475d4SAnson Huang /* can only be enabled when stop_mode_config is clear. */
44263475d4SAnson Huang reg = ANADIG_REG_2P5;
45263475d4SAnson Huang reg += (enable && (val & BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG) == 0) ?
46263475d4SAnson Huang REG_SET : REG_CLR;
47263475d4SAnson Huang regmap_write(anatop, reg, BM_ANADIG_REG_2P5_ENABLE_WEAK_LINREG);
48263475d4SAnson Huang }
49263475d4SAnson Huang
imx_anatop_enable_fet_odrive(bool enable)50e95dddb3SAnson Huang static void imx_anatop_enable_fet_odrive(bool enable)
51e95dddb3SAnson Huang {
52e95dddb3SAnson Huang regmap_write(anatop, ANADIG_REG_CORE + (enable ? REG_SET : REG_CLR),
53e95dddb3SAnson Huang BM_ANADIG_REG_CORE_FET_ODRIVE);
54e95dddb3SAnson Huang }
55e95dddb3SAnson Huang
imx_anatop_enable_2p5_pulldown(bool enable)56bc4abc3eSAnson Huang static inline void imx_anatop_enable_2p5_pulldown(bool enable)
57bc4abc3eSAnson Huang {
58bc4abc3eSAnson Huang regmap_write(anatop, ANADIG_REG_2P5 + (enable ? REG_SET : REG_CLR),
59bc4abc3eSAnson Huang BM_ANADIG_REG_2P5_ENABLE_PULLDOWN);
60bc4abc3eSAnson Huang }
61bc4abc3eSAnson Huang
imx_anatop_disconnect_high_snvs(bool enable)62bc4abc3eSAnson Huang static inline void imx_anatop_disconnect_high_snvs(bool enable)
63bc4abc3eSAnson Huang {
64bc4abc3eSAnson Huang regmap_write(anatop, ANADIG_ANA_MISC0 + (enable ? REG_SET : REG_CLR),
65bc4abc3eSAnson Huang BM_ANADIG_ANA_MISC0_DISCON_HIGH_SNVS);
66bc4abc3eSAnson Huang }
67bc4abc3eSAnson Huang
imx_anatop_pre_suspend(void)68e95dddb3SAnson Huang void imx_anatop_pre_suspend(void)
69e95dddb3SAnson Huang {
70bc4abc3eSAnson Huang if (imx_mmdc_get_ddr_type() == IMX_DDR_TYPE_LPDDR2)
71bc4abc3eSAnson Huang imx_anatop_enable_2p5_pulldown(true);
72bc4abc3eSAnson Huang else
73263475d4SAnson Huang imx_anatop_enable_weak2p5(true);
74bc4abc3eSAnson Huang
75e95dddb3SAnson Huang imx_anatop_enable_fet_odrive(true);
76bc4abc3eSAnson Huang
77bc4abc3eSAnson Huang if (cpu_is_imx6sl())
78bc4abc3eSAnson Huang imx_anatop_disconnect_high_snvs(true);
79e95dddb3SAnson Huang }
80e95dddb3SAnson Huang
imx_anatop_post_resume(void)81e95dddb3SAnson Huang void imx_anatop_post_resume(void)
82e95dddb3SAnson Huang {
83bc4abc3eSAnson Huang if (imx_mmdc_get_ddr_type() == IMX_DDR_TYPE_LPDDR2)
84bc4abc3eSAnson Huang imx_anatop_enable_2p5_pulldown(false);
85bc4abc3eSAnson Huang else
86263475d4SAnson Huang imx_anatop_enable_weak2p5(false);
87bc4abc3eSAnson Huang
88bc4abc3eSAnson Huang imx_anatop_enable_fet_odrive(false);
89bc4abc3eSAnson Huang
90bc4abc3eSAnson Huang if (cpu_is_imx6sl())
91bc4abc3eSAnson Huang imx_anatop_disconnect_high_snvs(false);
92e95dddb3SAnson Huang }
93e95dddb3SAnson Huang
imx_init_revision_from_anatop(void)94f1c6f314SShawn Guo void __init imx_init_revision_from_anatop(void)
95e95dddb3SAnson Huang {
964a4fb661SAnson Huang struct device_node *np, *src_np;
977006ba24SShawn Guo void __iomem *anatop_base;
98f1c6f314SShawn Guo unsigned int revision;
99f1c6f314SShawn Guo u32 digprog;
100d8ce823fSShawn Guo u16 offset = ANADIG_DIGPROG;
101261b3503SBai Ping u8 major_part, minor_part;
1027006ba24SShawn Guo
1037006ba24SShawn Guo np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-anatop");
1047006ba24SShawn Guo anatop_base = of_iomap(np, 0);
1057006ba24SShawn Guo WARN_ON(!anatop_base);
106d8ce823fSShawn Guo if (of_device_is_compatible(np, "fsl,imx6sl-anatop"))
107d8ce823fSShawn Guo offset = ANADIG_DIGPROG_IMX6SL;
1085739b919SAnson Huang if (of_device_is_compatible(np, "fsl,imx7d-anatop"))
1095739b919SAnson Huang offset = ANADIG_DIGPROG_IMX7D;
110d8ce823fSShawn Guo digprog = readl_relaxed(anatop_base + offset);
111f1c6f314SShawn Guo iounmap(anatop_base);
1127006ba24SShawn Guo
113c5a890a4SBai Ping /*
114261b3503SBai Ping * On i.MX7D digprog value match linux version format, so
115261b3503SBai Ping * it needn't map again and we can use register value directly.
116c5a890a4SBai Ping */
117261b3503SBai Ping if (of_device_is_compatible(np, "fsl,imx7d-anatop")) {
118e914ecebSFrank Li revision = digprog & 0xff;
119261b3503SBai Ping } else {
120261b3503SBai Ping /*
121261b3503SBai Ping * MAJOR: [15:8], the major silicon revison;
122261b3503SBai Ping * MINOR: [7: 0], the minor silicon revison;
123261b3503SBai Ping *
124261b3503SBai Ping * please refer to the i.MX RM for the detailed
125261b3503SBai Ping * silicon revison bit define.
126261b3503SBai Ping * format the major part and minor part to match the
127261b3503SBai Ping * linux kernel soc version format.
128261b3503SBai Ping */
129261b3503SBai Ping major_part = (digprog >> 8) & 0xf;
130261b3503SBai Ping minor_part = digprog & 0xf;
131261b3503SBai Ping revision = ((major_part + 1) << 4) | minor_part;
132c90dec00SAnson Huang
133c90dec00SAnson Huang if ((digprog >> 16) == MXC_CPU_IMX6ULL) {
134c90dec00SAnson Huang void __iomem *src_base;
135c90dec00SAnson Huang u32 sbmr2;
136c90dec00SAnson Huang
1374a4fb661SAnson Huang src_np = of_find_compatible_node(NULL, NULL,
138c90dec00SAnson Huang "fsl,imx6ul-src");
139*70e734feSRobert Karszniewicz src_base = of_iomap(src_np, 0);
1404a4fb661SAnson Huang of_node_put(src_np);
141c90dec00SAnson Huang WARN_ON(!src_base);
142c90dec00SAnson Huang sbmr2 = readl_relaxed(src_base + SRC_SBMR2);
143c90dec00SAnson Huang iounmap(src_base);
144c90dec00SAnson Huang
145c90dec00SAnson Huang /* src_sbmr2 bit 6 is to identify if it is i.MX6ULZ */
146c90dec00SAnson Huang if (sbmr2 & (1 << 6)) {
147c90dec00SAnson Huang digprog &= ~(0xff << 16);
148c90dec00SAnson Huang digprog |= (MXC_CPU_IMX6ULZ << 16);
149c90dec00SAnson Huang }
150c90dec00SAnson Huang }
151f1c6f314SShawn Guo }
1524a4fb661SAnson Huang of_node_put(np);
153f1c6f314SShawn Guo
154f1c6f314SShawn Guo mxc_set_cpu_type(digprog >> 16 & 0xff);
155f1c6f314SShawn Guo imx_set_soc_revision(revision);
156e95dddb3SAnson Huang }
157e95dddb3SAnson Huang
imx_anatop_init(void)158e95dddb3SAnson Huang void __init imx_anatop_init(void)
159e95dddb3SAnson Huang {
160e95dddb3SAnson Huang anatop = syscon_regmap_lookup_by_compatible("fsl,imx6q-anatop");
161427fca60SAndrey Smirnov if (IS_ERR(anatop))
162e95dddb3SAnson Huang pr_err("%s: failed to find imx6q-anatop regmap!\n", __func__);
163e95dddb3SAnson Huang }
164