123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- /*
- * This file is part of FH8620 BSP for RT-Thread distribution.
- *
- * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
- * All rights reserved
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Visit http://www.fullhan.com to get contact with Fullhan.
- *
- * Change Logs:
- * Date Author Notes
- */
- #include "fh_def.h"
- #include "gpio.h"
- #include "libraries/inc/fh_gpio.h"
- #include "interrupt.h"
- #include "board_info.h"
- #include <rtdevice.h>
- #include "fh_arch.h"
- //#define FH_GPIO_DEBUG
- #ifdef FH_GPIO_DEBUG
- #define PRINT_GPIO_DBG(fmt, args...) \
- do \
- { \
- rt_kprintf("FH_GPIO_DEBUG: "); \
- rt_kprintf(fmt, ## args); \
- } \
- while(0)
- #else
- #define PRINT_GPIO_DBG(fmt, args...) do { } while (0)
- #endif
- int gpio_available[NUM_OF_GPIO];
- extern struct rt_irq_desc irq_desc[];
- static inline rt_uint32_t gpio_to_base(rt_uint32_t gpio)
- {
- if (gpio >= 32 && gpio < 64)
- {
- return GPIO1_REG_BASE;
- }
- else if(gpio < 32)
- {
- return GPIO0_REG_BASE;
- }
- else
- {
- rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
- return -RT_ERROR;
- }
- }
- static inline rt_uint32_t irq_to_base(rt_uint32_t irq)
- {
- return (irq-NR_INTERNAL_IRQS > 32) ? GPIO1_REG_BASE : GPIO0_REG_BASE;
- }
- static inline rt_uint32_t irq_to_bit(rt_uint32_t irq)
- {
- if(irq >= NR_INTERNAL_IRQS && irq < NR_INTERNAL_IRQS + 32)
- return 0;
- else
- return 32;
- }
- rt_uint32_t gpio_to_irq(rt_uint32_t gpio)
- {
- return (gpio + NR_INTERNAL_IRQS);
- }
- void gpio_enable_debounce(rt_uint32_t gpio)
- {
- rt_uint32_t tmp, base, offset;
- offset = gpio % 32;
- base = gpio_to_base(gpio);
- tmp = GET_REG(base + REG_GPIO_DEBOUNCE);
- tmp |= BIT(offset);
- SET_REG(base + REG_GPIO_DEBOUNCE, tmp);
- }
- void gpio_disable_debounce(rt_uint32_t gpio)
- {
- rt_uint32_t tmp, base, offset;
- offset = gpio % 32;
- base = gpio_to_base(gpio);
- tmp = GET_REG(base + REG_GPIO_DEBOUNCE);
- tmp &= ~BIT(offset);
- SET_REG(base + REG_GPIO_DEBOUNCE, tmp);
- }
- int gpio_get_value(rt_uint32_t gpio)
- {
- rt_uint32_t tmp, base, offset;
- offset = gpio % 32;
- base = gpio_to_base(gpio);
- tmp = GET_REG(base + REG_GPIO_SWPORTA_DDR);
- tmp &= BIT(offset);
- if (tmp) {
- tmp = GET_REG(base + REG_GPIO_SWPORTA_DR);
- } else {
- tmp = GET_REG(base + REG_GPIO_EXT_PORTA);
- }
- tmp &= BIT(offset);
- tmp = tmp >> offset;
- return tmp;
- }
- void gpio_set_value(rt_uint32_t gpio, int val)
- {
- rt_uint32_t tmp, base, offset;
- offset = gpio % 32;
- base = gpio_to_base(gpio);
- tmp = GET_REG(base + REG_GPIO_SWPORTA_DR);
- if(val)
- tmp |= BIT(offset);
- else
- tmp &= ~BIT(offset);
- SET_REG(base + REG_GPIO_SWPORTA_DR, tmp);
- }
- int gpio_get_direction(rt_uint32_t gpio)
- {
- rt_uint32_t tmp, base, offset;
- offset = gpio % 32;
- base = gpio_to_base(gpio);
- tmp = GET_REG(base + REG_GPIO_SWPORTA_DDR);
- tmp &= BIT(offset);
- tmp = tmp >> offset;
- return tmp;
- }
- void gpio_set_direction(rt_uint32_t gpio, rt_uint32_t direction)
- {
- rt_uint32_t tmp, base, offset;
- offset = gpio % 32;
- base = gpio_to_base(gpio);
- tmp = GET_REG(base + REG_GPIO_SWPORTA_DDR);
- if(direction == GPIO_DIR_OUTPUT)
- tmp |= BIT(offset);
- else
- tmp &= ~BIT(offset);
- SET_REG(base + REG_GPIO_SWPORTA_DDR, tmp);
- }
- int gpio_set_irq_type(rt_uint32_t gpio, rt_uint32_t type)
- {
- rt_uint32_t int_type, int_polarity;
- rt_uint32_t bit = gpio % 32;
- rt_uint32_t base;
- base = gpio_to_base(gpio);
- int_type = GET_REG(base + REG_GPIO_INTTYPE_LEVEL);
- int_polarity = GET_REG(base + REG_GPIO_INT_POLARITY);
- switch (type & IRQ_TYPE_TRIGGER_MASK) {
- case IRQ_TYPE_EDGE_BOTH:
- int_type |= BIT(bit);
- // toggle trigger
- if (gpio_get_value(gpio))
- int_polarity &= ~BIT(bit);
- else
- int_polarity |= BIT(bit);
- break;
- case IRQ_TYPE_EDGE_RISING:
- int_type |= BIT(bit);
- int_polarity |= BIT(bit);
- break;
- case IRQ_TYPE_EDGE_FALLING:
- int_type |= BIT(bit);
- int_polarity &= ~BIT(bit);
- break;
- case IRQ_TYPE_LEVEL_HIGH:
- int_type &= ~BIT(bit);
- int_polarity |= BIT(bit);
- break;
- case IRQ_TYPE_LEVEL_LOW:
- int_type &= ~BIT(bit);
- int_polarity &= ~BIT(bit);
- break;
- case IRQ_TYPE_NONE:
- return 0;
- default:
- return -RT_ERROR;
- }
- SET_REG(base + REG_GPIO_INTTYPE_LEVEL, int_type);
- SET_REG(base + REG_GPIO_INT_POLARITY, int_polarity);
- return 0;
- }
- int gpio_irq_mask(rt_uint32_t irq)
- {
- rt_uint32_t tmp, base, bit;
- base = irq_to_base(irq);
- bit = irq_to_bit(irq);
- tmp = GET_REG(base + REG_GPIO_INTMASK);
- tmp |= BIT(irq - NR_INTERNAL_IRQS - bit);
- SET_REG(base + REG_GPIO_INTMASK, tmp);
- return 0;
- }
- int gpio_irq_unmask(rt_uint32_t irq)
- {
- rt_uint32_t tmp, base, bit;
- base = irq_to_base(irq);
- bit = irq_to_bit(irq);
- tmp = GET_REG(base + REG_GPIO_INTMASK);
- tmp &= ~BIT((irq - NR_INTERNAL_IRQS - bit));
- SET_REG(base + REG_GPIO_INTMASK, tmp);
- return 0;
- }
- void gpio_irq_enable(rt_uint32_t irq)
- {
- rt_uint32_t tmp, base, bit;
- base = irq_to_base(irq);
- bit = irq_to_bit(irq);
- tmp = GET_REG(base + REG_GPIO_INTEN);
- tmp |= BIT(irq - NR_INTERNAL_IRQS - bit);
- SET_REG(base + REG_GPIO_INTEN, tmp);
- }
- void gpio_irq_disable(rt_uint32_t irq)
- {
- rt_uint32_t tmp, base, bit;
- base = irq_to_base(irq);
- bit = irq_to_bit(irq);
- tmp = GET_REG(base + REG_GPIO_INTEN);
- tmp &= ~BIT((irq - NR_INTERNAL_IRQS - bit));
- SET_REG(base + REG_GPIO_INTEN, tmp);
- }
- static void fh_gpio_interrupt(int irq, void *param)
- {
- rt_uint32_t irq_status;
- int gpio_num, gpio;
- rt_uint32_t base;
- struct fh_gpio_obj *gpio_obj = (struct fh_gpio_obj *)param;
- //rt_kprintf("fh_gpio_interrupt start\n");
- //fixme: spin lock???
- base = (irq==40) ? GPIO0_REG_BASE : GPIO1_REG_BASE;
- irq_status = GET_REG(base + REG_GPIO_INTSTATUS);
- if (irq_status == 0) {
- rt_kprintf("gpio irq status is zero.\n");
- return;
- }
- /* temporarily mask (level sensitive) parent IRQ */
- gpio_irq_mask(irq);
- gpio_num = __rt_ffs(irq_status) - 1;
- SET_REG(base + REG_GPIO_PORTA_EOI, BIT(gpio_num));
- gpio = gpio_num + ((irq==40) ? 0 : 32);
- //generic_handle_irq(gpio_to_irq(gpio));
- if(irq_desc[gpio_to_irq(gpio)].handler)
- irq_desc[gpio_to_irq(gpio)].handler(gpio_to_irq(gpio), irq_desc[gpio_to_irq(gpio)].param);
- gpio_irq_mask(irq);
- /* now it may re-trigger */
- }
- int gpio_direction_input(rt_uint32_t gpio)
- {
- rt_uint32_t reg, base;
- if(gpio > NUM_OF_GPIO)
- {
- rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
- return -RT_ERROR;
- }
- if(!gpio_available[gpio])
- {
- rt_kprintf("ERROR: %s, GPIO %d is not available\n", __func__, gpio);
- return -RT_EBUSY;
- }
- base = gpio_to_base(gpio);
- gpio = gpio % 32;
- //fixme: lock
- //spin_lock_irqsave(&chip->lock, flags);
- reg = GET_REG(base + REG_GPIO_SWPORTA_DDR);
- reg &= ~(1 << gpio);
- SET_REG(base + REG_GPIO_SWPORTA_DDR, reg);
- //spin_unlock_irqrestore(&chip->lock, flags);
- return 0;
- }
- int gpio_direction_output(rt_uint32_t gpio, rt_uint32_t val)
- {
- rt_uint32_t reg, base;
- if(gpio > NUM_OF_GPIO)
- {
- rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
- return -RT_ERROR;
- }
- if(!gpio_available[gpio])
- {
- rt_kprintf("ERROR: %s, GPIO %d is not available\n", __func__, gpio);
- return -RT_EBUSY;
- }
- base = gpio_to_base(gpio);
- gpio = gpio % 32;
- //fixme: lock
- //spin_lock_irqsave(&chip->lock, flags);
- reg = GET_REG(base + REG_GPIO_SWPORTA_DDR);
- reg |= (1 << gpio);
- SET_REG(base + REG_GPIO_SWPORTA_DDR, reg);
- reg = GET_REG(base + REG_GPIO_SWPORTA_DR);
- if(val)
- reg |= (1 << gpio);
- else
- reg &= ~(1 << gpio);
- SET_REG(base + REG_GPIO_SWPORTA_DR, reg);
- //spin_unlock_irqrestore(&chip->lock, flags);
- return 0;
- }
- int gpio_request(rt_uint32_t gpio)
- {
- if(gpio > NUM_OF_GPIO)
- {
- rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
- return -RT_ERROR;
- }
- gpio_available[gpio] = 1;
- return 0;
- }
- int gpio_release(rt_uint32_t gpio)
- {
- if(gpio > NUM_OF_GPIO)
- {
- rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
- return -RT_ERROR;
- }
- gpio_available[gpio] = 0;
- return 0;
- }
- int fh_gpio_probe(void *priv_data)
- {
- struct fh_gpio_obj *gpio_obj = (struct fh_gpio_obj *)priv_data;
- int i;
- if(gpio_obj->id == 0){
- rt_hw_interrupt_install(gpio_obj->irq, fh_gpio_interrupt, (void *)gpio_obj, "gpio_0");
- }
- else if(gpio_obj->id == 1){
- rt_hw_interrupt_install(gpio_obj->irq, fh_gpio_interrupt, (void *)gpio_obj, "gpio_1");
- }
- rt_hw_interrupt_umask(gpio_obj->irq);
- for(i=0; i<32; i++)
- {
- irq_desc[NR_INTERNAL_IRQS + 32 * gpio_obj->id + i].param = gpio_obj;
- }
- return 0;
- }
- int fh_gpio_exit(void *priv_data)
- {
- return 0;
- }
- struct fh_board_ops gpio_driver_ops =
- {
- .probe = fh_gpio_probe,
- .exit = fh_gpio_exit,
- };
- void rt_hw_gpio_init(void)
- {
- PRINT_GPIO_DBG("%s start\n", __func__);
- rt_memset(gpio_available, 0, sizeof(int) * NUM_OF_GPIO);
- fh_board_driver_register("gpio", &gpio_driver_ops);
- PRINT_GPIO_DBG("%s end\n", __func__);
- }
|