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 dev_err(&sdev->func->dev, "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 dev_err(&sdev->func->dev, "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)
250 "gdmwms: %s error: ret = %d\n",
257 remain = (remain + 3) & ~3;
260 ret = sdio_memcpy_toio(func, 0, data + n, remain);
262 if (ret != -ENOMEDIUM)
264 "gdmwms: %s error: ret = %d\n",
271 sdio_release_host(func);
274 static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
276 struct list_head *l, *next;
279 int pos, len, i, estlen, aggr_num = 0, aggr_len;
283 spin_lock_irqsave(&tx->lock, flags);
285 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
286 list_for_each_entry(t, &tx->sdu_list, list) {
287 estlen = ((t->len + 3) & ~3) + 4;
288 if ((pos + estlen) > SDU_TX_BUF_SIZE)
292 memcpy(tx->sdu_buf + pos, t->buf, t->len);
293 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
298 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
299 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
300 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
302 spin_unlock_irqrestore(&tx->lock, flags);
305 hexdump("sdio_send", tx->sdu_buf + TYPE_A_HEADER_SIZE,
306 aggr_len - TYPE_A_HEADER_SIZE);
309 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
310 len = aggr_len - pos;
311 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
312 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
315 buf[1] = (len >> 8) & 0xff;
316 buf[2] = (len >> 16) & 0xff;
317 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
318 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
321 spin_lock_irqsave(&tx->lock, flags);
323 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
325 t = list_entry(l, struct sdio_tx, list);
327 t->callback(t->cb_data);
330 put_tx_struct(t->tx_cxt, t);
333 do_gettimeofday(&tx->sdu_stamp);
334 spin_unlock_irqrestore(&tx->lock, flags);
337 static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
343 hexdump("sdio_send", t->buf + TYPE_A_HEADER_SIZE,
344 t->len - TYPE_A_HEADER_SIZE);
346 send_sdio_pkt(func, t->buf, t->len);
348 spin_lock_irqsave(&tx->lock, flags);
350 t->callback(t->cb_data);
352 spin_unlock_irqrestore(&tx->lock, flags);
355 static void do_tx(struct work_struct *work)
357 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
358 struct sdio_func *func = sdev->func;
359 struct tx_cxt *tx = &sdev->tx;
360 struct sdio_tx *t = NULL;
361 struct timeval now, *before;
366 spin_lock_irqsave(&tx->lock, flags);
368 spin_unlock_irqrestore(&tx->lock, flags);
372 if (!list_empty(&tx->hci_list)) {
373 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
376 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
377 do_gettimeofday(&now);
378 before = &tx->sdu_stamp;
380 diff = (now.tv_sec - before->tv_sec) * 1000000 +
381 (now.tv_usec - before->tv_usec);
382 if (diff >= 0 && diff < TX_INTERVAL) {
383 schedule_work(&sdev->ws);
384 spin_unlock_irqrestore(&tx->lock, flags);
390 if (!is_sdu && t == NULL) {
391 spin_unlock_irqrestore(&tx->lock, flags);
397 spin_unlock_irqrestore(&tx->lock, flags);
402 send_hci(func, tx, t);
405 static int gdm_sdio_send(void *priv_dev, void *data, int len,
406 void (*cb)(void *data), void *cb_data)
408 struct sdiowm_dev *sdev = priv_dev;
409 struct tx_cxt *tx = &sdev->tx;
416 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
418 spin_lock_irqsave(&tx->lock, flags);
420 cmd_evt = (pkt[0] << 8) | pkt[1];
421 if (cmd_evt == WIMAX_TX_SDU) {
422 t = get_tx_struct(tx, &no_spc);
424 /* This case must not happen. */
425 spin_unlock_irqrestore(&tx->lock, flags);
428 list_add_tail(&t->list, &tx->sdu_list);
430 memcpy(t->buf, data, len);
434 t->cb_data = cb_data;
436 t = alloc_tx_struct(tx);
438 spin_unlock_irqrestore(&tx->lock, flags);
441 list_add_tail(&t->list, &tx->hci_list);
443 t->buf[0] = len & 0xff;
444 t->buf[1] = (len >> 8) & 0xff;
445 t->buf[2] = (len >> 16) & 0xff;
447 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
449 t->len = len + TYPE_A_HEADER_SIZE;
451 t->cb_data = cb_data;
455 schedule_work(&sdev->ws);
457 spin_unlock_irqrestore(&tx->lock, flags);
466 * Handle the HCI, WIMAX_SDU_TX_FLOW.
468 static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
470 struct tx_cxt *tx = &sdev->tx;
474 spin_lock_irqsave(&tx->lock, flags);
476 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
477 if (cmd_evt != WIMAX_SDU_TX_FLOW)
480 if (hci_data[4] == 0) {
482 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
485 } else if (hci_data[4] == 1) {
487 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
491 schedule_work(&sdev->ws);
493 * If free buffer for sdu tx doesn't exist, then tx queue
494 * should not be woken. For this reason, don't pass the command,
497 if (list_empty(&tx->free_list))
502 spin_unlock_irqrestore(&tx->lock, flags);
506 static void gdm_sdio_irq(struct sdio_func *func)
508 struct phy_dev *phy_dev = sdio_get_drvdata(func);
509 struct sdiowm_dev *sdev = phy_dev->priv_dev;
510 struct tx_cxt *tx = &sdev->tx;
511 struct rx_cxt *rx = &sdev->rx;
514 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
518 /* Check interrupt */
519 val = sdio_readb(func, 0x13, &ret);
521 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
525 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
528 "Cannot read from function %d\n", func->num);
532 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
533 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
534 dev_err(&func->dev, "Too big Type-A size: %d\n", len);
538 if (hdr[3] == 1) { /* Ack */
540 u32 *ack_seq = (u32 *)&hdr[4];
542 spin_lock_irqsave(&tx->lock, flags);
545 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
546 schedule_work(&sdev->ws);
547 spin_unlock_irqrestore(&tx->lock, flags);
549 printk(KERN_DEBUG "Ack... %0x\n", ntohl(*ack_seq));
554 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
555 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
557 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
558 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
562 blocks = remain / func->cur_blksize;
565 n = blocks * func->cur_blksize;
566 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
569 "Cannot read from function %d\n", func->num);
577 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
580 "Cannot read from function %d\n", func->num);
587 hexdump("sdio_receive", rx->rx_buf, len);
589 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
591 spin_lock_irqsave(&rx->lock, flags);
593 if (!list_empty(&rx->req_list)) {
594 r = list_entry(rx->req_list.next, struct sdio_rx, list);
595 spin_unlock_irqrestore(&rx->lock, flags);
597 r->callback(r->cb_data, rx->rx_buf, len);
598 spin_lock_irqsave(&rx->lock, flags);
600 put_rx_struct(rx, r);
603 spin_unlock_irqrestore(&rx->lock, flags);
606 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
607 if (!phy_dev->netdev)
608 register_wimax_device(phy_dev, &func->dev);
611 static int gdm_sdio_receive(void *priv_dev,
612 void (*cb)(void *cb_data, void *data, int len),
615 struct sdiowm_dev *sdev = priv_dev;
616 struct rx_cxt *rx = &sdev->rx;
620 spin_lock_irqsave(&rx->lock, flags);
621 r = get_rx_struct(rx);
623 spin_unlock_irqrestore(&rx->lock, flags);
628 r->cb_data = cb_data;
630 list_add_tail(&r->list, &rx->req_list);
631 spin_unlock_irqrestore(&rx->lock, flags);
636 static int sdio_wimax_probe(struct sdio_func *func,
637 const struct sdio_device_id *id)
640 struct phy_dev *phy_dev = NULL;
641 struct sdiowm_dev *sdev = NULL;
643 dev_info(&func->dev, "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
644 func->vendor, func->device);
645 dev_info(&func->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
647 sdio_claim_host(func);
648 sdio_enable_func(func);
649 sdio_claim_irq(func, gdm_sdio_irq);
651 ret = sdio_boot(func);
655 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
656 if (phy_dev == NULL) {
660 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
666 phy_dev->priv_dev = (void *)sdev;
667 phy_dev->send_func = gdm_sdio_send;
668 phy_dev->rcv_func = gdm_sdio_receive;
670 ret = init_sdio(sdev);
676 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
677 sdio_release_host(func);
679 INIT_WORK(&sdev->ws, do_tx);
681 sdio_set_drvdata(func, phy_dev);
691 static void sdio_wimax_remove(struct sdio_func *func)
693 struct phy_dev *phy_dev = sdio_get_drvdata(func);
694 struct sdiowm_dev *sdev = phy_dev->priv_dev;
696 cancel_work_sync(&sdev->ws);
698 unregister_wimax_device(phy_dev);
699 sdio_claim_host(func);
700 sdio_release_irq(func);
701 sdio_disable_func(func);
702 sdio_release_host(func);
709 static const struct sdio_device_id sdio_wimax_ids[] = {
710 { SDIO_DEVICE(0x0296, 0x5347) },
714 MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
716 static struct sdio_driver sdio_wimax_driver = {
717 .probe = sdio_wimax_probe,
718 .remove = sdio_wimax_remove,
719 .name = "sdio_wimax",
720 .id_table = sdio_wimax_ids,
723 static int __init sdio_gdm_wimax_init(void)
725 return sdio_register_driver(&sdio_wimax_driver);
728 static void __exit sdio_gdm_wimax_exit(void)
730 sdio_unregister_driver(&sdio_wimax_driver);
733 module_init(sdio_gdm_wimax_init);
734 module_exit(sdio_gdm_wimax_exit);
736 MODULE_VERSION(DRIVER_VERSION);
737 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
738 MODULE_AUTHOR("Ethan Park");
739 MODULE_LICENSE("GPL");