⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at91_mci.c

📁 《linux驱动程序设计从入门到精通》一书中所有的程序代码含驱动和相应的应用程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/drivers/mmc/at91_mci.c - ATMEL AT91 MCI Driver * *  Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved * *  Copyright (C) 2006 Malcolm Noyes * * 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. *//*   This is the AT91 MCI driver that has been tested with both MMC cards   and SD-cards.  Boards that support write protect are now supported.   The CCAT91SBC001 board does not support SD cards.   The three entry points are at91_mci_request, at91_mci_set_ios   and at91_mci_get_ro.   SET IOS     This configures the device to put it into the correct mode and clock speed     required.   MCI REQUEST     MCI request processes the commands sent in the mmc_request structure. This     can consist of a processing command and a stop command in the case of     multiple block transfers.     There are three main types of request, commands, reads and writes.     Commands are straight forward. The command is submitted to the controller and     the request function returns. When the controller generates an interrupt to indicate     the command is finished, the response to the command are read and the mmc_request_done     function called to end the request.     Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)     controller to manage the transfers.     A read is done from the controller directly to the scatterlist passed in from the request.     Due to a bug in the AT91RM9200 controller, when a read is completed, all the words are byte     swapped in the scatterlist buffers.  AT91SAM926x are not affected by this bug.     The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY     A write is slightly different in that the bytes to write are read from the scatterlist     into a dma memory buffer (this is in case the source buffer should be read only). The     entire write buffer is then done from this single dma memory buffer.     The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY   GET RO     Gets the status of the write protect pin, if available.*/#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/init.h>#include <linux/ioport.h>#include <linux/platform_device.h>#include <linux/interrupt.h>#include <linux/blkdev.h>#include <linux/delay.h>#include <linux/err.h>#include <linux/dma-mapping.h>#include <linux/clk.h>#include <linux/mmc/host.h>#include <linux/mmc/protocol.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/mach/mmc.h>#include <asm/arch/board.h>#include <asm/arch/cpu.h>#include <asm/arch/gpio.h>#include <asm/arch/at91_mci.h>#include <asm/arch/at91_pdc.h>#define DRIVER_NAME "at91_mci"#undef	SUPPORT_4WIRE#define FL_SENT_COMMAND	(1 << 0)#define FL_SENT_STOP	(1 << 1)#define AT91_MCI_ERRORS	(AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE	\		| AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE		\		| AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)			#define at91_mci_read(host, reg)	__raw_readl((host)->baseaddr + (reg))#define at91_mci_write(host, reg, val)	__raw_writel((val), (host)->baseaddr + (reg))/* * Low level type for this driver */struct at91mci_host{	struct mmc_host *mmc;	struct mmc_command *cmd;	struct mmc_request *request;	void __iomem *baseaddr;	int irq;	struct at91_mmc_data *board;	int present;	struct clk *mci_clk;	/*	 * Flag indicating when the command has been sent. This is used to	 * work out whether or not to send the stop	 */	unsigned int flags;	/* flag for current bus settings */	u32 bus_mode;	/* DMA buffer used for transmitting */	unsigned int* buffer;	dma_addr_t physical_address;	unsigned int total_length;	/* Latest in the scatterlist that has been enabled for transfer, but not freed */	int in_use_index;	/* Latest in the scatterlist that has been enabled for transfer */	int transfer_index;};/* * Copy from sg to a dma block - used for transfers */static inline void at91mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data){	unsigned int len, i, size;	unsigned *dmabuf = host->buffer;	size = host->total_length;	len = data->sg_len;	/*	 * Just loop through all entries. Size might not	 * be the entire list though so make sure that	 * we do not transfer too much.	 */	for (i = 0; i < len; i++) {		struct scatterlist *sg;		int amount;		unsigned int *sgbuffer;		sg = &data->sg[i];		sgbuffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;		amount = min(size, sg->length);		size -= amount;		if (cpu_is_at91rm9200()) {	/* AT91RM9200 errata */			int index;			for (index = 0; index < (amount / 4); index++)				*dmabuf++ = swab32(sgbuffer[index]);		}		else			memcpy(dmabuf, sgbuffer, amount);		kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);		if (size == 0)			break;	}	/*	 * Check that we didn't get a request to transfer	 * more data than can fit into the SG list.	 */	BUG_ON(size != 0);}/* * Prepare a dma read */static void at91mci_pre_dma_read(struct at91mci_host *host){	int i;	struct scatterlist *sg;	struct mmc_command *cmd;	struct mmc_data *data;	pr_debug("pre dma read\n");	cmd = host->cmd;	if (!cmd) {		pr_debug("no command\n");		return;	}	data = cmd->data;	if (!data) {		pr_debug("no data\n");		return;	}	for (i = 0; i < 2; i++) {		/* nothing left to transfer */		if (host->transfer_index >= data->sg_len) {			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(host, AT91_PDC_RCR) != 0) {				pr_debug("Transfer active in current\n");				continue;			}		}		else {			if (at91_mci_read(host, AT91_PDC_RNCR) != 0) {				pr_debug("Transfer active in next\n");				continue;			}		}		/* Setup the next transfer */		pr_debug("Using transfer index %d\n", host->transfer_index);		sg = &data->sg[host->transfer_index++];		pr_debug("sg = %p\n", sg);		sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);		pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);		if (i == 0) {			at91_mci_write(host, AT91_PDC_RPR, sg->dma_address);			at91_mci_write(host, AT91_PDC_RCR, sg->length / 4);		}		else {			at91_mci_write(host, AT91_PDC_RNPR, sg->dma_address);			at91_mci_write(host, AT91_PDC_RNCR, sg->length / 4);		}	}	pr_debug("pre dma read done\n");}/* * Handle after a dma read */static void at91mci_post_dma_read(struct at91mci_host *host){	struct mmc_command *cmd;	struct mmc_data *data;	pr_debug("post dma read\n");	cmd = host->cmd;	if (!cmd) {		pr_debug("no command\n");		return;	}	data = cmd->data;	if (!data) {		pr_debug("no data\n");		return;	}	while (host->in_use_index < host->transfer_index) {		unsigned int *buffer;		struct scatterlist *sg;		pr_debug("finishing index %d\n", host->in_use_index);		sg = &data->sg[host->in_use_index++];		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;		pr_debug("buffer = %p, length = %d\n", buffer, sg->length);		data->bytes_xfered += sg->length;		if (cpu_is_at91rm9200()) {	/* AT91RM9200 errata */			int index;			for (index = 0; index < (sg->length / 4); index++)				buffer[index] = swab32(buffer[index]);		}		kunmap_atomic(buffer, KM_BIO_SRC_IRQ);		flush_dcache_page(sg->page);	}	/* Is there another transfer to trigger? */	if (host->transfer_index < data->sg_len)		at91mci_pre_dma_read(host);	else {		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);		at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);	}	pr_debug("post dma read done\n");}/* * Handle transmitted data */static void at91_mci_handle_transmitted(struct at91mci_host *host){	struct mmc_command *cmd;	struct mmc_data *data;	pr_debug("Handling the transmit\n");	/* Disable the transfer */	at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);	/* Now wait for cmd ready */	at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);	at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);	cmd = host->cmd;	if (!cmd) return;	data = cmd->data;	if (!data) return;	data->bytes_xfered = host->total_length;}/* * Enable the controller */static void at91_mci_enable(struct at91mci_host *host){	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);	at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);	at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);	at91_mci_write(host, AT91_MCI_MR, AT91_MCI_PDCMODE | 0x34a);	/* use Slot A or B (only one at same time) */	at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);}/* * Disable the controller */static void at91_mci_disable(struct at91mci_host *host){	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);}/* * Send a command * return the interrupts to enable */static unsigned int at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd){	unsigned int cmdr, mr;	unsigned int block_length;	struct mmc_data *data = cmd->data;	unsigned int blocks;	unsigned int ier = 0;	host->cmd = cmd;	/* Not sure if this is needed */#if 0	if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {		pr_debug("Clearing timeout\n");		at91_mci_write(host, AT91_MCI_ARGR, 0);		at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);		while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {			/* spin */			pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));		}	}#endif	cmdr = cmd->opcode;	if (mmc_resp_type(cmd) == MMC_RSP_NONE)		cmdr |= AT91_MCI_RSPTYP_NONE;	else {		/* if a response is expected then allow maximum response latancy */		cmdr |= AT91_MCI_MAXLAT;		/* set 136 bit response for R2, 48 bit response otherwise */		if (mmc_resp_type(cmd) == MMC_RSP_R2)			cmdr |= AT91_MCI_RSPTYP_136;		else			cmdr |= AT91_MCI_RSPTYP_48;	}	if (data) {		block_length = data->blksz;		blocks = data->blocks;		/* always set data start - also set direction flag for read */		if (data->flags & MMC_DATA_READ)			cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);		else if (data->flags & MMC_DATA_WRITE)			cmdr |= AT91_MCI_TRCMD_START;		if (data->flags & MMC_DATA_STREAM)			cmdr |= AT91_MCI_TRTYP_STREAM;		if (data->flags & MMC_DATA_MULTI)			cmdr |= AT91_MCI_TRTYP_MULTIPLE;	}	else {		block_length = 0;		blocks = 0;	}	if (cmd->opcode == MMC_STOP_TRANSMISSION)		cmdr |= AT91_MCI_TRCMD_STOP;	if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)		cmdr |= AT91_MCI_OPDCMD;	/*	 * Set the arguments and send the command	 */	pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",		cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));	if (!data) {		at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);		at91_mci_write(host, AT91_PDC_RPR, 0);		at91_mci_write(host, AT91_PDC_RCR, 0);		at91_mci_write(host, AT91_PDC_RNPR, 0);		at91_mci_write(host, AT91_PDC_RNCR, 0);		at91_mci_write(host, AT91_PDC_TPR, 0);		at91_mci_write(host, AT91_PDC_TCR, 0);		at91_mci_write(host, AT91_PDC_TNPR, 0);		at91_mci_write(host, AT91_PDC_TNCR, 0);		at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);		at91_mci_write(host, AT91_MCI_CMDR, cmdr);		return AT91_MCI_CMDRDY;	}	mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;	/* zero block length and PDC mode */	at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);	/*	 * Disable the PDC controller	 */	at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);	if (cmdr & AT91_MCI_TRCMD_START) {		data->bytes_xfered = 0;		host->transfer_index = 0;		host->in_use_index = 0;		if (cmdr & AT91_MCI_TRDIR) {			/*			 * Handle a read			 */			host->buffer = NULL;			host->total_length = 0;			at91mci_pre_dma_read(host);			ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;		}		else {			/*			 * Handle a write			 */			host->total_length = block_length * blocks;			host->buffer = dma_alloc_coherent(NULL,						  host->total_length,						  &host->physical_address, GFP_KERNEL);			at91mci_sg_to_dma(host, data);			pr_debug("Transmitting %d bytes\n", host->total_length);			at91_mci_write(host, AT91_PDC_TPR, host->physical_address);			at91_mci_write(host, AT91_PDC_TCR, host->total_length / 4);			ier = AT91_MCI_TXBUFE;		}	}	/*	 * Send the command and then enable the PDC - not the other way round as	 * the data sheet says	 */	at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);	at91_mci_write(host, AT91_MCI_CMDR, cmdr);	if (cmdr & AT91_MCI_TRCMD_START) {		if (cmdr & AT91_MCI_TRDIR)			at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTEN);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -