1*1bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2ce72d0c6SVictor Chong /* 3ce72d0c6SVictor Chong * Copyright (c) 2016, Linaro Limited 4ce72d0c6SVictor Chong * All rights reserved. 5ce72d0c6SVictor Chong * 6ce72d0c6SVictor Chong * Redistribution and use in source and binary forms, with or without 7ce72d0c6SVictor Chong * modification, are permitted provided that the following conditions are met: 8ce72d0c6SVictor Chong * 9ce72d0c6SVictor Chong * 1. Redistributions of source code must retain the above copyright notice, 10ce72d0c6SVictor Chong * this list of conditions and the following disclaimer. 11ce72d0c6SVictor Chong * 12ce72d0c6SVictor Chong * 2. Redistributions in binary form must reproduce the above copyright notice, 13ce72d0c6SVictor Chong * this list of conditions and the following disclaimer in the documentation 14ce72d0c6SVictor Chong * and/or other materials provided with the distribution. 15ce72d0c6SVictor Chong * 16ce72d0c6SVictor Chong * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17ce72d0c6SVictor Chong * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18ce72d0c6SVictor Chong * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19ce72d0c6SVictor Chong * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20ce72d0c6SVictor Chong * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21ce72d0c6SVictor Chong * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22ce72d0c6SVictor Chong * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23ce72d0c6SVictor Chong * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24ce72d0c6SVictor Chong * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25ce72d0c6SVictor Chong * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26ce72d0c6SVictor Chong * POSSIBILITY OF SUCH DAMAGE. 27ce72d0c6SVictor Chong */ 28ce72d0c6SVictor Chong 29ce72d0c6SVictor Chong #include <assert.h> 30ce72d0c6SVictor Chong #include <drivers/pl061_gpio.h> 31bbab0cddSVictor Chong #include <io.h> 32bbab0cddSVictor Chong #include <trace.h> 33bbab0cddSVictor Chong #include <util.h> 34ce72d0c6SVictor Chong 35ce72d0c6SVictor Chong #ifndef PLAT_PL061_MAX_GPIOS 36ce72d0c6SVictor Chong # define PLAT_PL061_MAX_GPIOS 32 37ce72d0c6SVictor Chong #endif /* PLAT_PL061_MAX_GPIOS */ 38ce72d0c6SVictor Chong 39ce72d0c6SVictor Chong #define MAX_GPIO_DEVICES ((PLAT_PL061_MAX_GPIOS + \ 40ce72d0c6SVictor Chong (GPIOS_PER_PL061 - 1)) / GPIOS_PER_PL061) 41ce72d0c6SVictor Chong 42ce72d0c6SVictor Chong #define GPIOS_PER_PL061 8 43ce72d0c6SVictor Chong 44bbab0cddSVictor Chong /* gpio register offsets */ 45bbab0cddSVictor Chong #define GPIODIR 0x400 46bbab0cddSVictor Chong #define GPIOIS 0x404 47bbab0cddSVictor Chong #define GPIOIBE 0x408 48bbab0cddSVictor Chong #define GPIOIEV 0x40C 49bbab0cddSVictor Chong #define GPIOIE 0x410 50bbab0cddSVictor Chong #define GPIORIS 0x414 51bbab0cddSVictor Chong #define GPIOMIS 0x418 52bbab0cddSVictor Chong #define GPIOIC 0x41C 53bbab0cddSVictor Chong #define GPIOAFSEL 0x420 54bbab0cddSVictor Chong 55bbab0cddSVictor Chong /* gpio register masks */ 56bbab0cddSVictor Chong #define GPIOIE_ENABLED SHIFT_U32(1, 0) 57bbab0cddSVictor Chong #define GPIOIE_MASKED SHIFT_U32(0, 0) 58bbab0cddSVictor Chong #define GPIOAFSEL_HW SHIFT_U32(1, 0) 59bbab0cddSVictor Chong #define GPIOAFSEL_SW SHIFT_U32(0, 0) 60bbab0cddSVictor Chong #define GPIODIR_OUT SHIFT_U32(1, 0) 61bbab0cddSVictor Chong #define GPIODIR_IN SHIFT_U32(0, 0) 62ce72d0c6SVictor Chong 63ce72d0c6SVictor Chong static vaddr_t pl061_reg_base[MAX_GPIO_DEVICES]; 64ce72d0c6SVictor Chong 65ce72d0c6SVictor Chong static enum gpio_dir pl061_get_direction(unsigned int gpio_pin) 66ce72d0c6SVictor Chong { 67ce72d0c6SVictor Chong vaddr_t base_addr; 68ce72d0c6SVictor Chong uint8_t data; 69ce72d0c6SVictor Chong unsigned int offset; 70ce72d0c6SVictor Chong 71ce72d0c6SVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 72ce72d0c6SVictor Chong 73ce72d0c6SVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 74ce72d0c6SVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 75bbab0cddSVictor Chong data = read8(base_addr + GPIODIR); 76ce72d0c6SVictor Chong if (data & BIT(offset)) 77ce72d0c6SVictor Chong return GPIO_DIR_OUT; 78ce72d0c6SVictor Chong return GPIO_DIR_IN; 79ce72d0c6SVictor Chong } 80ce72d0c6SVictor Chong 81ce72d0c6SVictor Chong static void pl061_set_direction(unsigned int gpio_pin, enum gpio_dir direction) 82ce72d0c6SVictor Chong { 83ce72d0c6SVictor Chong vaddr_t base_addr; 84ce72d0c6SVictor Chong uint8_t data; 85ce72d0c6SVictor Chong unsigned int offset; 86ce72d0c6SVictor Chong 87ce72d0c6SVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 88ce72d0c6SVictor Chong 89ce72d0c6SVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 90ce72d0c6SVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 91ce72d0c6SVictor Chong if (direction == GPIO_DIR_OUT) { 92bbab0cddSVictor Chong data = read8(base_addr + GPIODIR) | BIT(offset); 93bbab0cddSVictor Chong write8(data, base_addr + GPIODIR); 94ce72d0c6SVictor Chong } else { 95bbab0cddSVictor Chong data = read8(base_addr + GPIODIR) & ~BIT(offset); 96bbab0cddSVictor Chong write8(data, base_addr + GPIODIR); 97ce72d0c6SVictor Chong } 98ce72d0c6SVictor Chong } 99ce72d0c6SVictor Chong 100ce72d0c6SVictor Chong /* 101ce72d0c6SVictor Chong * The offset of GPIODATA register is 0. 102ce72d0c6SVictor Chong * The values read from GPIODATA are determined for each bit, by the mask bit 103ce72d0c6SVictor Chong * derived from the address used to access the data register, PADDR[9:2]. 104ce72d0c6SVictor Chong * Bits that are 1 in the address mask cause the corresponding bits in GPIODATA 105ce72d0c6SVictor Chong * to be read, and bits that are 0 in the address mask cause the corresponding 106ce72d0c6SVictor Chong * bits in GPIODATA to be read as 0, regardless of their value. 107ce72d0c6SVictor Chong */ 108ce72d0c6SVictor Chong static enum gpio_level pl061_get_value(unsigned int gpio_pin) 109ce72d0c6SVictor Chong { 110ce72d0c6SVictor Chong vaddr_t base_addr; 111ce72d0c6SVictor Chong unsigned int offset; 112ce72d0c6SVictor Chong 113ce72d0c6SVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 114ce72d0c6SVictor Chong 115ce72d0c6SVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 116ce72d0c6SVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 117ce72d0c6SVictor Chong if (read8(base_addr + BIT(offset + 2))) 118ce72d0c6SVictor Chong return GPIO_LEVEL_HIGH; 119ce72d0c6SVictor Chong return GPIO_LEVEL_LOW; 120ce72d0c6SVictor Chong } 121ce72d0c6SVictor Chong 122ce72d0c6SVictor Chong /* 123ce72d0c6SVictor Chong * In order to write GPIODATA, the corresponding bits in the mask, resulting 124ce72d0c6SVictor Chong * from the address bus, PADDR[9:2], must be HIGH. Otherwise the bit values 125ce72d0c6SVictor Chong * remain unchanged by the write. 126ce72d0c6SVictor Chong */ 127ce72d0c6SVictor Chong static void pl061_set_value(unsigned int gpio_pin, enum gpio_level value) 128ce72d0c6SVictor Chong { 129ce72d0c6SVictor Chong vaddr_t base_addr; 130ce72d0c6SVictor Chong unsigned int offset; 131ce72d0c6SVictor Chong 132ce72d0c6SVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 133ce72d0c6SVictor Chong 134ce72d0c6SVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 135ce72d0c6SVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 136ce72d0c6SVictor Chong if (value == GPIO_LEVEL_HIGH) 1373af633ebSVictor Chong write8(BIT(offset), base_addr + BIT(offset + 2)); 138ce72d0c6SVictor Chong else 1393af633ebSVictor Chong write8(0, base_addr + BIT(offset + 2)); 140ce72d0c6SVictor Chong } 141ce72d0c6SVictor Chong 142f1d7853eSVictor Chong static enum gpio_interrupt pl061_get_interrupt(unsigned int gpio_pin) 143f1d7853eSVictor Chong { 144f1d7853eSVictor Chong vaddr_t base_addr; 145f1d7853eSVictor Chong uint8_t data; 146f1d7853eSVictor Chong unsigned int offset; 147f1d7853eSVictor Chong 148f1d7853eSVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 149f1d7853eSVictor Chong 150f1d7853eSVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 151f1d7853eSVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 152f1d7853eSVictor Chong data = read8(base_addr + GPIOIE); 153f1d7853eSVictor Chong if (data & BIT(offset)) 154f1d7853eSVictor Chong return GPIO_INTERRUPT_ENABLE; 155f1d7853eSVictor Chong return GPIO_INTERRUPT_DISABLE; 156f1d7853eSVictor Chong } 157f1d7853eSVictor Chong 158f1d7853eSVictor Chong static void pl061_set_interrupt(unsigned int gpio_pin, 159f1d7853eSVictor Chong enum gpio_interrupt ena_dis) 160f1d7853eSVictor Chong { 161f1d7853eSVictor Chong vaddr_t base_addr; 162f1d7853eSVictor Chong uint8_t data; 163f1d7853eSVictor Chong unsigned int offset; 164f1d7853eSVictor Chong 165f1d7853eSVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 166f1d7853eSVictor Chong 167f1d7853eSVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 168f1d7853eSVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 169f1d7853eSVictor Chong if (ena_dis == GPIO_INTERRUPT_ENABLE) { 170f1d7853eSVictor Chong data = read8(base_addr + GPIOIE) | BIT(offset); 171f1d7853eSVictor Chong write8(data, base_addr + GPIOIE); 172f1d7853eSVictor Chong } else { 173f1d7853eSVictor Chong data = read8(base_addr + GPIOIE) & ~BIT(offset); 174f1d7853eSVictor Chong write8(data, base_addr + GPIOIE); 175f1d7853eSVictor Chong } 176f1d7853eSVictor Chong } 177ce72d0c6SVictor Chong 178ce72d0c6SVictor Chong /* 179ce72d0c6SVictor Chong * Register the PL061 GPIO controller with a base address and the offset 180ce72d0c6SVictor Chong * of start pin in this GPIO controller. 181bbab0cddSVictor Chong * This function is called after pl061_init(). 182ce72d0c6SVictor Chong */ 183bbab0cddSVictor Chong void pl061_register(vaddr_t base_addr, unsigned int gpio_dev) 184ce72d0c6SVictor Chong { 185ce72d0c6SVictor Chong assert(gpio_dev < MAX_GPIO_DEVICES); 186ce72d0c6SVictor Chong 187ce72d0c6SVictor Chong pl061_reg_base[gpio_dev] = base_addr; 188ce72d0c6SVictor Chong } 189ce72d0c6SVictor Chong 190bbab0cddSVictor Chong static const struct gpio_ops pl061_ops = { 191bbab0cddSVictor Chong .get_direction = pl061_get_direction, 192bbab0cddSVictor Chong .set_direction = pl061_set_direction, 193bbab0cddSVictor Chong .get_value = pl061_get_value, 194bbab0cddSVictor Chong .set_value = pl061_set_value, 195f1d7853eSVictor Chong .get_interrupt = pl061_get_interrupt, 196f1d7853eSVictor Chong .set_interrupt = pl061_set_interrupt, 197bbab0cddSVictor Chong }; 198bbab0cddSVictor Chong 199ce72d0c6SVictor Chong /* 200bbab0cddSVictor Chong * Initialize PL061 GPIO controller 201ce72d0c6SVictor Chong */ 202bbab0cddSVictor Chong void pl061_init(struct pl061_data *pd) 203ce72d0c6SVictor Chong { 204ce72d0c6SVictor Chong COMPILE_TIME_ASSERT(PLAT_PL061_MAX_GPIOS > 0); 205bbab0cddSVictor Chong 206bbab0cddSVictor Chong assert(pd); 207bbab0cddSVictor Chong pd->chip.ops = &pl061_ops; 208ce72d0c6SVictor Chong } 209f1d7853eSVictor Chong 210f1d7853eSVictor Chong enum pl061_mode_control pl061_get_mode_control(unsigned int gpio_pin) 211f1d7853eSVictor Chong { 212f1d7853eSVictor Chong vaddr_t base_addr; 213f1d7853eSVictor Chong uint8_t data; 214f1d7853eSVictor Chong unsigned int offset; 215f1d7853eSVictor Chong 216f1d7853eSVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 217f1d7853eSVictor Chong 218f1d7853eSVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 219f1d7853eSVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 220f1d7853eSVictor Chong data = read8(base_addr + GPIOAFSEL); 221f1d7853eSVictor Chong if (data & BIT(offset)) 222f1d7853eSVictor Chong return PL061_MC_HW; 223f1d7853eSVictor Chong return PL061_MC_SW; 224f1d7853eSVictor Chong } 225f1d7853eSVictor Chong 226f1d7853eSVictor Chong void pl061_set_mode_control(unsigned int gpio_pin, 227f1d7853eSVictor Chong enum pl061_mode_control hw_sw) 228f1d7853eSVictor Chong { 229f1d7853eSVictor Chong vaddr_t base_addr; 230f1d7853eSVictor Chong uint8_t data; 231f1d7853eSVictor Chong unsigned int offset; 232f1d7853eSVictor Chong 233f1d7853eSVictor Chong assert(gpio_pin < PLAT_PL061_MAX_GPIOS); 234f1d7853eSVictor Chong 235f1d7853eSVictor Chong base_addr = pl061_reg_base[gpio_pin / GPIOS_PER_PL061]; 236f1d7853eSVictor Chong offset = gpio_pin % GPIOS_PER_PL061; 237f1d7853eSVictor Chong if (hw_sw == PL061_MC_HW) { 238f1d7853eSVictor Chong data = read8(base_addr + GPIOAFSEL) | BIT(offset); 239f1d7853eSVictor Chong write8(data, base_addr + GPIOAFSEL); 240f1d7853eSVictor Chong } else { 241f1d7853eSVictor Chong data = read8(base_addr + GPIOAFSEL) & ~BIT(offset); 242f1d7853eSVictor Chong write8(data, base_addr + GPIOAFSEL); 243f1d7853eSVictor Chong } 244f1d7853eSVictor Chong } 245