PERFORCE change 107033 for review

Warner Losh imp at FreeBSD.org
Sat Sep 30 23:58:03 PDT 2006


http://perforce.freebsd.org/chv.cgi?CH=107033

Change 107033 by imp at imp_lighthouse on 2006/10/01 06:57:08

	Raid the at91_qdmmc driver and snag the best stuff from there
	to write the mmcsd driver.
	
	Still need to add data support to the at91_mci driver, which is
	going to be the linchpin in this whole shebang.  But this is a
	good stopping point for new.

Affected files ...

.. //depot/projects/arm/src/sys/dev/mmc/mmcbus_if.m#4 edit
.. //depot/projects/arm/src/sys/dev/mmc/mmcsd.c#3 edit

Differences ...

==== //depot/projects/arm/src/sys/dev/mmc/mmcbus_if.m#4 (text+ko) ====

@@ -37,20 +37,20 @@
 INTERFACE mmcbus;
 
 #
-# Called by the mmcbr devices when a mmc_request has completed
+# Queue and wait for a request.
 #
-METHOD int xfer_done {
+METHOD int wait_for_request {
 	device_t	brdev;
 	device_t	reqdev;
 	struct mmc_request *req;
-	int		status;
+	int		*status;
 };
 
 #
 # Claim the current bridge, blocking the current thread until the host
 # is no longer busy.
 #
-METHOD int aquire_bus {
+METHOD int acquire_bus {
 	device_t	brdev;
 	device_t	reqdev;
 }

==== //depot/projects/arm/src/sys/dev/mmc/mmcsd.c#3 (text+ko) ====

@@ -28,18 +28,28 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
+#include <sys/bio.h>
+#include <sys/bus.h>
+#include <sys/conf.h>
 #include <sys/kernel.h>
+#include <sys/kthread.h>
+#include <sys/lock.h>
 #include <sys/malloc.h>
-#include <sys/lock.h>
 #include <sys/module.h>
 #include <sys/mutex.h>
-#include <sys/bus.h>
+#include <geom/geom_disk.h>
 
+#include <dev/mmc/mmcvar.h>
 #include <dev/mmc/mmcreg.h>
 
+#include "mmcbus_if.h"
+
 struct mmcsd_softc {
 	device_t dev;
 	struct mtx sc_mtx;
+	struct disk *disk;
+	struct proc *p;
+	struct bio_queue_head bio_queue;
 };
 
 /* bus entry points */
@@ -47,6 +57,12 @@
 static int mmcsd_attach(device_t dev);
 static int mmcsd_detach(device_t dev);
 
+/* disk routines */
+static int mmcsd_open(struct disk *dp);
+static int mmcsd_close(struct disk *dp);
+static void mmcsd_strategy(struct bio *bp);
+static void mmcsd_task(void *arg);
+
 #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
 #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
 #define MMCSD_LOCK_INIT(_sc) \
@@ -60,7 +76,7 @@
 mmcsd_probe(device_t dev)
 {
 
-	device_set_desc(dev, "mmc/sd bus");
+	device_set_desc(dev, "mmc or sd flash card");
 	return (0);
 }
 
@@ -73,6 +89,21 @@
 	sc->dev = dev;
 	MMCSD_LOCK_INIT(sc);
 
+	MMCSD_LOCK(sc);
+	sc->disk = disk_alloc();
+	sc->disk->d_open = mmcsd_open;
+	sc->disk->d_close = mmcsd_close;
+	sc->disk->d_strategy = mmcsd_strategy;
+	// sc->disk->d_dump = mmcsd_dump;	Need polling mmc layer
+	sc->disk->d_name = "flash/card";
+	sc->disk->d_drv1 = sc;
+	sc->disk->d_maxsize = DFLTPHYS;
+	sc->disk->d_unit = device_get_unit(dev);
+	disk_create(sc->disk, DISK_VERSION);
+	bioq_init(&sc->bio_queue);
+	MMCSD_UNLOCK(sc);
+	kthread_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
+
 	return (0);
 }
 
@@ -82,6 +113,111 @@
 	return (EBUSY);	/* XXX */
 }
 
+static int
+mmcsd_open(struct disk *dp)
+{
+	struct mmcsd_softc *sc;
+
+	sc = (struct mmcsd_softc *)dp->d_drv1;
+
+	MMCSD_LOCK(sc);
+	sc->disk->d_sectorsize = mmc_get_sector_size(sc->dev);
+	sc->disk->d_mediasize = mmc_get_media_size(sc->dev);
+	MMCSD_UNLOCK(sc);
+
+	return 0;
+}
+
+static int
+mmcsd_close(struct disk *dp)
+{
+	struct mmcsd_softc *sc;
+
+	sc = (struct mmcsd_softc *)dp->d_drv1;
+
+	MMCSD_LOCK(sc);
+	MMCSD_UNLOCK(sc);
+
+	// XXX do nothing since we don't lock for now
+	return 0;
+}
+
+static void
+mmcsd_strategy(struct bio *bp)
+{
+	struct mmcsd_softc *sc;
+
+	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
+	MMCSD_LOCK(sc);
+	bioq_disksort(&sc->bio_queue, bp);
+	wakeup(sc);
+	MMCSD_UNLOCK(sc);
+}
+
+static void
+mmcsd_task(void *arg)
+{
+	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
+	struct bio *bp;
+	int sz, err;
+	daddr_t end;
+	struct mmc_command cmd;
+	struct mmc_command stop;
+	struct mmc_request req;
+	struct mmc_data data;
+	uint32_t block;
+	device_t dev;
+
+	MMCSD_LOCK(sc);
+	printf("mmcsd_task: start\n");
+	dev = sc->dev;
+	for (;;) {
+		do {
+			bp = bioq_first(&sc->bio_queue);
+			if (bp == NULL)
+				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
+		} while (bp == NULL);
+		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
+		bioq_remove(&sc->bio_queue, bp);
+		printf("at91_qdmmc_task: request %p\n", bp);
+		sz = sc->disk->d_sectorsize;
+		end = bp->bio_pblkno + (bp->bio_bcount / sz);
+		// XXX should use read/write_mulit
+		for (block = bp->bio_pblkno; block < end; block++) {
+			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
+			req.cmd = &cmd;
+			cmd.data = &data;
+			req.stop = &stop;
+			if (bp->bio_cmd == BIO_READ)
+				cmd.opcode = MMC_READ_SINGLE_BLOCK;
+			else
+				cmd.opcode = MMC_WRITE_BLOCK;
+			cmd.arg = block << 9;
+			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
+			// data.timeout_ns = ;
+			// data.timeout_clks = ;
+			data.len = 512;
+			data.data = vaddr;
+			data.mrq = &req;
+			if (bp->bio_cmd == BIO_READ)
+				data.flags = MMC_DATA_READ;
+			else
+				data.flags = MMC_DATA_WRITE;
+			stop.opcode = MMC_STOP_TRANSMISSION;
+			stop.arg = 0;
+			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
+			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
+			    &req, &err);
+			// XXX error handling
+//XXX			while (!(mmc_send_status(dev) & R1_READY_FOR_DATA))
+//				continue;
+			// XXX decode mmc status
+		}
+		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
+		biodone(bp);
+	}
+}
+
 static device_method_t mmcsd_methods[] = {
 	DEVMETHOD(device_probe, mmcsd_probe),
 	DEVMETHOD(device_attach, mmcsd_attach),


More information about the p4-projects mailing list