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