1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4 <http://rt2x00.serialmonkey.com>
10 Abstract: rt2x00 generic mmio device routines.
13 #include <linux/dma-mapping.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
19 #include "rt2x00mmio.h"
24 int rt2x00mmio_regbusy_read(struct rt2x00_dev
*rt2x00dev
,
25 const unsigned int offset
,
26 const struct rt2x00_field32 field
,
31 if (!test_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
))
34 for (i
= 0; i
< REGISTER_BUSY_COUNT
; i
++) {
35 *reg
= rt2x00mmio_register_read(rt2x00dev
, offset
);
36 if (!rt2x00_get_field32(*reg
, field
))
38 udelay(REGISTER_BUSY_DELAY
);
41 printk_once(KERN_ERR
"%s() Indirect register access failed: "
42 "offset=0x%.08x, value=0x%.08x\n", __func__
, offset
, *reg
);
47 EXPORT_SYMBOL_GPL(rt2x00mmio_regbusy_read
);
49 bool rt2x00mmio_rxdone(struct rt2x00_dev
*rt2x00dev
)
51 struct data_queue
*queue
= rt2x00dev
->rx
;
52 struct queue_entry
*entry
;
53 struct queue_entry_priv_mmio
*entry_priv
;
54 struct skb_frame_desc
*skbdesc
;
58 entry
= rt2x00queue_get_entry(queue
, Q_INDEX
);
59 entry_priv
= entry
->priv_data
;
61 if (rt2x00dev
->ops
->lib
->get_entry_state(entry
))
65 * Fill in desc fields of the skb descriptor
67 skbdesc
= get_skb_frame_desc(entry
->skb
);
68 skbdesc
->desc
= entry_priv
->desc
;
69 skbdesc
->desc_len
= entry
->queue
->desc_size
;
72 * DMA is already done, notify rt2x00lib that
73 * it finished successfully.
75 rt2x00lib_dmastart(entry
);
76 rt2x00lib_dmadone(entry
);
79 * Send the frame to rt2x00lib for further processing.
81 rt2x00lib_rxdone(entry
, GFP_ATOMIC
);
86 EXPORT_SYMBOL_GPL(rt2x00mmio_rxdone
);
88 void rt2x00mmio_flush_queue(struct data_queue
*queue
, bool drop
)
92 for (i
= 0; !rt2x00queue_empty(queue
) && i
< 10; i
++)
95 EXPORT_SYMBOL_GPL(rt2x00mmio_flush_queue
);
98 * Device initialization handlers.
100 static int rt2x00mmio_alloc_queue_dma(struct rt2x00_dev
*rt2x00dev
,
101 struct data_queue
*queue
)
103 struct queue_entry_priv_mmio
*entry_priv
;
109 * Allocate DMA memory for descriptor and buffer.
111 addr
= dma_alloc_coherent(rt2x00dev
->dev
,
112 queue
->limit
* queue
->desc_size
, &dma
,
118 * Initialize all queue entries to contain valid addresses.
120 for (i
= 0; i
< queue
->limit
; i
++) {
121 entry_priv
= queue
->entries
[i
].priv_data
;
122 entry_priv
->desc
= addr
+ i
* queue
->desc_size
;
123 entry_priv
->desc_dma
= dma
+ i
* queue
->desc_size
;
129 static void rt2x00mmio_free_queue_dma(struct rt2x00_dev
*rt2x00dev
,
130 struct data_queue
*queue
)
132 struct queue_entry_priv_mmio
*entry_priv
=
133 queue
->entries
[0].priv_data
;
135 if (entry_priv
->desc
)
136 dma_free_coherent(rt2x00dev
->dev
,
137 queue
->limit
* queue
->desc_size
,
138 entry_priv
->desc
, entry_priv
->desc_dma
);
139 entry_priv
->desc
= NULL
;
142 int rt2x00mmio_initialize(struct rt2x00_dev
*rt2x00dev
)
144 struct data_queue
*queue
;
150 queue_for_each(rt2x00dev
, queue
) {
151 status
= rt2x00mmio_alloc_queue_dma(rt2x00dev
, queue
);
157 * Register interrupt handler.
159 status
= request_irq(rt2x00dev
->irq
,
160 rt2x00dev
->ops
->lib
->irq_handler
,
161 IRQF_SHARED
, rt2x00dev
->name
, rt2x00dev
);
163 rt2x00_err(rt2x00dev
, "IRQ %d allocation failed (error %d)\n",
164 rt2x00dev
->irq
, status
);
171 queue_for_each(rt2x00dev
, queue
)
172 rt2x00mmio_free_queue_dma(rt2x00dev
, queue
);
176 EXPORT_SYMBOL_GPL(rt2x00mmio_initialize
);
178 void rt2x00mmio_uninitialize(struct rt2x00_dev
*rt2x00dev
)
180 struct data_queue
*queue
;
185 free_irq(rt2x00dev
->irq
, rt2x00dev
);
190 queue_for_each(rt2x00dev
, queue
)
191 rt2x00mmio_free_queue_dma(rt2x00dev
, queue
);
193 EXPORT_SYMBOL_GPL(rt2x00mmio_uninitialize
);
196 * rt2x00mmio module information.
198 MODULE_AUTHOR(DRV_PROJECT
);
199 MODULE_VERSION(DRV_VERSION
);
200 MODULE_DESCRIPTION("rt2x00 mmio library");
201 MODULE_LICENSE("GPL");