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 = NULL;
65 t = kzalloc(sizeof(*t), GFP_ATOMIC);
69 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
84 static void free_tx_struct(struct sdio_tx *t)
92 static struct sdio_rx *alloc_rx_struct(struct rx_cxt *rx)
94 struct sdio_rx *r = NULL;
96 r = kmalloc(sizeof(*r), GFP_ATOMIC);
100 memset(r, 0, sizeof(*r));
110 static void free_rx_struct(struct sdio_rx *r)
115 /* Before this function is called, spin lock should be locked. */
116 static struct sdio_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
120 if (list_empty(&tx->free_list))
123 t = list_entry(tx->free_list.prev, struct sdio_tx, list);
126 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
131 /* Before this function is called, spin lock should be locked. */
132 static void put_tx_struct(struct tx_cxt *tx, struct sdio_tx *t)
134 list_add_tail(&t->list, &tx->free_list);
137 /* Before this function is called, spin lock should be locked. */
138 static struct sdio_rx *get_rx_struct(struct rx_cxt *rx)
142 if (list_empty(&rx->free_list))
145 r = list_entry(rx->free_list.prev, struct sdio_rx, list);
151 /* Before this function is called, spin lock should be locked. */
152 static void put_rx_struct(struct rx_cxt *rx, struct sdio_rx *r)
154 list_add_tail(&r->list, &rx->free_list);
157 static int init_sdio(struct sdiowm_dev *sdev)
160 struct tx_cxt *tx = &sdev->tx;
161 struct rx_cxt *rx = &sdev->rx;
165 INIT_LIST_HEAD(&tx->free_list);
166 INIT_LIST_HEAD(&tx->sdu_list);
167 INIT_LIST_HEAD(&tx->hci_list);
169 spin_lock_init(&tx->lock);
171 tx->sdu_buf = kmalloc(SDU_TX_BUF_SIZE, GFP_KERNEL);
172 if (tx->sdu_buf == NULL) {
173 printk(KERN_ERR "Failed to allocate SDU tx buffer.\n");
177 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
178 t = alloc_tx_struct(tx);
183 list_add(&t->list, &tx->free_list);
186 INIT_LIST_HEAD(&rx->free_list);
187 INIT_LIST_HEAD(&rx->req_list);
189 spin_lock_init(&rx->lock);
191 for (i = 0; i < MAX_NR_RX_BUF; i++) {
192 r = alloc_rx_struct(rx);
197 list_add(&r->list, &rx->free_list);
200 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
201 if (rx->rx_buf == NULL) {
202 printk(KERN_ERR "Failed to allocate rx buffer.\n");
213 static void release_sdio(struct sdiowm_dev *sdev)
215 struct tx_cxt *tx = &sdev->tx;
216 struct rx_cxt *rx = &sdev->rx;
217 struct sdio_tx *t, *t_next;
218 struct sdio_rx *r, *r_next;
222 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
227 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
232 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
239 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
244 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
250 static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
252 int n, blocks, ret, remain;
254 sdio_claim_host(func);
256 blocks = len / func->cur_blksize;
257 n = blocks * func->cur_blksize;
259 ret = sdio_memcpy_toio(func, 0, data, n);
261 if (ret != -ENOMEDIUM)
262 printk(KERN_ERR "gdmwms: %s error: ret = %d\n",
269 remain = (remain + 3) & ~3;
272 ret = sdio_memcpy_toio(func, 0, data + n, remain);
274 if (ret != -ENOMEDIUM)
275 printk(KERN_ERR "gdmwms: %s error: ret = %d\n",
282 sdio_release_host(func);
285 static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
287 struct list_head *l, *next;
290 int pos, len, i, estlen, aggr_num = 0, aggr_len;
294 spin_lock_irqsave(&tx->lock, flags);
296 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
297 list_for_each_entry(t, &tx->sdu_list, list) {
298 estlen = ((t->len + 3) & ~3) + 4;
299 if ((pos + estlen) > SDU_TX_BUF_SIZE)
303 memcpy(tx->sdu_buf + pos, t->buf, t->len);
304 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
309 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
310 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
311 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
313 spin_unlock_irqrestore(&tx->lock, flags);
316 hexdump("sdio_send", tx->sdu_buf + TYPE_A_HEADER_SIZE,
317 aggr_len - TYPE_A_HEADER_SIZE);
320 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
321 len = aggr_len - pos;
322 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
323 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
326 buf[1] = (len >> 8) & 0xff;
327 buf[2] = (len >> 16) & 0xff;
328 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
329 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
332 spin_lock_irqsave(&tx->lock, flags);
334 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
336 t = list_entry(l, struct sdio_tx, list);
338 t->callback(t->cb_data);
341 put_tx_struct(t->tx_cxt, t);
344 do_gettimeofday(&tx->sdu_stamp);
345 spin_unlock_irqrestore(&tx->lock, flags);
348 static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
354 hexdump("sdio_send", t->buf + TYPE_A_HEADER_SIZE,
355 t->len - TYPE_A_HEADER_SIZE);
357 send_sdio_pkt(func, t->buf, t->len);
359 spin_lock_irqsave(&tx->lock, flags);
361 t->callback(t->cb_data);
363 spin_unlock_irqrestore(&tx->lock, flags);
366 static void do_tx(struct work_struct *work)
368 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
369 struct sdio_func *func = sdev->func;
370 struct tx_cxt *tx = &sdev->tx;
371 struct sdio_tx *t = NULL;
372 struct timeval now, *before;
377 spin_lock_irqsave(&tx->lock, flags);
379 spin_unlock_irqrestore(&tx->lock, flags);
383 if (!list_empty(&tx->hci_list)) {
384 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
387 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
388 do_gettimeofday(&now);
389 before = &tx->sdu_stamp;
391 diff = (now.tv_sec - before->tv_sec) * 1000000 +
392 (now.tv_usec - before->tv_usec);
393 if (diff >= 0 && diff < TX_INTERVAL) {
394 schedule_work(&sdev->ws);
395 spin_unlock_irqrestore(&tx->lock, flags);
401 if (!is_sdu && t == NULL) {
402 spin_unlock_irqrestore(&tx->lock, flags);
408 spin_unlock_irqrestore(&tx->lock, flags);
413 send_hci(func, tx, t);
416 static int gdm_sdio_send(void *priv_dev, void *data, int len,
417 void (*cb)(void *data), void *cb_data)
419 struct sdiowm_dev *sdev = priv_dev;
420 struct tx_cxt *tx = &sdev->tx;
427 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
429 spin_lock_irqsave(&tx->lock, flags);
431 cmd_evt = (pkt[0] << 8) | pkt[1];
432 if (cmd_evt == WIMAX_TX_SDU) {
433 t = get_tx_struct(tx, &no_spc);
435 /* This case must not happen. */
436 spin_unlock_irqrestore(&tx->lock, flags);
439 list_add_tail(&t->list, &tx->sdu_list);
441 memcpy(t->buf, data, len);
445 t->cb_data = cb_data;
447 t = alloc_tx_struct(tx);
449 spin_unlock_irqrestore(&tx->lock, flags);
452 list_add_tail(&t->list, &tx->hci_list);
454 t->buf[0] = len & 0xff;
455 t->buf[1] = (len >> 8) & 0xff;
456 t->buf[2] = (len >> 16) & 0xff;
458 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
460 t->len = len + TYPE_A_HEADER_SIZE;
462 t->cb_data = cb_data;
466 schedule_work(&sdev->ws);
468 spin_unlock_irqrestore(&tx->lock, flags);
477 * Handle the HCI, WIMAX_SDU_TX_FLOW.
479 static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
481 struct tx_cxt *tx = &sdev->tx;
485 spin_lock_irqsave(&tx->lock, flags);
487 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
488 if (cmd_evt != WIMAX_SDU_TX_FLOW)
491 if (hci_data[4] == 0) {
493 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
496 } else if (hci_data[4] == 1) {
498 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
502 schedule_work(&sdev->ws);
504 * If free buffer for sdu tx doesn't exist, then tx queue
505 * should not be woken. For this reason, don't pass the command,
508 if (list_empty(&tx->free_list))
513 spin_unlock_irqrestore(&tx->lock, flags);
517 static void gdm_sdio_irq(struct sdio_func *func)
519 struct phy_dev *phy_dev = sdio_get_drvdata(func);
520 struct sdiowm_dev *sdev = phy_dev->priv_dev;
521 struct tx_cxt *tx = &sdev->tx;
522 struct rx_cxt *rx = &sdev->rx;
525 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
529 /* Check interrupt */
530 val = sdio_readb(func, 0x13, &ret);
532 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
536 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
538 printk(KERN_ERR "Cannot read from function %d\n", func->num);
542 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
543 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
544 printk(KERN_ERR "Too big Type-A size: %d\n", len);
548 if (hdr[3] == 1) { /* Ack */
550 u32 *ack_seq = (u32 *)&hdr[4];
552 spin_lock_irqsave(&tx->lock, flags);
555 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
556 schedule_work(&sdev->ws);
557 spin_unlock_irqrestore(&tx->lock, flags);
559 printk(KERN_DEBUG "Ack... %0x\n", ntohl(*ack_seq));
564 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
565 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
567 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
568 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
572 blocks = remain / func->cur_blksize;
575 n = blocks * func->cur_blksize;
576 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
578 printk(KERN_ERR "Cannot read from function %d\n",
587 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
589 printk(KERN_ERR "Cannot read from function %d\n",
597 hexdump("sdio_receive", rx->rx_buf, len);
599 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
601 spin_lock_irqsave(&rx->lock, flags);
603 if (!list_empty(&rx->req_list)) {
604 r = list_entry(rx->req_list.next, struct sdio_rx, list);
605 spin_unlock_irqrestore(&rx->lock, flags);
607 r->callback(r->cb_data, rx->rx_buf, len);
608 spin_lock_irqsave(&rx->lock, flags);
610 put_rx_struct(rx, r);
613 spin_unlock_irqrestore(&rx->lock, flags);
616 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
617 if (!phy_dev->netdev)
618 register_wimax_device(phy_dev, &func->dev);
621 static int gdm_sdio_receive(void *priv_dev,
622 void (*cb)(void *cb_data, void *data, int len),
625 struct sdiowm_dev *sdev = priv_dev;
626 struct rx_cxt *rx = &sdev->rx;
630 spin_lock_irqsave(&rx->lock, flags);
631 r = get_rx_struct(rx);
633 spin_unlock_irqrestore(&rx->lock, flags);
638 r->cb_data = cb_data;
640 list_add_tail(&r->list, &rx->req_list);
641 spin_unlock_irqrestore(&rx->lock, flags);
646 static int sdio_wimax_probe(struct sdio_func *func,
647 const struct sdio_device_id *id)
650 struct phy_dev *phy_dev = NULL;
651 struct sdiowm_dev *sdev = NULL;
653 printk(KERN_INFO "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
654 func->vendor, func->device);
655 printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
657 sdio_claim_host(func);
658 sdio_enable_func(func);
659 sdio_claim_irq(func, gdm_sdio_irq);
661 ret = sdio_boot(func);
665 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
666 if (phy_dev == NULL) {
670 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
676 phy_dev->priv_dev = (void *)sdev;
677 phy_dev->send_func = gdm_sdio_send;
678 phy_dev->rcv_func = gdm_sdio_receive;
680 ret = init_sdio(sdev);
686 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
687 sdio_release_host(func);
689 INIT_WORK(&sdev->ws, do_tx);
691 sdio_set_drvdata(func, phy_dev);
701 static void sdio_wimax_remove(struct sdio_func *func)
703 struct phy_dev *phy_dev = sdio_get_drvdata(func);
704 struct sdiowm_dev *sdev = phy_dev->priv_dev;
707 unregister_wimax_device(phy_dev);
708 sdio_claim_host(func);
709 sdio_release_irq(func);
710 sdio_disable_func(func);
711 sdio_release_host(func);
718 static const struct sdio_device_id sdio_wimax_ids[] = {
719 { SDIO_DEVICE(0x0296, 0x5347) },
723 MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
725 static struct sdio_driver sdio_wimax_driver = {
726 .probe = sdio_wimax_probe,
727 .remove = sdio_wimax_remove,
728 .name = "sdio_wimax",
729 .id_table = sdio_wimax_ids,
732 static int __init sdio_gdm_wimax_init(void)
734 return sdio_register_driver(&sdio_wimax_driver);
737 static void __exit sdio_gdm_wimax_exit(void)
739 sdio_unregister_driver(&sdio_wimax_driver);
742 module_init(sdio_gdm_wimax_init);
743 module_exit(sdio_gdm_wimax_exit);
745 MODULE_VERSION(DRIVER_VERSION);
746 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
747 MODULE_AUTHOR("Ethan Park");
748 MODULE_LICENSE("GPL");