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