au1xmmc.c 27.8 KB
Newer Older
1
/*
P
Pierre Ossman 已提交
2
 * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *
 *  Copyright (c) 2005, Advanced Micro Devices, Inc.
 *
 *  Developed with help from the 2.4.30 MMC AU1XXX controller including
 *  the following copyright notices:
 *     Copyright (c) 2003-2004 Embedded Edge, LLC.
 *     Portions Copyright (C) 2002 Embedix, Inc
 *     Copyright 2002 Hewlett-Packard Company

 *  2.6 version of this driver inspired by:
 *     (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
 *     All Rights Reserved.
 *     (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
 *     All Rights Reserved.
 *

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/* Why is a timer used to detect insert events?
 *
 * From the AU1100 MMC application guide:
 * If the Au1100-based design is intended to support both MultiMediaCards
 * and 1- or 4-data bit SecureDigital cards, then the solution is to
 * connect a weak (560KOhm) pull-up resistor to connector pin 1.
 * In doing so, a MMC card never enters SPI-mode communications,
 * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
 * (the low to high transition will not occur).
 *
 * So we use the timer to check the status manually.
 */

#include <linux/module.h>
#include <linux/init.h>
39
#include <linux/platform_device.h>
40 41 42
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
A
Al Viro 已提交
43
#include <linux/scatterlist.h>
44
#include <linux/leds.h>
45
#include <linux/mmc/host.h>
46

47 48 49 50 51 52 53 54
#include <asm/io.h>
#include <asm/mach-au1x00/au1000.h>
#include <asm/mach-au1x00/au1xxx_dbdma.h>
#include <asm/mach-au1x00/au1100_mmc.h>

#define DRIVER_NAME "au1xxx-mmc"

/* Set this to enable special debugging macros */
55
/* #define DEBUG */
56

57
#ifdef DEBUG
M
Manuel Lauss 已提交
58 59
#define DBG(fmt, idx, args...)	\
	printk(KERN_DEBUG "au1xmmc(%d): DEBUG: " fmt, idx, ##args)
60
#else
M
Manuel Lauss 已提交
61
#define DBG(fmt, idx, args...) do {} while (0)
62 63
#endif

M
Manuel Lauss 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/* Hardware definitions */
#define AU1XMMC_DESCRIPTOR_COUNT 1
#define AU1XMMC_DESCRIPTOR_SIZE  2048

#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
		     MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
		     MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)

/* This gives us a hard value for the stop command that we can write directly
 * to the command register.
 */
#define STOP_CMD	\
	(SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)

/* This is the set of interrupts that we configure by default. */
#define AU1XMMC_INTERRUPTS 				\
	(SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT |	\
	 SD_CONFIG_CR | SD_CONFIG_I)

/* The poll event (looking for insert/remove events runs twice a second. */
#define AU1XMMC_DETECT_TIMEOUT (HZ/2)

struct au1xmmc_host {
	struct mmc_host *mmc;
	struct mmc_request *mrq;

	u32 flags;
	u32 iobase;
	u32 clock;
	u32 bus_width;
	u32 power_mode;

	int status;

	struct {
		int len;
		int dir;
	} dma;

	struct {
		int index;
		int offset;
		int len;
	} pio;

	u32 tx_chan;
	u32 rx_chan;

	int irq;

	struct timer_list timer;
	struct tasklet_struct finish_task;
	struct tasklet_struct data_task;
	struct au1xmmc_platform_data *platdata;
	struct platform_device *pdev;
	struct resource *ioarea;
};

/* Status flags used by the host structure */
#define HOST_F_XMIT	0x0001
#define HOST_F_RECV	0x0002
#define HOST_F_DMA	0x0010
#define HOST_F_ACTIVE	0x0100
#define HOST_F_STOP	0x1000

#define HOST_S_IDLE	0x0001
#define HOST_S_CMD	0x0002
#define HOST_S_DATA	0x0003
#define HOST_S_STOP	0x0004

/* Easy access macros */
#define HOST_STATUS(h)	((h)->iobase + SD_STATUS)
#define HOST_CONFIG(h)	((h)->iobase + SD_CONFIG)
#define HOST_ENABLE(h)	((h)->iobase + SD_ENABLE)
#define HOST_TXPORT(h)	((h)->iobase + SD_TXPORT)
#define HOST_RXPORT(h)	((h)->iobase + SD_RXPORT)
#define HOST_CMDARG(h)	((h)->iobase + SD_CMDARG)
#define HOST_BLKSIZE(h)	((h)->iobase + SD_BLKSIZE)
#define HOST_CMD(h)	((h)->iobase + SD_CMD)
#define HOST_CONFIG2(h)	((h)->iobase + SD_CONFIG2)
#define HOST_TIMEOUT(h)	((h)->iobase + SD_TIMEOUT)
#define HOST_DEBUG(h)	((h)->iobase + SD_DEBUG)

