Index: trunk/hal/tsar_mips32/drivers/soclib_sdc.c
===================================================================
--- trunk/hal/tsar_mips32/drivers/soclib_sdc.c	(revision 550)
+++ trunk/hal/tsar_mips32/drivers/soclib_sdc.c	(revision 558)
@@ -19,4 +19,6 @@
 #define SDCARD_RESET_ITER_MAX 4
 
+extern   chdev_directory_t    chdev_dir;  // allocated in the kernel_init.c file.
+
 ///////////////////////////////////////////////////////////////////////////////
 //   Global variables
@@ -24,5 +26,5 @@
 
 __attribute__((section(".kdata")))
-static struct sdcard_dev sdcard;
+static struct sdcard_dev global_sdcard;
 
 __attribute__((section(".kdata")))
@@ -32,7 +34,7 @@
 // This function enables SD Card select signal
 ///////////////////////////////////////////////////////////////////////////////
-static void _sdc_enable()
-{
-    spi_ss_assert(sdcard.spi, sdcard.slave_id);
+static void _sdc_enable(sdcard_dev_t *sdcard)
+{
+    spi_ss_assert(sdcard->spi, sdcard->slave_id);
 }
 
@@ -40,7 +42,7 @@
 // This function disables SD Card select signal
 ///////////////////////////////////////////////////////////////////////////////
-static void _sdc_disable()
-{
-    spi_ss_deassert(sdcard.spi, sdcard.slave_id);
+static void _sdc_disable(sdcard_dev_t *sdcard)
+{
+    spi_ss_deassert(sdcard->spi, sdcard->slave_id);
 }
 
@@ -49,8 +51,8 @@
 // - tick_count: number of ticks to generate (1 tick -> 8 clocks) 
 ///////////////////////////////////////////////////////////////////////////////
-static void _sdc_gen_tick(unsigned int tick_count)
+static void _sdc_gen_tick(sdcard_dev_t *sdcard, unsigned int tick_count)
 {
     register int i = 0;
-    while(i++ < tick_count) spi_put_tx(sdcard.spi, 0xFF, 0);
+    while(i++ < tick_count) spi_put_tx(sdcard->spi, 0xFF, 0);
 }
 
@@ -60,7 +62,7 @@
 // - lba: number of logical block to move the pointer
 ///////////////////////////////////////////////////////////////////////////////
-void _sdc_lseek(unsigned int lba)
-{
-    sdcard.access_pointer = sdcard.block_length * lba;
+void _sdc_lseek(sdcard_dev_t *sdcard, unsigned int lba)
+{
+    sdcard->access_pointer = sdcard->block_length * lba;
 }
 
@@ -68,9 +70,9 @@
 // This function gets a byte from the SD card
 ///////////////////////////////////////////////////////////////////////////////
-static unsigned char _sdc_receive_char()
-{
-    _sdc_gen_tick(1);
-
-    return spi_get_rx(sdcard.spi, 0);
+static unsigned char _sdc_receive_char(sdcard_dev_t *sdcard)
+{
+    _sdc_gen_tick(sdcard, 1);
+
+    return spi_get_rx(sdcard->spi, 0);
 }
 
@@ -80,5 +82,5 @@
 // Returns the SD card response value
 ///////////////////////////////////////////////////////////////////////////////
-static unsigned char _sdc_wait_response()
+static unsigned char _sdc_wait_response(sdcard_dev_t *sdcard)
 {
     unsigned char sdcard_rsp;
@@ -86,5 +88,5 @@
 
     iter       = 0;
-    sdcard_rsp = _sdc_receive_char();
+    sdcard_rsp = _sdc_receive_char(sdcard);
     while (
             (iter < SDCARD_COMMAND_TIMEOUT) &&
@@ -92,5 +94,5 @@
           )
     {
-        sdcard_rsp = _sdc_receive_char();
+        sdcard_rsp = _sdc_receive_char(sdcard);
         iter++;
     }
@@ -104,7 +106,7 @@
 // It must be called after a read command
 ///////////////////////////////////////////////////////////////////////////////
-static void _sdc_wait_data_block()
-{
-	while (_sdc_receive_char() != 0xFE);
+static void _sdc_wait_data_block(sdcard_dev_t *sdcard)
+{
+	while (_sdc_receive_char(sdcard) != 0xFE);
 }
 
@@ -116,5 +118,6 @@
 // - crc7: CRC (7 bits) to send
 ///////////////////////////////////////////////////////////////////////////////
-static int _sdc_send_command ( int      index,
+static int _sdc_send_command ( sdcard_dev_t *sdcard,
+                               int      index,
                                int      app  ,
                                void *   args ,
@@ -124,16 +127,16 @@
     unsigned char * _args;
 
-    _sdc_gen_tick(5);  
+    _sdc_gen_tick(sdcard, 5);  
 
     if (app == SDCARD_ACMD)
     {
-        spi_put_tx(sdcard.spi, 0x40 | 55         , 0 );// CMD and START bit
-        spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[0]
-        spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[1]
-        spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[2]
-        spi_put_tx(sdcard.spi, 0x00              , 0 );// Argument[3]
-        spi_put_tx(sdcard.spi, 0x01 | (crc7 << 1), 0 );// END bit
-
-        sdcard_rsp = _sdc_wait_response();
+        spi_put_tx(sdcard->spi, 0x40 | 55         , 0 );// CMD and START bit
+        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[0]
+        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[1]
+        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[2]
+        spi_put_tx(sdcard->spi, 0x00              , 0 );// Argument[3]
+        spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 );// END bit
+
+        sdcard_rsp = _sdc_wait_response(sdcard);
         if (SDCARD_CHECK_R1_ERROR(sdcard_rsp))
         {
@@ -144,14 +147,14 @@
     _args = (unsigned char *) args;
 
-    _sdc_gen_tick(1);  
-
-    spi_put_tx(sdcard.spi, 0x40 | index      , 0 );
-    spi_put_tx(sdcard.spi, _args[0]          , 0 );
-    spi_put_tx(sdcard.spi, _args[1]          , 0 );
-    spi_put_tx(sdcard.spi, _args[2]          , 0 );
-    spi_put_tx(sdcard.spi, _args[3]          , 0 );
-    spi_put_tx(sdcard.spi, 0x01 | (crc7 << 1), 0 );
-
-    return _sdc_wait_response();
+    _sdc_gen_tick(sdcard, 1);  
+
+    spi_put_tx(sdcard->spi, 0x40 | index      , 0 );
+    spi_put_tx(sdcard->spi, _args[0]          , 0 );
+    spi_put_tx(sdcard->spi, _args[1]          , 0 );
+    spi_put_tx(sdcard->spi, _args[2]          , 0 );
+    spi_put_tx(sdcard->spi, _args[3]          , 0 );
+    spi_put_tx(sdcard->spi, 0x01 | (crc7 << 1), 0 );
+
+    return _sdc_wait_response(sdcard);
 }
 
@@ -161,5 +164,5 @@
 // Returns 0 if success, other value if failure
 ///////////////////////////////////////////////////////////////////////////////
-static int _sdc_open( unsigned int channel )
+static int _sdc_open( sdcard_dev_t *sdcard, xptr_t base, unsigned int channel )
 {
 	unsigned char args[4];
@@ -167,14 +170,14 @@
 	unsigned int  iter, ersp;
 
-	sdcard.spi      = spi;
-	sdcard.slave_id = channel;
+	sdcard->spi      = base;
+	sdcard->slave_id = channel;
 
 	// supply SD card ramp up time (min 74 cycles)
-	_sdc_gen_tick(10);
+	_sdc_gen_tick(sdcard, 10);
 
 	// Assert slave select signal
 	// Send CMD0 (Reset Command)
 	// Deassert slave select signal
-	_sdc_enable();
+	_sdc_enable(sdcard);
 
 	args[0] = 0;
@@ -182,5 +185,5 @@
 	args[2] = 0;
 	args[3] = 0;
-	sdcard_rsp = _sdc_send_command(0, SDCARD_CMD, args, 0x4A);
+	sdcard_rsp = _sdc_send_command(sdcard, 0, SDCARD_CMD, args, 0x4A);
 
 	if ( sdcard_rsp != 0x01 )
@@ -190,14 +193,14 @@
 	}
 
-	_sdc_disable();
+	_sdc_disable(sdcard);
 
 	// send CMD8. If card is pre-v2, It will reply with illegal command.
 	// Otherwise we announce sdhc support.
-	_sdc_enable();
+	_sdc_enable(sdcard);
 	args[0] = 0;
 	args[1] = 0;
 	args[2] = 0x01;
 	args[3] = 0x01;
-	sdcard_rsp = _sdc_send_command(8, SDCARD_CMD, args, 0x63);
+	sdcard_rsp = _sdc_send_command(sdcard, 8, SDCARD_CMD, args, 0x63);
 	if (!SDCARD_CHECK_R1_VALID(sdcard_rsp))
     {
@@ -208,8 +211,8 @@
     {
 		// no error, command accepted. get whole reply
-		ersp = _sdc_receive_char();
-		ersp = (ersp << 8) | _sdc_receive_char();
-		ersp = (ersp << 8) | _sdc_receive_char();
-		ersp = (ersp << 8) | _sdc_receive_char();
+		ersp = _sdc_receive_char(sdcard);
+		ersp = (ersp << 8) | _sdc_receive_char(sdcard);
+		ersp = (ersp << 8) | _sdc_receive_char(sdcard);
+		ersp = (ersp << 8) | _sdc_receive_char(sdcard);
 		if ((ersp & 0xffff) != 0x0101) 
         {
@@ -219,5 +222,5 @@
 		}
 		printk("[SDC WARNING] v2 or later ");
-		sdcard.sdhc = 1;
+		sdcard->sdhc = 1;
 	}
     else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0)
@@ -229,11 +232,11 @@
     else
     {
-		sdcard.sdhc = 0;
-	}
-	_sdc_disable();
+		sdcard->sdhc = 0;
+	}
+	_sdc_disable(sdcard);
 
 	// send CMD41, enabling the card
-	_sdc_enable();
-	args[0] = sdcard.sdhc ? 0x40: 0;
+	_sdc_enable(sdcard);
+	args[0] = sdcard->sdhc ? 0x40: 0;
 	args[1] = 0;
 	args[2] = 0;
@@ -243,5 +246,5 @@
 	while( iter++ < SDCARD_COMMAND_TIMEOUT )
 	{
-		sdcard_rsp = _sdc_send_command(41, SDCARD_ACMD, args, 0x00);
+		sdcard_rsp = _sdc_send_command(sdcard, 41, SDCARD_ACMD, args, 0x00);
 		if( sdcard_rsp == 0x01 )
 		{
@@ -252,5 +255,5 @@
 	}
 
-	_sdc_disable();
+	_sdc_disable(sdcard);
 	if (sdcard_rsp)
     {
@@ -258,13 +261,13 @@
 		return sdcard_rsp;
 	}
-	if (sdcard.sdhc != 0)
+	if (sdcard->sdhc != 0)
     {
 		// get the card capacity to see if it's really HC
-		_sdc_enable();
-		args[0] = sdcard.sdhc ? 0x40: 0;
+		_sdc_enable(sdcard);
+		args[0] = sdcard->sdhc ? 0x40: 0;
 		args[1] = 0;
 		args[2] = 0;
 		args[3] = 0;
-        sdcard_rsp = _sdc_send_command(58, SDCARD_CMD, args, 0x00);
+        sdcard_rsp = _sdc_send_command(sdcard, 58, SDCARD_CMD, args, 0x00);
 		if (sdcard_rsp)
         {
@@ -272,8 +275,8 @@
 			return sdcard_rsp;
 		}
-		ersp = _sdc_receive_char();
-		ersp = (ersp << 8) | _sdc_receive_char();
-		ersp = (ersp << 8) | _sdc_receive_char();
-		ersp = (ersp << 8) | _sdc_receive_char();
+		ersp = _sdc_receive_char(sdcard);
+		ersp = (ersp << 8) | _sdc_receive_char(sdcard);
+		ersp = (ersp << 8) | _sdc_receive_char(sdcard);
+		ersp = (ersp << 8) | _sdc_receive_char(sdcard);
 		if (ersp & 0x40000000)
         {
@@ -282,7 +285,7 @@
         else
         {
-			sdcard.sdhc = 0;
+			sdcard->sdhc = 0;
 		}
-		_sdc_disable();
+		_sdc_disable(sdcard);
 	}
 	printk("card detected\n");
@@ -295,5 +298,5 @@
 // Returns 0 if success, other value if failure
 ///////////////////////////////////////////////////////////////////////////////
-static unsigned int _sdc_set_block_size(unsigned int len)
+static unsigned int _sdc_set_block_size(sdcard_dev_t *sdcard, unsigned int len)
 {
     unsigned char args[4];
@@ -306,7 +309,7 @@
     // When using high capacity SDCARD, the block_length is not a number of bytes
     // but a number of blocks (transfer unit)
-    if (sdcard.sdhc)
-    {
-        sdcard.block_length = len / 512;
+    if (sdcard->sdhc)
+    {
+        sdcard->block_length = len / 512;
         return 0;
     }
@@ -317,16 +320,16 @@
     }
 
-    _sdc_enable();
-
-    sdcard_rsp = _sdc_send_command(16, SDCARD_CMD, args, 0x00);
+    _sdc_enable(sdcard);
+
+    sdcard_rsp = _sdc_send_command(sdcard, 16, SDCARD_CMD, args, 0x00);
     if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) )
     {
-        _sdc_disable();
+        _sdc_disable(sdcard);
         return sdcard_rsp;
     }
 
-    _sdc_disable();
-
-    sdcard.block_length = len;
+    _sdc_disable(sdcard);
+
+    sdcard->block_length = len;
 
 	return 0;
@@ -340,9 +343,7 @@
 void soclib_sdc_init( chdev_t* chdev )
 {
-    spi = (struct spi_dev*)SEG_IOC_BASE;
-
     // initializing the SPI controller
     _spi_init (
-        spi           ,
+        chdev->base   ,
         200000        , /**< SPI_clk: 200 Khz */
         SYSCLK_FREQ   , /**< Sys_clk          */
@@ -361,5 +362,5 @@
         printk("[SDC WARNING] Trying to initialize SD card...\n");
 
-        sdcard_rsp = _sdc_open( 0 );  // only channel 0
+        sdcard_rsp = _sdc_open(&global_sdcard, chdev->base, 0 );  // only channel 0
         if (sdcard_rsp == 0)
         {
@@ -379,5 +380,5 @@
 
     // set the block length of the SD Card
-    sdcard_rsp = _sdc_set_block_size(512);
+    sdcard_rsp = _sdc_set_block_size(&global_sdcard, 512);
     if (sdcard_rsp)
     {
@@ -387,5 +388,5 @@
     // incrementing SDCARD clock frequency for normal function
     _spi_init (
-        spi         ,
+        chdev->base ,
         10000000    , // SPI_clk 10 Mhz
         SYSCLK_FREQ , // Sys_clk
@@ -429,33 +430,33 @@
         for ( ; curr < last ; curr++ )
         {
-            _sdc_lseek(curr);
+            _sdc_lseek(&global_sdcard, curr);
 
             for (i = 0; i < 4; i++)
             {
-                args[i] = (sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF;
+                args[i] = (global_sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF;
             }
 
-            _sdc_enable();
-
-            sdcard_rsp = _sdc_send_command(17, SDCARD_CMD, args, 0x00);
+            _sdc_enable(&global_sdcard);
+
+            sdcard_rsp = _sdc_send_command(&global_sdcard, 17, SDCARD_CMD, args, 0x00);
             if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) )
             {
-                _sdc_disable();
+                _sdc_disable(&global_sdcard);
                 return sdcard_rsp;
             }
 
-            _sdc_wait_data_block();
-
-            if (spi_get_data(sdcard.spi, buf_xp, 512 ))
+            _sdc_wait_data_block(&global_sdcard);
+
+            if (spi_get_data(global_sdcard.spi, buf_xp, 512 ))
             {
-                _sdc_disable();
+                _sdc_disable(&global_sdcard);
                 return 1;
             }
 
             // Get the CRC16 (comes at the end of the data block)
-            _sdc_receive_char(); // first byte
-            _sdc_receive_char(); // second byte
-
-            _sdc_disable();
+            _sdc_receive_char(&global_sdcard); // first byte
+            _sdc_receive_char(&global_sdcard); // second byte
+
+            _sdc_disable(&global_sdcard);
 
             buf_xp += 512;
