diff --git a/arch/arm/src/rtl8720c/ameba_flash.c b/arch/arm/src/rtl8720c/ameba_flash.c index b49f5b09abf..ac6bd4dfc5e 100644 --- a/arch/arm/src/rtl8720c/ameba_flash.c +++ b/arch/arm/src/rtl8720c/ameba_flash.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -131,6 +132,8 @@ static const struct partition_s ptable[5] = }, }; +static spinlock_t g_lock = SP_UNLOCKED; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -138,7 +141,7 @@ static const struct partition_s ptable[5] = static irqstate_t flash_resource_lock(void) { irqstate_t state; - state = enter_critical_section(); + state = spin_lock_irqsave(&g_lock); icache_disable(); dcache_disable(); return state; @@ -149,7 +152,7 @@ static void flash_resource_unlock(irqstate_t state) dcache_enable(); icache_enable(); icache_invalidate(); - leave_critical_section(state); + spin_unlock_irqrestore(&g_lock, state); } static int ameba_flash_erase(struct mtd_dev_s *dev, diff --git a/arch/arm/src/rtl8720c/ameba_uart.c b/arch/arm/src/rtl8720c/ameba_uart.c index 0833dc22544..ab0a8488ac1 100644 --- a/arch/arm/src/rtl8720c/ameba_uart.c +++ b/arch/arm/src/rtl8720c/ameba_uart.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include #include @@ -107,6 +107,7 @@ struct ameba_s bool flow; /* flow control (RTS/CTS) enabled */ #endif #endif + spinlock_t lock; /* Ensure mutually exclusive access */ }; /**************************************************************************** @@ -213,6 +214,7 @@ static struct ameba_s g_uart0priv = .flow = true, #endif #endif + .lock = SP_UNLOCKED, }; static uart_dev_t g_uart0port = @@ -251,6 +253,7 @@ static struct ameba_s g_uart1priv = .flow = true, #endif #endif + .lock = SP_UNLOCKED, }; static uart_dev_t g_uart1port = @@ -289,6 +292,7 @@ static struct ameba_s g_uart2priv = .flow = true, #endif #endif + .lock = SP_UNLOCKED, }; static uart_dev_t g_uart2port = @@ -327,6 +331,7 @@ static struct ameba_s g_uart3priv = .flow = true, #endif #endif + .lock = SP_UNLOCKED, }; static uart_dev_t g_uart3port = @@ -753,7 +758,7 @@ static int ameba_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); cfsetispeed(termiosp, priv->baud); termiosp->c_cflag = ((priv->parity != 0) ? PARENB : 0) | ((priv->parity == 1) ? PARODD : 0); @@ -778,7 +783,7 @@ static int ameba_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); } break; @@ -792,7 +797,7 @@ static int ameba_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); switch (termiosp->c_cflag & CSIZE) { case CS5: @@ -826,7 +831,7 @@ static int ameba_ioctl(struct file *filep, int cmd, unsigned long arg) priv->flow = (termiosp->c_cflag & CRTSCTS) != 0; #endif ameba_setup(dev); - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); } break; diff --git a/arch/arm/src/rtl8720c/ameba_wdt.c b/arch/arm/src/rtl8720c/ameba_wdt.c index 7c68b3ab9fe..bc584cde58c 100644 --- a/arch/arm/src/rtl8720c/ameba_wdt.c +++ b/arch/arm/src/rtl8720c/ameba_wdt.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -53,6 +54,7 @@ struct ameba_lowerhalf_s bool started; /* true: The watchdog timer has * been started */ + spinlock_t lock; /* Ensure mutually exclusive access */ }; /**************************************************************************** @@ -113,11 +115,11 @@ static int ameba_start(struct watchdog_lowerhalf_s *lower) { struct ameba_lowerhalf_s *priv = (struct ameba_lowerhalf_s *)lower; irqstate_t flags; - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); priv->started = true; priv->lastreset = clock_systime_ticks(); hal_misc_wdt_enable(); - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); return OK; } @@ -140,12 +142,12 @@ static int ameba_stop(struct watchdog_lowerhalf_s *lower) { struct ameba_lowerhalf_s *priv = (struct ameba_lowerhalf_s *)lower; irqstate_t flags; - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); hal_misc_wdt_disable(); priv->started = false; priv->timeout = 0; priv->lastreset = 0; - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); return OK; } @@ -176,10 +178,10 @@ static int ameba_keepalive(struct watchdog_lowerhalf_s *lower) /* Reload the WDT timer */ - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); priv->lastreset = clock_systime_ticks(); hal_misc_wdt_refresh(); - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); return OK; } @@ -254,10 +256,10 @@ static int ameba_settimeout(struct watchdog_lowerhalf_s *lower, { struct ameba_lowerhalf_s *priv = (struct ameba_lowerhalf_s *)lower; irqstate_t flags; - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); priv->timeout = timeout; hal_misc_wdt_init(timeout * 1000); - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); return OK; } @@ -287,6 +289,7 @@ void ameba_wdt_initialize(void) /* Initialize the driver state structure. */ priv->ops = &g_wdgops; + spin_lock_init(&priv->lock); watchdog_register(CONFIG_WATCHDOG_DEVPATH, (struct watchdog_lowerhalf_s *)priv); } diff --git a/arch/arm/src/rtl8720c/amebaz_depend.c b/arch/arm/src/rtl8720c/amebaz_depend.c index 0e257061988..6ba9241ec4f 100644 --- a/arch/arm/src/rtl8720c/amebaz_depend.c +++ b/arch/arm/src/rtl8720c/amebaz_depend.c @@ -28,6 +28,7 @@ #include #include #include +#include #include /**************************************************************************** @@ -52,24 +53,23 @@ int __wrap_printf(const char *fmt, ...) /* stdio.h Wrapper End */ -static int uxcriticalnesting = 0; +static rspinlock_t g_lock = RSPINLOCK_INITIALIZER; +static irqstate_t g_flags = 0; /* Critical Operation Start */ void save_and_cli(void) { - enter_critical_section(); - uxcriticalnesting++; + irqstate_t flags = rspin_lock_irqsave(&g_lock); + if (!rspin_lock_is_recursive(&g_lock)) + { + g_flags = flags; + } } void restore_flags(void) { - ASSERT(uxcriticalnesting); - uxcriticalnesting--; - if (uxcriticalnesting == 0) - { - leave_critical_section(0); - } + rspin_unlock_irqrestore(&g_lock, g_flags); } void rtw_enter_critical(void **plock, unsigned long *pirql) @@ -871,21 +871,14 @@ uint32_t rtw_end_of_queue_search(struct list_head *head, /* Device lock Wrapper Start */ -static uint32_t mutex_init; +static atomic_t mutex_init; static void *device_mutex[5]; static void device_mutex_init(uint32_t device) { irqstate_t status; - if (!(mutex_init & (1 << device))) + if (atomic_fetch_or(&mutex_init, (1 << device)) & (1 << device) == 0) { - status = enter_critical_section(); - if (!(mutex_init & (1 << device))) - { - rtw_mutex_init(&device_mutex[device]); - mutex_init |= (1 << device); - } - - leave_critical_section(status); + rtw_mutex_init(&device_mutex[device]); } } diff --git a/arch/arm/src/rtl8720c/amebaz_driver.h b/arch/arm/src/rtl8720c/amebaz_driver.h index a7ff963941d..79bba355859 100644 --- a/arch/arm/src/rtl8720c/amebaz_driver.h +++ b/arch/arm/src/rtl8720c/amebaz_driver.h @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -86,6 +87,7 @@ struct amebaz_dev_s rtw_scan_result_t scan_data[AMEBAZ_SCAN_AP_COUNT]; unsigned int scan_count; unsigned char country[2]; + mutex_t lock; }; int amebaz_wl_start_scan(struct amebaz_dev_s *priv, diff --git a/arch/arm/src/rtl8720c/amebaz_netdev.c b/arch/arm/src/rtl8720c/amebaz_netdev.c index 9f136f5d90f..3fa58b7e6d5 100644 --- a/arch/arm/src/rtl8720c/amebaz_netdev.c +++ b/arch/arm/src/rtl8720c/amebaz_netdev.c @@ -296,14 +296,13 @@ static int amebaz_ifdown(struct net_driver_s *dev) { int ret = 0; struct amebaz_dev_s *priv = (struct amebaz_dev_s *)dev->d_private; - irqstate_t flags; if (priv->devnum == 0 && rltk_wlan_running(1)) { printf("must ifdown wlan 1 first\r\n"); return ERROR; } - flags = enter_critical_section(); + nxmutex_lock(&priv->lock); if (IFF_IS_UP(dev->d_flags)) { if (priv->curr) @@ -333,7 +332,7 @@ static int amebaz_ifdown(struct net_driver_s *dev) } } - leave_critical_section(flags); + nxmutex_unlock(&priv->lock); return ret; } @@ -343,6 +342,7 @@ int amebaz_netdev_register(struct amebaz_dev_s *priv) dev->d_ifup = amebaz_ifup; dev->d_ifdown = amebaz_ifdown; dev->d_txavail = amebaz_txavail; + nxmutex_init(priv->lock); #ifdef CONFIG_NETDEV_IOCTL dev->d_ioctl = amebaz_ioctl; #endif