#define DMA_CHANNEL(h)	\
	(((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
{
	u32 val = au_readl(HOST_CONFIG(host));
	val |= mask;
	au_writel(val, HOST_CONFIG(host));
	au_sync();
}

static inline void FLUSH_FIFO(struct au1xmmc_host *host)
{
	u32 val = au_readl(HOST_CONFIG2(host));

	au_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
	au_sync_delay(1);

	/* SEND_STOP will turn off clock control - this re-enables it */
	val &= ~SD_CONFIG2_DF;

	au_writel(val, HOST_CONFIG2(host));
	au_sync();
}

static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
{
	u32 val = au_readl(HOST_CONFIG(host));
	val &= ~mask;
	au_writel(val, HOST_CONFIG(host));
	au_sync();
}

static inline void SEND_STOP(struct au1xmmc_host *host)
{
182
	u32 config2;
183 184 185 186

	WARN_ON(host->status != HOST_S_DATA);
	host->status = HOST_S_STOP;

187 188
	config2 = au_readl(HOST_CONFIG2(host));
	au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
189 190 191 192 193 194 195 196
	au_sync();

	/* Send the stop commmand */
	au_writel(STOP_CMD, HOST_CMD(host));
}

static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
{
197 198
	if (host->platdata && host->platdata->set_power)
		host->platdata->set_power(host->mmc, state);
199 200
}

201
static int au1xmmc_card_inserted(struct au1xmmc_host *host)
202
{
203 204 205 206 207 208 209 210
	int ret;

	if (host->platdata && host->platdata->card_inserted)
		ret = host->platdata->card_inserted(host->mmc);
	else
		ret = 1;	/* assume there is a card */

	return ret;
211 212
}

213
static int au1xmmc_card_readonly(struct mmc_host *mmc)
214
{
215
	struct au1xmmc_host *host = mmc_priv(mmc);
216 217 218 219 220 221 222 223
	int ret;

	if (host->platdata && host->platdata->card_readonly)
		ret = host->platdata->card_readonly(mmc);
	else
		ret = 0;	/* assume card is read-write */

	return ret;
224 225 226 227 228 229 230
}

static void au1xmmc_finish_request(struct au1xmmc_host *host)
{
	struct mmc_request *mrq = host->mrq;

	host->mrq = NULL;
231
	host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	host->dma.len = 0;
	host->dma.dir = 0;

	host->pio.index  = 0;
	host->pio.offset = 0;
	host->pio.len = 0;

	host->status = HOST_S_IDLE;

	mmc_request_done(host->mmc, mrq);
}

static void au1xmmc_tasklet_finish(unsigned long param)
{
	struct au1xmmc_host *host = (struct au1xmmc_host *) param;
	au1xmmc_finish_request(host);
}

static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
P
Pierre Ossman 已提交
252
				struct mmc_command *cmd, struct mmc_data *data)
253 254 255
{
	u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);

256
	switch (mmc_resp_type(cmd)) {
257 258
	case MMC_RSP_NONE:
		break;
259 260 261 262 263 264 265 266 267 268 269 270
	case MMC_RSP_R1:
		mmccmd |= SD_CMD_RT_1;
		break;
	case MMC_RSP_R1B:
		mmccmd |= SD_CMD_RT_1B;
		break;
	case MMC_RSP_R2:
		mmccmd |= SD_CMD_RT_2;
		break;
	case MMC_RSP_R3:
		mmccmd |= SD_CMD_RT_3;
		break;
271 272 273
	default:
		printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
			mmc_resp_type(cmd));
P
Pierre Ossman 已提交
274
		return -EINVAL;
275 276
	}

P
Pierre Ossman 已提交
277
	if (data) {
P
Pierre Ossman 已提交
278
		if (data->flags & MMC_DATA_READ) {
P
Pierre Ossman 已提交
279 280 281 282
			if (data->blocks > 1)
				mmccmd |= SD_CMD_CT_4;
			else
				mmccmd |= SD_CMD_CT_2;
P
Pierre Ossman 已提交
283
		} else if (data->flags & MMC_DATA_WRITE) {
P
Pierre Ossman 已提交
284 285 286 287 288
			if (data->blocks > 1)
				mmccmd |= SD_CMD_CT_3;
			else
				mmccmd |= SD_CMD_CT_1;
		}
289 290 291 292 293 294 295 296 297 298 299 300
	}

	au_writel(cmd->arg, HOST_CMDARG(host));
	au_sync();

	if (wait)
		IRQ_OFF(host, SD_CONFIG_CR);

	au_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
	au_sync();

	/* Wait for the command to go on the line */
M
Manuel Lauss 已提交
301 302
	while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
		/* nop */;
303 304 305 306 307

	/* Wait for the command to come back */
	if (wait) {
		u32 status = au_readl(HOST_STATUS(host));

M
Manuel Lauss 已提交
308
		while (!(status & SD_STATUS_CR))
309 310 311 312 313 314 315 316
			status = au_readl(HOST_STATUS(host));

		/* Clear the CR status */
		au_writel(SD_STATUS_CR, HOST_STATUS(host));

		IRQ_ON(host, SD_CONFIG_CR);
	}

P
Pierre Ossman 已提交
317
	return 0;
318 319 320 321 322 323 324 325
}

