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