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