static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
{
	struct mmc_request *mrq = host->mrq;
	struct mmc_data *data;
	u32 crc;

M
Manuel Lauss 已提交
326
	WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
327 328 329 330 331 332 333 334 335 336

	if (host->mrq == NULL)
		return;

	data = mrq->cmd->data;

	if (status == 0)
		status = au_readl(HOST_STATUS(host));

	/* The transaction is really over when the SD_STATUS_DB bit is clear */
M
Manuel Lauss 已提交
337
	while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
338 339
		status = au_readl(HOST_STATUS(host));

P
Pierre Ossman 已提交
340
	data->error = 0;
341 342 343 344 345 346 347 348
	dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);

        /* Process any errors */
	crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
	if (host->flags & HOST_F_XMIT)
		crc |= ((status & 0x07) == 0x02) ? 0 : 1;

	if (crc)
P
Pierre Ossman 已提交
349
		data->error = -EILSEQ;
350 351 352 353 354 355

	/* Clear the CRC bits */
	au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));

	data->bytes_xfered = 0;

P
Pierre Ossman 已提交
356
	if (!data->error) {
357
		if (host->flags & HOST_F_DMA) {
358
#ifdef CONFIG_SOC_AU1200	/* DBDMA */
359 360
			u32 chan = DMA_CHANNEL(host);

M
Manuel Lauss 已提交
361
			chan_tab_t *c = *((chan_tab_t **)chan);
362 363
			au1x_dma_chan_t *cp = c->chan_ptr;
			data->bytes_xfered = cp->ddma_bytecnt;
364
#endif
M
Manuel Lauss 已提交
365
		} else
366
			data->bytes_xfered =
M
Manuel Lauss 已提交
367
				(data->blocks * data->blksz) - host->pio.len;
368 369 370 371 372 373 374
	}

	au1xmmc_finish_request(host);
}

static void au1xmmc_tasklet_data(unsigned long param)
{
M
Manuel Lauss 已提交
375
	struct au1xmmc_host *host = (struct au1xmmc_host *)param;
376 377 378 379 380 381 382 383 384

	u32 status = au_readl(HOST_STATUS(host));
	au1xmmc_data_complete(host, status);
}

#define AU1XMMC_MAX_TRANSFER 8

static void au1xmmc_send_pio(struct au1xmmc_host *host)
{
M
Manuel Lauss 已提交
385 386 387 388
	struct mmc_data *data;
	int sg_len, max, count;
	unsigned char *sg_ptr, val;
	u32 status;
389 390 391 392 393 394 395 396 397
	struct scatterlist *sg;

	data = host->mrq->data;

	if (!(host->flags & HOST_F_XMIT))
		return;

	/* This is the pointer to the data buffer */
	sg = &data->sg[host->pio.index];
J
Jens Axboe 已提交
398
	sg_ptr = sg_virt(sg) + host->pio.offset;
399 400 401 402

	/* This is the space left inside the buffer */
	sg_len = data->sg[host->pio.index].length - host->pio.offset;

M
Manuel Lauss 已提交
403
	/* Check if we need less than the size of the sg_buffer */
404
	max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
M
Manuel Lauss 已提交
405 406
	if (max > AU1XMMC_MAX_TRANSFER)
		max = AU1XMMC_MAX_TRANSFER;
407

M
Manuel Lauss 已提交
408
	for (count = 0; count < max; count++) {
409 410 411 412 413 414 415
		status = au_readl(HOST_STATUS(host));

		if (!(status & SD_STATUS_TH))
			break;

		val = *sg_ptr++;

M
Manuel Lauss 已提交
416
		au_writel((unsigned long)val, HOST_TXPORT(host));
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
		au_sync();
	}

	host->pio.len -= count;
	host->pio.offset += count;

	if (count == sg_len) {
		host->pio.index++;
		host->pio.offset = 0;
	}

	if (host->pio.len == 0) {
		IRQ_OFF(host, SD_CONFIG_TH);

		if (host->flags & HOST_F_STOP)
			SEND_STOP(host);

		tasklet_schedule(&host->data_task);
	}
}

