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