blob: 82a3151e393c239ece245e4ffc743978113cde60 [file] [log] [blame]
John Linn61ec9012011-04-30 00:07:43 -04001/*
2 * Xilinx PS UART driver
3 *
4 * 2011 (c) Xilinx Inc.
5 *
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.
11 *
12 */
13
14#include <linux/platform_device.h>
John Linn61ec9012011-04-30 00:07:43 -040015#include <linux/serial.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020016#include <linux/serial_core.h>
17#include <linux/tty.h>
18#include <linux/tty_flip.h>
19#include <linux/console.h>
John Linn61ec9012011-04-30 00:07:43 -040020#include <linux/irq.h>
21#include <linux/io.h>
22#include <linux/of.h>
Paul Gortmaker578b9ce2011-05-27 16:14:23 -040023#include <linux/module.h>
John Linn61ec9012011-04-30 00:07:43 -040024
25#define XUARTPS_TTY_NAME "ttyPS"
26#define XUARTPS_NAME "xuartps"
27#define XUARTPS_MAJOR 0 /* use dynamic node allocation */
28#define XUARTPS_MINOR 0 /* works best with devtmpfs */
29#define XUARTPS_NR_PORTS 2
30#define XUARTPS_FIFO_SIZE 16 /* FIFO size */
31#define XUARTPS_REGISTER_SPACE 0xFFF
32
33#define xuartps_readl(offset) ioread32(port->membase + offset)
34#define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
35
36/********************************Register Map********************************/
37/** UART
38 *
39 * Register offsets for the UART.
40 *
41 */
42#define XUARTPS_CR_OFFSET 0x00 /* Control Register [8:0] */
43#define XUARTPS_MR_OFFSET 0x04 /* Mode Register [10:0] */
44#define XUARTPS_IER_OFFSET 0x08 /* Interrupt Enable [10:0] */
45#define XUARTPS_IDR_OFFSET 0x0C /* Interrupt Disable [10:0] */
46#define XUARTPS_IMR_OFFSET 0x10 /* Interrupt Mask [10:0] */
47#define XUARTPS_ISR_OFFSET 0x14 /* Interrupt Status [10:0]*/
48#define XUARTPS_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator [15:0] */
49#define XUARTPS_RXTOUT_OFFSET 0x1C /* RX Timeout [7:0] */
50#define XUARTPS_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level [5:0] */
51#define XUARTPS_MODEMCR_OFFSET 0x24 /* Modem Control [5:0] */
52#define XUARTPS_MODEMSR_OFFSET 0x28 /* Modem Status [8:0] */
53#define XUARTPS_SR_OFFSET 0x2C /* Channel Status [11:0] */
54#define XUARTPS_FIFO_OFFSET 0x30 /* FIFO [15:0] or [7:0] */
55#define XUARTPS_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider [7:0] */
56#define XUARTPS_FLOWDEL_OFFSET 0x38 /* Flow Delay [15:0] */
57#define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C /* IR Minimum Received Pulse
58 Width [15:0] */
59#define XUARTPS_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse
60 Width [7:0] */
61#define XUARTPS_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level [5:0] */
62
63/** Control Register
64 *
65 * The Control register (CR) controls the major functions of the device.
66 *
67 * Control Register Bit Definitions
68 */
69#define XUARTPS_CR_STOPBRK 0x00000100 /* Stop TX break */
70#define XUARTPS_CR_STARTBRK 0x00000080 /* Set TX break */
71#define XUARTPS_CR_TX_DIS 0x00000020 /* TX disabled. */
72#define XUARTPS_CR_TX_EN 0x00000010 /* TX enabled */
73#define XUARTPS_CR_RX_DIS 0x00000008 /* RX disabled. */
74#define XUARTPS_CR_RX_EN 0x00000004 /* RX enabled */
75#define XUARTPS_CR_TXRST 0x00000002 /* TX logic reset */
76#define XUARTPS_CR_RXRST 0x00000001 /* RX logic reset */
77#define XUARTPS_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
78
79/** Mode Register
80 *
81 * The mode register (MR) defines the mode of transfer as well as the data
82 * format. If this register is modified during transmission or reception,
83 * data validity cannot be guaranteed.
84 *
85 * Mode Register Bit Definitions
86 *
87 */
88#define XUARTPS_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
89#define XUARTPS_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
90#define XUARTPS_MR_CHMODE_NORM 0x00000000 /* Normal mode */
91
92#define XUARTPS_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
93#define XUARTPS_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
94
95#define XUARTPS_MR_PARITY_NONE 0x00000020 /* No parity mode */
96#define XUARTPS_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
97#define XUARTPS_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
98#define XUARTPS_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
99#define XUARTPS_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
100
101#define XUARTPS_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
102#define XUARTPS_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
103#define XUARTPS_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
104
105/** Interrupt Registers
106 *
107 * Interrupt control logic uses the interrupt enable register (IER) and the
108 * interrupt disable register (IDR) to set the value of the bits in the
109 * interrupt mask register (IMR). The IMR determines whether to pass an
110 * interrupt to the interrupt status register (ISR).
111 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
112 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
113 * Reading either IER or IDR returns 0x00.
114 *
115 * All four registers have the same bit definitions.
116 */
117#define XUARTPS_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
118#define XUARTPS_IXR_PARITY 0x00000080 /* Parity error interrupt */
119#define XUARTPS_IXR_FRAMING 0x00000040 /* Framing error interrupt */
120#define XUARTPS_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
121#define XUARTPS_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
122#define XUARTPS_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
123#define XUARTPS_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
124#define XUARTPS_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
125#define XUARTPS_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
126#define XUARTPS_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
127#define XUARTPS_IXR_MASK 0x00001FFF /* Valid bit mask */
128
129/** Channel Status Register
130 *
131 * The channel status register (CSR) is provided to enable the control logic
132 * to monitor the status of bits in the channel interrupt status register,
133 * even if these are masked out by the interrupt mask register.
134 */
135#define XUARTPS_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
136#define XUARTPS_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
137#define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
138#define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
139
140/**
141 * xuartps_isr - Interrupt handler
142 * @irq: Irq number
143 * @dev_id: Id of the port
144 *
145 * Returns IRQHANDLED
146 **/
147static irqreturn_t xuartps_isr(int irq, void *dev_id)
148{
149 struct uart_port *port = (struct uart_port *)dev_id;
John Linn61ec9012011-04-30 00:07:43 -0400150 unsigned long flags;
151 unsigned int isrstatus, numbytes;
152 unsigned int data;
153 char status = TTY_NORMAL;
154
John Linn61ec9012011-04-30 00:07:43 -0400155 spin_lock_irqsave(&port->lock, flags);
156
157 /* Read the interrupt status register to determine which
158 * interrupt(s) is/are active.
159 */
160 isrstatus = xuartps_readl(XUARTPS_ISR_OFFSET);
161
162 /* drop byte with parity error if IGNPAR specified */
163 if (isrstatus & port->ignore_status_mask & XUARTPS_IXR_PARITY)
164 isrstatus &= ~(XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT);
165
166 isrstatus &= port->read_status_mask;
167 isrstatus &= ~port->ignore_status_mask;
168
169 if ((isrstatus & XUARTPS_IXR_TOUT) ||
170 (isrstatus & XUARTPS_IXR_RXTRIG)) {
171 /* Receive Timeout Interrupt */
172 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
173 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
174 data = xuartps_readl(XUARTPS_FIFO_OFFSET);
175 port->icount.rx++;
176
177 if (isrstatus & XUARTPS_IXR_PARITY) {
178 port->icount.parity++;
179 status = TTY_PARITY;
180 } else if (isrstatus & XUARTPS_IXR_FRAMING) {
181 port->icount.frame++;
182 status = TTY_FRAME;
183 } else if (isrstatus & XUARTPS_IXR_OVERRUN)
184 port->icount.overrun++;
185
Jiri Slaby2e124b42013-01-03 15:53:06 +0100186 uart_insert_char(port, isrstatus, XUARTPS_IXR_OVERRUN,
187 data, status);
John Linn61ec9012011-04-30 00:07:43 -0400188 }
189 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100190 tty_flip_buffer_push(&port->state->port);
John Linn61ec9012011-04-30 00:07:43 -0400191 spin_lock(&port->lock);
192 }
193
194 /* Dispatch an appropriate handler */
195 if ((isrstatus & XUARTPS_IXR_TXEMPTY) == XUARTPS_IXR_TXEMPTY) {
196 if (uart_circ_empty(&port->state->xmit)) {
197 xuartps_writel(XUARTPS_IXR_TXEMPTY,
198 XUARTPS_IDR_OFFSET);
199 } else {
200 numbytes = port->fifosize;
201 /* Break if no more data available in the UART buffer */
202 while (numbytes--) {
203 if (uart_circ_empty(&port->state->xmit))
204 break;
205 /* Get the data from the UART circular buffer
206 * and write it to the xuartps's TX_FIFO
207 * register.
208 */
209 xuartps_writel(
210 port->state->xmit.buf[port->state->xmit.
211 tail], XUARTPS_FIFO_OFFSET);
212
213 port->icount.tx++;
214
215 /* Adjust the tail of the UART buffer and wrap
216 * the buffer if it reaches limit.
217 */
218 port->state->xmit.tail =
219 (port->state->xmit.tail + 1) & \
220 (UART_XMIT_SIZE - 1);
221 }
222
223 if (uart_circ_chars_pending(
224 &port->state->xmit) < WAKEUP_CHARS)
225 uart_write_wakeup(port);
226 }
227 }
228
229 xuartps_writel(isrstatus, XUARTPS_ISR_OFFSET);
230
231 /* be sure to release the lock and tty before leaving */
232 spin_unlock_irqrestore(&port->lock, flags);
John Linn61ec9012011-04-30 00:07:43 -0400233
234 return IRQ_HANDLED;
235}
236
237/**
238 * xuartps_set_baud_rate - Calculate and set the baud rate
239 * @port: Handle to the uart port structure
240 * @baud: Baud rate to set
241 *
242 * Returns baud rate, requested baud when possible, or actual baud when there
243 * was too much error
244 **/
245static unsigned int xuartps_set_baud_rate(struct uart_port *port,
246 unsigned int baud)
247{
248 unsigned int sel_clk;
249 unsigned int calc_baud = 0;
250 unsigned int brgr_val, brdiv_val;
251 unsigned int bauderror;
252
253 /* Formula to obtain baud rate is
254 * baud_tx/rx rate = sel_clk/CD * (BDIV + 1)
255 * input_clk = (Uart User Defined Clock or Apb Clock)
256 * depends on UCLKEN in MR Reg
257 * sel_clk = input_clk or input_clk/8;
258 * depends on CLKS in MR reg
259 * CD and BDIV depends on values in
260 * baud rate generate register
261 * baud rate clock divisor register
262 */
263 sel_clk = port->uartclk;
264 if (xuartps_readl(XUARTPS_MR_OFFSET) & XUARTPS_MR_CLKSEL)
265 sel_clk = sel_clk / 8;
266
267 /* Find the best values for baud generation */
268 for (brdiv_val = 4; brdiv_val < 255; brdiv_val++) {
269
270 brgr_val = sel_clk / (baud * (brdiv_val + 1));
271 if (brgr_val < 2 || brgr_val > 65535)
272 continue;
273
274 calc_baud = sel_clk / (brgr_val * (brdiv_val + 1));
275
276 if (baud > calc_baud)
277 bauderror = baud - calc_baud;
278 else
279 bauderror = calc_baud - baud;
280
281 /* use the values when percent error is acceptable */
282 if (((bauderror * 100) / baud) < 3) {
283 calc_baud = baud;
284 break;
285 }
286 }
287
288 /* Set the values for the new baud rate */
289 xuartps_writel(brgr_val, XUARTPS_BAUDGEN_OFFSET);
290 xuartps_writel(brdiv_val, XUARTPS_BAUDDIV_OFFSET);
291
292 return calc_baud;
293}
294
295/*----------------------Uart Operations---------------------------*/
296
297/**
298 * xuartps_start_tx - Start transmitting bytes
299 * @port: Handle to the uart port structure
300 *
301 **/
302static void xuartps_start_tx(struct uart_port *port)
303{
304 unsigned int status, numbytes = port->fifosize;
305
306 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
307 return;
308
309 status = xuartps_readl(XUARTPS_CR_OFFSET);
310 /* Set the TX enable bit and clear the TX disable bit to enable the
311 * transmitter.
312 */
313 xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
314 XUARTPS_CR_OFFSET);
315
316 while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
317 & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
318
319 /* Break if no more data available in the UART buffer */
320 if (uart_circ_empty(&port->state->xmit))
321 break;
322
323 /* Get the data from the UART circular buffer and
324 * write it to the xuartps's TX_FIFO register.
325 */
326 xuartps_writel(
327 port->state->xmit.buf[port->state->xmit.tail],
328 XUARTPS_FIFO_OFFSET);
329 port->icount.tx++;
330
331 /* Adjust the tail of the UART buffer and wrap
332 * the buffer if it reaches limit.
333 */
334 port->state->xmit.tail = (port->state->xmit.tail + 1) &
335 (UART_XMIT_SIZE - 1);
336 }
337
338 /* Enable the TX Empty interrupt */
339 xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
340
341 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
342 uart_write_wakeup(port);
343}
344
345/**
346 * xuartps_stop_tx - Stop TX
347 * @port: Handle to the uart port structure
348 *
349 **/
350static void xuartps_stop_tx(struct uart_port *port)
351{
352 unsigned int regval;
353
354 regval = xuartps_readl(XUARTPS_CR_OFFSET);
355 regval |= XUARTPS_CR_TX_DIS;
356 /* Disable the transmitter */
357 xuartps_writel(regval, XUARTPS_CR_OFFSET);
358}
359
360/**
361 * xuartps_stop_rx - Stop RX
362 * @port: Handle to the uart port structure
363 *
364 **/
365static void xuartps_stop_rx(struct uart_port *port)
366{
367 unsigned int regval;
368
369 regval = xuartps_readl(XUARTPS_CR_OFFSET);
370 regval |= XUARTPS_CR_RX_DIS;
371 /* Disable the receiver */
372 xuartps_writel(regval, XUARTPS_CR_OFFSET);
373}
374
375/**
376 * xuartps_tx_empty - Check whether TX is empty
377 * @port: Handle to the uart port structure
378 *
379 * Returns TIOCSER_TEMT on success, 0 otherwise
380 **/
381static unsigned int xuartps_tx_empty(struct uart_port *port)
382{
383 unsigned int status;
384
385 status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
386 return status ? TIOCSER_TEMT : 0;
387}
388
389/**
390 * xuartps_break_ctl - Based on the input ctl we have to start or stop
391 * transmitting char breaks
392 * @port: Handle to the uart port structure
393 * @ctl: Value based on which start or stop decision is taken
394 *
395 **/
396static void xuartps_break_ctl(struct uart_port *port, int ctl)
397{
398 unsigned int status;
399 unsigned long flags;
400
401 spin_lock_irqsave(&port->lock, flags);
402
403 status = xuartps_readl(XUARTPS_CR_OFFSET);
404
405 if (ctl == -1)
406 xuartps_writel(XUARTPS_CR_STARTBRK | status,
407 XUARTPS_CR_OFFSET);
408 else {
409 if ((status & XUARTPS_CR_STOPBRK) == 0)
410 xuartps_writel(XUARTPS_CR_STOPBRK | status,
411 XUARTPS_CR_OFFSET);
412 }
413 spin_unlock_irqrestore(&port->lock, flags);
414}
415
416/**
417 * xuartps_set_termios - termios operations, handling data length, parity,
418 * stop bits, flow control, baud rate
419 * @port: Handle to the uart port structure
420 * @termios: Handle to the input termios structure
421 * @old: Values of the previously saved termios structure
422 *
423 **/
424static void xuartps_set_termios(struct uart_port *port,
425 struct ktermios *termios, struct ktermios *old)
426{
427 unsigned int cval = 0;
428 unsigned int baud;
429 unsigned long flags;
430 unsigned int ctrl_reg, mode_reg;
431
432 spin_lock_irqsave(&port->lock, flags);
433
434 /* Empty the receive FIFO 1st before making changes */
435 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
436 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
437 xuartps_readl(XUARTPS_FIFO_OFFSET);
438 }
439
440 /* Disable the TX and RX to set baud rate */
441 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
442 (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
443 XUARTPS_CR_OFFSET);
444
445 /* Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk */
446 baud = uart_get_baud_rate(port, termios, old, 0, 10000000);
447 baud = xuartps_set_baud_rate(port, baud);
448 if (tty_termios_baud_rate(termios))
449 tty_termios_encode_baud_rate(termios, baud, baud);
450
451 /*
452 * Update the per-port timeout.
453 */
454 uart_update_timeout(port, termios->c_cflag, baud);
455
456 /* Set TX/RX Reset */
457 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
458 (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
459 XUARTPS_CR_OFFSET);
460
461 ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
462
463 /* Clear the RX disable and TX disable bits and then set the TX enable
464 * bit and RX enable bit to enable the transmitter and receiver.
465 */
466 xuartps_writel(
467 (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
468 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
469 XUARTPS_CR_OFFSET);
470
471 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
472
473 port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
474 XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
475 port->ignore_status_mask = 0;
476
477 if (termios->c_iflag & INPCK)
478 port->read_status_mask |= XUARTPS_IXR_PARITY |
479 XUARTPS_IXR_FRAMING;
480
481 if (termios->c_iflag & IGNPAR)
482 port->ignore_status_mask |= XUARTPS_IXR_PARITY |
483 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
484
485 /* ignore all characters if CREAD is not set */
486 if ((termios->c_cflag & CREAD) == 0)
487 port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
488 XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
489 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
490
491 mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
492
493 /* Handling Data Size */
494 switch (termios->c_cflag & CSIZE) {
495 case CS6:
496 cval |= XUARTPS_MR_CHARLEN_6_BIT;
497 break;
498 case CS7:
499 cval |= XUARTPS_MR_CHARLEN_7_BIT;
500 break;
501 default:
502 case CS8:
503 cval |= XUARTPS_MR_CHARLEN_8_BIT;
504 termios->c_cflag &= ~CSIZE;
505 termios->c_cflag |= CS8;
506 break;
507 }
508
509 /* Handling Parity and Stop Bits length */
510 if (termios->c_cflag & CSTOPB)
511 cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
512 else
513 cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
514
515 if (termios->c_cflag & PARENB) {
516 /* Mark or Space parity */
517 if (termios->c_cflag & CMSPAR) {
518 if (termios->c_cflag & PARODD)
519 cval |= XUARTPS_MR_PARITY_MARK;
520 else
521 cval |= XUARTPS_MR_PARITY_SPACE;
522 } else if (termios->c_cflag & PARODD)
523 cval |= XUARTPS_MR_PARITY_ODD;
524 else
525 cval |= XUARTPS_MR_PARITY_EVEN;
526 } else
527 cval |= XUARTPS_MR_PARITY_NONE;
528 xuartps_writel(cval , XUARTPS_MR_OFFSET);
529
530 spin_unlock_irqrestore(&port->lock, flags);
531}
532
533/**
534 * xuartps_startup - Called when an application opens a xuartps port
535 * @port: Handle to the uart port structure
536 *
537 * Returns 0 on success, negative error otherwise
538 **/
539static int xuartps_startup(struct uart_port *port)
540{
541 unsigned int retval = 0, status = 0;
542
543 retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
544 (void *)port);
545 if (retval)
546 return retval;
547
548 /* Disable the TX and RX */
549 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
550 XUARTPS_CR_OFFSET);
551
552 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
553 * no break chars.
554 */
555 xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
556 XUARTPS_CR_OFFSET);
557
558 status = xuartps_readl(XUARTPS_CR_OFFSET);
559
560 /* Clear the RX disable and TX disable bits and then set the TX enable
561 * bit and RX enable bit to enable the transmitter and receiver.
562 */
563 xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
564 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
565 XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
566
567 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
568 * no parity.
569 */
570 xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
571 | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
572 XUARTPS_MR_OFFSET);
573
574 /* Set the RX FIFO Trigger level to 14 assuming FIFO size as 16 */
575 xuartps_writel(14, XUARTPS_RXWM_OFFSET);
576
577 /* Receive Timeout register is enabled with value of 10 */
578 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
579
580
581 /* Set the Interrupt Registers with desired interrupts */
582 xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
583 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
584 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
585 xuartps_writel(~(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
586 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
587 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT), XUARTPS_IDR_OFFSET);
588
589 return retval;
590}
591
592/**
593 * xuartps_shutdown - Called when an application closes a xuartps port
594 * @port: Handle to the uart port structure
595 *
596 **/
597static void xuartps_shutdown(struct uart_port *port)
598{
599 int status;
600
601 /* Disable interrupts */
602 status = xuartps_readl(XUARTPS_IMR_OFFSET);
603 xuartps_writel(status, XUARTPS_IDR_OFFSET);
604
605 /* Disable the TX and RX */
606 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
607 XUARTPS_CR_OFFSET);
608 free_irq(port->irq, port);
609}
610
611/**
612 * xuartps_type - Set UART type to xuartps port
613 * @port: Handle to the uart port structure
614 *
615 * Returns string on success, NULL otherwise
616 **/
617static const char *xuartps_type(struct uart_port *port)
618{
619 return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
620}
621
622/**
623 * xuartps_verify_port - Verify the port params
624 * @port: Handle to the uart port structure
625 * @ser: Handle to the structure whose members are compared
626 *
627 * Returns 0 if success otherwise -EINVAL
628 **/
629static int xuartps_verify_port(struct uart_port *port,
630 struct serial_struct *ser)
631{
632 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
633 return -EINVAL;
634 if (port->irq != ser->irq)
635 return -EINVAL;
636 if (ser->io_type != UPIO_MEM)
637 return -EINVAL;
638 if (port->iobase != ser->port)
639 return -EINVAL;
640 if (ser->hub6 != 0)
641 return -EINVAL;
642 return 0;
643}
644
645/**
646 * xuartps_request_port - Claim the memory region attached to xuartps port,
647 * called when the driver adds a xuartps port via
648 * uart_add_one_port()
649 * @port: Handle to the uart port structure
650 *
651 * Returns 0, -ENOMEM if request fails
652 **/
653static int xuartps_request_port(struct uart_port *port)
654{
655 if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
656 XUARTPS_NAME)) {
657 return -ENOMEM;
658 }
659
660 port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
661 if (!port->membase) {
662 dev_err(port->dev, "Unable to map registers\n");
663 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
664 return -ENOMEM;
665 }
666 return 0;
667}
668
669/**
670 * xuartps_release_port - Release the memory region attached to a xuartps
671 * port, called when the driver removes a xuartps
672 * port via uart_remove_one_port().
673 * @port: Handle to the uart port structure
674 *
675 **/
676static void xuartps_release_port(struct uart_port *port)
677{
678 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
679 iounmap(port->membase);
680 port->membase = NULL;
681}
682
683/**
684 * xuartps_config_port - Configure xuartps, called when the driver adds a
685 * xuartps port
686 * @port: Handle to the uart port structure
687 * @flags: If any
688 *
689 **/
690static void xuartps_config_port(struct uart_port *port, int flags)
691{
692 if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
693 port->type = PORT_XUARTPS;
694}
695
696/**
697 * xuartps_get_mctrl - Get the modem control state
698 *
699 * @port: Handle to the uart port structure
700 *
701 * Returns the modem control state
702 *
703 **/
704static unsigned int xuartps_get_mctrl(struct uart_port *port)
705{
706 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
707}
708
709static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
710{
711 /* N/A */
712}
713
714static void xuartps_enable_ms(struct uart_port *port)
715{
716 /* N/A */
717}
718
719/** The UART operations structure
720 */
721static struct uart_ops xuartps_ops = {
722 .set_mctrl = xuartps_set_mctrl,
723 .get_mctrl = xuartps_get_mctrl,
724 .enable_ms = xuartps_enable_ms,
725
726 .start_tx = xuartps_start_tx, /* Start transmitting */
727 .stop_tx = xuartps_stop_tx, /* Stop transmission */
728 .stop_rx = xuartps_stop_rx, /* Stop reception */
729 .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
730 .break_ctl = xuartps_break_ctl, /* Start/stop
731 * transmitting break
732 */
733 .set_termios = xuartps_set_termios, /* Set termios */
734 .startup = xuartps_startup, /* App opens xuartps */
735 .shutdown = xuartps_shutdown, /* App closes xuartps */
736 .type = xuartps_type, /* Set UART type */
737 .verify_port = xuartps_verify_port, /* Verification of port
738 * params
739 */
740 .request_port = xuartps_request_port, /* Claim resources
741 * associated with a
742 * xuartps port
743 */
744 .release_port = xuartps_release_port, /* Release resources
745 * associated with a
746 * xuartps port
747 */
748 .config_port = xuartps_config_port, /* Configure when driver
749 * adds a xuartps port
750 */
751};
752
753static struct uart_port xuartps_port[2];
754
755/**
756 * xuartps_get_port - Configure the port from the platform device resource
757 * info
758 *
759 * Returns a pointer to a uart_port or NULL for failure
760 **/
761static struct uart_port *xuartps_get_port(void)
762{
763 struct uart_port *port;
764 int id;
765
766 /* Find the next unused port */
767 for (id = 0; id < XUARTPS_NR_PORTS; id++)
768 if (xuartps_port[id].mapbase == 0)
769 break;
770
771 if (id >= XUARTPS_NR_PORTS)
772 return NULL;
773
774 port = &xuartps_port[id];
775
776 /* At this point, we've got an empty uart_port struct, initialize it */
777 spin_lock_init(&port->lock);
778 port->membase = NULL;
779 port->iobase = 1; /* mark port in use */
780 port->irq = 0;
781 port->type = PORT_UNKNOWN;
782 port->iotype = UPIO_MEM32;
783 port->flags = UPF_BOOT_AUTOCONF;
784 port->ops = &xuartps_ops;
785 port->fifosize = XUARTPS_FIFO_SIZE;
786 port->line = id;
787 port->dev = NULL;
788 return port;
789}
790
791/*-----------------------Console driver operations--------------------------*/
792
793#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
794/**
795 * xuartps_console_wait_tx - Wait for the TX to be full
796 * @port: Handle to the uart port structure
797 *
798 **/
799static void xuartps_console_wait_tx(struct uart_port *port)
800{
801 while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
802 != XUARTPS_SR_TXEMPTY)
803 barrier();
804}
805
806/**
807 * xuartps_console_putchar - write the character to the FIFO buffer
808 * @port: Handle to the uart port structure
809 * @ch: Character to be written
810 *
811 **/
812static void xuartps_console_putchar(struct uart_port *port, int ch)
813{
814 xuartps_console_wait_tx(port);
815 xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
816}
817
818/**
819 * xuartps_console_write - perform write operation
820 * @port: Handle to the uart port structure
821 * @s: Pointer to character array
822 * @count: No of characters
823 **/
824static void xuartps_console_write(struct console *co, const char *s,
825 unsigned int count)
826{
827 struct uart_port *port = &xuartps_port[co->index];
828 unsigned long flags;
829 unsigned int imr;
830 int locked = 1;
831
832 if (oops_in_progress)
833 locked = spin_trylock_irqsave(&port->lock, flags);
834 else
835 spin_lock_irqsave(&port->lock, flags);
836
837 /* save and disable interrupt */
838 imr = xuartps_readl(XUARTPS_IMR_OFFSET);
839 xuartps_writel(imr, XUARTPS_IDR_OFFSET);
840
841 uart_console_write(port, s, count, xuartps_console_putchar);
842 xuartps_console_wait_tx(port);
843
844 /* restore interrupt state, it seems like there may be a h/w bug
845 * in that the interrupt enable register should not need to be
846 * written based on the data sheet
847 */
848 xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
849 xuartps_writel(imr, XUARTPS_IER_OFFSET);
850
851 if (locked)
852 spin_unlock_irqrestore(&port->lock, flags);
853}
854
855/**
856 * xuartps_console_setup - Initialize the uart to default config
857 * @co: Console handle
858 * @options: Initial settings of uart
859 *
860 * Returns 0, -ENODEV if no device
861 **/
862static int __init xuartps_console_setup(struct console *co, char *options)
863{
864 struct uart_port *port = &xuartps_port[co->index];
865 int baud = 9600;
866 int bits = 8;
867 int parity = 'n';
868 int flow = 'n';
869
870 if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
871 return -EINVAL;
872
873 if (!port->mapbase) {
874 pr_debug("console on ttyPS%i not present\n", co->index);
875 return -ENODEV;
876 }
877
878 if (options)
879 uart_parse_options(options, &baud, &parity, &bits, &flow);
880
881 return uart_set_options(port, co, baud, parity, bits, flow);
882}
883
884static struct uart_driver xuartps_uart_driver;
885
886static struct console xuartps_console = {
887 .name = XUARTPS_TTY_NAME,
888 .write = xuartps_console_write,
889 .device = uart_console_device,
890 .setup = xuartps_console_setup,
891 .flags = CON_PRINTBUFFER,
892 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
893 .data = &xuartps_uart_driver,
894};
895
896/**
897 * xuartps_console_init - Initialization call
898 *
899 * Returns 0 on success, negative error otherwise
900 **/
901static int __init xuartps_console_init(void)
902{
903 register_console(&xuartps_console);
904 return 0;
905}
906
907console_initcall(xuartps_console_init);
908
909#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
910
911/** Structure Definitions
912 */
913static struct uart_driver xuartps_uart_driver = {
914 .owner = THIS_MODULE, /* Owner */
915 .driver_name = XUARTPS_NAME, /* Driver name */
916 .dev_name = XUARTPS_TTY_NAME, /* Node name */
917 .major = XUARTPS_MAJOR, /* Major number */
918 .minor = XUARTPS_MINOR, /* Minor number */
919 .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
920#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
921 .cons = &xuartps_console, /* Console */
922#endif
923};
924
925/* ---------------------------------------------------------------------
926 * Platform bus binding
927 */
928/**
929 * xuartps_probe - Platform driver probe
930 * @pdev: Pointer to the platform device structure
931 *
932 * Returns 0 on success, negative error otherwise
933 **/
Bill Pemberton9671f092012-11-19 13:21:50 -0500934static int xuartps_probe(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -0400935{
936 int rc;
937 struct uart_port *port;
938 struct resource *res, *res2;
939 int clk = 0;
940
John Linn61ec9012011-04-30 00:07:43 -0400941 const unsigned int *prop;
942
943 prop = of_get_property(pdev->dev.of_node, "clock", NULL);
944 if (prop)
945 clk = be32_to_cpup(prop);
John Linn61ec9012011-04-30 00:07:43 -0400946 if (!clk) {
947 dev_err(&pdev->dev, "no clock specified\n");
948 return -ENODEV;
949 }
950
951 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
952 if (!res)
953 return -ENODEV;
954
955 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
956 if (!res2)
957 return -ENODEV;
958
959 /* Initialize the port structure */
960 port = xuartps_get_port();
961
962 if (!port) {
963 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
964 return -ENODEV;
965 } else {
966 /* Register the port.
967 * This function also registers this device with the tty layer
968 * and triggers invocation of the config_port() entry point.
969 */
970 port->mapbase = res->start;
971 port->irq = res2->start;
972 port->dev = &pdev->dev;
973 port->uartclk = clk;
974 dev_set_drvdata(&pdev->dev, port);
975 rc = uart_add_one_port(&xuartps_uart_driver, port);
976 if (rc) {
977 dev_err(&pdev->dev,
978 "uart_add_one_port() failed; err=%i\n", rc);
979 dev_set_drvdata(&pdev->dev, NULL);
980 return rc;
981 }
982 return 0;
983 }
984}
985
986/**
987 * xuartps_remove - called when the platform driver is unregistered
988 * @pdev: Pointer to the platform device structure
989 *
990 * Returns 0 on success, negative error otherwise
991 **/
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500992static int xuartps_remove(struct platform_device *pdev)
John Linn61ec9012011-04-30 00:07:43 -0400993{
994 struct uart_port *port = dev_get_drvdata(&pdev->dev);
995 int rc = 0;
996
997 /* Remove the xuartps port from the serial core */
998 if (port) {
999 rc = uart_remove_one_port(&xuartps_uart_driver, port);
1000 dev_set_drvdata(&pdev->dev, NULL);
1001 port->mapbase = 0;
1002 }
1003 return rc;
1004}
1005
1006/**
1007 * xuartps_suspend - suspend event
1008 * @pdev: Pointer to the platform device structure
1009 * @state: State of the device
1010 *
1011 * Returns 0
1012 **/
1013static int xuartps_suspend(struct platform_device *pdev, pm_message_t state)
1014{
1015 /* Call the API provided in serial_core.c file which handles
1016 * the suspend.
1017 */
1018 uart_suspend_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1019 return 0;
1020}
1021
1022/**
1023 * xuartps_resume - Resume after a previous suspend
1024 * @pdev: Pointer to the platform device structure
1025 *
1026 * Returns 0
1027 **/
1028static int xuartps_resume(struct platform_device *pdev)
1029{
1030 uart_resume_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1031 return 0;
1032}
1033
1034/* Match table for of_platform binding */
Bill Pembertonde88b342012-11-19 13:24:32 -05001035static struct of_device_id xuartps_of_match[] = {
John Linn61ec9012011-04-30 00:07:43 -04001036 { .compatible = "xlnx,xuartps", },
1037 {}
1038};
1039MODULE_DEVICE_TABLE(of, xuartps_of_match);
John Linn61ec9012011-04-30 00:07:43 -04001040
1041static struct platform_driver xuartps_platform_driver = {
1042 .probe = xuartps_probe, /* Probe method */
1043 .remove = __exit_p(xuartps_remove), /* Detach method */
1044 .suspend = xuartps_suspend, /* Suspend */
1045 .resume = xuartps_resume, /* Resume after a suspend */
1046 .driver = {
1047 .owner = THIS_MODULE,
1048 .name = XUARTPS_NAME, /* Driver name */
1049 .of_match_table = xuartps_of_match,
1050 },
1051};
1052
1053/* ---------------------------------------------------------------------
1054 * Module Init and Exit
1055 */
1056/**
1057 * xuartps_init - Initial driver registration call
1058 *
1059 * Returns whether the registration was successful or not
1060 **/
1061static int __init xuartps_init(void)
1062{
1063 int retval = 0;
1064
1065 /* Register the xuartps driver with the serial core */
1066 retval = uart_register_driver(&xuartps_uart_driver);
1067 if (retval)
1068 return retval;
1069
1070 /* Register the platform driver */
1071 retval = platform_driver_register(&xuartps_platform_driver);
1072 if (retval)
1073 uart_unregister_driver(&xuartps_uart_driver);
1074
1075 return retval;
1076}
1077
1078/**
1079 * xuartps_exit - Driver unregistration call
1080 **/
1081static void __exit xuartps_exit(void)
1082{
1083 /* The order of unregistration is important. Unregister the
1084 * UART driver before the platform driver crashes the system.
1085 */
1086
1087 /* Unregister the platform driver */
1088 platform_driver_unregister(&xuartps_platform_driver);
1089
1090 /* Unregister the xuartps driver */
1091 uart_unregister_driver(&xuartps_uart_driver);
1092}
1093
1094module_init(xuartps_init);
1095module_exit(xuartps_exit);
1096
1097MODULE_DESCRIPTION("Driver for PS UART");
1098MODULE_AUTHOR("Xilinx Inc.");
1099MODULE_LICENSE("GPL");