GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / wireless / marvell / libertas / if_sdio.c
1 /*
2  *  linux/drivers/net/wireless/libertas/if_sdio.c
3  *
4  *  Copyright 2007-2008 Pierre Ossman
5  *
6  * Inspired by if_cs.c, Copyright 2007 Holger Schurig
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  *
13  * This hardware has more or less no CMD53 support, so all registers
14  * must be accessed using sdio_readb()/sdio_writeb().
15  *
16  * Transfers must be in one transaction or the firmware goes bonkers.
17  * This means that the transfer must either be small enough to do a
18  * byte based transfer or it must be padded to a multiple of the
19  * current block size.
20  *
21  * As SDIO is still new to the kernel, it is unfortunately common with
22  * bugs in the host controllers related to that. One such bug is that
23  * controllers cannot do transfers that aren't a multiple of 4 bytes.
24  * If you don't have time to fix the host controller driver, you can
25  * work around the problem by modifying if_sdio_host_to_card() and
26  * if_sdio_card_to_host() to pad the data.
27  */
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/netdevice.h>
36 #include <linux/delay.h>
37 #include <linux/mmc/card.h>
38 #include <linux/mmc/sdio_func.h>
39 #include <linux/mmc/sdio_ids.h>
40 #include <linux/mmc/sdio.h>
41 #include <linux/mmc/host.h>
42 #include <linux/pm_runtime.h>
43
44 #include "host.h"
45 #include "decl.h"
46 #include "defs.h"
47 #include "dev.h"
48 #include "cmd.h"
49 #include "if_sdio.h"
50
51 static void if_sdio_interrupt(struct sdio_func *func);
52
53 /* The if_sdio_remove() callback function is called when
54  * user removes this module from kernel space or ejects
55  * the card from the slot. The driver handles these 2 cases
56  * differently for SD8688 combo chip.
57  * If the user is removing the module, the FUNC_SHUTDOWN
58  * command for SD8688 is sent to the firmware.
59  * If the card is removed, there is no need to send this command.
60  *
61  * The variable 'user_rmmod' is used to distinguish these two
62  * scenarios. This flag is initialized as FALSE in case the card
63  * is removed, and will be set to TRUE for module removal when
64  * module_exit function is called.
65  */
66 static u8 user_rmmod;
67
68 static const struct sdio_device_id if_sdio_ids[] = {
69         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
70                         SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
71         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
72                         SDIO_DEVICE_ID_MARVELL_8688WLAN) },
73         { /* end: all zeroes */                         },
74 };
75
76 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
77
78 #define MODEL_8385      0x04
79 #define MODEL_8686      0x0b
80 #define MODEL_8688      0x10
81
82 static const struct lbs_fw_table fw_table[] = {
83         { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
84         { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
85         { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
86         { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
87         { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
88         { MODEL_8688, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
89         { MODEL_8688, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
90         { 0, NULL, NULL }
91 };
92 /*(DEBLOBBED)*/
93
94 struct if_sdio_packet {
95         struct if_sdio_packet   *next;
96         u16                     nb;
97         u8                      buffer[0] __attribute__((aligned(4)));
98 };
99
100 struct if_sdio_card {
101         struct sdio_func        *func;
102         struct lbs_private      *priv;
103
104         int                     model;
105         unsigned long           ioport;
106         unsigned int            scratch_reg;
107         bool                    started;
108         wait_queue_head_t       pwron_waitq;
109
110         u8                      buffer[65536] __attribute__((aligned(4)));
111
112         spinlock_t              lock;
113         struct if_sdio_packet   *packets;
114
115         struct workqueue_struct *workqueue;
116         struct work_struct      packet_worker;
117
118         u8                      rx_unit;
119 };
120
121 static void if_sdio_finish_power_on(struct if_sdio_card *card);
122 static int if_sdio_power_off(struct if_sdio_card *card);
123
124 /********************************************************************/
125 /* I/O                                                              */
126 /********************************************************************/
127
128 /*
129  *  For SD8385/SD8686, this function reads firmware status after
130  *  the image is downloaded, or reads RX packet length when
131  *  interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
132  *  For SD8688, this function reads firmware status only.
133  */
134 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
135 {
136         int ret;
137         u16 scratch;
138
139         scratch = sdio_readb(card->func, card->scratch_reg, &ret);
140         if (!ret)
141                 scratch |= sdio_readb(card->func, card->scratch_reg + 1,
142                                         &ret) << 8;
143
144         if (err)
145                 *err = ret;
146
147         if (ret)
148                 return 0xffff;
149
150         return scratch;
151 }
152
153 static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
154 {
155         int ret;
156         u8 rx_unit;
157
158         rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
159
160         if (ret)
161                 rx_unit = 0;
162
163         return rx_unit;
164 }
165
166 static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
167 {
168         int ret;
169         u16 rx_len;
170
171         switch (card->model) {
172         case MODEL_8385:
173         case MODEL_8686:
174                 rx_len = if_sdio_read_scratch(card, &ret);
175                 break;
176         case MODEL_8688:
177         default: /* for newer chipsets */
178                 rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
179                 if (!ret)
180                         rx_len <<= card->rx_unit;
181                 else
182                         rx_len = 0xffff;        /* invalid length */
183
184                 break;
185         }
186
187         if (err)
188                 *err = ret;
189
190         return rx_len;
191 }
192
193 static int if_sdio_handle_cmd(struct if_sdio_card *card,
194                 u8 *buffer, unsigned size)
195 {
196         struct lbs_private *priv = card->priv;
197         int ret;
198         unsigned long flags;
199         u8 i;
200
201         if (size > LBS_CMD_BUFFER_SIZE) {
202                 lbs_deb_sdio("response packet too large (%d bytes)\n",
203                         (int)size);
204                 ret = -E2BIG;
205                 goto out;
206         }
207
208         spin_lock_irqsave(&priv->driver_lock, flags);
209
210         i = (priv->resp_idx == 0) ? 1 : 0;
211         BUG_ON(priv->resp_len[i]);
212         priv->resp_len[i] = size;
213         memcpy(priv->resp_buf[i], buffer, size);
214         lbs_notify_command_response(priv, i);
215
216         spin_unlock_irqrestore(&priv->driver_lock, flags);
217
218         ret = 0;
219
220 out:
221         return ret;
222 }
223
224 static int if_sdio_handle_data(struct if_sdio_card *card,
225                 u8 *buffer, unsigned size)
226 {
227         int ret;
228         struct sk_buff *skb;
229
230         if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
231                 lbs_deb_sdio("response packet too large (%d bytes)\n",
232                         (int)size);
233                 ret = -E2BIG;
234                 goto out;
235         }
236
237         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
238         if (!skb) {
239                 ret = -ENOMEM;
240                 goto out;
241         }
242
243         skb_reserve(skb, NET_IP_ALIGN);
244
245         skb_put_data(skb, buffer, size);
246
247         lbs_process_rxed_packet(card->priv, skb);
248
249         ret = 0;
250
251 out:
252         return ret;
253 }
254
255 static int if_sdio_handle_event(struct if_sdio_card *card,
256                 u8 *buffer, unsigned size)
257 {
258         int ret;
259         u32 event;
260
261         if (card->model == MODEL_8385) {
262                 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
263                 if (ret)
264                         goto out;
265
266                 /* right shift 3 bits to get the event id */
267                 event >>= 3;
268         } else {
269                 if (size < 4) {
270                         lbs_deb_sdio("event packet too small (%d bytes)\n",
271                                 (int)size);
272                         ret = -EINVAL;
273                         goto out;
274                 }
275                 event = buffer[3] << 24;
276                 event |= buffer[2] << 16;
277                 event |= buffer[1] << 8;
278                 event |= buffer[0] << 0;
279         }
280
281         lbs_queue_event(card->priv, event & 0xFF);
282         ret = 0;
283
284 out:
285         return ret;
286 }
287
288 static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
289 {
290         u8 status;
291         unsigned long timeout;
292         int ret = 0;
293
294         timeout = jiffies + HZ;
295         while (1) {
296                 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
297                 if (ret)
298                         return ret;
299                 if ((status & condition) == condition)
300                         break;
301                 if (time_after(jiffies, timeout))
302                         return -ETIMEDOUT;
303                 mdelay(1);
304         }
305         return ret;
306 }
307
308 static int if_sdio_card_to_host(struct if_sdio_card *card)
309 {
310         int ret;
311         u16 size, type, chunk;
312
313         size = if_sdio_read_rx_len(card, &ret);
314         if (ret)
315                 goto out;
316
317         if (size < 4) {
318                 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
319                         (int)size);
320                 ret = -EINVAL;
321                 goto out;
322         }
323
324         ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
325         if (ret)
326                 goto out;
327
328         /*
329          * The transfer must be in one transaction or the firmware
330          * goes suicidal. There's no way to guarantee that for all
331          * controllers, but we can at least try.
332          */
333         chunk = sdio_align_size(card->func, size);
334
335         ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
336         if (ret)
337                 goto out;
338
339         chunk = card->buffer[0] | (card->buffer[1] << 8);
340         type = card->buffer[2] | (card->buffer[3] << 8);
341
342         lbs_deb_sdio("packet of type %d and size %d bytes\n",
343                 (int)type, (int)chunk);
344
345         if (chunk > size) {
346                 lbs_deb_sdio("packet fragment (%d > %d)\n",
347                         (int)chunk, (int)size);
348                 ret = -EINVAL;
349                 goto out;
350         }
351
352         if (chunk < size) {
353                 lbs_deb_sdio("packet fragment (%d < %d)\n",
354                         (int)chunk, (int)size);
355         }
356
357         switch (type) {
358         case MVMS_CMD:
359                 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
360                 if (ret)
361                         goto out;
362                 break;
363         case MVMS_DAT:
364                 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
365                 if (ret)
366                         goto out;
367                 break;
368         case MVMS_EVENT:
369                 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
370                 if (ret)
371                         goto out;
372                 break;
373         default:
374                 lbs_deb_sdio("invalid type (%d) from firmware\n",
375                                 (int)type);
376                 ret = -EINVAL;
377                 goto out;
378         }
379
380 out:
381         if (ret)
382                 pr_err("problem fetching packet from firmware\n");
383
384         return ret;
385 }
386
387 static void if_sdio_host_to_card_worker(struct work_struct *work)
388 {
389         struct if_sdio_card *card;
390         struct if_sdio_packet *packet;
391         int ret;
392         unsigned long flags;
393
394         card = container_of(work, struct if_sdio_card, packet_worker);
395
396         while (1) {
397                 spin_lock_irqsave(&card->lock, flags);
398                 packet = card->packets;
399                 if (packet)
400                         card->packets = packet->next;
401                 spin_unlock_irqrestore(&card->lock, flags);
402
403                 if (!packet)
404                         break;
405
406                 sdio_claim_host(card->func);
407
408                 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
409                 if (ret == 0) {
410                         ret = sdio_writesb(card->func, card->ioport,
411                                            packet->buffer, packet->nb);
412                 }
413
414                 if (ret)
415                         pr_err("error %d sending packet to firmware\n", ret);
416
417                 sdio_release_host(card->func);
418
419                 kfree(packet);
420         }
421 }
422
423 /********************************************************************/
424 /* Firmware                                                         */
425 /********************************************************************/
426
427 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
428
429 static int if_sdio_prog_helper(struct if_sdio_card *card,
430                                 const struct firmware *fw)
431 {
432         int ret;
433         unsigned long timeout;
434         u8 *chunk_buffer;
435         u32 chunk_size;
436         const u8 *firmware;
437         size_t size;
438
439         chunk_buffer = kzalloc(64, GFP_KERNEL);
440         if (!chunk_buffer) {
441                 ret = -ENOMEM;
442                 goto out;
443         }
444
445         sdio_claim_host(card->func);
446
447         ret = sdio_set_block_size(card->func, 32);
448         if (ret)
449                 goto release;
450
451         firmware = fw->data;
452         size = fw->size;
453
454         while (size) {
455                 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
456                 if (ret)
457                         goto release;
458
459                 /* On some platforms (like Davinci) the chip needs more time
460                  * between helper blocks.
461                  */
462                 mdelay(2);
463
464                 chunk_size = min_t(size_t, size, 60);
465
466                 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
467                 memcpy(chunk_buffer + 4, firmware, chunk_size);
468 /*
469                 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
470 */
471                 ret = sdio_writesb(card->func, card->ioport,
472                                 chunk_buffer, 64);
473                 if (ret)
474                         goto release;
475
476                 firmware += chunk_size;
477                 size -= chunk_size;
478         }
479
480         /* an empty block marks the end of the transfer */
481         memset(chunk_buffer, 0, 4);
482         ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
483         if (ret)
484                 goto release;
485
486         lbs_deb_sdio("waiting for helper to boot...\n");
487
488         /* wait for the helper to boot by looking at the size register */
489         timeout = jiffies + HZ;
490         while (1) {
491                 u16 req_size;
492
493                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
494                 if (ret)
495                         goto release;
496
497                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
498                 if (ret)
499                         goto release;
500
501                 if (req_size != 0)
502                         break;
503
504                 if (time_after(jiffies, timeout)) {
505                         ret = -ETIMEDOUT;
506                         goto release;
507                 }
508
509                 msleep(10);
510         }
511
512         ret = 0;
513
514 release:
515         sdio_release_host(card->func);
516         kfree(chunk_buffer);
517
518 out:
519         if (ret)
520                 pr_err("failed to load helper firmware\n");
521
522         return ret;
523 }
524
525 static int if_sdio_prog_real(struct if_sdio_card *card,
526                                 const struct firmware *fw)
527 {
528         int ret;
529         unsigned long timeout;
530         u8 *chunk_buffer;
531         u32 chunk_size;
532         const u8 *firmware;
533         size_t size, req_size;
534
535         chunk_buffer = kzalloc(512, GFP_KERNEL);
536         if (!chunk_buffer) {
537                 ret = -ENOMEM;
538                 goto out;
539         }
540
541         sdio_claim_host(card->func);
542
543         ret = sdio_set_block_size(card->func, 32);
544         if (ret)
545                 goto release;
546
547         firmware = fw->data;
548         size = fw->size;
549
550         while (size) {
551                 timeout = jiffies + HZ;
552                 while (1) {
553                         ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
554                         if (ret)
555                                 goto release;
556
557                         req_size = sdio_readb(card->func, IF_SDIO_RD_BASE,
558                                         &ret);
559                         if (ret)
560                                 goto release;
561
562                         req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1,
563                                         &ret) << 8;
564                         if (ret)
565                                 goto release;
566
567                         /*
568                          * For SD8688 wait until the length is not 0, 1 or 2
569                          * before downloading the first FW block,
570                          * since BOOT code writes the register to indicate the
571                          * helper/FW download winner,
572                          * the value could be 1 or 2 (Func1 or Func2).
573                          */
574                         if ((size != fw->size) || (req_size > 2))
575                                 break;
576                         if (time_after(jiffies, timeout)) {
577                                 ret = -ETIMEDOUT;
578                                 goto release;
579                         }
580                         mdelay(1);
581                 }
582
583 /*
584                 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
585 */
586                 if (req_size == 0) {
587                         lbs_deb_sdio("firmware helper gave up early\n");
588                         ret = -EIO;
589                         goto release;
590                 }
591
592                 if (req_size & 0x01) {
593                         lbs_deb_sdio("firmware helper signalled error\n");
594                         ret = -EIO;
595                         goto release;
596                 }
597
598                 if (req_size > size)
599                         req_size = size;
600
601                 while (req_size) {
602                         chunk_size = min_t(size_t, req_size, 512);
603
604                         memcpy(chunk_buffer, firmware, chunk_size);
605 /*
606                         lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
607                                 chunk_size, (chunk_size + 31) / 32 * 32);
608 */
609                         ret = sdio_writesb(card->func, card->ioport,
610                                 chunk_buffer, roundup(chunk_size, 32));
611                         if (ret)
612                                 goto release;
613
614                         firmware += chunk_size;
615                         size -= chunk_size;
616                         req_size -= chunk_size;
617                 }
618         }
619
620         ret = 0;
621
622         lbs_deb_sdio("waiting for firmware to boot...\n");
623
624         /* wait for the firmware to boot */
625         timeout = jiffies + HZ;
626         while (1) {
627                 u16 scratch;
628
629                 scratch = if_sdio_read_scratch(card, &ret);
630                 if (ret)
631                         goto release;
632
633                 if (scratch == IF_SDIO_FIRMWARE_OK)
634                         break;
635
636                 if (time_after(jiffies, timeout)) {
637                         ret = -ETIMEDOUT;
638                         goto release;
639                 }
640
641                 msleep(10);
642         }
643
644         ret = 0;
645
646 release:
647         sdio_release_host(card->func);
648         kfree(chunk_buffer);
649
650 out:
651         if (ret)
652                 pr_err("failed to load firmware\n");
653
654         return ret;
655 }
656
657 static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret,
658                                      const struct firmware *helper,
659                                      const struct firmware *mainfw)
660 {
661         struct if_sdio_card *card = priv->card;
662
663         if (ret) {
664                 pr_err("failed to find firmware (%d)\n", ret);
665                 return;
666         }
667
668         ret = if_sdio_prog_helper(card, helper);
669         if (ret)
670                 return;
671
672         lbs_deb_sdio("Helper firmware loaded\n");
673
674         ret = if_sdio_prog_real(card, mainfw);
675         if (ret)
676                 return;
677
678         lbs_deb_sdio("Firmware loaded\n");
679         if_sdio_finish_power_on(card);
680 }
681
682 static int if_sdio_prog_firmware(struct if_sdio_card *card)
683 {
684         int ret;
685         u16 scratch;
686
687         /*
688          * Disable interrupts
689          */
690         sdio_claim_host(card->func);
691         sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
692         sdio_release_host(card->func);
693
694         sdio_claim_host(card->func);
695         scratch = if_sdio_read_scratch(card, &ret);
696         sdio_release_host(card->func);
697
698         lbs_deb_sdio("firmware status = %#x\n", scratch);
699         lbs_deb_sdio("scratch ret = %d\n", ret);
700
701         if (ret)
702                 goto out;
703
704
705         /*
706          * The manual clearly describes that FEDC is the right code to use
707          * to detect firmware presence, but for SD8686 it is not that simple.
708          * Scratch is also used to store the RX packet length, so we lose
709          * the FEDC value early on. So we use a non-zero check in order
710          * to validate firmware presence.
711          * Additionally, the SD8686 in the Gumstix always has the high scratch
712          * bit set, even when the firmware is not loaded. So we have to
713          * exclude that from the test.
714          */
715         if (scratch == IF_SDIO_FIRMWARE_OK) {
716                 lbs_deb_sdio("firmware already loaded\n");
717                 if_sdio_finish_power_on(card);
718                 return 0;
719         } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
720                 lbs_deb_sdio("firmware may be running\n");
721                 if_sdio_finish_power_on(card);
722                 return 0;
723         }
724
725         ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model,
726                                      fw_table, if_sdio_do_prog_firmware);
727
728 out:
729         return ret;
730 }
731
732 /********************************************************************/
733 /* Power management                                                 */
734 /********************************************************************/
735
736 /* Finish power on sequence (after firmware is loaded) */
737 static void if_sdio_finish_power_on(struct if_sdio_card *card)
738 {
739         struct sdio_func *func = card->func;
740         struct lbs_private *priv = card->priv;
741         int ret;
742
743         sdio_claim_host(func);
744         sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
745
746         /*
747          * Get rx_unit if the chip is SD8688 or newer.
748          * SD8385 & SD8686 do not have rx_unit.
749          */
750         if ((card->model != MODEL_8385)
751                         && (card->model != MODEL_8686))
752                 card->rx_unit = if_sdio_read_rx_unit(card);
753         else
754                 card->rx_unit = 0;
755
756         /*
757          * Set up the interrupt handler late.
758          *
759          * If we set it up earlier, the (buggy) hardware generates a spurious
760          * interrupt, even before the interrupt has been enabled, with
761          * CCCR_INTx = 0.
762          *
763          * We register the interrupt handler late so that we can handle any
764          * spurious interrupts, and also to avoid generation of that known
765          * spurious interrupt in the first place.
766          */
767         ret = sdio_claim_irq(func, if_sdio_interrupt);
768         if (ret)
769                 goto release;
770
771         /*
772          * Enable interrupts now that everything is set up
773          */
774         sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
775         if (ret)
776                 goto release_irq;
777
778         sdio_release_host(func);
779
780         /* Set fw_ready before queuing any commands so that
781          * lbs_thread won't block from sending them to firmware.
782          */
783         priv->fw_ready = 1;
784
785         /*
786          * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
787          */
788         if (card->model == MODEL_8688) {
789                 struct cmd_header cmd;
790
791                 memset(&cmd, 0, sizeof(cmd));
792
793                 lbs_deb_sdio("send function INIT command\n");
794                 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
795                                 lbs_cmd_copyback, (unsigned long) &cmd))
796                         netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
797         }
798
799         wake_up(&card->pwron_waitq);
800
801         if (!card->started) {
802                 ret = lbs_start_card(priv);
803                 if_sdio_power_off(card);
804                 if (ret == 0) {
805                         card->started = true;
806                         /* Tell PM core that we don't need the card to be
807                          * powered now */
808                         pm_runtime_put(&func->dev);
809                 }
810         }
811
812         return;
813
814 release_irq:
815         sdio_release_irq(func);
816 release:
817         sdio_release_host(func);
818 }
819
820 static int if_sdio_power_on(struct if_sdio_card *card)
821 {
822         struct sdio_func *func = card->func;
823         struct mmc_host *host = func->card->host;
824         int ret;
825
826         sdio_claim_host(func);
827
828         ret = sdio_enable_func(func);
829         if (ret)
830                 goto release;
831
832         /* For 1-bit transfers to the 8686 model, we need to enable the
833          * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
834          * bit to allow access to non-vendor registers. */
835         if ((card->model == MODEL_8686) &&
836             (host->caps & MMC_CAP_SDIO_IRQ) &&
837             (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
838                 u8 reg;
839
840                 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
841                 reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
842                 if (ret)
843                         goto disable;
844
845                 reg |= SDIO_BUS_ECSI;
846                 sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
847                 if (ret)
848                         goto disable;
849         }
850
851         card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
852         if (ret)
853                 goto disable;
854
855         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
856         if (ret)
857                 goto disable;
858
859         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
860         if (ret)
861                 goto disable;
862
863         sdio_release_host(func);
864         ret = if_sdio_prog_firmware(card);
865         if (ret) {
866                 sdio_claim_host(func);
867                 goto disable;
868         }
869
870         return 0;
871
872 disable:
873         sdio_disable_func(func);
874 release:
875         sdio_release_host(func);
876         return ret;
877 }
878
879 static int if_sdio_power_off(struct if_sdio_card *card)
880 {
881         struct sdio_func *func = card->func;
882         struct lbs_private *priv = card->priv;
883
884         priv->fw_ready = 0;
885
886         sdio_claim_host(func);
887         sdio_release_irq(func);
888         sdio_disable_func(func);
889         sdio_release_host(func);
890         return 0;
891 }
892
893
894 /*******************************************************************/
895 /* Libertas callbacks                                              */
896 /*******************************************************************/
897
898 static int if_sdio_host_to_card(struct lbs_private *priv,
899                 u8 type, u8 *buf, u16 nb)
900 {
901         int ret;
902         struct if_sdio_card *card;
903         struct if_sdio_packet *packet, *cur;
904         u16 size;
905         unsigned long flags;
906
907         card = priv->card;
908
909         if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
910                 ret = -EINVAL;
911                 goto out;
912         }
913
914         /*
915          * The transfer must be in one transaction or the firmware
916          * goes suicidal. There's no way to guarantee that for all
917          * controllers, but we can at least try.
918          */
919         size = sdio_align_size(card->func, nb + 4);
920
921         packet = kzalloc(sizeof(struct if_sdio_packet) + size,
922                         GFP_ATOMIC);
923         if (!packet) {
924                 ret = -ENOMEM;
925                 goto out;
926         }
927
928         packet->next = NULL;
929         packet->nb = size;
930
931         /*
932          * SDIO specific header.
933          */
934         packet->buffer[0] = (nb + 4) & 0xff;
935         packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
936         packet->buffer[2] = type;
937         packet->buffer[3] = 0;
938
939         memcpy(packet->buffer + 4, buf, nb);
940
941         spin_lock_irqsave(&card->lock, flags);
942
943         if (!card->packets)
944                 card->packets = packet;
945         else {
946                 cur = card->packets;
947                 while (cur->next)
948                         cur = cur->next;
949                 cur->next = packet;
950         }
951
952         switch (type) {
953         case MVMS_CMD:
954                 priv->dnld_sent = DNLD_CMD_SENT;
955                 break;
956         case MVMS_DAT:
957                 priv->dnld_sent = DNLD_DATA_SENT;
958                 break;
959         default:
960                 lbs_deb_sdio("unknown packet type %d\n", (int)type);
961         }
962
963         spin_unlock_irqrestore(&card->lock, flags);
964
965         queue_work(card->workqueue, &card->packet_worker);
966
967         ret = 0;
968
969 out:
970         return ret;
971 }
972
973 static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
974 {
975         int ret = -1;
976         struct cmd_header cmd;
977
978         memset(&cmd, 0, sizeof(cmd));
979
980         lbs_deb_sdio("send DEEP_SLEEP command\n");
981         ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
982                         lbs_cmd_copyback, (unsigned long) &cmd);
983         if (ret)
984                 netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
985
986         mdelay(200);
987         return ret;
988 }
989
990 static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
991 {
992         struct if_sdio_card *card = priv->card;
993         int ret = -1;
994
995         sdio_claim_host(card->func);
996
997         sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
998         if (ret)
999                 netdev_err(priv->dev, "sdio_writeb failed!\n");
1000
1001         sdio_release_host(card->func);
1002
1003         return ret;
1004 }
1005
1006 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
1007 {
1008         struct if_sdio_card *card = priv->card;
1009         int ret = -1;
1010
1011         sdio_claim_host(card->func);
1012
1013         sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
1014         if (ret)
1015                 netdev_err(priv->dev, "sdio_writeb failed!\n");
1016
1017         sdio_release_host(card->func);
1018
1019         return ret;
1020
1021 }
1022
1023 static struct mmc_host *reset_host;
1024
1025 static void if_sdio_reset_card_worker(struct work_struct *work)
1026 {
1027         /*
1028          * The actual reset operation must be run outside of lbs_thread. This
1029          * is because mmc_remove_host() will cause the device to be instantly
1030          * destroyed, and the libertas driver then needs to end lbs_thread,
1031          * leading to a deadlock.
1032          *
1033          * We run it in a workqueue totally independent from the if_sdio_card
1034          * instance for that reason.
1035          */
1036
1037         pr_info("Resetting card...");
1038         mmc_remove_host(reset_host);
1039         mmc_add_host(reset_host);
1040 }
1041 static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);
1042
1043 static void if_sdio_reset_card(struct lbs_private *priv)
1044 {
1045         struct if_sdio_card *card = priv->card;
1046
1047         if (work_pending(&card_reset_work))
1048                 return;
1049
1050         reset_host = card->func->card->host;
1051         schedule_work(&card_reset_work);
1052 }
1053
1054 static int if_sdio_power_save(struct lbs_private *priv)
1055 {
1056         struct if_sdio_card *card = priv->card;
1057         int ret;
1058
1059         flush_workqueue(card->workqueue);
1060
1061         ret = if_sdio_power_off(card);
1062
1063         /* Let runtime PM know the card is powered off */
1064         pm_runtime_put_sync(&card->func->dev);
1065
1066         return ret;
1067 }
1068
1069 static int if_sdio_power_restore(struct lbs_private *priv)
1070 {
1071         struct if_sdio_card *card = priv->card;
1072         int r;
1073
1074         /* Make sure the card will not be powered off by runtime PM */
1075         pm_runtime_get_sync(&card->func->dev);
1076
1077         r = if_sdio_power_on(card);
1078         if (r)
1079                 return r;
1080
1081         wait_event(card->pwron_waitq, priv->fw_ready);
1082         return 0;
1083 }
1084
1085
1086 /*******************************************************************/
1087 /* SDIO callbacks                                                  */
1088 /*******************************************************************/
1089
1090 static void if_sdio_interrupt(struct sdio_func *func)
1091 {
1092         int ret;
1093         struct if_sdio_card *card;
1094         u8 cause;
1095
1096         card = sdio_get_drvdata(func);
1097
1098         cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
1099         if (ret || !cause)
1100                 return;
1101
1102         lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
1103
1104         sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
1105         if (ret)
1106                 return;
1107
1108         /*
1109          * Ignore the define name, this really means the card has
1110          * successfully received the command.
1111          */
1112         card->priv->is_activity_detected = 1;
1113         if (cause & IF_SDIO_H_INT_DNLD)
1114                 lbs_host_to_card_done(card->priv);
1115
1116
1117         if (cause & IF_SDIO_H_INT_UPLD) {
1118                 ret = if_sdio_card_to_host(card);
1119                 if (ret)
1120                         return;
1121         }
1122 }
1123
1124 static int if_sdio_probe(struct sdio_func *func,
1125                 const struct sdio_device_id *id)
1126 {
1127         struct if_sdio_card *card;
1128         struct lbs_private *priv;
1129         int ret, i;
1130         unsigned int model;
1131         struct if_sdio_packet *packet;
1132
1133         for (i = 0;i < func->card->num_info;i++) {
1134                 if (sscanf(func->card->info[i],
1135                                 "802.11 SDIO ID: %x", &model) == 1)
1136                         break;
1137                 if (sscanf(func->card->info[i],
1138                                 "ID: %x", &model) == 1)
1139                         break;
1140                 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
1141                         model = MODEL_8385;
1142                         break;
1143                 }
1144         }
1145
1146         if (i == func->card->num_info) {
1147                 pr_err("unable to identify card model\n");
1148                 return -ENODEV;
1149         }
1150
1151         card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
1152         if (!card)
1153                 return -ENOMEM;
1154
1155         card->func = func;
1156         card->model = model;
1157
1158         switch (card->model) {
1159         case MODEL_8385:
1160                 card->scratch_reg = IF_SDIO_SCRATCH_OLD;
1161                 break;
1162         case MODEL_8686:
1163                 card->scratch_reg = IF_SDIO_SCRATCH;
1164                 break;
1165         case MODEL_8688:
1166         default: /* for newer chipsets */
1167                 card->scratch_reg = IF_SDIO_FW_STATUS;
1168                 break;
1169         }
1170
1171         spin_lock_init(&card->lock);
1172         card->workqueue = alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM, 0);
1173         if (unlikely(!card->workqueue)) {
1174                 ret = -ENOMEM;
1175                 goto err_queue;
1176         }
1177         INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
1178         init_waitqueue_head(&card->pwron_waitq);
1179
1180         /* Check if we support this card */
1181         for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1182                 if (card->model == fw_table[i].model)
1183                         break;
1184         }
1185         if (i == ARRAY_SIZE(fw_table)) {
1186                 pr_err("unknown card model 0x%x\n", card->model);
1187                 ret = -ENODEV;
1188                 goto free;
1189         }
1190
1191         sdio_set_drvdata(func, card);
1192
1193         lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1194                         "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1195                         func->class, func->vendor, func->device,
1196                         model, (unsigned)card->ioport);
1197
1198
1199         priv = lbs_add_card(card, &func->dev);
1200         if (!priv) {
1201                 ret = -ENOMEM;
1202                 goto free;
1203         }
1204
1205         card->priv = priv;
1206
1207         priv->card = card;
1208         priv->hw_host_to_card = if_sdio_host_to_card;
1209         priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1210         priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1211         priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1212         priv->reset_card = if_sdio_reset_card;
1213         priv->power_save = if_sdio_power_save;
1214         priv->power_restore = if_sdio_power_restore;
1215         priv->is_polling = !(func->card->host->caps & MMC_CAP_SDIO_IRQ);
1216         ret = if_sdio_power_on(card);
1217         if (ret)
1218                 goto err_activate_card;
1219
1220 out:
1221         return ret;
1222
1223 err_activate_card:
1224         flush_workqueue(card->workqueue);
1225         lbs_remove_card(priv);
1226 free:
1227         destroy_workqueue(card->workqueue);
1228 err_queue:
1229         while (card->packets) {
1230                 packet = card->packets;
1231                 card->packets = card->packets->next;
1232                 kfree(packet);
1233         }
1234
1235         kfree(card);
1236
1237         goto out;
1238 }
1239
1240 static void if_sdio_remove(struct sdio_func *func)
1241 {
1242         struct if_sdio_card *card;
1243         struct if_sdio_packet *packet;
1244
1245         card = sdio_get_drvdata(func);
1246
1247         /* Undo decrement done above in if_sdio_probe */
1248         pm_runtime_get_noresume(&func->dev);
1249
1250         if (user_rmmod && (card->model == MODEL_8688)) {
1251                 /*
1252                  * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1253                  * multiple functions
1254                  */
1255                 struct cmd_header cmd;
1256
1257                 memset(&cmd, 0, sizeof(cmd));
1258
1259                 lbs_deb_sdio("send function SHUTDOWN command\n");
1260                 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1261                                 &cmd, sizeof(cmd), lbs_cmd_copyback,
1262                                 (unsigned long) &cmd))
1263                         pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1264         }
1265
1266
1267         lbs_deb_sdio("call remove card\n");
1268         lbs_stop_card(card->priv);
1269         lbs_remove_card(card->priv);
1270
1271         destroy_workqueue(card->workqueue);
1272
1273         while (card->packets) {
1274                 packet = card->packets;
1275                 card->packets = card->packets->next;
1276                 kfree(packet);
1277         }
1278
1279         kfree(card);
1280 }
1281
1282 static int if_sdio_suspend(struct device *dev)
1283 {
1284         struct sdio_func *func = dev_to_sdio_func(dev);
1285         struct if_sdio_card *card = sdio_get_drvdata(func);
1286         struct lbs_private *priv = card->priv;
1287         int ret;
1288
1289         mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1290         priv->power_up_on_resume = false;
1291
1292         /* If we're powered off anyway, just let the mmc layer remove the
1293          * card. */
1294         if (!lbs_iface_active(priv)) {
1295                 if (priv->fw_ready) {
1296                         priv->power_up_on_resume = true;
1297                         if_sdio_power_off(card);
1298                 }
1299
1300                 return 0;
1301         }
1302
1303         dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
1304                  sdio_func_id(func), flags);
1305
1306         /* If we aren't being asked to wake on anything, we should bail out
1307          * and let the SD stack power down the card.
1308          */
1309         if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1310                 dev_info(dev, "Suspend without wake params -- powering down card\n");
1311                 if (priv->fw_ready) {
1312                         ret = lbs_suspend(priv);
1313                         if (ret)
1314                                 return ret;
1315
1316                         priv->power_up_on_resume = true;
1317                         if_sdio_power_off(card);
1318                 }
1319
1320                 return 0;
1321         }
1322
1323         if (!(flags & MMC_PM_KEEP_POWER)) {
1324                 dev_err(dev, "%s: cannot remain alive while host is suspended\n",
1325                         sdio_func_id(func));
1326                 return -ENOSYS;
1327         }
1328
1329         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1330         if (ret)
1331                 return ret;
1332
1333         ret = lbs_suspend(priv);
1334         if (ret)
1335                 return ret;
1336
1337         return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1338 }
1339
1340 static int if_sdio_resume(struct device *dev)
1341 {
1342         struct sdio_func *func = dev_to_sdio_func(dev);
1343         struct if_sdio_card *card = sdio_get_drvdata(func);
1344         int ret;
1345
1346         dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
1347
1348         if (card->priv->power_up_on_resume) {
1349                 if_sdio_power_on(card);
1350                 wait_event(card->pwron_waitq, card->priv->fw_ready);
1351         }
1352
1353         ret = lbs_resume(card->priv);
1354
1355         return ret;
1356 }
1357
1358 static const struct dev_pm_ops if_sdio_pm_ops = {
1359         .suspend        = if_sdio_suspend,
1360         .resume         = if_sdio_resume,
1361 };
1362
1363 static struct sdio_driver if_sdio_driver = {
1364         .name           = "libertas_sdio",
1365         .id_table       = if_sdio_ids,
1366         .probe          = if_sdio_probe,
1367         .remove         = if_sdio_remove,
1368         .drv = {
1369                 .pm = &if_sdio_pm_ops,
1370         },
1371 };
1372
1373 /*******************************************************************/
1374 /* Module functions                                                */
1375 /*******************************************************************/
1376
1377 static int __init if_sdio_init_module(void)
1378 {
1379         int ret = 0;
1380
1381         printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1382         printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1383
1384         ret = sdio_register_driver(&if_sdio_driver);
1385
1386         /* Clear the flag in case user removes the card. */
1387         user_rmmod = 0;
1388
1389         return ret;
1390 }
1391
1392 static void __exit if_sdio_exit_module(void)
1393 {
1394         /* Set the flag as user is removing this module. */
1395         user_rmmod = 1;
1396
1397         cancel_work_sync(&card_reset_work);
1398
1399         sdio_unregister_driver(&if_sdio_driver);
1400 }
1401
1402 module_init(if_sdio_init_module);
1403 module_exit(if_sdio_exit_module);
1404
1405 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1406 MODULE_AUTHOR("Pierre Ossman");
1407 MODULE_LICENSE("GPL");