static void au1xmmc_receive_pio(struct au1xmmc_host *host)
{
M
Manuel Lauss 已提交
440 441 442 443
	struct mmc_data *data;
	int max, count, sg_len = 0;
	unsigned char *sg_ptr = NULL;
	u32 status, val;
444 445 446 447 448 449 450 451 452 453 454
	struct scatterlist *sg;

	data = host->mrq->data;

	if (!(host->flags & HOST_F_RECV))
		return;

	max = host->pio.len;

	if (host->pio.index < host->dma.len) {
		sg = &data->sg[host->pio.index];
J
Jens Axboe 已提交
455
		sg_ptr = sg_virt(sg) + host->pio.offset;
456 457 458 459

		/* This is the space left inside the buffer */
		sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;

M
Manuel Lauss 已提交
460 461 462
		/* Check if we need less than the size of the sg_buffer */
		if (sg_len < max)
			max = sg_len;
463 464 465 466 467
	}

	if (max > AU1XMMC_MAX_TRANSFER)
		max = AU1XMMC_MAX_TRANSFER;

M
Manuel Lauss 已提交
468
	for (count = 0; count < max; count++) {
469 470 471 472 473 474
		status = au_readl(HOST_STATUS(host));

		if (!(status & SD_STATUS_NE))
			break;

		if (status & SD_STATUS_RC) {
475
			DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
476 477 478 479 480
					host->pio.len, count);
			break;
		}

		if (status & SD_STATUS_RO) {
481
			DBG("RX Overrun [%d + %d]\n", host->pdev->id,
482 483 484 485
					host->pio.len, count);
			break;
		}
		else if (status & SD_STATUS_RU) {
486
			DBG("RX Underrun [%d + %d]\n", host->pdev->id,
487 488 489 490 491 492 493
					host->pio.len,	count);
			break;
		}

		val = au_readl(HOST_RXPORT(host));

		if (sg_ptr)
M
Manuel Lauss 已提交
494
			*sg_ptr++ = (unsigned char)(val & 0xFF);
495 496 497 498 499 500 501 502 503 504 505
	}

	host->pio.len -= count;
	host->pio.offset += count;

	if (sg_len && count == sg_len) {
		host->pio.index++;
		host->pio.offset = 0;
	}

	if (host->pio.len == 0) {
M
Manuel Lauss 已提交
506
		/* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
507 508 509 510 511 512 513 514 515
		IRQ_OFF(host, SD_CONFIG_NE);

		if (host->flags & HOST_F_STOP)
			SEND_STOP(host);

		tasklet_schedule(&host->data_task);
	}
}

M
Manuel Lauss 已提交
516 517 518
/* This is called when a command has been completed - grab the response
 * and check for errors.  Then start the data transfer if it is indicated.
 */
519 520 521 522
static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
{
	struct mmc_request *mrq = host->mrq;
	struct mmc_command *cmd;
M
Manuel Lauss 已提交
523 524
	u32 r[4];
	int i, trans;
525 526 527 528 529

	if (!host->mrq)
		return;

	cmd = mrq->cmd;
P
Pierre Ossman 已提交
530
	cmd->error = 0;
531

R
Russell King 已提交
532 533 534 535 536 537 538 539 540
	if (cmd->flags & MMC_RSP_PRESENT) {
		if (cmd->flags & MMC_RSP_136) {
			r[0] = au_readl(host->iobase + SD_RESP3);
			r[1] = au_readl(host->iobase + SD_RESP2);
			r[2] = au_readl(host->iobase + SD_RESP1);
			r[3] = au_readl(host->iobase + SD_RESP0);

			/* The CRC is omitted from the response, so really
			 * we only got 120 bytes, but the engine expects
M
Manuel Lauss 已提交
541
			 * 128 bits, so we have to shift things up.
R
Russell King 已提交
542
			 */
M
Manuel Lauss 已提交
543
			for (i = 0; i < 4; i++) {
R
Russell King 已提交
544 545 546 547 548 549 550 551 552 553
				cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
				if (i != 3)
					cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
			}
		} else {
			/* Techincally, we should be getting all 48 bits of
			 * the response (SD_RESP1 + SD_RESP2), but because
			 * our response omits the CRC, our data ends up
			 * being shifted 8 bits to the right.  In this case,
			 * that means that the OSR data starts at bit 31,
M
Manuel Lauss 已提交
554
			 * so we can just read RESP0 and return that.
R
Russell King 已提交
555 556
			 */
			cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
557 558 559 560 561
		}
	}

        /* Figure out errors */
	if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
P
Pierre Ossman 已提交
562
		cmd->error = -EILSEQ;
563 564 565

	trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);

P
Pierre Ossman 已提交
566
	if (!trans || cmd->error) {
M
Manuel Lauss 已提交
567
		IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
568 569 570 571 572 573 574
		tasklet_schedule(&host->finish_task);
		return;
	}

	host->status = HOST_S_DATA;

	if (host->flags & HOST_F_DMA) {
575
#ifdef CONFIG_SOC_AU1200	/* DBDMA */
576 577 578 579 580 581 582 583 584 585 586 587
		u32 channel = DMA_CHANNEL(host);

		/* Start the DMA as soon as the buffer gets something in it */

		if (host->flags & HOST_F_RECV) {
			u32 mask = SD_STATUS_DB | SD_STATUS_NE;

			while((status & mask) != mask)
				status = au_readl(HOST_STATUS(host));
		}

		au1xxx_dbdma_start(channel);
588
#endif
589 590 591 592 593 594 595 596 597 598
	}
}

