Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Atmel PIO4 device driver |
| 4 | * |
| 5 | * Copyright (C) 2015 Atmel Corporation |
| 6 | * Wenyou.Yang <wenyou.yang@atmel.com> |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 7 | */ |
| 8 | #include <common.h> |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 9 | #include <clk.h> |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 10 | #include <dm.h> |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 11 | #include <fdtdec.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 13 | #include <asm/arch/hardware.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 17 | #include <mach/gpio.h> |
| 18 | #include <mach/atmel_pio4.h> |
| 19 | |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 22 | static struct atmel_pio4_port *atmel_pio4_port_base(u32 port) |
| 23 | { |
| 24 | struct atmel_pio4_port *base = NULL; |
| 25 | |
| 26 | switch (port) { |
| 27 | case AT91_PIO_PORTA: |
| 28 | base = (struct atmel_pio4_port *)ATMEL_BASE_PIOA; |
| 29 | break; |
| 30 | case AT91_PIO_PORTB: |
| 31 | base = (struct atmel_pio4_port *)ATMEL_BASE_PIOB; |
| 32 | break; |
| 33 | case AT91_PIO_PORTC: |
| 34 | base = (struct atmel_pio4_port *)ATMEL_BASE_PIOC; |
| 35 | break; |
| 36 | case AT91_PIO_PORTD: |
| 37 | base = (struct atmel_pio4_port *)ATMEL_BASE_PIOD; |
| 38 | break; |
Mihai Sain | 0595e88 | 2022-05-25 13:32:08 +0300 | [diff] [blame] | 39 | #if (ATMEL_PIO_PORTS > 4) |
| 40 | case AT91_PIO_PORTE: |
| 41 | base = (struct atmel_pio4_port *)ATMEL_BASE_PIOE; |
| 42 | break; |
| 43 | #endif |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 44 | default: |
| 45 | printf("Error: Atmel PIO4: Failed to get PIO base of port#%d!\n", |
| 46 | port); |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | return base; |
| 51 | } |
| 52 | |
| 53 | static int atmel_pio4_config_io_func(u32 port, u32 pin, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 54 | u32 func, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 55 | { |
| 56 | struct atmel_pio4_port *port_base; |
| 57 | u32 reg, mask; |
| 58 | |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 59 | if (pin >= ATMEL_PIO_NPINS_PER_BANK) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 60 | return -EINVAL; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 61 | |
| 62 | port_base = atmel_pio4_port_base(port); |
| 63 | if (!port_base) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 64 | return -EINVAL; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 65 | |
| 66 | mask = 1 << pin; |
| 67 | reg = func; |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 68 | reg |= config; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 69 | |
| 70 | writel(mask, &port_base->mskr); |
| 71 | writel(reg, &port_base->cfgr); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 76 | int atmel_pio4_set_gpio(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 77 | { |
| 78 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 79 | ATMEL_PIO_CFGR_FUNC_GPIO, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 80 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 81 | } |
| 82 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 83 | int atmel_pio4_set_a_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 84 | { |
| 85 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 86 | ATMEL_PIO_CFGR_FUNC_PERIPH_A, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 87 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 90 | int atmel_pio4_set_b_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 91 | { |
| 92 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 93 | ATMEL_PIO_CFGR_FUNC_PERIPH_B, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 94 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 95 | } |
| 96 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 97 | int atmel_pio4_set_c_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 98 | { |
| 99 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 100 | ATMEL_PIO_CFGR_FUNC_PERIPH_C, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 101 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 102 | } |
| 103 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 104 | int atmel_pio4_set_d_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 105 | { |
| 106 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 107 | ATMEL_PIO_CFGR_FUNC_PERIPH_D, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 108 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 109 | } |
| 110 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 111 | int atmel_pio4_set_e_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 112 | { |
| 113 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 114 | ATMEL_PIO_CFGR_FUNC_PERIPH_E, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 115 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 118 | int atmel_pio4_set_f_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 119 | { |
| 120 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 121 | ATMEL_PIO_CFGR_FUNC_PERIPH_F, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 122 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 125 | int atmel_pio4_set_g_periph(u32 port, u32 pin, u32 config) |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 126 | { |
| 127 | return atmel_pio4_config_io_func(port, pin, |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 128 | ATMEL_PIO_CFGR_FUNC_PERIPH_G, |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 129 | config); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int atmel_pio4_set_pio_output(u32 port, u32 pin, u32 value) |
| 133 | { |
| 134 | struct atmel_pio4_port *port_base; |
| 135 | u32 reg, mask; |
| 136 | |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 137 | if (pin >= ATMEL_PIO_NPINS_PER_BANK) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 138 | return -EINVAL; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 139 | |
| 140 | port_base = atmel_pio4_port_base(port); |
| 141 | if (!port_base) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 142 | return -EINVAL; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 143 | |
| 144 | mask = 0x01 << pin; |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 145 | reg = ATMEL_PIO_CFGR_FUNC_GPIO | ATMEL_PIO_DIR_MASK; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 146 | |
| 147 | writel(mask, &port_base->mskr); |
| 148 | writel(reg, &port_base->cfgr); |
| 149 | |
| 150 | if (value) |
| 151 | writel(mask, &port_base->sodr); |
| 152 | else |
| 153 | writel(mask, &port_base->codr); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int atmel_pio4_get_pio_input(u32 port, u32 pin) |
| 159 | { |
| 160 | struct atmel_pio4_port *port_base; |
| 161 | u32 reg, mask; |
| 162 | |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 163 | if (pin >= ATMEL_PIO_NPINS_PER_BANK) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 164 | return -EINVAL; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 165 | |
| 166 | port_base = atmel_pio4_port_base(port); |
| 167 | if (!port_base) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 168 | return -EINVAL; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 169 | |
| 170 | mask = 0x01 << pin; |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 171 | reg = ATMEL_PIO_CFGR_FUNC_GPIO; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 172 | |
| 173 | writel(mask, &port_base->mskr); |
| 174 | writel(reg, &port_base->cfgr); |
| 175 | |
| 176 | return (readl(&port_base->pdsr) & mask) ? 1 : 0; |
| 177 | } |
| 178 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 179 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 180 | |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 181 | /** |
| 182 | * struct atmel_pioctrl_data - Atmel PIO controller (pinmux + gpio) data struct |
| 183 | * @nbanks: number of PIO banks |
| 184 | * @last_bank_count: number of lines in the last bank (can be less than |
| 185 | * the rest of the banks). |
| 186 | */ |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 187 | struct atmel_pioctrl_data { |
| 188 | u32 nbanks; |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 189 | u32 last_bank_count; |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 190 | }; |
| 191 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 192 | struct atmel_pio4_plat { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 193 | struct atmel_pio4_port *reg_base; |
| 194 | }; |
| 195 | |
| 196 | static struct atmel_pio4_port *atmel_pio4_bank_base(struct udevice *dev, |
| 197 | u32 bank) |
| 198 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 199 | struct atmel_pio4_plat *plat = dev_get_plat(dev); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 200 | struct atmel_pio4_port *port_base = |
| 201 | (struct atmel_pio4_port *)((u32)plat->reg_base + |
| 202 | ATMEL_PIO_BANK_OFFSET * bank); |
| 203 | |
| 204 | return port_base; |
| 205 | } |
| 206 | |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 207 | static int atmel_pio4_direction_input(struct udevice *dev, unsigned offset) |
| 208 | { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 209 | u32 bank = ATMEL_PIO_BANK(offset); |
| 210 | u32 line = ATMEL_PIO_LINE(offset); |
| 211 | struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank); |
| 212 | u32 mask = BIT(line); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 213 | |
| 214 | writel(mask, &port_base->mskr); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 215 | |
| 216 | clrbits_le32(&port_base->cfgr, |
| 217 | ATMEL_PIO_CFGR_FUNC_MASK | ATMEL_PIO_DIR_MASK); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int atmel_pio4_direction_output(struct udevice *dev, |
| 223 | unsigned offset, int value) |
| 224 | { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 225 | u32 bank = ATMEL_PIO_BANK(offset); |
| 226 | u32 line = ATMEL_PIO_LINE(offset); |
| 227 | struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank); |
| 228 | u32 mask = BIT(line); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 229 | |
| 230 | writel(mask, &port_base->mskr); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 231 | |
| 232 | clrsetbits_le32(&port_base->cfgr, |
| 233 | ATMEL_PIO_CFGR_FUNC_MASK, ATMEL_PIO_DIR_MASK); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 234 | |
| 235 | if (value) |
| 236 | writel(mask, &port_base->sodr); |
| 237 | else |
| 238 | writel(mask, &port_base->codr); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int atmel_pio4_get_value(struct udevice *dev, unsigned offset) |
| 244 | { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 245 | u32 bank = ATMEL_PIO_BANK(offset); |
| 246 | u32 line = ATMEL_PIO_LINE(offset); |
| 247 | struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank); |
| 248 | u32 mask = BIT(line); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 249 | |
| 250 | return (readl(&port_base->pdsr) & mask) ? 1 : 0; |
| 251 | } |
| 252 | |
| 253 | static int atmel_pio4_set_value(struct udevice *dev, |
| 254 | unsigned offset, int value) |
| 255 | { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 256 | u32 bank = ATMEL_PIO_BANK(offset); |
| 257 | u32 line = ATMEL_PIO_LINE(offset); |
| 258 | struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank); |
| 259 | u32 mask = BIT(line); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 260 | |
| 261 | if (value) |
| 262 | writel(mask, &port_base->sodr); |
| 263 | else |
| 264 | writel(mask, &port_base->codr); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int atmel_pio4_get_function(struct udevice *dev, unsigned offset) |
| 270 | { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 271 | u32 bank = ATMEL_PIO_BANK(offset); |
| 272 | u32 line = ATMEL_PIO_LINE(offset); |
| 273 | struct atmel_pio4_port *port_base = atmel_pio4_bank_base(dev, bank); |
| 274 | u32 mask = BIT(line); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 275 | |
| 276 | writel(mask, &port_base->mskr); |
| 277 | |
| 278 | return (readl(&port_base->cfgr) & |
Wenyou Yang | 312bf89 | 2016-07-20 17:16:25 +0800 | [diff] [blame] | 279 | ATMEL_PIO_DIR_MASK) ? GPIOF_OUTPUT : GPIOF_INPUT; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static const struct dm_gpio_ops atmel_pio4_ops = { |
| 283 | .direction_input = atmel_pio4_direction_input, |
| 284 | .direction_output = atmel_pio4_direction_output, |
| 285 | .get_value = atmel_pio4_get_value, |
| 286 | .set_value = atmel_pio4_set_value, |
| 287 | .get_function = atmel_pio4_get_function, |
| 288 | }; |
| 289 | |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 290 | static int atmel_pio4_bind(struct udevice *dev) |
| 291 | { |
Simon Glass | 292796f | 2017-05-17 17:18:06 -0600 | [diff] [blame] | 292 | return dm_scan_fdt_dev(dev); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 293 | } |
| 294 | |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 295 | static int atmel_pio4_probe(struct udevice *dev) |
| 296 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 297 | struct atmel_pio4_plat *plat = dev_get_plat(dev); |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 298 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 299 | struct atmel_pioctrl_data *pioctrl_data; |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 300 | struct clk clk; |
| 301 | fdt_addr_t addr_base; |
| 302 | u32 nbanks; |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 303 | int ret; |
| 304 | |
| 305 | ret = clk_get_by_index(dev, 0, &clk); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 309 | ret = clk_enable(&clk); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | |
| 313 | clk_free(&clk); |
| 314 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 315 | addr_base = dev_read_addr(dev); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 316 | if (addr_base == FDT_ADDR_T_NONE) |
| 317 | return -EINVAL; |
| 318 | |
| 319 | plat->reg_base = (struct atmel_pio4_port *)addr_base; |
| 320 | |
| 321 | pioctrl_data = (struct atmel_pioctrl_data *)dev_get_driver_data(dev); |
| 322 | nbanks = pioctrl_data->nbanks; |
| 323 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 324 | uc_priv->bank_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), |
| 325 | NULL); |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 326 | uc_priv->gpio_count = nbanks * ATMEL_PIO_NPINS_PER_BANK; |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 327 | |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 328 | /* if last bank has limited number of pins, adjust accordingly */ |
| 329 | if (pioctrl_data->last_bank_count != ATMEL_PIO_NPINS_PER_BANK) { |
| 330 | uc_priv->gpio_count -= ATMEL_PIO_NPINS_PER_BANK; |
| 331 | uc_priv->gpio_count += pioctrl_data->last_bank_count; |
| 332 | } |
| 333 | |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 337 | /* |
| 338 | * The number of banks can be different from a SoC to another one. |
| 339 | * We can have up to 16 banks. |
| 340 | */ |
| 341 | static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = { |
| 342 | .nbanks = 4, |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 343 | .last_bank_count = ATMEL_PIO_NPINS_PER_BANK, |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 344 | }; |
| 345 | |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 346 | static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data = { |
| 347 | .nbanks = 5, |
| 348 | .last_bank_count = 8, /* 5th bank has only 8 lines on sama7g5 */ |
| 349 | }; |
| 350 | |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 351 | static const struct udevice_id atmel_pio4_ids[] = { |
| 352 | { |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 353 | .data = (ulong)&atmel_sama5d2_pioctrl_data, |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 354 | }, { |
Eugen Hristev | 2adaea1 | 2021-04-07 11:39:28 +0300 | [diff] [blame] | 355 | .data = (ulong)µchip_sama7g5_pioctrl_data, |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 356 | }, |
| 357 | {} |
| 358 | }; |
| 359 | |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 360 | U_BOOT_DRIVER(gpio_atmel_pio4) = { |
| 361 | .name = "gpio_atmel_pio4", |
| 362 | .id = UCLASS_GPIO, |
| 363 | .ops = &atmel_pio4_ops, |
| 364 | .probe = atmel_pio4_probe, |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 365 | .bind = atmel_pio4_bind, |
| 366 | .of_match = atmel_pio4_ids, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 367 | .plat_auto = sizeof(struct atmel_pio4_plat), |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 368 | }; |
Wenyou Yang | 0b97152 | 2016-07-20 17:16:26 +0800 | [diff] [blame] | 369 | |
Wenyou Yang | 5a09d13 | 2015-11-04 14:25:13 +0800 | [diff] [blame] | 370 | #endif |