1 /*
2  * Copyright (C) 2020 ETH Zurich and University of Bologna
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * Author: Robert Balas (balasr@iis.ee.ethz.ch)
18  */
19 
20 /* Driver to control and configure pad mux */
21 
22 #include <stdint.h>
23 #include <assert.h>
24 #include <hal_pinmux1.h>
25 #include <pulp_io.h>
26 #include "core-v-mcu-pulp-mem-map.h"
27 #include "hal_apb_soc.h"
28 
29 /* TODO: we only support pin 0-31 */
30 
pinmux_pin_set(int pin,uint32_t func)31 int pinmux_pin_set(int pin, uint32_t func)
32 {
33 	assert(0 <= pin && pin < 32);
34 
35 	uintptr_t padfun_reg =
36 		((pin & 0xf) >> 4) * 4 +
37 		(PULP_APB_SOC_CTRL_ADDR + APB_SOC_PADFUN0_OFFSET);
38 	uint32_t padfun_shift = (pin & 0x7) << 1; /* 16 pads a 2 bits per reg */
39 	writew((func & 0x3) << padfun_shift, padfun_reg);
40 
41 	return 0;
42 }
43 
pinmux_pin_get(int pin,uint32_t * func)44 int pinmux_pin_get(int pin, uint32_t *func)
45 {
46 	assert(0 <= pin && pin < 32);
47 
48 	uintptr_t padfun_reg =
49 		((pin & 0xf) >> 4) * 4 +
50 		(PULP_APB_SOC_CTRL_ADDR + APB_SOC_PADFUN0_OFFSET);
51 	uint32_t padfun_shift = (pin & 0x7) << 1; /* 16 pads a 2 bits per reg */
52 	uint32_t padfunval = readw(padfun_reg);
53 	*func = (padfunval >> padfun_shift) & 0x3;
54 	return 0;
55 }
56