Blame view

drivers/serial/vr41xx_siu.c 19.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   *  Driver for NEC VR4100 series Serial Interface Unit.
   *
891649409   Yoichi Yuasa   [MIPS] separate p...
4
   *  Copyright (C) 2004-2007  Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *
   *  Based on drivers/serial/8250.c, by Russell King.
   *
   *  This program 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.
   *
   *  This program 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 this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
  
  #if defined(CONFIG_SERIAL_VR41XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  #define SUPPORT_SYSRQ
  #endif
  
  #include <linux/console.h>
891649409   Yoichi Yuasa   [MIPS] separate p...
28
  #include <linux/errno.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
  #include <linux/init.h>
  #include <linux/interrupt.h>
891649409   Yoichi Yuasa   [MIPS] separate p...
31
  #include <linux/ioport.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  #include <linux/module.h>
891649409   Yoichi Yuasa   [MIPS] separate p...
33
  #include <linux/platform_device.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
37
38
39
40
41
42
  #include <linux/serial.h>
  #include <linux/serial_core.h>
  #include <linux/serial_reg.h>
  #include <linux/tty.h>
  #include <linux/tty_flip.h>
  
  #include <asm/io.h>
  #include <asm/vr41xx/siu.h>
  #include <asm/vr41xx/vr41xx.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #define SIU_BAUD_BASE	1152000
  #define SIU_MAJOR	204
  #define SIU_MINOR_BASE	82
  
  #define RX_MAX_COUNT	256
  #define TX_MAX_COUNT	15
  
  #define SIUIRSEL	0x08
   #define TMICMODE	0x20
   #define TMICTX		0x10
   #define IRMSEL		0x0c
   #define IRMSEL_HP	0x08
   #define IRMSEL_TEMIC	0x04
   #define IRMSEL_SHARP	0x00
   #define IRUSESEL	0x02
   #define SIRSEL		0x01
