Linux-libre 5.3.12-gnu
[librecmc/linux-libre.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         if (!host->ocr_avail) {
234                 err = rsi_issue_sdiocommand(pfunction,  SD_IO_SEND_OP_COND, 0,
235                                             (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
236                 if (err)
237                         rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
238                                 __func__, err);
239
240                 host->ocr_avail = resp;
241         }
242         /* Issue CMD5, arg = ocr. Wait till card is ready  */
243         for (i = 0; i < 100; i++) {
244                 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
245                                             host->ocr_avail,
246                                             (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
247                 if (err) {
248                         rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
249                                 __func__, err);
250                         break;
251                 }
252
253                 if (resp & MMC_CARD_BUSY)
254                         break;
255                 msleep(20);
256         }
257
258         if ((i == 100) || (err)) {
259                 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
260                         __func__, i, err);
261                 return;
262         }
263
264         /* Issue CMD3, get RCA */
265         err = rsi_issue_sdiocommand(pfunction,
266                                     SD_SEND_RELATIVE_ADDR,
267                                     0,
268                                     (MMC_RSP_R6 | MMC_CMD_BCR),
269                                     &resp);
270         if (err) {
271                 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
272                 return;
273         }
274         rca = resp >> 16;
275         host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
276         host->ops->set_ios(host, &host->ios);
277
278         /* Issue CMD7, select card  */
279         err = rsi_issue_sdiocommand(pfunction,
280                                     MMC_SELECT_CARD,
281                                     (rca << 16),
282                                     (MMC_RSP_R1 | MMC_CMD_AC),
283                                     NULL);
284         if (err) {
285                 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
286                 return;
287         }
288
289         /* Enable high speed */
290         if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
291                 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
292                 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
293                 if (err) {
294                         rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
295                                 __func__, err);
296                 } else {
297                         err = rsi_cmd52writebyte(card,
298                                                  SDIO_CCCR_SPEED,
299                                                  (cmd52_resp | SDIO_SPEED_EHS));
300                         if (err) {
301                                 rsi_dbg(ERR_ZONE,
302                                         "%s: CCR speed regwrite failed %d\n",
303                                         __func__, err);
304                                 return;
305                         }
306                         host->ios.timing = MMC_TIMING_SD_HS;
307                         host->ops->set_ios(host, &host->ios);
308                 }
309         }
310
311         /* Set clock */
312         if (mmc_card_hs(card))
313                 clock = 50000000;
314         else
315                 clock = card->cis.max_dtr;
316
317         if (clock > host->f_max)
318                 clock = host->f_max;
319
320         host->ios.clock = clock;
321         host->ops->set_ios(host, &host->ios);
322
323         if (card->host->caps & MMC_CAP_4_BIT_DATA) {
324                 /* CMD52: Set bus width & disable card detect resistor */
325                 err = rsi_cmd52writebyte(card,
326                                          SDIO_CCCR_IF,
327                                          (SDIO_BUS_CD_DISABLE |
328                                           SDIO_BUS_WIDTH_4BIT));
329                 if (err) {
330                         rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
331                                 __func__, err);
332                         return;
333                 }
334                 host->ios.bus_width = MMC_BUS_WIDTH_4;
335                 host->ops->set_ios(host, &host->ios);
336         }
337 }
338
339 /**
340  * rsi_setclock() - This function sets the clock frequency.
341  * @adapter: Pointer to the adapter structure.
342  * @freq: Clock frequency.
343  *
344  * Return: None.
345  */
346 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
347 {
348         struct rsi_91x_sdiodev *dev =
349                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
350         struct mmc_host *host = dev->pfunction->card->host;
351         u32 clock;
352
353         clock = freq * 1000;
354         if (clock > host->f_max)
355                 clock = host->f_max;
356         host->ios.clock = clock;
357         host->ops->set_ios(host, &host->ios);
358 }
359
360 /**
361  * rsi_setblocklength() - This function sets the host block length.
362  * @adapter: Pointer to the adapter structure.
363  * @length: Block length to be set.
364  *
365  * Return: status: 0 on success, -1 on failure.
366  */
367 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
368 {
369         struct rsi_91x_sdiodev *dev =
370                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
371         int status;
372         rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
373
374         status = sdio_set_block_size(dev->pfunction, length);
375         dev->pfunction->max_blksize = 256;
376         adapter->block_size = dev->pfunction->max_blksize;
377
378         rsi_dbg(INFO_ZONE,
379                 "%s: Operational blk length is %d\n", __func__, length);
380         return status;
381 }
382
383 /**
384  * rsi_setupcard() - This function queries and sets the card's features.
385  * @adapter: Pointer to the adapter structure.
386  *
387  * Return: status: 0 on success, -1 on failure.
388  */
389 static int rsi_setupcard(struct rsi_hw *adapter)
390 {
391         struct rsi_91x_sdiodev *dev =
392                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
393         int status = 0;
394
395         rsi_setclock(adapter, 50000);
396
397         dev->tx_blk_size = 256;
398         status = rsi_setblocklength(adapter, dev->tx_blk_size);
399         if (status)
400                 rsi_dbg(ERR_ZONE,
401                         "%s: Unable to set block length\n", __func__);
402         return status;
403 }
404
405 /**
406  * rsi_sdio_read_register() - This function reads one byte of information
407  *                            from a register.
408  * @adapter: Pointer to the adapter structure.
409  * @addr: Address of the register.
410  * @data: Pointer to the data that stores the data read.
411  *
412  * Return: 0 on success, -1 on failure.
413  */
414 int rsi_sdio_read_register(struct rsi_hw *adapter,
415                            u32 addr,
416                            u8 *data)
417 {
418         struct rsi_91x_sdiodev *dev =
419                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
420         u8 fun_num = 0;
421         int status;
422
423         if (likely(dev->sdio_irq_task != current))
424                 sdio_claim_host(dev->pfunction);
425
426         if (fun_num == 0)
427                 *data = sdio_f0_readb(dev->pfunction, addr, &status);
428         else
429                 *data = sdio_readb(dev->pfunction, addr, &status);
430
431         if (likely(dev->sdio_irq_task != current))
432                 sdio_release_host(dev->pfunction);
433
434         return status;
435 }
436
437 /**
438  * rsi_sdio_write_register() - This function writes one byte of information
439  *                             into a register.
440  * @adapter: Pointer to the adapter structure.
441  * @function: Function Number.
442  * @addr: Address of the register.
443  * @data: Pointer to the data tha has to be written.
444  *
445  * Return: 0 on success, -1 on failure.
446  */
447 int rsi_sdio_write_register(struct rsi_hw *adapter,
448                             u8 function,
449                             u32 addr,
450                             u8 *data)
451 {
452         struct rsi_91x_sdiodev *dev =
453                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
454         int status = 0;
455
456         if (likely(dev->sdio_irq_task != current))
457                 sdio_claim_host(dev->pfunction);
458
459         if (function == 0)
460                 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
461         else
462                 sdio_writeb(dev->pfunction, *data, addr, &status);
463
464         if (likely(dev->sdio_irq_task != current))
465                 sdio_release_host(dev->pfunction);
466
467         return status;
468 }
469
470 /**
471  * rsi_sdio_ack_intr() - This function acks the interrupt received.
472  * @adapter: Pointer to the adapter structure.
473  * @int_bit: Interrupt bit to write into register.
474  *
475  * Return: None.
476  */
477 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
478 {
479         int status;
480         status = rsi_sdio_write_register(adapter,
481                                          1,
482                                          (SDIO_FUN1_INTR_CLR_REG |
483                                           RSI_SD_REQUEST_MASTER),
484                                          &int_bit);
485         if (status)
486                 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
487 }
488
489
490
491 /**
492  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
493  *                                     information from the SD card.
494  * @adapter: Pointer to the adapter structure.
495  * @addr: Address of the register.
496  * @count: Number of multiple bytes to be read.
497  * @data: Pointer to the read data.
498  *
499  * Return: 0 on success, -1 on failure.
500  */
501 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
502                                            u32 addr,
503                                            u8 *data,
504                                            u16 count)
505 {
506         struct rsi_91x_sdiodev *dev =
507                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
508         u32 status;
509
510         if (likely(dev->sdio_irq_task != current))
511                 sdio_claim_host(dev->pfunction);
512
513         status =  sdio_readsb(dev->pfunction, data, addr, count);
514
515         if (likely(dev->sdio_irq_task != current))
516                 sdio_release_host(dev->pfunction);
517
518         if (status != 0)
519                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
520         return status;
521 }
522
523 /**
524  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
525  *                                      information to the SD card.
526  * @adapter: Pointer to the adapter structure.
527  * @addr: Address of the register.
528  * @data: Pointer to the data that has to be written.
529  * @count: Number of multiple bytes to be written.
530  *
531  * Return: 0 on success, -1 on failure.
532  */
533 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
534                                      u32 addr,
535                                      u8 *data,
536                                      u16 count)
537 {
538         struct rsi_91x_sdiodev *dev =
539                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
540         int status;
541
542         if (dev->write_fail > 1) {
543                 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
544                 return 0;
545         } else if (dev->write_fail == 1) {
546                 /**
547                  * Assuming it is a CRC failure, we want to allow another
548                  *  card write
549                  */
550                 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
551                 dev->write_fail++;
552         }
553
554         if (likely(dev->sdio_irq_task != current))
555                 sdio_claim_host(dev->pfunction);
556
557         status = sdio_writesb(dev->pfunction, addr, data, count);
558
559         if (likely(dev->sdio_irq_task != current))
560                 sdio_release_host(dev->pfunction);
561
562         if (status) {
563                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
564                         __func__, status);
565                 dev->write_fail = 2;
566         } else {
567                 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
568         }
569         return status;
570 }
571
572 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
573                                            u32 base_address,
574                                            u32 instructions_sz,
575                                            u16 block_size,
576                                            u8 *ta_firmware)
577 {
578         u32 num_blocks, offset, i;
579         u16 msb_address, lsb_address;
580         u8 *temp_buf;
581         int status;
582
583         num_blocks = instructions_sz / block_size;
584         msb_address = base_address >> 16;
585
586         rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
587                 instructions_sz, num_blocks);
588
589         temp_buf = kmalloc(block_size, GFP_KERNEL);
590         if (!temp_buf)
591                 return -ENOMEM;
592
593         /* Loading DM ms word in the sdio slave */
594         status = rsi_sdio_master_access_msword(adapter, msb_address);
595         if (status < 0) {
596                 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
597                 goto out_free;
598         }
599
600         for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
601                 memcpy(temp_buf, ta_firmware + offset, block_size);
602                 lsb_address = (u16)base_address;
603                 status = rsi_sdio_write_register_multiple
604                                         (adapter,
605                                          lsb_address | RSI_SD_REQUEST_MASTER,
606                                          temp_buf, block_size);
607                 if (status < 0) {
608                         rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
609                         goto out_free;
610                 }
611                 rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
612                 base_address += block_size;
613
614                 if ((base_address >> 16) != msb_address) {
615                         msb_address += 1;
616
617                         /* Loading DM ms word in the sdio slave */
618                         status = rsi_sdio_master_access_msword(adapter,
619                                                                msb_address);
620                         if (status < 0) {
621                                 rsi_dbg(ERR_ZONE,
622                                         "%s: Unable to set ms word reg\n",
623                                         __func__);
624                                 goto out_free;
625                         }
626                 }
627         }
628
629         if (instructions_sz % block_size) {
630                 memset(temp_buf, 0, block_size);
631                 memcpy(temp_buf, ta_firmware + offset,
632                        instructions_sz % block_size);
633                 lsb_address = (u16)base_address;
634                 status = rsi_sdio_write_register_multiple
635                                         (adapter,
636                                          lsb_address | RSI_SD_REQUEST_MASTER,
637                                          temp_buf,
638                                          instructions_sz % block_size);
639                 if (status < 0)
640                         goto out_free;
641                 rsi_dbg(INFO_ZONE,
642                         "Written Last Block in Address 0x%x Successfully\n",
643                         offset | RSI_SD_REQUEST_MASTER);
644         }
645
646         status = 0;
647 out_free:
648         kfree(temp_buf);
649         return status;
650 }
651
652 #define FLASH_SIZE_ADDR                 0x04000016
653 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
654                                     u32 *read_buf, u16 size)
655 {
656         u32 addr_on_bus, *data;
657         u16 ms_addr;
658         int status;
659
660         data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
661         if (!data)
662                 return -ENOMEM;
663
664         ms_addr = (addr >> 16);
665         status = rsi_sdio_master_access_msword(adapter, ms_addr);
666         if (status < 0) {
667                 rsi_dbg(ERR_ZONE,
668                         "%s: Unable to set ms word to common reg\n",
669                         __func__);
670                 goto err;
671         }
672         addr &= 0xFFFF;
673
674         addr_on_bus = (addr & 0xFF000000);
675         if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
676             (addr_on_bus == 0x0))
677                 addr_on_bus = (addr & ~(0x3));
678         else
679                 addr_on_bus = addr;
680
681         /* Bring TA out of reset */
682         status = rsi_sdio_read_register_multiple
683                                         (adapter,
684                                          (addr_on_bus | RSI_SD_REQUEST_MASTER),
685                                          (u8 *)data, 4);
686         if (status < 0) {
687                 rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
688                 goto err;
689         }
690         if (size == 2) {
691                 if ((addr & 0x3) == 0)
692                         *read_buf = *data;
693                 else
694                         *read_buf  = (*data >> 16);
695                 *read_buf = (*read_buf & 0xFFFF);
696         } else if (size == 1) {
697                 if ((addr & 0x3) == 0)
698                         *read_buf = *data;
699                 else if ((addr & 0x3) == 1)
700                         *read_buf = (*data >> 8);
701                 else if ((addr & 0x3) == 2)
702                         *read_buf = (*data >> 16);
703                 else
704                         *read_buf = (*data >> 24);
705                 *read_buf = (*read_buf & 0xFF);
706         } else {
707                 *read_buf = *data;
708         }
709
710 err:
711         kfree(data);
712         return status;
713 }
714
715 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
716                                      unsigned long addr,
717                                      unsigned long data, u16 size)
718 {
719         unsigned long *data_aligned;
720         int status;
721
722         data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
723         if (!data_aligned)
724                 return -ENOMEM;
725
726         if (size == 2) {
727                 *data_aligned = ((data << 16) | (data & 0xFFFF));
728         } else if (size == 1) {
729                 u32 temp_data = data & 0xFF;
730
731                 *data_aligned = ((temp_data << 24) | (temp_data << 16) |
732                                  (temp_data << 8) | temp_data);
733         } else {
734                 *data_aligned = data;
735         }
736         size = 4;
737
738         status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
739         if (status < 0) {
740                 rsi_dbg(ERR_ZONE,
741                         "%s: Unable to set ms word to common reg\n",
742                         __func__);
743                 kfree(data_aligned);
744                 return -EIO;
745         }
746         addr = addr & 0xFFFF;
747
748         /* Bring TA out of reset */
749         status = rsi_sdio_write_register_multiple
750                                         (adapter,
751                                          (addr | RSI_SD_REQUEST_MASTER),
752                                          (u8 *)data_aligned, size);
753         if (status < 0)
754                 rsi_dbg(ERR_ZONE,
755                         "%s: Unable to do AHB reg write\n", __func__);
756
757         kfree(data_aligned);
758         return status;
759 }
760
761 /**
762  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
763  * @adapter: Pointer to the adapter structure.
764  * @pkt: Pointer to the data to be written on to the device.
765  * @len: length of the data to be written on to the device.
766  *
767  * Return: 0 on success, -1 on failure.
768  */
769 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
770                                         u8 *pkt,
771                                         u32 len)
772 {
773         struct rsi_91x_sdiodev *dev =
774                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
775         u32 block_size = dev->tx_blk_size;
776         u32 num_blocks, address, length;
777         u32 queueno;
778         int status;
779
780         queueno = ((pkt[1] >> 4) & 0xf);
781         if (queueno == RSI_BT_MGMT_Q || queueno == RSI_BT_DATA_Q)
782                 queueno = RSI_BT_Q;
783
784         num_blocks = len / block_size;
785
786         if (len % block_size)
787                 num_blocks++;
788
789         address = (num_blocks * block_size | (queueno << 12));
790         length  = num_blocks * block_size;
791
792         status = rsi_sdio_write_register_multiple(adapter,
793                                                   address,
794                                                   (u8 *)pkt,
795                                                   length);
796         if (status)
797                 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
798                         __func__, status);
799         rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
800         return status;
801 }
802
803 /**
804  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
805                                    from the device.
806  * @adapter: Pointer to the adapter data structure.
807  * @pkt: Pointer to the packet data to be read from the the device.
808  * @length: Length of the data to be read from the device.
809  *
810  * Return: 0 on success, -1 on failure.
811  */
812 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
813                                 u8 *pkt,
814                                 u32 length)
815 {
816         int status = -EINVAL;
817
818         if (!length) {
819                 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
820                 return status;
821         }
822
823         status = rsi_sdio_read_register_multiple(adapter,
824                                                  length,
825                                                  (u8 *)pkt,
826                                                  length); /*num of bytes*/
827
828         if (status)
829                 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
830                         status);
831         return status;
832 }
833
834 /**
835  * rsi_init_sdio_interface() - This function does init specific to SDIO.
836  *
837  * @adapter: Pointer to the adapter data structure.
838  * @pkt: Pointer to the packet data to be read from the the device.
839  *
840  * Return: 0 on success, -1 on failure.
841  */
842
843 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
844                                    struct sdio_func *pfunction)
845 {
846         struct rsi_91x_sdiodev *rsi_91x_dev;
847         int status = -ENOMEM;
848
849         rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
850         if (!rsi_91x_dev)
851                 return status;
852
853         adapter->rsi_dev = rsi_91x_dev;
854
855         sdio_claim_host(pfunction);
856
857         pfunction->enable_timeout = 100;
858         status = sdio_enable_func(pfunction);
859         if (status) {
860                 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
861                 sdio_release_host(pfunction);
862                 return status;
863         }
864
865         rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
866
867         rsi_91x_dev->pfunction = pfunction;
868         adapter->device = &pfunction->dev;
869
870         sdio_set_drvdata(pfunction, adapter);
871
872         status = rsi_setupcard(adapter);
873         if (status) {
874                 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
875                 goto fail;
876         }
877
878         rsi_dbg(INIT_ZONE, "%s: Setup card successfully\n", __func__);
879
880         status = rsi_init_sdio_slave_regs(adapter);
881         if (status) {
882                 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
883                 goto fail;
884         }
885         sdio_release_host(pfunction);
886
887         adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
888         adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
889
890 #ifdef CONFIG_RSI_DEBUGFS
891         adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
892 #endif
893         return status;
894 fail:
895         sdio_disable_func(pfunction);
896         sdio_release_host(pfunction);
897         return status;
898 }
899
900 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
901 {
902         struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
903         struct sdio_func *pfunction = sdev->pfunction;
904         int ii;
905
906         for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
907                 skb_queue_purge(&adapter->priv->tx_queue[ii]);
908
909         /* Initialize device again */
910         sdio_claim_host(pfunction);
911
912         sdio_release_irq(pfunction);
913         rsi_reset_card(pfunction);
914
915         sdio_enable_func(pfunction);
916         rsi_setupcard(adapter);
917         rsi_init_sdio_slave_regs(adapter);
918         sdio_claim_irq(pfunction, rsi_handle_interrupt);
919         rsi_hal_device_init(adapter);
920
921         sdio_release_host(pfunction);
922
923         return 0;
924 }
925
926 static int rsi_sdio_ta_reset(struct rsi_hw *adapter)
927 {
928         int status;
929         u32 addr;
930         u8 *data;
931
932         data = kzalloc(RSI_9116_REG_SIZE, GFP_KERNEL);
933         if (!data)
934                 return -ENOMEM;
935
936         status = rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR);
937         if (status < 0) {
938                 rsi_dbg(ERR_ZONE,
939                         "Unable to set ms word to common reg\n");
940                 goto err;
941         }
942
943         rsi_dbg(INIT_ZONE, "%s: Bring TA out of reset\n", __func__);
944         put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
945         addr = TA_HOLD_THREAD_REG | RSI_SD_REQUEST_MASTER;
946         status = rsi_sdio_write_register_multiple(adapter, addr,
947                                                   (u8 *)&data,
948                                                   RSI_9116_REG_SIZE);
949         if (status < 0) {
950                 rsi_dbg(ERR_ZONE, "Unable to hold TA threads\n");
951                 goto err;
952         }
953
954         put_unaligned_le32(TA_SOFT_RST_CLR, data);
955         addr = TA_SOFT_RESET_REG | RSI_SD_REQUEST_MASTER;
956         status = rsi_sdio_write_register_multiple(adapter, addr,
957                                                   (u8 *)&data,
958                                                   RSI_9116_REG_SIZE);
959         if (status < 0) {
960                 rsi_dbg(ERR_ZONE, "Unable to get TA out of reset\n");
961                 goto err;
962         }
963
964         put_unaligned_le32(TA_PC_ZERO, data);
965         addr = TA_TH0_PC_REG | RSI_SD_REQUEST_MASTER;
966         status = rsi_sdio_write_register_multiple(adapter, addr,
967                                                   (u8 *)&data,
968                                                   RSI_9116_REG_SIZE);
969         if (status < 0) {
970                 rsi_dbg(ERR_ZONE, "Unable to Reset TA PC value\n");
971                 status = -EINVAL;
972                 goto err;
973         }
974
975         put_unaligned_le32(TA_RELEASE_THREAD_VALUE, data);
976         addr = TA_RELEASE_THREAD_REG | RSI_SD_REQUEST_MASTER;
977         status = rsi_sdio_write_register_multiple(adapter, addr,
978                                                   (u8 *)&data,
979                                                   RSI_9116_REG_SIZE);
980         if (status < 0) {
981                 rsi_dbg(ERR_ZONE, "Unable to release TA threads\n");
982                 goto err;
983         }
984
985         status = rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR);
986         if (status < 0) {
987                 rsi_dbg(ERR_ZONE, "Unable to set ms word to common reg\n");
988                 goto err;
989         }
990         rsi_dbg(INIT_ZONE, "***** TA Reset done *****\n");
991
992 err:
993         kfree(data);
994         return status;
995 }
996
997 static struct rsi_host_intf_ops sdio_host_intf_ops = {
998         .write_pkt              = rsi_sdio_host_intf_write_pkt,
999         .read_pkt               = rsi_sdio_host_intf_read_pkt,
1000         .master_access_msword   = rsi_sdio_master_access_msword,
1001         .read_reg_multiple      = rsi_sdio_read_register_multiple,
1002         .write_reg_multiple     = rsi_sdio_write_register_multiple,
1003         .master_reg_read        = rsi_sdio_master_reg_read,
1004         .master_reg_write       = rsi_sdio_master_reg_write,
1005         .load_data_master_write = rsi_sdio_load_data_master_write,
1006         .reinit_device          = rsi_sdio_reinit_device,
1007         .ta_reset               = rsi_sdio_ta_reset,
1008 };
1009
1010 /**
1011  * rsi_probe() - This function is called by kernel when the driver provided
1012  *               Vendor and device IDs are matched. All the initialization
1013  *               work is done here.
1014  * @pfunction: Pointer to the sdio_func structure.
1015  * @id: Pointer to sdio_device_id structure.
1016  *
1017  * Return: 0 on success, 1 on failure.
1018  */
1019 static int rsi_probe(struct sdio_func *pfunction,
1020                      const struct sdio_device_id *id)
1021 {
1022         struct rsi_hw *adapter;
1023         struct rsi_91x_sdiodev *sdev;
1024         int status = -EINVAL;
1025
1026         rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
1027
1028         adapter = rsi_91x_init(dev_oper_mode);
1029         if (!adapter) {
1030                 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
1031                         __func__);
1032                 return -EINVAL;
1033         }
1034         adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
1035         adapter->host_intf_ops = &sdio_host_intf_ops;
1036
1037         if (rsi_init_sdio_interface(adapter, pfunction)) {
1038                 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
1039                         __func__);
1040                 status = -EIO;
1041                 goto fail_free_adapter;
1042         }
1043
1044         if (pfunction->device == RSI_SDIO_PID_9113) {
1045                 rsi_dbg(ERR_ZONE, "%s: 9113 module detected\n", __func__);
1046                 adapter->device_model = RSI_DEV_9113;
1047         } else  if (pfunction->device == RSI_SDIO_PID_9116) {
1048                 rsi_dbg(ERR_ZONE, "%s: 9116 module detected\n", __func__);
1049                 adapter->device_model = RSI_DEV_9116;
1050         } else {
1051                 rsi_dbg(ERR_ZONE,
1052                         "%s: Unsupported RSI device id 0x%x\n", __func__,
1053                         pfunction->device);
1054                 goto fail_free_adapter;
1055         }
1056
1057         sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1058         rsi_init_event(&sdev->rx_thread.event);
1059         status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
1060                                     rsi_sdio_rx_thread, "SDIO-RX-Thread");
1061         if (status) {
1062                 rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
1063                 goto fail_kill_thread;
1064         }
1065         skb_queue_head_init(&sdev->rx_q.head);
1066         sdev->rx_q.num_rx_pkts = 0;
1067
1068         sdio_claim_host(pfunction);
1069         if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
1070                 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
1071                 sdio_release_host(pfunction);
1072                 status = -EIO;
1073                 goto fail_claim_irq;
1074         }
1075         sdio_release_host(pfunction);
1076         rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
1077
1078         if (rsi_hal_device_init(adapter)) {
1079                 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
1080                 status = -EINVAL;
1081                 goto fail_dev_init;
1082         }
1083         rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
1084
1085         if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
1086                 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
1087                 status = -EIO;
1088                 goto fail_dev_init;
1089         }
1090
1091         adapter->priv->hibernate_resume = false;
1092         adapter->priv->reinit_hw = false;
1093         return 0;
1094
1095 fail_dev_init:
1096         sdio_claim_host(pfunction);
1097         sdio_release_irq(pfunction);
1098         sdio_release_host(pfunction);
1099 fail_claim_irq:
1100         rsi_kill_thread(&sdev->rx_thread);
1101 fail_kill_thread:
1102         sdio_claim_host(pfunction);
1103         sdio_disable_func(pfunction);
1104         sdio_release_host(pfunction);
1105 fail_free_adapter:
1106         rsi_91x_deinit(adapter);
1107         rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
1108         return status;
1109 }
1110
1111 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
1112                            u16 len_in_bits)
1113 {
1114         rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
1115                                   ((addr << 6) | ((data >> 16) & 0xffff)), 2);
1116         rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
1117                                   (data & 0xffff), 2);
1118         rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
1119                                   RSI_GSPI_CTRL_REG0_VALUE, 2);
1120         rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
1121                                   ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
1122         msleep(20);
1123 }
1124
1125 /*This function resets and re-initializes the chip.*/
1126 static void rsi_reset_chip(struct rsi_hw *adapter)
1127 {
1128         u8 *data;
1129         u8 sdio_interrupt_status = 0;
1130         u8 request = 1;
1131         int ret;
1132
1133         data = kzalloc(sizeof(u32), GFP_KERNEL);
1134         if (!data)
1135                 return;
1136
1137         rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
1138         ret =  rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
1139         if (ret < 0) {
1140                 rsi_dbg(ERR_ZONE,
1141                         "%s: Failed to write SDIO wakeup register\n", __func__);
1142                 goto err;
1143         }
1144         msleep(20);
1145         ret =  rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1146                                       &sdio_interrupt_status);
1147         if (ret < 0) {
1148                 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1149                         __func__);
1150                 goto err;
1151         }
1152         rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1153                 __func__, sdio_interrupt_status);
1154
1155         /* Put Thread-Arch processor on hold */
1156         if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1157                 rsi_dbg(ERR_ZONE,
1158                         "%s: Unable to set ms word to common reg\n",
1159                         __func__);
1160                 goto err;
1161         }
1162
1163         put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1164         if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1165                                              RSI_SD_REQUEST_MASTER,
1166                                              data, 4)) {
1167                 rsi_dbg(ERR_ZONE,
1168                         "%s: Unable to hold Thread-Arch processor threads\n",
1169                         __func__);
1170                 goto err;
1171         }
1172
1173         /* This msleep will ensure Thread-Arch processor to go to hold
1174          * and any pending dma transfers to rf spi in device to finish.
1175          */
1176         msleep(100);
1177         if (adapter->device_model != RSI_DEV_9116) {
1178                 ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1179                 ulp_read_write(adapter,
1180                                RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1181                 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0,
1182                                32);
1183                 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
1184                                RSI_ULP_WRITE_50, 32);
1185                 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
1186                                RSI_ULP_WRITE_0, 32);
1187                 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1188                                RSI_ULP_TIMER_ENABLE, 32);
1189         } else {
1190                 if ((rsi_sdio_master_reg_write(adapter,
1191                                                NWP_WWD_INTERRUPT_TIMER,
1192                                                NWP_WWD_INT_TIMER_CLKS,
1193                                                RSI_9116_REG_SIZE)) < 0) {
1194                         rsi_dbg(ERR_ZONE, "Failed to write to intr timer\n");
1195                 }
1196                 if ((rsi_sdio_master_reg_write(adapter,
1197                                                NWP_WWD_SYSTEM_RESET_TIMER,
1198                                                NWP_WWD_SYS_RESET_TIMER_CLKS,
1199                                                RSI_9116_REG_SIZE)) < 0) {
1200                         rsi_dbg(ERR_ZONE,
1201                                 "Failed to write to system reset timer\n");
1202                 }
1203                 if ((rsi_sdio_master_reg_write(adapter,
1204                                                NWP_WWD_MODE_AND_RSTART,
1205                                                NWP_WWD_TIMER_DISABLE,
1206                                                RSI_9116_REG_SIZE)) < 0) {
1207                         rsi_dbg(ERR_ZONE,
1208                                 "Failed to write to mode and restart\n");
1209                 }
1210                 rsi_dbg(ERR_ZONE, "***** Watch Dog Reset Successful *****\n");
1211         }
1212         /* This msleep will be sufficient for the ulp
1213          * read write operations to complete for chip reset.
1214          */
1215         msleep(500);
1216 err:
1217         kfree(data);
1218         return;
1219 }
1220
1221 /**
1222  * rsi_disconnect() - This function performs the reverse of the probe function.
1223  * @pfunction: Pointer to the sdio_func structure.
1224  *
1225  * Return: void.
1226  */
1227 static void rsi_disconnect(struct sdio_func *pfunction)
1228 {
1229         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1230         struct rsi_91x_sdiodev *dev;
1231
1232         if (!adapter)
1233                 return;
1234
1235         dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1236
1237         rsi_kill_thread(&dev->rx_thread);
1238         sdio_claim_host(pfunction);
1239         sdio_release_irq(pfunction);
1240         sdio_release_host(pfunction);
1241         mdelay(10);
1242
1243         rsi_mac80211_detach(adapter);
1244         mdelay(10);
1245
1246         if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1247             adapter->priv->bt_adapter) {
1248                 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1249                 adapter->priv->bt_adapter = NULL;
1250         }
1251
1252         /* Reset Chip */
1253         rsi_reset_chip(adapter);
1254
1255         /* Resetting to take care of the case, where-in driver is re-loaded */
1256         sdio_claim_host(pfunction);
1257         rsi_reset_card(pfunction);
1258         sdio_disable_func(pfunction);
1259         sdio_release_host(pfunction);
1260         dev->write_fail = 2;
1261         rsi_91x_deinit(adapter);
1262         rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1263
1264 }
1265
1266 #ifdef CONFIG_PM
1267 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1268 {
1269         struct rsi_91x_sdiodev *dev =
1270                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1271         struct sdio_func *func = dev->pfunction;
1272         int ret;
1273
1274         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1275         if (ret)
1276                 rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1277
1278         return ret;
1279 }
1280
1281 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1282 {
1283         struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1284         u8 isr_status = 0, data = 0;
1285         int ret;
1286         unsigned long t1;
1287
1288         rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1289         t1 = jiffies;
1290         do {
1291                 rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1292                                        &isr_status);
1293                 rsi_dbg(INFO_ZONE, ".");
1294         } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1295         rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1296
1297         sdio_claim_host(pfunc);
1298         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1299         if (ret < 0) {
1300                 rsi_dbg(ERR_ZONE,
1301                         "%s: Failed to read int enable register\n",
1302                         __func__);
1303                 goto done;
1304         }
1305
1306         data &= RSI_INT_ENABLE_MASK;
1307         ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1308         if (ret < 0) {
1309                 rsi_dbg(ERR_ZONE,
1310                         "%s: Failed to write to int enable register\n",
1311                         __func__);
1312                 goto done;
1313         }
1314         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1315         if (ret < 0) {
1316                 rsi_dbg(ERR_ZONE,
1317                         "%s: Failed to read int enable register\n",
1318                         __func__);
1319                 goto done;
1320         }
1321         rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1322
1323 done:
1324         sdio_release_host(pfunc);
1325         return ret;
1326 }
1327
1328 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1329 {
1330         u8 data;
1331         int ret;
1332         struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1333         struct rsi_common *common = adapter->priv;
1334
1335         sdio_claim_host(pfunc);
1336         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1337         if (ret < 0) {
1338                 rsi_dbg(ERR_ZONE,
1339                         "%s: Failed to read int enable register\n", __func__);
1340                 goto done;
1341         }
1342
1343         data |= ~RSI_INT_ENABLE_MASK & 0xff;
1344
1345         ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1346         if (ret < 0) {
1347                 rsi_dbg(ERR_ZONE,
1348                         "%s: Failed to write to int enable register\n",
1349                         __func__);
1350                 goto done;
1351         }
1352
1353         if ((common->wow_flags & RSI_WOW_ENABLED) &&
1354             (common->wow_flags & RSI_WOW_NO_CONNECTION))
1355                 rsi_dbg(ERR_ZONE,
1356                         "##### Device can not wake up through WLAN\n");
1357
1358         ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1359         if (ret < 0) {
1360                 rsi_dbg(ERR_ZONE,
1361                         "%s: Failed to read int enable register\n", __func__);
1362                 goto done;
1363         }
1364         rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1365
1366 done:
1367         sdio_release_host(pfunc);
1368         return ret;
1369 }
1370
1371 static int rsi_suspend(struct device *dev)
1372 {
1373         int ret;
1374         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1375         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1376         struct rsi_common *common;
1377
1378         if (!adapter) {
1379                 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1380                 return -ENODEV;
1381         }
1382         common = adapter->priv;
1383         rsi_sdio_disable_interrupts(pfunction);
1384
1385         ret = rsi_set_sdio_pm_caps(adapter);
1386         if (ret)
1387                 rsi_dbg(INFO_ZONE,
1388                         "Setting power management caps failed\n");
1389         common->fsm_state = FSM_CARD_NOT_READY;
1390
1391         return 0;
1392 }
1393
1394 static int rsi_resume(struct device *dev)
1395 {
1396         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1397         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1398         struct rsi_common *common = adapter->priv;
1399
1400         common->fsm_state = FSM_MAC_INIT_DONE;
1401         rsi_sdio_enable_interrupts(pfunction);
1402
1403         return 0;
1404 }
1405
1406 static int rsi_freeze(struct device *dev)
1407 {
1408         int ret;
1409         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1410         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1411         struct rsi_common *common;
1412         struct rsi_91x_sdiodev *sdev;
1413
1414         rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1415
1416         if (!adapter) {
1417                 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1418                 return -ENODEV;
1419         }
1420         common = adapter->priv;
1421         sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1422
1423         if ((common->wow_flags & RSI_WOW_ENABLED) &&
1424             (common->wow_flags & RSI_WOW_NO_CONNECTION))
1425                 rsi_dbg(ERR_ZONE,
1426                         "##### Device can not wake up through WLAN\n");
1427
1428         if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
1429             common->bt_adapter) {
1430                 rsi_bt_ops.detach(common->bt_adapter);
1431                 common->bt_adapter = NULL;
1432         }
1433
1434         ret = rsi_sdio_disable_interrupts(pfunction);
1435
1436         if (sdev->write_fail)
1437                 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1438
1439         ret = rsi_set_sdio_pm_caps(adapter);
1440         if (ret)
1441                 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1442
1443         rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1444
1445         return 0;
1446 }
1447
1448 static int rsi_thaw(struct device *dev)
1449 {
1450         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1451         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1452         struct rsi_common *common = adapter->priv;
1453
1454         rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1455
1456         common->hibernate_resume = true;
1457         common->fsm_state = FSM_CARD_NOT_READY;
1458         common->iface_down = true;
1459
1460         rsi_sdio_enable_interrupts(pfunction);
1461
1462         rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1463
1464         return 0;
1465 }
1466
1467 static void rsi_shutdown(struct device *dev)
1468 {
1469         struct sdio_func *pfunction = dev_to_sdio_func(dev);
1470         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1471         struct rsi_91x_sdiodev *sdev =
1472                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1473         struct ieee80211_hw *hw = adapter->hw;
1474         struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1475
1476         rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1477
1478         if (rsi_config_wowlan(adapter, wowlan))
1479                 rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
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 /*(DEBLOBBED)*/
1582 MODULE_VERSION("0.1");
1583 MODULE_LICENSE("Dual BSD/GPL");