1 /* $NetBSD: ld_sdmmc.c,v 1.4 2009/11/28 10:00:24 nonaka 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.4 2009/11/28 10:00:24 nonaka Exp $"); 32 33 #include "rnd.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 #include <sys/buf.h> 40 #include <sys/bufq.h> 41 #include <sys/bus.h> 42 #include <sys/callout.h> 43 #include <sys/endian.h> 44 #include <sys/dkio.h> 45 #include <sys/disk.h> 46 #include <sys/kthread.h> 47 #if NRND > 0 48 #include <sys/rnd.h> 49 #endif 50 51 #include <uvm/uvm_extern.h> 52 53 #include <dev/ldvar.h> 54 55 #include <dev/sdmmc/sdmmcvar.h> 56 57 #ifdef SDMMC_DEBUG 58 #define DPRINTF(s) printf s 59 #else 60 #define DPRINTF(s) /**/ 61 #endif 62 63 struct ld_sdmmc_softc; 64 65 struct ld_sdmmc_task { 66 struct sdmmc_task task; 67 68 struct ld_sdmmc_softc *task_sc; 69 struct buf *task_bp; 70 callout_t task_callout; 71 }; 72 73 struct ld_sdmmc_softc { 74 struct ld_softc sc_ld; 75 int sc_hwunit; 76 77 struct sdmmc_function *sc_sf; 78 struct ld_sdmmc_task sc_task; 79 }; 80 81 static int ld_sdmmc_match(device_t, cfdata_t, void *); 82 static void ld_sdmmc_attach(device_t, device_t, void *); 83 static int ld_sdmmc_detach(device_t, int); 84 85 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int); 86 static int ld_sdmmc_start(struct ld_softc *, struct buf *); 87 88 static void ld_sdmmc_doattach(void *); 89 static void ld_sdmmc_dobio(void *); 90 static void ld_sdmmc_timeout(void *); 91 92 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc), 93 ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL); 94 95 96 /* ARGSUSED */ 97 static int 98 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux) 99 { 100 struct sdmmc_softc *sdmsc = device_private(parent); 101 102 if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE)) 103 return 1; 104 return 0; 105 } 106 107 /* ARGSUSED */ 108 static void 109 ld_sdmmc_attach(device_t parent, device_t self, void *aux) 110 { 111 struct ld_sdmmc_softc *sc = device_private(self); 112 struct sdmmc_attach_args *sa = aux; 113 struct ld_softc *ld = &sc->sc_ld; 114 struct lwp *lwp; 115 116 ld->sc_dv = self; 117 118 aprint_normal("\n"); 119 aprint_naive("\n"); 120 121 callout_init(&sc->sc_task.task_callout, CALLOUT_MPSAFE); 122 123 sc->sc_hwunit = 0; /* always 0? */ 124 sc->sc_sf = sa->sf; 125 126 ld->sc_flags = LDF_ENABLED; 127 ld->sc_secperunit = sc->sc_sf->csd.capacity; 128 ld->sc_secsize = SDMMC_SECTOR_SIZE; 129 ld->sc_maxxfer = MAXPHYS; 130 ld->sc_maxqueuecnt = 1; 131 ld->sc_dump = ld_sdmmc_dump; 132 ld->sc_start = ld_sdmmc_start; 133 134 /* 135 * It is avoided that the error occurs when the card attaches it, 136 * when wedge is supported. 137 */ 138 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, 139 ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) { 140 aprint_error_dev(self, "couldn't create thread\n"); 141 } 142 } 143 144 static void 145 ld_sdmmc_doattach(void *arg) 146 { 147 struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg; 148 struct ld_softc *ld = &sc->sc_ld; 149 150 ldattach(ld); 151 kthread_exit(0); 152 } 153 154 static int 155 ld_sdmmc_detach(device_t dev, int flags) 156 { 157 struct ld_sdmmc_softc *sc = device_private(dev); 158 struct ld_softc *ld = &sc->sc_ld; 159 int rv; 160 161 if ((rv = ldbegindetach(ld, flags)) != 0) 162 return rv; 163 ldenddetach(ld); 164 165 return 0; 166 } 167 168 static int 169 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp) 170 { 171 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv); 172 struct ld_sdmmc_task *task = &sc->sc_task; 173 174 task->task_sc = sc; 175 task->task_bp = bp; 176 sdmmc_init_task(&task->task, ld_sdmmc_dobio, task); 177 178 callout_reset(&task->task_callout, hz, ld_sdmmc_timeout, task); 179 sdmmc_add_task(sc->sc_sf->sc, &task->task); 180 181 return 0; 182 } 183 184 static void 185 ld_sdmmc_dobio(void *arg) 186 { 187 struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg; 188 struct ld_sdmmc_softc *sc = task->task_sc; 189 struct buf *bp = task->task_bp; 190 int error, s; 191 192 callout_stop(&task->task_callout); 193 194 /* 195 * I/O operation 196 */ 197 DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n", 198 device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT", 199 bp->b_rawblkno, bp->b_bcount)); 200 201 /* is everything done in terms of blocks? */ 202 if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) { 203 /* trying to read or write past end of device */ 204 DPRINTF(("%s: blkno exceeds capacity 0x%x\n", 205 device_xname(sc->sc_ld.sc_dv), sc->sc_sf->csd.capacity)); 206 bp->b_error = EIO; /* XXX */ 207 bp->b_resid = bp->b_bcount; 208 lddone(&sc->sc_ld, bp); 209 return; 210 } 211 212 s = splbio(); 213 if (bp->b_flags & B_READ) 214 error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno, 215 bp->b_data, bp->b_bcount); 216 else 217 error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno, 218 bp->b_data, bp->b_bcount); 219 if (error) { 220 DPRINTF(("%s: error %d\n", device_xname(sc->sc_ld.sc_dv), 221 error)); 222 bp->b_error = EIO; /* XXXX */ 223 bp->b_resid = bp->b_bcount; 224 } else { 225 bp->b_resid = 0; 226 } 227 splx(s); 228 229 lddone(&sc->sc_ld, bp); 230 } 231 232 static void 233 ld_sdmmc_timeout(void *arg) 234 { 235 struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg; 236 struct ld_sdmmc_softc *sc = task->task_sc; 237 struct buf *bp = task->task_bp; 238 int s; 239 240 s = splbio(); 241 if (!sdmmc_task_pending(&task->task)) { 242 splx(s); 243 return; 244 } 245 bp->b_error = EIO; /* XXXX */ 246 bp->b_resid = bp->b_bcount; 247 sdmmc_del_task(&task->task); 248 splx(s); 249 250 lddone(&sc->sc_ld, bp); 251 } 252 253 static int 254 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt) 255 { 256 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv); 257 258 return sdmmc_mem_write_block(sc->sc_sf, blkno, data, 259 blkcnt * ld->sc_secsize); 260 } 261