1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MHI PCI driver - MHI over PCI controller driver
5 * This module is a generic driver for registering MHI-over-PCI devices,
6 * such as PCIe QCOM modems.
8 * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/mhi.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/timer.h>
18 #include <linux/workqueue.h>
20 #define MHI_PCI_DEFAULT_BAR_NUM 0
22 #define MHI_POST_RESET_DELAY_MS 2000
24 #define HEALTH_CHECK_PERIOD (HZ * 2)
26 /* PCI VID definitions */
27 #define PCI_VENDOR_ID_THALES 0x1269
28 #define PCI_VENDOR_ID_QUECTEL 0x1eac
31 * struct mhi_pci_dev_info - MHI PCI device specific information
32 * @config: MHI controller configuration
33 * @name: name of the PCI module
34 * @fw: firmware path (if any)
35 * @edl: emergency download mode firmware path (if any)
36 * @bar_num: PCI base address register to use for MHI MMIO register space
37 * @dma_data_width: DMA transfer word size (32 or 64 bits)
38 * @mru_default: default MRU size for MBIM network packets
39 * @sideband_wake: Devices using dedicated sideband GPIO for wakeup instead
40 * of inband wake support (such as sdx24)
42 struct mhi_pci_dev_info {
43 const struct mhi_controller_config *config;
48 unsigned int dma_data_width;
49 unsigned int mru_default;
53 #define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \
57 .num_elements = el_count, \
58 .event_ring = ev_ring, \
59 .dir = DMA_TO_DEVICE, \
60 .ee_mask = BIT(MHI_EE_AMSS), \
62 .doorbell = MHI_DB_BRST_DISABLE, \
63 .lpm_notify = false, \
64 .offload_channel = false, \
65 .doorbell_mode_switch = false, \
68 #define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \
72 .num_elements = el_count, \
73 .event_ring = ev_ring, \
74 .dir = DMA_FROM_DEVICE, \
75 .ee_mask = BIT(MHI_EE_AMSS), \
77 .doorbell = MHI_DB_BRST_DISABLE, \
78 .lpm_notify = false, \
79 .offload_channel = false, \
80 .doorbell_mode_switch = false, \
83 #define MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(ch_num, ch_name, el_count, ev_ring) \
87 .num_elements = el_count, \
88 .event_ring = ev_ring, \
89 .dir = DMA_FROM_DEVICE, \
90 .ee_mask = BIT(MHI_EE_AMSS), \
92 .doorbell = MHI_DB_BRST_DISABLE, \
93 .lpm_notify = false, \
94 .offload_channel = false, \
95 .doorbell_mode_switch = false, \
99 #define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \
101 .num_elements = el_count, \
102 .irq_moderation_ms = 0, \
103 .irq = (ev_ring) + 1, \
105 .mode = MHI_DB_BRST_DISABLE, \
106 .data_type = MHI_ER_CTRL, \
107 .hardware_event = false, \
108 .client_managed = false, \
109 .offload_channel = false, \
112 #define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \
116 .num_elements = el_count, \
117 .event_ring = ev_ring, \
118 .dir = DMA_TO_DEVICE, \
119 .ee_mask = BIT(MHI_EE_AMSS), \
121 .doorbell = MHI_DB_BRST_ENABLE, \
122 .lpm_notify = false, \
123 .offload_channel = false, \
124 .doorbell_mode_switch = true, \
127 #define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \
131 .num_elements = el_count, \
132 .event_ring = ev_ring, \
133 .dir = DMA_FROM_DEVICE, \
134 .ee_mask = BIT(MHI_EE_AMSS), \
136 .doorbell = MHI_DB_BRST_ENABLE, \
137 .lpm_notify = false, \
138 .offload_channel = false, \
139 .doorbell_mode_switch = true, \
142 #define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \
146 .num_elements = el_count, \
147 .event_ring = ev_ring, \
148 .dir = DMA_TO_DEVICE, \
149 .ee_mask = BIT(MHI_EE_SBL), \
151 .doorbell = MHI_DB_BRST_DISABLE, \
152 .lpm_notify = false, \
153 .offload_channel = false, \
154 .doorbell_mode_switch = false, \
157 #define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \
161 .num_elements = el_count, \
162 .event_ring = ev_ring, \
163 .dir = DMA_FROM_DEVICE, \
164 .ee_mask = BIT(MHI_EE_SBL), \
166 .doorbell = MHI_DB_BRST_DISABLE, \
167 .lpm_notify = false, \
168 .offload_channel = false, \
169 .doorbell_mode_switch = false, \
172 #define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \
176 .num_elements = el_count, \
177 .event_ring = ev_ring, \
178 .dir = DMA_TO_DEVICE, \
179 .ee_mask = BIT(MHI_EE_FP), \
181 .doorbell = MHI_DB_BRST_DISABLE, \
182 .lpm_notify = false, \
183 .offload_channel = false, \
184 .doorbell_mode_switch = false, \
187 #define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \
191 .num_elements = el_count, \
192 .event_ring = ev_ring, \
193 .dir = DMA_FROM_DEVICE, \
194 .ee_mask = BIT(MHI_EE_FP), \
196 .doorbell = MHI_DB_BRST_DISABLE, \
197 .lpm_notify = false, \
198 .offload_channel = false, \
199 .doorbell_mode_switch = false, \
202 #define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \
204 .num_elements = el_count, \
205 .irq_moderation_ms = 5, \
206 .irq = (ev_ring) + 1, \
208 .mode = MHI_DB_BRST_DISABLE, \
209 .data_type = MHI_ER_DATA, \
210 .hardware_event = false, \
211 .client_managed = false, \
212 .offload_channel = false, \
215 #define MHI_EVENT_CONFIG_SW_DATA(ev_ring, el_count) \
217 .num_elements = el_count, \
218 .irq_moderation_ms = 0, \
219 .irq = (ev_ring) + 1, \
221 .mode = MHI_DB_BRST_DISABLE, \
222 .data_type = MHI_ER_DATA, \
223 .hardware_event = false, \
224 .client_managed = false, \
225 .offload_channel = false, \
228 #define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \
230 .num_elements = el_count, \
231 .irq_moderation_ms = 1, \
232 .irq = (ev_ring) + 1, \
234 .mode = MHI_DB_BRST_DISABLE, \
235 .data_type = MHI_ER_DATA, \
236 .hardware_event = true, \
237 .client_managed = false, \
238 .offload_channel = false, \
242 static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = {
243 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1),
244 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1),
245 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0),
246 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0),
247 MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0),
248 MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0),
249 MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0),
250 MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(21, "IPCR", 8, 0),
251 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
252 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
253 MHI_CHANNEL_CONFIG_UL(46, "IP_SW0", 64, 2),
254 MHI_CHANNEL_CONFIG_DL(47, "IP_SW0", 64, 3),
255 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 4),
256 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 5),
259 static struct mhi_event_config modem_qcom_v1_mhi_events[] = {
260 /* first ring is control+data ring */
261 MHI_EVENT_CONFIG_CTRL(0, 64),
262 /* DIAG dedicated event ring */
263 MHI_EVENT_CONFIG_DATA(1, 128),
264 /* Software channels dedicated event ring */
265 MHI_EVENT_CONFIG_SW_DATA(2, 64),
266 MHI_EVENT_CONFIG_SW_DATA(3, 64),
267 /* Hardware channels request dedicated hardware event rings */
268 MHI_EVENT_CONFIG_HW_DATA(4, 1024, 100),
269 MHI_EVENT_CONFIG_HW_DATA(5, 2048, 101)
272 static const struct mhi_controller_config modem_qcom_v1_mhiv_config = {
275 .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels),
276 .ch_cfg = modem_qcom_v1_mhi_channels,
277 .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events),
278 .event_cfg = modem_qcom_v1_mhi_events,
281 static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = {
282 .name = "qcom-sdx65m",
283 .fw = "/*(DEBLOBBED)*/",
284 .edl = "/*(DEBLOBBED)*/",
285 .config = &modem_qcom_v1_mhiv_config,
286 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
287 .dma_data_width = 32,
288 .sideband_wake = false,
291 static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = {
292 .name = "qcom-sdx55m",
293 .fw = "/*(DEBLOBBED)*/",
294 .edl = "/*(DEBLOBBED)*/",
295 .config = &modem_qcom_v1_mhiv_config,
296 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
297 .dma_data_width = 32,
298 .mru_default = 32768,
299 .sideband_wake = false,
302 static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = {
303 .name = "qcom-sdx24",
304 .edl = "/*(DEBLOBBED)*/",
305 .config = &modem_qcom_v1_mhiv_config,
306 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
307 .dma_data_width = 32,
308 .sideband_wake = true,
311 static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = {
312 MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0),
313 MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0),
314 MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0),
315 MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0),
316 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
317 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
318 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
319 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
320 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
321 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
322 /* The EDL firmware is a flash-programmer exposing firehose protocol */
323 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
324 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
325 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
326 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
329 static struct mhi_event_config mhi_quectel_em1xx_events[] = {
330 MHI_EVENT_CONFIG_CTRL(0, 128),
331 MHI_EVENT_CONFIG_DATA(1, 128),
332 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
333 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
336 static const struct mhi_controller_config modem_quectel_em1xx_config = {
339 .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels),
340 .ch_cfg = mhi_quectel_em1xx_channels,
341 .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events),
342 .event_cfg = mhi_quectel_em1xx_events,
345 static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = {
346 .name = "quectel-em1xx",
347 .edl = "/*(DEBLOBBED)*/",
348 .config = &modem_quectel_em1xx_config,
349 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
350 .dma_data_width = 32,
351 .mru_default = 32768,
352 .sideband_wake = true,
355 static const struct mhi_pci_dev_info mhi_quectel_rm5xx_info = {
356 .name = "quectel-rm5xx",
357 .edl = "/*(DEBLOBBED)*/",
358 .config = &modem_quectel_em1xx_config,
359 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
360 .dma_data_width = 32,
361 .mru_default = 32768,
362 .sideband_wake = true,
365 static const struct mhi_channel_config mhi_foxconn_sdx55_channels[] = {
366 MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 32, 0),
367 MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 32, 0),
368 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
369 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
370 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
371 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
372 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
373 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
374 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
375 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
378 static struct mhi_event_config mhi_foxconn_sdx55_events[] = {
379 MHI_EVENT_CONFIG_CTRL(0, 128),
380 MHI_EVENT_CONFIG_DATA(1, 128),
381 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
382 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
385 static const struct mhi_controller_config modem_foxconn_sdx55_config = {
388 .num_channels = ARRAY_SIZE(mhi_foxconn_sdx55_channels),
389 .ch_cfg = mhi_foxconn_sdx55_channels,
390 .num_events = ARRAY_SIZE(mhi_foxconn_sdx55_events),
391 .event_cfg = mhi_foxconn_sdx55_events,
394 static const struct mhi_pci_dev_info mhi_foxconn_sdx24_info = {
395 .name = "foxconn-sdx24",
396 .config = &modem_foxconn_sdx55_config,
397 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
398 .dma_data_width = 32,
399 .mru_default = 32768,
400 .sideband_wake = false,
403 static const struct mhi_pci_dev_info mhi_foxconn_sdx55_info = {
404 .name = "foxconn-sdx55",
405 .fw = "/*(DEBLOBBED)*/",
406 .edl = "/*(DEBLOBBED)*/",
407 .config = &modem_foxconn_sdx55_config,
408 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
409 .dma_data_width = 32,
410 .mru_default = 32768,
411 .sideband_wake = false,
414 static const struct mhi_pci_dev_info mhi_foxconn_sdx65_info = {
415 .name = "foxconn-sdx65",
416 .config = &modem_foxconn_sdx55_config,
417 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
418 .dma_data_width = 32,
419 .mru_default = 32768,
420 .sideband_wake = false,
423 static const struct mhi_channel_config mhi_mv3x_channels[] = {
424 MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 64, 0),
425 MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 64, 0),
426 /* MBIM Control Channel */
427 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 64, 0),
428 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 64, 0),
429 /* MBIM Data Channel */
430 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 512, 2),
431 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 512, 3),
434 static struct mhi_event_config mhi_mv3x_events[] = {
435 MHI_EVENT_CONFIG_CTRL(0, 256),
436 MHI_EVENT_CONFIG_DATA(1, 256),
437 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
438 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101),
441 static const struct mhi_controller_config modem_mv3x_config = {
444 .num_channels = ARRAY_SIZE(mhi_mv3x_channels),
445 .ch_cfg = mhi_mv3x_channels,
446 .num_events = ARRAY_SIZE(mhi_mv3x_events),
447 .event_cfg = mhi_mv3x_events,
450 static const struct mhi_pci_dev_info mhi_mv31_info = {
451 .name = "cinterion-mv31",
452 .config = &modem_mv3x_config,
453 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
454 .dma_data_width = 32,
455 .mru_default = 32768,
458 static const struct mhi_pci_dev_info mhi_mv32_info = {
459 .name = "cinterion-mv32",
460 .config = &modem_mv3x_config,
461 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
462 .dma_data_width = 32,
463 .mru_default = 32768,
466 static const struct mhi_channel_config mhi_sierra_em919x_channels[] = {
467 MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0),
468 MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 256, 0),
469 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 0),
470 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 0),
471 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 128, 0),
472 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 128, 0),
473 MHI_CHANNEL_CONFIG_UL(14, "QMI", 32, 0),
474 MHI_CHANNEL_CONFIG_DL(15, "QMI", 32, 0),
475 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
476 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
477 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 512, 1),
478 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 512, 2),
481 static struct mhi_event_config modem_sierra_em919x_mhi_events[] = {
482 /* first ring is control+data and DIAG ring */
483 MHI_EVENT_CONFIG_CTRL(0, 2048),
484 /* Hardware channels request dedicated hardware event rings */
485 MHI_EVENT_CONFIG_HW_DATA(1, 2048, 100),
486 MHI_EVENT_CONFIG_HW_DATA(2, 2048, 101)
489 static const struct mhi_controller_config modem_sierra_em919x_config = {
492 .num_channels = ARRAY_SIZE(mhi_sierra_em919x_channels),
493 .ch_cfg = mhi_sierra_em919x_channels,
494 .num_events = ARRAY_SIZE(modem_sierra_em919x_mhi_events),
495 .event_cfg = modem_sierra_em919x_mhi_events,
498 static const struct mhi_pci_dev_info mhi_sierra_em919x_info = {
499 .name = "sierra-em919x",
500 .config = &modem_sierra_em919x_config,
501 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
502 .dma_data_width = 32,
503 .sideband_wake = false,
506 static const struct mhi_channel_config mhi_telit_fn980_hw_v1_channels[] = {
507 MHI_CHANNEL_CONFIG_UL(14, "QMI", 32, 0),
508 MHI_CHANNEL_CONFIG_DL(15, "QMI", 32, 0),
509 MHI_CHANNEL_CONFIG_UL(20, "IPCR", 16, 0),
510 MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(21, "IPCR", 16, 0),
511 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 1),
512 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 2),
515 static struct mhi_event_config mhi_telit_fn980_hw_v1_events[] = {
516 MHI_EVENT_CONFIG_CTRL(0, 128),
517 MHI_EVENT_CONFIG_HW_DATA(1, 1024, 100),
518 MHI_EVENT_CONFIG_HW_DATA(2, 2048, 101)
521 static struct mhi_controller_config modem_telit_fn980_hw_v1_config = {
524 .num_channels = ARRAY_SIZE(mhi_telit_fn980_hw_v1_channels),
525 .ch_cfg = mhi_telit_fn980_hw_v1_channels,
526 .num_events = ARRAY_SIZE(mhi_telit_fn980_hw_v1_events),
527 .event_cfg = mhi_telit_fn980_hw_v1_events,
530 static const struct mhi_pci_dev_info mhi_telit_fn980_hw_v1_info = {
531 .name = "telit-fn980-hwv1",
532 .fw = "/*(DEBLOBBED)*/",
533 .edl = "/*(DEBLOBBED)*/",
534 .config = &modem_telit_fn980_hw_v1_config,
535 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
536 .dma_data_width = 32,
537 .mru_default = 32768,
538 .sideband_wake = false,
541 static const struct mhi_channel_config mhi_telit_fn990_channels[] = {
542 MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0),
543 MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0),
544 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 64, 1),
545 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 64, 1),
546 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
547 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
548 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
549 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
550 MHI_CHANNEL_CONFIG_UL(92, "DUN2", 32, 1),
551 MHI_CHANNEL_CONFIG_DL(93, "DUN2", 32, 1),
552 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
553 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
556 static struct mhi_event_config mhi_telit_fn990_events[] = {
557 MHI_EVENT_CONFIG_CTRL(0, 128),
558 MHI_EVENT_CONFIG_DATA(1, 128),
559 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
560 MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101)
563 static const struct mhi_controller_config modem_telit_fn990_config = {
566 .num_channels = ARRAY_SIZE(mhi_telit_fn990_channels),
567 .ch_cfg = mhi_telit_fn990_channels,
568 .num_events = ARRAY_SIZE(mhi_telit_fn990_events),
569 .event_cfg = mhi_telit_fn990_events,
572 static const struct mhi_pci_dev_info mhi_telit_fn990_info = {
573 .name = "telit-fn990",
574 .config = &modem_telit_fn990_config,
575 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
576 .dma_data_width = 32,
577 .sideband_wake = false,
578 .mru_default = 32768,
581 /* Keep the list sorted based on the PID. New VID should be added as the last entry */
582 static const struct pci_device_id mhi_pci_id_table[] = {
583 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304),
584 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info },
585 { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x0306, PCI_VENDOR_ID_QCOM, 0x010c),
586 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
587 /* EM919x (sdx55), use the same vid:pid as qcom-sdx55m */
588 { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x0306, 0x18d7, 0x0200),
589 .driver_data = (kernel_ulong_t) &mhi_sierra_em919x_info },
590 /* Telit FN980 hardware revision v1 */
591 { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x0306, 0x1C5D, 0x2000),
592 .driver_data = (kernel_ulong_t) &mhi_telit_fn980_hw_v1_info },
593 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306),
594 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info },
596 { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x0308, 0x1c5d, 0x2010),
597 .driver_data = (kernel_ulong_t) &mhi_telit_fn990_info },
599 { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x0308, 0x1c5d, 0x2015),
600 .driver_data = (kernel_ulong_t) &mhi_telit_fn990_info },
601 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308),
602 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info },
603 { PCI_DEVICE(PCI_VENDOR_ID_QUECTEL, 0x1001), /* EM120R-GL (sdx24) */
604 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
605 { PCI_DEVICE(PCI_VENDOR_ID_QUECTEL, 0x1002), /* EM160R-GL (sdx24) */
606 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
607 /* RM520N-GL (sdx6x), eSIM */
608 { PCI_DEVICE(PCI_VENDOR_ID_QUECTEL, 0x1004),
609 .driver_data = (kernel_ulong_t) &mhi_quectel_rm5xx_info },
610 /* RM520N-GL (sdx6x), Lenovo variant */
611 { PCI_DEVICE(PCI_VENDOR_ID_QUECTEL, 0x1007),
612 .driver_data = (kernel_ulong_t) &mhi_quectel_rm5xx_info },
613 { PCI_DEVICE(PCI_VENDOR_ID_QUECTEL, 0x100d), /* EM160R-GL (sdx24) */
614 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
615 { PCI_DEVICE(PCI_VENDOR_ID_QUECTEL, 0x2001), /* EM120R-GL for FCCL (sdx24) */
616 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
617 /* T99W175 (sdx55), Both for eSIM and Non-eSIM */
618 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0ab),
619 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
620 /* DW5930e (sdx55), With eSIM, It's also T99W175 */
621 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b0),
622 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
623 /* DW5930e (sdx55), Non-eSIM, It's also T99W175 */
624 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b1),
625 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
626 /* T99W175 (sdx55), Based on Qualcomm new baseline */
627 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0bf),
628 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
629 /* T99W175 (sdx55) */
630 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0c3),
631 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
632 /* T99W368 (sdx65) */
633 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0d8),
634 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx65_info },
635 /* T99W373 (sdx62) */
636 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0d9),
637 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx65_info },
638 /* T99W510 (sdx24), variant 1 */
639 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0f0),
640 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx24_info },
641 /* T99W510 (sdx24), variant 2 */
642 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0f1),
643 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx24_info },
644 /* T99W510 (sdx24), variant 3 */
645 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0f2),
646 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx24_info },
647 /* DW5932e-eSIM (sdx62), With eSIM */
648 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0f5),
649 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx65_info },
650 /* DW5932e (sdx62), Non-eSIM */
651 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0f9),
652 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx65_info },
653 /* MV31-W (Cinterion) */
654 { PCI_DEVICE(PCI_VENDOR_ID_THALES, 0x00b3),
655 .driver_data = (kernel_ulong_t) &mhi_mv31_info },
656 /* MV31-W (Cinterion), based on new baseline */
657 { PCI_DEVICE(PCI_VENDOR_ID_THALES, 0x00b4),
658 .driver_data = (kernel_ulong_t) &mhi_mv31_info },
659 /* MV32-WA (Cinterion) */
660 { PCI_DEVICE(PCI_VENDOR_ID_THALES, 0x00ba),
661 .driver_data = (kernel_ulong_t) &mhi_mv32_info },
662 /* MV32-WB (Cinterion) */
663 { PCI_DEVICE(PCI_VENDOR_ID_THALES, 0x00bb),
664 .driver_data = (kernel_ulong_t) &mhi_mv32_info },
665 /* T99W175 (sdx55), HP variant */
666 { PCI_DEVICE(0x03f0, 0x0a6c),
667 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
670 MODULE_DEVICE_TABLE(pci, mhi_pci_id_table);
672 enum mhi_pci_device_status {
674 MHI_PCI_DEV_SUSPENDED,
677 struct mhi_pci_device {
678 struct mhi_controller mhi_cntrl;
679 struct pci_saved_state *pci_state;
680 struct work_struct recovery_work;
681 struct timer_list health_check_timer;
682 unsigned long status;
685 static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl,
686 void __iomem *addr, u32 *out)
692 static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl,
693 void __iomem *addr, u32 val)
698 static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl,
699 enum mhi_callback cb)
701 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
703 /* Nothing to do for now */
705 case MHI_CB_FATAL_ERROR:
706 case MHI_CB_SYS_ERROR:
707 dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb);
708 pm_runtime_forbid(&pdev->dev);
710 case MHI_CB_EE_MISSION_MODE:
711 pm_runtime_allow(&pdev->dev);
718 static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force)
723 static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override)
728 static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl)
733 static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl)
735 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
738 if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor))
741 if (vendor == (u16) ~0 || vendor == 0)
747 static int mhi_pci_claim(struct mhi_controller *mhi_cntrl,
748 unsigned int bar_num, u64 dma_mask)
750 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
753 err = pci_assign_resource(pdev, bar_num);
757 err = pcim_enable_device(pdev);
759 dev_err(&pdev->dev, "failed to enable pci device: %d\n", err);
763 err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev));
765 dev_err(&pdev->dev, "failed to map pci region: %d\n", err);
768 mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num];
769 mhi_cntrl->reg_len = pci_resource_len(pdev, bar_num);
771 err = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
773 dev_err(&pdev->dev, "Cannot set proper DMA mask\n");
777 pci_set_master(pdev);
782 static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl,
783 const struct mhi_controller_config *mhi_cntrl_config)
785 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
790 * Alloc one MSI vector for BHI + one vector per event ring, ideally...
791 * No explicit pci_free_irq_vectors required, done by pcim_release.
793 mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events;
795 nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI);
796 if (nr_vectors < 0) {
797 dev_err(&pdev->dev, "Error allocating MSI vectors %d\n",
802 if (nr_vectors < mhi_cntrl->nr_irqs) {
803 dev_warn(&pdev->dev, "using shared MSI\n");
805 /* Patch msi vectors, use only one (shared) */
806 for (i = 0; i < mhi_cntrl_config->num_events; i++)
807 mhi_cntrl_config->event_cfg[i].irq = 0;
808 mhi_cntrl->nr_irqs = 1;
811 irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL);
815 for (i = 0; i < mhi_cntrl->nr_irqs; i++) {
816 int vector = i >= nr_vectors ? (nr_vectors - 1) : i;
818 irq[i] = pci_irq_vector(pdev, vector);
821 mhi_cntrl->irq = irq;
826 static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl)
828 /* The runtime_get() MHI callback means:
829 * Do whatever is requested to leave M3.
831 return pm_runtime_get(mhi_cntrl->cntrl_dev);
834 static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl)
836 /* The runtime_put() MHI callback means:
837 * Device can be moved in M3 state.
839 pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev);
840 pm_runtime_put(mhi_cntrl->cntrl_dev);
843 static void mhi_pci_recovery_work(struct work_struct *work)
845 struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device,
847 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
848 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
851 dev_warn(&pdev->dev, "device recovery started\n");
853 del_timer(&mhi_pdev->health_check_timer);
854 pm_runtime_forbid(&pdev->dev);
856 /* Clean up MHI state */
857 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
858 mhi_power_down(mhi_cntrl, false);
859 mhi_unprepare_after_power_down(mhi_cntrl);
862 pci_set_power_state(pdev, PCI_D0);
863 pci_load_saved_state(pdev, mhi_pdev->pci_state);
864 pci_restore_state(pdev);
866 if (!mhi_pci_is_alive(mhi_cntrl))
869 err = mhi_prepare_for_power_up(mhi_cntrl);
873 err = mhi_sync_power_up(mhi_cntrl);
877 dev_dbg(&pdev->dev, "Recovery completed\n");
879 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
880 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
884 mhi_unprepare_after_power_down(mhi_cntrl);
886 if (pci_reset_function(pdev))
887 dev_err(&pdev->dev, "Recovery failed\n");
890 static void health_check(struct timer_list *t)
892 struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer);
893 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
895 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
896 test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
899 if (!mhi_pci_is_alive(mhi_cntrl)) {
900 dev_err(mhi_cntrl->cntrl_dev, "Device died\n");
901 queue_work(system_long_wq, &mhi_pdev->recovery_work);
905 /* reschedule in two seconds */
906 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
909 static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
911 const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data;
912 const struct mhi_controller_config *mhi_cntrl_config;
913 struct mhi_pci_device *mhi_pdev;
914 struct mhi_controller *mhi_cntrl;
917 dev_info(&pdev->dev, "MHI PCI device found: %s\n", info->name);
919 /* mhi_pdev.mhi_cntrl must be zero-initialized */
920 mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL);
924 INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work);
925 timer_setup(&mhi_pdev->health_check_timer, health_check, 0);
927 mhi_cntrl_config = info->config;
928 mhi_cntrl = &mhi_pdev->mhi_cntrl;
930 mhi_cntrl->cntrl_dev = &pdev->dev;
931 mhi_cntrl->iova_start = 0;
932 mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width);
933 mhi_cntrl->fw_image = info->fw;
934 mhi_cntrl->edl_image = info->edl;
936 mhi_cntrl->read_reg = mhi_pci_read_reg;
937 mhi_cntrl->write_reg = mhi_pci_write_reg;
938 mhi_cntrl->status_cb = mhi_pci_status_cb;
939 mhi_cntrl->runtime_get = mhi_pci_runtime_get;
940 mhi_cntrl->runtime_put = mhi_pci_runtime_put;
941 mhi_cntrl->mru = info->mru_default;
943 if (info->sideband_wake) {
944 mhi_cntrl->wake_get = mhi_pci_wake_get_nop;
945 mhi_cntrl->wake_put = mhi_pci_wake_put_nop;
946 mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop;
949 err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width));
953 err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config);
957 pci_set_drvdata(pdev, mhi_pdev);
959 /* Have stored pci confspace at hand for restore in sudden PCI error.
960 * cache the state locally and discard the PCI core one.
962 pci_save_state(pdev);
963 mhi_pdev->pci_state = pci_store_saved_state(pdev);
964 pci_load_saved_state(pdev, NULL);
966 err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config);
970 /* MHI bus does not power up the controller by default */
971 err = mhi_prepare_for_power_up(mhi_cntrl);
973 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
977 err = mhi_sync_power_up(mhi_cntrl);
979 dev_err(&pdev->dev, "failed to power up MHI controller\n");
983 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
985 /* start health check */
986 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
988 /* Only allow runtime-suspend if PME capable (for wakeup) */
989 if (pci_pme_capable(pdev, PCI_D3hot)) {
990 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
991 pm_runtime_use_autosuspend(&pdev->dev);
992 pm_runtime_mark_last_busy(&pdev->dev);
993 pm_runtime_put_noidle(&pdev->dev);
999 mhi_unprepare_after_power_down(mhi_cntrl);
1001 mhi_unregister_controller(mhi_cntrl);
1006 static void mhi_pci_remove(struct pci_dev *pdev)
1008 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
1009 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1011 del_timer_sync(&mhi_pdev->health_check_timer);
1012 cancel_work_sync(&mhi_pdev->recovery_work);
1014 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
1015 mhi_power_down(mhi_cntrl, true);
1016 mhi_unprepare_after_power_down(mhi_cntrl);
1019 /* balancing probe put_noidle */
1020 if (pci_pme_capable(pdev, PCI_D3hot))
1021 pm_runtime_get_noresume(&pdev->dev);
1023 mhi_unregister_controller(mhi_cntrl);
1026 static void mhi_pci_shutdown(struct pci_dev *pdev)
1028 mhi_pci_remove(pdev);
1029 pci_set_power_state(pdev, PCI_D3hot);
1032 static void mhi_pci_reset_prepare(struct pci_dev *pdev)
1034 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
1035 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1037 dev_info(&pdev->dev, "reset\n");
1039 del_timer(&mhi_pdev->health_check_timer);
1041 /* Clean up MHI state */
1042 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
1043 mhi_power_down(mhi_cntrl, false);
1044 mhi_unprepare_after_power_down(mhi_cntrl);
1047 /* cause internal device reset */
1048 mhi_soc_reset(mhi_cntrl);
1050 /* Be sure device reset has been executed */
1051 msleep(MHI_POST_RESET_DELAY_MS);
1054 static void mhi_pci_reset_done(struct pci_dev *pdev)
1056 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
1057 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1060 /* Restore initial known working PCI state */
1061 pci_load_saved_state(pdev, mhi_pdev->pci_state);
1062 pci_restore_state(pdev);
1064 /* Is device status available ? */
1065 if (!mhi_pci_is_alive(mhi_cntrl)) {
1066 dev_err(&pdev->dev, "reset failed\n");
1070 err = mhi_prepare_for_power_up(mhi_cntrl);
1072 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
1076 err = mhi_sync_power_up(mhi_cntrl);
1078 dev_err(&pdev->dev, "failed to power up MHI controller\n");
1079 mhi_unprepare_after_power_down(mhi_cntrl);
1083 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
1084 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
1087 static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev,
1088 pci_channel_state_t state)
1090 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
1091 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1093 dev_err(&pdev->dev, "PCI error detected, state = %u\n", state);
1095 if (state == pci_channel_io_perm_failure)
1096 return PCI_ERS_RESULT_DISCONNECT;
1098 /* Clean up MHI state */
1099 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
1100 mhi_power_down(mhi_cntrl, false);
1101 mhi_unprepare_after_power_down(mhi_cntrl);
1104 return PCI_ERS_RESULT_RECOVERED;
1107 pci_disable_device(pdev);
1109 return PCI_ERS_RESULT_NEED_RESET;
1112 static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev)
1114 if (pci_enable_device(pdev)) {
1115 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n");
1116 return PCI_ERS_RESULT_DISCONNECT;
1119 return PCI_ERS_RESULT_RECOVERED;
1122 static void mhi_pci_io_resume(struct pci_dev *pdev)
1124 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
1126 dev_err(&pdev->dev, "PCI slot reset done\n");
1128 queue_work(system_long_wq, &mhi_pdev->recovery_work);
1131 static const struct pci_error_handlers mhi_pci_err_handler = {
1132 .error_detected = mhi_pci_error_detected,
1133 .slot_reset = mhi_pci_slot_reset,
1134 .resume = mhi_pci_io_resume,
1135 .reset_prepare = mhi_pci_reset_prepare,
1136 .reset_done = mhi_pci_reset_done,
1139 static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev)
1141 struct pci_dev *pdev = to_pci_dev(dev);
1142 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
1143 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1146 if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
1149 del_timer(&mhi_pdev->health_check_timer);
1150 cancel_work_sync(&mhi_pdev->recovery_work);
1152 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
1153 mhi_cntrl->ee != MHI_EE_AMSS)
1154 goto pci_suspend; /* Nothing to do at MHI level */
1156 /* Transition to M3 state */
1157 err = mhi_pm_suspend(mhi_cntrl);
1159 dev_err(&pdev->dev, "failed to suspend device: %d\n", err);
1160 clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status);
1165 pci_disable_device(pdev);
1166 pci_wake_from_d3(pdev, true);
1171 static int __maybe_unused mhi_pci_runtime_resume(struct device *dev)
1173 struct pci_dev *pdev = to_pci_dev(dev);
1174 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
1175 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1178 if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
1181 err = pci_enable_device(pdev);
1185 pci_set_master(pdev);
1186 pci_wake_from_d3(pdev, false);
1188 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
1189 mhi_cntrl->ee != MHI_EE_AMSS)
1190 return 0; /* Nothing to do at MHI level */
1192 /* Exit M3, transition to M0 state */
1193 err = mhi_pm_resume(mhi_cntrl);
1195 dev_err(&pdev->dev, "failed to resume device: %d\n", err);
1199 /* Resume health check */
1200 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
1202 /* It can be a remote wakeup (no mhi runtime_get), update access time */
1203 pm_runtime_mark_last_busy(dev);
1208 /* Do not fail to not mess up our PCI device state, the device likely
1209 * lost power (d3cold) and we simply need to reset it from the recovery
1210 * procedure, trigger the recovery asynchronously to prevent system
1211 * suspend exit delaying.
1213 queue_work(system_long_wq, &mhi_pdev->recovery_work);
1214 pm_runtime_mark_last_busy(dev);
1219 static int __maybe_unused mhi_pci_suspend(struct device *dev)
1221 pm_runtime_disable(dev);
1222 return mhi_pci_runtime_suspend(dev);
1225 static int __maybe_unused mhi_pci_resume(struct device *dev)
1229 /* Depending the platform, device may have lost power (d3cold), we need
1230 * to resume it now to check its state and recover when necessary.
1232 ret = mhi_pci_runtime_resume(dev);
1233 pm_runtime_enable(dev);
1238 static int __maybe_unused mhi_pci_freeze(struct device *dev)
1240 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
1241 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1243 /* We want to stop all operations, hibernation does not guarantee that
1244 * device will be in the same state as before freezing, especially if
1245 * the intermediate restore kernel reinitializes MHI device with new
1248 flush_work(&mhi_pdev->recovery_work);
1249 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
1250 mhi_power_down(mhi_cntrl, true);
1251 mhi_unprepare_after_power_down(mhi_cntrl);
1257 static int __maybe_unused mhi_pci_restore(struct device *dev)
1259 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
1261 /* Reinitialize the device */
1262 queue_work(system_long_wq, &mhi_pdev->recovery_work);
1267 static const struct dev_pm_ops mhi_pci_pm_ops = {
1268 SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL)
1269 #ifdef CONFIG_PM_SLEEP
1270 .suspend = mhi_pci_suspend,
1271 .resume = mhi_pci_resume,
1272 .freeze = mhi_pci_freeze,
1273 .thaw = mhi_pci_restore,
1274 .poweroff = mhi_pci_freeze,
1275 .restore = mhi_pci_restore,
1279 static struct pci_driver mhi_pci_driver = {
1280 .name = "mhi-pci-generic",
1281 .id_table = mhi_pci_id_table,
1282 .probe = mhi_pci_probe,
1283 .remove = mhi_pci_remove,
1284 .shutdown = mhi_pci_shutdown,
1285 .err_handler = &mhi_pci_err_handler,
1286 .driver.pm = &mhi_pci_pm_ops
1288 module_pci_driver(mhi_pci_driver);
1290 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1291 MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver");
1292 MODULE_LICENSE("GPL");