1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2013-2015 Freescale Semiconductor, Inc.
4*4882a593Smuzhiyun * Copyright 2017-2018 NXP.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/err.h>
8*4882a593Smuzhiyun #include <linux/io.h>
9*4882a593Smuzhiyun #include <linux/of.h>
10*4882a593Smuzhiyun #include <linux/of_address.h>
11*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
12*4882a593Smuzhiyun #include <linux/regmap.h>
13*4882a593Smuzhiyun #include "common.h"
14*4882a593Smuzhiyun #include "hardware.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define REG_SET 0x4
17*4882a593Smuzhiyun #define REG_CLR 0x8
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define ANADIG_REG_2P5 0x130
20*4882a593Smuzhiyun #define ANADIG_REG_CORE 0x140
21*4882a593Smuzhiyun #define ANADIG_ANA_MISC0 0x150
22*4882a593Smuzhiyun #define ANADIG_DIGPROG 0x260
23*4882a593Smuzhiyun #define ANADIG_DIGPROG_IMX6SL 0x280
24*4882a593Smuzhiyun #define ANADIG_DIGPROG_IMX7D 0x800
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define SRC_SBMR2 0x1c
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define BM_ANADIG_REG_2P5_ENABLE_WEAK_LINREG 0x40000
29*4882a593Smuzhiyun #define BM_ANADIG_REG_2P5_ENABLE_PULLDOWN 0x8
30*4882a593Smuzhiyun #define BM_ANADIG_REG_CORE_FET_ODRIVE 0x20000000
31*4882a593Smuzhiyun #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG 0x1000
32*4882a593Smuzhiyun /* Below MISC0_DISCON_HIGH_SNVS is only for i.MX6SL */
33*4882a593Smuzhiyun #define BM_ANADIG_ANA_MISC0_DISCON_HIGH_SNVS 0x2000
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static struct regmap *anatop;
36*4882a593Smuzhiyun
imx_anatop_enable_weak2p5(bool enable)37*4882a593Smuzhiyun static void imx_anatop_enable_weak2p5(bool enable)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun u32 reg, val;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun regmap_read(anatop, ANADIG_ANA_MISC0, &val);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* can only be enabled when stop_mode_config is clear. */
44*4882a593Smuzhiyun reg = ANADIG_REG_2P5;
45*4882a593Smuzhiyun reg += (enable && (val & BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG) == 0) ?
46*4882a593Smuzhiyun REG_SET : REG_CLR;
47*4882a593Smuzhiyun regmap_write(anatop, reg, BM_ANADIG_REG_2P5_ENABLE_WEAK_LINREG);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
imx_anatop_enable_fet_odrive(bool enable)50*4882a593Smuzhiyun static void imx_anatop_enable_fet_odrive(bool enable)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun regmap_write(anatop, ANADIG_REG_CORE + (enable ? REG_SET : REG_CLR),
53*4882a593Smuzhiyun BM_ANADIG_REG_CORE_FET_ODRIVE);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
imx_anatop_enable_2p5_pulldown(bool enable)56*4882a593Smuzhiyun static inline void imx_anatop_enable_2p5_pulldown(bool enable)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun regmap_write(anatop, ANADIG_REG_2P5 + (enable ? REG_SET : REG_CLR),
59*4882a593Smuzhiyun BM_ANADIG_REG_2P5_ENABLE_PULLDOWN);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
imx_anatop_disconnect_high_snvs(bool enable)62*4882a593Smuzhiyun static inline void imx_anatop_disconnect_high_snvs(bool enable)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun regmap_write(anatop, ANADIG_ANA_MISC0 + (enable ? REG_SET : REG_CLR),
65*4882a593Smuzhiyun BM_ANADIG_ANA_MISC0_DISCON_HIGH_SNVS);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
imx_anatop_pre_suspend(void)68*4882a593Smuzhiyun void imx_anatop_pre_suspend(void)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun if (imx_mmdc_get_ddr_type() == IMX_DDR_TYPE_LPDDR2)
71*4882a593Smuzhiyun imx_anatop_enable_2p5_pulldown(true);
72*4882a593Smuzhiyun else
73*4882a593Smuzhiyun imx_anatop_enable_weak2p5(true);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun imx_anatop_enable_fet_odrive(true);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (cpu_is_imx6sl())
78*4882a593Smuzhiyun imx_anatop_disconnect_high_snvs(true);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
imx_anatop_post_resume(void)81*4882a593Smuzhiyun void imx_anatop_post_resume(void)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun if (imx_mmdc_get_ddr_type() == IMX_DDR_TYPE_LPDDR2)
84*4882a593Smuzhiyun imx_anatop_enable_2p5_pulldown(false);
85*4882a593Smuzhiyun else
86*4882a593Smuzhiyun imx_anatop_enable_weak2p5(false);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun imx_anatop_enable_fet_odrive(false);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (cpu_is_imx6sl())
91*4882a593Smuzhiyun imx_anatop_disconnect_high_snvs(false);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
imx_init_revision_from_anatop(void)94*4882a593Smuzhiyun void __init imx_init_revision_from_anatop(void)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun struct device_node *np, *src_np;
97*4882a593Smuzhiyun void __iomem *anatop_base;
98*4882a593Smuzhiyun unsigned int revision;
99*4882a593Smuzhiyun u32 digprog;
100*4882a593Smuzhiyun u16 offset = ANADIG_DIGPROG;
101*4882a593Smuzhiyun u8 major_part, minor_part;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-anatop");
104*4882a593Smuzhiyun anatop_base = of_iomap(np, 0);
105*4882a593Smuzhiyun WARN_ON(!anatop_base);
106*4882a593Smuzhiyun if (of_device_is_compatible(np, "fsl,imx6sl-anatop"))
107*4882a593Smuzhiyun offset = ANADIG_DIGPROG_IMX6SL;
108*4882a593Smuzhiyun if (of_device_is_compatible(np, "fsl,imx7d-anatop"))
109*4882a593Smuzhiyun offset = ANADIG_DIGPROG_IMX7D;
110*4882a593Smuzhiyun digprog = readl_relaxed(anatop_base + offset);
111*4882a593Smuzhiyun iounmap(anatop_base);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * On i.MX7D digprog value match linux version format, so
115*4882a593Smuzhiyun * it needn't map again and we can use register value directly.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun if (of_device_is_compatible(np, "fsl,imx7d-anatop")) {
118*4882a593Smuzhiyun revision = digprog & 0xff;
119*4882a593Smuzhiyun } else {
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * MAJOR: [15:8], the major silicon revison;
122*4882a593Smuzhiyun * MINOR: [7: 0], the minor silicon revison;
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * please refer to the i.MX RM for the detailed
125*4882a593Smuzhiyun * silicon revison bit define.
126*4882a593Smuzhiyun * format the major part and minor part to match the
127*4882a593Smuzhiyun * linux kernel soc version format.
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun major_part = (digprog >> 8) & 0xf;
130*4882a593Smuzhiyun minor_part = digprog & 0xf;
131*4882a593Smuzhiyun revision = ((major_part + 1) << 4) | minor_part;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if ((digprog >> 16) == MXC_CPU_IMX6ULL) {
134*4882a593Smuzhiyun void __iomem *src_base;
135*4882a593Smuzhiyun u32 sbmr2;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun src_np = of_find_compatible_node(NULL, NULL,
138*4882a593Smuzhiyun "fsl,imx6ul-src");
139*4882a593Smuzhiyun src_base = of_iomap(src_np, 0);
140*4882a593Smuzhiyun of_node_put(src_np);
141*4882a593Smuzhiyun WARN_ON(!src_base);
142*4882a593Smuzhiyun sbmr2 = readl_relaxed(src_base + SRC_SBMR2);
143*4882a593Smuzhiyun iounmap(src_base);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /* src_sbmr2 bit 6 is to identify if it is i.MX6ULZ */
146*4882a593Smuzhiyun if (sbmr2 & (1 << 6)) {
147*4882a593Smuzhiyun digprog &= ~(0xff << 16);
148*4882a593Smuzhiyun digprog |= (MXC_CPU_IMX6ULZ << 16);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun of_node_put(np);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun mxc_set_cpu_type(digprog >> 16 & 0xff);
155*4882a593Smuzhiyun imx_set_soc_revision(revision);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
imx_anatop_init(void)158*4882a593Smuzhiyun void __init imx_anatop_init(void)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun anatop = syscon_regmap_lookup_by_compatible("fsl,imx6q-anatop");
161*4882a593Smuzhiyun if (IS_ERR(anatop))
162*4882a593Smuzhiyun pr_err("%s: failed to find imx6q-anatop regmap!\n", __func__);
163*4882a593Smuzhiyun }
164