staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / soc / bcm / brcmstb / biuctrl.c
blobd326915e0f40f38322d5fe0fe742832254341ee8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Broadcom STB SoCs Bus Unit Interface controls
5 * Copyright (C) 2015, Broadcom Corporation
6 */
8 #define pr_fmt(fmt) "brcmstb: " KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/of_address.h>
13 #include <linux/syscore_ops.h>
14 #include <linux/soc/brcmstb/brcmstb.h>
16 #define CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK 0x70000000
17 #define CPU_CREDIT_REG_MCPx_READ_CRED_MASK 0xf
18 #define CPU_CREDIT_REG_MCPx_WRITE_CRED_MASK 0xf
19 #define CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(x) ((x) * 8)
20 #define CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(x) (((x) * 8) + 4)
22 #define CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_SHIFT(x) ((x) * 8)
23 #define CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_MASK 0xff
25 #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_THRESHOLD_MASK 0xf
26 #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_MASK 0xf
27 #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT 4
28 #define CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_ENABLE BIT(8)
30 static void __iomem *cpubiuctrl_base;
31 static bool mcp_wr_pairing_en;
32 static const int *cpubiuctrl_regs;
34 static inline u32 cbc_readl(int reg)
36 int offset = cpubiuctrl_regs[reg];
38 if (offset == -1)
39 return (u32)-1;
41 return readl_relaxed(cpubiuctrl_base + offset);
44 static inline void cbc_writel(u32 val, int reg)
46 int offset = cpubiuctrl_regs[reg];
48 if (offset == -1)
49 return;
51 writel(val, cpubiuctrl_base + offset);
54 enum cpubiuctrl_regs {
55 CPU_CREDIT_REG = 0,
56 CPU_MCP_FLOW_REG,
57 CPU_WRITEBACK_CTRL_REG
60 static const int b15_cpubiuctrl_regs[] = {
61 [CPU_CREDIT_REG] = 0x184,
62 [CPU_MCP_FLOW_REG] = -1,
63 [CPU_WRITEBACK_CTRL_REG] = -1,
66 /* Odd cases, e.g: 7260 */
67 static const int b53_cpubiuctrl_no_wb_regs[] = {
68 [CPU_CREDIT_REG] = 0x0b0,
69 [CPU_MCP_FLOW_REG] = 0x0b4,
70 [CPU_WRITEBACK_CTRL_REG] = -1,
73 static const int b53_cpubiuctrl_regs[] = {
74 [CPU_CREDIT_REG] = 0x0b0,
75 [CPU_MCP_FLOW_REG] = 0x0b4,
76 [CPU_WRITEBACK_CTRL_REG] = 0x22c,
79 #define NUM_CPU_BIUCTRL_REGS 3
81 static int __init mcp_write_pairing_set(void)
83 u32 creds = 0;
85 if (!cpubiuctrl_base)
86 return -1;
88 creds = cbc_readl(CPU_CREDIT_REG);
89 if (mcp_wr_pairing_en) {
90 pr_info("MCP: Enabling write pairing\n");
91 cbc_writel(creds | CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK,
92 CPU_CREDIT_REG);
93 } else if (creds & CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK) {
94 pr_info("MCP: Disabling write pairing\n");
95 cbc_writel(creds & ~CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK,
96 CPU_CREDIT_REG);
97 } else {
98 pr_info("MCP: Write pairing already disabled\n");
101 return 0;
104 static const u32 b53_mach_compat[] = {
105 0x7268,
106 0x7271,
107 0x7278,
110 static void __init mcp_b53_set(void)
112 unsigned int i;
113 u32 reg;
115 reg = brcmstb_get_family_id();
117 for (i = 0; i < ARRAY_SIZE(b53_mach_compat); i++) {
118 if (BRCM_ID(reg) == b53_mach_compat[i])
119 break;
122 if (i == ARRAY_SIZE(b53_mach_compat))
123 return;
125 /* Set all 3 MCP interfaces to 8 credits */
126 reg = cbc_readl(CPU_CREDIT_REG);
127 for (i = 0; i < 3; i++) {
128 reg &= ~(CPU_CREDIT_REG_MCPx_WRITE_CRED_MASK <<
129 CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(i));
130 reg &= ~(CPU_CREDIT_REG_MCPx_READ_CRED_MASK <<
131 CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(i));
132 reg |= 8 << CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(i);
133 reg |= 8 << CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(i);
135 cbc_writel(reg, CPU_CREDIT_REG);
137 /* Max out the number of in-flight Jwords reads on the MCP interface */
138 reg = cbc_readl(CPU_MCP_FLOW_REG);
139 for (i = 0; i < 3; i++)
140 reg |= CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_MASK <<
141 CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_SHIFT(i);
142 cbc_writel(reg, CPU_MCP_FLOW_REG);
144 /* Enable writeback throttling, set timeout to 128 cycles, 256 cycles
145 * threshold
147 reg = cbc_readl(CPU_WRITEBACK_CTRL_REG);
148 reg |= CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_ENABLE;
149 reg &= ~CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_THRESHOLD_MASK;
150 reg &= ~(CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_MASK <<
151 CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT);
152 reg |= 8;
153 reg |= 7 << CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT;
154 cbc_writel(reg, CPU_WRITEBACK_CTRL_REG);
157 static int __init setup_hifcpubiuctrl_regs(struct device_node *np)
159 struct device_node *cpu_dn;
160 int ret = 0;
162 cpubiuctrl_base = of_iomap(np, 0);
163 if (!cpubiuctrl_base) {
164 pr_err("failed to remap BIU control base\n");
165 ret = -ENOMEM;
166 goto out;
169 mcp_wr_pairing_en = of_property_read_bool(np, "brcm,write-pairing");
171 cpu_dn = of_get_cpu_node(0, NULL);
172 if (!cpu_dn) {
173 pr_err("failed to obtain CPU device node\n");
174 ret = -ENODEV;
175 goto out;
178 if (of_device_is_compatible(cpu_dn, "brcm,brahma-b15"))
179 cpubiuctrl_regs = b15_cpubiuctrl_regs;
180 else if (of_device_is_compatible(cpu_dn, "brcm,brahma-b53"))
181 cpubiuctrl_regs = b53_cpubiuctrl_regs;
182 else {
183 pr_err("unsupported CPU\n");
184 ret = -EINVAL;
186 of_node_put(cpu_dn);
188 if (BRCM_ID(brcmstb_get_family_id()) == 0x7260)
189 cpubiuctrl_regs = b53_cpubiuctrl_no_wb_regs;
190 out:
191 of_node_put(np);
192 return ret;
195 #ifdef CONFIG_PM_SLEEP
196 static u32 cpubiuctrl_reg_save[NUM_CPU_BIUCTRL_REGS];
198 static int brcmstb_cpu_credit_reg_suspend(void)
200 unsigned int i;
202 if (!cpubiuctrl_base)
203 return 0;
205 for (i = 0; i < NUM_CPU_BIUCTRL_REGS; i++)
206 cpubiuctrl_reg_save[i] = cbc_readl(i);
208 return 0;
211 static void brcmstb_cpu_credit_reg_resume(void)
213 unsigned int i;
215 if (!cpubiuctrl_base)
216 return;
218 for (i = 0; i < NUM_CPU_BIUCTRL_REGS; i++)
219 cbc_writel(cpubiuctrl_reg_save[i], i);
222 static struct syscore_ops brcmstb_cpu_credit_syscore_ops = {
223 .suspend = brcmstb_cpu_credit_reg_suspend,
224 .resume = brcmstb_cpu_credit_reg_resume,
226 #endif
229 static int __init brcmstb_biuctrl_init(void)
231 struct device_node *np;
232 int ret;
234 /* We might be running on a multi-platform kernel, don't make this a
235 * fatal error, just bail out early
237 np = of_find_compatible_node(NULL, NULL, "brcm,brcmstb-cpu-biu-ctrl");
238 if (!np)
239 return 0;
241 ret = setup_hifcpubiuctrl_regs(np);
242 if (ret)
243 return ret;
245 ret = mcp_write_pairing_set();
246 if (ret) {
247 pr_err("MCP: Unable to disable write pairing!\n");
248 return ret;
251 mcp_b53_set();
252 #ifdef CONFIG_PM_SLEEP
253 register_syscore_ops(&brcmstb_cpu_credit_syscore_ops);
254 #endif
255 return 0;
257 early_initcall(brcmstb_biuctrl_init);