Version:  2.0.40 2.2.26 2.4.37 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 3.17

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

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