xref: /linux/drivers/soc/mediatek/mtk-infracfg.c (revision a02633e9b13dcb9b1a656b08f81bc8ba2d4d2294)
1 /*
2  * Copyright (c) 2015 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/export.h>
15 #include <linux/jiffies.h>
16 #include <linux/regmap.h>
17 #include <linux/soc/mediatek/infracfg.h>
18 #include <asm/processor.h>
19 
20 #define INFRA_TOPAXI_PROTECTEN		0x0220
21 #define INFRA_TOPAXI_PROTECTSTA1	0x0228
22 #define INFRA_TOPAXI_PROTECTEN_SET	0x0260
23 #define INFRA_TOPAXI_PROTECTEN_CLR	0x0264
24 
25 /**
26  * mtk_infracfg_set_bus_protection - enable bus protection
27  * @regmap: The infracfg regmap
28  * @mask: The mask containing the protection bits to be enabled.
29  * @reg_update: The boolean flag determines to set the protection bits
30  *              by regmap_update_bits with enable register(PROTECTEN) or
31  *              by regmap_write with set register(PROTECTEN_SET).
32  *
33  * This function enables the bus protection bits for disabled power
34  * domains so that the system does not hang when some unit accesses the
35  * bus while in power down.
36  */
37 int mtk_infracfg_set_bus_protection(struct regmap *infracfg, u32 mask,
38 		bool reg_update)
39 {
40 	unsigned long expired;
41 	u32 val;
42 	int ret;
43 
44 	if (reg_update)
45 		regmap_update_bits(infracfg, INFRA_TOPAXI_PROTECTEN, mask,
46 				mask);
47 	else
48 		regmap_write(infracfg, INFRA_TOPAXI_PROTECTEN_SET, mask);
49 
50 	expired = jiffies + HZ;
51 
52 	while (1) {
53 		ret = regmap_read(infracfg, INFRA_TOPAXI_PROTECTSTA1, &val);
54 		if (ret)
55 			return ret;
56 
57 		if ((val & mask) == mask)
58 			break;
59 
60 		cpu_relax();
61 		if (time_after(jiffies, expired))
62 			return -EIO;
63 	}
64 
65 	return 0;
66 }
67 
68 /**
69  * mtk_infracfg_clear_bus_protection - disable bus protection
70  * @regmap: The infracfg regmap
71  * @mask: The mask containing the protection bits to be disabled.
72  * @reg_update: The boolean flag determines to clear the protection bits
73  *              by regmap_update_bits with enable register(PROTECTEN) or
74  *              by regmap_write with clear register(PROTECTEN_CLR).
75  *
76  * This function disables the bus protection bits previously enabled with
77  * mtk_infracfg_set_bus_protection.
78  */
79 
80 int mtk_infracfg_clear_bus_protection(struct regmap *infracfg, u32 mask,
81 		bool reg_update)
82 {
83 	unsigned long expired;
84 	int ret;
85 
86 	if (reg_update)
87 		regmap_update_bits(infracfg, INFRA_TOPAXI_PROTECTEN, mask, 0);
88 	else
89 		regmap_write(infracfg, INFRA_TOPAXI_PROTECTEN_CLR, mask);
90 
91 	expired = jiffies + HZ;
92 
93 	while (1) {
94 		u32 val;
95 
96 		ret = regmap_read(infracfg, INFRA_TOPAXI_PROTECTSTA1, &val);
97 		if (ret)
98 			return ret;
99 
100 		if (!(val & mask))
101 			break;
102 
103 		cpu_relax();
104 		if (time_after(jiffies, expired))
105 			return -EIO;
106 	}
107 
108 	return 0;
109 }
110