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 (!card
->priv
->cur_cmd
) {
140 lbs_deb_sdio("discarding spurious response\n");
145 if (size
> LBS_CMD_BUFFER_SIZE
) {
146 lbs_deb_sdio("response packet too large (%d bytes)\n",
152 memcpy(card
->priv
->cur_cmd
->cmdbuf
, buffer
, size
);
153 card
->priv
->upld_len
= size
;
155 card
->int_cause
|= MRVDRV_CMD_UPLD_RDY
;
157 lbs_interrupt(card
->priv
);
162 spin_unlock_irqrestore(&card
->priv
->driver_lock
, flags
);
164 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
169 static int if_sdio_handle_data(struct if_sdio_card
*card
,
170 u8
*buffer
, unsigned size
)
176 lbs_deb_enter(LBS_DEB_SDIO
);
178 if (size
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
) {
179 lbs_deb_sdio("response packet too large (%d bytes)\n",
185 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ NET_IP_ALIGN
);
191 skb_reserve(skb
, NET_IP_ALIGN
);
193 data
= skb_put(skb
, size
);
195 memcpy(data
, buffer
, size
);
197 lbs_process_rxed_packet(card
->priv
, skb
);
202 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
207 static int if_sdio_handle_event(struct if_sdio_card
*card
,
208 u8
*buffer
, unsigned size
)
214 lbs_deb_enter(LBS_DEB_SDIO
);
216 if (card
->model
== 0x04) {
217 event
= sdio_readb(card
->func
, IF_SDIO_EVENT
, &ret
);
222 lbs_deb_sdio("event packet too small (%d bytes)\n",
227 event
= buffer
[3] << 24;
228 event
|= buffer
[2] << 16;
229 event
|= buffer
[1] << 8;
230 event
|= buffer
[0] << 0;
231 event
<<= SBI_EVENT_CAUSE_SHIFT
;
234 spin_lock_irqsave(&card
->priv
->driver_lock
, flags
);
237 card
->int_cause
|= MRVDRV_CARDEVENT
;
239 lbs_interrupt(card
->priv
);
241 spin_unlock_irqrestore(&card
->priv
->driver_lock
, flags
);
246 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
251 static int if_sdio_card_to_host(struct if_sdio_card
*card
)
255 u16 size
, type
, chunk
;
256 unsigned long timeout
;
258 lbs_deb_enter(LBS_DEB_SDIO
);
260 size
= if_sdio_read_scratch(card
, &ret
);
265 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
271 timeout
= jiffies
+ HZ
;
273 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
276 if (status
& IF_SDIO_IO_RDY
)
278 if (time_after(jiffies
, timeout
)) {
286 * The transfer must be in one transaction or the firmware
290 if ((chunk
> card
->func
->cur_blksize
) || (chunk
> 512)) {
291 chunk
= (chunk
+ card
->func
->cur_blksize
- 1) /
292 card
->func
->cur_blksize
* card
->func
->cur_blksize
;
295 ret
= sdio_readsb(card
->func
, card
->buffer
, card
->ioport
, chunk
);
299 chunk
= card
->buffer
[0] | (card
->buffer
[1] << 8);
300 type
= card
->buffer
[2] | (card
->buffer
[3] << 8);
302 lbs_deb_sdio("packet of type %d and size %d bytes\n",
303 (int)type
, (int)chunk
);
306 lbs_deb_sdio("packet fragment (%d > %d)\n",
307 (int)chunk
, (int)size
);
313 lbs_deb_sdio("packet fragment (%d < %d)\n",
314 (int)chunk
, (int)size
);
319 ret
= if_sdio_handle_cmd(card
, card
->buffer
+ 4, chunk
- 4);
324 ret
= if_sdio_handle_data(card
, card
->buffer
+ 4, chunk
- 4);
329 ret
= if_sdio_handle_event(card
, card
->buffer
+ 4, chunk
- 4);
334 lbs_deb_sdio("invalid type (%d) from firmware\n",
342 lbs_pr_err("problem fetching packet from firmware\n");
344 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
349 static void if_sdio_host_to_card_worker(struct work_struct
*work
)
351 struct if_sdio_card
*card
;
352 struct if_sdio_packet
*packet
;
353 unsigned long timeout
;
358 lbs_deb_enter(LBS_DEB_SDIO
);
360 card
= container_of(work
, struct if_sdio_card
, packet_worker
);
363 spin_lock_irqsave(&card
->lock
, flags
);
364 packet
= card
->packets
;
366 card
->packets
= packet
->next
;
367 spin_unlock_irqrestore(&card
->lock
, flags
);
372 sdio_claim_host(card
->func
);
374 timeout
= jiffies
+ HZ
;
376 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
379 if (status
& IF_SDIO_IO_RDY
)
381 if (time_after(jiffies
, timeout
)) {
388 ret
= sdio_writesb(card
->func
, card
->ioport
,
389 packet
->buffer
, packet
->nb
);
393 sdio_release_host(card
->func
);
398 lbs_deb_leave(LBS_DEB_SDIO
);
401 /********************************************************************/
403 /********************************************************************/
405 static int if_sdio_prog_helper(struct if_sdio_card
*card
)
409 const struct firmware
*fw
;
410 unsigned long timeout
;
416 lbs_deb_enter(LBS_DEB_SDIO
);
418 ret
= request_firmware(&fw
, card
->helper
, &card
->func
->dev
);
420 lbs_pr_err("can't load helper firmware\n");
424 chunk_buffer
= kzalloc(64, GFP_KERNEL
);
430 sdio_claim_host(card
->func
);
432 ret
= sdio_set_block_size(card
->func
, 32);
440 timeout
= jiffies
+ HZ
;
442 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
445 if ((status
& IF_SDIO_IO_RDY
) &&
446 (status
& IF_SDIO_DL_RDY
))
448 if (time_after(jiffies
, timeout
)) {
455 chunk_size
= min(size
, (size_t)60);
457 *((__le32
*)chunk_buffer
) = cpu_to_le32(chunk_size
);
458 memcpy(chunk_buffer
+ 4, firmware
, chunk_size
);
460 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
462 ret
= sdio_writesb(card
->func
, card
->ioport
,
467 firmware
+= chunk_size
;
471 /* an empty block marks the end of the transfer */
472 memset(chunk_buffer
, 0, 4);
473 ret
= sdio_writesb(card
->func
, card
->ioport
, chunk_buffer
, 64);
477 lbs_deb_sdio("waiting for helper to boot...\n");
479 /* wait for the helper to boot by looking at the size register */
480 timeout
= jiffies
+ HZ
;
484 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
488 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
495 if (time_after(jiffies
, timeout
)) {
506 sdio_set_block_size(card
->func
, 0);
507 sdio_release_host(card
->func
);
510 release_firmware(fw
);
514 lbs_pr_err("failed to load helper firmware\n");
516 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
521 static int if_sdio_prog_real(struct if_sdio_card
*card
)
525 const struct firmware
*fw
;
526 unsigned long timeout
;
530 size_t size
, req_size
;
532 lbs_deb_enter(LBS_DEB_SDIO
);
534 ret
= request_firmware(&fw
, card
->firmware
, &card
->func
->dev
);
536 lbs_pr_err("can't load firmware\n");
540 chunk_buffer
= kzalloc(512, GFP_KERNEL
);
546 sdio_claim_host(card
->func
);
548 ret
= sdio_set_block_size(card
->func
, 32);
556 timeout
= jiffies
+ HZ
;
558 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
561 if ((status
& IF_SDIO_IO_RDY
) &&
562 (status
& IF_SDIO_DL_RDY
))
564 if (time_after(jiffies
, timeout
)) {
571 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
575 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
579 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
582 lbs_deb_sdio("firmware helper gave up early\n");
587 if (req_size
& 0x01) {
588 lbs_deb_sdio("firmware helper signalled error\n");
597 chunk_size
= min(req_size
, (size_t)512);
599 memcpy(chunk_buffer
, firmware
, chunk_size
);
601 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
602 chunk_size, (chunk_size + 31) / 32 * 32);
604 ret
= sdio_writesb(card
->func
, card
->ioport
,
605 chunk_buffer
, (chunk_size
+ 31) / 32 * 32);
609 firmware
+= chunk_size
;
611 req_size
-= chunk_size
;
617 lbs_deb_sdio("waiting for firmware to boot...\n");
619 /* wait for the firmware to boot */
620 timeout
= jiffies
+ HZ
;
624 scratch
= if_sdio_read_scratch(card
, &ret
);
628 if (scratch
== IF_SDIO_FIRMWARE_OK
)
631 if (time_after(jiffies
, timeout
)) {
642 sdio_set_block_size(card
->func
, 0);
643 sdio_release_host(card
->func
);
646 release_firmware(fw
);
650 lbs_pr_err("failed to load firmware\n");
652 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
657 static int if_sdio_prog_firmware(struct if_sdio_card
*card
)
662 lbs_deb_enter(LBS_DEB_SDIO
);
664 sdio_claim_host(card
->func
);
665 scratch
= if_sdio_read_scratch(card
, &ret
);
666 sdio_release_host(card
->func
);
671 if (scratch
== IF_SDIO_FIRMWARE_OK
) {
672 lbs_deb_sdio("firmware already loaded\n");
676 ret
= if_sdio_prog_helper(card
);
680 ret
= if_sdio_prog_real(card
);
688 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
693 /*******************************************************************/
694 /* Libertas callbacks */
695 /*******************************************************************/
697 static int if_sdio_host_to_card(struct lbs_private
*priv
,
698 u8 type
, u8
*buf
, u16 nb
)
701 struct if_sdio_card
*card
;
702 struct if_sdio_packet
*packet
, *cur
;
706 lbs_deb_enter_args(LBS_DEB_SDIO
, "type %d, bytes %d", type
, nb
);
710 if (nb
> (65536 - sizeof(struct if_sdio_packet
) - 4)) {
716 * The transfer must be in one transaction or the firmware
720 if ((size
> card
->func
->cur_blksize
) || (size
> 512)) {
721 size
= (size
+ card
->func
->cur_blksize
- 1) /
722 card
->func
->cur_blksize
* card
->func
->cur_blksize
;
725 packet
= kzalloc(sizeof(struct if_sdio_packet
) + size
,
736 * SDIO specific header.
738 packet
->buffer
[0] = (nb
+ 4) & 0xff;
739 packet
->buffer
[1] = ((nb
+ 4) >> 8) & 0xff;
740 packet
->buffer
[2] = type
;
741 packet
->buffer
[3] = 0;
743 memcpy(packet
->buffer
+ 4, buf
, nb
);
745 spin_lock_irqsave(&card
->lock
, flags
);
748 card
->packets
= packet
;
758 priv
->dnld_sent
= DNLD_CMD_SENT
;
761 priv
->dnld_sent
= DNLD_DATA_SENT
;
764 lbs_deb_sdio("unknown packet type %d\n", (int)type
);
767 spin_unlock_irqrestore(&card
->lock
, flags
);
769 schedule_work(&card
->packet_worker
);
774 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
779 static int if_sdio_get_int_status(struct lbs_private
*priv
, u8
*ireg
)
781 struct if_sdio_card
*card
;
783 lbs_deb_enter(LBS_DEB_SDIO
);
787 *ireg
= card
->int_cause
;
790 lbs_deb_leave(LBS_DEB_SDIO
);
795 static int if_sdio_read_event_cause(struct lbs_private
*priv
)
797 struct if_sdio_card
*card
;
799 lbs_deb_enter(LBS_DEB_SDIO
);
803 priv
->eventcause
= card
->event
;
805 lbs_deb_leave(LBS_DEB_SDIO
);
810 /*******************************************************************/
812 /*******************************************************************/
814 static void if_sdio_interrupt(struct sdio_func
*func
)
817 struct if_sdio_card
*card
;
820 lbs_deb_enter(LBS_DEB_SDIO
);
822 card
= sdio_get_drvdata(func
);
824 cause
= sdio_readb(card
->func
, IF_SDIO_H_INT_STATUS
, &ret
);
828 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause
);
830 sdio_writeb(card
->func
, ~cause
, IF_SDIO_H_INT_STATUS
, &ret
);
835 * Ignore the define name, this really means the card has
836 * successfully received the command.
838 if (cause
& IF_SDIO_H_INT_DNLD
)
839 lbs_host_to_card_done(card
->priv
);
842 if (cause
& IF_SDIO_H_INT_UPLD
) {
843 ret
= if_sdio_card_to_host(card
);
851 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
854 static int if_sdio_probe(struct sdio_func
*func
,
855 const struct sdio_device_id
*id
)
857 struct if_sdio_card
*card
;
858 struct lbs_private
*priv
;
861 struct if_sdio_packet
*packet
;
863 lbs_deb_enter(LBS_DEB_SDIO
);
865 for (i
= 0;i
< func
->card
->num_info
;i
++) {
866 if (sscanf(func
->card
->info
[i
],
867 "802.11 SDIO ID: %x", &model
) == 1)
869 if (sscanf(func
->card
->info
[i
],
870 "ID: %x", &model
) == 1)
874 if (i
== func
->card
->num_info
) {
875 lbs_pr_err("unable to identify card model\n");
879 card
= kzalloc(sizeof(struct if_sdio_card
), GFP_KERNEL
);
885 spin_lock_init(&card
->lock
);
886 INIT_WORK(&card
->packet_worker
, if_sdio_host_to_card_worker
);
888 for (i
= 0;i
< ARRAY_SIZE(if_sdio_models
);i
++) {
889 if (card
->model
== if_sdio_models
[i
].model
)
893 if (i
== ARRAY_SIZE(if_sdio_models
)) {
894 lbs_pr_err("unkown card model 0x%x\n", card
->model
);
899 card
->helper
= if_sdio_models
[i
].helper
;
900 card
->firmware
= if_sdio_models
[i
].firmware
;
902 if (lbs_helper_name
) {
903 lbs_deb_sdio("overriding helper firmware: %s\n",
905 card
->helper
= lbs_helper_name
;
909 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name
);
910 card
->firmware
= lbs_fw_name
;
913 sdio_claim_host(func
);
915 ret
= sdio_enable_func(func
);
919 ret
= sdio_claim_irq(func
, if_sdio_interrupt
);
923 card
->ioport
= sdio_readb(func
, IF_SDIO_IOPORT
, &ret
);
927 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 1, &ret
) << 8;
931 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 2, &ret
) << 16;
935 sdio_release_host(func
);
937 sdio_set_drvdata(func
, card
);
939 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
940 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
941 func
->class, func
->vendor
, func
->device
,
942 model
, (unsigned)card
->ioport
);
944 ret
= if_sdio_prog_firmware(card
);
948 priv
= lbs_add_card(card
, &func
->dev
);
957 priv
->hw_host_to_card
= if_sdio_host_to_card
;
958 priv
->hw_get_int_status
= if_sdio_get_int_status
;
959 priv
->hw_read_event_cause
= if_sdio_read_event_cause
;
964 * Enable interrupts now that everything is set up
966 sdio_claim_host(func
);
967 sdio_writeb(func
, 0x0f, IF_SDIO_H_INT_MASK
, &ret
);
968 sdio_release_host(func
);
972 ret
= lbs_start_card(priv
);
974 goto err_activate_card
;
977 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
982 flush_scheduled_work();
983 free_netdev(priv
->dev
);
986 sdio_claim_host(func
);
988 sdio_release_irq(func
);
990 sdio_disable_func(func
);
992 sdio_release_host(func
);
994 while (card
->packets
) {
995 packet
= card
->packets
;
996 card
->packets
= card
->packets
->next
;
1005 static void if_sdio_remove(struct sdio_func
*func
)
1007 struct if_sdio_card
*card
;
1008 struct if_sdio_packet
*packet
;
1010 lbs_deb_enter(LBS_DEB_SDIO
);
1012 card
= sdio_get_drvdata(func
);
1014 card
->priv
->surpriseremoved
= 1;
1016 lbs_deb_sdio("call remove card\n");
1017 lbs_stop_card(card
->priv
);
1018 lbs_remove_card(card
->priv
);
1020 flush_scheduled_work();
1022 sdio_claim_host(func
);
1023 sdio_release_irq(func
);
1024 sdio_disable_func(func
);
1025 sdio_release_host(func
);
1027 while (card
->packets
) {
1028 packet
= card
->packets
;
1029 card
->packets
= card
->packets
->next
;
1035 lbs_deb_leave(LBS_DEB_SDIO
);
1038 static struct sdio_driver if_sdio_driver
= {
1039 .name
= "libertas_sdio",
1040 .id_table
= if_sdio_ids
,
1041 .probe
= if_sdio_probe
,
1042 .remove
= if_sdio_remove
,
1045 /*******************************************************************/
1046 /* Module functions */
1047 /*******************************************************************/
1049 static int __init
if_sdio_init_module(void)
1053 lbs_deb_enter(LBS_DEB_SDIO
);
1055 printk(KERN_INFO
"libertas_sdio: Libertas SDIO driver\n");
1056 printk(KERN_INFO
"libertas_sdio: Copyright Pierre Ossman\n");
1058 ret
= sdio_register_driver(&if_sdio_driver
);
1060 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1065 static void __exit
if_sdio_exit_module(void)
1067 lbs_deb_enter(LBS_DEB_SDIO
);
1069 sdio_unregister_driver(&if_sdio_driver
);
1071 lbs_deb_leave(LBS_DEB_SDIO
);
1074 module_init(if_sdio_init_module
);
1075 module_exit(if_sdio_exit_module
);
1077 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1078 MODULE_AUTHOR("Pierre Ossman");
1079 MODULE_LICENSE("GPL");