891649409   Yoichi Yuasa   [MIPS] separate p...
59
60
61
62
63
  static struct uart_port siu_uart_ports[SIU_PORTS_MAX] = {
  	[0 ... SIU_PORTS_MAX-1] = {
  		.lock	= __SPIN_LOCK_UNLOCKED(siu_uart_ports->lock),
  		.irq	= -1,
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  static uint8_t lsr_break_flag[SIU_PORTS_MAX];
  
  #define siu_read(port, offset)		readb((port)->membase + (offset))
  #define siu_write(port, offset, value)	writeb((value), (port)->membase + (offset))
  
  void vr41xx_select_siu_interface(siu_interface_t interface)
  {
  	struct uart_port *port;
  	unsigned long flags;
  	uint8_t irsel;
  
  	port = &siu_uart_ports[0];
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	irsel = siu_read(port, SIUIRSEL);
  	if (interface == SIU_INTERFACE_IRDA)
  		irsel |= SIRSEL;
  	else
  		irsel &= ~SIRSEL;
  	siu_write(port, SIUIRSEL, irsel);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  EXPORT_SYMBOL_GPL(vr41xx_select_siu_interface);
  
  void vr41xx_use_irda(irda_use_t use)
  {
  	struct uart_port *port;
  	unsigned long flags;
  	uint8_t irsel;
  
  	port = &siu_uart_ports[0];
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	irsel = siu_read(port, SIUIRSEL);
  	if (use == FIR_USE_IRDA)
  		irsel |= IRUSESEL;
  	else
  		irsel &= ~IRUSESEL;
  	siu_write(port, SIUIRSEL, irsel);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  EXPORT_SYMBOL_GPL(vr41xx_use_irda);
  
  void vr41xx_select_irda_module(irda_module_t module, irda_speed_t speed)
  {
  	struct uart_port *port;
  	unsigned long flags;
  	uint8_t irsel;
  
  	port = &siu_uart_ports[0];
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	irsel = siu_read(port, SIUIRSEL);
  	irsel &= ~(IRMSEL | TMICTX | TMICMODE);
  	switch (module) {
  	case SHARP_IRDA:
  		irsel |= IRMSEL_SHARP;
  		break;
  	case TEMIC_IRDA:
  		irsel |= IRMSEL_TEMIC | TMICMODE;
  		if (speed == IRDA_TX_4MBPS)
  			irsel |= TMICTX;
  		break;
  	case HP_IRDA:
  		irsel |= IRMSEL_HP;
  		break;
  	default:
  		break;
  	}
  	siu_write(port, SIUIRSEL, irsel);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
146
147
148
149
150
151
  EXPORT_SYMBOL_GPL(vr41xx_select_irda_module);
  
  static inline void siu_clear_fifo(struct uart_port *port)
  {
  	siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  	siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
  	                          UART_FCR_CLEAR_XMIT);
  	siu_write(port, UART_FCR, 0);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  static inline unsigned long siu_port_size(struct uart_port *port)
  {
  	switch (port->type) {
  	case PORT_VR41XX_SIU:
  		return 11UL;
  	case PORT_VR41XX_DSIU:
  		return 8UL;
  	}
  
  	return 0;
  }
  
  static inline unsigned int siu_check_type(struct uart_port *port)
  {
891649409   Yoichi Yuasa   [MIPS] separate p...
166
167
168
169
  	if (port->line == 0)
  		return PORT_VR41XX_SIU;
  	if (port->line == 1 && port->irq != -1)
  		return PORT_VR41XX_DSIU;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
172
173
174
175
176
177
178
179
180
181
  
  	return PORT_UNKNOWN;
  }
  
  static inline const char *siu_type_name(struct uart_port *port)
  {
  	switch (port->type) {
  	case PORT_VR41XX_SIU:
  		return "SIU";
  	case PORT_VR41XX_DSIU:
  		return "DSIU";
  	}
eae936e21   Yoichi Yuasa   [PATCH] serial: u...
182
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  }
  
  static unsigned int siu_tx_empty(struct uart_port *port)
  {
  	uint8_t lsr;
  
  	lsr = siu_read(port, UART_LSR);
  	if (lsr & UART_LSR_TEMT)
  		return TIOCSER_TEMT;
  
  	return 0;
  }
  
  static void siu_set_mctrl(struct uart_port *port, unsigned int mctrl)
  {
  	uint8_t mcr = 0;
  
  	if (mctrl & TIOCM_DTR)
  		mcr |= UART_MCR_DTR;
  	if (mctrl & TIOCM_RTS)
  		mcr |= UART_MCR_RTS;
  	if (mctrl & TIOCM_OUT1)
  		mcr |= UART_MCR_OUT1;
  	if (mctrl & TIOCM_OUT2)
  		mcr |= UART_MCR_OUT2;
  	if (mctrl & TIOCM_LOOP)
  		mcr |= UART_MCR_LOOP;
  
  	siu_write(port, UART_MCR, mcr);
  }
  
  static unsigned int siu_get_mctrl(struct uart_port *port)
  {
  	uint8_t msr;
  	unsigned int mctrl = 0;
  
  	msr = siu_read(port, UART_MSR);
  	if (msr & UART_MSR_DCD)
  		mctrl |= TIOCM_CAR;
  	if (msr & UART_MSR_RI)
  		mctrl |= TIOCM_RNG;
  	if (msr & UART_MSR_DSR)
  		mctrl |= TIOCM_DSR;
  	if (msr & UART_MSR_CTS)
  		mctrl |= TIOCM_CTS;
  
  	return mctrl;
  }
b129a8ccd   Russell King   [SERIAL] Clean up...
231
  static void siu_stop_tx(struct uart_port *port)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
235
236
237
238
239
240
241
242
243
  {
  	unsigned long flags;
  	uint8_t ier;
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	ier = siu_read(port, UART_IER);
  	ier &= ~UART_IER_THRI;
  	siu_write(port, UART_IER, ier);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
b129a8ccd   Russell King   [SERIAL] Clean up...
244
  static void siu_start_tx(struct uart_port *port)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  {
  	unsigned long flags;
  	uint8_t ier;
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	ier = siu_read(port, UART_IER);
  	ier |= UART_IER_THRI;
  	siu_write(port, UART_IER, ier);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
  
  static void siu_stop_rx(struct uart_port *port)
  {
  	unsigned long flags;
  	uint8_t ier;
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	ier = siu_read(port, UART_IER);
  	ier &= ~UART_IER_RLSI;
  	siu_write(port, UART_IER, ier);
  
  	port->read_status_mask &= ~UART_LSR_DR;
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
  
  static void siu_enable_ms(struct uart_port *port)
  {
  	unsigned long flags;
  	uint8_t ier;
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	ier = siu_read(port, UART_IER);
  	ier |= UART_IER_MSI;
  	siu_write(port, UART_IER, ier);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
  
  static void siu_break_ctl(struct uart_port *port, int ctl)
  {
  	unsigned long flags;
  	uint8_t lcr;
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	lcr = siu_read(port, UART_LCR);
  	if (ctl == -1)
  		lcr |= UART_LCR_SBC;
  	else
  		lcr &= ~UART_LCR_SBC;
  	siu_write(port, UART_LCR, lcr);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
7d12e780e   David Howells   IRQ: Maintain reg...
304
  static inline void receive_chars(struct uart_port *port, uint8_t *status)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
307
308
309
310
311
312
313
314
  {
  	struct tty_struct *tty;
  	uint8_t lsr, ch;
  	char flag;
  	int max_count = RX_MAX_COUNT;
  
  	tty = port->info->tty;
  	lsr = *status;
  
  	do {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  		ch = siu_read(port, UART_RX);
  		port->icount.rx++;
  		flag = TTY_NORMAL;
  
  #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
  		lsr |= lsr_break_flag[port->line];
  		lsr_break_flag[port->line] = 0;
  #endif
  		if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
  		                    UART_LSR_PE | UART_LSR_OE))) {
  			if (lsr & UART_LSR_BI) {
  				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  				port->icount.brk++;
  
  				if (uart_handle_break(port))
  					goto ignore_char;
  			}
  
  			if (lsr & UART_LSR_FE)
  				port->icount.frame++;
  			if (lsr & UART_LSR_PE)
  				port->icount.parity++;
  			if (lsr & UART_LSR_OE)
  				port->icount.overrun++;
  
  			lsr &= port->read_status_mask;
  			if (lsr & UART_LSR_BI)
  				flag = TTY_BREAK;
  			if (lsr & UART_LSR_FE)
  				flag = TTY_FRAME;
  			if (lsr & UART_LSR_PE)
  				flag = TTY_PARITY;
  		}
7d12e780e   David Howells   IRQ: Maintain reg...
348
  		if (uart_handle_sysrq_char(port, ch))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
  			goto ignore_char;
05ab30146   Russell King   [PATCH] Serial: A...
350
351
  
  		uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  
  	ignore_char:
  		lsr = siu_read(port, UART_LSR);
  	} while ((lsr & UART_LSR_DR) && (max_count-- > 0));
  
  	tty_flip_buffer_push(tty);
  
  	*status = lsr;
  }
  
  static inline void check_modem_status(struct uart_port *port)
  {
  	uint8_t msr;
  
  	msr = siu_read(port, UART_MSR);
  	if ((msr & UART_MSR_ANY_DELTA) == 0)
  		return;
  	if (msr & UART_MSR_DDCD)
  		uart_handle_dcd_change(port, msr & UART_MSR_DCD);
  	if (msr & UART_MSR_TERI)
  		port->icount.rng++;
  	if (msr & UART_MSR_DDSR)
  		port->icount.dsr++;
  	if (msr & UART_MSR_DCTS)
  		uart_handle_cts_change(port, msr & UART_MSR_CTS);
  
  	wake_up_interruptible(&port->info->delta_msr_wait);
  }
  
  static inline void transmit_chars(struct uart_port *port)
  {
  	struct circ_buf *xmit;
  	int max_count = TX_MAX_COUNT;
  
  	xmit = &port->info->xmit;
  
  	if (port->x_char) {
  		siu_write(port, UART_TX, port->x_char);
  		port->icount.tx++;
  		port->x_char = 0;
  		return;
  	}
  
  	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
b129a8ccd   Russell King   [SERIAL] Clean up...
396
  		siu_stop_tx(port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  		return;
  	}
  
  	do {
  		siu_write(port, UART_TX, xmit->buf[xmit->tail]);
  		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  		port->icount.tx++;
  		if (uart_circ_empty(xmit))
  			break;
  	} while (max_count-- > 0);
  
  	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  		uart_write_wakeup(port);
  
  	if (uart_circ_empty(xmit))
b129a8ccd   Russell King   [SERIAL] Clean up...
412
  		siu_stop_tx(port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
  }
7d12e780e   David Howells   IRQ: Maintain reg...
414
  static irqreturn_t siu_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
  {
  	struct uart_port *port;
  	uint8_t iir, lsr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
419
420
421
422
423
424
425
  	port = (struct uart_port *)dev_id;
  
  	iir = siu_read(port, UART_IIR);
  	if (iir & UART_IIR_NO_INT)
  		return IRQ_NONE;
  
  	lsr = siu_read(port, UART_LSR);
  	if (lsr & UART_LSR_DR)
7d12e780e   David Howells   IRQ: Maintain reg...
426
  		receive_chars(port, &lsr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
429
430
431
432
433
434
435
436
437
438
  
  	check_modem_status(port);
  
  	if (lsr & UART_LSR_THRE)
  		transmit_chars(port);
  
  	return IRQ_HANDLED;
  }
  
  static int siu_startup(struct uart_port *port)
  {
  	int retval;
eae936e21   Yoichi Yuasa   [PATCH] serial: u...
439
440
  	if (port->membase == NULL)
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	siu_clear_fifo(port);
  
  	(void)siu_read(port, UART_LSR);
  	(void)siu_read(port, UART_RX);
  	(void)siu_read(port, UART_IIR);
  	(void)siu_read(port, UART_MSR);
  
  	if (siu_read(port, UART_LSR) == 0xff)
  		return -ENODEV;
  
  	retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
  	if (retval)
  		return retval;
  
  	if (port->type == PORT_VR41XX_DSIU)
  		vr41xx_enable_dsiuint(DSIUINT_ALL);
  
  	siu_write(port, UART_LCR, UART_LCR_WLEN8);
  
  	spin_lock_irq(&port->lock);
  	siu_set_mctrl(port, port->mctrl);
  	spin_unlock_irq(&port->lock);
  
  	siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
  
  	(void)siu_read(port, UART_LSR);
  	(void)siu_read(port, UART_RX);
  	(void)siu_read(port, UART_IIR);
  	(void)siu_read(port, UART_MSR);
  
  	return 0;
  }
  
  static void siu_shutdown(struct uart_port *port)
  {
  	unsigned long flags;
  	uint8_t lcr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
  	siu_write(port, UART_IER, 0);
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	port->mctrl &= ~TIOCM_OUT2;
  	siu_set_mctrl(port, port->mctrl);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  
  	lcr = siu_read(port, UART_LCR);
  	lcr &= ~UART_LCR_SBC;
  	siu_write(port, UART_LCR, lcr);
  
  	siu_clear_fifo(port);
  
  	(void)siu_read(port, UART_RX);
  
  	if (port->type == PORT_VR41XX_DSIU)
  		vr41xx_disable_dsiuint(DSIUINT_ALL);
  
  	free_irq(port->irq, port);
  }
606d099cd   Alan Cox   [PATCH] tty: swit...
500
501
  static void siu_set_termios(struct uart_port *port, struct ktermios *new,
                              struct ktermios *old)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  {
  	tcflag_t c_cflag, c_iflag;
  	uint8_t lcr, fcr, ier;
  	unsigned int baud, quot;
  	unsigned long flags;
  
  	c_cflag = new->c_cflag;
  	switch (c_cflag & CSIZE) {
  	case CS5:
  		lcr = UART_LCR_WLEN5;
  		break;
  	case CS6:
  		lcr = UART_LCR_WLEN6;
  		break;
  	case CS7:
  		lcr = UART_LCR_WLEN7;
  		break;
  	default:
  		lcr = UART_LCR_WLEN8;
  		break;
  	}
  
  	if (c_cflag & CSTOPB)
  		lcr |= UART_LCR_STOP;
  	if (c_cflag & PARENB)
  		lcr |= UART_LCR_PARITY;
  	if ((c_cflag & PARODD) != PARODD)
  		lcr |= UART_LCR_EPAR;
  	if (c_cflag & CMSPAR)
  		lcr |= UART_LCR_SPAR;
  
  	baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
  	quot = uart_get_divisor(port, baud);
  
  	fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
  
  	spin_lock_irqsave(&port->lock, flags);
  
  	uart_update_timeout(port, c_cflag, baud);
  
  	c_iflag = new->c_iflag;
  
  	port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
  	if (c_iflag & INPCK)
  		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  	if (c_iflag & (BRKINT | PARMRK))
  		port->read_status_mask |= UART_LSR_BI;
  
  	port->ignore_status_mask = 0;
  	if (c_iflag & IGNPAR)
  		port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
  	if (c_iflag & IGNBRK) {
  		port->ignore_status_mask |= UART_LSR_BI;
  		if (c_iflag & IGNPAR)
  			port->ignore_status_mask |= UART_LSR_OE;
  	}
  
  	if ((c_cflag & CREAD) == 0)
  		port->ignore_status_mask |= UART_LSR_DR;
  
  	ier = siu_read(port, UART_IER);
  	ier &= ~UART_IER_MSI;
  	if (UART_ENABLE_MS(port, c_cflag))
  		ier |= UART_IER_MSI;
  	siu_write(port, UART_IER, ier);
  
  	siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
  
  	siu_write(port, UART_DLL, (uint8_t)quot);
  	siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
  
  	siu_write(port, UART_LCR, lcr);
  
  	siu_write(port, UART_FCR, fcr);
  
  	siu_set_mctrl(port, port->mctrl);
  
  	spin_unlock_irqrestore(&port->lock, flags);
  }
  
  static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  {
  	switch (state) {
  	case 0:
  		switch (port->type) {
  		case PORT_VR41XX_SIU:
  			vr41xx_supply_clock(SIU_CLOCK);
  			break;
  		case PORT_VR41XX_DSIU:
  			vr41xx_supply_clock(DSIU_CLOCK);
  			break;
  		}
  		break;
  	case 3:
  		switch (port->type) {
  		case PORT_VR41XX_SIU:
  			vr41xx_mask_clock(SIU_CLOCK);
  			break;
  		case PORT_VR41XX_DSIU:
  			vr41xx_mask_clock(DSIU_CLOCK);
  			break;
  		}
  		break;
  	}
  }
  
  static const char *siu_type(struct uart_port *port)
  {
  	return siu_type_name(port);
  }
  
  static void siu_release_port(struct uart_port *port)
  {
  	unsigned long size;
  
  	if (port->flags	& UPF_IOREMAP) {
  		iounmap(port->membase);
  		port->membase = NULL;
  	}
  
  	size = siu_port_size(port);
  	release_mem_region(port->mapbase, size);
  }
  
  static int siu_request_port(struct uart_port *port)
  {
  	unsigned long size;
  	struct resource *res;
  
  	size = siu_port_size(port);
  	res = request_mem_region(port->mapbase, size, siu_type_name(port));
  	if (res == NULL)
  		return -EBUSY;
  
  	if (port->flags & UPF_IOREMAP) {
  		port->membase = ioremap(port->mapbase, size);
  		if (port->membase == NULL) {
  			release_resource(res);
  			return -ENOMEM;
  		}
  	}
  
  	return 0;
  }
  
  static void siu_config_port(struct uart_port *port, int flags)
  {
  	if (flags & UART_CONFIG_TYPE) {
  		port->type = siu_check_type(port);
  		(void)siu_request_port(port);
  	}
  }
  
  static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
  {
  	if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
  		return -EINVAL;
  	if (port->irq != serial->irq)
  		return -EINVAL;
  	if (port->iotype != serial->io_type)
  		return -EINVAL;
  	if (port->mapbase != (unsigned long)serial->iomem_base)
  		return -EINVAL;
  
  	return 0;
  }
  
  static struct uart_ops siu_uart_ops = {
  	.tx_empty	= siu_tx_empty,
  	.set_mctrl	= siu_set_mctrl,
  	.get_mctrl	= siu_get_mctrl,
  	.stop_tx	= siu_stop_tx,
  	.start_tx	= siu_start_tx,
  	.stop_rx	= siu_stop_rx,
  	.enable_ms	= siu_enable_ms,
  	.break_ctl	= siu_break_ctl,
  	.startup	= siu_startup,
  	.shutdown	= siu_shutdown,
  	.set_termios	= siu_set_termios,
  	.pm		= siu_pm,
  	.type		= siu_type,
  	.release_port	= siu_release_port,
  	.request_port	= siu_request_port,
  	.config_port	= siu_config_port,
  	.verify_port	= siu_verify_port,
  };
891649409   Yoichi Yuasa   [MIPS] separate p...
688
  static int siu_init_ports(struct platform_device *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
  	struct uart_port *port;
891649409   Yoichi Yuasa   [MIPS] separate p...
691
692
693
  	struct resource *res;
  	int *type = pdev->dev.platform_data;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694

891649409   Yoichi Yuasa   [MIPS] separate p...
695
  	if (!type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
698
  
  	port = siu_uart_ports;
891649409   Yoichi Yuasa   [MIPS] separate p...
699
700
701
702
703
  	for (i = 0; i < SIU_PORTS_MAX; i++) {
  		port->type = type[i];
  		if (port->type == PORT_UNKNOWN)
  			continue;
  		port->irq = platform_get_irq(pdev, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
705
706
707
708
  		port->uartclk = SIU_BAUD_BASE * 16;
  		port->fifosize = 16;
  		port->regshift = 0;
  		port->iotype = UPIO_MEM;
  		port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
  		port->line = i;
891649409   Yoichi Yuasa   [MIPS] separate p...
710
711
  		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  		port->mapbase = res->start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
713
  		port++;
  	}
891649409   Yoichi Yuasa   [MIPS] separate p...
714
  	return i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
716
717
  }
  
  #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
  #define BOTH_EMPTY	(UART_LSR_TEMT | UART_LSR_THRE)
  
  static void wait_for_xmitr(struct uart_port *port)
  {
  	int timeout = 10000;
  	uint8_t lsr, msr;
  
  	do {
  		lsr = siu_read(port, UART_LSR);
  		if (lsr & UART_LSR_BI)
  			lsr_break_flag[port->line] = UART_LSR_BI;
  
  		if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
  			break;
  	} while (timeout-- > 0);
  
  	if (port->flags & UPF_CONS_FLOW) {
  		timeout = 1000000;
  
  		do {
  			msr = siu_read(port, UART_MSR);
  			if ((msr & UART_MSR_CTS) != 0)
  				break;
  		} while (timeout-- > 0);
  	}
  }
d358788f3   Russell King   [SERIAL] kernel c...
744
745
746
747
748
  static void siu_console_putchar(struct uart_port *port, int ch)
  {
  	wait_for_xmitr(port);
  	siu_write(port, UART_TX, ch);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
750
751
752
  static void siu_console_write(struct console *con, const char *s, unsigned count)
  {
  	struct uart_port *port;
  	uint8_t ier;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
  
  	port = &siu_uart_ports[con->index];
  
  	ier = siu_read(port, UART_IER);
  	siu_write(port, UART_IER, 0);
d358788f3   Russell King   [SERIAL] kernel c...
758
  	uart_console_write(port, s, count, siu_console_putchar);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  
  	wait_for_xmitr(port);
  	siu_write(port, UART_IER, ier);
  }
  
  static int siu_console_setup(struct console *con, char *options)
  {
  	struct uart_port *port;
  	int baud = 9600;
  	int parity = 'n';
  	int bits = 8;
  	int flow = 'n';
  
  	if (con->index >= SIU_PORTS_MAX)
  		con->index = 0;
  
  	port = &siu_uart_ports[con->index];
  	if (port->membase == NULL) {
  		if (port->mapbase == 0)
  			return -ENODEV;
eae936e21   Yoichi Yuasa   [PATCH] serial: u...
779
  		port->membase = ioremap(port->mapbase, siu_port_size(port));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
  	}
  
  	vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
  
  	if (options != NULL)
  		uart_parse_options(options, &baud, &parity, &bits, &flow);
  
  	return uart_set_options(port, con, baud, parity, bits, flow);
  }
  
  static struct uart_driver siu_uart_driver;
  
  static struct console siu_console = {
  	.name	= "ttyVR",
  	.write	= siu_console_write,
  	.device	= uart_console_device,
  	.setup	= siu_console_setup,
  	.flags	= CON_PRINTBUFFER,
  	.index	= -1,
  	.data	= &siu_uart_driver,
  };
  
  static int __devinit siu_console_init(void)
  {
  	struct uart_port *port;
891649409   Yoichi Yuasa   [MIPS] separate p...
805
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806

891649409   Yoichi Yuasa   [MIPS] separate p...
807
  	for (i = 0; i < SIU_PORTS_MAX; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
  		port = &siu_uart_ports[i];
eae936e21   Yoichi Yuasa   [PATCH] serial: u...
809
  		port->ops = &siu_uart_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
  	}
  
  	register_console(&siu_console);
  
  	return 0;
  }
  
  console_initcall(siu_console_init);
  
  #define SERIAL_VR41XX_CONSOLE	&siu_console
  #else
  #define SERIAL_VR41XX_CONSOLE	NULL
  #endif
  
  static struct uart_driver siu_uart_driver = {
  	.owner		= THIS_MODULE,
  	.driver_name	= "SIU",
  	.dev_name	= "ttyVR",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
831
  	.major		= SIU_MAJOR,
  	.minor		= SIU_MINOR_BASE,
  	.cons		= SERIAL_VR41XX_CONSOLE,
  };
d39b6cfe6   Dmitry Torokhov   [PATCH] vr41xx: c...
832
  static int __devinit siu_probe(struct platform_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
834
835
  {
  	struct uart_port *port;
  	int num, i, retval;
891649409   Yoichi Yuasa   [MIPS] separate p...
836
  	num = siu_init_ports(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
838
839
840
841
842
843
844
845
846
847
  	if (num <= 0)
  		return -ENODEV;
  
  	siu_uart_driver.nr = num;
  	retval = uart_register_driver(&siu_uart_driver);
  	if (retval)
  		return retval;
  
  	for (i = 0; i < num; i++) {
  		port = &siu_uart_ports[i];
  		port->ops = &siu_uart_ops;
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
848
  		port->dev = &dev->dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
850
  
  		retval = uart_add_one_port(&siu_uart_driver, port);
eae936e21   Yoichi Yuasa   [PATCH] serial: u...
851
852
  		if (retval < 0) {
  			port->dev = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
853
  			break;
eae936e21   Yoichi Yuasa   [PATCH] serial: u...
854
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
857
858
859
860
861
862
863
  	}
  
  	if (i == 0 && retval < 0) {
  		uart_unregister_driver(&siu_uart_driver);
  		return retval;
  	}
  
  	return 0;
  }
d39b6cfe6   Dmitry Torokhov   [PATCH] vr41xx: c...
864
  static int __devexit siu_remove(struct platform_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
866
867
868
869
870
  {
  	struct uart_port *port;
  	int i;
  
  	for (i = 0; i < siu_uart_driver.nr; i++) {
  		port = &siu_uart_ports[i];
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
871
  		if (port->dev == &dev->dev) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
872
873
874
875
876
877
878
879
880
  			uart_remove_one_port(&siu_uart_driver, port);
  			port->dev = NULL;
  		}
  	}
  
  	uart_unregister_driver(&siu_uart_driver);
  
  	return 0;
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
881
  static int siu_suspend(struct platform_device *dev, pm_message_t state)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
884
  {
  	struct uart_port *port;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
886
887
  	for (i = 0; i < siu_uart_driver.nr; i++) {
  		port = &siu_uart_ports[i];
  		if ((port->type == PORT_VR41XX_SIU ||
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
888
  		     port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
890
891
892
893
894
  			uart_suspend_port(&siu_uart_driver, port);
  
  	}
  
  	return 0;
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
895
  static int siu_resume(struct platform_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
896
897
898
  {
  	struct uart_port *port;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
900
901
  	for (i = 0; i < siu_uart_driver.nr; i++) {
  		port = &siu_uart_ports[i];
  		if ((port->type == PORT_VR41XX_SIU ||
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
902
  		     port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
904
905
906
907
  			uart_resume_port(&siu_uart_driver, port);
  	}
  
  	return 0;
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
908
  static struct platform_driver siu_device_driver = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
909
  	.probe		= siu_probe,
d39b6cfe6   Dmitry Torokhov   [PATCH] vr41xx: c...
910
  	.remove		= __devexit_p(siu_remove),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
911
912
  	.suspend	= siu_suspend,
  	.resume		= siu_resume,
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
913
914
  	.driver		= {
  		.name	= "SIU",
d39b6cfe6   Dmitry Torokhov   [PATCH] vr41xx: c...
915
  		.owner	= THIS_MODULE,
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
916
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
  };
d39b6cfe6   Dmitry Torokhov   [PATCH] vr41xx: c...
918
  static int __init vr41xx_siu_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
  {
891649409   Yoichi Yuasa   [MIPS] separate p...
920
  	return platform_driver_register(&siu_device_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921
  }
d39b6cfe6   Dmitry Torokhov   [PATCH] vr41xx: c...
922
  static void __exit vr41xx_siu_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923
  {
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
924
  	platform_driver_unregister(&siu_device_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
925
926
927
928
  }
  
  module_init(vr41xx_siu_init);
  module_exit(vr41xx_siu_exit);