tmio_mmc.c revision 544f277bb849da0ba86cfc4203a4c9139e2cd927
14a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/*
24a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *  linux/drivers/mmc/tmio_mmc.c
34a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
44a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *  Copyright (C) 2004 Ian Molton
54a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *  Copyright (C) 2007 Ian Molton
64a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
74a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * This program is free software; you can redistribute it and/or modify
84a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * it under the terms of the GNU General Public License version 2 as
94a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * published by the Free Software Foundation.
104a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * Driver for the MMC / SD / SDIO cell found in:
124a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
134a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * TC6393XB TC6391XB TC6387XB T7L66XB
144a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
154a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * This driver draws mainly on scattered spec sheets, Reverse engineering
164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * of the toshiba e800  SD driver and some parts of the 2.4 ASIC3 driver (4 bit
174a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * support). (Further 4 bit support from a later datasheet).
184a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
194a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * TODO:
204a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *   Investigate using a workqueue for PIO transfers
214a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *   Eliminate FIXMEs
224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *   SDIO support
234a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *   Better Power management
244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *   Handle MMC errors better
254a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *   double buffer support
264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
274a48998fa16121d0fe3436cce43afd6f47424103Ian Molton */
284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/module.h>
294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/irq.h>
304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/device.h>
314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/delay.h>
324a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/mmc/host.h>
334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/mfd/core.h>
344a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include <linux/mfd/tmio.h>
354a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
364a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#include "tmio_mmc.h"
374a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
384a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
394a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
40f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel	u32 clk = 0, clock, f_min = host->mmc->f_min;
414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (new_clock) {
43f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel		for (clock = f_min, clk = 0x100; new_clock >= (clock<<1); ) {
444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			clock <<= 1;
454a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			clk >>= 1;
464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		}
474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (clk & 0x1)
484a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			clk = 0x20000;
494a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
504a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		clk >>= 2;
515e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_config_write8(host, CNF_SD_CLK_MODE, (clk & 0x8000) ? 0 : 1);
524a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		clk |= 0x100;
534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
555e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk);
564a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
574a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
584a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
605e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	msleep(10);
625e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
635e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
644a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	msleep(10);
654a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
664a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
674a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void tmio_mmc_clk_start(struct tmio_mmc_host *host)
684a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
695e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
705e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
714a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	msleep(10);
725e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
734a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	msleep(10);
744a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
754a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
764a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void reset(struct tmio_mmc_host *host)
774a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
784a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* FIXME - should we set stop clock reg here */
795e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
805e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
814a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	msleep(10);
825e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
835e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
844a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	msleep(10);
854a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
864a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
874a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void
884a48998fa16121d0fe3436cce43afd6f47424103Ian Moltontmio_mmc_finish_request(struct tmio_mmc_host *host)
894a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
904a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_request *mrq = host->mrq;
914a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
924a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->mrq = NULL;
934a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->cmd = NULL;
944a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->data = NULL;
954a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
964a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc_request_done(host->mmc, mrq);
974a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
984a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
994a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/* These are the bitmasks the tmio chip requires to implement the MMC response
1004a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * types. Note that R1 and R6 are the same in this scheme. */
1014a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define APP_CMD        0x0040
1024a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define RESP_NONE      0x0300
1034a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define RESP_R1        0x0400
1044a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define RESP_R1B       0x0500
1054a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define RESP_R2        0x0600
1064a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define RESP_R3        0x0700
1074a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define DATA_PRESENT   0x0800
1084a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define TRANSFER_READ  0x1000
1094a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define TRANSFER_MULTI 0x2000
1104a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define SECURITY_CMD   0x4000
1114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1124a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int
1134a48998fa16121d0fe3436cce43afd6f47424103Ian Moltontmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
1144a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
1154a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_data *data = host->data;
1164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	int c = cmd->opcode;
1174a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1184a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Command 12 is handled by hardware */
1194a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (cmd->opcode == 12 && !cmd->arg) {
1205e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
1214a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return 0;
1224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
1234a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	switch (mmc_resp_type(cmd)) {
1254a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_RSP_NONE: c |= RESP_NONE; break;
1264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_RSP_R1:   c |= RESP_R1;   break;
1274a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_RSP_R1B:  c |= RESP_R1B;  break;
1284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_RSP_R2:   c |= RESP_R2;   break;
1294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_RSP_R3:   c |= RESP_R3;   break;
1304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	default:
1314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
1324a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return -EINVAL;
1334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
1344a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1354a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->cmd = cmd;
1364a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1374a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/* FIXME - this seems to be ok comented out but the spec suggest this bit should
1384a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *         be set when issuing app commands.
1394a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *	if(cmd->flags & MMC_FLAG_ACMD)
1404a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *		c |= APP_CMD;
1414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton */
1424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (data) {
1434a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		c |= DATA_PRESENT;
1444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (data->blocks > 1) {
1455e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
1464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			c |= TRANSFER_MULTI;
1474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		}
1484a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (data->flags & MMC_DATA_READ)
1494a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			c |= TRANSFER_READ;
1504a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
1514a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1525e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	enable_mmc_irqs(host, TMIO_MASK_CMD);
1534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Fire off the command */
1555e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
1565e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_SD_CMD, c);
1574a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1584a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return 0;
1594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
1604a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/* This chip always returns (at least?) as much data as you ask for.
1624a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * I'm unsure what happens if you ask for less than a block. This should be
1634a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * looked into to ensure that a funny length read doesnt hose the controller.
1644a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *
1654a48998fa16121d0fe3436cce43afd6f47424103Ian Molton */
1664a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic inline void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
1674a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
1684a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_data *data = host->data;
1694a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	unsigned short *buf;
1704a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	unsigned int count;
1714a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	unsigned long flags;
1724a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1734a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!data) {
1744a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("Spurious PIO IRQ\n");
1754a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return;
1764a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
1774a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1784a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	buf = (unsigned short *)(tmio_mmc_kmap_atomic(host, &flags) +
1794a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	      host->sg_off);
1804a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1814a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	count = host->sg_ptr->length - host->sg_off;
1824a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (count > data->blksz)
1834a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		count = data->blksz;
1844a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1854a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug("count: %08x offset: %08x flags %08x\n",
1864a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	    count, host->sg_off, data->flags);
1874a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1884a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Transfer the data */
1894a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (data->flags & MMC_DATA_READ)
1905e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
1914a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	else
1925e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
1934a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1944a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->sg_off += count;
1954a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1964a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	tmio_mmc_kunmap_atomic(host, &flags);
1974a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
1984a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (host->sg_off == host->sg_ptr->length)
1994a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		tmio_mmc_next_sg(host);
2004a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2014a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return;
2024a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
2034a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2044a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic inline void tmio_mmc_data_irq(struct tmio_mmc_host *host)
2054a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
2064a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_data *data = host->data;
207a0d045cac9bcb3e9a9796d596415f7ffb64852e2Julia Lawall	struct mmc_command *stop;
2084a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2094a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->data = NULL;
2104a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!data) {
2124a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("Spurious data end IRQ\n");
2134a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return;
2144a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
215a0d045cac9bcb3e9a9796d596415f7ffb64852e2Julia Lawall	stop = data->stop;
2164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2174a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* FIXME - return correct transfer count on errors */
2184a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!data->error)
2194a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		data->bytes_xfered = data->blocks * data->blksz;
2204a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	else
2214a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		data->bytes_xfered = 0;
2224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2234a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug("Completed data request\n");
2244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2254a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/*FIXME - other drivers allow an optional stop command of any given type
2264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 *        which we dont do, as the chip can auto generate them.
2274a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 *        Perhaps we can be smarter about when to use auto CMD12 and
2284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 *        only issue the auto request when we know this is the desired
2294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 *        stop command, allowing fallback to the stop command the
2304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 *        upper layers expect. For now, we do what works.
2314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 */
2324a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (data->flags & MMC_DATA_READ)
2345e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		disable_mmc_irqs(host, TMIO_MASK_READOP);
2354a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	else
2365e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
2374a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2384a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (stop) {
2394a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (stop->opcode == 12 && !stop->arg)
2405e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
2414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		else
2424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			BUG();
2434a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
2444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2454a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	tmio_mmc_finish_request(host);
2464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
2474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2484a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic inline void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
2494a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	unsigned int stat)
2504a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
2514a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_command *cmd = host->cmd;
2525e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	int i, addr;
2534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!host->cmd) {
2554a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("Spurious CMD irq\n");
2564a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return;
2574a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
2584a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->cmd = NULL;
2604a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* This controller is sicker than the PXA one. Not only do we need to
2624a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 * drop the top 8 bits of the first response word, we also need to
2634a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 * modify the order of the response for short response command types.
2644a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 */
2654a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2665e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
2675e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		cmd->resp[i] = sd_ctrl_read32(host, addr);
2684a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2694a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (cmd->flags &  MMC_RSP_136) {
2704a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
2714a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
2724a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
2734a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->resp[3] <<= 8;
2744a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	} else if (cmd->flags & MMC_RSP_R3) {
2754a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->resp[0] = cmd->resp[3];
2764a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
2774a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2784a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (stat & TMIO_STAT_CMDTIMEOUT)
2794a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->error = -ETIMEDOUT;
2804a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
2814a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cmd->error = -EILSEQ;
2824a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2834a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* If there is data to handle we enable data IRQs here, and
2844a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 * we will ultimatley finish the request in the data_end handler.
2854a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 * If theres no data or we encountered an error, finish now.
2864a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	 */
2874a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (host->data && !cmd->error) {
2884a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (host->data->flags & MMC_DATA_READ)
2895e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			enable_mmc_irqs(host, TMIO_MASK_READOP);
2904a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		else
2915e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
2924a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	} else {
2934a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		tmio_mmc_finish_request(host);
2944a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
2954a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2964a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return;
2974a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
2984a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
2994a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3004a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic irqreturn_t tmio_mmc_irq(int irq, void *devid)
3014a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
3024a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct tmio_mmc_host *host = devid;
3034a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	unsigned int ireg, irq_mask, status;
3044a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3054a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug("MMC IRQ begin\n");
3064a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3075e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	status = sd_ctrl_read32(host, CTL_STATUS);
3085e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
3094a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	ireg = status & TMIO_MASK_IRQ & ~irq_mask;
3104a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug_status(status);
3124a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug_status(ireg);
3134a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3144a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!ireg) {
3155e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		disable_mmc_irqs(host, status & ~irq_mask);
3164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3174a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("tmio_mmc: Spurious irq, disabling! "
3184a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			"0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
3194a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug_status(status);
3204a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3214a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto out;
3224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
3234a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	while (ireg) {
3254a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		/* Card insert / remove attempts */
3264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
3275e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
3284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton				TMIO_STAT_CARD_REMOVE);
3294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			mmc_detect_change(host->mmc, 0);
3304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		}
3314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3324a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		/* CRC and other errors */
3334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/*		if (ireg & TMIO_STAT_ERR_IRQ)
3344a48998fa16121d0fe3436cce43afd6f47424103Ian Molton *			handled |= tmio_error_irq(host, irq, stat);
3354a48998fa16121d0fe3436cce43afd6f47424103Ian Molton */
3364a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3374a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		/* Command completion */
3384a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ireg & TMIO_MASK_CMD) {
3395e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			ack_mmc_irqs(host, TMIO_MASK_CMD);
3404a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			tmio_mmc_cmd_irq(host, status);
3414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		}
3424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3434a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		/* Data transfer */
3444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
3455e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
3464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			tmio_mmc_pio_irq(host);
3474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		}
3484a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3494a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		/* Data transfer completion */
3504a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ireg & TMIO_STAT_DATAEND) {
3515e74672c0925335bb00772530634ac70179e8a19Philipp Zabel			ack_mmc_irqs(host, TMIO_STAT_DATAEND);
3524a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			tmio_mmc_data_irq(host);
3534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		}
3544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3554a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		/* Check status - keep going until we've handled it all */
3565e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		status = sd_ctrl_read32(host, CTL_STATUS);
3575e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
3584a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		ireg = status & TMIO_MASK_IRQ & ~irq_mask;
3594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3604a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("Status at end of loop: %08x\n", status);
3614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug_status(status);
3624a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
3634a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug("MMC IRQ end\n");
3644a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3654a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonout:
3664a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return IRQ_HANDLED;
3674a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
3684a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3694a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int tmio_mmc_start_data(struct tmio_mmc_host *host,
3704a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_data *data)
3714a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
3724a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	pr_debug("setup data transfer: blocksize %08x  nr_blocks %d\n",
3734a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	    data->blksz, data->blocks);
3744a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3754a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Hardware cannot perform 1 and 2 byte requests in 4 bit mode */
3764a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (data->blksz < 4 && host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
3774a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		printk(KERN_ERR "%s: %d byte block unsupported in 4 bit mode\n",
3784a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			mmc_hostname(host->mmc), data->blksz);
3794a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return -EINVAL;
3804a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
3814a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3824a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	tmio_mmc_init_sg(host, data);
3834a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->data = data;
3844a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3854a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Set transfer length / blocksize */
3865e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
3875e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
3884a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3894a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return 0;
3904a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
3914a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3924a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/* Process requests from the MMC layer */
3934a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
3944a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
3954a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct tmio_mmc_host *host = mmc_priv(mmc);
3964a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	int ret;
3974a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
3984a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (host->mrq)
3994a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		pr_debug("request not null\n");
4004a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4014a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->mrq = mrq;
4024a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4034a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (mrq->data) {
4044a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		ret = tmio_mmc_start_data(host, mrq->data);
4054a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ret)
4064a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			goto fail;
4074a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
4084a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4094a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	ret = tmio_mmc_start_command(host, mrq->cmd);
4104a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!ret)
4124a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		return;
4134a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4144a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonfail:
4154a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mrq->cmd->error = ret;
4164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc_request_done(mmc, mrq);
4174a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
4184a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4194a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/* Set MMC clock / power.
4204a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * Note: This controller uses a simple divider scheme therefore it cannot
4214a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
4224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * MMC wont run that fast, it has to be clocked at 12MHz which is the next
4234a48998fa16121d0fe3436cce43afd6f47424103Ian Molton * slowest setting.
4244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton */
4254a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
4264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
4274a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct tmio_mmc_host *host = mmc_priv(mmc);
4284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (ios->clock)
4304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		tmio_mmc_set_clock(host, ios->clock);
4314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4324a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Power sequence - OFF -> ON -> UP */
4334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	switch (ios->power_mode) {
4344a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_POWER_OFF: /* power down SD bus */
4355e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_config_write8(host, CNF_PWR_CTL_2, 0x00);
4364a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		tmio_mmc_clk_stop(host);
4374a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		break;
4384a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_POWER_ON: /* power up SD bus */
4394a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4405e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_config_write8(host, CNF_PWR_CTL_2, 0x02);
4414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		break;
4424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_POWER_UP: /* start bus clock */
4434a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		tmio_mmc_clk_start(host);
4444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		break;
4454a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
4464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	switch (ios->bus_width) {
4484a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_BUS_WIDTH_1:
4495e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
4504a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	break;
4514a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	case MMC_BUS_WIDTH_4:
4525e74672c0925335bb00772530634ac70179e8a19Philipp Zabel		sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
4534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	break;
4544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
4554a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4564a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Let things settle. delay taken from winCE driver */
4574a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	udelay(140);
4584a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
4594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4604a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int tmio_mmc_get_ro(struct mmc_host *mmc)
4614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
4624a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct tmio_mmc_host *host = mmc_priv(mmc);
4634a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4645e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	return (sd_ctrl_read16(host, CTL_STATUS) & TMIO_STAT_WRPROTECT) ? 0 : 1;
4654a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
4664a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4674a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic struct mmc_host_ops tmio_mmc_ops = {
4684a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.request	= tmio_mmc_request,
4694a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.set_ios	= tmio_mmc_set_ios,
4704a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.get_ro         = tmio_mmc_get_ro,
4714a48998fa16121d0fe3436cce43afd6f47424103Ian Molton};
4724a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4734a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#ifdef CONFIG_PM
4744a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
4754a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
4764a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mfd_cell	*cell = (struct mfd_cell *)dev->dev.platform_data;
4774a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_host *mmc = platform_get_drvdata(dev);
4784a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	int ret;
4794a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4804a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	ret = mmc_suspend_host(mmc, state);
4814a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4824a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Tell MFD core it can disable us now.*/
4834a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!ret && cell->disable)
4844a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		cell->disable(dev);
4854a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4864a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return ret;
4874a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
4884a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4894a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int tmio_mmc_resume(struct platform_device *dev)
4904a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
4914a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mfd_cell	*cell = (struct mfd_cell *)dev->dev.platform_data;
4924a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_host *mmc = platform_get_drvdata(dev);
4934a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct tmio_mmc_host *host = mmc_priv(mmc);
4944a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	int ret = 0;
4954a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
4964a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Tell the MFD core we are ready to be enabled */
4974a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (cell->enable) {
4984a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		ret = cell->enable(dev);
4994a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ret)
5004a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			goto out;
5014a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
5024a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
503544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel	/* Enable the MMC/SD Control registers */
504544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel	sd_config_write16(host, CNF_CMD, SDCREN);
505544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel	sd_config_write32(host, CNF_CTL_BASE,
506544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel		(dev->resource[0].start >> host->bus_shift) & 0xfffe);
507544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel
5084a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc_resume_host(mmc);
5094a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5104a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonout:
5114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return ret;
5124a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
5134a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#else
5144a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define tmio_mmc_suspend NULL
5154a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#define tmio_mmc_resume NULL
5164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton#endif
5174a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5184a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int __devinit tmio_mmc_probe(struct platform_device *dev)
5194a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
5204a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mfd_cell	*cell = (struct mfd_cell *)dev->dev.platform_data;
521f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel	struct tmio_mmc_data *pdata;
5224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct resource *res_ctl, *res_cnf;
5234a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct tmio_mmc_host *host;
5244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_host *mmc;
525d6c9b5ed37c26503795d241474a17db1d306e7eaPhilipp Zabel	int ret = -EINVAL;
5264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5274a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (dev->num_resources != 3)
5284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto out;
5294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
5314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	res_cnf = platform_get_resource(dev, IORESOURCE_MEM, 1);
532d6c9b5ed37c26503795d241474a17db1d306e7eaPhilipp Zabel	if (!res_ctl || !res_cnf)
5334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto out;
5344a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
535f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel	pdata = cell->driver_data;
536d6c9b5ed37c26503795d241474a17db1d306e7eaPhilipp Zabel	if (!pdata || !pdata->hclk)
537f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel		goto out;
538d6c9b5ed37c26503795d241474a17db1d306e7eaPhilipp Zabel
539d6c9b5ed37c26503795d241474a17db1d306e7eaPhilipp Zabel	ret = -ENOMEM;
540f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel
5414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
5424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!mmc)
5434a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto out;
5444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5454a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host = mmc_priv(mmc);
5464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	host->mmc = mmc;
5474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	platform_set_drvdata(dev, mmc);
5484a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5495e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	/* SD control register space size is 0x200, 0x400 for bus_shift=1 */
5505e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	host->bus_shift = resource_size(res_ctl) >> 10;
5515e74672c0925335bb00772530634ac70179e8a19Philipp Zabel
552bc6772a023ceab8df404b18b31c27f764dcf5b3fMagnus Damm	host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
5534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!host->ctl)
5544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto host_free;
5554a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
556bc6772a023ceab8df404b18b31c27f764dcf5b3fMagnus Damm	host->cnf = ioremap(res_cnf->start, resource_size(res_cnf));
5574a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (!host->cnf)
5584a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto unmap_ctl;
5594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5604a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc->ops = &tmio_mmc_ops;
5614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc->caps = MMC_CAP_4_BIT_DATA;
562f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel	mmc->f_max = pdata->hclk;
563f0e46cc4971f6be96010d9248e0fc076b229d989Philipp Zabel	mmc->f_min = mmc->f_max / 512;
5644a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
5654a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5664a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Tell the MFD core we are ready to be enabled */
5674a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (cell->enable) {
5684a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		ret = cell->enable(dev);
5694a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		if (ret)
5704a48998fa16121d0fe3436cce43afd6f47424103Ian Molton			goto unmap_cnf;
5714a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
5724a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
573544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel	/* Enable the MMC/SD Control registers */
574544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel	sd_config_write16(host, CNF_CMD, SDCREN);
575544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel	sd_config_write32(host, CNF_CTL_BASE,
576544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel		(dev->resource[0].start >> host->bus_shift) & 0xfffe);
577544f277bb849da0ba86cfc4203a4c9139e2cd927Philipp Zabel
5784a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Disable SD power during suspend */
5795e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_config_write8(host, CNF_PWR_CTL_3, 0x01);
5804a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5814a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* The below is required but why? FIXME */
5825e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_config_write8(host, CNF_STOP_CLK_CTL, 0x1f);
5834a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5844a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Power down SD bus*/
5855e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	sd_config_write8(host, CNF_PWR_CTL_2, 0x00);
5864a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5874a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	tmio_mmc_clk_stop(host);
5884a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	reset(host);
5894a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5904a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	ret = platform_get_irq(dev, 0);
5914a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (ret >= 0)
5924a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		host->irq = ret;
5934a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	else
5944a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto unmap_cnf;
5954a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5965e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	disable_mmc_irqs(host, TMIO_MASK_ALL);
5974a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
5986c413cc76b893310b3b258b7de47fb74dcc50203Philipp Zabel	ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
5996c413cc76b893310b3b258b7de47fb74dcc50203Philipp Zabel		IRQF_TRIGGER_FALLING, "tmio-mmc", host);
6004a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (ret)
6014a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		goto unmap_cnf;
6024a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6034a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc_add_host(mmc);
6044a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6054a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	printk(KERN_INFO "%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
6064a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	       (unsigned long)host->ctl, host->irq);
6074a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6084a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	/* Unmask the IRQs we want to know about */
6095e74672c0925335bb00772530634ac70179e8a19Philipp Zabel	enable_mmc_irqs(host, TMIO_MASK_IRQ);
6104a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6114a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return 0;
6124a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6134a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonunmap_cnf:
6144a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	iounmap(host->cnf);
6154a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonunmap_ctl:
6164a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	iounmap(host->ctl);
6174a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonhost_free:
6184a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	mmc_free_host(mmc);
6194a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonout:
6204a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return ret;
6214a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
6224a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6234a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int __devexit tmio_mmc_remove(struct platform_device *dev)
6244a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
6254a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	struct mmc_host *mmc = platform_get_drvdata(dev);
6264a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6274a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	platform_set_drvdata(dev, NULL);
6284a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6294a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	if (mmc) {
6304a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		struct tmio_mmc_host *host = mmc_priv(mmc);
6314a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		mmc_remove_host(mmc);
6324a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		free_irq(host->irq, host);
6334a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		iounmap(host->ctl);
6344a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		iounmap(host->cnf);
635bedcc45c2e5d72b1c4b087b725c391441a93eee6Magnus Damm		mmc_free_host(mmc);
6364a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	}
6374a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6384a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return 0;
6394a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
6404a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6414a48998fa16121d0fe3436cce43afd6f47424103Ian Molton/* ------------------- device registration ----------------------- */
6424a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6434a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic struct platform_driver tmio_mmc_driver = {
6444a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.driver = {
6454a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		.name = "tmio-mmc",
6464a48998fa16121d0fe3436cce43afd6f47424103Ian Molton		.owner = THIS_MODULE,
6474a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	},
6484a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.probe = tmio_mmc_probe,
6494a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.remove = __devexit_p(tmio_mmc_remove),
6504a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.suspend = tmio_mmc_suspend,
6514a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	.resume = tmio_mmc_resume,
6524a48998fa16121d0fe3436cce43afd6f47424103Ian Molton};
6534a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6544a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6554a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic int __init tmio_mmc_init(void)
6564a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
6574a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	return platform_driver_register(&tmio_mmc_driver);
6584a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
6594a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6604a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonstatic void __exit tmio_mmc_exit(void)
6614a48998fa16121d0fe3436cce43afd6f47424103Ian Molton{
6624a48998fa16121d0fe3436cce43afd6f47424103Ian Molton	platform_driver_unregister(&tmio_mmc_driver);
6634a48998fa16121d0fe3436cce43afd6f47424103Ian Molton}
6644a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6654a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonmodule_init(tmio_mmc_init);
6664a48998fa16121d0fe3436cce43afd6f47424103Ian Moltonmodule_exit(tmio_mmc_exit);
6674a48998fa16121d0fe3436cce43afd6f47424103Ian Molton
6684a48998fa16121d0fe3436cce43afd6f47424103Ian MoltonMODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
6694a48998fa16121d0fe3436cce43afd6f47424103Ian MoltonMODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
6704a48998fa16121d0fe3436cce43afd6f47424103Ian MoltonMODULE_LICENSE("GPL v2");
6714a48998fa16121d0fe3436cce43afd6f47424103Ian MoltonMODULE_ALIAS("platform:tmio-mmc");
672