summaryrefslogtreecommitdiff
path: root/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c')
-rw-r--r--firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c96
1 files changed, 24 insertions, 72 deletions
diff --git a/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c b/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
index 0e74444cf3..3ddbff0510 100644
--- a/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
+++ b/firmware/target/mips/ingenic_jz47xx/ata-sd-jz4740.c
@@ -22,32 +22,23 @@
22#include "config.h" 22#include "config.h"
23#include "gcc_extensions.h" 23#include "gcc_extensions.h"
24#include "jz4740.h" 24#include "jz4740.h"
25#include "ata.h"
26#include "ata_idle_notify.h"
27#include "ata-sd-target.h" 25#include "ata-sd-target.h"
28#include "disk.h"
29#include "led.h" 26#include "led.h"
30#include "sdmmc.h" 27#include "sdmmc.h"
31#include "logf.h" 28#include "logf.h"
32#include "sd.h"
33#include "system.h"
34#include "kernel.h"
35#include "storage.h" 29#include "storage.h"
36#include "string.h" 30#include "string.h"
37#include "usb.h"
38 31
39static long last_disk_activity = -1; 32static long last_disk_activity = -1;
40#if defined(CONFIG_STORAGE_MULTI) || defined(HAVE_HOTSWAP) 33#if defined(CONFIG_STORAGE_MULTI) || defined(HAVE_HOTSWAP)
41static int sd_drive_nr = 0; 34static int sd_drive_nr = 0;
35#else
36#define sd_drive_nr 0
42#endif 37#endif
43static tCardInfo card; 38static tCardInfo card;
44 39
45static long sd_stack[(DEFAULT_STACK_SIZE*2 + 0x1c0)/sizeof(long)];
46static const char sd_thread_name[] = "ata/sd";
47static struct event_queue sd_queue;
48static struct mutex sd_mtx; 40static struct mutex sd_mtx;
49static struct semaphore sd_wakeup; 41static struct semaphore sd_wakeup;
50static void sd_thread(void) NORETURN_ATTR;
51 42
52static int use_4bit; 43static int use_4bit;
53static int num_6; 44static int num_6;
@@ -1229,11 +1220,6 @@ int sd_init(void)
1229 { 1220 {
1230 semaphore_init(&sd_wakeup, 1, 0); 1221 semaphore_init(&sd_wakeup, 1, 0);
1231 mutex_init(&sd_mtx); 1222 mutex_init(&sd_mtx);
1232 queue_init(&sd_queue, true);
1233 create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
1234 sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE)
1235 IF_COP(, CPU));
1236
1237 inited = true; 1223 inited = true;
1238 } 1224 }
1239 1225
@@ -1265,7 +1251,7 @@ static inline void sd_stop_transfer(void)
1265 mutex_unlock(&sd_mtx); 1251 mutex_unlock(&sd_mtx);
1266} 1252}
1267 1253
1268int sd_read_sectors(IF_MV(int drive,) unsigned long start, int count, void* buf) 1254int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count, void* buf)
1269{ 1255{
1270#ifdef HAVE_MULTIVOLUME 1256#ifdef HAVE_MULTIVOLUME
1271 (void)drive; 1257 (void)drive;
@@ -1404,7 +1390,7 @@ int sd_soft_reset(void)
1404} 1390}
1405 1391
1406#ifdef HAVE_HOTSWAP 1392#ifdef HAVE_HOTSWAP
1407bool sd_removable(IF_MV_NONVOID(int drive)) 1393bool sd_removable(IF_MD_NONVOID(int drive))
1408{ 1394{
1409#ifdef HAVE_MULTIVOLUME 1395#ifdef HAVE_MULTIVOLUME
1410 (void)drive; 1396 (void)drive;
@@ -1414,19 +1400,16 @@ bool sd_removable(IF_MV_NONVOID(int drive))
1414 1400
1415static int sd_oneshot_callback(struct timeout *tmo) 1401static int sd_oneshot_callback(struct timeout *tmo)
1416{ 1402{
1417 (void)tmo;
1418 int state = card_detect_target(); 1403 int state = card_detect_target();
1419 1404
1420 /* This is called only if the state was stable for 300ms - check state 1405 /* This is called only if the state was stable for 300ms - check state
1421 * and post appropriate event. */ 1406 * and post appropriate event. */
1422 if (state) 1407 queue_broadcast(state ? SYS_HOTSWAP_INSERTED : SYS_HOTSWAP_EXTRACTED,
1423 queue_broadcast(SYS_HOTSWAP_INSERTED, 0); 1408 sd_drive_nr);
1424 else
1425 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
1426 1409
1427 sd_gpio_setup_irq(state); 1410 sd_gpio_setup_irq(state);
1428
1429 return 0; 1411 return 0;
1412 (void)tmo;
1430} 1413}
1431 1414
1432/* called on insertion/removal interrupt */ 1415/* called on insertion/removal interrupt */
@@ -1453,58 +1436,27 @@ int sd_num_drives(int first_drive)
1453} 1436}
1454#endif 1437#endif
1455 1438
1456static void sd_thread(void) 1439int sd_event(long id, intptr_t data)
1457{ 1440{
1458 struct queue_event ev; 1441 int rc = 0;
1459 bool idle_notified = false;
1460 1442
1461 while (1) 1443 switch (id)
1462 { 1444 {
1463 queue_wait_w_tmo(&sd_queue, &ev, HZ);
1464
1465 switch (ev.id)
1466 {
1467#ifdef HAVE_HOTSWAP 1445#ifdef HAVE_HOTSWAP
1468 case SYS_HOTSWAP_INSERTED: 1446 case SYS_HOTSWAP_INSERTED:
1469 case SYS_HOTSWAP_EXTRACTED:; 1447 case SYS_HOTSWAP_EXTRACTED:
1470 int success = 1; 1448 mutex_lock(&sd_mtx); /* lock-out card activity */
1471 1449 /* Force card init for new card, re-init for re-inserted one or
1472 disk_unmount(sd_drive_nr); /* release "by force" */ 1450 * clear if the last attempt to init failed with an error. */
1473 1451 card.initialized = 0;
1474 mutex_lock(&sd_mtx); /* lock-out card activity */ 1452 mutex_unlock(&sd_mtx);
1475 1453 break;
1476 /* Force card init for new card, re-init for re-inserted one or
1477 * clear if the last attempt to init failed with an error. */
1478 card.initialized = 0;
1479
1480 mutex_unlock(&sd_mtx);
1481
1482 if(ev.id == SYS_HOTSWAP_INSERTED)
1483 success = disk_mount(sd_drive_nr); /* 0 if fail */
1484
1485 if(success)
1486 queue_broadcast(SYS_FS_CHANGED, 0);
1487
1488 break;
1489#endif /* HAVE_HOTSWAP */ 1454#endif /* HAVE_HOTSWAP */
1490 1455 default:
1491 case SYS_TIMEOUT: 1456 rc = storage_event_default_handler(id, data, last_disk_activity,
1492 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 1457 STORAGE_SD);
1493 idle_notified = false; 1458 break;
1494 else
1495 {
1496 if (!idle_notified)
1497 {
1498 call_storage_idle_notifys(false);
1499 idle_notified = true;
1500 }
1501 }
1502 break;
1503 case SYS_USB_CONNECTED:
1504 usb_acknowledge(SYS_USB_CONNECTED_ACK);
1505 /* Wait until the USB cable is extracted again */
1506 usb_wait_for_disconnect(&sd_queue);
1507 break;
1508 }
1509 } 1459 }
1460
1461 return rc;
1510} 1462}