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)
162 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
163 t = alloc_tx_struct(tx);
168 list_add(&t->list, &tx->free_list);
171 INIT_LIST_HEAD(&rx->free_list);
172 INIT_LIST_HEAD(&rx->req_list);
174 spin_lock_init(&rx->lock);
176 for (i = 0; i < MAX_NR_RX_BUF; i++) {
177 r = alloc_rx_struct(rx);
182 list_add(&r->list, &rx->free_list);
185 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
186 if (rx->rx_buf == NULL)
196 static void release_sdio(struct sdiowm_dev *sdev)
198 struct tx_cxt *tx = &sdev->tx;
199 struct rx_cxt *rx = &sdev->rx;
200 struct sdio_tx *t, *t_next;
201 struct sdio_rx *r, *r_next;
205 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
210 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
215 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
222 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
227 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
233 static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
235 int n, blocks, ret, remain;
237 sdio_claim_host(func);
239 blocks = len / func->cur_blksize;
240 n = blocks * func->cur_blksize;
242 ret = sdio_memcpy_toio(func, 0, data, n);
244 if (ret != -ENOMEDIUM)
246 "gdmwms: %s error: ret = %d\n",
253 remain = (remain + 3) & ~3;
256 ret = sdio_memcpy_toio(func, 0, data + n, remain);
258 if (ret != -ENOMEDIUM)
260 "gdmwms: %s error: ret = %d\n",
267 sdio_release_host(func);
270 static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
272 struct list_head *l, *next;
275 int pos, len, i, estlen, aggr_num = 0, aggr_len;
279 spin_lock_irqsave(&tx->lock, flags);
281 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
282 list_for_each_entry(t, &tx->sdu_list, list) {
283 estlen = ((t->len + 3) & ~3) + 4;
284 if ((pos + estlen) > SDU_TX_BUF_SIZE)
288 memcpy(tx->sdu_buf + pos, t->buf, t->len);
289 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
294 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
295 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
296 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
298 spin_unlock_irqrestore(&tx->lock, flags);
301 hexdump("sdio_send", tx->sdu_buf + TYPE_A_HEADER_SIZE,
302 aggr_len - TYPE_A_HEADER_SIZE);
305 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
306 len = aggr_len - pos;
307 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
308 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
311 buf[1] = (len >> 8) & 0xff;
312 buf[2] = (len >> 16) & 0xff;
313 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
314 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
317 spin_lock_irqsave(&tx->lock, flags);
319 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
321 t = list_entry(l, struct sdio_tx, list);
323 t->callback(t->cb_data);
326 put_tx_struct(t->tx_cxt, t);
329 do_gettimeofday(&tx->sdu_stamp);
330 spin_unlock_irqrestore(&tx->lock, flags);
333 static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
339 hexdump("sdio_send", t->buf + TYPE_A_HEADER_SIZE,
340 t->len - TYPE_A_HEADER_SIZE);
342 send_sdio_pkt(func, t->buf, t->len);
344 spin_lock_irqsave(&tx->lock, flags);
346 t->callback(t->cb_data);
348 spin_unlock_irqrestore(&tx->lock, flags);
351 static void do_tx(struct work_struct *work)
353 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
354 struct sdio_func *func = sdev->func;
355 struct tx_cxt *tx = &sdev->tx;
356 struct sdio_tx *t = NULL;
357 struct timeval now, *before;
362 spin_lock_irqsave(&tx->lock, flags);
364 spin_unlock_irqrestore(&tx->lock, flags);
368 if (!list_empty(&tx->hci_list)) {
369 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
372 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
373 do_gettimeofday(&now);
374 before = &tx->sdu_stamp;
376 diff = (now.tv_sec - before->tv_sec) * 1000000 +
377 (now.tv_usec - before->tv_usec);
378 if (diff >= 0 && diff < TX_INTERVAL) {
379 schedule_work(&sdev->ws);
380 spin_unlock_irqrestore(&tx->lock, flags);
386 if (!is_sdu && t == NULL) {
387 spin_unlock_irqrestore(&tx->lock, flags);
393 spin_unlock_irqrestore(&tx->lock, flags);
398 send_hci(func, tx, t);
401 static int gdm_sdio_send(void *priv_dev, void *data, int len,
402 void (*cb)(void *data), void *cb_data)
404 struct sdiowm_dev *sdev = priv_dev;
405 struct tx_cxt *tx = &sdev->tx;
412 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
414 spin_lock_irqsave(&tx->lock, flags);
416 cmd_evt = (pkt[0] << 8) | pkt[1];
417 if (cmd_evt == WIMAX_TX_SDU) {
418 t = get_tx_struct(tx, &no_spc);
420 /* This case must not happen. */
421 spin_unlock_irqrestore(&tx->lock, flags);
424 list_add_tail(&t->list, &tx->sdu_list);
426 memcpy(t->buf, data, len);
430 t->cb_data = cb_data;
432 t = alloc_tx_struct(tx);
434 spin_unlock_irqrestore(&tx->lock, flags);
437 list_add_tail(&t->list, &tx->hci_list);
439 t->buf[0] = len & 0xff;
440 t->buf[1] = (len >> 8) & 0xff;
441 t->buf[2] = (len >> 16) & 0xff;
443 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
445 t->len = len + TYPE_A_HEADER_SIZE;
447 t->cb_data = cb_data;
451 schedule_work(&sdev->ws);
453 spin_unlock_irqrestore(&tx->lock, flags);
462 * Handle the HCI, WIMAX_SDU_TX_FLOW.
464 static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
466 struct tx_cxt *tx = &sdev->tx;
470 spin_lock_irqsave(&tx->lock, flags);
472 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
473 if (cmd_evt != WIMAX_SDU_TX_FLOW)
476 if (hci_data[4] == 0) {
478 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
481 } else if (hci_data[4] == 1) {
483 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
487 schedule_work(&sdev->ws);
489 * If free buffer for sdu tx doesn't exist, then tx queue
490 * should not be woken. For this reason, don't pass the command,
493 if (list_empty(&tx->free_list))
498 spin_unlock_irqrestore(&tx->lock, flags);
502 static void gdm_sdio_irq(struct sdio_func *func)
504 struct phy_dev *phy_dev = sdio_get_drvdata(func);
505 struct sdiowm_dev *sdev = phy_dev->priv_dev;
506 struct tx_cxt *tx = &sdev->tx;
507 struct rx_cxt *rx = &sdev->rx;
510 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
514 /* Check interrupt */
515 val = sdio_readb(func, 0x13, &ret);
517 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
521 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
524 "Cannot read from function %d\n", func->num);
528 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
529 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
530 dev_err(&func->dev, "Too big Type-A size: %d\n", len);
534 if (hdr[3] == 1) { /* Ack */
536 u32 *ack_seq = (u32 *)&hdr[4];
538 spin_lock_irqsave(&tx->lock, flags);
541 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
542 schedule_work(&sdev->ws);
543 spin_unlock_irqrestore(&tx->lock, flags);
545 printk(KERN_DEBUG "Ack... %0x\n", ntohl(*ack_seq));
550 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
551 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
553 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
554 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
558 blocks = remain / func->cur_blksize;
561 n = blocks * func->cur_blksize;
562 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
565 "Cannot read from function %d\n", func->num);
573 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
576 "Cannot read from function %d\n", func->num);
583 hexdump("sdio_receive", rx->rx_buf, len);
585 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
587 spin_lock_irqsave(&rx->lock, flags);
589 if (!list_empty(&rx->req_list)) {
590 r = list_entry(rx->req_list.next, struct sdio_rx, list);
591 spin_unlock_irqrestore(&rx->lock, flags);
593 r->callback(r->cb_data, rx->rx_buf, len);
594 spin_lock_irqsave(&rx->lock, flags);
596 put_rx_struct(rx, r);
599 spin_unlock_irqrestore(&rx->lock, flags);
602 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
603 if (!phy_dev->netdev)
604 register_wimax_device(phy_dev, &func->dev);
607 static int gdm_sdio_receive(void *priv_dev,
608 void (*cb)(void *cb_data, void *data, int len),
611 struct sdiowm_dev *sdev = priv_dev;
612 struct rx_cxt *rx = &sdev->rx;
616 spin_lock_irqsave(&rx->lock, flags);
617 r = get_rx_struct(rx);
619 spin_unlock_irqrestore(&rx->lock, flags);
624 r->cb_data = cb_data;
626 list_add_tail(&r->list, &rx->req_list);
627 spin_unlock_irqrestore(&rx->lock, flags);
632 static int sdio_wimax_probe(struct sdio_func *func,
633 const struct sdio_device_id *id)
636 struct phy_dev *phy_dev = NULL;
637 struct sdiowm_dev *sdev = NULL;
639 dev_info(&func->dev, "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
640 func->vendor, func->device);
641 dev_info(&func->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
643 sdio_claim_host(func);
644 sdio_enable_func(func);
645 sdio_claim_irq(func, gdm_sdio_irq);
647 ret = sdio_boot(func);
651 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
652 if (phy_dev == NULL) {
656 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
662 phy_dev->priv_dev = (void *)sdev;
663 phy_dev->send_func = gdm_sdio_send;
664 phy_dev->rcv_func = gdm_sdio_receive;
666 ret = init_sdio(sdev);
672 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
673 sdio_release_host(func);
675 INIT_WORK(&sdev->ws, do_tx);
677 sdio_set_drvdata(func, phy_dev);
687 static void sdio_wimax_remove(struct sdio_func *func)
689 struct phy_dev *phy_dev = sdio_get_drvdata(func);
690 struct sdiowm_dev *sdev = phy_dev->priv_dev;
692 cancel_work_sync(&sdev->ws);
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");