rt2x00mmio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Module: rt2x00mmio
  17. Abstract: rt2x00 generic mmio device routines.
  18. */
  19. #include <linux/dma-mapping.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00mmio.h"
  25. /*
  26. * Register access.
  27. */
  28. int rt2x00mmio_regbusy_read(struct rt2x00_dev *rt2x00dev,
  29. const unsigned int offset,
  30. const struct rt2x00_field32 field,
  31. u32 *reg)
  32. {
  33. unsigned int i;
  34. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  35. return 0;
  36. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  37. rt2x00mmio_register_read(rt2x00dev, offset, reg);
  38. if (!rt2x00_get_field32(*reg, field))
  39. return 1;
  40. udelay(REGISTER_BUSY_DELAY);
  41. }
  42. printk_once(KERN_ERR "%s() Indirect register access failed: "
  43. "offset=0x%.08x, value=0x%.08x\n", __func__, offset, *reg);
  44. *reg = ~0;
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(rt2x00mmio_regbusy_read);
  48. bool rt2x00mmio_rxdone(struct rt2x00_dev *rt2x00dev)
  49. {
  50. struct data_queue *queue = rt2x00dev->rx;
  51. struct queue_entry *entry;
  52. struct queue_entry_priv_mmio *entry_priv;
  53. struct skb_frame_desc *skbdesc;
  54. int max_rx = 16;
  55. while (--max_rx) {
  56. entry = rt2x00queue_get_entry(queue, Q_INDEX);
  57. entry_priv = entry->priv_data;
  58. if (rt2x00dev->ops->lib->get_entry_state(entry))
  59. break;
  60. /*
  61. * Fill in desc fields of the skb descriptor
  62. */
  63. skbdesc = get_skb_frame_desc(entry->skb);
  64. skbdesc->desc = entry_priv->desc;
  65. skbdesc->desc_len = entry->queue->desc_size;
  66. /*
  67. * DMA is already done, notify rt2x00lib that
  68. * it finished successfully.
  69. */
  70. rt2x00lib_dmastart(entry);
  71. rt2x00lib_dmadone(entry);
  72. /*
  73. * Send the frame to rt2x00lib for further processing.
  74. */
  75. rt2x00lib_rxdone(entry, GFP_ATOMIC);
  76. }
  77. return !max_rx;
  78. }
  79. EXPORT_SYMBOL_GPL(rt2x00mmio_rxdone);
  80. void rt2x00mmio_flush_queue(struct data_queue *queue, bool drop)
  81. {
  82. unsigned int i;
  83. for (i = 0; !rt2x00queue_empty(queue) && i < 10; i++)
  84. msleep(10);
  85. }
  86. EXPORT_SYMBOL_GPL(rt2x00mmio_flush_queue);
  87. /*
  88. * Device initialization handlers.
  89. */
  90. static int rt2x00mmio_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
  91. struct data_queue *queue)
  92. {
  93. struct queue_entry_priv_mmio *entry_priv;
  94. void *addr;
  95. dma_addr_t dma;
  96. unsigned int i;
  97. /*
  98. * Allocate DMA memory for descriptor and buffer.
  99. */
  100. addr = dma_alloc_coherent(rt2x00dev->dev,
  101. queue->limit * queue->desc_size,
  102. &dma, GFP_KERNEL);
  103. if (!addr)
  104. return -ENOMEM;
  105. memset(addr, 0, queue->limit * queue->desc_size);
  106. /*
  107. * Initialize all queue entries to contain valid addresses.
  108. */
  109. for (i = 0; i < queue->limit; i++) {
  110. entry_priv = queue->entries[i].priv_data;
  111. entry_priv->desc = addr + i * queue->desc_size;
  112. entry_priv->desc_dma = dma + i * queue->desc_size;
  113. }
  114. return 0;
  115. }
  116. static void rt2x00mmio_free_queue_dma(struct rt2x00_dev *rt2x00dev,
  117. struct data_queue *queue)
  118. {
  119. struct queue_entry_priv_mmio *entry_priv =
  120. queue->entries[0].priv_data;
  121. if (entry_priv->desc)
  122. dma_free_coherent(rt2x00dev->dev,
  123. queue->limit * queue->desc_size,
  124. entry_priv->desc, entry_priv->desc_dma);
  125. entry_priv->desc = NULL;
  126. }
  127. int rt2x00mmio_initialize(struct rt2x00_dev *rt2x00dev)
  128. {
  129. struct data_queue *queue;
  130. int status;
  131. /*
  132. * Allocate DMA
  133. */
  134. queue_for_each(rt2x00dev, queue) {
  135. status = rt2x00mmio_alloc_queue_dma(rt2x00dev, queue);
  136. if (status)
  137. goto exit;
  138. }
  139. /*
  140. * Register interrupt handler.
  141. */
  142. status = request_irq(rt2x00dev->irq,
  143. rt2x00dev->ops->lib->irq_handler,
  144. IRQF_SHARED, rt2x00dev->name, rt2x00dev);
  145. if (status) {
  146. rt2x00_err(rt2x00dev, "IRQ %d allocation failed (error %d)\n",
  147. rt2x00dev->irq, status);
  148. goto exit;
  149. }
  150. return 0;
  151. exit:
  152. queue_for_each(rt2x00dev, queue)
  153. rt2x00mmio_free_queue_dma(rt2x00dev, queue);
  154. return status;
  155. }
  156. EXPORT_SYMBOL_GPL(rt2x00mmio_initialize);
  157. void rt2x00mmio_uninitialize(struct rt2x00_dev *rt2x00dev)
  158. {
  159. struct data_queue *queue;
  160. /*
  161. * Free irq line.
  162. */
  163. free_irq(rt2x00dev->irq, rt2x00dev);
  164. /*
  165. * Free DMA
  166. */
  167. queue_for_each(rt2x00dev, queue)
  168. rt2x00mmio_free_queue_dma(rt2x00dev, queue);
  169. }
  170. EXPORT_SYMBOL_GPL(rt2x00mmio_uninitialize);
  171. /*
  172. * rt2x00mmio module information.
  173. */
  174. MODULE_AUTHOR(DRV_PROJECT);
  175. MODULE_VERSION(DRV_VERSION);
  176. MODULE_DESCRIPTION("rt2x00 mmio library");
  177. MODULE_LICENSE("GPL");