blob: c9549aa135b29f55e206b90ba08fceafbeefbb8c [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* sunsab.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
2 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 * Copyright (C) 2002, 2006 David S. Miller (davem@davemloft.net)
5 *
6 * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7 * Maxim Krasnyanskiy <maxk@qualcomm.com>
8 *
9 * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10 * rates to be programmed into the UART. Also eliminated a lot of
11 * duplicated code in the console setup.
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13 *
14 * Ported to new 2.5.x UART layer.
15 * David S. Miller <davem@davemloft.net>
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/major.h>
24#include <linux/string.h>
25#include <linux/ptrace.h>
26#include <linux/ioport.h>
27#include <linux/circ_buf.h>
28#include <linux/serial.h>
29#include <linux/sysrq.h>
30#include <linux/console.h>
31#include <linux/spinlock.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include <linux/init.h>
35#include <linux/of_device.h>
36
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/prom.h>
40#include <asm/setup.h>
41
42#if defined(CONFIG_SERIAL_SUNSAB_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
43#define SUPPORT_SYSRQ
44#endif
45
46#include <linux/serial_core.h>
47#include <linux/sunserialcore.h>
48
49#include "sunsab.h"
50
51struct uart_sunsab_port {
52 struct uart_port port; /* Generic UART port */
53 union sab82532_async_regs __iomem *regs; /* Chip registers */
54 unsigned long irqflags; /* IRQ state flags */
55 int dsr; /* Current DSR state */
56 unsigned int cec_timeout; /* Chip poll timeout... */
57 unsigned int tec_timeout; /* likewise */
58 unsigned char interrupt_mask0;/* ISR0 masking */
59 unsigned char interrupt_mask1;/* ISR1 masking */
60 unsigned char pvr_dtr_bit; /* Which PVR bit is DTR */
61 unsigned char pvr_dsr_bit; /* Which PVR bit is DSR */
62 unsigned int gis_shift;
63 int type; /* SAB82532 version */
64
65 /* Setting configuration bits while the transmitter is active
66 * can cause garbage characters to get emitted by the chip.
67 * Therefore, we cache such writes here and do the real register
68 * write the next time the transmitter becomes idle.
69 */
70 unsigned int cached_ebrg;
71 unsigned char cached_mode;
72 unsigned char cached_pvr;
73 unsigned char cached_dafo;
74};
75
76/*
77 * This assumes you have a 29.4912 MHz clock for your UART.
78 */
79#define SAB_BASE_BAUD ( 29491200 / 16 )
80
81static char *sab82532_version[16] = {
82 "V1.0", "V2.0", "V3.2", "V(0x03)",
83 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
84 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
85 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
86};
87
88#define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
89#define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
90
91#define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
92#define SAB82532_XMIT_FIFO_SIZE 32
93
94static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
95{
96 int timeout = up->tec_timeout;
97
98 while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
99 udelay(1);
100}
101
102static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
103{
104 int timeout = up->cec_timeout;
105
106 while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
107 udelay(1);
108}
109
110static struct tty_struct *
111receive_chars(struct uart_sunsab_port *up,
112 union sab82532_irq_status *stat)
113{
114 struct tty_struct *tty = NULL;
115 unsigned char buf[32];
116 int saw_console_brk = 0;
117 int free_fifo = 0;
118 int count = 0;
119 int i;
120
121 if (up->port.state != NULL) /* Unopened serial console */
122 tty = up->port.state->port.tty;
123
124 /* Read number of BYTES (Character + Status) available. */
125 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
126 count = SAB82532_RECV_FIFO_SIZE;
127 free_fifo++;
128 }
129
130 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
131 count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
132 free_fifo++;
133 }
134
135 /* Issue a FIFO read command in case we where idle. */
136 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
137 sunsab_cec_wait(up);
138 writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
139 return tty;
140 }
141
142 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
143 free_fifo++;
144
145 /* Read the FIFO. */
146 for (i = 0; i < count; i++)
147 buf[i] = readb(&up->regs->r.rfifo[i]);
148
149 /* Issue Receive Message Complete command. */
150 if (free_fifo) {
151 sunsab_cec_wait(up);
152 writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
153 }
154
155 /* Count may be zero for BRK, so we check for it here */
156 if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
157 (up->port.line == up->port.cons->index))
158 saw_console_brk = 1;
159
160 if (count == 0) {
161 if (unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
162 stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
163 SAB82532_ISR0_FERR);
164 up->port.icount.brk++;
165 uart_handle_break(&up->port);
166 }
167 }
168
169 for (i = 0; i < count; i++) {
170 unsigned char ch = buf[i], flag;
171
172 if (tty == NULL) {
173 uart_handle_sysrq_char(&up->port, ch);
174 continue;
175 }
176
177 flag = TTY_NORMAL;
178 up->port.icount.rx++;
179
180 if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
181 SAB82532_ISR0_FERR |
182 SAB82532_ISR0_RFO)) ||
183 unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
184 /*
185 * For statistics only
186 */
187 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
188 stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
189 SAB82532_ISR0_FERR);
190 up->port.icount.brk++;
191 /*
192 * We do the SysRQ and SAK checking
193 * here because otherwise the break
194 * may get masked by ignore_status_mask
195 * or read_status_mask.
196 */
197 if (uart_handle_break(&up->port))
198 continue;
199 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
200 up->port.icount.parity++;
201 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
202 up->port.icount.frame++;
203 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
204 up->port.icount.overrun++;
205
206 /*
207 * Mask off conditions which should be ingored.
208 */
209 stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
210 stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
211
212 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
213 flag = TTY_BREAK;
214 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
215 flag = TTY_PARITY;
216 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
217 flag = TTY_FRAME;
218 }
219
220 if (uart_handle_sysrq_char(&up->port, ch))
221 continue;
222
223 if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
224 (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0)
225 tty_insert_flip_char(tty, ch, flag);
226 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
227 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
228 }
229
230 if (saw_console_brk)
231 sun_do_break();
232
233 return tty;
234}
235
236static void sunsab_stop_tx(struct uart_port *);
237static void sunsab_tx_idle(struct uart_sunsab_port *);
238
239static void transmit_chars(struct uart_sunsab_port *up,
240 union sab82532_irq_status *stat)
241{
242 struct circ_buf *xmit = &up->port.state->xmit;
243 int i;
244
245 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
246 up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
247 writeb(up->interrupt_mask1, &up->regs->w.imr1);
248 set_bit(SAB82532_ALLS, &up->irqflags);
249 }
250
251#if 0 /* bde@nwlink.com says this check causes problems */
252 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
253 return;
254#endif
255
256 if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
257 return;
258
259 set_bit(SAB82532_XPR, &up->irqflags);
260 sunsab_tx_idle(up);
261
262 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
263 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
264 writeb(up->interrupt_mask1, &up->regs->w.imr1);
265 return;
266 }
267
268 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
269 writeb(up->interrupt_mask1, &up->regs->w.imr1);
270 clear_bit(SAB82532_ALLS, &up->irqflags);
271
272 /* Stuff 32 bytes into Transmit FIFO. */
273 clear_bit(SAB82532_XPR, &up->irqflags);
274 for (i = 0; i < up->port.fifosize; i++) {
275 writeb(xmit->buf[xmit->tail],
276 &up->regs->w.xfifo[i]);
277 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
278 up->port.icount.tx++;
279 if (uart_circ_empty(xmit))
280 break;
281 }
282
283 /* Issue a Transmit Frame command. */
284 sunsab_cec_wait(up);
285 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
286
287 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
288 uart_write_wakeup(&up->port);
289
290 if (uart_circ_empty(xmit))
291 sunsab_stop_tx(&up->port);
292}
293
294static void check_status(struct uart_sunsab_port *up,
295 union sab82532_irq_status *stat)
296{
297 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
298 uart_handle_dcd_change(&up->port,
299 !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
300
301 if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
302 uart_handle_cts_change(&up->port,
303 (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
304
305 if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
306 up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
307 up->port.icount.dsr++;
308 }
309
310 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
311}
312
313static irqreturn_t sunsab_interrupt(int irq, void *dev_id)
314{
315 struct uart_sunsab_port *up = dev_id;
316 struct tty_struct *tty;
317 union sab82532_irq_status status;
318 unsigned long flags;
319 unsigned char gis;
320
321 spin_lock_irqsave(&up->port.lock, flags);
322
323 status.stat = 0;
324 gis = readb(&up->regs->r.gis) >> up->gis_shift;
325 if (gis & 1)
326 status.sreg.isr0 = readb(&up->regs->r.isr0);
327 if (gis & 2)
328 status.sreg.isr1 = readb(&up->regs->r.isr1);
329
330 tty = NULL;
331 if (status.stat) {
332 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
333 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
334 (status.sreg.isr1 & SAB82532_ISR1_BRK))
335 tty = receive_chars(up, &status);
336 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
337 (status.sreg.isr1 & SAB82532_ISR1_CSC))
338 check_status(up, &status);
339 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
340 transmit_chars(up, &status);
341 }
342
343 spin_unlock_irqrestore(&up->port.lock, flags);
344
345 if (tty)
346 tty_flip_buffer_push(tty);
347
348 return IRQ_HANDLED;
349}
350
351/* port->lock is not held. */
352static unsigned int sunsab_tx_empty(struct uart_port *port)
353{
354 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
355 int ret;
356
357 /* Do not need a lock for a state test like this. */
358 if (test_bit(SAB82532_ALLS, &up->irqflags))
359 ret = TIOCSER_TEMT;
360 else
361 ret = 0;
362
363 return ret;
364}
365
366/* port->lock held by caller. */
367static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
368{
369 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
370
371 if (mctrl & TIOCM_RTS) {
372 up->cached_mode &= ~SAB82532_MODE_FRTS;
373 up->cached_mode |= SAB82532_MODE_RTS;
374 } else {
375 up->cached_mode |= (SAB82532_MODE_FRTS |
376 SAB82532_MODE_RTS);
377 }
378 if (mctrl & TIOCM_DTR) {
379 up->cached_pvr &= ~(up->pvr_dtr_bit);
380 } else {
381 up->cached_pvr |= up->pvr_dtr_bit;
382 }
383
384 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
385 if (test_bit(SAB82532_XPR, &up->irqflags))
386 sunsab_tx_idle(up);
387}
388
389/* port->lock is held by caller and interrupts are disabled. */
390static unsigned int sunsab_get_mctrl(struct uart_port *port)
391{
392 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
393 unsigned char val;
394 unsigned int result;
395
396 result = 0;
397
398 val = readb(&up->regs->r.pvr);
399 result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
400
401 val = readb(&up->regs->r.vstr);
402 result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
403
404 val = readb(&up->regs->r.star);
405 result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
406
407 return result;
408}
409
410/* port->lock held by caller. */
411static void sunsab_stop_tx(struct uart_port *port)
412{
413 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
414
415 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
416 writeb(up->interrupt_mask1, &up->regs->w.imr1);
417}
418
419/* port->lock held by caller. */
420static void sunsab_tx_idle(struct uart_sunsab_port *up)
421{
422 if (test_bit(SAB82532_REGS_PENDING, &up->irqflags)) {
423 u8 tmp;
424
425 clear_bit(SAB82532_REGS_PENDING, &up->irqflags);
426 writeb(up->cached_mode, &up->regs->rw.mode);
427 writeb(up->cached_pvr, &up->regs->rw.pvr);
428 writeb(up->cached_dafo, &up->regs->w.dafo);
429
430 writeb(up->cached_ebrg & 0xff, &up->regs->w.bgr);
431 tmp = readb(&up->regs->rw.ccr2);
432 tmp &= ~0xc0;
433 tmp |= (up->cached_ebrg >> 2) & 0xc0;
434 writeb(tmp, &up->regs->rw.ccr2);
435 }
436}
437
438/* port->lock held by caller. */
439static void sunsab_start_tx(struct uart_port *port)
440{
441 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
442 struct circ_buf *xmit = &up->port.state->xmit;
443 int i;
444
445 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
446 writeb(up->interrupt_mask1, &up->regs->w.imr1);
447
448 if (!test_bit(SAB82532_XPR, &up->irqflags))
449 return;
450
451 clear_bit(SAB82532_ALLS, &up->irqflags);
452 clear_bit(SAB82532_XPR, &up->irqflags);
453
454 for (i = 0; i < up->port.fifosize; i++) {
455 writeb(xmit->buf[xmit->tail],
456 &up->regs->w.xfifo[i]);
457 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
458 up->port.icount.tx++;
459 if (uart_circ_empty(xmit))
460 break;
461 }
462
463 /* Issue a Transmit Frame command. */
464 sunsab_cec_wait(up);
465 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
466}
467
468/* port->lock is not held. */
469static void sunsab_send_xchar(struct uart_port *port, char ch)
470{
471 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
472 unsigned long flags;
473
474 spin_lock_irqsave(&up->port.lock, flags);
475
476 sunsab_tec_wait(up);
477 writeb(ch, &up->regs->w.tic);
478
479 spin_unlock_irqrestore(&up->port.lock, flags);
480}
481
482/* port->lock held by caller. */
483static void sunsab_stop_rx(struct uart_port *port)
484{
485 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
486
487 up->interrupt_mask0 |= SAB82532_IMR0_TCD;
488 writeb(up->interrupt_mask1, &up->regs->w.imr0);
489}
490
491/* port->lock held by caller. */
492static void sunsab_enable_ms(struct uart_port *port)
493{
494 /* For now we always receive these interrupts. */
495}
496
497/* port->lock is not held. */
498static void sunsab_break_ctl(struct uart_port *port, int break_state)
499{
500 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
501 unsigned long flags;
502 unsigned char val;
503
504 spin_lock_irqsave(&up->port.lock, flags);
505
506 val = up->cached_dafo;
507 if (break_state)
508 val |= SAB82532_DAFO_XBRK;
509 else
510 val &= ~SAB82532_DAFO_XBRK;
511 up->cached_dafo = val;
512
513 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
514 if (test_bit(SAB82532_XPR, &up->irqflags))
515 sunsab_tx_idle(up);
516
517 spin_unlock_irqrestore(&up->port.lock, flags);
518}
519
520/* port->lock is not held. */
521static int sunsab_startup(struct uart_port *port)
522{
523 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
524 unsigned long flags;
525 unsigned char tmp;
526 int err = request_irq(up->port.irq, sunsab_interrupt,
527 IRQF_SHARED, "sab", up);
528 if (err)
529 return err;
530
531 spin_lock_irqsave(&up->port.lock, flags);
532
533 /*
534 * Wait for any commands or immediate characters
535 */
536 sunsab_cec_wait(up);
537 sunsab_tec_wait(up);
538
539 /*
540 * Clear the FIFO buffers.
541 */
542 writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
543 sunsab_cec_wait(up);
544 writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
545
546 /*
547 * Clear the interrupt registers.
548 */
549 (void) readb(&up->regs->r.isr0);
550 (void) readb(&up->regs->r.isr1);
551
552 /*
553 * Now, initialize the UART
554 */
555 writeb(0, &up->regs->w.ccr0); /* power-down */
556 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
557 SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
558 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
559 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
560 SAB82532_CCR2_TOE, &up->regs->w.ccr2);
561 writeb(0, &up->regs->w.ccr3);
562 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
563 up->cached_mode = (SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
564 SAB82532_MODE_RAC);
565 writeb(up->cached_mode, &up->regs->w.mode);
566 writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
567
568 tmp = readb(&up->regs->rw.ccr0);
569 tmp |= SAB82532_CCR0_PU; /* power-up */
570 writeb(tmp, &up->regs->rw.ccr0);
571
572 /*
573 * Finally, enable interrupts
574 */
575 up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
576 SAB82532_IMR0_PLLA);
577 writeb(up->interrupt_mask0, &up->regs->w.imr0);
578 up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
579 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
580 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
581 SAB82532_IMR1_XPR);
582 writeb(up->interrupt_mask1, &up->regs->w.imr1);
583 set_bit(SAB82532_ALLS, &up->irqflags);
584 set_bit(SAB82532_XPR, &up->irqflags);
585
586 spin_unlock_irqrestore(&up->port.lock, flags);
587
588 return 0;
589}
590
591/* port->lock is not held. */
592static void sunsab_shutdown(struct uart_port *port)
593{
594 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
595 unsigned long flags;
596
597 spin_lock_irqsave(&up->port.lock, flags);
598
599 /* Disable Interrupts */
600 up->interrupt_mask0 = 0xff;
601 writeb(up->interrupt_mask0, &up->regs->w.imr0);
602 up->interrupt_mask1 = 0xff;
603 writeb(up->interrupt_mask1, &up->regs->w.imr1);
604
605 /* Disable break condition */
606 up->cached_dafo = readb(&up->regs->rw.dafo);
607 up->cached_dafo &= ~SAB82532_DAFO_XBRK;
608 writeb(up->cached_dafo, &up->regs->rw.dafo);
609
610 /* Disable Receiver */
611 up->cached_mode &= ~SAB82532_MODE_RAC;
612 writeb(up->cached_mode, &up->regs->rw.mode);
613
614 /*
615 * XXX FIXME
616 *
617 * If the chip is powered down here the system hangs/crashes during
618 * reboot or shutdown. This needs to be investigated further,
619 * similar behaviour occurs in 2.4 when the driver is configured
620 * as a module only. One hint may be that data is sometimes
621 * transmitted at 9600 baud during shutdown (regardless of the
622 * speed the chip was configured for when the port was open).
623 */
624#if 0
625 /* Power Down */
626 tmp = readb(&up->regs->rw.ccr0);
627 tmp &= ~SAB82532_CCR0_PU;
628 writeb(tmp, &up->regs->rw.ccr0);
629#endif
630
631 spin_unlock_irqrestore(&up->port.lock, flags);
632 free_irq(up->port.irq, up);
633}
634
635/*
636 * This is used to figure out the divisor speeds.
637 *
638 * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
639 *
640 * with 0 <= N < 64 and 0 <= M < 16
641 */
642
643static void calc_ebrg(int baud, int *n_ret, int *m_ret)
644{
645 int n, m;
646
647 if (baud == 0) {
648 *n_ret = 0;
649 *m_ret = 0;
650 return;
651 }
652
653 /*
654 * We scale numbers by 10 so that we get better accuracy
655 * without having to use floating point. Here we increment m
656 * until n is within the valid range.
657 */
658 n = (SAB_BASE_BAUD * 10) / baud;
659 m = 0;
660 while (n >= 640) {
661 n = n / 2;
662 m++;
663 }
664 n = (n+5) / 10;
665 /*
666 * We try very hard to avoid speeds with M == 0 since they may
667 * not work correctly for XTAL frequences above 10 MHz.
668 */
669 if ((m == 0) && ((n & 1) == 0)) {
670 n = n / 2;
671 m++;
672 }
673 *n_ret = n - 1;
674 *m_ret = m;
675}
676
677/* Internal routine, port->lock is held and local interrupts are disabled. */
678static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
679 unsigned int iflag, unsigned int baud,
680 unsigned int quot)
681{
682 unsigned char dafo;
683 int bits, n, m;
684
685 /* Byte size and parity */
686 switch (cflag & CSIZE) {
687 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
688 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
689 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
690 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
691 /* Never happens, but GCC is too dumb to figure it out */
692 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
693 }
694
695 if (cflag & CSTOPB) {
696 dafo |= SAB82532_DAFO_STOP;
697 bits++;
698 }
699
700 if (cflag & PARENB) {
701 dafo |= SAB82532_DAFO_PARE;
702 bits++;
703 }
704
705 if (cflag & PARODD) {
706 dafo |= SAB82532_DAFO_PAR_ODD;
707 } else {
708 dafo |= SAB82532_DAFO_PAR_EVEN;
709 }
710 up->cached_dafo = dafo;
711
712 calc_ebrg(baud, &n, &m);
713
714 up->cached_ebrg = n | (m << 6);
715
716 up->tec_timeout = (10 * 1000000) / baud;
717 up->cec_timeout = up->tec_timeout >> 2;
718
719 /* CTS flow control flags */
720 /* We encode read_status_mask and ignore_status_mask like so:
721 *
722 * ---------------------
723 * | ... | ISR1 | ISR0 |
724 * ---------------------
725 * .. 15 8 7 0
726 */
727
728 up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
729 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
730 SAB82532_ISR0_CDSC);
731 up->port.read_status_mask |= (SAB82532_ISR1_CSC |
732 SAB82532_ISR1_ALLS |
733 SAB82532_ISR1_XPR) << 8;
734 if (iflag & INPCK)
735 up->port.read_status_mask |= (SAB82532_ISR0_PERR |
736 SAB82532_ISR0_FERR);
737 if (iflag & (BRKINT | PARMRK))
738 up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
739
740 /*
741 * Characteres to ignore
742 */
743 up->port.ignore_status_mask = 0;
744 if (iflag & IGNPAR)
745 up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
746 SAB82532_ISR0_FERR);
747 if (iflag & IGNBRK) {
748 up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
749 /*
750 * If we're ignoring parity and break indicators,
751 * ignore overruns too (for real raw support).
752 */
753 if (iflag & IGNPAR)
754 up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
755 }
756
757 /*
758 * ignore all characters if CREAD is not set
759 */
760 if ((cflag & CREAD) == 0)
761 up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
762 SAB82532_ISR0_TCD);
763
764 uart_update_timeout(&up->port, cflag,
765 (up->port.uartclk / (16 * quot)));
766
767 /* Now schedule a register update when the chip's
768 * transmitter is idle.
769 */
770 up->cached_mode |= SAB82532_MODE_RAC;
771 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
772 if (test_bit(SAB82532_XPR, &up->irqflags))
773 sunsab_tx_idle(up);
774}
775
776/* port->lock is not held. */
777static void sunsab_set_termios(struct uart_port *port, struct ktermios *termios,
778 struct ktermios *old)
779{
780 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
781 unsigned long flags;
782 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
783 unsigned int quot = uart_get_divisor(port, baud);
784
785 spin_lock_irqsave(&up->port.lock, flags);
786 sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud, quot);
787 spin_unlock_irqrestore(&up->port.lock, flags);
788}
789
790static const char *sunsab_type(struct uart_port *port)
791{
792 struct uart_sunsab_port *up = (void *)port;
793 static char buf[36];
794
795 sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
796 return buf;
797}
798
799static void sunsab_release_port(struct uart_port *port)
800{
801}
802
803static int sunsab_request_port(struct uart_port *port)
804{
805 return 0;
806}
807
808static void sunsab_config_port(struct uart_port *port, int flags)
809{
810}
811
812static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
813{
814 return -EINVAL;
815}
816
817static struct uart_ops sunsab_pops = {
818 .tx_empty = sunsab_tx_empty,
819 .set_mctrl = sunsab_set_mctrl,
820 .get_mctrl = sunsab_get_mctrl,
821 .stop_tx = sunsab_stop_tx,
822 .start_tx = sunsab_start_tx,
823 .send_xchar = sunsab_send_xchar,
824 .stop_rx = sunsab_stop_rx,
825 .enable_ms = sunsab_enable_ms,
826 .break_ctl = sunsab_break_ctl,
827 .startup = sunsab_startup,
828 .shutdown = sunsab_shutdown,
829 .set_termios = sunsab_set_termios,
830 .type = sunsab_type,
831 .release_port = sunsab_release_port,
832 .request_port = sunsab_request_port,
833 .config_port = sunsab_config_port,
834 .verify_port = sunsab_verify_port,
835};
836
837static struct uart_driver sunsab_reg = {
838 .owner = THIS_MODULE,
839 .driver_name = "sunsab",
840 .dev_name = "ttyS",
841 .major = TTY_MAJOR,
842};
843
844static struct uart_sunsab_port *sunsab_ports;
845
846#ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
847
848static void sunsab_console_putchar(struct uart_port *port, int c)
849{
850 struct uart_sunsab_port *up = (struct uart_sunsab_port *)port;
851
852 sunsab_tec_wait(up);
853 writeb(c, &up->regs->w.tic);
854}
855
856static void sunsab_console_write(struct console *con, const char *s, unsigned n)
857{
858 struct uart_sunsab_port *up = &sunsab_ports[con->index];
859 unsigned long flags;
860 int locked = 1;
861
862 local_irq_save(flags);
863 if (up->port.sysrq) {
864 locked = 0;
865 } else if (oops_in_progress) {
866 locked = spin_trylock(&up->port.lock);
867 } else
868 spin_lock(&up->port.lock);
869
870 uart_console_write(&up->port, s, n, sunsab_console_putchar);
871 sunsab_tec_wait(up);
872
873 if (locked)
874 spin_unlock(&up->port.lock);
875 local_irq_restore(flags);
876}
877
878static int sunsab_console_setup(struct console *con, char *options)
879{
880 struct uart_sunsab_port *up = &sunsab_ports[con->index];
881 unsigned long flags;
882 unsigned int baud, quot;
883
884 /*
885 * The console framework calls us for each and every port
886 * registered. Defer the console setup until the requested
887 * port has been properly discovered. A bit of a hack,
888 * though...
889 */
890 if (up->port.type != PORT_SUNSAB)
891 return -1;
892
893 printk("Console: ttyS%d (SAB82532)\n",
894 (sunsab_reg.minor - 64) + con->index);
895
896 sunserial_console_termios(con, up->port.dev->of_node);
897
898 switch (con->cflag & CBAUD) {
899 case B150: baud = 150; break;
900 case B300: baud = 300; break;
901 case B600: baud = 600; break;
902 case B1200: baud = 1200; break;
903 case B2400: baud = 2400; break;
904 case B4800: baud = 4800; break;
905 default: case B9600: baud = 9600; break;
906 case B19200: baud = 19200; break;
907 case B38400: baud = 38400; break;
908 case B57600: baud = 57600; break;
909 case B115200: baud = 115200; break;
910 case B230400: baud = 230400; break;
911 case B460800: baud = 460800; break;
912 };
913
914 /*
915 * Temporary fix.
916 */
917 spin_lock_init(&up->port.lock);
918
919 /*
920 * Initialize the hardware
921 */
922 sunsab_startup(&up->port);
923
924 spin_lock_irqsave(&up->port.lock, flags);
925
926 /*
927 * Finally, enable interrupts
928 */
929 up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
930 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
931 writeb(up->interrupt_mask0, &up->regs->w.imr0);
932 up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
933 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
934 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
935 SAB82532_IMR1_XPR;
936 writeb(up->interrupt_mask1, &up->regs->w.imr1);
937
938 quot = uart_get_divisor(&up->port, baud);
939 sunsab_convert_to_sab(up, con->cflag, 0, baud, quot);
940 sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
941
942 spin_unlock_irqrestore(&up->port.lock, flags);
943
944 return 0;
945}
946
947static struct console sunsab_console = {
948 .name = "ttyS",
949 .write = sunsab_console_write,
950 .device = uart_console_device,
951 .setup = sunsab_console_setup,
952 .flags = CON_PRINTBUFFER,
953 .index = -1,
954 .data = &sunsab_reg,
955};
956
957static inline struct console *SUNSAB_CONSOLE(void)
958{
959 return &sunsab_console;
960}
961#else
962#define SUNSAB_CONSOLE() (NULL)
963#define sunsab_console_init() do { } while (0)
964#endif
965
966static int __devinit sunsab_init_one(struct uart_sunsab_port *up,
967 struct platform_device *op,
968 unsigned long offset,
969 int line)
970{
971 up->port.line = line;
972 up->port.dev = &op->dev;
973
974 up->port.mapbase = op->resource[0].start + offset;
975 up->port.membase = of_ioremap(&op->resource[0], offset,
976 sizeof(union sab82532_async_regs),
977 "sab");
978 if (!up->port.membase)
979 return -ENOMEM;
980 up->regs = (union sab82532_async_regs __iomem *) up->port.membase;
981
982 up->port.irq = op->archdata.irqs[0];
983
984 up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
985 up->port.iotype = UPIO_MEM;
986
987 writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
988
989 up->port.ops = &sunsab_pops;
990 up->port.type = PORT_SUNSAB;
991 up->port.uartclk = SAB_BASE_BAUD;
992
993 up->type = readb(&up->regs->r.vstr) & 0x0f;
994 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
995 writeb(0xff, &up->regs->w.pim);
996 if ((up->port.line & 0x1) == 0) {
997 up->pvr_dsr_bit = (1 << 0);
998 up->pvr_dtr_bit = (1 << 1);
999 up->gis_shift = 2;
1000 } else {
1001 up->pvr_dsr_bit = (1 << 3);
1002 up->pvr_dtr_bit = (1 << 2);
1003 up->gis_shift = 0;
1004 }
1005 up->cached_pvr = (1 << 1) | (1 << 2) | (1 << 4);
1006 writeb(up->cached_pvr, &up->regs->w.pvr);
1007 up->cached_mode = readb(&up->regs->rw.mode);
1008 up->cached_mode |= SAB82532_MODE_FRTS;
1009 writeb(up->cached_mode, &up->regs->rw.mode);
1010 up->cached_mode |= SAB82532_MODE_RTS;
1011 writeb(up->cached_mode, &up->regs->rw.mode);
1012
1013 up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
1014 up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1015
1016 return 0;
1017}
1018
1019static int __devinit sab_probe(struct platform_device *op)
1020{
1021 static int inst;
1022 struct uart_sunsab_port *up;
1023 int err;
1024
1025 up = &sunsab_ports[inst * 2];
1026
1027 err = sunsab_init_one(&up[0], op,
1028 0,
1029 (inst * 2) + 0);
1030 if (err)
1031 goto out;
1032
1033 err = sunsab_init_one(&up[1], op,
1034 sizeof(union sab82532_async_regs),
1035 (inst * 2) + 1);
1036 if (err)
1037 goto out1;
1038
1039 sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
1040 &sunsab_reg, up[0].port.line,
1041 false);
1042
1043 sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
1044 &sunsab_reg, up[1].port.line,
1045 false);
1046
1047 err = uart_add_one_port(&sunsab_reg, &up[0].port);
1048 if (err)
1049 goto out2;
1050
1051 err = uart_add_one_port(&sunsab_reg, &up[1].port);
1052 if (err)
1053 goto out3;
1054
1055 dev_set_drvdata(&op->dev, &up[0]);
1056
1057 inst++;
1058
1059 return 0;
1060
1061out3:
1062 uart_remove_one_port(&sunsab_reg, &up[0].port);
1063out2:
1064 of_iounmap(&op->resource[0],
1065 up[1].port.membase,
1066 sizeof(union sab82532_async_regs));
1067out1:
1068 of_iounmap(&op->resource[0],
1069 up[0].port.membase,
1070 sizeof(union sab82532_async_regs));
1071out:
1072 return err;
1073}
1074
1075static int __devexit sab_remove(struct platform_device *op)
1076{
1077 struct uart_sunsab_port *up = dev_get_drvdata(&op->dev);
1078
1079 uart_remove_one_port(&sunsab_reg, &up[1].port);
1080 uart_remove_one_port(&sunsab_reg, &up[0].port);
1081 of_iounmap(&op->resource[0],
1082 up[1].port.membase,
1083 sizeof(union sab82532_async_regs));
1084 of_iounmap(&op->resource[0],
1085 up[0].port.membase,
1086 sizeof(union sab82532_async_regs));
1087
1088 dev_set_drvdata(&op->dev, NULL);
1089
1090 return 0;
1091}
1092
1093static const struct of_device_id sab_match[] = {
1094 {
1095 .name = "se",
1096 },
1097 {
1098 .name = "serial",
1099 .compatible = "sab82532",
1100 },
1101 {},
1102};
1103MODULE_DEVICE_TABLE(of, sab_match);
1104
1105static struct platform_driver sab_driver = {
1106 .driver = {
1107 .name = "sab",
1108 .owner = THIS_MODULE,
1109 .of_match_table = sab_match,
1110 },
1111 .probe = sab_probe,
1112 .remove = __devexit_p(sab_remove),
1113};
1114
1115static int __init sunsab_init(void)
1116{
1117 struct device_node *dp;
1118 int err;
1119 int num_channels = 0;
1120
1121 for_each_node_by_name(dp, "se")
1122 num_channels += 2;
1123 for_each_node_by_name(dp, "serial") {
1124 if (of_device_is_compatible(dp, "sab82532"))
1125 num_channels += 2;
1126 }
1127
1128 if (num_channels) {
1129 sunsab_ports = kzalloc(sizeof(struct uart_sunsab_port) *
1130 num_channels, GFP_KERNEL);
1131 if (!sunsab_ports)
1132 return -ENOMEM;
1133
1134 err = sunserial_register_minors(&sunsab_reg, num_channels);
1135 if (err) {
1136 kfree(sunsab_ports);
1137 sunsab_ports = NULL;
1138
1139 return err;
1140 }
1141 }
1142
1143 return platform_driver_register(&sab_driver);
1144}
1145
1146static void __exit sunsab_exit(void)
1147{
1148 platform_driver_unregister(&sab_driver);
1149 if (sunsab_reg.nr) {
1150 sunserial_unregister_minors(&sunsab_reg, sunsab_reg.nr);
1151 }
1152
1153 kfree(sunsab_ports);
1154 sunsab_ports = NULL;
1155}
1156
1157module_init(sunsab_init);
1158module_exit(sunsab_exit);
1159
1160MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
1161MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
1162MODULE_LICENSE("GPL");