2 * Copyright (c) 2010 -2013 Espressif System.
4 * sdio serial i/f driver
5 * - sdio device control routines
6 * - sync/async DMA/PIO read/write
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>
26 #include "esp_debug.h"
27 #include "slc_host_register.h"
28 #include "esp_version.h"
31 #include "esp_android.h"
35 #endif /* USE_EXT_GPIO */
38 static int esp_sdio_init(void);
39 static void esp_sdio_exit(void);
42 #define ESP_DMA_IBUFSZ 2048
44 //unsigned int esp_msg_level = 0;
45 unsigned int esp_msg_level = ESP_DBG_ERROR | ESP_SHOW;
47 static struct semaphore esp_powerup_sem;
49 static enum esp_sdio_state sif_sdio_state;
50 struct esp_sdio_ctrl *sif_sctrl = NULL;
52 #ifdef ESP_ANDROID_LOGGER
54 #endif /* ESP_ANDROID_LOGGER */
56 static int esdio_power_off(struct esp_sdio_ctrl *sctrl);
57 static int esdio_power_on(struct esp_sdio_ctrl *sctrl);
59 void sif_set_clock(struct sdio_func *func, int clk);
61 struct sif_req * sif_alloc_req(struct esp_sdio_ctrl *sctrl);
63 #include "sdio_stub.c"
65 void sif_lock_bus(struct esp_pub *epub)
67 EPUB_FUNC_CHECK(epub, _exit);
69 sdio_claim_host(EPUB_TO_FUNC(epub));
74 void sif_unlock_bus(struct esp_pub *epub)
76 EPUB_FUNC_CHECK(epub, _exit);
78 sdio_release_host(EPUB_TO_FUNC(epub));
84 static void sif_test_tx(struct esp_sdio_ctrl *sctrl)
88 for (i = 0; i < 500; i++) {
89 sctrl->dma_buffer[i] = i;
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);
97 esp_dbg(ESP_DBG, "%s toio err %d\n", __func__, err);
100 static void sif_test_dsr(struct sdio_func *func)
102 struct esp_sdio_ctrl *sctrl = sdio_get_drvdata(func);
104 sdio_release_host(sctrl->func);
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);
109 esp_dsr(sctrl->epub);
111 sdio_claim_host(func);
113 //show_buf(sctrl->dma_buffer, 64);
116 void sif_test_rx(struct esp_sdio_ctrl *sctrl)
120 sdio_claim_host(sctrl->func);
122 err = sdio_claim_irq(sctrl->func, sif_test_dsr);
125 esp_dbg(ESP_DBG_ERROR, "sif %s failed\n", __func__);
127 sdio_release_host(sctrl->func);
131 static inline bool bad_buf(u8 * buf)
133 return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
136 u8 sdio_io_readb(struct esp_pub *epub, int addr, int *res)
138 struct esp_sdio_ctrl *sctrl = NULL;
139 struct sdio_func *func = NULL;
140 sctrl = (struct esp_sdio_ctrl *)epub->sif;
144 return sdio_f0_readb(func, addr, res);
146 return sdio_readb(func, addr, res);
149 void sdio_io_writeb(struct esp_pub *epub, u8 value, int addr, int *res)
151 struct esp_sdio_ctrl *sctrl = NULL;
152 struct sdio_func *func = NULL;
153 sctrl = (struct esp_sdio_ctrl *)epub->sif;
157 sdio_f0_writeb(func, value, addr, res);
159 sdio_writeb(func, value, addr, res);
160 sif_platform_check_r1_ready(epub);
163 int sif_io_raw(struct esp_pub *epub, u32 addr, u8 *buf, u32 len, u32 flag)
167 bool need_ibuf = false;
168 struct esp_sdio_ctrl *sctrl = NULL;
169 struct sdio_func *func = NULL;
171 if (epub == NULL || buf == NULL) {
177 sctrl = (struct esp_sdio_ctrl *)epub->sif;
186 esp_dbg(ESP_DBG_TRACE, "%s dst 0x%08x, len %d badbuf\n", __func__, addr, len);
188 ibuf = sctrl->dma_buffer;
193 if (flag & SIF_BLOCK_BASIS) {
194 /* round up for block data transcation */
197 if (flag & SIF_TO_DEVICE) {
200 memcpy(ibuf, buf, len);
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);
207 sif_platform_check_r1_ready(epub);
208 } else if (flag & SIF_FROM_DEVICE) {
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);
217 if (!err && need_ibuf)
218 memcpy(buf, ibuf, len);
225 int sif_io_sync(struct esp_pub *epub, u32 addr, u8 *buf, u32 len, u32 flag)
229 bool need_ibuf = false;
230 struct esp_sdio_ctrl *sctrl = NULL;
231 struct sdio_func *func = NULL;
233 if (epub == NULL || buf == NULL) {
239 sctrl = (struct esp_sdio_ctrl *)epub->sif;
248 esp_dbg(ESP_DBG_TRACE, "%s dst 0x%08x, len %d badbuf\n", __func__, addr, len);
250 ibuf = sctrl->dma_buffer;
255 if (flag & SIF_BLOCK_BASIS) {
256 /* round up for block data transcation */
259 if (flag & SIF_TO_DEVICE) {
261 esp_dbg(ESP_DBG_TRACE, "%s to addr 0x%08x, len %d \n", __func__, addr, len);
263 memcpy(ibuf, buf, len);
265 sdio_claim_host(func);
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);
272 sif_platform_check_r1_ready(epub);
273 sdio_release_host(func);
274 } else if (flag & SIF_FROM_DEVICE) {
276 esp_dbg(ESP_DBG_TRACE, "%s from addr 0x%08x, len %d \n", __func__, addr, len);
278 sdio_claim_host(func);
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);
286 sdio_release_host(func);
288 if (!err && need_ibuf)
289 memcpy(buf, ibuf, len);
296 int sif_lldesc_read_sync(struct esp_pub *epub, u8 *buf, u32 len)
298 struct esp_sdio_ctrl *sctrl = NULL;
301 if (epub == NULL || buf == NULL) {
306 sctrl = (struct esp_sdio_ctrl *)epub->sif;
308 switch(sctrl->target_id) {
313 read_len = roundup(len, sctrl->slc_blk_sz);
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);
323 int sif_lldesc_write_sync(struct esp_pub *epub, u8 *buf, u32 len)
325 struct esp_sdio_ctrl *sctrl = NULL;
328 if (epub == NULL || buf == NULL) {
333 sctrl = (struct esp_sdio_ctrl *)epub->sif;
335 switch(sctrl->target_id) {
340 write_len = roundup(len, sctrl->slc_blk_sz);
347 return sif_io_sync((epub), (sctrl->slc_window_end_addr - (len)), (buf), (write_len), SIF_TO_DEVICE | SIF_BYTE_BASIS | SIF_INC_ADDR);
350 int sif_lldesc_read_raw(struct esp_pub *epub, u8 *buf, u32 len, bool noround)
352 struct esp_sdio_ctrl *sctrl = NULL;
355 if (epub == NULL || buf == NULL) {
360 sctrl = (struct esp_sdio_ctrl *)epub->sif;
362 switch(sctrl->target_id) {
368 read_len = roundup(len, sctrl->slc_blk_sz);
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);
380 int sif_lldesc_write_raw(struct esp_pub *epub, u8 *buf, u32 len)
382 struct esp_sdio_ctrl *sctrl = NULL;
385 if (epub == NULL || buf == NULL) {
390 sctrl = (struct esp_sdio_ctrl *)epub->sif;
392 switch(sctrl->target_id) {
397 write_len = roundup(len, sctrl->slc_blk_sz);
403 return sif_io_raw((epub), (sctrl->slc_window_end_addr - (len)), (buf), (write_len), SIF_TO_DEVICE | SIF_BYTE_BASIS | SIF_INC_ADDR);
407 #define MANUFACTURER_ID_EAGLE_BASE 0x1110
408 #define MANUFACTURER_ID_EAGLE_BASE_MASK 0xFF00
409 #define MANUFACTURER_CODE 0x6666
411 static const struct sdio_device_id esp_sdio_devices[] = {
412 {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_EAGLE_BASE | 0x1))},
416 static int esdio_power_on(struct esp_sdio_ctrl *sctrl)
420 if (sctrl->off == false)
423 sdio_claim_host(sctrl->func);
424 err = sdio_enable_func(sctrl->func);
427 esp_dbg(ESP_DBG_ERROR, "Unable to enable sdio func: %d\n", err);
428 sdio_release_host(sctrl->func);
432 sdio_release_host(sctrl->func);
434 /* ensure device is up */
442 static int esdio_power_off(struct esp_sdio_ctrl *sctrl)
449 sdio_claim_host(sctrl->func);
450 err = sdio_disable_func(sctrl->func);
451 sdio_release_host(sctrl->func);
461 void sif_enable_irq(struct esp_pub *epub)
464 struct esp_sdio_ctrl *sctrl = NULL;
466 sctrl = (struct esp_sdio_ctrl *)epub->sif;
468 sdio_claim_host(sctrl->func);
470 err = sdio_claim_irq(sctrl->func, sif_dsr);
473 esp_dbg(ESP_DBG_ERROR, "sif %s failed\n", __func__);
475 atomic_set(&epub->sip->state, SIP_BOOT);
477 atomic_set(&sctrl->irq_installed, 1);
479 sdio_release_host(sctrl->func);
482 void sif_disable_irq(struct esp_pub *epub)
485 struct esp_sdio_ctrl *sctrl = (struct esp_sdio_ctrl *)epub->sif;
488 if (atomic_read(&sctrl->irq_installed) == 0)
491 sdio_claim_host(sctrl->func);
493 while (atomic_read(&sctrl->irq_handling)) {
494 sdio_release_host(sctrl->func);
495 schedule_timeout(HZ / 100);
496 sdio_claim_host(sctrl->func);
498 esp_dbg(ESP_DBG_ERROR, "%s force to stop irq\n", __func__);
503 err = sdio_release_irq(sctrl->func);
506 esp_dbg(ESP_DBG_ERROR, "%s release irq failed\n", __func__);
509 atomic_set(&sctrl->irq_installed, 0);
511 sdio_release_host(sctrl->func);
515 void sif_set_clock(struct sdio_func *func, int clk)
517 struct mmc_host *host = NULL;
518 struct mmc_card *card = NULL;
523 sdio_claim_host(func);
525 //currently only set clock
526 host->ios.clock = clk * 1000000;
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;
532 host->ops->set_ios(host, &host->ios);
536 sdio_release_host(func);
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);
542 static int esp_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
545 struct esp_pub *epub;
546 struct esp_sdio_ctrl *sctrl;
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,
552 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT){
553 sctrl = kzalloc(sizeof(struct esp_sdio_ctrl), GFP_KERNEL);
559 /* temp buffer reserved for un-dma-able request */
560 sctrl->dma_buffer = kzalloc(ESP_DMA_IBUFSZ, GFP_KERNEL);
562 if (sctrl->dma_buffer == NULL) {
567 sctrl->slc_blk_sz = SIF_SLC_BLOCK_SIZE;
569 epub = esp_pub_alloc_mac80211(&func->dev);
572 esp_dbg(ESP_DBG_ERROR, "no mem for epub \n");
576 epub->sif = (void *)sctrl;
580 if (sif_get_ate_config() == 0) {
581 err = ext_gpio_init(epub);
583 esp_dbg(ESP_DBG_ERROR, "ext_irq_work_init failed %d\n", err);
593 SET_IEEE80211_DEV(epub->hw, &func->dev);
594 epub->dev = &func->dev;
597 epub->sdio_state = sif_sdio_state;
600 sdio_set_drvdata(func, sctrl);
605 /* give us some time to enable, in ms */
606 func->enable_timeout = 100;
608 err = esdio_power_on(sctrl);
609 esp_dbg(ESP_DBG_TRACE, " %s >> power_on err %d \n", __func__, err);
612 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT)
615 goto _err_second_init;
617 check_target_id(epub);
619 sdio_claim_host(func);
621 err = sdio_set_block_size(func, sctrl->slc_blk_sz);
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)
630 goto _err_second_init;
633 sdio_release_host(func);
640 /* fix clock for dongle */
641 sif_set_clock(func, 23);
644 err = esp_pub_init_all(epub);
647 esp_dbg(ESP_DBG_ERROR, "esp_init_all failed: %d\n", err);
648 if(sif_sdio_state == ESP_SDIO_STATE_FIRST_INIT){
650 goto _err_first_init;
652 if(sif_sdio_state == ESP_SDIO_STATE_SECOND_INIT)
653 goto _err_second_init;
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);
667 esdio_power_off(sctrl);
670 if (sif_get_ate_config() == 0)
674 esp_pub_dealloc_mac80211(epub);
676 kfree(sctrl->dma_buffer);
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);
687 sif_sdio_state = ESP_SDIO_STATE_SECOND_ERROR_EXIT;
688 esp_sdio_remove(func);
692 static void esp_sdio_remove(struct sdio_func *func)
694 struct esp_sdio_ctrl *sctrl = NULL;
696 sctrl = sdio_get_drvdata(func);
699 esp_dbg(ESP_DBG_ERROR, "%s no sctrl\n", __func__);
704 if (sctrl->epub == NULL) {
705 esp_dbg(ESP_DBG_ERROR, "%s epub null\n", __func__);
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__);
716 if (sif_get_ate_config() == 0)
720 //sif_disable_target_interrupt(sctrl->epub);
721 atomic_set(&sctrl->epub->sip->state, SIP_STOP);
722 sif_disable_irq(sctrl->epub);
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 */
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__);
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__);
748 sdio_set_drvdata(func,NULL);
750 esp_dbg(ESP_DBG_TRACE, "eagle sdio remove complete\n");
753 MODULE_DEVICE_TABLE(sdio, esp_sdio_devices);
755 static int esp_sdio_suspend(struct device *dev)
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);
761 struct sdio_func *func = container_of(dev, struct sdio_func, dev);
763 struct esp_sdio_ctrl *sctrl = sdio_get_drvdata(func);
764 struct esp_pub *epub = sctrl->epub;
766 printk("%s", __func__);
768 sip_send_suspend_config(epub, 1);
770 atomic_set(&epub->ps.state, ESP_PM_ON);
772 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
776 sdio_flags = sdio_get_host_pm_caps(func);
778 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
779 printk("%s can't keep power while host is suspended\n", __func__);
782 /* keep power while host suspended */
783 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
785 printk("%s error while trying to keep power\n", __func__);
795 static int esp_sdio_resume(struct device *dev)
797 esp_dbg(ESP_DBG_ERROR, "%s", __func__);
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,
808 static struct pm_ops esp_sdio_pm_ops = {
809 .suspend= esp_sdio_suspend,
810 .resume= esp_sdio_resume,
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, },
822 static int esp_sdio_dummy_probe(struct sdio_func *func, const struct sdio_device_id *id)
824 esp_dbg(ESP_DBG_ERROR, "%s enter\n", __func__);
826 up(&esp_powerup_sem);
831 static void esp_sdio_dummy_remove(struct sdio_func *func)
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,
843 static int esp_sdio_init(void)
845 #define ESP_WAIT_UP_TIME_MS 11000
849 bool powerup = false;
852 esp_dbg(ESP_DBG_TRACE, "%s \n", __func__);
856 esp_dbg(ESP_SHOW, "\n*****%s %s EAGLE DRIVER VER:%llx*****\n\n", __DATE__, __TIME__, ver);
858 edf_ret = esp_debugfs_init();
861 android_request_init_conf();
862 #endif /* defined(ANDROID)*/
868 sema_init(&esp_powerup_sem, 0);
870 sif_platform_target_poweron();
872 sif_platform_rescan_card(1);
874 err = sdio_register_driver(&esp_sdio_dummy_driver);
876 esp_dbg(ESP_DBG_ERROR, "eagle sdio driver registration failed, error code: %d\n", err);
880 if (down_timeout(&esp_powerup_sem,
881 msecs_to_jiffies(ESP_WAIT_UP_TIME_MS)) == 0)
889 esp_dbg(ESP_SHOW, "%s ------ RETRY ------ \n", __func__);
891 sif_record_retry_config();
893 sdio_unregister_driver(&esp_sdio_dummy_driver);
895 sif_platform_rescan_card(0);
897 sif_platform_target_poweroff();
902 esp_dbg(ESP_DBG_ERROR, "eagle sdio can not power up!\n");
908 esp_dbg(ESP_SHOW, "%s power up OK\n", __func__);
910 sdio_unregister_driver(&esp_sdio_dummy_driver);
912 sif_sdio_state = ESP_SDIO_STATE_FIRST_INIT;
913 sema_init(&esp_powerup_sem, 0);
915 sdio_register_driver(&esp_sdio_driver);
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);
922 sif_platform_rescan_card(0);
926 sif_platform_rescan_card(1);
928 sif_sdio_state = ESP_SDIO_STATE_SECOND_INIT;
930 sdio_register_driver(&esp_sdio_driver);
936 esp_register_early_suspend();
942 esp_wakelock_destroy();
947 static void esp_sdio_exit(void)
949 esp_dbg(ESP_DBG_TRACE, "%s \n", __func__);
953 esp_unregister_early_suspend();
955 sdio_unregister_driver(&esp_sdio_driver);
957 sif_platform_rescan_card(0);
960 sif_platform_target_poweroff();
961 #endif /* !FPGA_DEBUG */
963 esp_wakelock_destroy();
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 */