rt2x00mmio.c 4.3 KB

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