blob: 6be502cb7a4c9c0657bb97604fc9e9f13f91551f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#ifndef LINUX_MMC_CQHCI_H
13#define LINUX_MMC_CQHCI_H
14
15#include <linux/compiler.h>
16#include <linux/bitops.h>
17#include <linux/spinlock_types.h>
18#include <linux/types.h>
19#include <linux/completion.h>
20#include <linux/wait.h>
21#include <linux/irqreturn.h>
22#include <asm/io.h>
23
24/* registers */
25/* version */
26#define CQHCI_VER 0x00
27#define CQHCI_VER_MAJOR(x) (((x) & GENMASK(11, 8)) >> 8)
28#define CQHCI_VER_MINOR1(x) (((x) & GENMASK(7, 4)) >> 4)
29#define CQHCI_VER_MINOR2(x) ((x) & GENMASK(3, 0))
30
31/* capabilities */
32#define CQHCI_CAP 0x04
33/* configuration */
34#define CQHCI_CFG 0x08
35#define CQHCI_DCMD 0x00001000
36#define CQHCI_TASK_DESC_SZ 0x00000100
37#define CQHCI_ENABLE 0x00000001
38
39/* control */
40#define CQHCI_CTL 0x0C
41#define CQHCI_CLEAR_ALL_TASKS 0x00000100
42#define CQHCI_HALT 0x00000001
43
44/* interrupt status */
45#define CQHCI_IS 0x10
46#define CQHCI_IS_HAC BIT(0)
47#define CQHCI_IS_TCC BIT(1)
48#define CQHCI_IS_RED BIT(2)
49#define CQHCI_IS_TCL BIT(3)
50
51#define CQHCI_IS_MASK (CQHCI_IS_TCC | CQHCI_IS_RED)
52
53/* interrupt status enable */
54#define CQHCI_ISTE 0x14
55
56/* interrupt signal enable */
57#define CQHCI_ISGE 0x18
58
59/* interrupt coalescing */
60#define CQHCI_IC 0x1C
61#define CQHCI_IC_ENABLE BIT(31)
62#define CQHCI_IC_RESET BIT(16)
63#define CQHCI_IC_ICCTHWEN BIT(15)
64#define CQHCI_IC_ICCTH(x) (((x) & 0x1F) << 8)
65#define CQHCI_IC_ICTOVALWEN BIT(7)
66#define CQHCI_IC_ICTOVAL(x) ((x) & 0x7F)
67
68/* task list base address */
69#define CQHCI_TDLBA 0x20
70
71/* task list base address upper */
72#define CQHCI_TDLBAU 0x24
73
74/* door-bell */
75#define CQHCI_TDBR 0x28
76
77/* task completion notification */
78#define CQHCI_TCN 0x2C
79
80/* device queue status */
81#define CQHCI_DQS 0x30
82
83/* device pending tasks */
84#define CQHCI_DPT 0x34
85
86/* task clear */
87#define CQHCI_TCLR 0x38
88
89/* send status config 1 */
90#define CQHCI_SSC1 0x40
91
92/* send status config 2 */
93#define CQHCI_SSC2 0x44
94
95/* response for dcmd */
96#define CQHCI_CRDCT 0x48
97
98/* response mode error mask */
99#define CQHCI_RMEM 0x50
100
101/* task error info */
102#define CQHCI_TERRI 0x54
103
104#define CQHCI_TERRI_C_INDEX(x) ((x) & GENMASK(5, 0))
105#define CQHCI_TERRI_C_TASK(x) (((x) & GENMASK(12, 8)) >> 8)
106#define CQHCI_TERRI_C_VALID(x) ((x) & BIT(15))
107#define CQHCI_TERRI_D_INDEX(x) (((x) & GENMASK(21, 16)) >> 16)
108#define CQHCI_TERRI_D_TASK(x) (((x) & GENMASK(28, 24)) >> 24)
109#define CQHCI_TERRI_D_VALID(x) ((x) & BIT(31))
110
111/* command response index */
112#define CQHCI_CRI 0x58
113
114/* command response argument */
115#define CQHCI_CRA 0x5C
116
117#define CQHCI_INT_ALL 0xF
118#define CQHCI_IC_DEFAULT_ICCTH 31
119#define CQHCI_IC_DEFAULT_ICTOVAL 1
120
121/* attribute fields */
122#define CQHCI_VALID(x) (((x) & 1) << 0)
123#define CQHCI_END(x) (((x) & 1) << 1)
124#define CQHCI_INT(x) (((x) & 1) << 2)
125#define CQHCI_ACT(x) (((x) & 0x7) << 3)
126
127/* data command task descriptor fields */
128#define CQHCI_FORCED_PROG(x) (((x) & 1) << 6)
129#define CQHCI_CONTEXT(x) (((x) & 0xF) << 7)
130#define CQHCI_DATA_TAG(x) (((x) & 1) << 11)
131#define CQHCI_DATA_DIR(x) (((x) & 1) << 12)
132#define CQHCI_PRIORITY(x) (((x) & 1) << 13)
133#define CQHCI_QBAR(x) (((x) & 1) << 14)
134#define CQHCI_REL_WRITE(x) (((x) & 1) << 15)
135#define CQHCI_BLK_COUNT(x) (((x) & 0xFFFF) << 16)
136#define CQHCI_BLK_ADDR(x) (((x) & 0xFFFFFFFF) << 32)
137
138/* direct command task descriptor fields */
139#define CQHCI_CMD_INDEX(x) (((x) & 0x3F) << 16)
140#define CQHCI_CMD_TIMING(x) (((x) & 1) << 22)
141#define CQHCI_RESP_TYPE(x) (((x) & 0x3) << 23)
142
143/* transfer descriptor fields */
144#define CQHCI_DAT_LENGTH(x) (((x) & 0xFFFF) << 16)
145#define CQHCI_DAT_ADDR_LO(x) (((x) & 0xFFFFFFFF) << 32)
146#define CQHCI_DAT_ADDR_HI(x) (((x) & 0xFFFFFFFF) << 0)
147
148struct cqhci_host_ops;
149struct mmc_host;
150struct cqhci_slot;
151
152struct cqhci_host {
153 const struct cqhci_host_ops *ops;
154 void __iomem *mmio;
155 struct mmc_host *mmc;
156
157 spinlock_t lock;
158
159 /* relative card address of device */
160 unsigned int rca;
161
162 /* 64 bit DMA */
163 bool dma64;
164 int num_slots;
165 int qcnt;
166
167 u32 dcmd_slot;
168 u32 caps;
169#define CQHCI_TASK_DESC_SZ_128 0x1
170
171 u32 quirks;
172#define CQHCI_QUIRK_SHORT_TXFR_DESC_SZ (1 << 0)
173#define CQHCI_QUIRK_DIS_BEFORE_NON_CQ_CMD (1 << 1)
174
175 bool enabled;
176 bool halted;
177 bool init_done;
178 bool activated;
179 bool waiting_for_idle;
180 bool recovery_halt;
181
182 size_t desc_size;
183 size_t data_size;
184
185 u8 *desc_base;
186
187 /* total descriptor size */
188 u8 slot_sz;
189
190 /* 64/128 bit depends on CQHCI_CFG */
191 u8 task_desc_len;
192
193 /* 64 bit on 32-bit arch, 128 bit on 64-bit */
194 u8 link_desc_len;
195
196 u8 *trans_desc_base;
197 /* same length as transfer descriptor */
198 u8 trans_desc_len;
199
200 dma_addr_t desc_dma_base;
201 dma_addr_t trans_desc_dma_base;
202
203 struct completion halt_comp;
204 wait_queue_head_t wait_queue;
205 struct cqhci_slot *slot;
206};
207
208struct cqhci_host_ops {
209 void (*dumpregs)(struct mmc_host *mmc);
210 void (*write_l)(struct cqhci_host *host, u32 val, int reg);
211 u32 (*read_l)(struct cqhci_host *host, int reg);
212 void (*enable)(struct mmc_host *mmc);
213 void (*disable)(struct mmc_host *mmc, bool recovery);
214};
215
216static inline void cqhci_writel(struct cqhci_host *host, u32 val, int reg)
217{
218 if (unlikely(host->ops->write_l))
219 host->ops->write_l(host, val, reg);
220 else
221 writel_relaxed(val, host->mmio + reg);
222}
223
224static inline u32 cqhci_readl(struct cqhci_host *host, int reg)
225{
226 if (unlikely(host->ops->read_l))
227 return host->ops->read_l(host, reg);
228 else
229 return readl_relaxed(host->mmio + reg);
230}
231
232struct platform_device;
233
234irqreturn_t cqhci_irq(struct mmc_host *mmc, u32 intmask, int cmd_error,
235 int data_error);
236int cqhci_init(struct cqhci_host *cq_host, struct mmc_host *mmc, bool dma64);
237struct cqhci_host *cqhci_pltfm_init(struct platform_device *pdev);
238int cqhci_suspend(struct mmc_host *mmc);
239int cqhci_resume(struct mmc_host *mmc);
240
241#endif