Skip to contentSkip to conversation

mmc: mxcmmc: add mpc512x support

PATCH Superseded Anatolij Gustschin 2 participants 11 messages Mar 31, 2013 – Apr 2, 2013

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 v2 series are mentioned in each patch

Anatolij Gustschin (5):
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
mmc: mxcmmc: fix race conditions for host->req and host->data access

 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
#1 lore raw

PATCH v2 1/5 mmc: mxcmmc: add mpc512x SDHC support

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.

---
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 d88219e..eda4376 100644
@@ -319,12 +319,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
#2 lore raw

PATCH v2 2/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.

---
v2:
- no changes

 drivers/mmc/host/mxcmmc.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
drivers/mmc/host/mxcmmc.c +5 -3
index 07a37d8..7fe93dd 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>
@@ -901,10 +902,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
#3 lore raw

PATCH v2 3/5 mmc: mxcmmc: constify mxcmci_devtype

In reply to Anatolij Gustschin

mxcmci_devtype struct contains constant data, so
constify this struct.

---
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 7fe93dd..dbacbde 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
#4 lore raw

PATCH v2 4/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.

---
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 d1fe070..712b15a 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 dbacbde..2861e0d 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_fixup_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_fixup_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_fixup_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_fixup_buffers(data);
+	}
 
 	if (stat & STATUS_ERR_MASK) {
 		dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
@@ -1033,7 +1060,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;
@@ -1052,6 +1078,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
maintainer
#5 lore raw

Re: [PATCH v2 4/5] mmc: mxcmmc: enable DMA support on mpc512x

Quoted 70 lines · 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.

---
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 d1fe070..712b15a 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 +23 -0
index dbacbde..2861e0d 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_fixup_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_fixup_buffers(struct mmc_data *data) {}
+#endif
+
 static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
 {
 	unsigned int nob = data->blocks;

drivers/mmc/host/mxcmmc.c static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data) · line 363

Quoted 5 lines
drivers/mmc/host/mxcmmc.c +2 -0
@@ -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_fixup_buffers(data);

For the register accesses it's fine the way you did in this version, but here I think a

	if (is_mpc512x_mmc())
		swap_buffer()

would look better.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw

Re: [PATCH v2 4/5] mmc: mxcmmc: enable DMA support on mpc512x

On Tue, 2 Apr 2013 08:56:54 +0200
Sascha Hauer <[email protected]> wrote:
...

Quoted 10 lines

drivers/mmc/host/mxcmmc.c static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data) · line 363

Quoted 6 lines
drivers/mmc/host/mxcmmc.c +2 -0
@@ -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_fixup_buffers(data);

For the register accesses it's fine the way you did in this version, but here I think a

Quoted 4 lines
	if (is_mpc512x_mmc())
		swap_buffer()

would look better.

the swapping function uses optimised st_le32() which is not available on ARM, that's the reason why this is build time selected. Maybe the "fixup" in the function name is not pretty, I can change it to "swap" then.

Thanks,

Anatolij

maintainer
#7 lore raw

Re: [PATCH v2 4/5] mmc: mxcmmc: enable DMA support on mpc512x

Quoted 13 lines · Anatolij Gustschin

On Tue, 2 Apr 2013 08:56:54 +0200
Sascha Hauer <[email protected]> wrote:
...

Quoted 10 lines

drivers/mmc/host/mxcmmc.c static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data) · line 363

Quoted 6 lines
drivers/mmc/host/mxcmmc.c +2 -0
@@ -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_fixup_buffers(data);

For the register accesses it's fine the way you did in this version, but here I think a

Quoted 7 lines
Quoted 4 lines
	if (is_mpc512x_mmc())
		swap_buffer()

would look better.

the swapping function uses optimised st_le32() which is not available on ARM, that's the reason why this is build time selected.

Ah, ok. I think it's fine then.

Quoted 3 lines

Maybe the
"fixup" in the function name is not pretty, I can change it to "swap" then.

Yes, that would be better.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw

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

In reply to Anatolij Gustschin

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.

---
v2:
- only rebased

 drivers/mmc/host/mxcmmc.c |   31 ++++++++++++++++++++++++-------
 1 files changed, 24 insertions(+), 7 deletions(-)
drivers/mmc/host/mxcmmc.c +24 -7
index 2861e0d..23deb2a 100644
@@ -694,24 +694,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);
 	}
 }
 
@@ -1005,7 +1021,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
maintainer
#9 lore raw

Re: [PATCH v2 5/5] mmc: mxcmmc: fix race conditions for host->req and host->data access

Quoted 11 lines · Anatolij Gustschin

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.

This looks like a bugfix which should be first in this series.

Sascha

Quoted 72 lines

---
v2:
- only rebased

 drivers/mmc/host/mxcmmc.c |   31 ++++++++++++++++++++++++-------
 1 files changed, 24 insertions(+), 7 deletions(-)
drivers/mmc/host/mxcmmc.c +24 -7
index 2861e0d..23deb2a 100644
@@ -694,24 +694,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);
 	}
 }
 
@@ -1005,7 +1021,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
Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
maintainer Acked-by on the v2 series
#10 lore raw
In reply to Anatolij Gustschin
Quoted 9 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 v2 series are mentioned in each patch

With the two comments I made fixed you can add my:

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |