Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * MHI PCI driver - MHI over PCI controller driver |
| 4 | * |
| 5 | * This module is a generic driver for registering MHI-over-PCI devices, |
| 6 | * such as PCIe QCOM modems. |
| 7 | * |
| 8 | * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org> |
| 9 | */ |
| 10 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 11 | #include <linux/aer.h> |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 12 | #include <linux/delay.h> |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/mhi.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/pci.h> |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 18 | #include <linux/timer.h> |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 20 | |
| 21 | #define MHI_PCI_DEFAULT_BAR_NUM 0 |
| 22 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 23 | #define MHI_POST_RESET_DELAY_MS 500 |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 24 | |
| 25 | #define HEALTH_CHECK_PERIOD (HZ * 2) |
| 26 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 27 | /** |
| 28 | * struct mhi_pci_dev_info - MHI PCI device specific information |
| 29 | * @config: MHI controller configuration |
| 30 | * @name: name of the PCI module |
| 31 | * @fw: firmware path (if any) |
| 32 | * @edl: emergency download mode firmware path (if any) |
| 33 | * @bar_num: PCI base address register to use for MHI MMIO register space |
| 34 | * @dma_data_width: DMA transfer word size (32 or 64 bits) |
| 35 | */ |
| 36 | struct mhi_pci_dev_info { |
| 37 | const struct mhi_controller_config *config; |
| 38 | const char *name; |
| 39 | const char *fw; |
| 40 | const char *edl; |
| 41 | unsigned int bar_num; |
| 42 | unsigned int dma_data_width; |
| 43 | }; |
| 44 | |
| 45 | #define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \ |
| 46 | { \ |
| 47 | .num = ch_num, \ |
| 48 | .name = ch_name, \ |
| 49 | .num_elements = el_count, \ |
| 50 | .event_ring = ev_ring, \ |
| 51 | .dir = DMA_TO_DEVICE, \ |
| 52 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 53 | .pollcfg = 0, \ |
| 54 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 55 | .lpm_notify = false, \ |
| 56 | .offload_channel = false, \ |
| 57 | .doorbell_mode_switch = false, \ |
| 58 | } \ |
| 59 | |
| 60 | #define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \ |
| 61 | { \ |
| 62 | .num = ch_num, \ |
| 63 | .name = ch_name, \ |
| 64 | .num_elements = el_count, \ |
| 65 | .event_ring = ev_ring, \ |
| 66 | .dir = DMA_FROM_DEVICE, \ |
| 67 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 68 | .pollcfg = 0, \ |
| 69 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 70 | .lpm_notify = false, \ |
| 71 | .offload_channel = false, \ |
| 72 | .doorbell_mode_switch = false, \ |
| 73 | } |
| 74 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 75 | #define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 76 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 77 | .num_elements = el_count, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 78 | .irq_moderation_ms = 0, \ |
| 79 | .irq = (ev_ring) + 1, \ |
| 80 | .priority = 1, \ |
| 81 | .mode = MHI_DB_BRST_DISABLE, \ |
| 82 | .data_type = MHI_ER_CTRL, \ |
| 83 | .hardware_event = false, \ |
| 84 | .client_managed = false, \ |
| 85 | .offload_channel = false, \ |
| 86 | } |
| 87 | |
Loic Poulain | eb96787 | 2021-01-04 17:14:52 +0100 | [diff] [blame] | 88 | #define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \ |
| 89 | { \ |
| 90 | .num = ch_num, \ |
| 91 | .name = ch_name, \ |
| 92 | .num_elements = el_count, \ |
| 93 | .event_ring = ev_ring, \ |
| 94 | .dir = DMA_TO_DEVICE, \ |
| 95 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 96 | .pollcfg = 0, \ |
| 97 | .doorbell = MHI_DB_BRST_ENABLE, \ |
| 98 | .lpm_notify = false, \ |
| 99 | .offload_channel = false, \ |
| 100 | .doorbell_mode_switch = true, \ |
| 101 | } \ |
| 102 | |
| 103 | #define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \ |
| 104 | { \ |
| 105 | .num = ch_num, \ |
| 106 | .name = ch_name, \ |
| 107 | .num_elements = el_count, \ |
| 108 | .event_ring = ev_ring, \ |
| 109 | .dir = DMA_FROM_DEVICE, \ |
| 110 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 111 | .pollcfg = 0, \ |
| 112 | .doorbell = MHI_DB_BRST_ENABLE, \ |
| 113 | .lpm_notify = false, \ |
| 114 | .offload_channel = false, \ |
| 115 | .doorbell_mode_switch = true, \ |
| 116 | } |
| 117 | |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 118 | #define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \ |
| 119 | { \ |
| 120 | .num = ch_num, \ |
| 121 | .name = ch_name, \ |
| 122 | .num_elements = el_count, \ |
| 123 | .event_ring = ev_ring, \ |
| 124 | .dir = DMA_TO_DEVICE, \ |
| 125 | .ee_mask = BIT(MHI_EE_SBL), \ |
| 126 | .pollcfg = 0, \ |
| 127 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 128 | .lpm_notify = false, \ |
| 129 | .offload_channel = false, \ |
| 130 | .doorbell_mode_switch = false, \ |
| 131 | } \ |
| 132 | |
| 133 | #define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \ |
| 134 | { \ |
| 135 | .num = ch_num, \ |
| 136 | .name = ch_name, \ |
| 137 | .num_elements = el_count, \ |
| 138 | .event_ring = ev_ring, \ |
| 139 | .dir = DMA_FROM_DEVICE, \ |
| 140 | .ee_mask = BIT(MHI_EE_SBL), \ |
| 141 | .pollcfg = 0, \ |
| 142 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 143 | .lpm_notify = false, \ |
| 144 | .offload_channel = false, \ |
| 145 | .doorbell_mode_switch = false, \ |
| 146 | } |
| 147 | |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame] | 148 | #define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \ |
| 149 | { \ |
| 150 | .num = ch_num, \ |
| 151 | .name = ch_name, \ |
| 152 | .num_elements = el_count, \ |
| 153 | .event_ring = ev_ring, \ |
| 154 | .dir = DMA_TO_DEVICE, \ |
| 155 | .ee_mask = BIT(MHI_EE_FP), \ |
| 156 | .pollcfg = 0, \ |
| 157 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 158 | .lpm_notify = false, \ |
| 159 | .offload_channel = false, \ |
| 160 | .doorbell_mode_switch = false, \ |
| 161 | } \ |
| 162 | |
| 163 | #define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \ |
| 164 | { \ |
| 165 | .num = ch_num, \ |
| 166 | .name = ch_name, \ |
| 167 | .num_elements = el_count, \ |
| 168 | .event_ring = ev_ring, \ |
| 169 | .dir = DMA_FROM_DEVICE, \ |
| 170 | .ee_mask = BIT(MHI_EE_FP), \ |
| 171 | .pollcfg = 0, \ |
| 172 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 173 | .lpm_notify = false, \ |
| 174 | .offload_channel = false, \ |
| 175 | .doorbell_mode_switch = false, \ |
| 176 | } |
| 177 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 178 | #define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 179 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 180 | .num_elements = el_count, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 181 | .irq_moderation_ms = 5, \ |
| 182 | .irq = (ev_ring) + 1, \ |
| 183 | .priority = 1, \ |
| 184 | .mode = MHI_DB_BRST_DISABLE, \ |
| 185 | .data_type = MHI_ER_DATA, \ |
| 186 | .hardware_event = false, \ |
| 187 | .client_managed = false, \ |
| 188 | .offload_channel = false, \ |
| 189 | } |
| 190 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 191 | #define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 192 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 193 | .num_elements = el_count, \ |
Loic Poulain | ec751369 | 2021-01-04 17:14:59 +0100 | [diff] [blame] | 194 | .irq_moderation_ms = 1, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 195 | .irq = (ev_ring) + 1, \ |
| 196 | .priority = 1, \ |
| 197 | .mode = MHI_DB_BRST_DISABLE, \ |
| 198 | .data_type = MHI_ER_DATA, \ |
| 199 | .hardware_event = true, \ |
| 200 | .client_managed = false, \ |
| 201 | .offload_channel = false, \ |
| 202 | .channel = ch_num, \ |
| 203 | } |
| 204 | |
| 205 | static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = { |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 206 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1), |
| 207 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 208 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0), |
| 209 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0), |
| 210 | MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0), |
| 211 | MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0), |
| 212 | MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0), |
| 213 | MHI_CHANNEL_CONFIG_DL(21, "IPCR", 8, 0), |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame] | 214 | MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0), |
| 215 | MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0), |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 216 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2), |
| 217 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 218 | }; |
| 219 | |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 220 | static struct mhi_event_config modem_qcom_v1_mhi_events[] = { |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 221 | /* first ring is control+data ring */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 222 | MHI_EVENT_CONFIG_CTRL(0, 64), |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 223 | /* DIAG dedicated event ring */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 224 | MHI_EVENT_CONFIG_DATA(1, 128), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 225 | /* Hardware channels request dedicated hardware event rings */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 226 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 227 | MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 228 | }; |
| 229 | |
Manivannan Sadhasivam | 5630c10 | 2021-04-08 19:15:29 +0530 | [diff] [blame^] | 230 | static const struct mhi_controller_config modem_qcom_v1_mhiv_config = { |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 231 | .max_channels = 128, |
Loic Poulain | 84026a5 | 2021-01-04 17:14:57 +0100 | [diff] [blame] | 232 | .timeout_ms = 8000, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 233 | .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels), |
| 234 | .ch_cfg = modem_qcom_v1_mhi_channels, |
| 235 | .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events), |
| 236 | .event_cfg = modem_qcom_v1_mhi_events, |
| 237 | }; |
| 238 | |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 239 | static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = { |
| 240 | .name = "qcom-sdx65m", |
| 241 | .fw = "qcom/sdx65m/xbl.elf", |
| 242 | .edl = "qcom/sdx65m/edl.mbn", |
| 243 | .config = &modem_qcom_v1_mhiv_config, |
| 244 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 245 | .dma_data_width = 32 |
| 246 | }; |
| 247 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 248 | static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = { |
| 249 | .name = "qcom-sdx55m", |
| 250 | .fw = "qcom/sdx55m/sbl1.mbn", |
| 251 | .edl = "qcom/sdx55m/edl.mbn", |
| 252 | .config = &modem_qcom_v1_mhiv_config, |
| 253 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 254 | .dma_data_width = 32 |
| 255 | }; |
| 256 | |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 257 | static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = { |
| 258 | .name = "qcom-sdx24", |
| 259 | .edl = "qcom/prog_firehose_sdx24.mbn", |
| 260 | .config = &modem_qcom_v1_mhiv_config, |
| 261 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 262 | .dma_data_width = 32 |
| 263 | }; |
| 264 | |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 265 | static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = { |
| 266 | MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0), |
| 267 | MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0), |
| 268 | MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0), |
| 269 | MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0), |
| 270 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1), |
| 271 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1), |
| 272 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0), |
| 273 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0), |
| 274 | MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0), |
| 275 | MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0), |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame] | 276 | /* The EDL firmware is a flash-programmer exposing firehose protocol */ |
| 277 | MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0), |
| 278 | MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0), |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 279 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2), |
| 280 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3), |
| 281 | }; |
| 282 | |
| 283 | static struct mhi_event_config mhi_quectel_em1xx_events[] = { |
| 284 | MHI_EVENT_CONFIG_CTRL(0, 128), |
| 285 | MHI_EVENT_CONFIG_DATA(1, 128), |
| 286 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 287 | MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101) |
| 288 | }; |
| 289 | |
Manivannan Sadhasivam | 5630c10 | 2021-04-08 19:15:29 +0530 | [diff] [blame^] | 290 | static const struct mhi_controller_config modem_quectel_em1xx_config = { |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 291 | .max_channels = 128, |
| 292 | .timeout_ms = 20000, |
| 293 | .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels), |
| 294 | .ch_cfg = mhi_quectel_em1xx_channels, |
| 295 | .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events), |
| 296 | .event_cfg = mhi_quectel_em1xx_events, |
| 297 | }; |
| 298 | |
| 299 | static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = { |
| 300 | .name = "quectel-em1xx", |
| 301 | .edl = "qcom/prog_firehose_sdx24.mbn", |
| 302 | .config = &modem_quectel_em1xx_config, |
| 303 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 304 | .dma_data_width = 32 |
| 305 | }; |
| 306 | |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 307 | static const struct mhi_channel_config mhi_foxconn_sdx55_channels[] = { |
| 308 | MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 32, 0), |
| 309 | MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 32, 0), |
| 310 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1), |
| 311 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1), |
| 312 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0), |
| 313 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0), |
| 314 | MHI_CHANNEL_CONFIG_UL(32, "AT", 32, 0), |
| 315 | MHI_CHANNEL_CONFIG_DL(33, "AT", 32, 0), |
| 316 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2), |
| 317 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3), |
| 318 | }; |
| 319 | |
| 320 | static struct mhi_event_config mhi_foxconn_sdx55_events[] = { |
| 321 | MHI_EVENT_CONFIG_CTRL(0, 128), |
| 322 | MHI_EVENT_CONFIG_DATA(1, 128), |
| 323 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 324 | MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101) |
| 325 | }; |
| 326 | |
Manivannan Sadhasivam | 5630c10 | 2021-04-08 19:15:29 +0530 | [diff] [blame^] | 327 | static const struct mhi_controller_config modem_foxconn_sdx55_config = { |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 328 | .max_channels = 128, |
| 329 | .timeout_ms = 20000, |
| 330 | .num_channels = ARRAY_SIZE(mhi_foxconn_sdx55_channels), |
| 331 | .ch_cfg = mhi_foxconn_sdx55_channels, |
| 332 | .num_events = ARRAY_SIZE(mhi_foxconn_sdx55_events), |
| 333 | .event_cfg = mhi_foxconn_sdx55_events, |
| 334 | }; |
| 335 | |
| 336 | static const struct mhi_pci_dev_info mhi_foxconn_sdx55_info = { |
| 337 | .name = "foxconn-sdx55", |
| 338 | .fw = "qcom/sdx55m/sbl1.mbn", |
| 339 | .edl = "qcom/sdx55m/edl.mbn", |
| 340 | .config = &modem_foxconn_sdx55_config, |
| 341 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 342 | .dma_data_width = 32 |
| 343 | }; |
| 344 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 345 | static const struct pci_device_id mhi_pci_id_table[] = { |
| 346 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306), |
| 347 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info }, |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 348 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304), |
| 349 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info }, |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 350 | { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */ |
| 351 | .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info }, |
| 352 | { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */ |
| 353 | .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info }, |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 354 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308), |
| 355 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info }, |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 356 | /* T99W175 (sdx55), Both for eSIM and Non-eSIM */ |
| 357 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0ab), |
| 358 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
| 359 | /* DW5930e (sdx55), With eSIM, It's also T99W175 */ |
| 360 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b0), |
| 361 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
| 362 | /* DW5930e (sdx55), Non-eSIM, It's also T99W175 */ |
| 363 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b1), |
| 364 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 365 | { } |
| 366 | }; |
| 367 | MODULE_DEVICE_TABLE(pci, mhi_pci_id_table); |
| 368 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 369 | enum mhi_pci_device_status { |
| 370 | MHI_PCI_DEV_STARTED, |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 371 | MHI_PCI_DEV_SUSPENDED, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | struct mhi_pci_device { |
| 375 | struct mhi_controller mhi_cntrl; |
| 376 | struct pci_saved_state *pci_state; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 377 | struct work_struct recovery_work; |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 378 | struct timer_list health_check_timer; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 379 | unsigned long status; |
| 380 | }; |
| 381 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 382 | static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl, |
| 383 | void __iomem *addr, u32 *out) |
| 384 | { |
| 385 | *out = readl(addr); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl, |
| 390 | void __iomem *addr, u32 val) |
| 391 | { |
| 392 | writel(val, addr); |
| 393 | } |
| 394 | |
| 395 | static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl, |
| 396 | enum mhi_callback cb) |
| 397 | { |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 398 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 399 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 400 | /* Nothing to do for now */ |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 401 | switch (cb) { |
| 402 | case MHI_CB_FATAL_ERROR: |
| 403 | case MHI_CB_SYS_ERROR: |
| 404 | dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 405 | pm_runtime_forbid(&pdev->dev); |
| 406 | break; |
| 407 | case MHI_CB_EE_MISSION_MODE: |
| 408 | pm_runtime_allow(&pdev->dev); |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 409 | break; |
| 410 | default: |
| 411 | break; |
| 412 | } |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Loic Poulain | e3e5e650 | 2021-03-05 20:16:46 +0100 | [diff] [blame] | 415 | static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force) |
| 416 | { |
| 417 | /* no-op */ |
| 418 | } |
| 419 | |
| 420 | static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override) |
| 421 | { |
| 422 | /* no-op */ |
| 423 | } |
| 424 | |
| 425 | static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl) |
| 426 | { |
| 427 | /* no-op */ |
| 428 | } |
| 429 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 430 | static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl) |
| 431 | { |
| 432 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 433 | u16 vendor = 0; |
| 434 | |
| 435 | if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor)) |
| 436 | return false; |
| 437 | |
| 438 | if (vendor == (u16) ~0 || vendor == 0) |
| 439 | return false; |
| 440 | |
| 441 | return true; |
| 442 | } |
| 443 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 444 | static int mhi_pci_claim(struct mhi_controller *mhi_cntrl, |
| 445 | unsigned int bar_num, u64 dma_mask) |
| 446 | { |
| 447 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 448 | int err; |
| 449 | |
| 450 | err = pci_assign_resource(pdev, bar_num); |
| 451 | if (err) |
| 452 | return err; |
| 453 | |
| 454 | err = pcim_enable_device(pdev); |
| 455 | if (err) { |
| 456 | dev_err(&pdev->dev, "failed to enable pci device: %d\n", err); |
| 457 | return err; |
| 458 | } |
| 459 | |
| 460 | err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev)); |
| 461 | if (err) { |
| 462 | dev_err(&pdev->dev, "failed to map pci region: %d\n", err); |
| 463 | return err; |
| 464 | } |
| 465 | mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num]; |
| 466 | |
| 467 | err = pci_set_dma_mask(pdev, dma_mask); |
| 468 | if (err) { |
| 469 | dev_err(&pdev->dev, "Cannot set proper DMA mask\n"); |
| 470 | return err; |
| 471 | } |
| 472 | |
| 473 | err = pci_set_consistent_dma_mask(pdev, dma_mask); |
| 474 | if (err) { |
| 475 | dev_err(&pdev->dev, "set consistent dma mask failed\n"); |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | pci_set_master(pdev); |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl, |
| 485 | const struct mhi_controller_config *mhi_cntrl_config) |
| 486 | { |
| 487 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 488 | int nr_vectors, i; |
| 489 | int *irq; |
| 490 | |
| 491 | /* |
| 492 | * Alloc one MSI vector for BHI + one vector per event ring, ideally... |
| 493 | * No explicit pci_free_irq_vectors required, done by pcim_release. |
| 494 | */ |
| 495 | mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events; |
| 496 | |
| 497 | nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI); |
| 498 | if (nr_vectors < 0) { |
| 499 | dev_err(&pdev->dev, "Error allocating MSI vectors %d\n", |
| 500 | nr_vectors); |
| 501 | return nr_vectors; |
| 502 | } |
| 503 | |
| 504 | if (nr_vectors < mhi_cntrl->nr_irqs) { |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 505 | dev_warn(&pdev->dev, "using shared MSI\n"); |
| 506 | |
| 507 | /* Patch msi vectors, use only one (shared) */ |
| 508 | for (i = 0; i < mhi_cntrl_config->num_events; i++) |
| 509 | mhi_cntrl_config->event_cfg[i].irq = 0; |
| 510 | mhi_cntrl->nr_irqs = 1; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL); |
| 514 | if (!irq) |
| 515 | return -ENOMEM; |
| 516 | |
| 517 | for (i = 0; i < mhi_cntrl->nr_irqs; i++) { |
| 518 | int vector = i >= nr_vectors ? (nr_vectors - 1) : i; |
| 519 | |
| 520 | irq[i] = pci_irq_vector(pdev, vector); |
| 521 | } |
| 522 | |
| 523 | mhi_cntrl->irq = irq; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl) |
| 529 | { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 530 | /* The runtime_get() MHI callback means: |
| 531 | * Do whatever is requested to leave M3. |
| 532 | */ |
| 533 | return pm_runtime_get(mhi_cntrl->cntrl_dev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl) |
| 537 | { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 538 | /* The runtime_put() MHI callback means: |
| 539 | * Device can be moved in M3 state. |
| 540 | */ |
| 541 | pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev); |
| 542 | pm_runtime_put(mhi_cntrl->cntrl_dev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 545 | static void mhi_pci_recovery_work(struct work_struct *work) |
| 546 | { |
| 547 | struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device, |
| 548 | recovery_work); |
| 549 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 550 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 551 | int err; |
| 552 | |
| 553 | dev_warn(&pdev->dev, "device recovery started\n"); |
| 554 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 555 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 556 | pm_runtime_forbid(&pdev->dev); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 557 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 558 | /* Clean up MHI state */ |
| 559 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 560 | mhi_power_down(mhi_cntrl, false); |
| 561 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 562 | } |
| 563 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 564 | pci_set_power_state(pdev, PCI_D0); |
| 565 | pci_load_saved_state(pdev, mhi_pdev->pci_state); |
| 566 | pci_restore_state(pdev); |
| 567 | |
| 568 | if (!mhi_pci_is_alive(mhi_cntrl)) |
| 569 | goto err_try_reset; |
| 570 | |
| 571 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 572 | if (err) |
| 573 | goto err_try_reset; |
| 574 | |
| 575 | err = mhi_sync_power_up(mhi_cntrl); |
| 576 | if (err) |
| 577 | goto err_unprepare; |
| 578 | |
| 579 | dev_dbg(&pdev->dev, "Recovery completed\n"); |
| 580 | |
| 581 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 582 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 583 | return; |
| 584 | |
| 585 | err_unprepare: |
| 586 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 587 | err_try_reset: |
| 588 | if (pci_reset_function(pdev)) |
| 589 | dev_err(&pdev->dev, "Recovery failed\n"); |
| 590 | } |
| 591 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 592 | static void health_check(struct timer_list *t) |
| 593 | { |
| 594 | struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer); |
| 595 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 596 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 597 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 598 | test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 599 | return; |
| 600 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 601 | if (!mhi_pci_is_alive(mhi_cntrl)) { |
| 602 | dev_err(mhi_cntrl->cntrl_dev, "Device died\n"); |
| 603 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | /* reschedule in two seconds */ |
| 608 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 609 | } |
| 610 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 611 | static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 612 | { |
| 613 | const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data; |
| 614 | const struct mhi_controller_config *mhi_cntrl_config; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 615 | struct mhi_pci_device *mhi_pdev; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 616 | struct mhi_controller *mhi_cntrl; |
| 617 | int err; |
| 618 | |
| 619 | dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name); |
| 620 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 621 | /* mhi_pdev.mhi_cntrl must be zero-initialized */ |
| 622 | mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL); |
| 623 | if (!mhi_pdev) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 624 | return -ENOMEM; |
| 625 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 626 | INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 627 | timer_setup(&mhi_pdev->health_check_timer, health_check, 0); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 628 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 629 | mhi_cntrl_config = info->config; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 630 | mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 631 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 632 | mhi_cntrl->cntrl_dev = &pdev->dev; |
| 633 | mhi_cntrl->iova_start = 0; |
Loic Poulain | 4ea6fa2 | 2020-12-02 09:12:30 +0100 | [diff] [blame] | 634 | mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 635 | mhi_cntrl->fw_image = info->fw; |
| 636 | mhi_cntrl->edl_image = info->edl; |
| 637 | |
| 638 | mhi_cntrl->read_reg = mhi_pci_read_reg; |
| 639 | mhi_cntrl->write_reg = mhi_pci_write_reg; |
| 640 | mhi_cntrl->status_cb = mhi_pci_status_cb; |
| 641 | mhi_cntrl->runtime_get = mhi_pci_runtime_get; |
| 642 | mhi_cntrl->runtime_put = mhi_pci_runtime_put; |
Loic Poulain | e3e5e650 | 2021-03-05 20:16:46 +0100 | [diff] [blame] | 643 | mhi_cntrl->wake_get = mhi_pci_wake_get_nop; |
| 644 | mhi_cntrl->wake_put = mhi_pci_wake_put_nop; |
| 645 | mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 646 | |
| 647 | err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width)); |
| 648 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 649 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 650 | |
| 651 | err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config); |
| 652 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 653 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 654 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 655 | pci_set_drvdata(pdev, mhi_pdev); |
| 656 | |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 657 | /* Have stored pci confspace at hand for restore in sudden PCI error. |
| 658 | * cache the state locally and discard the PCI core one. |
| 659 | */ |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 660 | pci_save_state(pdev); |
| 661 | mhi_pdev->pci_state = pci_store_saved_state(pdev); |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 662 | pci_load_saved_state(pdev, NULL); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 663 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 664 | pci_enable_pcie_error_reporting(pdev); |
| 665 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 666 | err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config); |
| 667 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 668 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 669 | |
| 670 | /* MHI bus does not power up the controller by default */ |
| 671 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 672 | if (err) { |
| 673 | dev_err(&pdev->dev, "failed to prepare MHI controller\n"); |
| 674 | goto err_unregister; |
| 675 | } |
| 676 | |
| 677 | err = mhi_sync_power_up(mhi_cntrl); |
| 678 | if (err) { |
| 679 | dev_err(&pdev->dev, "failed to power up MHI controller\n"); |
| 680 | goto err_unprepare; |
| 681 | } |
| 682 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 683 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
| 684 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 685 | /* start health check */ |
| 686 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 687 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 688 | /* Only allow runtime-suspend if PME capable (for wakeup) */ |
| 689 | if (pci_pme_capable(pdev, PCI_D3hot)) { |
| 690 | pm_runtime_set_autosuspend_delay(&pdev->dev, 2000); |
| 691 | pm_runtime_use_autosuspend(&pdev->dev); |
| 692 | pm_runtime_mark_last_busy(&pdev->dev); |
| 693 | pm_runtime_put_noidle(&pdev->dev); |
| 694 | } |
| 695 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 696 | return 0; |
| 697 | |
| 698 | err_unprepare: |
| 699 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 700 | err_unregister: |
| 701 | mhi_unregister_controller(mhi_cntrl); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 702 | |
| 703 | return err; |
| 704 | } |
| 705 | |
| 706 | static void mhi_pci_remove(struct pci_dev *pdev) |
| 707 | { |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 708 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 709 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 710 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 711 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 712 | cancel_work_sync(&mhi_pdev->recovery_work); |
| 713 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 714 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 715 | mhi_power_down(mhi_cntrl, true); |
| 716 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 717 | } |
| 718 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 719 | /* balancing probe put_noidle */ |
| 720 | if (pci_pme_capable(pdev, PCI_D3hot)) |
| 721 | pm_runtime_get_noresume(&pdev->dev); |
| 722 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 723 | mhi_unregister_controller(mhi_cntrl); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Loic Poulain | 757072a | 2021-03-19 16:50:37 +0100 | [diff] [blame] | 726 | static void mhi_pci_shutdown(struct pci_dev *pdev) |
| 727 | { |
| 728 | mhi_pci_remove(pdev); |
| 729 | pci_set_power_state(pdev, PCI_D3hot); |
| 730 | } |
| 731 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 732 | static void mhi_pci_reset_prepare(struct pci_dev *pdev) |
| 733 | { |
| 734 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 735 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 736 | |
| 737 | dev_info(&pdev->dev, "reset\n"); |
| 738 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 739 | del_timer(&mhi_pdev->health_check_timer); |
| 740 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 741 | /* Clean up MHI state */ |
| 742 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 743 | mhi_power_down(mhi_cntrl, false); |
| 744 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 745 | } |
| 746 | |
| 747 | /* cause internal device reset */ |
| 748 | mhi_soc_reset(mhi_cntrl); |
| 749 | |
| 750 | /* Be sure device reset has been executed */ |
| 751 | msleep(MHI_POST_RESET_DELAY_MS); |
| 752 | } |
| 753 | |
| 754 | static void mhi_pci_reset_done(struct pci_dev *pdev) |
| 755 | { |
| 756 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 757 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 758 | int err; |
| 759 | |
| 760 | /* Restore initial known working PCI state */ |
| 761 | pci_load_saved_state(pdev, mhi_pdev->pci_state); |
| 762 | pci_restore_state(pdev); |
| 763 | |
| 764 | /* Is device status available ? */ |
| 765 | if (!mhi_pci_is_alive(mhi_cntrl)) { |
| 766 | dev_err(&pdev->dev, "reset failed\n"); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 771 | if (err) { |
| 772 | dev_err(&pdev->dev, "failed to prepare MHI controller\n"); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | err = mhi_sync_power_up(mhi_cntrl); |
| 777 | if (err) { |
| 778 | dev_err(&pdev->dev, "failed to power up MHI controller\n"); |
| 779 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 784 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 787 | static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev, |
| 788 | pci_channel_state_t state) |
| 789 | { |
| 790 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 791 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 792 | |
| 793 | dev_err(&pdev->dev, "PCI error detected, state = %u\n", state); |
| 794 | |
| 795 | if (state == pci_channel_io_perm_failure) |
| 796 | return PCI_ERS_RESULT_DISCONNECT; |
| 797 | |
| 798 | /* Clean up MHI state */ |
| 799 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 800 | mhi_power_down(mhi_cntrl, false); |
| 801 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 802 | } else { |
| 803 | /* Nothing to do */ |
| 804 | return PCI_ERS_RESULT_RECOVERED; |
| 805 | } |
| 806 | |
| 807 | pci_disable_device(pdev); |
| 808 | |
| 809 | return PCI_ERS_RESULT_NEED_RESET; |
| 810 | } |
| 811 | |
| 812 | static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev) |
| 813 | { |
| 814 | if (pci_enable_device(pdev)) { |
| 815 | dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n"); |
| 816 | return PCI_ERS_RESULT_DISCONNECT; |
| 817 | } |
| 818 | |
| 819 | return PCI_ERS_RESULT_RECOVERED; |
| 820 | } |
| 821 | |
| 822 | static void mhi_pci_io_resume(struct pci_dev *pdev) |
| 823 | { |
| 824 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 825 | |
| 826 | dev_err(&pdev->dev, "PCI slot reset done\n"); |
| 827 | |
| 828 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 829 | } |
| 830 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 831 | static const struct pci_error_handlers mhi_pci_err_handler = { |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 832 | .error_detected = mhi_pci_error_detected, |
| 833 | .slot_reset = mhi_pci_slot_reset, |
| 834 | .resume = mhi_pci_io_resume, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 835 | .reset_prepare = mhi_pci_reset_prepare, |
| 836 | .reset_done = mhi_pci_reset_done, |
| 837 | }; |
| 838 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 839 | static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 840 | { |
| 841 | struct pci_dev *pdev = to_pci_dev(dev); |
| 842 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 843 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 844 | int err; |
| 845 | |
| 846 | if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 847 | return 0; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 848 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 849 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 850 | cancel_work_sync(&mhi_pdev->recovery_work); |
| 851 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 852 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 853 | mhi_cntrl->ee != MHI_EE_AMSS) |
| 854 | goto pci_suspend; /* Nothing to do at MHI level */ |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 855 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 856 | /* Transition to M3 state */ |
| 857 | err = mhi_pm_suspend(mhi_cntrl); |
| 858 | if (err) { |
| 859 | dev_err(&pdev->dev, "failed to suspend device: %d\n", err); |
| 860 | clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status); |
| 861 | return -EBUSY; |
| 862 | } |
| 863 | |
| 864 | pci_suspend: |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 865 | pci_disable_device(pdev); |
| 866 | pci_wake_from_d3(pdev, true); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 871 | static int __maybe_unused mhi_pci_runtime_resume(struct device *dev) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 872 | { |
| 873 | struct pci_dev *pdev = to_pci_dev(dev); |
| 874 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 875 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 876 | int err; |
| 877 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 878 | if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 879 | return 0; |
| 880 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 881 | err = pci_enable_device(pdev); |
| 882 | if (err) |
| 883 | goto err_recovery; |
| 884 | |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 885 | pci_set_master(pdev); |
| 886 | pci_wake_from_d3(pdev, false); |
| 887 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 888 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 889 | mhi_cntrl->ee != MHI_EE_AMSS) |
| 890 | return 0; /* Nothing to do at MHI level */ |
| 891 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 892 | /* Exit M3, transition to M0 state */ |
| 893 | err = mhi_pm_resume(mhi_cntrl); |
| 894 | if (err) { |
| 895 | dev_err(&pdev->dev, "failed to resume device: %d\n", err); |
| 896 | goto err_recovery; |
| 897 | } |
| 898 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 899 | /* Resume health check */ |
| 900 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 901 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 902 | /* It can be a remote wakeup (no mhi runtime_get), update access time */ |
| 903 | pm_runtime_mark_last_busy(dev); |
| 904 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 905 | return 0; |
| 906 | |
| 907 | err_recovery: |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 908 | /* Do not fail to not mess up our PCI device state, the device likely |
| 909 | * lost power (d3cold) and we simply need to reset it from the recovery |
| 910 | * procedure, trigger the recovery asynchronously to prevent system |
| 911 | * suspend exit delaying. |
| 912 | */ |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 913 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 914 | pm_runtime_mark_last_busy(dev); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 915 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | static int __maybe_unused mhi_pci_suspend(struct device *dev) |
| 920 | { |
| 921 | pm_runtime_disable(dev); |
| 922 | return mhi_pci_runtime_suspend(dev); |
| 923 | } |
| 924 | |
| 925 | static int __maybe_unused mhi_pci_resume(struct device *dev) |
| 926 | { |
| 927 | int ret; |
| 928 | |
| 929 | /* Depending the platform, device may have lost power (d3cold), we need |
| 930 | * to resume it now to check its state and recover when necessary. |
| 931 | */ |
| 932 | ret = mhi_pci_runtime_resume(dev); |
| 933 | pm_runtime_enable(dev); |
| 934 | |
| 935 | return ret; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static const struct dev_pm_ops mhi_pci_pm_ops = { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 939 | SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 940 | SET_SYSTEM_SLEEP_PM_OPS(mhi_pci_suspend, mhi_pci_resume) |
| 941 | }; |
| 942 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 943 | static struct pci_driver mhi_pci_driver = { |
| 944 | .name = "mhi-pci-generic", |
| 945 | .id_table = mhi_pci_id_table, |
| 946 | .probe = mhi_pci_probe, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 947 | .remove = mhi_pci_remove, |
Loic Poulain | 757072a | 2021-03-19 16:50:37 +0100 | [diff] [blame] | 948 | .shutdown = mhi_pci_shutdown, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 949 | .err_handler = &mhi_pci_err_handler, |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 950 | .driver.pm = &mhi_pci_pm_ops |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 951 | }; |
| 952 | module_pci_driver(mhi_pci_driver); |
| 953 | |
| 954 | MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>"); |
| 955 | MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver"); |
| 956 | MODULE_LICENSE("GPL"); |