[ARM] 3565/1: AT91RM9200 MMC update

Patch from Andrew Victor

This patch includes code cleanups and minor fixes to the AT91RM9200 MMC
driver.

1. Replace calls to DBG() with pr_debug().
2. 'host' can never be null, so don't bother checking for that case.
3. Remove SA_SAMPLE_RANDOM from request_irq().  [Patch from Matt
Mackall]
4. clk_get() doesn't return NULL on error - need to test returned value
with IS_ERR().
5. Free resources if clk_get() or request_irq() fails.

Signed-off-by: Andrew Victor <andrew@sanpeople.com>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
diff --git a/drivers/mmc/at91_mci.c b/drivers/mmc/at91_mci.c
index 42284e1..3228516 100644
--- a/drivers/mmc/at91_mci.c
+++ b/drivers/mmc/at91_mci.c
@@ -81,13 +81,6 @@
 
 #undef	SUPPORT_4WIRE
 
-#ifdef CONFIG_MMC_DEBUG
-#define DBG(fmt...)	\
-	printk(fmt)
-#else
-#define DBG(fmt...)	do { } while (0)
-#endif
-
 static struct clk *mci_clk;
 
 #define FL_SENT_COMMAND (1 << 0)
@@ -202,50 +195,50 @@
 	struct mmc_command *cmd;
 	struct mmc_data *data;
 
-	DBG("pre dma read\n");
+	pr_debug("pre dma read\n");
 
 	cmd = host->cmd;
 	if (!cmd) {
-		DBG("no command\n");
+		pr_debug("no command\n");
 		return;
 	}
 
 	data = cmd->data;
 	if (!data) {
-		DBG("no data\n");
+		pr_debug("no data\n");
 		return;
 	}
 
 	for (i = 0; i < 2; i++) {
 		/* nothing left to transfer */
 		if (host->transfer_index >= data->sg_len) {
-			DBG("Nothing left to transfer (index = %d)\n", host->transfer_index);
+			pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
 			break;
 		}
 
 		/* Check to see if this needs filling */
 		if (i == 0) {
 			if (at91_mci_read(AT91_PDC_RCR) != 0) {
-				DBG("Transfer active in current\n");
+				pr_debug("Transfer active in current\n");
 				continue;
 			}
 		}
 		else {
 			if (at91_mci_read(AT91_PDC_RNCR) != 0) {
-				DBG("Transfer active in next\n");
+				pr_debug("Transfer active in next\n");
 				continue;
 			}
 		}
 
 		/* Setup the next transfer */
-		DBG("Using transfer index %d\n", host->transfer_index);
+		pr_debug("Using transfer index %d\n", host->transfer_index);
 
 		sg = &data->sg[host->transfer_index++];
-		DBG("sg = %p\n", sg);
+		pr_debug("sg = %p\n", sg);
 
 		sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);
 
-		DBG("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
+		pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
 
 		if (i == 0) {
 			at91_mci_write(AT91_PDC_RPR, sg->dma_address);
@@ -257,7 +250,7 @@
 		}
 	}
 
-	DBG("pre dma read done\n");
+	pr_debug("pre dma read done\n");
 }
 
 /*
@@ -268,17 +261,17 @@
 	struct mmc_command *cmd;
 	struct mmc_data *data;
 
-	DBG("post dma read\n");
+	pr_debug("post dma read\n");
 
 	cmd = host->cmd;
 	if (!cmd) {
-		DBG("no command\n");
+		pr_debug("no command\n");
 		return;
 	}
 
 	data = cmd->data;
 	if (!data) {
-		DBG("no data\n");
+		pr_debug("no data\n");
 		return;
 	}
 
@@ -289,17 +282,17 @@
 
 		struct scatterlist *sg;
 
-		DBG("finishing index %d\n", host->in_use_index);
+		pr_debug("finishing index %d\n", host->in_use_index);
 
 		sg = &data->sg[host->in_use_index++];
 
-		DBG("Unmapping page %08X\n", sg->dma_address);
+		pr_debug("Unmapping page %08X\n", sg->dma_address);
 
 		dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
 
 		/* Swap the contents of the buffer */
 		buffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
