Home | History | Annotate | Line # | Download | only in sdmmc
ld_sdmmc.c revision 1.24
      1 /*	$NetBSD: ld_sdmmc.c,v 1.24 2017/01/07 14:49:53 kiyohara Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 KIYOHARA Takashi
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.24 2017/01/07 14:49:53 kiyohara Exp $");
     32 
     33 #ifdef _KERNEL_OPT
     34 #include "opt_sdmmc.h"
     35 #endif
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/buf.h>
     42 #include <sys/bufq.h>
     43 #include <sys/bus.h>
     44 #include <sys/endian.h>
     45 #include <sys/dkio.h>
     46 #include <sys/disk.h>
     47 #include <sys/kthread.h>
     48 #include <sys/module.h>
     49 
     50 #include <dev/ldvar.h>
     51 
     52 #include <dev/sdmmc/sdmmcvar.h>
     53 
     54 #include "ioconf.h"
     55 
     56 #ifdef LD_SDMMC_DEBUG
     57 #define DPRINTF(s)	printf s
     58 #else
     59 #define DPRINTF(s)	/**/
     60 #endif
     61 
     62 #define	LD_SDMMC_IORETRIES	5	/* number of retries before giving up */
     63 #define	RECOVERYTIME		hz/2	/* time to wait before retrying a cmd */
     64 
     65 struct ld_sdmmc_softc;
     66 
     67 struct ld_sdmmc_task {
     68 	struct sdmmc_task task;
     69 
     70 	struct ld_sdmmc_softc *task_sc;
     71 	struct buf *task_bp;
     72 	int task_retries; /* number of xfer retry */
     73 	struct callout task_restart_ch;
     74 };
     75 
     76 struct ld_sdmmc_softc {
     77 	struct ld_softc sc_ld;
     78 	int sc_hwunit;
     79 
     80 	struct sdmmc_function *sc_sf;
     81 #define LD_SDMMC_MAXQUEUECNT 4
     82 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
     83 	TAILQ_HEAD(, sdmmc_task) sc_freeq;
     84 };
     85 
     86 static int ld_sdmmc_match(device_t, cfdata_t, void *);
     87 static void ld_sdmmc_attach(device_t, device_t, void *);
     88 static int ld_sdmmc_detach(device_t, int);
     89 
     90 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
     91 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
     92 static void ld_sdmmc_restart(void *);
     93 
     94 static void ld_sdmmc_doattach(void *);
     95 static void ld_sdmmc_dobio(void *);
     96 
     97 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
     98     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
     99 
    100 
    101 /* ARGSUSED */
    102 static int
    103 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
    104 {
    105 	struct sdmmc_softc *sdmsc = device_private(parent);
    106 
    107 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
    108 		return 1;
    109 	return 0;
    110 }
    111 
    112 /* ARGSUSED */
    113 static void
    114 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct ld_sdmmc_softc *sc = device_private(self);
    117 	struct sdmmc_attach_args *sa = aux;
    118 	struct ld_softc *ld = &sc->sc_ld;
    119 	struct ld_sdmmc_task *task;
    120 	struct lwp *lwp;
    121 	int i;
    122 
    123 	ld->sc_dv = self;
    124 
    125 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
    126 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
    127 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
    128 	aprint_naive("\n");
    129 
    130 	TAILQ_INIT(&sc->sc_freeq);
    131 	for (i = 0; i < __arraycount(sc->sc_task); i++) {
    132 		task = &sc->sc_task[i];
    133 		task->task_sc = sc;
    134 		callout_init(&task->task_restart_ch, 0);
    135 		TAILQ_INSERT_TAIL(&sc->sc_freeq, &task->task, next);
    136 	}
    137 
    138 	sc->sc_hwunit = 0;	/* always 0? */
    139 	sc->sc_sf = sa->sf;
    140 
    141 	ld->sc_flags = LDF_ENABLED;
    142 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
    143 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
    144 	ld->sc_maxxfer = MAXPHYS;
    145 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
    146 	ld->sc_dump = ld_sdmmc_dump;
    147 	ld->sc_start = ld_sdmmc_start;
    148 
    149 	/*
    150 	 * It is avoided that the error occurs when the card attaches it,
    151 	 * when wedge is supported.
    152 	 */
    153 	config_pending_incr(self);
    154 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    155 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
    156 		aprint_error_dev(self, "couldn't create thread\n");
    157 	}
    158 }
    159 
    160 static void
    161 ld_sdmmc_doattach(void *arg)
    162 {
    163 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
    164 	struct ld_softc *ld = &sc->sc_ld;
    165 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
    166 
    167 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    168 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
    169 	if (ssc->sc_transfer_mode != NULL)
    170 		aprint_normal(" %s,", ssc->sc_transfer_mode);
    171 	if ((ssc->sc_busclk / 1000) != 0)
    172 		aprint_normal(" %u.%03u MHz\n",
    173 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
    174 	else
    175 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
    176 	config_pending_decr(ld->sc_dv);
    177 	kthread_exit(0);
    178 }
    179 
    180 static int
    181 ld_sdmmc_detach(device_t dev, int flags)
    182 {
    183 	struct ld_sdmmc_softc *sc = device_private(dev);
    184 	struct ld_softc *ld = &sc->sc_ld;
    185 	int rv, i;
    186 
    187 	if ((rv = ldbegindetach(ld, flags)) != 0)
    188 		return rv;
    189 	ldenddetach(ld);
    190 
    191 	for (i = 0; i < __arraycount(sc->sc_task); i++)
    192 		callout_destroy(&sc->sc_task[i].task_restart_ch);
    193 
    194 	return 0;
    195 }
    196 
    197 static int
    198 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
    199 {
    200 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    201 	struct ld_sdmmc_task *task = (void *)TAILQ_FIRST(&sc->sc_freeq);
    202 
    203 	TAILQ_REMOVE(&sc->sc_freeq, &task->task, next);
    204 
    205 	task->task_bp = bp;
    206 	task->task_retries = 0;
    207 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
    208 
    209 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    210 
    211 	return 0;
    212 }
    213 
    214 static void
    215 ld_sdmmc_restart(void *arg)
    216 {
    217 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    218 	struct ld_sdmmc_softc *sc = task->task_sc;
    219 	struct buf *bp = task->task_bp;
    220 
    221 	bp->b_resid = bp->b_bcount;
    222 
    223 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    224 }
    225 
    226 static void
    227 ld_sdmmc_dobio(void *arg)
    228 {
    229 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    230 	struct ld_sdmmc_softc *sc = task->task_sc;
    231 	struct buf *bp = task->task_bp;
    232 	int error;
    233 
    234 	/*
    235 	 * I/O operation
    236 	 */
    237 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
    238 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
    239 	    bp->b_rawblkno, bp->b_bcount));
    240 
    241 	/* is everything done in terms of blocks? */
    242 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
    243 		/* trying to read or write past end of device */
    244 		aprint_error_dev(sc->sc_ld.sc_dv,
    245 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
    246 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
    247 		bp->b_error = EINVAL;
    248 		bp->b_resid = bp->b_bcount;
    249 		lddone(&sc->sc_ld, bp);
    250 		return;
    251 	}
    252 
    253 	if (bp->b_flags & B_READ)
    254 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
    255 		    bp->b_data, bp->b_bcount);
    256 	else
    257 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
    258 		    bp->b_data, bp->b_bcount);
    259 	if (error) {
    260 		if (task->task_retries < LD_SDMMC_IORETRIES) {
    261 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
    262 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    263 
    264 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    265 				dksc->sc_dkdev.dk_label);
    266 			printf(", retrying\n");
    267 			task->task_retries++;
    268 			callout_reset(&task->task_restart_ch, RECOVERYTIME,
    269 			    ld_sdmmc_restart, task);
    270 			return;
    271 		}
    272 		bp->b_error = error;
    273 		bp->b_resid = bp->b_bcount;
    274 	} else {
    275 		bp->b_resid = 0;
    276 	}
    277 
    278 	TAILQ_INSERT_TAIL(&sc->sc_freeq, &task->task, next);
    279 
    280 	lddone(&sc->sc_ld, bp);
    281 }
    282 
    283 static int
    284 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    285 {
    286 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    287 
    288 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
    289 	    blkcnt * ld->sc_secsize);
    290 }
    291 
    292 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
    293 
    294 #ifdef _MODULE
    295 /*
    296  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    297  * XXX it will be defined in the common-code module
    298  */
    299 #undef  CFDRIVER_DECL
    300 #define CFDRIVER_DECL(name, class, attr)
    301 #include "ioconf.c"
    302 #endif
    303 
    304 static int
    305 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
    306 {
    307 #ifdef _MODULE
    308 	/*
    309 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    310 	 * the cfdrivers are attached already.
    311 	 */
    312 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    313 #endif
    314 	int error = 0;
    315 
    316 #ifdef _MODULE
    317 	switch (cmd) {
    318 	case MODULE_CMD_INIT:
    319 		error = config_init_component(no_cfdriver_vec,
    320 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    321         	break;
    322 	case MODULE_CMD_FINI:
    323 		error = config_fini_component(no_cfdriver_vec,
    324 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    325 		break;
    326 	default:
    327 		error = ENOTTY;
    328 		break;
    329 	}
    330 #endif
    331 
    332 	return error;
    333 }
    334