Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / tty / serial / xilinx_uartps.c
blob5d043de65dcf78bc422231f259daf4ec35222d53
1 /*
2 * Xilinx PS UART driver
4 * 2011 (c) Xilinx Inc.
6 * This program is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation;
9 * either version 2 of the License, or (at your option) any
10 * later version.
14 #include <linux/platform_device.h>
15 #include <linux/serial_core.h>
16 #include <linux/console.h>
17 #include <linux/serial.h>
18 #include <linux/irq.h>
19 #include <linux/io.h>
20 #include <linux/of.h>
21 #include <linux/module.h>
23 #define XUARTPS_TTY_NAME "ttyPS"
24 #define XUARTPS_NAME "xuartps"
25 #define XUARTPS_MAJOR 0 /* use dynamic node allocation */
26 #define XUARTPS_MINOR 0 /* works best with devtmpfs */
27 #define XUARTPS_NR_PORTS 2
28 #define XUARTPS_FIFO_SIZE 16 /* FIFO size */
29 #define XUARTPS_REGISTER_SPACE 0xFFF
31 #define xuartps_readl(offset) ioread32(port->membase + offset)
32 #define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
34 /********************************Register Map********************************/
35 /** UART
37 * Register offsets for the UART.
40 #define XUARTPS_CR_OFFSET 0x00 /* Control Register [8:0] */
41 #define XUARTPS_MR_OFFSET 0x04 /* Mode Register [10:0] */
42 #define XUARTPS_IER_OFFSET 0x08 /* Interrupt Enable [10:0] */
43 #define XUARTPS_IDR_OFFSET 0x0C /* Interrupt Disable [10:0] */
44 #define XUARTPS_IMR_OFFSET 0x10 /* Interrupt Mask [10:0] */
45 #define XUARTPS_ISR_OFFSET 0x14 /* Interrupt Status [10:0]*/
46 #define XUARTPS_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator [15:0] */
47 #define XUARTPS_RXTOUT_OFFSET 0x1C /* RX Timeout [7:0] */
48 #define XUARTPS_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level [5:0] */
49 #define XUARTPS_MODEMCR_OFFSET 0x24 /* Modem Control [5:0] */
50 #define XUARTPS_MODEMSR_OFFSET 0x28 /* Modem Status [8:0] */
51 #define XUARTPS_SR_OFFSET 0x2C /* Channel Status [11:0] */
52 #define XUARTPS_FIFO_OFFSET 0x30 /* FIFO [15:0] or [7:0] */
53 #define XUARTPS_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider [7:0] */
54 #define XUARTPS_FLOWDEL_OFFSET 0x38 /* Flow Delay [15:0] */
55 #define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C /* IR Minimum Received Pulse
56 Width [15:0] */
57 #define XUARTPS_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse
58 Width [7:0] */
59 #define XUARTPS_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level [5:0] */
61 /** Control Register
63 * The Control register (CR) controls the major functions of the device.
65 * Control Register Bit Definitions
67 #define XUARTPS_CR_STOPBRK 0x00000100 /* Stop TX break */
68 #define XUARTPS_CR_STARTBRK 0x00000080 /* Set TX break */
69 #define XUARTPS_CR_TX_DIS 0x00000020 /* TX disabled. */
70 #define XUARTPS_CR_TX_EN 0x00000010 /* TX enabled */
71 #define XUARTPS_CR_RX_DIS 0x00000008 /* RX disabled. */
72 #define XUARTPS_CR_RX_EN 0x00000004 /* RX enabled */
73 #define XUARTPS_CR_TXRST 0x00000002 /* TX logic reset */
74 #define XUARTPS_CR_RXRST 0x00000001 /* RX logic reset */
75 #define XUARTPS_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
77 /** Mode Register
79 * The mode register (MR) defines the mode of transfer as well as the data
80 * format. If this register is modified during transmission or reception,
81 * data validity cannot be guaranteed.
83 * Mode Register Bit Definitions
86 #define XUARTPS_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
87 #define XUARTPS_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
88 #define XUARTPS_MR_CHMODE_NORM 0x00000000 /* Normal mode */
90 #define XUARTPS_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
91 #define XUARTPS_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
93 #define XUARTPS_MR_PARITY_NONE 0x00000020 /* No parity mode */
94 #define XUARTPS_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
95 #define XUARTPS_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
96 #define XUARTPS_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
97 #define XUARTPS_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
99 #define XUARTPS_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
100 #define XUARTPS_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
101 #define XUARTPS_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
103 /** Interrupt Registers
105 * Interrupt control logic uses the interrupt enable register (IER) and the
106 * interrupt disable register (IDR) to set the value of the bits in the
107 * interrupt mask register (IMR). The IMR determines whether to pass an
108 * interrupt to the interrupt status register (ISR).
109 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
110 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
111 * Reading either IER or IDR returns 0x00.
113 * All four registers have the same bit definitions.
115 #define XUARTPS_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
116 #define XUARTPS_IXR_PARITY 0x00000080 /* Parity error interrupt */
117 #define XUARTPS_IXR_FRAMING 0x00000040 /* Framing error interrupt */
118 #define XUARTPS_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
119 #define XUARTPS_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
120 #define XUARTPS_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
121 #define XUARTPS_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
122 #define XUARTPS_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
123 #define XUARTPS_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
124 #define XUARTPS_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
125 #define XUARTPS_IXR_MASK 0x00001FFF /* Valid bit mask */
127 /** Channel Status Register
129 * The channel status register (CSR) is provided to enable the control logic
130 * to monitor the status of bits in the channel interrupt status register,
131 * even if these are masked out by the interrupt mask register.
133 #define XUARTPS_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
134 #define XUARTPS_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
135 #define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
136 #define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
139 * xuartps_isr - Interrupt handler
140 * @irq: Irq number
141 * @dev_id: Id of the port
143 * Returns IRQHANDLED
145 static irqreturn_t xuartps_isr(int irq, void *dev_id)
147 struct uart_port *port = (struct uart_port *)dev_id;
148 struct tty_struct *tty;
149 unsigned long flags;
150 unsigned int isrstatus, numbytes;
151 unsigned int data;
152 char status = TTY_NORMAL;
154 /* Get the tty which could be NULL so don't assume it's valid */
155 tty = tty_port_tty_get(&port->state->port);
157 spin_lock_irqsave(&port->lock, flags);
159 /* Read the interrupt status register to determine which
160 * interrupt(s) is/are active.
162 isrstatus = xuartps_readl(XUARTPS_ISR_OFFSET);
164 /* drop byte with parity error if IGNPAR specified */
165 if (isrstatus & port->ignore_status_mask & XUARTPS_IXR_PARITY)
166 isrstatus &= ~(XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT);
168 isrstatus &= port->read_status_mask;
169 isrstatus &= ~port->ignore_status_mask;
171 if ((isrstatus & XUARTPS_IXR_TOUT) ||
172 (isrstatus & XUARTPS_IXR_RXTRIG)) {
173 /* Receive Timeout Interrupt */
174 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
175 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
176 data = xuartps_readl(XUARTPS_FIFO_OFFSET);
177 port->icount.rx++;
179 if (isrstatus & XUARTPS_IXR_PARITY) {
180 port->icount.parity++;
181 status = TTY_PARITY;
182 } else if (isrstatus & XUARTPS_IXR_FRAMING) {
183 port->icount.frame++;
184 status = TTY_FRAME;
185 } else if (isrstatus & XUARTPS_IXR_OVERRUN)
186 port->icount.overrun++;
188 if (tty)
189 uart_insert_char(port, isrstatus,
190 XUARTPS_IXR_OVERRUN, data,
191 status);
193 spin_unlock(&port->lock);
194 if (tty)
195 tty_flip_buffer_push(tty);
196 spin_lock(&port->lock);
199 /* Dispatch an appropriate handler */
200 if ((isrstatus & XUARTPS_IXR_TXEMPTY) == XUARTPS_IXR_TXEMPTY) {
201 if (uart_circ_empty(&port->state->xmit)) {
202 xuartps_writel(XUARTPS_IXR_TXEMPTY,
203 XUARTPS_IDR_OFFSET);
204 } else {
205 numbytes = port->fifosize;
206 /* Break if no more data available in the UART buffer */
207 while (numbytes--) {
208 if (uart_circ_empty(&port->state->xmit))
209 break;
210 /* Get the data from the UART circular buffer
211 * and write it to the xuartps's TX_FIFO
212 * register.
214 xuartps_writel(
215 port->state->xmit.buf[port->state->xmit.
216 tail], XUARTPS_FIFO_OFFSET);
218 port->icount.tx++;
220 /* Adjust the tail of the UART buffer and wrap
221 * the buffer if it reaches limit.
223 port->state->xmit.tail =
224 (port->state->xmit.tail + 1) & \
225 (UART_XMIT_SIZE - 1);
228 if (uart_circ_chars_pending(
229 &port->state->xmit) < WAKEUP_CHARS)
230 uart_write_wakeup(port);
234 xuartps_writel(isrstatus, XUARTPS_ISR_OFFSET);
236 /* be sure to release the lock and tty before leaving */
237 spin_unlock_irqrestore(&port->lock, flags);
238 tty_kref_put(tty);
240 return IRQ_HANDLED;
244 * xuartps_set_baud_rate - Calculate and set the baud rate
245 * @port: Handle to the uart port structure
246 * @baud: Baud rate to set
248 * Returns baud rate, requested baud when possible, or actual baud when there
249 * was too much error
251 static unsigned int xuartps_set_baud_rate(struct uart_port *port,
252 unsigned int baud)
254 unsigned int sel_clk;
255 unsigned int calc_baud = 0;
256 unsigned int brgr_val, brdiv_val;
257 unsigned int bauderror;
259 /* Formula to obtain baud rate is
260 * baud_tx/rx rate = sel_clk/CD * (BDIV + 1)
261 * input_clk = (Uart User Defined Clock or Apb Clock)
262 * depends on UCLKEN in MR Reg
263 * sel_clk = input_clk or input_clk/8;
264 * depends on CLKS in MR reg
265 * CD and BDIV depends on values in
266 * baud rate generate register
267 * baud rate clock divisor register
269 sel_clk = port->uartclk;
270 if (xuartps_readl(XUARTPS_MR_OFFSET) & XUARTPS_MR_CLKSEL)
271 sel_clk = sel_clk / 8;
273 /* Find the best values for baud generation */
274 for (brdiv_val = 4; brdiv_val < 255; brdiv_val++) {
276 brgr_val = sel_clk / (baud * (brdiv_val + 1));
277 if (brgr_val < 2 || brgr_val > 65535)
278 continue;
280 calc_baud = sel_clk / (brgr_val * (brdiv_val + 1));
282 if (baud > calc_baud)
283 bauderror = baud - calc_baud;
284 else
285 bauderror = calc_baud - baud;
287 /* use the values when percent error is acceptable */
288 if (((bauderror * 100) / baud) < 3) {
289 calc_baud = baud;
290 break;
294 /* Set the values for the new baud rate */
295 xuartps_writel(brgr_val, XUARTPS_BAUDGEN_OFFSET);
296 xuartps_writel(brdiv_val, XUARTPS_BAUDDIV_OFFSET);
298 return calc_baud;
301 /*----------------------Uart Operations---------------------------*/
304 * xuartps_start_tx - Start transmitting bytes
305 * @port: Handle to the uart port structure
308 static void xuartps_start_tx(struct uart_port *port)
310 unsigned int status, numbytes = port->fifosize;
312 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
313 return;
315 status = xuartps_readl(XUARTPS_CR_OFFSET);
316 /* Set the TX enable bit and clear the TX disable bit to enable the
317 * transmitter.
319 xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
320 XUARTPS_CR_OFFSET);
322 while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
323 & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
325 /* Break if no more data available in the UART buffer */
326 if (uart_circ_empty(&port->state->xmit))
327 break;
329 /* Get the data from the UART circular buffer and
330 * write it to the xuartps's TX_FIFO register.
332 xuartps_writel(
333 port->state->xmit.buf[port->state->xmit.tail],
334 XUARTPS_FIFO_OFFSET);
335 port->icount.tx++;
337 /* Adjust the tail of the UART buffer and wrap
338 * the buffer if it reaches limit.
340 port->state->xmit.tail = (port->state->xmit.tail + 1) &
341 (UART_XMIT_SIZE - 1);
344 /* Enable the TX Empty interrupt */
345 xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
347 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
348 uart_write_wakeup(port);
352 * xuartps_stop_tx - Stop TX
353 * @port: Handle to the uart port structure
356 static void xuartps_stop_tx(struct uart_port *port)
358 unsigned int regval;
360 regval = xuartps_readl(XUARTPS_CR_OFFSET);
361 regval |= XUARTPS_CR_TX_DIS;
362 /* Disable the transmitter */
363 xuartps_writel(regval, XUARTPS_CR_OFFSET);
367 * xuartps_stop_rx - Stop RX
368 * @port: Handle to the uart port structure
371 static void xuartps_stop_rx(struct uart_port *port)
373 unsigned int regval;
375 regval = xuartps_readl(XUARTPS_CR_OFFSET);
376 regval |= XUARTPS_CR_RX_DIS;
377 /* Disable the receiver */
378 xuartps_writel(regval, XUARTPS_CR_OFFSET);
382 * xuartps_tx_empty - Check whether TX is empty
383 * @port: Handle to the uart port structure
385 * Returns TIOCSER_TEMT on success, 0 otherwise
387 static unsigned int xuartps_tx_empty(struct uart_port *port)
389 unsigned int status;
391 status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
392 return status ? TIOCSER_TEMT : 0;
396 * xuartps_break_ctl - Based on the input ctl we have to start or stop
397 * transmitting char breaks
398 * @port: Handle to the uart port structure
399 * @ctl: Value based on which start or stop decision is taken
402 static void xuartps_break_ctl(struct uart_port *port, int ctl)
404 unsigned int status;
405 unsigned long flags;
407 spin_lock_irqsave(&port->lock, flags);
409 status = xuartps_readl(XUARTPS_CR_OFFSET);
411 if (ctl == -1)
412 xuartps_writel(XUARTPS_CR_STARTBRK | status,
413 XUARTPS_CR_OFFSET);
414 else {
415 if ((status & XUARTPS_CR_STOPBRK) == 0)
416 xuartps_writel(XUARTPS_CR_STOPBRK | status,
417 XUARTPS_CR_OFFSET);
419 spin_unlock_irqrestore(&port->lock, flags);
423 * xuartps_set_termios - termios operations, handling data length, parity,
424 * stop bits, flow control, baud rate
425 * @port: Handle to the uart port structure
426 * @termios: Handle to the input termios structure
427 * @old: Values of the previously saved termios structure
430 static void xuartps_set_termios(struct uart_port *port,
431 struct ktermios *termios, struct ktermios *old)
433 unsigned int cval = 0;
434 unsigned int baud;
435 unsigned long flags;
436 unsigned int ctrl_reg, mode_reg;
438 spin_lock_irqsave(&port->lock, flags);
440 /* Empty the receive FIFO 1st before making changes */
441 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
442 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
443 xuartps_readl(XUARTPS_FIFO_OFFSET);
446 /* Disable the TX and RX to set baud rate */
447 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
448 (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
449 XUARTPS_CR_OFFSET);
451 /* Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk */
452 baud = uart_get_baud_rate(port, termios, old, 0, 10000000);
453 baud = xuartps_set_baud_rate(port, baud);
454 if (tty_termios_baud_rate(termios))
455 tty_termios_encode_baud_rate(termios, baud, baud);
458 * Update the per-port timeout.
460 uart_update_timeout(port, termios->c_cflag, baud);
462 /* Set TX/RX Reset */
463 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
464 (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
465 XUARTPS_CR_OFFSET);
467 ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
469 /* Clear the RX disable and TX disable bits and then set the TX enable
470 * bit and RX enable bit to enable the transmitter and receiver.
472 xuartps_writel(
473 (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
474 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
475 XUARTPS_CR_OFFSET);
477 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
479 port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
480 XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
481 port->ignore_status_mask = 0;
483 if (termios->c_iflag & INPCK)
484 port->read_status_mask |= XUARTPS_IXR_PARITY |
485 XUARTPS_IXR_FRAMING;
487 if (termios->c_iflag & IGNPAR)
488 port->ignore_status_mask |= XUARTPS_IXR_PARITY |
489 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
491 /* ignore all characters if CREAD is not set */
492 if ((termios->c_cflag & CREAD) == 0)
493 port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
494 XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
495 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
497 mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
499 /* Handling Data Size */
500 switch (termios->c_cflag & CSIZE) {
501 case CS6:
502 cval |= XUARTPS_MR_CHARLEN_6_BIT;
503 break;
504 case CS7:
505 cval |= XUARTPS_MR_CHARLEN_7_BIT;
506 break;
507 default:
508 case CS8:
509 cval |= XUARTPS_MR_CHARLEN_8_BIT;
510 termios->c_cflag &= ~CSIZE;
511 termios->c_cflag |= CS8;
512 break;
515 /* Handling Parity and Stop Bits length */
516 if (termios->c_cflag & CSTOPB)
517 cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
518 else
519 cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
521 if (termios->c_cflag & PARENB) {
522 /* Mark or Space parity */
523 if (termios->c_cflag & CMSPAR) {
524 if (termios->c_cflag & PARODD)
525 cval |= XUARTPS_MR_PARITY_MARK;
526 else
527 cval |= XUARTPS_MR_PARITY_SPACE;
528 } else if (termios->c_cflag & PARODD)
529 cval |= XUARTPS_MR_PARITY_ODD;
530 else
531 cval |= XUARTPS_MR_PARITY_EVEN;
532 } else
533 cval |= XUARTPS_MR_PARITY_NONE;
534 xuartps_writel(cval , XUARTPS_MR_OFFSET);
536 spin_unlock_irqrestore(&port->lock, flags);
540 * xuartps_startup - Called when an application opens a xuartps port
541 * @port: Handle to the uart port structure
543 * Returns 0 on success, negative error otherwise
545 static int xuartps_startup(struct uart_port *port)
547 unsigned int retval = 0, status = 0;
549 retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
550 (void *)port);
551 if (retval)
552 return retval;
554 /* Disable the TX and RX */
555 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
556 XUARTPS_CR_OFFSET);
558 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
559 * no break chars.
561 xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
562 XUARTPS_CR_OFFSET);
564 status = xuartps_readl(XUARTPS_CR_OFFSET);
566 /* Clear the RX disable and TX disable bits and then set the TX enable
567 * bit and RX enable bit to enable the transmitter and receiver.
569 xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
570 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
571 XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
573 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
574 * no parity.
576 xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
577 | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
578 XUARTPS_MR_OFFSET);
580 /* Set the RX FIFO Trigger level to 14 assuming FIFO size as 16 */
581 xuartps_writel(14, XUARTPS_RXWM_OFFSET);
583 /* Receive Timeout register is enabled with value of 10 */
584 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
587 /* Set the Interrupt Registers with desired interrupts */
588 xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
589 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
590 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
591 xuartps_writel(~(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
592 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
593 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT), XUARTPS_IDR_OFFSET);
595 return retval;
599 * xuartps_shutdown - Called when an application closes a xuartps port
600 * @port: Handle to the uart port structure
603 static void xuartps_shutdown(struct uart_port *port)
605 int status;
607 /* Disable interrupts */
608 status = xuartps_readl(XUARTPS_IMR_OFFSET);
609 xuartps_writel(status, XUARTPS_IDR_OFFSET);
611 /* Disable the TX and RX */
612 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
613 XUARTPS_CR_OFFSET);
614 free_irq(port->irq, port);
618 * xuartps_type - Set UART type to xuartps port
619 * @port: Handle to the uart port structure
621 * Returns string on success, NULL otherwise
623 static const char *xuartps_type(struct uart_port *port)
625 return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
629 * xuartps_verify_port - Verify the port params
630 * @port: Handle to the uart port structure
631 * @ser: Handle to the structure whose members are compared
633 * Returns 0 if success otherwise -EINVAL
635 static int xuartps_verify_port(struct uart_port *port,
636 struct serial_struct *ser)
638 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
639 return -EINVAL;
640 if (port->irq != ser->irq)
641 return -EINVAL;
642 if (ser->io_type != UPIO_MEM)
643 return -EINVAL;
644 if (port->iobase != ser->port)
645 return -EINVAL;
646 if (ser->hub6 != 0)
647 return -EINVAL;
648 return 0;
652 * xuartps_request_port - Claim the memory region attached to xuartps port,
653 * called when the driver adds a xuartps port via
654 * uart_add_one_port()
655 * @port: Handle to the uart port structure
657 * Returns 0, -ENOMEM if request fails
659 static int xuartps_request_port(struct uart_port *port)
661 if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
662 XUARTPS_NAME)) {
663 return -ENOMEM;
666 port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
667 if (!port->membase) {
668 dev_err(port->dev, "Unable to map registers\n");
669 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
670 return -ENOMEM;
672 return 0;
676 * xuartps_release_port - Release the memory region attached to a xuartps
677 * port, called when the driver removes a xuartps
678 * port via uart_remove_one_port().
679 * @port: Handle to the uart port structure
682 static void xuartps_release_port(struct uart_port *port)
684 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
685 iounmap(port->membase);
686 port->membase = NULL;
690 * xuartps_config_port - Configure xuartps, called when the driver adds a
691 * xuartps port
692 * @port: Handle to the uart port structure
693 * @flags: If any
696 static void xuartps_config_port(struct uart_port *port, int flags)
698 if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
699 port->type = PORT_XUARTPS;
703 * xuartps_get_mctrl - Get the modem control state
705 * @port: Handle to the uart port structure
707 * Returns the modem control state
710 static unsigned int xuartps_get_mctrl(struct uart_port *port)
712 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
715 static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
717 /* N/A */
720 static void xuartps_enable_ms(struct uart_port *port)
722 /* N/A */
725 /** The UART operations structure
727 static struct uart_ops xuartps_ops = {
728 .set_mctrl = xuartps_set_mctrl,
729 .get_mctrl = xuartps_get_mctrl,
730 .enable_ms = xuartps_enable_ms,
732 .start_tx = xuartps_start_tx, /* Start transmitting */
733 .stop_tx = xuartps_stop_tx, /* Stop transmission */
734 .stop_rx = xuartps_stop_rx, /* Stop reception */
735 .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
736 .break_ctl = xuartps_break_ctl, /* Start/stop
737 * transmitting break
739 .set_termios = xuartps_set_termios, /* Set termios */
740 .startup = xuartps_startup, /* App opens xuartps */
741 .shutdown = xuartps_shutdown, /* App closes xuartps */
742 .type = xuartps_type, /* Set UART type */
743 .verify_port = xuartps_verify_port, /* Verification of port
744 * params
746 .request_port = xuartps_request_port, /* Claim resources
747 * associated with a
748 * xuartps port
750 .release_port = xuartps_release_port, /* Release resources
751 * associated with a
752 * xuartps port
754 .config_port = xuartps_config_port, /* Configure when driver
755 * adds a xuartps port
759 static struct uart_port xuartps_port[2];
762 * xuartps_get_port - Configure the port from the platform device resource
763 * info
765 * Returns a pointer to a uart_port or NULL for failure
767 static struct uart_port *xuartps_get_port(void)
769 struct uart_port *port;
770 int id;
772 /* Find the next unused port */
773 for (id = 0; id < XUARTPS_NR_PORTS; id++)
774 if (xuartps_port[id].mapbase == 0)
775 break;
777 if (id >= XUARTPS_NR_PORTS)
778 return NULL;
780 port = &xuartps_port[id];
782 /* At this point, we've got an empty uart_port struct, initialize it */
783 spin_lock_init(&port->lock);
784 port->membase = NULL;
785 port->iobase = 1; /* mark port in use */
786 port->irq = 0;
787 port->type = PORT_UNKNOWN;
788 port->iotype = UPIO_MEM32;
789 port->flags = UPF_BOOT_AUTOCONF;
790 port->ops = &xuartps_ops;
791 port->fifosize = XUARTPS_FIFO_SIZE;
792 port->line = id;
793 port->dev = NULL;
794 return port;
797 /*-----------------------Console driver operations--------------------------*/
799 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
801 * xuartps_console_wait_tx - Wait for the TX to be full
802 * @port: Handle to the uart port structure
805 static void xuartps_console_wait_tx(struct uart_port *port)
807 while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
808 != XUARTPS_SR_TXEMPTY)
809 barrier();
813 * xuartps_console_putchar - write the character to the FIFO buffer
814 * @port: Handle to the uart port structure
815 * @ch: Character to be written
818 static void xuartps_console_putchar(struct uart_port *port, int ch)
820 xuartps_console_wait_tx(port);
821 xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
825 * xuartps_console_write - perform write operation
826 * @port: Handle to the uart port structure
827 * @s: Pointer to character array
828 * @count: No of characters
830 static void xuartps_console_write(struct console *co, const char *s,
831 unsigned int count)
833 struct uart_port *port = &xuartps_port[co->index];
834 unsigned long flags;
835 unsigned int imr;
836 int locked = 1;
838 if (oops_in_progress)
839 locked = spin_trylock_irqsave(&port->lock, flags);
840 else
841 spin_lock_irqsave(&port->lock, flags);
843 /* save and disable interrupt */
844 imr = xuartps_readl(XUARTPS_IMR_OFFSET);
845 xuartps_writel(imr, XUARTPS_IDR_OFFSET);
847 uart_console_write(port, s, count, xuartps_console_putchar);
848 xuartps_console_wait_tx(port);
850 /* restore interrupt state, it seems like there may be a h/w bug
851 * in that the interrupt enable register should not need to be
852 * written based on the data sheet
854 xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
855 xuartps_writel(imr, XUARTPS_IER_OFFSET);
857 if (locked)
858 spin_unlock_irqrestore(&port->lock, flags);
862 * xuartps_console_setup - Initialize the uart to default config
863 * @co: Console handle
864 * @options: Initial settings of uart
866 * Returns 0, -ENODEV if no device
868 static int __init xuartps_console_setup(struct console *co, char *options)
870 struct uart_port *port = &xuartps_port[co->index];
871 int baud = 9600;
872 int bits = 8;
873 int parity = 'n';
874 int flow = 'n';
876 if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
877 return -EINVAL;
879 if (!port->mapbase) {
880 pr_debug("console on ttyPS%i not present\n", co->index);
881 return -ENODEV;
884 if (options)
885 uart_parse_options(options, &baud, &parity, &bits, &flow);
887 return uart_set_options(port, co, baud, parity, bits, flow);
890 static struct uart_driver xuartps_uart_driver;
892 static struct console xuartps_console = {
893 .name = XUARTPS_TTY_NAME,
894 .write = xuartps_console_write,
895 .device = uart_console_device,
896 .setup = xuartps_console_setup,
897 .flags = CON_PRINTBUFFER,
898 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
899 .data = &xuartps_uart_driver,
903 * xuartps_console_init - Initialization call
905 * Returns 0 on success, negative error otherwise
907 static int __init xuartps_console_init(void)
909 register_console(&xuartps_console);
910 return 0;
913 console_initcall(xuartps_console_init);
915 #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
917 /** Structure Definitions
919 static struct uart_driver xuartps_uart_driver = {
920 .owner = THIS_MODULE, /* Owner */
921 .driver_name = XUARTPS_NAME, /* Driver name */
922 .dev_name = XUARTPS_TTY_NAME, /* Node name */
923 .major = XUARTPS_MAJOR, /* Major number */
924 .minor = XUARTPS_MINOR, /* Minor number */
925 .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
926 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
927 .cons = &xuartps_console, /* Console */
928 #endif
931 /* ---------------------------------------------------------------------
932 * Platform bus binding
935 * xuartps_probe - Platform driver probe
936 * @pdev: Pointer to the platform device structure
938 * Returns 0 on success, negative error otherwise
940 static int __devinit xuartps_probe(struct platform_device *pdev)
942 int rc;
943 struct uart_port *port;
944 struct resource *res, *res2;
945 int clk = 0;
947 #ifdef CONFIG_OF
948 const unsigned int *prop;
950 prop = of_get_property(pdev->dev.of_node, "clock", NULL);
951 if (prop)
952 clk = be32_to_cpup(prop);
953 #else
954 clk = *((unsigned int *)(pdev->dev.platform_data));
955 #endif
956 if (!clk) {
957 dev_err(&pdev->dev, "no clock specified\n");
958 return -ENODEV;
961 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
962 if (!res)
963 return -ENODEV;
965 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
966 if (!res2)
967 return -ENODEV;
969 /* Initialize the port structure */
970 port = xuartps_get_port();
972 if (!port) {
973 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
974 return -ENODEV;
975 } else {
976 /* Register the port.
977 * This function also registers this device with the tty layer
978 * and triggers invocation of the config_port() entry point.
980 port->mapbase = res->start;
981 port->irq = res2->start;
982 port->dev = &pdev->dev;
983 port->uartclk = clk;
984 dev_set_drvdata(&pdev->dev, port);
985 rc = uart_add_one_port(&xuartps_uart_driver, port);
986 if (rc) {
987 dev_err(&pdev->dev,
988 "uart_add_one_port() failed; err=%i\n", rc);
989 dev_set_drvdata(&pdev->dev, NULL);
990 return rc;
992 return 0;
997 * xuartps_remove - called when the platform driver is unregistered
998 * @pdev: Pointer to the platform device structure
1000 * Returns 0 on success, negative error otherwise
1002 static int __devexit xuartps_remove(struct platform_device *pdev)
1004 struct uart_port *port = dev_get_drvdata(&pdev->dev);
1005 int rc = 0;
1007 /* Remove the xuartps port from the serial core */
1008 if (port) {
1009 rc = uart_remove_one_port(&xuartps_uart_driver, port);
1010 dev_set_drvdata(&pdev->dev, NULL);
1011 port->mapbase = 0;
1013 return rc;
1017 * xuartps_suspend - suspend event
1018 * @pdev: Pointer to the platform device structure
1019 * @state: State of the device
1021 * Returns 0
1023 static int xuartps_suspend(struct platform_device *pdev, pm_message_t state)
1025 /* Call the API provided in serial_core.c file which handles
1026 * the suspend.
1028 uart_suspend_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1029 return 0;
1033 * xuartps_resume - Resume after a previous suspend
1034 * @pdev: Pointer to the platform device structure
1036 * Returns 0
1038 static int xuartps_resume(struct platform_device *pdev)
1040 uart_resume_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1041 return 0;
1044 /* Match table for of_platform binding */
1046 #ifdef CONFIG_OF
1047 static struct of_device_id xuartps_of_match[] __devinitdata = {
1048 { .compatible = "xlnx,xuartps", },
1051 MODULE_DEVICE_TABLE(of, xuartps_of_match);
1052 #else
1053 #define xuartps_of_match NULL
1054 #endif
1056 static struct platform_driver xuartps_platform_driver = {
1057 .probe = xuartps_probe, /* Probe method */
1058 .remove = __exit_p(xuartps_remove), /* Detach method */
1059 .suspend = xuartps_suspend, /* Suspend */
1060 .resume = xuartps_resume, /* Resume after a suspend */
1061 .driver = {
1062 .owner = THIS_MODULE,
1063 .name = XUARTPS_NAME, /* Driver name */
1064 .of_match_table = xuartps_of_match,
1068 /* ---------------------------------------------------------------------
1069 * Module Init and Exit
1072 * xuartps_init - Initial driver registration call
1074 * Returns whether the registration was successful or not
1076 static int __init xuartps_init(void)
1078 int retval = 0;
1080 /* Register the xuartps driver with the serial core */
1081 retval = uart_register_driver(&xuartps_uart_driver);
1082 if (retval)
1083 return retval;
1085 /* Register the platform driver */
1086 retval = platform_driver_register(&xuartps_platform_driver);
1087 if (retval)
1088 uart_unregister_driver(&xuartps_uart_driver);
1090 return retval;
1094 * xuartps_exit - Driver unregistration call
1096 static void __exit xuartps_exit(void)
1098 /* The order of unregistration is important. Unregister the
1099 * UART driver before the platform driver crashes the system.
1102 /* Unregister the platform driver */
1103 platform_driver_unregister(&xuartps_platform_driver);
1105 /* Unregister the xuartps driver */
1106 uart_unregister_driver(&xuartps_uart_driver);
1109 module_init(xuartps_init);
1110 module_exit(xuartps_exit);
1112 MODULE_DESCRIPTION("Driver for PS UART");
1113 MODULE_AUTHOR("Xilinx Inc.");
1114 MODULE_LICENSE("GPL");