static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
{
	unsigned int pbus = get_au1x00_speed();
	unsigned int divisor;
	u32 config;

	/* From databook:
M
Manuel Lauss 已提交
599 600
	 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
	 */
601 602 603 604 605 606 607 608 609 610 611 612 613
	pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
	pbus /= 2;
	divisor = ((pbus / rate) / 2) - 1;

	config = au_readl(HOST_CONFIG(host));

	config &= ~(SD_CONFIG_DIV);
	config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;

	au_writel(config, HOST_CONFIG(host));
	au_sync();
}

M
Manuel Lauss 已提交
614 615
static int au1xmmc_prepare_data(struct au1xmmc_host *host,
				struct mmc_data *data)
616
{
617
	int datalen = data->blocks * data->blksz;
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

	if (data->flags & MMC_DATA_READ)
		host->flags |= HOST_F_RECV;
	else
		host->flags |= HOST_F_XMIT;

	if (host->mrq->stop)
		host->flags |= HOST_F_STOP;

	host->dma.dir = DMA_BIDIRECTIONAL;

	host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
				   data->sg_len, host->dma.dir);

	if (host->dma.len == 0)
P
Pierre Ossman 已提交
633
		return -ETIMEDOUT;
634

635
	au_writel(data->blksz - 1, HOST_BLKSIZE(host));
