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