2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
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.
17 #include <linux/scatterlist.h>
18 #include <linux/mmc/core.h>
19 #include <linux/dmaengine.h>
20 #include <linux/reset.h>
21 #include <linux/interrupt.h>
31 STATE_WAITING_CMD11_DONE,
35 EVENT_CMD_COMPLETE = 0,
43 COOKIE_PRE_MAPPED, /* mapped by pre_req() of dwmmc */
44 COOKIE_MAPPED, /* mapped by prepare_data() of dwmmc */
55 struct dw_mci_dma_slave {
57 enum dma_transfer_direction direction;
61 * struct dw_mci - MMC controller state shared between all slots
62 * @lock: Spinlock protecting the queue and associated data.
63 * @irq_lock: Spinlock protecting the INTMASK setting.
64 * @regs: Pointer to MMIO registers.
65 * @fifo_reg: Pointer to MMIO registers for data FIFO
66 * @sg: Scatterlist entry currently being processed by PIO code, if any.
67 * @sg_miter: PIO mapping scatterlist iterator.
68 * @cur_slot: The slot which is currently using the controller.
69 * @mrq: The request currently being processed on @cur_slot,
70 * or NULL if the controller is idle.
71 * @cmd: The command currently being sent to the card, or NULL.
72 * @data: The data currently being transferred, or NULL if no data
73 * transfer is in progress.
74 * @stop_abort: The command currently prepared for stoping transfer.
75 * @prev_blksz: The former transfer blksz record.
76 * @timing: Record of current ios timing.
77 * @use_dma: Whether DMA channel is initialized or not.
78 * @using_dma: Whether DMA is in use for the current transfer.
79 * @dma_64bit_address: Whether DMA supports 64-bit address mode or not.
80 * @sg_dma: Bus address of DMA buffer.
81 * @sg_cpu: Virtual address of DMA buffer.
82 * @dma_ops: Pointer to platform-specific DMA callbacks.
83 * @cmd_status: Snapshot of SR taken upon completion of the current
84 * @ring_size: Buffer size for idma descriptors.
85 * command. Only valid when EVENT_CMD_COMPLETE is pending.
86 * @dms: structure of slave-dma private data.
87 * @phy_regs: physical address of controller's register map
88 * @data_status: Snapshot of SR taken upon completion of the current
89 * data transfer. Only valid when EVENT_DATA_COMPLETE or
90 * EVENT_DATA_ERROR is pending.
91 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
93 * @dir_status: Direction of current transfer.
94 * @tasklet: Tasklet running the request state machine.
95 * @pending_events: Bitmask of events flagged by the interrupt handler
96 * to be processed by the tasklet.
97 * @completed_events: Bitmask of events which the state machine has
99 * @state: Tasklet state.
100 * @queue: List of slots waiting for access to the controller.
101 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
102 * rate and timeout calculations.
103 * @current_speed: Configured rate of the controller.
104 * @num_slots: Number of slots available.
105 * @fifoth_val: The value of FIFOTH register.
106 * @verid: Denote Version ID.
107 * @dev: Device associated with the MMC controller.
108 * @pdata: Platform data associated with the MMC controller.
109 * @drv_data: Driver specific data for identified variant of the controller
110 * @priv: Implementation defined private data.
111 * @biu_clk: Pointer to bus interface unit clock instance.
112 * @ciu_clk: Pointer to card interface unit clock instance.
113 * @slot: Slots sharing this MMC controller.
114 * @fifo_depth: depth of FIFO.
115 * @data_addr_override: override fifo reg offset with this value.
116 * @wm_aligned: force fifo watermark equal with data length in PIO mode.
117 * Set as true if alignment is needed.
118 * @data_shift: log2 of FIFO item size.
119 * @part_buf_start: Start index in part_buf.
120 * @part_buf_count: Bytes of partial data in part_buf.
121 * @part_buf: Simple buffer for partial fifo reads/writes.
122 * @push_data: Pointer to FIFO push function.
123 * @pull_data: Pointer to FIFO pull function.
124 * @vqmmc_enabled: Status of vqmmc, should be true or false.
125 * @irq_flags: The flags to be passed to request_irq.
126 * @irq: The irq value to be passed to request_irq.
127 * @sdio_id0: Number of slot0 in the SDIO interrupt registers.
128 * @cmd11_timer: Timer for SD3.0 voltage switch over scheme.
129 * @dto_timer: Timer for broken data transfer over scheme.
134 * @lock is a softirq-safe spinlock protecting @queue as well as
135 * at the same time while holding @lock.
137 * @irq_lock is an irq-safe spinlock protecting the INTMASK register
138 * to allow the interrupt handler to modify it directly. Held for only long
139 * enough to read-modify-write INTMASK and no other locks are grabbed when
142 * The @mrq field of struct dw_mci_slot is also protected by @lock,
143 * and must always be written at the same time as the slot is added to
146 * @pending_events and @completed_events are accessed using atomic bit
147 * operations, so they don't need any locking.
149 * None of the fields touched by the interrupt handler need any
150 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
151 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
152 * interrupts must be disabled and @data_status updated with a
153 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
154 * CMDRDY interrupt must be disabled and @cmd_status updated with a
155 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
156 * bytes_xfered field of @data must be written. This is ensured by
163 void __iomem *fifo_reg;
164 u32 data_addr_override;
167 struct scatterlist *sg;
168 struct sg_mapping_iter sg_miter;
170 struct mmc_request *mrq;
171 struct mmc_command *cmd;
172 struct mmc_data *data;
173 struct mmc_command stop_abort;
174 unsigned int prev_blksz;
175 unsigned char timing;
177 /* DMA interface members*/
180 int dma_64bit_address;
184 const struct dw_mci_dma_ops *dma_ops;
186 unsigned int ring_size;
189 struct dw_mci_dma_slave *dms;
190 /* Registers's physical base address */
191 resource_size_t phy_regs;
197 struct tasklet_struct tasklet;
198 unsigned long pending_events;
199 unsigned long completed_events;
200 enum dw_mci_state state;
201 struct list_head queue;
208 struct dw_mci_board *pdata;
209 const struct dw_mci_drv_data *drv_data;
213 struct dw_mci_slot *slot;
215 /* FIFO push and pull */
225 void (*push_data)(struct dw_mci *host, void *buf, int cnt);
226 void (*pull_data)(struct dw_mci *host, void *buf, int cnt);
229 unsigned long irq_flags; /* IRQ flags */
234 struct timer_list cmd11_timer;
235 struct timer_list dto_timer;
238 /* DMA ops for Internal/External DMAC interface */
239 struct dw_mci_dma_ops {
241 int (*init)(struct dw_mci *host);
242 int (*start)(struct dw_mci *host, unsigned int sg_len);
243 void (*complete)(void *host);
244 void (*stop)(struct dw_mci *host);
245 void (*cleanup)(struct dw_mci *host);
246 void (*exit)(struct dw_mci *host);
251 /* Board platform data */
252 struct dw_mci_board {
255 unsigned int bus_hz; /* Clock speed at the cclk_in pad */
257 u32 caps; /* Capabilities */
258 u32 caps2; /* More capabilities */
259 u32 pm_caps; /* PM capabilities */
261 * Override fifo depth. If 0, autodetect it from the FIFOTH register,
262 * but note that this may not be reliable after a bootloader has used
265 unsigned int fifo_depth;
267 /* delay in mS before detecting cards after interrupt */
270 struct reset_control *rstc;
271 struct dw_mci_dma_ops *dma_ops;
272 struct dma_pdata *data;
275 #define DW_MMC_240A 0x240a
276 #define DW_MMC_280A 0x280a
278 #define SDMMC_CTRL 0x000
279 #define SDMMC_PWREN 0x004
280 #define SDMMC_CLKDIV 0x008
281 #define SDMMC_CLKSRC 0x00c
282 #define SDMMC_CLKENA 0x010
283 #define SDMMC_TMOUT 0x014
284 #define SDMMC_CTYPE 0x018
285 #define SDMMC_BLKSIZ 0x01c
286 #define SDMMC_BYTCNT 0x020
287 #define SDMMC_INTMASK 0x024
288 #define SDMMC_CMDARG 0x028
289 #define SDMMC_CMD 0x02c
290 #define SDMMC_RESP0 0x030
291 #define SDMMC_RESP1 0x034
292 #define SDMMC_RESP2 0x038
293 #define SDMMC_RESP3 0x03c
294 #define SDMMC_MINTSTS 0x040
295 #define SDMMC_RINTSTS 0x044
296 #define SDMMC_STATUS 0x048
297 #define SDMMC_FIFOTH 0x04c
298 #define SDMMC_CDETECT 0x050
299 #define SDMMC_WRTPRT 0x054
300 #define SDMMC_GPIO 0x058
301 #define SDMMC_TCBCNT 0x05c
302 #define SDMMC_TBBCNT 0x060
303 #define SDMMC_DEBNCE 0x064
304 #define SDMMC_USRID 0x068
305 #define SDMMC_VERID 0x06c
306 #define SDMMC_HCON 0x070
307 #define SDMMC_UHS_REG 0x074
308 #define SDMMC_RST_N 0x078
309 #define SDMMC_BMOD 0x080
310 #define SDMMC_PLDMND 0x084
311 #define SDMMC_DBADDR 0x088
312 #define SDMMC_IDSTS 0x08c
313 #define SDMMC_IDINTEN 0x090
314 #define SDMMC_DSCADDR 0x094
315 #define SDMMC_BUFADDR 0x098
316 #define SDMMC_CDTHRCTL 0x100
317 #define SDMMC_DATA(x) (x)
319 * Registers to support idmac 64-bit address mode
321 #define SDMMC_DBADDRL 0x088
322 #define SDMMC_DBADDRU 0x08c
323 #define SDMMC_IDSTS64 0x090
324 #define SDMMC_IDINTEN64 0x094
325 #define SDMMC_DSCADDRL 0x098
326 #define SDMMC_DSCADDRU 0x09c
327 #define SDMMC_BUFADDRL 0x0A0
328 #define SDMMC_BUFADDRU 0x0A4
331 * Data offset is difference according to Version
332 * Lower than 2.40a : data register offest is 0x100
334 #define DATA_OFFSET 0x100
335 #define DATA_240A_OFFSET 0x200
337 /* shift bit field */
338 #define _SBF(f, v) ((v) << (f))
340 /* Control register defines */
341 #define SDMMC_CTRL_USE_IDMAC BIT(25)
342 #define SDMMC_CTRL_CEATA_INT_EN BIT(11)
343 #define SDMMC_CTRL_SEND_AS_CCSD BIT(10)
344 #define SDMMC_CTRL_SEND_CCSD BIT(9)
345 #define SDMMC_CTRL_ABRT_READ_DATA BIT(8)
346 #define SDMMC_CTRL_SEND_IRQ_RESP BIT(7)
347 #define SDMMC_CTRL_READ_WAIT BIT(6)
348 #define SDMMC_CTRL_DMA_ENABLE BIT(5)
349 #define SDMMC_CTRL_INT_ENABLE BIT(4)
350 #define SDMMC_CTRL_DMA_RESET BIT(2)
351 #define SDMMC_CTRL_FIFO_RESET BIT(1)
352 #define SDMMC_CTRL_RESET BIT(0)
353 /* Clock Enable register defines */
354 #define SDMMC_CLKEN_LOW_PWR BIT(16)
355 #define SDMMC_CLKEN_ENABLE BIT(0)
356 /* time-out register defines */
357 #define SDMMC_TMOUT_DATA(n) _SBF(8, (n))
358 #define SDMMC_TMOUT_DATA_MSK 0xFFFFFF00
359 #define SDMMC_TMOUT_RESP(n) ((n) & 0xFF)
360 #define SDMMC_TMOUT_RESP_MSK 0xFF
361 /* card-type register defines */
362 #define SDMMC_CTYPE_8BIT BIT(16)
363 #define SDMMC_CTYPE_4BIT BIT(0)
364 #define SDMMC_CTYPE_1BIT 0
365 /* Interrupt status & mask register defines */
366 #define SDMMC_INT_SDIO(n) BIT(16 + (n))
367 #define SDMMC_INT_EBE BIT(15)
368 #define SDMMC_INT_ACD BIT(14)
369 #define SDMMC_INT_SBE BIT(13)
370 #define SDMMC_INT_HLE BIT(12)
371 #define SDMMC_INT_FRUN BIT(11)
372 #define SDMMC_INT_HTO BIT(10)
373 #define SDMMC_INT_VOLT_SWITCH BIT(10) /* overloads bit 10! */
374 #define SDMMC_INT_DRTO BIT(9)
375 #define SDMMC_INT_RTO BIT(8)
376 #define SDMMC_INT_DCRC BIT(7)
377 #define SDMMC_INT_RCRC BIT(6)
378 #define SDMMC_INT_RXDR BIT(5)
379 #define SDMMC_INT_TXDR BIT(4)
380 #define SDMMC_INT_DATA_OVER BIT(3)
381 #define SDMMC_INT_CMD_DONE BIT(2)
382 #define SDMMC_INT_RESP_ERR BIT(1)
383 #define SDMMC_INT_CD BIT(0)
384 #define SDMMC_INT_ERROR 0xbfc2
385 /* Command register defines */
386 #define SDMMC_CMD_START BIT(31)
387 #define SDMMC_CMD_USE_HOLD_REG BIT(29)
388 #define SDMMC_CMD_VOLT_SWITCH BIT(28)
389 #define SDMMC_CMD_CCS_EXP BIT(23)
390 #define SDMMC_CMD_CEATA_RD BIT(22)
391 #define SDMMC_CMD_UPD_CLK BIT(21)
392 #define SDMMC_CMD_INIT BIT(15)
393 #define SDMMC_CMD_STOP BIT(14)
394 #define SDMMC_CMD_PRV_DAT_WAIT BIT(13)
395 #define SDMMC_CMD_SEND_STOP BIT(12)
396 #define SDMMC_CMD_STRM_MODE BIT(11)
397 #define SDMMC_CMD_DAT_WR BIT(10)
398 #define SDMMC_CMD_DAT_EXP BIT(9)
399 #define SDMMC_CMD_RESP_CRC BIT(8)
400 #define SDMMC_CMD_RESP_LONG BIT(7)
401 #define SDMMC_CMD_RESP_EXP BIT(6)
402 #define SDMMC_CMD_INDX(n) ((n) & 0x1F)
403 /* Status register defines */
404 #define SDMMC_GET_FCNT(x) (((x)>>17) & 0x1FFF)
405 #define SDMMC_STATUS_DMA_REQ BIT(31)
406 #define SDMMC_STATUS_BUSY BIT(9)
407 /* FIFOTH register defines */
408 #define SDMMC_SET_FIFOTH(m, r, t) (((m) & 0x7) << 28 | \
409 ((r) & 0xFFF) << 16 | \
411 /* HCON register defines */
412 #define DMA_INTERFACE_IDMA (0x0)
413 #define DMA_INTERFACE_DWDMA (0x1)
414 #define DMA_INTERFACE_GDMA (0x2)
415 #define DMA_INTERFACE_NODMA (0x3)
416 #define SDMMC_GET_TRANS_MODE(x) (((x)>>16) & 0x3)
417 #define SDMMC_GET_SLOT_NUM(x) ((((x)>>1) & 0x1F) + 1)
418 #define SDMMC_GET_HDATA_WIDTH(x) (((x)>>7) & 0x7)
419 #define SDMMC_GET_ADDR_CONFIG(x) (((x)>>27) & 0x1)
420 /* Internal DMAC interrupt defines */
421 #define SDMMC_IDMAC_INT_AI BIT(9)
422 #define SDMMC_IDMAC_INT_NI BIT(8)
423 #define SDMMC_IDMAC_INT_CES BIT(5)
424 #define SDMMC_IDMAC_INT_DU BIT(4)
425 #define SDMMC_IDMAC_INT_FBE BIT(2)
426 #define SDMMC_IDMAC_INT_RI BIT(1)
427 #define SDMMC_IDMAC_INT_TI BIT(0)
428 /* Internal DMAC bus mode bits */
429 #define SDMMC_IDMAC_ENABLE BIT(7)
430 #define SDMMC_IDMAC_FB BIT(1)
431 #define SDMMC_IDMAC_SWRESET BIT(0)
433 #define SDMMC_RST_HWACTIVE 0x1
434 /* Version ID register define */
435 #define SDMMC_GET_VERID(x) ((x) & 0xFFFF)
436 /* Card read threshold */
437 #define SDMMC_SET_THLD(v, x) (((v) & 0xFFF) << 16 | (x))
438 #define SDMMC_CARD_WR_THR_EN BIT(2)
439 #define SDMMC_CARD_RD_THR_EN BIT(0)
440 /* UHS-1 register defines */
441 #define SDMMC_UHS_18V BIT(0)
442 /* All ctrl reset bits */
443 #define SDMMC_CTRL_ALL_RESET_FLAGS \
444 (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET | SDMMC_CTRL_DMA_RESET)
446 /* FIFO register access macros. These should not change the data endian-ness
447 * as they are written to memory to be dealt with by the upper layers */
448 #define mci_fifo_readw(__reg) __raw_readw(__reg)
449 #define mci_fifo_readl(__reg) __raw_readl(__reg)
450 #define mci_fifo_readq(__reg) __raw_readq(__reg)
452 #define mci_fifo_writew(__value, __reg) __raw_writew(__reg, __value)
453 #define mci_fifo_writel(__value, __reg) __raw_writel(__reg, __value)
454 #define mci_fifo_writeq(__value, __reg) __raw_writeq(__reg, __value)
456 /* Register access macros */
457 #define mci_readl(dev, reg) \
458 readl_relaxed((dev)->regs + SDMMC_##reg)
459 #define mci_writel(dev, reg, value) \
460 writel_relaxed((value), (dev)->regs + SDMMC_##reg)
462 /* 16-bit FIFO access macros */
463 #define mci_readw(dev, reg) \
464 readw_relaxed((dev)->regs + SDMMC_##reg)
465 #define mci_writew(dev, reg, value) \
466 writew_relaxed((value), (dev)->regs + SDMMC_##reg)
468 /* 64-bit FIFO access macros */
470 #define mci_readq(dev, reg) \
471 readq_relaxed((dev)->regs + SDMMC_##reg)
472 #define mci_writeq(dev, reg, value) \
473 writeq_relaxed((value), (dev)->regs + SDMMC_##reg)
476 * Dummy readq implementation for architectures that don't define it.
478 * We would assume that none of these architectures would configure
479 * the IP block with a 64bit FIFO width, so this code will never be
480 * executed on those machines. Defining these macros here keeps the
481 * rest of the code free from ifdefs.
483 #define mci_readq(dev, reg) \
484 (*(volatile u64 __force *)((dev)->regs + SDMMC_##reg))
485 #define mci_writeq(dev, reg, value) \
486 (*(volatile u64 __force *)((dev)->regs + SDMMC_##reg) = (value))
488 #define __raw_writeq(__value, __reg) \
489 (*(volatile u64 __force *)(__reg) = (__value))
490 #define __raw_readq(__reg) (*(volatile u64 __force *)(__reg))
493 extern int dw_mci_probe(struct dw_mci *host);
494 extern void dw_mci_remove(struct dw_mci *host);
496 extern int dw_mci_runtime_suspend(struct device *device);
497 extern int dw_mci_runtime_resume(struct device *device);
501 * struct dw_mci_slot - MMC slot state
502 * @mmc: The mmc_host representing this slot.
503 * @host: The MMC controller this slot is using.
504 * @ctype: Card type for this slot.
505 * @mrq: mmc_request currently being processed or waiting to be
506 * processed, or NULL when the slot is idle.
507 * @queue_node: List node for placing this node in the @queue list of
509 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
510 * @__clk_old: The last clock value that was requested from core.
511 * Keeping track of this helps us to avoid spamming the console.
512 * @flags: Random state bits associated with the slot.
513 * @id: Number of this slot.
514 * @sdio_id: Number of this slot in the SDIO interrupt registers.
517 struct mmc_host *mmc;
522 struct mmc_request *mrq;
523 struct list_head queue_node;
526 unsigned int __clk_old;
529 #define DW_MMC_CARD_PRESENT 0
530 #define DW_MMC_CARD_NEED_INIT 1
531 #define DW_MMC_CARD_NO_LOW_PWR 2
532 #define DW_MMC_CARD_NO_USE_HOLD 3
533 #define DW_MMC_CARD_NEEDS_POLL 4
539 * dw_mci driver data - dw-mshc implementation specific driver data.
540 * @caps: mmc subsystem specified capabilities of the controller(s).
541 * @init: early implementation specific initialization.
542 * @set_ios: handle bus specific extensions.
543 * @parse_dt: parse implementation specific device tree properties.
544 * @execute_tuning: implementation specific tuning procedure.
546 * Provide controller implementation specific extensions. The usage of this
547 * data structure is fully optional and usage of each member in this structure
548 * is optional as well.
550 struct dw_mci_drv_data {
552 int (*init)(struct dw_mci *host);
553 void (*set_ios)(struct dw_mci *host, struct mmc_ios *ios);
554 int (*parse_dt)(struct dw_mci *host);
555 int (*execute_tuning)(struct dw_mci_slot *slot, u32 opcode);
556 int (*prepare_hs400_tuning)(struct dw_mci *host,
557 struct mmc_ios *ios);
558 int (*switch_voltage)(struct mmc_host *mmc,
559 struct mmc_ios *ios);
561 #endif /* _DW_MMC_H_ */