1 --- linux-2.6.21.1.old/drivers/mmc/at91_mci.c 2007-06-05 09:08:57.000000000 +0200
2 +++ linux-2.6.21.1/drivers/mmc/at91_mci.c 2007-06-05 10:59:11.000000000 +0200
5 #define DRIVER_NAME "at91_mci"
8 +//#undef SUPPORT_4WIRE
11 #define FL_SENT_COMMAND (1 << 0)
12 #define FL_SENT_STOP (1 << 1)
15 * Copy from sg to a dma block - used for transfers
17 -static inline void at91mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
18 +static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
20 unsigned int len, i, size;
21 unsigned *dmabuf = host->buffer;
26 -static void at91mci_pre_dma_read(struct at91mci_host *host)
27 +static void at91_mci_pre_dma_read(struct at91mci_host *host)
30 struct scatterlist *sg;
33 * Handle after a dma read
35 -static void at91mci_post_dma_read(struct at91mci_host *host)
36 +static int at91_mci_post_dma_read(struct at91mci_host *host)
38 struct mmc_command *cmd;
39 struct mmc_data *data;
42 pr_debug("post dma read\n");
46 pr_debug("no command\n");
53 pr_debug("no data\n");
58 while (host->in_use_index < host->transfer_index) {
61 /* Is there another transfer to trigger? */
62 if (host->transfer_index < data->sg_len)
63 - at91mci_pre_dma_read(host);
64 + at91_mci_pre_dma_read(host);
66 + at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
67 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
68 - at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
71 pr_debug("post dma read done\n");
75 - * Handle transmitted data
77 -static void at91_mci_handle_transmitted(struct at91mci_host *host)
79 - struct mmc_command *cmd;
80 - struct mmc_data *data;
82 - pr_debug("Handling the transmit\n");
84 - /* Disable the transfer */
85 - at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
87 - /* Now wait for cmd ready */
88 - at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
89 - at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
97 - data->bytes_xfered = host->total_length;
102 @@ -340,10 +317,17 @@
104 static void at91_mci_enable(struct at91mci_host *host)
108 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
109 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
110 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
111 - at91_mci_write(host, AT91_MCI_MR, AT91_MCI_PDCMODE | 0x34a);
112 + mr = AT91_MCI_PDCMODE | 0x34a;
114 + if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
115 + mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
117 + at91_mci_write(host, AT91_MCI_MR, mr);
119 /* use Slot A or B (only one at same time) */
120 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
125 - * return the interrupts to enable
127 -static unsigned int at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
128 +static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
130 unsigned int cmdr, mr;
131 unsigned int block_length;
136 - /* Not sure if this is needed */
138 + /* Needed for leaving busy state before CMD1 */
139 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
140 pr_debug("Clearing timeout\n");
141 at91_mci_write(host, AT91_MCI_ARGR, 0);
143 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
150 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
151 @@ -440,50 +422,48 @@
152 at91_mci_write(host, ATMEL_PDC_TCR, 0);
153 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
154 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
155 + ier = AT91_MCI_CMDRDY;
157 + /* zero block length in PDC mode */
158 + mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
159 + at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
162 + * Disable the PDC controller
164 + at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
166 - at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
167 - at91_mci_write(host, AT91_MCI_CMDR, cmdr);
168 - return AT91_MCI_CMDRDY;
171 - mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff; /* zero block length and PDC mode */
172 - at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
175 - * Disable the PDC controller
177 - at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
179 - if (cmdr & AT91_MCI_TRCMD_START) {
180 - data->bytes_xfered = 0;
181 - host->transfer_index = 0;
182 - host->in_use_index = 0;
183 - if (cmdr & AT91_MCI_TRDIR) {
187 - host->buffer = NULL;
188 - host->total_length = 0;
189 + if (cmdr & AT91_MCI_TRCMD_START) {
190 + data->bytes_xfered = 0;
191 + host->transfer_index = 0;
192 + host->in_use_index = 0;
193 + if (cmdr & AT91_MCI_TRDIR) {
197 + host->buffer = NULL;
198 + host->total_length = 0;
200 - at91mci_pre_dma_read(host);
201 - ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
207 - host->total_length = block_length * blocks;
208 - host->buffer = dma_alloc_coherent(NULL,
209 - host->total_length,
210 - &host->physical_address, GFP_KERNEL);
212 - at91mci_sg_to_dma(host, data);
214 - pr_debug("Transmitting %d bytes\n", host->total_length);
216 - at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
217 - at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4);
218 - ier = AT91_MCI_TXBUFE;
219 + at91_mci_pre_dma_read(host);
220 + ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
226 + host->total_length = block_length * blocks;
227 + host->buffer = dma_alloc_coherent(NULL,
228 + host->total_length,
229 + &host->physical_address, GFP_KERNEL);
231 + at91_mci_sg_to_dma(host, data);
233 + pr_debug("Transmitting %d bytes\n", host->total_length);
235 + at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
236 + at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4);
237 + ier = AT91_MCI_CMDRDY;
242 @@ -498,39 +478,24 @@
243 if (cmdr & AT91_MCI_TRCMD_START) {
244 if (cmdr & AT91_MCI_TRDIR)
245 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
247 - at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
253 - * Wait for a command to complete
255 -static void at91mci_process_command(struct at91mci_host *host, struct mmc_command *cmd)
259 - ier = at91_mci_send_command(host, cmd);
261 - pr_debug("setting ier to %08X\n", ier);
263 - /* Stop on errors or the required value */
264 + /* Enable selected interrupts */
265 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
269 * Process the next step in the request
271 -static void at91mci_process_next(struct at91mci_host *host)
272 +static void at91_mci_process_next(struct at91mci_host *host)
274 if (!(host->flags & FL_SENT_COMMAND)) {
275 host->flags |= FL_SENT_COMMAND;
276 - at91mci_process_command(host, host->request->cmd);
277 + at91_mci_send_command(host, host->request->cmd);
279 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
280 host->flags |= FL_SENT_STOP;
281 - at91mci_process_command(host, host->request->stop);
282 + at91_mci_send_command(host, host->request->stop);
285 mmc_request_done(host->mmc, host->request);
288 * Handle a command that has been completed
290 -static void at91mci_completed_command(struct at91mci_host *host)
291 +static void at91_mci_completed_command(struct at91mci_host *host)
293 struct mmc_command *cmd = host->cmd;
297 cmd->error = MMC_ERR_NONE;
299 - at91mci_process_next(host);
300 + at91_mci_process_next(host);
308 - at91mci_process_next(host);
309 + at91_mci_process_next(host);
313 + * Handle transmitted data
315 +static void at91_mci_handle_transmitted(struct at91mci_host *host)
317 + struct mmc_command *cmd;
318 + struct mmc_data *data;
320 + pr_debug("Handling the transmit\n");
322 + /* Disable the transfer */
323 + at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
325 + /* Now wait for cmd ready */
326 + at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
334 + if (cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) {
335 + pr_debug("multiple write : wait for BLKE...\n");
336 + at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
338 + at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
340 + data->bytes_xfered = host->total_length;
344 +/*Handle after command sent ready*/
345 +static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
349 + else if (!host->cmd->data) {
350 + if (host->flags & FL_SENT_STOP) {
351 + /*After multi block write, we mus wait for NOTBUSY*/
352 + at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
354 + } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
355 + /*After sending multi-block-write command, start DMA transfer*/
356 + at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE);
357 + at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
358 + at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
361 + /* command not completed, have to wait */
366 @@ -698,29 +716,33 @@
367 at91_mci_handle_transmitted(host);
370 + if (int_status & AT91_MCI_ENDRX) {
371 + pr_debug("ENDRX\n");
372 + at91_mci_post_dma_read(host);
375 if (int_status & AT91_MCI_RXBUFF) {
376 pr_debug("RX buffer full\n");
377 - at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
378 + at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
379 + at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
383 if (int_status & AT91_MCI_ENDTX)
384 pr_debug("Transmit has ended\n");
386 - if (int_status & AT91_MCI_ENDRX) {
387 - pr_debug("Receive has ended\n");
388 - at91mci_post_dma_read(host);
391 if (int_status & AT91_MCI_NOTBUSY) {
392 pr_debug("Card is ready\n");
393 - at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
397 if (int_status & AT91_MCI_DTIP)
398 pr_debug("Data transfer in progress\n");
400 - if (int_status & AT91_MCI_BLKE)
401 + if (int_status & AT91_MCI_BLKE) {
402 pr_debug("Block transfer has ended\n");
406 if (int_status & AT91_MCI_TXRDY)
407 pr_debug("Ready to transmit\n");
408 @@ -730,14 +752,14 @@
410 if (int_status & AT91_MCI_CMDRDY) {
411 pr_debug("Command ready\n");
413 + completed = at91_mci_handle_cmdrdy(host);
418 pr_debug("Completed command\n");
419 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
420 - at91mci_completed_command(host);
421 + at91_mci_completed_command(host);
423 at91_mci_write(host, AT91_MCI_IDR, int_status);