aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/mach-common/usb_tio.c
blob: 95ace6db71fda9f28c8dc7d704ff4cb217381c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * arch/ubicom32/mach-common/usb_tio.c
 *  Linux side Ubicom USB TIO driver
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <asm/devtree.h>
#include "usb_tio.h"

#ifdef CONFIG_SMP
static DEFINE_SPINLOCK(tio_lock);
#define USB_TIO_LOCK(lock, flag) spin_lock_irqsave(lock, flag)
#define USB_TIO_UNLOCK(lock, flag) spin_unlock_irqrestore(lock, flag)
#define USB_TIO_LOCK_ISLOCKED(lock) spin_try_lock(lock)
#else
#define USB_TIO_LOCK(lock, flag) local_irq_save(flag)
#define USB_TIO_UNLOCK(lock, flag) local_irq_restore(flag)
#endif

spinlock_t usb_tio_lock;

/*
 * usb_tio_set_hrt_interrupt()
 */
static inline void usb_tio_set_hrt_interrupt(void)
{
	ubicom32_set_interrupt(usb_node->dn.sendirq);
}

static inline void usb_tio_wait_hrt(void)
{
	while (unlikely(usb_node->pdesc));
}

#if defined(USB_TIO_DEBUG)
static void usb_tio_request_verify_magic(volatile struct usb_tio_request *req)
{
	BUG_ON(req->magic != USB_TIO_REQUEST_MAGIC2);
}

static void usb_tio_request_clear_magic(volatile struct usb_tio_request *req)
{
	req->magic = 0;
}
#endif

static void usb_tio_request_set_magic(volatile struct usb_tio_request *req)
{
	req->magic = USB_TIO_REQUEST_MAGIC1;
}

/*
 * usb_tio_commit_request()
 */
static inline void usb_tio_commit_request(volatile struct usb_tio_request *request)
{
	wmb();
	usb_node->pdesc = request;

	/*
	 * next thing to do is alway checking if (usb_node->pdesc == NULL)
	 * to see if the request is done, so add a mb() here
	 */
	mb();
	usb_tio_set_hrt_interrupt();
}

/*
 * usb_tio_read_u16()
 *	Synchronously read 16 bits.
 */