636 637

	if (host->flags & HOST_F_DMA) {
638
#ifdef CONFIG_SOC_AU1200	/* DBDMA */
639 640 641 642 643
		int i;
		u32 channel = DMA_CHANNEL(host);

		au1xxx_dbdma_stop(channel);

M
Manuel Lauss 已提交
644
		for (i = 0; i < host->dma.len; i++) {
645 646 647 648 649 650 651 652 653
			u32 ret = 0, flags = DDMA_FLAGS_NOIE;
			struct scatterlist *sg = &data->sg[i];
			int sg_len = sg->length;

			int len = (datalen > sg_len) ? sg_len : datalen;

			if (i == host->dma.len - 1)
				flags = DDMA_FLAGS_IE;

M
Manuel Lauss 已提交
654 655 656 657 658 659
			if (host->flags & HOST_F_XMIT) {
				ret = au1xxx_dbdma_put_source_flags(channel,
					(void *)sg_virt(sg), len, flags);
			} else {
				ret = au1xxx_dbdma_put_dest_flags(channel,
					(void *)sg_virt(sg), len, flags);
660 661
			}

662
			if (!ret)
663 664 665 666
				goto dataerr;

			datalen -= len;
		}
667
#endif
M
Manuel Lauss 已提交
668
	} else {
669 670 671 672 673 674 675 676
		host->pio.index = 0;
		host->pio.offset = 0;
		host->pio.len = datalen;

		if (host->flags & HOST_F_XMIT)
			IRQ_ON(host, SD_CONFIG_TH);
		else
			IRQ_ON(host, SD_CONFIG_NE);
M
Manuel Lauss 已提交
677
			/* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
678 679
	}

P
Pierre Ossman 已提交
680
	return 0;
681

682 683 684
dataerr:
	dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
			host->dma.dir);
P
Pierre Ossman 已提交
685
	return -ETIMEDOUT;
686 687
}

M
Manuel Lauss 已提交
688
/* This actually starts a command or data transaction */
689 690 691
static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
{
	struct au1xmmc_host *host = mmc_priv(mmc);
P
Pierre Ossman 已提交
692
	int ret = 0;
693 694 695 696 697 698 699 700 701 702 703 704

	WARN_ON(irqs_disabled());
	WARN_ON(host->status != HOST_S_IDLE);

	host->mrq = mrq;
	host->status = HOST_S_CMD;

	if (mrq->data) {
		FLUSH_FIFO(host);
		ret = au1xmmc_prepare_data(host, mrq->data);
	}

P
Pierre Ossman 已提交
705
	if (!ret)
P
Pierre Ossman 已提交
706
		ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
707

P
Pierre Ossman 已提交
708
	if (ret) {
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
		mrq->cmd->error = ret;
		au1xmmc_finish_request(host);
	}
}

static void au1xmmc_reset_controller(struct au1xmmc_host *host)
{
	/* Apply the clock */
	au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
        au_sync_delay(1);

	au_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
	au_sync_delay(5);

	au_writel(~0, HOST_STATUS(host));
	au_sync();

	au_writel(0, HOST_BLKSIZE(host));
	au_writel(0x001fffff, HOST_TIMEOUT(host));
	au_sync();

	au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
        au_sync();

	au_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
	au_sync_delay(1);

	au_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
	au_sync();

	/* Configure interrupts */
	au_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
	au_sync();
}


M
Manuel Lauss 已提交
745
static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
746 747
{
	struct au1xmmc_host *host = mmc_priv(mmc);
748
	u32 config2;
749 750 751 752 753 754 755 756 757 758 759

	if (ios->power_mode == MMC_POWER_OFF)
		au1xmmc_set_power(host, 0);
	else if (ios->power_mode == MMC_POWER_ON) {
		au1xmmc_set_power(host, 1);
	}

	if (ios->clock && ios->clock != host->clock) {
		au1xmmc_set_clock(host, ios->clock);
		host->clock = ios->clock;
	}
760 761 762 763 764 765 766 767 768 769 770 771

	config2 = au_readl(HOST_CONFIG2(host));
	switch (ios->bus_width) {
	case MMC_BUS_WIDTH_4:
		config2 |= SD_CONFIG2_WB;
		break;
	case MMC_BUS_WIDTH_1:
		config2 &= ~SD_CONFIG2_WB;
		break;
	}
	au_writel(config2, HOST_CONFIG2(host));
	au_sync();
772 773 774 775 776 777
}

#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
#define STATUS_DATA_IN  (SD_STATUS_NE)
#define STATUS_DATA_OUT (SD_STATUS_TH)

778
static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
779
{
780
	struct au1xmmc_host *host = dev_id;
781 782
	u32 status;

783
	status = au_readl(HOST_STATUS(host));
784

785 786
	if (!(status & SD_STATUS_I))
		return IRQ_NONE;	/* not ours */
787

M
Manuel Lauss 已提交
788 789 790
	if (status & SD_STATUS_SI)	/* SDIO */
		mmc_signal_sdio_irq(host->mmc);

791 792 793 794 795
	if (host->mrq && (status & STATUS_TIMEOUT)) {
		if (status & SD_STATUS_RAT)
			host->mrq->cmd->error = -ETIMEDOUT;
		else if (status & SD_STATUS_DT)
			host->mrq->data->error = -ETIMEDOUT;
796

797 798
		/* In PIO mode, interrupts might still be enabled */
		IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
799

800 801 802
		/* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
		tasklet_schedule(&host->finish_task);
	}
803
#if 0
804 805 806 807 808 809 810
	else if (status & SD_STATUS_DD) {
		/* Sometimes we get a DD before a NE in PIO mode */
		if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
			au1xmmc_receive_pio(host);
		else {
			au1xmmc_data_complete(host, status);
			/* tasklet_schedule(&host->data_task); */
811 812
		}
	}
813 814 815 816 817 818 819 820 821 822 823 824 825 826
#endif
	else if (status & SD_STATUS_CR) {
		if (host->status == HOST_S_CMD)
			au1xmmc_cmd_complete(host, status);

	} else if (!(host->flags & HOST_F_DMA)) {
		if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
			au1xmmc_send_pio(host);
		else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
			au1xmmc_receive_pio(host);

	} else if (status & 0x203F3C70) {
			DBG("Unhandled status %8.8x\n", host->pdev->id,
				status);
827 828
	}

829 830
	au_writel(status, HOST_STATUS(host));
	au_sync();
831

832
	return IRQ_HANDLED;
833 834
}

835 836 837 838 839 840 841 842 843 844
#ifdef CONFIG_SOC_AU1200
/* 8bit memory DMA device */
static dbdev_tab_t au1xmmc_mem_dbdev = {
	.dev_id		= DSCR_CMD0_ALWAYS,
	.dev_flags	= DEV_FLAGS_ANYUSE,
	.dev_tsize	= 0,
	.dev_devwidth	= 8,
	.dev_physaddr	= 0x00000000,
	.dev_intlevel	= 0,
	.dev_intpolarity = 0,
845
};
846
static int memid;
847

848
static void au1xmmc_dbdma_callback(int irq, void *dev_id)
849
{
850
	struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
851

852 853 854
	/* Avoid spurious interrupts */
	if (!host->mrq)
		return;
855

856 857
	if (host->flags & HOST_F_STOP)
		SEND_STOP(host);
858

859 860
	tasklet_schedule(&host->data_task);
}
861

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
{
	struct resource *res;
	int txid, rxid;

	res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
	if (!res)
		return -ENODEV;
	txid = res->start;

	res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
	if (!res)
		return -ENODEV;
	rxid = res->start;

	if (!memid)
		return -ENODEV;

	host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
				au1xmmc_dbdma_callback, (void *)host);
	if (!host->tx_chan) {
		dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
		return -ENODEV;
	}

	host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
				au1xmmc_dbdma_callback, (void *)host);
	if (!host->rx_chan) {
		dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
		au1xxx_dbdma_chan_free(host->tx_chan);
		return -ENODEV;
	}
894

895 896
	au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
	au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
897

898 899
	au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
	au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
900

901 902
	/* DBDMA is good to go */
	host->flags |= HOST_F_DMA;
903

904 905
	return 0;
}
906

907 908 909 910 911 912 913
static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
{
	if (host->flags & HOST_F_DMA) {
		host->flags &= ~HOST_F_DMA;
		au1xxx_dbdma_chan_free(host->tx_chan);
		au1xxx_dbdma_chan_free(host->rx_chan);
	}
914
}
915
#endif
916

