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