dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / soc / bcm / brcmstb / biuctrl.c
blob61731e01f94bb93958e6d2eae0511785aafee60b
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: 7260A0 */
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 static const int a72_cpubiuctrl_regs[] = {
80 [CPU_CREDIT_REG] = 0x18,
81 [CPU_MCP_FLOW_REG] = 0x1c,
82 [CPU_WRITEBACK_CTRL_REG] = 0x20,
85 #define NUM_CPU_BIUCTRL_REGS 3
87 static int __init mcp_write_pairing_set(void)
89 u32 creds = 0;
91 if (!cpubiuctrl_base)
92 return -1;
94 creds = cbc_readl(CPU_CREDIT_REG);
95 if (mcp_wr_pairing_en) {
96 pr_info("MCP: Enabling write pairing\n");
97 cbc_writel(creds | CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK,
98 CPU_CREDIT_REG);
99 } else if (creds & CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK) {
100 pr_info("MCP: Disabling write pairing\n");
101 cbc_writel(creds & ~CPU_CREDIT_REG_MCPx_WR_PAIRING_EN_MASK,
102 CPU_CREDIT_REG);
103 } else {
104 pr_info("MCP: Write pairing already disabled\n");
107 return 0;
110 static const u32 a72_b53_mach_compat[] = {
111 0x7211,
112 0x7216,
113 0x7255,
114 0x7260,
115 0x7268,
116 0x7271,
117 0x7278,
120 static void __init mcp_a72_b53_set(void)
122 unsigned int i;
123 u32 reg;
125 reg = brcmstb_get_family_id();
127 for (i = 0; i < ARRAY_SIZE(a72_b53_mach_compat); i++) {
128 if (BRCM_ID(reg) == a72_b53_mach_compat[i])
129 break;
132 if (i == ARRAY_SIZE(a72_b53_mach_compat))
133 return;
135 /* Set all 3 MCP interfaces to 8 credits */
136 reg = cbc_readl(CPU_CREDIT_REG);
137 for (i = 0; i < 3; i++) {
138 reg &= ~(CPU_CREDIT_REG_MCPx_WRITE_CRED_MASK <<
139 CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(i));
140 reg &= ~(CPU_CREDIT_REG_MCPx_READ_CRED_MASK <<
141 CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(i));
142 reg |= 8 << CPU_CREDIT_REG_MCPx_WRITE_CRED_SHIFT(i);
143 reg |= 8 << CPU_CREDIT_REG_MCPx_READ_CRED_SHIFT(i);
145 cbc_writel(reg, CPU_CREDIT_REG);
147 /* Max out the number of in-flight Jwords reads on the MCP interface */
148 reg = cbc_readl(CPU_MCP_FLOW_REG);
149 for (i = 0; i < 3; i++)
150 reg |= CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_MASK <<
151 CPU_MCP_FLOW_REG_MCPx_RDBUFF_CRED_SHIFT(i);
152 cbc_writel(reg, CPU_MCP_FLOW_REG);
154 /* Enable writeback throttling, set timeout to 128 cycles, 256 cycles
155 * threshold
157 reg = cbc_readl(CPU_WRITEBACK_CTRL_REG);
158 reg |= CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_ENABLE;
159 reg &= ~CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_THRESHOLD_MASK;
160 reg &= ~(CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_MASK <<
161 CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT);
162 reg |= 8;
163 reg |= 7 << CPU_WRITEBACK_CTRL_REG_WB_THROTTLE_TIMEOUT_SHIFT;
164 cbc_writel(reg, CPU_WRITEBACK_CTRL_REG);
167 static int __init setup_hifcpubiuctrl_regs(struct device_node *np)
169 struct device_node *cpu_dn;
170 u32 family_id;
171 int ret = 0;
173 cpubiuctrl_base = of_iomap(np, 0);
174 if (!cpubiuctrl_base) {
175 pr_err("failed to remap BIU control base\n");
176 ret = -ENOMEM;
177 goto out;
180 mcp_wr_pairing_en = of_property_read_bool(np, "brcm,write-pairing");
182 cpu_dn = of_get_cpu_node(0, NULL);
183 if (!cpu_dn) {
184 pr_err("failed to obtain CPU device node\n");
185 ret = -ENODEV;
186 goto out;
189 if (of_device_is_compatible(cpu_dn, "brcm,brahma-b15"))
190 cpubiuctrl_regs = b15_cpubiuctrl_regs;
191 else if (of_device_is_compatible(cpu_dn, "brcm,brahma-b53"))
192 cpubiuctrl_regs = b53_cpubiuctrl_regs;
193 else if (of_device_is_compatible(cpu_dn, "arm,cortex-a72"))
194 cpubiuctrl_regs = a72_cpubiuctrl_regs;
195 else {
196 pr_err("unsupported CPU\n");
197 ret = -EINVAL;
199 of_node_put(cpu_dn);
201 family_id = brcmstb_get_family_id();
202 if (BRCM_ID(family_id) == 0x7260 && BRCM_REV(family_id) == 0)
203 cpubiuctrl_regs = b53_cpubiuctrl_no_wb_regs;
204 out:
205 of_node_put(np);
206 return ret;
209 #ifdef CONFIG_PM_SLEEP
210 static u32 cpubiuctrl_reg_save[NUM_CPU_BIUCTRL_REGS];
212 static int brcmstb_cpu_credit_reg_suspend(void)
214 unsigned int i;
216 if (!cpubiuctrl_base)
217 return 0;
219 for (i = 0; i < NUM_CPU_BIUCTRL_REGS; i++)
220 cpubiuctrl_reg_save[i] = cbc_readl(i);
222 return 0;
225 static void brcmstb_cpu_credit_reg_resume(void)
227 unsigned int i;
229 if (!cpubiuctrl_base)
230 return;
232 for (i = 0; i < NUM_CPU_BIUCTRL_REGS; i++)
233 cbc_writel(cpubiuctrl_reg_save[i], i);
236 static struct syscore_ops brcmstb_cpu_credit_syscore_ops = {
237 .suspend = brcmstb_cpu_credit_reg_suspend,
238 .resume = brcmstb_cpu_credit_reg_resume,
240 #endif
243 static int __init brcmstb_biuctrl_init(void)
245 struct device_node *np;
246 int ret;
248 /* We might be running on a multi-platform kernel, don't make this a
249 * fatal error, just bail out early
251 np = of_find_compatible_node(NULL, NULL, "brcm,brcmstb-cpu-biu-ctrl");
252 if (!np)
253 return 0;
255 ret = setup_hifcpubiuctrl_regs(np);
256 if (ret)
257 return ret;
259 ret = mcp_write_pairing_set();
260 if (ret) {
261 pr_err("MCP: Unable to disable write pairing!\n");
262 return ret;
265 mcp_a72_b53_set();
266 #ifdef CONFIG_PM_SLEEP
267 register_syscore_ops(&brcmstb_cpu_credit_syscore_ops);
268 #endif
269 return 0;
271 early_initcall(brcmstb_biuctrl_init);