Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | |
2 | Low Level Serial API | |
3 | -------------------- | |
4 | ||
5 | ||
1da177e4 LT |
6 | This document is meant as a brief overview of some aspects of the new serial |
7 | driver. It is not complete, any questions you have should be directed to | |
8 | <rmk@arm.linux.org.uk> | |
9 | ||
67ab7f59 | 10 | The reference implementation is contained within amba_pl011.c. |
1da177e4 LT |
11 | |
12 | ||
13 | ||
14 | Low Level Serial Hardware Driver | |
15 | -------------------------------- | |
16 | ||
17 | The low level serial hardware driver is responsible for supplying port | |
18 | information (defined by uart_port) and a set of control methods (defined | |
19 | by uart_ops) to the core serial driver. The low level driver is also | |
20 | responsible for handling interrupts for the port, and providing any | |
21 | console support. | |
22 | ||
23 | ||
24 | Console Support | |
25 | --------------- | |
26 | ||
27 | The serial core provides a few helper functions. This includes identifing | |
28 | the correct port structure (via uart_get_console) and decoding command line | |
29 | arguments (uart_parse_options). | |
30 | ||
d124fd3b GU |
31 | There is also a helper function (uart_console_write) which performs a |
32 | character by character write, translating newlines to CRLF sequences. | |
33 | Driver writers are recommended to use this function rather than implementing | |
34 | their own version. | |
35 | ||
1da177e4 LT |
36 | |
37 | Locking | |
38 | ------- | |
39 | ||
40 | It is the responsibility of the low level hardware driver to perform the | |
41 | necessary locking using port->lock. There are some exceptions (which | |
42 | are described in the uart_ops listing below.) | |
43 | ||
4895b1d7 | 44 | There are two locks. A per-port spinlock, and an overall semaphore. |
1da177e4 LT |
45 | |
46 | From the core driver perspective, the port->lock locks the following | |
47 | data: | |
48 | ||
49 | port->mctrl | |
50 | port->icount | |
51 | info->xmit.head (circ->head) | |
52 | info->xmit.tail (circ->tail) | |
53 | ||
54 | The low level driver is free to use this lock to provide any additional | |
55 | locking. | |
56 | ||
1da177e4 | 57 | The port_sem semaphore is used to protect against ports being added/ |
7c8ab967 PH |
58 | removed or reconfigured at inappropriate times. Since v2.6.27, this |
59 | semaphore has been the 'mutex' member of the tty_port struct, and | |
60 | commonly referred to as the port mutex (or port->mutex). | |
1da177e4 LT |
61 | |
62 | ||
63 | uart_ops | |
64 | -------- | |
65 | ||
66 | The uart_ops structure is the main interface between serial_core and the | |
67 | hardware specific driver. It contains all the methods to control the | |
68 | hardware. | |
69 | ||
70 | tx_empty(port) | |
71 | This function tests whether the transmitter fifo and shifter | |
72 | for the port described by 'port' is empty. If it is empty, | |
73 | this function should return TIOCSER_TEMT, otherwise return 0. | |
74 | If the port does not support this operation, then it should | |
75 | return TIOCSER_TEMT. | |
76 | ||
77 | Locking: none. | |
78 | Interrupts: caller dependent. | |
79 | This call must not sleep | |
80 | ||
81 | set_mctrl(port, mctrl) | |
82 | This function sets the modem control lines for port described | |
83 | by 'port' to the state described by mctrl. The relevant bits | |
84 | of mctrl are: | |
85 | - TIOCM_RTS RTS signal. | |
86 | - TIOCM_DTR DTR signal. | |
87 | - TIOCM_OUT1 OUT1 signal. | |
88 | - TIOCM_OUT2 OUT2 signal. | |
67ab7f59 | 89 | - TIOCM_LOOP Set the port into loopback mode. |
1da177e4 LT |
90 | If the appropriate bit is set, the signal should be driven |
91 | active. If the bit is clear, the signal should be driven | |
92 | inactive. | |
93 | ||
94 | Locking: port->lock taken. | |
95 | Interrupts: locally disabled. | |
96 | This call must not sleep | |
97 | ||
98 | get_mctrl(port) | |
99 | Returns the current state of modem control inputs. The state | |
100 | of the outputs should not be returned, since the core keeps | |
101 | track of their state. The state information should include: | |
343fe407 | 102 | - TIOCM_CAR state of DCD signal |
1da177e4 LT |
103 | - TIOCM_CTS state of CTS signal |
104 | - TIOCM_DSR state of DSR signal | |
105 | - TIOCM_RI state of RI signal | |
106 | The bit is set if the signal is currently driven active. If | |
107 | the port does not support CTS, DCD or DSR, the driver should | |
108 | indicate that the signal is permanently active. If RI is | |
109 | not available, the signal should not be indicated as active. | |
110 | ||
c5f4644e RK |
111 | Locking: port->lock taken. |
112 | Interrupts: locally disabled. | |
1da177e4 LT |
113 | This call must not sleep |
114 | ||
b129a8cc | 115 | stop_tx(port) |
1da177e4 LT |
116 | Stop transmitting characters. This might be due to the CTS |
117 | line becoming inactive or the tty layer indicating we want | |
b129a8cc | 118 | to stop transmission due to an XOFF character. |
1da177e4 | 119 | |
6a8f8d72 RK |
120 | The driver should stop transmitting characters as soon as |
121 | possible. | |
122 | ||
1da177e4 LT |
123 | Locking: port->lock taken. |
124 | Interrupts: locally disabled. | |
125 | This call must not sleep | |
126 | ||
b129a8cc | 127 | start_tx(port) |
6a8f8d72 | 128 | Start transmitting characters. |
1da177e4 LT |
129 | |
130 | Locking: port->lock taken. | |
131 | Interrupts: locally disabled. | |
132 | This call must not sleep | |
133 | ||
39c5144e GU |
134 | throttle(port) |
135 | Notify the serial driver that input buffers for the line discipline are | |
136 | close to full, and it should somehow signal that no more characters | |
137 | should be sent to the serial port. | |
a3bedc3b | 138 | This will be called only if hardware assisted flow control is enabled. |
39c5144e GU |
139 | |
140 | Locking: none. | |
141 | ||
e27585c7 GU |
142 | unthrottle(port) |
143 | Notify the serial driver that characters can now be sent to the serial | |
144 | port without fear of overrunning the input buffers of the line | |
145 | disciplines. | |
a3bedc3b | 146 | This will be called only if hardware assisted flow control is enabled. |
e27585c7 GU |
147 | |
148 | Locking: none. | |
149 | ||
e759d7c5 KC |
150 | send_xchar(port,ch) |
151 | Transmit a high priority character, even if the port is stopped. | |
152 | This is used to implement XON/XOFF flow control and tcflow(). If | |
153 | the serial driver does not implement this function, the tty core | |
154 | will append the character to the circular buffer and then call | |
155 | start_tx() / stop_tx() to flush the data out. | |
156 | ||
db106df3 PH |
157 | Do not transmit if ch == '\0' (__DISABLED_CHAR). |
158 | ||
e759d7c5 KC |
159 | Locking: none. |
160 | Interrupts: caller dependent. | |
161 | ||
1da177e4 LT |
162 | stop_rx(port) |
163 | Stop receiving characters; the port is in the process of | |
164 | being closed. | |
165 | ||
166 | Locking: port->lock taken. | |
167 | Interrupts: locally disabled. | |
168 | This call must not sleep | |
169 | ||
170 | enable_ms(port) | |
171 | Enable the modem status interrupts. | |
172 | ||
67ab7f59 RK |
173 | This method may be called multiple times. Modem status |
174 | interrupts should be disabled when the shutdown method is | |
175 | called. | |
176 | ||
1da177e4 LT |
177 | Locking: port->lock taken. |
178 | Interrupts: locally disabled. | |
179 | This call must not sleep | |
180 | ||
181 | break_ctl(port,ctl) | |
182 | Control the transmission of a break signal. If ctl is | |
183 | nonzero, the break signal should be transmitted. The signal | |
184 | should be terminated when another call is made with a zero | |
185 | ctl. | |
186 | ||
fbe3128b | 187 | Locking: caller holds port->mutex |
1da177e4 LT |
188 | This call must not sleep |
189 | ||
190 | startup(port) | |
191 | Grab any interrupt resources and initialise any low level driver | |
192 | state. Enable the port for reception. It should not activate | |
193 | RTS nor DTR; this will be done via a separate call to set_mctrl. | |
194 | ||
67ab7f59 RK |
195 | This method will only be called when the port is initially opened. |
196 | ||
1da177e4 LT |
197 | Locking: port_sem taken. |
198 | Interrupts: globally disabled. | |
199 | ||
200 | shutdown(port) | |
201 | Disable the port, disable any break condition that may be in | |
202 | effect, and free any interrupt resources. It should not disable | |
203 | RTS nor DTR; this will have already been done via a separate | |
204 | call to set_mctrl. | |
205 | ||
67ab7f59 RK |
206 | Drivers must not access port->info once this call has completed. |
207 | ||
208 | This method will only be called when there are no more users of | |
209 | this port. | |
210 | ||
1da177e4 LT |
211 | Locking: port_sem taken. |
212 | Interrupts: caller dependent. | |
213 | ||
6bb0e3a5 HS |
214 | flush_buffer(port) |
215 | Flush any write buffers, reset any DMA state and stop any | |
216 | ongoing DMA transfers. | |
217 | ||
218 | This will be called whenever the port->info->xmit circular | |
219 | buffer is cleared. | |
220 | ||
221 | Locking: port->lock taken. | |
222 | Interrupts: locally disabled. | |
223 | This call must not sleep | |
224 | ||
1da177e4 LT |
225 | set_termios(port,termios,oldtermios) |
226 | Change the port parameters, including word length, parity, stop | |
227 | bits. Update read_status_mask and ignore_status_mask to indicate | |
228 | the types of events we are interested in receiving. Relevant | |
229 | termios->c_cflag bits are: | |
230 | CSIZE - word size | |
231 | CSTOPB - 2 stop bits | |
232 | PARENB - parity enable | |
233 | PARODD - odd parity (when PARENB is in force) | |
234 | CREAD - enable reception of characters (if not set, | |
235 | still receive characters from the port, but | |
236 | throw them away. | |
237 | CRTSCTS - if set, enable CTS status change reporting | |
238 | CLOCAL - if not set, enable modem status change | |
239 | reporting. | |
240 | Relevant termios->c_iflag bits are: | |
241 | INPCK - enable frame and parity error events to be | |
242 | passed to the TTY layer. | |
243 | BRKINT | |
244 | PARMRK - both of these enable break events to be | |
245 | passed to the TTY layer. | |
246 | ||
247 | IGNPAR - ignore parity and framing errors | |
248 | IGNBRK - ignore break errors, If IGNPAR is also | |
249 | set, ignore overrun errors as well. | |
250 | The interaction of the iflag bits is as follows (parity error | |
251 | given as an example): | |
252 | Parity error INPCK IGNPAR | |
89f3da3e | 253 | n/a 0 n/a character received, marked as |
1da177e4 | 254 | TTY_NORMAL |
89f3da3e PK |
255 | None 1 n/a character received, marked as |
256 | TTY_NORMAL | |
257 | Yes 1 0 character received, marked as | |
1da177e4 | 258 | TTY_PARITY |
89f3da3e | 259 | Yes 1 1 character discarded |
1da177e4 LT |
260 | |
261 | Other flags may be used (eg, xon/xoff characters) if your | |
262 | hardware supports hardware "soft" flow control. | |
263 | ||
7c8ab967 | 264 | Locking: caller holds port->mutex |
1da177e4 LT |
265 | Interrupts: caller dependent. |
266 | This call must not sleep | |
267 | ||
0adc301e GU |
268 | set_ldisc(port,termios) |
269 | Notifier for discipline change. See Documentation/serial/tty.txt. | |
270 | ||
271 | Locking: caller holds port->mutex | |
272 | ||
1da177e4 LT |
273 | pm(port,state,oldstate) |
274 | Perform any power management related activities on the specified | |
6f538fe3 LW |
275 | port. State indicates the new state (defined by |
276 | enum uart_pm_state), oldstate indicates the previous state. | |
1da177e4 LT |
277 | |
278 | This function should not be used to grab any resources. | |
279 | ||
280 | This will be called when the port is initially opened and finally | |
281 | closed, except when the port is also the system console. This | |
282 | will occur even if CONFIG_PM is not set. | |
283 | ||
284 | Locking: none. | |
285 | Interrupts: caller dependent. | |
286 | ||
287 | type(port) | |
288 | Return a pointer to a string constant describing the specified | |
289 | port, or return NULL, in which case the string 'unknown' is | |
290 | substituted. | |
291 | ||
292 | Locking: none. | |
293 | Interrupts: caller dependent. | |
294 | ||
295 | release_port(port) | |
296 | Release any memory and IO region resources currently in use by | |
297 | the port. | |
298 | ||
299 | Locking: none. | |
300 | Interrupts: caller dependent. | |
301 | ||
302 | request_port(port) | |
303 | Request any memory and IO region resources required by the port. | |
304 | If any fail, no resources should be registered when this function | |
305 | returns, and it should return -EBUSY on failure. | |
306 | ||
307 | Locking: none. | |
308 | Interrupts: caller dependent. | |
309 | ||
310 | config_port(port,type) | |
311 | Perform any autoconfiguration steps required for the port. `type` | |
312 | contains a bit mask of the required configuration. UART_CONFIG_TYPE | |
313 | indicates that the port requires detection and identification. | |
314 | port->type should be set to the type found, or PORT_UNKNOWN if | |
315 | no port was detected. | |
316 | ||
317 | UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal, | |
318 | which should be probed using standard kernel autoprobing techniques. | |
319 | This is not necessary on platforms where ports have interrupts | |
320 | internally hard wired (eg, system on a chip implementations). | |
321 | ||
322 | Locking: none. | |
323 | Interrupts: caller dependent. | |
324 | ||
325 | verify_port(port,serinfo) | |
326 | Verify the new serial port information contained within serinfo is | |
327 | suitable for this port type. | |
328 | ||
329 | Locking: none. | |
330 | Interrupts: caller dependent. | |
331 | ||
332 | ioctl(port,cmd,arg) | |
333 | Perform any port specific IOCTLs. IOCTL commands must be defined | |
334 | using the standard numbering system found in <asm/ioctl.h> | |
335 | ||
336 | Locking: none. | |
337 | Interrupts: caller dependent. | |
338 | ||
e759d7c5 KC |
339 | poll_init(port) |
340 | Called by kgdb to perform the minimal hardware initialization needed | |
341 | to support poll_put_char() and poll_get_char(). Unlike ->startup() | |
342 | this should not request interrupts. | |
343 | ||
344 | Locking: tty_mutex and tty_port->mutex taken. | |
345 | Interrupts: n/a. | |
346 | ||
347 | poll_put_char(port,ch) | |
348 | Called by kgdb to write a single character directly to the serial | |
349 | port. It can and should block until there is space in the TX FIFO. | |
350 | ||
351 | Locking: none. | |
352 | Interrupts: caller dependent. | |
353 | This call must not sleep | |
354 | ||
355 | poll_get_char(port) | |
356 | Called by kgdb to read a single character directly from the serial | |
357 | port. If data is available, it should be returned; otherwise | |
358 | the function should return NO_POLL_CHAR immediately. | |
359 | ||
360 | Locking: none. | |
361 | Interrupts: caller dependent. | |
362 | This call must not sleep | |
363 | ||
1da177e4 LT |
364 | Other functions |
365 | --------------- | |
366 | ||
6a8f8d72 | 367 | uart_update_timeout(port,cflag,baud) |
1da177e4 | 368 | Update the FIFO drain timeout, port->timeout, according to the |
6a8f8d72 | 369 | number of bits, parity, stop bits and baud rate. |
1da177e4 LT |
370 | |
371 | Locking: caller is expected to take port->lock | |
372 | Interrupts: n/a | |
373 | ||
6a8f8d72 | 374 | uart_get_baud_rate(port,termios,old,min,max) |
1da177e4 LT |
375 | Return the numeric baud rate for the specified termios, taking |
376 | account of the special 38400 baud "kludge". The B0 baud rate | |
377 | is mapped to 9600 baud. | |
378 | ||
6a8f8d72 RK |
379 | If the baud rate is not within min..max, then if old is non-NULL, |
380 | the original baud rate will be tried. If that exceeds the | |
381 | min..max constraint, 9600 baud will be returned. termios will | |
382 | be updated to the baud rate in use. | |
383 | ||
384 | Note: min..max must always allow 9600 baud to be selected. | |
385 | ||
1da177e4 LT |
386 | Locking: caller dependent. |
387 | Interrupts: n/a | |
388 | ||
6a8f8d72 | 389 | uart_get_divisor(port,baud) |
93a2f632 | 390 | Return the divisor (baud_base / baud) for the specified baud |
6a8f8d72 | 391 | rate, appropriately rounded. |
1da177e4 LT |
392 | |
393 | If 38400 baud and custom divisor is selected, return the | |
394 | custom divisor instead. | |
395 | ||
396 | Locking: caller dependent. | |
397 | Interrupts: n/a | |
398 | ||
6a8f8d72 RK |
399 | uart_match_port(port1,port2) |
400 | This utility function can be used to determine whether two | |
401 | uart_port structures describe the same port. | |
402 | ||
403 | Locking: n/a | |
404 | Interrupts: n/a | |
405 | ||
406 | uart_write_wakeup(port) | |
407 | A driver is expected to call this function when the number of | |
408 | characters in the transmit buffer have dropped below a threshold. | |
409 | ||
410 | Locking: port->lock should be held. | |
411 | Interrupts: n/a | |
412 | ||
413 | uart_register_driver(drv) | |
414 | Register a uart driver with the core driver. We in turn register | |
415 | with the tty layer, and initialise the core driver per-port state. | |
416 | ||
417 | drv->port should be NULL, and the per-port structures should be | |
418 | registered using uart_add_one_port after this call has succeeded. | |
419 | ||
420 | Locking: none | |
421 | Interrupts: enabled | |
422 | ||
423 | uart_unregister_driver() | |
424 | Remove all references to a driver from the core driver. The low | |
425 | level driver must have removed all its ports via the | |
426 | uart_remove_one_port() if it registered them with uart_add_one_port(). | |
427 | ||
428 | Locking: none | |
429 | Interrupts: enabled | |
430 | ||
431 | uart_suspend_port() | |
432 | ||
433 | uart_resume_port() | |
434 | ||
435 | uart_add_one_port() | |
436 | ||
437 | uart_remove_one_port() | |
438 | ||
1da177e4 LT |
439 | Other notes |
440 | ----------- | |
441 | ||
442 | It is intended some day to drop the 'unused' entries from uart_port, and | |
443 | allow low level drivers to register their own individual uart_port's with | |
444 | the core. This will allow drivers to use uart_port as a pointer to a | |
445 | structure containing both the uart_port entry with their own extensions, | |
446 | thus: | |
447 | ||
448 | struct my_port { | |
449 | struct uart_port port; | |
450 | int my_stuff; | |
451 | }; | |
84130aac RG |
452 | |
453 | Modem control lines via GPIO | |
454 | ---------------------------- | |
455 | ||
456 | Some helpers are provided in order to set/get modem control lines via GPIO. | |
457 | ||
ce59e48f | 458 | mctrl_gpio_init(port, idx): |
84130aac RG |
459 | This will get the {cts,rts,...}-gpios from device tree if they are |
460 | present and request them, set direction etc, and return an | |
461 | allocated structure. devm_* functions are used, so there's no need | |
462 | to call mctrl_gpio_free(). | |
ce59e48f UKK |
463 | As this sets up the irq handling make sure to not handle changes to the |
464 | gpio input lines in your driver, too. | |
84130aac RG |
465 | |
466 | mctrl_gpio_free(dev, gpios): | |
467 | This will free the requested gpios in mctrl_gpio_init(). | |
d886e7ce | 468 | As devm_* functions are used, there's generally no need to call |
84130aac RG |
469 | this function. |
470 | ||
471 | mctrl_gpio_to_gpiod(gpios, gidx) | |
4817ebb1 GU |
472 | This returns the gpio_desc structure associated to the modem line |
473 | index. | |
84130aac RG |
474 | |
475 | mctrl_gpio_set(gpios, mctrl): | |
476 | This will sets the gpios according to the mctrl state. | |
477 | ||
478 | mctrl_gpio_get(gpios, mctrl): | |
479 | This will update mctrl with the gpios values. | |
ce59e48f UKK |
480 | |
481 | mctrl_gpio_enable_ms(gpios): | |
482 | Enables irqs and handling of changes to the ms lines. | |
483 | ||
484 | mctrl_gpio_disable_ms(gpios): | |
485 | Disables irqs and handling of changes to the ms lines. |