Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/bitops.h> |
| 8 | #include <asm/armv7m.h> |
Lokesh Vutla | 076ee45 | 2018-04-26 18:21:30 +0530 | [diff] [blame] | 9 | #include <asm/armv7_mpu.h> |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | |
Patrice Chotard | 574f457 | 2018-02-28 17:15:00 +0100 | [diff] [blame] | 12 | #define V7M_MPU_CTRL_ENABLE BIT(0) |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 13 | #define V7M_MPU_CTRL_DISABLE (0 << 0) |
Patrice Chotard | 574f457 | 2018-02-28 17:15:00 +0100 | [diff] [blame] | 14 | #define V7M_MPU_CTRL_HFNMIENA BIT(1) |
| 15 | #define V7M_MPU_CTRL_PRIVDEFENA BIT(2) |
| 16 | #define VALID_REGION BIT(4) |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 17 | |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 18 | void disable_mpu(void) |
| 19 | { |
| 20 | writel(0, &V7M_MPU->ctrl); |
| 21 | } |
| 22 | |
| 23 | void enable_mpu(void) |
| 24 | { |
Patrice Chotard | 574f457 | 2018-02-28 17:15:00 +0100 | [diff] [blame] | 25 | writel(V7M_MPU_CTRL_ENABLE | V7M_MPU_CTRL_PRIVDEFENA, &V7M_MPU->ctrl); |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 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 | |
| 32 | void mpu_config(struct mpu_region_config *reg_config) |
| 33 | { |
| 34 | uint32_t attr; |
| 35 | |
Lokesh Vutla | 076ee45 | 2018-04-26 18:21:30 +0530 | [diff] [blame] | 36 | attr = get_attr_encoding(reg_config->mr_attr); |
Vikas Manocha | 29645ec | 2017-05-03 16:38:55 -0700 | [diff] [blame] | 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 | } |