2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
18 #include <linux/mmc/core.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
24 #include "gdm_wimax.h"
25 #include "sdio_boot.h"
28 #define TYPE_A_HEADER_SIZE 4
29 #define TYPE_A_LOOKAHEAD_SIZE 16
31 #define MAX_NR_RX_BUF 4
33 #define SDU_TX_BUF_SIZE 2048
34 #define TX_BUF_SIZE 2048
35 #define TX_CHUNK_SIZE (2048 - TYPE_A_HEADER_SIZE)
36 #define RX_BUF_SIZE (25*1024)
39 #define TX_INTERVAL (1000000/TX_HZ)
43 static int init_sdio(struct sdiowm_dev
*sdev
);
44 static void release_sdio(struct sdiowm_dev
*sdev
);
47 static void hexdump(char *title
, u8
*data
, int len
)
51 printk(KERN_DEBUG
"%s: length = %d\n", title
, len
);
52 for (i
= 0; i
< len
; i
++) {
53 printk(KERN_DEBUG
"%02x ", data
[i
]);
55 printk(KERN_DEBUG
"\n");
57 printk(KERN_DEBUG
"\n");
61 static struct sdio_tx
*alloc_tx_struct(struct tx_cxt
*tx
)
63 struct sdio_tx
*t
= kzalloc(sizeof(*t
), GFP_ATOMIC
);
68 t
->buf
= kmalloc(TX_BUF_SIZE
, GFP_ATOMIC
);
79 static void free_tx_struct(struct sdio_tx
*t
)
87 static struct sdio_rx
*alloc_rx_struct(struct rx_cxt
*rx
)
89 struct sdio_rx
*r
= kzalloc(sizeof(*r
), GFP_ATOMIC
);
97 static void free_rx_struct(struct sdio_rx
*r
)
102 /* Before this function is called, spin lock should be locked. */
103 static struct sdio_tx
*get_tx_struct(struct tx_cxt
*tx
, int *no_spc
)
107 if (list_empty(&tx
->free_list
))
110 t
= list_entry(tx
->free_list
.prev
, struct sdio_tx
, list
);
113 *no_spc
= list_empty(&tx
->free_list
) ? 1 : 0;
118 /* Before this function is called, spin lock should be locked. */
119 static void put_tx_struct(struct tx_cxt
*tx
, struct sdio_tx
*t
)
121 list_add_tail(&t
->list
, &tx
->free_list
);
124 /* Before this function is called, spin lock should be locked. */
125 static struct sdio_rx
*get_rx_struct(struct rx_cxt
*rx
)
129 if (list_empty(&rx
->free_list
))
132 r
= list_entry(rx
->free_list
.prev
, struct sdio_rx
, list
);
138 /* Before this function is called, spin lock should be locked. */
139 static void put_rx_struct(struct rx_cxt
*rx
, struct sdio_rx
*r
)
141 list_add_tail(&r
->list
, &rx
->free_list
);
144 static int init_sdio(struct sdiowm_dev
*sdev
)
147 struct tx_cxt
*tx
= &sdev
->tx
;
148 struct rx_cxt
*rx
= &sdev
->rx
;
152 INIT_LIST_HEAD(&tx
->free_list
);
153 INIT_LIST_HEAD(&tx
->sdu_list
);
154 INIT_LIST_HEAD(&tx
->hci_list
);
156 spin_lock_init(&tx
->lock
);
158 tx
->sdu_buf
= kmalloc(SDU_TX_BUF_SIZE
, GFP_KERNEL
);
159 if (tx
->sdu_buf
== NULL
) {
160 printk(KERN_ERR
"Failed to allocate SDU tx buffer.\n");
164 for (i
= 0; i
< MAX_NR_SDU_BUF
; i
++) {
165 t
= alloc_tx_struct(tx
);
170 list_add(&t
->list
, &tx
->free_list
);
173 INIT_LIST_HEAD(&rx
->free_list
);
174 INIT_LIST_HEAD(&rx
->req_list
);
176 spin_lock_init(&rx
->lock
);
178 for (i
= 0; i
< MAX_NR_RX_BUF
; i
++) {
179 r
= alloc_rx_struct(rx
);
184 list_add(&r
->list
, &rx
->free_list
);
187 rx
->rx_buf
= kmalloc(RX_BUF_SIZE
, GFP_KERNEL
);
188 if (rx
->rx_buf
== NULL
) {
189 printk(KERN_ERR
"Failed to allocate rx buffer.\n");
200 static void release_sdio(struct sdiowm_dev
*sdev
)
202 struct tx_cxt
*tx
= &sdev
->tx
;
203 struct rx_cxt
*rx
= &sdev
->rx
;
204 struct sdio_tx
*t
, *t_next
;
205 struct sdio_rx
*r
, *r_next
;
209 list_for_each_entry_safe(t
, t_next
, &tx
->free_list
, list
) {
214 list_for_each_entry_safe(t
, t_next
, &tx
->sdu_list
, list
) {
219 list_for_each_entry_safe(t
, t_next
, &tx
->hci_list
, list
) {
226 list_for_each_entry_safe(r
, r_next
, &rx
->free_list
, list
) {
231 list_for_each_entry_safe(r
, r_next
, &rx
->req_list
, list
) {
237 static void send_sdio_pkt(struct sdio_func
*func
, u8
*data
, int len
)
239 int n
, blocks
, ret
, remain
;
241 sdio_claim_host(func
);
243 blocks
= len
/ func
->cur_blksize
;
244 n
= blocks
* func
->cur_blksize
;
246 ret
= sdio_memcpy_toio(func
, 0, data
, n
);
248 if (ret
!= -ENOMEDIUM
)
249 printk(KERN_ERR
"gdmwms: %s error: ret = %d\n",
256 remain
= (remain
+ 3) & ~3;
259 ret
= sdio_memcpy_toio(func
, 0, data
+ n
, remain
);
261 if (ret
!= -ENOMEDIUM
)
262 printk(KERN_ERR
"gdmwms: %s error: ret = %d\n",
269 sdio_release_host(func
);
272 static void send_sdu(struct sdio_func
*func
, struct tx_cxt
*tx
)
274 struct list_head
*l
, *next
;
277 int pos
, len
, i
, estlen
, aggr_num
= 0, aggr_len
;
281 spin_lock_irqsave(&tx
->lock
, flags
);
283 pos
= TYPE_A_HEADER_SIZE
+ HCI_HEADER_SIZE
;
284 list_for_each_entry(t
, &tx
->sdu_list
, list
) {
285 estlen
= ((t
->len
+ 3) & ~3) + 4;
286 if ((pos
+ estlen
) > SDU_TX_BUF_SIZE
)
290 memcpy(tx
->sdu_buf
+ pos
, t
->buf
, t
->len
);
291 memset(tx
->sdu_buf
+ pos
+ t
->len
, 0, estlen
- t
->len
);
296 hci
= (struct hci_s
*)(tx
->sdu_buf
+ TYPE_A_HEADER_SIZE
);
297 hci
->cmd_evt
= H2B(WIMAX_TX_SDU_AGGR
);
298 hci
->length
= H2B(aggr_len
- TYPE_A_HEADER_SIZE
- HCI_HEADER_SIZE
);
300 spin_unlock_irqrestore(&tx
->lock
, flags
);
303 hexdump("sdio_send", tx
->sdu_buf
+ TYPE_A_HEADER_SIZE
,
304 aggr_len
- TYPE_A_HEADER_SIZE
);
307 for (pos
= TYPE_A_HEADER_SIZE
; pos
< aggr_len
; pos
+= TX_CHUNK_SIZE
) {
308 len
= aggr_len
- pos
;
309 len
= len
> TX_CHUNK_SIZE
? TX_CHUNK_SIZE
: len
;
310 buf
= tx
->sdu_buf
+ pos
- TYPE_A_HEADER_SIZE
;
313 buf
[1] = (len
>> 8) & 0xff;
314 buf
[2] = (len
>> 16) & 0xff;
315 buf
[3] = (pos
+ len
) >= aggr_len
? 0 : 1;
316 send_sdio_pkt(func
, buf
, len
+ TYPE_A_HEADER_SIZE
);
319 spin_lock_irqsave(&tx
->lock
, flags
);
321 for (l
= tx
->sdu_list
.next
, i
= 0; i
< aggr_num
; i
++, l
= next
) {
323 t
= list_entry(l
, struct sdio_tx
, list
);
325 t
->callback(t
->cb_data
);
328 put_tx_struct(t
->tx_cxt
, t
);
331 do_gettimeofday(&tx
->sdu_stamp
);
332 spin_unlock_irqrestore(&tx
->lock
, flags
);
335 static void send_hci(struct sdio_func
*func
, struct tx_cxt
*tx
,
341 hexdump("sdio_send", t
->buf
+ TYPE_A_HEADER_SIZE
,
342 t
->len
- TYPE_A_HEADER_SIZE
);
344 send_sdio_pkt(func
, t
->buf
, t
->len
);
346 spin_lock_irqsave(&tx
->lock
, flags
);
348 t
->callback(t
->cb_data
);
350 spin_unlock_irqrestore(&tx
->lock
, flags
);
353 static void do_tx(struct work_struct
*work
)
355 struct sdiowm_dev
*sdev
= container_of(work
, struct sdiowm_dev
, ws
);
356 struct sdio_func
*func
= sdev
->func
;
357 struct tx_cxt
*tx
= &sdev
->tx
;
358 struct sdio_tx
*t
= NULL
;
359 struct timeval now
, *before
;
364 spin_lock_irqsave(&tx
->lock
, flags
);
366 spin_unlock_irqrestore(&tx
->lock
, flags
);
370 if (!list_empty(&tx
->hci_list
)) {
371 t
= list_entry(tx
->hci_list
.next
, struct sdio_tx
, list
);
374 } else if (!tx
->stop_sdu_tx
&& !list_empty(&tx
->sdu_list
)) {
375 do_gettimeofday(&now
);
376 before
= &tx
->sdu_stamp
;
378 diff
= (now
.tv_sec
- before
->tv_sec
) * 1000000 +
379 (now
.tv_usec
- before
->tv_usec
);
380 if (diff
>= 0 && diff
< TX_INTERVAL
) {
381 schedule_work(&sdev
->ws
);
382 spin_unlock_irqrestore(&tx
->lock
, flags
);
388 if (!is_sdu
&& t
== NULL
) {
389 spin_unlock_irqrestore(&tx
->lock
, flags
);
395 spin_unlock_irqrestore(&tx
->lock
, flags
);
400 send_hci(func
, tx
, t
);
403 static int gdm_sdio_send(void *priv_dev
, void *data
, int len
,
404 void (*cb
)(void *data
), void *cb_data
)
406 struct sdiowm_dev
*sdev
= priv_dev
;
407 struct tx_cxt
*tx
= &sdev
->tx
;
414 BUG_ON(len
> TX_BUF_SIZE
- TYPE_A_HEADER_SIZE
);
416 spin_lock_irqsave(&tx
->lock
, flags
);
418 cmd_evt
= (pkt
[0] << 8) | pkt
[1];
419 if (cmd_evt
== WIMAX_TX_SDU
) {
420 t
= get_tx_struct(tx
, &no_spc
);
422 /* This case must not happen. */
423 spin_unlock_irqrestore(&tx
->lock
, flags
);
426 list_add_tail(&t
->list
, &tx
->sdu_list
);
428 memcpy(t
->buf
, data
, len
);
432 t
->cb_data
= cb_data
;
434 t
= alloc_tx_struct(tx
);
436 spin_unlock_irqrestore(&tx
->lock
, flags
);
439 list_add_tail(&t
->list
, &tx
->hci_list
);
441 t
->buf
[0] = len
& 0xff;
442 t
->buf
[1] = (len
>> 8) & 0xff;
443 t
->buf
[2] = (len
>> 16) & 0xff;
445 memcpy(t
->buf
+ TYPE_A_HEADER_SIZE
, data
, len
);
447 t
->len
= len
+ TYPE_A_HEADER_SIZE
;
449 t
->cb_data
= cb_data
;
453 schedule_work(&sdev
->ws
);
455 spin_unlock_irqrestore(&tx
->lock
, flags
);
464 * Handle the HCI, WIMAX_SDU_TX_FLOW.
466 static int control_sdu_tx_flow(struct sdiowm_dev
*sdev
, u8
*hci_data
, int len
)
468 struct tx_cxt
*tx
= &sdev
->tx
;
472 spin_lock_irqsave(&tx
->lock
, flags
);
474 cmd_evt
= (hci_data
[0] << 8) | (hci_data
[1]);
475 if (cmd_evt
!= WIMAX_SDU_TX_FLOW
)
478 if (hci_data
[4] == 0) {
480 printk(KERN_DEBUG
"WIMAX ==> STOP SDU TX\n");
483 } else if (hci_data
[4] == 1) {
485 printk(KERN_DEBUG
"WIMAX ==> START SDU TX\n");
489 schedule_work(&sdev
->ws
);
491 * If free buffer for sdu tx doesn't exist, then tx queue
492 * should not be woken. For this reason, don't pass the command,
495 if (list_empty(&tx
->free_list
))
500 spin_unlock_irqrestore(&tx
->lock
, flags
);
504 static void gdm_sdio_irq(struct sdio_func
*func
)
506 struct phy_dev
*phy_dev
= sdio_get_drvdata(func
);
507 struct sdiowm_dev
*sdev
= phy_dev
->priv_dev
;
508 struct tx_cxt
*tx
= &sdev
->tx
;
509 struct rx_cxt
*rx
= &sdev
->rx
;
512 u8 val
, hdr
[TYPE_A_LOOKAHEAD_SIZE
], *buf
;
516 /* Check interrupt */
517 val
= sdio_readb(func
, 0x13, &ret
);
519 sdio_writeb(func
, 0x01, 0x13, &ret
); /* clear interrupt */
523 ret
= sdio_memcpy_fromio(func
, hdr
, 0x0, TYPE_A_LOOKAHEAD_SIZE
);
525 printk(KERN_ERR
"Cannot read from function %d\n", func
->num
);
529 len
= (hdr
[2] << 16) | (hdr
[1] << 8) | hdr
[0];
530 if (len
> (RX_BUF_SIZE
- TYPE_A_HEADER_SIZE
)) {
531 printk(KERN_ERR
"Too big Type-A size: %d\n", len
);
535 if (hdr
[3] == 1) { /* Ack */
537 u32
*ack_seq
= (u32
*)&hdr
[4];
539 spin_lock_irqsave(&tx
->lock
, flags
);
542 if (!list_empty(&tx
->sdu_list
) || !list_empty(&tx
->hci_list
))
543 schedule_work(&sdev
->ws
);
544 spin_unlock_irqrestore(&tx
->lock
, flags
);
546 printk(KERN_DEBUG
"Ack... %0x\n", ntohl(*ack_seq
));
551 memcpy(rx
->rx_buf
, hdr
+ TYPE_A_HEADER_SIZE
,
552 TYPE_A_LOOKAHEAD_SIZE
- TYPE_A_HEADER_SIZE
);
554 buf
= rx
->rx_buf
+ TYPE_A_LOOKAHEAD_SIZE
- TYPE_A_HEADER_SIZE
;
555 remain
= len
- TYPE_A_LOOKAHEAD_SIZE
+ TYPE_A_HEADER_SIZE
;
559 blocks
= remain
/ func
->cur_blksize
;
562 n
= blocks
* func
->cur_blksize
;
563 ret
= sdio_memcpy_fromio(func
, buf
, 0x0, n
);
565 printk(KERN_ERR
"Cannot read from function %d\n",
574 ret
= sdio_memcpy_fromio(func
, buf
, 0x0, remain
);
576 printk(KERN_ERR
"Cannot read from function %d\n",
584 hexdump("sdio_receive", rx
->rx_buf
, len
);
586 len
= control_sdu_tx_flow(sdev
, rx
->rx_buf
, len
);
588 spin_lock_irqsave(&rx
->lock
, flags
);
590 if (!list_empty(&rx
->req_list
)) {
591 r
= list_entry(rx
->req_list
.next
, struct sdio_rx
, list
);
592 spin_unlock_irqrestore(&rx
->lock
, flags
);
594 r
->callback(r
->cb_data
, rx
->rx_buf
, len
);
595 spin_lock_irqsave(&rx
->lock
, flags
);
597 put_rx_struct(rx
, r
);
600 spin_unlock_irqrestore(&rx
->lock
, flags
);
603 sdio_writeb(func
, 0x00, 0x10, &ret
); /* PCRRT */
604 if (!phy_dev
->netdev
)
605 register_wimax_device(phy_dev
, &func
->dev
);
608 static int gdm_sdio_receive(void *priv_dev
,
609 void (*cb
)(void *cb_data
, void *data
, int len
),
612 struct sdiowm_dev
*sdev
= priv_dev
;
613 struct rx_cxt
*rx
= &sdev
->rx
;
617 spin_lock_irqsave(&rx
->lock
, flags
);
618 r
= get_rx_struct(rx
);
620 spin_unlock_irqrestore(&rx
->lock
, flags
);
625 r
->cb_data
= cb_data
;
627 list_add_tail(&r
->list
, &rx
->req_list
);
628 spin_unlock_irqrestore(&rx
->lock
, flags
);
633 static int sdio_wimax_probe(struct sdio_func
*func
,
634 const struct sdio_device_id
*id
)
637 struct phy_dev
*phy_dev
= NULL
;
638 struct sdiowm_dev
*sdev
= NULL
;
640 printk(KERN_INFO
"Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
641 func
->vendor
, func
->device
);
642 printk(KERN_INFO
"GCT WiMax driver version %s\n", DRIVER_VERSION
);
644 sdio_claim_host(func
);
645 sdio_enable_func(func
);
646 sdio_claim_irq(func
, gdm_sdio_irq
);
648 ret
= sdio_boot(func
);
652 phy_dev
= kzalloc(sizeof(*phy_dev
), GFP_KERNEL
);
653 if (phy_dev
== NULL
) {
657 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
663 phy_dev
->priv_dev
= (void *)sdev
;
664 phy_dev
->send_func
= gdm_sdio_send
;
665 phy_dev
->rcv_func
= gdm_sdio_receive
;
667 ret
= init_sdio(sdev
);
673 sdio_writeb(func
, 1, 0x14, &ret
); /* Enable interrupt */
674 sdio_release_host(func
);
676 INIT_WORK(&sdev
->ws
, do_tx
);
678 sdio_set_drvdata(func
, phy_dev
);
688 static void sdio_wimax_remove(struct sdio_func
*func
)
690 struct phy_dev
*phy_dev
= sdio_get_drvdata(func
);
691 struct sdiowm_dev
*sdev
= phy_dev
->priv_dev
;
694 unregister_wimax_device(phy_dev
);
695 sdio_claim_host(func
);
696 sdio_release_irq(func
);
697 sdio_disable_func(func
);
698 sdio_release_host(func
);
705 static const struct sdio_device_id sdio_wimax_ids
[] = {
706 { SDIO_DEVICE(0x0296, 0x5347) },
710 MODULE_DEVICE_TABLE(sdio
, sdio_wimax_ids
);
712 static struct sdio_driver sdio_wimax_driver
= {
713 .probe
= sdio_wimax_probe
,
714 .remove
= sdio_wimax_remove
,
715 .name
= "sdio_wimax",
716 .id_table
= sdio_wimax_ids
,
719 static int __init
sdio_gdm_wimax_init(void)
721 return sdio_register_driver(&sdio_wimax_driver
);
724 static void __exit
sdio_gdm_wimax_exit(void)
726 sdio_unregister_driver(&sdio_wimax_driver
);
729 module_init(sdio_gdm_wimax_init
);
730 module_exit(sdio_gdm_wimax_exit
);
732 MODULE_VERSION(DRIVER_VERSION
);
733 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
734 MODULE_AUTHOR("Ethan Park");
735 MODULE_LICENSE("GPL");