Skip to contentSkip to conversation

mmc: mxcmmc: add mpc512x support

PATCH In review Anatolij Gustschin 3 participants 7 messages Apr 8, 2013 – Apr 12, 2013 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover mmc: mxcmmc: add mpc512x support

The SDHC controller on mpc512x is compatible with i.MX31 SDHC controller, the existing MMC host driver can be used on mpc512x with some modifications, the patch series extends the existing driver. It is based on the following v3 mxcmmc DT support patch:

https://patchwork.kernel.org/patch/2368631

Changes in v3 series are mentioned in each patch

Anatolij Gustschin (5):
mmc: mxcmmc: fix race conditions for host->req and host->data access
mmc: mxcmmc: add mpc512x SDHC support
mmc: mxcmmc: use slot-gpio API for write-protect detection
mmc: mxcmmc: constify mxcmci_devtype
mmc: mxcmmc: enable DMA support on mpc512x

 arch/powerpc/boot/dts/mpc5121.dtsi |    2 +
 drivers/mmc/host/Kconfig           |   10 +-
 drivers/mmc/host/mxcmmc.c          |  209 ++++++++++++++++++++++++++----------
 3 files changed, 159 insertions(+), 62 deletions(-)
Signature · 1 line
1.7.5.4

A message that is not in the archive (sent off-list or not yet received): 1365456489-13152-1-git-send-email-agust-ynQEQJNshbs@public.gmane.org

#2 lore raw

PATCH v3 1/5 mmc: mxcmmc: fix race conditions for host->req and host->data access

mxcmci_dma_callback() is invoked by DMA drivers in soft-irq context and can be interrupted by the mxcmci_irq() interrupt which can finish the mmc request or data transfer and set host->req or host->data pointers to NULL. Then mxcmci_data_done() crashes with a null pointer dereferences. Protect all accesses to host->req and host->data by spin locks.

Also check host->data pointer in mxcmci_watchdog() before dereferencing it.

---
v3:
- move to first patch in the series as asked by Sascha
- add Acked-by

v2:
- only rebased

 drivers/mmc/host/mxcmmc.c |   31 ++++++++++++++++++++++++-------
 1 files changed, 24 insertions(+), 7 deletions(-)
