Home | History | Annotate | Line # | Download | only in amlogic
mesongx_mmc.c revision 1.14
      1  1.14  jmcneill /* $NetBSD: mesongx_mmc.c,v 1.14 2021/01/28 11:45:31 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.14  jmcneill __KERNEL_RCSID(0, "$NetBSD: mesongx_mmc.c,v 1.14 2021/01/28 11:45:31 jmcneill Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/bus.h>
     34   1.1  jmcneill #include <sys/device.h>
     35   1.1  jmcneill #include <sys/intr.h>
     36   1.1  jmcneill #include <sys/systm.h>
     37   1.1  jmcneill #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/bitops.h>
     39   1.1  jmcneill #include <sys/gpio.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/sdmmc/sdmmcvar.h>
     42   1.1  jmcneill #include <dev/sdmmc/sdmmcchip.h>
     43   1.1  jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
     44   1.1  jmcneill 
     45   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill #define	SD_EMMC_CLOCK			0x00
     48   1.6       ryo #define	 CLOCK_CFG_V2_IRQ_SDIO_SLEEP		__BIT(25)
     49   1.6       ryo #define	 CLOCK_CFG_V2_ALWAYS_ON			__BIT(24)
     50   1.6       ryo #define	 CLOCK_CFG_V2_RX_DELAY			__BITS(23,20)
     51   1.6       ryo #define	 CLOCK_CFG_V2_TX_DELAY			__BITS(19,16)
     52   1.6       ryo #define	 CLOCK_CFG_V3_IRQ_SDIO_SLEEP		__BIT(29)
     53   1.6       ryo #define	 CLOCK_CFG_V3_ALWAYS_ON			__BIT(28)
     54   1.6       ryo #define	 CLOCK_CFG_V3_RX_DELAY			__BITS(27,22)
     55   1.6       ryo #define	 CLOCK_CFG_V3_TX_DELAY			__BITS(21,16)
     56   1.1  jmcneill #define	 CLOCK_CFG_SRAM_PD			__BITS(15,14)
     57   1.1  jmcneill #define	 CLOCK_CFG_RX_PHASE			__BITS(13,12)
     58   1.1  jmcneill #define	 CLOCK_CFG_TX_PHASE			__BITS(11,10)
     59   1.1  jmcneill #define	 CLOCK_CFG_CO_PHASE			__BITS(9,8)
     60   1.1  jmcneill #define	 CLOCK_CFG_SRC				__BITS(7,6)
     61   1.1  jmcneill #define	 CLOCK_CFG_DIV				__BITS(5,0)
     62   1.1  jmcneill #define	SD_EMMC_DELAY			0x04
     63   1.6       ryo #define	SD_EMMC_ADJUST			0x08	/* V2 */
     64   1.1  jmcneill #define	 ADJUST_ADJ_DELAY			__BITS(21,16)
     65   1.1  jmcneill #define	 ADJUST_CALI_RISE			__BIT(14)
     66   1.1  jmcneill #define	 ADJUST_ADJ_ENABLE			__BIT(13)
     67   1.1  jmcneill #define	 ADJUST_CALI_ENABLE			__BIT(12)
     68   1.1  jmcneill #define	 ADJUST_CALI_SEL			__BITS(11,8)
     69   1.1  jmcneill #define	SD_EMMC_CALOUT			0x10
     70   1.1  jmcneill #define	 CALOUT_CALI_SETUP			__BITS(15,8)
     71   1.1  jmcneill #define	 CALOUT_CALI_VLD			__BIT(7)
     72   1.1  jmcneill #define	 CALOUT_CALI_IDX			__BITS(5,0)
     73   1.7  jmcneill #define	SD_EMMC_V3_ADJUST		0x0c
     74   1.1  jmcneill #define	SD_EMMC_START			0x40
     75   1.1  jmcneill #define	 START_DESC_ADDR			__BITS(31,2)
     76   1.1  jmcneill #define	 START_DESC_BUSY			__BIT(1)
     77   1.1  jmcneill #define	 START_DESC_INT				__BIT(0)
     78   1.1  jmcneill #define	SD_EMMC_CFG			0x44
     79   1.1  jmcneill #define	 CFG_IP_TXD_ADJ				__BITS(31,28)
     80   1.1  jmcneill #define	 CFG_ERR_ABORT				__BIT(27)
     81   1.1  jmcneill #define	 CFG_IRQ_DS				__BIT(26)
     82   1.1  jmcneill #define	 CFG_TXD_RETRY				__BIT(25)
     83   1.1  jmcneill #define	 CFG_TXD_ADD_ERR			__BIT(24)
     84   1.1  jmcneill #define	 CFG_AUTO_CLK				__BIT(23)
     85   1.1  jmcneill #define	 CFG_STOP_CLK				__BIT(22)
     86   1.1  jmcneill #define	 CFG_CMD_LOW				__BIT(21)
     87   1.1  jmcneill #define	 CFG_CHK_DS				__BIT(20)
     88   1.1  jmcneill #define	 CFG_IGNORE_OWNER			__BIT(19)
     89   1.1  jmcneill #define	 CFG_SDCLK_ALWAYS_ON			__BIT(18)
     90   1.1  jmcneill #define	 CFG_BLK_GAP_IP				__BIT(17)
     91   1.1  jmcneill #define	 CFG_OUT_FALL				__BIT(16)
     92   1.1  jmcneill #define	 CFG_RC_CC				__BITS(15,12)
     93   1.1  jmcneill #define	 CFG_RESP_TIMEOUT			__BIT(11,8)
     94   1.1  jmcneill #define	 CFG_BL_LEN				__BITS(7,4)
     95   1.1  jmcneill #define	 CFG_DC_UGT				__BIT(3)
     96   1.1  jmcneill #define	 CFG_DDR				__BIT(2)
     97   1.1  jmcneill #define	 CFG_BUS_WIDTH				__BITS(1,0)
     98   1.1  jmcneill #define	  CFG_BUS_WIDTH_1			0
     99   1.1  jmcneill #define	  CFG_BUS_WIDTH_4			1
    100   1.1  jmcneill #define	  CFG_BUS_WIDTH_8			2
    101   1.1  jmcneill #define	SD_EMMC_STATUS			0x48
    102   1.1  jmcneill #define	 STATUS_CORE_BUSY			__BIT(31)
    103   1.1  jmcneill #define	 STATUS_DESC_BUSY			__BIT(30)
    104   1.1  jmcneill #define	 STATUS_BUS_FSM				__BIT(29,26)
    105   1.1  jmcneill #define	 STATUS_DS				__BIT(25)
    106   1.1  jmcneill #define	 STATUS_CMD_I				__BIT(24)
    107   1.1  jmcneill #define	 STATUS_DAT_I				__BITS(23,16)
    108   1.1  jmcneill #define	 STATUS_IRQ_SDIO			__BIT(15)
    109   1.1  jmcneill #define	 STATUS_RESP_STATUS			__BIT(14)
    110   1.1  jmcneill #define	 STATUS_END_OF_CHAIN			__BIT(13)
    111   1.1  jmcneill #define	 STATUS_DESC_TIMEOUT			__BIT(12)
    112   1.1  jmcneill #define	 STATUS_RESP_TIMEOUT			__BIT(11)
    113   1.1  jmcneill #define	 STATUS_RESP_ERR			__BIT(10)
    114   1.1  jmcneill #define	 STATUS_DESC_ERR			__BIT(9)
    115   1.1  jmcneill #define	 STATUS_TXD_ERR				__BIT(8)
    116   1.1  jmcneill #define	 STATUS_RXD_ERR				__BITS(7,0)
    117   1.1  jmcneill #define	 STATUS_TIMEOUT				(STATUS_DESC_TIMEOUT | STATUS_RESP_TIMEOUT)
    118   1.1  jmcneill #define	 STATUS_ERROR				(STATUS_RESP_ERR | STATUS_DESC_ERR | STATUS_RXD_ERR | STATUS_TXD_ERR)
    119   1.1  jmcneill #define	SD_EMMC_IRQ_EN			0x4c
    120   1.1  jmcneill #define	 IRQ_EN_CFG_SECURE			__BIT(16)
    121   1.1  jmcneill #define	 IRQ_EN_IRQ_SDIO			__BIT(15)
    122   1.1  jmcneill #define	 IRQ_EN_RESP_STATUS			__BIT(14)
    123   1.1  jmcneill #define	 IRQ_EN_END_OF_CHAIN			__BIT(13)
    124   1.1  jmcneill #define	 IRQ_EN_DESC_TIMEOUT			__BIT(12)
    125   1.1  jmcneill #define	 IRQ_EN_RESP_TIMEOUT			__BIT(11)
    126   1.1  jmcneill #define	 IRQ_EN_RESP_ERR			__BIT(10)
    127   1.1  jmcneill #define	 IRQ_EN_DESC_ERR			__BIT(9)
    128   1.1  jmcneill #define	 IRQ_EN_TXD_ERR				__BIT(8)
    129   1.1  jmcneill #define	 IRQ_EN_RXD_ERR				__BITS(7,0)
    130   1.1  jmcneill #define	SD_EMMC_CMD_CFG			0x50
    131   1.1  jmcneill #define	SD_EMMC_CMD_ARG			0x54
    132   1.1  jmcneill #define	SD_EMMC_CMD_DAT			0x58
    133   1.1  jmcneill #define	SD_EMMC_CMD_RSP			0x5c
    134   1.1  jmcneill #define	SD_EMMC_CMD_RSP1		0x60
    135   1.1  jmcneill #define	SD_EMMC_CMD_RSP2		0x64
    136   1.1  jmcneill #define	SD_EMMC_CMD_RSP3		0x68
    137   1.1  jmcneill 
    138   1.1  jmcneill struct mesongx_mmc_desc {
    139   1.1  jmcneill 	uint32_t		flags;
    140   1.1  jmcneill #define	MESONGX_MMC_FLAGS_OWNER		__BIT(31)
    141   1.1  jmcneill #define	MESONGX_MMC_FLAGS_ERROR		__BIT(30)
    142   1.1  jmcneill #define	MESONGX_MMC_FLAGS_CMD_INDEX	__BITS(29,24)
    143   1.1  jmcneill #define	MESONGX_MMC_FLAGS_DATA_NUM	__BIT(23)
    144   1.1  jmcneill #define	MESONGX_MMC_FLAGS_RESP_NUM	__BIT(22)
    145   1.1  jmcneill #define	MESONGX_MMC_FLAGS_RESP_128	__BIT(21)
    146   1.1  jmcneill #define	MESONGX_MMC_FLAGS_RESP_NOCRC	__BIT(20)
    147   1.1  jmcneill #define	MESONGX_MMC_FLAGS_DATA_WR	__BIT(19)
    148   1.1  jmcneill #define	MESONGX_MMC_FLAGS_DATA_IO	__BIT(18)
    149   1.1  jmcneill #define	MESONGX_MMC_FLAGS_NO_CMD	__BIT(17)
    150   1.1  jmcneill #define	MESONGX_MMC_FLAGS_NO_RESP	__BIT(16)
    151   1.1  jmcneill #define	MESONGX_MMC_FLAGS_TIMEOUT	__BITS(15,12)
    152   1.1  jmcneill #define	MESONGX_MMC_FLAGS_END_OF_CHAIN	__BIT(11)
    153   1.1  jmcneill #define	MESONGX_MMC_FLAGS_R1B		__BIT(10)
    154   1.1  jmcneill #define	MESONGX_MMC_FLAGS_BLOCK_MODE	__BIT(9)
    155   1.1  jmcneill #define	MESONGX_MMC_FLAGS_LENGTH	__BITS(8,0)
    156   1.1  jmcneill 	uint32_t		arg;
    157   1.1  jmcneill 	uint32_t		data;
    158   1.1  jmcneill #define	MESONGX_MMC_DATA_BIG_ENDIAN	__BIT(1)
    159   1.1  jmcneill #define	MESONGX_MMC_DATA_SRAM		__BIT(0)
    160   1.1  jmcneill 	uint32_t		resp;
    161   1.1  jmcneill #define	MESONGX_MMC_RESP_SRAM		__BIT(0)
    162   1.1  jmcneill } __packed;
    163   1.1  jmcneill 
    164   1.1  jmcneill #define MESONGX_MMC_NDESC		256
    165   1.1  jmcneill 
    166   1.1  jmcneill struct mesongx_mmc_softc;
    167   1.1  jmcneill 
    168   1.1  jmcneill static int	mesongx_mmc_match(device_t, cfdata_t, void *);
    169   1.1  jmcneill static void	mesongx_mmc_attach(device_t, device_t, void *);
    170   1.1  jmcneill static void	mesongx_mmc_attach_i(device_t);
    171   1.1  jmcneill 
    172   1.1  jmcneill static int	mesongx_mmc_intr(void *);
    173   1.1  jmcneill static int	mesongx_mmc_dma_setup(struct mesongx_mmc_softc *);
    174   1.1  jmcneill static int	mesongx_mmc_dmabounce_setup(struct mesongx_mmc_softc *);
    175   1.1  jmcneill 
    176   1.1  jmcneill static int	mesongx_mmc_host_reset(sdmmc_chipset_handle_t);
    177   1.1  jmcneill static uint32_t	mesongx_mmc_host_ocr(sdmmc_chipset_handle_t);
    178   1.1  jmcneill static int	mesongx_mmc_host_maxblklen(sdmmc_chipset_handle_t);
    179   1.1  jmcneill static int	mesongx_mmc_card_detect(sdmmc_chipset_handle_t);
    180   1.1  jmcneill static int	mesongx_mmc_write_protect(sdmmc_chipset_handle_t);
    181   1.1  jmcneill static int	mesongx_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    182   1.1  jmcneill static int	mesongx_mmc_bus_clock(sdmmc_chipset_handle_t, int, bool);
    183   1.1  jmcneill static int	mesongx_mmc_bus_width(sdmmc_chipset_handle_t, int);
    184   1.1  jmcneill static int	mesongx_mmc_bus_rod(sdmmc_chipset_handle_t, int);
    185   1.1  jmcneill static int	mesongx_mmc_signal_voltage(sdmmc_chipset_handle_t, int);
    186   1.1  jmcneill static int	mesongx_mmc_execute_tuning(sdmmc_chipset_handle_t, int);
    187   1.1  jmcneill static void	mesongx_mmc_exec_command(sdmmc_chipset_handle_t,
    188   1.1  jmcneill 				      struct sdmmc_command *);
    189   1.1  jmcneill static void	mesongx_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
    190   1.1  jmcneill static void	mesongx_mmc_card_intr_ack(sdmmc_chipset_handle_t);
    191   1.1  jmcneill 
    192   1.1  jmcneill static struct sdmmc_chip_functions mesongx_mmc_chip_functions = {
    193   1.1  jmcneill 	.host_reset = mesongx_mmc_host_reset,
    194   1.1  jmcneill 	.host_ocr = mesongx_mmc_host_ocr,
    195   1.1  jmcneill 	.host_maxblklen = mesongx_mmc_host_maxblklen,
    196   1.1  jmcneill 	.card_detect = mesongx_mmc_card_detect,
    197   1.1  jmcneill 	.write_protect = mesongx_mmc_write_protect,
    198   1.1  jmcneill 	.bus_power = mesongx_mmc_bus_power,
    199   1.1  jmcneill 	.bus_clock_ddr = mesongx_mmc_bus_clock,
    200   1.1  jmcneill 	.bus_width = mesongx_mmc_bus_width,
    201   1.1  jmcneill 	.bus_rod = mesongx_mmc_bus_rod,
    202   1.1  jmcneill 	.signal_voltage = mesongx_mmc_signal_voltage,
    203   1.1  jmcneill 	.execute_tuning = mesongx_mmc_execute_tuning,
    204   1.1  jmcneill 	.exec_command = mesongx_mmc_exec_command,
    205   1.1  jmcneill 	.card_enable_intr = mesongx_mmc_card_enable_intr,
    206   1.1  jmcneill 	.card_intr_ack = mesongx_mmc_card_intr_ack,
    207   1.1  jmcneill };
    208   1.1  jmcneill 
    209   1.1  jmcneill struct mesongx_mmc_softc {
    210   1.1  jmcneill 	device_t		sc_dev;
    211   1.1  jmcneill 	bus_space_tag_t		sc_bst;
    212   1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    213   1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
    214   1.1  jmcneill 	int			sc_phandle;
    215   1.1  jmcneill 
    216   1.1  jmcneill 	void			*sc_ih;
    217   1.1  jmcneill 	kmutex_t		sc_intr_lock;
    218   1.1  jmcneill 	kcondvar_t		sc_intr_cv;
    219   1.1  jmcneill 
    220   1.1  jmcneill 	device_t		sc_sdmmc_dev;
    221   1.1  jmcneill 	uint32_t		sc_host_ocr;
    222   1.6       ryo 	int			sc_hwtype;
    223   1.1  jmcneill 
    224   1.1  jmcneill 	struct sdmmc_command	*sc_cmd;
    225   1.1  jmcneill 
    226   1.1  jmcneill 	bus_dma_segment_t	sc_desc_segs[1];
    227   1.1  jmcneill 	int			sc_desc_nsegs;
    228   1.1  jmcneill 	bus_size_t		sc_desc_size;
    229   1.1  jmcneill 	bus_dmamap_t		sc_desc_map;
    230   1.1  jmcneill 	int			sc_desc_ndesc;
    231   1.1  jmcneill 	void			*sc_desc_desc;
    232   1.1  jmcneill 
    233   1.1  jmcneill 	bus_dmamap_t		sc_dmabounce_map;
    234   1.1  jmcneill 	void			*sc_dmabounce_buf;
    235   1.1  jmcneill 	size_t			sc_dmabounce_buflen;
    236   1.1  jmcneill 
    237   1.1  jmcneill 	struct clk		*sc_clk_core;
    238   1.1  jmcneill 	struct clk		*sc_clk_clkin[2];
    239   1.1  jmcneill 
    240   1.1  jmcneill 	struct fdtbus_reset	*sc_rst;
    241   1.1  jmcneill 
    242   1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_gpio_cd;
    243   1.1  jmcneill 	int			sc_gpio_cd_inverted;
    244   1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_gpio_wp;
    245   1.1  jmcneill 	int			sc_gpio_wp_inverted;
    246   1.1  jmcneill 
    247   1.1  jmcneill 	struct fdtbus_regulator	*sc_reg_vmmc;
    248   1.1  jmcneill 	struct fdtbus_regulator	*sc_reg_vqmmc;
    249   1.1  jmcneill 
    250   1.1  jmcneill 	struct fdtbus_mmc_pwrseq *sc_pwrseq;
    251   1.1  jmcneill 
    252   1.1  jmcneill 	u_int			sc_max_frequency;
    253   1.1  jmcneill 	bool			sc_non_removable;
    254   1.1  jmcneill 	bool			sc_broken_cd;
    255   1.1  jmcneill };
    256   1.1  jmcneill 
    257   1.1  jmcneill CFATTACH_DECL_NEW(mesongx_mmc, sizeof(struct mesongx_mmc_softc),
    258   1.1  jmcneill 	mesongx_mmc_match, mesongx_mmc_attach, NULL, NULL);
    259   1.1  jmcneill 
    260   1.1  jmcneill #define MMC_WRITE(sc, reg, val)	\
    261   1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    262   1.1  jmcneill #define MMC_READ(sc, reg) \
    263   1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    264   1.1  jmcneill 
    265   1.8  jmcneill enum {
    266   1.8  jmcneill 	MESONGX_MMC_V2 = 2,
    267   1.8  jmcneill 	MESONGX_MMC_V3 = 3,
    268   1.8  jmcneill };
    269   1.8  jmcneill 
    270  1.10   thorpej static const struct device_compatible_entry compat_data[] = {
    271  1.10   thorpej 	{ .compat = "amlogic,meson-gx-mmc",	.value = MESONGX_MMC_V2 },
    272  1.10   thorpej 	{ .compat = "amlogic,meson-gxbb-mmc",	.value = MESONGX_MMC_V2 },
    273  1.10   thorpej 	{ .compat = "amlogic,meson-axg-mmc",	.value = MESONGX_MMC_V3 },
    274  1.12   thorpej 	DEVICE_COMPAT_EOL
    275   1.1  jmcneill };
    276   1.1  jmcneill 
    277   1.1  jmcneill static int
    278   1.1  jmcneill mesongx_mmc_match(device_t parent, cfdata_t cf, void *aux)
    279   1.1  jmcneill {
    280   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    281   1.1  jmcneill 
    282  1.13   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    283   1.1  jmcneill }
    284   1.1  jmcneill 
    285   1.1  jmcneill static void
    286   1.1  jmcneill mesongx_mmc_attach(device_t parent, device_t self, void *aux)
    287   1.1  jmcneill {
    288   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = device_private(self);
    289   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    290   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    291   1.1  jmcneill 	char intrstr[128];
    292   1.1  jmcneill 	bus_addr_t addr;
    293   1.1  jmcneill 	bus_size_t size;
    294   1.1  jmcneill 
    295  1.13   thorpej 	sc->sc_hwtype = of_compatible_lookup(phandle, compat_data)->value;
    296   1.6       ryo 
    297   1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    298   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    299   1.1  jmcneill 		return;
    300   1.1  jmcneill 	}
    301   1.1  jmcneill 
    302   1.1  jmcneill 	sc->sc_clk_core = fdtbus_clock_get(phandle, "core");
    303   1.1  jmcneill 	sc->sc_clk_clkin[0] = fdtbus_clock_get(phandle, "clkin0");
    304   1.1  jmcneill 	sc->sc_clk_clkin[1] = fdtbus_clock_get(phandle, "clkin1");
    305   1.1  jmcneill 
    306   1.1  jmcneill 	if (sc->sc_clk_core == NULL || sc->sc_clk_clkin[0] == NULL ||
    307   1.1  jmcneill 	    sc->sc_clk_clkin[1] == NULL) {
    308   1.1  jmcneill 		aprint_error(": couldn't get clocks\n");
    309   1.1  jmcneill 		return;
    310   1.1  jmcneill 	}
    311   1.1  jmcneill 
    312   1.1  jmcneill 	sc->sc_rst = fdtbus_reset_get_index(phandle, 0);
    313   1.1  jmcneill 	if (sc->sc_rst == NULL) {
    314   1.1  jmcneill 		aprint_error(": couldn't get reset\n");
    315   1.1  jmcneill 		return;
    316   1.1  jmcneill 	}
    317   1.1  jmcneill 
    318   1.1  jmcneill 	sc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
    319   1.1  jmcneill 
    320   1.1  jmcneill 	if (clk_enable(sc->sc_clk_core) != 0) {
    321   1.1  jmcneill 		aprint_error(": couldn't enable core clock\n");
    322   1.1  jmcneill 		return;
    323   1.1  jmcneill 	}
    324   1.1  jmcneill 	if (clk_enable(sc->sc_clk_clkin[0]) != 0 ||
    325   1.1  jmcneill 	    clk_enable(sc->sc_clk_clkin[1]) != 0) {
    326   1.1  jmcneill 		aprint_error(": couldn't enable clkin clocks\n");
    327   1.1  jmcneill 		return;
    328   1.1  jmcneill 	}
    329   1.1  jmcneill 
    330   1.1  jmcneill 	if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
    331   1.1  jmcneill 		aprint_error(": couldn't de-assert reset\n");
    332   1.1  jmcneill 		return;
    333   1.1  jmcneill 	}
    334   1.1  jmcneill 
    335   1.1  jmcneill 	sc->sc_dev = self;
    336   1.1  jmcneill 	sc->sc_phandle = phandle;
    337   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    338   1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    339   1.1  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
    340   1.1  jmcneill 	cv_init(&sc->sc_intr_cv, "gxmmcirq");
    341   1.1  jmcneill 
    342   1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    343   1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    344   1.1  jmcneill 		return;
    345   1.1  jmcneill 	}
    346   1.1  jmcneill 
    347   1.1  jmcneill 	aprint_naive("\n");
    348   1.1  jmcneill 	aprint_normal(": eMMC/SD/SDIO controller\n");
    349   1.1  jmcneill 
    350   1.1  jmcneill 	sc->sc_reg_vmmc = fdtbus_regulator_acquire(phandle, "vmmc-supply");
    351   1.1  jmcneill 	sc->sc_reg_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
    352   1.1  jmcneill 
    353   1.1  jmcneill 	sc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
    354   1.1  jmcneill 	    GPIO_PIN_INPUT);
    355   1.1  jmcneill 	sc->sc_gpio_wp = fdtbus_gpio_acquire(phandle, "wp-gpios",
    356   1.1  jmcneill 	    GPIO_PIN_INPUT);
    357   1.1  jmcneill 
    358   1.1  jmcneill 	sc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 1 : 0;
    359   1.1  jmcneill 	sc->sc_gpio_wp_inverted = of_hasprop(phandle, "wp-inverted") ? 1 : 0;
    360   1.1  jmcneill 
    361   1.1  jmcneill 	sc->sc_non_removable = of_hasprop(phandle, "non-removable");
    362   1.1  jmcneill 	sc->sc_broken_cd = of_hasprop(phandle, "broken-cd");
    363   1.1  jmcneill 
    364   1.1  jmcneill 	if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_frequency))
    365   1.1  jmcneill 		sc->sc_max_frequency = 52000000;
    366   1.1  jmcneill 
    367   1.1  jmcneill 	if (mesongx_mmc_dma_setup(sc) != 0 ||
    368   1.1  jmcneill 	    mesongx_mmc_dmabounce_setup(sc) != 0) {
    369   1.1  jmcneill 		aprint_error_dev(self, "failed to setup DMA\n");
    370   1.1  jmcneill 		return;
    371   1.1  jmcneill 	}
    372   1.1  jmcneill 
    373   1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    374   1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    375   1.1  jmcneill 		return;
    376   1.1  jmcneill 	}
    377   1.1  jmcneill 
    378   1.9       ryo 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO,
    379   1.9       ryo 	    FDT_INTR_MPSAFE, mesongx_mmc_intr, sc, device_xname(self));
    380   1.1  jmcneill 	if (sc->sc_ih == NULL) {
    381   1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    382   1.1  jmcneill 		    intrstr);
    383   1.1  jmcneill 		return;
    384   1.1  jmcneill 	}
    385   1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    386   1.1  jmcneill 
    387   1.2  jmcneill 	if (sc->sc_pwrseq)
    388   1.2  jmcneill 		fdtbus_mmc_pwrseq_reset(sc->sc_pwrseq);
    389   1.2  jmcneill 
    390   1.1  jmcneill 	config_interrupts(self, mesongx_mmc_attach_i);
    391   1.1  jmcneill }
    392   1.1  jmcneill 
    393   1.1  jmcneill static int
    394   1.1  jmcneill mesongx_mmc_dma_setup(struct mesongx_mmc_softc *sc)
    395   1.1  jmcneill {
    396   1.1  jmcneill 	int error;
    397   1.1  jmcneill 
    398   1.1  jmcneill 	sc->sc_desc_ndesc = MESONGX_MMC_NDESC;
    399   1.1  jmcneill 	sc->sc_desc_size = sizeof(struct mesongx_mmc_desc) *
    400   1.1  jmcneill 	    sc->sc_desc_ndesc;
    401   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_size,
    402   1.1  jmcneill 	    sizeof(struct mesongx_mmc_desc),
    403   1.1  jmcneill 	    sc->sc_desc_size, sc->sc_desc_segs, 1,
    404   1.1  jmcneill 	    &sc->sc_desc_nsegs, BUS_DMA_WAITOK);
    405   1.1  jmcneill 	if (error)
    406   1.1  jmcneill 		return error;
    407   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, sc->sc_desc_segs,
    408   1.1  jmcneill 	    sc->sc_desc_nsegs, sc->sc_desc_size,
    409   1.1  jmcneill 	    &sc->sc_desc_desc, BUS_DMA_WAITOK);
    410   1.1  jmcneill 	if (error)
    411   1.1  jmcneill 		goto free;
    412   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_size, 1,
    413   1.1  jmcneill 	    sc->sc_desc_size, 0, BUS_DMA_WAITOK, &sc->sc_desc_map);
    414   1.1  jmcneill 	if (error)
    415   1.1  jmcneill 		goto unmap;
    416   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_desc_map,
    417   1.1  jmcneill 	    sc->sc_desc_desc, sc->sc_desc_size, NULL, BUS_DMA_WAITOK);
    418   1.1  jmcneill 	if (error)
    419   1.1  jmcneill 		goto destroy;
    420   1.1  jmcneill 	return 0;
    421   1.1  jmcneill 
    422   1.1  jmcneill destroy:
    423   1.1  jmcneill 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_desc_map);
    424   1.1  jmcneill unmap:
    425   1.1  jmcneill 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_desc_desc, sc->sc_desc_size);
    426   1.1  jmcneill free:
    427   1.1  jmcneill 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc_segs, sc->sc_desc_nsegs);
    428   1.1  jmcneill 	return error;
    429   1.1  jmcneill }
    430   1.1  jmcneill 
    431   1.1  jmcneill static int
    432   1.1  jmcneill mesongx_mmc_dmabounce_setup(struct mesongx_mmc_softc *sc)
    433   1.1  jmcneill {
    434   1.1  jmcneill 	bus_dma_segment_t ds[1];
    435   1.1  jmcneill 	int error, rseg;
    436   1.1  jmcneill 
    437   1.1  jmcneill 	sc->sc_dmabounce_buflen = MAXPHYS;
    438   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmabounce_buflen, 0,
    439   1.1  jmcneill 	    sc->sc_dmabounce_buflen, ds, 1, &rseg, BUS_DMA_WAITOK);
    440   1.1  jmcneill 	if (error)
    441   1.1  jmcneill 		return error;
    442   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, ds, 1, sc->sc_dmabounce_buflen,
    443   1.1  jmcneill 	    &sc->sc_dmabounce_buf, BUS_DMA_WAITOK);
    444   1.1  jmcneill 	if (error)
    445   1.1  jmcneill 		goto free;
    446   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmabounce_buflen, 1,
    447   1.1  jmcneill 	    sc->sc_dmabounce_buflen, 0, BUS_DMA_WAITOK, &sc->sc_dmabounce_map);
    448   1.1  jmcneill 	if (error)
    449   1.1  jmcneill 		goto unmap;
    450   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmabounce_map,
    451   1.1  jmcneill 	    sc->sc_dmabounce_buf, sc->sc_dmabounce_buflen, NULL,
    452   1.1  jmcneill 	    BUS_DMA_WAITOK);
    453   1.1  jmcneill 	if (error)
    454   1.1  jmcneill 		goto destroy;
    455   1.1  jmcneill 	return 0;
    456   1.1  jmcneill 
    457   1.1  jmcneill destroy:
    458   1.1  jmcneill 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmabounce_map);
    459   1.1  jmcneill unmap:
    460   1.1  jmcneill 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_dmabounce_buf,
    461   1.1  jmcneill 	    sc->sc_dmabounce_buflen);
    462   1.1  jmcneill free:
    463   1.1  jmcneill 	bus_dmamem_free(sc->sc_dmat, ds, rseg);
    464   1.1  jmcneill 	return error;
    465   1.1  jmcneill }
    466   1.1  jmcneill 
    467   1.1  jmcneill static int
    468   1.1  jmcneill mesongx_mmc_set_clock(struct mesongx_mmc_softc *sc, u_int freq, bool ddr)
    469   1.1  jmcneill {
    470   1.1  jmcneill 	int best_diff, best_sel, best_div, sel, div;
    471   1.1  jmcneill 	uint32_t val;
    472   1.1  jmcneill 
    473   1.1  jmcneill 	if (freq == 0)
    474   1.1  jmcneill 		freq = SDMMC_SDCLK_400K;
    475   1.1  jmcneill 
    476   1.1  jmcneill 	best_diff = INT_MAX;
    477   1.1  jmcneill 	best_sel = 0;
    478   1.1  jmcneill 	best_div = 0;
    479   1.1  jmcneill 
    480   1.5  jmcneill 	const u_int target_rate = (freq * 1000) << ddr;
    481   1.1  jmcneill 	for (sel = 0; sel <= 1; sel++) {
    482   1.1  jmcneill 		const u_int parent_rate = clk_get_rate(sc->sc_clk_clkin[sel]);
    483   1.1  jmcneill 		for (div = 1; div <= 63; div++) {
    484   1.1  jmcneill 			const u_int rate = parent_rate / div;
    485   1.1  jmcneill 			if (rate > target_rate)
    486   1.1  jmcneill 				continue;
    487   1.1  jmcneill 			const int diff = target_rate - rate;
    488   1.1  jmcneill 			if (diff < best_diff) {
    489   1.1  jmcneill 				best_diff = diff;
    490   1.1  jmcneill 				best_sel = sel;
    491   1.1  jmcneill 				best_div = div;
    492   1.1  jmcneill 			}
    493   1.1  jmcneill 		}
    494   1.1  jmcneill 	}
    495   1.1  jmcneill 
    496   1.1  jmcneill 	if (best_diff == INT_MAX)
    497   1.1  jmcneill 		return ERANGE;
    498   1.1  jmcneill 
    499  1.14  jmcneill 	val = MMC_READ(sc, SD_EMMC_CFG);
    500  1.14  jmcneill 	val |= CFG_STOP_CLK;
    501  1.14  jmcneill 	MMC_WRITE(sc, SD_EMMC_CFG, val);
    502  1.14  jmcneill 
    503  1.14  jmcneill 	val = MMC_READ(sc, SD_EMMC_CFG);
    504  1.14  jmcneill 	if (ddr)
    505  1.14  jmcneill 		val |= CFG_DDR;
    506  1.14  jmcneill 	else
    507  1.14  jmcneill 		val &= ~CFG_DDR;
    508  1.14  jmcneill 	MMC_WRITE(sc, SD_EMMC_CFG, val);
    509  1.14  jmcneill 
    510   1.1  jmcneill 	val = MMC_READ(sc, SD_EMMC_CLOCK);
    511   1.8  jmcneill 	if (sc->sc_hwtype == MESONGX_MMC_V3)
    512   1.6       ryo 		val |= CLOCK_CFG_V3_ALWAYS_ON;
    513   1.6       ryo 	else
    514   1.6       ryo 		val |= CLOCK_CFG_V2_ALWAYS_ON;
    515   1.1  jmcneill 	val &= ~CLOCK_CFG_RX_PHASE;
    516   1.1  jmcneill 	val |= __SHIFTIN(0, CLOCK_CFG_RX_PHASE);
    517   1.1  jmcneill 	val &= ~CLOCK_CFG_TX_PHASE;
    518  1.14  jmcneill 	val |= __SHIFTIN(0, CLOCK_CFG_TX_PHASE);
    519   1.1  jmcneill 	val &= ~CLOCK_CFG_CO_PHASE;
    520  1.14  jmcneill 	val |= __SHIFTIN(2, CLOCK_CFG_CO_PHASE);
    521   1.1  jmcneill 	val &= ~CLOCK_CFG_SRC;
    522   1.1  jmcneill 	val |= __SHIFTIN(best_sel, CLOCK_CFG_SRC);
    523   1.1  jmcneill 	val &= ~CLOCK_CFG_DIV;
    524   1.1  jmcneill 	val |= __SHIFTIN(best_div, CLOCK_CFG_DIV);
    525   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_CLOCK, val);
    526   1.1  jmcneill 
    527  1.14  jmcneill 	val = MMC_READ(sc, SD_EMMC_CFG);
    528  1.14  jmcneill 	val &= ~CFG_STOP_CLK;
    529  1.14  jmcneill 	MMC_WRITE(sc, SD_EMMC_CFG, val);
    530  1.14  jmcneill 
    531   1.1  jmcneill 	return 0;
    532   1.1  jmcneill }
    533   1.1  jmcneill 
    534   1.1  jmcneill static void
    535   1.1  jmcneill mesongx_mmc_attach_i(device_t self)
    536   1.1  jmcneill {
    537   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = device_private(self);
    538   1.1  jmcneill 	struct sdmmcbus_attach_args saa;
    539   1.1  jmcneill 	uint32_t width;
    540   1.1  jmcneill 
    541   1.1  jmcneill 	if (sc->sc_pwrseq)
    542   1.1  jmcneill 		fdtbus_mmc_pwrseq_pre_power_on(sc->sc_pwrseq);
    543   1.1  jmcneill 
    544   1.1  jmcneill 	mesongx_mmc_bus_clock(sc, SDMMC_SDCLK_400K, false);
    545   1.1  jmcneill 	mesongx_mmc_host_reset(sc);
    546   1.1  jmcneill 	mesongx_mmc_bus_width(sc, 1);
    547   1.1  jmcneill 
    548   1.1  jmcneill 	if (sc->sc_pwrseq)
    549   1.1  jmcneill 		fdtbus_mmc_pwrseq_post_power_on(sc->sc_pwrseq);
    550   1.1  jmcneill 
    551   1.1  jmcneill 	if (of_getprop_uint32(sc->sc_phandle, "bus-width", &width) != 0)
    552   1.1  jmcneill 		width = 4;
    553   1.1  jmcneill 
    554   1.1  jmcneill 	memset(&saa, 0, sizeof(saa));
    555   1.1  jmcneill 	saa.saa_busname = "sdmmc";
    556   1.1  jmcneill 	saa.saa_sct = &mesongx_mmc_chip_functions;
    557   1.1  jmcneill 	saa.saa_sch = sc;
    558   1.1  jmcneill 	saa.saa_dmat = sc->sc_dmat;
    559   1.1  jmcneill 	saa.saa_clkmin = SDMMC_SDCLK_400K;
    560   1.1  jmcneill 	saa.saa_clkmax = sc->sc_max_frequency / 1000;
    561   1.4  jmcneill 	saa.saa_caps = SMC_CAPS_DMA;
    562   1.4  jmcneill #if notyet
    563   1.4  jmcneill 	/* XXX causes init to die when using root on eMMC with ODROID-C2 */
    564   1.4  jmcneill 	saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
    565   1.4  jmcneill #endif
    566   1.1  jmcneill 
    567   1.1  jmcneill 	sc->sc_host_ocr = MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
    568   1.1  jmcneill 
    569   1.1  jmcneill 	if (of_getprop_bool(sc->sc_phandle, "cap-sd-highspeed")) {
    570   1.1  jmcneill 		saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
    571   1.1  jmcneill 		sc->sc_host_ocr |= MMC_OCR_HCS;
    572   1.1  jmcneill 	}
    573   1.1  jmcneill 	if (of_getprop_bool(sc->sc_phandle, "cap-mmc-highspeed"))
    574   1.1  jmcneill 		saa.saa_caps |= SMC_CAPS_MMC_HIGHSPEED;
    575   1.1  jmcneill 
    576   1.5  jmcneill 	if (of_getprop_bool(sc->sc_phandle, "mmc-ddr-3_3v")) {
    577   1.5  jmcneill 		saa.saa_caps |= SMC_CAPS_MMC_DDR52;
    578   1.5  jmcneill 	}
    579   1.5  jmcneill 
    580   1.2  jmcneill 	if (of_getprop_bool(sc->sc_phandle, "mmc-ddr-1_8v")) {
    581   1.2  jmcneill 		saa.saa_caps |= SMC_CAPS_MMC_DDR52;
    582   1.2  jmcneill 		sc->sc_host_ocr |= MMC_OCR_1_65V_1_95V;
    583   1.2  jmcneill 	}
    584   1.2  jmcneill 	if (of_getprop_bool(sc->sc_phandle, "mmc-hs200-1_8v")) {
    585   1.2  jmcneill 		saa.saa_caps |= SMC_CAPS_MMC_HS200;
    586   1.2  jmcneill 		sc->sc_host_ocr |= MMC_OCR_1_65V_1_95V;
    587   1.1  jmcneill 	}
    588   1.1  jmcneill 
    589   1.1  jmcneill 	if (width == 4)
    590   1.1  jmcneill 		saa.saa_caps |= SMC_CAPS_4BIT_MODE;
    591   1.1  jmcneill 	if (width == 8)
    592   1.1  jmcneill 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
    593   1.1  jmcneill 
    594   1.1  jmcneill 	if (sc->sc_gpio_cd)
    595   1.1  jmcneill 		saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
    596   1.1  jmcneill 
    597   1.1  jmcneill 	sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
    598   1.1  jmcneill }
    599   1.1  jmcneill 
    600   1.1  jmcneill static int
    601   1.1  jmcneill mesongx_mmc_intr(void *priv)
    602   1.1  jmcneill {
    603   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = priv;
    604   1.1  jmcneill 	struct sdmmc_command *cmd;
    605   1.1  jmcneill 	int rv = 0;
    606   1.1  jmcneill 
    607   1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    608   1.1  jmcneill 
    609   1.1  jmcneill 	const uint32_t irq_en = MMC_READ(sc, SD_EMMC_IRQ_EN);
    610   1.1  jmcneill 	const uint32_t status = MMC_READ(sc, SD_EMMC_STATUS) & irq_en;
    611   1.1  jmcneill 
    612   1.1  jmcneill 	if ((status & STATUS_IRQ_SDIO) != 0) {
    613   1.1  jmcneill 		rv = 1;
    614   1.1  jmcneill 		sdmmc_card_intr(sc->sc_sdmmc_dev);
    615   1.1  jmcneill 	}
    616   1.1  jmcneill 
    617   1.1  jmcneill 	cmd = sc->sc_cmd;
    618   1.1  jmcneill 	if (cmd == NULL) {
    619   1.1  jmcneill 		device_printf(sc->sc_dev, "WARNING: IRQ with no active command, status %#x\n", status);
    620   1.1  jmcneill 		goto done;
    621   1.1  jmcneill 	}
    622   1.1  jmcneill 
    623   1.1  jmcneill 	if ((status & STATUS_TIMEOUT) != 0) {
    624   1.1  jmcneill 		rv = 1;
    625   1.1  jmcneill 		cmd->c_error = ETIMEDOUT;
    626   1.1  jmcneill 		goto done;
    627   1.1  jmcneill 	}
    628   1.1  jmcneill 
    629   1.1  jmcneill 	if ((status & STATUS_ERROR) != 0) {
    630   1.1  jmcneill 		rv = 1;
    631   1.1  jmcneill 		cmd->c_error = EIO;
    632   1.1  jmcneill 		goto done;
    633   1.1  jmcneill 	}
    634   1.1  jmcneill 
    635   1.1  jmcneill 	if ((status & STATUS_END_OF_CHAIN) != 0 && (cmd->c_flags & SCF_ITSDONE) == 0) {
    636   1.1  jmcneill 		rv = 1;
    637   1.1  jmcneill 		if ((cmd->c_flags & SCF_RSP_PRESENT) != 0) {
    638   1.1  jmcneill 			if (cmd->c_flags & SCF_RSP_136) {
    639   1.1  jmcneill 				cmd->c_resp[0] = MMC_READ(sc, SD_EMMC_CMD_RSP);
    640   1.1  jmcneill 				cmd->c_resp[1] = MMC_READ(sc, SD_EMMC_CMD_RSP1);
    641   1.1  jmcneill 				cmd->c_resp[2] = MMC_READ(sc, SD_EMMC_CMD_RSP2);
    642   1.1  jmcneill 				cmd->c_resp[3] = MMC_READ(sc, SD_EMMC_CMD_RSP3);
    643   1.1  jmcneill 				if (cmd->c_flags & SCF_RSP_CRC) {
    644   1.1  jmcneill 					cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
    645   1.1  jmcneill 					    (cmd->c_resp[1] << 24);
    646   1.1  jmcneill 					cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
    647   1.1  jmcneill 					    (cmd->c_resp[2] << 24);
    648   1.1  jmcneill 					cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
    649   1.1  jmcneill 					    (cmd->c_resp[3] << 24);
    650   1.1  jmcneill 					cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
    651   1.1  jmcneill 				}
    652   1.1  jmcneill 			} else {
    653   1.1  jmcneill 				cmd->c_resp[0] = MMC_READ(sc, SD_EMMC_CMD_RSP);
    654   1.1  jmcneill 			}
    655   1.1  jmcneill 		}
    656   1.1  jmcneill 		cmd->c_flags |= SCF_ITSDONE;
    657   1.1  jmcneill 		cmd->c_error = 0;
    658   1.1  jmcneill 		goto done;
    659   1.1  jmcneill 	}
    660   1.1  jmcneill 
    661   1.1  jmcneill done:
    662   1.1  jmcneill 	if (rv) {
    663   1.1  jmcneill 		cv_broadcast(&sc->sc_intr_cv);
    664   1.1  jmcneill 		MMC_WRITE(sc, SD_EMMC_STATUS, irq_en);
    665   1.1  jmcneill 	}
    666   1.1  jmcneill 
    667   1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    668   1.1  jmcneill 
    669   1.1  jmcneill 	return rv;
    670   1.1  jmcneill }
    671   1.1  jmcneill 
    672   1.1  jmcneill static int
    673   1.1  jmcneill mesongx_mmc_host_reset(sdmmc_chipset_handle_t sch)
    674   1.1  jmcneill {
    675   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = sch;
    676   1.1  jmcneill 	uint32_t val;
    677   1.1  jmcneill 
    678   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_START, 0);
    679   1.1  jmcneill 
    680   1.1  jmcneill 	val = MMC_READ(sc, SD_EMMC_CFG);
    681   1.1  jmcneill 	val &= ~CFG_RC_CC;
    682   1.1  jmcneill 	val |= __SHIFTIN(ilog2(16), CFG_RC_CC);
    683   1.1  jmcneill 	val |= CFG_SDCLK_ALWAYS_ON;
    684   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_CFG, val);
    685   1.1  jmcneill 
    686   1.1  jmcneill 	return 0;
    687   1.1  jmcneill }
    688   1.1  jmcneill 
    689   1.1  jmcneill static uint32_t
    690   1.1  jmcneill mesongx_mmc_host_ocr(sdmmc_chipset_handle_t sch)
    691   1.1  jmcneill {
    692   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = sch;
    693   1.1  jmcneill 
    694   1.1  jmcneill 	return sc->sc_host_ocr;
    695   1.1  jmcneill }
    696   1.1  jmcneill 
    697   1.1  jmcneill static int
    698   1.1  jmcneill mesongx_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
    699   1.1  jmcneill {
    700   1.1  jmcneill 	return 512;
    701   1.1  jmcneill }
    702   1.1  jmcneill 
    703   1.1  jmcneill static int
    704   1.1  jmcneill mesongx_mmc_card_detect(sdmmc_chipset_handle_t sch)
    705   1.1  jmcneill {
    706   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = sch;
    707   1.1  jmcneill 	int val;
    708   1.1  jmcneill 
    709   1.1  jmcneill 	if (sc->sc_non_removable || sc->sc_broken_cd) {
    710   1.1  jmcneill 		/*
    711   1.1  jmcneill 		 * Non-removable or broken card detect flag set in
    712   1.1  jmcneill 		 * DT, assume always present
    713   1.1  jmcneill 		 */
    714   1.1  jmcneill 		return 1;
    715   1.1  jmcneill 	} else if (sc->sc_gpio_cd != NULL) {
    716   1.1  jmcneill 		val = fdtbus_gpio_read(sc->sc_gpio_cd);
    717   1.1  jmcneill 		if (sc->sc_gpio_cd_inverted)
    718   1.1  jmcneill 			val = !val;
    719   1.1  jmcneill 		return val;
    720   1.1  jmcneill 	} else {
    721   1.1  jmcneill 		return 1;
    722   1.1  jmcneill 	}
    723   1.1  jmcneill }
    724   1.1  jmcneill 
    725   1.1  jmcneill static int
    726   1.1  jmcneill mesongx_mmc_write_protect(sdmmc_chipset_handle_t sch)
    727   1.1  jmcneill {
    728   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = sch;
    729   1.1  jmcneill 	int val;
    730   1.1  jmcneill 
    731   1.1  jmcneill 	if (sc->sc_gpio_wp != NULL) {
    732   1.1  jmcneill 		val = fdtbus_gpio_read(sc->sc_gpio_wp);
    733   1.1  jmcneill 		if (sc->sc_gpio_wp_inverted)
    734   1.1  jmcneill 			val = !val;
    735   1.1  jmcneill 		return val;
    736   1.1  jmcneill 	}
    737   1.1  jmcneill 
    738   1.1  jmcneill 	return 0;
    739   1.1  jmcneill }
    740   1.1  jmcneill 
    741   1.1  jmcneill static int
    742   1.1  jmcneill mesongx_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    743   1.1  jmcneill {
    744   1.1  jmcneill 	return 0;
    745   1.1  jmcneill }
    746   1.1  jmcneill 
    747   1.1  jmcneill static int
    748   1.1  jmcneill mesongx_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
    749   1.1  jmcneill {
    750   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = sch;
    751   1.1  jmcneill 
    752  1.14  jmcneill 	return mesongx_mmc_set_clock(sc, freq, ddr);
    753   1.1  jmcneill }
    754   1.1  jmcneill 
    755   1.1  jmcneill static int
    756   1.1  jmcneill mesongx_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
    757   1.1  jmcneill {
    758   1.1  jmcneill 	struct mesongx_mmc_softc *sc = sch;
    759   1.1  jmcneill 	uint32_t val;
    760   1.1  jmcneill 
    761   1.1  jmcneill 	val = MMC_READ(sc, SD_EMMC_CFG);
    762   1.1  jmcneill 	val &= ~CFG_BUS_WIDTH;
    763   1.1  jmcneill 
    764   1.1  jmcneill 	switch (width) {
    765   1.1  jmcneill 	case 1:
    766   1.1  jmcneill 		val |= __SHIFTIN(CFG_BUS_WIDTH_1, CFG_BUS_WIDTH);
    767   1.1  jmcneill 		break;
    768   1.1  jmcneill 	case 4:
    769   1.1  jmcneill 		val |= __SHIFTIN(CFG_BUS_WIDTH_4, CFG_BUS_WIDTH);
    770   1.1  jmcneill 		break;
    771   1.1  jmcneill 	case 8:
    772   1.1  jmcneill 		val |= __SHIFTIN(CFG_BUS_WIDTH_8, CFG_BUS_WIDTH);
    773   1.1  jmcneill 		break;
    774   1.1  jmcneill 	default:
    775   1.1  jmcneill 		return EINVAL;
    776   1.1  jmcneill 	}
    777   1.1  jmcneill 
    778   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_CFG, val);
    779   1.1  jmcneill 
    780   1.1  jmcneill 	return 0;
    781   1.1  jmcneill }
    782   1.1  jmcneill 
    783   1.1  jmcneill static int
    784   1.1  jmcneill mesongx_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
    785   1.1  jmcneill {
    786   1.1  jmcneill 	return -1;
    787   1.1  jmcneill }
    788   1.1  jmcneill 
    789   1.1  jmcneill static int
    790   1.1  jmcneill mesongx_mmc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
    791   1.1  jmcneill {
    792   1.1  jmcneill 	struct mesongx_mmc_softc *sc = sch;
    793   1.1  jmcneill 	u_int uvol;
    794   1.1  jmcneill 	int error;
    795   1.1  jmcneill 
    796   1.1  jmcneill 	if (sc->sc_reg_vqmmc == NULL)
    797   1.1  jmcneill 		return 0;
    798   1.1  jmcneill 
    799   1.1  jmcneill 	switch (signal_voltage) {
    800   1.1  jmcneill 	case SDMMC_SIGNAL_VOLTAGE_330:
    801   1.1  jmcneill 		uvol = 3300000;
    802   1.1  jmcneill 		break;
    803   1.1  jmcneill 	case SDMMC_SIGNAL_VOLTAGE_180:
    804   1.1  jmcneill 		uvol = 1800000;
    805   1.1  jmcneill 		break;
    806   1.1  jmcneill 	default:
    807   1.1  jmcneill 		return EINVAL;
    808   1.1  jmcneill 	}
    809   1.1  jmcneill 
    810   1.1  jmcneill 	error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
    811   1.1  jmcneill 	if (error != 0)
    812   1.1  jmcneill 		return 0;
    813   1.1  jmcneill 
    814   1.1  jmcneill 	error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
    815   1.1  jmcneill 	if (error != 0)
    816   1.1  jmcneill 		return error;
    817   1.1  jmcneill 
    818   1.1  jmcneill 	return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
    819   1.1  jmcneill }
    820   1.1  jmcneill 
    821   1.1  jmcneill static int
    822   1.1  jmcneill mesongx_mmc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
    823   1.1  jmcneill {
    824   1.1  jmcneill 	switch (timing) {
    825   1.1  jmcneill 	case SDMMC_TIMING_MMC_HS200:
    826   1.1  jmcneill 		break;
    827   1.1  jmcneill 	default:
    828   1.1  jmcneill 		return EINVAL;
    829   1.1  jmcneill 	}
    830   1.1  jmcneill 
    831   1.1  jmcneill 	return 0;
    832   1.1  jmcneill }
    833   1.1  jmcneill 
    834   1.1  jmcneill static int
    835   1.1  jmcneill mesongx_mmc_dma_prepare(struct mesongx_mmc_softc *sc, struct sdmmc_command *cmd, uint32_t cmdflags)
    836   1.1  jmcneill {
    837   1.1  jmcneill 	struct mesongx_mmc_desc *dma = sc->sc_desc_desc;
    838   1.1  jmcneill 	bus_dmamap_t map = cmd->c_dmamap;
    839   1.1  jmcneill 	u_int xferlen, blen, resid;
    840   1.1  jmcneill 	bus_size_t off;
    841   1.1  jmcneill 	uint32_t flags;
    842   1.1  jmcneill 	int desc, seg;
    843   1.1  jmcneill 
    844   1.1  jmcneill 	if (cmd->c_blklen > 512) {
    845   1.1  jmcneill 		device_printf(sc->sc_dev, "block length %d not supported\n", cmd->c_blklen);
    846   1.1  jmcneill 		return EINVAL;
    847   1.1  jmcneill 	}
    848   1.1  jmcneill 
    849   1.1  jmcneill 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    850   1.1  jmcneill 		if (map->dm_segs[seg].ds_len % cmd->c_blklen != 0) {
    851   1.1  jmcneill 			/* Force DMA bounce for unaligned transfers */
    852   1.1  jmcneill 			map = NULL;
    853   1.1  jmcneill 			break;
    854   1.1  jmcneill 		}
    855   1.1  jmcneill 	}
    856   1.1  jmcneill 
    857   1.1  jmcneill 	if (map == NULL) {
    858   1.1  jmcneill 		map = sc->sc_dmabounce_map;
    859   1.1  jmcneill 		cmd->c_flags |= SCF_NEED_BOUNCE;
    860   1.1  jmcneill 
    861   1.1  jmcneill 		if ((cmd->c_flags & SCF_CMD_READ) != 0) {
    862   1.1  jmcneill 			memset(sc->sc_dmabounce_buf, 0, cmd->c_datalen);
    863   1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
    864   1.1  jmcneill 			    0, cmd->c_datalen, BUS_DMASYNC_PREREAD);
    865   1.1  jmcneill 		} else {
    866   1.1  jmcneill 			memcpy(sc->sc_dmabounce_buf, cmd->c_data, cmd->c_datalen);
    867   1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
    868   1.1  jmcneill 			    0, cmd->c_datalen, BUS_DMASYNC_PREWRITE);
    869   1.1  jmcneill 		}
    870   1.1  jmcneill 	}
    871   1.1  jmcneill 
    872   1.1  jmcneill 	desc = 0;
    873   1.1  jmcneill 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    874   1.1  jmcneill 		bus_addr_t paddr = map->dm_segs[seg].ds_addr;
    875   1.1  jmcneill 		bus_size_t len = map->dm_segs[seg].ds_len;
    876   1.1  jmcneill 		resid = uimin(len, cmd->c_resid);
    877   1.1  jmcneill 		off = 0;
    878   1.1  jmcneill 		while (resid > 0) {
    879   1.1  jmcneill 			if (desc == sc->sc_desc_ndesc)
    880   1.1  jmcneill 				break;
    881   1.1  jmcneill 
    882   1.1  jmcneill 			flags = cmdflags;
    883   1.1  jmcneill 
    884   1.1  jmcneill 			if (resid >= cmd->c_blklen) {
    885   1.1  jmcneill 				xferlen = (resid / cmd->c_blklen) * cmd->c_blklen;
    886   1.1  jmcneill 				blen = xferlen / cmd->c_blklen;
    887   1.1  jmcneill 				flags |= MESONGX_MMC_FLAGS_BLOCK_MODE;
    888   1.1  jmcneill 			} else {
    889   1.1  jmcneill 				blen = xferlen = resid;
    890   1.1  jmcneill 			}
    891   1.1  jmcneill 			KASSERT(xferlen > 0);
    892   1.1  jmcneill 			KASSERT(blen <= 512);
    893   1.1  jmcneill 
    894   1.1  jmcneill 			flags |= __SHIFTIN(blen % 512, MESONGX_MMC_FLAGS_LENGTH);
    895   1.1  jmcneill 			if (desc > 0)
    896   1.1  jmcneill 				flags |= MESONGX_MMC_FLAGS_NO_CMD;
    897   1.1  jmcneill 			if (cmd->c_resid == xferlen)
    898   1.1  jmcneill 				flags |= MESONGX_MMC_FLAGS_END_OF_CHAIN;
    899   1.1  jmcneill 
    900   1.1  jmcneill 			dma[desc].flags = htole32(flags);
    901   1.1  jmcneill 			dma[desc].arg = htole32(cmd->c_arg);
    902   1.1  jmcneill 			dma[desc].data = htole32(paddr + off);
    903   1.1  jmcneill 			dma[desc].resp = 0;
    904   1.1  jmcneill 
    905   1.1  jmcneill 			cmd->c_resid -= xferlen;
    906   1.1  jmcneill 			resid -= xferlen;
    907   1.1  jmcneill 			off += xferlen;
    908   1.1  jmcneill 
    909   1.1  jmcneill 			if (cmd->c_resid == 0)
    910   1.1  jmcneill 				break;
    911   1.1  jmcneill 
    912   1.1  jmcneill 			++desc;
    913   1.1  jmcneill 		}
    914   1.1  jmcneill 	}
    915   1.1  jmcneill 	if (desc == sc->sc_desc_ndesc) {
    916   1.1  jmcneill 		device_printf(sc->sc_dev,
    917   1.1  jmcneill 		    "not enough descriptors for %d byte transfer (%d segs)!\n",
    918   1.1  jmcneill 		    cmd->c_datalen, map->dm_nsegs);
    919   1.1  jmcneill 		return EIO;
    920   1.1  jmcneill 	}
    921   1.1  jmcneill 
    922   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->sc_desc_map, 0,
    923   1.1  jmcneill 	    sc->sc_desc_size, BUS_DMASYNC_PREWRITE);
    924   1.1  jmcneill 
    925   1.1  jmcneill 	return 0;
    926   1.1  jmcneill }
    927   1.1  jmcneill 
    928   1.1  jmcneill static void
    929   1.1  jmcneill mesongx_mmc_dma_complete(struct mesongx_mmc_softc *sc, struct sdmmc_command *cmd)
    930   1.1  jmcneill {
    931   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->sc_desc_map, 0,
    932   1.1  jmcneill 	    sc->sc_desc_size, BUS_DMASYNC_POSTWRITE);
    933   1.1  jmcneill 
    934   1.1  jmcneill 	if ((cmd->c_flags & SCF_NEED_BOUNCE) != 0) {
    935   1.1  jmcneill 		if ((cmd->c_flags & SCF_CMD_READ) != 0) {
    936   1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
    937   1.1  jmcneill 			    0, cmd->c_datalen, BUS_DMASYNC_POSTREAD);
    938   1.1  jmcneill 			memcpy(cmd->c_data, sc->sc_dmabounce_buf, cmd->c_datalen);
    939   1.1  jmcneill 		} else {
    940   1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
    941   1.1  jmcneill 			    0, cmd->c_datalen, BUS_DMASYNC_POSTWRITE);
    942   1.1  jmcneill 		}
    943   1.1  jmcneill 	}
    944   1.1  jmcneill }
    945   1.1  jmcneill 
    946   1.1  jmcneill static void
    947   1.1  jmcneill mesongx_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
    948   1.1  jmcneill {
    949   1.1  jmcneill 	struct mesongx_mmc_softc *sc = sch;
    950   1.1  jmcneill 	uint32_t cmdflags, val;
    951   1.1  jmcneill 	int error;
    952   1.1  jmcneill 
    953   1.1  jmcneill 	const uint32_t irq_mask = IRQ_EN_RESP_STATUS |
    954   1.1  jmcneill 				  IRQ_EN_END_OF_CHAIN |
    955   1.1  jmcneill 				  IRQ_EN_DESC_TIMEOUT |
    956   1.1  jmcneill 				  IRQ_EN_RESP_TIMEOUT |
    957   1.1  jmcneill 				  IRQ_EN_RESP_ERR |
    958   1.1  jmcneill 				  IRQ_EN_DESC_ERR |
    959   1.1  jmcneill 				  IRQ_EN_TXD_ERR |
    960   1.1  jmcneill 				  IRQ_EN_RXD_ERR;
    961   1.1  jmcneill 
    962   1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    963   1.1  jmcneill 
    964   1.1  jmcneill 	while (sc->sc_cmd != NULL)
    965   1.1  jmcneill 		cv_wait(&sc->sc_intr_cv, &sc->sc_intr_lock);
    966   1.1  jmcneill 	sc->sc_cmd = cmd;
    967   1.1  jmcneill 
    968   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_START, 0);
    969   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_STATUS, MMC_READ(sc, SD_EMMC_STATUS));
    970   1.1  jmcneill 
    971   1.1  jmcneill 	val = MMC_READ(sc, SD_EMMC_IRQ_EN);
    972   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_IRQ_EN, val | irq_mask);
    973   1.1  jmcneill 
    974   1.1  jmcneill 	cmdflags = MESONGX_MMC_FLAGS_OWNER;
    975   1.1  jmcneill 	cmdflags |= __SHIFTIN(12, MESONGX_MMC_FLAGS_TIMEOUT);	/* 2^12 = 4096 ms timeout */
    976   1.1  jmcneill 	cmdflags |= __SHIFTIN(cmd->c_opcode, MESONGX_MMC_FLAGS_CMD_INDEX);
    977   1.1  jmcneill 
    978   1.1  jmcneill 	if ((cmd->c_flags & SCF_RSP_PRESENT) == 0) {
    979   1.1  jmcneill 		cmdflags |= MESONGX_MMC_FLAGS_NO_RESP;
    980   1.1  jmcneill 	} else {
    981   1.1  jmcneill 		cmdflags |= MESONGX_MMC_FLAGS_RESP_NUM;
    982   1.1  jmcneill 		if ((cmd->c_flags & SCF_RSP_136) != 0)
    983   1.1  jmcneill 			cmdflags |= MESONGX_MMC_FLAGS_RESP_128;
    984   1.1  jmcneill 		if ((cmd->c_flags & SCF_RSP_CRC) == 0)
    985   1.1  jmcneill 			cmdflags |= MESONGX_MMC_FLAGS_RESP_NOCRC;
    986   1.3  jmcneill 		if ((cmd->c_flags & SCF_RSP_MASK) == SCF_RSP_R1B)
    987   1.1  jmcneill 			cmdflags |= MESONGX_MMC_FLAGS_R1B;
    988   1.1  jmcneill 	}
    989   1.1  jmcneill 
    990   1.1  jmcneill 	if (cmd->c_datalen > 0) {
    991   1.1  jmcneill 		cmdflags |= MESONGX_MMC_FLAGS_DATA_IO;
    992   1.1  jmcneill 		if ((cmd->c_flags & SCF_CMD_READ) == 0)
    993   1.1  jmcneill 			cmdflags |= MESONGX_MMC_FLAGS_DATA_WR;
    994   1.1  jmcneill 
    995   1.1  jmcneill 		val = MMC_READ(sc, SD_EMMC_CFG);
    996   1.1  jmcneill 		val &= ~CFG_BL_LEN;
    997   1.1  jmcneill 		val |= __SHIFTIN(ilog2(cmd->c_blklen), CFG_BL_LEN);
    998   1.1  jmcneill 		MMC_WRITE(sc, SD_EMMC_CFG, val);
    999   1.1  jmcneill 
   1000   1.1  jmcneill 		cmd->c_resid = cmd->c_datalen;
   1001   1.1  jmcneill 		cmd->c_error = mesongx_mmc_dma_prepare(sc, cmd, cmdflags);
   1002   1.1  jmcneill 		if (cmd->c_error != 0)
   1003   1.1  jmcneill 			goto done;
   1004   1.1  jmcneill 
   1005   1.1  jmcneill 		const bus_addr_t desc_paddr = sc->sc_desc_map->dm_segs[0].ds_addr;
   1006   1.1  jmcneill 		MMC_WRITE(sc, SD_EMMC_START, desc_paddr | START_DESC_BUSY);	/* starts transfer */
   1007   1.1  jmcneill 	} else {
   1008   1.1  jmcneill 		MMC_WRITE(sc, SD_EMMC_CMD_CFG, cmdflags | MESONGX_MMC_FLAGS_END_OF_CHAIN);
   1009   1.1  jmcneill 		MMC_WRITE(sc, SD_EMMC_CMD_DAT, 0);
   1010   1.1  jmcneill 		MMC_WRITE(sc, SD_EMMC_CMD_ARG, cmd->c_arg);			/* starts transfer */
   1011   1.1  jmcneill 	}
   1012   1.1  jmcneill 
   1013   1.1  jmcneill 	struct bintime timeout = { .sec = 5, .frac = 0 };
   1014   1.1  jmcneill 	const struct bintime epsilon = { .sec = 1, .frac = 0 };
   1015   1.1  jmcneill 
   1016   1.1  jmcneill 	while ((cmd->c_flags & SCF_ITSDONE) == 0 && cmd->c_error == 0) {
   1017   1.1  jmcneill 		error = cv_timedwaitbt(&sc->sc_intr_cv, &sc->sc_intr_lock, &timeout, &epsilon);
   1018   1.1  jmcneill 		if (error != 0) {
   1019   1.1  jmcneill 			cmd->c_error = error;
   1020   1.1  jmcneill 			goto done;
   1021   1.1  jmcneill 		}
   1022   1.1  jmcneill 	}
   1023   1.1  jmcneill 
   1024   1.1  jmcneill 	if (cmd->c_error == 0 && cmd->c_datalen > 0)
   1025   1.1  jmcneill 		mesongx_mmc_dma_complete(sc, cmd);
   1026   1.1  jmcneill 
   1027   1.1  jmcneill done:
   1028   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_START, 0);
   1029   1.1  jmcneill 
   1030   1.1  jmcneill 	val = MMC_READ(sc, SD_EMMC_IRQ_EN);
   1031   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_IRQ_EN, val & ~irq_mask);
   1032   1.1  jmcneill 
   1033   1.1  jmcneill 	sc->sc_cmd = NULL;
   1034   1.1  jmcneill 	cv_broadcast(&sc->sc_intr_cv);
   1035   1.1  jmcneill 
   1036   1.1  jmcneill #ifdef MESONGX_MMC_DEBUG
   1037   1.1  jmcneill 	if (cmd->c_error != 0) {
   1038   1.1  jmcneill 		for (u_int reg = 0x00; reg < 0x100; reg += 0x10) {
   1039   1.1  jmcneill 			device_printf(sc->sc_dev, "      %02x: %08x %08x %08x %08x\n", reg,
   1040   1.1  jmcneill 			    MMC_READ(sc, reg + 0),
   1041   1.1  jmcneill 			    MMC_READ(sc, reg + 4),
   1042   1.1  jmcneill 			    MMC_READ(sc, reg + 8),
   1043   1.1  jmcneill 			    MMC_READ(sc, reg + 12));
   1044   1.1  jmcneill 		}
   1045   1.1  jmcneill 	}
   1046   1.1  jmcneill #endif
   1047   1.1  jmcneill 
   1048   1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
   1049   1.1  jmcneill }
   1050   1.1  jmcneill 
   1051   1.1  jmcneill static void
   1052   1.1  jmcneill mesongx_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
   1053   1.1  jmcneill {
   1054   1.1  jmcneill 	struct mesongx_mmc_softc * const sc = sch;
   1055   1.1  jmcneill 	uint32_t val;
   1056   1.1  jmcneill 
   1057   1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
   1058   1.1  jmcneill 
   1059   1.1  jmcneill 	val = MMC_READ(sc, SD_EMMC_IRQ_EN);
   1060   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_IRQ_EN, val | IRQ_EN_IRQ_SDIO);
   1061   1.1  jmcneill 
   1062   1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
   1063   1.1  jmcneill }
   1064   1.1  jmcneill 
   1065   1.1  jmcneill static void
   1066   1.1  jmcneill mesongx_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
   1067   1.1  jmcneill {
   1068   1.1  jmcneill 	struct mesongx_mmc_softc *sc = sch;
   1069   1.1  jmcneill 
   1070   1.1  jmcneill 	MMC_WRITE(sc, SD_EMMC_STATUS, STATUS_IRQ_SDIO);
   1071   1.1  jmcneill }
   1072