Home | History | Annotate | Line # | Download | only in sdmmc
ld_sdmmc.c revision 1.25
      1  1.25    martin /*	$NetBSD: ld_sdmmc.c,v 1.25 2017/01/07 16:24:40 martin 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.25    martin __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.25 2017/01/07 16:24:40 martin 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.1    nonaka 
     52   1.1    nonaka #include <dev/ldvar.h>
     53   1.1    nonaka 
     54   1.1    nonaka #include <dev/sdmmc/sdmmcvar.h>
     55   1.1    nonaka 
     56  1.23  pgoyette #include "ioconf.h"
     57  1.23  pgoyette 
     58  1.14  jmcneill #ifdef LD_SDMMC_DEBUG
     59   1.1    nonaka #define DPRINTF(s)	printf s
     60   1.1    nonaka #else
     61   1.1    nonaka #define DPRINTF(s)	/**/
     62   1.1    nonaka #endif
     63   1.1    nonaka 
     64  1.24  kiyohara #define	LD_SDMMC_IORETRIES	5	/* number of retries before giving up */
     65  1.24  kiyohara #define	RECOVERYTIME		hz/2	/* time to wait before retrying a cmd */
     66  1.24  kiyohara 
     67   1.1    nonaka struct ld_sdmmc_softc;
     68   1.1    nonaka 
     69   1.1    nonaka struct ld_sdmmc_task {
     70   1.1    nonaka 	struct sdmmc_task task;
     71   1.1    nonaka 
     72   1.1    nonaka 	struct ld_sdmmc_softc *task_sc;
     73   1.1    nonaka 	struct buf *task_bp;
     74  1.24  kiyohara 	int task_retries; /* number of xfer retry */
     75  1.24  kiyohara 	struct callout task_restart_ch;
     76   1.1    nonaka };
     77   1.1    nonaka 
     78   1.1    nonaka struct ld_sdmmc_softc {
     79   1.1    nonaka 	struct ld_softc sc_ld;
     80   1.1    nonaka 	int sc_hwunit;
     81   1.1    nonaka 
     82   1.1    nonaka 	struct sdmmc_function *sc_sf;
     83  1.20   mlelstv #define LD_SDMMC_MAXQUEUECNT 4
     84  1.20   mlelstv 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
     85  1.24  kiyohara 	TAILQ_HEAD(, sdmmc_task) sc_freeq;
     86   1.1    nonaka };
     87   1.1    nonaka 
     88   1.2    cegger static int ld_sdmmc_match(device_t, cfdata_t, void *);
     89   1.1    nonaka static void ld_sdmmc_attach(device_t, device_t, void *);
     90   1.1    nonaka static int ld_sdmmc_detach(device_t, int);
     91   1.1    nonaka 
     92   1.1    nonaka static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
     93   1.1    nonaka static int ld_sdmmc_start(struct ld_softc *, struct buf *);
     94  1.24  kiyohara static void ld_sdmmc_restart(void *);
     95   1.1    nonaka 
     96   1.3    nonaka static void ld_sdmmc_doattach(void *);
     97   1.1    nonaka static void ld_sdmmc_dobio(void *);
     98   1.1    nonaka 
     99   1.1    nonaka CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
    100   1.1    nonaka     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
    101   1.1    nonaka 
    102   1.1    nonaka 
    103   1.1    nonaka /* ARGSUSED */
    104   1.1    nonaka static int
    105   1.2    cegger ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
    106   1.1    nonaka {
    107   1.1    nonaka 	struct sdmmc_softc *sdmsc = device_private(parent);
    108   1.1    nonaka 
    109   1.1    nonaka 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
    110   1.1    nonaka 		return 1;
    111   1.1    nonaka 	return 0;
    112   1.1    nonaka }
    113   1.1    nonaka 
    114   1.1    nonaka /* ARGSUSED */
    115   1.1    nonaka static void
    116   1.1    nonaka ld_sdmmc_attach(device_t parent, device_t self, void *aux)
    117   1.1    nonaka {
    118   1.1    nonaka 	struct ld_sdmmc_softc *sc = device_private(self);
    119   1.1    nonaka 	struct sdmmc_attach_args *sa = aux;
    120   1.1    nonaka 	struct ld_softc *ld = &sc->sc_ld;
    121  1.24  kiyohara 	struct ld_sdmmc_task *task;
    122   1.3    nonaka 	struct lwp *lwp;
    123  1.24  kiyohara 	int i;
    124   1.1    nonaka 
    125   1.1    nonaka 	ld->sc_dv = self;
    126   1.1    nonaka 
    127  1.11  jakllsch 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
    128  1.11  jakllsch 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
    129  1.11  jakllsch 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
    130   1.1    nonaka 	aprint_naive("\n");
    131   1.1    nonaka 
    132  1.24  kiyohara 	TAILQ_INIT(&sc->sc_freeq);
    133  1.24  kiyohara 	for (i = 0; i < __arraycount(sc->sc_task); i++) {
    134  1.24  kiyohara 		task = &sc->sc_task[i];
    135  1.24  kiyohara 		task->task_sc = sc;
    136  1.24  kiyohara 		callout_init(&task->task_restart_ch, 0);
    137  1.24  kiyohara 		TAILQ_INSERT_TAIL(&sc->sc_freeq, &task->task, next);
    138  1.24  kiyohara 	}
    139  1.20   mlelstv 
    140   1.1    nonaka 	sc->sc_hwunit = 0;	/* always 0? */
    141   1.1    nonaka 	sc->sc_sf = sa->sf;
    142   1.1    nonaka 
    143   1.1    nonaka 	ld->sc_flags = LDF_ENABLED;
    144   1.1    nonaka 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
    145   1.4    nonaka 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
    146   1.1    nonaka 	ld->sc_maxxfer = MAXPHYS;
    147  1.20   mlelstv 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
    148   1.1    nonaka 	ld->sc_dump = ld_sdmmc_dump;
    149   1.1    nonaka 	ld->sc_start = ld_sdmmc_start;
    150   1.1    nonaka 
    151   1.3    nonaka 	/*
    152  1.11  jakllsch 	 * It is avoided that the error occurs when the card attaches it,
    153   1.3    nonaka 	 * when wedge is supported.
    154   1.3    nonaka 	 */
    155  1.12  christos 	config_pending_incr(self);
    156   1.3    nonaka 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    157   1.3    nonaka 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
    158   1.3    nonaka 		aprint_error_dev(self, "couldn't create thread\n");
    159   1.3    nonaka 	}
    160   1.3    nonaka }
    161   1.3    nonaka 
    162   1.3    nonaka static void
    163   1.3    nonaka ld_sdmmc_doattach(void *arg)
    164   1.3    nonaka {
    165   1.3    nonaka 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
    166   1.3    nonaka 	struct ld_softc *ld = &sc->sc_ld;
    167   1.6  kiyohara 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
    168   1.3    nonaka 
    169  1.22  jdolecek 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    170  1.19  jmcneill 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
    171  1.19  jmcneill 	if (ssc->sc_transfer_mode != NULL)
    172  1.19  jmcneill 		aprint_normal(" %s,", ssc->sc_transfer_mode);
    173   1.6  kiyohara 	if ((ssc->sc_busclk / 1000) != 0)
    174   1.6  kiyohara 		aprint_normal(" %u.%03u MHz\n",
    175   1.6  kiyohara 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
    176   1.6  kiyohara 	else
    177   1.6  kiyohara 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
    178  1.12  christos 	config_pending_decr(ld->sc_dv);
    179   1.3    nonaka 	kthread_exit(0);
    180   1.1    nonaka }
    181   1.1    nonaka 
    182   1.1    nonaka static int
    183   1.1    nonaka ld_sdmmc_detach(device_t dev, int flags)
    184   1.1    nonaka {
    185   1.1    nonaka 	struct ld_sdmmc_softc *sc = device_private(dev);
    186   1.1    nonaka 	struct ld_softc *ld = &sc->sc_ld;
    187  1.24  kiyohara 	int rv, i;
    188   1.1    nonaka 
    189   1.1    nonaka 	if ((rv = ldbegindetach(ld, flags)) != 0)
    190   1.1    nonaka 		return rv;
    191   1.1    nonaka 	ldenddetach(ld);
    192   1.1    nonaka 
    193  1.24  kiyohara 	for (i = 0; i < __arraycount(sc->sc_task); i++)
    194  1.24  kiyohara 		callout_destroy(&sc->sc_task[i].task_restart_ch);
    195  1.24  kiyohara 
    196   1.1    nonaka 	return 0;
    197   1.1    nonaka }
    198   1.1    nonaka 
    199   1.1    nonaka static int
    200   1.1    nonaka ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
    201   1.1    nonaka {
    202   1.1    nonaka 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    203  1.24  kiyohara 	struct ld_sdmmc_task *task = (void *)TAILQ_FIRST(&sc->sc_freeq);
    204  1.20   mlelstv 
    205  1.24  kiyohara 	TAILQ_REMOVE(&sc->sc_freeq, &task->task, next);
    206   1.1    nonaka 
    207   1.1    nonaka 	task->task_bp = bp;
    208  1.24  kiyohara 	task->task_retries = 0;
    209   1.1    nonaka 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
    210   1.1    nonaka 
    211   1.1    nonaka 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    212   1.1    nonaka 
    213   1.1    nonaka 	return 0;
    214   1.1    nonaka }
    215   1.1    nonaka 
    216   1.1    nonaka static void
    217  1.24  kiyohara ld_sdmmc_restart(void *arg)
    218  1.24  kiyohara {
    219  1.24  kiyohara 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    220  1.24  kiyohara 	struct ld_sdmmc_softc *sc = task->task_sc;
    221  1.24  kiyohara 	struct buf *bp = task->task_bp;
    222  1.24  kiyohara 
    223  1.24  kiyohara 	bp->b_resid = bp->b_bcount;
    224  1.24  kiyohara 
    225  1.24  kiyohara 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
    226  1.24  kiyohara }
    227  1.24  kiyohara 
    228  1.24  kiyohara static void
    229   1.1    nonaka ld_sdmmc_dobio(void *arg)
    230   1.1    nonaka {
    231   1.1    nonaka 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
    232   1.1    nonaka 	struct ld_sdmmc_softc *sc = task->task_sc;
    233   1.1    nonaka 	struct buf *bp = task->task_bp;
    234  1.18   mlelstv 	int error;
    235   1.1    nonaka 
    236   1.1    nonaka 	/*
    237   1.1    nonaka 	 * I/O operation
    238   1.1    nonaka 	 */
    239   1.1    nonaka 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
    240   1.1    nonaka 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
    241   1.1    nonaka 	    bp->b_rawblkno, bp->b_bcount));
    242   1.1    nonaka 
    243   1.1    nonaka 	/* is everything done in terms of blocks? */
    244   1.1    nonaka 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
    245   1.1    nonaka 		/* trying to read or write past end of device */
    246  1.13   mlelstv 		aprint_error_dev(sc->sc_ld.sc_dv,
    247  1.13   mlelstv 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
    248  1.13   mlelstv 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
    249  1.13   mlelstv 		bp->b_error = EINVAL;
    250   1.1    nonaka 		bp->b_resid = bp->b_bcount;
    251   1.1    nonaka 		lddone(&sc->sc_ld, bp);
    252   1.1    nonaka 		return;
    253   1.1    nonaka 	}
    254   1.1    nonaka 
    255   1.1    nonaka 	if (bp->b_flags & B_READ)
    256   1.1    nonaka 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
    257   1.1    nonaka 		    bp->b_data, bp->b_bcount);
    258   1.1    nonaka 	else
    259   1.1    nonaka 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
    260   1.1    nonaka 		    bp->b_data, bp->b_bcount);
    261   1.1    nonaka 	if (error) {
    262  1.24  kiyohara 		if (task->task_retries < LD_SDMMC_IORETRIES) {
    263  1.24  kiyohara 			struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
    264  1.24  kiyohara 			struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
    265  1.24  kiyohara 
    266  1.24  kiyohara 			diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
    267  1.24  kiyohara 				dksc->sc_dkdev.dk_label);
    268  1.24  kiyohara 			printf(", retrying\n");
    269  1.24  kiyohara 			task->task_retries++;
    270  1.24  kiyohara 			callout_reset(&task->task_restart_ch, RECOVERYTIME,
    271  1.24  kiyohara 			    ld_sdmmc_restart, task);
    272  1.24  kiyohara 			return;
    273  1.24  kiyohara 		}
    274  1.13   mlelstv 		bp->b_error = error;
    275   1.1    nonaka 		bp->b_resid = bp->b_bcount;
    276   1.1    nonaka 	} else {
    277   1.1    nonaka 		bp->b_resid = 0;
    278   1.1    nonaka 	}
    279   1.1    nonaka 
    280  1.24  kiyohara 	TAILQ_INSERT_TAIL(&sc->sc_freeq, &task->task, next);
    281  1.24  kiyohara 
    282   1.1    nonaka 	lddone(&sc->sc_ld, bp);
    283   1.1    nonaka }
    284   1.1    nonaka 
    285   1.1    nonaka static int
    286   1.1    nonaka ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    287   1.1    nonaka {
    288   1.1    nonaka 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
    289   1.1    nonaka 
    290   1.1    nonaka 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
    291   1.1    nonaka 	    blkcnt * ld->sc_secsize);
    292   1.1    nonaka }
    293  1.23  pgoyette 
    294  1.23  pgoyette MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
    295  1.23  pgoyette 
    296  1.23  pgoyette #ifdef _MODULE
    297  1.23  pgoyette /*
    298  1.23  pgoyette  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    299  1.23  pgoyette  * XXX it will be defined in the common-code module
    300  1.23  pgoyette  */
    301  1.23  pgoyette #undef  CFDRIVER_DECL
    302  1.23  pgoyette #define CFDRIVER_DECL(name, class, attr)
    303  1.23  pgoyette #include "ioconf.c"
    304  1.23  pgoyette #endif
    305  1.23  pgoyette 
    306  1.23  pgoyette static int
    307  1.23  pgoyette ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
    308  1.23  pgoyette {
    309  1.23  pgoyette #ifdef _MODULE
    310  1.23  pgoyette 	/*
    311  1.23  pgoyette 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    312  1.23  pgoyette 	 * the cfdrivers are attached already.
    313  1.23  pgoyette 	 */
    314  1.23  pgoyette 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    315  1.23  pgoyette #endif
    316  1.23  pgoyette 	int error = 0;
    317  1.23  pgoyette 
    318  1.23  pgoyette #ifdef _MODULE
    319  1.23  pgoyette 	switch (cmd) {
    320  1.23  pgoyette 	case MODULE_CMD_INIT:
    321  1.23  pgoyette 		error = config_init_component(no_cfdriver_vec,
    322  1.23  pgoyette 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    323  1.23  pgoyette         	break;
    324  1.23  pgoyette 	case MODULE_CMD_FINI:
    325  1.23  pgoyette 		error = config_fini_component(no_cfdriver_vec,
    326  1.23  pgoyette 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
    327  1.23  pgoyette 		break;
    328  1.23  pgoyette 	default:
    329  1.23  pgoyette 		error = ENOTTY;
    330  1.23  pgoyette 		break;
    331  1.23  pgoyette 	}
    332  1.23  pgoyette #endif
    333  1.23  pgoyette 
    334  1.23  pgoyette 	return error;
    335  1.23  pgoyette }
    336