#ifndef _SDMMCVAR_H_
#define _SDMMCVAR_H_
#ifdef _KERNEL_OPT
#include "opt_sdmmc.h"
#endif
#include <sys/queue.h>
#include <sys/mutex.h>
#include <sys/callout.h>
#include <sys/evcnt.h>
#include <sys/bus.h>
#include <dev/sdmmc/sdmmcchip.h>
#include <dev/sdmmc/sdmmcreg.h>
#define SDMMC_SECTOR_SIZE_SB 9
#define SDMMC_SECTOR_SIZE (1 << SDMMC_SECTOR_SIZE_SB)
struct sdmmc_csd {
int csdver;
u_int mmcver;
int capacity;
int read_bl_len;
int write_bl_len;
int r2w_factor;
int tran_speed;
int ccc;
};
struct sdmmc_ext_csd {
uint8_t rev;
uint8_t rst_n_function;
uint32_t cache_size;
};
struct sdmmc_cid {
int mid;
int oid;
char pnm[8];
int rev;
int psn;
int mdt;
};
struct sdmmc_scr {
int sd_spec;
int sd_spec3;
int sd_spec4;
int bus_width;
bool support_cmd48;
};
struct sdmmc_ssr {
bool cache;
};
struct sdmmc_ext_regset {
bool valid;
uint8_t fno;
uint32_t start_addr;
};
struct sdmmc_ext_sd {
struct sdmmc_ext_regset pef;
};
typedef uint32_t sdmmc_response[4];
struct sdmmc_softc;
struct sdmmc_task {
void (*func)(void *arg);
void *arg;
int onqueue;
struct sdmmc_softc *sc;
TAILQ_ENTRY(sdmmc_task) next;
};
#define sdmmc_init_task(xtask, xfunc, xarg) \
do { \
(xtask)->func = (xfunc); \
(xtask)->arg = (xarg); \
(xtask)->onqueue = 0; \
(xtask)->sc = NULL; \
} while (0)
struct sdmmc_command {
struct sdmmc_task c_task;
uint16_t c_opcode;
uint32_t c_arg;
sdmmc_response c_resp;
bus_dmamap_t c_dmamap;
void *c_data;
int c_datalen;
int c_blklen;
int c_flags;
#define SCF_ITSDONE (1U << 0)
#define SCF_RSP_PRESENT (1U << 1)
#define SCF_RSP_BSY (1U << 2)
#define SCF_RSP_136 (1U << 3)
#define SCF_RSP_CRC (1U << 4)
#define SCF_RSP_IDX (1U << 5)
#define SCF_CMD_READ (1U << 6)
#define SCF_CMD_AC (0U << 8)
#define SCF_CMD_ADTC (1U << 8)
#define SCF_CMD_BC (2U << 8)
#define SCF_CMD_BCR (3U << 8)
#define SCF_CMD_MASK (3U << 8)
#define SCF_RSP_SPI_S1 (1U << 10)
#define SCF_RSP_SPI_S2 (1U << 11)
#define SCF_RSP_SPI_B4 (1U << 12)
#define SCF_RSP_SPI_BSY (1U << 13)
#define SCF_TOUT_OK (1U << 14)
#define SCF_XFER_SDHC (1U << 15)
#define SCF_POLL (1U << 16)
#define SCF_NEED_BOUNCE (1U << 17)
#define SCF_NO_STOP (1U << 18)
#define SCF_RSP_R0 0
#define SCF_RSP_R1 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
#define SCF_RSP_R1B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
#define SCF_RSP_R2 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_136)
#define SCF_RSP_R3 (SCF_RSP_PRESENT)
#define SCF_RSP_R4 (SCF_RSP_PRESENT)
#define SCF_RSP_R5 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
#define SCF_RSP_R5B (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX|SCF_RSP_BSY)
#define SCF_RSP_R6 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
#define SCF_RSP_R7 (SCF_RSP_PRESENT|SCF_RSP_CRC|SCF_RSP_IDX)
#define SCF_RSP_MASK (0x1f << 1)
#define SCF_RSP_SPI_R1 (SCF_RSP_SPI_S1)
#define SCF_RSP_SPI_R1B (SCF_RSP_SPI_S1|SCF_RSP_SPI_BSY)
#define SCF_RSP_SPI_R2 (SCF_RSP_SPI_S1|SCF_RSP_SPI_S2)
#define SCF_RSP_SPI_R3 (SCF_RSP_SPI_S1|SCF_RSP_SPI_B4)
#define SCF_RSP_SPI_R4 (SCF_RSP_SPI_S1|SCF_RSP_SPI_B4)
#define SCF_RSP_SPI_R5 (SCF_RSP_SPI_S1|SCF_RSP_SPI_S2)
#define SCF_RSP_SPI_R7 (SCF_RSP_SPI_S1|SCF_RSP_SPI_B4)
#define SCF_RSP_SPI_MASK (0xf << 10)
int c_error;
int c_resid;
u_char *c_buf;
};
struct sdmmc_cis {
uint16_t manufacturer;
#define SDMMC_VENDOR_INVALID 0xffff
uint16_t product;
#define SDMMC_PRODUCT_INVALID 0xffff
uint8_t function;
#define SDMMC_FUNCTION_INVALID 0xff
u_char cis1_major;
u_char cis1_minor;
char cis1_info_buf[256];
char *cis1_info[4];
uint8_t lan_nid[6];
};
struct sdmmc_function {
struct sdmmc_softc *sc;
uint16_t rca;
int interface;
int width;
u_int blklen;
int flags;
#define SFF_ERROR 0x0001
#define SFF_SDHC 0x0002
#define SFF_CACHE_ENABLED 0x0004
SIMPLEQ_ENTRY(sdmmc_function) sf_list;
int number;
device_t child;
struct sdmmc_cis cis;
struct sdmmc_csd csd;
struct sdmmc_ext_csd ext_csd;
struct sdmmc_cid cid;
sdmmc_response raw_cid;
uint32_t raw_scr[2];
struct sdmmc_scr scr;
struct sdmmc_ssr ssr;
struct sdmmc_ext_sd ext_sd;
void *bbuf;
bus_dmamap_t bbuf_dmap;
bus_dmamap_t sseg_dmap;
};
struct sdmmc_softc {
device_t sc_dev;
#define SDMMCDEVNAME(sc) (device_xname(sc->sc_dev))
sdmmc_chipset_tag_t sc_sct;
sdmmc_spi_chipset_tag_t sc_spi_sct;
sdmmc_chipset_handle_t sc_sch;
bus_dma_tag_t sc_dmat;
bus_dmamap_t sc_dmap;
#define SDMMC_MAXNSEGS ((MAXPHYS / PAGE_SIZE) + 1)
bus_addr_t sc_dma_align_mask;
struct kmutex sc_mtx;
int sc_dying;
uint32_t sc_flags;
#define SMF_INITED 0x0001
#define SMF_SD_MODE 0x0002
#define SMF_IO_MODE 0x0004
#define SMF_MEM_MODE 0x0008
#define SMF_CARD_PRESENT 0x4000
#define SMF_CARD_ATTACHED 0x8000
#define SMF_UHS_MODE 0x10000
uint32_t sc_caps;
#define SMC_CAPS_AUTO_STOP __BIT(0)
#define SMC_CAPS_4BIT_MODE __BIT(1)
#define SMC_CAPS_DMA __BIT(2)
#define SMC_CAPS_SPI_MODE __BIT(3)
#define SMC_CAPS_POLL_CARD_DET __BIT(4)
#define SMC_CAPS_SINGLE_ONLY __BIT(5)
#define SMC_CAPS_8BIT_MODE __BIT(6)
#define SMC_CAPS_MULTI_SEG_DMA __BIT(7)
#define SMC_CAPS_SD_HIGHSPEED __BIT(8)
#define SMC_CAPS_MMC_HIGHSPEED __BIT(9)
#define SMC_CAPS_MMC_DDR52 __BIT(10)
#define SMC_CAPS_UHS_SDR50 __BIT(12)
#define SMC_CAPS_UHS_SDR104 __BIT(13)
#define SMC_CAPS_UHS_DDR50 __BIT(14)
#define SMC_CAPS_UHS_MASK (SMC_CAPS_UHS_SDR50 \
| SMC_CAPS_UHS_SDR104 \
| SMC_CAPS_UHS_DDR50)
#define SMC_CAPS_MMC_HS200 __BIT(15)
#define SMC_CAPS_POLLING __BIT(30)
int sc_function_count;
struct sdmmc_function *sc_card;
struct sdmmc_function *sc_fn0;
SIMPLEQ_HEAD(, sdmmc_function) sf_head;
struct lwp *sc_tskq_lwp;
TAILQ_HEAD(, sdmmc_task) sc_tskq;
struct kmutex sc_tskq_mtx;
struct kcondvar sc_tskq_cv;
struct sdmmc_task *sc_curtask;
struct sdmmc_task sc_discover_task;
struct kmutex sc_discover_task_mtx;
struct sdmmc_task sc_intr_task;
TAILQ_HEAD(, sdmmc_intr_handler) sc_intrq;
u_int sc_clkmin;
u_int sc_clkmax;
u_int sc_busclk;
bool sc_busddr;
int sc_buswidth;
const char *sc_transfer_mode;
callout_t sc_card_detect_ch;
struct evcnt sc_ev_xfer;
struct evcnt sc_ev_xfer_aligned[8];
struct evcnt sc_ev_xfer_unaligned;
struct evcnt sc_ev_xfer_error;
uint32_t sc_max_seg;
};
struct sdmmc_attach_args {
uint16_t manufacturer;
uint16_t product;
int interface;
struct sdmmc_function *sf;
};
struct sdmmc_product {
uint16_t pp_vendor;
uint16_t pp_product;
const char *pp_cisinfo[4];
};
#ifndef IPL_SDMMC
#define IPL_SDMMC IPL_BIO
#endif
#ifndef splsdmmc
#define splsdmmc() splbio()
#endif
#define SDMMC_LOCK(sc)
#define SDMMC_UNLOCK(sc)
#ifdef SDMMC_DEBUG
extern int sdmmcdebug;
#endif
void sdmmc_add_task(struct sdmmc_softc *, struct sdmmc_task *);
bool sdmmc_del_task(struct sdmmc_softc *, struct sdmmc_task *, kmutex_t *);
struct sdmmc_function *sdmmc_function_alloc(struct sdmmc_softc *);
void sdmmc_function_free(struct sdmmc_function *);
int sdmmc_set_bus_power(struct sdmmc_softc *, uint32_t, uint32_t);
int sdmmc_mmc_command(struct sdmmc_softc *, struct sdmmc_command *);
int sdmmc_app_command(struct sdmmc_softc *, struct sdmmc_function *,
struct sdmmc_command *);
void sdmmc_stop_transmission(struct sdmmc_softc *);
void sdmmc_go_idle_state(struct sdmmc_softc *);
int sdmmc_select_card(struct sdmmc_softc *, struct sdmmc_function *);
int sdmmc_set_relative_addr(struct sdmmc_softc *, struct sdmmc_function *);
void sdmmc_intr_enable(struct sdmmc_function *);
void sdmmc_intr_disable(struct sdmmc_function *);
void *sdmmc_intr_establish(device_t, int (*)(void *), void *, const char *);
void sdmmc_intr_disestablish(void *);
void sdmmc_intr_task(void *);
int sdmmc_decode_csd(struct sdmmc_softc *, sdmmc_response,
struct sdmmc_function *);
int sdmmc_decode_cid(struct sdmmc_softc *, sdmmc_response,
struct sdmmc_function *);
void sdmmc_print_cid(struct sdmmc_cid *);
#ifdef SDMMC_DUMP_CSD
void sdmmc_print_csd(sdmmc_response, struct sdmmc_csd *);
#endif
void sdmmc_dump_data(const char *, void *, size_t);
int sdmmc_io_enable(struct sdmmc_softc *);
void sdmmc_io_scan(struct sdmmc_softc *);
int sdmmc_io_init(struct sdmmc_softc *, struct sdmmc_function *);
int sdmmc_io_set_blocklen(struct sdmmc_function *, int);
uint8_t sdmmc_io_read_1(struct sdmmc_function *, int);
uint16_t sdmmc_io_read_2(struct sdmmc_function *, int);
uint32_t sdmmc_io_read_4(struct sdmmc_function *, int);
int sdmmc_io_read_multi_1(struct sdmmc_function *, int, u_char *, int);
int sdmmc_io_read_region_1(struct sdmmc_function *, int, u_char *, int);
void sdmmc_io_write_1(struct sdmmc_function *, int, uint8_t);
void sdmmc_io_write_2(struct sdmmc_function *, int, uint16_t);
void sdmmc_io_write_4(struct sdmmc_function *, int, uint32_t);
int sdmmc_io_write_multi_1(struct sdmmc_function *, int, u_char *, int);
int sdmmc_io_write_region_1(struct sdmmc_function *, int, u_char *, int);
int sdmmc_io_function_enable(struct sdmmc_function *);
void sdmmc_io_function_disable(struct sdmmc_function *);
int sdmmc_io_function_abort(struct sdmmc_function *);
uint32_t sdmmc_cisptr(struct sdmmc_function *);
int sdmmc_read_cis(struct sdmmc_function *, struct sdmmc_cis *);
void sdmmc_print_cis(struct sdmmc_function *);
void sdmmc_check_cis_quirks(struct sdmmc_function *);
int sdmmc_mem_enable(struct sdmmc_softc *);
void sdmmc_mem_scan(struct sdmmc_softc *);
int sdmmc_mem_init(struct sdmmc_softc *, struct sdmmc_function *);
int sdmmc_mem_send_op_cond(struct sdmmc_softc *, uint32_t, uint32_t *);
int sdmmc_mem_send_if_cond(struct sdmmc_softc *, uint32_t, uint32_t *);
int sdmmc_mem_set_blocklen(struct sdmmc_softc *, struct sdmmc_function *,
int);
int sdmmc_mem_read_block(struct sdmmc_function *, uint32_t, u_char *,
size_t);
int sdmmc_mem_write_block(struct sdmmc_function *, uint32_t, u_char *,
size_t);
int sdmmc_mem_discard(struct sdmmc_function *, uint32_t, uint32_t);
int sdmmc_mem_flush_cache(struct sdmmc_function *, bool);
void sdmmc_pause(u_int, kmutex_t *);
static inline bool
sdmmc_alignment_ok(struct sdmmc_softc *sc, bus_dmamap_t dm)
{
const bus_addr_t align_mask = sc->sc_dma_align_mask;
if (align_mask != 0) {
int n;
for (n = 0; n < dm->dm_nsegs; n++) {
if ((dm->dm_segs[n].ds_addr & align_mask) != 0 ||
(dm->dm_segs[n].ds_len & align_mask) != 0) {
return false;
}
}
}
return true;
}
#endif