1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4  * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
5  */
6 
7 #include <linux/bitops.h>
8 #include <asm/armv7m.h>
9 #include <asm/armv7_mpu.h>
10 #include <asm/io.h>
11 
12 #define V7M_MPU_CTRL_ENABLE		BIT(0)
13 #define V7M_MPU_CTRL_DISABLE		(0 << 0)
14 #define V7M_MPU_CTRL_HFNMIENA		BIT(1)
15 #define V7M_MPU_CTRL_PRIVDEFENA		BIT(2)
16 #define VALID_REGION			BIT(4)
17 
disable_mpu(void)18 void disable_mpu(void)
19 {
20 	writel(0, &V7M_MPU->ctrl);
21 }
22 
enable_mpu(void)23 void enable_mpu(void)
24 {
25 	writel(V7M_MPU_CTRL_ENABLE | V7M_MPU_CTRL_PRIVDEFENA, &V7M_MPU->ctrl);
26 
27 	/* Make sure new mpu config is effective for next memory access */
28 	dsb();
29 	isb();	/* Make sure instruction stream sees it */
30 }
31 
mpu_config(struct mpu_region_config * reg_config)32 void mpu_config(struct mpu_region_config *reg_config)
33 {
34 	uint32_t attr;
35 
36 	attr = get_attr_encoding(reg_config->mr_attr);
37 
38 	writel(reg_config->start_addr | VALID_REGION | reg_config->region_no,
39 	       &V7M_MPU->rbar);
40 
41 	writel(reg_config->xn << XN_SHIFT | reg_config->ap << AP_SHIFT | attr
42 		| reg_config->reg_size << REGION_SIZE_SHIFT | ENABLE_REGION
43 	       , &V7M_MPU->rasr);
44 }
45