/*
 *               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 defines internal utility functions used by the SD/SDIO
 *
 */


#ifndef __SDI_UTILS_H__
#define __SDI_UTILS_H__

/*   */
extern u8 READ_HOST_U8(u32 base, u16 reg);
extern void WRITE_HOST_U8(u32 base, u16 reg, u8 val);
extern u16 READ_HOST_U16(u32 base, u16 reg);
extern void WRITE_HOST_U16(u32 base, u16 reg, u16 val);

#define READ_HOST_U32(base,reg)         IO_READ(base+((u16)reg))
#define WRITE_HOST_U32(base, reg, val)  IO_WRITE(base+((u16)reg),val)

/*
 *  Description:
 *      Error logging primitives
 *
 */
#define SD_DEBUG_CRIT             0x01
#define SD_DEBUG_ERROR            0x02
#define SD_DEBUG_WARN             0x04
#define SD_DEBUG_INFO             0x08
#define SD_DEBUG_NOTIFY           0x10
#define SD_DEBUG_DEBUG            0x20
#define SD_DEBUG_TRACE            0x40
#define SD_DEBUG_NONE             0x80  /* for timing interfering prints */

extern u32 SD_DEBUG_PLEVEL;


extern char* SD_IOCTL_STR[];

/* SD_DEBUG_THRdisable debug code - see makefile */
/*
#define SD_DEBUG_LOG
#define SD_DEBUG_THR
*/

#ifdef SD_DEBUG_LOG

    #include <ioslibc.h>

    #define SDPrint(l, s, args...)                                  \
        do {                                                        \
            if (l & SD_DEBUG_PLEVEL) {                              \
                /* The ## remove the ',' when there's no args */    \
                printf("SDI: " s, ## args);                                 \
            }                                                       \
        } while (0)
        
    #define SD_ENTRY()                                              \
        do {                                                        \
            if (SD_DEBUG_PLEVEL & SD_DEBUG_TRACE) {                 \
                printf("%s: enter >>> %s(), line %d\n",             \
                        __FILE__, __FUNCTION__, __LINE__);          \
            }                                                       \
        } while (0)
        
    #define SD_EXIT()                                               \
        do {                                                        \
            if (SD_DEBUG_PLEVEL & SD_DEBUG_TRACE) {                 \
                printf("%s: leave <<< %s(), line %d\n",             \
                        __FILE__, __FUNCTION__, __LINE__);          \
            }                                                       \
        } while (0)

#else

    #define SDPrint(l, s, ...)        (void)(0)
    #define SD_ENTRY()                (void)(0)
    #define SD_EXIT()                 (void)(0)

#endif                  /*  Enable file system logging */

/* Sleep for +/-1us units */
void sduUsleep(u32 usecs);

/* Sleep for 1ms units */
void sduMsleep(u32 msecs);

/* Debug Print Slots HC Registers */
void sduDumpSlotRegs(u32 slot_addr);

/* Run Debug Thread */
void sdiDebugInit(u32 arg);

/* Timeout message after usecs */
void sduTimeout(u32 slot, u32 usecs, IOSMessageQueueId mqs);

#endif /* __SDI_UTILS_H__ */
