56 #include <linux/module.h>
78 #include <mach/board.h>
83 #define DRIVER_NAME "at91_mci"
85 static inline int at91mci_is_mci1rev2xx(
void)
95 #define FL_SENT_COMMAND (1 << 0)
96 #define FL_SENT_STOP (1 << 1)
98 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
99 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
100 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
102 #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
103 #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
105 #define MCI_BLKSIZE 512
106 #define MCI_MAXBLKSIZE 4095
107 #define MCI_BLKATONCE 256
108 #define MCI_BUFSIZE (MCI_BLKSIZE * MCI_BLKATONCE)
187 static void at91_timeout_timer(
unsigned long data)
194 dev_err(host->
mmc->parent,
"Timeout waiting end of packet\n");
196 if (host->
cmd && host->
cmd->data) {
205 at91_reset_host(host);
216 unsigned *dmabuf = host->
buffer;
222 if (at91mci_is_mci1rev2xx())
231 for (i = 0; i < len; i++) {
234 unsigned int *sgbuffer;
245 for (index = 0; index < (amount / 4); index++)
246 *dmabuf++ =
swab32(sgbuffer[index]);
248 char *tmpv = (
char *)dmabuf;
249 memcpy(tmpv, sgbuffer, amount);
251 dmabuf = (
unsigned *)tmpv;
270 static void at91_mci_post_dma_read(
struct at91mci_host *host)
274 unsigned int len,
i,
size;
275 unsigned *dmabuf = host->
buffer;
297 for (i = 0; i < len; i++) {
300 unsigned int *sgbuffer;
310 for (index = 0; index < (amount / 4); index++)
311 sgbuffer[index] =
swab32(*dmabuf++);
313 char *tmpv = (
char *)dmabuf;
314 memcpy(sgbuffer, tmpv, amount);
316 dmabuf = (
unsigned *)tmpv;
319 flush_kernel_dcache_page(sg_page(sg));
332 static void at91_mci_handle_transmitted(
struct at91mci_host *host)
337 pr_debug(
"Handling the transmit\n");
351 if (cmd->
data->blocks > 1) {
352 pr_debug(
"multiple write : wait for BLKE...\n");
361 static void at91_mci_update_bytes_xfered(
struct at91mci_host *host)
374 pr_debug(
"-> bytes_xfered %d, total_length = %d\n",
383 static int at91_mci_handle_cmdrdy(
struct at91mci_host *host)
387 else if (!host->
cmd->data) {
415 if (at91mci_is_mci1rev2xx())
437 unsigned int cmdr, mr;
442 unsigned int ier = 0;
474 if (data->
blksz & 0x3) {
475 pr_debug(
"Unsupported block size\n");
481 pr_debug(
"Stream commands not supported\n");
488 block_length = data->
blksz;
520 pr_debug(
"Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
538 mr |= (block_length << 16);
564 (blocks * block_length) : (blocks * block_length) / 4);
579 if (at91mci_is_mci1rev2xx())
583 at91_mci_sg_to_dma(host, data);
604 if (cmdr & AT91_MCI_TRCMD_START) {
605 if (cmdr & AT91_MCI_TRDIR)
616 static void at91_mci_process_next(
struct at91mci_host *host)
620 at91_mci_send_command(host, host->
request->cmd);
624 at91_mci_send_command(host, host->
request->stop);
631 at91_reset_host(host);
639 static void at91_mci_completed_command(
struct at91mci_host *host,
unsigned int status)
651 pr_debug(
"Status = %08X/%08x [%08X %08X %08X %08X]\n",
668 if (status & AT91_MCI_RTOE)
670 else if (status & AT91_MCI_RCRCE)
676 pr_debug(
"Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
684 at91_mci_process_next(host);
699 at91_mci_process_next(host);
705 static void at91_mci_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
713 if (ios->
clock == 0) {
722 if ((at91_master_clock % (ios->
clock * 2)) == 0)
723 clkdiv = ((at91_master_clock / ios->
clock) / 2) - 1;
725 clkdiv = (at91_master_clock / ios->
clock) / 2;
727 pr_debug(
"clkdiv = %d. mcck = %ld\n", clkdiv,
728 at91_master_clock / (2 * (clkdiv + 1)));
731 pr_debug(
"MMC: Setting controller bus width to 4\n");
735 pr_debug(
"MMC: Setting controller bus width to 1\n");
743 if (gpio_is_valid(host->
board->vcc_pin)) {
771 pr_debug(
"MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
772 int_status & int_mask);
776 if (int_status & AT91_MCI_ERRORS) {
786 pr_debug(
"MMC: CRC error in data\n");
787 if (int_status & AT91_MCI_RTOE)
788 pr_debug(
"MMC: Response timeout\n");
790 pr_debug(
"MMC: Response end bit error\n");
791 if (int_status & AT91_MCI_RCRCE)
792 pr_debug(
"MMC: Response CRC error\n");
794 pr_debug(
"MMC: Response direction error\n");
796 pr_debug(
"MMC: Response index error\n");
802 at91_mci_handle_transmitted(host);
807 at91_mci_post_dma_read(host);
822 at91_mci_update_bytes_xfered(host);
827 pr_debug(
"Data transfer in progress\n");
830 pr_debug(
"Block transfer has ended\n");
841 mmc_signal_sdio_irq(host->
mmc);
844 mmc_signal_sdio_irq(host->
mmc);
852 if (int_status & AT91_MCI_CMDRDY) {
854 completed = at91_mci_handle_cmdrdy(host);
861 at91_mci_completed_command(host, int_status);
868 static irqreturn_t at91_mmc_det_irq(
int irq,
void *_host)
881 if (present != host->
present) {
884 present ?
"insert" :
"remove");
886 pr_debug(
"****** Resetting SD-card bus width ******\n");
895 static int at91_mci_get_ro(
struct mmc_host *mmc)
899 if (gpio_is_valid(host->
board->wp_pin))
908 static void at91_mci_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
913 host->
board->slot_b ?
'B':
'A', enable ?
"enable" :
"disable");
915 host->
board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
920 .request = at91_mci_request,
921 .set_ios = at91_mci_set_ios,
922 .get_ro = at91_mci_get_ro,
923 .enable_sdio_irq = at91_mci_enable_sdio_irq,
946 dev_dbg(&pdev->
dev,
"couldn't allocate mmc host\n");
950 mmc->
ops = &at91_mci_ops;
952 mmc->
f_max = 25000000;
962 host = mmc_priv(mmc);
965 host->
board = pdev->
dev.platform_data;
966 if (host->
board->wire4) {
967 if (at91mci_is_mci1rev2xx())
970 dev_warn(&pdev->
dev,
"4 wire bus mode not supported"
971 " - using 1 wire\n");
978 dev_err(&pdev->
dev,
"Can't allocate transmit buffer\n");
983 if (at91mci_is_mci1rev2xx()) {
985 if (host->
board->wire4 || !host->
board->slot_b)
993 if (gpio_is_valid(host->
board->det_pin)) {
996 dev_dbg(&pdev->
dev,
"couldn't claim card detect pin\n");
1000 if (gpio_is_valid(host->
board->wp_pin)) {
1003 dev_dbg(&pdev->
dev,
"couldn't claim wp sense pin\n");
1007 if (gpio_is_valid(host->
board->vcc_pin)) {
1010 dev_dbg(&pdev->
dev,
"couldn't claim vcc switch pin\n");
1038 at91_mci_disable(host);
1039 at91_mci_enable(host);
1048 dev_dbg(&pdev->
dev,
"request MCI interrupt failed\n");
1054 platform_set_drvdata(pdev, mmc);
1059 if (gpio_is_valid(host->
board->det_pin)) {
1070 if (gpio_is_valid(host->
board->det_pin)) {
1074 dev_warn(&pdev->
dev,
"request MMC detect irq failed\n");
1089 if (gpio_is_valid(host->
board->vcc_pin))
1092 if (gpio_is_valid(host->
board->wp_pin))
1095 if (gpio_is_valid(host->
board->det_pin))
1105 dev_err(&pdev->
dev,
"probe failed, err %d\n", ret);
1114 struct mmc_host *mmc = platform_get_drvdata(pdev);
1121 host = mmc_priv(mmc);
1127 if (gpio_is_valid(host->
board->det_pin)) {
1128 if (device_can_wakeup(&pdev->
dev))
1134 at91_mci_disable(host);
1142 if (gpio_is_valid(host->
board->vcc_pin))
1144 if (gpio_is_valid(host->
board->wp_pin))
1152 platform_set_drvdata(pdev,
NULL);
1161 struct mmc_host *mmc = platform_get_drvdata(pdev);
1165 if (gpio_is_valid(host->
board->det_pin) && device_may_wakeup(&pdev->
dev))
1166 enable_irq_wake(host->
board->det_pin);
1176 struct mmc_host *mmc = platform_get_drvdata(pdev);
1180 if (gpio_is_valid(host->
board->det_pin) && device_may_wakeup(&pdev->
dev))
1181 disable_irq_wake(host->
board->det_pin);
1189 #define at91_mci_suspend NULL
1190 #define at91_mci_resume NULL
1194 .remove =
__exit_p(at91_mci_remove),
1203 static int __init at91_mci_init(
void)
1208 static void __exit at91_mci_exit(
void)