Linux 4.1.16
[linux/fpc-iii.git] / drivers / tty / serial / men_z135_uart.c
blob5a41b8fbb10a86fa9d05fdf47f1ee85b070a28b5
1 /*
2 * MEN 16z135 High Speed UART
4 * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; version 2 of the License.
11 #define pr_fmt(fmt) KBUILD_MODNAME ":" fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/serial_core.h>
17 #include <linux/ioport.h>
18 #include <linux/io.h>
19 #include <linux/tty_flip.h>
20 #include <linux/bitops.h>
21 #include <linux/mcb.h>
23 #define MEN_Z135_MAX_PORTS 12
24 #define MEN_Z135_BASECLK 29491200
25 #define MEN_Z135_FIFO_SIZE 1024
26 #define MEN_Z135_FIFO_WATERMARK 1020
28 #define MEN_Z135_STAT_REG 0x0
29 #define MEN_Z135_RX_RAM 0x4
30 #define MEN_Z135_TX_RAM 0x400
31 #define MEN_Z135_RX_CTRL 0x800
32 #define MEN_Z135_TX_CTRL 0x804
33 #define MEN_Z135_CONF_REG 0x808
34 #define MEN_Z135_UART_FREQ 0x80c
35 #define MEN_Z135_BAUD_REG 0x810
36 #define MEN_Z135_TIMEOUT 0x814
38 #define MEN_Z135_MEM_SIZE 0x818
40 #define IRQ_ID(x) ((x) & 0x1f)
42 #define MEN_Z135_IER_RXCIEN BIT(0) /* RX Space IRQ */
43 #define MEN_Z135_IER_TXCIEN BIT(1) /* TX Space IRQ */
44 #define MEN_Z135_IER_RLSIEN BIT(2) /* Receiver Line Status IRQ */
45 #define MEN_Z135_IER_MSIEN BIT(3) /* Modem Status IRQ */
46 #define MEN_Z135_ALL_IRQS (MEN_Z135_IER_RXCIEN \
47 | MEN_Z135_IER_RLSIEN \
48 | MEN_Z135_IER_MSIEN \
49 | MEN_Z135_IER_TXCIEN)
51 #define MEN_Z135_MCR_DTR BIT(24)
52 #define MEN_Z135_MCR_RTS BIT(25)
53 #define MEN_Z135_MCR_OUT1 BIT(26)
54 #define MEN_Z135_MCR_OUT2 BIT(27)
55 #define MEN_Z135_MCR_LOOP BIT(28)
56 #define MEN_Z135_MCR_RCFC BIT(29)
58 #define MEN_Z135_MSR_DCTS BIT(0)
59 #define MEN_Z135_MSR_DDSR BIT(1)
60 #define MEN_Z135_MSR_DRI BIT(2)
61 #define MEN_Z135_MSR_DDCD BIT(3)
62 #define MEN_Z135_MSR_CTS BIT(4)
63 #define MEN_Z135_MSR_DSR BIT(5)
64 #define MEN_Z135_MSR_RI BIT(6)
65 #define MEN_Z135_MSR_DCD BIT(7)
67 #define MEN_Z135_LCR_SHIFT 8 /* LCR shift mask */
69 #define MEN_Z135_WL5 0 /* CS5 */
70 #define MEN_Z135_WL6 1 /* CS6 */
71 #define MEN_Z135_WL7 2 /* CS7 */
72 #define MEN_Z135_WL8 3 /* CS8 */
74 #define MEN_Z135_STB_SHIFT 2 /* Stopbits */
75 #define MEN_Z135_NSTB1 0
76 #define MEN_Z135_NSTB2 1
78 #define MEN_Z135_PEN_SHIFT 3 /* Parity enable */
79 #define MEN_Z135_PAR_DIS 0
80 #define MEN_Z135_PAR_ENA 1
82 #define MEN_Z135_PTY_SHIFT 4 /* Parity type */
83 #define MEN_Z135_PTY_ODD 0
84 #define MEN_Z135_PTY_EVN 1
86 #define MEN_Z135_LSR_DR BIT(0)
87 #define MEN_Z135_LSR_OE BIT(1)
88 #define MEN_Z135_LSR_PE BIT(2)
89 #define MEN_Z135_LSR_FE BIT(3)
90 #define MEN_Z135_LSR_BI BIT(4)
91 #define MEN_Z135_LSR_THEP BIT(5)
92 #define MEN_Z135_LSR_TEXP BIT(6)
93 #define MEN_Z135_LSR_RXFIFOERR BIT(7)
95 #define MEN_Z135_IRQ_ID_RLS BIT(0)
96 #define MEN_Z135_IRQ_ID_RDA BIT(1)
97 #define MEN_Z135_IRQ_ID_CTI BIT(2)
98 #define MEN_Z135_IRQ_ID_TSA BIT(3)
99 #define MEN_Z135_IRQ_ID_MST BIT(4)
101 #define LCR(x) (((x) >> MEN_Z135_LCR_SHIFT) & 0xff)
103 #define BYTES_TO_ALIGN(x) ((x) & 0x3)
105 static int line;
107 static int txlvl = 5;
108 module_param(txlvl, int, S_IRUGO);
109 MODULE_PARM_DESC(txlvl, "TX IRQ trigger level 0-7, default 5 (128 byte)");
111 static int rxlvl = 6;
112 module_param(rxlvl, int, S_IRUGO);
113 MODULE_PARM_DESC(rxlvl, "RX IRQ trigger level 0-7, default 6 (256 byte)");
115 static int align;
116 module_param(align, int, S_IRUGO);
117 MODULE_PARM_DESC(align, "Keep hardware FIFO write pointer aligned, default 0");
119 static uint rx_timeout;
120 module_param(rx_timeout, uint, S_IRUGO);
121 MODULE_PARM_DESC(rx_timeout, "RX timeout. "
122 "Timeout in seconds = (timeout_reg * baud_reg * 4) / freq_reg");
124 struct men_z135_port {
125 struct uart_port port;
126 struct mcb_device *mdev;
127 unsigned char *rxbuf;
128 u32 stat_reg;
129 spinlock_t lock;
130 bool automode;
132 #define to_men_z135(port) container_of((port), struct men_z135_port, port)
135 * men_z135_reg_set() - Set value in register
136 * @uart: The UART port
137 * @addr: Register address
138 * @val: value to set
140 static inline void men_z135_reg_set(struct men_z135_port *uart,
141 u32 addr, u32 val)
143 struct uart_port *port = &uart->port;
144 unsigned long flags;
145 u32 reg;
147 spin_lock_irqsave(&uart->lock, flags);
149 reg = ioread32(port->membase + addr);
150 reg |= val;
151 iowrite32(reg, port->membase + addr);
153 spin_unlock_irqrestore(&uart->lock, flags);
157 * men_z135_reg_clr() - Unset value in register
158 * @uart: The UART port
159 * @addr: Register address
160 * @val: value to clear
162 static inline void men_z135_reg_clr(struct men_z135_port *uart,
163 u32 addr, u32 val)
165 struct uart_port *port = &uart->port;
166 unsigned long flags;
167 u32 reg;
169 spin_lock_irqsave(&uart->lock, flags);
171 reg = ioread32(port->membase + addr);
172 reg &= ~val;
173 iowrite32(reg, port->membase + addr);
175 spin_unlock_irqrestore(&uart->lock, flags);
179 * men_z135_handle_modem_status() - Handle change of modem status
180 * @port: The UART port
182 * Handle change of modem status register. This is done by reading the "delta"
183 * versions of DCD (Data Carrier Detect) and CTS (Clear To Send).
185 static void men_z135_handle_modem_status(struct men_z135_port *uart)
187 u8 msr;
189 msr = (uart->stat_reg >> 8) & 0xff;
191 if (msr & MEN_Z135_MSR_DDCD)
192 uart_handle_dcd_change(&uart->port,
193 msr & MEN_Z135_MSR_DCD);
194 if (msr & MEN_Z135_MSR_DCTS)
195 uart_handle_cts_change(&uart->port,
196 msr & MEN_Z135_MSR_CTS);
199 static void men_z135_handle_lsr(struct men_z135_port *uart)
201 struct uart_port *port = &uart->port;
202 u8 lsr;
204 lsr = (uart->stat_reg >> 16) & 0xff;
206 if (lsr & MEN_Z135_LSR_OE)
207 port->icount.overrun++;
208 if (lsr & MEN_Z135_LSR_PE)
209 port->icount.parity++;
210 if (lsr & MEN_Z135_LSR_FE)
211 port->icount.frame++;
212 if (lsr & MEN_Z135_LSR_BI) {
213 port->icount.brk++;
214 uart_handle_break(port);
219 * get_rx_fifo_content() - Get the number of bytes in RX FIFO
220 * @uart: The UART port
222 * Read RXC register from hardware and return current FIFO fill size.
224 static u16 get_rx_fifo_content(struct men_z135_port *uart)
226 struct uart_port *port = &uart->port;
227 u32 stat_reg;
228 u16 rxc;
229 u8 rxc_lo;
230 u8 rxc_hi;
232 stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
233 rxc_lo = stat_reg >> 24;
234 rxc_hi = (stat_reg & 0xC0) >> 6;
236 rxc = rxc_lo | (rxc_hi << 8);
238 return rxc;
242 * men_z135_handle_rx() - RX tasklet routine
243 * @arg: Pointer to struct men_z135_port
245 * Copy from RX FIFO and acknowledge number of bytes copied.
247 static void men_z135_handle_rx(struct men_z135_port *uart)
249 struct uart_port *port = &uart->port;
250 struct tty_port *tport = &port->state->port;
251 int copied;
252 u16 size;
253 int room;
255 size = get_rx_fifo_content(uart);
257 if (size == 0)
258 return;
260 /* Avoid accidently accessing TX FIFO instead of RX FIFO. Last
261 * longword in RX FIFO cannot be read.(0x004-0x3FF)
263 if (size > MEN_Z135_FIFO_WATERMARK)
264 size = MEN_Z135_FIFO_WATERMARK;
266 room = tty_buffer_request_room(tport, size);
267 if (room != size)
268 dev_warn(&uart->mdev->dev,
269 "Not enough room in flip buffer, truncating to %d\n",
270 room);
272 if (room == 0)
273 return;
275 memcpy_fromio(uart->rxbuf, port->membase + MEN_Z135_RX_RAM, room);
276 /* Be sure to first copy all data and then acknowledge it */
277 mb();
278 iowrite32(room, port->membase + MEN_Z135_RX_CTRL);
280 copied = tty_insert_flip_string(tport, uart->rxbuf, room);
281 if (copied != room)
282 dev_warn(&uart->mdev->dev,
283 "Only copied %d instead of %d bytes\n",
284 copied, room);
286 port->icount.rx += copied;
288 tty_flip_buffer_push(tport);
293 * men_z135_handle_tx() - TX tasklet routine
294 * @arg: Pointer to struct men_z135_port
297 static void men_z135_handle_tx(struct men_z135_port *uart)
299 struct uart_port *port = &uart->port;
300 struct circ_buf *xmit = &port->state->xmit;
301 u32 txc;
302 u32 wptr;
303 int qlen;
304 int n;
305 int txfree;
306 int head;
307 int tail;
308 int s;
310 if (uart_circ_empty(xmit))
311 goto out;
313 if (uart_tx_stopped(port))
314 goto out;
316 if (port->x_char)
317 goto out;
319 /* calculate bytes to copy */
320 qlen = uart_circ_chars_pending(xmit);
321 if (qlen <= 0)
322 goto out;
324 wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
325 txc = (wptr >> 16) & 0x3ff;
326 wptr &= 0x3ff;
328 if (txc > MEN_Z135_FIFO_WATERMARK)
329 txc = MEN_Z135_FIFO_WATERMARK;
331 txfree = MEN_Z135_FIFO_WATERMARK - txc;
332 if (txfree <= 0) {
333 dev_err(&uart->mdev->dev,
334 "Not enough room in TX FIFO have %d, need %d\n",
335 txfree, qlen);
336 goto irq_en;
339 /* if we're not aligned, it's better to copy only 1 or 2 bytes and
340 * then the rest.
342 if (align && qlen >= 3 && BYTES_TO_ALIGN(wptr))
343 n = 4 - BYTES_TO_ALIGN(wptr);
344 else if (qlen > txfree)
345 n = txfree;
346 else
347 n = qlen;
349 if (n <= 0)
350 goto irq_en;
352 head = xmit->head & (UART_XMIT_SIZE - 1);
353 tail = xmit->tail & (UART_XMIT_SIZE - 1);
355 s = ((head >= tail) ? head : UART_XMIT_SIZE) - tail;
356 n = min(n, s);
358 memcpy_toio(port->membase + MEN_Z135_TX_RAM, &xmit->buf[xmit->tail], n);
359 xmit->tail = (xmit->tail + n) & (UART_XMIT_SIZE - 1);
360 mmiowb();
362 iowrite32(n & 0x3ff, port->membase + MEN_Z135_TX_CTRL);
364 port->icount.tx += n;
366 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
367 uart_write_wakeup(port);
369 irq_en:
370 if (!uart_circ_empty(xmit))
371 men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
372 else
373 men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
375 out:
376 return;
381 * men_z135_intr() - Handle legacy IRQs
382 * @irq: The IRQ number
383 * @data: Pointer to UART port
385 * Check IIR register to find the cause of the interrupt and handle it.
386 * It is possible that multiple interrupts reason bits are set and reading
387 * the IIR is a destructive read, so we always need to check for all possible
388 * interrupts and handle them.
390 static irqreturn_t men_z135_intr(int irq, void *data)
392 struct men_z135_port *uart = (struct men_z135_port *)data;
393 struct uart_port *port = &uart->port;
394 bool handled = false;
395 int irq_id;
397 uart->stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
398 irq_id = IRQ_ID(uart->stat_reg);
400 if (!irq_id)
401 goto out;
403 spin_lock(&port->lock);
404 /* It's save to write to IIR[7:6] RXC[9:8] */
405 iowrite8(irq_id, port->membase + MEN_Z135_STAT_REG);
407 if (irq_id & MEN_Z135_IRQ_ID_RLS) {
408 men_z135_handle_lsr(uart);
409 handled = true;
412 if (irq_id & (MEN_Z135_IRQ_ID_RDA | MEN_Z135_IRQ_ID_CTI)) {
413 if (irq_id & MEN_Z135_IRQ_ID_CTI)
414 dev_dbg(&uart->mdev->dev, "Character Timeout Indication\n");
415 men_z135_handle_rx(uart);
416 handled = true;
419 if (irq_id & MEN_Z135_IRQ_ID_TSA) {
420 men_z135_handle_tx(uart);
421 handled = true;
424 if (irq_id & MEN_Z135_IRQ_ID_MST) {
425 men_z135_handle_modem_status(uart);
426 handled = true;
429 spin_unlock(&port->lock);
430 out:
431 return IRQ_RETVAL(handled);
435 * men_z135_request_irq() - Request IRQ for 16z135 core
436 * @uart: z135 private uart port structure
438 * Request an IRQ for 16z135 to use. First try using MSI, if it fails
439 * fall back to using legacy interrupts.
441 static int men_z135_request_irq(struct men_z135_port *uart)
443 struct device *dev = &uart->mdev->dev;
444 struct uart_port *port = &uart->port;
445 int err = 0;
447 err = request_irq(port->irq, men_z135_intr, IRQF_SHARED,
448 "men_z135_intr", uart);
449 if (err)
450 dev_err(dev, "Error %d getting interrupt\n", err);
452 return err;
456 * men_z135_tx_empty() - Handle tx_empty call
457 * @port: The UART port
459 * This function tests whether the TX FIFO and shifter for the port
460 * described by @port is empty.
462 static unsigned int men_z135_tx_empty(struct uart_port *port)
464 u32 wptr;
465 u16 txc;
467 wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
468 txc = (wptr >> 16) & 0x3ff;
470 if (txc == 0)
471 return TIOCSER_TEMT;
472 else
473 return 0;
477 * men_z135_set_mctrl() - Set modem control lines
478 * @port: The UART port
479 * @mctrl: The modem control lines
481 * This function sets the modem control lines for a port described by @port
482 * to the state described by @mctrl
484 static void men_z135_set_mctrl(struct uart_port *port, unsigned int mctrl)
486 u32 old;
487 u32 conf_reg;
489 conf_reg = old = ioread32(port->membase + MEN_Z135_CONF_REG);
490 if (mctrl & TIOCM_RTS)
491 conf_reg |= MEN_Z135_MCR_RTS;
492 else
493 conf_reg &= ~MEN_Z135_MCR_RTS;
495 if (mctrl & TIOCM_DTR)
496 conf_reg |= MEN_Z135_MCR_DTR;
497 else
498 conf_reg &= ~MEN_Z135_MCR_DTR;
500 if (mctrl & TIOCM_OUT1)
501 conf_reg |= MEN_Z135_MCR_OUT1;
502 else
503 conf_reg &= ~MEN_Z135_MCR_OUT1;
505 if (mctrl & TIOCM_OUT2)
506 conf_reg |= MEN_Z135_MCR_OUT2;
507 else
508 conf_reg &= ~MEN_Z135_MCR_OUT2;
510 if (mctrl & TIOCM_LOOP)
511 conf_reg |= MEN_Z135_MCR_LOOP;
512 else
513 conf_reg &= ~MEN_Z135_MCR_LOOP;
515 if (conf_reg != old)
516 iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
520 * men_z135_get_mctrl() - Get modem control lines
521 * @port: The UART port
523 * Retruns the current state of modem control inputs.
525 static unsigned int men_z135_get_mctrl(struct uart_port *port)
527 unsigned int mctrl = 0;
528 u8 msr;
530 msr = ioread8(port->membase + MEN_Z135_STAT_REG + 1);
532 if (msr & MEN_Z135_MSR_CTS)
533 mctrl |= TIOCM_CTS;
534 if (msr & MEN_Z135_MSR_DSR)
535 mctrl |= TIOCM_DSR;
536 if (msr & MEN_Z135_MSR_RI)
537 mctrl |= TIOCM_RI;
538 if (msr & MEN_Z135_MSR_DCD)
539 mctrl |= TIOCM_CAR;
541 return mctrl;
545 * men_z135_stop_tx() - Stop transmitting characters
546 * @port: The UART port
548 * Stop transmitting characters. This might be due to CTS line becomming
549 * inactive or the tty layer indicating we want to stop transmission due to
550 * an XOFF character.
552 static void men_z135_stop_tx(struct uart_port *port)
554 struct men_z135_port *uart = to_men_z135(port);
556 men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
560 * men_z135_disable_ms() - Disable Modem Status
561 * port: The UART port
563 * Enable Modem Status IRQ.
565 static void men_z135_disable_ms(struct uart_port *port)
567 struct men_z135_port *uart = to_men_z135(port);
569 men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
573 * men_z135_start_tx() - Start transmitting characters
574 * @port: The UART port
576 * Start transmitting character. This actually doesn't transmit anything, but
577 * fires off the TX tasklet.
579 static void men_z135_start_tx(struct uart_port *port)
581 struct men_z135_port *uart = to_men_z135(port);
583 if (uart->automode)
584 men_z135_disable_ms(port);
586 men_z135_handle_tx(uart);
590 * men_z135_stop_rx() - Stop receiving characters
591 * @port: The UART port
593 * Stop receiving characters; the port is in the process of being closed.
595 static void men_z135_stop_rx(struct uart_port *port)
597 struct men_z135_port *uart = to_men_z135(port);
599 men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_RXCIEN);
603 * men_z135_enable_ms() - Enable Modem Status
604 * port:
606 * Enable Modem Status IRQ.
608 static void men_z135_enable_ms(struct uart_port *port)
610 struct men_z135_port *uart = to_men_z135(port);
612 men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
615 static int men_z135_startup(struct uart_port *port)
617 struct men_z135_port *uart = to_men_z135(port);
618 int err;
619 u32 conf_reg = 0;
621 err = men_z135_request_irq(uart);
622 if (err)
623 return -ENODEV;
625 conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
627 /* Activate all but TX space available IRQ */
628 conf_reg |= MEN_Z135_ALL_IRQS & ~MEN_Z135_IER_TXCIEN;
629 conf_reg &= ~(0xff << 16);
630 conf_reg |= (txlvl << 16);
631 conf_reg |= (rxlvl << 20);
633 iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
635 if (rx_timeout)
636 iowrite32(rx_timeout, port->membase + MEN_Z135_TIMEOUT);
638 return 0;
641 static void men_z135_shutdown(struct uart_port *port)
643 struct men_z135_port *uart = to_men_z135(port);
644 u32 conf_reg = 0;
646 conf_reg |= MEN_Z135_ALL_IRQS;
648 men_z135_reg_clr(uart, MEN_Z135_CONF_REG, conf_reg);
650 free_irq(uart->port.irq, uart);
653 static void men_z135_set_termios(struct uart_port *port,
654 struct ktermios *termios,
655 struct ktermios *old)
657 struct men_z135_port *uart = to_men_z135(port);
658 unsigned int baud;
659 u32 conf_reg;
660 u32 bd_reg;
661 u32 uart_freq;
662 u8 lcr;
664 conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
665 lcr = LCR(conf_reg);
667 /* byte size */
668 switch (termios->c_cflag & CSIZE) {
669 case CS5:
670 lcr |= MEN_Z135_WL5;
671 break;
672 case CS6:
673 lcr |= MEN_Z135_WL6;
674 break;
675 case CS7:
676 lcr |= MEN_Z135_WL7;
677 break;
678 case CS8:
679 lcr |= MEN_Z135_WL8;
680 break;
683 /* stop bits */
684 if (termios->c_cflag & CSTOPB)
685 lcr |= MEN_Z135_NSTB2 << MEN_Z135_STB_SHIFT;
687 /* parity */
688 if (termios->c_cflag & PARENB) {
689 lcr |= MEN_Z135_PAR_ENA << MEN_Z135_PEN_SHIFT;
691 if (termios->c_cflag & PARODD)
692 lcr |= MEN_Z135_PTY_ODD << MEN_Z135_PTY_SHIFT;
693 else
694 lcr |= MEN_Z135_PTY_EVN << MEN_Z135_PTY_SHIFT;
695 } else
696 lcr |= MEN_Z135_PAR_DIS << MEN_Z135_PEN_SHIFT;
698 conf_reg |= MEN_Z135_IER_MSIEN;
699 if (termios->c_cflag & CRTSCTS) {
700 conf_reg |= MEN_Z135_MCR_RCFC;
701 uart->automode = true;
702 termios->c_cflag &= ~CLOCAL;
703 } else {
704 conf_reg &= ~MEN_Z135_MCR_RCFC;
705 uart->automode = false;
708 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
710 conf_reg |= lcr << MEN_Z135_LCR_SHIFT;
711 iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
713 uart_freq = ioread32(port->membase + MEN_Z135_UART_FREQ);
714 if (uart_freq == 0)
715 uart_freq = MEN_Z135_BASECLK;
717 baud = uart_get_baud_rate(port, termios, old, 0, uart_freq / 16);
719 spin_lock_irq(&port->lock);
720 if (tty_termios_baud_rate(termios))
721 tty_termios_encode_baud_rate(termios, baud, baud);
723 bd_reg = uart_freq / (4 * baud);
724 iowrite32(bd_reg, port->membase + MEN_Z135_BAUD_REG);
726 uart_update_timeout(port, termios->c_cflag, baud);
727 spin_unlock_irq(&port->lock);
730 static const char *men_z135_type(struct uart_port *port)
732 return KBUILD_MODNAME;
735 static void men_z135_release_port(struct uart_port *port)
737 iounmap(port->membase);
738 port->membase = NULL;
740 release_mem_region(port->mapbase, MEN_Z135_MEM_SIZE);
743 static int men_z135_request_port(struct uart_port *port)
745 int size = MEN_Z135_MEM_SIZE;
747 if (!request_mem_region(port->mapbase, size, "men_z135_port"))
748 return -EBUSY;
750 port->membase = ioremap(port->mapbase, MEN_Z135_MEM_SIZE);
751 if (port->membase == NULL) {
752 release_mem_region(port->mapbase, MEN_Z135_MEM_SIZE);
753 return -ENOMEM;
756 return 0;
759 static void men_z135_config_port(struct uart_port *port, int type)
761 port->type = PORT_MEN_Z135;
762 men_z135_request_port(port);
765 static int men_z135_verify_port(struct uart_port *port,
766 struct serial_struct *serinfo)
768 return -EINVAL;
771 static struct uart_ops men_z135_ops = {
772 .tx_empty = men_z135_tx_empty,
773 .set_mctrl = men_z135_set_mctrl,
774 .get_mctrl = men_z135_get_mctrl,
775 .stop_tx = men_z135_stop_tx,
776 .start_tx = men_z135_start_tx,
777 .stop_rx = men_z135_stop_rx,
778 .enable_ms = men_z135_enable_ms,
779 .startup = men_z135_startup,
780 .shutdown = men_z135_shutdown,
781 .set_termios = men_z135_set_termios,
782 .type = men_z135_type,
783 .release_port = men_z135_release_port,
784 .request_port = men_z135_request_port,
785 .config_port = men_z135_config_port,
786 .verify_port = men_z135_verify_port,
789 static struct uart_driver men_z135_driver = {
790 .owner = THIS_MODULE,
791 .driver_name = KBUILD_MODNAME,
792 .dev_name = "ttyHSU",
793 .major = 0,
794 .minor = 0,
795 .nr = MEN_Z135_MAX_PORTS,
799 * men_z135_probe() - Probe a z135 instance
800 * @mdev: The MCB device
801 * @id: The MCB device ID
803 * men_z135_probe does the basic setup of hardware resources and registers the
804 * new uart port to the tty layer.
806 static int men_z135_probe(struct mcb_device *mdev,
807 const struct mcb_device_id *id)
809 struct men_z135_port *uart;
810 struct resource *mem;
811 struct device *dev;
812 int err;
814 dev = &mdev->dev;
816 uart = devm_kzalloc(dev, sizeof(struct men_z135_port), GFP_KERNEL);
817 if (!uart)
818 return -ENOMEM;
820 uart->rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
821 if (!uart->rxbuf)
822 return -ENOMEM;
824 mem = &mdev->mem;
826 mcb_set_drvdata(mdev, uart);
828 uart->port.uartclk = MEN_Z135_BASECLK * 16;
829 uart->port.fifosize = MEN_Z135_FIFO_SIZE;
830 uart->port.iotype = UPIO_MEM;
831 uart->port.ops = &men_z135_ops;
832 uart->port.irq = mcb_get_irq(mdev);
833 uart->port.iotype = UPIO_MEM;
834 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
835 uart->port.line = line++;
836 uart->port.dev = dev;
837 uart->port.type = PORT_MEN_Z135;
838 uart->port.mapbase = mem->start;
839 uart->port.membase = NULL;
840 uart->mdev = mdev;
842 spin_lock_init(&uart->port.lock);
843 spin_lock_init(&uart->lock);
845 err = uart_add_one_port(&men_z135_driver, &uart->port);
846 if (err)
847 goto err;
849 return 0;
851 err:
852 free_page((unsigned long) uart->rxbuf);
853 dev_err(dev, "Failed to add UART: %d\n", err);
855 return err;
859 * men_z135_remove() - Remove a z135 instance from the system
861 * @mdev: The MCB device
863 static void men_z135_remove(struct mcb_device *mdev)
865 struct men_z135_port *uart = mcb_get_drvdata(mdev);
867 line--;
868 uart_remove_one_port(&men_z135_driver, &uart->port);
869 free_page((unsigned long) uart->rxbuf);
872 static const struct mcb_device_id men_z135_ids[] = {
873 { .device = 0x87 },
876 MODULE_DEVICE_TABLE(mcb, men_z135_ids);
878 static struct mcb_driver mcb_driver = {
879 .driver = {
880 .name = "z135-uart",
881 .owner = THIS_MODULE,
883 .probe = men_z135_probe,
884 .remove = men_z135_remove,
885 .id_table = men_z135_ids,
889 * men_z135_init() - Driver Registration Routine
891 * men_z135_init is the first routine called when the driver is loaded. All it
892 * does is register with the legacy MEN Chameleon subsystem.
894 static int __init men_z135_init(void)
896 int err;
898 err = uart_register_driver(&men_z135_driver);
899 if (err) {
900 pr_err("Failed to register UART: %d\n", err);
901 return err;
904 err = mcb_register_driver(&mcb_driver);
905 if (err) {
906 pr_err("Failed to register MCB driver: %d\n", err);
907 uart_unregister_driver(&men_z135_driver);
908 return err;
911 return 0;
913 module_init(men_z135_init);
916 * men_z135_exit() - Driver Exit Routine
918 * men_z135_exit is called just before the driver is removed from memory.
920 static void __exit men_z135_exit(void)
922 mcb_unregister_driver(&mcb_driver);
923 uart_unregister_driver(&men_z135_driver);
925 module_exit(men_z135_exit);
927 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
928 MODULE_LICENSE("GPL v2");
929 MODULE_DESCRIPTION("MEN 16z135 High Speed UART");
930 MODULE_ALIAS("mcb:16z135");