2 * linux/drivers/net/wireless/libertas/if_sdio.c
4 * Copyright 2007-2008 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;
96 struct if_sdio_packet *packets;
97 struct work_struct packet_worker;
100 /********************************************************************/
102 /********************************************************************/
104 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
109 if (card->model == 0x04)
110 reg = IF_SDIO_SCRATCH_OLD;
112 reg = IF_SDIO_SCRATCH;
114 scratch = sdio_readb(card->func, reg, &ret);
116 scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;
127 static int if_sdio_handle_cmd(struct if_sdio_card *card,
128 u8 *buffer, unsigned size)
130 struct lbs_private *priv = card->priv;
135 lbs_deb_enter(LBS_DEB_SDIO);
137 if (size > LBS_CMD_BUFFER_SIZE) {
138 lbs_deb_sdio("response packet too large (%d bytes)\n",
144 spin_lock_irqsave(&priv->driver_lock, flags);
146 i = (priv->resp_idx == 0) ? 1 : 0;
147 BUG_ON(priv->resp_len[i]);
148 priv->resp_len[i] = size;
149 memcpy(priv->resp_buf[i], buffer, size);
150 lbs_notify_command_response(priv, i);
152 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
157 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
161 static int if_sdio_handle_data(struct if_sdio_card *card,
162 u8 *buffer, unsigned size)
168 lbs_deb_enter(LBS_DEB_SDIO);
170 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
171 lbs_deb_sdio("response packet too large (%d bytes)\n",
177 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
183 skb_reserve(skb, NET_IP_ALIGN);
185 data = skb_put(skb, size);
187 memcpy(data, buffer, size);
189 lbs_process_rxed_packet(card->priv, skb);
194 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
199 static int if_sdio_handle_event(struct if_sdio_card *card,
200 u8 *buffer, unsigned size)
205 lbs_deb_enter(LBS_DEB_SDIO);
207 if (card->model == 0x04) {
208 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
213 lbs_deb_sdio("event packet too small (%d bytes)\n",
218 event = buffer[3] << 24;
219 event |= buffer[2] << 16;
220 event |= buffer[1] << 8;
221 event |= buffer[0] << 0;
224 lbs_queue_event(card->priv, event & 0xFF);
228 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
233 static int if_sdio_card_to_host(struct if_sdio_card *card)
237 u16 size, type, chunk;
238 unsigned long timeout;
240 lbs_deb_enter(LBS_DEB_SDIO);
242 size = if_sdio_read_scratch(card, &ret);
247 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
253 timeout = jiffies + HZ;
255 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
258 if (status & IF_SDIO_IO_RDY)
260 if (time_after(jiffies, timeout)) {
268 * The transfer must be in one transaction or the firmware
269 * goes suicidal. There's no way to guarantee that for all
270 * controllers, but we can at least try.
272 chunk = sdio_align_size(card->func, size);
274 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
278 chunk = card->buffer[0] | (card->buffer[1] << 8);
279 type = card->buffer[2] | (card->buffer[3] << 8);
281 lbs_deb_sdio("packet of type %d and size %d bytes\n",
282 (int)type, (int)chunk);
285 lbs_deb_sdio("packet fragment (%d > %d)\n",
286 (int)chunk, (int)size);
292 lbs_deb_sdio("packet fragment (%d < %d)\n",
293 (int)chunk, (int)size);
298 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
303 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
308 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
313 lbs_deb_sdio("invalid type (%d) from firmware\n",
321 lbs_pr_err("problem fetching packet from firmware\n");
323 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
328 static void if_sdio_host_to_card_worker(struct work_struct *work)
330 struct if_sdio_card *card;
331 struct if_sdio_packet *packet;
332 unsigned long timeout;
337 lbs_deb_enter(LBS_DEB_SDIO);
339 card = container_of(work, struct if_sdio_card, packet_worker);
342 spin_lock_irqsave(&card->lock, flags);
343 packet = card->packets;
345 card->packets = packet->next;
346 spin_unlock_irqrestore(&card->lock, flags);
351 sdio_claim_host(card->func);
353 timeout = jiffies + HZ;
355 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
358 if (status & IF_SDIO_IO_RDY)
360 if (time_after(jiffies, timeout)) {
367 ret = sdio_writesb(card->func, card->ioport,
368 packet->buffer, packet->nb);
372 sdio_release_host(card->func);
377 lbs_deb_leave(LBS_DEB_SDIO);
380 /********************************************************************/
382 /********************************************************************/
384 static int if_sdio_prog_helper(struct if_sdio_card *card)
388 const struct firmware *fw;
389 unsigned long timeout;
395 lbs_deb_enter(LBS_DEB_SDIO);
397 ret = request_firmware(&fw, card->helper, &card->func->dev);
399 lbs_pr_err("can't load helper firmware\n");
403 chunk_buffer = kzalloc(64, GFP_KERNEL);
409 sdio_claim_host(card->func);
411 ret = sdio_set_block_size(card->func, 32);
419 timeout = jiffies + HZ;
421 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
424 if ((status & IF_SDIO_IO_RDY) &&
425 (status & IF_SDIO_DL_RDY))
427 if (time_after(jiffies, timeout)) {
434 chunk_size = min(size, (size_t)60);
436 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
437 memcpy(chunk_buffer + 4, firmware, chunk_size);
439 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
441 ret = sdio_writesb(card->func, card->ioport,
446 firmware += chunk_size;
450 /* an empty block marks the end of the transfer */
451 memset(chunk_buffer, 0, 4);
452 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
456 lbs_deb_sdio("waiting for helper to boot...\n");
458 /* wait for the helper to boot by looking at the size register */
459 timeout = jiffies + HZ;
463 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
467 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
474 if (time_after(jiffies, timeout)) {
485 sdio_set_block_size(card->func, 0);
486 sdio_release_host(card->func);
489 release_firmware(fw);
493 lbs_pr_err("failed to load helper firmware\n");
495 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
500 static int if_sdio_prog_real(struct if_sdio_card *card)
504 const struct firmware *fw;
505 unsigned long timeout;
509 size_t size, req_size;
511 lbs_deb_enter(LBS_DEB_SDIO);
513 ret = request_firmware(&fw, card->firmware, &card->func->dev);
515 lbs_pr_err("can't load firmware\n");
519 chunk_buffer = kzalloc(512, GFP_KERNEL);
525 sdio_claim_host(card->func);
527 ret = sdio_set_block_size(card->func, 32);
535 timeout = jiffies + HZ;
537 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
540 if ((status & IF_SDIO_IO_RDY) &&
541 (status & IF_SDIO_DL_RDY))
543 if (time_after(jiffies, timeout)) {
550 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
554 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
558 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
561 lbs_deb_sdio("firmware helper gave up early\n");
566 if (req_size & 0x01) {
567 lbs_deb_sdio("firmware helper signalled error\n");
576 chunk_size = min(req_size, (size_t)512);
578 memcpy(chunk_buffer, firmware, chunk_size);
580 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
581 chunk_size, (chunk_size + 31) / 32 * 32);
583 ret = sdio_writesb(card->func, card->ioport,
584 chunk_buffer, (chunk_size + 31) / 32 * 32);
588 firmware += chunk_size;
590 req_size -= chunk_size;
596 lbs_deb_sdio("waiting for firmware to boot...\n");
598 /* wait for the firmware to boot */
599 timeout = jiffies + HZ;
603 scratch = if_sdio_read_scratch(card, &ret);
607 if (scratch == IF_SDIO_FIRMWARE_OK)
610 if (time_after(jiffies, timeout)) {
621 sdio_set_block_size(card->func, 0);
622 sdio_release_host(card->func);
625 release_firmware(fw);
629 lbs_pr_err("failed to load firmware\n");
631 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
636 static int if_sdio_prog_firmware(struct if_sdio_card *card)
641 lbs_deb_enter(LBS_DEB_SDIO);
643 sdio_claim_host(card->func);
644 scratch = if_sdio_read_scratch(card, &ret);
645 sdio_release_host(card->func);
650 if (scratch == IF_SDIO_FIRMWARE_OK) {
651 lbs_deb_sdio("firmware already loaded\n");
655 ret = if_sdio_prog_helper(card);
659 ret = if_sdio_prog_real(card);
667 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
672 /*******************************************************************/
673 /* Libertas callbacks */
674 /*******************************************************************/
676 static int if_sdio_host_to_card(struct lbs_private *priv,
677 u8 type, u8 *buf, u16 nb)
680 struct if_sdio_card *card;
681 struct if_sdio_packet *packet, *cur;
685 lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
689 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
695 * The transfer must be in one transaction or the firmware
696 * goes suicidal. There's no way to guarantee that for all
697 * controllers, but we can at least try.
699 size = sdio_align_size(card->func, nb + 4);
701 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
712 * SDIO specific header.
714 packet->buffer[0] = (nb + 4) & 0xff;
715 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
716 packet->buffer[2] = type;
717 packet->buffer[3] = 0;
719 memcpy(packet->buffer + 4, buf, nb);
721 spin_lock_irqsave(&card->lock, flags);
724 card->packets = packet;
734 priv->dnld_sent = DNLD_CMD_SENT;
737 priv->dnld_sent = DNLD_DATA_SENT;
740 lbs_deb_sdio("unknown packet type %d\n", (int)type);
743 spin_unlock_irqrestore(&card->lock, flags);
745 schedule_work(&card->packet_worker);
750 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
755 /*******************************************************************/
757 /*******************************************************************/
759 static void if_sdio_interrupt(struct sdio_func *func)
762 struct if_sdio_card *card;
765 lbs_deb_enter(LBS_DEB_SDIO);
767 card = sdio_get_drvdata(func);
769 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
773 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
775 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
780 * Ignore the define name, this really means the card has
781 * successfully received the command.
783 if (cause & IF_SDIO_H_INT_DNLD)
784 lbs_host_to_card_done(card->priv);
787 if (cause & IF_SDIO_H_INT_UPLD) {
788 ret = if_sdio_card_to_host(card);
796 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
799 static int if_sdio_probe(struct sdio_func *func,
800 const struct sdio_device_id *id)
802 struct if_sdio_card *card;
803 struct lbs_private *priv;
806 struct if_sdio_packet *packet;
808 lbs_deb_enter(LBS_DEB_SDIO);
810 for (i = 0;i < func->card->num_info;i++) {
811 if (sscanf(func->card->info[i],
812 "802.11 SDIO ID: %x", &model) == 1)
814 if (sscanf(func->card->info[i],
815 "ID: %x", &model) == 1)
817 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
823 if (i == func->card->num_info) {
824 lbs_pr_err("unable to identify card model\n");
828 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
834 spin_lock_init(&card->lock);
835 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
837 for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
838 if (card->model == if_sdio_models[i].model)
842 if (i == ARRAY_SIZE(if_sdio_models)) {
843 lbs_pr_err("unkown card model 0x%x\n", card->model);
848 card->helper = if_sdio_models[i].helper;
849 card->firmware = if_sdio_models[i].firmware;
851 if (lbs_helper_name) {
852 lbs_deb_sdio("overriding helper firmware: %s\n",
854 card->helper = lbs_helper_name;
858 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
859 card->firmware = lbs_fw_name;
862 sdio_claim_host(func);
864 ret = sdio_enable_func(func);
868 ret = sdio_claim_irq(func, if_sdio_interrupt);
872 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
876 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
880 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
884 sdio_release_host(func);
886 sdio_set_drvdata(func, card);
888 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
889 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
890 func->class, func->vendor, func->device,
891 model, (unsigned)card->ioport);
893 ret = if_sdio_prog_firmware(card);
897 priv = lbs_add_card(card, &func->dev);
906 priv->hw_host_to_card = if_sdio_host_to_card;
911 * Enable interrupts now that everything is set up
913 sdio_claim_host(func);
914 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
915 sdio_release_host(func);
919 ret = lbs_start_card(priv);
921 goto err_activate_card;
924 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
929 flush_scheduled_work();
930 free_netdev(priv->dev);
933 sdio_claim_host(func);
935 sdio_release_irq(func);
937 sdio_disable_func(func);
939 sdio_release_host(func);
941 while (card->packets) {
942 packet = card->packets;
943 card->packets = card->packets->next;
952 static void if_sdio_remove(struct sdio_func *func)
954 struct if_sdio_card *card;
955 struct if_sdio_packet *packet;
957 lbs_deb_enter(LBS_DEB_SDIO);
959 card = sdio_get_drvdata(func);
961 card->priv->surpriseremoved = 1;
963 lbs_deb_sdio("call remove card\n");
964 lbs_stop_card(card->priv);
965 lbs_remove_card(card->priv);
967 flush_scheduled_work();
969 sdio_claim_host(func);
970 sdio_release_irq(func);
971 sdio_disable_func(func);
972 sdio_release_host(func);
974 while (card->packets) {
975 packet = card->packets;
976 card->packets = card->packets->next;
982 lbs_deb_leave(LBS_DEB_SDIO);
985 static struct sdio_driver if_sdio_driver = {
986 .name = "libertas_sdio",
987 .id_table = if_sdio_ids,
988 .probe = if_sdio_probe,
989 .remove = if_sdio_remove,
992 /*******************************************************************/
993 /* Module functions */
994 /*******************************************************************/
996 static int __init if_sdio_init_module(void)
1000 lbs_deb_enter(LBS_DEB_SDIO);
1002 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1003 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1005 ret = sdio_register_driver(&if_sdio_driver);
1007 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1012 static void __exit if_sdio_exit_module(void)
1014 lbs_deb_enter(LBS_DEB_SDIO);
1016 sdio_unregister_driver(&if_sdio_driver);
1018 lbs_deb_leave(LBS_DEB_SDIO);
1021 module_init(if_sdio_init_module);
1022 module_exit(if_sdio_exit_module);
1024 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1025 MODULE_AUTHOR("Pierre Ossman");
1026 MODULE_LICENSE("GPL");