Paul Barker | b378c40 | 2023-10-16 10:25:32 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * RZ/G2L Pin Function Controller |
| 4 | * |
| 5 | * Copyright (C) 2021-2023 Renesas Electronics Corp. |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/device-internal.h> |
| 13 | #include <dm/device.h> |
| 14 | #include <dm/device_compat.h> |
| 15 | #include <dm/devres.h> |
| 16 | #include <dm/lists.h> |
| 17 | #include <dm/pinctrl.h> |
| 18 | #include <renesas/rzg2l-pfc.h> |
| 19 | #include <reset.h> |
| 20 | |
| 21 | struct rzg2l_pfc_driver_data { |
| 22 | uint num_dedicated_pins; |
| 23 | uint num_ports; |
| 24 | const u32 *gpio_configs; |
| 25 | }; |
| 26 | |
| 27 | struct rzg2l_dedicated_configs { |
| 28 | const char *name; |
| 29 | u32 config; |
| 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * We need to ensure that the module clock is enabled and all resets are |
| 34 | * de-asserted before using either the gpio or pinctrl functionality. Error |
| 35 | * handling can be quite simple here as if the PFC cannot be enabled then we |
| 36 | * will not be able to progress with the boot anyway. |
| 37 | */ |
| 38 | int rzg2l_pfc_enable(struct udevice *dev) |
| 39 | { |
| 40 | struct reset_ctl_bulk rsts; |
| 41 | struct clk clk; |
| 42 | int ret; |
| 43 | |
| 44 | ret = clk_get_by_index(dev, 0, &clk); |
| 45 | if (ret < 0) { |
| 46 | dev_err(dev, "failed to get gpio module clock\n"); |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | ret = clk_enable(&clk); |
| 51 | if (ret < 0) { |
| 52 | dev_err(dev, "failed to enable gpio module clock\n"); |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | ret = reset_get_bulk(dev, &rsts); |
| 57 | if (ret < 0) { |
| 58 | dev_err(dev, "failed to get reset lines\n"); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | ret = reset_deassert_bulk(&rsts); |
| 63 | if (ret < 0) { |
| 64 | dev_err(dev, "failed to de-assert reset lines\n"); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | bool rzg2l_port_validate(const struct rzg2l_pfc_data *data, u32 port, u8 pin) |
| 72 | { |
| 73 | return (port < data->num_ports) && |
| 74 | (pin < RZG2L_GPIO_PORT_GET_PINCNT(data->gpio_configs[port])); |
| 75 | } |
| 76 | |
| 77 | /* Decode a pin selector, returning the port index and setting *pin to the pin |
| 78 | * index. Returns -1 on error, which can be checked directly or by calling |
| 79 | * rzg2l_port_validate(). |
| 80 | */ |
| 81 | static int rzg2l_selector_decode(const struct rzg2l_pfc_data *data, |
| 82 | unsigned int selector, |
| 83 | u8 *pin) |
| 84 | { |
| 85 | int port; |
| 86 | |
| 87 | selector -= data->num_dedicated_pins; |
| 88 | for (port = 0; port < data->num_ports; port++) { |
| 89 | u8 num_pins = RZG2L_GPIO_PORT_GET_PINCNT(data->gpio_configs[port]); |
| 90 | if (selector < num_pins) { |
| 91 | *pin = (u8)selector; |
| 92 | return port; |
| 93 | } |
| 94 | selector -= num_pins; |
| 95 | } |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | static unsigned int rzg2l_selector_encode(const struct rzg2l_pfc_data *data, |
| 100 | u32 port, u8 pin) |
| 101 | { |
| 102 | unsigned int selector = data->num_dedicated_pins + pin; |
| 103 | u32 i; |
| 104 | |
| 105 | for (i = 0; i < port; i++) |
| 106 | selector += RZG2L_GPIO_PORT_GET_PINCNT(data->gpio_configs[i]); |
| 107 | |
| 108 | return selector; |
| 109 | } |
| 110 | |
| 111 | static const char * const rzg2l_gpio_names[] = { |
| 112 | "P0_0", "P0_1", "P0_2", "P0_3", "P0_4", "P0_5", "P0_6", "P0_7", |
| 113 | "P1_0", "P1_1", "P1_2", "P1_3", "P1_4", "P1_5", "P1_6", "P1_7", |
| 114 | "P2_0", "P2_1", "P2_2", "P2_3", "P2_4", "P2_5", "P2_6", "P2_7", |
| 115 | "P3_0", "P3_1", "P3_2", "P3_3", "P3_4", "P3_5", "P3_6", "P3_7", |
| 116 | "P4_0", "P4_1", "P4_2", "P4_3", "P4_4", "P4_5", "P4_6", "P4_7", |
| 117 | "P5_0", "P5_1", "P5_2", "P5_3", "P5_4", "P5_5", "P5_6", "P5_7", |
| 118 | "P6_0", "P6_1", "P6_2", "P6_3", "P6_4", "P6_5", "P6_6", "P6_7", |
| 119 | "P7_0", "P7_1", "P7_2", "P7_3", "P7_4", "P7_5", "P7_6", "P7_7", |
| 120 | "P8_0", "P8_1", "P8_2", "P8_3", "P8_4", "P8_5", "P8_6", "P8_7", |
| 121 | "P9_0", "P9_1", "P9_2", "P9_3", "P9_4", "P9_5", "P9_6", "P9_7", |
| 122 | "P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7", |
| 123 | "P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7", |
| 124 | "P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7", |
| 125 | "P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7", |
| 126 | "P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7", |
| 127 | "P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7", |
| 128 | "P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7", |
| 129 | "P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7", |
| 130 | "P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7", |
| 131 | "P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7", |
| 132 | "P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7", |
| 133 | "P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7", |
| 134 | "P22_0", "P22_1", "P22_2", "P22_3", "P22_4", "P22_5", "P22_6", "P22_7", |
| 135 | "P23_0", "P23_1", "P23_2", "P23_3", "P23_4", "P23_5", "P23_6", "P23_7", |
| 136 | "P24_0", "P24_1", "P24_2", "P24_3", "P24_4", "P24_5", "P24_6", "P24_7", |
| 137 | "P25_0", "P25_1", "P25_2", "P25_3", "P25_4", "P25_5", "P25_6", "P25_7", |
| 138 | "P26_0", "P26_1", "P26_2", "P26_3", "P26_4", "P26_5", "P26_6", "P26_7", |
| 139 | "P27_0", "P27_1", "P27_2", "P27_3", "P27_4", "P27_5", "P27_6", "P27_7", |
| 140 | "P28_0", "P28_1", "P28_2", "P28_3", "P28_4", "P28_5", "P28_6", "P28_7", |
| 141 | "P29_0", "P29_1", "P29_2", "P29_3", "P29_4", "P29_5", "P29_6", "P29_7", |
| 142 | "P30_0", "P30_1", "P30_2", "P30_3", "P30_4", "P30_5", "P30_6", "P30_7", |
| 143 | "P31_0", "P31_1", "P31_2", "P31_3", "P31_4", "P31_5", "P31_6", "P31_7", |
| 144 | "P32_0", "P32_1", "P32_2", "P32_3", "P32_4", "P32_5", "P32_6", "P32_7", |
| 145 | "P33_0", "P33_1", "P33_2", "P33_3", "P33_4", "P33_5", "P33_6", "P33_7", |
| 146 | "P34_0", "P34_1", "P34_2", "P34_3", "P34_4", "P34_5", "P34_6", "P34_7", |
| 147 | "P35_0", "P35_1", "P35_2", "P35_3", "P35_4", "P35_5", "P35_6", "P35_7", |
| 148 | "P36_0", "P36_1", "P36_2", "P36_3", "P36_4", "P36_5", "P36_6", "P36_7", |
| 149 | "P37_0", "P37_1", "P37_2", "P37_3", "P37_4", "P37_5", "P37_6", "P37_7", |
| 150 | "P38_0", "P38_1", "P38_2", "P38_3", "P38_4", "P38_5", "P38_6", "P38_7", |
| 151 | "P39_0", "P39_1", "P39_2", "P39_3", "P39_4", "P39_5", "P39_6", "P39_7", |
| 152 | "P40_0", "P40_1", "P40_2", "P40_3", "P40_4", "P40_5", "P40_6", "P40_7", |
| 153 | "P41_0", "P41_1", "P41_2", "P41_3", "P41_4", "P41_5", "P41_6", "P41_7", |
| 154 | "P42_0", "P42_1", "P42_2", "P42_3", "P42_4", "P42_5", "P42_6", "P42_7", |
| 155 | "P43_0", "P43_1", "P43_2", "P43_3", "P43_4", "P43_5", "P43_6", "P43_7", |
| 156 | "P44_0", "P44_1", "P44_2", "P44_3", "P44_4", "P44_5", "P44_6", "P44_7", |
| 157 | "P45_0", "P45_1", "P45_2", "P45_3", "P45_4", "P45_5", "P45_6", "P45_7", |
| 158 | "P46_0", "P46_1", "P46_2", "P46_3", "P46_4", "P46_5", "P46_6", "P46_7", |
| 159 | "P47_0", "P47_1", "P47_2", "P47_3", "P47_4", "P47_5", "P47_6", "P47_7", |
| 160 | "P48_0", "P48_1", "P48_2", "P48_3", "P48_4", "P48_5", "P48_6", "P48_7", |
| 161 | }; |
| 162 | |
| 163 | static const u32 r9a07g044_gpio_configs[] = { |
| 164 | RZG2L_GPIO_PORT_PACK(2, 0x10, RZG2L_MPXED_PIN_FUNCS), |
| 165 | RZG2L_GPIO_PORT_PACK(2, 0x11, RZG2L_MPXED_PIN_FUNCS), |
| 166 | RZG2L_GPIO_PORT_PACK(2, 0x12, RZG2L_MPXED_PIN_FUNCS), |
| 167 | RZG2L_GPIO_PORT_PACK(2, 0x13, RZG2L_MPXED_PIN_FUNCS), |
| 168 | RZG2L_GPIO_PORT_PACK(2, 0x14, RZG2L_MPXED_PIN_FUNCS), |
| 169 | RZG2L_GPIO_PORT_PACK(3, 0x15, RZG2L_MPXED_PIN_FUNCS), |
| 170 | RZG2L_GPIO_PORT_PACK(2, 0x16, RZG2L_MPXED_PIN_FUNCS), |
| 171 | RZG2L_GPIO_PORT_PACK(3, 0x17, RZG2L_MPXED_PIN_FUNCS), |
| 172 | RZG2L_GPIO_PORT_PACK(3, 0x18, RZG2L_MPXED_PIN_FUNCS), |
| 173 | RZG2L_GPIO_PORT_PACK(2, 0x19, RZG2L_MPXED_PIN_FUNCS), |
| 174 | RZG2L_GPIO_PORT_PACK(2, 0x1a, RZG2L_MPXED_PIN_FUNCS), |
| 175 | RZG2L_GPIO_PORT_PACK(2, 0x1b, RZG2L_MPXED_PIN_FUNCS), |
| 176 | RZG2L_GPIO_PORT_PACK(2, 0x1c, RZG2L_MPXED_PIN_FUNCS), |
| 177 | RZG2L_GPIO_PORT_PACK(3, 0x1d, RZG2L_MPXED_PIN_FUNCS), |
| 178 | RZG2L_GPIO_PORT_PACK(2, 0x1e, RZG2L_MPXED_PIN_FUNCS), |
| 179 | RZG2L_GPIO_PORT_PACK(2, 0x1f, RZG2L_MPXED_PIN_FUNCS), |
| 180 | RZG2L_GPIO_PORT_PACK(2, 0x20, RZG2L_MPXED_PIN_FUNCS), |
| 181 | RZG2L_GPIO_PORT_PACK(3, 0x21, RZG2L_MPXED_PIN_FUNCS), |
| 182 | RZG2L_GPIO_PORT_PACK(2, 0x22, RZG2L_MPXED_PIN_FUNCS), |
| 183 | RZG2L_GPIO_PORT_PACK(2, 0x23, RZG2L_MPXED_PIN_FUNCS), |
| 184 | RZG2L_GPIO_PORT_PACK(3, 0x24, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 185 | RZG2L_GPIO_PORT_PACK(2, 0x25, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 186 | RZG2L_GPIO_PORT_PACK(2, 0x26, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 187 | RZG2L_GPIO_PORT_PACK(2, 0x27, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 188 | RZG2L_GPIO_PORT_PACK(2, 0x28, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 189 | RZG2L_GPIO_PORT_PACK(2, 0x29, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 190 | RZG2L_GPIO_PORT_PACK(2, 0x2a, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 191 | RZG2L_GPIO_PORT_PACK(2, 0x2b, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 192 | RZG2L_GPIO_PORT_PACK(2, 0x2c, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH0)), |
| 193 | RZG2L_GPIO_PORT_PACK(2, 0x2d, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 194 | RZG2L_GPIO_PORT_PACK(2, 0x2e, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 195 | RZG2L_GPIO_PORT_PACK(2, 0x2f, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 196 | RZG2L_GPIO_PORT_PACK(2, 0x30, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 197 | RZG2L_GPIO_PORT_PACK(2, 0x31, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 198 | RZG2L_GPIO_PORT_PACK(2, 0x32, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 199 | RZG2L_GPIO_PORT_PACK(2, 0x33, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 200 | RZG2L_GPIO_PORT_PACK(2, 0x34, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 201 | RZG2L_GPIO_PORT_PACK(3, 0x35, RZG2L_MPXED_ETH_PIN_FUNCS(PIN_CFG_IO_VMC_ETH1)), |
| 202 | RZG2L_GPIO_PORT_PACK(2, 0x36, RZG2L_MPXED_PIN_FUNCS), |
| 203 | RZG2L_GPIO_PORT_PACK(3, 0x37, RZG2L_MPXED_PIN_FUNCS), |
| 204 | RZG2L_GPIO_PORT_PACK(3, 0x38, RZG2L_MPXED_PIN_FUNCS), |
| 205 | RZG2L_GPIO_PORT_PACK(2, 0x39, RZG2L_MPXED_PIN_FUNCS), |
| 206 | RZG2L_GPIO_PORT_PACK(5, 0x3a, RZG2L_MPXED_PIN_FUNCS), |
| 207 | RZG2L_GPIO_PORT_PACK(4, 0x3b, RZG2L_MPXED_PIN_FUNCS), |
| 208 | RZG2L_GPIO_PORT_PACK(4, 0x3c, RZG2L_MPXED_PIN_FUNCS), |
| 209 | RZG2L_GPIO_PORT_PACK(4, 0x3d, RZG2L_MPXED_PIN_FUNCS), |
| 210 | RZG2L_GPIO_PORT_PACK(4, 0x3e, RZG2L_MPXED_PIN_FUNCS), |
| 211 | RZG2L_GPIO_PORT_PACK(4, 0x3f, RZG2L_MPXED_PIN_FUNCS), |
| 212 | RZG2L_GPIO_PORT_PACK(5, 0x40, RZG2L_MPXED_PIN_FUNCS), |
| 213 | }; |
| 214 | |
| 215 | static const struct { |
| 216 | struct rzg2l_dedicated_configs common[35]; |
| 217 | struct rzg2l_dedicated_configs rzg2l_pins[7]; |
| 218 | } rzg2l_dedicated_pins = { |
| 219 | .common = { |
| 220 | { "NMI", RZG2L_SINGLE_PIN_PACK(0x1, 0, |
| 221 | (PIN_CFG_FILONOFF | PIN_CFG_FILNUM | PIN_CFG_FILCLKSEL)) }, |
| 222 | { "TMS/SWDIO", RZG2L_SINGLE_PIN_PACK(0x2, 0, |
| 223 | (PIN_CFG_IOLH_A | PIN_CFG_SR | PIN_CFG_IEN)) }, |
| 224 | { "TDO", RZG2L_SINGLE_PIN_PACK(0x3, 0, |
| 225 | (PIN_CFG_IOLH_A | PIN_CFG_SR | PIN_CFG_IEN)) }, |
| 226 | { "AUDIO_CLK1", RZG2L_SINGLE_PIN_PACK(0x4, 0, PIN_CFG_IEN) }, |
| 227 | { "AUDIO_CLK2", RZG2L_SINGLE_PIN_PACK(0x4, 1, PIN_CFG_IEN) }, |
| 228 | { "SD0_CLK", RZG2L_SINGLE_PIN_PACK(0x6, 0, |
| 229 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD0)) }, |
| 230 | { "SD0_CMD", RZG2L_SINGLE_PIN_PACK(0x6, 1, |
| 231 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 232 | { "SD0_RST#", RZG2L_SINGLE_PIN_PACK(0x6, 2, |
| 233 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD0)) }, |
| 234 | { "SD0_DATA0", RZG2L_SINGLE_PIN_PACK(0x7, 0, |
| 235 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 236 | { "SD0_DATA1", RZG2L_SINGLE_PIN_PACK(0x7, 1, |
| 237 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 238 | { "SD0_DATA2", RZG2L_SINGLE_PIN_PACK(0x7, 2, |
| 239 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 240 | { "SD0_DATA3", RZG2L_SINGLE_PIN_PACK(0x7, 3, |
| 241 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 242 | { "SD0_DATA4", RZG2L_SINGLE_PIN_PACK(0x7, 4, |
| 243 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 244 | { "SD0_DATA5", RZG2L_SINGLE_PIN_PACK(0x7, 5, |
| 245 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 246 | { "SD0_DATA6", RZG2L_SINGLE_PIN_PACK(0x7, 6, |
| 247 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 248 | { "SD0_DATA7", RZG2L_SINGLE_PIN_PACK(0x7, 7, |
| 249 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD0)) }, |
| 250 | { "SD1_CLK", RZG2L_SINGLE_PIN_PACK(0x8, 0, |
| 251 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_SD1)) }, |
| 252 | { "SD1_CMD", RZG2L_SINGLE_PIN_PACK(0x8, 1, |
| 253 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) }, |
| 254 | { "SD1_DATA0", RZG2L_SINGLE_PIN_PACK(0x9, 0, |
| 255 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) }, |
| 256 | { "SD1_DATA1", RZG2L_SINGLE_PIN_PACK(0x9, 1, |
| 257 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) }, |
| 258 | { "SD1_DATA2", RZG2L_SINGLE_PIN_PACK(0x9, 2, |
| 259 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) }, |
| 260 | { "SD1_DATA3", RZG2L_SINGLE_PIN_PACK(0x9, 3, |
| 261 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IEN | PIN_CFG_IO_VMC_SD1)) }, |
| 262 | { "QSPI0_SPCLK", RZG2L_SINGLE_PIN_PACK(0xa, 0, |
| 263 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 264 | { "QSPI0_IO0", RZG2L_SINGLE_PIN_PACK(0xa, 1, |
| 265 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 266 | { "QSPI0_IO1", RZG2L_SINGLE_PIN_PACK(0xa, 2, |
| 267 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 268 | { "QSPI0_IO2", RZG2L_SINGLE_PIN_PACK(0xa, 3, |
| 269 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 270 | { "QSPI0_IO3", RZG2L_SINGLE_PIN_PACK(0xa, 4, |
| 271 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 272 | { "QSPI0_SSL", RZG2L_SINGLE_PIN_PACK(0xa, 5, |
| 273 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 274 | { "QSPI_RESET#", RZG2L_SINGLE_PIN_PACK(0xc, 0, |
| 275 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 276 | { "QSPI_WP#", RZG2L_SINGLE_PIN_PACK(0xc, 1, |
| 277 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 278 | { "WDTOVF_PERROUT#", RZG2L_SINGLE_PIN_PACK(0xd, 0, (PIN_CFG_IOLH_A | PIN_CFG_SR)) }, |
| 279 | { "RIIC0_SDA", RZG2L_SINGLE_PIN_PACK(0xe, 0, PIN_CFG_IEN) }, |
| 280 | { "RIIC0_SCL", RZG2L_SINGLE_PIN_PACK(0xe, 1, PIN_CFG_IEN) }, |
| 281 | { "RIIC1_SDA", RZG2L_SINGLE_PIN_PACK(0xe, 2, PIN_CFG_IEN) }, |
| 282 | { "RIIC1_SCL", RZG2L_SINGLE_PIN_PACK(0xe, 3, PIN_CFG_IEN) }, |
| 283 | }, |
| 284 | .rzg2l_pins = { |
| 285 | { "QSPI_INT#", RZG2L_SINGLE_PIN_PACK(0xc, 2, (PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 286 | { "QSPI1_SPCLK", RZG2L_SINGLE_PIN_PACK(0xb, 0, |
| 287 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 288 | { "QSPI1_IO0", RZG2L_SINGLE_PIN_PACK(0xb, 1, |
| 289 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 290 | { "QSPI1_IO1", RZG2L_SINGLE_PIN_PACK(0xb, 2, |
| 291 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 292 | { "QSPI1_IO2", RZG2L_SINGLE_PIN_PACK(0xb, 3, |
| 293 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 294 | { "QSPI1_IO3", RZG2L_SINGLE_PIN_PACK(0xb, 4, |
| 295 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 296 | { "QSPI1_SSL", RZG2L_SINGLE_PIN_PACK(0xb, 5, |
| 297 | (PIN_CFG_IOLH_B | PIN_CFG_SR | PIN_CFG_IO_VMC_QSPI)) }, |
| 298 | } |
| 299 | }; |
| 300 | |
| 301 | static void rzg2l_rmw_pin_config(const struct rzg2l_pfc_data *data, u32 offset, |
| 302 | u8 pin, u32 mask, u32 val) |
| 303 | { |
| 304 | void __iomem *addr = data->base + offset; |
| 305 | |
| 306 | /* handle _L/_H for 32-bit register read/write */ |
| 307 | if (pin >= 4) { |
| 308 | pin -= 4; |
| 309 | addr += 4; |
| 310 | } |
| 311 | |
| 312 | clrsetbits_le32(addr, mask << (pin * 8), val << (pin * 8)); |
| 313 | } |
| 314 | |
| 315 | static int rzg2l_get_pins_count(struct udevice *dev) |
| 316 | { |
| 317 | const struct rzg2l_pfc_data *data = |
| 318 | (const struct rzg2l_pfc_data *)dev_get_driver_data(dev); |
| 319 | |
| 320 | return data->num_dedicated_pins + data->num_pins; |
| 321 | } |
| 322 | |
| 323 | static const char *rzg2l_get_pin_name(struct udevice *dev, unsigned int selector) |
| 324 | { |
| 325 | const struct rzg2l_pfc_data *data = |
| 326 | (const struct rzg2l_pfc_data *)dev_get_driver_data(dev); |
| 327 | int port; |
| 328 | u8 pin; |
| 329 | |
| 330 | if (selector < data->num_dedicated_pins) { |
| 331 | if (selector >= ARRAY_SIZE(rzg2l_dedicated_pins.common)) { |
| 332 | unsigned int u = selector - ARRAY_SIZE(rzg2l_dedicated_pins.common); |
| 333 | return rzg2l_dedicated_pins.rzg2l_pins[u].name; |
| 334 | } else { |
| 335 | return rzg2l_dedicated_pins.common[selector].name; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | port = rzg2l_selector_decode(data, selector, &pin); |
| 340 | if (port < 0) |
| 341 | return "(invalid pin)"; |
| 342 | return rzg2l_gpio_names[pin + 8 * port]; |
| 343 | } |
| 344 | |
| 345 | static int rzg2l_pinconf_set(struct udevice *dev, unsigned int pin_selector, |
| 346 | unsigned int param, unsigned int argument) |
| 347 | { |
| 348 | const struct rzg2l_pfc_data *data = |
| 349 | (const struct rzg2l_pfc_data *)dev_get_driver_data(dev); |
| 350 | u32 cfg, port_offset; |
| 351 | u8 pin; |
| 352 | |
| 353 | if (pin_selector >= data->num_dedicated_pins) { |
| 354 | /* The pin selector refers to a multiplexed pin */ |
| 355 | int port = rzg2l_selector_decode(data, pin_selector, &pin); |
| 356 | if (port < 0) { |
| 357 | dev_err(dev, "Invalid pin selector %u:%u\n", port, pin); |
| 358 | return port; |
| 359 | } |
| 360 | |
| 361 | cfg = data->gpio_configs[port]; |
| 362 | port_offset = P(port); |
| 363 | } else { |
| 364 | /* The pin selector refers to a dedicated function pin */ |
| 365 | const struct rzg2l_dedicated_configs *dedicated_config; |
| 366 | |
| 367 | if (pin_selector >= data->num_dedicated_pins) { |
| 368 | dev_err(dev, "Invalid dedicated pin %u\n", pin_selector); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | |
| 372 | if (pin_selector >= ARRAY_SIZE(rzg2l_dedicated_pins.common)) { |
| 373 | pin_selector -= ARRAY_SIZE(rzg2l_dedicated_pins.common); |
| 374 | dedicated_config = &rzg2l_dedicated_pins.rzg2l_pins[pin_selector]; |
| 375 | } else { |
| 376 | dedicated_config = &rzg2l_dedicated_pins.common[pin_selector]; |
| 377 | } |
| 378 | |
| 379 | port_offset = RZG2L_SINGLE_PIN_GET_PORT_OFFSET(dedicated_config->config); |
| 380 | pin = RZG2L_SINGLE_PIN_GET_BIT(dedicated_config->config); |
| 381 | cfg = RZG2L_SINGLE_PIN_GET_CFGS(dedicated_config->config); |
| 382 | } |
| 383 | |
| 384 | switch (param) { |
| 385 | case PIN_CONFIG_INPUT_ENABLE: { |
| 386 | if (!(cfg & PIN_CFG_IEN)) { |
| 387 | dev_err(dev, "pin does not support IEN\n"); |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | dev_dbg(dev, "port off %u:%u set IEN=%u\n", |
| 392 | port_offset, pin, argument); |
| 393 | rzg2l_rmw_pin_config(data, IEN(port_offset), pin, IEN_MASK, !!argument); |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | case PIN_CONFIG_POWER_SOURCE: { |
| 398 | u32 pwr_reg = 0x0; |
| 399 | |
| 400 | /* argument is in mV */ |
| 401 | if (argument != 1800 && argument != 3300) { |
| 402 | dev_err(dev, "Invalid mV %u\n", argument); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * TODO: PIN_CFG_IO_VMC_ETH0 & PIN_CFG_IO_VMC_ETH1 will be |
| 408 | * handled when the RZ/G2L Ethernet driver is added. |
| 409 | */ |
| 410 | if (cfg & PIN_CFG_IO_VMC_SD0) { |
| 411 | dev_dbg(dev, "port off %u:%u set SD_CH 0 PVDD=%u\n", |
| 412 | port_offset, pin, argument); |
| 413 | pwr_reg = SD_CH(0); |
| 414 | } else if (cfg & PIN_CFG_IO_VMC_SD1) { |
| 415 | dev_dbg(dev, "port off %u:%u set SD_CH 1 PVDD=%u\n", |
| 416 | port_offset, pin, argument); |
| 417 | pwr_reg = SD_CH(1); |
| 418 | } else if (cfg & PIN_CFG_IO_VMC_QSPI) { |
| 419 | dev_dbg(dev, "port off %u:%u set QSPI PVDD=%u\n", |
| 420 | port_offset, pin, argument); |
| 421 | pwr_reg = QSPI; |
| 422 | } else { |
| 423 | dev_dbg(dev, "pin power source is not selectable\n"); |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | |
| 427 | writel((argument == 1800) ? PVDD_1800 : PVDD_3300, |
| 428 | data->base + pwr_reg); |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | default: |
| 433 | dev_err(dev, "Invalid pinconf parameter\n"); |
| 434 | return -EOPNOTSUPP; |
| 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int rzg2l_pinmux_property_set(struct udevice *dev, u32 pinmux_group) |
| 441 | { |
| 442 | const struct rzg2l_pfc_data *data = |
| 443 | (const struct rzg2l_pfc_data *)dev_get_driver_data(dev); |
| 444 | u32 port, pin, func, pfc_state; |
| 445 | u8 pmc_state; |
| 446 | |
| 447 | func = RZG2L_PINMUX_TO_FUNC(pinmux_group); |
| 448 | if (func > 5) { |
| 449 | dev_err(dev, "Invalid pin function %u\n", func); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | port = RZG2L_PINMUX_TO_PORT(pinmux_group); |
| 454 | pin = RZG2L_PINMUX_TO_PIN(pinmux_group); |
| 455 | if (!rzg2l_port_validate(data, port, pin)) { |
| 456 | dev_err(dev, "Invalid pin selector %u:%u\n", port, pin); |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
| 460 | /* Check current PMC & PFC to decide if we need to change anything. */ |
| 461 | pmc_state = readb(data->base + PMC(port)) & BIT(pin); |
| 462 | pfc_state = (readl(data->base + PFC(port)) >> (pin * 4)) & PFC_MASK; |
| 463 | if (pmc_state && pfc_state == func) |
| 464 | return 0; |
| 465 | |
| 466 | dev_dbg(dev, "pinmux port %u pin %u func %u\n", port, pin, func); |
| 467 | |
| 468 | /* Set pin to 'Non-use (Hi-Z input protection)' */ |
| 469 | clrbits_le16(data->base + PM(port), PM_MASK << (pin * 2)); |
| 470 | |
| 471 | /* Temporarily switch to GPIO mode with PMC register */ |
| 472 | clrbits_8(data->base + PMC(port), BIT(pin)); |
| 473 | |
| 474 | /* Set the PWPR register to allow PFC register to write */ |
| 475 | writel(0x0, data->base + PWPR); /* B0WI=0, PFCWE=0 */ |
| 476 | writel(PWPR_PFCWE, data->base + PWPR); /* B0WI=0, PFCWE=1 */ |
| 477 | |
| 478 | /* Select Pin function mode with PFC register */ |
| 479 | clrsetbits_le32(data->base + PFC(port), PFC_MASK << (pin * 4), |
| 480 | func << (pin * 4)); |
| 481 | |
| 482 | /* Set the PWPR register to be write-protected */ |
| 483 | writel(0x0, data->base + PWPR); /* B0WI=0, PFCWE=0 */ |
| 484 | writel(PWPR_B0WI, data->base + PWPR); /* B0WI=1, PFCWE=0 */ |
| 485 | |
| 486 | /* Switch to Peripheral pin function with PMC register */ |
| 487 | setbits_8(data->base + PMC(port), BIT(pin)); |
| 488 | |
| 489 | return rzg2l_selector_encode(data, port, pin); |
| 490 | } |
| 491 | |
| 492 | static int rzg2l_get_pin_muxing(struct udevice *dev, unsigned int selector, |
| 493 | char *buf, int size) |
| 494 | { |
| 495 | const struct rzg2l_pfc_data *data = |
| 496 | (const struct rzg2l_pfc_data *)dev_get_driver_data(dev); |
| 497 | u32 pmc_state; |
| 498 | int port; |
| 499 | u8 pin; |
| 500 | |
| 501 | if (selector < data->num_dedicated_pins) { |
| 502 | snprintf(buf, size, rzg2l_get_pin_name(dev, selector)); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | port = rzg2l_selector_decode(data, selector, &pin); |
| 507 | if (port < 0) { |
| 508 | dev_err(dev, "Invalid pin selector %u:%u\n", port, pin); |
| 509 | return port; |
| 510 | } |
| 511 | |
| 512 | pmc_state = readb(data->base + PMC(port)) & BIT(pin); |
| 513 | if (pmc_state) { |
| 514 | u32 pfc_state = (readl(data->base + PFC(port)) >> (pin * 4)) & PFC_MASK; |
| 515 | snprintf(buf, size, "Function %d", pfc_state); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | snprintf(buf, size, "GPIO"); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static const struct pinconf_param rzg2l_pinconf_params[] = { |
| 524 | { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 }, |
| 525 | { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 /* mV */ }, |
| 526 | }; |
| 527 | |
| 528 | static const struct pinctrl_ops rzg2l_pinctrl_ops = { |
| 529 | .get_pins_count = rzg2l_get_pins_count, |
| 530 | .get_pin_name = rzg2l_get_pin_name, |
| 531 | |
| 532 | .pinconf_num_params = ARRAY_SIZE(rzg2l_pinconf_params), |
| 533 | .pinconf_params = rzg2l_pinconf_params, |
| 534 | .pinconf_set = rzg2l_pinconf_set, |
| 535 | |
| 536 | .pinmux_property_set = rzg2l_pinmux_property_set, |
| 537 | .set_state = pinctrl_generic_set_state, |
| 538 | .get_pin_muxing = rzg2l_get_pin_muxing, |
| 539 | }; |
| 540 | |
| 541 | static int rzg2l_pinctrl_probe(struct udevice *dev) |
| 542 | { |
| 543 | return rzg2l_pfc_enable(dev); |
| 544 | } |
| 545 | |
| 546 | U_BOOT_DRIVER(rzg2l_pfc_pinctrl) = { |
| 547 | .name = "rzg2l-pfc-pinctrl", |
| 548 | .id = UCLASS_PINCTRL, |
| 549 | .ops = &rzg2l_pinctrl_ops, |
| 550 | .probe = rzg2l_pinctrl_probe, |
| 551 | }; |
| 552 | |
| 553 | static const struct rzg2l_pfc_driver_data r9a07g044_driver_data = { |
| 554 | .num_dedicated_pins = ARRAY_SIZE(rzg2l_dedicated_pins.common) + |
| 555 | ARRAY_SIZE(rzg2l_dedicated_pins.rzg2l_pins), |
| 556 | .num_ports = ARRAY_SIZE(r9a07g044_gpio_configs), |
| 557 | .gpio_configs = r9a07g044_gpio_configs, |
| 558 | }; |
| 559 | |
| 560 | static const struct udevice_id rzg2l_pfc_ids[] = { |
| 561 | { .compatible = "renesas,r9a07g044-pinctrl", .data = (ulong)&r9a07g044_driver_data }, |
| 562 | { /* sentinel */ } |
| 563 | }; |
| 564 | |
| 565 | static int rzg2l_pfc_bind(struct udevice *parent) |
| 566 | { |
| 567 | struct rzg2l_pfc_driver_data *driver_data; |
| 568 | struct rzg2l_pfc_data *data; |
| 569 | struct driver *drv; |
| 570 | unsigned int i; |
| 571 | |
| 572 | driver_data = |
| 573 | (struct rzg2l_pfc_driver_data *)dev_get_driver_data(parent); |
| 574 | if (!driver_data) |
| 575 | return -EINVAL; |
| 576 | data = devm_kmalloc(parent, sizeof(*data), 0); |
| 577 | if (!data) |
| 578 | return -ENOMEM; |
| 579 | |
| 580 | data->base = dev_read_addr_ptr(parent); |
| 581 | if (!data->base) |
| 582 | return -EINVAL; |
| 583 | data->num_dedicated_pins = driver_data->num_dedicated_pins; |
| 584 | data->num_ports = driver_data->num_ports; |
| 585 | data->gpio_configs = driver_data->gpio_configs; |
| 586 | |
| 587 | data->num_pins = 0; |
| 588 | for (i = 0; i < data->num_ports; i++) |
| 589 | data->num_pins += RZG2L_GPIO_PORT_GET_PINCNT(data->gpio_configs[i]); |
| 590 | dev_dbg(parent, "%u dedicated pins, %u muxed ports, %u muxed pins\n", |
| 591 | data->num_dedicated_pins, data->num_ports, data->num_pins); |
| 592 | |
| 593 | drv = lists_driver_lookup_name("rzg2l-pfc-pinctrl"); |
| 594 | if (!drv) |
| 595 | return -ENOENT; |
| 596 | |
| 597 | return device_bind_with_driver_data(parent, drv, parent->name, |
| 598 | (ulong)data, dev_ofnode(parent), |
| 599 | NULL); |
| 600 | } |
| 601 | |
| 602 | U_BOOT_DRIVER(rzg2l_pfc) = { |
| 603 | .name = "rzg2l-pfc", |
| 604 | .id = UCLASS_NOP, |
| 605 | .of_match = rzg2l_pfc_ids, |
| 606 | .bind = rzg2l_pfc_bind, |
| 607 | }; |