rt2x00mmio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_zalloc_coherent(rt2x00dev->dev,
  101. queue->limit * queue->desc_size, &dma,
  102. GFP_KERNEL);
  103. if (!addr)
  104. return -ENOMEM;
  105. /*
  106. * Initialize all queue entries to contain valid addresses.
  107. */
  108. for (i = 0; i < queue->limit; i++) {
  109. entry_priv = queue->entries[i].priv_data;
  110. entry_priv->desc = addr + i * queue->desc_size;
  111. entry_priv->desc_dma = dma + i * queue->desc_size;
  112. }
  113. return 0;
  114. }
  115. static void rt2x00mmio_free_queue_dma(struct rt2x00_dev *rt2x00dev,
  116. struct data_queue *queue)
  117. {
  118. struct queue_entry_priv_mmio *entry_priv =
  119. queue->entries[0].priv_data;
  120. if (entry_priv->desc)
  121. dma_free_coherent(rt2x00dev->dev,
  122. queue->limit * queue->desc_size,
  123. entry_priv->desc, entry_priv->desc_dma);
  124. entry_priv->desc = NULL;
  125. }
  126. int rt2x00mmio_initialize(struct rt2x00_dev *rt2x00dev)
  127. {
  128. struct data_queue *queue;
  129. int status;
  130. /*
  131. * Allocate DMA
  132. */
  133. queue_for_each(rt2x00dev, queue) {
  134. status = rt2x00mmio_alloc_queue_dma(rt2x00dev, queue);
  135. if (status)
  136. goto exit;
  137. }
  138. /*
  139. * Register interrupt handler.
  140. */
  141. status = request_irq(rt2x00dev->irq,
  142. rt2x00dev->ops->lib->irq_handler,
  143. IRQF_SHARED, rt2x00dev->name, rt2x00dev);
  144. if (status) {
  145. rt2x00_err(rt2x00dev, "IRQ %d allocation failed (error %d)\n",
  146. rt2x00dev->irq, status);
  147. goto exit;
  148. }
  149. return 0;
  150. exit:
  151. queue_for_each(rt2x00dev, queue)
  152. rt2x00mmio_free_queue_dma(rt2x00dev, queue);
  153. return status;
  154. }
  155. EXPORT_SYMBOL_GPL(rt2x00mmio_initialize);
  156. void rt2x00mmio_uninitialize(struct rt2x00_dev *rt2x00dev)
  157. {
  158. struct data_queue *queue;
  159. /*
  160. * Free irq line.
  161. */
  162. free_irq(rt2x00dev->irq, rt2x00dev);
  163. /*
  164. * Free DMA
  165. */
  166. queue_for_each(rt2x00dev, queue)
  167. rt2x00mmio_free_queue_dma(rt2x00dev, queue);
  168. }
  169. EXPORT_SYMBOL_GPL(rt2x00mmio_uninitialize);
  170. /*
  171. * rt2x00mmio module information.
  172. */
  173. MODULE_AUTHOR(DRV_PROJECT);
  174. MODULE_VERSION(DRV_VERSION);
  175. MODULE_DESCRIPTION("rt2x00 mmio library");
  176. MODULE_LICENSE("GPL");