Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Atmel MultiMedia Card Interface driver |
| 3 | * |
| 4 | * Copyright (C) 2004-2008 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/clk.h> |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 12 | #include <linux/debugfs.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 14 | #include <linux/dmaengine.h> |
| 15 | #include <linux/dma-mapping.h> |
Ben Nizette | fbfca4b | 2008-07-18 16:48:09 +1000 | [diff] [blame] | 16 | #include <linux/err.h> |
David Brownell | 3c26e17 | 2008-07-27 02:34:45 -0700 | [diff] [blame] | 17 | #include <linux/gpio.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/ioport.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/scatterlist.h> |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 26 | #include <linux/stat.h> |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 27 | #include <linux/types.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 28 | |
| 29 | #include <linux/mmc/host.h> |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 30 | #include <linux/mmc/sdio.h> |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 31 | |
| 32 | #include <mach/atmel-mci.h> |
Nicolas Ferre | c42aa77 | 2008-11-20 15:59:12 +0100 | [diff] [blame] | 33 | #include <linux/atmel-mci.h> |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 34 | #include <linux/atmel_pdc.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 35 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 36 | #include <asm/io.h> |
| 37 | #include <asm/unaligned.h> |
| 38 | |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 39 | #include <mach/cpu.h> |
Haavard Skinnemoen | 3663b73 | 2008-08-05 13:57:38 +0200 | [diff] [blame] | 40 | #include <mach/board.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 41 | |
| 42 | #include "atmel-mci-regs.h" |
| 43 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 44 | #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 45 | #define ATMCI_DMA_THRESHOLD 16 |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 46 | |
| 47 | enum { |
| 48 | EVENT_CMD_COMPLETE = 0, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 49 | EVENT_XFER_COMPLETE, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 50 | EVENT_DATA_COMPLETE, |
| 51 | EVENT_DATA_ERROR, |
| 52 | }; |
| 53 | |
| 54 | enum atmel_mci_state { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 55 | STATE_IDLE = 0, |
| 56 | STATE_SENDING_CMD, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 57 | STATE_SENDING_DATA, |
| 58 | STATE_DATA_BUSY, |
| 59 | STATE_SENDING_STOP, |
| 60 | STATE_DATA_ERROR, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 63 | enum atmci_xfer_dir { |
| 64 | XFER_RECEIVE = 0, |
| 65 | XFER_TRANSMIT, |
| 66 | }; |
| 67 | |
| 68 | enum atmci_pdc_buf { |
| 69 | PDC_FIRST_BUF = 0, |
| 70 | PDC_SECOND_BUF, |
| 71 | }; |
| 72 | |
| 73 | struct atmel_mci_caps { |
| 74 | bool has_dma; |
| 75 | bool has_pdc; |
| 76 | bool has_cfg_reg; |
| 77 | bool has_cstor_reg; |
| 78 | bool has_highspeed; |
| 79 | bool has_rwproof; |
| 80 | }; |
| 81 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 82 | struct atmel_mci_dma { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 83 | struct dma_chan *chan; |
| 84 | struct dma_async_tx_descriptor *data_desc; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 87 | /** |
| 88 | * struct atmel_mci - MMC controller state shared between all slots |
| 89 | * @lock: Spinlock protecting the queue and associated data. |
| 90 | * @regs: Pointer to MMIO registers. |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 91 | * @sg: Scatterlist entry currently being processed by PIO or PDC code. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 92 | * @pio_offset: Offset into the current scatterlist entry. |
| 93 | * @cur_slot: The slot which is currently using the controller. |
| 94 | * @mrq: The request currently being processed on @cur_slot, |
| 95 | * or NULL if the controller is idle. |
| 96 | * @cmd: The command currently being sent to the card, or NULL. |
| 97 | * @data: The data currently being transferred, or NULL if no data |
| 98 | * transfer is in progress. |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 99 | * @data_size: just data->blocks * data->blksz. |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 100 | * @dma: DMA client state. |
| 101 | * @data_chan: DMA channel being used for the current data transfer. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 102 | * @cmd_status: Snapshot of SR taken upon completion of the current |
| 103 | * command. Only valid when EVENT_CMD_COMPLETE is pending. |
| 104 | * @data_status: Snapshot of SR taken upon completion of the current |
| 105 | * data transfer. Only valid when EVENT_DATA_COMPLETE or |
| 106 | * EVENT_DATA_ERROR is pending. |
| 107 | * @stop_cmdr: Value to be loaded into CMDR when the stop command is |
| 108 | * to be sent. |
| 109 | * @tasklet: Tasklet running the request state machine. |
| 110 | * @pending_events: Bitmask of events flagged by the interrupt handler |
| 111 | * to be processed by the tasklet. |
| 112 | * @completed_events: Bitmask of events which the state machine has |
| 113 | * processed. |
| 114 | * @state: Tasklet state. |
| 115 | * @queue: List of slots waiting for access to the controller. |
| 116 | * @need_clock_update: Update the clock rate before the next request. |
| 117 | * @need_reset: Reset controller before next request. |
| 118 | * @mode_reg: Value of the MR register. |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 119 | * @cfg_reg: Value of the CFG register. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 120 | * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus |
| 121 | * rate and timeout calculations. |
| 122 | * @mapbase: Physical address of the MMIO registers. |
| 123 | * @mck: The peripheral bus clock hooked up to the MMC controller. |
| 124 | * @pdev: Platform device associated with the MMC controller. |
| 125 | * @slot: Slots sharing this MMC controller. |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 126 | * @caps: MCI capabilities depending on MCI version. |
| 127 | * @prepare_data: function to setup MCI before data transfer which |
| 128 | * depends on MCI capabilities. |
| 129 | * @submit_data: function to start data transfer which depends on MCI |
| 130 | * capabilities. |
| 131 | * @stop_transfer: function to stop data transfer which depends on MCI |
| 132 | * capabilities. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 133 | * |
| 134 | * Locking |
| 135 | * ======= |
| 136 | * |
| 137 | * @lock is a softirq-safe spinlock protecting @queue as well as |
| 138 | * @cur_slot, @mrq and @state. These must always be updated |
| 139 | * at the same time while holding @lock. |
| 140 | * |
| 141 | * @lock also protects mode_reg and need_clock_update since these are |
| 142 | * used to synchronize mode register updates with the queue |
| 143 | * processing. |
| 144 | * |
| 145 | * The @mrq field of struct atmel_mci_slot is also protected by @lock, |
| 146 | * and must always be written at the same time as the slot is added to |
| 147 | * @queue. |
| 148 | * |
| 149 | * @pending_events and @completed_events are accessed using atomic bit |
| 150 | * operations, so they don't need any locking. |
| 151 | * |
| 152 | * None of the fields touched by the interrupt handler need any |
| 153 | * locking. However, ordering is important: Before EVENT_DATA_ERROR or |
| 154 | * EVENT_DATA_COMPLETE is set in @pending_events, all data-related |
| 155 | * interrupts must be disabled and @data_status updated with a |
| 156 | * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 157 | * CMDRDY interrupt must be disabled and @cmd_status updated with a |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 158 | * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the |
| 159 | * bytes_xfered field of @data must be written. This is ensured by |
| 160 | * using barriers. |
| 161 | */ |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 162 | struct atmel_mci { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 163 | spinlock_t lock; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 164 | void __iomem *regs; |
| 165 | |
| 166 | struct scatterlist *sg; |
| 167 | unsigned int pio_offset; |
| 168 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 169 | struct atmel_mci_slot *cur_slot; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 170 | struct mmc_request *mrq; |
| 171 | struct mmc_command *cmd; |
| 172 | struct mmc_data *data; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 173 | unsigned int data_size; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 174 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 175 | struct atmel_mci_dma dma; |
| 176 | struct dma_chan *data_chan; |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 177 | struct dma_slave_config dma_conf; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 178 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 179 | u32 cmd_status; |
| 180 | u32 data_status; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 181 | u32 stop_cmdr; |
| 182 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 183 | struct tasklet_struct tasklet; |
| 184 | unsigned long pending_events; |
| 185 | unsigned long completed_events; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 186 | enum atmel_mci_state state; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 187 | struct list_head queue; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 188 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 189 | bool need_clock_update; |
| 190 | bool need_reset; |
| 191 | u32 mode_reg; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 192 | u32 cfg_reg; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 193 | unsigned long bus_hz; |
| 194 | unsigned long mapbase; |
| 195 | struct clk *mck; |
| 196 | struct platform_device *pdev; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 197 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 198 | struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS]; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 199 | |
| 200 | struct atmel_mci_caps caps; |
| 201 | |
| 202 | u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data); |
| 203 | void (*submit_data)(struct atmel_mci *host, struct mmc_data *data); |
| 204 | void (*stop_transfer)(struct atmel_mci *host); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * struct atmel_mci_slot - MMC slot state |
| 209 | * @mmc: The mmc_host representing this slot. |
| 210 | * @host: The MMC controller this slot is using. |
| 211 | * @sdc_reg: Value of SDCR to be written before using this slot. |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 212 | * @sdio_irq: SDIO irq mask for this slot. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 213 | * @mrq: mmc_request currently being processed or waiting to be |
| 214 | * processed, or NULL when the slot is idle. |
| 215 | * @queue_node: List node for placing this node in the @queue list of |
| 216 | * &struct atmel_mci. |
| 217 | * @clock: Clock rate configured by set_ios(). Protected by host->lock. |
| 218 | * @flags: Random state bits associated with the slot. |
| 219 | * @detect_pin: GPIO pin used for card detection, or negative if not |
| 220 | * available. |
| 221 | * @wp_pin: GPIO pin used for card write protect sending, or negative |
| 222 | * if not available. |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 223 | * @detect_is_active_high: The state of the detect pin when it is active. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 224 | * @detect_timer: Timer used for debouncing @detect_pin interrupts. |
| 225 | */ |
| 226 | struct atmel_mci_slot { |
| 227 | struct mmc_host *mmc; |
| 228 | struct atmel_mci *host; |
| 229 | |
| 230 | u32 sdc_reg; |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 231 | u32 sdio_irq; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 232 | |
| 233 | struct mmc_request *mrq; |
| 234 | struct list_head queue_node; |
| 235 | |
| 236 | unsigned int clock; |
| 237 | unsigned long flags; |
| 238 | #define ATMCI_CARD_PRESENT 0 |
| 239 | #define ATMCI_CARD_NEED_INIT 1 |
| 240 | #define ATMCI_SHUTDOWN 2 |
Nicolas Ferre | 5c2f2b9 | 2011-07-06 11:31:36 +0200 | [diff] [blame] | 241 | #define ATMCI_SUSPENDED 3 |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 242 | |
| 243 | int detect_pin; |
| 244 | int wp_pin; |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 245 | bool detect_is_active_high; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 246 | |
| 247 | struct timer_list detect_timer; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 248 | }; |
| 249 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 250 | #define atmci_test_and_clear_pending(host, event) \ |
| 251 | test_and_clear_bit(event, &host->pending_events) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 252 | #define atmci_set_completed(host, event) \ |
| 253 | set_bit(event, &host->completed_events) |
| 254 | #define atmci_set_pending(host, event) \ |
| 255 | set_bit(event, &host->pending_events) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 256 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 257 | /* |
| 258 | * The debugfs stuff below is mostly optimized away when |
| 259 | * CONFIG_DEBUG_FS is not set. |
| 260 | */ |
| 261 | static int atmci_req_show(struct seq_file *s, void *v) |
| 262 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 263 | struct atmel_mci_slot *slot = s->private; |
| 264 | struct mmc_request *mrq; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 265 | struct mmc_command *cmd; |
| 266 | struct mmc_command *stop; |
| 267 | struct mmc_data *data; |
| 268 | |
| 269 | /* Make sure we get a consistent snapshot */ |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 270 | spin_lock_bh(&slot->host->lock); |
| 271 | mrq = slot->mrq; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 272 | |
| 273 | if (mrq) { |
| 274 | cmd = mrq->cmd; |
| 275 | data = mrq->data; |
| 276 | stop = mrq->stop; |
| 277 | |
| 278 | if (cmd) |
| 279 | seq_printf(s, |
| 280 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 281 | cmd->opcode, cmd->arg, cmd->flags, |
| 282 | cmd->resp[0], cmd->resp[1], cmd->resp[2], |
Nicolas Ferre | d586ebb | 2010-05-11 14:06:50 -0700 | [diff] [blame] | 283 | cmd->resp[3], cmd->error); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 284 | if (data) |
| 285 | seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", |
| 286 | data->bytes_xfered, data->blocks, |
| 287 | data->blksz, data->flags, data->error); |
| 288 | if (stop) |
| 289 | seq_printf(s, |
| 290 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 291 | stop->opcode, stop->arg, stop->flags, |
| 292 | stop->resp[0], stop->resp[1], stop->resp[2], |
Nicolas Ferre | d586ebb | 2010-05-11 14:06:50 -0700 | [diff] [blame] | 293 | stop->resp[3], stop->error); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 296 | spin_unlock_bh(&slot->host->lock); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int atmci_req_open(struct inode *inode, struct file *file) |
| 302 | { |
| 303 | return single_open(file, atmci_req_show, inode->i_private); |
| 304 | } |
| 305 | |
| 306 | static const struct file_operations atmci_req_fops = { |
| 307 | .owner = THIS_MODULE, |
| 308 | .open = atmci_req_open, |
| 309 | .read = seq_read, |
| 310 | .llseek = seq_lseek, |
| 311 | .release = single_release, |
| 312 | }; |
| 313 | |
| 314 | static void atmci_show_status_reg(struct seq_file *s, |
| 315 | const char *regname, u32 value) |
| 316 | { |
| 317 | static const char *sr_bit[] = { |
| 318 | [0] = "CMDRDY", |
| 319 | [1] = "RXRDY", |
| 320 | [2] = "TXRDY", |
| 321 | [3] = "BLKE", |
| 322 | [4] = "DTIP", |
| 323 | [5] = "NOTBUSY", |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 324 | [6] = "ENDRX", |
| 325 | [7] = "ENDTX", |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 326 | [8] = "SDIOIRQA", |
| 327 | [9] = "SDIOIRQB", |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 328 | [12] = "SDIOWAIT", |
| 329 | [14] = "RXBUFF", |
| 330 | [15] = "TXBUFE", |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 331 | [16] = "RINDE", |
| 332 | [17] = "RDIRE", |
| 333 | [18] = "RCRCE", |
| 334 | [19] = "RENDE", |
| 335 | [20] = "RTOE", |
| 336 | [21] = "DCRCE", |
| 337 | [22] = "DTOE", |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 338 | [23] = "CSTOE", |
| 339 | [24] = "BLKOVRE", |
| 340 | [25] = "DMADONE", |
| 341 | [26] = "FIFOEMPTY", |
| 342 | [27] = "XFRDONE", |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 343 | [30] = "OVRE", |
| 344 | [31] = "UNRE", |
| 345 | }; |
| 346 | unsigned int i; |
| 347 | |
| 348 | seq_printf(s, "%s:\t0x%08x", regname, value); |
| 349 | for (i = 0; i < ARRAY_SIZE(sr_bit); i++) { |
| 350 | if (value & (1 << i)) { |
| 351 | if (sr_bit[i]) |
| 352 | seq_printf(s, " %s", sr_bit[i]); |
| 353 | else |
| 354 | seq_puts(s, " UNKNOWN"); |
| 355 | } |
| 356 | } |
| 357 | seq_putc(s, '\n'); |
| 358 | } |
| 359 | |
| 360 | static int atmci_regs_show(struct seq_file *s, void *v) |
| 361 | { |
| 362 | struct atmel_mci *host = s->private; |
| 363 | u32 *buf; |
| 364 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 365 | buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 366 | if (!buf) |
| 367 | return -ENOMEM; |
| 368 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 369 | /* |
| 370 | * Grab a more or less consistent snapshot. Note that we're |
| 371 | * not disabling interrupts, so IMR and SR may not be |
| 372 | * consistent. |
| 373 | */ |
| 374 | spin_lock_bh(&host->lock); |
Haavard Skinnemoen | 87e60f2 | 2008-09-19 21:09:27 +0200 | [diff] [blame] | 375 | clk_enable(host->mck); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 376 | memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE); |
Haavard Skinnemoen | 87e60f2 | 2008-09-19 21:09:27 +0200 | [diff] [blame] | 377 | clk_disable(host->mck); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 378 | spin_unlock_bh(&host->lock); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 379 | |
| 380 | seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n", |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 381 | buf[ATMCI_MR / 4], |
| 382 | buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "", |
| 383 | buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "", |
| 384 | buf[ATMCI_MR / 4] & 0xff); |
| 385 | seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]); |
| 386 | seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]); |
| 387 | seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 388 | seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n", |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 389 | buf[ATMCI_BLKR / 4], |
| 390 | buf[ATMCI_BLKR / 4] & 0xffff, |
| 391 | (buf[ATMCI_BLKR / 4] >> 16) & 0xffff); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 392 | if (host->caps.has_cstor_reg) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 393 | seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 394 | |
| 395 | /* Don't read RSPR and RDR; it will consume the data there */ |
| 396 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 397 | atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]); |
| 398 | atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 399 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 400 | if (host->caps.has_dma) { |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 401 | u32 val; |
| 402 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 403 | val = buf[ATMCI_DMA / 4]; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 404 | seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n", |
| 405 | val, val & 3, |
| 406 | ((val >> 4) & 3) ? |
| 407 | 1 << (((val >> 4) & 3) + 1) : 1, |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 408 | val & ATMCI_DMAEN ? " DMAEN" : ""); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 409 | } |
| 410 | if (host->caps.has_cfg_reg) { |
| 411 | u32 val; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 412 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 413 | val = buf[ATMCI_CFG / 4]; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 414 | seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n", |
| 415 | val, |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 416 | val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "", |
| 417 | val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "", |
| 418 | val & ATMCI_CFG_HSMODE ? " HSMODE" : "", |
| 419 | val & ATMCI_CFG_LSYNC ? " LSYNC" : ""); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Haavard Skinnemoen | b17339a | 2008-09-19 21:09:28 +0200 | [diff] [blame] | 422 | kfree(buf); |
| 423 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int atmci_regs_open(struct inode *inode, struct file *file) |
| 428 | { |
| 429 | return single_open(file, atmci_regs_show, inode->i_private); |
| 430 | } |
| 431 | |
| 432 | static const struct file_operations atmci_regs_fops = { |
| 433 | .owner = THIS_MODULE, |
| 434 | .open = atmci_regs_open, |
| 435 | .read = seq_read, |
| 436 | .llseek = seq_lseek, |
| 437 | .release = single_release, |
| 438 | }; |
| 439 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 440 | static void atmci_init_debugfs(struct atmel_mci_slot *slot) |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 441 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 442 | struct mmc_host *mmc = slot->mmc; |
| 443 | struct atmel_mci *host = slot->host; |
| 444 | struct dentry *root; |
| 445 | struct dentry *node; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 446 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 447 | root = mmc->debugfs_root; |
| 448 | if (!root) |
| 449 | return; |
| 450 | |
| 451 | node = debugfs_create_file("regs", S_IRUSR, root, host, |
| 452 | &atmci_regs_fops); |
| 453 | if (IS_ERR(node)) |
| 454 | return; |
| 455 | if (!node) |
| 456 | goto err; |
| 457 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 458 | node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 459 | if (!node) |
| 460 | goto err; |
| 461 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 462 | node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); |
| 463 | if (!node) |
| 464 | goto err; |
| 465 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 466 | node = debugfs_create_x32("pending_events", S_IRUSR, root, |
| 467 | (u32 *)&host->pending_events); |
| 468 | if (!node) |
| 469 | goto err; |
| 470 | |
| 471 | node = debugfs_create_x32("completed_events", S_IRUSR, root, |
| 472 | (u32 *)&host->completed_events); |
| 473 | if (!node) |
| 474 | goto err; |
| 475 | |
| 476 | return; |
| 477 | |
| 478 | err: |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 479 | dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 480 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 481 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 482 | static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 483 | unsigned int ns) |
| 484 | { |
Ludovic Desroches | 66292ad | 2012-03-28 12:28:33 +0200 | [diff] [blame^] | 485 | /* |
| 486 | * It is easier here to use us instead of ns for the timeout, |
| 487 | * it prevents from overflows during calculation. |
| 488 | */ |
| 489 | unsigned int us = DIV_ROUND_UP(ns, 1000); |
| 490 | |
| 491 | /* Maximum clock frequency is host->bus_hz/2 */ |
| 492 | return us * (DIV_ROUND_UP(host->bus_hz, 2000000)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static void atmci_set_timeout(struct atmel_mci *host, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 496 | struct atmel_mci_slot *slot, struct mmc_data *data) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 497 | { |
| 498 | static unsigned dtomul_to_shift[] = { |
| 499 | 0, 4, 7, 8, 10, 12, 16, 20 |
| 500 | }; |
| 501 | unsigned timeout; |
| 502 | unsigned dtocyc; |
| 503 | unsigned dtomul; |
| 504 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 505 | timeout = atmci_ns_to_clocks(host, data->timeout_ns) |
| 506 | + data->timeout_clks; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 507 | |
| 508 | for (dtomul = 0; dtomul < 8; dtomul++) { |
| 509 | unsigned shift = dtomul_to_shift[dtomul]; |
| 510 | dtocyc = (timeout + (1 << shift) - 1) >> shift; |
| 511 | if (dtocyc < 15) |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | if (dtomul >= 8) { |
| 516 | dtomul = 7; |
| 517 | dtocyc = 15; |
| 518 | } |
| 519 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 520 | dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n", |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 521 | dtocyc << dtomul_to_shift[dtomul]); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 522 | atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc))); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Return mask with command flags to be enabled for this command. |
| 527 | */ |
| 528 | static u32 atmci_prepare_command(struct mmc_host *mmc, |
| 529 | struct mmc_command *cmd) |
| 530 | { |
| 531 | struct mmc_data *data; |
| 532 | u32 cmdr; |
| 533 | |
| 534 | cmd->error = -EINPROGRESS; |
| 535 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 536 | cmdr = ATMCI_CMDR_CMDNB(cmd->opcode); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 537 | |
| 538 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 539 | if (cmd->flags & MMC_RSP_136) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 540 | cmdr |= ATMCI_CMDR_RSPTYP_136BIT; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 541 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 542 | cmdr |= ATMCI_CMDR_RSPTYP_48BIT; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | * This should really be MAXLAT_5 for CMD2 and ACMD41, but |
| 547 | * it's too difficult to determine whether this is an ACMD or |
| 548 | * not. Better make it 64. |
| 549 | */ |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 550 | cmdr |= ATMCI_CMDR_MAXLAT_64CYC; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 551 | |
| 552 | if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 553 | cmdr |= ATMCI_CMDR_OPDCMD; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 554 | |
| 555 | data = cmd->data; |
| 556 | if (data) { |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 557 | cmdr |= ATMCI_CMDR_START_XFER; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 558 | |
| 559 | if (cmd->opcode == SD_IO_RW_EXTENDED) { |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 560 | cmdr |= ATMCI_CMDR_SDIO_BLOCK; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 561 | } else { |
| 562 | if (data->flags & MMC_DATA_STREAM) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 563 | cmdr |= ATMCI_CMDR_STREAM; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 564 | else if (data->blocks > 1) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 565 | cmdr |= ATMCI_CMDR_MULTI_BLOCK; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 566 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 567 | cmdr |= ATMCI_CMDR_BLOCK; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 568 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 569 | |
| 570 | if (data->flags & MMC_DATA_READ) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 571 | cmdr |= ATMCI_CMDR_TRDIR_READ; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | return cmdr; |
| 575 | } |
| 576 | |
Ludovic Desroches | 11d1488 | 2011-08-11 15:25:45 +0000 | [diff] [blame] | 577 | static void atmci_send_command(struct atmel_mci *host, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 578 | struct mmc_command *cmd, u32 cmd_flags) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 579 | { |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 580 | WARN_ON(host->cmd); |
| 581 | host->cmd = cmd; |
| 582 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 583 | dev_vdbg(&host->pdev->dev, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 584 | "start command: ARGR=0x%08x CMDR=0x%08x\n", |
| 585 | cmd->arg, cmd_flags); |
| 586 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 587 | atmci_writel(host, ATMCI_ARGR, cmd->arg); |
| 588 | atmci_writel(host, ATMCI_CMDR, cmd_flags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 591 | static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 592 | { |
Ludovic Desroches | 11d1488 | 2011-08-11 15:25:45 +0000 | [diff] [blame] | 593 | atmci_send_command(host, data->stop, host->stop_cmdr); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 594 | atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 597 | /* |
| 598 | * Configure given PDC buffer taking care of alignement issues. |
| 599 | * Update host->data_size and host->sg. |
| 600 | */ |
| 601 | static void atmci_pdc_set_single_buf(struct atmel_mci *host, |
| 602 | enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 603 | { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 604 | u32 pointer_reg, counter_reg; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 605 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 606 | if (dir == XFER_RECEIVE) { |
| 607 | pointer_reg = ATMEL_PDC_RPR; |
| 608 | counter_reg = ATMEL_PDC_RCR; |
| 609 | } else { |
| 610 | pointer_reg = ATMEL_PDC_TPR; |
| 611 | counter_reg = ATMEL_PDC_TCR; |
| 612 | } |
| 613 | |
| 614 | if (buf_nb == PDC_SECOND_BUF) { |
Ludovic Desroches | 1ebbe3d | 2011-08-11 15:25:46 +0000 | [diff] [blame] | 615 | pointer_reg += ATMEL_PDC_SCND_BUF_OFF; |
| 616 | counter_reg += ATMEL_PDC_SCND_BUF_OFF; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | atmci_writel(host, pointer_reg, sg_dma_address(host->sg)); |
Ludovic Desroches | 341fa4c | 2011-08-11 15:25:47 +0000 | [diff] [blame] | 620 | if (host->data_size <= sg_dma_len(host->sg)) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 621 | if (host->data_size & 0x3) { |
| 622 | /* If size is different from modulo 4, transfer bytes */ |
| 623 | atmci_writel(host, counter_reg, host->data_size); |
| 624 | atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE); |
| 625 | } else { |
| 626 | /* Else transfer 32-bits words */ |
| 627 | atmci_writel(host, counter_reg, host->data_size / 4); |
| 628 | } |
| 629 | host->data_size = 0; |
| 630 | } else { |
| 631 | /* We assume the size of a page is 32-bits aligned */ |
Ludovic Desroches | 341fa4c | 2011-08-11 15:25:47 +0000 | [diff] [blame] | 632 | atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4); |
| 633 | host->data_size -= sg_dma_len(host->sg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 634 | if (host->data_size) |
| 635 | host->sg = sg_next(host->sg); |
| 636 | } |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 639 | /* |
| 640 | * Configure PDC buffer according to the data size ie configuring one or two |
| 641 | * buffers. Don't use this function if you want to configure only the second |
| 642 | * buffer. In this case, use atmci_pdc_set_single_buf. |
| 643 | */ |
| 644 | static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 645 | { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 646 | atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF); |
| 647 | if (host->data_size) |
| 648 | atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF); |
| 649 | } |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 650 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 651 | /* |
| 652 | * Unmap sg lists, called when transfer is finished. |
| 653 | */ |
| 654 | static void atmci_pdc_cleanup(struct atmel_mci *host) |
| 655 | { |
| 656 | struct mmc_data *data = host->data; |
| 657 | |
| 658 | if (data) |
| 659 | dma_unmap_sg(&host->pdev->dev, |
| 660 | data->sg, data->sg_len, |
| 661 | ((data->flags & MMC_DATA_WRITE) |
| 662 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after |
| 667 | * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY |
| 668 | * interrupt needed for both transfer directions. |
| 669 | */ |
| 670 | static void atmci_pdc_complete(struct atmel_mci *host) |
| 671 | { |
| 672 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
| 673 | atmci_pdc_cleanup(host); |
| 674 | |
| 675 | /* |
| 676 | * If the card was removed, data will be NULL. No point trying |
| 677 | * to send the stop command or waiting for NBUSY in this case. |
| 678 | */ |
| 679 | if (host->data) { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 680 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 681 | tasklet_schedule(&host->tasklet); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 682 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 686 | static void atmci_dma_cleanup(struct atmel_mci *host) |
| 687 | { |
| 688 | struct mmc_data *data = host->data; |
| 689 | |
| 690 | if (data) |
| 691 | dma_unmap_sg(host->dma.chan->device->dev, |
| 692 | data->sg, data->sg_len, |
| 693 | ((data->flags & MMC_DATA_WRITE) |
| 694 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * This function is called by the DMA driver from tasklet context. |
| 699 | */ |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 700 | static void atmci_dma_complete(void *arg) |
| 701 | { |
| 702 | struct atmel_mci *host = arg; |
| 703 | struct mmc_data *data = host->data; |
| 704 | |
| 705 | dev_vdbg(&host->pdev->dev, "DMA complete\n"); |
| 706 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 707 | if (host->caps.has_dma) |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 708 | /* Disable DMA hardware handshaking on MCI */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 709 | atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 710 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 711 | atmci_dma_cleanup(host); |
| 712 | |
| 713 | /* |
| 714 | * If the card was removed, data will be NULL. No point trying |
| 715 | * to send the stop command or waiting for NBUSY in this case. |
| 716 | */ |
| 717 | if (data) { |
| 718 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
| 719 | tasklet_schedule(&host->tasklet); |
| 720 | |
| 721 | /* |
| 722 | * Regardless of what the documentation says, we have |
| 723 | * to wait for NOTBUSY even after block read |
| 724 | * operations. |
| 725 | * |
| 726 | * When the DMA transfer is complete, the controller |
| 727 | * may still be reading the CRC from the card, i.e. |
| 728 | * the data transfer is still in progress and we |
| 729 | * haven't seen all the potential error bits yet. |
| 730 | * |
| 731 | * The interrupt handler will schedule a different |
| 732 | * tasklet to finish things up when the data transfer |
| 733 | * is completely done. |
| 734 | * |
| 735 | * We may not complete the mmc request here anyway |
| 736 | * because the mmc layer may call back and cause us to |
| 737 | * violate the "don't submit new operations from the |
| 738 | * completion callback" rule of the dma engine |
| 739 | * framework. |
| 740 | */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 741 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 745 | /* |
| 746 | * Returns a mask of interrupt flags to be enabled after the whole |
| 747 | * request has been prepared. |
| 748 | */ |
| 749 | static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data) |
| 750 | { |
| 751 | u32 iflags; |
| 752 | |
| 753 | data->error = -EINPROGRESS; |
| 754 | |
| 755 | host->sg = data->sg; |
| 756 | host->data = data; |
| 757 | host->data_chan = NULL; |
| 758 | |
| 759 | iflags = ATMCI_DATA_ERROR_FLAGS; |
| 760 | |
| 761 | /* |
| 762 | * Errata: MMC data write operation with less than 12 |
| 763 | * bytes is impossible. |
| 764 | * |
| 765 | * Errata: MCI Transmit Data Register (TDR) FIFO |
| 766 | * corruption when length is not multiple of 4. |
| 767 | */ |
| 768 | if (data->blocks * data->blksz < 12 |
| 769 | || (data->blocks * data->blksz) & 3) |
| 770 | host->need_reset = true; |
| 771 | |
| 772 | host->pio_offset = 0; |
| 773 | if (data->flags & MMC_DATA_READ) |
| 774 | iflags |= ATMCI_RXRDY; |
| 775 | else |
| 776 | iflags |= ATMCI_TXRDY; |
| 777 | |
| 778 | return iflags; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Set interrupt flags and set block length into the MCI mode register even |
| 783 | * if this value is also accessible in the MCI block register. It seems to be |
| 784 | * necessary before the High Speed MCI version. It also map sg and configure |
| 785 | * PDC registers. |
| 786 | */ |
| 787 | static u32 |
| 788 | atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data) |
| 789 | { |
| 790 | u32 iflags, tmp; |
| 791 | unsigned int sg_len; |
| 792 | enum dma_data_direction dir; |
| 793 | |
| 794 | data->error = -EINPROGRESS; |
| 795 | |
| 796 | host->data = data; |
| 797 | host->sg = data->sg; |
| 798 | iflags = ATMCI_DATA_ERROR_FLAGS; |
| 799 | |
| 800 | /* Enable pdc mode */ |
| 801 | atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE); |
| 802 | |
| 803 | if (data->flags & MMC_DATA_READ) { |
| 804 | dir = DMA_FROM_DEVICE; |
| 805 | iflags |= ATMCI_ENDRX | ATMCI_RXBUFF; |
| 806 | } else { |
| 807 | dir = DMA_TO_DEVICE; |
| 808 | iflags |= ATMCI_ENDTX | ATMCI_TXBUFE; |
| 809 | } |
| 810 | |
| 811 | /* Set BLKLEN */ |
| 812 | tmp = atmci_readl(host, ATMCI_MR); |
| 813 | tmp &= 0x0000ffff; |
| 814 | tmp |= ATMCI_BLKLEN(data->blksz); |
| 815 | atmci_writel(host, ATMCI_MR, tmp); |
| 816 | |
| 817 | /* Configure PDC */ |
| 818 | host->data_size = data->blocks * data->blksz; |
| 819 | sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 820 | if (host->data_size) |
| 821 | atmci_pdc_set_both_buf(host, |
| 822 | ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT)); |
| 823 | |
| 824 | return iflags; |
| 825 | } |
| 826 | |
| 827 | static u32 |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 828 | atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 829 | { |
| 830 | struct dma_chan *chan; |
| 831 | struct dma_async_tx_descriptor *desc; |
| 832 | struct scatterlist *sg; |
| 833 | unsigned int i; |
| 834 | enum dma_data_direction direction; |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 835 | enum dma_transfer_direction slave_dirn; |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 836 | unsigned int sglen; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 837 | u32 iflags; |
| 838 | |
| 839 | data->error = -EINPROGRESS; |
| 840 | |
| 841 | WARN_ON(host->data); |
| 842 | host->sg = NULL; |
| 843 | host->data = data; |
| 844 | |
| 845 | iflags = ATMCI_DATA_ERROR_FLAGS; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * We don't do DMA on "complex" transfers, i.e. with |
| 849 | * non-word-aligned buffers or lengths. Also, we don't bother |
| 850 | * with all the DMA setup overhead for short transfers. |
| 851 | */ |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 852 | if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD) |
| 853 | return atmci_prepare_data(host, data); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 854 | if (data->blksz & 3) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 855 | return atmci_prepare_data(host, data); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 856 | |
| 857 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 858 | if (sg->offset & 3 || sg->length & 3) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 859 | return atmci_prepare_data(host, data); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* If we don't have a channel, we can't do DMA */ |
| 863 | chan = host->dma.chan; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 864 | if (chan) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 865 | host->data_chan = chan; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 866 | |
| 867 | if (!chan) |
| 868 | return -ENODEV; |
| 869 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 870 | if (host->caps.has_dma) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 871 | atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(3) | ATMCI_DMAEN); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 872 | |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 873 | if (data->flags & MMC_DATA_READ) { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 874 | direction = DMA_FROM_DEVICE; |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 875 | host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM; |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 876 | } else { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 877 | direction = DMA_TO_DEVICE; |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 878 | host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV; |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 879 | } |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 880 | |
Linus Walleij | 266ac3f | 2011-02-10 16:08:06 +0100 | [diff] [blame] | 881 | sglen = dma_map_sg(chan->device->dev, data->sg, |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 882 | data->sg_len, direction); |
Linus Walleij | 88ce4db3 | 2011-02-10 16:08:16 +0100 | [diff] [blame] | 883 | |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 884 | dmaengine_slave_config(chan, &host->dma_conf); |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 885 | desc = dmaengine_prep_slave_sg(chan, |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 886 | data->sg, sglen, slave_dirn, |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 887 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 888 | if (!desc) |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 889 | goto unmap_exit; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 890 | |
| 891 | host->dma.data_desc = desc; |
| 892 | desc->callback = atmci_dma_complete; |
| 893 | desc->callback_param = host; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 894 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 895 | return iflags; |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 896 | unmap_exit: |
Linus Walleij | 88ce4db3 | 2011-02-10 16:08:16 +0100 | [diff] [blame] | 897 | dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction); |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 898 | return -ENOMEM; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 899 | } |
| 900 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 901 | static void |
| 902 | atmci_submit_data(struct atmel_mci *host, struct mmc_data *data) |
| 903 | { |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Start PDC according to transfer direction. |
| 909 | */ |
| 910 | static void |
| 911 | atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data) |
| 912 | { |
| 913 | if (data->flags & MMC_DATA_READ) |
| 914 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); |
| 915 | else |
| 916 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); |
| 917 | } |
| 918 | |
| 919 | static void |
| 920 | atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data) |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 921 | { |
| 922 | struct dma_chan *chan = host->data_chan; |
| 923 | struct dma_async_tx_descriptor *desc = host->dma.data_desc; |
| 924 | |
| 925 | if (chan) { |
Linus Walleij | 5328906 | 2011-02-10 16:08:26 +0100 | [diff] [blame] | 926 | dmaengine_submit(desc); |
| 927 | dma_async_issue_pending(chan); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 931 | static void atmci_stop_transfer(struct atmel_mci *host) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 932 | { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 933 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 934 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 937 | /* |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 938 | * Stop data transfer because error(s) occured. |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 939 | */ |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 940 | static void atmci_stop_transfer_pdc(struct atmel_mci *host) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 941 | { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 942 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
| 943 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 946 | static void atmci_stop_transfer_dma(struct atmel_mci *host) |
| 947 | { |
| 948 | struct dma_chan *chan = host->data_chan; |
| 949 | |
| 950 | if (chan) { |
| 951 | dmaengine_terminate_all(chan); |
| 952 | atmci_dma_cleanup(host); |
| 953 | } else { |
| 954 | /* Data transfer was stopped by the interrupt handler */ |
| 955 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
| 956 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Start a request: prepare data if needed, prepare the command and activate |
| 962 | * interrupts. |
| 963 | */ |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 964 | static void atmci_start_request(struct atmel_mci *host, |
| 965 | struct atmel_mci_slot *slot) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 966 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 967 | struct mmc_request *mrq; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 968 | struct mmc_command *cmd; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 969 | struct mmc_data *data; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 970 | u32 iflags; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 971 | u32 cmdflags; |
| 972 | |
| 973 | mrq = slot->mrq; |
| 974 | host->cur_slot = slot; |
| 975 | host->mrq = mrq; |
| 976 | |
| 977 | host->pending_events = 0; |
| 978 | host->completed_events = 0; |
Haavard Skinnemoen | ca55f46 | 2008-10-05 15:16:59 +0200 | [diff] [blame] | 979 | host->data_status = 0; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 980 | |
| 981 | if (host->need_reset) { |
Ludovic Desroches | 18ee684 | 2012-02-09 11:55:29 +0100 | [diff] [blame] | 982 | iflags = atmci_readl(host, ATMCI_IMR); |
| 983 | iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 984 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
| 985 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); |
| 986 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 987 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 988 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Ludovic Desroches | 18ee684 | 2012-02-09 11:55:29 +0100 | [diff] [blame] | 989 | atmci_writel(host, ATMCI_IER, iflags); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 990 | host->need_reset = false; |
| 991 | } |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 992 | atmci_writel(host, ATMCI_SDCR, slot->sdc_reg); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 993 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 994 | iflags = atmci_readl(host, ATMCI_IMR); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 995 | if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB)) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 996 | dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n", |
| 997 | iflags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 998 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 999 | if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) { |
| 1000 | /* Send init sequence (74 clock cycles) */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1001 | atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT); |
| 1002 | while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY)) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1003 | cpu_relax(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1004 | } |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1005 | iflags = 0; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1006 | data = mrq->data; |
| 1007 | if (data) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1008 | atmci_set_timeout(host, slot, data); |
Haavard Skinnemoen | a252e3e | 2008-10-03 14:46:17 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* Must set block count/size before sending command */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1011 | atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1012 | | ATMCI_BLKLEN(data->blksz)); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1013 | dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n", |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1014 | ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz)); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1015 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1016 | iflags |= host->prepare_data(host, data); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1019 | iflags |= ATMCI_CMDRDY; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1020 | cmd = mrq->cmd; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1021 | cmdflags = atmci_prepare_command(slot->mmc, cmd); |
Ludovic Desroches | 11d1488 | 2011-08-11 15:25:45 +0000 | [diff] [blame] | 1022 | atmci_send_command(host, cmd, cmdflags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1023 | |
| 1024 | if (data) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1025 | host->submit_data(host, data); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1026 | |
| 1027 | if (mrq->stop) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1028 | host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1029 | host->stop_cmdr |= ATMCI_CMDR_STOP_XFER; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1030 | if (!(data->flags & MMC_DATA_WRITE)) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1031 | host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1032 | if (data->flags & MMC_DATA_STREAM) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1033 | host->stop_cmdr |= ATMCI_CMDR_STREAM; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1034 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1035 | host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * We could have enabled interrupts earlier, but I suspect |
| 1040 | * that would open up a nice can of interesting race |
| 1041 | * conditions (e.g. command and data complete, but stop not |
| 1042 | * prepared yet.) |
| 1043 | */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1044 | atmci_writel(host, ATMCI_IER, iflags); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1045 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1046 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1047 | static void atmci_queue_request(struct atmel_mci *host, |
| 1048 | struct atmel_mci_slot *slot, struct mmc_request *mrq) |
| 1049 | { |
| 1050 | dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", |
| 1051 | host->state); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1052 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1053 | spin_lock_bh(&host->lock); |
| 1054 | slot->mrq = mrq; |
| 1055 | if (host->state == STATE_IDLE) { |
| 1056 | host->state = STATE_SENDING_CMD; |
| 1057 | atmci_start_request(host, slot); |
| 1058 | } else { |
| 1059 | list_add_tail(&slot->queue_node, &host->queue); |
| 1060 | } |
| 1061 | spin_unlock_bh(&host->lock); |
| 1062 | } |
| 1063 | |
| 1064 | static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1065 | { |
| 1066 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1067 | struct atmel_mci *host = slot->host; |
| 1068 | struct mmc_data *data; |
| 1069 | |
| 1070 | WARN_ON(slot->mrq); |
| 1071 | |
| 1072 | /* |
| 1073 | * We may "know" the card is gone even though there's still an |
| 1074 | * electrical connection. If so, we really need to communicate |
| 1075 | * this to the MMC core since there won't be any more |
| 1076 | * interrupts as the card is completely removed. Otherwise, |
| 1077 | * the MMC core might believe the card is still there even |
| 1078 | * though the card was just removed very slowly. |
| 1079 | */ |
| 1080 | if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) { |
| 1081 | mrq->cmd->error = -ENOMEDIUM; |
| 1082 | mmc_request_done(mmc, mrq); |
| 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | /* We don't support multiple blocks of weird lengths. */ |
| 1087 | data = mrq->data; |
| 1088 | if (data && data->blocks > 1 && data->blksz & 3) { |
| 1089 | mrq->cmd->error = -EINVAL; |
| 1090 | mmc_request_done(mmc, mrq); |
| 1091 | } |
| 1092 | |
| 1093 | atmci_queue_request(host, slot, mrq); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1097 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1098 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1099 | struct atmel_mci *host = slot->host; |
| 1100 | unsigned int i; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1101 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1102 | slot->sdc_reg &= ~ATMCI_SDCBUS_MASK; |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1103 | switch (ios->bus_width) { |
| 1104 | case MMC_BUS_WIDTH_1: |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1105 | slot->sdc_reg |= ATMCI_SDCBUS_1BIT; |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1106 | break; |
| 1107 | case MMC_BUS_WIDTH_4: |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1108 | slot->sdc_reg |= ATMCI_SDCBUS_4BIT; |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1109 | break; |
| 1110 | } |
| 1111 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1112 | if (ios->clock) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1113 | unsigned int clock_min = ~0U; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1114 | u32 clkdiv; |
| 1115 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1116 | spin_lock_bh(&host->lock); |
| 1117 | if (!host->mode_reg) { |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1118 | clk_enable(host->mck); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1119 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
| 1120 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1121 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1122 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1123 | } |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1124 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1125 | /* |
| 1126 | * Use mirror of ios->clock to prevent race with mmc |
| 1127 | * core ios update when finding the minimum. |
| 1128 | */ |
| 1129 | slot->clock = ios->clock; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1130 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1131 | if (host->slot[i] && host->slot[i]->clock |
| 1132 | && host->slot[i]->clock < clock_min) |
| 1133 | clock_min = host->slot[i]->clock; |
| 1134 | } |
| 1135 | |
| 1136 | /* Calculate clock divider */ |
| 1137 | clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1138 | if (clkdiv > 255) { |
| 1139 | dev_warn(&mmc->class_dev, |
| 1140 | "clock %u too slow; using %lu\n", |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1141 | clock_min, host->bus_hz / (2 * 256)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1142 | clkdiv = 255; |
| 1143 | } |
| 1144 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1145 | host->mode_reg = ATMCI_MR_CLKDIV(clkdiv); |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 1146 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1147 | /* |
| 1148 | * WRPROOF and RDPROOF prevent overruns/underruns by |
| 1149 | * stopping the clock when the FIFO is full/empty. |
| 1150 | * This state is not expected to last for long. |
| 1151 | */ |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1152 | if (host->caps.has_rwproof) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1153 | host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1154 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1155 | if (host->caps.has_cfg_reg) { |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1156 | /* setup High Speed mode in relation with card capacity */ |
| 1157 | if (ios->timing == MMC_TIMING_SD_HS) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1158 | host->cfg_reg |= ATMCI_CFG_HSMODE; |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1159 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1160 | host->cfg_reg &= ~ATMCI_CFG_HSMODE; |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | if (list_empty(&host->queue)) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1164 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1165 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1166 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1167 | } else { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1168 | host->need_clock_update = true; |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1169 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1170 | |
| 1171 | spin_unlock_bh(&host->lock); |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1172 | } else { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1173 | bool any_slot_active = false; |
| 1174 | |
| 1175 | spin_lock_bh(&host->lock); |
| 1176 | slot->clock = 0; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1177 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1178 | if (host->slot[i] && host->slot[i]->clock) { |
| 1179 | any_slot_active = true; |
| 1180 | break; |
| 1181 | } |
Haavard Skinnemoen | 945533b5 | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1182 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1183 | if (!any_slot_active) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1184 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1185 | if (host->mode_reg) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1186 | atmci_readl(host, ATMCI_MR); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1187 | clk_disable(host->mck); |
| 1188 | } |
| 1189 | host->mode_reg = 0; |
| 1190 | } |
| 1191 | spin_unlock_bh(&host->lock); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | switch (ios->power_mode) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1195 | case MMC_POWER_UP: |
| 1196 | set_bit(ATMCI_CARD_NEED_INIT, &slot->flags); |
| 1197 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1198 | default: |
| 1199 | /* |
| 1200 | * TODO: None of the currently available AVR32-based |
| 1201 | * boards allow MMC power to be turned off. Implement |
| 1202 | * power control when this can be tested properly. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1203 | * |
| 1204 | * We also need to hook this into the clock management |
| 1205 | * somehow so that newly inserted cards aren't |
| 1206 | * subjected to a fast clock before we have a chance |
| 1207 | * to figure out what the maximum rate is. Currently, |
| 1208 | * there's no way to avoid this, and there never will |
| 1209 | * be for boards that don't support power control. |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1210 | */ |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | static int atmci_get_ro(struct mmc_host *mmc) |
| 1216 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1217 | int read_only = -ENOSYS; |
| 1218 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1219 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1220 | if (gpio_is_valid(slot->wp_pin)) { |
| 1221 | read_only = gpio_get_value(slot->wp_pin); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1222 | dev_dbg(&mmc->class_dev, "card is %s\n", |
| 1223 | read_only ? "read-only" : "read-write"); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | return read_only; |
| 1227 | } |
| 1228 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1229 | static int atmci_get_cd(struct mmc_host *mmc) |
| 1230 | { |
| 1231 | int present = -ENOSYS; |
| 1232 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1233 | |
| 1234 | if (gpio_is_valid(slot->detect_pin)) { |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 1235 | present = !(gpio_get_value(slot->detect_pin) ^ |
| 1236 | slot->detect_is_active_high); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1237 | dev_dbg(&mmc->class_dev, "card is %spresent\n", |
| 1238 | present ? "" : "not "); |
| 1239 | } |
| 1240 | |
| 1241 | return present; |
| 1242 | } |
| 1243 | |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1244 | static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 1245 | { |
| 1246 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1247 | struct atmel_mci *host = slot->host; |
| 1248 | |
| 1249 | if (enable) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1250 | atmci_writel(host, ATMCI_IER, slot->sdio_irq); |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1251 | else |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1252 | atmci_writel(host, ATMCI_IDR, slot->sdio_irq); |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1255 | static const struct mmc_host_ops atmci_ops = { |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1256 | .request = atmci_request, |
| 1257 | .set_ios = atmci_set_ios, |
| 1258 | .get_ro = atmci_get_ro, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1259 | .get_cd = atmci_get_cd, |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1260 | .enable_sdio_irq = atmci_enable_sdio_irq, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1261 | }; |
| 1262 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1263 | /* Called with host->lock held */ |
| 1264 | static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq) |
| 1265 | __releases(&host->lock) |
| 1266 | __acquires(&host->lock) |
| 1267 | { |
| 1268 | struct atmel_mci_slot *slot = NULL; |
| 1269 | struct mmc_host *prev_mmc = host->cur_slot->mmc; |
| 1270 | |
| 1271 | WARN_ON(host->cmd || host->data); |
| 1272 | |
| 1273 | /* |
| 1274 | * Update the MMC clock rate if necessary. This may be |
| 1275 | * necessary if set_ios() is called when a different slot is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1276 | * busy transferring data. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1277 | */ |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1278 | if (host->need_clock_update) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1279 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1280 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1281 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1282 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1283 | |
| 1284 | host->cur_slot->mrq = NULL; |
| 1285 | host->mrq = NULL; |
| 1286 | if (!list_empty(&host->queue)) { |
| 1287 | slot = list_entry(host->queue.next, |
| 1288 | struct atmel_mci_slot, queue_node); |
| 1289 | list_del(&slot->queue_node); |
| 1290 | dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n", |
| 1291 | mmc_hostname(slot->mmc)); |
| 1292 | host->state = STATE_SENDING_CMD; |
| 1293 | atmci_start_request(host, slot); |
| 1294 | } else { |
| 1295 | dev_vdbg(&host->pdev->dev, "list empty\n"); |
| 1296 | host->state = STATE_IDLE; |
| 1297 | } |
| 1298 | |
| 1299 | spin_unlock(&host->lock); |
| 1300 | mmc_request_done(prev_mmc, mrq); |
| 1301 | spin_lock(&host->lock); |
| 1302 | } |
| 1303 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1304 | static void atmci_command_complete(struct atmel_mci *host, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1305 | struct mmc_command *cmd) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1306 | { |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1307 | u32 status = host->cmd_status; |
| 1308 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1309 | /* Read the response from the card (up to 16 bytes) */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1310 | cmd->resp[0] = atmci_readl(host, ATMCI_RSPR); |
| 1311 | cmd->resp[1] = atmci_readl(host, ATMCI_RSPR); |
| 1312 | cmd->resp[2] = atmci_readl(host, ATMCI_RSPR); |
| 1313 | cmd->resp[3] = atmci_readl(host, ATMCI_RSPR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1314 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1315 | if (status & ATMCI_RTOE) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1316 | cmd->error = -ETIMEDOUT; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1317 | else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE)) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1318 | cmd->error = -EILSEQ; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1319 | else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE)) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1320 | cmd->error = -EIO; |
| 1321 | else |
| 1322 | cmd->error = 0; |
| 1323 | |
| 1324 | if (cmd->error) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1325 | dev_dbg(&host->pdev->dev, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1326 | "command error: status=0x%08x\n", status); |
| 1327 | |
| 1328 | if (cmd->data) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1329 | host->stop_transfer(host); |
Nicolas Ferre | 009a891 | 2010-05-11 14:06:49 -0700 | [diff] [blame] | 1330 | host->data = NULL; |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1331 | atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1332 | | ATMCI_TXRDY | ATMCI_RXRDY |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1333 | | ATMCI_DATA_ERROR_FLAGS); |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | static void atmci_detect_change(unsigned long data) |
| 1339 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1340 | struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data; |
| 1341 | bool present; |
| 1342 | bool present_old; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1343 | |
| 1344 | /* |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1345 | * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before |
| 1346 | * freeing the interrupt. We must not re-enable the interrupt |
| 1347 | * if it has been freed, and if we're shutting down, it |
| 1348 | * doesn't really matter whether the card is present or not. |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1349 | */ |
| 1350 | smp_rmb(); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1351 | if (test_bit(ATMCI_SHUTDOWN, &slot->flags)) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1352 | return; |
| 1353 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1354 | enable_irq(gpio_to_irq(slot->detect_pin)); |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 1355 | present = !(gpio_get_value(slot->detect_pin) ^ |
| 1356 | slot->detect_is_active_high); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1357 | present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1358 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1359 | dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n", |
| 1360 | present, present_old); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1361 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1362 | if (present != present_old) { |
| 1363 | struct atmel_mci *host = slot->host; |
| 1364 | struct mmc_request *mrq; |
| 1365 | |
| 1366 | dev_dbg(&slot->mmc->class_dev, "card %s\n", |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1367 | present ? "inserted" : "removed"); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1368 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1369 | spin_lock(&host->lock); |
| 1370 | |
| 1371 | if (!present) |
| 1372 | clear_bit(ATMCI_CARD_PRESENT, &slot->flags); |
| 1373 | else |
| 1374 | set_bit(ATMCI_CARD_PRESENT, &slot->flags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1375 | |
| 1376 | /* Clean up queue if present */ |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1377 | mrq = slot->mrq; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1378 | if (mrq) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1379 | if (mrq == host->mrq) { |
| 1380 | /* |
| 1381 | * Reset controller to terminate any ongoing |
| 1382 | * commands or data transfers. |
| 1383 | */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1384 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
| 1385 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); |
| 1386 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1387 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1388 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1389 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1390 | host->data = NULL; |
| 1391 | host->cmd = NULL; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1392 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1393 | switch (host->state) { |
| 1394 | case STATE_IDLE: |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1395 | break; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1396 | case STATE_SENDING_CMD: |
| 1397 | mrq->cmd->error = -ENOMEDIUM; |
| 1398 | if (!mrq->data) |
| 1399 | break; |
| 1400 | /* fall through */ |
| 1401 | case STATE_SENDING_DATA: |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1402 | mrq->data->error = -ENOMEDIUM; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1403 | host->stop_transfer(host); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1404 | break; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1405 | case STATE_DATA_BUSY: |
| 1406 | case STATE_DATA_ERROR: |
| 1407 | if (mrq->data->error == -EINPROGRESS) |
| 1408 | mrq->data->error = -ENOMEDIUM; |
| 1409 | if (!mrq->stop) |
| 1410 | break; |
| 1411 | /* fall through */ |
| 1412 | case STATE_SENDING_STOP: |
| 1413 | mrq->stop->error = -ENOMEDIUM; |
| 1414 | break; |
| 1415 | } |
| 1416 | |
| 1417 | atmci_request_end(host, mrq); |
| 1418 | } else { |
| 1419 | list_del(&slot->queue_node); |
| 1420 | mrq->cmd->error = -ENOMEDIUM; |
| 1421 | if (mrq->data) |
| 1422 | mrq->data->error = -ENOMEDIUM; |
| 1423 | if (mrq->stop) |
| 1424 | mrq->stop->error = -ENOMEDIUM; |
| 1425 | |
| 1426 | spin_unlock(&host->lock); |
| 1427 | mmc_request_done(slot->mmc, mrq); |
| 1428 | spin_lock(&host->lock); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1429 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1430 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1431 | spin_unlock(&host->lock); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1432 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1433 | mmc_detect_change(slot->mmc, 0); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | static void atmci_tasklet_func(unsigned long priv) |
| 1438 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1439 | struct atmel_mci *host = (struct atmel_mci *)priv; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1440 | struct mmc_request *mrq = host->mrq; |
| 1441 | struct mmc_data *data = host->data; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1442 | struct mmc_command *cmd = host->cmd; |
| 1443 | enum atmel_mci_state state = host->state; |
| 1444 | enum atmel_mci_state prev_state; |
| 1445 | u32 status; |
| 1446 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1447 | spin_lock(&host->lock); |
| 1448 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1449 | state = host->state; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1450 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1451 | dev_vdbg(&host->pdev->dev, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1452 | "tasklet: state %u pending/completed/mask %lx/%lx/%x\n", |
| 1453 | state, host->pending_events, host->completed_events, |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1454 | atmci_readl(host, ATMCI_IMR)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1455 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1456 | do { |
| 1457 | prev_state = state; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1458 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1459 | switch (state) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1460 | case STATE_IDLE: |
| 1461 | break; |
| 1462 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1463 | case STATE_SENDING_CMD: |
| 1464 | if (!atmci_test_and_clear_pending(host, |
| 1465 | EVENT_CMD_COMPLETE)) |
| 1466 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1467 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1468 | host->cmd = NULL; |
| 1469 | atmci_set_completed(host, EVENT_CMD_COMPLETE); |
| 1470 | atmci_command_complete(host, mrq->cmd); |
| 1471 | if (!mrq->data || cmd->error) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1472 | atmci_request_end(host, host->mrq); |
| 1473 | goto unlock; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1474 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1475 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1476 | prev_state = state = STATE_SENDING_DATA; |
| 1477 | /* fall through */ |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1478 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1479 | case STATE_SENDING_DATA: |
| 1480 | if (atmci_test_and_clear_pending(host, |
| 1481 | EVENT_DATA_ERROR)) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1482 | host->stop_transfer(host); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1483 | if (data->stop) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1484 | atmci_send_stop_cmd(host, data); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1485 | state = STATE_DATA_ERROR; |
| 1486 | break; |
| 1487 | } |
| 1488 | |
| 1489 | if (!atmci_test_and_clear_pending(host, |
| 1490 | EVENT_XFER_COMPLETE)) |
| 1491 | break; |
| 1492 | |
| 1493 | atmci_set_completed(host, EVENT_XFER_COMPLETE); |
| 1494 | prev_state = state = STATE_DATA_BUSY; |
| 1495 | /* fall through */ |
| 1496 | |
| 1497 | case STATE_DATA_BUSY: |
| 1498 | if (!atmci_test_and_clear_pending(host, |
| 1499 | EVENT_DATA_COMPLETE)) |
| 1500 | break; |
| 1501 | |
| 1502 | host->data = NULL; |
| 1503 | atmci_set_completed(host, EVENT_DATA_COMPLETE); |
| 1504 | status = host->data_status; |
| 1505 | if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) { |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1506 | if (status & ATMCI_DTOE) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1507 | dev_dbg(&host->pdev->dev, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1508 | "data timeout error\n"); |
| 1509 | data->error = -ETIMEDOUT; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1510 | } else if (status & ATMCI_DCRCE) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1511 | dev_dbg(&host->pdev->dev, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1512 | "data CRC error\n"); |
| 1513 | data->error = -EILSEQ; |
| 1514 | } else { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1515 | dev_dbg(&host->pdev->dev, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1516 | "data FIFO error (status=%08x)\n", |
| 1517 | status); |
| 1518 | data->error = -EIO; |
| 1519 | } |
| 1520 | } else { |
| 1521 | data->bytes_xfered = data->blocks * data->blksz; |
| 1522 | data->error = 0; |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1523 | atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | if (!data->stop) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1527 | atmci_request_end(host, host->mrq); |
| 1528 | goto unlock; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | prev_state = state = STATE_SENDING_STOP; |
| 1532 | if (!data->error) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1533 | atmci_send_stop_cmd(host, data); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1534 | /* fall through */ |
| 1535 | |
| 1536 | case STATE_SENDING_STOP: |
| 1537 | if (!atmci_test_and_clear_pending(host, |
| 1538 | EVENT_CMD_COMPLETE)) |
| 1539 | break; |
| 1540 | |
| 1541 | host->cmd = NULL; |
| 1542 | atmci_command_complete(host, mrq->stop); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1543 | atmci_request_end(host, host->mrq); |
| 1544 | goto unlock; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1545 | |
| 1546 | case STATE_DATA_ERROR: |
| 1547 | if (!atmci_test_and_clear_pending(host, |
| 1548 | EVENT_XFER_COMPLETE)) |
| 1549 | break; |
| 1550 | |
| 1551 | state = STATE_DATA_BUSY; |
| 1552 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1553 | } |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1554 | } while (state != prev_state); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1555 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1556 | host->state = state; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1557 | |
| 1558 | unlock: |
| 1559 | spin_unlock(&host->lock); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | static void atmci_read_data_pio(struct atmel_mci *host) |
| 1563 | { |
| 1564 | struct scatterlist *sg = host->sg; |
| 1565 | void *buf = sg_virt(sg); |
| 1566 | unsigned int offset = host->pio_offset; |
| 1567 | struct mmc_data *data = host->data; |
| 1568 | u32 value; |
| 1569 | u32 status; |
| 1570 | unsigned int nbytes = 0; |
| 1571 | |
| 1572 | do { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1573 | value = atmci_readl(host, ATMCI_RDR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1574 | if (likely(offset + 4 <= sg->length)) { |
| 1575 | put_unaligned(value, (u32 *)(buf + offset)); |
| 1576 | |
| 1577 | offset += 4; |
| 1578 | nbytes += 4; |
| 1579 | |
| 1580 | if (offset == sg->length) { |
Haavard Skinnemoen | 5e7184a | 2008-10-05 15:27:50 +0200 | [diff] [blame] | 1581 | flush_dcache_page(sg_page(sg)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1582 | host->sg = sg = sg_next(sg); |
| 1583 | if (!sg) |
| 1584 | goto done; |
| 1585 | |
| 1586 | offset = 0; |
| 1587 | buf = sg_virt(sg); |
| 1588 | } |
| 1589 | } else { |
| 1590 | unsigned int remaining = sg->length - offset; |
| 1591 | memcpy(buf + offset, &value, remaining); |
| 1592 | nbytes += remaining; |
| 1593 | |
| 1594 | flush_dcache_page(sg_page(sg)); |
| 1595 | host->sg = sg = sg_next(sg); |
| 1596 | if (!sg) |
| 1597 | goto done; |
| 1598 | |
| 1599 | offset = 4 - remaining; |
| 1600 | buf = sg_virt(sg); |
| 1601 | memcpy(buf, (u8 *)&value + remaining, offset); |
| 1602 | nbytes += offset; |
| 1603 | } |
| 1604 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1605 | status = atmci_readl(host, ATMCI_SR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1606 | if (status & ATMCI_DATA_ERROR_FLAGS) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1607 | atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1608 | | ATMCI_DATA_ERROR_FLAGS)); |
| 1609 | host->data_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1610 | data->bytes_xfered += nbytes; |
| 1611 | smp_wmb(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1612 | atmci_set_pending(host, EVENT_DATA_ERROR); |
| 1613 | tasklet_schedule(&host->tasklet); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1614 | return; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1615 | } |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1616 | } while (status & ATMCI_RXRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1617 | |
| 1618 | host->pio_offset = offset; |
| 1619 | data->bytes_xfered += nbytes; |
| 1620 | |
| 1621 | return; |
| 1622 | |
| 1623 | done: |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1624 | atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY); |
| 1625 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1626 | data->bytes_xfered += nbytes; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1627 | smp_wmb(); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1628 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | static void atmci_write_data_pio(struct atmel_mci *host) |
| 1632 | { |
| 1633 | struct scatterlist *sg = host->sg; |
| 1634 | void *buf = sg_virt(sg); |
| 1635 | unsigned int offset = host->pio_offset; |
| 1636 | struct mmc_data *data = host->data; |
| 1637 | u32 value; |
| 1638 | u32 status; |
| 1639 | unsigned int nbytes = 0; |
| 1640 | |
| 1641 | do { |
| 1642 | if (likely(offset + 4 <= sg->length)) { |
| 1643 | value = get_unaligned((u32 *)(buf + offset)); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1644 | atmci_writel(host, ATMCI_TDR, value); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1645 | |
| 1646 | offset += 4; |
| 1647 | nbytes += 4; |
| 1648 | if (offset == sg->length) { |
| 1649 | host->sg = sg = sg_next(sg); |
| 1650 | if (!sg) |
| 1651 | goto done; |
| 1652 | |
| 1653 | offset = 0; |
| 1654 | buf = sg_virt(sg); |
| 1655 | } |
| 1656 | } else { |
| 1657 | unsigned int remaining = sg->length - offset; |
| 1658 | |
| 1659 | value = 0; |
| 1660 | memcpy(&value, buf + offset, remaining); |
| 1661 | nbytes += remaining; |
| 1662 | |
| 1663 | host->sg = sg = sg_next(sg); |
| 1664 | if (!sg) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1665 | atmci_writel(host, ATMCI_TDR, value); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1666 | goto done; |
| 1667 | } |
| 1668 | |
| 1669 | offset = 4 - remaining; |
| 1670 | buf = sg_virt(sg); |
| 1671 | memcpy((u8 *)&value + remaining, buf, offset); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1672 | atmci_writel(host, ATMCI_TDR, value); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1673 | nbytes += offset; |
| 1674 | } |
| 1675 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1676 | status = atmci_readl(host, ATMCI_SR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1677 | if (status & ATMCI_DATA_ERROR_FLAGS) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1678 | atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1679 | | ATMCI_DATA_ERROR_FLAGS)); |
| 1680 | host->data_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1681 | data->bytes_xfered += nbytes; |
| 1682 | smp_wmb(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1683 | atmci_set_pending(host, EVENT_DATA_ERROR); |
| 1684 | tasklet_schedule(&host->tasklet); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1685 | return; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1686 | } |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1687 | } while (status & ATMCI_TXRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1688 | |
| 1689 | host->pio_offset = offset; |
| 1690 | data->bytes_xfered += nbytes; |
| 1691 | |
| 1692 | return; |
| 1693 | |
| 1694 | done: |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1695 | atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY); |
| 1696 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1697 | data->bytes_xfered += nbytes; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1698 | smp_wmb(); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1699 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1702 | static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1703 | { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1704 | atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1705 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1706 | host->cmd_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1707 | smp_wmb(); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1708 | atmci_set_pending(host, EVENT_CMD_COMPLETE); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1709 | tasklet_schedule(&host->tasklet); |
| 1710 | } |
| 1711 | |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1712 | static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status) |
| 1713 | { |
| 1714 | int i; |
| 1715 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1716 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1717 | struct atmel_mci_slot *slot = host->slot[i]; |
| 1718 | if (slot && (status & slot->sdio_irq)) { |
| 1719 | mmc_signal_sdio_irq(slot->mmc); |
| 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1725 | static irqreturn_t atmci_interrupt(int irq, void *dev_id) |
| 1726 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1727 | struct atmel_mci *host = dev_id; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1728 | u32 status, mask, pending; |
| 1729 | unsigned int pass_count = 0; |
| 1730 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1731 | do { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1732 | status = atmci_readl(host, ATMCI_SR); |
| 1733 | mask = atmci_readl(host, ATMCI_IMR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1734 | pending = status & mask; |
| 1735 | if (!pending) |
| 1736 | break; |
| 1737 | |
| 1738 | if (pending & ATMCI_DATA_ERROR_FLAGS) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1739 | atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1740 | | ATMCI_RXRDY | ATMCI_TXRDY); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1741 | pending &= atmci_readl(host, ATMCI_IMR); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1742 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1743 | host->data_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1744 | smp_wmb(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1745 | atmci_set_pending(host, EVENT_DATA_ERROR); |
| 1746 | tasklet_schedule(&host->tasklet); |
| 1747 | } |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1748 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1749 | if (pending & ATMCI_TXBUFE) { |
| 1750 | atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE); |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 1751 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1752 | /* |
| 1753 | * We can receive this interruption before having configured |
| 1754 | * the second pdc buffer, so we need to reconfigure first and |
| 1755 | * second buffers again |
| 1756 | */ |
| 1757 | if (host->data_size) { |
| 1758 | atmci_pdc_set_both_buf(host, XFER_TRANSMIT); |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 1759 | atmci_writel(host, ATMCI_IER, ATMCI_ENDTX); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1760 | atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE); |
| 1761 | } else { |
| 1762 | atmci_pdc_complete(host); |
| 1763 | } |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 1764 | } else if (pending & ATMCI_ENDTX) { |
| 1765 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX); |
| 1766 | |
| 1767 | if (host->data_size) { |
| 1768 | atmci_pdc_set_single_buf(host, |
| 1769 | XFER_TRANSMIT, PDC_SECOND_BUF); |
| 1770 | atmci_writel(host, ATMCI_IER, ATMCI_ENDTX); |
| 1771 | } |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 1774 | if (pending & ATMCI_RXBUFF) { |
| 1775 | atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF); |
| 1776 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX); |
| 1777 | /* |
| 1778 | * We can receive this interruption before having configured |
| 1779 | * the second pdc buffer, so we need to reconfigure first and |
| 1780 | * second buffers again |
| 1781 | */ |
| 1782 | if (host->data_size) { |
| 1783 | atmci_pdc_set_both_buf(host, XFER_RECEIVE); |
| 1784 | atmci_writel(host, ATMCI_IER, ATMCI_ENDRX); |
| 1785 | atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF); |
| 1786 | } else { |
| 1787 | atmci_pdc_complete(host); |
| 1788 | } |
| 1789 | } else if (pending & ATMCI_ENDRX) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1790 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX); |
| 1791 | |
| 1792 | if (host->data_size) { |
| 1793 | atmci_pdc_set_single_buf(host, |
| 1794 | XFER_RECEIVE, PDC_SECOND_BUF); |
| 1795 | atmci_writel(host, ATMCI_IER, ATMCI_ENDRX); |
| 1796 | } |
| 1797 | } |
| 1798 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1799 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1800 | if (pending & ATMCI_NOTBUSY) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1801 | atmci_writel(host, ATMCI_IDR, |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1802 | ATMCI_DATA_ERROR_FLAGS | ATMCI_NOTBUSY); |
Haavard Skinnemoen | ca55f46 | 2008-10-05 15:16:59 +0200 | [diff] [blame] | 1803 | if (!host->data_status) |
| 1804 | host->data_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1805 | smp_wmb(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1806 | atmci_set_pending(host, EVENT_DATA_COMPLETE); |
| 1807 | tasklet_schedule(&host->tasklet); |
| 1808 | } |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1809 | if (pending & ATMCI_RXRDY) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1810 | atmci_read_data_pio(host); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1811 | if (pending & ATMCI_TXRDY) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1812 | atmci_write_data_pio(host); |
| 1813 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1814 | if (pending & ATMCI_CMDRDY) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1815 | atmci_cmd_interrupt(host, status); |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1816 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1817 | if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB)) |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1818 | atmci_sdio_interrupt(host, status); |
| 1819 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1820 | } while (pass_count++ < 5); |
| 1821 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1822 | return pass_count ? IRQ_HANDLED : IRQ_NONE; |
| 1823 | } |
| 1824 | |
| 1825 | static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id) |
| 1826 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1827 | struct atmel_mci_slot *slot = dev_id; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1828 | |
| 1829 | /* |
| 1830 | * Disable interrupts until the pin has stabilized and check |
| 1831 | * the state then. Use mod_timer() since we may be in the |
| 1832 | * middle of the timer routine when this interrupt triggers. |
| 1833 | */ |
| 1834 | disable_irq_nosync(irq); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1835 | mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1836 | |
| 1837 | return IRQ_HANDLED; |
| 1838 | } |
| 1839 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1840 | static int __init atmci_init_slot(struct atmel_mci *host, |
| 1841 | struct mci_slot_pdata *slot_data, unsigned int id, |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1842 | u32 sdc_reg, u32 sdio_irq) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1843 | { |
| 1844 | struct mmc_host *mmc; |
| 1845 | struct atmel_mci_slot *slot; |
| 1846 | |
| 1847 | mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev); |
| 1848 | if (!mmc) |
| 1849 | return -ENOMEM; |
| 1850 | |
| 1851 | slot = mmc_priv(mmc); |
| 1852 | slot->mmc = mmc; |
| 1853 | slot->host = host; |
| 1854 | slot->detect_pin = slot_data->detect_pin; |
| 1855 | slot->wp_pin = slot_data->wp_pin; |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 1856 | slot->detect_is_active_high = slot_data->detect_is_active_high; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1857 | slot->sdc_reg = sdc_reg; |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1858 | slot->sdio_irq = sdio_irq; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1859 | |
| 1860 | mmc->ops = &atmci_ops; |
| 1861 | mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512); |
| 1862 | mmc->f_max = host->bus_hz / 2; |
| 1863 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1864 | if (sdio_irq) |
| 1865 | mmc->caps |= MMC_CAP_SDIO_IRQ; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1866 | if (host->caps.has_highspeed) |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1867 | mmc->caps |= MMC_CAP_SD_HIGHSPEED; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1868 | if (slot_data->bus_width >= 4) |
| 1869 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 1870 | |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 1871 | mmc->max_segs = 64; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1872 | mmc->max_req_size = 32768 * 512; |
| 1873 | mmc->max_blk_size = 32768; |
| 1874 | mmc->max_blk_count = 512; |
| 1875 | |
| 1876 | /* Assume card is present initially */ |
| 1877 | set_bit(ATMCI_CARD_PRESENT, &slot->flags); |
| 1878 | if (gpio_is_valid(slot->detect_pin)) { |
| 1879 | if (gpio_request(slot->detect_pin, "mmc_detect")) { |
| 1880 | dev_dbg(&mmc->class_dev, "no detect pin available\n"); |
| 1881 | slot->detect_pin = -EBUSY; |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 1882 | } else if (gpio_get_value(slot->detect_pin) ^ |
| 1883 | slot->detect_is_active_high) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1884 | clear_bit(ATMCI_CARD_PRESENT, &slot->flags); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | if (!gpio_is_valid(slot->detect_pin)) |
| 1889 | mmc->caps |= MMC_CAP_NEEDS_POLL; |
| 1890 | |
| 1891 | if (gpio_is_valid(slot->wp_pin)) { |
| 1892 | if (gpio_request(slot->wp_pin, "mmc_wp")) { |
| 1893 | dev_dbg(&mmc->class_dev, "no WP pin available\n"); |
| 1894 | slot->wp_pin = -EBUSY; |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | host->slot[id] = slot; |
| 1899 | mmc_add_host(mmc); |
| 1900 | |
| 1901 | if (gpio_is_valid(slot->detect_pin)) { |
| 1902 | int ret; |
| 1903 | |
| 1904 | setup_timer(&slot->detect_timer, atmci_detect_change, |
| 1905 | (unsigned long)slot); |
| 1906 | |
| 1907 | ret = request_irq(gpio_to_irq(slot->detect_pin), |
| 1908 | atmci_detect_interrupt, |
| 1909 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 1910 | "mmc-detect", slot); |
| 1911 | if (ret) { |
| 1912 | dev_dbg(&mmc->class_dev, |
| 1913 | "could not request IRQ %d for detect pin\n", |
| 1914 | gpio_to_irq(slot->detect_pin)); |
| 1915 | gpio_free(slot->detect_pin); |
| 1916 | slot->detect_pin = -EBUSY; |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | atmci_init_debugfs(slot); |
| 1921 | |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot, |
| 1926 | unsigned int id) |
| 1927 | { |
| 1928 | /* Debugfs stuff is cleaned up by mmc core */ |
| 1929 | |
| 1930 | set_bit(ATMCI_SHUTDOWN, &slot->flags); |
| 1931 | smp_wmb(); |
| 1932 | |
| 1933 | mmc_remove_host(slot->mmc); |
| 1934 | |
| 1935 | if (gpio_is_valid(slot->detect_pin)) { |
| 1936 | int pin = slot->detect_pin; |
| 1937 | |
| 1938 | free_irq(gpio_to_irq(pin), slot); |
| 1939 | del_timer_sync(&slot->detect_timer); |
| 1940 | gpio_free(pin); |
| 1941 | } |
| 1942 | if (gpio_is_valid(slot->wp_pin)) |
| 1943 | gpio_free(slot->wp_pin); |
| 1944 | |
| 1945 | slot->host->slot[id] = NULL; |
| 1946 | mmc_free_host(slot->mmc); |
| 1947 | } |
| 1948 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1949 | static bool atmci_filter(struct dma_chan *chan, void *slave) |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 1950 | { |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1951 | struct mci_dma_data *sl = slave; |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 1952 | |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1953 | if (sl && find_slave_dev(sl) == chan->device->dev) { |
| 1954 | chan->private = slave_data_ptr(sl); |
Dan Williams | 7dd6025 | 2009-01-06 11:38:19 -0700 | [diff] [blame] | 1955 | return true; |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1956 | } else { |
Dan Williams | 7dd6025 | 2009-01-06 11:38:19 -0700 | [diff] [blame] | 1957 | return false; |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1958 | } |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 1959 | } |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1960 | |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 1961 | static bool atmci_configure_dma(struct atmel_mci *host) |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1962 | { |
| 1963 | struct mci_platform_data *pdata; |
| 1964 | |
| 1965 | if (host == NULL) |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 1966 | return false; |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1967 | |
| 1968 | pdata = host->pdev->dev.platform_data; |
| 1969 | |
| 1970 | if (pdata && find_slave_dev(pdata->dma_slave)) { |
| 1971 | dma_cap_mask_t mask; |
| 1972 | |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1973 | /* Try to grab a DMA channel */ |
| 1974 | dma_cap_zero(mask); |
| 1975 | dma_cap_set(DMA_SLAVE, mask); |
| 1976 | host->dma.chan = |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1977 | dma_request_channel(mask, atmci_filter, pdata->dma_slave); |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1978 | } |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 1979 | if (!host->dma.chan) { |
| 1980 | dev_warn(&host->pdev->dev, "no DMA channel available\n"); |
| 1981 | return false; |
| 1982 | } else { |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1983 | dev_info(&host->pdev->dev, |
Ludovic Desroches | b81cfc4 | 2012-02-09 16:33:54 +0100 | [diff] [blame] | 1984 | "using %s for DMA transfers\n", |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1985 | dma_chan_name(host->dma.chan)); |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 1986 | |
| 1987 | host->dma_conf.src_addr = host->mapbase + ATMCI_RDR; |
| 1988 | host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1989 | host->dma_conf.src_maxburst = 1; |
| 1990 | host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR; |
| 1991 | host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1992 | host->dma_conf.dst_maxburst = 1; |
| 1993 | host->dma_conf.device_fc = false; |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 1994 | return true; |
| 1995 | } |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 1996 | } |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1997 | |
| 1998 | static inline unsigned int atmci_get_version(struct atmel_mci *host) |
| 1999 | { |
| 2000 | return atmci_readl(host, ATMCI_VERSION) & 0x00000fff; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * HSMCI (High Speed MCI) module is not fully compatible with MCI module. |
| 2005 | * HSMCI provides DMA support and a new config register but no more supports |
| 2006 | * PDC. |
| 2007 | */ |
| 2008 | static void __init atmci_get_cap(struct atmel_mci *host) |
| 2009 | { |
| 2010 | unsigned int version; |
| 2011 | |
| 2012 | version = atmci_get_version(host); |
| 2013 | dev_info(&host->pdev->dev, |
| 2014 | "version: 0x%x\n", version); |
| 2015 | |
| 2016 | host->caps.has_dma = 0; |
| 2017 | host->caps.has_pdc = 0; |
| 2018 | host->caps.has_cfg_reg = 0; |
| 2019 | host->caps.has_cstor_reg = 0; |
| 2020 | host->caps.has_highspeed = 0; |
| 2021 | host->caps.has_rwproof = 0; |
| 2022 | |
| 2023 | /* keep only major version number */ |
| 2024 | switch (version & 0xf00) { |
| 2025 | case 0x100: |
| 2026 | case 0x200: |
| 2027 | host->caps.has_pdc = 1; |
| 2028 | host->caps.has_rwproof = 1; |
| 2029 | break; |
| 2030 | case 0x300: |
| 2031 | case 0x400: |
| 2032 | case 0x500: |
| 2033 | #ifdef CONFIG_AT_HDMAC |
| 2034 | host->caps.has_dma = 1; |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 2035 | #else |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2036 | host->caps.has_dma = 0; |
| 2037 | dev_info(&host->pdev->dev, |
| 2038 | "has dma capability but dma engine is not selected, then use pio\n"); |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2039 | #endif |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2040 | host->caps.has_cfg_reg = 1; |
| 2041 | host->caps.has_cstor_reg = 1; |
| 2042 | host->caps.has_highspeed = 1; |
| 2043 | host->caps.has_rwproof = 1; |
| 2044 | break; |
| 2045 | default: |
| 2046 | dev_warn(&host->pdev->dev, |
| 2047 | "Unmanaged mci version, set minimum capabilities\n"); |
| 2048 | break; |
| 2049 | } |
| 2050 | } |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2051 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2052 | static int __init atmci_probe(struct platform_device *pdev) |
| 2053 | { |
| 2054 | struct mci_platform_data *pdata; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2055 | struct atmel_mci *host; |
| 2056 | struct resource *regs; |
| 2057 | unsigned int nr_slots; |
| 2058 | int irq; |
| 2059 | int ret; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2060 | |
| 2061 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2062 | if (!regs) |
| 2063 | return -ENXIO; |
| 2064 | pdata = pdev->dev.platform_data; |
| 2065 | if (!pdata) |
| 2066 | return -ENXIO; |
| 2067 | irq = platform_get_irq(pdev, 0); |
| 2068 | if (irq < 0) |
| 2069 | return irq; |
| 2070 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2071 | host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL); |
| 2072 | if (!host) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2073 | return -ENOMEM; |
| 2074 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2075 | host->pdev = pdev; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2076 | spin_lock_init(&host->lock); |
| 2077 | INIT_LIST_HEAD(&host->queue); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2078 | |
| 2079 | host->mck = clk_get(&pdev->dev, "mci_clk"); |
| 2080 | if (IS_ERR(host->mck)) { |
| 2081 | ret = PTR_ERR(host->mck); |
| 2082 | goto err_clk_get; |
| 2083 | } |
| 2084 | |
| 2085 | ret = -ENOMEM; |
H Hartley Sweeten | e8e3f6c | 2009-12-14 14:11:56 -0500 | [diff] [blame] | 2086 | host->regs = ioremap(regs->start, resource_size(regs)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2087 | if (!host->regs) |
| 2088 | goto err_ioremap; |
| 2089 | |
| 2090 | clk_enable(host->mck); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2091 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2092 | host->bus_hz = clk_get_rate(host->mck); |
| 2093 | clk_disable(host->mck); |
| 2094 | |
| 2095 | host->mapbase = regs->start; |
| 2096 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2097 | tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2098 | |
Kay Sievers | 89c8aa2 | 2009-02-02 21:08:30 +0100 | [diff] [blame] | 2099 | ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2100 | if (ret) |
| 2101 | goto err_request_irq; |
| 2102 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2103 | /* Get MCI capabilities and set operations according to it */ |
| 2104 | atmci_get_cap(host); |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 2105 | if (host->caps.has_dma && atmci_configure_dma(host)) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2106 | host->prepare_data = &atmci_prepare_data_dma; |
| 2107 | host->submit_data = &atmci_submit_data_dma; |
| 2108 | host->stop_transfer = &atmci_stop_transfer_dma; |
| 2109 | } else if (host->caps.has_pdc) { |
| 2110 | dev_info(&pdev->dev, "using PDC\n"); |
| 2111 | host->prepare_data = &atmci_prepare_data_pdc; |
| 2112 | host->submit_data = &atmci_submit_data_pdc; |
| 2113 | host->stop_transfer = &atmci_stop_transfer_pdc; |
| 2114 | } else { |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 2115 | dev_info(&pdev->dev, "using PIO\n"); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2116 | host->prepare_data = &atmci_prepare_data; |
| 2117 | host->submit_data = &atmci_submit_data; |
| 2118 | host->stop_transfer = &atmci_stop_transfer; |
| 2119 | } |
| 2120 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2121 | platform_set_drvdata(pdev, host); |
| 2122 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2123 | /* We need at least one slot to succeed */ |
| 2124 | nr_slots = 0; |
| 2125 | ret = -ENODEV; |
| 2126 | if (pdata->slot[0].bus_width) { |
| 2127 | ret = atmci_init_slot(host, &pdata->slot[0], |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2128 | 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2129 | if (!ret) |
| 2130 | nr_slots++; |
| 2131 | } |
| 2132 | if (pdata->slot[1].bus_width) { |
| 2133 | ret = atmci_init_slot(host, &pdata->slot[1], |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2134 | 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2135 | if (!ret) |
| 2136 | nr_slots++; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2137 | } |
| 2138 | |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 2139 | if (!nr_slots) { |
| 2140 | dev_err(&pdev->dev, "init failed: no slot defined\n"); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2141 | goto err_init_slot; |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 2142 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2143 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2144 | dev_info(&pdev->dev, |
| 2145 | "Atmel MCI controller at 0x%08lx irq %d, %u slots\n", |
| 2146 | host->mapbase, irq, nr_slots); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 2147 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2148 | return 0; |
| 2149 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2150 | err_init_slot: |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2151 | if (host->dma.chan) |
| 2152 | dma_release_channel(host->dma.chan); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2153 | free_irq(irq, host); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2154 | err_request_irq: |
| 2155 | iounmap(host->regs); |
| 2156 | err_ioremap: |
| 2157 | clk_put(host->mck); |
| 2158 | err_clk_get: |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2159 | kfree(host); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2160 | return ret; |
| 2161 | } |
| 2162 | |
| 2163 | static int __exit atmci_remove(struct platform_device *pdev) |
| 2164 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2165 | struct atmel_mci *host = platform_get_drvdata(pdev); |
| 2166 | unsigned int i; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2167 | |
| 2168 | platform_set_drvdata(pdev, NULL); |
| 2169 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2170 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2171 | if (host->slot[i]) |
| 2172 | atmci_cleanup_slot(host->slot[i], i); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2173 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2174 | |
| 2175 | clk_enable(host->mck); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2176 | atmci_writel(host, ATMCI_IDR, ~0UL); |
| 2177 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); |
| 2178 | atmci_readl(host, ATMCI_SR); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2179 | clk_disable(host->mck); |
| 2180 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 2181 | #ifdef CONFIG_MMC_ATMELMCI_DMA |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2182 | if (host->dma.chan) |
| 2183 | dma_release_channel(host->dma.chan); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 2184 | #endif |
| 2185 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2186 | free_irq(platform_get_irq(pdev, 0), host); |
| 2187 | iounmap(host->regs); |
| 2188 | |
| 2189 | clk_put(host->mck); |
| 2190 | kfree(host); |
| 2191 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2192 | return 0; |
| 2193 | } |
| 2194 | |
Nicolas Ferre | 5c2f2b9 | 2011-07-06 11:31:36 +0200 | [diff] [blame] | 2195 | #ifdef CONFIG_PM |
| 2196 | static int atmci_suspend(struct device *dev) |
| 2197 | { |
| 2198 | struct atmel_mci *host = dev_get_drvdata(dev); |
| 2199 | int i; |
| 2200 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2201 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Nicolas Ferre | 5c2f2b9 | 2011-07-06 11:31:36 +0200 | [diff] [blame] | 2202 | struct atmel_mci_slot *slot = host->slot[i]; |
| 2203 | int ret; |
| 2204 | |
| 2205 | if (!slot) |
| 2206 | continue; |
| 2207 | ret = mmc_suspend_host(slot->mmc); |
| 2208 | if (ret < 0) { |
| 2209 | while (--i >= 0) { |
| 2210 | slot = host->slot[i]; |
| 2211 | if (slot |
| 2212 | && test_bit(ATMCI_SUSPENDED, &slot->flags)) { |
| 2213 | mmc_resume_host(host->slot[i]->mmc); |
| 2214 | clear_bit(ATMCI_SUSPENDED, &slot->flags); |
| 2215 | } |
| 2216 | } |
| 2217 | return ret; |
| 2218 | } else { |
| 2219 | set_bit(ATMCI_SUSPENDED, &slot->flags); |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | return 0; |
| 2224 | } |
| 2225 | |
| 2226 | static int atmci_resume(struct device *dev) |
| 2227 | { |
| 2228 | struct atmel_mci *host = dev_get_drvdata(dev); |
| 2229 | int i; |
| 2230 | int ret = 0; |
| 2231 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2232 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Nicolas Ferre | 5c2f2b9 | 2011-07-06 11:31:36 +0200 | [diff] [blame] | 2233 | struct atmel_mci_slot *slot = host->slot[i]; |
| 2234 | int err; |
| 2235 | |
| 2236 | slot = host->slot[i]; |
| 2237 | if (!slot) |
| 2238 | continue; |
| 2239 | if (!test_bit(ATMCI_SUSPENDED, &slot->flags)) |
| 2240 | continue; |
| 2241 | err = mmc_resume_host(slot->mmc); |
| 2242 | if (err < 0) |
| 2243 | ret = err; |
| 2244 | else |
| 2245 | clear_bit(ATMCI_SUSPENDED, &slot->flags); |
| 2246 | } |
| 2247 | |
| 2248 | return ret; |
| 2249 | } |
| 2250 | static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume); |
| 2251 | #define ATMCI_PM_OPS (&atmci_pm) |
| 2252 | #else |
| 2253 | #define ATMCI_PM_OPS NULL |
| 2254 | #endif |
| 2255 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2256 | static struct platform_driver atmci_driver = { |
| 2257 | .remove = __exit_p(atmci_remove), |
| 2258 | .driver = { |
| 2259 | .name = "atmel_mci", |
Nicolas Ferre | 5c2f2b9 | 2011-07-06 11:31:36 +0200 | [diff] [blame] | 2260 | .pm = ATMCI_PM_OPS, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2261 | }, |
| 2262 | }; |
| 2263 | |
| 2264 | static int __init atmci_init(void) |
| 2265 | { |
| 2266 | return platform_driver_probe(&atmci_driver, atmci_probe); |
| 2267 | } |
| 2268 | |
| 2269 | static void __exit atmci_exit(void) |
| 2270 | { |
| 2271 | platform_driver_unregister(&atmci_driver); |
| 2272 | } |
| 2273 | |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2274 | late_initcall(atmci_init); /* try to load after dma driver when built-in */ |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2275 | module_exit(atmci_exit); |
| 2276 | |
| 2277 | MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 2278 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2279 | MODULE_LICENSE("GPL v2"); |