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 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/types.h>
20 #include <linux/netdevice.h>
21 #include <linux/mmc/sdio.h>
22 #include <linux/mmc/core.h>
23 #include <linux/mmc/sdio_func.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/card.h>
26 #include <linux/suspend.h>
27 #include <linux/errno.h>
28 #include <linux/sched.h>	/* request_irq() */
29 #include <linux/module.h>
30 #include <net/cfg80211.h>
31 
32 #include <defs.h>
33 #include <brcm_hw_ids.h>
34 #include <brcmu_utils.h>
35 #include <brcmu_wifi.h>
36 #include "sdio_host.h"
37 #include "dhd_dbg.h"
38 #include "dhd_bus.h"
39 
40 #define SDIO_VENDOR_ID_BROADCOM		0x02d0
41 
42 #define DMA_ALIGN_MASK	0x03
43 
44 #define SDIO_DEVICE_ID_BROADCOM_4329	0x4329
45 #define SDIO_DEVICE_ID_BROADCOM_4330	0x4330
46 
47 #define SDIO_FUNC1_BLOCKSIZE		64
48 #define SDIO_FUNC2_BLOCKSIZE		512
49 
50 /* devices we support, null terminated */
51 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
52 	{SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
53 	{SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
54 	{ /* end: all zeroes */ },
55 };
56 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
57 
58 static bool
brcmf_pm_resume_error(struct brcmf_sdio_dev * sdiodev)59 brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
60 {
61 	bool is_err = false;
62 #ifdef CONFIG_PM_SLEEP
63 	is_err = atomic_read(&sdiodev->suspend);
64 #endif
65 	return is_err;
66 }
67 
68 static void
brcmf_pm_resume_wait(struct brcmf_sdio_dev * sdiodev,wait_queue_head_t * wq)69 brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq)
70 {
71 #ifdef CONFIG_PM_SLEEP
72 	int retry = 0;
73 	while (atomic_read(&sdiodev->suspend) && retry++ != 30)
74 		wait_event_timeout(*wq, false, HZ/100);
75 #endif
76 }
77 
brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev * sdiodev,uint regaddr,u8 * byte)78 static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev,
79 					    uint regaddr, u8 *byte)
80 {
81 	struct sdio_func *sdfunc = sdiodev->func[0];
82 	int err_ret;
83 
84 	/*
85 	 * Can only directly write to some F0 registers.
86 	 * Handle F2 enable/disable and Abort command
87 	 * as a special case.
88 	 */
89 	if (regaddr == SDIO_CCCR_IOEx) {
90 		sdfunc = sdiodev->func[2];
91 		if (sdfunc) {
92 			sdio_claim_host(sdfunc);
93 			if (*byte & SDIO_FUNC_ENABLE_2) {
94 				/* Enable Function 2 */
95 				err_ret = sdio_enable_func(sdfunc);
96 				if (err_ret)
97 					brcmf_dbg(ERROR,
98 						  "enable F2 failed:%d\n",
99 						  err_ret);
100 			} else {
101 				/* Disable Function 2 */
102 				err_ret = sdio_disable_func(sdfunc);
103 				if (err_ret)
104 					brcmf_dbg(ERROR,
105 						  "Disable F2 failed:%d\n",
106 						  err_ret);
107 			}
108 			sdio_release_host(sdfunc);
109 		}
110 	} else if (regaddr == SDIO_CCCR_ABORT) {
111 		sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func),
112 				 GFP_KERNEL);
113 		if (!sdfunc)
114 			return -ENOMEM;
115 		sdfunc->num = 0;
116 		sdio_claim_host(sdfunc);
117 		sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
118 		sdio_release_host(sdfunc);
119 		kfree(sdfunc);
120 	} else if (regaddr < 0xF0) {
121 		brcmf_dbg(ERROR, "F0 Wr:0x%02x: write disallowed\n", regaddr);
122 		err_ret = -EPERM;
123 	} else {
124 		sdio_claim_host(sdfunc);
125 		sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
126 		sdio_release_host(sdfunc);
127 	}
128 
129 	return err_ret;
130 }
131 
brcmf_sdioh_request_byte(struct brcmf_sdio_dev * sdiodev,uint rw,uint func,uint regaddr,u8 * byte)132 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func,
133 			     uint regaddr, u8 *byte)
134 {
135 	int err_ret;
136 
137 	brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr);
138 
139 	brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait);
140 	if (brcmf_pm_resume_error(sdiodev))
141 		return -EIO;
142 
143 	if (rw && func == 0) {
144 		/* handle F0 separately */
145 		err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte);
146 	} else {
147 		sdio_claim_host(sdiodev->func[func]);
148 		if (rw) /* CMD52 Write */
149 			sdio_writeb(sdiodev->func[func], *byte, regaddr,
150 				    &err_ret);
151 		else if (func == 0) {
152 			*byte = sdio_f0_readb(sdiodev->func[func], regaddr,
153 					      &err_ret);
154 		} else {
155 			*byte = sdio_readb(sdiodev->func[func], regaddr,
156 					   &err_ret);
157 		}
158 		sdio_release_host(sdiodev->func[func]);
159 	}
160 
161 	if (err_ret)
162 		brcmf_dbg(ERROR, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
163 			  rw ? "write" : "read", func, regaddr, *byte, err_ret);
164 
165 	return err_ret;
166 }
167 
brcmf_sdioh_request_word(struct brcmf_sdio_dev * sdiodev,uint rw,uint func,uint addr,u32 * word,uint nbytes)168 int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev,
169 			     uint rw, uint func, uint addr, u32 *word,
170 			     uint nbytes)
171 {
172 	int err_ret = -EIO;
173 
174 	if (func == 0) {
175 		brcmf_dbg(ERROR, "Only CMD52 allowed to F0\n");
176 		return -EINVAL;
177 	}
178 
179 	brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
180 		  rw, func, addr, nbytes);
181 
182 	brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
183 	if (brcmf_pm_resume_error(sdiodev))
184 		return -EIO;
185 	/* Claim host controller */
186 	sdio_claim_host(sdiodev->func[func]);
187 
188 	if (rw) {		/* CMD52 Write */
189 		if (nbytes == 4)
190 			sdio_writel(sdiodev->func[func], *word, addr,
191 				    &err_ret);
192 		else if (nbytes == 2)
193 			sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
194 				    addr, &err_ret);
195 		else
196 			brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
197 	} else {		/* CMD52 Read */
198 		if (nbytes == 4)
199 			*word = sdio_readl(sdiodev->func[func], addr, &err_ret);
200 		else if (nbytes == 2)
201 			*word = sdio_readw(sdiodev->func[func], addr,
202 					   &err_ret) & 0xFFFF;
203 		else
204 			brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
205 	}
206 
207 	/* Release host controller */
208 	sdio_release_host(sdiodev->func[func]);
209 
210 	if (err_ret)
211 		brcmf_dbg(ERROR, "Failed to %s word, Err: 0x%08x\n",
212 			  rw ? "write" : "read", err_ret);
213 
214 	return err_ret;
215 }
216 
217 /* precondition: host controller is claimed */
218 static int
brcmf_sdioh_request_data(struct brcmf_sdio_dev * sdiodev,uint write,bool fifo,uint func,uint addr,struct sk_buff * pkt,uint pktlen)219 brcmf_sdioh_request_data(struct brcmf_sdio_dev *sdiodev, uint write, bool fifo,
220 			 uint func, uint addr, struct sk_buff *pkt, uint pktlen)
221 {
222 	int err_ret = 0;
223 
224 	if ((write) && (!fifo)) {
225 		err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
226 					   ((u8 *) (pkt->data)), pktlen);
227 	} else if (write) {
228 		err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
229 					   ((u8 *) (pkt->data)), pktlen);
230 	} else if (fifo) {
231 		err_ret = sdio_readsb(sdiodev->func[func],
232 				      ((u8 *) (pkt->data)), addr, pktlen);
233 	} else {
234 		err_ret = sdio_memcpy_fromio(sdiodev->func[func],
235 					     ((u8 *) (pkt->data)),
236 					     addr, pktlen);
237 	}
238 
239 	return err_ret;
240 }
241 
242 /*
243  * This function takes a queue of packets. The packets on the queue
244  * are assumed to be properly aligned by the caller.
245  */
246 int
brcmf_sdioh_request_chain(struct brcmf_sdio_dev * sdiodev,uint fix_inc,uint write,uint func,uint addr,struct sk_buff_head * pktq)247 brcmf_sdioh_request_chain(struct brcmf_sdio_dev *sdiodev, uint fix_inc,
248 			  uint write, uint func, uint addr,
249 			  struct sk_buff_head *pktq)
250 {
251 	bool fifo = (fix_inc == SDIOH_DATA_FIX);
252 	u32 SGCount = 0;
253 	int err_ret = 0;
254 
255 	struct sk_buff *pkt;
256 
257 	brcmf_dbg(TRACE, "Enter\n");
258 
259 	brcmf_pm_resume_wait(sdiodev, &sdiodev->request_chain_wait);
260 	if (brcmf_pm_resume_error(sdiodev))
261 		return -EIO;
262 
263 	/* Claim host controller */
264 	sdio_claim_host(sdiodev->func[func]);
265 
266 	skb_queue_walk(pktq, pkt) {
267 		uint pkt_len = pkt->len;
268 		pkt_len += 3;
269 		pkt_len &= 0xFFFFFFFC;
270 
271 		err_ret = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
272 						   addr, pkt, pkt_len);
273 		if (err_ret) {
274 			brcmf_dbg(ERROR, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
275 				  write ? "TX" : "RX", pkt, SGCount, addr,
276 				  pkt_len, err_ret);
277 		} else {
278 			brcmf_dbg(TRACE, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n",
279 				  write ? "TX" : "RX", pkt, SGCount, addr,
280 				  pkt_len);
281 		}
282 		if (!fifo)
283 			addr += pkt_len;
284 
285 		SGCount++;
286 	}
287 
288 	/* Release host controller */
289 	sdio_release_host(sdiodev->func[func]);
290 
291 	brcmf_dbg(TRACE, "Exit\n");
292 	return err_ret;
293 }
294 
295 /*
296  * This function takes a single DMA-able packet.
297  */
brcmf_sdioh_request_buffer(struct brcmf_sdio_dev * sdiodev,uint fix_inc,uint write,uint func,uint addr,struct sk_buff * pkt)298 int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev *sdiodev,
299 			       uint fix_inc, uint write, uint func, uint addr,
300 			       struct sk_buff *pkt)
301 {
302 	int status;
303 	uint pkt_len;
304 	bool fifo = (fix_inc == SDIOH_DATA_FIX);
305 
306 	brcmf_dbg(TRACE, "Enter\n");
307 
308 	if (pkt == NULL)
309 		return -EINVAL;
310 	pkt_len = pkt->len;
311 
312 	brcmf_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
313 	if (brcmf_pm_resume_error(sdiodev))
314 		return -EIO;
315 
316 	/* Claim host controller */
317 	sdio_claim_host(sdiodev->func[func]);
318 
319 	pkt_len += 3;
320 	pkt_len &= (uint)~3;
321 
322 	status = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
323 					   addr, pkt, pkt_len);
324 	if (status) {
325 		brcmf_dbg(ERROR, "%s FAILED %p, addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
326 			  write ? "TX" : "RX", pkt, addr, pkt_len, status);
327 	} else {
328 		brcmf_dbg(TRACE, "%s xfr'd %p, addr=0x%05x, len=%d\n",
329 			  write ? "TX" : "RX", pkt, addr, pkt_len);
330 	}
331 
332 	/* Release host controller */
333 	sdio_release_host(sdiodev->func[func]);
334 
335 	return status;
336 }
337 
338 /* Read client card reg */
339 static int
brcmf_sdioh_card_regread(struct brcmf_sdio_dev * sdiodev,int func,u32 regaddr,int regsize,u32 * data)340 brcmf_sdioh_card_regread(struct brcmf_sdio_dev *sdiodev, int func, u32 regaddr,
341 			 int regsize, u32 *data)
342 {
343 
344 	if ((func == 0) || (regsize == 1)) {
345 		u8 temp = 0;
346 
347 		brcmf_sdioh_request_byte(sdiodev, SDIOH_READ, func, regaddr,
348 					 &temp);
349 		*data = temp;
350 		*data &= 0xff;
351 		brcmf_dbg(DATA, "byte read data=0x%02x\n", *data);
352 	} else {
353 		brcmf_sdioh_request_word(sdiodev, SDIOH_READ, func, regaddr,
354 					 data, regsize);
355 		if (regsize == 2)
356 			*data &= 0xffff;
357 
358 		brcmf_dbg(DATA, "word read data=0x%08x\n", *data);
359 	}
360 
361 	return SUCCESS;
362 }
363 
brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev * sdiodev,u32 regaddr)364 static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev *sdiodev, u32 regaddr)
365 {
366 	/* read 24 bits and return valid 17 bit addr */
367 	int i;
368 	u32 scratch, regdata;
369 	__le32 scratch_le;
370 	u8 *ptr = (u8 *)&scratch_le;
371 
372 	for (i = 0; i < 3; i++) {
373 		if ((brcmf_sdioh_card_regread(sdiodev, 0, regaddr, 1,
374 				&regdata)) != SUCCESS)
375 			brcmf_dbg(ERROR, "Can't read!\n");
376 
377 		*ptr++ = (u8) regdata;
378 		regaddr++;
379 	}
380 
381 	/* Only the lower 17-bits are valid */
382 	scratch = le32_to_cpu(scratch_le);
383 	scratch &= 0x0001FFFF;
384 	return scratch;
385 }
386 
brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev * sdiodev)387 static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev)
388 {
389 	int err_ret;
390 	u32 fbraddr;
391 	u8 func;
392 
393 	brcmf_dbg(TRACE, "\n");
394 
395 	/* Get the Card's common CIS address */
396 	sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev,
397 							   SDIO_CCCR_CIS);
398 	brcmf_dbg(INFO, "Card's Common CIS Ptr = 0x%x\n",
399 		  sdiodev->func_cis_ptr[0]);
400 
401 	/* Get the Card's function CIS (for each function) */
402 	for (fbraddr = SDIO_FBR_BASE(1), func = 1;
403 	     func <= sdiodev->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) {
404 		sdiodev->func_cis_ptr[func] =
405 		    brcmf_sdioh_get_cisaddr(sdiodev, SDIO_FBR_CIS + fbraddr);
406 		brcmf_dbg(INFO, "Function %d CIS Ptr = 0x%x\n",
407 			  func, sdiodev->func_cis_ptr[func]);
408 	}
409 
410 	/* Enable Function 1 */
411 	sdio_claim_host(sdiodev->func[1]);
412 	err_ret = sdio_enable_func(sdiodev->func[1]);
413 	sdio_release_host(sdiodev->func[1]);
414 	if (err_ret)
415 		brcmf_dbg(ERROR, "Failed to enable F1 Err: 0x%08x\n", err_ret);
416 
417 	return false;
418 }
419 
420 /*
421  *	Public entry points & extern's
422  */
brcmf_sdioh_attach(struct brcmf_sdio_dev * sdiodev)423 int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev)
424 {
425 	int err_ret = 0;
426 
427 	brcmf_dbg(TRACE, "\n");
428 
429 	sdiodev->num_funcs = 2;
430 
431 	sdio_claim_host(sdiodev->func[1]);
432 	err_ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
433 	sdio_release_host(sdiodev->func[1]);
434 	if (err_ret) {
435 		brcmf_dbg(ERROR, "Failed to set F1 blocksize\n");
436 		goto out;
437 	}
438 
439 	sdio_claim_host(sdiodev->func[2]);
440 	err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
441 	sdio_release_host(sdiodev->func[2]);
442 	if (err_ret) {
443 		brcmf_dbg(ERROR, "Failed to set F2 blocksize\n");
444 		goto out;
445 	}
446 
447 	brcmf_sdioh_enablefuncs(sdiodev);
448 
449 out:
450 	brcmf_dbg(TRACE, "Done\n");
451 	return err_ret;
452 }
453 
brcmf_sdioh_detach(struct brcmf_sdio_dev * sdiodev)454 void brcmf_sdioh_detach(struct brcmf_sdio_dev *sdiodev)
455 {
456 	brcmf_dbg(TRACE, "\n");
457 
458 	/* Disable Function 2 */
459 	sdio_claim_host(sdiodev->func[2]);
460 	sdio_disable_func(sdiodev->func[2]);
461 	sdio_release_host(sdiodev->func[2]);
462 
463 	/* Disable Function 1 */
464 	sdio_claim_host(sdiodev->func[1]);
465 	sdio_disable_func(sdiodev->func[1]);
466 	sdio_release_host(sdiodev->func[1]);
467 
468 }
469 
brcmf_ops_sdio_probe(struct sdio_func * func,const struct sdio_device_id * id)470 static int brcmf_ops_sdio_probe(struct sdio_func *func,
471 			      const struct sdio_device_id *id)
472 {
473 	int ret = 0;
474 	struct brcmf_sdio_dev *sdiodev;
475 	struct brcmf_bus *bus_if;
476 	brcmf_dbg(TRACE, "Enter\n");
477 	brcmf_dbg(TRACE, "func->class=%x\n", func->class);
478 	brcmf_dbg(TRACE, "sdio_vendor: 0x%04x\n", func->vendor);
479 	brcmf_dbg(TRACE, "sdio_device: 0x%04x\n", func->device);
480 	brcmf_dbg(TRACE, "Function#: 0x%04x\n", func->num);
481 
482 	if (func->num == 1) {
483 		if (dev_get_drvdata(&func->card->dev)) {
484 			brcmf_dbg(ERROR, "card private drvdata occupied\n");
485 			return -ENXIO;
486 		}
487 		bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
488 		if (!bus_if)
489 			return -ENOMEM;
490 		sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
491 		if (!sdiodev) {
492 			kfree(bus_if);
493 			return -ENOMEM;
494 		}
495 		sdiodev->func[0] = func;
496 		sdiodev->func[1] = func;
497 		sdiodev->bus_if = bus_if;
498 		bus_if->bus_priv.sdio = sdiodev;
499 		bus_if->type = SDIO_BUS;
500 		bus_if->align = BRCMF_SDALIGN;
501 		dev_set_drvdata(&func->card->dev, sdiodev);
502 
503 		atomic_set(&sdiodev->suspend, false);
504 		init_waitqueue_head(&sdiodev->request_byte_wait);
505 		init_waitqueue_head(&sdiodev->request_word_wait);
506 		init_waitqueue_head(&sdiodev->request_chain_wait);
507 		init_waitqueue_head(&sdiodev->request_buffer_wait);
508 	}
509 
510 	if (func->num == 2) {
511 		sdiodev = dev_get_drvdata(&func->card->dev);
512 		if ((!sdiodev) || (sdiodev->func[1]->card != func->card))
513 			return -ENODEV;
514 		sdiodev->func[2] = func;
515 
516 		bus_if = sdiodev->bus_if;
517 		sdiodev->dev = &func->dev;
518 		dev_set_drvdata(&func->dev, bus_if);
519 
520 		brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_probe...\n");
521 		ret = brcmf_sdio_probe(sdiodev);
522 	}
523 
524 	return ret;
525 }
526 
brcmf_ops_sdio_remove(struct sdio_func * func)527 static void brcmf_ops_sdio_remove(struct sdio_func *func)
528 {
529 	struct brcmf_bus *bus_if;
530 	struct brcmf_sdio_dev *sdiodev;
531 	brcmf_dbg(TRACE, "Enter\n");
532 	brcmf_dbg(INFO, "func->class=%x\n", func->class);
533 	brcmf_dbg(INFO, "sdio_vendor: 0x%04x\n", func->vendor);
534 	brcmf_dbg(INFO, "sdio_device: 0x%04x\n", func->device);
535 	brcmf_dbg(INFO, "Function#: 0x%04x\n", func->num);
536 
537 	if (func->num == 2) {
538 		bus_if = dev_get_drvdata(&func->dev);
539 		sdiodev = bus_if->bus_priv.sdio;
540 		brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_remove...\n");
541 		brcmf_sdio_remove(sdiodev);
542 		dev_set_drvdata(&func->card->dev, NULL);
543 		dev_set_drvdata(&func->dev, NULL);
544 		kfree(bus_if);
545 		kfree(sdiodev);
546 	}
547 }
548 
549 #ifdef CONFIG_PM_SLEEP
brcmf_sdio_suspend(struct device * dev)550 static int brcmf_sdio_suspend(struct device *dev)
551 {
552 	mmc_pm_flag_t sdio_flags;
553 	struct sdio_func *func = dev_to_sdio_func(dev);
554 	struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
555 	int ret = 0;
556 
557 	brcmf_dbg(TRACE, "\n");
558 
559 	atomic_set(&sdiodev->suspend, true);
560 
561 	sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
562 	if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
563 		brcmf_dbg(ERROR, "Host can't keep power while suspended\n");
564 		return -EINVAL;
565 	}
566 
567 	ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
568 	if (ret) {
569 		brcmf_dbg(ERROR, "Failed to set pm_flags\n");
570 		return ret;
571 	}
572 
573 	brcmf_sdio_wdtmr_enable(sdiodev, false);
574 
575 	return ret;
576 }
577 
brcmf_sdio_resume(struct device * dev)578 static int brcmf_sdio_resume(struct device *dev)
579 {
580 	struct sdio_func *func = dev_to_sdio_func(dev);
581 	struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
582 
583 	brcmf_sdio_wdtmr_enable(sdiodev, true);
584 	atomic_set(&sdiodev->suspend, false);
585 	return 0;
586 }
587 
588 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
589 	.suspend	= brcmf_sdio_suspend,
590 	.resume		= brcmf_sdio_resume,
591 };
592 #endif	/* CONFIG_PM_SLEEP */
593 
594 static struct sdio_driver brcmf_sdmmc_driver = {
595 	.probe = brcmf_ops_sdio_probe,
596 	.remove = brcmf_ops_sdio_remove,
597 	.name = "brcmfmac",
598 	.id_table = brcmf_sdmmc_ids,
599 #ifdef CONFIG_PM_SLEEP
600 	.drv = {
601 		.pm = &brcmf_sdio_pm_ops,
602 	},
603 #endif	/* CONFIG_PM_SLEEP */
604 };
605 
brcmf_sdio_exit(void)606 void brcmf_sdio_exit(void)
607 {
608 	brcmf_dbg(TRACE, "Enter\n");
609 
610 	sdio_unregister_driver(&brcmf_sdmmc_driver);
611 }
612 
brcmf_sdio_init(void)613 void brcmf_sdio_init(void)
614 {
615 	int ret;
616 
617 	brcmf_dbg(TRACE, "Enter\n");
618 
619 	ret = sdio_register_driver(&brcmf_sdmmc_driver);
620 
621 	if (ret)
622 		brcmf_dbg(ERROR, "sdio_register_driver failed: %d\n", ret);
623 }
624