2015-07-21 17:12:17 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation.
|
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-07-21 17:12:17 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-27 11:45:31 +01:00
|
|
|
#define DT_DRV_COMPAT snps_designware_gpio
|
|
|
|
|
2016-03-10 15:31:02 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/dt-bindings/gpio/snps-designware-gpio.h>
|
2015-10-15 19:54:35 +02:00
|
|
|
#include "gpio_dw.h"
|
2022-10-21 20:18:01 +02:00
|
|
|
#include <zephyr/drivers/gpio/gpio_utils.h>
|
2016-03-23 12:01:06 +01:00
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/pm/device.h>
|
|
|
|
#include <zephyr/sys/sys_io.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/sys/__assert.h>
|
|
|
|
#include <zephyr/drivers/clock_control.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2015-12-10 18:52:56 +01:00
|
|
|
#ifdef CONFIG_IOAPIC
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/interrupt_controller/ioapic.h>
|
2015-12-10 18:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_port_set_bits_raw(const struct device *port, uint32_t mask);
|
|
|
|
static int gpio_dw_port_clear_bits_raw(const struct device *port,
|
|
|
|
uint32_t mask);
|
2019-10-04 22:56:59 +02:00
|
|
|
|
2015-11-27 17:34:49 +01:00
|
|
|
/*
|
|
|
|
* ARC architecture configure IP through IO auxiliary registers.
|
|
|
|
* Other architectures as ARM and x86 configure IP through MMIO registers
|
|
|
|
*/
|
2016-04-01 19:24:12 +02:00
|
|
|
#ifdef GPIO_DW_IO_ACCESS
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline uint32_t dw_read(uint32_t base_addr, uint32_t offset)
|
2015-11-27 17:34:49 +01:00
|
|
|
{
|
|
|
|
return sys_in32(base_addr + offset);
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline void dw_write(uint32_t base_addr, uint32_t offset,
|
|
|
|
uint32_t val)
|
2015-11-27 17:34:49 +01:00
|
|
|
{
|
|
|
|
sys_out32(val, base_addr + offset);
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static void dw_set_bit(uint32_t base_addr, uint32_t offset,
|
|
|
|
uint32_t bit, bool value)
|
2015-11-27 17:34:49 +01:00
|
|
|
{
|
|
|
|
if (!value) {
|
|
|
|
sys_io_clear_bit(base_addr + offset, bit);
|
|
|
|
} else {
|
|
|
|
sys_io_set_bit(base_addr + offset, bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline uint32_t dw_read(uint32_t base_addr, uint32_t offset)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2015-08-25 16:11:28 +02:00
|
|
|
return sys_read32(base_addr + offset);
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline void dw_write(uint32_t base_addr, uint32_t offset,
|
|
|
|
uint32_t val)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2015-08-25 16:11:28 +02:00
|
|
|
sys_write32(val, base_addr + offset);
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static void dw_set_bit(uint32_t base_addr, uint32_t offset,
|
|
|
|
uint32_t bit, bool value)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2015-08-25 16:11:28 +02:00
|
|
|
if (!value) {
|
|
|
|
sys_clear_bit(base_addr + offset, bit);
|
|
|
|
} else {
|
|
|
|
sys_set_bit(base_addr + offset, bit);
|
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
2015-11-27 17:34:49 +01:00
|
|
|
#endif
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int dw_base_to_block_base(uint32_t base_addr)
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
{
|
|
|
|
return (base_addr & 0xFFFFFFC0);
|
|
|
|
}
|
2019-10-04 22:56:59 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline int dw_derive_port_from_base(uint32_t base_addr)
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t port = (base_addr & 0x3f) / 12U;
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
return port;
|
|
|
|
}
|
2019-10-04 22:56:59 +02:00
|
|
|
|
2016-10-11 00:20:26 +02:00
|
|
|
static inline int dw_interrupt_support(const struct gpio_dw_config *config)
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
{
|
2019-10-04 22:56:59 +02:00
|
|
|
return ((int)(config->irq_num) > 0U);
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline uint32_t dw_get_ext_port(uint32_t base_addr)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ext_port;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
/* 4-port GPIO implementation translates from base address to port */
|
|
|
|
switch (dw_derive_port_from_base(base_addr)) {
|
|
|
|
case 1:
|
|
|
|
ext_port = EXT_PORTB;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ext_port = EXT_PORTC;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ext_port = EXT_PORTD;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
ext_port = EXT_PORTA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext_port;
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
}
|
2015-10-30 19:31:21 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline uint32_t dw_get_data_port(uint32_t base_addr)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t dr_port;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
/* 4-port GPIO implementation translates from base address to port */
|
|
|
|
switch (dw_derive_port_from_base(base_addr)) {
|
|
|
|
case 1:
|
|
|
|
dr_port = SWPORTB_DR;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dr_port = SWPORTC_DR;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
dr_port = SWPORTD_DR;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
dr_port = SWPORTA_DR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dr_port;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static inline uint32_t dw_get_dir_port(uint32_t base_addr)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ddr_port;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
/* 4-port GPIO implementation translates from base address to port */
|
|
|
|
switch (dw_derive_port_from_base(base_addr)) {
|
|
|
|
case 1:
|
|
|
|
ddr_port = SWPORTB_DDR;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ddr_port = SWPORTC_DDR;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ddr_port = SWPORTD_DDR;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
ddr_port = SWPORTA_DDR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ddr_port;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_pin_interrupt_configure(const struct device *port,
|
|
|
|
gpio_pin_t pin,
|
|
|
|
enum gpio_int_mode mode,
|
|
|
|
enum gpio_int_trig trig)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct gpio_dw_config *config = port->config;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t dir_port = dw_get_dir_port(port_base_addr);
|
|
|
|
uint32_t data_port = dw_get_data_port(port_base_addr);
|
|
|
|
uint32_t dir_reg;
|
2016-12-03 01:21:17 +01:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
/* Check for invalid pin number */
|
2021-06-28 08:19:46 +02:00
|
|
|
if (pin >= config->ngpios) {
|
2019-10-04 22:56:59 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
/* Only PORT-A supports interrupts */
|
|
|
|
if (data_port != SWPORTA_DR) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
if (mode != GPIO_INT_MODE_DISABLED) {
|
|
|
|
/* Check if GPIO port supports interrupts */
|
|
|
|
if (!dw_interrupt_support(config)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
/* Interrupt to be enabled but pin is not set to input */
|
2022-09-22 22:03:50 +02:00
|
|
|
dir_reg = dw_read(base_addr, dir_port) & BIT(pin);
|
2019-10-04 22:56:59 +02:00
|
|
|
if (dir_reg != 0U) {
|
|
|
|
return -EINVAL;
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
}
|
2019-10-04 22:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Does not support both edges */
|
|
|
|
if ((mode == GPIO_INT_MODE_EDGE) &&
|
|
|
|
(trig == GPIO_INT_TRIG_BOTH)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
/* Clear interrupt enable */
|
|
|
|
dw_set_bit(base_addr, INTEN, pin, false);
|
|
|
|
|
|
|
|
/* Mask and clear interrupt */
|
|
|
|
dw_set_bit(base_addr, INTMASK, pin, true);
|
|
|
|
dw_write(base_addr, PORTA_EOI, BIT(pin));
|
|
|
|
|
|
|
|
if (mode != GPIO_INT_MODE_DISABLED) {
|
|
|
|
/* level (0) or edge (1) */
|
|
|
|
dw_set_bit(base_addr, INTTYPE_LEVEL, pin,
|
|
|
|
(mode == GPIO_INT_MODE_EDGE));
|
|
|
|
|
|
|
|
/* Active low/high */
|
|
|
|
dw_set_bit(base_addr, INT_POLARITY, pin,
|
|
|
|
(trig == GPIO_INT_TRIG_HIGH));
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
|
|
|
|
/* Finally enabling interrupt */
|
2019-10-04 22:56:59 +02:00
|
|
|
dw_set_bit(base_addr, INTEN, pin, true);
|
|
|
|
dw_set_bit(base_addr, INTMASK, pin, false);
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
}
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline void dw_pin_config(const struct device *port,
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t pin, int flags)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct gpio_dw_config *config = port->config;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t dir_port = dw_get_dir_port(port_base_addr);
|
2019-10-04 22:56:59 +02:00
|
|
|
bool pin_is_output, need_debounce;
|
|
|
|
|
|
|
|
/* Set init value then direction */
|
|
|
|
pin_is_output = (flags & GPIO_OUTPUT) != 0U;
|
2022-09-21 18:10:32 +02:00
|
|
|
|
2022-09-22 22:03:50 +02:00
|
|
|
dw_set_bit(base_addr, dir_port, pin, pin_is_output);
|
2022-09-21 18:10:32 +02:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
if (pin_is_output) {
|
|
|
|
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) {
|
|
|
|
gpio_dw_port_set_bits_raw(port, BIT(pin));
|
|
|
|
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0U) {
|
|
|
|
gpio_dw_port_clear_bits_raw(port, BIT(pin));
|
|
|
|
}
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
/* Use built-in debounce.
|
|
|
|
* Note debounce circuit is only available if also supporting
|
|
|
|
* interrupts according to datasheet.
|
|
|
|
*/
|
|
|
|
if (dw_interrupt_support(config) && (dir_port == SWPORTA_DDR)) {
|
2021-10-27 14:52:31 +02:00
|
|
|
need_debounce = (flags & DW_GPIO_DEBOUNCE);
|
2019-10-04 22:56:59 +02:00
|
|
|
dw_set_bit(base_addr, PORTA_DEBOUNCE, pin, need_debounce);
|
2016-03-23 10:53:04 +01:00
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline int gpio_dw_config(const struct device *port,
|
2020-01-30 19:12:39 +01:00
|
|
|
gpio_pin_t pin,
|
|
|
|
gpio_flags_t flags)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct gpio_dw_config *config = port->config;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t io_flags;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
/* Check for invalid pin number */
|
2021-06-28 08:19:46 +02:00
|
|
|
if (pin >= config->ngpios) {
|
2017-02-07 00:14:20 +01:00
|
|
|
return -EINVAL;
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 22:56:59 +02:00
|
|
|
/* Does not support disconnected pin, and
|
|
|
|
* not supporting both input/output at same time.
|
|
|
|
*/
|
|
|
|
io_flags = flags & (GPIO_INPUT | GPIO_OUTPUT);
|
|
|
|
if ((io_flags == GPIO_DISCONNECTED)
|
|
|
|
|| (io_flags == (GPIO_INPUT | GPIO_OUTPUT))) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No open-drain support */
|
|
|
|
if ((flags & GPIO_SINGLE_ENDED) != 0U) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does not support pull-up/pull-down */
|
|
|
|
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0U) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:31:07 +01:00
|
|
|
dw_pin_config(port, pin, flags);
|
2019-10-04 22:56:59 +02:00
|
|
|
|
2015-07-21 17:12:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_port_get_raw(const struct device *port, uint32_t *value)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t ext_port = dw_get_ext_port(port_base_addr);
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
*value = dw_read(base_addr, ext_port);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_port_set_masked_raw(const struct device *port,
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t mask, uint32_t value)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t data_port = dw_get_data_port(port_base_addr);
|
|
|
|
uint32_t pins;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
pins = dw_read(base_addr, data_port);
|
|
|
|
pins = (pins & ~mask) | (mask & value);
|
|
|
|
dw_write(base_addr, data_port, pins);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_port_set_bits_raw(const struct device *port, uint32_t mask)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t data_port = dw_get_data_port(port_base_addr);
|
|
|
|
uint32_t pins;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
pins = dw_read(base_addr, data_port);
|
|
|
|
pins |= mask;
|
|
|
|
dw_write(base_addr, data_port, pins);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_port_clear_bits_raw(const struct device *port,
|
|
|
|
uint32_t mask)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t data_port = dw_get_data_port(port_base_addr);
|
|
|
|
uint32_t pins;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
pins = dw_read(base_addr, data_port);
|
|
|
|
pins &= ~mask;
|
|
|
|
dw_write(base_addr, data_port, pins);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_port_toggle_bits(const struct device *port, uint32_t mask)
|
2019-10-04 22:56:59 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t port_base_addr = context->base_addr;
|
|
|
|
uint32_t data_port = dw_get_data_port(port_base_addr);
|
|
|
|
uint32_t pins;
|
2019-10-04 22:56:59 +02:00
|
|
|
|
|
|
|
pins = dw_read(base_addr, data_port);
|
|
|
|
pins ^= mask;
|
|
|
|
dw_write(base_addr, data_port, pins);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline int gpio_dw_manage_callback(const struct device *port,
|
2016-03-23 12:01:06 +01:00
|
|
|
struct gpio_callback *callback,
|
|
|
|
bool set)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
return gpio_manage_callback(&context->callbacks, callback, set);
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
|
|
|
|
2024-03-22 03:25:34 +01:00
|
|
|
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
static void gpio_dw_isr(const struct device *port)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr = dw_base_to_block_base(context->base_addr);
|
|
|
|
uint32_t int_status;
|
2015-07-21 17:12:17 +02:00
|
|
|
|
|
|
|
int_status = dw_read(base_addr, INTSTATUS);
|
2015-09-30 01:15:37 +02:00
|
|
|
|
2016-03-30 10:11:11 +02:00
|
|
|
dw_write(base_addr, PORTA_EOI, int_status);
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
gpio_fire_callbacks(&context->callbacks, port, int_status);
|
2015-07-21 17:12:17 +02:00
|
|
|
}
|
2024-03-22 03:25:34 +01:00
|
|
|
#endif /* DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts) */
|
2015-07-21 17:12:17 +02:00
|
|
|
|
2016-10-24 09:21:28 +02:00
|
|
|
static const struct gpio_driver_api api_funcs = {
|
2020-01-30 19:12:39 +01:00
|
|
|
.pin_configure = gpio_dw_config,
|
2019-10-04 22:56:59 +02:00
|
|
|
.port_get_raw = gpio_dw_port_get_raw,
|
|
|
|
.port_set_masked_raw = gpio_dw_port_set_masked_raw,
|
|
|
|
.port_set_bits_raw = gpio_dw_port_set_bits_raw,
|
|
|
|
.port_clear_bits_raw = gpio_dw_port_clear_bits_raw,
|
|
|
|
.port_toggle_bits = gpio_dw_port_toggle_bits,
|
|
|
|
.pin_interrupt_configure = gpio_dw_pin_interrupt_configure,
|
2016-03-23 12:01:06 +01:00
|
|
|
.manage_callback = gpio_dw_manage_callback,
|
2015-07-21 17:12:17 +02:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_dw_initialize(const struct device *port)
|
2015-07-21 17:12:17 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct gpio_dw_runtime *context = port->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct gpio_dw_config *config = port->config;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t base_addr;
|
2015-07-21 17:12:17 +02:00
|
|
|
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
if (dw_interrupt_support(config)) {
|
2015-10-15 23:53:29 +02:00
|
|
|
|
2016-10-11 00:20:26 +02:00
|
|
|
base_addr = dw_base_to_block_base(context->base_addr);
|
2015-12-17 23:08:33 +01:00
|
|
|
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
/* interrupts in sync with system clock */
|
|
|
|
dw_set_bit(base_addr, INT_CLOCK_SYNC, LS_SYNC_POS, 1);
|
2015-12-01 16:00:39 +01:00
|
|
|
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
/* mask and disable interrupts */
|
|
|
|
dw_write(base_addr, INTMASK, ~(0));
|
|
|
|
dw_write(base_addr, INTEN, 0);
|
|
|
|
dw_write(base_addr, PORTA_EOI, ~(0));
|
2015-07-21 17:12:17 +02:00
|
|
|
|
gpio: For ARC EM Starterkit, a 4-port GPIO implementation is being added
The file gpio_dw_registers.h already had the 4-port GPIO
registers defined, yet the gpio_dw.c implementation didn't
support it properly. There are 4 ports here, not 2, and only
PORTA can support interrupts and debounce.
On the em_starterkit board, for example, PORTA
has 3 bits for buttons: A, L and R. The other 3 ports should not
be used with interrupts & debounce.
I've re-worked this file to derive the port number from the
base address given. The lower 6 bits are divided by 12 to
derive the port number. From this, the registers EXT_PORTA,
EXT_PORTB, EXT_PORTC or EXT_PORTD can be read.
Also, for those ports that don't support interrupts,
set irq_num to 0, and that code will be avoided. I've verified
that I can access GPIO now correctly on the EM Starterkit. The
em_starterkit board support will be submitted soon but I'm
staging in this change first.
Change-Id: I98dbe083e03e046b40e07b4b14a99a39a6d0f0be
Signed-off-by: Chuck Jordan <cjordan@synopsys.com>
2016-05-17 06:55:01 +02:00
|
|
|
config->config_func(port);
|
|
|
|
}
|
2015-07-21 17:12:17 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-17 15:32:31 +02:00
|
|
|
|
2022-02-24 13:00:55 +01:00
|
|
|
/* Bindings to the platform */
|
2022-07-13 19:56:35 +02:00
|
|
|
#define INST_IRQ_FLAGS(n) \
|
|
|
|
COND_CODE_1(DT_INST_IRQ_HAS_CELL(n, flags), (DT_INST_IRQ(n, flags)), (0))
|
|
|
|
|
2022-07-18 21:44:36 +02:00
|
|
|
#define GPIO_CFG_IRQ(idx, n) \
|
2024-01-14 13:33:57 +01:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN_BY_IDX(n, idx), \
|
2022-07-13 19:56:35 +02:00
|
|
|
DT_INST_IRQ(n, priority), gpio_dw_isr, \
|
|
|
|
DEVICE_DT_INST_GET(n), INST_IRQ_FLAGS(n)); \
|
2024-01-14 13:33:57 +01:00
|
|
|
irq_enable(DT_INST_IRQN_BY_IDX(n, idx)); \
|
2022-07-13 19:56:35 +02:00
|
|
|
|
|
|
|
#define GPIO_DW_INIT(n) \
|
|
|
|
static void gpio_config_##n##_irq(const struct device *port) \
|
|
|
|
{ \
|
2022-07-18 21:44:36 +02:00
|
|
|
ARG_UNUSED(port); \
|
|
|
|
LISTIFY(DT_NUM_IRQS(DT_DRV_INST(n)), GPIO_CFG_IRQ, (), n) \
|
2022-07-13 19:56:35 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
static const struct gpio_dw_config gpio_dw_config_##n = { \
|
|
|
|
.common = { \
|
|
|
|
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
|
|
|
|
}, \
|
|
|
|
.irq_num = COND_CODE_1(DT_INST_IRQ_HAS_IDX(n, 0), (DT_INST_IRQN(n)), (0)), \
|
|
|
|
.ngpios = DT_INST_PROP(n, ngpios), \
|
|
|
|
.config_func = gpio_config_##n##_irq, \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static struct gpio_dw_runtime gpio_##n##_runtime = { \
|
|
|
|
.base_addr = DT_INST_REG_ADDR(n), \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(n, gpio_dw_initialize, NULL, &gpio_##n##_runtime, \
|
|
|
|
&gpio_dw_config_##n, PRE_KERNEL_1, \
|
|
|
|
CONFIG_GPIO_INIT_PRIORITY, &api_funcs); \
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(GPIO_DW_INIT)
|