1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
4 *
5 */
6
7 #ifndef _MHI_INT_H
8 #define _MHI_INT_H
9
10 #include <linux/mhi.h>
11
12 extern struct bus_type mhi_bus_type;
13
14 #define MHIREGLEN (0x0)
15 #define MHIREGLEN_MHIREGLEN_MASK (0xFFFFFFFF)
16 #define MHIREGLEN_MHIREGLEN_SHIFT (0)
17
18 #define MHIVER (0x8)
19 #define MHIVER_MHIVER_MASK (0xFFFFFFFF)
20 #define MHIVER_MHIVER_SHIFT (0)
21
22 #define MHICFG (0x10)
23 #define MHICFG_NHWER_MASK (0xFF000000)
24 #define MHICFG_NHWER_SHIFT (24)
25 #define MHICFG_NER_MASK (0xFF0000)
26 #define MHICFG_NER_SHIFT (16)
27 #define MHICFG_NHWCH_MASK (0xFF00)
28 #define MHICFG_NHWCH_SHIFT (8)
29 #define MHICFG_NCH_MASK (0xFF)
30 #define MHICFG_NCH_SHIFT (0)
31
32 #define CHDBOFF (0x18)
33 #define CHDBOFF_CHDBOFF_MASK (0xFFFFFFFF)
34 #define CHDBOFF_CHDBOFF_SHIFT (0)
35
36 #define ERDBOFF (0x20)
37 #define ERDBOFF_ERDBOFF_MASK (0xFFFFFFFF)
38 #define ERDBOFF_ERDBOFF_SHIFT (0)
39
40 #define BHIOFF (0x28)
41 #define BHIOFF_BHIOFF_MASK (0xFFFFFFFF)
42 #define BHIOFF_BHIOFF_SHIFT (0)
43
44 #define BHIEOFF (0x2C)
45 #define BHIEOFF_BHIEOFF_MASK (0xFFFFFFFF)
46 #define BHIEOFF_BHIEOFF_SHIFT (0)
47
48 #define DEBUGOFF (0x30)
49 #define DEBUGOFF_DEBUGOFF_MASK (0xFFFFFFFF)
50 #define DEBUGOFF_DEBUGOFF_SHIFT (0)
51
52 #define MHICTRL (0x38)
53 #define MHICTRL_MHISTATE_MASK (0x0000FF00)
54 #define MHICTRL_MHISTATE_SHIFT (8)
55 #define MHICTRL_RESET_MASK (0x2)
56 #define MHICTRL_RESET_SHIFT (1)
57
58 #define MHISTATUS (0x48)
59 #define MHISTATUS_MHISTATE_MASK (0x0000FF00)
60 #define MHISTATUS_MHISTATE_SHIFT (8)
61 #define MHISTATUS_SYSERR_MASK (0x4)
62 #define MHISTATUS_SYSERR_SHIFT (2)
63 #define MHISTATUS_READY_MASK (0x1)
64 #define MHISTATUS_READY_SHIFT (0)
65
66 #define CCABAP_LOWER (0x58)
67 #define CCABAP_LOWER_CCABAP_LOWER_MASK (0xFFFFFFFF)
68 #define CCABAP_LOWER_CCABAP_LOWER_SHIFT (0)
69
70 #define CCABAP_HIGHER (0x5C)
71 #define CCABAP_HIGHER_CCABAP_HIGHER_MASK (0xFFFFFFFF)
72 #define CCABAP_HIGHER_CCABAP_HIGHER_SHIFT (0)
73
74 #define ECABAP_LOWER (0x60)
75 #define ECABAP_LOWER_ECABAP_LOWER_MASK (0xFFFFFFFF)
76 #define ECABAP_LOWER_ECABAP_LOWER_SHIFT (0)
77
78 #define ECABAP_HIGHER (0x64)
79 #define ECABAP_HIGHER_ECABAP_HIGHER_MASK (0xFFFFFFFF)
80 #define ECABAP_HIGHER_ECABAP_HIGHER_SHIFT (0)
81
82 #define CRCBAP_LOWER (0x68)
83 #define CRCBAP_LOWER_CRCBAP_LOWER_MASK (0xFFFFFFFF)
84 #define CRCBAP_LOWER_CRCBAP_LOWER_SHIFT (0)
85
86 #define CRCBAP_HIGHER (0x6C)
87 #define CRCBAP_HIGHER_CRCBAP_HIGHER_MASK (0xFFFFFFFF)
88 #define CRCBAP_HIGHER_CRCBAP_HIGHER_SHIFT (0)
89
90 #define CRDB_LOWER (0x70)
91 #define CRDB_LOWER_CRDB_LOWER_MASK (0xFFFFFFFF)
92 #define CRDB_LOWER_CRDB_LOWER_SHIFT (0)
93
94 #define CRDB_HIGHER (0x74)
95 #define CRDB_HIGHER_CRDB_HIGHER_MASK (0xFFFFFFFF)
96 #define CRDB_HIGHER_CRDB_HIGHER_SHIFT (0)
97
98 #define MHICTRLBASE_LOWER (0x80)
99 #define MHICTRLBASE_LOWER_MHICTRLBASE_LOWER_MASK (0xFFFFFFFF)
100 #define MHICTRLBASE_LOWER_MHICTRLBASE_LOWER_SHIFT (0)
101
102 #define MHICTRLBASE_HIGHER (0x84)
103 #define MHICTRLBASE_HIGHER_MHICTRLBASE_HIGHER_MASK (0xFFFFFFFF)
104 #define MHICTRLBASE_HIGHER_MHICTRLBASE_HIGHER_SHIFT (0)
105
106 #define MHICTRLLIMIT_LOWER (0x88)
107 #define MHICTRLLIMIT_LOWER_MHICTRLLIMIT_LOWER_MASK (0xFFFFFFFF)
108 #define MHICTRLLIMIT_LOWER_MHICTRLLIMIT_LOWER_SHIFT (0)
109
110 #define MHICTRLLIMIT_HIGHER (0x8C)
111 #define MHICTRLLIMIT_HIGHER_MHICTRLLIMIT_HIGHER_MASK (0xFFFFFFFF)
112 #define MHICTRLLIMIT_HIGHER_MHICTRLLIMIT_HIGHER_SHIFT (0)
113
114 #define MHIDATABASE_LOWER (0x98)
115 #define MHIDATABASE_LOWER_MHIDATABASE_LOWER_MASK (0xFFFFFFFF)
116 #define MHIDATABASE_LOWER_MHIDATABASE_LOWER_SHIFT (0)
117
118 #define MHIDATABASE_HIGHER (0x9C)
119 #define MHIDATABASE_HIGHER_MHIDATABASE_HIGHER_MASK (0xFFFFFFFF)
120 #define MHIDATABASE_HIGHER_MHIDATABASE_HIGHER_SHIFT (0)
121
122 #define MHIDATALIMIT_LOWER (0xA0)
123 #define MHIDATALIMIT_LOWER_MHIDATALIMIT_LOWER_MASK (0xFFFFFFFF)
124 #define MHIDATALIMIT_LOWER_MHIDATALIMIT_LOWER_SHIFT (0)
125
126 #define MHIDATALIMIT_HIGHER (0xA4)
127 #define MHIDATALIMIT_HIGHER_MHIDATALIMIT_HIGHER_MASK (0xFFFFFFFF)
128 #define MHIDATALIMIT_HIGHER_MHIDATALIMIT_HIGHER_SHIFT (0)
129
130 /* Host request register */
131 #define MHI_SOC_RESET_REQ_OFFSET (0xB0)
132 #define MHI_SOC_RESET_REQ BIT(0)
133
134 /* MHI BHI offfsets */
135 #define BHI_BHIVERSION_MINOR (0x00)
136 #define BHI_BHIVERSION_MAJOR (0x04)
137 #define BHI_IMGADDR_LOW (0x08)
138 #define BHI_IMGADDR_HIGH (0x0C)
139 #define BHI_IMGSIZE (0x10)
140 #define BHI_RSVD1 (0x14)
141 #define BHI_IMGTXDB (0x18)
142 #define BHI_TXDB_SEQNUM_BMSK (0x3FFFFFFF)
143 #define BHI_TXDB_SEQNUM_SHFT (0)
144 #define BHI_RSVD2 (0x1C)
145 #define BHI_INTVEC (0x20)
146 #define BHI_RSVD3 (0x24)
147 #define BHI_EXECENV (0x28)
148 #define BHI_STATUS (0x2C)
149 #define BHI_ERRCODE (0x30)
150 #define BHI_ERRDBG1 (0x34)
151 #define BHI_ERRDBG2 (0x38)
152 #define BHI_ERRDBG3 (0x3C)
153 #define BHI_SERIALNU (0x40)
154 #define BHI_SBLANTIROLLVER (0x44)
155 #define BHI_NUMSEG (0x48)
156 #define BHI_MSMHWID(n) (0x4C + (0x4 * (n)))
157 #define BHI_OEMPKHASH(n) (0x64 + (0x4 * (n)))
158 #define BHI_RSVD5 (0xC4)
159 #define BHI_STATUS_MASK (0xC0000000)
160 #define BHI_STATUS_SHIFT (30)
161 #define BHI_STATUS_ERROR (3)
162 #define BHI_STATUS_SUCCESS (2)
163 #define BHI_STATUS_RESET (0)
164
165 /* MHI BHIE offsets */
166 #define BHIE_MSMSOCID_OFFS (0x0000)
167 #define BHIE_TXVECADDR_LOW_OFFS (0x002C)
168 #define BHIE_TXVECADDR_HIGH_OFFS (0x0030)
169 #define BHIE_TXVECSIZE_OFFS (0x0034)
170 #define BHIE_TXVECDB_OFFS (0x003C)
171 #define BHIE_TXVECDB_SEQNUM_BMSK (0x3FFFFFFF)
172 #define BHIE_TXVECDB_SEQNUM_SHFT (0)
173 #define BHIE_TXVECSTATUS_OFFS (0x0044)
174 #define BHIE_TXVECSTATUS_SEQNUM_BMSK (0x3FFFFFFF)
175 #define BHIE_TXVECSTATUS_SEQNUM_SHFT (0)
176 #define BHIE_TXVECSTATUS_STATUS_BMSK (0xC0000000)
177 #define BHIE_TXVECSTATUS_STATUS_SHFT (30)
178 #define BHIE_TXVECSTATUS_STATUS_RESET (0x00)
179 #define BHIE_TXVECSTATUS_STATUS_XFER_COMPL (0x02)
180 #define BHIE_TXVECSTATUS_STATUS_ERROR (0x03)
181 #define BHIE_RXVECADDR_LOW_OFFS (0x0060)
182 #define BHIE_RXVECADDR_HIGH_OFFS (0x0064)
183 #define BHIE_RXVECSIZE_OFFS (0x0068)
184 #define BHIE_RXVECDB_OFFS (0x0070)
185 #define BHIE_RXVECDB_SEQNUM_BMSK (0x3FFFFFFF)
186 #define BHIE_RXVECDB_SEQNUM_SHFT (0)
187 #define BHIE_RXVECSTATUS_OFFS (0x0078)
188 #define BHIE_RXVECSTATUS_SEQNUM_BMSK (0x3FFFFFFF)
189 #define BHIE_RXVECSTATUS_SEQNUM_SHFT (0)
190 #define BHIE_RXVECSTATUS_STATUS_BMSK (0xC0000000)
191 #define BHIE_RXVECSTATUS_STATUS_SHFT (30)
192 #define BHIE_RXVECSTATUS_STATUS_RESET (0x00)
193 #define BHIE_RXVECSTATUS_STATUS_XFER_COMPL (0x02)
194 #define BHIE_RXVECSTATUS_STATUS_ERROR (0x03)
195
196 #define SOC_HW_VERSION_OFFS (0x224)
197 #define SOC_HW_VERSION_FAM_NUM_BMSK (0xF0000000)
198 #define SOC_HW_VERSION_FAM_NUM_SHFT (28)
199 #define SOC_HW_VERSION_DEV_NUM_BMSK (0x0FFF0000)
200 #define SOC_HW_VERSION_DEV_NUM_SHFT (16)
201 #define SOC_HW_VERSION_MAJOR_VER_BMSK (0x0000FF00)
202 #define SOC_HW_VERSION_MAJOR_VER_SHFT (8)
203 #define SOC_HW_VERSION_MINOR_VER_BMSK (0x000000FF)
204 #define SOC_HW_VERSION_MINOR_VER_SHFT (0)
205
206 #define EV_CTX_RESERVED_MASK GENMASK(7, 0)
207 #define EV_CTX_INTMODC_MASK GENMASK(15, 8)
208 #define EV_CTX_INTMODC_SHIFT 8
209 #define EV_CTX_INTMODT_MASK GENMASK(31, 16)
210 #define EV_CTX_INTMODT_SHIFT 16
211 struct mhi_event_ctxt {
212 __le32 intmod;
213 __le32 ertype;
214 __le32 msivec;
215
216 __le64 rbase __packed __aligned(4);
217 __le64 rlen __packed __aligned(4);
218 __le64 rp __packed __aligned(4);
219 __le64 wp __packed __aligned(4);
220 };
221
222 #define CHAN_CTX_CHSTATE_MASK GENMASK(7, 0)
223 #define CHAN_CTX_CHSTATE_SHIFT 0
224 #define CHAN_CTX_BRSTMODE_MASK GENMASK(9, 8)
225 #define CHAN_CTX_BRSTMODE_SHIFT 8
226 #define CHAN_CTX_POLLCFG_MASK GENMASK(15, 10)
227 #define CHAN_CTX_POLLCFG_SHIFT 10
228 #define CHAN_CTX_RESERVED_MASK GENMASK(31, 16)
229 struct mhi_chan_ctxt {
230 __le32 chcfg;
231 __le32 chtype;
232 __le32 erindex;
233
234 __le64 rbase __packed __aligned(4);
235 __le64 rlen __packed __aligned(4);
236 __le64 rp __packed __aligned(4);
237 __le64 wp __packed __aligned(4);
238 };
239
240 struct mhi_cmd_ctxt {
241 __le32 reserved0;
242 __le32 reserved1;
243 __le32 reserved2;
244
245 __le64 rbase __packed __aligned(4);
246 __le64 rlen __packed __aligned(4);
247 __le64 rp __packed __aligned(4);
248 __le64 wp __packed __aligned(4);
249 };
250
251 struct mhi_ctxt {
252 struct mhi_event_ctxt *er_ctxt;
253 struct mhi_chan_ctxt *chan_ctxt;
254 struct mhi_cmd_ctxt *cmd_ctxt;
255 dma_addr_t er_ctxt_addr;
256 dma_addr_t chan_ctxt_addr;
257 dma_addr_t cmd_ctxt_addr;
258 };
259
260 struct mhi_ring_element {
261 __le64 ptr;
262 __le32 dword[2];
263 };
264
265 struct bhi_vec_entry {
266 u64 dma_addr;
267 u64 size;
268 };
269
270 enum mhi_cmd_type {
271 MHI_CMD_NOP = 1,
272 MHI_CMD_RESET_CHAN = 16,
273 MHI_CMD_STOP_CHAN = 17,
274 MHI_CMD_START_CHAN = 18,
275 };
276
277 /* No operation command */
278 #define MHI_TRE_CMD_NOOP_PTR (0)
279 #define MHI_TRE_CMD_NOOP_DWORD0 (0)
280 #define MHI_TRE_CMD_NOOP_DWORD1 (cpu_to_le32(MHI_CMD_NOP << 16))
281
282 /* Channel reset command */
283 #define MHI_TRE_CMD_RESET_PTR (0)
284 #define MHI_TRE_CMD_RESET_DWORD0 (0)
285 #define MHI_TRE_CMD_RESET_DWORD1(chid) (cpu_to_le32((chid << 24) | \
286 (MHI_CMD_RESET_CHAN << 16)))
287
288 /* Channel stop command */
289 #define MHI_TRE_CMD_STOP_PTR (0)
290 #define MHI_TRE_CMD_STOP_DWORD0 (0)
291 #define MHI_TRE_CMD_STOP_DWORD1(chid) (cpu_to_le32((chid << 24) | \
292 (MHI_CMD_STOP_CHAN << 16)))
293
294 /* Channel start command */
295 #define MHI_TRE_CMD_START_PTR (0)
296 #define MHI_TRE_CMD_START_DWORD0 (0)
297 #define MHI_TRE_CMD_START_DWORD1(chid) (cpu_to_le32((chid << 24) | \
298 (MHI_CMD_START_CHAN << 16)))
299
300 #define MHI_TRE_GET_DWORD(tre, word) (le32_to_cpu((tre)->dword[(word)]))
301 #define MHI_TRE_GET_CMD_CHID(tre) ((MHI_TRE_GET_DWORD(tre, 1) >> 24) & 0xFF)
302 #define MHI_TRE_GET_CMD_TYPE(tre) ((MHI_TRE_GET_DWORD(tre, 1) >> 16) & 0xFF)
303
304 /* Event descriptor macros */
305 #define MHI_TRE_EV_PTR(ptr) (cpu_to_le64(ptr))
306 #define MHI_TRE_EV_DWORD0(code, len) (cpu_to_le32((code << 24) | len))
307 #define MHI_TRE_EV_DWORD1(chid, type) (cpu_to_le32((chid << 24) | (type << 16)))
308 #define MHI_TRE_GET_EV_PTR(tre) (le64_to_cpu((tre)->ptr))
309 #define MHI_TRE_GET_EV_CODE(tre) ((MHI_TRE_GET_DWORD(tre, 0) >> 24) & 0xFF)
310 #define MHI_TRE_GET_EV_LEN(tre) (MHI_TRE_GET_DWORD(tre, 0) & 0xFFFF)
311 #define MHI_TRE_GET_EV_CHID(tre) ((MHI_TRE_GET_DWORD(tre, 1) >> 24) & 0xFF)
312 #define MHI_TRE_GET_EV_TYPE(tre) ((MHI_TRE_GET_DWORD(tre, 1) >> 16) & 0xFF)
313 #define MHI_TRE_GET_EV_STATE(tre) ((MHI_TRE_GET_DWORD(tre, 0) >> 24) & 0xFF)
314 #define MHI_TRE_GET_EV_EXECENV(tre) ((MHI_TRE_GET_DWORD(tre, 0) >> 24) & 0xFF)
315 #define MHI_TRE_GET_EV_SEQ(tre) MHI_TRE_GET_DWORD(tre, 0)
316 #define MHI_TRE_GET_EV_TIME(tre) (MHI_TRE_GET_EV_PTR(tre))
317 #define MHI_TRE_GET_EV_COOKIE(tre) lower_32_bits(MHI_TRE_GET_EV_PTR(tre))
318 #define MHI_TRE_GET_EV_VEID(tre) ((MHI_TRE_GET_DWORD(tre, 0) >> 16) & 0xFF)
319 #define MHI_TRE_GET_EV_LINKSPEED(tre) ((MHI_TRE_GET_DWORD(tre, 1) >> 24) & 0xFF)
320 #define MHI_TRE_GET_EV_LINKWIDTH(tre) (MHI_TRE_GET_DWORD(tre, 0) & 0xFF)
321
322 /* Transfer descriptor macros */
323 #define MHI_TRE_DATA_PTR(ptr) (cpu_to_le64(ptr))
324 #define MHI_TRE_DATA_DWORD0(len) (cpu_to_le32(len & MHI_MAX_MTU))
325 #define MHI_TRE_DATA_DWORD1(bei, ieot, ieob, chain) (cpu_to_le32((2 << 16) | (bei << 10) \
326 | (ieot << 9) | (ieob << 8) | chain))
327
328 /* RSC transfer descriptor macros */
329 #define MHI_RSCTRE_DATA_PTR(ptr, len) (cpu_to_le64(((u64)len << 48) | ptr))
330 #define MHI_RSCTRE_DATA_DWORD0(cookie) (cpu_to_le32(cookie))
331 #define MHI_RSCTRE_DATA_DWORD1 (cpu_to_le32(MHI_PKT_TYPE_COALESCING << 16))
332
333 enum mhi_pkt_type {
334 MHI_PKT_TYPE_INVALID = 0x0,
335 MHI_PKT_TYPE_NOOP_CMD = 0x1,
336 MHI_PKT_TYPE_TRANSFER = 0x2,
337 MHI_PKT_TYPE_COALESCING = 0x8,
338 MHI_PKT_TYPE_RESET_CHAN_CMD = 0x10,
339 MHI_PKT_TYPE_STOP_CHAN_CMD = 0x11,
340 MHI_PKT_TYPE_START_CHAN_CMD = 0x12,
341 MHI_PKT_TYPE_STATE_CHANGE_EVENT = 0x20,
342 MHI_PKT_TYPE_CMD_COMPLETION_EVENT = 0x21,
343 MHI_PKT_TYPE_TX_EVENT = 0x22,
344 MHI_PKT_TYPE_RSC_TX_EVENT = 0x28,
345 MHI_PKT_TYPE_EE_EVENT = 0x40,
346 MHI_PKT_TYPE_TSYNC_EVENT = 0x48,
347 MHI_PKT_TYPE_BW_REQ_EVENT = 0x50,
348 MHI_PKT_TYPE_STALE_EVENT, /* internal event */
349 };
350
351 /* MHI transfer completion events */
352 enum mhi_ev_ccs {
353 MHI_EV_CC_INVALID = 0x0,
354 MHI_EV_CC_SUCCESS = 0x1,
355 MHI_EV_CC_EOT = 0x2, /* End of transfer event */
356 MHI_EV_CC_OVERFLOW = 0x3,
357 MHI_EV_CC_EOB = 0x4, /* End of block event */
358 MHI_EV_CC_OOB = 0x5, /* Out of block event */
359 MHI_EV_CC_DB_MODE = 0x6,
360 MHI_EV_CC_UNDEFINED_ERR = 0x10,
361 MHI_EV_CC_BAD_TRE = 0x11,
362 };
363
364 enum mhi_ch_state {
365 MHI_CH_STATE_DISABLED = 0x0,
366 MHI_CH_STATE_ENABLED = 0x1,
367 MHI_CH_STATE_RUNNING = 0x2,
368 MHI_CH_STATE_SUSPENDED = 0x3,
369 MHI_CH_STATE_STOP = 0x4,
370 MHI_CH_STATE_ERROR = 0x5,
371 };
372
373 enum mhi_ch_state_type {
374 MHI_CH_STATE_TYPE_RESET,
375 MHI_CH_STATE_TYPE_STOP,
376 MHI_CH_STATE_TYPE_START,
377 MHI_CH_STATE_TYPE_MAX,
378 };
379
380 extern const char * const mhi_ch_state_type_str[MHI_CH_STATE_TYPE_MAX];
381 #define TO_CH_STATE_TYPE_STR(state) (((state) >= MHI_CH_STATE_TYPE_MAX) ? \
382 "INVALID_STATE" : \
383 mhi_ch_state_type_str[(state)])
384
385 #define MHI_INVALID_BRSTMODE(mode) (mode != MHI_DB_BRST_DISABLE && \
386 mode != MHI_DB_BRST_ENABLE)
387
388 extern const char * const mhi_ee_str[MHI_EE_MAX];
389 #define TO_MHI_EXEC_STR(ee) (((ee) >= MHI_EE_MAX) ? \
390 "INVALID_EE" : mhi_ee_str[ee])
391
392 #define MHI_IN_PBL(ee) (ee == MHI_EE_PBL || ee == MHI_EE_PTHRU || \
393 ee == MHI_EE_EDL)
394
395 #define MHI_IN_MISSION_MODE(ee) (ee == MHI_EE_AMSS || ee == MHI_EE_WFW || \
396 ee == MHI_EE_FP)
397
398 enum dev_st_transition {
399 DEV_ST_TRANSITION_PBL,
400 DEV_ST_TRANSITION_READY,
401 DEV_ST_TRANSITION_SBL,
402 DEV_ST_TRANSITION_MISSION_MODE,
403 DEV_ST_TRANSITION_FP,
404 DEV_ST_TRANSITION_SYS_ERR,
405 DEV_ST_TRANSITION_DISABLE,
406 DEV_ST_TRANSITION_MAX,
407 };
408
409 extern const char * const dev_state_tran_str[DEV_ST_TRANSITION_MAX];
410 #define TO_DEV_STATE_TRANS_STR(state) (((state) >= DEV_ST_TRANSITION_MAX) ? \
411 "INVALID_STATE" : dev_state_tran_str[state])
412
413 extern const char * const mhi_state_str[MHI_STATE_MAX];
414 #define TO_MHI_STATE_STR(state) ((state >= MHI_STATE_MAX || \
415 !mhi_state_str[state]) ? \
416 "INVALID_STATE" : mhi_state_str[state])
417
418 /* internal power states */
419 enum mhi_pm_state {
420 MHI_PM_STATE_DISABLE,
421 MHI_PM_STATE_POR,
422 MHI_PM_STATE_M0,
423 MHI_PM_STATE_M2,
424 MHI_PM_STATE_M3_ENTER,
425 MHI_PM_STATE_M3,
426 MHI_PM_STATE_M3_EXIT,
427 MHI_PM_STATE_FW_DL_ERR,
428 MHI_PM_STATE_SYS_ERR_DETECT,
429 MHI_PM_STATE_SYS_ERR_PROCESS,
430 MHI_PM_STATE_SHUTDOWN_PROCESS,
431 MHI_PM_STATE_LD_ERR_FATAL_DETECT,
432 MHI_PM_STATE_MAX
433 };
434
435 #define MHI_PM_DISABLE BIT(0)
436 #define MHI_PM_POR BIT(1)
437 #define MHI_PM_M0 BIT(2)
438 #define MHI_PM_M2 BIT(3)
439 #define MHI_PM_M3_ENTER BIT(4)
440 #define MHI_PM_M3 BIT(5)
441 #define MHI_PM_M3_EXIT BIT(6)
442 /* firmware download failure state */
443 #define MHI_PM_FW_DL_ERR BIT(7)
444 #define MHI_PM_SYS_ERR_DETECT BIT(8)
445 #define MHI_PM_SYS_ERR_PROCESS BIT(9)
446 #define MHI_PM_SHUTDOWN_PROCESS BIT(10)
447 /* link not accessible */
448 #define MHI_PM_LD_ERR_FATAL_DETECT BIT(11)
449
450 #define MHI_REG_ACCESS_VALID(pm_state) ((pm_state & (MHI_PM_POR | MHI_PM_M0 | \
451 MHI_PM_M2 | MHI_PM_M3_ENTER | MHI_PM_M3_EXIT | \
452 MHI_PM_SYS_ERR_DETECT | MHI_PM_SYS_ERR_PROCESS | \
453 MHI_PM_SHUTDOWN_PROCESS | MHI_PM_FW_DL_ERR)))
454 #define MHI_PM_IN_ERROR_STATE(pm_state) (pm_state >= MHI_PM_FW_DL_ERR)
455 #define MHI_PM_IN_FATAL_STATE(pm_state) (pm_state == MHI_PM_LD_ERR_FATAL_DETECT)
456 #define MHI_DB_ACCESS_VALID(mhi_cntrl) (mhi_cntrl->pm_state & \
457 mhi_cntrl->db_access)
458 #define MHI_WAKE_DB_CLEAR_VALID(pm_state) (pm_state & (MHI_PM_M0 | \
459 MHI_PM_M2 | MHI_PM_M3_EXIT))
460 #define MHI_WAKE_DB_SET_VALID(pm_state) (pm_state & MHI_PM_M2)
461 #define MHI_WAKE_DB_FORCE_SET_VALID(pm_state) MHI_WAKE_DB_CLEAR_VALID(pm_state)
462 #define MHI_EVENT_ACCESS_INVALID(pm_state) (pm_state == MHI_PM_DISABLE || \
463 MHI_PM_IN_ERROR_STATE(pm_state))
464 #define MHI_PM_IN_SUSPEND_STATE(pm_state) (pm_state & \
465 (MHI_PM_M3_ENTER | MHI_PM_M3))
466
467 #define NR_OF_CMD_RINGS 1
468 #define CMD_EL_PER_RING 128
469 #define PRIMARY_CMD_RING 0
470 #define MHI_DEV_WAKE_DB 127
471 #define MHI_MAX_MTU 0xffff
472 #define MHI_RANDOM_U32_NONZERO(bmsk) (prandom_u32_max(bmsk) + 1)
473
474 enum mhi_er_type {
475 MHI_ER_TYPE_INVALID = 0x0,
476 MHI_ER_TYPE_VALID = 0x1,
477 };
478
479 struct db_cfg {
480 bool reset_req;
481 bool db_mode;
482 u32 pollcfg;
483 enum mhi_db_brst_mode brstmode;
484 dma_addr_t db_val;
485 void (*process_db)(struct mhi_controller *mhi_cntrl,
486 struct db_cfg *db_cfg, void __iomem *io_addr,
487 dma_addr_t db_val);
488 };
489
490 struct mhi_pm_transitions {
491 enum mhi_pm_state from_state;
492 u32 to_states;
493 };
494
495 struct state_transition {
496 struct list_head node;
497 enum dev_st_transition state;
498 };
499
500 struct mhi_ring {
501 dma_addr_t dma_handle;
502 dma_addr_t iommu_base;
503 __le64 *ctxt_wp; /* point to ctxt wp */
504 void *pre_aligned;
505 void *base;
506 void *rp;
507 void *wp;
508 size_t el_size;
509 size_t len;
510 size_t elements;
511 size_t alloc_size;
512 void __iomem *db_addr;
513 };
514
515 struct mhi_cmd {
516 struct mhi_ring ring;
517 spinlock_t lock;
518 };
519
520 struct mhi_buf_info {
521 void *v_addr;
522 void *bb_addr;
523 void *wp;
524 void *cb_buf;
525 dma_addr_t p_addr;
526 size_t len;
527 enum dma_data_direction dir;
528 bool used; /* Indicates whether the buffer is used or not */
529 bool pre_mapped; /* Already pre-mapped by client */
530 };
531
532 struct mhi_event {
533 struct mhi_controller *mhi_cntrl;
534 struct mhi_chan *mhi_chan; /* dedicated to channel */
535 u32 er_index;
536 u32 intmod;
537 u32 irq;
538 int chan; /* this event ring is dedicated to a channel (optional) */
539 u32 priority;
540 enum mhi_er_data_type data_type;
541 struct mhi_ring ring;
542 struct db_cfg db_cfg;
543 struct tasklet_struct task;
544 spinlock_t lock;
545 int (*process_event)(struct mhi_controller *mhi_cntrl,
546 struct mhi_event *mhi_event,
547 u32 event_quota);
548 bool hw_ring;
549 bool cl_manage;
550 bool offload_ev; /* managed by a device driver */
551 };
552
553 struct mhi_chan {
554 const char *name;
555 /*
556 * Important: When consuming, increment tre_ring first and when
557 * releasing, decrement buf_ring first. If tre_ring has space, buf_ring
558 * is guranteed to have space so we do not need to check both rings.
559 */
560 struct mhi_ring buf_ring;
561 struct mhi_ring tre_ring;
562 u32 chan;
563 u32 er_index;
564 u32 intmod;
565 enum mhi_ch_type type;
566 enum dma_data_direction dir;
567 struct db_cfg db_cfg;
568 enum mhi_ch_ee_mask ee_mask;
569 enum mhi_ch_state ch_state;
570 enum mhi_ev_ccs ccs;
571 struct mhi_device *mhi_dev;
572 void (*xfer_cb)(struct mhi_device *mhi_dev, struct mhi_result *result);
573 struct mutex mutex;
574 struct completion completion;
575 rwlock_t lock;
576 struct list_head node;
577 bool lpm_notify;
578 bool configured;
579 bool offload_ch;
580 bool pre_alloc;
581 bool wake_capable;
582 };
583
584 /* Default MHI timeout */
585 #define MHI_TIMEOUT_MS (1000)
586
587 /* debugfs related functions */
588 #ifdef CONFIG_MHI_BUS_DEBUG
589 void mhi_create_debugfs(struct mhi_controller *mhi_cntrl);
590 void mhi_destroy_debugfs(struct mhi_controller *mhi_cntrl);
591 void mhi_debugfs_init(void);
592 void mhi_debugfs_exit(void);
593 #else
mhi_create_debugfs(struct mhi_controller * mhi_cntrl)594 static inline void mhi_create_debugfs(struct mhi_controller *mhi_cntrl)
595 {
596 }
597
mhi_destroy_debugfs(struct mhi_controller * mhi_cntrl)598 static inline void mhi_destroy_debugfs(struct mhi_controller *mhi_cntrl)
599 {
600 }
601
mhi_debugfs_init(void)602 static inline void mhi_debugfs_init(void)
603 {
604 }
605
mhi_debugfs_exit(void)606 static inline void mhi_debugfs_exit(void)
607 {
608 }
609 #endif
610
611 struct mhi_device *mhi_alloc_device(struct mhi_controller *mhi_cntrl);
612
613 int mhi_destroy_device(struct device *dev, void *data);
614 void mhi_create_devices(struct mhi_controller *mhi_cntrl);
615
616 int mhi_alloc_bhie_table(struct mhi_controller *mhi_cntrl,
617 struct image_info **image_info, size_t alloc_size);
618 void mhi_free_bhie_table(struct mhi_controller *mhi_cntrl,
619 struct image_info *image_info);
620
621 /* Power management APIs */
622 enum mhi_pm_state __must_check mhi_tryset_pm_state(
623 struct mhi_controller *mhi_cntrl,
624 enum mhi_pm_state state);
625 const char *to_mhi_pm_state_str(u32 state);
626 int mhi_queue_state_transition(struct mhi_controller *mhi_cntrl,
627 enum dev_st_transition state);
628 void mhi_pm_st_worker(struct work_struct *work);
629 void mhi_pm_sys_err_handler(struct mhi_controller *mhi_cntrl);
630 int mhi_ready_state_transition(struct mhi_controller *mhi_cntrl);
631 int mhi_pm_m0_transition(struct mhi_controller *mhi_cntrl);
632 void mhi_pm_m1_transition(struct mhi_controller *mhi_cntrl);
633 int mhi_pm_m3_transition(struct mhi_controller *mhi_cntrl);
634 int __mhi_device_get_sync(struct mhi_controller *mhi_cntrl);
635 int mhi_send_cmd(struct mhi_controller *mhi_cntrl, struct mhi_chan *mhi_chan,
636 enum mhi_cmd_type cmd);
637 int mhi_download_amss_image(struct mhi_controller *mhi_cntrl);
mhi_is_active(struct mhi_controller * mhi_cntrl)638 static inline bool mhi_is_active(struct mhi_controller *mhi_cntrl)
639 {
640 return (mhi_cntrl->dev_state >= MHI_STATE_M0 &&
641 mhi_cntrl->dev_state <= MHI_STATE_M3_FAST);
642 }
643
mhi_trigger_resume(struct mhi_controller * mhi_cntrl)644 static inline void mhi_trigger_resume(struct mhi_controller *mhi_cntrl)
645 {
646 pm_wakeup_event(&mhi_cntrl->mhi_dev->dev, 0);
647 mhi_cntrl->runtime_get(mhi_cntrl);
648 mhi_cntrl->runtime_put(mhi_cntrl);
649 }
650
651 /* Register access methods */
652 void mhi_db_brstmode(struct mhi_controller *mhi_cntrl, struct db_cfg *db_cfg,
653 void __iomem *db_addr, dma_addr_t db_val);
654 void mhi_db_brstmode_disable(struct mhi_controller *mhi_cntrl,
655 struct db_cfg *db_mode, void __iomem *db_addr,
656 dma_addr_t db_val);
657 int __must_check mhi_read_reg(struct mhi_controller *mhi_cntrl,
658 void __iomem *base, u32 offset, u32 *out);
659 int __must_check mhi_read_reg_field(struct mhi_controller *mhi_cntrl,
660 void __iomem *base, u32 offset, u32 mask,
661 u32 shift, u32 *out);
662 int __must_check mhi_poll_reg_field(struct mhi_controller *mhi_cntrl,
663 void __iomem *base, u32 offset, u32 mask,
664 u32 shift, u32 val, u32 delayus);
665 void mhi_write_reg(struct mhi_controller *mhi_cntrl, void __iomem *base,
666 u32 offset, u32 val);
667 void mhi_write_reg_field(struct mhi_controller *mhi_cntrl, void __iomem *base,
668 u32 offset, u32 mask, u32 shift, u32 val);
669 void mhi_ring_er_db(struct mhi_event *mhi_event);
670 void mhi_write_db(struct mhi_controller *mhi_cntrl, void __iomem *db_addr,
671 dma_addr_t db_val);
672 void mhi_ring_cmd_db(struct mhi_controller *mhi_cntrl, struct mhi_cmd *mhi_cmd);
673 void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,
674 struct mhi_chan *mhi_chan);
675
676 /* Initialization methods */
677 int mhi_init_mmio(struct mhi_controller *mhi_cntrl);
678 int mhi_init_dev_ctxt(struct mhi_controller *mhi_cntrl);
679 void mhi_deinit_dev_ctxt(struct mhi_controller *mhi_cntrl);
680 int mhi_init_irq_setup(struct mhi_controller *mhi_cntrl);
681 void mhi_deinit_free_irq(struct mhi_controller *mhi_cntrl);
682 void mhi_rddm_prepare(struct mhi_controller *mhi_cntrl,
683 struct image_info *img_info);
684 void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl);
685 int mhi_prepare_channel(struct mhi_controller *mhi_cntrl,
686 struct mhi_chan *mhi_chan);
687 int mhi_init_chan_ctxt(struct mhi_controller *mhi_cntrl,
688 struct mhi_chan *mhi_chan);
689 void mhi_deinit_chan_ctxt(struct mhi_controller *mhi_cntrl,
690 struct mhi_chan *mhi_chan);
691 void mhi_reset_chan(struct mhi_controller *mhi_cntrl,
692 struct mhi_chan *mhi_chan);
693
694 /* Event processing methods */
695 void mhi_ctrl_ev_task(unsigned long data);
696 void mhi_ev_task(unsigned long data);
697 int mhi_process_data_event_ring(struct mhi_controller *mhi_cntrl,
698 struct mhi_event *mhi_event, u32 event_quota);
699 int mhi_process_ctrl_ev_ring(struct mhi_controller *mhi_cntrl,
700 struct mhi_event *mhi_event, u32 event_quota);
701
702 /* ISR handlers */
703 irqreturn_t mhi_irq_handler(int irq_number, void *dev);
704 irqreturn_t mhi_intvec_threaded_handler(int irq_number, void *dev);
705 irqreturn_t mhi_intvec_handler(int irq_number, void *dev);
706
707 int mhi_gen_tre(struct mhi_controller *mhi_cntrl, struct mhi_chan *mhi_chan,
708 struct mhi_buf_info *info, enum mhi_flags flags);
709 int mhi_map_single_no_bb(struct mhi_controller *mhi_cntrl,
710 struct mhi_buf_info *buf_info);
711 int mhi_map_single_use_bb(struct mhi_controller *mhi_cntrl,
712 struct mhi_buf_info *buf_info);
713 void mhi_unmap_single_no_bb(struct mhi_controller *mhi_cntrl,
714 struct mhi_buf_info *buf_info);
715 void mhi_unmap_single_use_bb(struct mhi_controller *mhi_cntrl,
716 struct mhi_buf_info *buf_info);
717
718 #endif /* _MHI_INT_H */
719