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