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