mmc: add support for rk3036
[firefly-linux-kernel-4.4.55.git] / include / linux / mmc / rk_mmc.h
1 /*
2  * Synopsys DesignWare Multimedia Card Interface driver
3  *  (Based on NXP driver for lpc 31xx)
4  *
5  * Copyright (C) 2009 NXP Semiconductors
6  * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #ifndef LINUX_MMC_DW_MMC_H
15 #define LINUX_MMC_DW_MMC_H
16
17 #include <linux/scatterlist.h>
18 #include <linux/mmc/core.h>
19 #include <linux/dmaengine.h>
20 #include <linux/timer.h>
21
22 #define MAX_MCI_SLOTS   2
23
24 enum dw_mci_state {
25         STATE_IDLE = 0,
26         STATE_SENDING_CMD,
27         STATE_SENDING_DATA,
28         STATE_DATA_BUSY,
29         STATE_SENDING_STOP,
30         STATE_DATA_ERROR,
31 };
32
33 enum {
34         EVENT_CMD_COMPLETE = 0,
35         EVENT_XFER_COMPLETE,
36         EVENT_DATA_COMPLETE,
37         EVENT_DATA_ERROR,
38         EVENT_XFER_ERROR
39 };
40 struct dw_mci_dma_slave {
41         struct dma_chan *ch;
42         enum dma_transfer_direction direction;
43         unsigned int dmach;
44 };
45
46 struct mmc_data;
47
48 /**
49  * struct dw_mci - MMC controller state shared between all slots
50  * @lock: Spinlock protecting the queue and associated data.
51  * @regs: Pointer to MMIO registers.
52  * @sg: Scatterlist entry currently being processed by PIO code, if any.
53  * @sg_miter: PIO mapping scatterlist iterator.
54  * @cur_slot: The slot which is currently using the controller.
55  * @mrq: The request currently being processed on @cur_slot,
56  *      or NULL if the controller is idle.
57  * @cmd: The command currently being sent to the card, or NULL.
58  * @data: The data currently being transferred, or NULL if no data
59  *      transfer is in progress.
60  * @use_dma: Whether DMA channel is initialized or not.
61  * @using_dma: Whether DMA is in use for the current transfer.
62  * @sg_dma: Bus address of DMA buffer.
63  * @sg_cpu: Virtual address of DMA buffer.
64  * @dma_ops: Pointer to platform-specific DMA callbacks.
65  * @cmd_status: Snapshot of SR taken upon completion of the current
66  *      command. Only valid when EVENT_CMD_COMPLETE is pending.
67  * @data_status: Snapshot of SR taken upon completion of the current
68  *      data transfer. Only valid when EVENT_DATA_COMPLETE or
69  *      EVENT_DATA_ERROR is pending.
70  * @stop_cmdr: Value to be loaded into CMDR when the stop command is
71  *      to be sent.
72  * @dir_status: Direction of current transfer.
73  * @tasklet: Tasklet running the request state machine.
74  * @card_tasklet: Tasklet handling card detect.
75  * @pending_events: Bitmask of events flagged by the interrupt handler
76  *      to be processed by the tasklet.
77  * @completed_events: Bitmask of events which the state machine has
78  *      processed.
79  * @state: Tasklet state.
80  * @queue: List of slots waiting for access to the controller.
81  * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
82  *      rate and timeout calculations.
83  * @current_speed: Configured rate of the controller.
84  * @num_slots: Number of slots available.
85  * @verid: Denote Version ID.
86  * @data_offset: Set the offset of DATA register according to VERID.
87  * @dev: Device associated with the MMC controller.
88  * @pdata: Platform data associated with the MMC controller.
89  * @drv_data: Driver specific data for identified variant of the controller
90  * @priv: Implementation defined private data.
91  * @biu_clk: Pointer to bus interface unit clock instance.
92  * @ciu_clk: Pointer to card interface unit clock instance.
93  * @slot: Slots sharing this MMC controller.
94  * @fifo_depth: depth of FIFO.
95  * @data_shift: log2 of FIFO item size.
96  * @part_buf_start: Start index in part_buf.
97  * @part_buf_count: Bytes of partial data in part_buf.
98  * @part_buf: Simple buffer for partial fifo reads/writes.
99  * @push_data: Pointer to FIFO push function.
100  * @pull_data: Pointer to FIFO pull function.
101  * @quirks: Set of quirks that apply to specific versions of the IP.
102  * @irq_flags: The flags to be passed to request_irq.
103  * @irq: The irq value to be passed to request_irq.
104  *
105  * Locking
106  * =======
107  *
108  * @lock is a softirq-safe spinlock protecting @queue as well as
109  * @cur_slot, @mrq and @state. These must always be updated
110  * at the same time while holding @lock.
111  *
112  * The @mrq field of struct dw_mci_slot is also protected by @lock,
113  * and must always be written at the same time as the slot is added to
114  * @queue.
115  *
116  * @pending_events and @completed_events are accessed using atomic bit
117  * operations, so they don't need any locking.
118  *
119  * None of the fields touched by the interrupt handler need any
120  * locking. However, ordering is important: Before EVENT_DATA_ERROR or
121  * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
122  * interrupts must be disabled and @data_status updated with a
123  * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
124  * CMDRDY interrupt must be disabled and @cmd_status updated with a
125  * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
126  * bytes_xfered field of @data must be written. This is ensured by
127  * using barriers.
128  */
129 struct dw_mci {
130         spinlock_t              lock;
131         void __iomem            *regs;
132
133         struct scatterlist      *sg;
134         struct sg_mapping_iter  sg_miter;
135
136         struct dw_mci_slot      *cur_slot;
137         struct mmc_request      *mrq;
138         struct mmc_command      *cmd;
139         struct mmc_data         *data;
140         struct mmc_command      stop_abort;
141         unsigned int            prev_blksz;
142         unsigned char           timing;
143         struct workqueue_struct *card_workqueue;
144
145         /* DMA interface members*/
146         int                     use_dma;
147         int                     using_dma;
148
149         dma_addr_t              sg_dma;
150         void                    *sg_cpu;
151         const struct dw_mci_dma_ops     *dma_ops;
152 #ifdef CONFIG_MMC_DW_IDMAC
153         unsigned int            ring_size;
154 #else
155         struct dw_mci_dma_data  *dma_data;
156 #endif
157
158 #ifdef CONFIG_MMC_DW_EDMAC
159         struct dw_mci_dma_slave *dms;
160         void                    *phy_regs;
161 #endif
162         u32                     cmd_status;
163         u32                     data_status;
164         u32                     stop_cmdr;
165         u32                     dir_status;
166         struct tasklet_struct   tasklet;
167         struct work_struct      card_work;
168         unsigned long           pending_events;
169         unsigned long           completed_events;
170         enum dw_mci_state       state;
171         struct list_head        queue;
172
173         u32                     bus_hz;
174         u32                     current_speed;
175         u32         set_speed;
176         u32         set_div;
177         u32                     num_slots;
178         u32                     fifoth_val;
179         u16                     verid;
180         u16                     data_offset;
181         struct device           *dev;
182         struct dw_mci_board     *pdata;
183         const struct dw_mci_drv_data    *drv_data;
184         void                    *priv;
185         struct clk      *hclk_mmc;
186         struct clk      *clk_mmc;
187         struct dw_mci_slot      *slot[MAX_MCI_SLOTS];
188         struct mmc_host         *mmc;
189         struct mmc_command      *pre_cmd;
190         /* Fix the hold_reg value */
191         unsigned int    hold_reg_flag;
192         /* Timer for INT_DTO */
193         struct timer_list       dto_timer;
194         /* FIFO push and pull */
195         int                     fifo_depth;
196         int                     data_shift;
197         u8                      part_buf_start;
198         u8                      part_buf_count;
199         union {
200                 u16             part_buf16;
201                 u32             part_buf32;
202                 u64             part_buf;
203         };
204         void (*push_data)(struct dw_mci *host, void *buf, int cnt);
205         void (*pull_data)(struct dw_mci *host, void *buf, int cnt);
206
207         /* Workaround flags */
208         u32                     quirks;
209         bool        irq_state;
210         u32                     svi_flags; /*switch voltage interrupt flags*/
211         struct regulator        *vmmc;  /* Power regulator */
212         unsigned long           irq_flags; /* IRQ flags */
213         int                     irq;
214         u32         cmd_rto;     /*cmd response timeout hold times*/
215         struct pinctrl *pinctrl; /*Pinctrl state*/
216         struct pinctrl_state    *pins_default;
217         struct pinctrl_state    *pins_idle;
218         struct pinctrl_state    *pins_sleep;
219 };
220
221 /* DMA ops for Internal/External DMAC interface */
222 struct dw_mci_dma_ops {
223         /* DMA Ops */
224         int (*init)(struct dw_mci *host);
225         void (*start)(struct dw_mci *host, unsigned int sg_len);
226         void (*complete)(void *host);
227         void (*stop)(struct dw_mci *host);
228         void (*cleanup)(struct dw_mci *host);
229         void (*exit)(struct dw_mci *host);
230 };
231
232 /* IP Quirks/flags. */
233 /* DTO fix for command transmission with IDMAC configured */
234 #define DW_MCI_QUIRK_IDMAC_DTO                  BIT(0)
235 /* delay needed between retries on some 2.11a implementations */
236 #define DW_MCI_QUIRK_RETRY_DELAY                BIT(1)
237 /* High Speed Capable - Supports HS cards (up to 50MHz) */
238 #define DW_MCI_QUIRK_HIGHSPEED                  BIT(2)
239 /* Unreliable card detection */
240 #define DW_MCI_QUIRK_BROKEN_CARD_DETECTION      BIT(3)
241
242 /* Slot level quirks */
243 /* This slot has no write protect */
244 #define DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT      BIT(0)
245
246 struct dma_pdata;
247
248 struct block_settings {
249         unsigned short  max_segs;       /* see blk_queue_max_segments */
250         unsigned int    max_blk_size;   /* maximum size of one mmc block */
251         unsigned int    max_blk_count;  /* maximum number of blocks in one req*/
252         unsigned int    max_req_size;   /* maximum number of bytes in one req*/
253         unsigned int    max_seg_size;   /* see blk_queue_max_segment_size */
254 };
255
256 /* Board platform data */
257 struct dw_mci_board {
258         u32 num_slots;
259
260         u32 quirks; /* Workaround / Quirk flags */
261         unsigned int bus_hz; /* Clock speed at the cclk_in pad */
262
263         u32 caps;       /* Capabilities */
264         u32 caps2;      /* More capabilities */
265         u32 pm_caps;    /* PM capabilities */
266         u32 cardtype_restrict;  /*restrict the SDMMC controller to support card type;1--SD card; 2--sdio; 4--eMMC */
267         /*
268          * Override fifo depth. If 0, autodetect it from the FIFOTH register,
269          * but note that this may not be reliable after a bootloader has used
270          * it.
271          */
272         unsigned int fifo_depth;
273
274         /* delay in mS before detecting cards after interrupt */
275         u32 detect_delay_ms;
276
277         int (*init)(u32 slot_id, irq_handler_t , void *);
278         int (*get_ro)(u32 slot_id);
279         int (*get_cd)(u32 slot_id);
280         int (*get_ocr)(u32 slot_id);
281         int (*get_bus_wd)(u32 slot_id);
282         /*
283          * Enable power to selected slot and set voltage to desired level.
284          * Voltage levels are specified using MMC_VDD_xxx defines defined
285          * in linux/mmc/host.h file.
286          */
287         void (*setpower)(u32 slot_id, u32 volt);
288         void (*exit)(u32 slot_id);
289         void (*select_slot)(u32 slot_id);
290
291         struct dw_mci_dma_ops *dma_ops;
292         struct dma_pdata *data;
293         struct block_settings *blk_settings;
294 };
295
296 #endif /* LINUX_MMC_DW_MMC_H */