Index: freebsd/sys/arm/at91/at91_mci.c
===================================================================
--- freebsd/sys/arm/at91/at91_mci.c	(revision 3fac9e9cd46b0926f9d90b593f164eb4c718a2a3)
+++ freebsd/sys/arm/at91/at91_mci.c	(revision d45899bce8b0e0b813295b17c1d0da7312d7a1fc)
@@ -141,13 +141,12 @@
  * entire data cache, impacting overall system performance.
  */
+#ifndef __rtems__
 #define BBCOUNT     2
-#ifndef __rtems__
-#define BBSIZE      (16*1024)
-#define MAX_BLOCKS  ((BBSIZE*BBCOUNT)/512)
-#else /* __rtems__ */
 #define BBSIZE      (32*1024)
 #define MAX_BLOCKS  ((BBSIZE)/512)
 /* FIXME: It would be better to split the DMA up in that case like in the
  * original driver. But that would need some rework. */
+#else /* __rtems__ */
+#define	MAX_BLOCKS 32
 #endif /* __rtems__ */
 
@@ -178,9 +177,12 @@
 	RTEMS_INTERRUPT_LOCK_MEMBER(sc_lock)
 #endif /* __rtems__ */
+#ifndef __rtems__
 	bus_dma_tag_t dmatag;
+#endif /* __rtems__ */
 	struct mmc_host host;
 	int bus_busy;
 	struct mmc_request *req;
 	struct mmc_command *curcmd;
+#ifndef __rtems__
 	bus_dmamap_t bbuf_map[BBCOUNT];
 	char      *  bbuf_vaddr[BBCOUNT]; /* bounce bufs in KVA space */
@@ -188,5 +190,5 @@
 	uint32_t     bbuf_curidx;	  /* which bbuf is the active DMA buffer */
 	uint32_t     xfer_offset;	  /* offset so far into caller's buf */
-#ifdef __rtems__
+#else /* __rtems__ */
 	uint32_t xdma_tx_channel;
 	uint32_t xdma_rx_channel;
@@ -208,8 +210,8 @@
 static void at91_mci_deactivate(device_t dev);
 static int at91_mci_is_mci1rev2xx(void);
-#ifdef __rtems__
+#ifndef __rtems__
 static void at91_mci_read_done(struct at91_mci_softc *sc, uint32_t sr);
+#endif /* __rtems__ */
 static void at91_mci_write_done(struct at91_mci_softc *sc, uint32_t sr);
-#endif /* __rtems__ */
 
 #ifndef __rtems__
@@ -252,4 +254,5 @@
 }
 
+#ifndef __rtems__
 static void
 at91_bswap_buf(struct at91_mci_softc *sc, void * dptr, void * sptr, uint32_t memsize)
@@ -296,4 +299,5 @@
 	*(bus_addr_t *)arg = segs[0].ds_addr;
 }
+#endif /* __rtems__ */
 
 static void
@@ -431,5 +435,9 @@
 	struct sysctl_oid *soid;
 	device_t child;
+#ifndef __rtems__
 	int err, i;
+#else /* __rtems__ */
+	int err;
+#endif /* __rtems__ */
 
 #ifdef __rtems__
@@ -516,4 +524,5 @@
 	at91_mci_init(dev);
 
+#ifndef __rtems__
 	/*
 	 * Allocate DMA tags and maps and bounce buffers.
@@ -544,5 +553,4 @@
 	 * Activate the interrupt
 	 */
-#ifndef __rtems__
 	err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
 	    NULL, at91_mci_intr, sc, &sc->intrhand);
@@ -605,4 +613,7 @@
 
 	child = device_add_child(dev, "mmc", 0);
+#ifdef __rtems__
+	(void)child;
+#endif /* __rtems__ */
 	device_set_ivars(dev, &sc->host);
 	err = bus_generic_attach(dev);
@@ -616,12 +627,16 @@
 at91_mci_detach(device_t dev)
 {
+#ifndef __rtems__
 	struct at91_mci_softc *sc = device_get_softc(dev);
+#endif /* __rtems__ */
 
 	at91_mci_fini(dev);
 	at91_mci_deactivate(dev);
 
+#ifndef __rtems__
 	bus_dmamem_free(sc->dmatag, sc->bbuf_vaddr[0], sc->bbuf_map[0]);
 	bus_dmamem_free(sc->dmatag, sc->bbuf_vaddr[1], sc->bbuf_map[1]);
 	bus_dma_tag_destroy(sc->dmatag);
+#endif /* __rtems__ */
 
 	return (EBUSY);	/* XXX */
@@ -760,5 +775,5 @@
 static void
 at91_mci_setup_xdma(struct at91_mci_softc *sc, bool read, uint32_t block_size,
-    uint32_t number_blocks, bus_addr_t paddr, uint32_t len)
+    uint32_t block_count, void *data, uint32_t len)
 {
 	sXdmadCfg *xdma_cfg;
@@ -783,15 +798,15 @@
 	}
 
