summaryrefslogtreecommitdiffstats
path: root/drivers/tty/metag_da.c
blob: 25ccef2fe748b5c753ca40da356cc6441e5e2a77 (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 *  dashtty.c - tty driver for Dash channels interface.
 *
 *  Copyright (C) 2007,2008,2012 Imagination Technologies
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive
 * for more details.
 *
 */

#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/export.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/serial.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/uaccess.h>

#include <asm/da.h>

/* Channel error codes */
#define CONAOK	0
#define CONERR	1
#define CONBAD	2
#define CONPRM	3
#define CONADR	4
#define CONCNT	5
#define CONCBF	6
#define CONCBE	7
#define CONBSY	8

/* Default channel for the console */
#define CONSOLE_CHANNEL      1

#define NUM_TTY_CHANNELS     6

/* Auto allocate */
#define DA_TTY_MAJOR        0

/* A speedy poll rate helps the userland debug process connection response.
 * But, if you set it too high then no other userland processes get much
 * of a look in.
 */
#define DA_TTY_POLL (HZ / 50)

/*
 * A short put delay improves latency but has a high throughput overhead
 */
#define DA_TTY_PUT_DELAY (HZ / 100)

static atomic_t num_channels_need_poll = ATOMIC_INIT(0);

static struct timer_list poll_timer;

static struct tty_driver *channel_driver;

static struct timer_list put_timer;
static struct task_struct *dashtty_thread;

/*
 * The console_poll parameter determines whether the console channel should be
 * polled for input.
 * By default the console channel isn't polled at all, in order to avoid the
 * overhead, but that means it isn't possible to have a login on /dev/console.
 */
static bool console_poll;
module_param(console_poll, bool, S_IRUGO);

#define RX_BUF_SIZE 1024

enum {
	INCHR = 1,
	OUTCHR,
	RDBUF,
	WRBUF,
	RDSTAT
};

/**
 * struct dashtty_port - Wrapper struct for dashtty tty_port.
 * @port:		TTY port data
 * @rx_lock:		Lock for rx_buf.
 *			This protects between the poll timer and user context.
 *			It's also held during read SWITCH operations.
 * @rx_buf:		Read buffer
 * @xmit_lock:		Lock for xmit_*, and port.xmit_buf.
 *			This protects between user context and kernel thread.
 *			It's also held during write SWITCH operations.
 * @xmit_cnt:		Size of xmit buffer contents
 * @xmit_head:		Head of xmit buffer where data is written
 * @xmit_tail:		Tail of xmit buffer where data is read
 * @xmit_empty:		Completion for xmit buffer being empty
 */
struct dashtty_port {
	struct tty_port		 port;
	spinlock_t		 rx_lock;
	void			*rx_buf;
	struct mutex		 xmit_lock;
	unsigned int		 xmit_cnt;
	unsigned int		 xmit_head;
	unsigned int		 xmit_tail;
	struct completion	 xmit_empty;
};

static struct dashtty_port dashtty_ports[NUM_TTY_CHANNELS];

static atomic_t dashtty_xmit_cnt = ATOMIC_INIT(0);
static wait_queue_head_t dashtty_waitqueue;

/*
 * Low-level DA channel access routines
 */
static int chancall(int in_bios_function, int in_channel,
		    int in_arg2, void *in_arg3,
		    void *in_arg4)
{
	register int   bios_function asm("D1Ar1") = in_bios_function;
	register int   channel       asm("D0Ar2") = in_channel;
	register int   arg2          asm("D1Ar3") = in_arg2;
	register void *arg3          asm("D0Ar4") = in_arg3;
	register void *arg4          asm("D1Ar5") = in_arg4;
	register int   bios_call     asm("D0Ar6") = 3;
	register int   result        asm("D0Re0");

	asm volatile (
		"MSETL	[A0StP++], %6,%4,%2\n\t"
		"ADD	A0StP, A0StP, #8\n\t"
		"SWITCH	#0x0C30208\n\t"
		"GETD	%0, [A0StP+#-8]\n\t"
		"SUB	A0StP, A0StP, #(4*6)+8\n\t"
		: "=d" (result)   /* outs */
		: "d" (bios_function),
		  "d" (channel),
		  "d" (arg2),
		  "d" (arg3),
		  "d" (arg4),
		  "d" (bios_call) /* ins */
		: "memory");

	return result;
}

/*
 * Attempts to fetch count bytes from channel and returns actual count.
 */
static int fetch_data(unsigned int channel)
{
	struct dashtty_port *dport = &dashtty_ports[channel];
	int received = 0;

	spin_lock_bh(&dport->rx_lock);
	/* check the port isn't being shut down */
	if (!dport->rx_buf)
		goto unlock;
	if (chancall(RDBUF, channel, RX_BUF_SIZE,
		     (void *)dport->rx_buf, &received) == CONAOK) {
		if (received) {
			int space;
			unsigned char *cbuf;

			space = tty_prepare_flip_string(&dport->port, &cbuf,
							received);

			if (space <= 0)
				goto unlock;

			memcpy(cbuf, dport->rx_buf, space);
			tty_flip_buffer_push(&dport->port);
		}
	}
unlock:
	spin_unlock_bh(&dport->rx_lock);

	return received;
}

/**
 * find_channel_to_poll() - Returns number of the next channel to poll.
 * Returns:	The number of the next channel to poll, or -1 if none need
 *		polling.
 */
static int find_channel_to_poll(void)
{
	static int last_polled_channel;
	int last = last_polled_channel;
	int chan;
	struct dashtty_port *dport;

	for (chan = last + 1; ; ++chan) {
		if (chan >= NUM_TTY_CHANNELS)
			chan = 0;

		dport = &dashtty_ports[chan];
		if (dport->rx_buf) {
			last_polled_channel = chan;
			return chan;
		}

		if (chan == last)
			break;
	}
	return -1;
}

/**
 * put_channel_data() - Write out a block of channel data.
 * @chan:	DA channel number.
 *
 * Write a single block of data out to the debug adapter. If the circular buffer
 * is wrapped then only the first block is written.
 *
 * Returns:	1 if the remote buffer was too full to accept data.
 *		0 otherwise.
 */
static int put_channel_data(unsigned int chan)
{
	struct dashtty_port *dport;
	struct tty_struct *tty;
	int number_written;
	unsigned int count = 0;

	dport = &dashtty_ports[chan];
	mutex_lock(&dport->xmit_lock);
	if (dport->xmit_cnt) {
		count = min((unsigned int)(SERIAL_XMIT_SIZE - dport->xmit_tail),
			    dport->xmit_cnt);
		chancall(WRBUF, chan, count,
			 dport->port.xmit_buf + dport->xmit_tail,
			 &number_written);
		dport->xmit_cnt -= number_written;
		if (!dport->xmit_cnt) {
			/* reset pointers to avoid wraps */
			dport->xmit_head = 0;
			dport->xmit_tail = 0;
			complete(&dport->xmit_empty);
		} else {
			dport->xmit_tail += number_written;
			if (dport->xmit_tail >= SERIAL_XMIT_SIZE)
				dport->xmit_tail -= SERIAL_XMIT_SIZE;
		}
		atomic_sub(number_written, &dashtty_xmit_cnt);
	}
	mutex_unlock(&dport->xmit_lock);

	/* if we've made more data available, wake up tty */
	if (count && number_written) {
		tty = tty_port_tty_get(&dport->port);
		if (tty) {
			tty_wakeup(tty);
			tty_kref_put(tty);
		}
	}

	/* did the write fail? */
	return count && !number_written;
}

/**
 * put_data() - Kernel thread to write out blocks of channel data to DA.
 * @arg:	Unused.
 *
 * This kernel thread runs while @dashtty_xmit_cnt != 0, and loops over the