/*
 *               Copyright (C) 2006, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#ifndef __SDI_H__
#define __SDI_H__

#include <iostypes.h>

#define SD_IOWHCREG         1
#define SD_IORHCREG         2
#define SD_IORCREG          3
#define SD_RESET            4
#define SD_IOWCREG          5
#define SD_SETCLK           6
#define SD_CMD              7 
#define SD_SET_BUS_WIDTH    8 
#define SD_IORMCREG         9 
#define SD_IOWMCREG         10
#define SD_GET_STATUS       11
#define SD_GET_OCR          12
#define SD_READ_DATA        13
#define SD_WRITE_DATA       14


#define SD_CMD0             0       /* Reset */
#define SD_CMD2             2
#define SD_CMD3             3
#define SD_CMD8             8       /* Initialize - Ver 2.0 */
#define SD_CMD12            12
#define SD_CMD17            17      /* Read */
#define SD_CMD18            18      /* Multi-block Read */
#define SD_CMD24            24      /* Write */
#define SD_CMD25            25      /* Multi-block Write */
#define SD_CMD38            38      /* Erase */
#define SD_ACMD41           41
#define SD_CMD51            51      /* Get SCR */
#define SD_CMD55            55


/* SD card related command error codes */
typedef enum SDStatusCode
{
        SD_STATUS_SUCCESS                         = 0x00000000L,
        SD_STATUS_PENDING                         = 0x00000001L,
        SD_STATUS_BUFFER_OVERFLOW                 = 0xC0000001L,
        SD_STATUS_DEVICE_BUSY                     = 0xC0000002L,
        SD_STATUS_UNSUCCESSFUL                    = 0xC0000003L,
        SD_STATUS_NOT_IMPLEMENTED                 = 0xC0000004L,
        SD_STATUS_ACCESS_VIOLATION                = 0xC0000005L,
        SD_STATUS_INVALID_HANDLE                  = 0xC0000006L,
        SD_STATUS_INVALID_PARAMETER               = 0xC0000007L,
        SD_STATUS_NO_SUCH_DEVICE                  = 0xC0000008L,
        SD_STATUS_INVALID_DEVICE_REQUEST          = 0xC0000009L,
        SD_STATUS_NO_MEMORY                       = 0xC000000AL,
        SD_STATUS_BUS_DRIVER_NOT_READY            = 0xC000000BL,
        SD_STATUS_DATA_ERROR                      = 0xC000000CL,
        SD_STATUS_CRC_ERROR                       = 0xC000000DL,
        SD_STATUS_INSUFFICIENT_RESOURCES          = 0xC000000EL,
        SD_STATUS_DEVICE_NOT_CONNECTED            = 0xC0000010L,
        SD_STATUS_DEVICE_REMOVED                  = 0xC0000011L,
        SD_STATUS_DEVICE_NOT_RESPONDING           = 0xC0000012L,
        SD_STATUS_CANCELED                        = 0xC0000013L,
        SD_STATUS_RESPONSE_TIMEOUT                = 0xC0000014L,
        SD_STATUS_DATA_TIMEOUT                    = 0xC0000015L,
        SD_STATUS_DEVICE_RESPONSE_ERROR           = 0xC0000016L,
        SD_STATUS_DEVICE_UNSUPPORTED              = 0xC0000017L,
        SD_STATUS_SHUT_DOWN                       = 0xC0000018L,
        SD_STATUS_OUT_OF_RANGE                    = 0xC0000019L,
        SD_STATUS_FUNCTION_NUMBER_ERR             = 0xC0000020L,
        SD_STATUS_ILLEGAL_CMD                     = 0xC0000021L,
        SD_STATUS_CMD_TIMEOUT                     = 0xC0000022L,
        SD_STATUS_UNKNOWN_ERR                     = 0xC0000023L
} SD_API_STATUS;

/*
 * Status bits for device status query
 */
#define SD_CARD_INSERTED   0x00000001
#define SD_CARD_REMOVED    0x00000002
#define SD_CARD_WPROTECTED 0x00000004
#define SD_STATE_SUSPEND   0x00000008
#define SD_STATE_BUSY      0x00000010

#define SD_TYPE_UNKNOWN    0x00000000
#define SD_TYPE_MEMORY     0x00010000
#define SD_TYPE_SDIO       0x00020000
#define SD_TYPE_COMBO      0x00040000
#define SD_TYPE_MMC        0x00080000
#define SD_TYPE_HC         0x00100000

/*
 * enum for response types used in command() calls
 */
typedef enum  _SDIResponse
{
    NoResponse  = 0,
    ResponseR1  = 1,
    ResponseR1b = 2,
    ResponseR2  = 3,
    ResponseR3  = 4,
    ResponseR4  = 5,
    ResponseR5  = 6,
    ResponseR6  = 7
} SDIResponse;

#define SDI_RESP_REGS 4

/* Regiter call configuration */
typedef struct _SDIReg {
    u32 reg;        /* Register Address */
    u32 blk_size;   /* Block Size       */
    u32 blk_num;    /* Number of Blocks */
    u32 width;
    u32 value;      /* writing register */
    u32 dma;        
} SDIReg;

/*
 * SDIcmd
 * 
 * Command Type Structure, used extensively by the SD card driver.
 * 
 * command:         command number 1..63 to execute.
 * commmand type:   read write.
 * response type:   see SDIResponse.
 * req_size:        for block copy.
 * 
 * Structure used to call the following function:
 * 
 * do_command (handle , int cmd, int cmd_type, 
 *           int resp_type, int req_size, int cmd_arg, int sys_addr,
 *           int blk_count, int blk_size, int dma, )
 */
 
typedef struct _SDCmd {
    u32 command;    /* command number 1..63 to execute. */
    u32 cmd_type;   /* command types */
    u32 resp_type;
    u32 cmd_arg;
    u32 blk_count;
    u32 blk_size;    
    u32 sys_addr;   /* User Buffer Address */
    u32 dma;        /* Use DMA, else memory copy */
    u32 offset;     /* SD Card Address */    
} SDCmd;

/*
 * command types
 */
#define SD_CMD_NONE         0
#define SD_READ             1
#define SD_WRITE            2
#define SD_COMMAND          3

/*
 * Command codes from the SDIO Specification 1.10.
 */
#define SDIO_CMD52          52
#define SDI0_CMD53          53

/* 
 * error codes 
 */
#define SD_ERROR_OK         0               /* Success */
#define SD_ERROR_FAIL       0xC1000000      /* General SD Error code - to be expanded */
#define SD_ERROR_SWTIMEOUT  0xC1000001      /* Software Reset (host controller) timeout */
#define SD_ERROR_IRQ        0xC1000002      /* Unspecified IRQ Error */
#define SD_ERROR_TIMEOUT    0xC1000003      /* Command Timed Out */
#define SD_ERROR_REMOVE     0xC1000004      /* Card removed while data transfer busy */
#define SD_ERROR_BUSY       0xC1000005      /* SD Card busy with command */
#define SD_ERROR_HC         0xC1000020      /* SD Card is SDHC card */

#define SD_ERROR_EVENT      0xC2000000      /* Event Register while another has been registered */
#define SD_ERROR_EVENT_CLR  0xC2100000      /* Event Cleared */

#endif /* __SDI_H__ */

/* eof */