-	for (i = 0; i < number_blocks; ++i) {
+	for (i = 0; i < block_count; ++i) {
 		if (read) {
 			dma_desc[i].mbr_sa = sa_rdr;
-			dma_desc[i].mbr_da = ((uint32_t)paddr) + i * block_size;
+			dma_desc[i].mbr_da = ((uint32_t)data) + i * block_size;
 		} else {
-			dma_desc[i].mbr_sa = ((uint32_t)paddr) + i * block_size;
+			dma_desc[i].mbr_sa = ((uint32_t)data) + i * block_size;
 			dma_desc[i].mbr_da = da_tdr;
 		}
 		dma_desc[i].mbr_ubc = XDMA_UBC_NVIEW_NDV1 |
 		    XDMA_UBC_NDEN_UPDATED | (block_size/4);
-		if (i == number_blocks - 1) {
+		if (i == block_count - 1) {
 			dma_desc[i].mbr_ubc |= XDMA_UBC_NDE_FETCH_DIS;
 			dma_desc[i].mbr_nda = 0;
@@ -809,7 +824,7 @@
 	/* FIXME: Is that correct? */
 	if (read) {
-		rtems_cache_invalidate_multiple_data_lines(paddr, len);
+		rtems_cache_invalidate_multiple_data_lines(data, len);
 	} else {
-		rtems_cache_flush_multiple_data_lines(paddr, len);
+		rtems_cache_flush_multiple_data_lines(data, len);
 	}
 	rtems_cache_flush_multiple_data_lines(dma_desc, sizeof(dma_desc));
@@ -820,5 +835,4 @@
 
 }
-
 #endif /* __rtems__ */
 static void
@@ -826,9 +840,9 @@
 {
 	uint32_t cmdr, mr;
+	struct mmc_data *data;
 #ifdef __rtems__
-	uint32_t number_blocks;
+	uint32_t block_count;
 	uint32_t block_size;
 #endif /* __rtems__ */
-	struct mmc_data *data;
 
 	sc->curcmd = cmd;
@@ -928,14 +942,4 @@
 	mr |=  min(data->len, 512) << 16;
 	WR4(sc, MCI_MR, mr | MCI_MR_PDCMODE|MCI_MR_PDCPADV);
-#else /* __rtems__ */
-	mr = RD4(sc,MCI_MR);
-	WR4(sc, MCI_MR, mr | MCI_MR_PDCPADV);
-
-	WR4(sc, MCI_DMA, MCI_DMA_DMAEN | MCI_DMA_CHKSIZE_1);
-
-	block_size = min(data->len, 512);
-	number_blocks = data->len / block_size;
-	WR4(sc, MCI_BLKR, block_size << 16 | number_blocks);
-#endif /* __rtems__ */
 
 	/*
@@ -971,6 +975,17 @@
 	sc->xfer_offset = 0;
 	sc->bbuf_curidx = 0;
+#else /* __rtems__ */
+	mr = RD4(sc,MCI_MR);
+	WR4(sc, MCI_MR, mr | MCI_MR_PDCPADV);
+
+	WR4(sc, MCI_DMA, MCI_DMA_DMAEN | MCI_DMA_CHKSIZE_1);
+
+	block_size = min(data->len, 512);
+	block_count = data->len / block_size;
+	WR4(sc, MCI_BLKR, (block_size << 16) | block_count);
+#endif /* __rtems__ */
 
 	if (data->flags & (MMC_DATA_READ | MMC_DATA_WRITE)) {
+#ifndef __rtems__
 		uint32_t len;
 		uint32_t remaining = data->len;
@@ -978,10 +993,7 @@
 		int err;
 
-#ifndef __rtems__
 		if (remaining > (BBCOUNT*BBSIZE))
-#else /* __rtems__ */
-		if (remaining > (BBSIZE))
-#endif /* __rtems__ */
 			panic("IO read size exceeds MAXDATA\n");
+#endif /* __rtems__ */
 
 		if (data->flags & MMC_DATA_READ) {
@@ -990,8 +1002,4 @@
 				len = remaining / 2;
 			else
-#else
-			/* FIXME: This reduces performance. Set up DMA in two
-			 * parts instead like done on AT91. */
-#endif /* __rtems__ */
 				len = remaining;
 			err = bus_dmamap_load(sc->dmatag, sc->bbuf_map[0],
@@ -1002,5 +1010,4 @@
 			bus_dmamap_sync(sc->dmatag, sc->bbuf_map[0],
 			    BUS_DMASYNC_PREREAD);
-#ifndef __rtems__
 			WR4(sc, PDC_RPR, paddr);
 			WR4(sc, PDC_RCR, len / 4);
@@ -1025,14 +1032,9 @@
 			WR4(sc, PDC_PTCR, PDC_PTCR_RXTEN);
 #else /* __rtems__ */
-			at91_mci_setup_xdma(sc, true, block_size,
-			    number_blocks, paddr, len);
-
-			sc->bbuf_len[0] = len;
-			remaining -= len;
-			sc->bbuf_len[1] = 0;
-			if (remaining != 0)
-				panic("Still rx-data left. This should never happen.");
+			at91_mci_setup_xdma(sc, true, block_size, block_count,
+			    data->data, data->len);
 #endif /* __rtems__ */
 		} else {
+#ifndef __rtems__
 			len = min(BBSIZE, remaining);
 			at91_bswap_buf(sc, sc->bbuf_vaddr[0], data->data, len);
@@ -1044,5 +1046,4 @@
 			bus_dmamap_sync(sc->dmatag, sc->bbuf_map[0],
 			    BUS_DMASYNC_PREWRITE);
-#ifndef __rtems__
 			/*
 			 * Erratum workaround:  PDC transfer length on a write
@@ -1074,13 +1075,6 @@
 			/* do not enable PDC xfer until CMDRDY asserted */
 #else /* __rtems__ */
-			at91_mci_setup_xdma(sc, false, block_size,
-			    number_blocks, paddr, len);
-
-			sc->bbuf_len[0] = len;
-			remaining -= len;
-			sc->bbuf_len[1] = 0;
-			if (remaining != 0)
-				panic("Still tx-data left. This should never happen.");
-
+			at91_mci_setup_xdma(sc, false, block_size, block_count,
+			    data->data, data->len);
 #endif /* __rtems__ */
 		}
@@ -1191,4 +1185,5 @@
 }
 
+#ifndef __rtems__
 static void
 at91_mci_read_done(struct at91_mci_softc *sc, uint32_t sr)
@@ -1208,8 +1203,6 @@
 	 */
 
-#ifndef __rtems__
 	bus_dmamap_sync(sc->dmatag, sc->bbuf_map[curidx], BUS_DMASYNC_POSTREAD);
 	bus_dmamap_unload(sc->dmatag, sc->bbuf_map[curidx]);
-#endif /* __rtems__ */
 
 	at91_bswap_buf(sc, dataptr + sc->xfer_offset, sc->bbuf_vaddr[curidx], len);
@@ -1236,11 +1229,8 @@
 	} else {
 		WR4(sc, PDC_RNCR, 0);
-#ifndef __rtems__
 		WR4(sc, MCI_IER, MCI_SR_ERROR | MCI_SR_ENDRX);
-#else /* __rtems__ */
-		WR4(sc, MCI_IER, MCI_SR_ERROR | MCI_SR_XFRDONE);
-#endif /* __rtems__ */
-	}
-}
+	}
+}
+#endif /* __rtems__ */
 
 static void
@@ -1260,7 +1250,9 @@
 	WR4(sc, PDC_PTCR, PDC_PTCR_RXTDIS | PDC_PTCR_TXTDIS);
 
+#ifndef __rtems__
 	bus_dmamap_sync(sc->dmatag, sc->bbuf_map[sc->bbuf_curidx],
 	    BUS_DMASYNC_POSTWRITE);
 	bus_dmamap_unload(sc->dmatag, sc->bbuf_map[sc->bbuf_curidx]);
+#endif /* __rtems__ */
 
 	if ((cmd->data->flags & MMC_DATA_MULTI) || (sr & MCI_SR_NOTBUSY)) {
@@ -1533,7 +1525,9 @@
 #else /* __rtems__ */
 		if (isr & MCI_SR_XFRDONE) {
-			struct mmc_command *cmd = sc->curcmd;
 			if (cmd->data->flags & MMC_DATA_READ) {
-				at91_mci_read_done(sc, sr);
+				WR4(sc, PDC_PTCR, PDC_PTCR_RXTDIS |
+				    PDC_PTCR_TXTDIS);
+				cmd->error = MMC_ERR_NONE;
+				at91_mci_next_operation(sc);
 			} else {
 				if (sr & MCI_SR_BLKE)
