2 * linux/drivers/net/wireless/libertas/if_sdio.c
4 * Copyright 2007 Pierre Ossman
6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This hardware has more or less no CMD53 support, so all registers
14 * must be accessed using sdio_readb()/sdio_writeb().
16 * Transfers must be in one transaction or the firmware goes bonkers.
17 * This means that the transfer must either be small enough to do a
18 * byte based transfer or it must be padded to a multiple of the
21 * As SDIO is still new to the kernel, it is unfortunately common with
22 * bugs in the host controllers related to that. One such bug is that
23 * controllers cannot do transfers that aren't a multiple of 4 bytes.
24 * If you don't have time to fix the host controller driver, you can
25 * work around the problem by modifying if_sdio_host_to_card() and
26 * if_sdio_card_to_host() to pad the data.
29 #include <linux/moduleparam.h>
30 #include <linux/firmware.h>
31 #include <linux/netdevice.h>
32 #include <linux/delay.h>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/sdio_func.h>
35 #include <linux/mmc/sdio_ids.h>
43 static char *lbs_helper_name = NULL;
44 module_param_named(helper_name, lbs_helper_name, charp, 0644);
46 static char *lbs_fw_name = NULL;
47 module_param_named(fw_name, lbs_fw_name, charp, 0644);
49 static const struct sdio_device_id if_sdio_ids[] = {
50 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
51 { /* end: all zeroes */ },
54 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
56 struct if_sdio_model {
62 static struct if_sdio_model if_sdio_models[] = {
66 .helper = "sd8385_helper.bin",
67 .firmware = "sd8385.bin",
72 .helper = "sd8686_helper.bin",
73 .firmware = "sd8686.bin",
77 struct if_sdio_packet {
78 struct if_sdio_packet *next;
80 u8 buffer[0] __attribute__((aligned(4)));
84 struct sdio_func *func;
85 struct lbs_private *priv;
98 struct if_sdio_packet *packets;
99 struct work_struct packet_worker;
102 /********************************************************************/
104 /********************************************************************/
106 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
111 if (card->model == 0x04)
112 reg = IF_SDIO_SCRATCH_OLD;
114 reg = IF_SDIO_SCRATCH;
116 scratch = sdio_readb(card->func, reg, &ret);
118 scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;
129 static int if_sdio_handle_cmd(struct if_sdio_card *card,
130 u8 *buffer, unsigned size)
135 lbs_deb_enter(LBS_DEB_SDIO);
137 spin_lock_irqsave(&card->priv->driver_lock, flags);
139 if (size > LBS_CMD_BUFFER_SIZE) {
140 lbs_deb_sdio("response packet too large (%d bytes)\n",
146 memcpy(card->priv->upld_buf, buffer, size);
147 card->priv->upld_len = size;
149 card->int_cause |= MRVDRV_CMD_UPLD_RDY;
151 lbs_interrupt(card->priv);
156 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
158 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
163 static int if_sdio_handle_data(struct if_sdio_card *card,
164 u8 *buffer, unsigned size)
170 lbs_deb_enter(LBS_DEB_SDIO);
172 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
173 lbs_deb_sdio("response packet too large (%d bytes)\n",
179 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
185 skb_reserve(skb, NET_IP_ALIGN);
187 data = skb_put(skb, size);
189 memcpy(data, buffer, size);
191 lbs_process_rxed_packet(card->priv, skb);
196 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
201 static int if_sdio_handle_event(struct if_sdio_card *card,
202 u8 *buffer, unsigned size)
208 lbs_deb_enter(LBS_DEB_SDIO);
210 if (card->model == 0x04) {
211 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
216 lbs_deb_sdio("event packet too small (%d bytes)\n",
221 event = buffer[3] << 24;
222 event |= buffer[2] << 16;
223 event |= buffer[1] << 8;
224 event |= buffer[0] << 0;
225 event <<= SBI_EVENT_CAUSE_SHIFT;
228 spin_lock_irqsave(&card->priv->driver_lock, flags);
231 card->int_cause |= MRVDRV_CARDEVENT;
233 lbs_interrupt(card->priv);
235 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
240 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
245 static int if_sdio_card_to_host(struct if_sdio_card *card)
249 u16 size, type, chunk;
250 unsigned long timeout;
252 lbs_deb_enter(LBS_DEB_SDIO);
254 size = if_sdio_read_scratch(card, &ret);
259 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
265 timeout = jiffies + HZ;
267 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
270 if (status & IF_SDIO_IO_RDY)
272 if (time_after(jiffies, timeout)) {
280 * The transfer must be in one transaction or the firmware
284 if ((chunk > card->func->cur_blksize) || (chunk > 512)) {
285 chunk = (chunk + card->func->cur_blksize - 1) /
286 card->func->cur_blksize * card->func->cur_blksize;
289 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
293 chunk = card->buffer[0] | (card->buffer[1] << 8);
294 type = card->buffer[2] | (card->buffer[3] << 8);
296 lbs_deb_sdio("packet of type %d and size %d bytes\n",
297 (int)type, (int)chunk);
300 lbs_deb_sdio("packet fragment (%d > %d)\n",
301 (int)chunk, (int)size);
307 lbs_deb_sdio("packet fragment (%d < %d)\n",
308 (int)chunk, (int)size);
313 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
318 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
323 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
328 lbs_deb_sdio("invalid type (%d) from firmware\n",
336 lbs_pr_err("problem fetching packet from firmware\n");
338 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
343 static void if_sdio_host_to_card_worker(struct work_struct *work)
345 struct if_sdio_card *card;
346 struct if_sdio_packet *packet;
347 unsigned long timeout;
352 lbs_deb_enter(LBS_DEB_SDIO);
354 card = container_of(work, struct if_sdio_card, packet_worker);
357 spin_lock_irqsave(&card->lock, flags);
358 packet = card->packets;
360 card->packets = packet->next;
361 spin_unlock_irqrestore(&card->lock, flags);
366 sdio_claim_host(card->func);
368 timeout = jiffies + HZ;
370 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
373 if (status & IF_SDIO_IO_RDY)
375 if (time_after(jiffies, timeout)) {
382 ret = sdio_writesb(card->func, card->ioport,
383 packet->buffer, packet->nb);
387 sdio_release_host(card->func);
392 lbs_deb_leave(LBS_DEB_SDIO);
395 /********************************************************************/
397 /********************************************************************/
399 static int if_sdio_prog_helper(struct if_sdio_card *card)
403 const struct firmware *fw;
404 unsigned long timeout;
410 lbs_deb_enter(LBS_DEB_SDIO);
412 ret = request_firmware(&fw, card->helper, &card->func->dev);
414 lbs_pr_err("can't load helper firmware\n");
418 chunk_buffer = kzalloc(64, GFP_KERNEL);
424 sdio_claim_host(card->func);
426 ret = sdio_set_block_size(card->func, 32);
434 timeout = jiffies + HZ;
436 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
439 if ((status & IF_SDIO_IO_RDY) &&
440 (status & IF_SDIO_DL_RDY))
442 if (time_after(jiffies, timeout)) {
449 chunk_size = min(size, (size_t)60);
451 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
452 memcpy(chunk_buffer + 4, firmware, chunk_size);
454 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
456 ret = sdio_writesb(card->func, card->ioport,
461 firmware += chunk_size;
465 /* an empty block marks the end of the transfer */
466 memset(chunk_buffer, 0, 4);
467 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
471 lbs_deb_sdio("waiting for helper to boot...\n");
473 /* wait for the helper to boot by looking at the size register */
474 timeout = jiffies + HZ;
478 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
482 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
489 if (time_after(jiffies, timeout)) {
500 sdio_set_block_size(card->func, 0);
501 sdio_release_host(card->func);
504 release_firmware(fw);
508 lbs_pr_err("failed to load helper firmware\n");
510 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
515 static int if_sdio_prog_real(struct if_sdio_card *card)
519 const struct firmware *fw;
520 unsigned long timeout;
524 size_t size, req_size;
526 lbs_deb_enter(LBS_DEB_SDIO);
528 ret = request_firmware(&fw, card->firmware, &card->func->dev);
530 lbs_pr_err("can't load firmware\n");
534 chunk_buffer = kzalloc(512, GFP_KERNEL);
540 sdio_claim_host(card->func);
542 ret = sdio_set_block_size(card->func, 32);
550 timeout = jiffies + HZ;
552 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
555 if ((status & IF_SDIO_IO_RDY) &&
556 (status & IF_SDIO_DL_RDY))
558 if (time_after(jiffies, timeout)) {
565 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
569 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
573 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
576 lbs_deb_sdio("firmware helper gave up early\n");
581 if (req_size & 0x01) {
582 lbs_deb_sdio("firmware helper signalled error\n");
591 chunk_size = min(req_size, (size_t)512);
593 memcpy(chunk_buffer, firmware, chunk_size);
595 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
596 chunk_size, (chunk_size + 31) / 32 * 32);
598 ret = sdio_writesb(card->func, card->ioport,
599 chunk_buffer, (chunk_size + 31) / 32 * 32);
603 firmware += chunk_size;
605 req_size -= chunk_size;
611 lbs_deb_sdio("waiting for firmware to boot...\n");
613 /* wait for the firmware to boot */
614 timeout = jiffies + HZ;
618 scratch = if_sdio_read_scratch(card, &ret);
622 if (scratch == IF_SDIO_FIRMWARE_OK)
625 if (time_after(jiffies, timeout)) {
636 sdio_set_block_size(card->func, 0);
637 sdio_release_host(card->func);
640 release_firmware(fw);
644 lbs_pr_err("failed to load firmware\n");
646 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
651 static int if_sdio_prog_firmware(struct if_sdio_card *card)
656 lbs_deb_enter(LBS_DEB_SDIO);
658 sdio_claim_host(card->func);
659 scratch = if_sdio_read_scratch(card, &ret);
660 sdio_release_host(card->func);
665 if (scratch == IF_SDIO_FIRMWARE_OK) {
666 lbs_deb_sdio("firmware already loaded\n");
670 ret = if_sdio_prog_helper(card);
674 ret = if_sdio_prog_real(card);
682 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
687 /*******************************************************************/
688 /* Libertas callbacks */
689 /*******************************************************************/
691 static int if_sdio_host_to_card(struct lbs_private *priv,
692 u8 type, u8 *buf, u16 nb)
695 struct if_sdio_card *card;
696 struct if_sdio_packet *packet, *cur;
700 lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
704 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
710 * The transfer must be in one transaction or the firmware
714 if ((size > card->func->cur_blksize) || (size > 512)) {
715 size = (size + card->func->cur_blksize - 1) /
716 card->func->cur_blksize * card->func->cur_blksize;
719 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
730 * SDIO specific header.
732 packet->buffer[0] = (nb + 4) & 0xff;
733 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
734 packet->buffer[2] = type;
735 packet->buffer[3] = 0;
737 memcpy(packet->buffer + 4, buf, nb);
739 spin_lock_irqsave(&card->lock, flags);
742 card->packets = packet;
752 priv->dnld_sent = DNLD_CMD_SENT;
755 priv->dnld_sent = DNLD_DATA_SENT;
758 lbs_deb_sdio("unknown packet type %d\n", (int)type);
761 spin_unlock_irqrestore(&card->lock, flags);
763 schedule_work(&card->packet_worker);
768 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
773 static int if_sdio_get_int_status(struct lbs_private *priv, u8 *ireg)
775 struct if_sdio_card *card;
777 lbs_deb_enter(LBS_DEB_SDIO);
781 *ireg = card->int_cause;
784 lbs_deb_leave(LBS_DEB_SDIO);
789 static int if_sdio_read_event_cause(struct lbs_private *priv)
791 struct if_sdio_card *card;
793 lbs_deb_enter(LBS_DEB_SDIO);
797 priv->eventcause = card->event;
799 lbs_deb_leave(LBS_DEB_SDIO);
804 /*******************************************************************/
806 /*******************************************************************/
808 static void if_sdio_interrupt(struct sdio_func *func)
811 struct if_sdio_card *card;
814 lbs_deb_enter(LBS_DEB_SDIO);
816 card = sdio_get_drvdata(func);
818 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
822 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
824 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
829 * Ignore the define name, this really means the card has
830 * successfully received the command.
832 if (cause & IF_SDIO_H_INT_DNLD)
833 lbs_host_to_card_done(card->priv);
836 if (cause & IF_SDIO_H_INT_UPLD) {
837 ret = if_sdio_card_to_host(card);
845 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
848 static int if_sdio_probe(struct sdio_func *func,
849 const struct sdio_device_id *id)
851 struct if_sdio_card *card;
852 struct lbs_private *priv;
855 struct if_sdio_packet *packet;
857 lbs_deb_enter(LBS_DEB_SDIO);
859 for (i = 0;i < func->card->num_info;i++) {
860 if (sscanf(func->card->info[i],
861 "802.11 SDIO ID: %x", &model) == 1)
863 if (sscanf(func->card->info[i],
864 "ID: %x", &model) == 1)
866 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
872 if (i == func->card->num_info) {
873 lbs_pr_err("unable to identify card model\n");
877 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
883 spin_lock_init(&card->lock);
884 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
886 for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
887 if (card->model == if_sdio_models[i].model)
891 if (i == ARRAY_SIZE(if_sdio_models)) {
892 lbs_pr_err("unkown card model 0x%x\n", card->model);
897 card->helper = if_sdio_models[i].helper;
898 card->firmware = if_sdio_models[i].firmware;
900 if (lbs_helper_name) {
901 lbs_deb_sdio("overriding helper firmware: %s\n",
903 card->helper = lbs_helper_name;
907 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
908 card->firmware = lbs_fw_name;
911 sdio_claim_host(func);
913 ret = sdio_enable_func(func);
917 ret = sdio_claim_irq(func, if_sdio_interrupt);
921 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
925 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
929 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
933 sdio_release_host(func);
935 sdio_set_drvdata(func, card);
937 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
938 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
939 func->class, func->vendor, func->device,
940 model, (unsigned)card->ioport);
942 ret = if_sdio_prog_firmware(card);
946 priv = lbs_add_card(card, &func->dev);
955 priv->hw_host_to_card = if_sdio_host_to_card;
956 priv->hw_get_int_status = if_sdio_get_int_status;
957 priv->hw_read_event_cause = if_sdio_read_event_cause;
962 * Enable interrupts now that everything is set up
964 sdio_claim_host(func);
965 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
966 sdio_release_host(func);
970 ret = lbs_start_card(priv);
972 goto err_activate_card;
975 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
980 flush_scheduled_work();
981 free_netdev(priv->dev);
984 sdio_claim_host(func);
986 sdio_release_irq(func);
988 sdio_disable_func(func);
990 sdio_release_host(func);
992 while (card->packets) {
993 packet = card->packets;
994 card->packets = card->packets->next;
1003 static void if_sdio_remove(struct sdio_func *func)
1005 struct if_sdio_card *card;
1006 struct if_sdio_packet *packet;
1008 lbs_deb_enter(LBS_DEB_SDIO);
1010 card = sdio_get_drvdata(func);
1012 card->priv->surpriseremoved = 1;
1014 lbs_deb_sdio("call remove card\n");
1015 lbs_stop_card(card->priv);
1016 lbs_remove_card(card->priv);
1018 flush_scheduled_work();
1020 sdio_claim_host(func);
1021 sdio_release_irq(func);
1022 sdio_disable_func(func);
1023 sdio_release_host(func);
1025 while (card->packets) {
1026 packet = card->packets;
1027 card->packets = card->packets->next;
1033 lbs_deb_leave(LBS_DEB_SDIO);
1036 static struct sdio_driver if_sdio_driver = {
1037 .name = "libertas_sdio",
1038 .id_table = if_sdio_ids,
1039 .probe = if_sdio_probe,
1040 .remove = if_sdio_remove,
1043 /*******************************************************************/
1044 /* Module functions */
1045 /*******************************************************************/
1047 static int __init if_sdio_init_module(void)
1051 lbs_deb_enter(LBS_DEB_SDIO);
1053 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1054 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1056 ret = sdio_register_driver(&if_sdio_driver);
1058 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1063 static void __exit if_sdio_exit_module(void)
1065 lbs_deb_enter(LBS_DEB_SDIO);
1067 sdio_unregister_driver(&if_sdio_driver);
1069 lbs_deb_leave(LBS_DEB_SDIO);
1072 module_init(if_sdio_init_module);
1073 module_exit(if_sdio_exit_module);
1075 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1076 MODULE_AUTHOR("Pierre Ossman");
1077 MODULE_LICENSE("GPL");