GNU Linux-libre 4.14.251-gnu1
[releases.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_hal.h"
22
23 /**
24  * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
25  * @rw: Read/write
26  * @func: function number
27  * @raw: indicates whether to perform read after write
28  * @address: address to which to read/write
29  * @writedata: data to write
30  *
31  * Return: argument
32  */
33 static u32 rsi_sdio_set_cmd52_arg(bool rw,
34                                   u8 func,
35                                   u8 raw,
36                                   u32 address,
37                                   u8 writedata)
38 {
39         return ((rw & 1) << 31) | ((func & 0x7) << 28) |
40                 ((raw & 1) << 27) | (1 << 26) |
41                 ((address & 0x1FFFF) << 9) | (1 << 8) |
42                 (writedata & 0xFF);
43 }
44
45 /**
46  * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
47  * @card: Pointer to the mmc_card.
48  * @address: Address to write.
49  * @byte: Data to write.
50  *
51  * Return: Write status.
52  */
53 static int rsi_cmd52writebyte(struct mmc_card *card,
54                               u32 address,
55                               u8 byte)
56 {
57         struct mmc_command io_cmd;
58         u32 arg;
59
60         memset(&io_cmd, 0, sizeof(io_cmd));
61         arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
62         io_cmd.opcode = SD_IO_RW_DIRECT;
63         io_cmd.arg = arg;
64         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
65
66         return mmc_wait_for_cmd(card->host, &io_cmd, 0);
67 }
68
69 /**
70  * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
71  * @card: Pointer to the mmc_card.
72  * @address: Address to read from.
73  * @byte: Variable to store read value.
74  *
75  * Return: Read status.
76  */
77 static int rsi_cmd52readbyte(struct mmc_card *card,
78                              u32 address,
79                              u8 *byte)
80 {
81         struct mmc_command io_cmd;
82         u32 arg;
83         int err;
84
85         memset(&io_cmd, 0, sizeof(io_cmd));
86         arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
87         io_cmd.opcode = SD_IO_RW_DIRECT;
88         io_cmd.arg = arg;
89         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
90
91         err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
92         if ((!err) && (byte))
93                 *byte =  io_cmd.resp[0] & 0xFF;
94         return err;
95 }
96
97 /**
98  * rsi_issue_sdiocommand() - This function issues sdio commands.
99  * @func: Pointer to the sdio_func structure.
100  * @opcode: Opcode value.
101  * @arg: Arguments to pass.
102  * @flags: Flags which are set.
103  * @resp: Pointer to store response.
104  *
105  * Return: err: command status as 0 or -1.
106  */
107 static int rsi_issue_sdiocommand(struct sdio_func *func,
108                                  u32 opcode,
109                                  u32 arg,
110                                  u32 flags,
111                                  u32 *resp)
112 {
113         struct mmc_command cmd;
114         struct mmc_host *host;
115         int err;
116
117         host = func->card->host;
118
119         memset(&cmd, 0, sizeof(struct mmc_command));
120         cmd.opcode = opcode;
121         cmd.arg = arg;
122         cmd.flags = flags;
123         err = mmc_wait_for_cmd(host, &cmd, 3);
124
125         if ((!err) && (resp))
126                 *resp = cmd.resp[0];
127
128         return err;
129 }
130
131 /**
132  * rsi_handle_interrupt() - This function is called upon the occurence
133  *                          of an interrupt.
134  * @function: Pointer to the sdio_func structure.
135  *
136  * Return: None.
137  */
138 static void rsi_handle_interrupt(struct sdio_func *function)
139 {
140         struct rsi_hw *adapter = sdio_get_drvdata(function);
141         struct rsi_91x_sdiodev *dev =
142                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
143
144         if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
145                 return;
146
147         dev->sdio_irq_task = current;
148         rsi_interrupt_handler(adapter);
149         dev->sdio_irq_task = NULL;
150 }
151
152 /**
153  * rsi_reset_card() - This function resets and re-initializes the card.
154  * @pfunction: Pointer to the sdio_func structure.
155  *
156  * Return: None.
157  */
158 static void rsi_reset_card(struct sdio_func *pfunction)
159 {
160         int ret = 0;
161         int err;
162         struct mmc_card *card = pfunction->card;
163         struct mmc_host *host = card->host;
164         u8 cmd52_resp;
165         u32 clock, resp, i;
166         u16 rca;
167
168         /* Reset 9110 chip */
169         ret = rsi_cmd52writebyte(pfunction->card,
170                                  SDIO_CCCR_ABORT,
171                                  (1 << 3));
172
173         /* Card will not send any response as it is getting reset immediately
174          * Hence expect a timeout status from host controller
175          */
176         if (ret != -ETIMEDOUT)
177                 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
178
179         /* Wait for few milli seconds to get rid of residue charges if any */
180         msleep(20);
181
182         /* Initialize the SDIO card */
183         host->ios.chip_select = MMC_CS_DONTCARE;
184         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
185         host->ios.power_mode = MMC_POWER_UP;
186         host->ios.bus_width = MMC_BUS_WIDTH_1;
187         host->ios.timing = MMC_TIMING_LEGACY;
188         host->ops->set_ios(host, &host->ios);
189
190         /*
191          * This delay should be sufficient to allow the power supply
192          * to reach the minimum voltage.
193          */
194         msleep(20);
195
196         host->ios.clock = host->f_min;
197         host->ios.power_mode = MMC_POWER_ON;
198         host->ops->set_ios(host, &host->ios);
199
200         /*
201          * This delay must be at least 74 clock sizes, or 1 ms, or the
202          * time required to reach a stable voltage.
203          */
204         msleep(20);
205
206         /* Issue CMD0. Goto idle state */
207         host->ios.chip_select = MMC_CS_HIGH;
208         host->ops->set_ios(host, &host->ios);
209         msleep(20);
210         err = rsi_issue_sdiocommand(pfunction,
211                                     MMC_GO_IDLE_STATE,
212                                     0,
213                                     (MMC_RSP_NONE | MMC_CMD_BC),
214                                     NULL);
215         host->ios.chip_select = MMC_CS_DONTCARE;
216         host->ops->set_ios(host, &host->ios);
217         msleep(20);
218         host->use_spi_crc = 0;
219
220         if (err)
221                 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
222
223         /* Issue CMD5, arg = 0 */
224         err = rsi_issue_sdiocommand(pfunction,  SD_IO_SEND_OP_COND, 0,
225                                     (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
226         if (err)
227                 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n", __func__, err);
228         card->ocr = resp;
229
230         /* Issue CMD5, arg = ocr. Wait till card is ready  */
231         for (i = 0; i < 100; i++) {
232                 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
233                                             card->ocr,
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                         break;
239                 }
240
241                 if (resp & MMC_CARD_BUSY)
242                         break;
243                 msleep(20);
244         }
245
246         if ((i == 100) || (err)) {
247                 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
248                         __func__, i, err);
249                 return;
250         }
251
252         /* Issue CMD3, get RCA */
253         err = rsi_issue_sdiocommand(pfunction,
254                                     SD_SEND_RELATIVE_ADDR,
255                                     0,
256                                     (MMC_RSP_R6 | MMC_CMD_BCR),
257                                     &resp);
258         if (err) {
259                 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
260                 return;
261         }
262         rca = resp >> 16;
263         host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
264         host->ops->set_ios(host, &host->ios);
265
266         /* Issue CMD7, select card  */
267         err = rsi_issue_sdiocommand(pfunction,
268                                     MMC_SELECT_CARD,
269                                     (rca << 16),
270                                     (MMC_RSP_R1 | MMC_CMD_AC),
271                                     NULL);
272         if (err) {
273                 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
274                 return;
275         }
276
277         /* Enable high speed */
278         if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
279                 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
280                 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
281                 if (err) {
282                         rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
283                                 __func__, err);
284                 } else {
285                         err = rsi_cmd52writebyte(card,
286                                                  SDIO_CCCR_SPEED,
287                                                  (cmd52_resp | SDIO_SPEED_EHS));
288                         if (err) {
289                                 rsi_dbg(ERR_ZONE,
290                                         "%s: CCR speed regwrite failed %d\n",
291                                         __func__, err);
292                                 return;
293                         }
294                         host->ios.timing = MMC_TIMING_SD_HS;
295                         host->ops->set_ios(host, &host->ios);
296                 }
297         }
298
299         /* Set clock */
300         if (mmc_card_hs(card))
301                 clock = 50000000;
302         else
303                 clock = card->cis.max_dtr;
304
305         if (clock > host->f_max)
306                 clock = host->f_max;
307
308         host->ios.clock = clock;
309         host->ops->set_ios(host, &host->ios);
310
311         if (card->host->caps & MMC_CAP_4_BIT_DATA) {
312                 /* CMD52: Set bus width & disable card detect resistor */
313                 err = rsi_cmd52writebyte(card,
314                                          SDIO_CCCR_IF,
315                                          (SDIO_BUS_CD_DISABLE |
316                                           SDIO_BUS_WIDTH_4BIT));
317                 if (err) {
318                         rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
319                                 __func__, err);
320                         return;
321                 }
322                 host->ios.bus_width = MMC_BUS_WIDTH_4;
323                 host->ops->set_ios(host, &host->ios);
324         }
325 }
326
327 /**
328  * rsi_setclock() - This function sets the clock frequency.
329  * @adapter: Pointer to the adapter structure.
330  * @freq: Clock frequency.
331  *
332  * Return: None.
333  */
334 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
335 {
336         struct rsi_91x_sdiodev *dev =
337                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
338         struct mmc_host *host = dev->pfunction->card->host;
339         u32 clock;
340
341         clock = freq * 1000;
342         if (clock > host->f_max)
343                 clock = host->f_max;
344         host->ios.clock = clock;
345         host->ops->set_ios(host, &host->ios);
346 }
347
348 /**
349  * rsi_setblocklength() - This function sets the host block length.
350  * @adapter: Pointer to the adapter structure.
351  * @length: Block length to be set.
352  *
353  * Return: status: 0 on success, -1 on failure.
354  */
355 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
356 {
357         struct rsi_91x_sdiodev *dev =
358                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
359         int status;
360         rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
361
362         status = sdio_set_block_size(dev->pfunction, length);
363         dev->pfunction->max_blksize = 256;
364         adapter->block_size = dev->pfunction->max_blksize;
365
366         rsi_dbg(INFO_ZONE,
367                 "%s: Operational blk length is %d\n", __func__, length);
368         return status;
369 }
370
371 /**
372  * rsi_setupcard() - This function queries and sets the card's features.
373  * @adapter: Pointer to the adapter structure.
374  *
375  * Return: status: 0 on success, -1 on failure.
376  */
377 static int rsi_setupcard(struct rsi_hw *adapter)
378 {
379         struct rsi_91x_sdiodev *dev =
380                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
381         int status = 0;
382
383         rsi_setclock(adapter, 50000);
384
385         dev->tx_blk_size = 256;
386         status = rsi_setblocklength(adapter, dev->tx_blk_size);
387         if (status)
388                 rsi_dbg(ERR_ZONE,
389                         "%s: Unable to set block length\n", __func__);
390         return status;
391 }
392
393 /**
394  * rsi_sdio_read_register() - This function reads one byte of information
395  *                            from a register.
396  * @adapter: Pointer to the adapter structure.
397  * @addr: Address of the register.
398  * @data: Pointer to the data that stores the data read.
399  *
400  * Return: 0 on success, -1 on failure.
401  */
402 int rsi_sdio_read_register(struct rsi_hw *adapter,
403                            u32 addr,
404                            u8 *data)
405 {
406         struct rsi_91x_sdiodev *dev =
407                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
408         u8 fun_num = 0;
409         int status;
410
411         if (likely(dev->sdio_irq_task != current))
412                 sdio_claim_host(dev->pfunction);
413
414         if (fun_num == 0)
415                 *data = sdio_f0_readb(dev->pfunction, addr, &status);
416         else
417                 *data = sdio_readb(dev->pfunction, addr, &status);
418
419         if (likely(dev->sdio_irq_task != current))
420                 sdio_release_host(dev->pfunction);
421
422         return status;
423 }
424
425 /**
426  * rsi_sdio_write_register() - This function writes one byte of information
427  *                             into a register.
428  * @adapter: Pointer to the adapter structure.
429  * @function: Function Number.
430  * @addr: Address of the register.
431  * @data: Pointer to the data tha has to be written.
432  *
433  * Return: 0 on success, -1 on failure.
434  */
435 int rsi_sdio_write_register(struct rsi_hw *adapter,
436                             u8 function,
437                             u32 addr,
438                             u8 *data)
439 {
440         struct rsi_91x_sdiodev *dev =
441                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
442         int status = 0;
443
444         if (likely(dev->sdio_irq_task != current))
445                 sdio_claim_host(dev->pfunction);
446
447         if (function == 0)
448                 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
449         else
450                 sdio_writeb(dev->pfunction, *data, addr, &status);
451
452         if (likely(dev->sdio_irq_task != current))
453                 sdio_release_host(dev->pfunction);
454
455         return status;
456 }
457
458 /**
459  * rsi_sdio_ack_intr() - This function acks the interrupt received.
460  * @adapter: Pointer to the adapter structure.
461  * @int_bit: Interrupt bit to write into register.
462  *
463  * Return: None.
464  */
465 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
466 {
467         int status;
468         status = rsi_sdio_write_register(adapter,
469                                          1,
470                                          (SDIO_FUN1_INTR_CLR_REG |
471                                           RSI_SD_REQUEST_MASTER),
472                                          &int_bit);
473         if (status)
474                 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
475 }
476
477
478
479 /**
480  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
481  *                                     information from the SD card.
482  * @adapter: Pointer to the adapter structure.
483  * @addr: Address of the register.
484  * @count: Number of multiple bytes to be read.
485  * @data: Pointer to the read data.
486  *
487  * Return: 0 on success, -1 on failure.
488  */
489 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
490                                            u32 addr,
491                                            u8 *data,
492                                            u16 count)
493 {
494         struct rsi_91x_sdiodev *dev =
495                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
496         u32 status;
497
498         if (likely(dev->sdio_irq_task != current))
499                 sdio_claim_host(dev->pfunction);
500
501         status =  sdio_readsb(dev->pfunction, data, addr, count);
502
503         if (likely(dev->sdio_irq_task != current))
504                 sdio_release_host(dev->pfunction);
505
506         if (status != 0)
507                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
508         return status;
509 }
510
511 /**
512  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
513  *                                      information to the SD card.
514  * @adapter: Pointer to the adapter structure.
515  * @addr: Address of the register.
516  * @data: Pointer to the data that has to be written.
517  * @count: Number of multiple bytes to be written.
518  *
519  * Return: 0 on success, -1 on failure.
520  */
521 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
522                                      u32 addr,
523                                      u8 *data,
524                                      u16 count)
525 {
526         struct rsi_91x_sdiodev *dev =
527                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
528         int status;
529
530         if (dev->write_fail > 1) {
531                 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
532                 return 0;
533         } else if (dev->write_fail == 1) {
534                 /**
535                  * Assuming it is a CRC failure, we want to allow another
536                  *  card write
537                  */
538                 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
539                 dev->write_fail++;
540         }
541
542         if (likely(dev->sdio_irq_task != current))
543                 sdio_claim_host(dev->pfunction);
544
545         status = sdio_writesb(dev->pfunction, addr, data, count);
546
547         if (likely(dev->sdio_irq_task != current))
548                 sdio_release_host(dev->pfunction);
549
550         if (status) {
551                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
552                         __func__, status);
553                 dev->write_fail = 2;
554         } else {
555                 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
556         }
557         return status;
558 }
559
560 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
561                                            u32 base_address,
562                                            u32 instructions_sz,
563                                            u16 block_size,
564                                            u8 *ta_firmware)
565 {
566         u32 num_blocks, offset, i;
567         u16 msb_address, lsb_address;
568         u8 temp_buf[block_size];
569         int status;
570
571         num_blocks = instructions_sz / block_size;
572         msb_address = base_address >> 16;
573
574         rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
575                 instructions_sz, num_blocks);
576
577         /* Loading DM ms word in the sdio slave */
578         status = rsi_sdio_master_access_msword(adapter, msb_address);
579         if (status < 0) {
580                 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
581                 return status;
582         }
583
584         for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
585                 memcpy(temp_buf, ta_firmware + offset, block_size);
586                 lsb_address = (u16)base_address;
587                 status = rsi_sdio_write_register_multiple
588                                         (adapter,
589                                          lsb_address | RSI_SD_REQUEST_MASTER,
590                                          temp_buf, block_size);
591                 if (status < 0) {
592                         rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
593                         return status;
594                 }
595                 rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
596                 base_address += block_size;
597
598                 if ((base_address >> 16) != msb_address) {
599                         msb_address += 1;
600
601                         /* Loading DM ms word in the sdio slave */
602                         status = rsi_sdio_master_access_msword(adapter,
603                                                                msb_address);
604                         if (status < 0) {
605                                 rsi_dbg(ERR_ZONE,
606                                         "%s: Unable to set ms word reg\n",
607                                         __func__);
608                                 return status;
609                         }
610                 }
611         }
612
613         if (instructions_sz % block_size) {
614                 memset(temp_buf, 0, block_size);
615                 memcpy(temp_buf, ta_firmware + offset,
616                        instructions_sz % block_size);
617                 lsb_address = (u16)base_address;
618                 status = rsi_sdio_write_register_multiple
619                                         (adapter,
620                                          lsb_address | RSI_SD_REQUEST_MASTER,
621                                          temp_buf,
622                                          instructions_sz % block_size);
623                 if (status < 0)
624                         return status;
625                 rsi_dbg(INFO_ZONE,
626                         "Written Last Block in Address 0x%x Successfully\n",
627                         offset | RSI_SD_REQUEST_MASTER);
628         }
629         return 0;
630 }
631
632 #define FLASH_SIZE_ADDR                 0x04000016
633 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
634                                     u32 *read_buf, u16 size)
635 {
636         u32 addr_on_bus, *data;
637         u16 ms_addr;
638         int status;
639
640         data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
641         if (!data)
642                 return -ENOMEM;
643
644         data = PTR_ALIGN(data, 8);
645
646         ms_addr = (addr >> 16);
647         status = rsi_sdio_master_access_msword(adapter, ms_addr);
648         if (status < 0) {
649                 rsi_dbg(ERR_ZONE,
650                         "%s: Unable to set ms word to common reg\n",
651                         __func__);
652                 goto err;
653         }
654         addr &= 0xFFFF;
655
656         addr_on_bus = (addr & 0xFF000000);
657         if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
658             (addr_on_bus == 0x0))
659                 addr_on_bus = (addr & ~(0x3));
660         else
661                 addr_on_bus = addr;
662
663         /* Bring TA out of reset */
664         status = rsi_sdio_read_register_multiple
665                                         (adapter,
666                                          (addr_on_bus | RSI_SD_REQUEST_MASTER),
667                                          (u8 *)data, 4);
668         if (status < 0) {
669                 rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
670                 goto err;
671         }
672         if (size == 2) {
673                 if ((addr & 0x3) == 0)
674                         *read_buf = *data;
675                 else
676                         *read_buf  = (*data >> 16);
677                 *read_buf = (*read_buf & 0xFFFF);
678         } else if (size == 1) {
679                 if ((addr & 0x3) == 0)
680                         *read_buf = *data;
681                 else if ((addr & 0x3) == 1)
682                         *read_buf = (*data >> 8);
683                 else if ((addr & 0x3) == 2)
684                         *read_buf = (*data >> 16);
685                 else
686                         *read_buf = (*data >> 24);
687                 *read_buf = (*read_buf & 0xFF);
688         } else {
689                 *read_buf = *data;
690         }
691
692 err:
693         kfree(data);
694         return status;
695 }
696
697 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
698                                      unsigned long addr,
699                                      unsigned long data, u16 size)
700 {
701         unsigned long *data_aligned;
702         int status;
703
704         data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
705         if (!data_aligned)
706                 return -ENOMEM;
707
708         data_aligned = PTR_ALIGN(data_aligned, 8);
709
710         if (size == 2) {
711                 *data_aligned = ((data << 16) | (data & 0xFFFF));
712         } else if (size == 1) {
713                 u32 temp_data = data & 0xFF;
714
715                 *data_aligned = ((temp_data << 24) | (temp_data << 16) |
716                                  (temp_data << 8) | temp_data);
717         } else {
718                 *data_aligned = data;
719         }
720         size = 4;
721
722         status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
723         if (status < 0) {
724                 rsi_dbg(ERR_ZONE,
725                         "%s: Unable to set ms word to common reg\n",
726                         __func__);
727                 kfree(data_aligned);
728                 return -EIO;
729         }
730         addr = addr & 0xFFFF;
731
732         /* Bring TA out of reset */
733         status = rsi_sdio_write_register_multiple
734                                         (adapter,
735                                          (addr | RSI_SD_REQUEST_MASTER),
736                                          (u8 *)data_aligned, size);
737         if (status < 0)
738                 rsi_dbg(ERR_ZONE,
739                         "%s: Unable to do AHB reg write\n", __func__);
740
741         kfree(data_aligned);
742         return status;
743 }
744
745 /**
746  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
747  * @adapter: Pointer to the adapter structure.
748  * @pkt: Pointer to the data to be written on to the device.
749  * @len: length of the data to be written on to the device.
750  *
751  * Return: 0 on success, -1 on failure.
752  */
753 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
754                                         u8 *pkt,
755                                         u32 len)
756 {
757         struct rsi_91x_sdiodev *dev =
758                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
759         u32 block_size = dev->tx_blk_size;
760         u32 num_blocks, address, length;
761         u32 queueno;
762         int status;
763
764         queueno = ((pkt[1] >> 4) & 0xf);
765
766         num_blocks = len / block_size;
767
768         if (len % block_size)
769                 num_blocks++;
770
771         address = (num_blocks * block_size | (queueno << 12));
772         length  = num_blocks * block_size;
773
774         status = rsi_sdio_write_register_multiple(adapter,
775                                                   address,
776                                                   (u8 *)pkt,
777                                                   length);
778         if (status)
779                 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
780                         __func__, status);
781         rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
782         return status;
783 }
784
785 /**
786  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
787                                    from the device.
788  * @adapter: Pointer to the adapter data structure.
789  * @pkt: Pointer to the packet data to be read from the the device.
790  * @length: Length of the data to be read from the device.
791  *
792  * Return: 0 on success, -1 on failure.
793  */
794 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
795                                 u8 *pkt,
796                                 u32 length)
797 {
798         int status = -EINVAL;
799
800         if (!length) {
801                 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
802                 return status;
803         }
804
805         status = rsi_sdio_read_register_multiple(adapter,
806                                                  length,
807                                                  (u8 *)pkt,
808                                                  length); /*num of bytes*/
809
810         if (status)
811                 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
812                         status);
813         return status;
814 }
815
816 /**
817  * rsi_init_sdio_interface() - This function does init specific to SDIO.
818  *
819  * @adapter: Pointer to the adapter data structure.
820  * @pkt: Pointer to the packet data to be read from the the device.
821  *
822  * Return: 0 on success, -1 on failure.
823  */
824
825 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
826                                    struct sdio_func *pfunction)
827 {
828         struct rsi_91x_sdiodev *rsi_91x_dev;
829         int status = -ENOMEM;
830
831         rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
832         if (!rsi_91x_dev)
833                 return status;
834
835         adapter->rsi_dev = rsi_91x_dev;
836
837         sdio_claim_host(pfunction);
838
839         pfunction->enable_timeout = 100;
840         status = sdio_enable_func(pfunction);
841         if (status) {
842                 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
843                 sdio_release_host(pfunction);
844                 return status;
845         }
846
847         rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
848
849         rsi_91x_dev->pfunction = pfunction;
850         adapter->device = &pfunction->dev;
851
852         sdio_set_drvdata(pfunction, adapter);
853
854         status = rsi_setupcard(adapter);
855         if (status) {
856                 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
857                 goto fail;
858         }
859
860         rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
861
862         status = rsi_init_sdio_slave_regs(adapter);
863         if (status) {
864                 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
865                 goto fail;
866         }
867         sdio_release_host(pfunction);
868
869         adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
870         adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
871
872 #ifdef CONFIG_RSI_DEBUGFS
873         adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
874 #endif
875         return status;
876 fail:
877         sdio_disable_func(pfunction);
878         sdio_release_host(pfunction);
879         return status;
880 }
881
882 static struct rsi_host_intf_ops sdio_host_intf_ops = {
883         .write_pkt              = rsi_sdio_host_intf_write_pkt,
884         .read_pkt               = rsi_sdio_host_intf_read_pkt,
885         .master_access_msword   = rsi_sdio_master_access_msword,
886         .read_reg_multiple      = rsi_sdio_read_register_multiple,
887         .write_reg_multiple     = rsi_sdio_write_register_multiple,
888         .master_reg_read        = rsi_sdio_master_reg_read,
889         .master_reg_write       = rsi_sdio_master_reg_write,
890         .load_data_master_write = rsi_sdio_load_data_master_write,
891 };
892
893 /**
894  * rsi_probe() - This function is called by kernel when the driver provided
895  *               Vendor and device IDs are matched. All the initialization
896  *               work is done here.
897  * @pfunction: Pointer to the sdio_func structure.
898  * @id: Pointer to sdio_device_id structure.
899  *
900  * Return: 0 on success, 1 on failure.
901  */
902 static int rsi_probe(struct sdio_func *pfunction,
903                      const struct sdio_device_id *id)
904 {
905         struct rsi_hw *adapter;
906
907         rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
908
909         adapter = rsi_91x_init();
910         if (!adapter) {
911                 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
912                         __func__);
913                 return 1;
914         }
915         adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
916         adapter->host_intf_ops = &sdio_host_intf_ops;
917
918         if (rsi_init_sdio_interface(adapter, pfunction)) {
919                 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
920                         __func__);
921                 goto fail;
922         }
923         sdio_claim_host(pfunction);
924         if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
925                 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
926                 sdio_release_host(pfunction);
927                 goto fail;
928         }
929         sdio_release_host(pfunction);
930         rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
931
932         if (rsi_hal_device_init(adapter)) {
933                 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
934                 sdio_claim_host(pfunction);
935                 sdio_release_irq(pfunction);
936                 sdio_disable_func(pfunction);
937                 sdio_release_host(pfunction);
938                 goto fail;
939         }
940         rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
941
942         if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
943                 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
944                 return -EIO;
945         }
946
947         return 0;
948 fail:
949         rsi_91x_deinit(adapter);
950         rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
951         return 1;
952 }
953
954 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
955                            u16 len_in_bits)
956 {
957         rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
958                                   ((addr << 6) | ((data >> 16) & 0xffff)), 2);
959         rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
960                                   (data & 0xffff), 2);
961         rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
962                                   RSI_GSPI_CTRL_REG0_VALUE, 2);
963         rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
964                                   ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
965         msleep(20);
966 }
967
968 /*This function resets and re-initializes the chip.*/
969 static void rsi_reset_chip(struct rsi_hw *adapter)
970 {
971         u8 *data;
972         u8 sdio_interrupt_status = 0;
973         u8 request = 1;
974         int ret;
975
976         data = kzalloc(sizeof(u32), GFP_KERNEL);
977         if (!data)
978                 return;
979
980         rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
981         ret =  rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
982         if (ret < 0) {
983                 rsi_dbg(ERR_ZONE,
984                         "%s: Failed to write SDIO wakeup register\n", __func__);
985                 goto err;
986         }
987         msleep(20);
988         ret =  rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
989                                       &sdio_interrupt_status);
990         if (ret < 0) {
991                 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
992                         __func__);
993                 goto err;
994         }
995         rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
996                 __func__, sdio_interrupt_status);
997
998         /* Put Thread-Arch processor on hold */
999         if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1000                 rsi_dbg(ERR_ZONE,
1001                         "%s: Unable to set ms word to common reg\n",
1002                         __func__);
1003                 goto err;
1004         }
1005
1006         put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1007         if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1008                                              RSI_SD_REQUEST_MASTER,
1009                                              data, 4)) {
1010                 rsi_dbg(ERR_ZONE,
1011                         "%s: Unable to hold Thread-Arch processor threads\n",
1012                         __func__);
1013                 goto err;
1014         }
1015
1016         /* This msleep will ensure Thread-Arch processor to go to hold
1017          * and any pending dma transfers to rf spi in device to finish.
1018          */
1019         msleep(100);
1020
1021         ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1022         ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1023         ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0, 32);
1024         ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1, RSI_ULP_WRITE_50,
1025                        32);
1026         ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2, RSI_ULP_WRITE_0,
1027                        32);
1028         ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1029                        RSI_ULP_TIMER_ENABLE, 32);
1030         /* This msleep will be sufficient for the ulp
1031          * read write operations to complete for chip reset.
1032          */
1033         msleep(500);
1034 err:
1035         kfree(data);
1036         return;
1037 }
1038
1039 /**
1040  * rsi_disconnect() - This function performs the reverse of the probe function.
1041  * @pfunction: Pointer to the sdio_func structure.
1042  *
1043  * Return: void.
1044  */
1045 static void rsi_disconnect(struct sdio_func *pfunction)
1046 {
1047         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1048         struct rsi_91x_sdiodev *dev;
1049
1050         if (!adapter)
1051                 return;
1052
1053         dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1054         sdio_claim_host(pfunction);
1055         sdio_release_irq(pfunction);
1056         sdio_release_host(pfunction);
1057         mdelay(10);
1058
1059         rsi_mac80211_detach(adapter);
1060         mdelay(10);
1061
1062         /* Reset Chip */
1063         rsi_reset_chip(adapter);
1064
1065         /* Resetting to take care of the case, where-in driver is re-loaded */
1066         sdio_claim_host(pfunction);
1067         rsi_reset_card(pfunction);
1068         sdio_disable_func(pfunction);
1069         sdio_release_host(pfunction);
1070         dev->write_fail = 2;
1071         rsi_91x_deinit(adapter);
1072         rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1073
1074 }
1075
1076 #ifdef CONFIG_PM
1077 static int rsi_suspend(struct device *dev)
1078 {
1079         /* Not yet implemented */
1080         return -ENOSYS;
1081 }
1082
1083 static int rsi_resume(struct device *dev)
1084 {
1085         /* Not yet implemented */
1086         return -ENOSYS;
1087 }
1088
1089 static const struct dev_pm_ops rsi_pm_ops = {
1090         .suspend = rsi_suspend,
1091         .resume = rsi_resume,
1092 };
1093 #endif
1094
1095 static const struct sdio_device_id rsi_dev_table[] =  {
1096         { SDIO_DEVICE(0x303, 0x100) },
1097         { SDIO_DEVICE(0x041B, 0x0301) },
1098         { SDIO_DEVICE(0x041B, 0x0201) },
1099         { SDIO_DEVICE(0x041B, 0x9330) },
1100         { /* Blank */},
1101 };
1102
1103 static struct sdio_driver rsi_driver = {
1104         .name       = "RSI-SDIO WLAN",
1105         .probe      = rsi_probe,
1106         .remove     = rsi_disconnect,
1107         .id_table   = rsi_dev_table,
1108 #ifdef CONFIG_PM
1109         .drv = {
1110                 .pm = &rsi_pm_ops,
1111         }
1112 #endif
1113 };
1114
1115 /**
1116  * rsi_module_init() - This function registers the sdio module.
1117  * @void: Void.
1118  *
1119  * Return: 0 on success.
1120  */
1121 static int rsi_module_init(void)
1122 {
1123         int ret;
1124
1125         ret = sdio_register_driver(&rsi_driver);
1126         rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1127         return ret;
1128 }
1129
1130 /**
1131  * rsi_module_exit() - This function unregisters the sdio module.
1132  * @void: Void.
1133  *
1134  * Return: None.
1135  */
1136 static void rsi_module_exit(void)
1137 {
1138         sdio_unregister_driver(&rsi_driver);
1139         rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1140 }
1141
1142 module_init(rsi_module_init);
1143 module_exit(rsi_module_exit);
1144
1145 MODULE_AUTHOR("Redpine Signals Inc");
1146 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1147 MODULE_SUPPORTED_DEVICE("RSI-91x");
1148 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1149 /*(DEBLOBBED)*/
1150 MODULE_VERSION("0.1");
1151 MODULE_LICENSE("Dual BSD/GPL");