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)
41 static int init_sdio(struct sdiowm_dev *sdev);
42 static void release_sdio(struct sdiowm_dev *sdev);
44 static struct sdio_tx *alloc_tx_struct(struct tx_cxt *tx)
46 struct sdio_tx *t = kzalloc(sizeof(*t), GFP_ATOMIC);
51 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
62 static void free_tx_struct(struct sdio_tx *t)
70 static struct sdio_rx *alloc_rx_struct(struct rx_cxt *rx)
72 struct sdio_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
80 static void free_rx_struct(struct sdio_rx *r)
85 /* Before this function is called, spin lock should be locked. */
86 static struct sdio_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
90 if (list_empty(&tx->free_list))
93 t = list_entry(tx->free_list.prev, struct sdio_tx, list);
96 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
101 /* Before this function is called, spin lock should be locked. */
102 static void put_tx_struct(struct tx_cxt *tx, struct sdio_tx *t)
104 list_add_tail(&t->list, &tx->free_list);
107 /* Before this function is called, spin lock should be locked. */
108 static struct sdio_rx *get_rx_struct(struct rx_cxt *rx)
112 if (list_empty(&rx->free_list))
115 r = list_entry(rx->free_list.prev, struct sdio_rx, list);
121 /* Before this function is called, spin lock should be locked. */
122 static void put_rx_struct(struct rx_cxt *rx, struct sdio_rx *r)
124 list_add_tail(&r->list, &rx->free_list);
127 static int init_sdio(struct sdiowm_dev *sdev)
130 struct tx_cxt *tx = &sdev->tx;
131 struct rx_cxt *rx = &sdev->rx;
135 INIT_LIST_HEAD(&tx->free_list);
136 INIT_LIST_HEAD(&tx->sdu_list);
137 INIT_LIST_HEAD(&tx->hci_list);
139 spin_lock_init(&tx->lock);
141 tx->sdu_buf = kmalloc(SDU_TX_BUF_SIZE, GFP_KERNEL);
142 if (tx->sdu_buf == NULL)
145 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
146 t = alloc_tx_struct(tx);
151 list_add(&t->list, &tx->free_list);
154 INIT_LIST_HEAD(&rx->free_list);
155 INIT_LIST_HEAD(&rx->req_list);
157 spin_lock_init(&rx->lock);
159 for (i = 0; i < MAX_NR_RX_BUF; i++) {
160 r = alloc_rx_struct(rx);
165 list_add(&r->list, &rx->free_list);
168 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
169 if (rx->rx_buf == NULL)
179 static void release_sdio(struct sdiowm_dev *sdev)
181 struct tx_cxt *tx = &sdev->tx;
182 struct rx_cxt *rx = &sdev->rx;
183 struct sdio_tx *t, *t_next;
184 struct sdio_rx *r, *r_next;
188 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
193 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
198 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
205 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
210 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
216 static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
218 int n, blocks, ret, remain;
220 sdio_claim_host(func);
222 blocks = len / func->cur_blksize;
223 n = blocks * func->cur_blksize;
225 ret = sdio_memcpy_toio(func, 0, data, n);
227 if (ret != -ENOMEDIUM)
229 "gdmwms: %s error: ret = %d\n",
236 remain = (remain + 3) & ~3;
239 ret = sdio_memcpy_toio(func, 0, data + n, remain);
241 if (ret != -ENOMEDIUM)
243 "gdmwms: %s error: ret = %d\n",
250 sdio_release_host(func);
253 static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
255 struct list_head *l, *next;
258 int pos, len, i, estlen, aggr_num = 0, aggr_len;
262 spin_lock_irqsave(&tx->lock, flags);
264 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
265 list_for_each_entry(t, &tx->sdu_list, list) {
266 estlen = ((t->len + 3) & ~3) + 4;
267 if ((pos + estlen) > SDU_TX_BUF_SIZE)
271 memcpy(tx->sdu_buf + pos, t->buf, t->len);
272 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
277 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
278 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
279 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
281 spin_unlock_irqrestore(&tx->lock, flags);
283 print_hex_dump_debug("sdio_send: ", DUMP_PREFIX_NONE, 16, 1,
284 tx->sdu_buf + TYPE_A_HEADER_SIZE,
285 aggr_len - TYPE_A_HEADER_SIZE, false);
287 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
288 len = aggr_len - pos;
289 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
290 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
293 buf[1] = (len >> 8) & 0xff;
294 buf[2] = (len >> 16) & 0xff;
295 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
296 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
299 spin_lock_irqsave(&tx->lock, flags);
301 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
303 t = list_entry(l, struct sdio_tx, list);
305 t->callback(t->cb_data);
308 put_tx_struct(t->tx_cxt, t);
311 do_gettimeofday(&tx->sdu_stamp);
312 spin_unlock_irqrestore(&tx->lock, flags);
315 static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
320 print_hex_dump_debug("sdio_send: ", DUMP_PREFIX_NONE, 16, 1,
321 t->buf + TYPE_A_HEADER_SIZE,
322 t->len - TYPE_A_HEADER_SIZE, false);
323 send_sdio_pkt(func, t->buf, t->len);
325 spin_lock_irqsave(&tx->lock, flags);
327 t->callback(t->cb_data);
329 spin_unlock_irqrestore(&tx->lock, flags);
332 static void do_tx(struct work_struct *work)
334 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
335 struct sdio_func *func = sdev->func;
336 struct tx_cxt *tx = &sdev->tx;
337 struct sdio_tx *t = NULL;
338 struct timeval now, *before;
343 spin_lock_irqsave(&tx->lock, flags);
345 spin_unlock_irqrestore(&tx->lock, flags);
349 if (!list_empty(&tx->hci_list)) {
350 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
353 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
354 do_gettimeofday(&now);
355 before = &tx->sdu_stamp;
357 diff = (now.tv_sec - before->tv_sec) * 1000000 +
358 (now.tv_usec - before->tv_usec);
359 if (diff >= 0 && diff < TX_INTERVAL) {
360 schedule_work(&sdev->ws);
361 spin_unlock_irqrestore(&tx->lock, flags);
367 if (!is_sdu && t == NULL) {
368 spin_unlock_irqrestore(&tx->lock, flags);
374 spin_unlock_irqrestore(&tx->lock, flags);
379 send_hci(func, tx, t);
382 static int gdm_sdio_send(void *priv_dev, void *data, int len,
383 void (*cb)(void *data), void *cb_data)
385 struct sdiowm_dev *sdev = priv_dev;
386 struct tx_cxt *tx = &sdev->tx;
393 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
395 spin_lock_irqsave(&tx->lock, flags);
397 cmd_evt = (pkt[0] << 8) | pkt[1];
398 if (cmd_evt == WIMAX_TX_SDU) {
399 t = get_tx_struct(tx, &no_spc);
401 /* This case must not happen. */
402 spin_unlock_irqrestore(&tx->lock, flags);
405 list_add_tail(&t->list, &tx->sdu_list);
407 memcpy(t->buf, data, len);
411 t->cb_data = cb_data;
413 t = alloc_tx_struct(tx);
415 spin_unlock_irqrestore(&tx->lock, flags);
418 list_add_tail(&t->list, &tx->hci_list);
420 t->buf[0] = len & 0xff;
421 t->buf[1] = (len >> 8) & 0xff;
422 t->buf[2] = (len >> 16) & 0xff;
424 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
426 t->len = len + TYPE_A_HEADER_SIZE;
428 t->cb_data = cb_data;
432 schedule_work(&sdev->ws);
434 spin_unlock_irqrestore(&tx->lock, flags);
443 * Handle the HCI, WIMAX_SDU_TX_FLOW.
445 static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
447 struct tx_cxt *tx = &sdev->tx;
451 spin_lock_irqsave(&tx->lock, flags);
453 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
454 if (cmd_evt != WIMAX_SDU_TX_FLOW)
457 if (hci_data[4] == 0) {
458 dev_dbg(&sdev->func->dev, "WIMAX ==> STOP SDU TX\n");
460 } else if (hci_data[4] == 1) {
461 dev_dbg(&sdev->func->dev, "WIMAX ==> START SDU TX\n");
464 schedule_work(&sdev->ws);
466 * If free buffer for sdu tx doesn't exist, then tx queue
467 * should not be woken. For this reason, don't pass the command,
470 if (list_empty(&tx->free_list))
475 spin_unlock_irqrestore(&tx->lock, flags);
479 static void gdm_sdio_irq(struct sdio_func *func)
481 struct phy_dev *phy_dev = sdio_get_drvdata(func);
482 struct sdiowm_dev *sdev = phy_dev->priv_dev;
483 struct tx_cxt *tx = &sdev->tx;
484 struct rx_cxt *rx = &sdev->rx;
487 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
491 /* Check interrupt */
492 val = sdio_readb(func, 0x13, &ret);
494 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
498 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
501 "Cannot read from function %d\n", func->num);
505 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
506 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
507 dev_err(&func->dev, "Too big Type-A size: %d\n", len);
511 if (hdr[3] == 1) { /* Ack */
512 u32 *ack_seq = (u32 *)&hdr[4];
514 spin_lock_irqsave(&tx->lock, flags);
517 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
518 schedule_work(&sdev->ws);
519 spin_unlock_irqrestore(&tx->lock, flags);
520 dev_dbg(&func->dev, "Ack... %0x\n", ntohl(*ack_seq));
524 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
525 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
527 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
528 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
532 blocks = remain / func->cur_blksize;
535 n = blocks * func->cur_blksize;
536 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
539 "Cannot read from function %d\n", func->num);
547 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
550 "Cannot read from function %d\n", func->num);
556 print_hex_dump_debug("sdio_receive: ", DUMP_PREFIX_NONE, 16, 1,
557 rx->rx_buf, len, false);
558 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
560 spin_lock_irqsave(&rx->lock, flags);
562 if (!list_empty(&rx->req_list)) {
563 r = list_entry(rx->req_list.next, struct sdio_rx, list);
564 spin_unlock_irqrestore(&rx->lock, flags);
566 r->callback(r->cb_data, rx->rx_buf, len);
567 spin_lock_irqsave(&rx->lock, flags);
569 put_rx_struct(rx, r);
572 spin_unlock_irqrestore(&rx->lock, flags);
575 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
576 if (!phy_dev->netdev)
577 register_wimax_device(phy_dev, &func->dev);
580 static int gdm_sdio_receive(void *priv_dev,
581 void (*cb)(void *cb_data, void *data, int len),
584 struct sdiowm_dev *sdev = priv_dev;
585 struct rx_cxt *rx = &sdev->rx;
589 spin_lock_irqsave(&rx->lock, flags);
590 r = get_rx_struct(rx);
592 spin_unlock_irqrestore(&rx->lock, flags);
597 r->cb_data = cb_data;
599 list_add_tail(&r->list, &rx->req_list);
600 spin_unlock_irqrestore(&rx->lock, flags);
605 static int sdio_wimax_probe(struct sdio_func *func,
606 const struct sdio_device_id *id)
609 struct phy_dev *phy_dev = NULL;
610 struct sdiowm_dev *sdev = NULL;
612 dev_info(&func->dev, "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
613 func->vendor, func->device);
614 dev_info(&func->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
616 sdio_claim_host(func);
617 sdio_enable_func(func);
618 sdio_claim_irq(func, gdm_sdio_irq);
620 ret = sdio_boot(func);
624 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
625 if (phy_dev == NULL) {
629 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
635 phy_dev->priv_dev = (void *)sdev;
636 phy_dev->send_func = gdm_sdio_send;
637 phy_dev->rcv_func = gdm_sdio_receive;
639 ret = init_sdio(sdev);
645 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
646 sdio_release_host(func);
648 INIT_WORK(&sdev->ws, do_tx);
650 sdio_set_drvdata(func, phy_dev);
660 static void sdio_wimax_remove(struct sdio_func *func)
662 struct phy_dev *phy_dev = sdio_get_drvdata(func);
663 struct sdiowm_dev *sdev = phy_dev->priv_dev;
665 cancel_work_sync(&sdev->ws);
667 unregister_wimax_device(phy_dev);
668 sdio_claim_host(func);
669 sdio_release_irq(func);
670 sdio_disable_func(func);
671 sdio_release_host(func);
678 static const struct sdio_device_id sdio_wimax_ids[] = {
679 { SDIO_DEVICE(0x0296, 0x5347) },
683 MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
685 static struct sdio_driver sdio_wimax_driver = {
686 .probe = sdio_wimax_probe,
687 .remove = sdio_wimax_remove,
688 .name = "sdio_wimax",
689 .id_table = sdio_wimax_ids,
692 static int __init sdio_gdm_wimax_init(void)
694 return sdio_register_driver(&sdio_wimax_driver);
697 static void __exit sdio_gdm_wimax_exit(void)
699 sdio_unregister_driver(&sdio_wimax_driver);
702 module_init(sdio_gdm_wimax_init);
703 module_exit(sdio_gdm_wimax_exit);
705 MODULE_VERSION(DRIVER_VERSION);
706 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
707 MODULE_AUTHOR("Ethan Park");
708 MODULE_LICENSE("GPL");