blob: 6f236ea180aa3df23dbf8be831f367c020f4a1a2 [file] [log] [blame]
Gabor Juhos69a2bac2013-03-29 15:52:27 +01001/*
2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
3 <http://rt2x00.serialmonkey.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
Jeff Kirshera05b8c52013-12-06 03:32:11 -080016 along with this program; if not, see <http://www.gnu.org/licenses/>.
Gabor Juhos69a2bac2013-03-29 15:52:27 +010017 */
18
19/*
20 Module: rt2x00mmio
21 Abstract: rt2x00 generic mmio device routines.
22 */
23
24#include <linux/dma-mapping.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28
29#include "rt2x00.h"
30#include "rt2x00mmio.h"
31
32/*
33 * Register access.
34 */
Gabor Juhos58959bd2013-04-05 08:27:00 +020035int rt2x00mmio_regbusy_read(struct rt2x00_dev *rt2x00dev,
36 const unsigned int offset,
37 const struct rt2x00_field32 field,
38 u32 *reg)
Gabor Juhos69a2bac2013-03-29 15:52:27 +010039{
40 unsigned int i;
41
42 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
43 return 0;
44
45 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Gabor Juhos58959bd2013-04-05 08:27:00 +020046 rt2x00mmio_register_read(rt2x00dev, offset, reg);
Gabor Juhos69a2bac2013-03-29 15:52:27 +010047 if (!rt2x00_get_field32(*reg, field))
48 return 1;
49 udelay(REGISTER_BUSY_DELAY);
50 }
51
52 printk_once(KERN_ERR "%s() Indirect register access failed: "
53 "offset=0x%.08x, value=0x%.08x\n", __func__, offset, *reg);
54 *reg = ~0;
55
56 return 0;
57}
Gabor Juhos58959bd2013-04-05 08:27:00 +020058EXPORT_SYMBOL_GPL(rt2x00mmio_regbusy_read);
Gabor Juhos69a2bac2013-03-29 15:52:27 +010059
Gabor Juhos58959bd2013-04-05 08:27:00 +020060bool rt2x00mmio_rxdone(struct rt2x00_dev *rt2x00dev)
Gabor Juhos69a2bac2013-03-29 15:52:27 +010061{
62 struct data_queue *queue = rt2x00dev->rx;
63 struct queue_entry *entry;
Gabor Juhos58959bd2013-04-05 08:27:00 +020064 struct queue_entry_priv_mmio *entry_priv;
Gabor Juhos69a2bac2013-03-29 15:52:27 +010065 struct skb_frame_desc *skbdesc;
66 int max_rx = 16;
67
68 while (--max_rx) {
69 entry = rt2x00queue_get_entry(queue, Q_INDEX);
70 entry_priv = entry->priv_data;
71
72 if (rt2x00dev->ops->lib->get_entry_state(entry))
73 break;
74
75 /*
76 * Fill in desc fields of the skb descriptor
77 */
78 skbdesc = get_skb_frame_desc(entry->skb);
79 skbdesc->desc = entry_priv->desc;
80 skbdesc->desc_len = entry->queue->desc_size;
81
82 /*
83 * DMA is already done, notify rt2x00lib that
84 * it finished successfully.
85 */
86 rt2x00lib_dmastart(entry);
87 rt2x00lib_dmadone(entry);
88
89 /*
90 * Send the frame to rt2x00lib for further processing.
91 */
92 rt2x00lib_rxdone(entry, GFP_ATOMIC);
93 }
94
95 return !max_rx;
96}
Gabor Juhos58959bd2013-04-05 08:27:00 +020097EXPORT_SYMBOL_GPL(rt2x00mmio_rxdone);
Gabor Juhos69a2bac2013-03-29 15:52:27 +010098
Gabor Juhos58959bd2013-04-05 08:27:00 +020099void rt2x00mmio_flush_queue(struct data_queue *queue, bool drop)
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100100{
101 unsigned int i;
102
103 for (i = 0; !rt2x00queue_empty(queue) && i < 10; i++)
104 msleep(10);
105}
Gabor Juhos58959bd2013-04-05 08:27:00 +0200106EXPORT_SYMBOL_GPL(rt2x00mmio_flush_queue);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100107
108/*
109 * Device initialization handlers.
110 */
Gabor Juhos58959bd2013-04-05 08:27:00 +0200111static int rt2x00mmio_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
112 struct data_queue *queue)
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100113{
Gabor Juhos58959bd2013-04-05 08:27:00 +0200114 struct queue_entry_priv_mmio *entry_priv;
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100115 void *addr;
116 dma_addr_t dma;
117 unsigned int i;
118
119 /*
120 * Allocate DMA memory for descriptor and buffer.
121 */
122 addr = dma_alloc_coherent(rt2x00dev->dev,
123 queue->limit * queue->desc_size,
124 &dma, GFP_KERNEL);
125 if (!addr)
126 return -ENOMEM;
127
128 memset(addr, 0, queue->limit * queue->desc_size);
129
130 /*
131 * Initialize all queue entries to contain valid addresses.
132 */
133 for (i = 0; i < queue->limit; i++) {
134 entry_priv = queue->entries[i].priv_data;
135 entry_priv->desc = addr + i * queue->desc_size;
136 entry_priv->desc_dma = dma + i * queue->desc_size;
137 }
138
139 return 0;
140}
141
Gabor Juhos58959bd2013-04-05 08:27:00 +0200142static void rt2x00mmio_free_queue_dma(struct rt2x00_dev *rt2x00dev,
143 struct data_queue *queue)
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100144{
Gabor Juhos58959bd2013-04-05 08:27:00 +0200145 struct queue_entry_priv_mmio *entry_priv =
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100146 queue->entries[0].priv_data;
147
148 if (entry_priv->desc)
149 dma_free_coherent(rt2x00dev->dev,
150 queue->limit * queue->desc_size,
151 entry_priv->desc, entry_priv->desc_dma);
152 entry_priv->desc = NULL;
153}
154
Gabor Juhos58959bd2013-04-05 08:27:00 +0200155int rt2x00mmio_initialize(struct rt2x00_dev *rt2x00dev)
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100156{
157 struct data_queue *queue;
158 int status;
159
160 /*
161 * Allocate DMA
162 */
163 queue_for_each(rt2x00dev, queue) {
Gabor Juhos58959bd2013-04-05 08:27:00 +0200164 status = rt2x00mmio_alloc_queue_dma(rt2x00dev, queue);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100165 if (status)
166 goto exit;
167 }
168
169 /*
170 * Register interrupt handler.
171 */
172 status = request_irq(rt2x00dev->irq,
173 rt2x00dev->ops->lib->irq_handler,
174 IRQF_SHARED, rt2x00dev->name, rt2x00dev);
175 if (status) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700176 rt2x00_err(rt2x00dev, "IRQ %d allocation failed (error %d)\n",
177 rt2x00dev->irq, status);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100178 goto exit;
179 }
180
181 return 0;
182
183exit:
184 queue_for_each(rt2x00dev, queue)
Gabor Juhos58959bd2013-04-05 08:27:00 +0200185 rt2x00mmio_free_queue_dma(rt2x00dev, queue);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100186
187 return status;
188}
Gabor Juhos58959bd2013-04-05 08:27:00 +0200189EXPORT_SYMBOL_GPL(rt2x00mmio_initialize);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100190
Gabor Juhos58959bd2013-04-05 08:27:00 +0200191void rt2x00mmio_uninitialize(struct rt2x00_dev *rt2x00dev)
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100192{
193 struct data_queue *queue;
194
195 /*
196 * Free irq line.
197 */
198 free_irq(rt2x00dev->irq, rt2x00dev);
199
200 /*
201 * Free DMA
202 */
203 queue_for_each(rt2x00dev, queue)
Gabor Juhos58959bd2013-04-05 08:27:00 +0200204 rt2x00mmio_free_queue_dma(rt2x00dev, queue);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100205}
Gabor Juhos58959bd2013-04-05 08:27:00 +0200206EXPORT_SYMBOL_GPL(rt2x00mmio_uninitialize);
Gabor Juhos69a2bac2013-03-29 15:52:27 +0100207
208/*
209 * rt2x00mmio module information.
210 */
211MODULE_AUTHOR(DRV_PROJECT);
212MODULE_VERSION(DRV_VERSION);
213MODULE_DESCRIPTION("rt2x00 mmio library");
214MODULE_LICENSE("GPL");