-		DBG("buffer = %p, length = %d\n", buffer, sg->length);
+		pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
 
 		data->bytes_xfered += sg->length;
 
@@ -320,7 +313,7 @@
 		at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
 	}
 
-	DBG("post dma read done\n");
+	pr_debug("post dma read done\n");
 }
 
 /*
@@ -331,7 +324,7 @@
 	struct mmc_command *cmd;
 	struct mmc_data *data;
 
-	DBG("Handling the transmit\n");
+	pr_debug("Handling the transmit\n");
 
 	/* Disable the transfer */
 	at91_mci_write(AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
@@ -387,12 +380,12 @@
 	/* Not sure if this is needed */
 #if 0
 	if ((at91_mci_read(AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
-		DBG("Clearing timeout\n");
+		pr_debug("Clearing timeout\n");
 		at91_mci_write(AT91_MCI_ARGR, 0);
 		at91_mci_write(AT91_MCI_CMDR, AT91_MCI_OPDCMD);
 		while (!(at91_mci_read(AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
 			/* spin */
-			DBG("Clearing: SR = %08X\n", at91_mci_read(AT91_MCI_SR));
+			pr_debug("Clearing: SR = %08X\n", at91_mci_read(AT91_MCI_SR));
 		}
 	}
 #endif
@@ -439,7 +432,7 @@
 	/*
 	 * Set the arguments and send the command
 	 */
-	DBG("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08lX)\n",
+	pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08lX)\n",
 		cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(AT91_MCI_MR));
 
 	if (!data) {
@@ -491,7 +484,7 @@
 
 			at91mci_sg_to_dma(host, data);
 
-			DBG("Transmitting %d bytes\n", host->total_length);
+			pr_debug("Transmitting %d bytes\n", host->total_length);
 
 			at91_mci_write(AT91_PDC_TPR, host->physical_address);
 			at91_mci_write(AT91_PDC_TCR, host->total_length / 4);
@@ -525,7 +518,7 @@
 
 	ier = at91_mci_send_command(host, cmd);
 
-	DBG("setting ier to %08X\n", ier);
+	pr_debug("setting ier to %08X\n", ier);
 
 	/* Stop on errors or the required value */
 	at91_mci_write(AT91_MCI_IER, 0xffff0000 | ier);
@@ -570,7 +563,7 @@
 
 	status = at91_mci_read(AT91_MCI_SR);
 
-	DBG("Status = %08X [%08X %08X %08X %08X]\n",
+	pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
 		 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
 
 	if (status & (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE |
@@ -590,7 +583,7 @@
 			else
 				cmd->error = MMC_ERR_FAILED;
 
-			DBG("Error detected and set to %d (cmd = %d, retries = %d)\n",
+			pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
 				 cmd->error, cmd->opcode, cmd->retries);
 		}
 	}
@@ -621,10 +614,7 @@
 	struct at91mci_host *host = mmc_priv(mmc);
 	unsigned long at91_master_clock = clk_get_rate(mci_clk);
 
-	if (host)
-		host->bus_mode = ios->bus_mode;
-	else
-		printk("MMC: No host for bus_mode\n");
+	host->bus_mode = ios->bus_mode;
 
 	if (ios->clock == 0) {
 		/* Disable the MCI controller */
@@ -640,15 +630,15 @@
 		else
 			clkdiv = (at91_master_clock / ios->clock) / 2;
 
-		DBG("clkdiv = %d. mcck = %ld\n", clkdiv,
+		pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
 			at91_master_clock / (2 * (clkdiv + 1)));
 	}
 	if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
-		DBG("MMC: Setting controller bus width to 4\n");
+		pr_debug("MMC: Setting controller bus width to 4\n");
 		at91_mci_write(AT91_MCI_SDCR, at91_mci_read(AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
 	}
 	else {
-		DBG("MMC: Setting controller bus width to 1\n");
+		pr_debug("MMC: Setting controller bus width to 1\n");
 		at91_mci_write(AT91_MCI_SDCR, at91_mci_read(AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
 	}
 
@@ -656,7 +646,7 @@
 	at91_mci_write(AT91_MCI_MR, (at91_mci_read(AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
 
 	/* maybe switch power to the card */
-	if (host && host->board->vcc_pin) {
+	if (host->board->vcc_pin) {
 		switch (ios->power_mode) {
 			case MMC_POWER_OFF:
 				at91_set_gpio_output(host->board->vcc_pin, 0);
@@ -679,11 +669,8 @@
 
 	unsigned int int_status;
 
-	if (host == NULL)
-		return IRQ_HANDLED;
-
 	int_status = at91_mci_read(AT91_MCI_SR);
-	DBG("MCI irq: status = %08X, %08lX, %08lX\n", int_status, at91_mci_read(AT91_MCI_IMR),
+	pr_debug("MCI irq: status = %08X, %08lX, %08lX\n", int_status, at91_mci_read(AT91_MCI_IMR),
 		int_status & at91_mci_read(AT91_MCI_IMR));
 
 	if ((int_status & at91_mci_read(AT91_MCI_IMR)) & 0xffff0000)
@@ -692,75 +679,75 @@
 	int_status &= at91_mci_read(AT91_MCI_IMR);
 
 	if (int_status & AT91_MCI_UNRE)
-		DBG("MMC: Underrun error\n");
+		pr_debug("MMC: Underrun error\n");
 	if (int_status & AT91_MCI_OVRE)
-		DBG("MMC: Overrun error\n");
+		pr_debug("MMC: Overrun error\n");
 	if (int_status & AT91_MCI_DTOE)
-		DBG("MMC: Data timeout\n");
+		pr_debug("MMC: Data timeout\n");
 	if (int_status & AT91_MCI_DCRCE)
-		DBG("MMC: CRC error in data\n");
+		pr_debug("MMC: CRC error in data\n");
 	if (int_status & AT91_MCI_RTOE)
-		DBG("MMC: Response timeout\n");
+		pr_debug("MMC: Response timeout\n");
 	if (int_status & AT91_MCI_RENDE)
-		DBG("MMC: Response end bit error\n");
+		pr_debug("MMC: Response end bit error\n");
 	if (int_status & AT91_MCI_RCRCE)
-		DBG("MMC: Response CRC error\n");
+		pr_debug("MMC: Response CRC error\n");
 	if (int_status & AT91_MCI_RDIRE)
-		DBG("MMC: Response direction error\n");
+		pr_debug("MMC: Response direction error\n");
 	if (int_status & AT91_MCI_RINDE)
-		DBG("MMC: Response index error\n");
+		pr_debug("MMC: Response index error\n");
 
 	/* Only continue processing if no errors */
 	if (!completed) {
 		if (int_status & AT91_MCI_TXBUFE) {
-			DBG("TX buffer empty\n");
+			pr_debug("TX buffer empty\n");
 			at91_mci_handle_transmitted(host);
 		}
 
 		if (int_status & AT91_MCI_RXBUFF) {
-			DBG("RX buffer full\n");
+			pr_debug("RX buffer full\n");
 			at91_mci_write(AT91_MCI_IER, AT91_MCI_CMDRDY);
 		}
 
 		if (int_status & AT91_MCI_ENDTX) {
-			DBG("Transmit has ended\n");
+			pr_debug("Transmit has ended\n");
 		}
 
 		if (int_status & AT91_MCI_ENDRX) {
-			DBG("Receive has ended\n");
+			pr_debug("Receive has ended\n");
 			at91mci_post_dma_read(host);
 		}
 
 		if (int_status & AT91_MCI_NOTBUSY) {
-			DBG("Card is ready\n");
+			pr_debug("Card is ready\n");
 			at91_mci_write(AT91_MCI_IER, AT91_MCI_CMDRDY);
 		}
 
 		if (int_status & AT91_MCI_DTIP) {
-			DBG("Data transfer in progress\n");
+			pr_debug("Data transfer in progress\n");
 		}
 
 		if (int_status & AT91_MCI_BLKE) {
-			DBG("Block transfer has ended\n");
+			pr_debug("Block transfer has ended\n");
 		}
 
 		if (int_status & AT91_MCI_TXRDY) {
-			DBG("Ready to transmit\n");
+			pr_debug("Ready to transmit\n");
 		}
 
 		if (int_status & AT91_MCI_RXRDY) {
-			DBG("Ready to receive\n");
+			pr_debug("Ready to receive\n");
 		}
 
 		if (int_status & AT91_MCI_CMDRDY) {
-			DBG("Command ready\n");
+			pr_debug("Command ready\n");
 			completed = 1;
 		}
 	}
 	at91_mci_write(AT91_MCI_IDR, int_status);
 
 	if (completed) {
-		DBG("Completed command\n");
+		pr_debug("Completed command\n");
 		at91_mci_write(AT91_MCI_IDR, 0xffffffff);
 		at91mci_completed_command(host);
 	}
@@ -779,10 +766,10 @@
 	 */
 	if (present != host->present) {
 		host->present = present;
-		DBG("%s: card %s\n", mmc_hostname(host->mmc),
+		pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
 			present ? "insert" : "remove");
 		if (!present) {
-			DBG("****** Resetting SD-card bus width ******\n");
+			pr_debug("****** Resetting SD-card bus width ******\n");
 			at91_mci_write(AT91_MCI_SDCR, 0);
 		}
 		mmc_detect_change(host->mmc, msecs_to_jiffies(100));
@@ -822,13 +809,13 @@
 	struct at91mci_host *host;
 	int ret;
 
-	DBG("Probe MCI devices\n");
+	pr_debug("Probe MCI devices\n");
 	at91_mci_disable();
 	at91_mci_enable();
 
 	mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
 	if (!mmc) {
-		DBG("Failed to allocate mmc host\n");
+		pr_debug("Failed to allocate mmc host\n");
 		return -ENOMEM;
 	}
 
@@ -854,8 +841,9 @@
 	 * Get Clock
 	 */
 	mci_clk = clk_get(&pdev->dev, "mci_clk");
-	if (!mci_clk) {
+	if (IS_ERR(mci_clk)) {
 		printk(KERN_ERR "AT91 MMC: no clock defined.\n");
+		mmc_free_host(mmc);
 		return -ENODEV;
 	}
 	clk_enable(mci_clk);			/* Enable the peripheral clock */
@@ -865,7 +853,10 @@
 	 */
 	ret = request_irq(AT91_ID_MCI, at91_mci_irq, SA_SHIRQ, DRIVER_NAME, host);
 	if (ret) {
-		DBG("Failed to request MCI interrupt\n");
+		printk(KERN_ERR "Failed to request MCI interrupt\n");
+		clk_disable(mci_clk);
+		clk_put(mci_clk);
+		mmc_free_host(mmc);
 		return ret;
 	}
 
@@ -886,12 +877,12 @@
 	 */
 	if (host->board->det_pin) {
 		ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
-				SA_SAMPLE_RANDOM, DRIVER_NAME, host);
+				0, DRIVER_NAME, host);
 		if (ret)
-			DBG("couldn't allocate MMC detect irq\n");
+			printk(KERN_ERR "couldn't allocate MMC detect irq\n");
 	}
 
-	DBG(KERN_INFO "Added MCI driver\n");
+	pr_debug(KERN_INFO "Added MCI driver\n");
 
 	return 0;
 }
@@ -924,7 +915,7 @@
 
 	platform_set_drvdata(pdev, NULL);
 
-	DBG("Removed\n");
+	pr_debug("MCI Removed\n");
 
 	return 0;
 }