M
Manuel Lauss 已提交
917 918 919 920 921 922 923 924 925 926
static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
{
	struct au1xmmc_host *host = mmc_priv(mmc);

	if (en)
		IRQ_ON(host, SD_CONFIG_SI);
	else
		IRQ_OFF(host, SD_CONFIG_SI);
}

Y
Yoichi Yuasa 已提交
927
static const struct mmc_host_ops au1xmmc_ops = {
928 929
	.request	= au1xmmc_request,
	.set_ios	= au1xmmc_set_ios,
930
	.get_ro		= au1xmmc_card_readonly,
M
Manuel Lauss 已提交
931
	.enable_sdio_irq = au1xmmc_enable_sdio_irq,
932 933
};

934
static void au1xmmc_poll_event(unsigned long arg)
935
{
936 937 938 939 940 941 942 943 944 945
	struct au1xmmc_host *host = (struct au1xmmc_host *)arg;
	int card = au1xmmc_card_inserted(host);
	int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0;

	if (card != controller) {
		host->flags &= ~HOST_F_ACTIVE;
		if (card)
			host->flags |= HOST_F_ACTIVE;
		mmc_detect_change(host->mmc, 0);
	}
946

947 948 949 950 951 952 953 954
#ifdef DEBUG
	if (host->mrq != NULL) {
		u32 status = au_readl(HOST_STATUS(host));
		DBG("PENDING - %8.8x\n", host->pdev->id, status);
	}
#endif
	mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT);
}
955

956 957 958 959 960 961 962
static void au1xmmc_init_cd_poll_timer(struct au1xmmc_host *host)
{
	init_timer(&host->timer);
	host->timer.function = au1xmmc_poll_event;
	host->timer.data = (unsigned long)host;
	host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
}
963

964 965 966 967 968 969 970 971 972 973 974 975
static int __devinit au1xmmc_probe(struct platform_device *pdev)
{
	struct mmc_host *mmc;
	struct au1xmmc_host *host;
	struct resource *r;
	int ret;

	mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
	if (!mmc) {
		dev_err(&pdev->dev, "no memory for mmc_host\n");
		ret = -ENOMEM;
		goto out0;
976 977
	}

978 979 980 981
	host = mmc_priv(mmc);
	host->mmc = mmc;
	host->platdata = pdev->dev.platform_data;
	host->pdev = pdev;
982

983 984 985 986 987 988
	ret = -ENODEV;
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r) {
		dev_err(&pdev->dev, "no mmio defined\n");
		goto out1;
	}
989

990 991 992 993 994 995
	host->ioarea = request_mem_region(r->start, r->end - r->start + 1,
					   pdev->name);
	if (!host->ioarea) {
		dev_err(&pdev->dev, "mmio already in use\n");
		goto out1;
	}
996

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	host->iobase = (unsigned long)ioremap(r->start, 0x3c);
	if (!host->iobase) {
		dev_err(&pdev->dev, "cannot remap mmio\n");
		goto out2;
	}

	r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!r) {
		dev_err(&pdev->dev, "no IRQ defined\n");
		goto out3;
	}
1008

1009 1010 1011 1012 1013 1014 1015 1016
	host->irq = r->start;
	/* IRQ is shared among both SD controllers */
	ret = request_irq(host->irq, au1xmmc_irq, IRQF_SHARED,
			  DRIVER_NAME, host);
	if (ret) {
		dev_err(&pdev->dev, "cannot grab IRQ\n");
		goto out3;
	}
1017

1018
	mmc->ops = &au1xmmc_ops;
1019

1020 1021
	mmc->f_min =   450000;
	mmc->f_max = 24000000;
1022

1023 1024
	mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
	mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
1025

1026 1027
	mmc->max_blk_size = 2048;
	mmc->max_blk_count = 512;
1028

1029
	mmc->ocr_avail = AU1XMMC_OCR;
M
Manuel Lauss 已提交
1030
	mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1031

1032
	host->status = HOST_S_IDLE;
1033

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	/* board-specific carddetect setup, if any */
	if (host->platdata && host->platdata->cd_setup) {
		ret = host->platdata->cd_setup(mmc, 1);
		if (ret) {
			dev_err(&pdev->dev, "board CD setup failed\n");
			goto out4;
		}
	} else {
		/* poll the board-specific is-card-in-socket-? method */
		au1xmmc_init_cd_poll_timer(host);
	}
1045

1046 1047
	tasklet_init(&host->data_task, au1xmmc_tasklet_data,
			(unsigned long)host);
1048

1049 1050
	tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
			(unsigned long)host);
1051

1052 1053 1054 1055 1056
#ifdef CONFIG_SOC_AU1200
	ret = au1xmmc_dbdma_init(host);
	if (ret)
		printk(KERN_INFO DRIVER_NAME ": DBDMA init failed; using PIO\n");
