Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 MCI Driver |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved |
| 5 | * |
| 6 | * Copyright (C) 2006 Malcolm Noyes |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | /* |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 14 | This is the AT91 MCI driver that has been tested with both MMC cards |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 15 | and SD-cards. Boards that support write protect are now supported. |
| 16 | The CCAT91SBC001 board does not support SD cards. |
| 17 | |
| 18 | The three entry points are at91_mci_request, at91_mci_set_ios |
| 19 | and at91_mci_get_ro. |
| 20 | |
| 21 | SET IOS |
| 22 | This configures the device to put it into the correct mode and clock speed |
| 23 | required. |
| 24 | |
| 25 | MCI REQUEST |
| 26 | MCI request processes the commands sent in the mmc_request structure. This |
| 27 | can consist of a processing command and a stop command in the case of |
| 28 | multiple block transfers. |
| 29 | |
| 30 | There are three main types of request, commands, reads and writes. |
| 31 | |
| 32 | Commands are straight forward. The command is submitted to the controller and |
| 33 | the request function returns. When the controller generates an interrupt to indicate |
| 34 | the command is finished, the response to the command are read and the mmc_request_done |
| 35 | function called to end the request. |
| 36 | |
| 37 | Reads and writes work in a similar manner to normal commands but involve the PDC (DMA) |
| 38 | controller to manage the transfers. |
| 39 | |
| 40 | A read is done from the controller directly to the scatterlist passed in from the request. |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 41 | Due to a bug in the AT91RM9200 controller, when a read is completed, all the words are byte |
| 42 | swapped in the scatterlist buffers. AT91SAM926x are not affected by this bug. |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 43 | |
| 44 | The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY |
| 45 | |
| 46 | A write is slightly different in that the bytes to write are read from the scatterlist |
| 47 | into a dma memory buffer (this is in case the source buffer should be read only). The |
| 48 | entire write buffer is then done from this single dma memory buffer. |
| 49 | |
| 50 | The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY |
| 51 | |
| 52 | GET RO |
| 53 | Gets the status of the write protect pin, if available. |
| 54 | */ |
| 55 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 56 | #include <linux/module.h> |
| 57 | #include <linux/moduleparam.h> |
| 58 | #include <linux/init.h> |
| 59 | #include <linux/ioport.h> |
| 60 | #include <linux/platform_device.h> |
| 61 | #include <linux/interrupt.h> |
| 62 | #include <linux/blkdev.h> |
| 63 | #include <linux/delay.h> |
| 64 | #include <linux/err.h> |
| 65 | #include <linux/dma-mapping.h> |
| 66 | #include <linux/clk.h> |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 67 | #include <linux/atmel_pdc.h> |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 68 | |
| 69 | #include <linux/mmc/host.h> |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 70 | |
| 71 | #include <asm/io.h> |
| 72 | #include <asm/irq.h> |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 73 | #include <asm/gpio.h> |
| 74 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 75 | #include <asm/mach/mmc.h> |
| 76 | #include <asm/arch/board.h> |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 77 | #include <asm/arch/cpu.h> |
Andrew Victor | 55d8bae | 2006-11-30 17:16:43 +0100 | [diff] [blame] | 78 | #include <asm/arch/at91_mci.h> |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 79 | |
| 80 | #define DRIVER_NAME "at91_mci" |
| 81 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 82 | #define FL_SENT_COMMAND (1 << 0) |
| 83 | #define FL_SENT_STOP (1 << 1) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 84 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 85 | #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \ |
| 86 | | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \ |
Nicolas Ferre | 37b758e | 2007-08-08 12:01:44 +0200 | [diff] [blame] | 87 | | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 88 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 89 | #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg)) |
| 90 | #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg)) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 91 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Low level type for this driver |
| 95 | */ |
| 96 | struct at91mci_host |
| 97 | { |
| 98 | struct mmc_host *mmc; |
| 99 | struct mmc_command *cmd; |
| 100 | struct mmc_request *request; |
| 101 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 102 | void __iomem *baseaddr; |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 103 | int irq; |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 104 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 105 | struct at91_mmc_data *board; |
| 106 | int present; |
| 107 | |
Andrew Victor | 3dd3b03 | 2006-10-23 14:46:54 +0200 | [diff] [blame] | 108 | struct clk *mci_clk; |
| 109 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 110 | /* |
| 111 | * Flag indicating when the command has been sent. This is used to |
| 112 | * work out whether or not to send the stop |
| 113 | */ |
| 114 | unsigned int flags; |
| 115 | /* flag for current bus settings */ |
| 116 | u32 bus_mode; |
| 117 | |
| 118 | /* DMA buffer used for transmitting */ |
| 119 | unsigned int* buffer; |
| 120 | dma_addr_t physical_address; |
| 121 | unsigned int total_length; |
| 122 | |
| 123 | /* Latest in the scatterlist that has been enabled for transfer, but not freed */ |
| 124 | int in_use_index; |
| 125 | |
| 126 | /* Latest in the scatterlist that has been enabled for transfer */ |
| 127 | int transfer_index; |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 128 | |
| 129 | /* Timer for timeouts */ |
| 130 | struct timer_list timer; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 133 | static void at91_timeout_timer(unsigned long data) |
| 134 | { |
| 135 | struct at91mci_host *host; |
| 136 | |
| 137 | host = (struct at91mci_host *)data; |
| 138 | |
| 139 | if (host->request) { |
| 140 | dev_err(host->mmc->parent, "Timeout waiting end of packet\n"); |
| 141 | |
| 142 | if (host->cmd && host->cmd->data) { |
| 143 | host->cmd->data->error = -ETIMEDOUT; |
| 144 | } else { |
| 145 | if (host->cmd) |
| 146 | host->cmd->error = -ETIMEDOUT; |
| 147 | else |
| 148 | host->request->cmd->error = -ETIMEDOUT; |
| 149 | } |
| 150 | |
| 151 | mmc_request_done(host->mmc, host->request); |
| 152 | } |
| 153 | } |
| 154 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 155 | /* |
| 156 | * Copy from sg to a dma block - used for transfers |
| 157 | */ |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 158 | static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 159 | { |
| 160 | unsigned int len, i, size; |
| 161 | unsigned *dmabuf = host->buffer; |
| 162 | |
| 163 | size = host->total_length; |
| 164 | len = data->sg_len; |
| 165 | |
| 166 | /* |
| 167 | * Just loop through all entries. Size might not |
| 168 | * be the entire list though so make sure that |
| 169 | * we do not transfer too much. |
| 170 | */ |
| 171 | for (i = 0; i < len; i++) { |
| 172 | struct scatterlist *sg; |
| 173 | int amount; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 174 | unsigned int *sgbuffer; |
| 175 | |
| 176 | sg = &data->sg[i]; |
| 177 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 178 | sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 179 | amount = min(size, sg->length); |
| 180 | size -= amount; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 181 | |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 182 | if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */ |
| 183 | int index; |
| 184 | |
| 185 | for (index = 0; index < (amount / 4); index++) |
| 186 | *dmabuf++ = swab32(sgbuffer[index]); |
| 187 | } |
| 188 | else |
| 189 | memcpy(dmabuf, sgbuffer, amount); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 190 | |
| 191 | kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ); |
| 192 | |
| 193 | if (size == 0) |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Check that we didn't get a request to transfer |
| 199 | * more data than can fit into the SG list. |
| 200 | */ |
| 201 | BUG_ON(size != 0); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Prepare a dma read |
| 206 | */ |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 207 | static void at91_mci_pre_dma_read(struct at91mci_host *host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 208 | { |
| 209 | int i; |
| 210 | struct scatterlist *sg; |
| 211 | struct mmc_command *cmd; |
| 212 | struct mmc_data *data; |
| 213 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 214 | pr_debug("pre dma read\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 215 | |
| 216 | cmd = host->cmd; |
| 217 | if (!cmd) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 218 | pr_debug("no command\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | data = cmd->data; |
| 223 | if (!data) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 224 | pr_debug("no data\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
| 228 | for (i = 0; i < 2; i++) { |
| 229 | /* nothing left to transfer */ |
| 230 | if (host->transfer_index >= data->sg_len) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 231 | pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | |
| 235 | /* Check to see if this needs filling */ |
| 236 | if (i == 0) { |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 237 | if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 238 | pr_debug("Transfer active in current\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 239 | continue; |
| 240 | } |
| 241 | } |
| 242 | else { |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 243 | if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 244 | pr_debug("Transfer active in next\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 245 | continue; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* Setup the next transfer */ |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 250 | pr_debug("Using transfer index %d\n", host->transfer_index); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 251 | |
| 252 | sg = &data->sg[host->transfer_index++]; |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 253 | pr_debug("sg = %p\n", sg); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 254 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 255 | sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 256 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 257 | pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 258 | |
| 259 | if (i == 0) { |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 260 | at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address); |
Marc Pignat | 80f9254 | 2008-05-30 14:05:24 +0200 | [diff] [blame] | 261 | at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 262 | } |
| 263 | else { |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 264 | at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address); |
Marc Pignat | 80f9254 | 2008-05-30 14:05:24 +0200 | [diff] [blame] | 265 | at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 269 | pr_debug("pre dma read done\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Handle after a dma read |
| 274 | */ |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 275 | static void at91_mci_post_dma_read(struct at91mci_host *host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 276 | { |
| 277 | struct mmc_command *cmd; |
| 278 | struct mmc_data *data; |
| 279 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 280 | pr_debug("post dma read\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 281 | |
| 282 | cmd = host->cmd; |
| 283 | if (!cmd) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 284 | pr_debug("no command\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
| 288 | data = cmd->data; |
| 289 | if (!data) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 290 | pr_debug("no data\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
| 294 | while (host->in_use_index < host->transfer_index) { |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 295 | struct scatterlist *sg; |
| 296 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 297 | pr_debug("finishing index %d\n", host->in_use_index); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 298 | |
| 299 | sg = &data->sg[host->in_use_index++]; |
| 300 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 301 | pr_debug("Unmapping page %08X\n", sg->dma_address); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 302 | |
| 303 | dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE); |
| 304 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 305 | data->bytes_xfered += sg->length; |
| 306 | |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 307 | if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */ |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 308 | unsigned int *buffer; |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 309 | int index; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 310 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 311 | /* Swap the contents of the buffer */ |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 312 | buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset; |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 313 | pr_debug("buffer = %p, length = %d\n", buffer, sg->length); |
| 314 | |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 315 | for (index = 0; index < (sg->length / 4); index++) |
| 316 | buffer[index] = swab32(buffer[index]); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 317 | |
| 318 | kunmap_atomic(buffer, KM_BIO_SRC_IRQ); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 319 | } |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 320 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 321 | flush_dcache_page(sg_page(sg)); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* Is there another transfer to trigger? */ |
| 325 | if (host->transfer_index < data->sg_len) |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 326 | at91_mci_pre_dma_read(host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 327 | else { |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 328 | at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX); |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 329 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 332 | pr_debug("post dma read done\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Handle transmitted data |
| 337 | */ |
| 338 | static void at91_mci_handle_transmitted(struct at91mci_host *host) |
| 339 | { |
| 340 | struct mmc_command *cmd; |
| 341 | struct mmc_data *data; |
| 342 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 343 | pr_debug("Handling the transmit\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 344 | |
| 345 | /* Disable the transfer */ |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 346 | at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 347 | |
| 348 | /* Now wait for cmd ready */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 349 | at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 350 | |
| 351 | cmd = host->cmd; |
| 352 | if (!cmd) return; |
| 353 | |
| 354 | data = cmd->data; |
| 355 | if (!data) return; |
| 356 | |
Pierre Ossman | be0192a | 2007-07-24 21:11:47 +0200 | [diff] [blame] | 357 | if (cmd->data->blocks > 1) { |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 358 | pr_debug("multiple write : wait for BLKE...\n"); |
| 359 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE); |
| 360 | } else |
| 361 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY); |
| 362 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 363 | data->bytes_xfered = host->total_length; |
| 364 | } |
| 365 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 366 | /*Handle after command sent ready*/ |
| 367 | static int at91_mci_handle_cmdrdy(struct at91mci_host *host) |
| 368 | { |
| 369 | if (!host->cmd) |
| 370 | return 1; |
| 371 | else if (!host->cmd->data) { |
| 372 | if (host->flags & FL_SENT_STOP) { |
| 373 | /*After multi block write, we must wait for NOTBUSY*/ |
| 374 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY); |
| 375 | } else return 1; |
| 376 | } else if (host->cmd->data->flags & MMC_DATA_WRITE) { |
| 377 | /*After sendding multi-block-write command, start DMA transfer*/ |
| 378 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE); |
| 379 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE); |
| 380 | at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); |
| 381 | } |
| 382 | |
| 383 | /* command not completed, have to wait */ |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 388 | /* |
| 389 | * Enable the controller |
| 390 | */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 391 | static void at91_mci_enable(struct at91mci_host *host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 392 | { |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 393 | unsigned int mr; |
| 394 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 395 | at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN); |
Andrew Victor | f3a8efa | 2006-10-23 14:53:20 +0200 | [diff] [blame] | 396 | at91_mci_write(host, AT91_MCI_IDR, 0xffffffff); |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 397 | at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 398 | mr = AT91_MCI_PDCMODE | 0x34a; |
| 399 | |
| 400 | if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) |
| 401 | mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF; |
| 402 | |
| 403 | at91_mci_write(host, AT91_MCI_MR, mr); |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 404 | |
| 405 | /* use Slot A or B (only one at same time) */ |
| 406 | at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Disable the controller |
| 411 | */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 412 | static void at91_mci_disable(struct at91mci_host *host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 413 | { |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 414 | at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Send a command |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 419 | */ |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 420 | static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 421 | { |
| 422 | unsigned int cmdr, mr; |
| 423 | unsigned int block_length; |
| 424 | struct mmc_data *data = cmd->data; |
| 425 | |
| 426 | unsigned int blocks; |
| 427 | unsigned int ier = 0; |
| 428 | |
| 429 | host->cmd = cmd; |
| 430 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 431 | /* Needed for leaving busy state before CMD1 */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 432 | if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 433 | pr_debug("Clearing timeout\n"); |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 434 | at91_mci_write(host, AT91_MCI_ARGR, 0); |
| 435 | at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD); |
| 436 | while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) { |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 437 | /* spin */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 438 | pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR)); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 439 | } |
| 440 | } |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 441 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 442 | cmdr = cmd->opcode; |
| 443 | |
| 444 | if (mmc_resp_type(cmd) == MMC_RSP_NONE) |
| 445 | cmdr |= AT91_MCI_RSPTYP_NONE; |
| 446 | else { |
| 447 | /* if a response is expected then allow maximum response latancy */ |
| 448 | cmdr |= AT91_MCI_MAXLAT; |
| 449 | /* set 136 bit response for R2, 48 bit response otherwise */ |
| 450 | if (mmc_resp_type(cmd) == MMC_RSP_R2) |
| 451 | cmdr |= AT91_MCI_RSPTYP_136; |
| 452 | else |
| 453 | cmdr |= AT91_MCI_RSPTYP_48; |
| 454 | } |
| 455 | |
| 456 | if (data) { |
Marc Pignat | 1d4de9e | 2007-08-09 13:56:29 +0200 | [diff] [blame] | 457 | |
Marc Pignat | 80f9254 | 2008-05-30 14:05:24 +0200 | [diff] [blame] | 458 | if ( cpu_is_at91rm9200() && (data->blksz & 0x3) ) { |
Marc Pignat | 1d4de9e | 2007-08-09 13:56:29 +0200 | [diff] [blame] | 459 | pr_debug("Unsupported block size\n"); |
| 460 | cmd->error = -EINVAL; |
| 461 | mmc_request_done(host->mmc, host->request); |
| 462 | return; |
| 463 | } |
| 464 | |
Russell King | a3fd4a1 | 2006-06-04 17:51:15 +0100 | [diff] [blame] | 465 | block_length = data->blksz; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 466 | blocks = data->blocks; |
| 467 | |
| 468 | /* always set data start - also set direction flag for read */ |
| 469 | if (data->flags & MMC_DATA_READ) |
| 470 | cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START); |
| 471 | else if (data->flags & MMC_DATA_WRITE) |
| 472 | cmdr |= AT91_MCI_TRCMD_START; |
| 473 | |
| 474 | if (data->flags & MMC_DATA_STREAM) |
| 475 | cmdr |= AT91_MCI_TRTYP_STREAM; |
Pierre Ossman | be0192a | 2007-07-24 21:11:47 +0200 | [diff] [blame] | 476 | if (data->blocks > 1) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 477 | cmdr |= AT91_MCI_TRTYP_MULTIPLE; |
| 478 | } |
| 479 | else { |
| 480 | block_length = 0; |
| 481 | blocks = 0; |
| 482 | } |
| 483 | |
Marc Pignat | b6cedb3 | 2007-06-06 20:27:59 +0200 | [diff] [blame] | 484 | if (host->flags & FL_SENT_STOP) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 485 | cmdr |= AT91_MCI_TRCMD_STOP; |
| 486 | |
| 487 | if (host->bus_mode == MMC_BUSMODE_OPENDRAIN) |
| 488 | cmdr |= AT91_MCI_OPDCMD; |
| 489 | |
| 490 | /* |
| 491 | * Set the arguments and send the command |
| 492 | */ |
Andrew Victor | f3a8efa | 2006-10-23 14:53:20 +0200 | [diff] [blame] | 493 | pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n", |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 494 | cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR)); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 495 | |
| 496 | if (!data) { |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 497 | at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS); |
| 498 | at91_mci_write(host, ATMEL_PDC_RPR, 0); |
| 499 | at91_mci_write(host, ATMEL_PDC_RCR, 0); |
| 500 | at91_mci_write(host, ATMEL_PDC_RNPR, 0); |
| 501 | at91_mci_write(host, ATMEL_PDC_RNCR, 0); |
| 502 | at91_mci_write(host, ATMEL_PDC_TPR, 0); |
| 503 | at91_mci_write(host, ATMEL_PDC_TCR, 0); |
| 504 | at91_mci_write(host, ATMEL_PDC_TNPR, 0); |
| 505 | at91_mci_write(host, ATMEL_PDC_TNCR, 0); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 506 | ier = AT91_MCI_CMDRDY; |
| 507 | } else { |
| 508 | /* zero block length and PDC mode */ |
| 509 | mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff; |
Marc Pignat | 80f9254 | 2008-05-30 14:05:24 +0200 | [diff] [blame] | 510 | mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0; |
| 511 | mr |= (block_length << 16); |
| 512 | mr |= AT91_MCI_PDCMODE; |
| 513 | at91_mci_write(host, AT91_MCI_MR, mr); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 514 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 515 | /* |
| 516 | * Disable the PDC controller |
| 517 | */ |
| 518 | at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 519 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 520 | if (cmdr & AT91_MCI_TRCMD_START) { |
| 521 | data->bytes_xfered = 0; |
| 522 | host->transfer_index = 0; |
| 523 | host->in_use_index = 0; |
| 524 | if (cmdr & AT91_MCI_TRDIR) { |
| 525 | /* |
| 526 | * Handle a read |
| 527 | */ |
| 528 | host->buffer = NULL; |
| 529 | host->total_length = 0; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 530 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 531 | at91_mci_pre_dma_read(host); |
| 532 | ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */; |
| 533 | } |
| 534 | else { |
| 535 | /* |
| 536 | * Handle a write |
| 537 | */ |
| 538 | host->total_length = block_length * blocks; |
| 539 | host->buffer = dma_alloc_coherent(NULL, |
| 540 | host->total_length, |
| 541 | &host->physical_address, GFP_KERNEL); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 542 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 543 | at91_mci_sg_to_dma(host, data); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 544 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 545 | pr_debug("Transmitting %d bytes\n", host->total_length); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 546 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 547 | at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address); |
Marc Pignat | 80f9254 | 2008-05-30 14:05:24 +0200 | [diff] [blame] | 548 | at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ? |
| 549 | host->total_length : host->total_length / 4); |
| 550 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 551 | ier = AT91_MCI_CMDRDY; |
| 552 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Send the command and then enable the PDC - not the other way round as |
| 558 | * the data sheet says |
| 559 | */ |
| 560 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 561 | at91_mci_write(host, AT91_MCI_ARGR, cmd->arg); |
| 562 | at91_mci_write(host, AT91_MCI_CMDR, cmdr); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 563 | |
| 564 | if (cmdr & AT91_MCI_TRCMD_START) { |
| 565 | if (cmdr & AT91_MCI_TRDIR) |
Andrew Victor | 93a3ddc | 2007-02-08 11:31:22 +0100 | [diff] [blame] | 566 | at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 567 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 568 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 569 | /* Enable selected interrupts */ |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 570 | at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Process the next step in the request |
| 575 | */ |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 576 | static void at91_mci_process_next(struct at91mci_host *host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 577 | { |
| 578 | if (!(host->flags & FL_SENT_COMMAND)) { |
| 579 | host->flags |= FL_SENT_COMMAND; |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 580 | at91_mci_send_command(host, host->request->cmd); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 581 | } |
| 582 | else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) { |
| 583 | host->flags |= FL_SENT_STOP; |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 584 | at91_mci_send_command(host, host->request->stop); |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 585 | } else { |
| 586 | del_timer(&host->timer); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 587 | mmc_request_done(host->mmc, host->request); |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 588 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Handle a command that has been completed |
| 593 | */ |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 594 | static void at91_mci_completed_command(struct at91mci_host *host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 595 | { |
| 596 | struct mmc_command *cmd = host->cmd; |
| 597 | unsigned int status; |
| 598 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 599 | at91_mci_write(host, AT91_MCI_IDR, 0xffffffff); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 600 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 601 | cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0)); |
| 602 | cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1)); |
| 603 | cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2)); |
| 604 | cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3)); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 605 | |
| 606 | if (host->buffer) { |
| 607 | dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address); |
| 608 | host->buffer = NULL; |
| 609 | } |
| 610 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 611 | status = at91_mci_read(host, AT91_MCI_SR); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 612 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 613 | pr_debug("Status = %08X [%08X %08X %08X %08X]\n", |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 614 | status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); |
| 615 | |
Andrew Victor | 9e3866b | 2007-10-17 11:53:40 +0200 | [diff] [blame] | 616 | if (status & AT91_MCI_ERRORS) { |
Marc Pignat | b6cedb3 | 2007-06-06 20:27:59 +0200 | [diff] [blame] | 617 | if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) { |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 618 | cmd->error = 0; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 619 | } |
| 620 | else { |
| 621 | if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 622 | cmd->error = -ETIMEDOUT; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 623 | else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 624 | cmd->error = -EILSEQ; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 625 | else |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 626 | cmd->error = -EIO; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 627 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 628 | pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n", |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 629 | cmd->error, cmd->opcode, cmd->retries); |
| 630 | } |
| 631 | } |
| 632 | else |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 633 | cmd->error = 0; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 634 | |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 635 | at91_mci_process_next(host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Handle an MMC request |
| 640 | */ |
| 641 | static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 642 | { |
| 643 | struct at91mci_host *host = mmc_priv(mmc); |
| 644 | host->request = mrq; |
| 645 | host->flags = 0; |
| 646 | |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 647 | mod_timer(&host->timer, jiffies + HZ); |
| 648 | |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 649 | at91_mci_process_next(host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Set the IOS |
| 654 | */ |
| 655 | static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 656 | { |
| 657 | int clkdiv; |
| 658 | struct at91mci_host *host = mmc_priv(mmc); |
Andrew Victor | 3dd3b03 | 2006-10-23 14:46:54 +0200 | [diff] [blame] | 659 | unsigned long at91_master_clock = clk_get_rate(host->mci_clk); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 660 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 661 | host->bus_mode = ios->bus_mode; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 662 | |
| 663 | if (ios->clock == 0) { |
| 664 | /* Disable the MCI controller */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 665 | at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 666 | clkdiv = 0; |
| 667 | } |
| 668 | else { |
| 669 | /* Enable the MCI controller */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 670 | at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 671 | |
| 672 | if ((at91_master_clock % (ios->clock * 2)) == 0) |
| 673 | clkdiv = ((at91_master_clock / ios->clock) / 2) - 1; |
| 674 | else |
| 675 | clkdiv = (at91_master_clock / ios->clock) / 2; |
| 676 | |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 677 | pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv, |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 678 | at91_master_clock / (2 * (clkdiv + 1))); |
| 679 | } |
| 680 | if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 681 | pr_debug("MMC: Setting controller bus width to 4\n"); |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 682 | at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 683 | } |
| 684 | else { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 685 | pr_debug("MMC: Setting controller bus width to 1\n"); |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 686 | at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Set the clock divider */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 690 | at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 691 | |
| 692 | /* maybe switch power to the card */ |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 693 | if (host->board->vcc_pin) { |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 694 | switch (ios->power_mode) { |
| 695 | case MMC_POWER_OFF: |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 696 | gpio_set_value(host->board->vcc_pin, 0); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 697 | break; |
| 698 | case MMC_POWER_UP: |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 699 | gpio_set_value(host->board->vcc_pin, 1); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 700 | break; |
Marc Pignat | e5c0ef9 | 2008-05-09 11:07:07 +0200 | [diff] [blame] | 701 | case MMC_POWER_ON: |
| 702 | break; |
| 703 | default: |
| 704 | WARN_ON(1); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Handle an interrupt |
| 711 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 712 | static irqreturn_t at91_mci_irq(int irq, void *devid) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 713 | { |
| 714 | struct at91mci_host *host = devid; |
| 715 | int completed = 0; |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 716 | unsigned int int_status, int_mask; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 717 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 718 | int_status = at91_mci_read(host, AT91_MCI_SR); |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 719 | int_mask = at91_mci_read(host, AT91_MCI_IMR); |
Nicolas Ferre | 37b758e | 2007-08-08 12:01:44 +0200 | [diff] [blame] | 720 | |
Andrew Victor | f3a8efa | 2006-10-23 14:53:20 +0200 | [diff] [blame] | 721 | pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask, |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 722 | int_status & int_mask); |
Nicolas Ferre | 37b758e | 2007-08-08 12:01:44 +0200 | [diff] [blame] | 723 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 724 | int_status = int_status & int_mask; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 725 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 726 | if (int_status & AT91_MCI_ERRORS) { |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 727 | completed = 1; |
Nicolas Ferre | 37b758e | 2007-08-08 12:01:44 +0200 | [diff] [blame] | 728 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 729 | if (int_status & AT91_MCI_UNRE) |
| 730 | pr_debug("MMC: Underrun error\n"); |
| 731 | if (int_status & AT91_MCI_OVRE) |
| 732 | pr_debug("MMC: Overrun error\n"); |
| 733 | if (int_status & AT91_MCI_DTOE) |
| 734 | pr_debug("MMC: Data timeout\n"); |
| 735 | if (int_status & AT91_MCI_DCRCE) |
| 736 | pr_debug("MMC: CRC error in data\n"); |
| 737 | if (int_status & AT91_MCI_RTOE) |
| 738 | pr_debug("MMC: Response timeout\n"); |
| 739 | if (int_status & AT91_MCI_RENDE) |
| 740 | pr_debug("MMC: Response end bit error\n"); |
| 741 | if (int_status & AT91_MCI_RCRCE) |
| 742 | pr_debug("MMC: Response CRC error\n"); |
| 743 | if (int_status & AT91_MCI_RDIRE) |
| 744 | pr_debug("MMC: Response direction error\n"); |
| 745 | if (int_status & AT91_MCI_RINDE) |
| 746 | pr_debug("MMC: Response index error\n"); |
| 747 | } else { |
| 748 | /* Only continue processing if no errors */ |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 749 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 750 | if (int_status & AT91_MCI_TXBUFE) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 751 | pr_debug("TX buffer empty\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 752 | at91_mci_handle_transmitted(host); |
| 753 | } |
| 754 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 755 | if (int_status & AT91_MCI_ENDRX) { |
| 756 | pr_debug("ENDRX\n"); |
| 757 | at91_mci_post_dma_read(host); |
| 758 | } |
| 759 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 760 | if (int_status & AT91_MCI_RXBUFF) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 761 | pr_debug("RX buffer full\n"); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 762 | at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
| 763 | at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX); |
| 764 | completed = 1; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 767 | if (int_status & AT91_MCI_ENDTX) |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 768 | pr_debug("Transmit has ended\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 769 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 770 | if (int_status & AT91_MCI_NOTBUSY) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 771 | pr_debug("Card is ready\n"); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 772 | completed = 1; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 775 | if (int_status & AT91_MCI_DTIP) |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 776 | pr_debug("Data transfer in progress\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 777 | |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 778 | if (int_status & AT91_MCI_BLKE) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 779 | pr_debug("Block transfer has ended\n"); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 780 | completed = 1; |
| 781 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 782 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 783 | if (int_status & AT91_MCI_TXRDY) |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 784 | pr_debug("Ready to transmit\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 785 | |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 786 | if (int_status & AT91_MCI_RXRDY) |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 787 | pr_debug("Ready to receive\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 788 | |
| 789 | if (int_status & AT91_MCI_CMDRDY) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 790 | pr_debug("Command ready\n"); |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 791 | completed = at91_mci_handle_cmdrdy(host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 792 | } |
| 793 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 794 | |
| 795 | if (completed) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 796 | pr_debug("Completed command\n"); |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 797 | at91_mci_write(host, AT91_MCI_IDR, 0xffffffff); |
Nicolas Ferre | e8d04d3 | 2007-06-19 18:32:34 +0200 | [diff] [blame] | 798 | at91_mci_completed_command(host); |
Andrew Victor | df05a30 | 2006-10-23 14:50:09 +0200 | [diff] [blame] | 799 | } else |
| 800 | at91_mci_write(host, AT91_MCI_IDR, int_status); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 801 | |
| 802 | return IRQ_HANDLED; |
| 803 | } |
| 804 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 805 | static irqreturn_t at91_mmc_det_irq(int irq, void *_host) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 806 | { |
| 807 | struct at91mci_host *host = _host; |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 808 | int present = !gpio_get_value(irq_to_gpio(irq)); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * we expect this irq on both insert and remove, |
| 812 | * and use a short delay to debounce. |
| 813 | */ |
| 814 | if (present != host->present) { |
| 815 | host->present = present; |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 816 | pr_debug("%s: card %s\n", mmc_hostname(host->mmc), |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 817 | present ? "insert" : "remove"); |
| 818 | if (!present) { |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 819 | pr_debug("****** Resetting SD-card bus width ******\n"); |
Andrew Victor | 99eeb8d | 2006-12-11 12:40:23 +0100 | [diff] [blame] | 820 | at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 821 | } |
| 822 | mmc_detect_change(host->mmc, msecs_to_jiffies(100)); |
| 823 | } |
| 824 | return IRQ_HANDLED; |
| 825 | } |
| 826 | |
David Brownell | a26b498 | 2006-12-26 14:45:26 -0800 | [diff] [blame] | 827 | static int at91_mci_get_ro(struct mmc_host *mmc) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 828 | { |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 829 | struct at91mci_host *host = mmc_priv(mmc); |
| 830 | |
Anton Vorontsov | 08f80bb | 2008-06-17 18:17:39 +0400 | [diff] [blame] | 831 | if (host->board->wp_pin) |
| 832 | return !!gpio_get_value(host->board->wp_pin); |
| 833 | /* |
| 834 | * Board doesn't support read only detection; let the mmc core |
| 835 | * decide what to do. |
| 836 | */ |
| 837 | return -ENOSYS; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 838 | } |
| 839 | |
David Brownell | ab7aefd | 2006-11-12 17:55:30 -0800 | [diff] [blame] | 840 | static const struct mmc_host_ops at91_mci_ops = { |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 841 | .request = at91_mci_request, |
| 842 | .set_ios = at91_mci_set_ios, |
| 843 | .get_ro = at91_mci_get_ro, |
| 844 | }; |
| 845 | |
| 846 | /* |
| 847 | * Probe for the device |
| 848 | */ |
David Brownell | a26b498 | 2006-12-26 14:45:26 -0800 | [diff] [blame] | 849 | static int __init at91_mci_probe(struct platform_device *pdev) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 850 | { |
| 851 | struct mmc_host *mmc; |
| 852 | struct at91mci_host *host; |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 853 | struct resource *res; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 854 | int ret; |
| 855 | |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 856 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 857 | if (!res) |
| 858 | return -ENXIO; |
| 859 | |
| 860 | if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME)) |
| 861 | return -EBUSY; |
| 862 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 863 | mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev); |
| 864 | if (!mmc) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 865 | ret = -ENOMEM; |
| 866 | dev_dbg(&pdev->dev, "couldn't allocate mmc host\n"); |
| 867 | goto fail6; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | mmc->ops = &at91_mci_ops; |
| 871 | mmc->f_min = 375000; |
| 872 | mmc->f_max = 25000000; |
| 873 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 874 | |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 875 | mmc->max_blk_size = 4095; |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 876 | mmc->max_blk_count = mmc->max_req_size; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 877 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 878 | host = mmc_priv(mmc); |
| 879 | host->mmc = mmc; |
| 880 | host->buffer = NULL; |
| 881 | host->bus_mode = 0; |
| 882 | host->board = pdev->dev.platform_data; |
| 883 | if (host->board->wire4) { |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 884 | if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) |
| 885 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 886 | else |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 887 | dev_warn(&pdev->dev, "4 wire bus mode not supported" |
Nicolas Ferre | ed99c54 | 2007-07-09 14:58:16 +0200 | [diff] [blame] | 888 | " - using 1 wire\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 892 | * Reserve GPIOs ... board init code makes sure these pins are set |
| 893 | * up as GPIOs with the right direction (input, except for vcc) |
| 894 | */ |
| 895 | if (host->board->det_pin) { |
| 896 | ret = gpio_request(host->board->det_pin, "mmc_detect"); |
| 897 | if (ret < 0) { |
| 898 | dev_dbg(&pdev->dev, "couldn't claim card detect pin\n"); |
| 899 | goto fail5; |
| 900 | } |
| 901 | } |
| 902 | if (host->board->wp_pin) { |
| 903 | ret = gpio_request(host->board->wp_pin, "mmc_wp"); |
| 904 | if (ret < 0) { |
| 905 | dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n"); |
| 906 | goto fail4; |
| 907 | } |
| 908 | } |
| 909 | if (host->board->vcc_pin) { |
| 910 | ret = gpio_request(host->board->vcc_pin, "mmc_vcc"); |
| 911 | if (ret < 0) { |
| 912 | dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n"); |
| 913 | goto fail3; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /* |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 918 | * Get Clock |
| 919 | */ |
Andrew Victor | 3dd3b03 | 2006-10-23 14:46:54 +0200 | [diff] [blame] | 920 | host->mci_clk = clk_get(&pdev->dev, "mci_clk"); |
| 921 | if (IS_ERR(host->mci_clk)) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 922 | ret = -ENODEV; |
| 923 | dev_dbg(&pdev->dev, "no mci_clk?\n"); |
| 924 | goto fail2; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 925 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 926 | |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 927 | /* |
| 928 | * Map I/O region |
| 929 | */ |
| 930 | host->baseaddr = ioremap(res->start, res->end - res->start + 1); |
| 931 | if (!host->baseaddr) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 932 | ret = -ENOMEM; |
| 933 | goto fail1; |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 934 | } |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 935 | |
| 936 | /* |
| 937 | * Reset hardware |
| 938 | */ |
Andrew Victor | 3dd3b03 | 2006-10-23 14:46:54 +0200 | [diff] [blame] | 939 | clk_enable(host->mci_clk); /* Enable the peripheral clock */ |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 940 | at91_mci_disable(host); |
| 941 | at91_mci_enable(host); |
| 942 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 943 | /* |
| 944 | * Allocate the MCI interrupt |
| 945 | */ |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 946 | host->irq = platform_get_irq(pdev, 0); |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 947 | ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED, |
| 948 | mmc_hostname(mmc), host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 949 | if (ret) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 950 | dev_dbg(&pdev->dev, "request MCI interrupt failed\n"); |
| 951 | goto fail0; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | platform_set_drvdata(pdev, mmc); |
| 955 | |
| 956 | /* |
| 957 | * Add host to MMC layer |
| 958 | */ |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 959 | if (host->board->det_pin) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 960 | host->present = !gpio_get_value(host->board->det_pin); |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 961 | } |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 962 | else |
| 963 | host->present = -1; |
| 964 | |
| 965 | mmc_add_host(mmc); |
| 966 | |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 967 | setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host); |
| 968 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 969 | /* |
| 970 | * monitor card insertion/removal if we can |
| 971 | */ |
| 972 | if (host->board->det_pin) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 973 | ret = request_irq(gpio_to_irq(host->board->det_pin), |
| 974 | at91_mmc_det_irq, 0, mmc_hostname(mmc), host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 975 | if (ret) |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 976 | dev_warn(&pdev->dev, "request MMC detect irq failed\n"); |
| 977 | else |
| 978 | device_init_wakeup(&pdev->dev, 1); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 979 | } |
| 980 | |
Andrew Victor | f3a8efa | 2006-10-23 14:53:20 +0200 | [diff] [blame] | 981 | pr_debug("Added MCI driver\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 982 | |
| 983 | return 0; |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 984 | |
| 985 | fail0: |
| 986 | clk_disable(host->mci_clk); |
| 987 | iounmap(host->baseaddr); |
| 988 | fail1: |
| 989 | clk_put(host->mci_clk); |
| 990 | fail2: |
| 991 | if (host->board->vcc_pin) |
| 992 | gpio_free(host->board->vcc_pin); |
| 993 | fail3: |
| 994 | if (host->board->wp_pin) |
| 995 | gpio_free(host->board->wp_pin); |
| 996 | fail4: |
| 997 | if (host->board->det_pin) |
| 998 | gpio_free(host->board->det_pin); |
| 999 | fail5: |
| 1000 | mmc_free_host(mmc); |
| 1001 | fail6: |
| 1002 | release_mem_region(res->start, res->end - res->start + 1); |
| 1003 | dev_err(&pdev->dev, "probe failed, err %d\n", ret); |
| 1004 | return ret; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Remove a device |
| 1009 | */ |
David Brownell | a26b498 | 2006-12-26 14:45:26 -0800 | [diff] [blame] | 1010 | static int __exit at91_mci_remove(struct platform_device *pdev) |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1011 | { |
| 1012 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 1013 | struct at91mci_host *host; |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 1014 | struct resource *res; |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1015 | |
| 1016 | if (!mmc) |
| 1017 | return -1; |
| 1018 | |
| 1019 | host = mmc_priv(mmc); |
| 1020 | |
Anti Sullin | e0cda54 | 2007-08-30 16:15:16 +0200 | [diff] [blame] | 1021 | if (host->board->det_pin) { |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 1022 | if (device_can_wakeup(&pdev->dev)) |
| 1023 | free_irq(gpio_to_irq(host->board->det_pin), host); |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 1024 | device_init_wakeup(&pdev->dev, 0); |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 1025 | gpio_free(host->board->det_pin); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
Andrew Victor | e0b19b8 | 2006-10-25 19:42:38 +0200 | [diff] [blame] | 1028 | at91_mci_disable(host); |
Marc Pignat | e181dce | 2008-05-30 14:06:32 +0200 | [diff] [blame^] | 1029 | del_timer_sync(&host->timer); |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 1030 | mmc_remove_host(mmc); |
| 1031 | free_irq(host->irq, host); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1032 | |
Andrew Victor | 3dd3b03 | 2006-10-23 14:46:54 +0200 | [diff] [blame] | 1033 | clk_disable(host->mci_clk); /* Disable the peripheral clock */ |
| 1034 | clk_put(host->mci_clk); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1035 | |
David Brownell | 6e996ee | 2008-02-04 18:12:48 +0100 | [diff] [blame] | 1036 | if (host->board->vcc_pin) |
| 1037 | gpio_free(host->board->vcc_pin); |
| 1038 | if (host->board->wp_pin) |
| 1039 | gpio_free(host->board->wp_pin); |
| 1040 | |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 1041 | iounmap(host->baseaddr); |
| 1042 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1043 | release_mem_region(res->start, res->end - res->start + 1); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1044 | |
Andrew Victor | 17ea059 | 2006-10-23 14:44:40 +0200 | [diff] [blame] | 1045 | mmc_free_host(mmc); |
| 1046 | platform_set_drvdata(pdev, NULL); |
Andrew Victor | b44fb7a | 2006-06-19 13:06:05 +0100 | [diff] [blame] | 1047 | pr_debug("MCI Removed\n"); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | #ifdef CONFIG_PM |
| 1053 | static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state) |
| 1054 | { |
| 1055 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 1056 | struct at91mci_host *host = mmc_priv(mmc); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1057 | int ret = 0; |
| 1058 | |
Anti Sullin | e0cda54 | 2007-08-30 16:15:16 +0200 | [diff] [blame] | 1059 | if (host->board->det_pin && device_may_wakeup(&pdev->dev)) |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 1060 | enable_irq_wake(host->board->det_pin); |
| 1061 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1062 | if (mmc) |
| 1063 | ret = mmc_suspend_host(mmc, state); |
| 1064 | |
| 1065 | return ret; |
| 1066 | } |
| 1067 | |
| 1068 | static int at91_mci_resume(struct platform_device *pdev) |
| 1069 | { |
| 1070 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 1071 | struct at91mci_host *host = mmc_priv(mmc); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1072 | int ret = 0; |
| 1073 | |
Anti Sullin | e0cda54 | 2007-08-30 16:15:16 +0200 | [diff] [blame] | 1074 | if (host->board->det_pin && device_may_wakeup(&pdev->dev)) |
Marc Pignat | 63b6643 | 2007-07-16 11:07:02 +0200 | [diff] [blame] | 1075 | disable_irq_wake(host->board->det_pin); |
| 1076 | |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1077 | if (mmc) |
| 1078 | ret = mmc_resume_host(mmc); |
| 1079 | |
| 1080 | return ret; |
| 1081 | } |
| 1082 | #else |
| 1083 | #define at91_mci_suspend NULL |
| 1084 | #define at91_mci_resume NULL |
| 1085 | #endif |
| 1086 | |
| 1087 | static struct platform_driver at91_mci_driver = { |
David Brownell | a26b498 | 2006-12-26 14:45:26 -0800 | [diff] [blame] | 1088 | .remove = __exit_p(at91_mci_remove), |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1089 | .suspend = at91_mci_suspend, |
| 1090 | .resume = at91_mci_resume, |
| 1091 | .driver = { |
| 1092 | .name = DRIVER_NAME, |
| 1093 | .owner = THIS_MODULE, |
| 1094 | }, |
| 1095 | }; |
| 1096 | |
| 1097 | static int __init at91_mci_init(void) |
| 1098 | { |
David Brownell | a26b498 | 2006-12-26 14:45:26 -0800 | [diff] [blame] | 1099 | return platform_driver_probe(&at91_mci_driver, at91_mci_probe); |
Andrew Victor | 65dbf34 | 2006-04-02 19:18:51 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | static void __exit at91_mci_exit(void) |
| 1103 | { |
| 1104 | platform_driver_unregister(&at91_mci_driver); |
| 1105 | } |
| 1106 | |
| 1107 | module_init(at91_mci_init); |
| 1108 | module_exit(at91_mci_exit); |
| 1109 | |
| 1110 | MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver"); |
| 1111 | MODULE_AUTHOR("Nick Randell"); |
| 1112 | MODULE_LICENSE("GPL"); |
Kay Sievers | bc65c72 | 2008-04-15 14:34:28 -0700 | [diff] [blame] | 1113 | MODULE_ALIAS("platform:at91_mci"); |