Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next
[linux-2.6-microblaze.git] / drivers / net / wireless / rsi / rsi_91x_sdio.c
1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  */
17
18 #include <linux/module.h>
19 #include "rsi_sdio.h"
20 #include "rsi_common.h"
21 #include "rsi_coex.h"
22 #include "rsi_hal.h"
23
24 /* Default operating mode is wlan STA + BT */
25 static u16 dev_oper_mode = DEV_OPMODE_STA_BT_DUAL;
26 module_param(dev_oper_mode, ushort, 0444);
27 MODULE_PARM_DESC(dev_oper_mode,
28                  "1[Wi-Fi], 4[BT], 8[BT LE], 5[Wi-Fi STA + BT classic]\n"
29                  "9[Wi-Fi STA + BT LE], 13[Wi-Fi STA + BT classic + BT LE]\n"
30                  "6[AP + BT classic], 14[AP + BT classic + BT LE]");
31
32 /**
33  * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
34  * @rw: Read/write
35  * @func: function number
36  * @raw: indicates whether to perform read after write
37  * @address: address to which to read/write
38  * @writedata: data to write
39  *
40  * Return: argument
41  */
42 static u32 rsi_sdio_set_cmd52_arg(bool rw,
43                                   u8 func,
44                                   u8 raw,
45                                   u32 address,
46                                   u8 writedata)
47 {
48         return ((rw & 1) << 31) | ((func & 0x7) << 28) |
49                 ((raw & 1) << 27) | (1 << 26) |
50                 ((address & 0x1FFFF) << 9) | (1 << 8) |
51                 (writedata & 0xFF);
52 }
53
54 /**
55  * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
56  * @card: Pointer to the mmc_card.
57  * @address: Address to write.
58  * @byte: Data to write.
59  *
60  * Return: Write status.
61  */
62 static int rsi_cmd52writebyte(struct mmc_card *card,
63                               u32 address,
64                               u8 byte)
65 {
66         struct mmc_command io_cmd;
67         u32 arg;
68
69         memset(&io_cmd, 0, sizeof(io_cmd));
70         arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
71         io_cmd.opcode = SD_IO_RW_DIRECT;
72         io_cmd.arg = arg;
73         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
74
75         return mmc_wait_for_cmd(card->host, &io_cmd, 0);
76 }
77
78 /**
79  * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
80  * @card: Pointer to the mmc_card.
81  * @address: Address to read from.
82  * @byte: Variable to store read value.
83  *
84  * Return: Read status.
85  */
86 static int rsi_cmd52readbyte(struct mmc_card *card,
87                              u32 address,
88                              u8 *byte)
89 {
90         struct mmc_command io_cmd;
91         u32 arg;
92         int err;
93
94         memset(&io_cmd, 0, sizeof(io_cmd));
95         arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
96         io_cmd.opcode = SD_IO_RW_DIRECT;
97         io_cmd.arg = arg;
98         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
99
100         err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
101         if ((!err) && (byte))
102                 *byte =  io_cmd.resp[0] & 0xFF;
103         return err;
104 }
105
106 /**
107  * rsi_issue_sdiocommand() - This function issues sdio commands.
108  * @func: Pointer to the sdio_func structure.
109  * @opcode: Opcode value.
110  * @arg: Arguments to pass.
111  * @flags: Flags which are set.
112  * @resp: Pointer to store response.
113  *
114  * Return: err: command status as 0 or -1.
115  */
116 static int rsi_issue_sdiocommand(struct sdio_func *func,
117                                  u32 opcode,
118                                  u32 arg,
119                                  u32 flags,
120                                  u32 *resp)
121 {
122         struct mmc_command cmd;
123         struct mmc_host *host;
124         int err;
125
126         host = func->card->host;
127
128         memset(&cmd, 0, sizeof(struct mmc_command));
129         cmd.opcode = opcode;
130         cmd.arg = arg;
131         cmd.flags = flags;
132         err = mmc_wait_for_cmd(host, &cmd, 3);
133
134         if ((!err) && (resp))
135                 *resp = cmd.resp[0];
136
137         return err;
138 }
139
140 /**
141  * rsi_handle_interrupt() - This function is called upon the occurrence
142  *                          of an interrupt.
143  * @function: Pointer to the sdio_func structure.
144  *
145  * Return: None.
146  */
147 static void rsi_handle_interrupt(struct sdio_func *function)
148 {
149         struct rsi_hw *adapter = sdio_get_drvdata(function);
150         struct rsi_91x_sdiodev *dev =
151                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
152
153         if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
154                 return;
155
156         dev->sdio_irq_task = current;
157         rsi_interrupt_handler(adapter);
158         dev->sdio_irq_task = NULL;
159 }
160
161 /**
162  * rsi_reset_card() - This function resets and re-initializes the card.
163  * @pfunction: Pointer to the sdio_func structure.
164  *
165  * Return: None.
166  */
167 static void rsi_reset_card(struct sdio_func *pfunction)
168 {
169         int ret = 0;
170         int err;
171         struct mmc_card *card = pfunction->card;
172         struct mmc_host *host = card->host;
173         u8 cmd52_resp;
174         u32 clock, resp, i;
175         u16 rca;
176
177         /* Reset 9110 chip */
178         ret = rsi_cmd52writebyte(pfunction->card,
179                                  SDIO_CCCR_ABORT,
180                                  (1 << 3));
181
182         /* Card will not send any response as it is getting reset immediately
183          * Hence expect a timeout status from host controller
184          */
185         if (ret != -ETIMEDOUT)
186                 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
187
188         /* Wait for few milli seconds to get rid of residue charges if any */
189         msleep(20);
190
191         /* Initialize the SDIO card */
192         host->ios.chip_select = MMC_CS_DONTCARE;
193         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
194         host->ios.power_mode = MMC_POWER_UP;
195         host->ios.bus_width = MMC_BUS_WIDTH_1;
196         host->ios.timing = MMC_TIMING_LEGACY;
197         host->ops->set_ios(host, &host->ios);
198
199         /*
200          * This delay should be sufficient to allow the power supply
201          * to reach the minimum voltage.
202          */
203         msleep(20);
204
205         host->ios.clock = host->f_min;
206         host->ios.power_mode = MMC_POWER_ON;
207         host->ops->set_ios(host, &host->ios);
208
209         /*
210          * This delay must be at least 74 clock sizes, or 1 ms, or the
211          * time required to reach a stable voltage.
212          */
213         msleep(20);
214
215         /* Issue CMD0. Goto idle state */
216         host->ios.chip_select = MMC_CS_HIGH;
217         host->ops->set_ios(host, &host->ios);
218         msleep(20);
219         err = rsi_issue_sdiocommand(pfunction,
220                                     MMC_GO_IDLE_STATE,
221                                     0,
222                                     (MMC_RSP_NONE | MMC_CMD_BC),
223                                     NULL);
224         host->ios.chip_select = MMC_CS_DONTCARE;
225         host->ops->set_ios(host, &host->ios);
226         msleep(20);
227         host->use_spi_crc = 0;
228
229         if (err)
230                 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
231
232         /* Issue CMD5, arg = 0 */
233         err = rsi_issue_sdiocommand(pfunction,  SD_IO_SEND_OP_COND, 0,
234                                     (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
235         if (err)
236                 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
237                         __func__, err);
238         card->ocr = resp;
239         /* Issue CMD5, arg = ocr. Wait till card is ready  */
240         for (i = 0; i < 100; i++) {
241                 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
242                                             card->ocr,
243                                             (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
244                 if (err) {
245                         rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
246                                 __func__, err);
247                         break;
248                 }
249
250                 if (resp & MMC_CARD_BUSY)
251                         break;
252                 msleep(20);
253         }
254
255         if ((i == 100) || (err)) {
256                 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
257                         __func__, i, err);
258                 return;
259         }
260
261         /* Issue CMD3, get RCA */
262         err = rsi_issue_sdiocommand(pfunction,
263                                     SD_SEND_RELATIVE_ADDR,
264                                     0,
265                                     (MMC_RSP_R6 | MMC_CMD_BCR),
266                                     &resp);
267         if (err) {
268                 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
269                 return;
270         }
271         rca = resp >> 16;
272         host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
273         host->ops->set_ios(host, &host->ios);
274
275         /* Issue CMD7, select card  */
276         err = rsi_issue_sdiocommand(pfunction,
277                                     MMC_SELECT_CARD,
278                                     (rca << 16),
279                                     (MMC_RSP_R1 | MMC_CMD_AC),
280                                     NULL);
281         if (err) {
282                 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
283                 return;
284         }
285
286         /* Enable high speed */
287         if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
288                 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
289                 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
290                 if (err) {
291                         rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
292                                 __func__, err);
293                 } else {
294                         err = rsi_cmd52writebyte(card,
295                                                  SDIO_CCCR_SPEED,
296                                                  (cmd52_resp | SDIO_SPEED_EHS));
297                         if (err) {
298                                 rsi_dbg(ERR_ZONE,
299                                         "%s: CCR speed regwrite failed %d\n",
300                                         __func__, err);
301                                 return;
302                         }
303                         host->ios.timing = MMC_TIMING_SD_HS;
304                         host->ops->set_ios(host, &host->ios);
305                 }
306         }
307
308         /* Set clock */
309         if (mmc_card_hs(card))
310                 clock = 50000000;
311         else
312                 clock = card->cis.max_dtr;
313
314         if (clock > host->f_max)
315                 clock = host->f_max;
316
317         host->ios.clock = clock;
318         host->ops->set_ios(host, &host->ios);
319
320         if (card->host->caps & MMC_CAP_4_BIT_DATA) {
321                 /* CMD52: Set bus width & disable card detect resistor */
322                 err = rsi_cmd52writebyte(card,
323                                          SDIO_CCCR_IF,
324                                          (SDIO_BUS_CD_DISABLE |
325                                           SDIO_BUS_WIDTH_4BIT));
326                 if (err) {
327                         rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
328                                 __func__, err);
329                         return;
330                 }
331                 host->ios.bus_width = MMC_BUS_WIDTH_4;
332                 host->ops->set_ios(host, &host->ios);
333         }
334 }
335
336 /**
337  * rsi_setclock() - This function sets the clock frequency.
338  * @adapter: Pointer to the adapter structure.
339  * @freq: Clock frequency.
340  *
341  * Return: None.
342  */
343 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
344 {
345         struct rsi_91x_sdiodev *dev =
346                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
347         struct mmc_host *host = dev->pfunction->card->host;
348         u32 clock;
349
350         clock = freq * 1000;
351         if (clock > host->f_max)
352                 clock = host->f_max;
353         host->ios.clock = clock;
354         host->ops->set_ios(host, &host->ios);
355 }
356
357 /**
358  * rsi_setblocklength() - This function sets the host block length.
359  * @adapter: Pointer to the adapter structure.
360  * @length: Block length to be set.
361  *
362  * Return: status: 0 on success, -1 on failure.
363  */
364 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
365 {
366         struct rsi_91x_sdiodev *dev =
367                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
368         int status;
369         rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
370
371         status = sdio_set_block_size(dev->pfunction, length);
372         dev->pfunction->max_blksize = 256;
373         adapter->block_size = dev->pfunction->max_blksize;
374
375         rsi_dbg(INFO_ZONE,
376                 "%s: Operational blk length is %d\n", __func__, length);
377         return status;
378 }
379
380 /**
381  * rsi_setupcard() - This function queries and sets the card's features.
382  * @adapter: Pointer to the adapter structure.
383  *
384  * Return: status: 0 on success, -1 on failure.
385  */
386 static int rsi_setupcard(struct rsi_hw *adapter)
387 {
388         struct rsi_91x_sdiodev *dev =
389                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
390         int status = 0;
391
392         rsi_setclock(adapter, 50000);
393
394         dev->tx_blk_size = 256;
395         status = rsi_setblocklength(adapter, dev->tx_blk_size);
396         if (status)
397                 rsi_dbg(ERR_ZONE,
398                         "%s: Unable to set block length\n", __func__);
399         return status;
400 }
401
402 /**
403  * rsi_sdio_read_register() - This function reads one byte of information
404  *                            from a register.
405  * @adapter: Pointer to the adapter structure.
406  * @addr: Address of the register.
407  * @data: Pointer to the data that stores the data read.
408  *
409  * Return: 0 on success, -1 on failure.
410  */
411 int rsi_sdio_read_register(struct rsi_hw *adapter,
412                            u32 addr,
413                            u8 *data)
414 {
415         struct rsi_91x_sdiodev *dev =
416                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
417         u8 fun_num = 0;
418         int status;
419
420         if (likely(dev->sdio_irq_task != current))
421                 sdio_claim_host(dev->pfunction);
422
423         if (fun_num == 0)
424                 *data = sdio_f0_readb(dev->pfunction, addr, &status);
425         else
426                 *data = sdio_readb(dev->pfunction, addr, &status);
427
428         if (likely(dev->sdio_irq_task != current))
429                 sdio_release_host(dev->pfunction);
430
431         return status;
432 }
433
434 /**
435  * rsi_sdio_write_register() - This function writes one byte of information
436  *                             into a register.
437  * @adapter: Pointer to the adapter structure.
438  * @function: Function Number.
439  * @addr: Address of the register.
440  * @data: Pointer to the data tha has to be written.
441  *
442  * Return: 0 on success, -1 on failure.
443  */
444 int rsi_sdio_write_register(struct rsi_hw *adapter,
445                             u8 function,
446                             u32 addr,
447                             u8 *data)
448 {
449         struct rsi_91x_sdiodev *dev =
450                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
451         int status = 0;
452
453         if (likely(dev->sdio_irq_task != current))
454                 sdio_claim_host(dev->pfunction);
455
456         if (function == 0)
457                 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
458         else
459                 sdio_writeb(dev->pfunction, *data, addr, &status);
460
461         if (likely(dev->sdio_irq_task != current))
462                 sdio_release_host(dev->pfunction);
463
464         return status;
465 }
466
467 /**
468  * rsi_sdio_ack_intr() - This function acks the interrupt received.
469  * @adapter: Pointer to the adapter structure.
470  * @int_bit: Interrupt bit to write into register.
471  *
472  * Return: None.
473  */
474 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
475 {
476         int status;
477         status = rsi_sdio_write_register(adapter,
478                                          1,
479                                          (SDIO_FUN1_INTR_CLR_REG |
480                                           RSI_SD_REQUEST_MASTER),
481                                          &int_bit);
482         if (status)
483                 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
484 }
485
486
487
488 /**
489  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
490  *                                     information from the SD card.
491  * @adapter: Pointer to the adapter structure.
492  * @addr: Address of the register.
493  * @count: Number of multiple bytes to be read.
494  * @data: Pointer to the read data.
495  *
496  * Return: 0 on success, -1 on failure.
497  */
498 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
499                                            u32 addr,
500                                            u8 *data,
501                                            u16 count)
502 {
503         struct rsi_91x_sdiodev *dev =
504                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
505         u32 status;
506
507         if (likely(dev->sdio_irq_task != current))
508                 sdio_claim_host(dev->pfunction);
509
510         status =  sdio_readsb(dev->pfunction, data, addr, count);
511
512         if (likely(dev->sdio_irq_task != current))
513                 sdio_release_host(dev->pfunction);
514
515         if (status != 0)
516                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
517         return status;
518 }
519
520 /**
521  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
522  *                                      information to the SD card.
523  * @adapter: Pointer to the adapter structure.
524  * @addr: Address of the register.
525  * @data: Pointer to the data that has to be written.
526  * @count: Number of multiple bytes to be written.
527  *
528  * Return: 0 on success, -1 on failure.
529  */
530 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
531                                      u32 addr,
532                                      u8 *data,
533                                      u16 count)
534 {
535         struct rsi_91x_sdiodev *dev =
536                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
537         int status;
538
539         if (dev->write_fail > 1) {
540                 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
541                 return 0;
542         } else if (dev->write_fail == 1) {
543                 /**
544                  * Assuming it is a CRC failure, we want to allow another
545                  *  card write
546                  */
547                 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
548                 dev->write_fail++;
549         }
550
551         if (likely(dev->sdio_irq_task != current))
552                 sdio_claim_host(dev->pfunction);
553
554         status = sdio_writesb(dev->pfunction, addr, data, count);
555
556         if (likely(dev->sdio_irq_task != current))
557                 sdio_release_host(dev->pfunction);
558
559         if (status) {
560                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
561                         __func__, status);
562                 dev->write_fail = 2;
563         } else {
564                 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
565         }
566         return status;
567 }
568
569 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
570                                            u32 base_address,
571                                            u32 instructions_sz,
572                                            u16 block_size,
573                                            u8 *ta_firmware)
574 {
575         u32 num_blocks, offset, i;
576         u16 msb_address, lsb_address;
577         u8 *temp_buf;
578         int status;
579
580         num_blocks = instructions_sz / block_size;
581         msb_address = base_address >> 16;
582
583         rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
584                 instructions_sz, num_blocks);
585
586         temp_buf = kmalloc(block_size, GFP_KERNEL);
587         if (!temp_buf)
588                 return -ENOMEM;
589
590         /* Loading DM ms word in the sdio slave */
591         status = rsi_sdio_master_access_msword(adapter, msb_address);
592         if (status < 0) {
593                 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
594                 goto out_free;
595         }
596
597         for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
598                 memcpy(temp_buf, ta_firmware + offset, block_size);
599                 lsb_address = (u16)base_address;
600                 status = rsi_sdio_write_register_multiple
601                                         (adapter,
602                                          lsb_address | RSI_SD_REQUEST_MASTER,
603                                          temp_buf, block_size);
604                 if (status < 0) {
605                         rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
606                         goto out_free;
607                 }
608                 rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
609                 base_address += block_size;
610
611                 if ((base_address >> 16) != msb_address) {
612                         msb_address += 1;
613
614                         /* Loading DM ms word in the sdio slave */
615                         status = rsi_sdio_master_access_msword(adapter,
616                                                                msb_address);
617                         if (status < 0) {
618                                 rsi_dbg(ERR_ZONE,
619                                         "%s: Unable to set ms word reg\n",
620                                         __func__);
621                                 goto out_free;
622                         }
623                 }
624         }
625
626         if (instructions_sz % block_size) {
627                 memset(temp_buf, 0, block_size);
628                 memcpy(temp_buf, ta_firmware + offset,
629                        instructions_sz % block_size);
630                 lsb_address = (u16)base_address;
631                 status = rsi_sdio_write_register_multiple
632                                         (adapter,
633                                          lsb_address | RSI_SD_REQUEST_MASTER,
634                                          temp_buf,
635                                          instructions_sz % block_size);
636                 if (status < 0)
637                         goto out_free;
638                 rsi_dbg(INFO_ZONE,
639                         "Written Last Block in Address 0x%x Successfully\n",
640                         offset | RSI_SD_REQUEST_MASTER);
641         }
642
643         status = 0;
644 out_free:
645         kfree(temp_buf);
646         return status;
647 }
648
649 #define FLASH_SIZE_ADDR                 0x04000016
650 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
651                                     u32 *read_buf, u16 size)
652 {
653         u32 addr_on_bus, *data;
654         u16 ms_addr;
655         int status;
656
657         data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
658         if (!data)
659                 return -ENOMEM;
660
661         ms_addr = (addr >> 16);
662         status = rsi_sdio_master_access_msword(adapter, ms_addr);
663         if (status < 0) {
664                 rsi_dbg(ERR_ZONE,
665                         "%s: Unable to set ms word to common reg\n",
666                         __func__);
667                 goto err;
668         }
669         addr &= 0xFFFF;
670
671         addr_on_bus = (addr & 0xFF000000);
672         if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
673             (addr_on_bus == 0x0))
674                 addr_on_bus = (addr & ~(0x3));
675         else
676                 addr_on_bus = addr;
677
678         /* Bring TA out of reset */
679         status = rsi_sdio_read_register_multiple
680                                         (adapter,
681                                          (addr_on_bus | RSI_SD_REQUEST_MASTER),
682                                          (u8 *)data, 4);
683         if (status < 0) {
684                 rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
685                 goto err;
686         }
687         if (size == 2) {
688                 if ((addr & 0x3) == 0)
689                         *read_buf = *data;
690                 else
691                         *read_buf  = (*data >> 16);
692                 *read_buf = (*read_buf & 0xFFFF);
693         } else if (size == 1) {
694                 if ((addr & 0x3) == 0)
695                         *read_buf = *data;
696                 else if ((addr & 0x3) == 1)
697                         *read_buf = (*data >> 8);
698                 else if ((addr & 0x3) == 2)
699                         *read_buf = (*data >> 16);
700                 else
701                         *read_buf = (*data >> 24);
702                 *read_buf = (*read_buf & 0xFF);
703         } else {
704                 *read_buf = *data;
705         }
706
707 err:
708         kfree(data);
709         return status;
710 }
711
712 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
713                                      unsigned long addr,
714                                      unsigned long data, u16 size)
715 {
716         unsigned long *data_aligned;
717         int status;
718
719         data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
720         if (!data_aligned)
721                 return -ENOMEM;
722
723         if (size == 2) {
724                 *data_aligned = ((data << 16) | (data & 0xFFFF));
725         } else if (size == 1) {
726                 u32 temp_data = data & 0xFF;
727
728                 *data_aligned = ((temp_data << 24) | (temp_data << 16) |
729                                  (temp_data << 8) | temp_data);
730         } else {
731                 *data_aligned = data;
732         }
733         size = 4;
734
735         status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
736         if (status < 0) {
737                 rsi_dbg(ERR_ZONE,
738                         "%s: Unable to set ms word to common reg\n",
739                         __func__);
740                 kfree(data_aligned);
741                 return -EIO;
742         }
743         addr = addr & 0xFFFF;
744
745         /* Bring TA out of reset */
746         status = rsi_sdio_write_register_multiple
747                                         (adapter,
748                                          (addr | RSI_SD_REQUEST_MASTER),
749                                          (u8 *)data_aligned, size);
750         if (status < 0)
751                 rsi_dbg(ERR_ZONE,
752                         "%s: Unable to do AHB reg write\n", __func__);
753
754         kfree(data_aligned);
755         return status;
756 }
757
758 /**
759  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
760  * @adapter: Pointer to the adapter structure.
761  * @pkt: Pointer to the data to be written on to the device.
762  * @len: length of the data to be written on to the device.
763  *
764  * Return: 0 on success, -1 on failure.
765  */
766 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
767                                         u8 *pkt,
768                                         u32 len)
769 {
770         struct rsi_91x_sdiodev *dev =
771                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
772         u32 block_size = dev->tx_blk_size;
773         u32 num_blocks, address, length;
774         u32 queueno;
775         int status;
776
777         queueno = ((pkt[1] >> 4) & 0xf);
778         if (queueno == RSI_BT_MGMT_Q || queueno == RSI_BT_DATA_Q)
779                 queueno = RSI_BT_Q;
780
781         num_blocks = len / block_size;
782
783         if (len % block_size)
784                 num_blocks++;
785
786         address = (num_blocks * block_size | (queueno << 12));
787         length  = num_blocks * block_size;
788
789         status = rsi_sdio_write_register_multiple(adapter,
790                                                   address,
791                                                   (u8 *)pkt,
792                                                   length);
793         if (status)
794                 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
795                         __func__, status);
796         rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
797         return status;
798 }
799
800 /**
801  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
802                                    from the device.
803  * @adapter: Pointer to the adapter data structure.
804  * @pkt: Pointer to the packet data to be read from the the device.
805  * @length: Length of the data to be read from the device.
806  *
807  * Return: 0 on success, -1 on failure.
808  */
809 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
810                                 u8 *pkt,
811                                 u32 length)
812 {
813         int status = -EINVAL;
814
815         if (!length) {
816                 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
817                 return status;
818         }
819
820         status = rsi_sdio_read_register_multiple(adapter,
821                                                  length,
822                                                  (u8 *)pkt,
823                                                  length); /*num of bytes*/
824
825         if (status)
826                 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
827                         status);
828         return status;
829 }
830
831 /**
832  * rsi_init_sdio_interface() - This function does init specific to SDIO.
833  *
834  * @adapter: Pointer to the adapter data structure.
835  * @pkt: Pointer to the packet data to be read from the the device.
836  *
837  * Return: 0 on success, -1 on failure.
838  */
839
840 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
841                                    struct sdio_func *pfunction)
842 {
843         struct rsi_91x_sdiodev *rsi_91x_dev;
844         int status;
845
846         rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
847         if (!rsi_91x_dev)
848                 return -ENOMEM;
849
850         adapter->rsi_dev = rsi_91x_dev;
851
852         sdio_claim_host(pfunction);
853
854         pfunction->enable_timeout = 100;
855         status = sdio_enable_func(pfunction);
856         if (status) {
857                 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
858                 sdio_release_host(pfunction);
859                 return status;
860         }
861
862         rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
863
864         rsi_91x_dev->pfunction = pfunction;
865         adapter->device = &pfunction->dev;
866
867         sdio_set_drvdata(pfunction, adapter);
868
869         status = rsi_setupcard(adapter);
870         if (status) {
871                 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
872                 goto fail;
873         }
874
875         rsi_dbg(INIT_ZONE, "%s: Setup card successfully\n", __func__);
876
877         status = rsi_init_sdio_slave_regs(adapter);
878         if (status) {
879                 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
880                 goto fail;
881         }
882         sdio_release_host(pfunction);
883
884         adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
885         adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
886
887 #ifdef CONFIG_RSI_DEBUGFS
888         adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
889 #endif
890         return 0;
891 fail:
892         sdio_disable_func(pfunction);
893         sdio_release_host(pfunction);
894         return status;
895 }
896
897 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
898 {
899         struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
900         struct sdio_func *pfunction = sdev->pfunction;
901         int ii;
902
903         for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
904                 skb_queue_purge(&adapter->priv->tx_queue[ii]);
905
906         /* Initialize device again */
907         sdio_claim_host(pfunction);
908
909         sdio_release_irq(pfunction);
910         rsi_reset_card(pfunction);
911
912         sdio_enable_func(pfunction);
913         rsi_setupcard(adapter);
914         rsi_init_sdio_slave_regs(adapter);
915         sdio_claim_irq(pfunction, rsi_handle_interrupt);
916         rsi_hal_device_init(adapter);
917
918         sdio_release_host(pfunction);
919
920         return 0;
921 }
922
923 static int rsi_sdio_ta_reset(struct rsi_hw *adapter)
924 {
925         int status;
926         u32 addr;
927         u8 *data;
928
929         data = kzalloc(RSI_9116_REG_SIZE, GFP_KERNEL);
930         if (!data)
931                 return -ENOMEM;
932
933         status = rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR);
934         if (status < 0) {
935                 rsi_dbg(ERR_ZONE,
936                         "Unable to set ms word to common reg\n");
937                 goto err;
938         }
939
940         rsi_dbg(INIT_ZONE, "%s: Bring TA out of reset\n", __func__);
941         put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
942         addr = TA_HOLD_THREAD_REG | RSI_SD_REQUEST_MASTER;
943         status = rsi_sdio_write_register_multiple(adapter, addr,
944                                                   (u8 *)data,
945                                                   RSI_9116_REG_SIZE);
946         if (status < 0) {
947                 rsi_dbg(ERR_ZONE, "Unable to hold TA threads\n");
948                 goto err;
949         }
950
951         put_unaligned_le32(TA_SOFT_RST_CLR, data);
952         addr = TA_SOFT_RESET_REG | RSI_SD_REQUEST_MASTER;
953         status = rsi_sdio_write_register_multiple(adapter, addr,
954                                                   (u8 *)data,
955                                                   RSI_9116_REG_SIZE);
956         if (status < 0) {
957                 rsi_dbg(ERR_ZONE, "Unable to get TA out of reset\n");
958                 goto err;
959         }
960
961         put_unaligned_le32(TA_PC_ZERO, data);
962         addr = TA_TH0_PC_REG | RSI_SD_REQUEST_MASTER;
963         status = rsi_sdio_write_register_multiple(adapter, addr,
964                                                   (u8 *)data,
965                                                   RSI_9116_REG_SIZE);
966         if (status < 0) {
967                 rsi_dbg(ERR_ZONE, "Unable to Reset TA PC value\n");
968                 status = -EINVAL;
969                 goto err;
970         }
971
972         put_unaligned_le32(TA_RELEASE_THREAD_VALUE, data);
973         addr = TA_RELEASE_THREAD_REG | RSI_SD_REQUEST_MASTER;
974         status = rsi_sdio_write_register_multiple(adapter, addr,
975                                                   (u8 *)data,
976                                                   RSI_9116_REG_SIZE);
977         if (status < 0) {
978                 rsi_dbg(ERR_ZONE, "Unable to release TA threads\n");
979                 goto err;
980         }
981
982         status = rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR);
983         if (status < 0) {
984                 rsi_dbg(ERR_ZONE, "Unable to set ms word to common reg\n");
985                 goto err;
986         }
987         rsi_dbg(INIT_ZONE, "***** TA Reset done *****\n");
988
989 err:
990         kfree(data);
991         return status;
992 }
993
994 static struct rsi_host_intf_ops sdio_host_intf_ops = {
995         .write_pkt              = rsi_sdio_host_intf_write_pkt,
996         .read_pkt               = rsi_sdio_host_intf_read_pkt,
997         .master_access_msword   = rsi_sdio_master_access_msword,
998         .read_reg_multiple      = rsi_sdio_read_register_multiple,
999         .write_reg_multiple     = rsi_sdio_write_register_multiple,
1000         .master_reg_read        = rsi_sdio_master_reg_read,
1001         .master_reg_write       = rsi_sdio_master_reg_write,
1002         .load_data_master_write = rsi_sdio_load_data_master_write,
1003         .reinit_device          = rsi_sdio_reinit_device,
1004         .ta_reset               = rsi_sdio_ta_reset,
1005 };
1006
1007 /**
1008  * rsi_probe() - This function is called by kernel when the driver provided
1009  *               Vendor and device IDs are matched. All the initialization
1010  *               work is done here.
1011  * @pfunction: Pointer to the sdio_func structure.
1012  * @id: Pointer to sdio_device_id structure.
1013  *
1014  * Return: 0 on success, 1 on failure.
1015  */
1016 static int rsi_probe(struct sdio_func *pfunction,
1017                      const struct sdio_device_id *id)
1018 {
1019         struct rsi_hw *adapter;
1020         struct rsi_91x_sdiodev *sdev;
1021         int status = -EINVAL;
1022
1023         rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
1024
1025         adapter = rsi_91x_init(dev_oper_mode);
1026         if (!adapter) {
1027                 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
1028                         __func__);
1029                 return -EINVAL;
1030         }
1031         adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
1032         adapter->host_intf_ops = &sdio_host_intf_ops;
1033
1034         if (rsi_init_sdio_interface(adapter, pfunction)) {
1035                 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
1036                         __func__);
1037                 status = -EIO;
1038                 goto fail_free_adapter;
1039         }
1040
1041         if (pfunction->device == RSI_SDIO_PID_9113) {
1042                 rsi_dbg(ERR_ZONE, "%s: 9113 module detected\n", __func__);
1043                 adapter->device_model = RSI_DEV_9113;
1044         } else  if (pfunction->device == RSI_SDIO_PID_9116) {
1045                 rsi_dbg(ERR_ZONE, "%s: 9116 module detected\n", __func__);
1046                 adapter->device_model = RSI_DEV_9116;
1047         } else {
1048                 rsi_dbg(ERR_ZONE,
1049                         "%s: Unsupported RSI device id 0x%x\n", __func__,
1050                         pfunction->device);
1051                 goto fail_free_adapter;
1052         }
1053
1054         sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1055         rsi_init_event(&sdev->rx_thread.event);
1056         status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
1057                                     rsi_sdio_rx_thread, "SDIO-RX-Thread");
1058         if (status) {
1059                 rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
1060                 goto fail_kill_thread;
1061         }
1062         skb_queue_head_init(&sdev->rx_q.head);
1063         sdev->rx_q.num_rx_pkts = 0;
1064
1065         sdio_claim_host(pfunction);
1066         if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
1067                 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
1068                 sdio_release_host(pfunction);
1069                 status = -EIO;
1070                 goto fail_claim_irq;
1071         }
1072         sdio_release_host(pfunction);
1073         rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
1074
1075         if (rsi_hal_device_init(adapter)) {
1076                 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
1077                 status = -EINVAL;
1078                 goto fail_dev_init;
1079         }
1080         rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
1081
1082         if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
1083                 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
1084                 status = -EIO;
1085                 goto fail_dev_init;
1086         }
1087
1088         adapter->priv->hibernate_resume = false;
1089         adapter->priv->reinit_hw = false;
1090         return 0;
1091
1092 fail_dev_init:
1093         sdio_claim_host(pfunction);
1094         sdio_release_irq(pfunction);
1095         sdio_release_host(pfunction);
1096 fail_claim_irq:
1097         rsi_kill_thread(&sdev->rx_thread);
1098 fail_kill_thread:
1099         sdio_claim_host(pfunction);
1100         sdio_disable_func(pfunction);
1101         sdio_release_host(pfunction);
1102 fail_free_adapter:
1103         rsi_91x_deinit(adapter);
1104         rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
1105         return status;
1106 }
1107
1108 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
1109                            u16 len_in_bits)
1110 {
1111         rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
1112                                   ((addr << 6) | ((data >> 16) & 0xffff)), 2);
1113         rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
1114                                   (data & 0xffff), 2);
1115         rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
1116                                   RSI_GSPI_CTRL_REG0_VALUE, 2);
1117         rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
1118                                   ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
1119         msleep(20);
1120 }
1121
1122 /*This function resets and re-initializes the chip.*/
1123 static void rsi_reset_chip(struct rsi_hw *adapter)
1124 {
1125         u8 *data;
1126         u8 sdio_interrupt_status = 0;
1127         u8 request = 1;
1128         int ret;
1129
1130         data = kzalloc(sizeof(u32), GFP_KERNEL);
1131         if (!data)
1132                 return;
1133
1134         rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
1135         ret =  rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
1136         if (ret < 0) {
1137                 rsi_dbg(ERR_ZONE,
1138                         "%s: Failed to write SDIO wakeup register\n", __func__);
1139                 goto err;
1140         }
1141         msleep(20);
1142         ret =  rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1143                                       &sdio_interrupt_status);
1144         if (ret < 0) {
1145                 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1146                         __func__);
1147                 goto err;
1148         }
1149         rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1150                 __func__, sdio_interrupt_status);
1151
1152         /* Put Thread-Arch processor on hold */
1153         if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1154                 rsi_dbg(ERR_ZONE,
1155                         "%s: Unable to set ms word to common reg\n",
1156                         __func__);
1157                 goto err;
1158         }
1159
1160         put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1161         if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1162                                              RSI_SD_REQUEST_MASTER,
1163                                              data, 4)) {
1164                 rsi_dbg(ERR_ZONE,
1165                         "%s: Unable to hold Thread-Arch processor threads\n",
1166                         __func__);
1167                 goto err;
1168         }
1169
1170         /* This msleep will ensure Thread-Arch processor to go to hold
1171          * and any pending dma transfers to rf spi in device to finish.
1172          */
1173         msleep(100);
1174         if (adapter->device_model != RSI_DEV_9116) {
1175                 ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1176                 ulp_read_write(adapter,
1177                                RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1178                 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0,
1179                                32);
1180                 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
1181                                RSI_ULP_WRITE_50, 32);
1182                 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
1183                                RSI_ULP_WRITE_0, 32);
1184                 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1185                                RSI_ULP_TIMER_ENABLE, 32);
1186         } else {
1187                 if ((rsi_sdio_master_reg_write(adapter,
1188                                                NWP_WWD_INTERRUPT_TIMER,
1189                                                NWP_WWD_INT_TIMER_CLKS,
1190                                                RSI_9116_REG_SIZE)) < 0) {
1191                         rsi_dbg(ERR_ZONE, "Failed to write to intr timer\n");
1192                 }
1193                 if ((rsi_sdio_master_reg_write(adapter,
1194                                                NWP_WWD_SYSTEM_RESET_TIMER,
1195                                                NWP_WWD_SYS_RESET_TIMER_CLKS,
1196                                                RSI_9116_REG_SIZE)) < 0) {
1197                         rsi_dbg(ERR_ZONE,
1198                                 "Failed to write to system reset timer\n");
1199                 }
1200                 if ((rsi_sdio_master_reg_write(adapter,
1201                                                NWP_WWD_MODE_AND_RSTART,
1202                                                NWP_WWD_TIMER_DISABLE,
1203                                                RSI_9116_REG_SIZE)) < 0) {
1204                         rsi_dbg(ERR_ZONE,
1205                                 "Failed to write to mode and restart\n");
1206                 }
1207                 rsi_dbg(ERR_ZONE, "***** Watch Dog Reset Successful *****\n");
1208         }
1209         /* This msleep will be sufficient for the ulp
1210          * read write operations to complete for chip reset.
1211          */
1212         msleep(500);
1213 err:
1214         kfree(data);
1215         return;
1216 }
1217
1218 /**
1219  * rsi_disconnect() - This function performs the reverse of the probe function.
1220  * @pfunction: Pointer to the sdio_func structure.
1221  *
1222  * Return: void.
1223  */
1224 static void rsi_disconnect(struct sdio_func *pfunction)
1225 {
1226         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1227         struct rsi_91x_sdiodev *dev;
1228
1229         if (!adapter)
1230                 return;
1231
1232         dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1233
1234         rsi_kill_thread(&dev->rx_thread);
1235         sdio_claim_host(pfunction);
1236         sdio_release_irq(pfunction);
1237         sdio_release_host(pfunction);
1238         mdelay(10);
1239
1240         rsi_mac80211_detach(adapter);
1241         mdelay(10);
1242
1243         if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1244             adapter->priv->bt_adapter) {
1245                 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1246                 adapter->priv->bt_adapter = NULL;
1247         }
1248
1249         /* Reset Chip */
1250         rsi_reset_chip(adapter);
1251
1252         /* Resetting to take care of the case, where-in driver is re-loaded */
1253         sdio_claim_host(pfunction);
1254         rsi_reset_card(pfunction);
1255         sdio_disable_func(pfunction);
1256         sdio_release_host(pfunction);
1257         dev->write_fail = 2;
1258         rsi_91x_deinit(adapter);
1259         rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1260
1261 }
1262
1263 #ifdef CONFIG_PM
1264 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1265 {
1266         struct rsi_91x_sdiodev *dev =
1267                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1268         struct sdio_func *func = dev->pfunction;
1269         int ret;
1270
1271         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1272         if (ret)
1273                 rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1274
1275         return ret;
1276 }
1277
1278 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1279 {
1280         struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1281         u8 isr_status = 0, data = 0;
1282         int ret;
1283         unsigned long t1;
1284
1285         rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1286         t1 = jiffies;
1287         do {
1288                 rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1289                                        &isr_status);
1290                 rsi_dbg(INFO_ZONE, ".");
1291         } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1292         rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1293
1294         sdio_claim_host(pfunc);
1295         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1296         if (ret < 0) {
1297                 rsi_dbg(ERR_ZONE,
1298                         "%s: Failed to read int enable register\n",
1299                         __func__);
1300                 goto done;
1301         }
1302
1303         data &= RSI_INT_ENABLE_MASK;
1304         ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1305         if (ret < 0) {
1306                 rsi_dbg(ERR_ZONE,
1307                         "%s: Failed to write to int enable register\n",
1308                         __func__);
1309                 goto done;
1310         }
1311         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1312         if (ret < 0) {
1313                 rsi_dbg(ERR_ZONE,
1314                         "%s: Failed to read int enable register\n",
1315                         __func__);
1316                 goto done;
1317         }
1318         rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1319
1320 done:
1321         sdio_release_host(pfunc);
1322         return ret;
1323 }
1324
1325 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1326 {
1327         u8 data;
1328         int ret;
1329         struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1330         struct rsi_common *common = adapter->priv;
1331
1332         sdio_claim_host(pfunc);
1333         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1334         if (ret < 0) {
1335                 rsi_dbg(ERR_ZONE,
1336                         "%s: Failed to read int enable register\n", __func__);
1337                 goto done;
1338         }
1339
1340         data |= ~RSI_INT_ENABLE_MASK & 0xff;
1341
1342         ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1343         if (ret < 0) {
1344                 rsi_dbg(ERR_ZONE,
1345                         "%s: Failed to write to int enable register\n",
1346                         __func__);
1347                 goto done;
1348         }
1349
1350         if ((common->wow_flags & RSI_WOW_ENABLED) &&
1351             (common->wow_flags & RSI_WOW_NO_CONNECTION))
1352                 rsi_dbg(ERR_ZONE,
1353                         "##### Device can not wake up through WLAN\n");
1354
1355         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1356         if (ret < 0) {
1357                 rsi_dbg(ERR_ZONE,
1358                         "%s: Failed to read int enable register\n", __func__);
1359                 goto done;
1360         }
1361         rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1362
1363 done:
1364         sdio_release_host(pfunc);
1365         return ret;
1366 }
1367
1368 static int rsi_suspend(struct device *dev)
1369 {
1370         int ret;
1371         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1372         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1373         struct rsi_common *common;
1374
1375         if (!adapter) {
1376                 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1377                 return -ENODEV;
1378         }
1379         common = adapter->priv;
1380         rsi_sdio_disable_interrupts(pfunction);
1381
1382         ret = rsi_set_sdio_pm_caps(adapter);
1383         if (ret)
1384                 rsi_dbg(INFO_ZONE,
1385                         "Setting power management caps failed\n");
1386         common->fsm_state = FSM_CARD_NOT_READY;
1387
1388         return 0;
1389 }
1390
1391 static int rsi_resume(struct device *dev)
1392 {
1393         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1394         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1395         struct rsi_common *common = adapter->priv;
1396
1397         common->fsm_state = FSM_MAC_INIT_DONE;
1398         rsi_sdio_enable_interrupts(pfunction);
1399
1400         return 0;
1401 }
1402
1403 static int rsi_freeze(struct device *dev)
1404 {
1405         int ret;
1406         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1407         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1408         struct rsi_common *common;
1409         struct rsi_91x_sdiodev *sdev;
1410
1411         rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1412
1413         if (!adapter) {
1414                 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1415                 return -ENODEV;
1416         }
1417         common = adapter->priv;
1418         sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1419
1420         if ((common->wow_flags & RSI_WOW_ENABLED) &&
1421             (common->wow_flags & RSI_WOW_NO_CONNECTION))
1422                 rsi_dbg(ERR_ZONE,
1423                         "##### Device can not wake up through WLAN\n");
1424
1425         if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
1426             common->bt_adapter) {
1427                 rsi_bt_ops.detach(common->bt_adapter);
1428                 common->bt_adapter = NULL;
1429         }
1430
1431         ret = rsi_sdio_disable_interrupts(pfunction);
1432
1433         if (sdev->write_fail)
1434                 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1435
1436         ret = rsi_set_sdio_pm_caps(adapter);
1437         if (ret)
1438                 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1439
1440         rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1441
1442         return 0;
1443 }
1444
1445 static int rsi_thaw(struct device *dev)
1446 {
1447         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1448         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1449         struct rsi_common *common = adapter->priv;
1450
1451         rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1452
1453         common->hibernate_resume = true;
1454         common->fsm_state = FSM_CARD_NOT_READY;
1455         common->iface_down = true;
1456
1457         rsi_sdio_enable_interrupts(pfunction);
1458
1459         rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1460
1461         return 0;
1462 }
1463
1464 static void rsi_shutdown(struct device *dev)
1465 {
1466         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1467         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1468         struct rsi_91x_sdiodev *sdev =
1469                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1470         struct ieee80211_hw *hw = adapter->hw;
1471
1472         rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1473
1474         if (hw) {
1475                 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1476
1477                 if (rsi_config_wowlan(adapter, wowlan))
1478                         rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1479         }
1480
1481         if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1482             adapter->priv->bt_adapter) {
1483                 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1484                 adapter->priv->bt_adapter = NULL;
1485         }
1486
1487         rsi_sdio_disable_interrupts(sdev->pfunction);
1488
1489         if (sdev->write_fail)
1490                 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1491
1492         if (rsi_set_sdio_pm_caps(adapter))
1493                 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1494
1495         rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
1496 }
1497
1498 static int rsi_restore(struct device *dev)
1499 {
1500         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1501         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1502         struct rsi_common *common = adapter->priv;
1503
1504         rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
1505         common->hibernate_resume = true;
1506         common->fsm_state = FSM_FW_NOT_LOADED;
1507         common->iface_down = true;
1508
1509         adapter->sc_nvifs = 0;
1510         adapter->ps_state = PS_NONE;
1511
1512         common->wow_flags = 0;
1513         common->iface_down = false;
1514
1515         rsi_dbg(INFO_ZONE, "RSI module restored\n");
1516
1517         return 0;
1518 }
1519 static const struct dev_pm_ops rsi_pm_ops = {
1520         .suspend = rsi_suspend,
1521         .resume = rsi_resume,
1522         .freeze = rsi_freeze,
1523         .thaw = rsi_thaw,
1524         .restore = rsi_restore,
1525 };
1526 #endif
1527
1528 static const struct sdio_device_id rsi_dev_table[] =  {
1529         { SDIO_DEVICE(RSI_SDIO_VENDOR_ID, RSI_SDIO_PID_9113) },
1530         { SDIO_DEVICE(RSI_SDIO_VENDOR_ID, RSI_SDIO_PID_9116) },
1531         { /* Blank */},
1532 };
1533
1534 static struct sdio_driver rsi_driver = {
1535         .name       = "RSI-SDIO WLAN",
1536         .probe      = rsi_probe,
1537         .remove     = rsi_disconnect,
1538         .id_table   = rsi_dev_table,
1539 #ifdef CONFIG_PM
1540         .drv = {
1541                 .pm = &rsi_pm_ops,
1542                 .shutdown   = rsi_shutdown,
1543         }
1544 #endif
1545 };
1546
1547 /**
1548  * rsi_module_init() - This function registers the sdio module.
1549  * @void: Void.
1550  *
1551  * Return: 0 on success.
1552  */
1553 static int rsi_module_init(void)
1554 {
1555         int ret;
1556
1557         ret = sdio_register_driver(&rsi_driver);
1558         rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1559         return ret;
1560 }
1561
1562 /**
1563  * rsi_module_exit() - This function unregisters the sdio module.
1564  * @void: Void.
1565  *
1566  * Return: None.
1567  */
1568 static void rsi_module_exit(void)
1569 {
1570         sdio_unregister_driver(&rsi_driver);
1571         rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1572 }
1573
1574 module_init(rsi_module_init);
1575 module_exit(rsi_module_exit);
1576
1577 MODULE_AUTHOR("Redpine Signals Inc");
1578 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1579 MODULE_SUPPORTED_DEVICE("RSI-91x");
1580 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1581 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1582 MODULE_VERSION("0.1");
1583 MODULE_LICENSE("Dual BSD/GPL");