#endif
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
#ifdef CONFIG_LEDS_CLASS
	if (host->platdata && host->platdata->led) {
		struct led_classdev *led = host->platdata->led;
		led->name = mmc_hostname(mmc);
		led->brightness = LED_OFF;
		led->default_trigger = mmc_hostname(mmc);
		ret = led_classdev_register(mmc_dev(mmc), led);
		if (ret)
			goto out5;
	}
#endif
1069

1070
	au1xmmc_reset_controller(host);
1071

1072 1073 1074 1075 1076
	ret = mmc_add_host(mmc);
	if (ret) {
		dev_err(&pdev->dev, "cannot add mmc host\n");
		goto out6;
	}
1077

1078
	platform_set_drvdata(pdev, mmc);
1079

1080 1081
	/* start the carddetect poll timer if necessary */
	if (!(host->platdata && host->platdata->cd_setup))
1082 1083
		add_timer(&host->timer);

1084 1085 1086
	printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X"
		" (mode=%s)\n", pdev->id, host->iobase,
		host->flags & HOST_F_DMA ? "dma" : "pio");
1087

1088
	return 0;	/* all ok */
1089

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
out6:
#ifdef CONFIG_LEDS_CLASS
	if (host->platdata && host->platdata->led)
		led_classdev_unregister(host->platdata->led);
out5:
#endif
	au_writel(0, HOST_ENABLE(host));
	au_writel(0, HOST_CONFIG(host));
	au_writel(0, HOST_CONFIG2(host));
	au_sync();

#ifdef CONFIG_SOC_AU1200
	au1xmmc_dbdma_shutdown(host);
#endif

	tasklet_kill(&host->data_task);
	tasklet_kill(&host->finish_task);

	if (host->platdata && host->platdata->cd_setup)
		host->platdata->cd_setup(mmc, 0);
out4:
	free_irq(host->irq, host);
out3:
	iounmap((void *)host->iobase);
out2:
	release_resource(host->ioarea);
	kfree(host->ioarea);
out1:
	mmc_free_host(mmc);
out0:
	return ret;
1121 1122
}

1123
static int __devexit au1xmmc_remove(struct platform_device *pdev)
1124
{
1125 1126 1127 1128 1129 1130 1131
	struct mmc_host *mmc = platform_get_drvdata(pdev);
	struct au1xmmc_host *host;

	if (mmc) {
		host  = mmc_priv(mmc);

		mmc_remove_host(mmc);
1132

1133 1134 1135 1136
#ifdef CONFIG_LEDS_CLASS
		if (host->platdata && host->platdata->led)
			led_classdev_unregister(host->platdata->led);
#endif
1137

1138 1139 1140 1141
		if (host->platdata && host->platdata->cd_setup)
			host->platdata->cd_setup(mmc, 0);
		else
			del_timer_sync(&host->timer);
1142

1143 1144 1145 1146
		au_writel(0, HOST_ENABLE(host));
		au_writel(0, HOST_CONFIG(host));
		au_writel(0, HOST_CONFIG2(host));
		au_sync();
1147 1148 1149 1150

		tasklet_kill(&host->data_task);
		tasklet_kill(&host->finish_task);

1151 1152 1153
#ifdef CONFIG_SOC_AU1200
		au1xmmc_dbdma_shutdown(host);
#endif
1154 1155
		au1xmmc_set_power(host, 0);

1156 1157 1158 1159
		free_irq(host->irq, host);
		iounmap((void *)host->iobase);
		release_resource(host->ioarea);
		kfree(host->ioarea);
1160

1161
		mmc_free_host(mmc);
1162 1163 1164 1165
	}
	return 0;
}

1166
static struct platform_driver au1xmmc_driver = {
1167 1168 1169
	.probe         = au1xmmc_probe,
	.remove        = au1xmmc_remove,
	.suspend       = NULL,
1170 1171 1172
	.resume        = NULL,
	.driver        = {
		.name  = DRIVER_NAME,
1173
		.owner = THIS_MODULE,
1174
	},
1175 1176 1177 1178
};

static int __init au1xmmc_init(void)
{
1179 1180 1181 1182 1183 1184 1185 1186 1187
#ifdef CONFIG_SOC_AU1200
	/* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
	 * of 8 bits.  And since devices are shared, we need to create
	 * our own to avoid freaking out other devices.
	 */
	memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
	if (!memid)
		printk(KERN_ERR "au1xmmc: cannot add memory dbdma dev\n");
#endif
1188
	return platform_driver_register(&au1xmmc_driver);
1189 1190 1191 1192
}

static void __exit au1xmmc_exit(void)
{
1193 1194 1195 1196
#ifdef CONFIG_SOC_AU1200
	if (memid)
		au1xxx_ddma_del_device(memid);
#endif
1197
	platform_driver_unregister(&au1xmmc_driver);
1198 1199 1200 1201 1202 1203 1204 1205
}

module_init(au1xmmc_init);
module_exit(au1xmmc_exit);

MODULE_AUTHOR("Advanced Micro Devices, Inc");
MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
MODULE_LICENSE("GPL");
1206
MODULE_ALIAS("platform:au1xxx-mmc");