1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 Pengutronix, Sascha Hauer <kernel@pengutronix.de>
4  */
5 
6 #include <linux/export.h>
7 #include <linux/jiffies.h>
8 #include <linux/regmap.h>
9 #include <linux/soc/mediatek/infracfg.h>
10 #include <asm/processor.h>
11 
12 #define MTK_POLL_DELAY_US   10
13 #define MTK_POLL_TIMEOUT    (jiffies_to_usecs(HZ))
14 
15 /**
16  * mtk_infracfg_set_bus_protection - enable bus protection
17  * @infracfg: The infracfg regmap
18  * @mask: The mask containing the protection bits to be enabled.
19  * @reg_update: The boolean flag determines to set the protection bits
20  *              by regmap_update_bits with enable register(PROTECTEN) or
21  *              by regmap_write with set register(PROTECTEN_SET).
22  *
23  * This function enables the bus protection bits for disabled power
24  * domains so that the system does not hang when some unit accesses the
25  * bus while in power down.
26  */
27 int mtk_infracfg_set_bus_protection(struct regmap *infracfg, u32 mask,
28 		bool reg_update)
29 {
30 	u32 val;
31 	int ret;
32 
33 	if (reg_update)
34 		regmap_update_bits(infracfg, INFRA_TOPAXI_PROTECTEN, mask,
35 				mask);
36 	else
37 		regmap_write(infracfg, INFRA_TOPAXI_PROTECTEN_SET, mask);
38 
39 	ret = regmap_read_poll_timeout(infracfg, INFRA_TOPAXI_PROTECTSTA1,
40 				       val, (val & mask) == mask,
41 				       MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
42 
43 	return ret;
44 }
45 
46 /**
47  * mtk_infracfg_clear_bus_protection - disable bus protection
48  * @infracfg: The infracfg regmap
49  * @mask: The mask containing the protection bits to be disabled.
50  * @reg_update: The boolean flag determines to clear the protection bits
51  *              by regmap_update_bits with enable register(PROTECTEN) or
52  *              by regmap_write with clear register(PROTECTEN_CLR).
53  *
54  * This function disables the bus protection bits previously enabled with
55  * mtk_infracfg_set_bus_protection.
56  */
57 
58 int mtk_infracfg_clear_bus_protection(struct regmap *infracfg, u32 mask,
59 		bool reg_update)
60 {
61 	int ret;
62 	u32 val;
63 
64 	if (reg_update)
65 		regmap_update_bits(infracfg, INFRA_TOPAXI_PROTECTEN, mask, 0);
66 	else
67 		regmap_write(infracfg, INFRA_TOPAXI_PROTECTEN_CLR, mask);
68 
69 	ret = regmap_read_poll_timeout(infracfg, INFRA_TOPAXI_PROTECTSTA1,
70 				       val, !(val & mask),
71 				       MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
72 
73 	return ret;
74 }
75