Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2

Linux/drivers/net/wireless/brcm80211/brcmfmac/bcmsdh.c

  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/platform_device.h>
 31 #include <linux/platform_data/brcmfmac-sdio.h>
 32 #include <linux/pm_runtime.h>
 33 #include <linux/suspend.h>
 34 #include <linux/errno.h>
 35 #include <linux/module.h>
 36 #include <linux/acpi.h>
 37 #include <net/cfg80211.h>
 38 
 39 #include <defs.h>
 40 #include <brcm_hw_ids.h>
 41 #include <brcmu_utils.h>
 42 #include <brcmu_wifi.h>
 43 #include <chipcommon.h>
 44 #include <soc.h>
 45 #include "chip.h"
 46 #include "bus.h"
 47 #include "debug.h"
 48 #include "sdio.h"
 49 #include "of.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_TXGLOM_SIZE       32  /* max tx frames in glom chain */
 61 #define BRCMF_DEFAULT_RXGLOM_SIZE       32  /* max rx frames in glom chain */
 62 
 63 struct brcmf_sdiod_freezer {
 64         atomic_t freezing;
 65         atomic_t thread_count;
 66         u32 frozen_count;
 67         wait_queue_head_t thread_freeze;
 68         struct completion resumed;
 69 };
 70 
 71 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
 72 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
 73 MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
 74 
 75 static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
 76 {
 77         struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
 78         struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
 79 
 80         brcmf_dbg(INTR, "OOB intr triggered\n");
 81 
 82         /* out-of-band interrupt is level-triggered which won't
 83          * be cleared until dpc
 84          */
 85         if (sdiodev->irq_en) {
 86                 disable_irq_nosync(irq);
 87                 sdiodev->irq_en = false;
 88         }
 89 
 90         brcmf_sdio_isr(sdiodev->bus);
 91 
 92         return IRQ_HANDLED;
 93 }
 94 
 95 static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
 96 {
 97         struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
 98         struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
 99 
100         brcmf_dbg(INTR, "IB intr triggered\n");
101 
102         brcmf_sdio_isr(sdiodev->bus);
103 }
104 
105 /* dummy handler for SDIO function 2 interrupt */
106 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
107 {
108 }
109 
110 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
111 {
112         int ret = 0;
113         u8 data;
114         u32 addr, gpiocontrol;
115         unsigned long flags;
116 
117         if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
118                 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
119                           sdiodev->pdata->oob_irq_nr);
120                 ret = request_irq(sdiodev->pdata->oob_irq_nr,
121                                   brcmf_sdiod_oob_irqhandler,
122                                   sdiodev->pdata->oob_irq_flags,
123                                   "brcmf_oob_intr",
124                                   &sdiodev->func[1]->dev);
125                 if (ret != 0) {
126                         brcmf_err("request_irq failed %d\n", ret);
127                         return ret;
128                 }
129                 sdiodev->oob_irq_requested = true;
130                 spin_lock_init(&sdiodev->irq_en_lock);
131                 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
132                 sdiodev->irq_en = true;
133                 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
134 
135                 ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr);
136                 if (ret != 0) {
137                         brcmf_err("enable_irq_wake failed %d\n", ret);
138                         return ret;
139                 }
140                 sdiodev->irq_wake = true;
141 
142                 sdio_claim_host(sdiodev->func[1]);
143 
144                 if (sdiodev->bus_if->chip == BRCM_CC_43362_CHIP_ID) {
145                         /* assign GPIO to SDIO core */
146                         addr = CORE_CC_REG(SI_ENUM_BASE, gpiocontrol);
147                         gpiocontrol = brcmf_sdiod_regrl(sdiodev, addr, &ret);
148                         gpiocontrol |= 0x2;
149                         brcmf_sdiod_regwl(sdiodev, addr, gpiocontrol, &ret);
150 
151                         brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_SELECT, 0xf,
152                                           &ret);
153                         brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_OUT, 0, &ret);
154                         brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_EN, 0x2, &ret);
155                 }
156 
157                 /* must configure SDIO_CCCR_IENx to enable irq */
158                 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
159                 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
160                 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
161 
162                 /* redirect, configure and enable io for interrupt signal */
163                 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
164                 if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
165                         data |= SDIO_SEPINT_ACT_HI;
166                 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
167 
168                 sdio_release_host(sdiodev->func[1]);
169         } else {
170                 brcmf_dbg(SDIO, "Entering\n");
171                 sdio_claim_host(sdiodev->func[1]);
172                 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler);
173                 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler);
174                 sdio_release_host(sdiodev->func[1]);
175         }
176 
177         return 0;
178 }
179 
180 int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
181 {
182         brcmf_dbg(SDIO, "Entering\n");
183 
184         if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
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                 if (sdiodev->oob_irq_requested) {
191                         sdiodev->oob_irq_requested = false;
192                         if (sdiodev->irq_wake) {
193                                 disable_irq_wake(sdiodev->pdata->oob_irq_nr);
194                                 sdiodev->irq_wake = false;
195                         }
196                         free_irq(sdiodev->pdata->oob_irq_nr,
197                                  &sdiodev->func[1]->dev);
198                         sdiodev->irq_en = false;
199                 }
200         } else {
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         }
206 
207         return 0;
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;
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 (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
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 (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
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                         goto done;
731 
732                 skb_queue_walk(pktq, skb) {
733                         memcpy(skb->data, glom_skb->data, skb->len);
734                         skb_pull(glom_skb, skb->len);
735                 }
736         } else
737                 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr,
738                                             pktq);
739 
740 done:
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 static void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev)
886 {
887         uint nents;
888         int err;
889 
890         if (!sdiodev->sg_support)
891                 return;
892 
893         nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE, brcmf_sdiod_txglomsz);
894         nents += (nents >> 4) + 1;
895 
896         WARN_ON(nents > sdiodev->max_segment_count);
897 
898         brcmf_dbg(TRACE, "nents=%d\n", nents);
899         err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL);
900         if (err < 0) {
901                 brcmf_err("allocation failed: disable scatter-gather");
902                 sdiodev->sg_support = false;
903         }
904 
905         sdiodev->txglomsz = brcmf_sdiod_txglomsz;
906 }
907 
908 #ifdef CONFIG_PM_SLEEP
909 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev)
910 {
911         sdiodev->freezer = kzalloc(sizeof(*sdiodev->freezer), GFP_KERNEL);
912         if (!sdiodev->freezer)
913                 return -ENOMEM;
914         atomic_set(&sdiodev->freezer->thread_count, 0);
915         atomic_set(&sdiodev->freezer->freezing, 0);
916         init_waitqueue_head(&sdiodev->freezer->thread_freeze);
917         init_completion(&sdiodev->freezer->resumed);
918         return 0;
919 }
920 
921 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev)
922 {
923         if (sdiodev->freezer) {
924                 WARN_ON(atomic_read(&sdiodev->freezer->freezing));
925                 kfree(sdiodev->freezer);
926         }
927 }
928 
929 static int brcmf_sdiod_freezer_on(struct brcmf_sdio_dev *sdiodev)
930 {
931         atomic_t *expect = &sdiodev->freezer->thread_count;
932         int res = 0;
933 
934         sdiodev->freezer->frozen_count = 0;
935         reinit_completion(&sdiodev->freezer->resumed);
936         atomic_set(&sdiodev->freezer->freezing, 1);
937         brcmf_sdio_trigger_dpc(sdiodev->bus);
938         wait_event(sdiodev->freezer->thread_freeze,
939                    atomic_read(expect) == sdiodev->freezer->frozen_count);
940         sdio_claim_host(sdiodev->func[1]);
941         res = brcmf_sdio_sleep(sdiodev->bus, true);
942         sdio_release_host(sdiodev->func[1]);
943         return res;
944 }
945 
946 static void brcmf_sdiod_freezer_off(struct brcmf_sdio_dev *sdiodev)
947 {
948         sdio_claim_host(sdiodev->func[1]);
949         brcmf_sdio_sleep(sdiodev->bus, false);
950         sdio_release_host(sdiodev->func[1]);
951         atomic_set(&sdiodev->freezer->freezing, 0);
952         complete_all(&sdiodev->freezer->resumed);
953 }
954 
955 bool brcmf_sdiod_freezing(struct brcmf_sdio_dev *sdiodev)
956 {
957         return atomic_read(&sdiodev->freezer->freezing);
958 }
959 
960 void brcmf_sdiod_try_freeze(struct brcmf_sdio_dev *sdiodev)
961 {
962         if (!brcmf_sdiod_freezing(sdiodev))
963                 return;
964         sdiodev->freezer->frozen_count++;
965         wake_up(&sdiodev->freezer->thread_freeze);
966         wait_for_completion(&sdiodev->freezer->resumed);
967 }
968 
969 void brcmf_sdiod_freezer_count(struct brcmf_sdio_dev *sdiodev)
970 {
971         atomic_inc(&sdiodev->freezer->thread_count);
972 }
973 
974 void brcmf_sdiod_freezer_uncount(struct brcmf_sdio_dev *sdiodev)
975 {
976         atomic_dec(&sdiodev->freezer->thread_count);
977 }
978 #else
979 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev)
980 {
981         return 0;
982 }
983 
984 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev)
985 {
986 }
987 #endif /* CONFIG_PM_SLEEP */
988 
989 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
990 {
991         sdiodev->state = BRCMF_SDIOD_DOWN;
992         if (sdiodev->bus) {
993                 brcmf_sdio_remove(sdiodev->bus);
994                 sdiodev->bus = NULL;
995         }
996 
997         brcmf_sdiod_freezer_detach(sdiodev);
998 
999         /* Disable Function 2 */
1000         sdio_claim_host(sdiodev->func[2]);
1001         sdio_disable_func(sdiodev->func[2]);
1002         sdio_release_host(sdiodev->func[2]);
1003 
1004         /* Disable Function 1 */
1005         sdio_claim_host(sdiodev->func[1]);
1006         sdio_disable_func(sdiodev->func[1]);
1007         sdio_release_host(sdiodev->func[1]);
1008 
1009         sg_free_table(&sdiodev->sgtable);
1010         sdiodev->sbwad = 0;
1011 
1012         pm_runtime_allow(sdiodev->func[1]->card->host->parent);
1013         return 0;
1014 }
1015 
1016 static void brcmf_sdiod_host_fixup(struct mmc_host *host)
1017 {
1018         /* runtime-pm powers off the device */
1019         pm_runtime_forbid(host->parent);
1020         /* avoid removal detection upon resume */
1021         host->caps |= MMC_CAP_NONREMOVABLE;
1022 }
1023 
1024 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
1025 {
1026         struct sdio_func *func;
1027         struct mmc_host *host;
1028         uint max_blocks;
1029         int ret = 0;
1030 
1031         sdiodev->num_funcs = 2;
1032 
1033         sdio_claim_host(sdiodev->func[1]);
1034 
1035         ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
1036         if (ret) {
1037                 brcmf_err("Failed to set F1 blocksize\n");
1038                 sdio_release_host(sdiodev->func[1]);
1039                 goto out;
1040         }
1041         ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
1042         if (ret) {
1043                 brcmf_err("Failed to set F2 blocksize\n");
1044                 sdio_release_host(sdiodev->func[1]);
1045                 goto out;
1046         }
1047 
1048         /* increase F2 timeout */
1049         sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY;
1050 
1051         /* Enable Function 1 */
1052         ret = sdio_enable_func(sdiodev->func[1]);
1053         sdio_release_host(sdiodev->func[1]);
1054         if (ret) {
1055                 brcmf_err("Failed to enable F1: err=%d\n", ret);
1056                 goto out;
1057         }
1058 
1059         /*
1060          * determine host related variables after brcmf_sdiod_probe()
1061          * as func->cur_blksize is properly set and F2 init has been
1062          * completed successfully.
1063          */
1064         func = sdiodev->func[2];
1065         host = func->card->host;
1066         sdiodev->sg_support = host->max_segs > 1;
1067         max_blocks = min_t(uint, host->max_blk_count, 511u);
1068         sdiodev->max_request_size = min_t(uint, host->max_req_size,
1069                                           max_blocks * func->cur_blksize);
1070         sdiodev->max_segment_count = min_t(uint, host->max_segs,
1071                                            SG_MAX_SINGLE_ALLOC);
1072         sdiodev->max_segment_size = host->max_seg_size;
1073 
1074         /* allocate scatter-gather table. sg support
1075          * will be disabled upon allocation failure.
1076          */
1077         brcmf_sdiod_sgtable_alloc(sdiodev);
1078 
1079         ret = brcmf_sdiod_freezer_attach(sdiodev);
1080         if (ret)
1081                 goto out;
1082 
1083         /* try to attach to the target device */
1084         sdiodev->bus = brcmf_sdio_probe(sdiodev);
1085         if (!sdiodev->bus) {
1086                 ret = -ENODEV;
1087                 goto out;
1088         }
1089         brcmf_sdiod_host_fixup(host);
1090 out:
1091         if (ret)
1092                 brcmf_sdiod_remove(sdiodev);
1093 
1094         return ret;
1095 }
1096 
1097 #define BRCMF_SDIO_DEVICE(dev_id)       \
1098         {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, dev_id)}
1099 
1100 /* devices we support, null terminated */
1101 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
1102         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43143),
1103         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43241),
1104         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4329),
1105         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4330),
1106         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4334),
1107         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43340),
1108         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43341),
1109         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43362),
1110         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4335_4339),
1111         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43430),
1112         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4345),
1113         BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4354),
1114         { /* end: all zeroes */ }
1115 };
1116 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
1117 
1118 static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata;
1119 
1120 
1121 static void brcmf_sdiod_acpi_set_power_manageable(struct device *dev,
1122                                                   int val)
1123 {
1124 #if IS_ENABLED(CONFIG_ACPI)
1125         struct acpi_device *adev;
1126 
1127         adev = ACPI_COMPANION(dev);
1128         if (adev)
1129                 adev->flags.power_manageable = 0;
1130 #endif
1131 }
1132 
1133 static int brcmf_ops_sdio_probe(struct sdio_func *func,
1134                                 const struct sdio_device_id *id)
1135 {
1136         int err;
1137         struct brcmf_sdio_dev *sdiodev;
1138         struct brcmf_bus *bus_if;
1139         struct device *dev;
1140 
1141         brcmf_dbg(SDIO, "Enter\n");
1142         brcmf_dbg(SDIO, "Class=%x\n", func->class);
1143         brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1144         brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1145         brcmf_dbg(SDIO, "Function#: %d\n", func->num);
1146 
1147         dev = &func->dev;
1148         /* prohibit ACPI power management for this device */
1149         brcmf_sdiod_acpi_set_power_manageable(dev, 0);
1150 
1151         /* Consume func num 1 but dont do anything with it. */
1152         if (func->num == 1)
1153                 return 0;
1154 
1155         /* Ignore anything but func 2 */
1156         if (func->num != 2)
1157                 return -ENODEV;
1158 
1159         bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
1160         if (!bus_if)
1161                 return -ENOMEM;
1162         sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
1163         if (!sdiodev) {
1164                 kfree(bus_if);
1165                 return -ENOMEM;
1166         }
1167 
1168         /* store refs to functions used. mmc_card does
1169          * not hold the F0 function pointer.
1170          */
1171         sdiodev->func[0] = kmemdup(func, sizeof(*func), GFP_KERNEL);
1172         sdiodev->func[0]->num = 0;
1173         sdiodev->func[1] = func->card->sdio_func[0];
1174         sdiodev->func[2] = func;
1175 
1176         sdiodev->bus_if = bus_if;
1177         bus_if->bus_priv.sdio = sdiodev;
1178         bus_if->proto_type = BRCMF_PROTO_BCDC;
1179         dev_set_drvdata(&func->dev, bus_if);
1180         dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
1181         sdiodev->dev = &sdiodev->func[1]->dev;
1182         sdiodev->pdata = brcmfmac_sdio_pdata;
1183 
1184         if (!sdiodev->pdata)
1185                 brcmf_of_probe(sdiodev);
1186 
1187 #ifdef CONFIG_PM_SLEEP
1188         /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
1189          * is true or when platform data OOB irq is true).
1190          */
1191         if ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_KEEP_POWER) &&
1192             ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_WAKE_SDIO_IRQ) ||
1193              (sdiodev->pdata && sdiodev->pdata->oob_irq_supported)))
1194                 bus_if->wowl_supported = true;
1195 #endif
1196 
1197         brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_DOWN);
1198 
1199         brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1200         err = brcmf_sdiod_probe(sdiodev);
1201         if (err) {
1202                 brcmf_err("F2 error, probe failed %d...\n", err);
1203                 goto fail;
1204         }
1205 
1206         brcmf_dbg(SDIO, "F2 init completed...\n");
1207         return 0;
1208 
1209 fail:
1210         dev_set_drvdata(&func->dev, NULL);
1211         dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1212         kfree(sdiodev->func[0]);
1213         kfree(sdiodev);
1214         kfree(bus_if);
1215         return err;
1216 }
1217 
1218 static void brcmf_ops_sdio_remove(struct sdio_func *func)
1219 {
1220         struct brcmf_bus *bus_if;
1221         struct brcmf_sdio_dev *sdiodev;
1222 
1223         brcmf_dbg(SDIO, "Enter\n");
1224         brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1225         brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1226         brcmf_dbg(SDIO, "Function: %d\n", func->num);
1227 
1228         if (func->num != 1)
1229                 return;
1230 
1231         bus_if = dev_get_drvdata(&func->dev);
1232         if (bus_if) {
1233                 sdiodev = bus_if->bus_priv.sdio;
1234                 brcmf_sdiod_remove(sdiodev);
1235 
1236                 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1237                 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
1238 
1239                 kfree(bus_if);
1240                 kfree(sdiodev->func[0]);
1241                 kfree(sdiodev);
1242         }
1243 
1244         brcmf_dbg(SDIO, "Exit\n");
1245 }
1246 
1247 void brcmf_sdio_wowl_config(struct device *dev, bool enabled)
1248 {
1249         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1250         struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1251 
1252         brcmf_dbg(SDIO, "Configuring WOWL, enabled=%d\n", enabled);
1253         sdiodev->wowl_enabled = enabled;
1254 }
1255 
1256 #ifdef CONFIG_PM_SLEEP
1257 static int brcmf_ops_sdio_suspend(struct device *dev)
1258 {
1259         struct sdio_func *func;
1260         struct brcmf_bus *bus_if;
1261         struct brcmf_sdio_dev *sdiodev;
1262         mmc_pm_flag_t sdio_flags;
1263 
1264         func = container_of(dev, struct sdio_func, dev);
1265         brcmf_dbg(SDIO, "Enter: F%d\n", func->num);
1266         if (func->num != SDIO_FUNC_1)
1267                 return 0;
1268 
1269 
1270         bus_if = dev_get_drvdata(dev);
1271         sdiodev = bus_if->bus_priv.sdio;
1272 
1273         brcmf_sdiod_freezer_on(sdiodev);
1274         brcmf_sdio_wd_timer(sdiodev->bus, 0);
1275 
1276         sdio_flags = MMC_PM_KEEP_POWER;
1277         if (sdiodev->wowl_enabled) {
1278                 if (sdiodev->pdata->oob_irq_supported)
1279                         enable_irq_wake(sdiodev->pdata->oob_irq_nr);
1280                 else
1281                         sdio_flags |= MMC_PM_WAKE_SDIO_IRQ;
1282         }
1283         if (sdio_set_host_pm_flags(sdiodev->func[1], sdio_flags))
1284                 brcmf_err("Failed to set pm_flags %x\n", sdio_flags);
1285         return 0;
1286 }
1287 
1288 static int brcmf_ops_sdio_resume(struct device *dev)
1289 {
1290         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1291         struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1292         struct sdio_func *func = container_of(dev, struct sdio_func, dev);
1293 
1294         brcmf_dbg(SDIO, "Enter: F%d\n", func->num);
1295         if (func->num != SDIO_FUNC_2)
1296                 return 0;
1297 
1298         brcmf_sdiod_freezer_off(sdiodev);
1299         return 0;
1300 }
1301 
1302 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
1303         .suspend        = brcmf_ops_sdio_suspend,
1304         .resume         = brcmf_ops_sdio_resume,
1305 };
1306 #endif  /* CONFIG_PM_SLEEP */
1307 
1308 static struct sdio_driver brcmf_sdmmc_driver = {
1309         .probe = brcmf_ops_sdio_probe,
1310         .remove = brcmf_ops_sdio_remove,
1311         .name = BRCMFMAC_SDIO_PDATA_NAME,
1312         .id_table = brcmf_sdmmc_ids,
1313         .drv = {
1314                 .owner = THIS_MODULE,
1315 #ifdef CONFIG_PM_SLEEP
1316                 .pm = &brcmf_sdio_pm_ops,
1317 #endif  /* CONFIG_PM_SLEEP */
1318         },
1319 };
1320 
1321 static int __init brcmf_sdio_pd_probe(struct platform_device *pdev)
1322 {
1323         brcmf_dbg(SDIO, "Enter\n");
1324 
1325         brcmfmac_sdio_pdata = dev_get_platdata(&pdev->dev);
1326 
1327         if (brcmfmac_sdio_pdata->power_on)
1328                 brcmfmac_sdio_pdata->power_on();
1329 
1330         return 0;
1331 }
1332 
1333 static int brcmf_sdio_pd_remove(struct platform_device *pdev)
1334 {
1335         brcmf_dbg(SDIO, "Enter\n");
1336 
1337         if (brcmfmac_sdio_pdata->power_off)
1338                 brcmfmac_sdio_pdata->power_off();
1339 
1340         sdio_unregister_driver(&brcmf_sdmmc_driver);
1341 
1342         return 0;
1343 }
1344 
1345 static struct platform_driver brcmf_sdio_pd = {
1346         .remove         = brcmf_sdio_pd_remove,
1347         .driver         = {
1348                 .name   = BRCMFMAC_SDIO_PDATA_NAME,
1349         }
1350 };
1351 
1352 void brcmf_sdio_register(void)
1353 {
1354         int ret;
1355 
1356         ret = sdio_register_driver(&brcmf_sdmmc_driver);
1357         if (ret)
1358                 brcmf_err("sdio_register_driver failed: %d\n", ret);
1359 }
1360 
1361 void brcmf_sdio_exit(void)
1362 {
1363         brcmf_dbg(SDIO, "Enter\n");
1364 
1365         if (brcmfmac_sdio_pdata)
1366                 platform_driver_unregister(&brcmf_sdio_pd);
1367         else
1368                 sdio_unregister_driver(&brcmf_sdmmc_driver);
1369 }
1370 
1371 void __init brcmf_sdio_init(void)
1372 {
1373         int ret;
1374 
1375         brcmf_dbg(SDIO, "Enter\n");
1376 
1377         ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
1378         if (ret == -ENODEV)
1379                 brcmf_dbg(SDIO, "No platform data available.\n");
1380 }
1381 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us