13e0381e0bc477d474812d0e777e8634897078c7
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / esp8089 / esp_driver / sdio_sif_esp.c
1 /*
2  * Copyright (c) 2010 -2013 Espressif System.
3  *
4  *   sdio serial i/f driver
5  *    - sdio device control routines
6  *    - sync/async DMA/PIO read/write
7  *
8  */
9 #ifdef ESP_USE_SDIO
10 #include <linux/mmc/card.h>
11 #include <linux/mmc/mmc.h>
12 #include <linux/mmc/core.h>
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/sdio_func.h>
15 #include <linux/mmc/sdio_ids.h>
16 #include <linux/mmc/sdio.h>
17 #include <linux/mmc/sd.h>
18 #include <linux/module.h>
19 #include <net/mac80211.h>
20 #include <linux/time.h>
21 #include <linux/pm.h>
22
23 #include "esp_pub.h"
24 #include "esp_sif.h"
25 #include "esp_sip.h"
26 #include "esp_debug.h"
27 #include "slc_host_register.h"
28 #include "esp_version.h"
29 #include "esp_ctrl.h"
30 #ifdef ANDROID
31 #include "esp_android.h"
32 #endif /* ANDROID */
33 #ifdef USE_EXT_GPIO
34 #include "esp_ext.h"
35 #endif /* USE_EXT_GPIO */
36
37
38 static int  esp_sdio_init(void);
39 static void  esp_sdio_exit(void);
40
41
42 #define ESP_DMA_IBUFSZ   2048
43
44 //unsigned int esp_msg_level = 0;
45 unsigned int esp_msg_level = ESP_DBG_ERROR | ESP_SHOW;
46
47 static struct semaphore esp_powerup_sem;
48
49 static enum esp_sdio_state sif_sdio_state;
50 struct esp_sdio_ctrl *sif_sctrl = NULL;
51
52 #ifdef ESP_ANDROID_LOGGER
53 bool log_off = false;
54 #endif /* ESP_ANDROID_LOGGER */
55
56 static int esdio_power_off(struct esp_sdio_ctrl *sctrl);
57 static int esdio_power_on(struct esp_sdio_ctrl *sctrl);
58
59 void sif_set_clock(struct sdio_func *func, int clk);
60
61 struct sif_req * sif_alloc_req(struct esp_sdio_ctrl *sctrl);
62
63 #include "sdio_stub.c"
64
65 void sif_lock_bus(struct esp_pub *epub)
66 {
67         EPUB_FUNC_CHECK(epub, _exit);
68
69         sdio_claim_host(EPUB_TO_FUNC(epub));
70 _exit:
71         return;
72 }
73
74 void sif_unlock_bus(struct esp_pub *epub)
75 {
76         EPUB_FUNC_CHECK(epub, _exit);
77
78         sdio_release_host(EPUB_TO_FUNC(epub));
79 _exit:
80         return;
81 }
82
83 #ifdef SDIO_TEST
84 static void sif_test_tx(struct esp_sdio_ctrl *sctrl)
85 {
86         int i, err = 0;
87
88         for (i = 0; i < 500; i++) {
89                 sctrl->dma_buffer[i] = i;
90         }
91
92         sdio_claim_host(sctrl->func);
93         err = sdio_memcpy_toio(sctrl->func, 0x10001 - 500, sctrl->dma_buffer, 500);
94         sif_platform_check_r1_ready(sctrl->epub);
95         sdio_release_host(sctrl->func);
96
97         esp_dbg(ESP_DBG, "%s toio err %d\n", __func__, err);
98 }
99
100 static void sif_test_dsr(struct sdio_func *func)
101 {
102         struct esp_sdio_ctrl *sctrl = sdio_get_drvdata(func);
103
104         sdio_release_host(sctrl->func);
105
106         /* no need to read out registers in normal operation any more */
107         //sif_io_sync(sctrl->epub, SIF_SLC_WINDOW_END_ADDR - 64, sctrl->dma_buffer, 64, SIF_FROM_DEVICE | SIF_INC_ADDR | SIF_SYNC | SIF_BYTE_BASIS);
108         //
109         esp_dsr(sctrl->epub);
110
111         sdio_claim_host(func);
112
113         //show_buf(sctrl->dma_buffer, 64);
114 }
115
116 void sif_test_rx(struct esp_sdio_ctrl *sctrl)
117 {
118         int err = 0;
119
120         sdio_claim_host(sctrl->func);
121
122         err = sdio_claim_irq(sctrl->func, sif_test_dsr);
123
124         if (err)
125                 esp_dbg(ESP_DBG_ERROR, "sif %s failed\n", __func__);
126
127         sdio_release_host(sctrl->func);
128 }
129 #endif //SDIO_TEST
130
131 static inline bool bad_buf(u8 * buf)
132 {
133        return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
134 }
135
136 u8 sdio_io_readb(struct esp_pub *epub, int addr, int *res)
137 {
138         struct esp_sdio_ctrl *sctrl = NULL;
139         struct sdio_func *func = NULL;
140         sctrl = (struct esp_sdio_ctrl *)epub->sif;
141         func = sctrl->func;
142
143         if(func->num == 0)
144                 return sdio_f0_readb(func, addr, res);
145         else
146                 return sdio_readb(func, addr, res);
147 }
148
149 void sdio_io_writeb(struct esp_pub *epub, u8 value, int addr, int *res)
150 {
151         struct esp_sdio_ctrl *sctrl = NULL;
152         struct sdio_func *func = NULL;
153         sctrl = (struct esp_sdio_ctrl *)epub->sif;
154         func = sctrl->func;
155         
156         if(func->num == 0)
157                 sdio_f0_writeb(func, value, addr, res);
158         else
159                 sdio_writeb(func, value, addr, res);
160         sif_platform_check_r1_ready(epub);
161 }
162
163 int sif_io_raw(struct esp_pub *epub, u32 addr, u8 *buf, u32 len, u32 flag)
164 {
165         int err = 0;
166         u8 *ibuf = NULL;
167         bool need_ibuf = false;
168         struct esp_sdio_ctrl *sctrl = NULL;
169         struct sdio_func *func = NULL;
170
171         if (epub == NULL || buf == NULL) {
172                 ESSERT(0);
173                 err = -EINVAL;
174                 goto _exit;
175         }
176
177         sctrl = (struct esp_sdio_ctrl *)epub->sif;
178         func = sctrl->func;
179         if (func == NULL) {
180                 ESSERT(0);
181                 err = -EINVAL;
182                 goto _exit;
183         }
184
185         if (bad_buf(buf)) {
186                 esp_dbg(ESP_DBG_TRACE, "%s dst 0x%08x, len %d badbuf\n", __func__, addr, len);
187                 need_ibuf = true;
188                 ibuf = sctrl->dma_buffer;
189         } else {
190                 ibuf = buf;
191         }
192
193         if (flag & SIF_BLOCK_BASIS) {
194                 /* round up for block data transcation */
195         }
196
197         if (flag & SIF_TO_DEVICE) {
198
199                 if (need_ibuf)
200                         memcpy(ibuf, buf, len);
201
202                 if (flag & SIF_FIXED_ADDR)
203                         err = sdio_writesb(func, addr, ibuf, len);
204                 else if (flag & SIF_INC_ADDR) {
205                         err = sdio_memcpy_toio(func, addr, ibuf, len);
206                 }
207                 sif_platform_check_r1_ready(epub);
208         } else if (flag & SIF_FROM_DEVICE) {
209
210                 if (flag & SIF_FIXED_ADDR)
211                         err = sdio_readsb(func, ibuf, addr, len);
212                 else if (flag & SIF_INC_ADDR) {
213                         err = sdio_memcpy_fromio(func, ibuf, addr, len);
214                 }
215
216
217                 if (!err && need_ibuf)
218                         memcpy(buf, ibuf, len);
219         }
220
221 _exit:
222        return err;
223 }
224
225 int sif_io_sync(struct esp_pub *epub, u32 addr, u8 *buf, u32 len, u32 flag)
226 {
227         int err = 0;
228         u8 * ibuf = NULL;
229         bool need_ibuf = false;
230         struct esp_sdio_ctrl *sctrl = NULL;
231         struct sdio_func *func = NULL;
232
233         if (epub == NULL || buf == NULL) {
234                 ESSERT(0);
235                 err = -EINVAL;
236                 goto _exit;
237         }
238
239         sctrl = (struct esp_sdio_ctrl *)epub->sif;
240         func = sctrl->func;
241         if (func == NULL) {
242                 ESSERT(0);      
243                 err = -EINVAL;
244                 goto _exit;
245         }
246
247         if (bad_buf(buf)) {
248                 esp_dbg(ESP_DBG_TRACE, "%s dst 0x%08x, len %d badbuf\n", __func__, addr, len);
249                 need_ibuf = true;
250                 ibuf = sctrl->dma_buffer;
251         } else {
252                 ibuf = buf;
253         }
254
255         if (flag & SIF_BLOCK_BASIS) {
256                 /* round up for block data transcation */
257         }
258
259         if (flag & SIF_TO_DEVICE) {
260
261                 esp_dbg(ESP_DBG_TRACE, "%s to addr 0x%08x, len %d \n", __func__, addr, len);
262                 if (need_ibuf)
263                         memcpy(ibuf, buf, len);
264
265                 sdio_claim_host(func);
266
267                 if (flag & SIF_FIXED_ADDR)
268                         err = sdio_writesb(func, addr, ibuf, len);
269                 else if (flag & SIF_INC_ADDR) {
270                         err = sdio_memcpy_toio(func, addr, ibuf, len);
271                 }
272                 sif_platform_check_r1_ready(epub);
273                 sdio_release_host(func);
274         } else if (flag & SIF_FROM_DEVICE) {
275
276                 esp_dbg(ESP_DBG_TRACE, "%s from addr 0x%08x, len %d \n", __func__, addr, len);
277
278                 sdio_claim_host(func);
279
280                 if (flag & SIF_FIXED_ADDR)
281                         err = sdio_readsb(func, ibuf, addr, len);
282                 else if (flag & SIF_INC_ADDR) {
283                         err = sdio_memcpy_fromio(func, ibuf, addr, len);
284                 }
285
286                 sdio_release_host(func);
287
288                 if (!err && need_ibuf)
289                         memcpy(buf, ibuf, len);
290         }
291
292 _exit:
293         return err;
294 }
295
296 int sif_lldesc_read_sync(struct esp_pub *epub, u8 *buf, u32 len)
297 {
298         struct esp_sdio_ctrl *sctrl = NULL;
299         u32 read_len;
300
301         if (epub == NULL || buf == NULL) {
302                 ESSERT(0);
303                 return -EINVAL;
304         }
305
306         sctrl = (struct esp_sdio_ctrl *)epub->sif;
307
308         switch(sctrl->target_id) {
309         case 0x100:
310                 read_len = len;
311                 break;
312         case 0x600:
313                 read_len = roundup(len, sctrl->slc_blk_sz);
314                 break;
315         default:
316                 read_len = len;
317                 break;
318         }
319
320         return sif_io_sync((epub), (sctrl->slc_window_end_addr - 2 - (len)), (buf), (read_len), SIF_FROM_DEVICE | SIF_BYTE_BASIS | SIF_INC_ADDR);
321 }
322
323 int sif_lldesc_write_sync(struct esp_pub *epub, u8 *buf, u32 len)
324 {
325         struct esp_sdio_ctrl *sctrl = NULL;
326         u32 write_len;
327
328         if (epub == NULL || buf == NULL) {
329                 ESSERT(0);
330                 return -EINVAL;
331         }
332
333         sctrl = (struct esp_sdio_ctrl *)epub->sif;
334
335         switch(sctrl->target_id) {
336         case 0x100:
337                 write_len = len;
338                 break;
339         case 0x600:
340                 write_len = roundup(len, sctrl->slc_blk_sz);
341                 break;
342         default:
343                 write_len = len;
344                 break;
345         }
346
347         return sif_io_sync((epub), (sctrl->slc_window_end_addr - (len)), (buf), (write_len), SIF_TO_DEVICE | SIF_BYTE_BASIS | SIF_INC_ADDR);
348 }
349
350 int sif_lldesc_read_raw(struct esp_pub *epub, u8 *buf, u32 len, bool noround)
351 {
352         struct esp_sdio_ctrl *sctrl = NULL;
353         u32 read_len;
354
355         if (epub == NULL || buf == NULL) {
356                 ESSERT(0);
357                 return -EINVAL;
358         }
359
360         sctrl = (struct esp_sdio_ctrl *)epub->sif;
361
362         switch(sctrl->target_id) {
363         case 0x100:
364                 read_len = len;
365                 break;
366         case 0x600:
367                 if(!noround)
368                         read_len = roundup(len, sctrl->slc_blk_sz);
369                 else
370                         read_len = len;
371                 break;
372         default:
373                 read_len = len;
374                 break;
375         }
376
377         return sif_io_raw((epub), (sctrl->slc_window_end_addr - 2 - (len)), (buf), (read_len), SIF_FROM_DEVICE | SIF_BYTE_BASIS | SIF_INC_ADDR);
378 }
379
380 int sif_lldesc_write_raw(struct esp_pub *epub, u8 *buf, u32 len)
381 {
382         struct esp_sdio_ctrl *sctrl = NULL;
383         u32 write_len;
384
385         if (epub == NULL || buf == NULL) {
386                 ESSERT(0);
387                 return -EINVAL;
388         }
389
390         sctrl = (struct esp_sdio_ctrl *)epub->sif;
391
392         switch(sctrl->target_id) {
393         case 0x100:
394                 write_len = len;
395                 break;
396         case 0x600:
397                 write_len = roundup(len, sctrl->slc_blk_sz);
398                 break;
399         default:
400                 write_len = len;
401                 break;
402         }
403         return sif_io_raw((epub), (sctrl->slc_window_end_addr - (len)), (buf), (write_len), SIF_TO_DEVICE | SIF_BYTE_BASIS | SIF_INC_ADDR);
404
405 }
406
407 #define MANUFACTURER_ID_EAGLE_BASE        0x1110
408 #define MANUFACTURER_ID_EAGLE_BASE_MASK     0xFF00
409 #define MANUFACTURER_CODE                  0x6666
410
411 static const struct sdio_device_id esp_sdio_devices[] = {
412         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_EAGLE_BASE | 0x1))},
413         {},
414 };
415
416 static int esdio_power_on(struct esp_sdio_ctrl *sctrl)
417 {
418         int err = 0;
419
420         if (sctrl->off == false)
421                 return err;
422
423         sdio_claim_host(sctrl->func);
424         err = sdio_enable_func(sctrl->func);
425
426         if (err) {
427                 esp_dbg(ESP_DBG_ERROR, "Unable to enable sdio func: %d\n", err);
428                 sdio_release_host(sctrl->func);
429                 return err;
430         }
431
432         sdio_release_host(sctrl->func);
433
434         /* ensure device is up */
435         msleep(5);
436
437         sctrl->off = false;
438
439         return err;
440 }
441
442 static int esdio_power_off(struct esp_sdio_ctrl *sctrl)
443 {
444         int err;
445
446         if (sctrl->off)
447                 return 0;
448
449         sdio_claim_host(sctrl->func);
450         err = sdio_disable_func(sctrl->func);
451         sdio_release_host(sctrl->func);
452
453         if (err)
454                 return err;
455
456         sctrl->off = true;
457
458         return err;
459 }
460
461 void sif_enable_irq(struct esp_pub *epub) 
462 {
463         int err;
464         struct esp_sdio_ctrl *sctrl = NULL;
465
466         sctrl = (struct esp_sdio_ctrl *)epub->sif;
467
468         sdio_claim_host(sctrl->func);
469
470         err = sdio_claim_irq(sctrl->func, sif_dsr);
471
472         if (err)
473                 esp_dbg(ESP_DBG_ERROR, "sif %s failed\n", __func__);
474
475         atomic_set(&epub->sip->state, SIP_BOOT);
476
477         atomic_set(&sctrl->irq_installed, 1);
478
479         sdio_release_host(sctrl->func);
480 }
481
482 void sif_disable_irq(struct esp_pub *epub) 
483 {
484         int err;
485         struct esp_sdio_ctrl *sctrl = (struct esp_sdio_ctrl *)epub->sif;
486         int i = 0;
487                 
488         if (atomic_read(&sctrl->irq_installed) == 0)
489                 return;
490         
491             sdio_claim_host(sctrl->func);
492
493         while (atomic_read(&sctrl->irq_handling)) {
494                 sdio_release_host(sctrl->func);
495                 schedule_timeout(HZ / 100);
496                 sdio_claim_host(sctrl->func);
497                 if (i++ >= 400) {
498                         esp_dbg(ESP_DBG_ERROR, "%s force to stop irq\n", __func__);
499                         break;
500                 }
501         }
502
503         err = sdio_release_irq(sctrl->func);
504
505         if (err) {
506                 esp_dbg(ESP_DBG_ERROR, "%s release irq failed\n", __func__);
507         }
508
509         atomic_set(&sctrl->irq_installed, 0);
510
511         sdio_release_host(sctrl->func);
512
513 }
514
515 void sif_set_clock(struct sdio_func *func, int clk)
516 {
517         struct mmc_host *host = NULL;
518         struct mmc_card *card = NULL;
519         
520         card = func->card;
521         host = card->host;
522
523         sdio_claim_host(func);
524
525         //currently only set clock
526         host->ios.clock = clk * 1000000;
527
528         esp_dbg(ESP_SHOW, "%s clock is %u\n", __func__, host->ios.clock);
529         if (host->ios.clock > host->f_max) {
530                 host->ios.clock = host->f_max;
531         }
532         host->ops->set_ios(host, &host->ios);
533
534         mdelay(2);
535
536         sdio_release_host(func);
537 }
538
539 static int esp_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id);
540 static void esp_sdio_remove(struct sdio_func *func);
541
542 static int esp_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id) 
543 {
544         int err = 0;
545         struct esp_pub *epub;
546         struct esp_sdio_ctrl *sctrl;
547
548         esp_dbg(ESP_DBG_TRACE,
549                         "sdio_func_num: 0x%X, vendor id: 0x%X, dev id: 0x%X, block size: 0x%X/0x%X\n",
550                         func->num, func->vendor, func->device, func->max_blksize,
551                         func->cur_blksize);
552         if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT){
553                 sctrl = kzalloc(sizeof(struct esp_sdio_ctrl), GFP_KERNEL);
554
555                 if (sctrl == NULL) {
556                         return -ENOMEM;
557                 }
558
559                 /* temp buffer reserved for un-dma-able request */
560                 sctrl->dma_buffer = kzalloc(ESP_DMA_IBUFSZ, GFP_KERNEL);
561
562                 if (sctrl->dma_buffer == NULL) {
563                         err = -ENOMEM;
564                         goto _err_last;
565                 }
566                 sif_sctrl = sctrl;
567                 sctrl->slc_blk_sz = SIF_SLC_BLOCK_SIZE;
568                 
569                 epub = esp_pub_alloc_mac80211(&func->dev);
570
571                 if (epub == NULL) {
572                         esp_dbg(ESP_DBG_ERROR, "no mem for epub \n");
573                         err = -ENOMEM;
574                         goto _err_dma;
575                 }
576                 epub->sif = (void *)sctrl;
577                 sctrl->epub = epub;
578
579 #ifdef USE_EXT_GPIO
580                 if (sif_get_ate_config() == 0) {
581                         err = ext_gpio_init(epub);
582                         if (err) {
583                                 esp_dbg(ESP_DBG_ERROR, "ext_irq_work_init failed %d\n", err);
584                                 goto _err_epub;
585                         }
586                 }
587 #endif
588
589         } else {
590                 sctrl = sif_sctrl;
591                 sif_sctrl = NULL;
592                 epub = sctrl->epub;
593                 SET_IEEE80211_DEV(epub->hw, &func->dev);
594                 epub->dev = &func->dev;
595         }
596
597         epub->sdio_state = sif_sdio_state;
598
599         sctrl->func = func;
600         sdio_set_drvdata(func, sctrl);
601
602         sctrl->id = id;
603         sctrl->off = true;
604
605         /* give us some time to enable, in ms */
606         func->enable_timeout = 100;
607
608         err = esdio_power_on(sctrl);
609         esp_dbg(ESP_DBG_TRACE, " %s >> power_on err %d \n", __func__, err);
610
611         if (err){
612                 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT)
613                         goto _err_ext_gpio;
614                 else
615                         goto _err_second_init;
616         }
617         check_target_id(epub);
618
619         sdio_claim_host(func);
620
621         err = sdio_set_block_size(func, sctrl->slc_blk_sz);
622
623         if (err) {
624                 esp_dbg(ESP_DBG_ERROR, "Set sdio block size %d failed: %d)\n",
625                                 sctrl->slc_blk_sz, err);
626                 sdio_release_host(func);
627                 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT)
628                         goto _err_off;
629                 else
630                         goto _err_second_init;
631         }
632
633         sdio_release_host(func);
634
635 #ifdef SDIO_TEST
636         sif_test_tx(sctrl);
637 #else
638
639 #ifdef LOWER_CLK 
640         /* fix clock for dongle */
641         sif_set_clock(func, 23);
642 #endif //LOWER_CLK
643
644         err = esp_pub_init_all(epub);
645
646         if (err) {
647                 esp_dbg(ESP_DBG_ERROR, "esp_init_all failed: %d\n", err);
648                 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT){
649                         err = 0;
650                         goto _err_first_init;
651                 }
652                 if(sif_sdio_state == ESP_SDIO_STATE_SECOND_INIT)
653                         goto _err_second_init;
654         }
655
656 #endif //SDIO_TEST
657         esp_dbg(ESP_DBG_TRACE, " %s return  %d\n", __func__, err);
658         if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT){
659                 esp_dbg(ESP_DBG_ERROR, "first normal exit\n");
660                 sif_sdio_state = ESP_SDIO_STATE_FIRST_NORMAL_EXIT;
661                 up(&esp_powerup_sem);
662         }
663
664         return err;
665
666 _err_off:
667         esdio_power_off(sctrl);
668 _err_ext_gpio:
669 #ifdef USE_EXT_GPIO
670         if (sif_get_ate_config() == 0)
671                 ext_gpio_deinit();
672 _err_epub:
673 #endif
674         esp_pub_dealloc_mac80211(epub);
675 _err_dma:
676         kfree(sctrl->dma_buffer);
677 _err_last:
678         kfree(sctrl);
679 _err_first_init:
680         if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT){
681                 esp_dbg(ESP_DBG_ERROR, "first error exit\n");
682                 sif_sdio_state = ESP_SDIO_STATE_FIRST_ERROR_EXIT;
683                 up(&esp_powerup_sem);
684         }
685         return err;
686 _err_second_init:
687         sif_sdio_state = ESP_SDIO_STATE_SECOND_ERROR_EXIT;
688         esp_sdio_remove(func);
689         return err;
690 }
691
692 static void esp_sdio_remove(struct sdio_func *func) 
693 {
694         struct esp_sdio_ctrl *sctrl = NULL;
695
696         sctrl = sdio_get_drvdata(func);
697
698         if (sctrl == NULL) {
699                 esp_dbg(ESP_DBG_ERROR, "%s no sctrl\n", __func__);
700                 return;
701         }
702
703         do {
704                 if (sctrl->epub == NULL) {
705                         esp_dbg(ESP_DBG_ERROR, "%s epub null\n", __func__);
706                         break;
707                 }
708                 sctrl->epub->sdio_state = sif_sdio_state;
709                 if(sif_sdio_state != ESP_SDIO_STATE_FIRST_NORMAL_EXIT){
710                         if (sctrl->epub->sip) {
711                                 sip_detach(sctrl->epub->sip);
712                                 sctrl->epub->sip = NULL;
713                                 esp_dbg(ESP_DBG_TRACE, "%s sip detached \n", __func__);
714                         }
715 #ifdef USE_EXT_GPIO     
716                         if (sif_get_ate_config() == 0)
717                                 ext_gpio_deinit();
718 #endif
719                 } else {
720                         //sif_disable_target_interrupt(sctrl->epub);
721                         atomic_set(&sctrl->epub->sip->state, SIP_STOP);
722                         sif_disable_irq(sctrl->epub);
723                 }
724
725 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0))
726                 esdio_power_off(sctrl);
727                 esp_dbg(ESP_DBG_TRACE, "%s power off \n", __func__);
728 #endif /* kernel < 3.3.0 */
729
730 #ifdef TEST_MODE
731                 test_exit_netlink();
732 #endif /* TEST_MODE */
733                 if(sif_sdio_state != ESP_SDIO_STATE_FIRST_NORMAL_EXIT){
734                         esp_pub_dealloc_mac80211(sctrl->epub);
735                         esp_dbg(ESP_DBG_TRACE, "%s dealloc mac80211 \n", __func__);
736                         
737                         if (sctrl->dma_buffer) {
738                                 kfree(sctrl->dma_buffer);
739                                 sctrl->dma_buffer = NULL;
740                                 esp_dbg(ESP_DBG_TRACE, "%s free dma_buffer \n", __func__);
741                         }
742
743                         kfree(sctrl);
744                 }
745
746         } while (0);
747         
748         sdio_set_drvdata(func,NULL);
749         
750         esp_dbg(ESP_DBG_TRACE, "eagle sdio remove complete\n");
751 }
752
753 MODULE_DEVICE_TABLE(sdio, esp_sdio_devices);
754
755 static int esp_sdio_suspend(struct device *dev)
756 {
757     //#define dev_to_sdio_func(d)     container_of(d, struct sdio_func, dev)
758 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
759     struct sdio_func *func = dev_to_sdio_func(dev);
760 #else
761     struct sdio_func *func = container_of(dev, struct sdio_func, dev);
762 #endif
763         struct esp_sdio_ctrl *sctrl = sdio_get_drvdata(func);
764         struct esp_pub *epub = sctrl->epub;     
765
766         printk("%s", __func__);
767 #if 0
768         sip_send_suspend_config(epub, 1);
769 #endif
770         atomic_set(&epub->ps.state, ESP_PM_ON);
771
772 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
773     do{
774         u32 sdio_flags = 0;
775         int ret = 0;
776         sdio_flags = sdio_get_host_pm_caps(func);
777
778         if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
779             printk("%s can't keep power while host is suspended\n", __func__);
780         }
781
782         /* keep power while host suspended */
783         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
784         if (ret) {
785                 printk("%s error while trying to keep power\n", __func__);
786         }
787     }while(0);
788 #endif
789
790
791         return 0;
792
793 }
794
795 static int esp_sdio_resume(struct device *dev)
796 {
797         esp_dbg(ESP_DBG_ERROR, "%s", __func__);
798
799         return 0;
800 }
801
802 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
803 static const struct dev_pm_ops esp_sdio_pm_ops = {
804         .suspend= esp_sdio_suspend,
805         .resume= esp_sdio_resume,
806 };
807 #else
808 static struct pm_ops esp_sdio_pm_ops = {
809         .suspend= esp_sdio_suspend,
810         .resume= esp_sdio_resume,
811 };
812 #endif
813
814 static struct sdio_driver esp_sdio_driver = {
815                 .name = "eagle_sdio",
816                 .id_table = esp_sdio_devices,
817                 .probe = esp_sdio_probe,
818                 .remove = esp_sdio_remove,
819                 .drv = { .pm = &esp_sdio_pm_ops, },
820 };
821
822 static int esp_sdio_dummy_probe(struct sdio_func *func, const struct sdio_device_id *id)
823 {
824         esp_dbg(ESP_DBG_ERROR, "%s enter\n", __func__);
825
826         up(&esp_powerup_sem);
827         
828         return 0;
829 }
830
831 static void esp_sdio_dummy_remove(struct sdio_func *func) 
832 {
833         return;
834 }
835
836 static struct sdio_driver esp_sdio_dummy_driver = {
837                 .name = "eagle_sdio_dummy",
838                 .id_table = esp_sdio_devices,
839                 .probe = esp_sdio_dummy_probe,
840                 .remove = esp_sdio_dummy_remove,
841 };
842
843 static int esp_sdio_init(void) 
844 {
845 #define ESP_WAIT_UP_TIME_MS 11000
846         int err;
847         u64 ver;
848         int retry = 3;
849         bool powerup = false;
850         int edf_ret = 0;
851
852         esp_dbg(ESP_DBG_TRACE, "%s \n", __func__);
853
854 #ifdef DRIVER_VER
855         ver = DRIVER_VER;
856         esp_dbg(ESP_SHOW, "\n*****%s %s EAGLE DRIVER VER:%llx*****\n\n", __DATE__, __TIME__, ver);
857 #endif
858         edf_ret = esp_debugfs_init();
859
860 #ifdef ANDROID
861         android_request_init_conf();
862 #endif /* defined(ANDROID)*/
863
864         esp_wakelock_init();
865         esp_wake_lock();
866
867         do {
868                 sema_init(&esp_powerup_sem, 0);
869
870                 sif_platform_target_poweron();
871
872                 sif_platform_rescan_card(1);
873
874                 err = sdio_register_driver(&esp_sdio_dummy_driver);
875                 if (err) {
876                         esp_dbg(ESP_DBG_ERROR, "eagle sdio driver registration failed, error code: %d\n", err);
877                         goto _fail;
878                 }
879
880                 if (down_timeout(&esp_powerup_sem,
881                                  msecs_to_jiffies(ESP_WAIT_UP_TIME_MS)) == 0) 
882                 {
883
884                         powerup = true;
885                         msleep(200);
886                         break;
887                 }
888
889                 esp_dbg(ESP_SHOW, "%s ------ RETRY ------ \n", __func__);
890
891                 sif_record_retry_config();
892
893                 sdio_unregister_driver(&esp_sdio_dummy_driver);
894
895                 sif_platform_rescan_card(0);
896                 
897                 sif_platform_target_poweroff();
898                 
899         } while (retry--);
900
901         if (!powerup) {
902                 esp_dbg(ESP_DBG_ERROR, "eagle sdio can not power up!\n");
903
904                 err = -ENODEV;
905                 goto _fail;
906         }
907
908         esp_dbg(ESP_SHOW, "%s power up OK\n", __func__);
909
910         sdio_unregister_driver(&esp_sdio_dummy_driver);
911         
912         sif_sdio_state = ESP_SDIO_STATE_FIRST_INIT;
913         sema_init(&esp_powerup_sem, 0);
914
915         sdio_register_driver(&esp_sdio_driver);
916
917         if ((down_timeout(&esp_powerup_sem,
918                                  msecs_to_jiffies(ESP_WAIT_UP_TIME_MS)) == 0 ) && sif_get_ate_config() == 0) {
919                 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_NORMAL_EXIT){
920                         sdio_unregister_driver(&esp_sdio_driver);
921
922                         sif_platform_rescan_card(0);
923
924                         msleep(100);
925                 
926                         sif_platform_rescan_card(1);
927
928                         sif_sdio_state = ESP_SDIO_STATE_SECOND_INIT;
929                 
930                         sdio_register_driver(&esp_sdio_driver);
931                 }
932                 
933         }
934
935
936         esp_register_early_suspend();
937         esp_wake_unlock();
938         return err;
939
940 _fail:
941         esp_wake_unlock();
942         esp_wakelock_destroy();
943
944         return err;
945 }
946
947 static void  esp_sdio_exit(void) 
948 {
949         esp_dbg(ESP_DBG_TRACE, "%s \n", __func__);
950
951         esp_debugfs_exit();
952         
953         esp_unregister_early_suspend();
954
955         sdio_unregister_driver(&esp_sdio_driver);
956         
957         sif_platform_rescan_card(0);
958
959 #ifndef FPGA_DEBUG
960         sif_platform_target_poweroff();
961 #endif /* !FPGA_DEBUG */
962
963         esp_wakelock_destroy();
964 }
965
966
967 MODULE_AUTHOR("Espressif System");
968 MODULE_DESCRIPTION("Driver for SDIO interconnected eagle low-power WLAN devices");
969 MODULE_LICENSE("GPL");
970 #endif /* ESP_USE_SDIO */