xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/ssv6xxx/hwif/sdio/sdio.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
3  * Copyright (c) 2015 iComm Corporation
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/irq.h>
18 #include <linux/module.h>
19 #include <linux/vmalloc.h>
20 #include <linux/platform_device.h>
21 #include <linux/mmc/sdio.h>
22 #include <linux/mmc/sdio_func.h>
23 #include <linux/mmc/sdio_ids.h>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/host.h>
26 #include "sdio_def.h"
27 #include <linux/pm_runtime.h>
28 #include <linux/version.h>
29 #include <linux/firmware.h>
30 #include <linux/reboot.h>
31 #ifdef CONFIG_FW_ALIGNMENT_CHECK
32 #include <linux/skbuff.h>
33 #endif
34 #define SDIO_USE_SLOW_CLOCK
35 #define LOW_SPEED_SDIO_CLOCK (25000000)
36 #define HIGH_SPEED_SDIO_CLOCK (50000000)
37 static struct ssv6xxx_platform_data wlan_data;
38 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
39 #include <linux/printk.h>
40 #else
41 #include <linux/kernel.h>
42 #endif
43 #include <ssv6200.h>
44 #define MAX_RX_FRAME_SIZE 0x900
45 #define SSV_VENDOR_ID 0x3030
46 #define SSV_CABRIO_DEVID 0x3030
47 #define ENABLE_FW_SELF_CHECK 1
48 #define FW_BLOCK_SIZE 0x8000
49 #define CHECKSUM_BLOCK_SIZE 1024
50 #define FW_CHECKSUM_INIT (0x12345678)
51 #define FW_STATUS_REG ADR_TX_SEG
52 #define FW_STATUS_MASK (0x00FF0000)
53 #ifdef CONFIG_PM
54 static int ssv6xxx_sdio_trigger_pmu(struct device *dev);
55 static void ssv6xxx_sdio_reset(struct device *child);
56 #else
ssv6xxx_sdio_reset(struct device * child)57 static void ssv6xxx_sdio_reset(struct device *child) { ; }
58 #endif
59 static void ssv6xxx_high_sdio_clk(struct sdio_func *func);
60 static void ssv6xxx_low_sdio_clk(struct sdio_func *func);
61 extern void *ssv6xxx_ifdebug_info[];
62 extern int ssv_devicetype;
63 extern void ssv6xxx_deinit_prepare(void);
64 static int ssv6xxx_sdio_status = 0;
65 u32 sdio_sr_bhvr = SUSPEND_RESUME_0;
66 EXPORT_SYMBOL(sdio_sr_bhvr);
67 
68 static DEFINE_MUTEX(reboot_lock);
69 u32 shutdown_flags = SSV_SYS_REBOOT;
70 
71 struct ssv6xxx_sdio_glue
72 {
73     struct device *dev;
74     struct platform_device *core;
75 #ifdef CONFIG_FW_ALIGNMENT_CHECK
76     struct sk_buff *dmaSkb;
77 #endif
78 #ifdef CONFIG_PM
79     struct sk_buff *cmd_skb;
80 #endif
81     unsigned int dataIOPort;
82     unsigned int regIOPort;
83     irq_handler_t irq_handler;
84     void *irq_dev;
85     bool dev_ready;
86 };
87 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
88 static const struct sdio_device_id ssv6xxx_sdio_devices[] __devinitconst =
89 #else
90 static const struct sdio_device_id ssv6xxx_sdio_devices[] =
91 #endif
92 {
93     { SDIO_DEVICE(SSV_VENDOR_ID, SSV_CABRIO_DEVID) },
94     {}
95 };
96 MODULE_DEVICE_TABLE(sdio, ssv6xxx_sdio_devices);
ssv6xxx_is_ready(struct device * child)97 static bool ssv6xxx_is_ready (struct device *child)
98 {
99     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
100     if ( (wlan_data.is_enabled == false)
101         || (glue == NULL)
102         || (glue->dev_ready == false))
103         return false;
104     return glue->dev_ready;
105 }
ssv6xxx_sdio_cmd52_read(struct device * child,u32 addr,u32 * value)106 static int ssv6xxx_sdio_cmd52_read(struct device *child, u32 addr,
107         u32 *value)
108 {
109     int ret = -1;
110     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
111     struct sdio_func *func;
112     if ( (wlan_data.is_enabled == false)
113         || (glue == NULL)
114         || (glue->dev_ready == false))
115   return ret;
116     if ( glue != NULL )
117     {
118         func = dev_to_sdio_func(glue->dev);
119         sdio_claim_host(func);
120         *value = sdio_readb(func, addr, &ret);
121         sdio_release_host(func);
122     }
123     return ret;
124 }
ssv6xxx_sdio_cmd52_write(struct device * child,u32 addr,u32 value)125 static int ssv6xxx_sdio_cmd52_write(struct device *child, u32 addr,
126         u32 value)
127 {
128     int ret = -1;
129     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
130     struct sdio_func *func;
131     if ( (wlan_data.is_enabled == false)
132         || (glue == NULL)
133         || (glue->dev_ready == false))
134   return ret;
135     if ( glue != NULL )
136     {
137         func = dev_to_sdio_func(glue->dev);
138         sdio_claim_host(func);
139         sdio_writeb(func, value, addr, &ret);
140         sdio_release_host(func);
141     }
142     return ret;
143 }
ssv6xxx_sdio_read_reg(struct device * child,u32 addr,u32 * buf)144 static int __must_check ssv6xxx_sdio_read_reg(struct device *child, u32 addr,
145         u32 *buf)
146 {
147     int ret = (-1);
148     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
149     struct sdio_func *func ;
150     u8 data[4];
151     if ( (wlan_data.is_enabled == false)
152         || (glue == NULL)
153         || (glue->dev_ready == false))
154   return ret;
155     if ( glue != NULL )
156     {
157         func = dev_to_sdio_func(glue->dev);
158         sdio_claim_host(func);
159         data[0] = (addr >> ( 0 )) &0xff;
160         data[1] = (addr >> ( 8 )) &0xff;
161         data[2] = (addr >> ( 16 )) &0xff;
162         data[3] = (addr >> ( 24 )) &0xff;
163         ret = sdio_memcpy_toio(func, glue->regIOPort, data, 4);
164         if (WARN_ON(ret))
165         {
166             dev_err(child->parent, "sdio read reg write address failed (%d)\n", ret);
167             goto io_err;
168         }
169         ret = sdio_memcpy_fromio(func, data, glue->regIOPort, 4);
170         if (WARN_ON(ret))
171         {
172             dev_err(child->parent, "sdio read reg from I/O failed (%d)\n",ret);
173          goto io_err;
174       }
175         if(ret == 0)
176         {
177             *buf = (data[0]&0xff);
178             *buf = *buf | ((data[1]&0xff)<<( 8 ));
179             *buf = *buf | ((data[2]&0xff)<<( 16 ));
180             *buf = *buf | ((data[3]&0xff)<<( 24 ));
181         }
182         else
183             *buf = 0xffffffff;
184 io_err:
185         sdio_release_host(func);
186     }
187     else
188     {
189         dev_err(child->parent, "sdio read reg glue == NULL!!!\n");
190     }
191     return ret;
192 }
193 #ifdef ENABLE_WAKE_IO_ISR_WHEN_HCI_ENQUEUE
ssv6xxx_sdio_trigger_tx_rx(struct device * child)194 static int ssv6xxx_sdio_trigger_tx_rx (struct device *child)
195 {
196     int ret = (-1);
197     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
198     struct sdio_func *func;
199     struct mmc_host *host;
200     if (glue == NULL)
201         return ret;
202     func = dev_to_sdio_func(glue->dev);
203     host = func->card->host;
204     mmc_signal_sdio_irq(host);
205     return 0;
206 }
207 #endif
ssv6xxx_sdio_write_reg(struct device * child,u32 addr,u32 buf)208 static int __must_check ssv6xxx_sdio_write_reg(struct device *child, u32 addr,
209         u32 buf)
210 {
211     int ret = (-1);
212     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
213     struct sdio_func *func;
214     u8 data[8];
215     if ( (wlan_data.is_enabled == false)
216         || (glue == NULL)
217         || (glue->dev_ready == false))
218   return ret;
219     if ( glue != NULL )
220     {
221         func = dev_to_sdio_func(glue->dev);
222         dev_dbg(child->parent, "sdio write reg addr 0x%x, 0x%x\n",addr, buf);
223         sdio_claim_host(func);
224         data[0] = (addr >> ( 0 )) &0xff;
225         data[1] = (addr >> ( 8 )) &0xff;
226         data[2] = (addr >> ( 16 )) &0xff;
227         data[3] = (addr >> ( 24 )) &0xff;
228         data[4] = (buf >> ( 0 )) &0xff;
229         data[5] = (buf >> ( 8 )) &0xff;
230         data[6] = (buf >> ( 16 )) &0xff;
231         data[7] = (buf >> ( 24 )) &0xff;
232         ret = sdio_memcpy_toio(func, glue->regIOPort, data, 8);
233         sdio_release_host(func);
234 #ifdef __x86_64
235         udelay(50);
236 #endif
237     }
238     else
239     {
240         dev_err(child->parent, "sdio write reg glue == NULL!!!\n");
241     }
242     return ret;
243 }
ssv6xxx_sdio_write_sram(struct device * child,u32 addr,u8 * data,u32 size)244 static int ssv6xxx_sdio_write_sram(struct device *child, u32 addr, u8 *data, u32 size)
245 {
246     int ret = -1;
247     struct ssv6xxx_sdio_glue *glue;
248     struct sdio_func *func=NULL;
249     glue = dev_get_drvdata(child->parent);
250     if ( (wlan_data.is_enabled == false)
251         || (glue == NULL)
252         || (glue->dev_ready == false))
253         return ret;
254     func = dev_to_sdio_func(glue->dev);
255     sdio_claim_host(func);
256     do {
257         if (ssv6xxx_sdio_write_reg(child,0xc0000860,addr)) ;
258         sdio_writeb(func, 0x2, REG_Fn1_STATUS, &ret);
259         if (unlikely(ret)) break;
260         ret = sdio_memcpy_toio(func, glue->dataIOPort, data, size);
261         if (unlikely(ret)) return ret;
262         sdio_writeb(func, 0, REG_Fn1_STATUS, &ret);
263         if (unlikely(ret)) return ret;
264     } while (0);
265     sdio_release_host(func);
266     return ret;
267 }
ssv6xxx_open_firmware(char * user_mainfw)268 void * ssv6xxx_open_firmware(char *user_mainfw)
269 {
270     struct file *fp;
271     fp = filp_open(user_mainfw, O_RDONLY, 0);
272     if (IS_ERR(fp))
273     {
274         printk("ssv6xxx_open_firmware failed!!\n");
275         fp = NULL;
276     }
277     return fp;
278 }
ssv6xxx_read_fw_block(char * buf,int len,void * image)279 int ssv6xxx_read_fw_block(char *buf, int len, void *image)
280 {
281  struct file *fp = (struct file *)image;
282  int rdlen;
283  if (!image)
284   return 0;
285  rdlen = kernel_read(fp, fp->f_pos, buf, len);
286  if (rdlen > 0)
287   fp->f_pos += rdlen;
288  return rdlen;
289 }
ssv6xxx_close_firmware(void * image)290 void ssv6xxx_close_firmware(void *image)
291 {
292  if (image)
293   filp_close((struct file *)image, NULL);
294 }
ssv6xxx_sdio_load_firmware_openfile(struct device * child,u8 * firmware_name)295 static int ssv6xxx_sdio_load_firmware_openfile(struct device *child, u8 *firmware_name)
296 {
297     int ret = 0;
298     struct ssv6xxx_sdio_glue *glue;
299     u8 *fw_buffer = NULL;
300     u32 sram_addr = 0x00000000;
301     u32 block_count = 0;
302     u32 res_size=0,len=0,tolen=0;
303     void *fw_fp=NULL;
304 #ifdef ENABLE_FW_SELF_CHECK
305     u32 checksum = FW_CHECKSUM_INIT;
306     u32 fw_checksum,fw_clkcnt;
307     u32 retry_count = 3;
308     u32 *fw_data32;
309 #else
310     int writesize=0;
311     u32 retry_count = 1;
312 #endif
313     u32 word_count,i;
314     u32 j,jblk;
315     #ifndef SDIO_USE_SLOW_CLOCK
316     struct sdio_func *func=NULL;
317     struct mmc_card *card = NULL;
318     struct mmc_host *host = NULL;
319     #endif
320     glue = dev_get_drvdata(child->parent);
321     if ( (wlan_data.is_enabled != false)
322         || (glue != NULL)
323         || (glue->dev_ready != false))
324     {
325         #ifndef SDIO_USE_SLOW_CLOCK
326         func = dev_to_sdio_func(glue->dev);
327         card = func->card;
328         host = card->host;
329         #endif
330         fw_fp = ssv6xxx_open_firmware(firmware_name);
331         if (!fw_fp) {
332             printk("failed to find firmware (%s)\n", firmware_name);
333             ret = -1;
334             goto out;
335         }
336         fw_buffer = (u8 *)kzalloc(FW_BLOCK_SIZE, GFP_KERNEL);
337         if (fw_buffer == NULL) {
338             printk("Failed to allocate buffer for firmware.\n");
339             goto out;
340         }
341         do {
342             u32 clk_en;
343             if(1){
344             if(ssv6xxx_sdio_write_reg(child, ADR_BRG_SW_RST, 0x0));
345             if(ssv6xxx_sdio_write_reg(child, ADR_BOOT, 0x01));
346             if(ssv6xxx_sdio_read_reg(child, ADR_PLATFORM_CLOCK_ENABLE, &clk_en));
347             if(ssv6xxx_sdio_write_reg(child, ADR_PLATFORM_CLOCK_ENABLE, clk_en | (1 << 2)));
348             }
349             printk("Writing firmware to SSV6XXX...\n");
350             memset(fw_buffer, 0xA5, FW_BLOCK_SIZE);
351             while ((len = ssv6xxx_read_fw_block((char*)fw_buffer, FW_BLOCK_SIZE, fw_fp))) {
352                 tolen += len;
353                 if(len < FW_BLOCK_SIZE){
354                     res_size = len;
355                     break;
356                 }
357                 if(0)
358                 {
359                     jblk = len / 128;
360                     for(j=0;j<jblk;j++)
361                     {
362                         ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)(fw_buffer+j*128), 128);
363                         if (ret){
364                             printk("ssv6xxx_sdio_write_sram failed!!\n");
365                             break;
366                         }
367                         sram_addr += 128;
368                     }
369                 }else{
370                     ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)fw_buffer, FW_BLOCK_SIZE);
371                     if (ret)
372                         break;
373                     sram_addr += FW_BLOCK_SIZE;
374                 }
375                 word_count = (len / sizeof(u32));
376                 fw_data32 = (u32 *)fw_buffer;
377                 for (i = 0; i < word_count; i++){
378                     checksum += fw_data32[i];
379                 }
380                 memset(fw_buffer, 0xA5, FW_BLOCK_SIZE);
381             }
382             if(res_size)
383             {
384                 u32 cks_blk_cnt,cks_blk_res;
385                 cks_blk_cnt = res_size / CHECKSUM_BLOCK_SIZE;
386                 cks_blk_res = res_size % CHECKSUM_BLOCK_SIZE;
387                 if(0)
388                 {
389                     jblk = ((cks_blk_cnt+1)*CHECKSUM_BLOCK_SIZE)/128;
390                     for(j=0;j<jblk;j++){
391                         ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)(fw_buffer+j*128), 128);
392                         sram_addr += 128;
393                     }
394                 }else{
395                     ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)fw_buffer, (cks_blk_cnt+1)*CHECKSUM_BLOCK_SIZE);
396                 }
397                 word_count = (cks_blk_cnt * CHECKSUM_BLOCK_SIZE / sizeof(u32));
398                 fw_data32 = (u32 *)fw_buffer;
399                 for (i = 0; i < word_count; i++)
400                     checksum += *fw_data32++;
401                 if(cks_blk_res)
402                 {
403                     word_count = (CHECKSUM_BLOCK_SIZE / sizeof(u32));
404                     for (i = 0; i < word_count; i++) {
405                         checksum += *fw_data32++;
406                     }
407                 }
408             }
409             checksum = ((checksum >> 24) + (checksum >> 16) + (checksum >> 8) + checksum) & 0x0FF;
410             checksum <<= 16;
411             if (ret == 0) {
412                 block_count = tolen / CHECKSUM_BLOCK_SIZE;
413                 res_size = tolen % CHECKSUM_BLOCK_SIZE;
414                 if(res_size)
415                     block_count++;
416                 if(ssv6xxx_sdio_write_reg(child, FW_STATUS_REG, (block_count << 16)));
417                 if(ssv6xxx_sdio_read_reg(child, FW_STATUS_REG, &fw_clkcnt));
418                 printk("(block_count << 16) = %x,reg =%x\n",(block_count << 16),fw_clkcnt);
419                 if(ssv6xxx_sdio_write_reg(child, ADR_BRG_SW_RST, 0x1));
420                 printk("Firmware \"%s\" loaded\n", firmware_name);
421                 msleep(50);
422                 if(ssv6xxx_sdio_read_reg(child, FW_STATUS_REG, &fw_checksum));
423                 fw_checksum = fw_checksum & FW_STATUS_MASK;
424                 if (fw_checksum == checksum) {
425                     if(ssv6xxx_sdio_write_reg(child, FW_STATUS_REG, (~checksum & FW_STATUS_MASK)));
426                     ret = 0;
427                     printk("Firmware check OK.%04x = %04x\n", fw_checksum, checksum);
428                     break;
429                 } else {
430                     printk("FW checksum error: %04x != %04x\n", fw_checksum, checksum);
431                     ret = -1;
432                 }
433             } else {
434                 printk("Firmware \"%s\" download failed. (%d)\n", firmware_name, ret);
435                 ret = -1;
436             }
437         } while (--retry_count);
438         if (ret)
439             goto out;
440         ret = 0;
441     }
442 out:
443     if(fw_fp)
444         ssv6xxx_close_firmware(fw_fp);
445     if (fw_buffer != NULL)
446         kfree(fw_buffer);
447     msleep(50);
448     return ret;
449 }
ssv6xxx_get_firmware(struct device * dev,char * user_mainfw,const struct firmware ** mainfw)450 int ssv6xxx_get_firmware(struct device *dev,
451             char *user_mainfw,
452             const struct firmware **mainfw)
453 {
454     int ret;
455     BUG_ON(mainfw == NULL);
456     if (*user_mainfw) {
457         ret = request_firmware(mainfw, user_mainfw, dev);
458         if (ret) {
459             dev_err(dev, "couldn't find main firmware %s\n",user_mainfw);
460             goto fail;
461         }
462         if (*mainfw)
463             return 0;
464     }
465 fail:
466     if (*mainfw) {
467         release_firmware(*mainfw);
468         *mainfw = NULL;
469     }
470     return -ENOENT;
471 }
ssv6xxx_sdio_load_firmware_request(struct device * child,u8 * firmware_name)472 static int ssv6xxx_sdio_load_firmware_request(struct device *child ,u8 *firmware_name)
473 {
474     int ret = 0;
475     const struct firmware *ssv6xxx_fw = NULL;
476     struct ssv6xxx_sdio_glue *glue;
477     u8 *fw_buffer = NULL;
478     u32 sram_addr = 0x00000000;
479     u32 block_count = 0;
480     u32 block_idx = 0;
481     u32 res_size;
482     u8 *fw_data;
483 #ifdef ENABLE_FW_SELF_CHECK
484     u32 checksum = FW_CHECKSUM_INIT;
485     u32 fw_checksum;
486     u32 retry_count = 3;
487     u32 *fw_data32;
488 #else
489     int writesize=0;
490     u32 retry_count = 1;
491 #endif
492     #ifndef SDIO_USE_SLOW_CLOCK
493     struct sdio_func *func=NULL;
494     struct mmc_card *card = NULL;
495     struct mmc_host *host = NULL;
496     #endif
497     glue = dev_get_drvdata(child->parent);
498     if ( (wlan_data.is_enabled != false)
499         || (glue != NULL)
500         || (glue->dev_ready != false))
501     {
502         #ifndef SDIO_USE_SLOW_CLOCK
503         func = dev_to_sdio_func(glue->dev);
504         card = func->card;
505         host = card->host;
506         #endif
507         ret = ssv6xxx_get_firmware(glue->dev, firmware_name, &ssv6xxx_fw);
508         if (ret) {
509             pr_err("failed to find firmware (%d)\n", ret);
510             goto out;
511         }
512         fw_buffer = (u8 *)kzalloc(FW_BLOCK_SIZE, GFP_KERNEL);
513         if (fw_buffer == NULL) {
514             pr_err("Failed to allocate buffer for firmware.\n");
515             goto out;
516         }
517 #ifdef ENABLE_FW_SELF_CHECK
518         block_count = ssv6xxx_fw->size / CHECKSUM_BLOCK_SIZE;
519         res_size = ssv6xxx_fw->size % CHECKSUM_BLOCK_SIZE;
520         {
521             int word_count = (int)(block_count * CHECKSUM_BLOCK_SIZE / sizeof(u32));
522             int i;
523             fw_data32 = (u32 *)ssv6xxx_fw->data;
524             for (i = 0; i < word_count; i++)
525                 checksum += fw_data32[i];
526             if(res_size)
527             {
528                 memset(fw_buffer, 0xA5, CHECKSUM_BLOCK_SIZE);
529                 memcpy(fw_buffer, &ssv6xxx_fw->data[block_count * CHECKSUM_BLOCK_SIZE], res_size);
530                 word_count = (int)(CHECKSUM_BLOCK_SIZE / sizeof(u32));
531                 fw_data32 = (u32 *)fw_buffer;
532                 for (i = 0; i < word_count; i++) {
533                     checksum += fw_data32[i];
534                 }
535             }
536         }
537         checksum = ((checksum >> 24) + (checksum >> 16) + (checksum >> 8) + checksum) & 0x0FF;
538         checksum <<= 16;
539 #endif
540         do {
541             u32 clk_en;
542             if(ssv6xxx_sdio_write_reg(child, ADR_BRG_SW_RST, 0x0));
543             if(ssv6xxx_sdio_write_reg(child, ADR_BOOT, 0x01));
544             if(ssv6xxx_sdio_read_reg(child, ADR_PLATFORM_CLOCK_ENABLE, &clk_en));
545             if(ssv6xxx_sdio_write_reg(child, ADR_PLATFORM_CLOCK_ENABLE, clk_en | (1 << 2)));
546 #ifdef ENABLE_FW_SELF_CHECK
547             block_count = ssv6xxx_fw->size / FW_BLOCK_SIZE;
548             res_size = ssv6xxx_fw->size % FW_BLOCK_SIZE;
549             printk("Writing %d blocks to SSV6XXX...", block_count);
550             for (block_idx = 0, fw_data = (u8 *)ssv6xxx_fw->data, sram_addr = 0;block_idx < block_count;
551                 block_idx++, fw_data += FW_BLOCK_SIZE, sram_addr += FW_BLOCK_SIZE) {
552                 memcpy(fw_buffer, fw_data, FW_BLOCK_SIZE);
553                 ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)fw_buffer, FW_BLOCK_SIZE);
554                 if (ret)
555                     break;
556             }
557             if(res_size)
558             {
559                 memset(fw_buffer, 0xA5, FW_BLOCK_SIZE);
560                 memcpy(fw_buffer, &ssv6xxx_fw->data[block_count * FW_BLOCK_SIZE], res_size);
561                 ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)fw_buffer, ((res_size/CHECKSUM_BLOCK_SIZE)+1)*CHECKSUM_BLOCK_SIZE);
562             }
563 #else
564             block_count = ssv6xxx_fw->size / FW_BLOCK_SIZE;
565             res_size = ssv6xxx_fw->size % FW_BLOCK_SIZE;
566             writesize = sdio_align_size(func,res_size);
567             printk("Writing %d blocks to SSV6XXX...", block_count);
568             for (block_idx = 0, fw_data = (u8 *)ssv6xxx_fw->data, sram_addr = 0;block_idx < block_count;
569                 block_idx++, fw_data += FW_BLOCK_SIZE, sram_addr += FW_BLOCK_SIZE) {
570                 memcpy(fw_buffer, fw_data, FW_BLOCK_SIZE);
571                 ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)fw_buffer, FW_BLOCK_SIZE);
572                 if (ret)
573                     break;
574             }
575             if(res_size)
576             {
577                 memcpy(fw_buffer, &ssv6xxx_fw->data[block_count * FW_BLOCK_SIZE], res_size);
578                 ret = ssv6xxx_sdio_write_sram(child, sram_addr, (u8 *)fw_buffer, writesize);
579             }
580 #endif
581             if (ret == 0) {
582 #ifdef ENABLE_FW_SELF_CHECK
583                 block_count = ssv6xxx_fw->size / CHECKSUM_BLOCK_SIZE;
584                 res_size = ssv6xxx_fw->size % CHECKSUM_BLOCK_SIZE;
585                 if(res_size)
586                     block_count++;
587                 if(ssv6xxx_sdio_write_reg(child, FW_STATUS_REG, (block_count << 16)));
588 #endif
589                 if(ssv6xxx_sdio_write_reg(child, ADR_BRG_SW_RST, 0x1));
590                 printk("Firmware \"%s\" loaded\n", firmware_name);
591 #ifdef ENABLE_FW_SELF_CHECK
592                 msleep(50);
593                 if(ssv6xxx_sdio_read_reg(child, FW_STATUS_REG, &fw_checksum));
594                 fw_checksum = fw_checksum & FW_STATUS_MASK;
595                 if (fw_checksum == checksum) {
596                     if(ssv6xxx_sdio_write_reg(child, FW_STATUS_REG, (~checksum & FW_STATUS_MASK)));
597                     ret = 0;
598                     printk("Firmware check OK.\n");
599                     break;
600                 } else {
601                     printk("FW checksum error: %04x != %04x\n", fw_checksum, checksum);
602                     ret = -1;
603                 }
604 #endif
605             } else {
606                 printk("Firmware \"%s\" download failed. (%d)\n", firmware_name, ret);
607                 ret = -1;
608             }
609         } while (--retry_count);
610         if (ret)
611             goto out;
612         ret = 0;
613     }
614 out:
615     if (ssv6xxx_fw)
616         release_firmware(ssv6xxx_fw);
617     if (fw_buffer != NULL)
618         kfree(fw_buffer);
619     msleep(50);
620     return ret;
621 }
ssv6xxx_sdio_load_firmware(struct device * child,u8 * firmware_name,u8 openfile)622 static int ssv6xxx_sdio_load_firmware(struct device *child ,u8 *firmware_name, u8 openfile)
623 {
624  int ret = -1;
625  struct ssv6xxx_sdio_glue *glue;
626  struct sdio_func *func;
627  glue = dev_get_drvdata(child->parent);
628     if(openfile)
629         ret = ssv6xxx_sdio_load_firmware_openfile(child,firmware_name);
630     else
631         ret = ssv6xxx_sdio_load_firmware_request(child,firmware_name);
632  if(glue != NULL)
633  {
634   func = dev_to_sdio_func(glue->dev);
635   ssv6xxx_high_sdio_clk(func);
636  }
637  return ret;
638 }
ssv6xxx_sdio_irq_getstatus(struct device * child,int * status)639 static int ssv6xxx_sdio_irq_getstatus(struct device *child,int *status)
640 {
641     int ret = (-1);
642     struct ssv6xxx_sdio_glue *glue;
643     struct sdio_func *func;
644     glue = dev_get_drvdata(child->parent);
645     if ( (wlan_data.is_enabled == false)
646         || (glue == NULL)
647         || (glue->dev_ready == false))
648   return ret;
649     if ( glue != NULL )
650     {
651         func = dev_to_sdio_func(glue->dev);
652         sdio_claim_host(func);
653         *status = sdio_readb(func, REG_INT_STATUS, &ret);
654         sdio_release_host(func);
655     }
656     return ret;
657 }
658 #if 0
659 static void _sdio_hexdump(const u8 *buf,
660                              size_t len)
661 {
662     size_t i;
663     printk("\n-----------------------------\n");
664     printk("hexdump(len=%lu):\n", (unsigned long) len);
665     {
666         for (i = 0; i < len; i++){
667             printk(" %02x", buf[i]);
668             if((i+1)%40 ==0)
669                 printk("\n");
670         }
671     }
672     printk("\n-----------------------------\n");
673 }
674 #endif
ssv6xxx_sdio_read(struct device * child,void * buf,size_t * size)675 static int __must_check ssv6xxx_sdio_read(struct device *child,
676         void *buf, size_t *size)
677 {
678     int ret = (-1), readsize = 0;
679     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
680     struct sdio_func *func ;
681     if ( (wlan_data.is_enabled == false)
682         || (glue == NULL)
683         || (glue->dev_ready == false))
684   return ret;
685     if ( glue != NULL )
686     {
687         func = dev_to_sdio_func(glue->dev);
688         sdio_claim_host(func);
689         *size = (uint)sdio_readb(func, REG_CARD_PKT_LEN_0, &ret);
690         if (ret)
691             dev_err(child->parent, "sdio read hight len failed ret[%d]\n",ret);
692         if (ret == 0)
693         {
694             *size = *size | ((uint)sdio_readb(func, REG_CARD_PKT_LEN_1, &ret)<<0x8);
695             if (ret)
696                 dev_err(child->parent, "sdio read low len failed ret[%d]\n",ret);
697         }
698         if (ret == 0)
699         {
700             readsize = sdio_align_size(func,*size);
701             ret = sdio_memcpy_fromio(func, buf, glue->dataIOPort, readsize);
702             if (ret)
703                 dev_err(child->parent, "sdio read failed size ret[%d]\n",ret);
704         }
705         sdio_release_host(func);
706     }
707 #if 0
708     if(*size > 1500)
709         _sdio_hexdump(buf,*size);
710 #endif
711     return ret;
712 }
ssv6xxx_sdio_write(struct device * child,void * buf,size_t len,u8 queue_num)713 static int __must_check ssv6xxx_sdio_write(struct device *child,
714         void *buf, size_t len,u8 queue_num)
715 {
716     int ret = (-1);
717     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
718     struct sdio_func *func;
719     int writesize;
720     void *tempPointer;
721     if ( (wlan_data.is_enabled == false)
722         || (glue == NULL)
723         || (glue->dev_ready == false))
724   return ret;
725     if ( glue != NULL )
726     {
727 #ifdef CONFIG_FW_ALIGNMENT_CHECK
728 #ifdef CONFIG_ARM64
729         if (((u64)buf) & 3) {
730 #else
731         if (((u32)buf) & 3) {
732 #endif
733             memcpy(glue->dmaSkb->data,buf,len);
734             tempPointer = glue->dmaSkb->data;
735         }
736         else
737 #endif
738             tempPointer = buf;
739 #if 0
740         if(len > 1500)
741             _sdio_hexdump(buf,len);
742 #endif
743         func = dev_to_sdio_func(glue->dev);
744         sdio_claim_host(func);
745         writesize = sdio_align_size(func,len);
746         do
747         {
748             ret = sdio_memcpy_toio(func, glue->dataIOPort, tempPointer, writesize);
749             if ( ret == -EILSEQ || ret == -ETIMEDOUT )
750             {
751                 ret = -1;
752                 break;
753             }
754             else
755             {
756                 if(ret)
757                     dev_err(glue->dev,"Unexpected return value ret=[%d]\n",ret);
758             }
759         }
760         while( ret == -EILSEQ || ret == -ETIMEDOUT);
761         sdio_release_host(func);
762         if (ret)
763             dev_err(glue->dev, "sdio write failed (%d)\n", ret);
764     }
765     return ret;
766 }
767 static void ssv6xxx_sdio_irq_handler(struct sdio_func *func)
768 {
769     int status;
770     struct ssv6xxx_sdio_glue *glue = sdio_get_drvdata(func);
771     struct ssv6xxx_platform_data *pwlan_data = &wlan_data;
772     if ( (wlan_data.is_enabled == false)
773         || (glue == NULL)
774         || (glue->dev_ready == false))
775         return;
776     if ( glue != NULL && glue->irq_handler != NULL )
777     {
778         atomic_set(&pwlan_data->irq_handling, 1);
779         sdio_release_host(func);
780         if ( glue->irq_handler != NULL )
781             status = glue->irq_handler(0,glue->irq_dev);
782         sdio_claim_host(func);
783         atomic_set(&pwlan_data->irq_handling, 0);
784     }
785 }
786 static void ssv6xxx_sdio_irq_setmask(struct device *child,int mask)
787 {
788     int err_ret;
789     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
790     struct sdio_func *func;
791     if ( (wlan_data.is_enabled == false)
792         || (glue == NULL)
793         || (glue->dev_ready == false))
794   return;
795     if ( glue != NULL )
796     {
797         func = dev_to_sdio_func(glue->dev);
798         sdio_claim_host(func);
799         sdio_writeb(func,mask, REG_INT_MASK, &err_ret);
800         sdio_release_host(func);
801     }
802 }
803 static void ssv6xxx_sdio_irq_trigger(struct device *child)
804 {
805     int err_ret;
806     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
807     struct sdio_func *func;
808     if ( (wlan_data.is_enabled == false)
809         || (glue == NULL)
810         || (glue->dev_ready == false))
811   return;
812     if ( glue != NULL )
813     {
814         func = dev_to_sdio_func(glue->dev);
815         sdio_claim_host(func);
816         sdio_writeb(func,0x2, REG_INT_TRIGGER, &err_ret);
817         sdio_release_host(func);
818     }
819 }
820 static int ssv6xxx_sdio_irq_getmask(struct device *child, u32 *mask)
821 {
822     u8 imask = 0;
823     int ret = (-1);
824     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
825     struct sdio_func *func;
826     if ( (wlan_data.is_enabled == false)
827         || (glue == NULL)
828         || (glue->dev_ready == false))
829   return ret;
830     if ( glue != NULL )
831     {
832         func = dev_to_sdio_func(glue->dev);
833         sdio_claim_host(func);
834         imask = sdio_readb(func,REG_INT_MASK, &ret);
835         *mask = imask;
836         sdio_release_host(func);
837     }
838     return ret;
839 }
840 static void ssv6xxx_sdio_irq_enable(struct device *child)
841 {
842     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
843     struct sdio_func *func;
844     int ret;
845     struct ssv6xxx_platform_data *pwlan_data = &wlan_data;
846     if ( (pwlan_data->is_enabled == false)
847         || (glue == NULL)
848         || (glue->dev_ready == false))
849   return;
850     if ( glue != NULL )
851     {
852         func = dev_to_sdio_func(glue->dev);
853         sdio_claim_host(func);
854         ret = sdio_claim_irq(func, ssv6xxx_sdio_irq_handler);
855         if (ret)
856             dev_err(child->parent, "Failed to claim sdio irq: %d\n", ret);
857         sdio_release_host(func);
858     }
859 
860     printk("ssv6xxx_sdio_irq_enable\n");
861 }
862 static void ssv6xxx_sdio_irq_disable(struct device *child,bool iswaitirq)
863 {
864     struct ssv6xxx_sdio_glue *glue = NULL;
865     struct sdio_func *func;
866     struct ssv6xxx_platform_data *pwlan_data = &wlan_data;
867     int ret;
868     printk("ssv6xxx_sdio_irq_disable\n");
869     if ( (wlan_data.is_enabled == false)
870         || (child->parent == NULL))
871   return;
872     glue = dev_get_drvdata(child->parent);
873     if ( (glue == NULL)
874         || (glue->dev_ready == false)
875         || (glue->dev == NULL))
876         return;
877     {
878         func = dev_to_sdio_func(glue->dev);
879         if(func == NULL){
880             printk("func == NULL\n");
881             return;
882         }
883         sdio_claim_host(func);
884         while(atomic_read(&pwlan_data->irq_handling)){
885             sdio_release_host(func);
886       schedule_timeout(HZ / 10);
887       sdio_claim_host(func);
888         }
889         ret = sdio_release_irq(func);
890         if (ret)
891             dev_err(child->parent, "Failed to release sdio irq: %d\n", ret);
892         sdio_release_host(func);
893     }
894 }
895 static void ssv6xxx_sdio_irq_request(struct device *child,irq_handler_t irq_handler,void *irq_dev)
896 {
897     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
898     struct sdio_func *func;
899     bool isIrqEn = false;
900     if ( (wlan_data.is_enabled == false)
901         || (glue == NULL)
902         || (glue->dev_ready == false))
903   return;
904     if ( glue != NULL )
905     {
906         func = dev_to_sdio_func(glue->dev);
907         glue->irq_handler = irq_handler;
908         glue->irq_dev = irq_dev;
909         if (isIrqEn )
910         {
911             ssv6xxx_sdio_irq_enable(child);
912         }
913     }
914 }
915 static void ssv6xxx_sdio_read_parameter(struct sdio_func *func,
916         struct ssv6xxx_sdio_glue *glue)
917 {
918     int err_ret;
919     sdio_claim_host(func);
920     glue->dataIOPort = 0;
921     glue->dataIOPort = glue->dataIOPort | (sdio_readb(func, REG_DATA_IO_PORT_0, &err_ret) << ( 8*0 ));
922     glue->dataIOPort = glue->dataIOPort | (sdio_readb(func, REG_DATA_IO_PORT_1, &err_ret) << ( 8*1 ));
923     glue->dataIOPort = glue->dataIOPort | (sdio_readb(func, REG_DATA_IO_PORT_2, &err_ret) << ( 8*2 ));
924     glue->regIOPort = 0;
925     glue->regIOPort = glue->regIOPort | (sdio_readb(func, REG_REG_IO_PORT_0, &err_ret) << ( 8*0 ));
926     glue->regIOPort = glue->regIOPort | (sdio_readb(func, REG_REG_IO_PORT_1, &err_ret) << ( 8*1 ));
927     glue->regIOPort = glue->regIOPort | (sdio_readb(func, REG_REG_IO_PORT_2, &err_ret) << ( 8*2 ));
928     dev_err(&func->dev, "dataIOPort 0x%x regIOPort 0x%x\n",glue->dataIOPort,glue->regIOPort);
929 #ifdef CONFIG_PLATFORM_SDIO_BLOCK_SIZE
930     err_ret = sdio_set_block_size(func,CONFIG_PLATFORM_SDIO_BLOCK_SIZE);
931 #else
932     err_ret = sdio_set_block_size(func,SDIO_DEF_BLOCK_SIZE);
933 #endif
934     if (err_ret != 0) {
935         printk("SDIO setting SDIO_DEF_BLOCK_SIZE fail!!\n");
936     }
937 #ifdef CONFIG_PLATFORM_SDIO_OUTPUT_TIMING
938     sdio_writeb(func, CONFIG_PLATFORM_SDIO_OUTPUT_TIMING,REG_OUTPUT_TIMING_REG, &err_ret);
939 #else
940     sdio_writeb(func, SDIO_DEF_OUTPUT_TIMING,REG_OUTPUT_TIMING_REG, &err_ret);
941 #endif
942     sdio_writeb(func, 0x00,REG_Fn1_STATUS, &err_ret);
943 #if 0
944     sdio_writeb(func,SDIO_TX_ALLOC_SIZE_SHIFT|SDIO_TX_ALLOC_ENABLE,REG_SDIO_TX_ALLOC_SHIFT, &err_ret);
945 #endif
946     sdio_release_host(func);
947 }
948 static void ssv6xxx_do_sdio_wakeup(struct sdio_func *func)
949 {
950  int err_ret;
951  if(func != NULL)
952  {
953   sdio_claim_host(func);
954   sdio_writeb(func, 0x01, REG_PMU_WAKEUP, &err_ret);
955   mdelay(10);
956   sdio_writeb(func, 0x00, REG_PMU_WAKEUP, &err_ret);
957   sdio_release_host(func);
958  }
959 }
960 static void ssv6xxx_sdio_pmu_wakeup(struct device *child)
961 {
962  struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
963  struct sdio_func *func;
964  if (glue != NULL) {
965   func = dev_to_sdio_func(glue->dev);
966   ssv6xxx_do_sdio_wakeup(func);
967  }
968 }
969 static bool ssv6xxx_sdio_support_scatter(struct device *child)
970 {
971     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
972     struct sdio_func *func;
973     bool support = false;
974     do{
975         if(!glue){
976             dev_err(child->parent, "ssv6xxx_sdio_enable_scatter glue == NULL!!!\n");
977             break;
978         }
979         func = dev_to_sdio_func(glue->dev);
980 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,0,0)
981         if (func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
982             dev_err(child->parent, "host controller only supports scatter of :%d entries, driver need: %d\n",
983    func->card->host->max_segs,
984    MAX_SCATTER_ENTRIES_PER_REQ);
985             break;
986      }
987         support = true;
988 #endif
989     }while(0);
990     return support;
991 }
992 static void ssv6xxx_sdio_setup_scat_data(struct sdio_scatter_req *scat_req,
993      struct mmc_data *data)
994 {
995  struct scatterlist *sg;
996  int i;
997  data->blksz = SDIO_DEF_BLOCK_SIZE;
998  data->blocks = scat_req->len / SDIO_DEF_BLOCK_SIZE;
999  printk("scatter: (%s)  (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
1000      (scat_req->req & SDIO_WRITE) ? "WR" : "RD",
1001      data->blksz, data->blocks, scat_req->len,
1002      scat_req->scat_entries);
1003  data->flags = (scat_req->req & SDIO_WRITE) ? MMC_DATA_WRITE :
1004           MMC_DATA_READ;
1005  sg = scat_req->sgentries;
1006  sg_init_table(sg, scat_req->scat_entries);
1007  for (i = 0; i < scat_req->scat_entries; i++, sg++) {
1008   printk("%d: addr:0x%p, len:%d\n",
1009       i, scat_req->scat_list[i].buf,
1010       scat_req->scat_list[i].len);
1011   sg_set_buf(sg, scat_req->scat_list[i].buf,
1012       scat_req->scat_list[i].len);
1013  }
1014  data->sg = scat_req->sgentries;
1015  data->sg_len = scat_req->scat_entries;
1016 }
1017 static inline void ssv6xxx_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
1018           u8 mode, u8 opcode, u32 addr,
1019           u16 blksz)
1020 {
1021  *arg = (((rw & 1) << 31) |
1022   ((func & 0x7) << 28) |
1023   ((mode & 1) << 27) |
1024   ((opcode & 1) << 26) |
1025   ((addr & 0x1FFFF) << 9) |
1026   (blksz & 0x1FF));
1027 }
1028 static int ssv6xxx_sdio_rw_scatter(struct device *child,
1029           struct sdio_scatter_req *scat_req)
1030 {
1031     struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
1032     struct sdio_func *func;
1033  struct mmc_request mmc_req;
1034  struct mmc_command cmd;
1035  struct mmc_data data;
1036  u8 opcode, rw;
1037  int status = 1;
1038     do{
1039         if(!glue){
1040             dev_err(child->parent, "ssv6xxx_sdio_enable_scatter glue == NULL!!!\n");
1041             break;
1042         }
1043         func = dev_to_sdio_func(glue->dev);
1044      memset(&mmc_req, 0, sizeof(struct mmc_request));
1045      memset(&cmd, 0, sizeof(struct mmc_command));
1046      memset(&data, 0, sizeof(struct mmc_data));
1047      ssv6xxx_sdio_setup_scat_data(scat_req, &data);
1048      opcode = 0;
1049      rw = (scat_req->req & SDIO_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
1050      ssv6xxx_sdio_set_cmd53_arg(&cmd.arg, rw, func->num,
1051           CMD53_ARG_BLOCK_BASIS, opcode, glue->dataIOPort,
1052           data.blocks);
1053      cmd.opcode = SD_IO_RW_EXTENDED;
1054      cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
1055      mmc_req.cmd = &cmd;
1056      mmc_req.data = &data;
1057      mmc_set_data_timeout(&data, func->card);
1058      mmc_wait_for_req(func->card->host, &mmc_req);
1059      status = cmd.error ? cmd.error : data.error;
1060         if (cmd.error)
1061       return cmd.error;
1062      if (data.error)
1063       return data.error;
1064     }while(0);
1065  return status;
1066 }
1067 static void ssv6xxx_set_sdio_clk(struct sdio_func *func,u32 sdio_hz)
1068 {
1069  struct mmc_host *host;
1070  host = func->card->host;
1071  if(sdio_hz < host->f_min )
1072   sdio_hz = host->f_min;
1073  else if(sdio_hz > host->f_max)
1074   sdio_hz = host->f_max;
1075  printk("%s:set sdio clk %dHz\n", __FUNCTION__,sdio_hz);
1076     sdio_claim_host(func);
1077     host->ios.clock = sdio_hz;
1078     host->ops->set_ios(host, &host->ios);
1079     mdelay(20);
1080     sdio_release_host(func);
1081 }
1082 static void ssv6xxx_low_sdio_clk(struct sdio_func *func)
1083 {
1084  ssv6xxx_set_sdio_clk(func,LOW_SPEED_SDIO_CLOCK);
1085 }
1086 static void ssv6xxx_high_sdio_clk(struct sdio_func *func)
1087 {
1088 #ifndef SDIO_USE_SLOW_CLOCK
1089  ssv6xxx_set_sdio_clk(func,HIGH_SPEED_SDIO_CLOCK);
1090 #endif
1091 }
1092 static struct ssv6xxx_hwif_ops sdio_ops =
1093 {
1094     .read = ssv6xxx_sdio_read,
1095     .write = ssv6xxx_sdio_write,
1096     .readreg = ssv6xxx_sdio_read_reg,
1097     .writereg = ssv6xxx_sdio_write_reg,
1098 #ifdef ENABLE_WAKE_IO_ISR_WHEN_HCI_ENQUEUE
1099     .trigger_tx_rx = ssv6xxx_sdio_trigger_tx_rx,
1100 #endif
1101     .irq_getmask = ssv6xxx_sdio_irq_getmask,
1102     .irq_setmask = ssv6xxx_sdio_irq_setmask,
1103     .irq_enable = ssv6xxx_sdio_irq_enable,
1104     .irq_disable = ssv6xxx_sdio_irq_disable,
1105     .irq_getstatus = ssv6xxx_sdio_irq_getstatus,
1106     .irq_request = ssv6xxx_sdio_irq_request,
1107     .irq_trigger = ssv6xxx_sdio_irq_trigger,
1108     .pmu_wakeup = ssv6xxx_sdio_pmu_wakeup,
1109     .load_fw = ssv6xxx_sdio_load_firmware,
1110     .cmd52_read = ssv6xxx_sdio_cmd52_read,
1111     .cmd52_write = ssv6xxx_sdio_cmd52_write,
1112     .support_scatter = ssv6xxx_sdio_support_scatter,
1113     .rw_scatter = ssv6xxx_sdio_rw_scatter,
1114     .is_ready = ssv6xxx_is_ready,
1115     .write_sram = ssv6xxx_sdio_write_sram,
1116     .interface_reset = ssv6xxx_sdio_reset,
1117 };
1118 #ifdef CONFIG_PCIEASPM
1119 #include <linux/pci.h>
1120 #include <linux/pci-aspm.h>
1121 static int cabrio_sdio_pm_check(struct sdio_func *func)
1122 {
1123  struct pci_dev *pci_dev = NULL;
1124  struct mmc_card *card = func->card;
1125  struct mmc_host *host = card->host;
1126  if (strcmp(host->parent->bus->name, "pci"))
1127  {
1128   dev_info(&func->dev, "SDIO host is not PCI device, but \"%s\".", host->parent->bus->name);
1129   return 0;
1130  }
1131  for_each_pci_dev(pci_dev) {
1132   if ( ((pci_dev->class >> 8) != PCI_CLASS_SYSTEM_SDHCI)
1133    && ( (pci_dev->driver == NULL)
1134     || (strcmp(pci_dev->driver->name, "sdhci-pci") != 0)))
1135    continue;
1136   if (pci_is_pcie(pci_dev)) {
1137    u8 aspm;
1138    int pos;
1139    pos = pci_pcie_cap(pci_dev);
1140          if (pos) {
1141              struct pci_dev *parent = pci_dev->bus->self;
1142              pci_read_config_byte(pci_dev, pos + PCI_EXP_LNKCTL, &aspm);
1143              aspm &= ~(PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
1144              pci_write_config_byte(pci_dev, pos + PCI_EXP_LNKCTL, aspm);
1145              pos = pci_pcie_cap(parent);
1146              pci_read_config_byte(parent, pos + PCI_EXP_LNKCTL, &aspm);
1147              aspm &= ~(PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
1148              pci_write_config_byte(parent, pos + PCI_EXP_LNKCTL, aspm);
1149              dev_info(&pci_dev->dev, "Clear PCI-E device and its parent link state L0S and L1 and CLKPM.\n");
1150          }
1151   }
1152  }
1153  return 0;
1154 }
1155 #endif
1156 static int ssv6xxx_sdio_power_on(struct ssv6xxx_platform_data * pdata, struct sdio_func *func)
1157 {
1158  int ret = 0;
1159  if (pdata->is_enabled == true)
1160   return 0;
1161     printk("ssv6xxx_sdio_power_on\n");
1162  sdio_claim_host(func);
1163  ret = sdio_enable_func(func);
1164  sdio_release_host(func);
1165  if (ret) {
1166   printk("Unable to enable sdio func: %d)\n", ret);
1167   return ret;
1168  }
1169  msleep(10);
1170  pdata->is_enabled = true;
1171  return ret;
1172 }
1173 static int ssv6xxx_sdio_power_off(struct ssv6xxx_platform_data * pdata, struct sdio_func *func)
1174 {
1175  int ret;
1176  if (pdata->is_enabled == false)
1177   return 0;
1178     printk("ssv6xxx_sdio_power_off\n");
1179  sdio_claim_host(func);
1180  ret = sdio_disable_func(func);
1181  sdio_release_host(func);
1182  if (ret)
1183   return ret;
1184  pdata->is_enabled = false;
1185  return ret;
1186 }
1187 int ssv6xxx_get_dev_status(void)
1188 {
1189  return ssv6xxx_sdio_status;
1190 }
1191 EXPORT_SYMBOL(ssv6xxx_get_dev_status);
1192 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
1193 static int __devinit ssv6xxx_sdio_probe(struct sdio_func *func,
1194         const struct sdio_device_id *id)
1195 #else
1196 static int ssv6xxx_sdio_probe(struct sdio_func *func,
1197         const struct sdio_device_id *id)
1198 #endif
1199 {
1200     struct ssv6xxx_platform_data *pwlan_data = &wlan_data;
1201     struct ssv6xxx_sdio_glue *glue;
1202     int ret = -ENOMEM;
1203     const char *chip_family = "ssv6200";
1204     if (ssv_devicetype != 0) {
1205         printk(KERN_INFO "Not using SSV6200 normal SDIO driver.\n");
1206         return -ENODEV;
1207     }
1208     printk(KERN_INFO "=======================================\n");
1209     printk(KERN_INFO "==           RUN SDIO                ==\n");
1210     printk(KERN_INFO "=======================================\n");
1211     if (func->num != 0x01)
1212         return -ENODEV;
1213     glue = kzalloc(sizeof(*glue), GFP_KERNEL);
1214     if (!glue)
1215     {
1216         dev_err(&func->dev, "can't allocate glue\n");
1217         goto out;
1218     }
1219     ssv6xxx_sdio_status = 1;
1220  ssv6xxx_low_sdio_clk(func);
1221 #ifdef CONFIG_FW_ALIGNMENT_CHECK
1222     glue->dmaSkb=__dev_alloc_skb(SDIO_DMA_BUFFER_LEN , GFP_KERNEL);
1223 #endif
1224 #ifdef CONFIG_PM
1225     glue->cmd_skb=__dev_alloc_skb(SDIO_COMMAND_BUFFER_LEN , GFP_KERNEL);
1226 #endif
1227     memset(pwlan_data, 0, sizeof(struct ssv6xxx_platform_data));
1228     atomic_set(&pwlan_data->irq_handling, 0);
1229     glue->dev = &func->dev;
1230     func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
1231     func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
1232     glue->dev_ready = true;
1233     pwlan_data->vendor = func->vendor;
1234     pwlan_data->device = func->device;
1235     dev_err(glue->dev, "vendor = 0x%x device = 0x%x\n", pwlan_data->vendor,pwlan_data->device);
1236     #ifdef CONFIG_PCIEASPM
1237     cabrio_sdio_pm_check(func);
1238     #endif
1239     pwlan_data->ops = &sdio_ops;
1240     sdio_set_drvdata(func, glue);
1241 #ifdef CONFIG_PM
1242         ssv6xxx_do_sdio_wakeup(func);
1243 #endif
1244     ssv6xxx_sdio_power_on(pwlan_data, func);
1245     ssv6xxx_sdio_read_parameter(func,glue);
1246     glue->core = platform_device_alloc(chip_family, -1);
1247     if (!glue->core)
1248     {
1249         dev_err(glue->dev, "can't allocate platform_device");
1250         ret = -ENOMEM;
1251         goto out_free_glue;
1252     }
1253     glue->core->dev.parent = &func->dev;
1254     ret = platform_device_add_data(glue->core, pwlan_data,
1255                                    sizeof(*pwlan_data));
1256     if (ret)
1257     {
1258         dev_err(glue->dev, "can't add platform data\n");
1259         goto out_dev_put;
1260     }
1261     ret = platform_device_add(glue->core);
1262     if (ret)
1263     {
1264         dev_err(glue->dev, "can't add platform device\n");
1265         goto out_dev_put;
1266     }
1267     ssv6xxx_sdio_irq_setmask(&glue->core->dev,0xff);
1268 #if 0
1269     ssv6xxx_sdio_irq_enable(&glue->core->dev);
1270 #else
1271 #endif
1272 #if 0
1273     glue->dev->platform_data = (void *)pwlan_data;
1274     ret = ssv6xxx_dev_probe(glue->dev);
1275     if (ret)
1276     {
1277         dev_err(glue->dev, "failed to initial ssv6xxx device !!\n");
1278         platform_device_del(glue->core);
1279         goto out_dev_put;
1280     }
1281 #endif
1282     ssv6xxx_ifdebug_info[0] = (void *)&glue->core->dev;
1283     ssv6xxx_ifdebug_info[1] = (void *)glue->core;
1284     ssv6xxx_ifdebug_info[2] = (void *)&sdio_ops;
1285     return 0;
1286 out_dev_put:
1287     platform_device_put(glue->core);
1288 out_free_glue:
1289     kfree(glue);
1290 out:
1291     return ret;
1292 }
1293 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
1294 static void __devexit ssv6xxx_sdio_remove(struct sdio_func *func)
1295 #else
1296 static void ssv6xxx_sdio_remove(struct sdio_func *func)
1297 #endif
1298 {
1299     struct ssv6xxx_sdio_glue *glue = sdio_get_drvdata(func);
1300     struct ssv6xxx_platform_data *pwlan_data = &wlan_data;
1301     printk("ssv6xxx_sdio_remove..........\n");
1302     ssv6xxx_sdio_status = 0;
1303     if ( glue )
1304     {
1305         printk("ssv6xxx_sdio_remove - ssv6xxx_sdio_irq_disable\n");
1306         ssv6xxx_sdio_irq_disable(&glue->core->dev,false);
1307         glue->dev_ready = false;
1308 #if 0
1309         ssv6xxx_dev_remove(glue->dev);
1310 #endif
1311         ssv6xxx_low_sdio_clk(func);
1312 #ifdef CONFIG_FW_ALIGNMENT_CHECK
1313   if(glue->dmaSkb != NULL)
1314          dev_kfree_skb(glue->dmaSkb);
1315 #endif
1316         printk("ssv6xxx_sdio_remove - disable mask\n");
1317         ssv6xxx_sdio_irq_setmask(&glue->core->dev,0xff);
1318 #ifdef CONFIG_PM
1319         ssv6xxx_sdio_trigger_pmu(glue->dev);
1320         if(glue->cmd_skb != NULL)
1321             dev_kfree_skb(glue->cmd_skb);
1322 #endif
1323         ssv6xxx_sdio_power_off(pwlan_data, func);
1324         printk("platform_device_del \n");
1325         platform_device_del(glue->core);
1326         printk("platform_device_put \n");
1327         platform_device_put(glue->core);
1328         kfree(glue);
1329     }
1330     sdio_set_drvdata(func, NULL);
1331     printk("ssv6xxx_sdio_remove leave..........\n");
1332 }
1333 #ifdef CONFIG_PM
1334 static int ssv6xxx_sdio_trigger_pmu(struct device *dev)
1335 {
1336     struct sdio_func *func = dev_to_sdio_func(dev);
1337     struct ssv6xxx_sdio_glue *glue = sdio_get_drvdata(func);
1338     struct cfg_host_cmd *host_cmd;
1339     int writesize;
1340     int ret = 0;
1341     void *tempPointer;
1342 #ifdef SSV_WAKEUP_HOST
1343     if(ssv6xxx_sdio_write_reg(dev, ADR_RX_FLOW_MNG, M_ENG_MACRX|(M_ENG_CPU<<4)|(M_ENG_HWHCI<<8)));
1344     if(ssv6xxx_sdio_write_reg(dev, ADR_RX_FLOW_DATA, M_ENG_MACRX|(M_ENG_CPU<<4)|(M_ENG_HWHCI<<8)));
1345     if(ssv6xxx_sdio_write_reg(dev, ADR_MRX_FLT_TB0+6*4, (sc->mac_deci_tbl[6]|1)));
1346 #else
1347     if(ssv6xxx_sdio_write_reg(dev, ADR_RX_FLOW_MNG, M_ENG_MACRX|(M_ENG_TRASH_CAN<<4)));
1348     if(ssv6xxx_sdio_write_reg(dev, ADR_RX_FLOW_DATA, M_ENG_MACRX|(M_ENG_TRASH_CAN<<4)));
1349     if(ssv6xxx_sdio_write_reg(dev, ADR_RX_FLOW_CTRL, M_ENG_MACRX|(M_ENG_TRASH_CAN<<4)));
1350 #endif
1351     host_cmd = (struct cfg_host_cmd *)glue->cmd_skb->data;
1352     host_cmd->c_type = HOST_CMD;
1353     host_cmd->RSVD0 = 0;
1354     host_cmd->h_cmd = (u8)SSV6XXX_HOST_CMD_PS;
1355     host_cmd->len = sizeof(struct cfg_host_cmd);
1356 #ifdef SSV_WAKEUP_HOST
1357     host_cmd->dummy = sc->ps_aid;
1358 #else
1359  host_cmd->dummy = 0;
1360 #endif
1361     {
1362         tempPointer = glue->cmd_skb->data;
1363         sdio_claim_host(func);
1364         writesize = sdio_align_size(func,sizeof(struct cfg_host_cmd));
1365         do
1366         {
1367             ret = sdio_memcpy_toio(func, glue->dataIOPort, tempPointer, writesize);
1368             if ( ret == -EILSEQ || ret == -ETIMEDOUT )
1369             {
1370                 ret = -1;
1371                 break;
1372             }
1373             else
1374             {
1375                 if(ret)
1376                     dev_err(glue->dev,"Unexpected return value ret=[%d]\n",ret);
1377             }
1378         }
1379         while( ret == -EILSEQ || ret == -ETIMEDOUT);
1380         sdio_release_host(func);
1381         if (ret)
1382             dev_err(glue->dev, "sdio write failed (%d)\n", ret);
1383     }
1384     return ret;
1385 }
1386 static void ssv6xxx_sdio_reset(struct device *child)
1387 {
1388  struct ssv6xxx_sdio_glue *glue = dev_get_drvdata(child->parent);
1389  struct sdio_func *func = dev_to_sdio_func(glue->dev);
1390  printk("%s\n", __FUNCTION__);
1391  if(glue == NULL || glue->dev == NULL || func == NULL)
1392   return;
1393     ssv6xxx_sdio_trigger_pmu(glue->dev);
1394  ssv6xxx_do_sdio_wakeup( func);
1395 }
1396 #ifdef AML_WIFI_MAC
1397 extern void sdio_reinit(void);
1398 extern void extern_wifi_set_enable(int is_on);
1399 #endif
1400 static int ssv6xxx_sdio_suspend(struct device *dev)
1401 {
1402     struct sdio_func *func = dev_to_sdio_func(dev);
1403      mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1404 #ifdef AML_WIFI_MAC
1405     if (!(flags & MMC_PM_KEEP_POWER) || sdio_sr_bhvr == SUSPEND_RESUME_1) {
1406         printk("%s : module will not get power support during suspend. %u\n", __func__, sdio_sr_bhvr);
1407         ssv6xxx_deinit_prepare();
1408         ssv6xxx_sdio_remove(func);
1409         mdelay(100);
1410         extern_wifi_set_enable(0);
1411         mdelay(10);
1412         return 0;
1413     }else
1414 #endif
1415     {
1416         int ret = 0;
1417         dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
1418                  sdio_func_id(func), flags);
1419      ssv6xxx_low_sdio_clk(func);
1420         ret = ssv6xxx_sdio_trigger_pmu(dev);
1421         if (ret)
1422             printk("ssv6xxx_sdio_trigger_pmu fail!!\n");
1423         if (!(flags & MMC_PM_KEEP_POWER))
1424         {
1425          dev_err(dev, "%s: cannot remain alive while host is suspended\n",
1426                     sdio_func_id(func));
1427         }
1428         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1429         if (ret)
1430          return ret;
1431         mdelay(10);
1432 #ifdef SSV_WAKEUP_HOST
1433         ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1434 #endif
1435 #if 0
1436         if (softc->wow_enabled)
1437         {
1438             sdio_flags = sdio_get_host_pm_caps(func);
1439             if (!(sdio_flags & MMC_PM_KEEP_POWER))
1440             {
1441                 dev_err(dev, "can't keep power while host "
1442                         "is suspended\n");
1443                 ret = -EINVAL;
1444                 goto out;
1445             }
1446             ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1447             if (ret)
1448             {
1449                 dev_err(dev, "error while trying to keep power\n");
1450                 goto out;
1451             }
1452         }else{
1453             ssv6xxx_sdio_irq_disable(&glue->core->dev,true);
1454         }
1455 #endif
1456         return ret;
1457     }
1458 }
1459 static int ssv6xxx_sdio_resume(struct device *dev)
1460 {
1461  struct sdio_func *func = dev_to_sdio_func(dev);
1462 #ifdef AML_WIFI_MAC
1463     mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1464 	mdelay(100);
1465     if (!(flags & MMC_PM_KEEP_POWER) || sdio_sr_bhvr == SUSPEND_RESUME_1) {
1466         printk("%s : module is reset, run probe now !! %u\n", __func__, sdio_sr_bhvr);
1467         extern_wifi_set_enable(1);
1468         mdelay(100);
1469         sdio_reinit();
1470         mdelay(150);
1471         ssv6xxx_sdio_probe(func, NULL);
1472         return 0;
1473     }else
1474 #endif
1475  {
1476      printk("ssv6xxx_sdio_resume\n");
1477      {
1478       ssv6xxx_do_sdio_wakeup(func);
1479             mdelay(10);
1480       ssv6xxx_high_sdio_clk(func);
1481             mdelay(10);
1482      }
1483     }
1484     return 0;
1485 }
1486 static const struct dev_pm_ops ssv6xxx_sdio_pm_ops =
1487 {
1488     .suspend = ssv6xxx_sdio_suspend,
1489     .resume = ssv6xxx_sdio_resume,
1490 };
1491 #endif
1492 
1493 
1494 #ifdef AML_WIFI_MAC
1495 static void ssv6xxx_sdio_shutdown(struct device *dev)
1496 {
1497 	struct sdio_func *func = dev_to_sdio_func(dev);
1498 	printk("%s  shutdown_flags:%d \n", __func__,shutdown_flags);
1499 	switch(shutdown_flags){
1500 		case SSV_SYS_REBOOT :
1501 		case SSV_SYS_HALF:
1502 				printk("%s ,system reboot  ..\n", __func__);
1503 				break;
1504 		case SSV_SYS_POWER_OFF :
1505 				printk("%s ,system shutdown .. \n", __func__);
1506 				ssv6xxx_deinit_prepare();
1507 				ssv6xxx_sdio_remove(func);
1508 				mdelay(100);
1509 				extern_wifi_set_enable(0);
1510 				mdelay(100);
1511 				break;
1512 		default:
1513 			printk("%s,unknown event code ..", __func__);
1514 	}
1515 
1516 }
1517 
1518 static int ssv6xxx_reboot_notify(struct notifier_block *nb,
1519 			       unsigned long event, void *p)
1520 {
1521 
1522 	printk("%s, code = %ld \n",__FUNCTION__,event);
1523 	switch (event){
1524 		case SYS_DOWN:
1525 			shutdown_flags = SYS_DOWN;
1526 			break;
1527 		case SYS_HALT:
1528 			shutdown_flags = SYS_HALT;
1529 			break;
1530 		case SYS_POWER_OFF:
1531 			shutdown_flags = SYS_POWER_OFF;
1532 			break;
1533 		default:
1534 			shutdown_flags = event;
1535 			break;
1536 	}
1537 	return NOTIFY_DONE;
1538 }
1539 static struct notifier_block ssv6xxx_reboot_notifier = {
1540 	.notifier_call = ssv6xxx_reboot_notify,
1541 	.next = NULL,
1542 	.priority = 0,
1543 };
1544 #endif
1545 
1546 struct sdio_driver ssv6xxx_sdio_driver =
1547 {
1548     .name = "SSV6XXX_SDIO",
1549     .id_table = ssv6xxx_sdio_devices,
1550     .probe = ssv6xxx_sdio_probe,
1551 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
1552     .remove = __devexit_p(ssv6xxx_sdio_remove),
1553 #else
1554     .remove = ssv6xxx_sdio_remove,
1555 #endif
1556 #ifdef CONFIG_PM
1557     .drv = {
1558         .pm = &ssv6xxx_sdio_pm_ops,
1559 #ifdef AML_WIFI_MAC
1560 	.shutdown = ssv6xxx_sdio_shutdown,
1561 #endif
1562     },
1563 #endif
1564 };
1565 EXPORT_SYMBOL(ssv6xxx_sdio_driver);
1566 #if (defined(CONFIG_SSV_SUPPORT_ANDROID)||defined(CONFIG_SSV_BUILD_AS_ONE_KO))
1567 int ssv6xxx_sdio_init(void)
1568 #else
1569 static int __init ssv6xxx_sdio_init(void)
1570 #endif
1571 {
1572     printk(KERN_INFO "ssv6xxx_sdio_init\n");
1573 #ifdef AML_WIFI_MAC
1574 	register_reboot_notifier(&ssv6xxx_reboot_notifier);
1575 #endif
1576     return sdio_register_driver(&ssv6xxx_sdio_driver);
1577 }
1578 #if (defined(CONFIG_SSV_SUPPORT_ANDROID)||defined(CONFIG_SSV_BUILD_AS_ONE_KO))
1579 void ssv6xxx_sdio_exit(void)
1580 #else
1581 static void __exit ssv6xxx_sdio_exit(void)
1582 #endif
1583 {
1584     printk(KERN_INFO "ssv6xxx_sdio_exit\n");
1585 #ifdef AML_WIFI_MAC
1586 	unregister_reboot_notifier(&ssv6xxx_reboot_notifier);
1587 #endif
1588     sdio_unregister_driver(&ssv6xxx_sdio_driver);
1589 }
1590 #if (defined(CONFIG_SSV_SUPPORT_ANDROID)||defined(CONFIG_SSV_BUILD_AS_ONE_KO))
1591 EXPORT_SYMBOL(ssv6xxx_sdio_init);
1592 EXPORT_SYMBOL(ssv6xxx_sdio_exit);
1593 #else
1594 module_init(ssv6xxx_sdio_init);
1595 module_exit(ssv6xxx_sdio_exit);
1596 #endif
1597 MODULE_LICENSE("GPL");
1598