1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * 'Standard' SDIO HOST CONTROLLER driver 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Copyright (C) 2020, Broadcom. 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Unless you and Broadcom execute a separate written software license 7*4882a593Smuzhiyun * agreement governing use of this software, this software is licensed to you 8*4882a593Smuzhiyun * under the terms of the GNU General Public License version 2 (the "GPL"), 9*4882a593Smuzhiyun * available at http://www.broadcom.com/licenses/GPLv2.php, with the 10*4882a593Smuzhiyun * following added to such license: 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * As a special exception, the copyright holders of this software give you 13*4882a593Smuzhiyun * permission to link this software with independent modules, and to copy and 14*4882a593Smuzhiyun * distribute the resulting executable under terms of your choice, provided that 15*4882a593Smuzhiyun * you also meet, for each linked independent module, the terms and conditions of 16*4882a593Smuzhiyun * the license of that module. An independent module is a module which is not 17*4882a593Smuzhiyun * derived from this software. The special exception does not apply to any 18*4882a593Smuzhiyun * modifications of the software. 19*4882a593Smuzhiyun * 20*4882a593Smuzhiyun * 21*4882a593Smuzhiyun * <<Broadcom-WL-IPTag/Open:>> 22*4882a593Smuzhiyun * 23*4882a593Smuzhiyun * $Id: bcmsdstd.h 833030 2019-08-02 17:22:42Z jl904071 $ 24*4882a593Smuzhiyun */ 25*4882a593Smuzhiyun #ifndef _BCM_SD_STD_H 26*4882a593Smuzhiyun #define _BCM_SD_STD_H 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun /* global msglevel for debug messages - bitvals come from sdiovar.h */ 29*4882a593Smuzhiyun #ifdef BCMDBG 30*4882a593Smuzhiyun #define sd_err(x) do { if (sd_msglevel & SDH_ERROR_VAL) printf x; } while (0) 31*4882a593Smuzhiyun #define sd_trace(x) do { if (sd_msglevel & SDH_TRACE_VAL) printf x; } while (0) 32*4882a593Smuzhiyun #define sd_info(x) do { if (sd_msglevel & SDH_INFO_VAL) printf x; } while (0) 33*4882a593Smuzhiyun #define sd_debug(x) do { if (sd_msglevel & SDH_DEBUG_VAL) printf x; } while (0) 34*4882a593Smuzhiyun #define sd_data(x) do { if (sd_msglevel & SDH_DATA_VAL) printf x; } while (0) 35*4882a593Smuzhiyun #define sd_ctrl(x) do { if (sd_msglevel & SDH_CTRL_VAL) printf x; } while (0) 36*4882a593Smuzhiyun #define sd_dma(x) do { if (sd_msglevel & SDH_DMA_VAL) printf x; } while (0) 37*4882a593Smuzhiyun #else 38*4882a593Smuzhiyun #define sd_err(x) do { if (sd_msglevel & SDH_ERROR_VAL) printf x; } while (0) 39*4882a593Smuzhiyun #define sd_trace(x) 40*4882a593Smuzhiyun #define sd_info(x) 41*4882a593Smuzhiyun #define sd_debug(x) 42*4882a593Smuzhiyun #define sd_data(x) 43*4882a593Smuzhiyun #define sd_ctrl(x) 44*4882a593Smuzhiyun #define sd_dma(x) 45*4882a593Smuzhiyun #endif /* BCMDBG */ 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun #define sd_sync_dma(sd, read, nbytes) 48*4882a593Smuzhiyun #define sd_init_dma(sd) 49*4882a593Smuzhiyun #define sd_ack_intr(sd) 50*4882a593Smuzhiyun #define sd_wakeup(sd); 51*4882a593Smuzhiyun /* Allocate/init/free per-OS private data */ 52*4882a593Smuzhiyun extern int sdstd_osinit(sdioh_info_t *sd); 53*4882a593Smuzhiyun extern void sdstd_osfree(sdioh_info_t *sd); 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun #ifdef BCMPERFSTATS 56*4882a593Smuzhiyun #define sd_log(x) do { if (sd_msglevel & SDH_LOG_VAL) bcmlog x; } while (0) 57*4882a593Smuzhiyun #else 58*4882a593Smuzhiyun #define sd_log(x) 59*4882a593Smuzhiyun #endif 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun #define SDIOH_ASSERT(exp) \ 62*4882a593Smuzhiyun do { if (!(exp)) \ 63*4882a593Smuzhiyun printf("!!!ASSERT fail: file %s lines %d", __FILE__, __LINE__); \ 64*4882a593Smuzhiyun } while (0) 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun #define BLOCK_SIZE_4318 64 67*4882a593Smuzhiyun #define BLOCK_SIZE_4328 512 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun /* internal return code */ 70*4882a593Smuzhiyun #define SUCCESS 0 71*4882a593Smuzhiyun #define ERROR 1 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun /* private bus modes */ 74*4882a593Smuzhiyun #define SDIOH_MODE_SPI 0 75*4882a593Smuzhiyun #define SDIOH_MODE_SD1 1 76*4882a593Smuzhiyun #define SDIOH_MODE_SD4 2 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun #define MAX_SLOTS 6 /* For PCI: Only 6 BAR entries => 6 slots */ 79*4882a593Smuzhiyun #define SDIOH_REG_WINSZ 0x100 /* Number of registers in Standard Host Controller */ 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun #define SDIOH_TYPE_ARASAN_HDK 1 82*4882a593Smuzhiyun #define SDIOH_TYPE_BCM27XX 2 83*4882a593Smuzhiyun #ifdef BCMINTERNAL 84*4882a593Smuzhiyun #define SDIOH_TYPE_JINVANI_GOLD 3 85*4882a593Smuzhiyun #endif 86*4882a593Smuzhiyun #define SDIOH_TYPE_TI_PCIXX21 4 /* TI PCIxx21 Standard Host Controller */ 87*4882a593Smuzhiyun #define SDIOH_TYPE_RICOH_R5C822 5 /* Ricoh Co Ltd R5C822 SD/SDIO/MMC/MS/MSPro Host Adapter */ 88*4882a593Smuzhiyun #define SDIOH_TYPE_JMICRON 6 /* JMicron Standard SDIO Host Controller */ 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun /* For linux, allow yielding for dongle */ 91*4882a593Smuzhiyun #if defined(linux) && defined(BCMDONGLEHOST) 92*4882a593Smuzhiyun #define BCMSDYIELD 93*4882a593Smuzhiyun #endif 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun /* Expected card status value for CMD7 */ 96*4882a593Smuzhiyun #define SDIOH_CMD7_EXP_STATUS 0x00001E00 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun #define RETRIES_LARGE 100000 99*4882a593Smuzhiyun #ifdef BCMQT 100*4882a593Smuzhiyun extern void sdstd_os_yield(sdioh_info_t *sd); 101*4882a593Smuzhiyun #define RETRIES_SMALL 10000 102*4882a593Smuzhiyun #else 103*4882a593Smuzhiyun #define sdstd_os_yield(sd) do {} while (0) 104*4882a593Smuzhiyun #define RETRIES_SMALL 100 105*4882a593Smuzhiyun #endif 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun #define USE_BLOCKMODE 0x2 /* Block mode can be single block or multi */ 108*4882a593Smuzhiyun #define USE_MULTIBLOCK 0x4 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun #define USE_FIFO 0x8 /* Fifo vs non-fifo */ 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun #define CLIENT_INTR 0x100 /* Get rid of this! */ 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun #define HC_INTR_RETUNING 0x1000 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun #ifdef BCMSDIOH_TXGLOM 117*4882a593Smuzhiyun /* Total glom pkt can not exceed 64K 118*4882a593Smuzhiyun * need one more slot for glom padding packet 119*4882a593Smuzhiyun */ 120*4882a593Smuzhiyun #define SDIOH_MAXGLOM_SIZE (40+1) 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun typedef struct glom_buf { 123*4882a593Smuzhiyun uint32 count; /* Total number of pkts queued */ 124*4882a593Smuzhiyun void *dma_buf_arr[SDIOH_MAXGLOM_SIZE]; /* Frame address */ 125*4882a593Smuzhiyun dmaaddr_t dma_phys_arr[SDIOH_MAXGLOM_SIZE]; /* DMA_MAPed address of frames */ 126*4882a593Smuzhiyun uint16 nbytes[SDIOH_MAXGLOM_SIZE]; /* Size of each frame */ 127*4882a593Smuzhiyun } glom_buf_t; 128*4882a593Smuzhiyun #endif 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun struct sdioh_info { 131*4882a593Smuzhiyun uint cfg_bar; /* pci cfg address for bar */ 132*4882a593Smuzhiyun uint32 caps; /* cached value of capabilities reg */ 133*4882a593Smuzhiyun uint32 curr_caps; /* max current capabilities reg */ 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun osl_t *osh; /* osh handler */ 136*4882a593Smuzhiyun volatile char *mem_space; /* pci device memory va */ 137*4882a593Smuzhiyun uint lockcount; /* nest count of sdstd_lock() calls */ 138*4882a593Smuzhiyun bool client_intr_enabled; /* interrupt connnected flag */ 139*4882a593Smuzhiyun bool intr_handler_valid; /* client driver interrupt handler valid */ 140*4882a593Smuzhiyun sdioh_cb_fn_t intr_handler; /* registered interrupt handler */ 141*4882a593Smuzhiyun void *intr_handler_arg; /* argument to call interrupt handler */ 142*4882a593Smuzhiyun bool initialized; /* card initialized */ 143*4882a593Smuzhiyun uint target_dev; /* Target device ID */ 144*4882a593Smuzhiyun uint16 intmask; /* Current active interrupts */ 145*4882a593Smuzhiyun void *sdos_info; /* Pointer to per-OS private data */ 146*4882a593Smuzhiyun void *bcmsdh; /* handler to upper layer stack (bcmsdh) */ 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun uint32 controller_type; /* Host controller type */ 149*4882a593Smuzhiyun uint8 version; /* Host Controller Spec Compliance Version */ 150*4882a593Smuzhiyun uint irq; /* Client irq */ 151*4882a593Smuzhiyun int intrcount; /* Client interrupts */ 152*4882a593Smuzhiyun int local_intrcount; /* Controller interrupts */ 153*4882a593Smuzhiyun bool host_init_done; /* Controller initted */ 154*4882a593Smuzhiyun bool card_init_done; /* Client SDIO interface initted */ 155*4882a593Smuzhiyun bool polled_mode; /* polling for command completion */ 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun bool sd_blockmode; /* sd_blockmode == FALSE => 64 Byte Cmd 53s. */ 158*4882a593Smuzhiyun /* Must be on for sd_multiblock to be effective */ 159*4882a593Smuzhiyun bool use_client_ints; /* If this is false, make sure to restore */ 160*4882a593Smuzhiyun /* polling hack in wl_linux.c:wl_timer() */ 161*4882a593Smuzhiyun int adapter_slot; /* Maybe dealing with multiple slots/controllers */ 162*4882a593Smuzhiyun int sd_mode; /* SD1/SD4/SPI */ 163*4882a593Smuzhiyun int client_block_size[SDIOD_MAX_IOFUNCS]; /* Blocksize */ 164*4882a593Smuzhiyun uint32 data_xfer_count; /* Current transfer */ 165*4882a593Smuzhiyun uint16 card_rca; /* Current Address */ 166*4882a593Smuzhiyun int8 sd_dma_mode; /* DMA Mode (PIO, SDMA, ... ADMA2) on CMD53 */ 167*4882a593Smuzhiyun uint8 num_funcs; /* Supported funcs on client */ 168*4882a593Smuzhiyun uint32 com_cis_ptr; 169*4882a593Smuzhiyun uint32 func_cis_ptr[SDIOD_MAX_IOFUNCS]; 170*4882a593Smuzhiyun void *dma_buf; /* DMA Buffer virtual address */ 171*4882a593Smuzhiyun dmaaddr_t dma_phys; /* DMA Buffer physical address */ 172*4882a593Smuzhiyun void *adma2_dscr_buf; /* ADMA2 Descriptor Buffer virtual address */ 173*4882a593Smuzhiyun dmaaddr_t adma2_dscr_phys; /* ADMA2 Descriptor Buffer physical address */ 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* adjustments needed to make the dma align properly */ 176*4882a593Smuzhiyun void *dma_start_buf; 177*4882a593Smuzhiyun dmaaddr_t dma_start_phys; 178*4882a593Smuzhiyun uint alloced_dma_size; 179*4882a593Smuzhiyun void *adma2_dscr_start_buf; 180*4882a593Smuzhiyun dmaaddr_t adma2_dscr_start_phys; 181*4882a593Smuzhiyun uint alloced_adma2_dscr_size; 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun int r_cnt; /* rx count */ 184*4882a593Smuzhiyun int t_cnt; /* tx_count */ 185*4882a593Smuzhiyun bool got_hcint; /* local interrupt flag */ 186*4882a593Smuzhiyun uint16 last_intrstatus; /* to cache intrstatus */ 187*4882a593Smuzhiyun int host_UHSISupported; /* whether UHSI is supported for HC. */ 188*4882a593Smuzhiyun int card_UHSI_voltage_Supported; /* whether UHSI is supported for 189*4882a593Smuzhiyun * Card in terms of Voltage [1.8 or 3.3]. 190*4882a593Smuzhiyun */ 191*4882a593Smuzhiyun int global_UHSI_Supp; /* type of UHSI support in both host and card. 192*4882a593Smuzhiyun * HOST_SDR_UNSUPP: capabilities not supported/matched 193*4882a593Smuzhiyun * HOST_SDR_12_25: SDR12 and SDR25 supported 194*4882a593Smuzhiyun * HOST_SDR_50_104_DDR: one of SDR50/SDR104 or DDR50 supptd 195*4882a593Smuzhiyun */ 196*4882a593Smuzhiyun volatile int sd3_dat_state; /* data transfer state used for retuning check */ 197*4882a593Smuzhiyun volatile int sd3_tun_state; /* tuning state used for retuning check */ 198*4882a593Smuzhiyun bool sd3_tuning_reqd; /* tuning requirement parameter */ 199*4882a593Smuzhiyun bool sd3_tuning_disable; /* tuning disable due to bus sleeping */ 200*4882a593Smuzhiyun uint32 caps3; /* cached value of 32 MSbits capabilities reg (SDIO 3.0) */ 201*4882a593Smuzhiyun #ifdef BCMSDIOH_TXGLOM 202*4882a593Smuzhiyun glom_buf_t glom_info; /* pkt information used for glomming */ 203*4882a593Smuzhiyun uint txglom_mode; /* Txglom mode: 0 - copy, 1 - multi-descriptor */ 204*4882a593Smuzhiyun #endif 205*4882a593Smuzhiyun }; 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun #define DMA_MODE_NONE 0 208*4882a593Smuzhiyun #define DMA_MODE_SDMA 1 209*4882a593Smuzhiyun #define DMA_MODE_ADMA1 2 210*4882a593Smuzhiyun #define DMA_MODE_ADMA2 3 211*4882a593Smuzhiyun #define DMA_MODE_ADMA2_64 4 212*4882a593Smuzhiyun #define DMA_MODE_AUTO -1 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun #define USE_DMA(sd) ((bool)((sd->sd_dma_mode > 0) ? TRUE : FALSE)) 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun /* States for Tuning and corr data */ 217*4882a593Smuzhiyun #define TUNING_IDLE 0 218*4882a593Smuzhiyun #define TUNING_START 1 219*4882a593Smuzhiyun #define TUNING_START_AFTER_DAT 2 220*4882a593Smuzhiyun #define TUNING_ONGOING 3 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun #define DATA_TRANSFER_IDLE 0 223*4882a593Smuzhiyun #define DATA_TRANSFER_ONGOING 1 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun #define CHECK_TUNING_PRE_DATA 1 226*4882a593Smuzhiyun #define CHECK_TUNING_POST_DATA 2 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun #ifdef DHD_DEBUG 229*4882a593Smuzhiyun #define SD_DHD_DISABLE_PERIODIC_TUNING 0x01 230*4882a593Smuzhiyun #define SD_DHD_ENABLE_PERIODIC_TUNING 0x00 231*4882a593Smuzhiyun #endif 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun /************************************************************ 234*4882a593Smuzhiyun * Internal interfaces: per-port references into bcmsdstd.c 235*4882a593Smuzhiyun */ 236*4882a593Smuzhiyun 237*4882a593Smuzhiyun /* Global message bits */ 238*4882a593Smuzhiyun extern uint sd_msglevel; 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun /* OS-independent interrupt handler */ 241*4882a593Smuzhiyun extern bool check_client_intr(sdioh_info_t *sd); 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun /* Core interrupt enable/disable of device interrupts */ 244*4882a593Smuzhiyun extern void sdstd_devintr_on(sdioh_info_t *sd); 245*4882a593Smuzhiyun extern void sdstd_devintr_off(sdioh_info_t *sd); 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun /* Enable/disable interrupts for local controller events */ 248*4882a593Smuzhiyun extern void sdstd_intrs_on(sdioh_info_t *sd, uint16 norm, uint16 err); 249*4882a593Smuzhiyun extern void sdstd_intrs_off(sdioh_info_t *sd, uint16 norm, uint16 err); 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun /* Wait for specified interrupt and error bits to be set */ 252*4882a593Smuzhiyun extern void sdstd_spinbits(sdioh_info_t *sd, uint16 norm, uint16 err); 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun /************************************************************** 255*4882a593Smuzhiyun * Internal interfaces: bcmsdstd.c references to per-port code 256*4882a593Smuzhiyun */ 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun /* Register mapping routines */ 259*4882a593Smuzhiyun extern uint32 *sdstd_reg_map(osl_t *osh, dmaaddr_t addr, int size); 260*4882a593Smuzhiyun extern void sdstd_reg_unmap(osl_t *osh, dmaaddr_t addr, int size); 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun /* Interrupt (de)registration routines */ 263*4882a593Smuzhiyun extern int sdstd_register_irq(sdioh_info_t *sd, uint irq); 264*4882a593Smuzhiyun extern void sdstd_free_irq(uint irq, sdioh_info_t *sd); 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun /* OS-specific interrupt wrappers (atomic interrupt enable/disable) */ 267*4882a593Smuzhiyun extern void sdstd_lock(sdioh_info_t *sd); 268*4882a593Smuzhiyun extern void sdstd_unlock(sdioh_info_t *sd); 269*4882a593Smuzhiyun extern void sdstd_waitlockfree(sdioh_info_t *sd); 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun /* OS-specific wrappers for safe concurrent register access */ 272*4882a593Smuzhiyun extern void sdstd_os_lock_irqsave(sdioh_info_t *sd, ulong* flags); 273*4882a593Smuzhiyun extern void sdstd_os_unlock_irqrestore(sdioh_info_t *sd, ulong* flags); 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun /* OS-specific wait-for-interrupt-or-status */ 276*4882a593Smuzhiyun extern int sdstd_waitbits(sdioh_info_t *sd, uint16 norm, uint16 err, bool yield, uint16 *bits); 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun /* used by bcmsdstd_linux [implemented in sdstd] */ 279*4882a593Smuzhiyun extern void sdstd_3_enable_retuning_int(sdioh_info_t *sd); 280*4882a593Smuzhiyun extern void sdstd_3_disable_retuning_int(sdioh_info_t *sd); 281*4882a593Smuzhiyun extern bool sdstd_3_is_retuning_int_set(sdioh_info_t *sd); 282*4882a593Smuzhiyun extern void sdstd_3_check_and_do_tuning(sdioh_info_t *sd, int tuning_param); 283*4882a593Smuzhiyun extern bool sdstd_3_check_and_set_retuning(sdioh_info_t *sd); 284*4882a593Smuzhiyun extern int sdstd_3_get_tune_state(sdioh_info_t *sd); 285*4882a593Smuzhiyun extern int sdstd_3_get_data_state(sdioh_info_t *sd); 286*4882a593Smuzhiyun extern void sdstd_3_set_tune_state(sdioh_info_t *sd, int state); 287*4882a593Smuzhiyun extern void sdstd_3_set_data_state(sdioh_info_t *sd, int state); 288*4882a593Smuzhiyun extern uint8 sdstd_3_get_tuning_exp(sdioh_info_t *sd); 289*4882a593Smuzhiyun extern uint32 sdstd_3_get_uhsi_clkmode(sdioh_info_t *sd); 290*4882a593Smuzhiyun extern int sdstd_3_clk_tuning(sdioh_info_t *sd, uint32 sd3ClkMode); 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun /* used by sdstd [implemented in bcmsdstd_linux/ndis] */ 293*4882a593Smuzhiyun extern void sdstd_3_start_tuning(sdioh_info_t *sd); 294*4882a593Smuzhiyun extern void sdstd_3_osinit_tuning(sdioh_info_t *sd); 295*4882a593Smuzhiyun extern void sdstd_3_osclean_tuning(sdioh_info_t *sd); 296*4882a593Smuzhiyun 297*4882a593Smuzhiyun extern void sdstd_enable_disable_periodic_timer(sdioh_info_t * sd, uint val); 298*4882a593Smuzhiyun 299*4882a593Smuzhiyun extern sdioh_info_t *sdioh_attach(osl_t *osh, void *bar0, uint irq); 300*4882a593Smuzhiyun extern SDIOH_API_RC sdioh_detach(osl_t *osh, sdioh_info_t *sd); 301*4882a593Smuzhiyun #endif /* _BCM_SD_STD_H */ 302