u8_t usb_tio_read_u16(u32_t address, u16_t *data)
{
	volatile struct usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	/*
	 * Wait for any previous request to complete and then make this request.
	 */
	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	/*
	 * Fill in the request.
	 */
	tio_req->address = address;
	tio_req->cmd = USB_TIO_READ16_SYNC;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);
	usb_tio_commit_request(tio_req);

	/*
	 * Wait for the result to show up.
	 */
	usb_tio_wait_hrt();
	USB_TIO_REQUEST_VERIFY_MAGIC(tio_req);
	*data = (u16_t)tio_req->data;
	USB_TIO_REQUEST_CLEAR_MAGIC(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_read_u8()
 *	Synchronously read 16 bits.
 */
u8_t usb_tio_read_u8(u32_t address, u8_t *data)
{
	volatile struct usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	/*
	 * Wait for any previous request to complete and then make this request.
	 */
	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	/*
	 * Fill in the request.
	 */
	tio_req->address = address;
	tio_req->cmd = USB_TIO_READ8_SYNC;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);

	/*
	 * commit the request
	 */
	usb_tio_commit_request(tio_req);

	/*
	 * Wait for the result to show up.
	 */
	usb_tio_wait_hrt();
	USB_TIO_REQUEST_VERIFY_MAGIC(tio_req);
	*data = (u8_t)tio_req->data;
	USB_TIO_REQUEST_CLEAR_MAGIC(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_write_u16()
 *	Asynchronously  write 16 bits.
 */
u8_t usb_tio_write_u16(u32_t address, u16_t data)
{
	volatile struct usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	/*
	 * Wait for any previous write or pending read to complete.
	 */
	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	tio_req->address = address;
	tio_req->data = data;
	tio_req->cmd = USB_TIO_WRITE16_ASYNC;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);

	/*
	 * commit the request
	 */
	usb_tio_commit_request(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_write_u8()
 *	Asynchronously  write 8 bits.
 */
u8_t usb_tio_write_u8(u32_t address, u8_t data)
{
	volatile struct usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	/*
	 * Wait for any previous write or pending read to complete.
	 */
	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	tio_req->address = address;
	tio_req->data = data;
	tio_req->cmd = USB_TIO_WRITE8_ASYNC;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);

	/*
	 * commit the request
	 */
	usb_tio_commit_request(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_read_fifo()
 *	Synchronously read FIFO.
 */
u8_t usb_tio_read_fifo(u32_t address, u32_t buffer, u32_t bytes)
{
	volatile struct usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	/*
	 * Wait for any previous request to complete and then make this request.
	 */
	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	/*
	 * Fill in the request.
	 */
	tio_req->address = address;
	tio_req->cmd = USB_TIO_READ_FIFO_SYNC;
	tio_req->buffer = buffer;
	tio_req->transfer_length = bytes;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);

	/*
	 * commit the request
	 */
	usb_tio_commit_request(tio_req);

	 /*
	 * Wait for the result to show up.
	 */
	usb_tio_wait_hrt();
	USB_TIO_REQUEST_VERIFY_MAGIC(tio_req);
	USB_TIO_REQUEST_CLEAR_MAGIC(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_write_fifo()
 *	Synchronously  write 32 bits.
 */
u8_t usb_tio_write_fifo(u32_t address, u32_t buffer, u32_t bytes)
{
	volatile struct	usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	tio_req->address = address;
	tio_req->buffer = buffer;
	tio_req->cmd = USB_TIO_WRITE_FIFO_SYNC;
	tio_req->transfer_length = bytes;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);
	/*
	 * commit the request
	 */
	usb_tio_commit_request(tio_req);

	/*
	 * Wait for the result to show up.
	 */
	usb_tio_wait_hrt();
	USB_TIO_REQUEST_VERIFY_MAGIC(tio_req);
	USB_TIO_REQUEST_CLEAR_MAGIC(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_write_fifo_async()
 *	Asynchronously write 32 bits.
 */
u8_t usb_tio_write_fifo_async(u32_t address, u32_t buffer, u32_t bytes)
{
	volatile struct	usb_tio_request *tio_req = &usb_node->request;
	unsigned long flag;

	USB_TIO_LOCK(&tio_lock, flag);
	usb_tio_wait_hrt();

	tio_req->address = address;

	/*
	 * Is it necessary to make a local copy of the buffer? Any chance the URB is aborted before TIO finished the FIFO write?
	 */
	tio_req->buffer = buffer;
	tio_req->cmd = USB_TIO_WRITE_FIFO_SYNC;
	tio_req->transfer_length = bytes;
	USB_TIO_REQUEST_SET_MAGIC(tio_req);
	/*
	 * commit the request
	 */
	usb_tio_commit_request(tio_req);
	USB_TIO_UNLOCK(&tio_lock, flag);
	return USB_TIO_OK;
}

/*
 * usb_tio_read_int_status()
 * 	read and clear the interrupt status registers
 */
void usb_tio_read_int_status(u8_t *int_usb, u16_t *int_tx, u16_t *int_rx)
{

	/*
	 * clear the interrupt must be syncronized with the TIO thread to prevent the racing condiiton
	 * that TIO thread try to set it at same time
	 */
	asm volatile (
	"1:	bset (%0), (%0), #0	\n\t" \
	"	jmpne.f 1b		\n\t" \
		:
		: "a" (&usb_node->usb_vp_control)
		: "memory", "cc"
	);

	*int_usb = usb_node->usb_vp_hw_int_usb;
	*int_tx  = cpu_to_le16(usb_node->usb_vp_hw_int_tx);
	*int_rx  = cpu_to_le16(usb_node->usb_vp_hw_int_rx);

	//printk(KERN_INFO "int read %x, %x, %x\n", *int_usb, *int_tx, *int_rx);

	/*
	 * The interrupt status register is read-clean, so clear it now
	 */
	usb_node->usb_vp_hw_int_usb = 0;
	usb_node->usb_vp_hw_int_tx = 0;
	usb_node->usb_vp_hw_int_rx = 0;

	/*
	 * release the lock bit
	 */
	usb_node->usb_vp_control &= 0xfffe;
}