rt2x00mmio.c 4.9 KB

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