From 1654efc31339972d0e6bd41a499fcffc0a45822e Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Wed, 15 Mar 2017 01:51:54 -0400 Subject: Unify storage threads into one * Editing a bunch of drivers' thread routines in order to implement a new feature is tedious. * No matter the number of storage drivers, they share one thread. No extra threads needed for CONFIG_STORAGE_MULTI. * Each has an event callback called by the storage thread. * A default callback is provided to fake sleeping in order to trigger idle callbacks. It could also do other default processing. Changes to it will be part of driver code without editing each one. * Drivers may sleep and wake as they please as long as they give a low pulse on their storage bit to ask to go into sleep mode. Idle callback is called on its behalf and driver immediately put into sleep mode. * Drivers may indicate they are to continue receiving events in USB mode, otherwise they receve nothing until disconnect (they do receive SYS_USB_DISCONNECTED no matter what). * Rework a few things to keep the callback implementation sane and maintainable. ata.c was dreadful with all those bools; make it a state machine and easier to follow. Remove last_user_activity; it has no purpose that isn't served by keeping the disk active through last_disk_activity instead. * Even-out stack sizes partly because of a lack of a decent place to define them by driver or SoC or whatever; it doesn't seem too critical to do that anyway. Many are simply too large while at least one isn't really adequate. They may be individually overridden if necessary (figure out where). The thread uses the greatest size demanded. Newer file code is much more frugal with stack space. I barely see use crack 50% after idle callbacks (usually mid-40s). Card insert/eject doesn't demand much. * No forcing of idle callbacks. If it isn't necessary for one or more non-disk storage types, it really isn't any more necessary for disk storage. Besides, it makes the whole thing easier to implement. Change-Id: Id30c284d82a8af66e47f2cfe104c52cbd8aa7215 --- firmware/target/sh/archos/ondio/ata_mmc.c | 139 ++++++++++-------------------- 1 file changed, 45 insertions(+), 94 deletions(-) (limited to 'firmware/target/sh/archos/ondio/ata_mmc.c') diff --git a/firmware/target/sh/archos/ondio/ata_mmc.c b/firmware/target/sh/archos/ondio/ata_mmc.c index 5d95a0e789..f252e1c4ce 100644 --- a/firmware/target/sh/archos/ondio/ata_mmc.c +++ b/firmware/target/sh/archos/ondio/ata_mmc.c @@ -18,27 +18,29 @@ * KIND, either express or implied. * ****************************************************************************/ -#include -#include "mmc.h" +#include "config.h" #include "ata_mmc.h" #include "sdmmc.h" -#include "ata_idle_notify.h" #include "kernel.h" -#include "thread.h" #include "led.h" #include "sh7034.h" #include "system.h" #include "debug.h" #include "panic.h" -#include "usb.h" #include "power.h" #include "string.h" #include "hwcompat.h" #include "adc.h" #include "bitswap.h" -#include "disk.h" /* for mount/unmount */ #include "storage.h" + +#ifdef HAVE_MULTIDRIVE +#define MMC_NUM_DRIVES 2 +#else +#define MMC_NUM_DRIVES 1 +#endif + #define BLOCK_SIZE 512 /* fixed */ /* Command definitions */ @@ -90,15 +92,14 @@ static long last_disk_activity = -1; /* private variables */ -static struct mutex mmc_mutex SHAREDBSS_ATTR; - -#ifdef HAVE_HOTSWAP -static long mmc_stack[((DEFAULT_STACK_SIZE*2) + 0x800)/sizeof(long)]; +#ifdef CONFIG_STORAGE_MULTI +static int mmc_first_drive = 0; #else -static long mmc_stack[(DEFAULT_STACK_SIZE*2)/sizeof(long)]; +#define mmc_first_drive 0 #endif -static const char mmc_thread_name[] = "mmc"; -static struct event_queue mmc_queue SHAREDBSS_ATTR; + +static struct mutex mmc_mutex SHAREDBSS_ATTR; + static bool initialized = false; static bool new_mmc_circuit; @@ -158,6 +159,21 @@ static void mmc_tick(void); /* implementation */ +static void enable_controller(bool on) +{ + PBCR1 &= ~0x0CF0; /* PB13, PB11 and PB10 become GPIO, + * if not modified below */ + if (on) + PBCR1 |= 0x08A0; /* as SCK1, TxD1, RxD1 */ + + and_b(~0x80, &PADRL); /* assert flash reset */ + sleep(HZ/100); + or_b(0x80, &PADRL); /* de-assert flash reset */ + sleep(HZ/100); + card_info[0].initialized = false; + card_info[1].initialized = false; +} + void mmc_enable_int_flash_clock(bool on) { /* Internal flash clock is enabled by setting PA12 high with the new @@ -763,51 +779,6 @@ bool mmc_disk_is_active(void) return mutex_test(&mmc_mutex); } -static void mmc_thread(void) -{ - struct queue_event ev; - bool idle_notified = false; - - while (1) { - queue_wait_w_tmo(&mmc_queue, &ev, HZ); - switch ( ev.id ) - { - case SYS_USB_CONNECTED: - usb_acknowledge(SYS_USB_CONNECTED_ACK); - /* Wait until the USB cable is extracted again */ - usb_wait_for_disconnect(&mmc_queue); - break; - -#ifdef HAVE_HOTSWAP - case SYS_HOTSWAP_INSERTED: - disk_mount(1); /* mount MMC */ - queue_broadcast(SYS_FS_CHANGED, 0); - break; - - case SYS_HOTSWAP_EXTRACTED: - disk_unmount(1); /* release "by force" */ - queue_broadcast(SYS_FS_CHANGED, 0); - break; -#endif - - default: - if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) - { - idle_notified = false; - } - else - { - if (!idle_notified) - { - call_storage_idle_notifys(false); - idle_notified = true; - } - } - break; - } - } -} - bool mmc_detect(void) { return (adc_read(ADC_MMC_SWITCH) < 0x200); @@ -868,11 +839,11 @@ static void mmc_tick(void) { if (current_status) { - queue_broadcast(SYS_HOTSWAP_INSERTED, 0); + queue_broadcast(SYS_HOTSWAP_INSERTED, mmc_first_drive + 1); } else { - queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0); + queue_broadcast(SYS_HOTSWAP_EXTRACTED, mmc_first_drive + 1); mmc_status = MMC_UNTOUCHED; card_info[1].initialized = false; } @@ -882,17 +853,9 @@ static void mmc_tick(void) void mmc_enable(bool on) { - PBCR1 &= ~0x0CF0; /* PB13, PB11 and PB10 become GPIO, - * if not modified below */ - if (on) - PBCR1 |= 0x08A0; /* as SCK1, TxD1, RxD1 */ - - and_b(~0x80, &PADRL); /* assert flash reset */ - sleep(HZ/100); - or_b(0x80, &PADRL); /* de-assert flash reset */ - sleep(HZ/100); - card_info[0].initialized = false; - card_info[1].initialized = false; + mutex_lock(&mmc_mutex); + enable_controller(on); + mutex_unlock(&mmc_mutex); } int mmc_init(void) @@ -900,10 +863,8 @@ int mmc_init(void) int rc = 0; if (!initialized) - { mutex_init(&mmc_mutex); - queue_init(&mmc_queue, true); - } + mutex_lock(&mmc_mutex); led(false); @@ -933,15 +894,10 @@ int mmc_init(void) IPRE &= 0x0FFF; /* disable SCI1 interrupts for the CPU */ new_mmc_circuit = ((HW_MASK & MMC_CLOCK_POLARITY) != 0); - - create_thread(mmc_thread, mmc_stack, - sizeof(mmc_stack), 0, mmc_thread_name - IF_PRIO(, PRIORITY_SYSTEM) - IF_COP(, CPU)); tick_add_task(mmc_tick); initialized = true; } - mmc_enable(true); + enable_controller(true); mutex_unlock(&mmc_mutex); return rc; @@ -998,11 +954,6 @@ bool mmc_present(IF_MD_NONVOID(int drive)) } #endif - -void mmc_sleep(void) -{ -} - void mmc_spin(void) { } @@ -1015,13 +966,13 @@ void mmc_spindown(int seconds) #ifdef CONFIG_STORAGE_MULTI int mmc_num_drives(int first_drive) { - /* We don't care which logical drive number(s) we have been assigned */ - (void)first_drive; - -#ifdef HAVE_MULTIDRIVE - return 2; -#else - return 1; -#endif + mmc_first_drive = first_drive; + return MMC_NUM_DRIVES; +} +#endif /* CONFIG_STORAGE_MULTI */ + +int mmc_event(long id, intptr_t data) +{ + return storage_event_default_handler(id, data, last_disk_activity, + STORAGE_MMC); } -#endif -- cgit v1.2.3