From a9e20df6873c9bb3cb72365b5b4fdf40e243ea66 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 17 Oct 2022 15:05:51 +0200 Subject: [PATCH 1/2] for kernel v 5.15.61 --- platform/plat_raspberrypi.h | 68 ------------------- platform/plat_spi.h | 43 +++++------- rfm12b.c | 127 ++++++++++++++++++++++++++++-------- 3 files changed, 116 insertions(+), 122 deletions(-) diff --git a/platform/plat_raspberrypi.h b/platform/plat_raspberrypi.h index 092a04a..f35835c 100644 --- a/platform/plat_raspberrypi.h +++ b/platform/plat_raspberrypi.h @@ -70,79 +70,11 @@ struct spi_rfm12_board_config board_configs[NUM_RFM12_BOARDS] = { } }; -static int -spi_rfm12_init_pinmux_settings(void); -static int -spi_rfm12_init_irq_pin_settings(rfm12_module_type_t module_type); static int spi_rfm12_cleanup_pinmux_settings(void); static int spi_rfm12_cleanup_irq_pin_settings(rfm12_module_type_t module_type); -static int -spi_rfm12_init_pinmux_settings(void) -{ -// taken from https://github.com/bootc/linux/blob/rpi-i2cspi/drivers/spi/spi-bcm2708.c - -#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) -#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) -#define GPIO_PULL *(gpio+37) -#define GPIO_PULLCLK0 *(gpio+38) - - int pin; - u32* gpio = ioremap(GPIO_MEM_BASE, SZ_16K); - - // SPI0 is on gpio 7..11 - for (pin = 7; pin <= 11; pin++) { - INP_GPIO(pin); - SET_GPIO_ALT(pin, 0); - } - - iounmap(gpio); - - return 0; -} - -static int -spi_rfm12_init_irq_pin_settings(rfm12_module_type_t module_type) -{ - u8 pull_mode = 0; - u32* gpio = NULL; - - switch (module_type) { - case RFM12_TYPE_RF12: - pull_mode = 0x2; // pullup - break; - case RFM12_TYPE_RF69: - default: - pull_mode = 0; // no pull - break; - } - - gpio = ioremap(GPIO_MEM_BASE, SZ_16K); - - if (NULL != gpio) { - INP_GPIO(25); - SET_GPIO_ALT(25, 0); - - GPIO_PULL = pull_mode; - udelay(500); - GPIO_PULLCLK0 = (1 << 25); - udelay(500); - GPIO_PULL = 0; - GPIO_PULLCLK0 = 0; - - iounmap(gpio); - } - - return 0; -} - -#undef INP_GPIO -#undef SET_GPIO_ALT -#undef GPIO_PULL -#undef GPIO_PULLCLK0 - static int spi_rfm12_cleanup_pinmux_settings(void) { diff --git a/platform/plat_spi.h b/platform/plat_spi.h index 247bafc..1a95427 100644 --- a/platform/plat_spi.h +++ b/platform/plat_spi.h @@ -32,10 +32,6 @@ #include #include -#if !defined(IRQF_DISABLED) -#define IRQF_DISABLED 0 -#endif - struct spi_rfm12_active_board { u16 irq; void* irq_data; @@ -53,8 +49,6 @@ struct spi_rfm12_active_board { static struct spi_rfm12_active_board active_boards[NUM_RFM12_BOARDS]; -static int -spi_rfm12_init_pinmux_settings(void); static int spi_rfm12_cleanup_pinmux_settings(void); @@ -100,7 +94,6 @@ platform_irq_handled(void* identifier) rfmXX_handle_interrupt((struct rfm12_data*)brd->irq_data); else { brd->state.irq_enabled = 1; - enable_irq(brd->irq); } } @@ -113,20 +106,23 @@ spi_rfm12_setup_irq_pins(void) int err, i; for (i=0; istate.irq_claimed) return -EBUSY; - err = spi_rfm12_init_irq_pin_settings(module_type); - - if (err) { - return err; - } - brd->state.irq_pinmuxed = 1; brd->module_type = module_type; @@ -261,7 +248,7 @@ platform_irq_init(void* identifier, rfm12_module_type_t module_type, err = request_any_context_irq( brd->irq, spi_rfm12_irq_handler, - irq_trigger | IRQF_DISABLED, + irq_trigger, RFM12B_DRV_NAME, (void*)brd ); @@ -424,7 +411,7 @@ spi_rfm12_deregister_spi_devices(void) for (i=0; ispi_speed_hz }; @@ -469,6 +484,10 @@ rfm_send_generic_async_cmd(struct rfm12_data* rfm12, uint16_t* cmds, { int err, i; struct rfm12_spi_message* spi_msg; + struct spi_delay spi_dly = { + .value = delay_usecs, + .unit = 0 + }; spi_msg = rfm_claim_spi_message(rfm12); @@ -487,10 +506,11 @@ rfm_send_generic_async_cmd(struct rfm12_data* rfm12, uint16_t* cmds, rfm_control_spi_transfer(rfm12, spi_msg, 0, cmds[0]); i=1; + if (num_cmds > 1) { for (i=1; ispi_transfers[i-1].cs_change = 1; - spi_msg->spi_transfers[i-1].delay_usecs = delay_usecs; + spi_msg->spi_transfers[i-1].delay = spi_dly; spi_message_add_tail(&spi_msg->spi_transfers[i-1], &spi_msg->spi_msg); spi_msg->spi_transfers[i] = @@ -726,13 +746,13 @@ rfm_begin_sending_or_receiving(struct rfm12_data* rfm12) } } -static void -rfm_rxtx_watchdog_expired(unsigned long ptr) +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) +static void rfm_rxtx_watchdog_expired(unsigned long ptr) { unsigned long flags; struct rfm12_data* rfm12 = (struct rfm12_data*)ptr; - spin_lock_irqsave(&rfm12->lock, flags); + spin_lock_irqsave(&rfm12->lock, flags); if (RFM12_STATE_RECV >= rfm12->state && RFM12_STATE_LISTEN <= rfm12->state) { @@ -748,24 +768,58 @@ rfm_rxtx_watchdog_expired(unsigned long ptr) spin_unlock_irqrestore(&rfm12->lock, flags); } +#else +static void rfm_rxtx_watchdog_expired(struct timer_list *t) +{ + unsigned long flags; + //struct rfm12_data* rfm12 = from_timer + struct rfm12_data* rfm12 = from_timer(rfm12, t, rxtx_watchdog); + + spin_lock_irqsave(&rfm12->lock, flags); + + if (RFM12_STATE_RECV >= rfm12->state && + RFM12_STATE_LISTEN <= rfm12->state) { + rfm12->num_recv_timeouts++; + (void)rfm_finish_receiving(rfm12, 1); + } else if (RFM12_STATE_SEND_PRE1 <= rfm12->state && + RFM12_STATE_SEND_TAIL3 >= rfm12->state) { + rfm12->num_send_timeouts++; + (void)rfm_finish_sending(rfm12, 0); + } + + rfm12->rxtx_watchdog_running = 0; + + spin_unlock_irqrestore(&rfm12->lock, flags); +} +#endif static void rfm_update_rxtx_watchdog(struct rfm12_data* rfm12, u8 cancelTimer) { - if (rfm12->rxtx_watchdog_running) { - if (cancelTimer) { + if (rfm12->rxtx_watchdog_running) + { + if (cancelTimer) + { // not del_timer_sync, because we might be called within our // own expiration handler del_timer(&rfm12->rxtx_watchdog); rfm12->rxtx_watchdog_running = 0; - } else - mod_timer(&rfm12->rxtx_watchdog, - jiffies + RXTX_WATCHDOG_JIFFIES); - } else if (!cancelTimer) { + } + else + mod_timer(&rfm12->rxtx_watchdog, jiffies + RXTX_WATCHDOG_JIFFIES); + } + else if (!cancelTimer) + { +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) init_timer(&rfm12->rxtx_watchdog); - rfm12->rxtx_watchdog.expires = jiffies + RXTX_WATCHDOG_JIFFIES; - rfm12->rxtx_watchdog.data = (unsigned long)rfm12; rfm12->rxtx_watchdog.function = rfm_rxtx_watchdog_expired; +#else + timer_setup(&rfm12->rxtx_watchdog, rfm_rxtx_watchdog_expired, 0); +#endif + + rfm12->rxtx_watchdog.expires = jiffies + RXTX_WATCHDOG_JIFFIES; + //rfm12->rxtx_watchdog.data = (unsigned long)rfm12; + add_timer(&rfm12->rxtx_watchdog); rfm12->rxtx_watchdog_running = 1; } @@ -805,10 +859,9 @@ static void rfm_finish_send_or_recv_callback(void *arg) { unsigned long flags; - struct rfm12_spi_message* spi_msg = - (struct rfm12_spi_message*)arg; - struct rfm12_data* rfm12 = - (struct rfm12_data*)spi_msg->context; + struct rfm12_spi_message* spi_msg = (struct rfm12_spi_message*)arg; + struct rfm12_data* rfm12 = (struct rfm12_data*)spi_msg->context; + u8 should_release = 0; spin_lock_irqsave(&rfm12->lock, flags); @@ -840,16 +893,20 @@ rfm_finish_trysend(struct rfm12_data* rfm12) static void rfm_start_trysend_retry_timer(struct rfm12_data* rfm12) { - init_timer(&rfm12->retry_sending_timer); + #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) + init_timer(&rfm12->retry_sending_timer); + rfm12->retry_sending_timer.function = rfm_trysend_retry_timer_expired; +#else + timer_setup(&rfm12->retry_sending_timer, rfm_trysend_retry_timer_expired, 0); +#endif + rfm12->retry_sending_timer.expires = jiffies + TRYSEND_RETRY_JIFFIES; - rfm12->retry_sending_timer.data = (unsigned long)rfm12; - rfm12->retry_sending_timer.function = rfm_trysend_retry_timer_expired; add_timer(&rfm12->retry_sending_timer); rfm12->retry_sending_running = 1; } -static void -rfm_trysend_retry_timer_expired(unsigned long ptr) +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) +static void rfm_trysend_retry_timer_expired(unsigned long ptr) { unsigned long flags; struct rfm12_data* rfm12 = (struct rfm12_data*)ptr; @@ -865,6 +922,24 @@ rfm_trysend_retry_timer_expired(unsigned long ptr) spin_unlock_irqrestore(&rfm12->lock, flags); } +#else +static void rfm_trysend_retry_timer_expired(struct timer_list *t) +{ + unsigned long flags; + struct rfm12_data* rfm12 = from_timer(rfm12, t, retry_sending_timer); + + spin_lock_irqsave(&rfm12->lock, flags); + + if (rfm12->retry_sending_running) { + rfm_finish_trysend(rfm12); + rfmXX_try_sending(rfm12); + } else { + rfm_finish_trysend(rfm12); + } + + spin_unlock_irqrestore(&rfm12->lock, flags); +} +#endif static int rfm_finish_sending(struct rfm12_data* rfm12, int success) From 646529b3edc17f4e74d86cc36d493b0040ae02e3 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 17 Oct 2022 16:08:30 +0200 Subject: [PATCH 2/2] refinement --- rfm12b_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfm12b_config.h b/rfm12b_config.h index 98a6b5b..918a39b 100644 --- a/rfm12b_config.h +++ b/rfm12b_config.h @@ -36,7 +36,7 @@ Beaglebone Black 3 platform/plat_beaglebone.h Raspberry Pi 2 4 platform/plat_raspberrypi.h */ -#define RFM12B_BOARD 0 +#define RFM12B_BOARD 0 /* The name of the driver within the kernel (e.g. shows up in logs, etc...)