/*
 *               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.
 *
 *  Description:
 *      This file declares the SD/SDIO driver operations
 *
 */


#ifndef __SDI_OPS_H__
#define __SDI_OPS_H__



#include <iostypes.h>

/*
 * SDIO R/W Flags, SDIO SPec 1.10 section 5.1
 */
#define SDIO_READ_FLAG  0
#define SDIO_WRITE_FLAG 1

#define SDIO_DO_RAW     1
#define SDIO_NO_RAW     0

#define SDI_EVENT_SD0   0x11223300
#define SDI_EVENT_SD1   0x11223311
#define SDI_TIMER00     0xABBA0000
#define SDI_TIMER01     0xABBA1000
#define SDI_TIMER10     0xABBA2000

#define SDI_SD0_SLOT0   0
#define SDI_SD0_SLOT1   1
#define SDI_SD1_SLOTW   2

#define SDI_HOST_CTL0   0
#define SDI_HOST_CTL1   1

#define SD_BLOCK_SIZE       (512)

#define SDI_MAX_HC         2
#define SDI_MAX_DEV        3

#define SDI_COMMAND_BUSY    0x100

/* max reposne size is u32[4] from HC register response */
#define SDI_MAX_RESPONS     16

/* SD/SDIO Descriptor Context */
typedef struct {
    u32 inUse;      /* Keeps track of some driver initialisation */
    u32 debug;
    u32 hc;         /* Host controller number 0..1 */
    u32 slot;       /* Slot number 0..2 */
    u32 base;       /* Base Address of Slot */
    u32 clock;      /* SD Card clock register value */
    u32 vsup;       /* Supply Voltage */
    u32 event;      /* Reply if event occurs and this is set */
    u32 eventresp;  /* Send Response to Event */
    u32 ocr;        /* Operating Conditions Register - Clock/Volatge */
    u32 cardtype;   /* None/SD/SDIO */
    u32 command;    /* keep track of command pending */
    u32 complete;   /* command completion */
    u32 datacmd;    /* keep track of data command started */
    u32 hostcmd;    /* allow host_commands that do not interfere with commands */
    u32 pending;    /* irq pending */
    u32 read;       /* direction for flushing */
    u32 bufptr;     /* user buffer for flushing */
    u32 bytes;      /* data chache flushing size */
    u32 timerid;    /* this is temp, but looks like timer has to be destroyed */
    u32 sleep;      /* rm was requested to sleep - not a timeout */
    u32 ioctl;      /* keep track of complex ioctls */
    u32 state;      /* certain command require state machine - with single thread */
    u32 arg;        /* keep temp argument for state machine - OCR */
    u32 *response;  /* pointer to response buffer */
    u32 temp[4];    /* response scratchpad */
    u32 perf[4];    /* performance stats */
} SDIContext;

/*
 * Commmand register (0x0e) format
 */
#define CMD_FORMAT(index, type, data_present, index_check, crc_check, resp_type) \
        (((index & 0x3f) << 8) | \
         ((type & 0x3) << 6) | \
         ((data_present & 0x1) << 5) | \
         ((index_check & 0x1) << 4) | \
         ((crc_check & 0x1) << 3) | \
          (resp_type & 0x7))
 
/*
 * SDIO direct I/O commands
 *
 * Data format is as follows:
 *
 *   31   30  28  27   26   25             9   8  7          0
 * +-----+------+-----+---+------------------+---+------------+
 * | R/W | Func | Raw | S | Register Address | S | Write Data |
 * +-----+------+-----+---+------------------+---+------------+
 *    1     3      1    1          17          1        8
 */
#define IO_RW_DIRECT_ARG(Read_Write, Function, Raw, Address, Data) \
        (((Read_Write & 1) << 31) | \
         ((Function & 0x7) << 28) | \
         ((Raw & 1) << 27) | \
         ((0 & 1) << 26) | \
         ((Address & 0x1ffff) << 9) | \
         ((0 & 1) << 8) | \
         (Data & 0xff))

/*
 * SDIO extended I/O commands
 *
 * Data format is as follows:
 *
 *   31   30  28  27   26   25             9   8             0
 * +-----+------+-----+---+------------------+----------------+
 * | R/W | Func | Blk | O | Register Address |   Data Count   |
 * +-----+------+-----+---+------------------+----------------+
 *    1     3      1    1          17          1        8
 */
#define IO_RW_DIRECT_EXT_ARG(Read_Write, Function, Blk, Op, Address, Count) \
        (((Read_Write & 1) << 31) | \
         ((Function & 0x7) << 28) | \
         ((Blk & 1) << 27) | \
         ((Op) << 26) | \
         ((Address & 0x1ffff) << 9) | \
         (Count & 0x1ff))
         
/*
 *  Description:
 *      SD/SDIO driver operations
 *
 */
IOSError sdiOpen(int hc, IOSMessageQueueId qid);
IOSError sdiClose(void);
IOSError sdiInitSlot(u32 ctx);
IOSError sdiReset(u32 ctx, u32* rca);

IOSError sdiClockOn(u32 ctx);
IOSError sdiClockInit(u32 ctx);
IOSError sdiClockSet(u32 ctx, u32 newclk);

IOSError sdiHandleinterrupt(u32 ctx);
IOSError sdiCommand (u32 ctx, u32 cmd, u32 cmd_type, 
            u32 resp_type, u32 req_size, u32 cmd_arg, u32 sys_addr,
            u32 blk_count, u32 blk_size, u32 dma, u32 *resp);
IOSError sdiReadSlotRegister(int ctx, int reg, int width, u32* data);   
IOSError sdiWriteSlotRegister(int ctx, int reg, int width, int data);
IOSError sdiHandleTimeout(u32 msg);
IOSError sdiHandleCache(u32 ctx);

#endif /* __SDI_OPS_H__ */