drivers/mmc/host/mxcmmc.c +24 -7
index ff9e496..8ae7cf5 100644
@@ -623,24 +623,40 @@ static void mxcmci_datawork(struct work_struct *work)
 
 static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
 {
-	struct mmc_data *data = host->data;
+	struct mmc_request *req;
 	int data_error;
+	unsigned long flags;
+
+	spin_lock_irqsave(&host->lock, flags);
 
-	if (!data)
+	if (!host->data) {
+		spin_unlock_irqrestore(&host->lock, flags);
 		return;
+	}
+
+	if (!host->req) {
+		spin_unlock_irqrestore(&host->lock, flags);
+		return;
+	}
+
+	req = host->req;
+	if (!req->stop)
+		host->req = NULL; /* we will handle finish req below */
 
 	data_error = mxcmci_finish_data(host, stat);
 
+	spin_unlock_irqrestore(&host->lock, flags);
+
 	mxcmci_read_response(host, stat);
 	host->cmd = NULL;
 
-	if (host->req->stop) {
-		if (mxcmci_start_cmd(host, host->req->stop, 0)) {
-			mxcmci_finish_request(host, host->req);
+	if (req->stop) {
+		if (mxcmci_start_cmd(host, req->stop, 0)) {
+			mxcmci_finish_request(host, req);
 			return;
 		}
 	} else {
-		mxcmci_finish_request(host, host->req);
+		mxcmci_finish_request(host, req);
 	}
 }
 
@@ -931,7 +947,8 @@ static void mxcmci_watchdog(unsigned long data)
 
 	/* Mark transfer as erroneus and inform the upper layers */
 
-	host->data->error = -ETIMEDOUT;
+	if (host->data)
+		host->data->error = -ETIMEDOUT;
 	host->req = NULL;
 	host->cmd = NULL;
 	host->data = NULL;
Signature · 1 line
1.7.5.4
#3 lore raw

PATCH v3 2/5 mmc: mxcmmc: add mpc512x SDHC support

In reply to Anatolij Gustschin

The SDHC controller on mpc512x is compatible with i.MX31 SDHC, so the mxcmmc driver can be used on mpc512x, too. Extend the driver to support mpc512x as well.

---
v3:
- no changes, only add Acked-by tag

v2:
- change to select register accessors at build time and use ioread32be() and iowrite32be() accessors
- drop 512x specific clk_get() changes for now
- rebase on top of v3 mxcmmc DT support patch from Markus https://patchwork.kernel.org/patch/2368631

 drivers/mmc/host/Kconfig  |   10 ++--
 drivers/mmc/host/mxcmmc.c |  133 ++++++++++++++++++++++++++++++---------------
 2 files changed, 94 insertions(+), 49 deletions(-)
drivers/mmc/host/Kconfig +5 -5
index f83f245..9ab8f8d 100644
@@ -320,12 +320,12 @@ config MMC_MSM
 	  support for SDIO devices.
 
 config MMC_MXC
-	tristate "Freescale i.MX21/27/31 Multimedia Card Interface support"
-	depends on ARCH_MXC
+	tristate "Freescale i.MX21/27/31 or MPC512x Multimedia Card support"
+	depends on ARCH_MXC || PPC_MPC512x
 	help
-	  This selects the Freescale i.MX21, i.MX27 and i.MX31 Multimedia card
-	  Interface. If you have a i.MX platform with a Multimedia Card slot,
-	  say Y or M here.
+	  This selects the Freescale i.MX21, i.MX27, i.MX31 or MPC512x
+	  Multimedia Card Interface. If you have an i.MX or MPC512x platform
+	  with a Multimedia Card slot, say Y or M here.
 
 	  If unsure, say N.
 
drivers/mmc/host/mxcmmc.c +89 -44

311 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#4 lore raw

PATCH v3 3/5 mmc: mxcmmc: use slot-gpio API for write-protect detection

In reply to Anatolij Gustschin

slot-gpio API suppors read-only detection when "wp-gpios" property is present in the device tree mmc node. Use this API for write-protect detection.

---
v3:
- add Acked-by line

v2:
- no changes

 drivers/mmc/host/mxcmmc.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
drivers/mmc/host/mxcmmc.c +5 -3
index 9845b2d..eb45bf0 100644
@@ -38,6 +38,7 @@
 #include <linux/of_device.h>
 #include <linux/of_dma.h>
 #include <linux/of_gpio.h>
+#include <linux/mmc/slot-gpio.h>
 
 #include <asm/dma.h>
 #include <asm/irq.h>
@@ -917,10 +918,11 @@ static int mxcmci_get_ro(struct mmc_host *mmc)
 	if (host->pdata && host->pdata->get_ro)
 		return !!host->pdata->get_ro(mmc_dev(mmc));
 	/*
-	 * Board doesn't support read only detection; let the mmc core
-	 * decide what to do.
+	 * If board doesn't support read only detection (no mmc_gpio
+	 * context or gpio is invalid), then let the mmc core decide
+	 * what to do.
 	 */
-	return -ENOSYS;
+	return mmc_gpio_get_ro(mmc);
 }
 
 static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
Signature · 1 line
1.7.5.4
#5 lore raw

PATCH v3 4/5 mmc: mxcmmc: constify mxcmci_devtype

In reply to Anatolij Gustschin

mxcmci_devtype struct contains constant data, so
constify this struct.

---
v3:
- add Acked-by line

v2:
- remove build warning fix (already fixed elsewhere) and change commit log

 drivers/mmc/host/mxcmmc.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/mmc/host/mxcmmc.c +1 -1
index eb45bf0..9b01acb 100644
@@ -165,7 +165,7 @@ struct mxcmci_host {
 	enum mxcmci_type	devtype;
 };
 
-static struct platform_device_id mxcmci_devtype[] = {
+static const struct platform_device_id mxcmci_devtype[] = {
 	{
 		.name = "imx21-mmc",
 		.driver_data = IMX21_MMC,
Signature · 1 line
1.7.5.4
#6 lore raw

PATCH v3 5/5 mmc: mxcmmc: enable DMA support on mpc512x

In reply to Anatolij Gustschin

Add SDHC DMA channel description to the mpc512x device tree to enable slave channel requesting in the mxcmmc driver.

mpc512x DMA engine doesn't support endianness conversion when reading/writing data from peripheral's FIFO, so we have to swap data buffers before each DMA write and after each DMA read transfer manually.

Since chained SDHC DMA transfers are not supported on mpc512x, limit 'max_segs' tunable parameter to one and initialise it to 64 only when running on i.MX platforms.

---
v3:
- change swapping function name to mxcmci_swap_buffers()
- add Acked-by line

v2:
- only rebased

 arch/powerpc/boot/dts/mpc5121.dtsi |    2 ++
 drivers/mmc/host/mxcmmc.c          |   35 +++++++++++++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)
arch/powerpc/boot/dts/mpc5121.dtsi +2 -0
index 723e292..710aae6 100644
@@ -152,6 +152,8 @@
 			compatible = "fsl,mpc5121-sdhc";
 			reg = <0x1500 0x100>;
 			interrupts = <8 0x8>;
+			dmas = <&dma0 30>;
+			dma-names = "rx-tx";
 		};
 
 		i2c@1700 {
drivers/mmc/host/mxcmmc.c +33 -2
index 9b01acb..b937407 100644
@@ -301,6 +301,29 @@ static void mxcmci_softreset(struct mxcmci_host *host)
 }
 static int mxcmci_setup_dma(struct mmc_host *mmc);
 
+#if IS_ENABLED(CONFIG_PPC_MPC512x)
+static inline void buffer_swap32(u32 *buf, int len)
+{
+	int i;
+
+	for (i = 0; i < ((len + 3) / 4); i++) {
+		st_le32(buf, *buf);
+		buf++;
+	}
+}
+
+static void mxcmci_swap_buffers(struct mmc_data *data)
+{
+	struct scatterlist *sg;
+	int i;
+
+	for_each_sg(data->sg, sg, data->sg_len, i)
+		buffer_swap32(sg_virt(sg), sg->length);
+}
+#else
+static inline void mxcmci_swap_buffers(struct mmc_data *data) {}
+#endif
+
 static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
 {
 	unsigned int nob = data->blocks;
@@ -336,6 +359,8 @@ static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
 	} else {
 		host->dma_dir = DMA_TO_DEVICE;
 		slave_dirn = DMA_MEM_TO_DEV;
+
+		mxcmci_swap_buffers(data);
 	}
 
 	nents = dma_map_sg(host->dma->device->dev, data->sg,
@@ -461,9 +486,11 @@ static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
 	struct mmc_data *data = host->data;
 	int data_error;
 
-	if (mxcmci_use_dma(host))
+	if (mxcmci_use_dma(host)) {
 		dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
 				host->dma_dir);
+		mxcmci_swap_buffers(data);
+	}
 
 	if (stat & STATUS_ERR_MASK) {
 		dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
@@ -1050,7 +1077,6 @@ static int mxcmci_probe(struct platform_device *pdev)
 		mmc->caps |= MMC_CAP_SDIO_IRQ;
 
 	/* MMC core transfer sizes tunable parameters */
-	mmc->max_segs = 64;
 	mmc->max_blk_size = 2048;
 	mmc->max_blk_count = 65535;
 	mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
@@ -1069,6 +1095,11 @@ static int mxcmci_probe(struct platform_device *pdev)
 	} else {
 		host->devtype = pdev->id_entry->driver_data;
 	}
+
+	/* adjust max_segs after devtype detection */
+	if (!is_mpc512x_mmc(host))
+		mmc->max_segs = 64;
+
 	host->mmc = mmc;
 	host->pdata = pdata;
 	spin_lock_init(&host->lock);
Signature · 1 line
1.7.5.4
#7 lore raw
In reply to Anatolij Gustschin

Hi,

Quoted 17 lines · Anatolij Gustschin

The SDHC controller on mpc512x is compatible with i.MX31 SDHC controller, the existing MMC host driver can be used on mpc512x with some modifications, the patch series extends the existing driver. It is based on the following v3 mxcmmc DT support patch:

   https://patchwork.kernel.org/patch/2368631

Changes in v3 series are mentioned in each patch

Anatolij Gustschin (5):
mmc: mxcmmc: fix race conditions for host->req and host->data access
mmc: mxcmmc: add mpc512x SDHC support
mmc: mxcmmc: use slot-gpio API for write-protect detection
mmc: mxcmmc: constify mxcmci_devtype
mmc: mxcmmc: enable DMA support on mpc512x

Quoted 4 lines
 arch/powerpc/boot/dts/mpc5121.dtsi |    2 +
 drivers/mmc/host/Kconfig           |   10 +-
 drivers/mmc/host/mxcmmc.c          |  209 ++++++++++++++++++++++++++----------
 3 files changed, 159 insertions(+), 62 deletions(-)

Thanks, all pushed to mmc-next for 3.10.

- Chris.

Signature · 2 lines
Chris Ball   <[email protected]>   <http://printf.net/>
One Laptop Per Child