Software APIs
pinmux_testutils.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_PINMUX_TESTUTILS_H_
6 #define OPENTITAN_SW_DEVICE_LIB_TESTING_PINMUX_TESTUTILS_H_
7 
8 #include <stdint.h>
9 
10 #include "dt/dt_pinmux.h"
11 #include "sw/device/lib/base/status.h"
14 
15 /**
16  * Define a pinmux configuration for a peripheral input and output .
17  */
19  dt_pinmux_peripheral_in_t peripheral_in;
20  dt_pinmux_outsel_t outsel;
22 
23 /**
24  * Define a pinmux configuration for a mio input and output.
25  */
26 typedef struct pinmux_testutils_mio_pin {
27  dt_pinmux_mio_out_t mio_out;
28  dt_pinmux_insel_t insel;
30 
31 /**
32  * Default pinmux initialization.
33  *
34  * Initializes GPIOs to map to the lowest-numbered MIOs, except where it
35  * conflicts with UARTs. Then initializes 2 UART mappings to (IOC3,IOC8) and
36  * (IOC4,IOC9), denoted as (RX pin,TX pin).
37  *
38  * Also ensures IOR0 and IOR1 outputs are disabled, for use as USB sense inputs.
39  *
40  * This function is specific to top_earlgrey and top_englishbreakfast.
41  */
42 void pinmux_testutils_init(dif_pinmux_t *pinmux);
43 
44 /**
45  * Maps the chip IOs to the GPIO peripheral in input and output directions.
46  */
47 extern const dt_pad_t kPinmuxTestutilsGpioPads[kDifGpioNumPins];
48 
49 /**
50  * Connect a peripheral I/O to a pad.
51  *
52  * This will try connect a peripheral I/O to a pad. More precisely,
53  * the behaviour depends on the type of the I/O and pad:
54  * - If both the peripheral I/O and the pad are of MIO type, this
55  * function will configure the MIO to connect them. Depending on the
56  * direction indicated by the `dir` argument, it will connect the input,
57  * output or both. If the MIO pad is an input/output but the requested
58  * direction is only an input, the MIO output will be configured as
59  * high-Z.
60  * - If both are of DIO type, this function will not do anything but
61  * it will check that this peripheral I/O is indeed directly connected
62  * to this pad.
63  * - Any other combination will produce an error.
64  *
65  * In all cases, this function will return an error if the direction(s)
66  * passed as argument are incompatible with the direction(s) of the
67  * peripheral I/O and the pad.
68  *
69  * @param pinmux A pinmux handle
70  * @param pin A peripheral I/O.
71  * @param dir Direction(s) to configure.
72  * @param pad A pad.
73  * @return The result of the operation.
74  */
76 status_t pinmux_testutils_connect(const dif_pinmux_t *pinmux,
77  dt_periph_io_t periph_io,
78  dt_periph_io_dir_t dir, dt_pad_t pad);
79 
80 /**
81  * Returns the mask of testable GPIO pins.
82  *
83  * Returns a simulation-device-specific mask that enables testing of only a
84  * subset of GPIOs depending on the IO allocation limitations.
85  */
86 uint32_t pinmux_testutils_get_testable_gpios_mask(void);
87 
88 /**
89  * Reads a pin as a four-valued strapping pin.
90  *
91  * The value returned will be in the range [0..3] and represent the following
92  * values:
93  * 0: Strong 0
94  * 1: Weak 0
95  * 2: Weak 1
96  * 3: Strong 1
97  *
98  * @param pinmux A pinmux handle.
99  * @param gpio A GPIO handle.
100  * @param io The GPIO pin to read.
101  * @param pad The pinmux pad to which the GPIO pin is connected.
102  * @return A result in the range [0..3].
103  */
104 uint32_t pinmux_testutils_read_strap_pin(dif_pinmux_t *pinmux, dif_gpio_t *gpio,
105  dif_gpio_pin_t io, dt_pad_t pad);
106 
107 /**
108  * Reads the OpenTitan sw strap pins for the strap configuration value.
109  *
110  * The value returned is the combination of 2-bit values returned from
111  * [IOC2, IOC1, IOC0] and ranges from [0..63].
112  * This function assumes the Earlgrey configuration where the external
113  * pads IOC{0,1,2} are used as software straps and that the pinmux is
114  * configured to map those pads to GPIOs 22, 23 and 24.
115  *
116  * @return A result in the range [0..63].
117  */
118 uint32_t pinmux_testutils_read_straps(dif_pinmux_t *pinmux, dif_gpio_t *gpio);
119 
120 /**
121  * A convenience struct to associate pad attributes with a specific pad.
122  */
123 typedef struct pinmux_pad_attributes {
124  dt_pad_t pad;
127 
128 /**
129  * Configures several pad attributes.
130  */
131 void pinmux_testutils_configure_pads(const dif_pinmux_t *pinmux,
132  const pinmux_pad_attributes_t *attrs,
133  size_t num_attrs);
134 
135 #endif // OPENTITAN_SW_DEVICE_LIB_TESTING_PINMUX_TESTUTILS_H_