1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 /* ****************** SDIO CARD Interface Functions **************************/
6 
7 #include <linux/types.h>
8 #include <linux/netdevice.h>
9 #include <linux/pci.h>
10 #include <linux/pci_ids.h>
11 #include <linux/sched.h>
12 #include <linux/completion.h>
13 #include <linux/interrupt.h>
14 #include <linux/scatterlist.h>
15 #include <linux/mmc/sdio.h>
16 #include <linux/mmc/core.h>
17 #include <linux/mmc/sdio_func.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/suspend.h>
22 #include <linux/errno.h>
23 #include <linux/module.h>
24 #include <linux/acpi.h>
25 #include <net/cfg80211.h>
26 
27 #include <defs.h>
28 #include <brcm_hw_ids.h>
29 #include <brcmu_utils.h>
30 #include <brcmu_wifi.h>
31 #include <chipcommon.h>
32 #include <soc.h>
33 #include "chip.h"
34 #include "bus.h"
35 #include "debug.h"
36 #include "sdio.h"
37 #include "core.h"
38 #include "common.h"
39 
40 #define SDIOH_API_ACCESS_RETRY_LIMIT	2
41 
42 #define DMA_ALIGN_MASK	0x03
43 
44 #define SDIO_FUNC1_BLOCKSIZE		64
45 #define SDIO_FUNC2_BLOCKSIZE		512
46 #define SDIO_4373_FUNC2_BLOCKSIZE	256
47 #define SDIO_435X_FUNC2_BLOCKSIZE	256
48 /* Maximum milliseconds to wait for F2 to come up */
49 #define SDIO_WAIT_F2RDY	3000
50 
51 #define BRCMF_DEFAULT_RXGLOM_SIZE	32  /* max rx frames in glom chain */
52 
53 struct brcmf_sdiod_freezer {
54 	atomic_t freezing;
55 	atomic_t thread_count;
56 	u32 frozen_count;
57 	wait_queue_head_t thread_freeze;
58 	struct completion resumed;
59 };
60 
61 static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
62 {
63 	struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
64 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
65 
66 	brcmf_dbg(INTR, "OOB intr triggered\n");
67 
68 	/* out-of-band interrupt is level-triggered which won't
69 	 * be cleared until dpc
70 	 */
71 	if (sdiodev->irq_en) {
72 		disable_irq_nosync(irq);
73 		sdiodev->irq_en = false;
74 	}
75 
76 	brcmf_sdio_isr(sdiodev->bus);
77 
78 	return IRQ_HANDLED;
79 }
80 
81 static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
82 {
83 	struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
84 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
85 
86 	brcmf_dbg(INTR, "IB intr triggered\n");
87 
88 	brcmf_sdio_isr(sdiodev->bus);
89 }
90 
91 /* dummy handler for SDIO function 2 interrupt */
92 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
93 {
94 }
95 
96 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
97 {
98 	struct brcmfmac_sdio_pd *pdata;
99 	int ret = 0;
100 	u8 data;
101 	u32 addr, gpiocontrol;
102 
103 	pdata = &sdiodev->settings->bus.sdio;
104 	if (pdata->oob_irq_supported) {
105 		brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
106 			  pdata->oob_irq_nr);
107 		spin_lock_init(&sdiodev->irq_en_lock);
108 		sdiodev->irq_en = true;
109 
110 		ret = request_irq(pdata->oob_irq_nr, brcmf_sdiod_oob_irqhandler,
111 				  pdata->oob_irq_flags, "brcmf_oob_intr",
112 				  &sdiodev->func1->dev);
113 		if (ret != 0) {
114 			brcmf_err("request_irq failed %d\n", ret);
115 			return ret;
116 		}
117 		sdiodev->oob_irq_requested = true;
118 
119 		ret = enable_irq_wake(pdata->oob_irq_nr);
120 		if (ret != 0) {
121 			brcmf_err("enable_irq_wake failed %d\n", ret);
122 			return ret;
123 		}
124 		disable_irq_wake(pdata->oob_irq_nr);
125 
126 		sdio_claim_host(sdiodev->func1);
127 
128 		if (sdiodev->bus_if->chip == BRCM_CC_43362_CHIP_ID) {
129 			/* assign GPIO to SDIO core */
130 			addr = CORE_CC_REG(SI_ENUM_BASE, gpiocontrol);
131 			gpiocontrol = brcmf_sdiod_readl(sdiodev, addr, &ret);
132 			gpiocontrol |= 0x2;
133 			brcmf_sdiod_writel(sdiodev, addr, gpiocontrol, &ret);
134 
135 			brcmf_sdiod_writeb(sdiodev, SBSDIO_GPIO_SELECT,
136 					   0xf, &ret);
137 			brcmf_sdiod_writeb(sdiodev, SBSDIO_GPIO_OUT, 0, &ret);
138 			brcmf_sdiod_writeb(sdiodev, SBSDIO_GPIO_EN, 0x2, &ret);
139 		}
140 
141 		/* must configure SDIO_CCCR_IENx to enable irq */
142 		data = brcmf_sdiod_func0_rb(sdiodev, SDIO_CCCR_IENx, &ret);
143 		data |= SDIO_CCCR_IEN_FUNC1 | SDIO_CCCR_IEN_FUNC2 |
144 			SDIO_CCCR_IEN_FUNC0;
145 		brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_IENx, data, &ret);
146 
147 		/* redirect, configure and enable io for interrupt signal */
148 		data = SDIO_CCCR_BRCM_SEPINT_MASK | SDIO_CCCR_BRCM_SEPINT_OE;
149 		if (pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
150 			data |= SDIO_CCCR_BRCM_SEPINT_ACT_HI;
151 		brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_BRCM_SEPINT,
152 				     data, &ret);
153 		sdio_release_host(sdiodev->func1);
154 	} else {
155 		brcmf_dbg(SDIO, "Entering\n");
156 		sdio_claim_host(sdiodev->func1);
157 		sdio_claim_irq(sdiodev->func1, brcmf_sdiod_ib_irqhandler);
158 		sdio_claim_irq(sdiodev->func2, brcmf_sdiod_dummy_irqhandler);
159 		sdio_release_host(sdiodev->func1);
160 		sdiodev->sd_irq_requested = true;
161 	}
162 
163 	return 0;
164 }
165 
166 void brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
167 {
168 
169 	brcmf_dbg(SDIO, "Entering oob=%d sd=%d\n",
170 		  sdiodev->oob_irq_requested,
171 		  sdiodev->sd_irq_requested);
172 
173 	if (sdiodev->oob_irq_requested) {
174 		struct brcmfmac_sdio_pd *pdata;
175 
176 		pdata = &sdiodev->settings->bus.sdio;
177 		sdio_claim_host(sdiodev->func1);
178 		brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
179 		brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
180 		sdio_release_host(sdiodev->func1);
181 
182 		sdiodev->oob_irq_requested = false;
183 		free_irq(pdata->oob_irq_nr, &sdiodev->func1->dev);
184 		sdiodev->irq_en = false;
185 		sdiodev->oob_irq_requested = false;
186 	}
187 
188 	if (sdiodev->sd_irq_requested) {
189 		sdio_claim_host(sdiodev->func1);
190 		sdio_release_irq(sdiodev->func2);
191 		sdio_release_irq(sdiodev->func1);
192 		sdio_release_host(sdiodev->func1);
193 		sdiodev->sd_irq_requested = false;
194 	}
195 }
196 
197 void brcmf_sdiod_change_state(struct brcmf_sdio_dev *sdiodev,
198 			      enum brcmf_sdiod_state state)
199 {
200 	if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM ||
201 	    state == sdiodev->state)
202 		return;
203 
204 	brcmf_dbg(TRACE, "%d -> %d\n", sdiodev->state, state);
205 	switch (sdiodev->state) {
206 	case BRCMF_SDIOD_DATA:
207 		/* any other state means bus interface is down */
208 		brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_DOWN);
209 		break;
210 	case BRCMF_SDIOD_DOWN:
211 		/* transition from DOWN to DATA means bus interface is up */
212 		if (state == BRCMF_SDIOD_DATA)
213 			brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_UP);
214 		break;
215 	default:
216 		break;
217 	}
218 	sdiodev->state = state;
219 }
220 
221 static int brcmf_sdiod_set_backplane_window(struct brcmf_sdio_dev *sdiodev,
222 					    u32 addr)
223 {
224 	u32 v, bar0 = addr & SBSDIO_SBWINDOW_MASK;
225 	int err = 0, i;
226 
227 	if (bar0 == sdiodev->sbwad)
228 		return 0;
229 
230 	v = bar0 >> 8;
231 
232 	for (i = 0 ; i < 3 && !err ; i++, v >>= 8)
233 		brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_SBADDRLOW + i,
234 				   v & 0xff, &err);
235 
236 	if (!err)
237 		sdiodev->sbwad = bar0;
238 
239 	return err;
240 }
241 
242 u32 brcmf_sdiod_readl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
243 {
244 	u32 data = 0;
245 	int retval;
246 
247 	retval = brcmf_sdiod_set_backplane_window(sdiodev, addr);
248 	if (retval)
249 		goto out;
250 
251 	addr &= SBSDIO_SB_OFT_ADDR_MASK;
252 	addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
253 
254 	data = sdio_readl(sdiodev->func1, addr, &retval);
255 
256 out:
257 	if (ret)
258 		*ret = retval;
259 
260 	return data;
261 }
262 
263 void brcmf_sdiod_writel(struct brcmf_sdio_dev *sdiodev, u32 addr,
264 			u32 data, int *ret)
265 {
266 	int retval;
267 
268 	retval = brcmf_sdiod_set_backplane_window(sdiodev, addr);
269 	if (retval)
270 		goto out;
271 
272 	addr &= SBSDIO_SB_OFT_ADDR_MASK;
273 	addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
274 
275 	sdio_writel(sdiodev->func1, data, addr, &retval);
276 
277 out:
278 	if (ret)
279 		*ret = retval;
280 }
281 
282 static int brcmf_sdiod_skbuff_read(struct brcmf_sdio_dev *sdiodev,
283 				   struct sdio_func *func, u32 addr,
284 				   struct sk_buff *skb)
285 {
286 	unsigned int req_sz;
287 	int err;
288 
289 	/* Single skb use the standard mmc interface */
290 	req_sz = skb->len + 3;
291 	req_sz &= (uint)~3;
292 
293 	switch (func->num) {
294 	case 1:
295 		err = sdio_memcpy_fromio(func, ((u8 *)(skb->data)), addr,
296 					 req_sz);
297 		break;
298 	case 2:
299 		err = sdio_readsb(func, ((u8 *)(skb->data)), addr, req_sz);
300 		break;
301 	default:
302 		/* bail out as things are really fishy here */
303 		WARN(1, "invalid sdio function number: %d\n", func->num);
304 		err = -ENOMEDIUM;
305 	}
306 
307 	if (err == -ENOMEDIUM)
308 		brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM);
309 
310 	return err;
311 }
312 
313 static int brcmf_sdiod_skbuff_write(struct brcmf_sdio_dev *sdiodev,
314 				    struct sdio_func *func, u32 addr,
315 				    struct sk_buff *skb)
316 {
317 	unsigned int req_sz;
318 	int err;
319 
320 	/* Single skb use the standard mmc interface */
321 	req_sz = skb->len + 3;
322 	req_sz &= (uint)~3;
323 
324 	err = sdio_memcpy_toio(func, addr, ((u8 *)(skb->data)), req_sz);
325 
326 	if (err == -ENOMEDIUM)
327 		brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM);
328 
329 	return err;
330 }
331 
332 static int mmc_submit_one(struct mmc_data *md, struct mmc_request *mr,
333 			  struct mmc_command *mc, int sg_cnt, int req_sz,
334 			  int func_blk_sz, u32 *addr,
335 			  struct brcmf_sdio_dev *sdiodev,
336 			  struct sdio_func *func, int write)
337 {
338 	int ret;
339 
340 	md->sg_len = sg_cnt;
341 	md->blocks = req_sz / func_blk_sz;
342 	mc->arg |= (*addr & 0x1FFFF) << 9;	/* address */
343 	mc->arg |= md->blocks & 0x1FF;	/* block count */
344 	/* incrementing addr for function 1 */
345 	if (func->num == 1)
346 		*addr += req_sz;
347 
348 	mmc_set_data_timeout(md, func->card);
349 	mmc_wait_for_req(func->card->host, mr);
350 
351 	ret = mc->error ? mc->error : md->error;
352 	if (ret == -ENOMEDIUM) {
353 		brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM);
354 	} else if (ret != 0) {
355 		brcmf_err("CMD53 sg block %s failed %d\n",
356 			  write ? "write" : "read", ret);
357 		ret = -EIO;
358 	}
359 
360 	return ret;
361 }
362 
363 /**
364  * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
365  * @sdiodev: brcmfmac sdio device
366  * @func: SDIO function
367  * @write: direction flag
368  * @addr: dongle memory address as source/destination
369  * @pkt: skb pointer
370  *
371  * This function takes the respbonsibility as the interface function to MMC
372  * stack for block data access. It assumes that the skb passed down by the
373  * caller has already been padded and aligned.
374  */
375 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev,
376 				 struct sdio_func *func,
377 				 bool write, u32 addr,
378 				 struct sk_buff_head *pktlist)
379 {
380 	unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
381 	unsigned int max_req_sz, src_offset, dst_offset;
382 	unsigned char *pkt_data, *orig_data, *dst_data;
383 	struct sk_buff_head local_list, *target_list;
384 	struct sk_buff *pkt_next = NULL, *src;
385 	unsigned short max_seg_cnt;
386 	struct mmc_request mmc_req;
387 	struct mmc_command mmc_cmd;
388 	struct mmc_data mmc_dat;
389 	struct scatterlist *sgl;
390 	int ret = 0;
391 
392 	if (!pktlist->qlen)
393 		return -EINVAL;
394 
395 	target_list = pktlist;
396 	/* for host with broken sg support, prepare a page aligned list */
397 	__skb_queue_head_init(&local_list);
398 	if (!write && sdiodev->settings->bus.sdio.broken_sg_support) {
399 		req_sz = 0;
400 		skb_queue_walk(pktlist, pkt_next)
401 			req_sz += pkt_next->len;
402 		req_sz = ALIGN(req_sz, func->cur_blksize);
403 		while (req_sz > PAGE_SIZE) {
404 			pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE);
405 			if (pkt_next == NULL) {
406 				ret = -ENOMEM;
407 				goto exit;
408 			}
409 			__skb_queue_tail(&local_list, pkt_next);
410 			req_sz -= PAGE_SIZE;
411 		}
412 		pkt_next = brcmu_pkt_buf_get_skb(req_sz);
413 		if (pkt_next == NULL) {
414 			ret = -ENOMEM;
415 			goto exit;
416 		}
417 		__skb_queue_tail(&local_list, pkt_next);
418 		target_list = &local_list;
419 	}
420 
421 	func_blk_sz = func->cur_blksize;
422 	max_req_sz = sdiodev->max_request_size;
423 	max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count,
424 			    target_list->qlen);
425 
426 	memset(&mmc_req, 0, sizeof(struct mmc_request));
427 	memset(&mmc_cmd, 0, sizeof(struct mmc_command));
428 	memset(&mmc_dat, 0, sizeof(struct mmc_data));
429 
430 	mmc_dat.sg = sdiodev->sgtable.sgl;
431 	mmc_dat.blksz = func_blk_sz;
432 	mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
433 	mmc_cmd.opcode = SD_IO_RW_EXTENDED;
434 	mmc_cmd.arg = write ? 1<<31 : 0;	/* write flag  */
435 	mmc_cmd.arg |= (func->num & 0x7) << 28;	/* SDIO func num */
436 	mmc_cmd.arg |= 1 << 27;			/* block mode */
437 	/* for function 1 the addr will be incremented */
438 	mmc_cmd.arg |= (func->num == 1) ? 1 << 26 : 0;
439 	mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
440 	mmc_req.cmd = &mmc_cmd;
441 	mmc_req.data = &mmc_dat;
442 
443 	req_sz = 0;
444 	sg_cnt = 0;
445 	sgl = sdiodev->sgtable.sgl;
446 	skb_queue_walk(target_list, pkt_next) {
447 		pkt_offset = 0;
448 		while (pkt_offset < pkt_next->len) {
449 			pkt_data = pkt_next->data + pkt_offset;
450 			sg_data_sz = pkt_next->len - pkt_offset;
451 			if (sg_data_sz > sdiodev->max_segment_size)
452 				sg_data_sz = sdiodev->max_segment_size;
453 			if (sg_data_sz > max_req_sz - req_sz)
454 				sg_data_sz = max_req_sz - req_sz;
455 
456 			sg_set_buf(sgl, pkt_data, sg_data_sz);
457 			sg_cnt++;
458 
459 			sgl = sg_next(sgl);
460 			req_sz += sg_data_sz;
461 			pkt_offset += sg_data_sz;
462 			if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt) {
463 				ret = mmc_submit_one(&mmc_dat, &mmc_req, &mmc_cmd,
464 						     sg_cnt, req_sz, func_blk_sz,
465 						     &addr, sdiodev, func, write);
466 				if (ret)
467 					goto exit_queue_walk;
468 				req_sz = 0;
469 				sg_cnt = 0;
470 				sgl = sdiodev->sgtable.sgl;
471 			}
472 		}
473 	}
474 	if (sg_cnt)
475 		ret = mmc_submit_one(&mmc_dat, &mmc_req, &mmc_cmd,
476 				     sg_cnt, req_sz, func_blk_sz,
477 				     &addr, sdiodev, func, write);
478 exit_queue_walk:
479 	if (!write && sdiodev->settings->bus.sdio.broken_sg_support) {
480 		src = __skb_peek(&local_list);
481 		src_offset = 0;
482 		skb_queue_walk(pktlist, pkt_next) {
483 			dst_offset = 0;
484 
485 			/* This is safe because we must have enough SKB data
486 			 * in the local list to cover everything in pktlist.
487 			 */
488 			while (1) {
489 				req_sz = pkt_next->len - dst_offset;
490 				if (req_sz > src->len - src_offset)
491 					req_sz = src->len - src_offset;
492 
493 				orig_data = src->data + src_offset;
494 				dst_data = pkt_next->data + dst_offset;
495 				memcpy(dst_data, orig_data, req_sz);
496 
497 				src_offset += req_sz;
498 				if (src_offset == src->len) {
499 					src_offset = 0;
500 					src = skb_peek_next(src, &local_list);
501 				}
502 				dst_offset += req_sz;
503 				if (dst_offset == pkt_next->len)
504 					break;
505 			}
506 		}
507 	}
508 
509 exit:
510 	sg_init_table(sdiodev->sgtable.sgl, sdiodev->sgtable.orig_nents);
511 	while ((pkt_next = __skb_dequeue(&local_list)) != NULL)
512 		brcmu_pkt_buf_free_skb(pkt_next);
513 
514 	return ret;
515 }
516 
517 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
518 {
519 	struct sk_buff *mypkt;
520 	int err;
521 
522 	mypkt = brcmu_pkt_buf_get_skb(nbytes);
523 	if (!mypkt) {
524 		brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
525 			  nbytes);
526 		return -EIO;
527 	}
528 
529 	err = brcmf_sdiod_recv_pkt(sdiodev, mypkt);
530 	if (!err)
531 		memcpy(buf, mypkt->data, nbytes);
532 
533 	brcmu_pkt_buf_free_skb(mypkt);
534 	return err;
535 }
536 
537 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt)
538 {
539 	u32 addr = sdiodev->cc_core->base;
540 	int err = 0;
541 
542 	brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len);
543 
544 	err = brcmf_sdiod_set_backplane_window(sdiodev, addr);
545 	if (err)
546 		goto done;
547 
548 	addr &= SBSDIO_SB_OFT_ADDR_MASK;
549 	addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
550 
551 	err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func2, addr, pkt);
552 
553 done:
554 	return err;
555 }
556 
557 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev,
558 			   struct sk_buff_head *pktq, uint totlen)
559 {
560 	struct sk_buff *glom_skb = NULL;
561 	struct sk_buff *skb;
562 	u32 addr = sdiodev->cc_core->base;
563 	int err = 0;
564 
565 	brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n",
566 		  addr, pktq->qlen);
567 
568 	err = brcmf_sdiod_set_backplane_window(sdiodev, addr);
569 	if (err)
570 		goto done;
571 
572 	addr &= SBSDIO_SB_OFT_ADDR_MASK;
573 	addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
574 
575 	if (pktq->qlen == 1)
576 		err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func2, addr,
577 					      __skb_peek(pktq));
578 	else if (!sdiodev->sg_support) {
579 		glom_skb = brcmu_pkt_buf_get_skb(totlen);
580 		if (!glom_skb)
581 			return -ENOMEM;
582 		err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func2, addr,
583 					      glom_skb);
584 		if (err)
585 			goto done;
586 
587 		skb_queue_walk(pktq, skb) {
588 			memcpy(skb->data, glom_skb->data, skb->len);
589 			skb_pull(glom_skb, skb->len);
590 		}
591 	} else
592 		err = brcmf_sdiod_sglist_rw(sdiodev, sdiodev->func2, false,
593 					    addr, pktq);
594 
595 done:
596 	brcmu_pkt_buf_free_skb(glom_skb);
597 	return err;
598 }
599 
600 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
601 {
602 	struct sk_buff *mypkt;
603 	u32 addr = sdiodev->cc_core->base;
604 	int err;
605 
606 	mypkt = brcmu_pkt_buf_get_skb(nbytes);
607 
608 	if (!mypkt) {
609 		brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
610 			  nbytes);
611 		return -EIO;
612 	}
613 
614 	memcpy(mypkt->data, buf, nbytes);
615 
616 	err = brcmf_sdiod_set_backplane_window(sdiodev, addr);
617 	if (err)
618 		goto out;
619 
620 	addr &= SBSDIO_SB_OFT_ADDR_MASK;
621 	addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
622 
623 	err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func2, addr, mypkt);
624 out:
625 	brcmu_pkt_buf_free_skb(mypkt);
626 
627 	return err;
628 }
629 
630 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev,
631 			 struct sk_buff_head *pktq)
632 {
633 	struct sk_buff *skb;
634 	u32 addr = sdiodev->cc_core->base;
635 	int err;
636 
637 	brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen);
638 
639 	err = brcmf_sdiod_set_backplane_window(sdiodev, addr);
640 	if (err)
641 		return err;
642 
643 	addr &= SBSDIO_SB_OFT_ADDR_MASK;
644 	addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
645 
646 	if (pktq->qlen == 1 || !sdiodev->sg_support) {
647 		skb_queue_walk(pktq, skb) {
648 			err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func2,
649 						       addr, skb);
650 			if (err)
651 				break;
652 		}
653 	} else {
654 		err = brcmf_sdiod_sglist_rw(sdiodev, sdiodev->func2, true,
655 					    addr, pktq);
656 	}
657 
658 	return err;
659 }
660 
661 int
662 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
663 		  u8 *data, uint size)
664 {
665 	int err = 0;
666 	struct sk_buff *pkt;
667 	u32 sdaddr;
668 	uint dsize;
669 
670 	dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
671 	pkt = dev_alloc_skb(dsize);
672 	if (!pkt) {
673 		brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
674 		return -EIO;
675 	}
676 	pkt->priority = 0;
677 
678 	/* Determine initial transfer parameters */
679 	sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
680 	if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
681 		dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
682 	else
683 		dsize = size;
684 
685 	sdio_claim_host(sdiodev->func1);
686 
687 	/* Do the transfer(s) */
688 	while (size) {
689 		/* Set the backplane window to include the start address */
690 		err = brcmf_sdiod_set_backplane_window(sdiodev, address);
691 		if (err)
692 			break;
693 
694 		brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
695 			  write ? "write" : "read", dsize,
696 			  sdaddr, address & SBSDIO_SBWINDOW_MASK);
697 
698 		sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
699 		sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
700 
701 		skb_put(pkt, dsize);
702 
703 		if (write) {
704 			memcpy(pkt->data, data, dsize);
705 			err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func1,
706 						       sdaddr, pkt);
707 		} else {
708 			err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func1,
709 						      sdaddr, pkt);
710 		}
711 
712 		if (err) {
713 			brcmf_err("membytes transfer failed\n");
714 			break;
715 		}
716 		if (!write)
717 			memcpy(data, pkt->data, dsize);
718 		skb_trim(pkt, 0);
719 
720 		/* Adjust for next transfer (if any) */
721 		size -= dsize;
722 		if (size) {
723 			data += dsize;
724 			address += dsize;
725 			sdaddr = 0;
726 			dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
727 		}
728 	}
729 
730 	dev_kfree_skb(pkt);
731 
732 	sdio_release_host(sdiodev->func1);
733 
734 	return err;
735 }
736 
737 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, struct sdio_func *func)
738 {
739 	brcmf_dbg(SDIO, "Enter\n");
740 
741 	/* Issue abort cmd52 command through F0 */
742 	brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_ABORT, func->num, NULL);
743 
744 	brcmf_dbg(SDIO, "Exit\n");
745 	return 0;
746 }
747 
748 void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev)
749 {
750 	struct sdio_func *func;
751 	struct mmc_host *host;
752 	uint max_blocks;
753 	uint nents;
754 	int err;
755 
756 	func = sdiodev->func2;
757 	host = func->card->host;
758 	sdiodev->sg_support = host->max_segs > 1;
759 	max_blocks = min_t(uint, host->max_blk_count, 511u);
760 	sdiodev->max_request_size = min_t(uint, host->max_req_size,
761 					  max_blocks * func->cur_blksize);
762 	sdiodev->max_segment_count = min_t(uint, host->max_segs,
763 					   SG_MAX_SINGLE_ALLOC);
764 	sdiodev->max_segment_size = host->max_seg_size;
765 
766 	if (!sdiodev->sg_support)
767 		return;
768 
769 	nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE,
770 		      sdiodev->settings->bus.sdio.txglomsz);
771 	nents += (nents >> 4) + 1;
772 
773 	WARN_ON(nents > sdiodev->max_segment_count);
774 
775 	brcmf_dbg(TRACE, "nents=%d\n", nents);
776 	err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL);
777 	if (err < 0) {
778 		brcmf_err("allocation failed: disable scatter-gather");
779 		sdiodev->sg_support = false;
780 	}
781 
782 	sdiodev->txglomsz = sdiodev->settings->bus.sdio.txglomsz;
783 }
784 
785 #ifdef CONFIG_PM_SLEEP
786 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev)
787 {
788 	sdiodev->freezer = kzalloc(sizeof(*sdiodev->freezer), GFP_KERNEL);
789 	if (!sdiodev->freezer)
790 		return -ENOMEM;
791 	atomic_set(&sdiodev->freezer->thread_count, 0);
792 	atomic_set(&sdiodev->freezer->freezing, 0);
793 	init_waitqueue_head(&sdiodev->freezer->thread_freeze);
794 	init_completion(&sdiodev->freezer->resumed);
795 	return 0;
796 }
797 
798 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev)
799 {
800 	if (sdiodev->freezer) {
801 		WARN_ON(atomic_read(&sdiodev->freezer->freezing));
802 		kfree(sdiodev->freezer);
803 	}
804 }
805 
806 static int brcmf_sdiod_freezer_on(struct brcmf_sdio_dev *sdiodev)
807 {
808 	atomic_t *expect = &sdiodev->freezer->thread_count;
809 	int res = 0;
810 
811 	sdiodev->freezer->frozen_count = 0;
812 	reinit_completion(&sdiodev->freezer->resumed);
813 	atomic_set(&sdiodev->freezer->freezing, 1);
814 	brcmf_sdio_trigger_dpc(sdiodev->bus);
815 	wait_event(sdiodev->freezer->thread_freeze,
816 		   atomic_read(expect) == sdiodev->freezer->frozen_count);
817 	sdio_claim_host(sdiodev->func1);
818 	res = brcmf_sdio_sleep(sdiodev->bus, true);
819 	sdio_release_host(sdiodev->func1);
820 	return res;
821 }
822 
823 static void brcmf_sdiod_freezer_off(struct brcmf_sdio_dev *sdiodev)
824 {
825 	sdio_claim_host(sdiodev->func1);
826 	brcmf_sdio_sleep(sdiodev->bus, false);
827 	sdio_release_host(sdiodev->func1);
828 	atomic_set(&sdiodev->freezer->freezing, 0);
829 	complete_all(&sdiodev->freezer->resumed);
830 }
831 
832 bool brcmf_sdiod_freezing(struct brcmf_sdio_dev *sdiodev)
833 {
834 	return atomic_read(&sdiodev->freezer->freezing);
835 }
836 
837 void brcmf_sdiod_try_freeze(struct brcmf_sdio_dev *sdiodev)
838 {
839 	if (!brcmf_sdiod_freezing(sdiodev))
840 		return;
841 	sdiodev->freezer->frozen_count++;
842 	wake_up(&sdiodev->freezer->thread_freeze);
843 	wait_for_completion(&sdiodev->freezer->resumed);
844 }
845 
846 void brcmf_sdiod_freezer_count(struct brcmf_sdio_dev *sdiodev)
847 {
848 	atomic_inc(&sdiodev->freezer->thread_count);
849 }
850 
851 void brcmf_sdiod_freezer_uncount(struct brcmf_sdio_dev *sdiodev)
852 {
853 	atomic_dec(&sdiodev->freezer->thread_count);
854 }
855 #else
856 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev)
857 {
858 	return 0;
859 }
860 
861 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev)
862 {
863 }
864 #endif /* CONFIG_PM_SLEEP */
865 
866 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
867 {
868 	sdiodev->state = BRCMF_SDIOD_DOWN;
869 	if (sdiodev->bus) {
870 		brcmf_sdio_remove(sdiodev->bus);
871 		sdiodev->bus = NULL;
872 	}
873 
874 	brcmf_sdiod_freezer_detach(sdiodev);
875 
876 	/* Disable Function 2 */
877 	sdio_claim_host(sdiodev->func2);
878 	sdio_disable_func(sdiodev->func2);
879 	sdio_release_host(sdiodev->func2);
880 
881 	/* Disable Function 1 */
882 	sdio_claim_host(sdiodev->func1);
883 	sdio_disable_func(sdiodev->func1);
884 	sdio_release_host(sdiodev->func1);
885 
886 	sg_free_table(&sdiodev->sgtable);
887 	sdiodev->sbwad = 0;
888 
889 	pm_runtime_allow(sdiodev->func1->card->host->parent);
890 	return 0;
891 }
892 
893 static void brcmf_sdiod_host_fixup(struct mmc_host *host)
894 {
895 	/* runtime-pm powers off the device */
896 	pm_runtime_forbid(host->parent);
897 	/* avoid removal detection upon resume */
898 	host->caps |= MMC_CAP_NONREMOVABLE;
899 }
900 
901 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
902 {
903 	int ret = 0;
904 	unsigned int f2_blksz = SDIO_FUNC2_BLOCKSIZE;
905 
906 	sdio_claim_host(sdiodev->func1);
907 
908 	ret = sdio_set_block_size(sdiodev->func1, SDIO_FUNC1_BLOCKSIZE);
909 	if (ret) {
910 		brcmf_err("Failed to set F1 blocksize\n");
911 		sdio_release_host(sdiodev->func1);
912 		goto out;
913 	}
914 	switch (sdiodev->func2->device) {
915 	case SDIO_DEVICE_ID_BROADCOM_CYPRESS_4373:
916 		f2_blksz = SDIO_4373_FUNC2_BLOCKSIZE;
917 		break;
918 	case SDIO_DEVICE_ID_BROADCOM_4359:
919 		/* fallthrough */
920 	case SDIO_DEVICE_ID_BROADCOM_4354:
921 		/* fallthrough */
922 	case SDIO_DEVICE_ID_BROADCOM_4356:
923 		f2_blksz = SDIO_435X_FUNC2_BLOCKSIZE;
924 		break;
925 	default:
926 		break;
927 	}
928 
929 	ret = sdio_set_block_size(sdiodev->func2, f2_blksz);
930 	if (ret) {
931 		brcmf_err("Failed to set F2 blocksize\n");
932 		sdio_release_host(sdiodev->func1);
933 		goto out;
934 	} else {
935 		brcmf_dbg(SDIO, "set F2 blocksize to %d\n", f2_blksz);
936 	}
937 
938 	/* increase F2 timeout */
939 	sdiodev->func2->enable_timeout = SDIO_WAIT_F2RDY;
940 
941 	/* Enable Function 1 */
942 	ret = sdio_enable_func(sdiodev->func1);
943 	sdio_release_host(sdiodev->func1);
944 	if (ret) {
945 		brcmf_err("Failed to enable F1: err=%d\n", ret);
946 		goto out;
947 	}
948 
949 	ret = brcmf_sdiod_freezer_attach(sdiodev);
950 	if (ret)
951 		goto out;
952 
953 	/* try to attach to the target device */
954 	sdiodev->bus = brcmf_sdio_probe(sdiodev);
955 	if (!sdiodev->bus) {
956 		ret = -ENODEV;
957 		goto out;
958 	}
959 	brcmf_sdiod_host_fixup(sdiodev->func2->card->host);
960 out:
961 	if (ret)
962 		brcmf_sdiod_remove(sdiodev);
963 
964 	return ret;
965 }
966 
967 #define BRCMF_SDIO_DEVICE(dev_id)	\
968 	{SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, dev_id)}
969 
970 /* devices we support, null terminated */
971 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
972 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43143),
973 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43241),
974 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4329),
975 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4330),
976 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4334),
977 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43340),
978 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43341),
979 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43362),
980 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43364),
981 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4335_4339),
982 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4339),
983 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43430),
984 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4345),
985 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43455),
986 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4354),
987 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4356),
988 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4359),
989 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_CYPRESS_4373),
990 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_CYPRESS_43012),
991 	BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_CYPRESS_89359),
992 	{ /* end: all zeroes */ }
993 };
994 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
995 
996 
997 static void brcmf_sdiod_acpi_set_power_manageable(struct device *dev,
998 						  int val)
999 {
1000 #if IS_ENABLED(CONFIG_ACPI)
1001 	struct acpi_device *adev;
1002 
1003 	adev = ACPI_COMPANION(dev);
1004 	if (adev)
1005 		adev->flags.power_manageable = 0;
1006 #endif
1007 }
1008 
1009 static int brcmf_ops_sdio_probe(struct sdio_func *func,
1010 				const struct sdio_device_id *id)
1011 {
1012 	int err;
1013 	struct brcmf_sdio_dev *sdiodev;
1014 	struct brcmf_bus *bus_if;
1015 	struct device *dev;
1016 
1017 	brcmf_dbg(SDIO, "Enter\n");
1018 	brcmf_dbg(SDIO, "Class=%x\n", func->class);
1019 	brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1020 	brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1021 	brcmf_dbg(SDIO, "Function#: %d\n", func->num);
1022 
1023 	dev = &func->dev;
1024 
1025 	/* Set MMC_QUIRK_LENIENT_FN0 for this card */
1026 	func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
1027 
1028 	/* prohibit ACPI power management for this device */
1029 	brcmf_sdiod_acpi_set_power_manageable(dev, 0);
1030 
1031 	/* Consume func num 1 but dont do anything with it. */
1032 	if (func->num == 1)
1033 		return 0;
1034 
1035 	/* Ignore anything but func 2 */
1036 	if (func->num != 2)
1037 		return -ENODEV;
1038 
1039 	bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
1040 	if (!bus_if)
1041 		return -ENOMEM;
1042 	sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
1043 	if (!sdiodev) {
1044 		kfree(bus_if);
1045 		return -ENOMEM;
1046 	}
1047 
1048 	/* store refs to functions used. mmc_card does
1049 	 * not hold the F0 function pointer.
1050 	 */
1051 	sdiodev->func1 = func->card->sdio_func[0];
1052 	sdiodev->func2 = func;
1053 
1054 	sdiodev->bus_if = bus_if;
1055 	bus_if->bus_priv.sdio = sdiodev;
1056 	bus_if->proto_type = BRCMF_PROTO_BCDC;
1057 	dev_set_drvdata(&func->dev, bus_if);
1058 	dev_set_drvdata(&sdiodev->func1->dev, bus_if);
1059 	sdiodev->dev = &sdiodev->func1->dev;
1060 
1061 	brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_DOWN);
1062 
1063 	brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1064 	err = brcmf_sdiod_probe(sdiodev);
1065 	if (err) {
1066 		brcmf_err("F2 error, probe failed %d...\n", err);
1067 		goto fail;
1068 	}
1069 
1070 	brcmf_dbg(SDIO, "F2 init completed...\n");
1071 	return 0;
1072 
1073 fail:
1074 	dev_set_drvdata(&func->dev, NULL);
1075 	dev_set_drvdata(&sdiodev->func1->dev, NULL);
1076 	kfree(sdiodev);
1077 	kfree(bus_if);
1078 	return err;
1079 }
1080 
1081 static void brcmf_ops_sdio_remove(struct sdio_func *func)
1082 {
1083 	struct brcmf_bus *bus_if;
1084 	struct brcmf_sdio_dev *sdiodev;
1085 
1086 	brcmf_dbg(SDIO, "Enter\n");
1087 	brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1088 	brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1089 	brcmf_dbg(SDIO, "Function: %d\n", func->num);
1090 
1091 	bus_if = dev_get_drvdata(&func->dev);
1092 	if (bus_if) {
1093 		sdiodev = bus_if->bus_priv.sdio;
1094 
1095 		/* start by unregistering irqs */
1096 		brcmf_sdiod_intr_unregister(sdiodev);
1097 
1098 		if (func->num != 1)
1099 			return;
1100 
1101 		/* only proceed with rest of cleanup if func 1 */
1102 		brcmf_sdiod_remove(sdiodev);
1103 
1104 		dev_set_drvdata(&sdiodev->func1->dev, NULL);
1105 		dev_set_drvdata(&sdiodev->func2->dev, NULL);
1106 
1107 		kfree(bus_if);
1108 		kfree(sdiodev);
1109 	}
1110 
1111 	brcmf_dbg(SDIO, "Exit\n");
1112 }
1113 
1114 void brcmf_sdio_wowl_config(struct device *dev, bool enabled)
1115 {
1116 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1117 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1118 
1119 	brcmf_dbg(SDIO, "Configuring WOWL, enabled=%d\n", enabled);
1120 	sdiodev->wowl_enabled = enabled;
1121 }
1122 
1123 #ifdef CONFIG_PM_SLEEP
1124 static int brcmf_ops_sdio_suspend(struct device *dev)
1125 {
1126 	struct sdio_func *func;
1127 	struct brcmf_bus *bus_if;
1128 	struct brcmf_sdio_dev *sdiodev;
1129 	mmc_pm_flag_t pm_caps, sdio_flags;
1130 	int ret = 0;
1131 
1132 	func = container_of(dev, struct sdio_func, dev);
1133 	brcmf_dbg(SDIO, "Enter: F%d\n", func->num);
1134 	if (func->num != 1)
1135 		return 0;
1136 
1137 
1138 	bus_if = dev_get_drvdata(dev);
1139 	sdiodev = bus_if->bus_priv.sdio;
1140 
1141 	pm_caps = sdio_get_host_pm_caps(func);
1142 
1143 	if (pm_caps & MMC_PM_KEEP_POWER) {
1144 		/* preserve card power during suspend */
1145 		brcmf_sdiod_freezer_on(sdiodev);
1146 		brcmf_sdio_wd_timer(sdiodev->bus, 0);
1147 
1148 		sdio_flags = MMC_PM_KEEP_POWER;
1149 		if (sdiodev->wowl_enabled) {
1150 			if (sdiodev->settings->bus.sdio.oob_irq_supported)
1151 				enable_irq_wake(sdiodev->settings->bus.sdio.oob_irq_nr);
1152 			else
1153 				sdio_flags |= MMC_PM_WAKE_SDIO_IRQ;
1154 		}
1155 
1156 		if (sdio_set_host_pm_flags(sdiodev->func1, sdio_flags))
1157 			brcmf_err("Failed to set pm_flags %x\n", sdio_flags);
1158 
1159 	} else {
1160 		/* power will be cut so remove device, probe again in resume */
1161 		brcmf_sdiod_intr_unregister(sdiodev);
1162 		ret = brcmf_sdiod_remove(sdiodev);
1163 		if (ret)
1164 			brcmf_err("Failed to remove device on suspend\n");
1165 	}
1166 
1167 	return ret;
1168 }
1169 
1170 static int brcmf_ops_sdio_resume(struct device *dev)
1171 {
1172 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1173 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1174 	struct sdio_func *func = container_of(dev, struct sdio_func, dev);
1175 	mmc_pm_flag_t pm_caps = sdio_get_host_pm_caps(func);
1176 	int ret = 0;
1177 
1178 	brcmf_dbg(SDIO, "Enter: F%d\n", func->num);
1179 	if (func->num != 2)
1180 		return 0;
1181 
1182 	if (!(pm_caps & MMC_PM_KEEP_POWER)) {
1183 		/* bus was powered off and device removed, probe again */
1184 		ret = brcmf_sdiod_probe(sdiodev);
1185 		if (ret)
1186 			brcmf_err("Failed to probe device on resume\n");
1187 	} else {
1188 		if (sdiodev->wowl_enabled &&
1189 		    sdiodev->settings->bus.sdio.oob_irq_supported)
1190 			disable_irq_wake(sdiodev->settings->bus.sdio.oob_irq_nr);
1191 
1192 		brcmf_sdiod_freezer_off(sdiodev);
1193 	}
1194 
1195 	return ret;
1196 }
1197 
1198 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
1199 	.suspend	= brcmf_ops_sdio_suspend,
1200 	.resume		= brcmf_ops_sdio_resume,
1201 };
1202 #endif	/* CONFIG_PM_SLEEP */
1203 
1204 static struct sdio_driver brcmf_sdmmc_driver = {
1205 	.probe = brcmf_ops_sdio_probe,
1206 	.remove = brcmf_ops_sdio_remove,
1207 	.name = KBUILD_MODNAME,
1208 	.id_table = brcmf_sdmmc_ids,
1209 	.drv = {
1210 		.owner = THIS_MODULE,
1211 #ifdef CONFIG_PM_SLEEP
1212 		.pm = &brcmf_sdio_pm_ops,
1213 #endif	/* CONFIG_PM_SLEEP */
1214 		.coredump = brcmf_dev_coredump,
1215 	},
1216 };
1217 
1218 void brcmf_sdio_register(void)
1219 {
1220 	int ret;
1221 
1222 	ret = sdio_register_driver(&brcmf_sdmmc_driver);
1223 	if (ret)
1224 		brcmf_err("sdio_register_driver failed: %d\n", ret);
1225 }
1226 
1227 void brcmf_sdio_exit(void)
1228 {
1229 	brcmf_dbg(SDIO, "Enter\n");
1230 
1231 	sdio_unregister_driver(&brcmf_sdmmc_driver);
1232 }
1233 
1234