Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/bluetooth/btmrvl_sdio.c

  1 /**
  2  * Marvell BT-over-SDIO driver: SDIO interface related functions.
  3  *
  4  * Copyright (C) 2009, Marvell International Ltd.
  5  *
  6  * This software file (the "File") is distributed by Marvell International
  7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8  * (the "License").  You may use, redistribute and/or modify this File in
  9  * accordance with the terms and conditions of the License, a copy of which
 10  * is available by writing to the Free Software Foundation, Inc.,
 11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
 12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 13  *
 14  *
 15  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 16  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 17  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 18  * this warranty disclaimer.
 19  **/
 20 
 21 #include <linux/firmware.h>
 22 #include <linux/slab.h>
 23 
 24 #include <linux/mmc/sdio_ids.h>
 25 #include <linux/mmc/sdio_func.h>
 26 #include <linux/module.h>
 27 
 28 #include <net/bluetooth/bluetooth.h>
 29 #include <net/bluetooth/hci_core.h>
 30 
 31 #include "btmrvl_drv.h"
 32 #include "btmrvl_sdio.h"
 33 
 34 #define VERSION "1.0"
 35 
 36 /* The btmrvl_sdio_remove() callback function is called
 37  * when user removes this module from kernel space or ejects
 38  * the card from the slot. The driver handles these 2 cases
 39  * differently.
 40  * If the user is removing the module, a MODULE_SHUTDOWN_REQ
 41  * command is sent to firmware and interrupt will be disabled.
 42  * If the card is removed, there is no need to send command
 43  * or disable interrupt.
 44  *
 45  * The variable 'user_rmmod' is used to distinguish these two
 46  * scenarios. This flag is initialized as FALSE in case the card
 47  * is removed, and will be set to TRUE for module removal when
 48  * module_exit function is called.
 49  */
 50 static u8 user_rmmod;
 51 static u8 sdio_ireg;
 52 
 53 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
 54         .cfg = 0x03,
 55         .host_int_mask = 0x04,
 56         .host_intstatus = 0x05,
 57         .card_status = 0x20,
 58         .sq_read_base_addr_a0 = 0x10,
 59         .sq_read_base_addr_a1 = 0x11,
 60         .card_fw_status0 = 0x40,
 61         .card_fw_status1 = 0x41,
 62         .card_rx_len = 0x42,
 63         .card_rx_unit = 0x43,
 64         .io_port_0 = 0x00,
 65         .io_port_1 = 0x01,
 66         .io_port_2 = 0x02,
 67         .int_read_to_clear = false,
 68 };
 69 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
 70         .cfg = 0x00,
 71         .host_int_mask = 0x02,
 72         .host_intstatus = 0x03,
 73         .card_status = 0x30,
 74         .sq_read_base_addr_a0 = 0x40,
 75         .sq_read_base_addr_a1 = 0x41,
 76         .card_revision = 0x5c,
 77         .card_fw_status0 = 0x60,
 78         .card_fw_status1 = 0x61,
 79         .card_rx_len = 0x62,
 80         .card_rx_unit = 0x63,
 81         .io_port_0 = 0x78,
 82         .io_port_1 = 0x79,
 83         .io_port_2 = 0x7a,
 84         .int_read_to_clear = false,
 85 };
 86 
 87 static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx = {
 88         .cfg = 0x00,
 89         .host_int_mask = 0x02,
 90         .host_intstatus = 0x03,
 91         .card_status = 0x50,
 92         .sq_read_base_addr_a0 = 0x60,
 93         .sq_read_base_addr_a1 = 0x61,
 94         .card_revision = 0xbc,
 95         .card_fw_status0 = 0xc0,
 96         .card_fw_status1 = 0xc1,
 97         .card_rx_len = 0xc2,
 98         .card_rx_unit = 0xc3,
 99         .io_port_0 = 0xd8,
100         .io_port_1 = 0xd9,
101         .io_port_2 = 0xda,
102         .int_read_to_clear = true,
103         .host_int_rsr = 0x01,
104         .card_misc_cfg = 0xcc,
105 };
106 
107 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
108         .helper         = "mrvl/sd8688_helper.bin",
109         .firmware       = "mrvl/sd8688.bin",
110         .reg            = &btmrvl_reg_8688,
111         .sd_blksz_fw_dl = 64,
112 };
113 
114 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
115         .helper         = NULL,
116         .firmware       = "mrvl/sd8787_uapsta.bin",
117         .reg            = &btmrvl_reg_87xx,
118         .sd_blksz_fw_dl = 256,
119 };
120 
121 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
122         .helper         = NULL,
123         .firmware       = "mrvl/sd8797_uapsta.bin",
124         .reg            = &btmrvl_reg_87xx,
125         .sd_blksz_fw_dl = 256,
126 };
127 
128 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
129         .helper         = NULL,
130         .firmware       = "mrvl/sd8897_uapsta.bin",
131         .reg            = &btmrvl_reg_88xx,
132         .sd_blksz_fw_dl = 256,
133 };
134 
135 static const struct sdio_device_id btmrvl_sdio_ids[] = {
136         /* Marvell SD8688 Bluetooth device */
137         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
138                         .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
139         /* Marvell SD8787 Bluetooth device */
140         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
141                         .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
142         /* Marvell SD8787 Bluetooth AMP device */
143         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
144                         .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
145         /* Marvell SD8797 Bluetooth device */
146         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
147                         .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
148         /* Marvell SD8897 Bluetooth device */
149         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
150                         .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
151 
152         { }     /* Terminating entry */
153 };
154 
155 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
156 
157 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
158 {
159         u8 reg;
160         int ret;
161 
162         reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
163         if (!ret)
164                 card->rx_unit = reg;
165 
166         return ret;
167 }
168 
169 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
170 {
171         u8 fws0, fws1;
172         int ret;
173 
174         *dat = 0;
175 
176         fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
177         if (ret)
178                 return -EIO;
179 
180         fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
181         if (ret)
182                 return -EIO;
183 
184         *dat = (((u16) fws1) << 8) | fws0;
185 
186         return 0;
187 }
188 
189 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
190 {
191         u8 reg;
192         int ret;
193 
194         reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
195         if (!ret)
196                 *dat = (u16) reg << card->rx_unit;
197 
198         return ret;
199 }
200 
201 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
202                                                                 u8 mask)
203 {
204         int ret;
205 
206         sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
207         if (ret) {
208                 BT_ERR("Unable to enable the host interrupt!");
209                 ret = -EIO;
210         }
211 
212         return ret;
213 }
214 
215 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
216                                                                 u8 mask)
217 {
218         u8 host_int_mask;
219         int ret;
220 
221         host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
222         if (ret)
223                 return -EIO;
224 
225         host_int_mask &= ~mask;
226 
227         sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
228         if (ret < 0) {
229                 BT_ERR("Unable to disable the host interrupt!");
230                 return -EIO;
231         }
232 
233         return 0;
234 }
235 
236 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
237 {
238         unsigned int tries;
239         u8 status;
240         int ret;
241 
242         for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
243                 status = sdio_readb(card->func, card->reg->card_status, &ret);
244                 if (ret)
245                         goto failed;
246                 if ((status & bits) == bits)
247                         return ret;
248 
249                 udelay(1);
250         }
251 
252         ret = -ETIMEDOUT;
253 
254 failed:
255         BT_ERR("FAILED! ret=%d", ret);
256 
257         return ret;
258 }
259 
260 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
261                                                                 int pollnum)
262 {
263         u16 firmwarestat;
264         int tries, ret;
265 
266          /* Wait for firmware to become ready */
267         for (tries = 0; tries < pollnum; tries++) {
268                 sdio_claim_host(card->func);
269                 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
270                 sdio_release_host(card->func);
271                 if (ret < 0)
272                         continue;
273 
274                 if (firmwarestat == FIRMWARE_READY)
275                         return 0;
276 
277                 msleep(10);
278         }
279 
280         return -ETIMEDOUT;
281 }
282 
283 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
284 {
285         const struct firmware *fw_helper = NULL;
286         const u8 *helper = NULL;
287         int ret;
288         void *tmphlprbuf = NULL;
289         int tmphlprbufsz, hlprblknow, helperlen;
290         u8 *helperbuf;
291         u32 tx_len;
292 
293         ret = request_firmware(&fw_helper, card->helper,
294                                                 &card->func->dev);
295         if ((ret < 0) || !fw_helper) {
296                 BT_ERR("request_firmware(helper) failed, error code = %d",
297                                                                         ret);
298                 ret = -ENOENT;
299                 goto done;
300         }
301 
302         helper = fw_helper->data;
303         helperlen = fw_helper->size;
304 
305         BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
306                                                 helperlen, SDIO_BLOCK_SIZE);
307 
308         tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
309 
310         tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
311         if (!tmphlprbuf) {
312                 BT_ERR("Unable to allocate buffer for helper."
313                         " Terminating download");
314                 ret = -ENOMEM;
315                 goto done;
316         }
317 
318         helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
319 
320         /* Perform helper data transfer */
321         tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
322                         - SDIO_HEADER_LEN;
323         hlprblknow = 0;
324 
325         do {
326                 ret = btmrvl_sdio_poll_card_status(card,
327                                             CARD_IO_READY | DN_LD_CARD_RDY);
328                 if (ret < 0) {
329                         BT_ERR("Helper download poll status timeout @ %d",
330                                 hlprblknow);
331                         goto done;
332                 }
333 
334                 /* Check if there is more data? */
335                 if (hlprblknow >= helperlen)
336                         break;
337 
338                 if (helperlen - hlprblknow < tx_len)
339                         tx_len = helperlen - hlprblknow;
340 
341                 /* Little-endian */
342                 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
343                 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
344                 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
345                 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
346 
347                 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
348                                 tx_len);
349 
350                 /* Now send the data */
351                 ret = sdio_writesb(card->func, card->ioport, helperbuf,
352                                 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
353                 if (ret < 0) {
354                         BT_ERR("IO error during helper download @ %d",
355                                 hlprblknow);
356                         goto done;
357                 }
358 
359                 hlprblknow += tx_len;
360         } while (true);
361 
362         BT_DBG("Transferring helper image EOF block");
363 
364         memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
365 
366         ret = sdio_writesb(card->func, card->ioport, helperbuf,
367                                                         SDIO_BLOCK_SIZE);
368         if (ret < 0) {
369                 BT_ERR("IO error in writing helper image EOF block");
370                 goto done;
371         }
372 
373         ret = 0;
374 
375 done:
376         kfree(tmphlprbuf);
377         release_firmware(fw_helper);
378         return ret;
379 }
380 
381 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
382 {
383         const struct firmware *fw_firmware = NULL;
384         const u8 *firmware = NULL;
385         int firmwarelen, tmpfwbufsz, ret;
386         unsigned int tries, offset;
387         u8 base0, base1;
388         void *tmpfwbuf = NULL;
389         u8 *fwbuf;
390         u16 len, blksz_dl = card->sd_blksz_fw_dl;
391         int txlen = 0, tx_blocks = 0, count = 0;
392 
393         ret = request_firmware(&fw_firmware, card->firmware,
394                                                         &card->func->dev);
395         if ((ret < 0) || !fw_firmware) {
396                 BT_ERR("request_firmware(firmware) failed, error code = %d",
397                                                                         ret);
398                 ret = -ENOENT;
399                 goto done;
400         }
401 
402         firmware = fw_firmware->data;
403         firmwarelen = fw_firmware->size;
404 
405         BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
406 
407         tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
408         tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
409         if (!tmpfwbuf) {
410                 BT_ERR("Unable to allocate buffer for firmware."
411                        " Terminating download");
412                 ret = -ENOMEM;
413                 goto done;
414         }
415 
416         /* Ensure aligned firmware buffer */
417         fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
418 
419         /* Perform firmware data transfer */
420         offset = 0;
421         do {
422                 ret = btmrvl_sdio_poll_card_status(card,
423                                         CARD_IO_READY | DN_LD_CARD_RDY);
424                 if (ret < 0) {
425                         BT_ERR("FW download with helper poll status"
426                                                 " timeout @ %d", offset);
427                         goto done;
428                 }
429 
430                 /* Check if there is more data ? */
431                 if (offset >= firmwarelen)
432                         break;
433 
434                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
435                         base0 = sdio_readb(card->func,
436                                         card->reg->sq_read_base_addr_a0, &ret);
437                         if (ret) {
438                                 BT_ERR("BASE0 register read failed:"
439                                         " base0 = 0x%04X(%d)."
440                                         " Terminating download",
441                                         base0, base0);
442                                 ret = -EIO;
443                                 goto done;
444                         }
445                         base1 = sdio_readb(card->func,
446                                         card->reg->sq_read_base_addr_a1, &ret);
447                         if (ret) {
448                                 BT_ERR("BASE1 register read failed:"
449                                         " base1 = 0x%04X(%d)."
450                                         " Terminating download",
451                                         base1, base1);
452                                 ret = -EIO;
453                                 goto done;
454                         }
455 
456                         len = (((u16) base1) << 8) | base0;
457                         if (len)
458                                 break;
459 
460                         udelay(10);
461                 }
462 
463                 if (!len)
464                         break;
465                 else if (len > BTM_UPLD_SIZE) {
466                         BT_ERR("FW download failure @%d, invalid length %d",
467                                                                 offset, len);
468                         ret = -EINVAL;
469                         goto done;
470                 }
471 
472                 txlen = len;
473 
474                 if (len & BIT(0)) {
475                         count++;
476                         if (count > MAX_WRITE_IOMEM_RETRY) {
477                                 BT_ERR("FW download failure @%d, "
478                                         "over max retry count", offset);
479                                 ret = -EIO;
480                                 goto done;
481                         }
482                         BT_ERR("FW CRC error indicated by the helper: "
483                                 "len = 0x%04X, txlen = %d", len, txlen);
484                         len &= ~BIT(0);
485                         /* Set txlen to 0 so as to resend from same offset */
486                         txlen = 0;
487                 } else {
488                         count = 0;
489 
490                         /* Last block ? */
491                         if (firmwarelen - offset < txlen)
492                                 txlen = firmwarelen - offset;
493 
494                         tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
495 
496                         memcpy(fwbuf, &firmware[offset], txlen);
497                 }
498 
499                 ret = sdio_writesb(card->func, card->ioport, fwbuf,
500                                                 tx_blocks * blksz_dl);
501 
502                 if (ret < 0) {
503                         BT_ERR("FW download, writesb(%d) failed @%d",
504                                                         count, offset);
505                         sdio_writeb(card->func, HOST_CMD53_FIN,
506                                                 card->reg->cfg, &ret);
507                         if (ret)
508                                 BT_ERR("writeb failed (CFG)");
509                 }
510 
511                 offset += txlen;
512         } while (true);
513 
514         BT_DBG("FW download over, size %d bytes", offset);
515 
516         ret = 0;
517 
518 done:
519         kfree(tmpfwbuf);
520         release_firmware(fw_firmware);
521         return ret;
522 }
523 
524 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
525 {
526         u16 buf_len = 0;
527         int ret, num_blocks, blksz;
528         struct sk_buff *skb = NULL;
529         u32 type;
530         u8 *payload = NULL;
531         struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
532         struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
533 
534         if (!card || !card->func) {
535                 BT_ERR("card or function is NULL!");
536                 ret = -EINVAL;
537                 goto exit;
538         }
539 
540         /* Read the length of data to be transferred */
541         ret = btmrvl_sdio_read_rx_len(card, &buf_len);
542         if (ret < 0) {
543                 BT_ERR("read rx_len failed");
544                 ret = -EIO;
545                 goto exit;
546         }
547 
548         blksz = SDIO_BLOCK_SIZE;
549         num_blocks = DIV_ROUND_UP(buf_len, blksz);
550 
551         if (buf_len <= SDIO_HEADER_LEN
552             || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
553                 BT_ERR("invalid packet length: %d", buf_len);
554                 ret = -EINVAL;
555                 goto exit;
556         }
557 
558         /* Allocate buffer */
559         skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
560         if (skb == NULL) {
561                 BT_ERR("No free skb");
562                 ret = -ENOMEM;
563                 goto exit;
564         }
565 
566         if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
567                 skb_put(skb, (unsigned long) skb->data &
568                                         (BTSDIO_DMA_ALIGN - 1));
569                 skb_pull(skb, (unsigned long) skb->data &
570                                         (BTSDIO_DMA_ALIGN - 1));
571         }
572 
573         payload = skb->data;
574 
575         ret = sdio_readsb(card->func, payload, card->ioport,
576                           num_blocks * blksz);
577         if (ret < 0) {
578                 BT_ERR("readsb failed: %d", ret);
579                 ret = -EIO;
580                 goto exit;
581         }
582 
583         /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
584          * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
585          */
586 
587         buf_len = payload[0];
588         buf_len |= payload[1] << 8;
589         buf_len |= payload[2] << 16;
590 
591         if (buf_len > blksz * num_blocks) {
592                 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
593                        buf_len, blksz * num_blocks);
594                 ret = -EIO;
595                 goto exit;
596         }
597 
598         type = payload[3];
599 
600         switch (type) {
601         case HCI_ACLDATA_PKT:
602         case HCI_SCODATA_PKT:
603         case HCI_EVENT_PKT:
604                 bt_cb(skb)->pkt_type = type;
605                 skb_put(skb, buf_len);
606                 skb_pull(skb, SDIO_HEADER_LEN);
607 
608                 if (type == HCI_EVENT_PKT) {
609                         if (btmrvl_check_evtpkt(priv, skb))
610                                 hci_recv_frame(hdev, skb);
611                 } else {
612                         hci_recv_frame(hdev, skb);
613                 }
614 
615                 hdev->stat.byte_rx += buf_len;
616                 break;
617 
618         case MRVL_VENDOR_PKT:
619                 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
620                 skb_put(skb, buf_len);
621                 skb_pull(skb, SDIO_HEADER_LEN);
622 
623                 if (btmrvl_process_event(priv, skb))
624                         hci_recv_frame(hdev, skb);
625 
626                 hdev->stat.byte_rx += buf_len;
627                 break;
628 
629         default:
630                 BT_ERR("Unknown packet type:%d", type);
631                 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
632 
633                 kfree_skb(skb);
634                 skb = NULL;
635                 break;
636         }
637 
638 exit:
639         if (ret) {
640                 hdev->stat.err_rx++;
641                 kfree_skb(skb);
642         }
643 
644         return ret;
645 }
646 
647 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
648 {
649         ulong flags;
650         u8 ireg;
651         struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
652 
653         spin_lock_irqsave(&priv->driver_lock, flags);
654         ireg = sdio_ireg;
655         sdio_ireg = 0;
656         spin_unlock_irqrestore(&priv->driver_lock, flags);
657 
658         sdio_claim_host(card->func);
659         if (ireg & DN_LD_HOST_INT_STATUS) {
660                 if (priv->btmrvl_dev.tx_dnld_rdy)
661                         BT_DBG("tx_done already received: "
662                                 " int_status=0x%x", ireg);
663                 else
664                         priv->btmrvl_dev.tx_dnld_rdy = true;
665         }
666 
667         if (ireg & UP_LD_HOST_INT_STATUS)
668                 btmrvl_sdio_card_to_host(priv);
669 
670         sdio_release_host(card->func);
671 
672         return 0;
673 }
674 
675 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
676 {
677         struct btmrvl_adapter *adapter = card->priv->adapter;
678         int ret;
679 
680         ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
681         if (ret) {
682                 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
683                 return ret;
684         }
685 
686         *ireg = adapter->hw_regs[card->reg->host_intstatus];
687         BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
688 
689         return 0;
690 }
691 
692 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
693 {
694         int ret;
695 
696         *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
697         if (ret) {
698                 BT_ERR("sdio_readb: read int status failed: %d", ret);
699                 return ret;
700         }
701 
702         if (*ireg) {
703                 /*
704                  * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
705                  * Clear the interrupt status register and re-enable the
706                  * interrupt.
707                  */
708                 BT_DBG("int_status = 0x%x", *ireg);
709 
710                 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
711                                                     UP_LD_HOST_INT_STATUS),
712                             card->reg->host_intstatus, &ret);
713                 if (ret) {
714                         BT_ERR("sdio_writeb: clear int status failed: %d", ret);
715                         return ret;
716                 }
717         }
718 
719         return 0;
720 }
721 
722 static void btmrvl_sdio_interrupt(struct sdio_func *func)
723 {
724         struct btmrvl_private *priv;
725         struct btmrvl_sdio_card *card;
726         ulong flags;
727         u8 ireg = 0;
728         int ret;
729 
730         card = sdio_get_drvdata(func);
731         if (!card || !card->priv) {
732                 BT_ERR("sbi_interrupt(%p) card or priv is "
733                                 "NULL, card=%p\n", func, card);
734                 return;
735         }
736 
737         priv = card->priv;
738 
739         if (card->reg->int_read_to_clear)
740                 ret = btmrvl_sdio_read_to_clear(card, &ireg);
741         else
742                 ret = btmrvl_sdio_write_to_clear(card, &ireg);
743 
744         if (ret)
745                 return;
746 
747         spin_lock_irqsave(&priv->driver_lock, flags);
748         sdio_ireg |= ireg;
749         spin_unlock_irqrestore(&priv->driver_lock, flags);
750 
751         btmrvl_interrupt(priv);
752 }
753 
754 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
755 {
756         struct sdio_func *func;
757         u8 reg;
758         int ret = 0;
759 
760         if (!card || !card->func) {
761                 BT_ERR("Error: card or function is NULL!");
762                 ret = -EINVAL;
763                 goto failed;
764         }
765 
766         func = card->func;
767 
768         sdio_claim_host(func);
769 
770         ret = sdio_enable_func(func);
771         if (ret) {
772                 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
773                 ret = -EIO;
774                 goto release_host;
775         }
776 
777         ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
778         if (ret) {
779                 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
780                 ret = -EIO;
781                 goto disable_func;
782         }
783 
784         ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
785         if (ret) {
786                 BT_ERR("cannot set SDIO block size");
787                 ret = -EIO;
788                 goto release_irq;
789         }
790 
791         reg = sdio_readb(func, card->reg->io_port_0, &ret);
792         if (ret < 0) {
793                 ret = -EIO;
794                 goto release_irq;
795         }
796 
797         card->ioport = reg;
798 
799         reg = sdio_readb(func, card->reg->io_port_1, &ret);
800         if (ret < 0) {
801                 ret = -EIO;
802                 goto release_irq;
803         }
804 
805         card->ioport |= (reg << 8);
806 
807         reg = sdio_readb(func, card->reg->io_port_2, &ret);
808         if (ret < 0) {
809                 ret = -EIO;
810                 goto release_irq;
811         }
812 
813         card->ioport |= (reg << 16);
814 
815         BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
816 
817         if (card->reg->int_read_to_clear) {
818                 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
819                 if (ret < 0) {
820                         ret = -EIO;
821                         goto release_irq;
822                 }
823                 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
824                 if (ret < 0) {
825                         ret = -EIO;
826                         goto release_irq;
827                 }
828 
829                 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
830                 if (ret < 0) {
831                         ret = -EIO;
832                         goto release_irq;
833                 }
834                 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
835                 if (ret < 0) {
836                         ret = -EIO;
837                         goto release_irq;
838                 }
839         }
840 
841         sdio_set_drvdata(func, card);
842 
843         sdio_release_host(func);
844 
845         return 0;
846 
847 release_irq:
848         sdio_release_irq(func);
849 
850 disable_func:
851         sdio_disable_func(func);
852 
853 release_host:
854         sdio_release_host(func);
855 
856 failed:
857         return ret;
858 }
859 
860 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
861 {
862         if (card && card->func) {
863                 sdio_claim_host(card->func);
864                 sdio_release_irq(card->func);
865                 sdio_disable_func(card->func);
866                 sdio_release_host(card->func);
867                 sdio_set_drvdata(card->func, NULL);
868         }
869 
870         return 0;
871 }
872 
873 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
874 {
875         int ret;
876 
877         if (!card || !card->func)
878                 return -EINVAL;
879 
880         sdio_claim_host(card->func);
881 
882         ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
883 
884         btmrvl_sdio_get_rx_unit(card);
885 
886         sdio_release_host(card->func);
887 
888         return ret;
889 }
890 
891 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
892 {
893         int ret;
894 
895         if (!card || !card->func)
896                 return -EINVAL;
897 
898         sdio_claim_host(card->func);
899 
900         ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
901 
902         sdio_release_host(card->func);
903 
904         return ret;
905 }
906 
907 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
908                                 u8 *payload, u16 nb)
909 {
910         struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
911         int ret = 0;
912         int buf_block_len;
913         int blksz;
914         int i = 0;
915         u8 *buf = NULL;
916         void *tmpbuf = NULL;
917         int tmpbufsz;
918 
919         if (!card || !card->func) {
920                 BT_ERR("card or function is NULL!");
921                 return -EINVAL;
922         }
923 
924         buf = payload;
925         if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
926                 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
927                 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
928                 if (!tmpbuf)
929                         return -ENOMEM;
930                 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
931                 memcpy(buf, payload, nb);
932         }
933 
934         blksz = SDIO_BLOCK_SIZE;
935         buf_block_len = DIV_ROUND_UP(nb, blksz);
936 
937         sdio_claim_host(card->func);
938 
939         do {
940                 /* Transfer data to card */
941                 ret = sdio_writesb(card->func, card->ioport, buf,
942                                    buf_block_len * blksz);
943                 if (ret < 0) {
944                         i++;
945                         BT_ERR("i=%d writesb failed: %d", i, ret);
946                         BT_ERR("hex: %*ph", nb, payload);
947                         ret = -EIO;
948                         if (i > MAX_WRITE_IOMEM_RETRY)
949                                 goto exit;
950                 }
951         } while (ret);
952 
953         priv->btmrvl_dev.tx_dnld_rdy = false;
954 
955 exit:
956         sdio_release_host(card->func);
957         kfree(tmpbuf);
958 
959         return ret;
960 }
961 
962 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
963 {
964         int ret;
965         u8 fws0;
966         int pollnum = MAX_POLL_TRIES;
967 
968         if (!card || !card->func) {
969                 BT_ERR("card or function is NULL!");
970                 return -EINVAL;
971         }
972 
973         if (!btmrvl_sdio_verify_fw_download(card, 1)) {
974                 BT_DBG("Firmware already downloaded!");
975                 return 0;
976         }
977 
978         sdio_claim_host(card->func);
979 
980         /* Check if other function driver is downloading the firmware */
981         fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
982         if (ret) {
983                 BT_ERR("Failed to read FW downloading status!");
984                 ret = -EIO;
985                 goto done;
986         }
987         if (fws0) {
988                 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
989 
990                 /* Give other function more time to download the firmware */
991                 pollnum *= 10;
992         } else {
993                 if (card->helper) {
994                         ret = btmrvl_sdio_download_helper(card);
995                         if (ret) {
996                                 BT_ERR("Failed to download helper!");
997                                 ret = -EIO;
998                                 goto done;
999                         }
1000                 }
1001 
1002                 if (btmrvl_sdio_download_fw_w_helper(card)) {
1003                         BT_ERR("Failed to download firmware!");
1004                         ret = -EIO;
1005                         goto done;
1006                 }
1007         }
1008 
1009         sdio_release_host(card->func);
1010 
1011         /*
1012          * winner or not, with this test the FW synchronizes when the
1013          * module can continue its initialization
1014          */
1015         if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1016                 BT_ERR("FW failed to be active in time!");
1017                 return -ETIMEDOUT;
1018         }
1019 
1020         return 0;
1021 
1022 done:
1023         sdio_release_host(card->func);
1024         return ret;
1025 }
1026 
1027 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1028 {
1029         struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1030         int ret = 0;
1031 
1032         if (!card || !card->func) {
1033                 BT_ERR("card or function is NULL!");
1034                 return -EINVAL;
1035         }
1036 
1037         sdio_claim_host(card->func);
1038 
1039         sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1040 
1041         sdio_release_host(card->func);
1042 
1043         BT_DBG("wake up firmware");
1044 
1045         return ret;
1046 }
1047 
1048 static int btmrvl_sdio_probe(struct sdio_func *func,
1049                                         const struct sdio_device_id *id)
1050 {
1051         int ret = 0;
1052         struct btmrvl_private *priv = NULL;
1053         struct btmrvl_sdio_card *card = NULL;
1054 
1055         BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1056                         id->vendor, id->device, id->class, func->num);
1057 
1058         card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1059         if (!card)
1060                 return -ENOMEM;
1061 
1062         card->func = func;
1063 
1064         if (id->driver_data) {
1065                 struct btmrvl_sdio_device *data = (void *) id->driver_data;
1066                 card->helper = data->helper;
1067                 card->firmware = data->firmware;
1068                 card->reg = data->reg;
1069                 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1070         }
1071 
1072         if (btmrvl_sdio_register_dev(card) < 0) {
1073                 BT_ERR("Failed to register BT device!");
1074                 return -ENODEV;
1075         }
1076 
1077         /* Disable the interrupts on the card */
1078         btmrvl_sdio_disable_host_int(card);
1079 
1080         if (btmrvl_sdio_download_fw(card)) {
1081                 BT_ERR("Downloading firmware failed!");
1082                 ret = -ENODEV;
1083                 goto unreg_dev;
1084         }
1085 
1086         btmrvl_sdio_enable_host_int(card);
1087 
1088         priv = btmrvl_add_card(card);
1089         if (!priv) {
1090                 BT_ERR("Initializing card failed!");
1091                 ret = -ENODEV;
1092                 goto disable_host_int;
1093         }
1094 
1095         card->priv = priv;
1096 
1097         /* Initialize the interface specific function pointers */
1098         priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1099         priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1100         priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1101 
1102         if (btmrvl_register_hdev(priv)) {
1103                 BT_ERR("Register hdev failed!");
1104                 ret = -ENODEV;
1105                 goto disable_host_int;
1106         }
1107 
1108         return 0;
1109 
1110 disable_host_int:
1111         btmrvl_sdio_disable_host_int(card);
1112 unreg_dev:
1113         btmrvl_sdio_unregister_dev(card);
1114         return ret;
1115 }
1116 
1117 static void btmrvl_sdio_remove(struct sdio_func *func)
1118 {
1119         struct btmrvl_sdio_card *card;
1120 
1121         if (func) {
1122                 card = sdio_get_drvdata(func);
1123                 if (card) {
1124                         /* Send SHUTDOWN command & disable interrupt
1125                          * if user removes the module.
1126                          */
1127                         if (user_rmmod) {
1128                                 btmrvl_send_module_cfg_cmd(card->priv,
1129                                                         MODULE_SHUTDOWN_REQ);
1130                                 btmrvl_sdio_disable_host_int(card);
1131                         }
1132                         BT_DBG("unregester dev");
1133                         btmrvl_sdio_unregister_dev(card);
1134                         btmrvl_remove_card(card->priv);
1135                 }
1136         }
1137 }
1138 
1139 static int btmrvl_sdio_suspend(struct device *dev)
1140 {
1141         struct sdio_func *func = dev_to_sdio_func(dev);
1142         struct btmrvl_sdio_card *card;
1143         struct btmrvl_private *priv;
1144         mmc_pm_flag_t pm_flags;
1145         struct hci_dev *hcidev;
1146 
1147         if (func) {
1148                 pm_flags = sdio_get_host_pm_caps(func);
1149                 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1150                        pm_flags);
1151                 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1152                         BT_ERR("%s: cannot remain alive while suspended",
1153                                sdio_func_id(func));
1154                         return -ENOSYS;
1155                 }
1156                 card = sdio_get_drvdata(func);
1157                 if (!card || !card->priv) {
1158                         BT_ERR("card or priv structure is not valid");
1159                         return 0;
1160                 }
1161         } else {
1162                 BT_ERR("sdio_func is not specified");
1163                 return 0;
1164         }
1165 
1166         priv = card->priv;
1167 
1168         if (priv->adapter->hs_state != HS_ACTIVATED) {
1169                 if (btmrvl_enable_hs(priv)) {
1170                         BT_ERR("HS not actived, suspend failed!");
1171                         return -EBUSY;
1172                 }
1173         }
1174         hcidev = priv->btmrvl_dev.hcidev;
1175         BT_DBG("%s: SDIO suspend", hcidev->name);
1176         hci_suspend_dev(hcidev);
1177         skb_queue_purge(&priv->adapter->tx_queue);
1178 
1179         priv->adapter->is_suspended = true;
1180 
1181         /* We will keep the power when hs enabled successfully */
1182         if (priv->adapter->hs_state == HS_ACTIVATED) {
1183                 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1184                 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1185         } else {
1186                 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1187                 return 0;
1188         }
1189 }
1190 
1191 static int btmrvl_sdio_resume(struct device *dev)
1192 {
1193         struct sdio_func *func = dev_to_sdio_func(dev);
1194         struct btmrvl_sdio_card *card;
1195         struct btmrvl_private *priv;
1196         mmc_pm_flag_t pm_flags;
1197         struct hci_dev *hcidev;
1198 
1199         if (func) {
1200                 pm_flags = sdio_get_host_pm_caps(func);
1201                 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1202                        pm_flags);
1203                 card = sdio_get_drvdata(func);
1204                 if (!card || !card->priv) {
1205                         BT_ERR("card or priv structure is not valid");
1206                         return 0;
1207                 }
1208         } else {
1209                 BT_ERR("sdio_func is not specified");
1210                 return 0;
1211         }
1212         priv = card->priv;
1213 
1214         if (!priv->adapter->is_suspended) {
1215                 BT_DBG("device already resumed");
1216                 return 0;
1217         }
1218 
1219         priv->adapter->is_suspended = false;
1220         hcidev = priv->btmrvl_dev.hcidev;
1221         BT_DBG("%s: SDIO resume", hcidev->name);
1222         hci_resume_dev(hcidev);
1223         priv->hw_wakeup_firmware(priv);
1224         priv->adapter->hs_state = HS_DEACTIVATED;
1225         BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1226 
1227         return 0;
1228 }
1229 
1230 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1231         .suspend        = btmrvl_sdio_suspend,
1232         .resume         = btmrvl_sdio_resume,
1233 };
1234 
1235 static struct sdio_driver bt_mrvl_sdio = {
1236         .name           = "btmrvl_sdio",
1237         .id_table       = btmrvl_sdio_ids,
1238         .probe          = btmrvl_sdio_probe,
1239         .remove         = btmrvl_sdio_remove,
1240         .drv = {
1241                 .owner = THIS_MODULE,
1242                 .pm = &btmrvl_sdio_pm_ops,
1243         }
1244 };
1245 
1246 static int __init btmrvl_sdio_init_module(void)
1247 {
1248         if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1249                 BT_ERR("SDIO Driver Registration Failed");
1250                 return -ENODEV;
1251         }
1252 
1253         /* Clear the flag in case user removes the card. */
1254         user_rmmod = 0;
1255 
1256         return 0;
1257 }
1258 
1259 static void __exit btmrvl_sdio_exit_module(void)
1260 {
1261         /* Set the flag as user is removing this module. */
1262         user_rmmod = 1;
1263 
1264         sdio_unregister_driver(&bt_mrvl_sdio);
1265 }
1266 
1267 module_init(btmrvl_sdio_init_module);
1268 module_exit(btmrvl_sdio_exit_module);
1269 
1270 MODULE_AUTHOR("Marvell International Ltd.");
1271 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1272 MODULE_VERSION(VERSION);
1273 MODULE_LICENSE("GPL v2");
1274 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1275 MODULE_FIRMWARE("mrvl/sd8688.bin");
1276 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1277 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1278 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
1279 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us