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