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