1 /*
2 * Copyright (c) 2013-2020 The Linux Foundation. All rights reserved.
3 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for
6 * any purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/sdio_func.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/sd.h>
27 #include <linux/version.h>
28 #include <linux/module.h>
29 #include <qdf_atomic.h>
30 #include <cds_utils.h>
31 #include <qdf_timer.h>
32 #include <cds_api.h>
33 #include <qdf_time.h>
34 #include "hif_sdio_dev.h"
35 #include "if_sdio.h"
36 #include "regtable_sdio.h"
37 #include "wma_api.h"
38 #include "hif_internal.h"
39 #include <transfer/transfer.h>
40
41 #define HIF_USE_DMA_BOUNCE_BUFFER 1
42 #define ATH_MODULE_NAME hif
43 #include "a_debug.h"
44
45 #define MAX_HIF_DEVICES 2
46 #ifdef HIF_MBOX_SLEEP_WAR
47 #define HIF_MIN_SLEEP_INACTIVITY_TIME_MS 50
48 #define HIF_SLEEP_DISABLE_UPDATE_DELAY 1
49 #define HIF_IS_WRITE_REQUEST_MBOX1_TO_3(request) \
50 ((request->request & HIF_SDIO_WRITE) && \
51 (request->address >= 0x1000 && \
52 request->address < 0x1FFFF))
53 #endif
54 unsigned int forcesleepmode;
55 module_param(forcesleepmode, uint, 0644);
56 MODULE_PARM_DESC(forcesleepmode,
57 "Set sleep mode: 0-host capbility, 1-force WOW, 2-force DeepSleep, 3-force CutPower");
58
59 unsigned int forcecard;
60 module_param(forcecard, uint, 0644);
61 MODULE_PARM_DESC(forcecard,
62 "Ignore card capabilities information to switch bus mode");
63
64 unsigned int debugcccr = 1;
65 module_param(debugcccr, uint, 0644);
66 MODULE_PARM_DESC(debugcccr, "Output this cccr values");
67
68 #define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
69 #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
70 static struct hif_sdio_dev *add_hif_device(struct hif_softc *hif_ctx,
71 struct sdio_func *func);
72 static void del_hif_device(struct hif_sdio_dev *device);
73
74 int reset_sdio_on_unload;
75 module_param(reset_sdio_on_unload, int, 0644);
76
77 uint32_t nohifscattersupport = 1;
78
79 /* ------ Static Variables ------ */
80 static const struct sdio_device_id ar6k_id_table[] = {
81 #ifdef AR6002_HEADERS_DEF
82 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6002_BASE | 0x0))},
83 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6002_BASE | 0x1))},
84 #endif
85 #ifdef AR6003_HEADERS_DEF
86 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
87 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
88 #endif
89 #ifdef AR6004_HEADERS_DEF
90 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
91 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
92 #endif
93 #ifdef AR6320_HEADERS_DEF
94 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x0))},
95 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x1))},
96 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x2))},
97 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x3))},
98 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x4))},
99 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x5))},
100 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x6))},
101 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x7))},
102 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x8))},
103 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0x9))},
104 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0xA))},
105 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0xB))},
106 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0xC))},
107 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0xD))},
108 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0xE))},
109 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6320_BASE | 0xF))},
110 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x0))},
111 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x1))},
112 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x2))},
113 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x3))},
114 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x4))},
115 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x5))},
116 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x6))},
117 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x7))},
118 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x8))},
119 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0x9))},
120 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0xA))},
121 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0xB))},
122 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0xC))},
123 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0xD))},
124 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0xE))},
125 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9377_BASE | 0xF))},
126 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x0))},
127 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x1))},
128 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x2))},
129 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x3))},
130 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x4))},
131 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x5))},
132 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x6))},
133 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x7))},
134 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x8))},
135 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0x9))},
136 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0xA))},
137 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0xB))},
138 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0xC))},
139 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0xD))},
140 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0xE))},
141 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_QCA9379_BASE | 0xF))},
142 {SDIO_DEVICE(MANUFACTURER_CODE, (0 | 0x0))},
143 {SDIO_DEVICE(MANUFACTURER_CODE, (0 | 0x1))},
144 #endif
145 { /* null */ },
146 };
147
148 struct hif_sdio_softc *scn;
149
150 static struct hif_sdio_dev *hif_devices[MAX_HIF_DEVICES];
151
152 #if defined(WLAN_DEBUG) || defined(DEBUG)
153 ATH_DEBUG_INSTANTIATE_MODULE_VAR(hif,
154 "hif",
155 "(Linux MMC) Host Interconnect Framework",
156 ATH_DEBUG_MASK_DEFAULTS, 0, NULL);
157 #endif
158
159 /**
160 * add_to_async_list() - add bus request to async task list
161 * @device: pointer to hif device
162 * @busrequest: pointer to type of bus request
163 *
164 * Return: None.
165 */
add_to_async_list(struct hif_sdio_dev * device,struct bus_request * busrequest)166 void add_to_async_list(struct hif_sdio_dev *device,
167 struct bus_request *busrequest)
168 {
169 struct bus_request *async;
170 struct bus_request *active;
171
172 qdf_spin_lock_irqsave(&device->asynclock);
173 active = device->asyncreq;
174 if (!active) {
175 device->asyncreq = busrequest;
176 device->asyncreq->inusenext = NULL;
177 } else {
178 for (async = device->asyncreq;
179 async; async = async->inusenext) {
180 active = async;
181 }
182 active->inusenext = busrequest;
183 busrequest->inusenext = NULL;
184 }
185 qdf_spin_unlock_irqrestore(&device->asynclock);
186 }
187
188 /*
189 * Setup IRQ mode for deep sleep and WoW
190 * Switch back to 1 bits mode when we suspend for
191 * WoW in order to detect SDIO irq without clock.
192 * Re-enable async 4-bit irq mode for some host controllers
193 * after resume.
194 */
195 #ifdef CONFIG_SDIO_TRANSFER_MAILBOX
sdio_enable4bits(struct hif_sdio_dev * device,int enable)196 static int sdio_enable4bits(struct hif_sdio_dev *device, int enable)
197 {
198 int ret = 0;
199 struct sdio_func *func = device->func;
200 struct mmc_card *card = func->card;
201 struct mmc_host *host = card->host;
202
203 if (!(host->caps & (MMC_CAP_4_BIT_DATA)))
204 return 0;
205
206 if (card->cccr.low_speed && !card->cccr.wide_bus)
207 return 0;
208
209 sdio_claim_host(func);
210 do {
211 int setAsyncIRQ = 0;
212 __u16 manufacturer_id =
213 device->id->device & MANUFACTURER_ID_AR6K_BASE_MASK;
214
215 /* Re-enable 4-bit ASYNC interrupt on AR6003x
216 * after system resume for some host controller
217 */
218 if (manufacturer_id == MANUFACTURER_ID_AR6003_BASE) {
219 setAsyncIRQ = 1;
220 ret =
221 func0_cmd52_write_byte(func->card,
222 CCCR_SDIO_IRQ_MODE_REG_AR6003,
223 enable ?
224 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_AR6003
225 : 0);
226 } else if (manufacturer_id == MANUFACTURER_ID_AR6320_BASE ||
227 manufacturer_id == MANUFACTURER_ID_QCA9377_BASE ||
228 manufacturer_id == MANUFACTURER_ID_QCA9379_BASE) {
229 unsigned char data = 0;
230
231 setAsyncIRQ = 1;
232 ret =
233 func0_cmd52_read_byte(func->card,
234 CCCR_SDIO_IRQ_MODE_REG_AR6320,
235 &data);
236 if (ret) {
237 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
238 ("%s: failed to read interrupt extension register %d\n",
239 __func__, ret));
240 sdio_release_host(func);
241 return ret;
242 }
243 if (enable)
244 data |= SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_AR6320;
245 else
246 data &= ~SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_AR6320;
247 ret =
248 func0_cmd52_write_byte(func->card,
249 CCCR_SDIO_IRQ_MODE_REG_AR6320,
250 data);
251 }
252 if (setAsyncIRQ) {
253 if (ret) {
254 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
255 ("%s: failed to setup 4-bit ASYNC IRQ mode into %d err %d\n",
256 __func__, enable, ret));
257 } else {
258 AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
259 ("%s: Setup 4-bit ASYNC IRQ mode into %d successfully\n",
260 __func__, enable));
261 }
262 }
263 } while (0);
264 sdio_release_host(func);
265
266 return ret;
267 }
268 #else
sdio_enable4bits(struct hif_sdio_dev * device,int enable)269 static int sdio_enable4bits(struct hif_sdio_dev *device, int enable)
270 {
271 return 0;
272 }
273 #endif
274
275 /**
276 * hif_sdio_probe() - configure sdio device
277 * @ol_sc: HIF device context
278 * @func: SDIO function context
279 * @device: pointer to hif handle
280 *
281 * Return: QDF_STATUS
282 */
hif_sdio_probe(struct hif_softc * ol_sc,struct sdio_func * func,struct hif_sdio_dev * device)283 QDF_STATUS hif_sdio_probe(struct hif_softc *ol_sc,
284 struct sdio_func *func,
285 struct hif_sdio_dev *device)
286 {
287 int ret = 0;
288 const struct sdio_device_id *id;
289 uint32_t target_type;
290 QDF_STATUS status = QDF_STATUS_SUCCESS;
291
292 HIF_ENTER();
293 scn = (struct hif_sdio_softc *)ol_sc;
294
295 scn->hif_handle = device;
296 spin_lock_init(&scn->target_lock);
297 /*
298 * Attach Target register table. This is needed early on
299 * even before BMI since PCI and HIF initialization
300 * directly access Target registers.
301 *
302 * TBDXXX: targetdef should not be global -- should be stored
303 * in per-device struct so that we can support multiple
304 * different Target types with a single Host driver.
305 * The whole notion of an "hif type" -- (not as in the hif
306 * module, but generic "Host Interface Type") is bizarre.
307 * At first, one one expect it to be things like SDIO, USB, PCI.
308 * But instead, it's an actual platform type. Inexplicably, the
309 * values used for HIF platform types are *different* from the
310 * values used for Target Types.
311 */
312
313 #if defined(CONFIG_AR9888_SUPPORT)
314 hif_register_tbl_attach(ol_sc, HIF_TYPE_AR9888);
315 target_register_tbl_attach(ol_sc, TARGET_TYPE_AR9888);
316 target_type = TARGET_TYPE_AR9888;
317 #elif defined(CONFIG_AR6320_SUPPORT)
318 id = device->id;
319 if (((id->device & MANUFACTURER_ID_AR6K_BASE_MASK) ==
320 MANUFACTURER_ID_QCA9377_BASE) ||
321 ((id->device & MANUFACTURER_ID_AR6K_BASE_MASK) ==
322 MANUFACTURER_ID_QCA9379_BASE)) {
323 hif_register_tbl_attach(ol_sc, HIF_TYPE_AR6320V2);
324 target_register_tbl_attach(ol_sc, TARGET_TYPE_AR6320V2);
325 } else if ((id->device & MANUFACTURER_ID_AR6K_BASE_MASK) ==
326 MANUFACTURER_ID_AR6320_BASE) {
327 int ar6kid = id->device & MANUFACTURER_ID_AR6K_REV_MASK;
328
329 if (ar6kid >= 1) {
330 /* v2 or higher silicon */
331 hif_register_tbl_attach(ol_sc, HIF_TYPE_AR6320V2);
332 target_register_tbl_attach(ol_sc, TARGET_TYPE_AR6320V2);
333 } else {
334 /* legacy v1 silicon */
335 hif_register_tbl_attach(ol_sc, HIF_TYPE_AR6320);
336 target_register_tbl_attach(ol_sc, TARGET_TYPE_AR6320);
337 }
338 }
339 target_type = TARGET_TYPE_AR6320;
340
341 #endif
342 scn->targetdef = ol_sc->targetdef;
343 scn->hostdef = ol_sc->hostdef;
344 scn->dev = &func->dev;
345 ol_sc->bus_type = QDF_BUS_TYPE_SDIO;
346 ol_sc->target_info.target_type = target_type;
347
348 scn->ramdump_base =
349 pld_hif_sdio_get_virt_ramdump_mem(&func->dev,
350 &scn->ramdump_size);
351 if (!scn->ramdump_base || !scn->ramdump_size) {
352 hf_err("Failed ramdump res alloc - base:%s, len:%lu",
353 scn->ramdump_base ? "ok" : "null",
354 scn->ramdump_size);
355 } else {
356 hif_info("ramdump base %pK size %lu",
357 scn->ramdump_base, scn->ramdump_size);
358 }
359
360 if (athdiag_procfs_init(scn) != 0) {
361 status = QDF_STATUS_E_FAILURE;
362 goto err_attach1;
363 }
364
365 ret = hif_dev_register_channels(device, func);
366
367 return qdf_status_from_os_return(ret);
368
369 err_attach1:
370 if (scn->ramdump_base)
371 pld_hif_sdio_release_ramdump_mem(scn->ramdump_base);
372 scn = NULL;
373 return status;
374 }
375
376 /**
377 * power_state_change_notify() - SDIO bus power notification handler
378 * @ol_sc: HIF device context
379 * @device: SDIO device
380 * @config: hif device power change type
381 *
382 * Return: 0 on success, error number otherwise.
383 */
384 static QDF_STATUS
power_state_change_notify(struct hif_softc * ol_sc,struct hif_sdio_dev * device,enum HIF_DEVICE_POWER_CHANGE_TYPE config)385 power_state_change_notify(struct hif_softc *ol_sc,
386 struct hif_sdio_dev *device,
387 enum HIF_DEVICE_POWER_CHANGE_TYPE config)
388 {
389 QDF_STATUS status = QDF_STATUS_SUCCESS;
390 struct sdio_func *func = device->func;
391
392 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
393 ("%s: config type %d\n",
394 __func__, config));
395 switch (config) {
396 case HIF_DEVICE_POWER_DOWN:
397 /* Disable 4bits to allow SDIO bus to detect
398 * DAT1 as interrupt source
399 */
400 sdio_enable4bits(device, 0);
401 break;
402 case HIF_DEVICE_POWER_CUT:
403 status = hif_disable_func(device, func, 1);
404 if (!device->is_suspend) {
405 device->power_config = config;
406 mmc_detect_change(device->host, HZ / 3);
407 }
408 break;
409 case HIF_DEVICE_POWER_UP:
410 if (device->power_config == HIF_DEVICE_POWER_CUT) {
411 if (device->is_suspend) {
412 status = reinit_sdio(device);
413 /* set power_config before EnableFunc to
414 * passthrough sdio r/w action when resuming
415 * from cut power
416 */
417 device->power_config = config;
418 if (status == QDF_STATUS_SUCCESS)
419 status = hif_enable_func(ol_sc, device,
420 func, true);
421 } else {
422 /* device->func is bad pointer at this time */
423 mmc_detect_change(device->host, 0);
424 return QDF_STATUS_E_PENDING;
425 }
426 } else if (device->power_config == HIF_DEVICE_POWER_DOWN) {
427 int ret = sdio_enable4bits(device, 1);
428
429 status = (ret == 0) ? QDF_STATUS_SUCCESS :
430 QDF_STATUS_E_FAILURE;
431 }
432 break;
433 }
434 device->power_config = config;
435
436 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
437 ("%s:\n", __func__));
438
439 return status;
440 }
441
442
443 /**
444 * hif_configure_device() - configure sdio device
445 * @ol_sc: HIF device context
446 * @device: pointer to hif device structure
447 * @opcode: configuration type
448 * @config: configuration value to set
449 * @config_len: configuration length
450 *
451 * Return: 0 on success, error number otherwise.
452 */
453 QDF_STATUS
hif_configure_device(struct hif_softc * ol_sc,struct hif_sdio_dev * device,enum hif_device_config_opcode opcode,void * config,uint32_t config_len)454 hif_configure_device(struct hif_softc *ol_sc, struct hif_sdio_dev *device,
455 enum hif_device_config_opcode opcode,
456 void *config, uint32_t config_len)
457 {
458 QDF_STATUS status = QDF_STATUS_SUCCESS;
459
460 switch (opcode) {
461 case HIF_DEVICE_GET_BLOCK_SIZE:
462 hif_dev_get_block_size(config);
463 break;
464
465 case HIF_DEVICE_GET_FIFO_ADDR:
466 hif_dev_get_fifo_address(device, config, config_len);
467 break;
468
469 case HIF_DEVICE_GET_PENDING_EVENTS_FUNC:
470 hif_warn("opcode %d", opcode);
471 status = QDF_STATUS_E_FAILURE;
472 break;
473 case HIF_DEVICE_GET_IRQ_PROC_MODE:
474 *((enum hif_device_irq_mode *) config) =
475 HIF_DEVICE_IRQ_SYNC_ONLY;
476 break;
477 case HIF_DEVICE_GET_RECV_EVENT_MASK_UNMASK_FUNC:
478 hif_warn("opcode %d", opcode);
479 status = QDF_STATUS_E_FAILURE;
480 break;
481 case HIF_CONFIGURE_QUERY_SCATTER_REQUEST_SUPPORT:
482 if (!device->scatter_enabled)
483 return QDF_STATUS_E_NOSUPPORT;
484 status =
485 setup_hif_scatter_support(device,
486 (struct HIF_DEVICE_SCATTER_SUPPORT_INFO *)
487 config);
488 if (QDF_IS_STATUS_ERROR(status))
489 device->scatter_enabled = false;
490 break;
491 case HIF_DEVICE_GET_OS_DEVICE:
492 /* pass back a pointer to the SDIO function's "dev" struct */
493 ((struct HIF_DEVICE_OS_DEVICE_INFO *) config)->os_dev =
494 &device->func->dev;
495 break;
496 case HIF_DEVICE_POWER_STATE_CHANGE:
497 status =
498 power_state_change_notify(ol_sc, device,
499 *(enum HIF_DEVICE_POWER_CHANGE_TYPE *)
500 config);
501 break;
502 case HIF_DEVICE_GET_IRQ_YIELD_PARAMS:
503 hif_warn("opcode %d", opcode);
504 status = QDF_STATUS_E_FAILURE;
505 break;
506 case HIF_DEVICE_SET_HTC_CONTEXT:
507 device->htc_context = config;
508 break;
509 case HIF_DEVICE_GET_HTC_CONTEXT:
510 if (!config) {
511 hif_err("htc context is NULL");
512 return QDF_STATUS_E_FAILURE;
513 }
514 *(void **)config = device->htc_context;
515 break;
516 case HIF_BMI_DONE:
517 hif_debug("BMI_DONE");
518 break;
519 default:
520 hif_err("Unsupported opcode: %d", opcode);
521 status = QDF_STATUS_E_FAILURE;
522 }
523
524 return status;
525 }
526
527 /**
528 * hif_sdio_shutdown() - hif-sdio shutdown routine
529 * @hif_ctx: pointer to hif_softc structore
530 *
531 * Return: None.
532 */
hif_sdio_shutdown(struct hif_softc * hif_ctx)533 void hif_sdio_shutdown(struct hif_softc *hif_ctx)
534 {
535 struct hif_sdio_softc *scn = HIF_GET_SDIO_SOFTC(hif_ctx);
536 struct hif_sdio_dev *hif_device = scn->hif_handle;
537
538 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
539 ("%s: Enter\n", __func__));
540 if (hif_device) {
541 AR_DEBUG_ASSERT(hif_device->power_config == HIF_DEVICE_POWER_CUT
542 || hif_device->func);
543 } else {
544 int i;
545 /* since we are unloading the driver anyways,
546 * reset all cards in case the SDIO card is
547 * externally powered and we are unloading the SDIO
548 * stack. This avoids the problem when the SDIO stack
549 * is reloaded and attempts are made to re-enumerate
550 * a card that is already enumerated
551 */
552 for (i = 0; i < MAX_HIF_DEVICES; ++i) {
553 if (hif_devices[i] && !hif_devices[i]->func) {
554 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
555 ("%s: Remove pending hif_device %pK\n",
556 __func__, hif_devices[i]));
557 del_hif_device(hif_devices[i]);
558 hif_devices[i] = NULL;
559 }
560 }
561 }
562 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
563 ("%s: Exit\n", __func__));
564 }
565
566 /**
567 * hif_device_inserted() - hif-sdio driver probe handler
568 * @ol_sc: HIF device context
569 * @func: pointer to sdio_func
570 * @id: pointer to sdio_device_id
571 *
572 * Return: QDF_STATUS
573 */
hif_device_inserted(struct hif_softc * ol_sc,struct sdio_func * func,const struct sdio_device_id * id)574 static QDF_STATUS hif_device_inserted(struct hif_softc *ol_sc,
575 struct sdio_func *func,
576 const struct sdio_device_id *id)
577 {
578 int i, count;
579 QDF_STATUS ret = QDF_STATUS_SUCCESS;
580 struct hif_sdio_dev *device = NULL;
581
582 hif_info("F%X, VID: 0x%X, DevID: 0x%X, block size: 0x%X/0x%X",
583 func->num, func->vendor, id->device,
584 func->max_blksize, func->cur_blksize);
585
586 /* dma_mask should be populated here. Use the parent device's setting */
587 func->dev.dma_mask = mmc_dev(func->card->host)->dma_mask;
588
589 for (i = 0; i < MAX_HIF_DEVICES; ++i) {
590 struct hif_sdio_dev *hifdevice = hif_devices[i];
591
592 if (hifdevice &&
593 hifdevice->power_config == HIF_DEVICE_POWER_CUT &&
594 hifdevice->host == func->card->host) {
595 device = get_hif_device(ol_sc, func);
596 hifdevice->func = func;
597 hifdevice->power_config = HIF_DEVICE_POWER_UP;
598 hif_sdio_set_drvdata(ol_sc, func, hifdevice);
599
600 if (device->is_suspend) {
601 hif_info("Resume from suspend");
602 ret = reinit_sdio(device);
603 }
604 break;
605 }
606 }
607
608 /* If device not found, then it is a new insertion, alloc and add it */
609 if (!device) {
610 if (!add_hif_device(ol_sc, func))
611 return QDF_STATUS_E_FAILURE;
612
613 device = get_hif_device(ol_sc, func);
614
615 for (i = 0; i < MAX_HIF_DEVICES; ++i) {
616 if (!hif_devices[i]) {
617 hif_devices[i] = device;
618 break;
619 }
620 }
621 if (i == MAX_HIF_DEVICES) {
622 hif_err("No more slots");
623 goto del_hif_dev;
624 }
625
626 device->id = id;
627 device->host = func->card->host;
628 device->is_disabled = true;
629 /* TODO: MMC SDIO3.0 Setting should also be modified in ReInit()
630 * function when Power Manage work.
631 */
632 sdio_claim_host(func);
633
634 hif_sdio_quirk_force_drive_strength(ol_sc, func);
635
636 hif_sdio_quirk_write_cccr(ol_sc, func);
637
638 ret = hif_sdio_set_bus_speed(ol_sc, func);
639
640 ret = hif_sdio_set_bus_width(ol_sc, func);
641 if (debugcccr)
642 hif_dump_cccr(device);
643
644 sdio_release_host(func);
645 }
646
647 qdf_spinlock_create(&device->lock);
648
649 qdf_spinlock_create(&device->asynclock);
650
651 DL_LIST_INIT(&device->scatter_req_head);
652
653 if (!nohifscattersupport) {
654 /* try to allow scatter operation on all instances,
655 * unless globally overridden
656 */
657 device->scatter_enabled = true;
658 } else
659 device->scatter_enabled = false;
660
661 /* Initialize the bus requests to be used later */
662 qdf_mem_zero(device->bus_request, sizeof(device->bus_request));
663 for (count = 0; count < BUS_REQUEST_MAX_NUM; count++) {
664 sema_init(&device->bus_request[count].sem_req, 0);
665 hif_free_bus_request(device, &device->bus_request[count]);
666 }
667 sema_init(&device->sem_async, 0);
668
669 ret = hif_enable_func(ol_sc, device, func, false);
670 if ((ret == QDF_STATUS_SUCCESS || ret == QDF_STATUS_E_PENDING))
671 return QDF_STATUS_SUCCESS;
672 ret = QDF_STATUS_E_FAILURE;
673 del_hif_dev:
674 del_hif_device(device);
675 for (i = 0; i < MAX_HIF_DEVICES; ++i) {
676 if (hif_devices[i] == device) {
677 hif_devices[i] = NULL;
678 break;
679 }
680 }
681 if (i == MAX_HIF_DEVICES) {
682 AR_DEBUG_PRINTF(ATH_DEBUG_ERROR,
683 ("%s: No hif_devices[] slot for %pK",
684 __func__, device));
685 }
686 return ret;
687 }
688
689 /**
690 * hif_ack_interrupt() - Acknowledge hif device irq
691 * @device: pointer to struct hif_sdio_dev
692 *
693 * This should translate to an acknowledgment to the bus driver indicating that
694 * the previous interrupt request has been serviced and the all the relevant
695 * sources have been cleared. HTC is ready to process more interrupts.
696 * This should prevent the bus driver from raising an interrupt unless the
697 * previous one has been serviced and acknowledged using the previous API.
698 *
699 * Return: None.
700 */
hif_ack_interrupt(struct hif_sdio_dev * device)701 void hif_ack_interrupt(struct hif_sdio_dev *device)
702 {
703 AR_DEBUG_ASSERT(device);
704
705 /* Acknowledge our function IRQ */
706 }
707
708 /**
709 * hif_sdio_configure_pipes - Configure pipes for the lower layer bus
710 * @dev: HIF layer object
711 * @func: SDIO bus function object
712 *
713 * Return: error in case of failure to configure, else success
714 */
hif_sdio_configure_pipes(struct hif_sdio_dev * dev,struct sdio_func * func)715 int hif_sdio_configure_pipes(struct hif_sdio_dev *dev, struct sdio_func *func)
716 {
717 return hif_dev_configure_pipes(dev, func);
718 }
719
720 /**
721 * hif_allocate_bus_request() - Allocate hif bus request
722 * @device: pointer to struct hif_sdio_dev
723 *
724 *
725 * Return: pointer to struct bus_request structure.
726 */
hif_allocate_bus_request(struct hif_sdio_dev * device)727 struct bus_request *hif_allocate_bus_request(struct hif_sdio_dev *device)
728 {
729 struct bus_request *busrequest;
730
731 qdf_spin_lock_irqsave(&device->lock);
732 busrequest = device->bus_request_free_queue;
733 /* Remove first in list */
734 if (busrequest)
735 device->bus_request_free_queue = busrequest->next;
736
737 /* Release lock */
738 qdf_spin_unlock_irqrestore(&device->lock);
739 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
740 ("%s: hif_allocate_bus_request: 0x%pK\n",
741 __func__, busrequest));
742
743 return busrequest;
744 }
745
746 /**
747 * hif_free_bus_request() - Free hif bus request
748 * @device: pointer to struct hif_sdio_dev
749 * @busrequest: bus request
750 *
751 * Return: None.
752 */
hif_free_bus_request(struct hif_sdio_dev * device,struct bus_request * busrequest)753 void hif_free_bus_request(struct hif_sdio_dev *device,
754 struct bus_request *busrequest)
755 {
756 AR_DEBUG_ASSERT(busrequest);
757 /* Acquire lock */
758 qdf_spin_lock_irqsave(&device->lock);
759
760 /* Insert first in list */
761 busrequest->next = device->bus_request_free_queue;
762 busrequest->inusenext = NULL;
763 device->bus_request_free_queue = busrequest;
764
765 /* Release lock */
766 qdf_spin_unlock_irqrestore(&device->lock);
767 }
768
hif_device_suspend(struct hif_softc * ol_sc,struct device * dev)769 int hif_device_suspend(struct hif_softc *ol_sc, struct device *dev)
770 {
771 struct sdio_func *func = dev_to_sdio_func(dev);
772 struct hif_sdio_dev *device = get_hif_device(ol_sc, func);
773 mmc_pm_flag_t pm_flag = 0;
774 enum HIF_DEVICE_POWER_CHANGE_TYPE config;
775 struct mmc_host *host = func->card->host;
776
777 host = device->func->card->host;
778
779 device->is_suspend = true;
780
781 switch (forcesleepmode) {
782 case 0: /* depend on sdio host pm capbility */
783 pm_flag = sdio_get_host_pm_caps(func);
784 break;
785 case 1: /* force WOW */
786 pm_flag |= MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
787 break;
788 case 2: /* force DeepSleep */
789 pm_flag &= ~MMC_PM_WAKE_SDIO_IRQ;
790 pm_flag |= MMC_PM_KEEP_POWER;
791 break;
792 case 3: /* force CutPower */
793 pm_flag &=
794 ~(MMC_PM_WAKE_SDIO_IRQ | MMC_PM_WAKE_SDIO_IRQ);
795 break;
796 }
797
798 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
799 /* setting power_config before hif_configure_device to
800 * skip sdio r/w when suspending with cut power
801 */
802 hif_info("Power cut");
803 config = HIF_DEVICE_POWER_CUT;
804 device->power_config = config;
805
806 hif_configure_device(ol_sc, device,
807 HIF_DEVICE_POWER_STATE_CHANGE,
808 &config,
809 sizeof(config));
810 hif_mask_interrupt(device);
811 device->device_state = HIF_DEVICE_STATE_CUTPOWER;
812 return 0;
813 }
814
815 if (sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER)) {
816 hif_err("set pm_flags failed");
817 return -EINVAL;
818 }
819
820 if (pm_flag & MMC_PM_WAKE_SDIO_IRQ) {
821 hif_info("WOW mode");
822 config = HIF_DEVICE_POWER_DOWN;
823 hif_configure_device(ol_sc, device,
824 HIF_DEVICE_POWER_STATE_CHANGE,
825 &config,
826 sizeof(config));
827
828 if (sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ)) {
829 hif_err("set pm_flags failed");
830 return -EINVAL;
831 }
832 hif_mask_interrupt(device);
833 device->device_state = HIF_DEVICE_STATE_WOW;
834 return 0;
835 } else {
836 hif_info("deep sleep enter");
837 msleep(100);
838 hif_mask_interrupt(device);
839 device->device_state = HIF_DEVICE_STATE_DEEPSLEEP;
840 return 0;
841 }
842
843 return 0;
844 }
845
hif_device_resume(struct hif_softc * ol_sc,struct device * dev)846 int hif_device_resume(struct hif_softc *ol_sc, struct device *dev)
847 {
848 struct sdio_func *func = dev_to_sdio_func(dev);
849 QDF_STATUS status = QDF_STATUS_SUCCESS;
850 enum HIF_DEVICE_POWER_CHANGE_TYPE config;
851 struct hif_sdio_dev *device;
852
853 device = get_hif_device(ol_sc, func);
854 if (!device) {
855 hif_err("hif object is null");
856 return -EINVAL;
857 }
858
859 if (device->device_state == HIF_DEVICE_STATE_CUTPOWER) {
860 config = HIF_DEVICE_POWER_UP;
861 hif_configure_device(ol_sc, device,
862 HIF_DEVICE_POWER_STATE_CHANGE,
863 &config,
864 sizeof(config));
865 hif_enable_func(ol_sc, device, func, true);
866 } else if (device->device_state == HIF_DEVICE_STATE_DEEPSLEEP) {
867 hif_un_mask_interrupt(device);
868 } else if (device->device_state == HIF_DEVICE_STATE_WOW) {
869 /*TODO:WOW support */
870 hif_un_mask_interrupt(device);
871 }
872
873 device->is_suspend = false;
874 device->device_state = HIF_DEVICE_STATE_ON;
875
876 return QDF_IS_STATUS_SUCCESS(status) ? 0 : status;
877 }
878
879 /**
880 * hif_sdio_remove() - remove sdio device
881 * @context: sdio device context
882 * @hif_handle: pointer to sdio function
883 *
884 * Return: 0 for success and non-zero for failure
885 */
hif_sdio_remove(void * context,void * hif_handle)886 static A_STATUS hif_sdio_remove(void *context, void *hif_handle)
887 {
888 HIF_ENTER();
889
890 if (!scn) {
891 QDF_TRACE(QDF_MODULE_ID_HIF, QDF_TRACE_LEVEL_ERROR,
892 "Global SDIO context is NULL");
893 return A_ERROR;
894 }
895
896 athdiag_procfs_remove();
897
898 #ifndef TARGET_DUMP_FOR_NON_QC_PLATFORM
899 iounmap(scn->ramdump_base);
900 #endif
901
902 HIF_EXIT();
903
904 return 0;
905 }
906
hif_device_removed(struct hif_softc * ol_sc,struct sdio_func * func)907 static void hif_device_removed(struct hif_softc *ol_sc, struct sdio_func *func)
908 {
909 QDF_STATUS status = QDF_STATUS_SUCCESS;
910 struct hif_sdio_dev *device;
911 int i;
912
913 AR_DEBUG_ASSERT(func);
914 HIF_ENTER();
915 device = get_hif_device(ol_sc, func);
916
917 if (device->power_config == HIF_DEVICE_POWER_CUT) {
918 device->func = NULL; /* func will be free by mmc stack */
919 return; /* Just return for cut-off mode */
920 }
921 for (i = 0; i < MAX_HIF_DEVICES; ++i) {
922 if (hif_devices[i] == device)
923 hif_devices[i] = NULL;
924 }
925
926 hif_sdio_remove(device->claimed_ctx, device);
927
928 hif_mask_interrupt(device);
929
930 if (device->is_disabled)
931 device->is_disabled = false;
932 else
933 status = hif_disable_func(device, func,
934 reset_sdio_on_unload ? true : false);
935
936
937 del_hif_device(device);
938 if (status != QDF_STATUS_SUCCESS)
939 AR_DEBUG_PRINTF(ATH_DEBUG_WARN,
940 ("%s: Unable to disable sdio func\n",
941 __func__));
942
943 HIF_EXIT();
944 }
945
add_hif_device(struct hif_softc * ol_sc,struct sdio_func * func)946 static struct hif_sdio_dev *add_hif_device(struct hif_softc *ol_sc,
947 struct sdio_func *func)
948 {
949 struct hif_sdio_dev *hifdevice = NULL;
950 int ret = 0;
951
952 HIF_ENTER();
953 AR_DEBUG_ASSERT(func);
954 hifdevice = (struct hif_sdio_dev *) qdf_mem_malloc(sizeof(
955 struct hif_sdio_dev));
956 AR_DEBUG_ASSERT(hifdevice);
957 if (!hifdevice)
958 return NULL;
959
960 #if HIF_USE_DMA_BOUNCE_BUFFER
961 hifdevice->dma_buffer = qdf_mem_malloc(HIF_DMA_BUFFER_SIZE);
962 AR_DEBUG_ASSERT(hifdevice->dma_buffer);
963 if (!hifdevice->dma_buffer) {
964 qdf_mem_free(hifdevice);
965 return NULL;
966 }
967 #endif
968 hifdevice->func = func;
969 hifdevice->power_config = HIF_DEVICE_POWER_UP;
970 hifdevice->device_state = HIF_DEVICE_STATE_ON;
971 ret = hif_sdio_set_drvdata(ol_sc, func, hifdevice);
972 hif_info("status %d", ret);
973
974 return hifdevice;
975 }
976
del_hif_device(struct hif_sdio_dev * device)977 static void del_hif_device(struct hif_sdio_dev *device)
978 {
979 AR_DEBUG_ASSERT(device);
980 AR_DEBUG_PRINTF(ATH_DEBUG_TRACE,
981 ("%s: deleting hif device 0x%pK\n",
982 __func__, device));
983 if (device->dma_buffer)
984 qdf_mem_free(device->dma_buffer);
985
986 qdf_mem_free(device);
987 }
988
hif_attach_htc(struct hif_sdio_dev * device,struct htc_callbacks * callbacks)989 QDF_STATUS hif_attach_htc(struct hif_sdio_dev *device,
990 struct htc_callbacks *callbacks)
991 {
992 if (device->htc_callbacks.context)
993 /* already in use! */
994 return QDF_STATUS_E_FAILURE;
995 device->htc_callbacks = *callbacks;
996
997 return QDF_STATUS_SUCCESS;
998 }
999
hif_detach_htc(struct hif_opaque_softc * hif_ctx)1000 void hif_detach_htc(struct hif_opaque_softc *hif_ctx)
1001 {
1002 struct hif_sdio_softc *scn = HIF_GET_SDIO_SOFTC(hif_ctx);
1003 struct hif_sdio_dev *hif_device = scn->hif_handle;
1004
1005 qdf_mem_zero(&hif_device->htc_callbacks,
1006 sizeof(hif_device->htc_callbacks));
1007 }
1008
func0_cmd52_write_byte(struct mmc_card * card,unsigned int address,unsigned char byte)1009 int func0_cmd52_write_byte(struct mmc_card *card,
1010 unsigned int address,
1011 unsigned char byte)
1012 {
1013 struct mmc_command io_cmd;
1014 unsigned long arg;
1015 int status = 0;
1016
1017 memset(&io_cmd, 0, sizeof(io_cmd));
1018 SDIO_SET_CMD52_WRITE_ARG(arg, 0, address, byte);
1019 io_cmd.opcode = SD_IO_RW_DIRECT;
1020 io_cmd.arg = arg;
1021 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1022 status = mmc_wait_for_cmd(card->host, &io_cmd, 0);
1023
1024 if (status)
1025 hif_err("mmc_wait_for_cmd returned %d", status);
1026
1027 return status;
1028 }
1029
func0_cmd52_read_byte(struct mmc_card * card,unsigned int address,unsigned char * byte)1030 int func0_cmd52_read_byte(struct mmc_card *card,
1031 unsigned int address,
1032 unsigned char *byte)
1033 {
1034 struct mmc_command io_cmd;
1035 unsigned long arg;
1036 int32_t err;
1037
1038 memset(&io_cmd, 0, sizeof(io_cmd));
1039 SDIO_SET_CMD52_READ_ARG(arg, 0, address);
1040 io_cmd.opcode = SD_IO_RW_DIRECT;
1041 io_cmd.arg = arg;
1042 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
1043
1044 err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
1045
1046 if ((!err) && (byte))
1047 *byte = io_cmd.resp[0] & 0xFF;
1048
1049 if (err)
1050 hif_err("mmc_wait_for_cmd returned %d", err);
1051
1052 return err;
1053 }
1054
hif_dump_cccr(struct hif_sdio_dev * hif_device)1055 void hif_dump_cccr(struct hif_sdio_dev *hif_device)
1056 {
1057 unsigned int i;
1058 uint8_t cccr_val;
1059 uint32_t err;
1060
1061 if (!hif_device || !hif_device->func ||
1062 !hif_device->func->card) {
1063 hif_err("Incorrect input");
1064 return;
1065 }
1066
1067 for (i = 0; i <= 0x16; i++) {
1068 err = func0_cmd52_read_byte(hif_device->func->card,
1069 i, &cccr_val);
1070 if (err)
1071 hif_err("Reading CCCR 0x%02X failed: %d",
1072 i, (unsigned int)err);
1073 else
1074 hif_err("%X(%X) ", i, (unsigned int)cccr_val);
1075 }
1076 }
1077
hif_sdio_device_inserted(struct hif_softc * ol_sc,struct device * dev,const struct sdio_device_id * id)1078 QDF_STATUS hif_sdio_device_inserted(struct hif_softc *ol_sc,
1079 struct device *dev,
1080 const struct sdio_device_id *id)
1081 {
1082 struct sdio_func *func = dev_to_sdio_func(dev);
1083 QDF_STATUS status = QDF_STATUS_SUCCESS;
1084
1085 hif_debug("Enter");
1086 status = hif_device_inserted(ol_sc, func, id);
1087 hif_debug("Exit: status: %d", status);
1088
1089 return status;
1090 }
1091
hif_sdio_device_removed(struct hif_softc * ol_sc,struct sdio_func * func)1092 void hif_sdio_device_removed(struct hif_softc *ol_sc, struct sdio_func *func)
1093 {
1094 hif_device_removed(ol_sc, func);
1095 }
1096