]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/mmc/mmcsd.c
MFC: r234524
[FreeBSD/stable/9.git] / sys / dev / mmc / mmcsd.c
1 /*-
2  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Portions of this software may have been developed with reference to
26  * the SD Simplified Specification.  The following disclaimer may apply:
27  *
28  * The following conditions apply to the release of the simplified
29  * specification ("Simplified Specification") by the SD Card Association and
30  * the SD Group. The Simplified Specification is a subset of the complete SD
31  * Specification which is owned by the SD Card Association and the SD
32  * Group. This Simplified Specification is provided on a non-confidential
33  * basis subject to the disclaimers below. Any implementation of the
34  * Simplified Specification may require a license from the SD Card
35  * Association, SD Group, SD-3C LLC or other third parties.
36  *
37  * Disclaimers:
38  *
39  * The information contained in the Simplified Specification is presented only
40  * as a standard specification for SD Cards and SD Host/Ancillary products and
41  * is provided "AS-IS" without any representations or warranties of any
42  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43  * Card Association for any damages, any infringements of patents or other
44  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45  * parties, which may result from its use. No license is granted by
46  * implication, estoppel or otherwise under any patent or other rights of the
47  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49  * or the SD Card Association to disclose or distribute any technical
50  * information, know-how or other confidential information to any third party.
51  */
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/bio.h>
59 #include <sys/bus.h>
60 #include <sys/conf.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/module.h>
66 #include <sys/mutex.h>
67 #include <geom/geom_disk.h>
68
69 #include <dev/mmc/mmcbrvar.h>
70 #include <dev/mmc/mmcreg.h>
71 #include <dev/mmc/mmcvar.h>
72
73 #include "mmcbus_if.h"
74
75 #if __FreeBSD_version < 800002
76 #define kproc_create    kthread_create
77 #define kproc_exit      kthread_exit
78 #endif
79
80 struct mmcsd_softc {
81         device_t dev;
82         struct mtx sc_mtx;
83         struct disk *disk;
84         struct proc *p;
85         struct bio_queue_head bio_queue;
86         daddr_t eblock, eend;   /* Range remaining after the last erase. */
87         int running;
88         int suspend;
89 };
90
91 /* bus entry points */
92 static int mmcsd_probe(device_t dev);
93 static int mmcsd_attach(device_t dev);
94 static int mmcsd_detach(device_t dev);
95
96 /* disk routines */
97 static int mmcsd_open(struct disk *dp);
98 static int mmcsd_close(struct disk *dp);
99 static void mmcsd_strategy(struct bio *bp);
100 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
101         off_t offset, size_t length);
102 static void mmcsd_task(void *arg);
103
104 static int mmcsd_bus_bit_width(device_t dev);
105
106 #define MMCSD_LOCK(_sc)         mtx_lock(&(_sc)->sc_mtx)
107 #define MMCSD_UNLOCK(_sc)       mtx_unlock(&(_sc)->sc_mtx)
108 #define MMCSD_LOCK_INIT(_sc) \
109         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
110             "mmcsd", MTX_DEF)
111 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
112 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
113 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
114
115 static int
116 mmcsd_probe(device_t dev)
117 {
118
119         device_quiet(dev);
120         device_set_desc(dev, "MMC/SD Memory Card");
121         return (0);
122 }
123
124 static int
125 mmcsd_attach(device_t dev)
126 {
127         struct mmcsd_softc *sc;
128         struct disk *d;
129         intmax_t mb;
130         uint32_t speed;
131         uint32_t maxblocks;
132         char unit;
133
134         sc = device_get_softc(dev);
135         sc->dev = dev;
136         MMCSD_LOCK_INIT(sc);
137
138         d = sc->disk = disk_alloc();
139         d->d_open = mmcsd_open;
140         d->d_close = mmcsd_close;
141         d->d_strategy = mmcsd_strategy;
142         d->d_dump = mmcsd_dump;
143         d->d_name = "mmcsd";
144         d->d_drv1 = sc;
145         d->d_maxsize = 4*1024*1024;     /* Maximum defined SD card AU size. */
146         d->d_sectorsize = mmc_get_sector_size(dev);
147         d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
148         d->d_stripeoffset = 0;
149         d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
150         d->d_unit = device_get_unit(dev);
151         d->d_flags = DISKFLAG_CANDELETE;
152         /*
153          * Display in most natural units.  There's no cards < 1MB.
154          * The SD standard goes to 2GiB, but the data format supports
155          * up to 4GiB and some card makers push it up to this limit.
156          * The SDHC standard only goes to 32GiB (the data format in
157          * SDHC is good to 2TiB however, which isn't too ugly at
158          * 2048GiBm, so we note it in passing here and don't add the
159          * code to print TiB).
160          */
161         mb = d->d_mediasize >> 20;      /* 1MiB == 1 << 20 */
162         unit = 'M';
163         if (mb >= 10240) {              /* 1GiB = 1024 MiB */
164                 unit = 'G';
165                 mb /= 1024;
166         }
167         /*
168          * Report the clock speed of the underlying hardware, which might be
169          * different than what the card reports due to hardware limitations.
170          * Report how many blocks the hardware transfers at once, but clip the
171          * number to MAXPHYS since the system won't initiate larger transfers.
172          */
173         speed = mmcbr_get_clock(device_get_parent(dev));
174         maxblocks = mmc_get_max_data(dev);
175         if (maxblocks > MAXPHYS)
176                 maxblocks = MAXPHYS;
177         device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
178             mb, unit, mmc_get_card_id_string(dev),
179             mmc_get_read_only(dev) ? " (read-only)" : "",
180             device_get_nameunit(device_get_parent(dev)),
181             speed / 1000000, (speed / 100000) % 10,
182             mmcsd_bus_bit_width(dev), maxblocks);
183         disk_create(d, DISK_VERSION);
184         bioq_init(&sc->bio_queue);
185
186         sc->running = 1;
187         sc->suspend = 0;
188         sc->eblock = sc->eend = 0;
189         kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
190
191         return (0);
192 }
193
194 static int
195 mmcsd_detach(device_t dev)
196 {
197         struct mmcsd_softc *sc = device_get_softc(dev);
198
199         MMCSD_LOCK(sc);
200         sc->suspend = 0;
201         if (sc->running > 0) {
202                 /* kill thread */
203                 sc->running = 0;
204                 wakeup(sc);
205                 /* wait for thread to finish. */
206                 while (sc->running != -1)
207                         msleep(sc, &sc->sc_mtx, 0, "detach", 0);
208         }
209         MMCSD_UNLOCK(sc);
210
211         /* Flush the request queue. */
212         bioq_flush(&sc->bio_queue, NULL, ENXIO);
213         /* kill disk */
214         disk_destroy(sc->disk);
215
216         MMCSD_LOCK_DESTROY(sc);
217
218         return (0);
219 }
220
221 static int
222 mmcsd_suspend(device_t dev)
223 {
224         struct mmcsd_softc *sc = device_get_softc(dev);
225
226         MMCSD_LOCK(sc);
227         sc->suspend = 1;
228         if (sc->running > 0) {
229                 /* kill thread */
230                 sc->running = 0;
231                 wakeup(sc);
232                 /* wait for thread to finish. */
233                 while (sc->running != -1)
234                         msleep(sc, &sc->sc_mtx, 0, "detach", 0);
235         }
236         MMCSD_UNLOCK(sc);
237         return (0);
238 }
239
240 static int
241 mmcsd_resume(device_t dev)
242 {
243         struct mmcsd_softc *sc = device_get_softc(dev);
244
245         MMCSD_LOCK(sc);
246         sc->suspend = 0;
247         if (sc->running <= 0) {
248                 sc->running = 1;
249                 MMCSD_UNLOCK(sc);
250                 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
251         } else
252                 MMCSD_UNLOCK(sc);
253         return (0);
254 }
255
256 static int
257 mmcsd_open(struct disk *dp)
258 {
259         return (0);
260 }
261
262 static int
263 mmcsd_close(struct disk *dp)
264 {
265         return (0);
266 }
267
268 static void
269 mmcsd_strategy(struct bio *bp)
270 {
271         struct mmcsd_softc *sc;
272
273         sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
274         MMCSD_LOCK(sc);
275         if (sc->running > 0 || sc->suspend > 0) {
276                 bioq_disksort(&sc->bio_queue, bp);
277                 MMCSD_UNLOCK(sc);
278                 wakeup(sc);
279         } else {
280                 MMCSD_UNLOCK(sc);
281                 biofinish(bp, NULL, ENXIO);
282         }
283 }
284
285 static daddr_t
286 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
287 {
288         daddr_t block, end;
289         struct mmc_command cmd;
290         struct mmc_command stop;
291         struct mmc_request req;
292         struct mmc_data data;
293         device_t dev = sc->dev;
294         int sz = sc->disk->d_sectorsize;
295
296         block = bp->bio_pblkno;
297         end = bp->bio_pblkno + (bp->bio_bcount / sz);
298         while (block < end) {
299                 char *vaddr = bp->bio_data +
300                     (block - bp->bio_pblkno) * sz;
301                 int numblocks = min(end - block, mmc_get_max_data(dev));
302                 memset(&req, 0, sizeof(req));
303                 memset(&cmd, 0, sizeof(cmd));
304                 memset(&stop, 0, sizeof(stop));
305                 req.cmd = &cmd;
306                 cmd.data = &data;
307                 if (bp->bio_cmd == BIO_READ) {
308                         if (numblocks > 1)
309                                 cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
310                         else
311                                 cmd.opcode = MMC_READ_SINGLE_BLOCK;
312                 } else {
313                         if (numblocks > 1)
314                                 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
315                         else
316                                 cmd.opcode = MMC_WRITE_BLOCK;
317                 }
318                 cmd.arg = block;
319                 if (!mmc_get_high_cap(dev))
320                         cmd.arg <<= 9;
321                 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
322                 data.data = vaddr;
323                 data.mrq = &req;
324                 if (bp->bio_cmd == BIO_READ)
325                         data.flags = MMC_DATA_READ;
326                 else
327                         data.flags = MMC_DATA_WRITE;
328                 data.len = numblocks * sz;
329                 if (numblocks > 1) {
330                         data.flags |= MMC_DATA_MULTI;
331                         stop.opcode = MMC_STOP_TRANSMISSION;
332                         stop.arg = 0;
333                         stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
334                         req.stop = &stop;
335                 }
336 //              printf("Len %d  %lld-%lld flags %#x sz %d\n",
337 //                  (int)data.len, (long long)block, (long long)end, data.flags, sz);
338                 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
339                     &req);
340                 if (req.cmd->error != MMC_ERR_NONE)
341                         break;
342                 block += numblocks;
343         }
344         return (block);
345 }
346
347 static daddr_t
348 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
349 {
350         daddr_t block, end, start, stop;
351         struct mmc_command cmd;
352         struct mmc_request req;
353         device_t dev = sc->dev;
354         int sz = sc->disk->d_sectorsize;
355         int erase_sector;
356
357         block = bp->bio_pblkno;
358         end = bp->bio_pblkno + (bp->bio_bcount / sz);
359         /* Coalesce with part remaining from previous request. */
360         if (block > sc->eblock && block <= sc->eend)
361                 block = sc->eblock;
362         if (end >= sc->eblock && end < sc->eend)
363                 end = sc->eend;
364         /* Safe round to the erase sector boundaries. */
365         erase_sector = mmc_get_erase_sector(dev);
366         start = block + erase_sector - 1;        /* Round up. */
367         start -= start % erase_sector;
368         stop = end;                             /* Round down. */
369         stop -= end % erase_sector;      
370         /* We can't erase area smaller then sector, store it for later. */
371         if (start >= stop) {
372                 sc->eblock = block;
373                 sc->eend = end;
374                 return (end);
375         }
376
377         /* Set erase start position. */
378         memset(&req, 0, sizeof(req));
379         memset(&cmd, 0, sizeof(cmd));
380         req.cmd = &cmd;
381         if (mmc_get_card_type(dev) == mode_sd)
382                 cmd.opcode = SD_ERASE_WR_BLK_START;
383         else
384                 cmd.opcode = MMC_ERASE_GROUP_START;
385         cmd.arg = start;
386         if (!mmc_get_high_cap(dev))
387                 cmd.arg <<= 9;
388         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
389         MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
390             &req);
391         if (req.cmd->error != MMC_ERR_NONE) {
392             printf("erase err1: %d\n", req.cmd->error);
393             return (block);
394         }
395         /* Set erase stop position. */
396         memset(&req, 0, sizeof(req));
397         memset(&cmd, 0, sizeof(cmd));
398         req.cmd = &cmd;
399         if (mmc_get_card_type(dev) == mode_sd)
400                 cmd.opcode = SD_ERASE_WR_BLK_END;
401         else
402                 cmd.opcode = MMC_ERASE_GROUP_END;
403         cmd.arg = stop;
404         if (!mmc_get_high_cap(dev))
405                 cmd.arg <<= 9;
406         cmd.arg--;
407         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
408         MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
409             &req);
410         if (req.cmd->error != MMC_ERR_NONE) {
411             printf("erase err2: %d\n", req.cmd->error);
412             return (block);
413         }
414         /* Erase range. */
415         memset(&req, 0, sizeof(req));
416         memset(&cmd, 0, sizeof(cmd));
417         req.cmd = &cmd;
418         cmd.opcode = MMC_ERASE;
419         cmd.arg = 0;
420         cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
421         MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
422             &req);
423         if (req.cmd->error != MMC_ERR_NONE) {
424             printf("erase err3 %d\n", req.cmd->error);
425             return (block);
426         }
427         /* Store one of remaining parts for the next call. */
428         if (bp->bio_pblkno >= sc->eblock || block == start) {
429                 sc->eblock = stop;      /* Predict next forward. */
430                 sc->eend = end;
431         } else {
432                 sc->eblock = block;     /* Predict next backward. */
433                 sc->eend = start;
434         }
435         return (end);
436 }
437
438 static int
439 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
440         off_t offset, size_t length)
441 {
442         struct disk *disk = arg;
443         struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
444         device_t dev = sc->dev;
445         struct bio bp;
446         daddr_t block, end;
447
448         /* length zero is special and really means flush buffers to media */
449         if (!length)
450                 return (0);
451
452         bzero(&bp, sizeof(struct bio));
453         bp.bio_disk = disk;
454         bp.bio_pblkno = offset / disk->d_sectorsize;
455         bp.bio_bcount = length;
456         bp.bio_data = virtual;
457         bp.bio_cmd = BIO_WRITE;
458         end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
459         MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
460         block = mmcsd_rw(sc, &bp);
461         MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
462         return ((end < block) ? EIO : 0);
463 }
464
465 static void
466 mmcsd_task(void *arg)
467 {
468         struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
469         struct bio *bp;
470         int sz;
471         daddr_t block, end;
472         device_t dev;
473
474         dev = sc->dev;
475         while (1) {
476                 MMCSD_LOCK(sc);
477                 do {
478                         if (sc->running == 0)
479                                 goto out;
480                         bp = bioq_takefirst(&sc->bio_queue);
481                         if (bp == NULL)
482                                 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
483                 } while (bp == NULL);
484                 MMCSD_UNLOCK(sc);
485                 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
486                         bp->bio_error = EROFS;
487                         bp->bio_resid = bp->bio_bcount;
488                         bp->bio_flags |= BIO_ERROR;
489                         biodone(bp);
490                         continue;
491                 }
492                 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
493                 sz = sc->disk->d_sectorsize;
494                 block = bp->bio_pblkno;
495                 end = bp->bio_pblkno + (bp->bio_bcount / sz);
496                 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
497                         /* Access to the remaining erase block obsoletes it. */
498                         if (block < sc->eend && end > sc->eblock)
499                                 sc->eblock = sc->eend = 0;
500                         block = mmcsd_rw(sc, bp);
501                 } else if (bp->bio_cmd == BIO_DELETE) {
502                         block = mmcsd_delete(sc, bp);
503                 }
504                 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
505                 if (block < end) {
506                         bp->bio_error = EIO;
507                         bp->bio_resid = (end - block) * sz;
508                         bp->bio_flags |= BIO_ERROR;
509                 }
510                 biodone(bp);
511         }
512 out:
513         /* tell parent we're done */
514         sc->running = -1;
515         MMCSD_UNLOCK(sc);
516         wakeup(sc);
517
518         kproc_exit(0);
519 }
520
521 static int
522 mmcsd_bus_bit_width(device_t dev)
523 {
524         if (mmc_get_bus_width(dev) == bus_width_1)
525                 return (1);
526         if (mmc_get_bus_width(dev) == bus_width_4)
527                 return (4);
528         return (8);
529 }
530
531 static device_method_t mmcsd_methods[] = {
532         DEVMETHOD(device_probe, mmcsd_probe),
533         DEVMETHOD(device_attach, mmcsd_attach),
534         DEVMETHOD(device_detach, mmcsd_detach),
535         DEVMETHOD(device_suspend, mmcsd_suspend),
536         DEVMETHOD(device_resume, mmcsd_resume),
537         DEVMETHOD_END
538 };
539
540 static driver_t mmcsd_driver = {
541         "mmcsd",
542         mmcsd_methods,
543         sizeof(struct mmcsd_softc),
544 };
545 static devclass_t mmcsd_devclass;
546
547 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);