blob: 03369d076ca20a9872d6e1fa18b0729e712aa166 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2 Low Level Serial API
3 --------------------
4
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006This document is meant as a brief overview of some aspects of the new serial
7driver. It is not complete, any questions you have should be directed to
8<rmk@arm.linux.org.uk>
9
Russell King67ab7f52006-04-15 20:46:11 +010010The reference implementation is contained within amba_pl011.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12
13
14Low Level Serial Hardware Driver
15--------------------------------
16
17The low level serial hardware driver is responsible for supplying port
18information (defined by uart_port) and a set of control methods (defined
19by uart_ops) to the core serial driver. The low level driver is also
20responsible for handling interrupts for the port, and providing any
21console support.
22
23
24Console Support
25---------------
26
27The serial core provides a few helper functions. This includes identifing
28the correct port structure (via uart_get_console) and decoding command line
29arguments (uart_parse_options).
30
Geert Uytterhoevend124fd32016-04-14 11:08:08 +020031There is also a helper function (uart_console_write) which performs a
32character by character write, translating newlines to CRLF sequences.
33Driver writers are recommended to use this function rather than implementing
34their own version.
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37Locking
38-------
39
40It is the responsibility of the low level hardware driver to perform the
41necessary locking using port->lock. There are some exceptions (which
42are described in the uart_ops listing below.)
43
Geert Uytterhoeven4895b1d2016-03-14 16:16:10 +010044There are two locks. A per-port spinlock, and an overall semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46From the core driver perspective, the port->lock locks the following
47data:
48
49 port->mctrl
50 port->icount
51 info->xmit.head (circ->head)
52 info->xmit.tail (circ->tail)
53
54The low level driver is free to use this lock to provide any additional
55locking.
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057The port_sem semaphore is used to protect against ports being added/
Peter Hurley7c8ab962014-10-16 16:54:20 -040058removed or reconfigured at inappropriate times. Since v2.6.27, this
59semaphore has been the 'mutex' member of the tty_port struct, and
60commonly referred to as the port mutex (or port->mutex).
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62
63uart_ops
64--------
65
66The uart_ops structure is the main interface between serial_core and the
67hardware specific driver. It contains all the methods to control the
68hardware.
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.
Russell King67ab7f52006-04-15 20:46:11 +010089 - TIOCM_LOOP Set the port into loopback mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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:
Uwe Kleine-König343fe402012-01-04 15:27:32 -0800102 - TIOCM_CAR state of DCD signal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 - 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
Russell Kingc5f46442005-06-29 09:42:38 +0100111 Locking: port->lock taken.
112 Interrupts: locally disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 This call must not sleep
114
Russell Kingb129a8c2005-08-31 10:12:14 +0100115 stop_tx(port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 Stop transmitting characters. This might be due to the CTS
117 line becoming inactive or the tty layer indicating we want
Russell Kingb129a8c2005-08-31 10:12:14 +0100118 to stop transmission due to an XOFF character.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Russell King6a8f8d72005-10-31 11:53:19 +0000120 The driver should stop transmitting characters as soon as
121 possible.
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 Locking: port->lock taken.
124 Interrupts: locally disabled.
125 This call must not sleep
126
Russell Kingb129a8c2005-08-31 10:12:14 +0100127 start_tx(port)
Russell King6a8f8d72005-10-31 11:53:19 +0000128 Start transmitting characters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 Locking: port->lock taken.
131 Interrupts: locally disabled.
132 This call must not sleep
133
Geert Uytterhoeven39c51442016-03-14 16:16:11 +0100134 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.
138
139 Locking: none.
140
Geert Uytterhoevene27585c2016-03-14 16:16:12 +0100141 unthrottle(port)
142 Notify the serial driver that characters can now be sent to the serial
143 port without fear of overrunning the input buffers of the line
144 disciplines.
145
146 Locking: none.
147
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800148 send_xchar(port,ch)
149 Transmit a high priority character, even if the port is stopped.
150 This is used to implement XON/XOFF flow control and tcflow(). If
151 the serial driver does not implement this function, the tty core
152 will append the character to the circular buffer and then call
153 start_tx() / stop_tx() to flush the data out.
154
Peter Hurleydb106df2014-09-02 17:39:15 -0400155 Do not transmit if ch == '\0' (__DISABLED_CHAR).
156
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800157 Locking: none.
158 Interrupts: caller dependent.
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 stop_rx(port)
161 Stop receiving characters; the port is in the process of
162 being closed.
163
164 Locking: port->lock taken.
165 Interrupts: locally disabled.
166 This call must not sleep
167
168 enable_ms(port)
169 Enable the modem status interrupts.
170
Russell King67ab7f52006-04-15 20:46:11 +0100171 This method may be called multiple times. Modem status
172 interrupts should be disabled when the shutdown method is
173 called.
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 Locking: port->lock taken.
176 Interrupts: locally disabled.
177 This call must not sleep
178
179 break_ctl(port,ctl)
180 Control the transmission of a break signal. If ctl is
181 nonzero, the break signal should be transmitted. The signal
182 should be terminated when another call is made with a zero
183 ctl.
184
Geert Uytterhoevenfbe31282016-03-14 16:16:14 +0100185 Locking: caller holds port->mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 This call must not sleep
187
188 startup(port)
189 Grab any interrupt resources and initialise any low level driver
190 state. Enable the port for reception. It should not activate
191 RTS nor DTR; this will be done via a separate call to set_mctrl.
192
Russell King67ab7f52006-04-15 20:46:11 +0100193 This method will only be called when the port is initially opened.
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 Locking: port_sem taken.
196 Interrupts: globally disabled.
197
198 shutdown(port)
199 Disable the port, disable any break condition that may be in
200 effect, and free any interrupt resources. It should not disable
201 RTS nor DTR; this will have already been done via a separate
202 call to set_mctrl.
203
Russell King67ab7f52006-04-15 20:46:11 +0100204 Drivers must not access port->info once this call has completed.
205
206 This method will only be called when there are no more users of
207 this port.
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 Locking: port_sem taken.
210 Interrupts: caller dependent.
211
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100212 flush_buffer(port)
213 Flush any write buffers, reset any DMA state and stop any
214 ongoing DMA transfers.
215
216 This will be called whenever the port->info->xmit circular
217 buffer is cleared.
218
219 Locking: port->lock taken.
220 Interrupts: locally disabled.
221 This call must not sleep
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 set_termios(port,termios,oldtermios)
224 Change the port parameters, including word length, parity, stop
225 bits. Update read_status_mask and ignore_status_mask to indicate
226 the types of events we are interested in receiving. Relevant
227 termios->c_cflag bits are:
228 CSIZE - word size
229 CSTOPB - 2 stop bits
230 PARENB - parity enable
231 PARODD - odd parity (when PARENB is in force)
232 CREAD - enable reception of characters (if not set,
233 still receive characters from the port, but
234 throw them away.
235 CRTSCTS - if set, enable CTS status change reporting
236 CLOCAL - if not set, enable modem status change
237 reporting.
238 Relevant termios->c_iflag bits are:
239 INPCK - enable frame and parity error events to be
240 passed to the TTY layer.
241 BRKINT
242 PARMRK - both of these enable break events to be
243 passed to the TTY layer.
244
245 IGNPAR - ignore parity and framing errors
246 IGNBRK - ignore break errors, If IGNPAR is also
247 set, ignore overrun errors as well.
248 The interaction of the iflag bits is as follows (parity error
249 given as an example):
250 Parity error INPCK IGNPAR
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100251 n/a 0 n/a character received, marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 TTY_NORMAL
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100253 None 1 n/a character received, marked as
254 TTY_NORMAL
255 Yes 1 0 character received, marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 TTY_PARITY
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100257 Yes 1 1 character discarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 Other flags may be used (eg, xon/xoff characters) if your
260 hardware supports hardware "soft" flow control.
261
Peter Hurley7c8ab962014-10-16 16:54:20 -0400262 Locking: caller holds port->mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 Interrupts: caller dependent.
264 This call must not sleep
265
Geert Uytterhoeven0adc3012016-03-14 16:16:13 +0100266 set_ldisc(port,termios)
267 Notifier for discipline change. See Documentation/serial/tty.txt.
268
269 Locking: caller holds port->mutex
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 pm(port,state,oldstate)
272 Perform any power management related activities on the specified
Linus Walleij6f538fe2012-12-07 11:36:08 +0100273 port. State indicates the new state (defined by
274 enum uart_pm_state), oldstate indicates the previous state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 This function should not be used to grab any resources.
277
278 This will be called when the port is initially opened and finally
279 closed, except when the port is also the system console. This
280 will occur even if CONFIG_PM is not set.
281
282 Locking: none.
283 Interrupts: caller dependent.
284
285 type(port)
286 Return a pointer to a string constant describing the specified
287 port, or return NULL, in which case the string 'unknown' is
288 substituted.
289
290 Locking: none.
291 Interrupts: caller dependent.
292
293 release_port(port)
294 Release any memory and IO region resources currently in use by
295 the port.
296
297 Locking: none.
298 Interrupts: caller dependent.
299
300 request_port(port)
301 Request any memory and IO region resources required by the port.
302 If any fail, no resources should be registered when this function
303 returns, and it should return -EBUSY on failure.
304
305 Locking: none.
306 Interrupts: caller dependent.
307
308 config_port(port,type)
309 Perform any autoconfiguration steps required for the port. `type`
310 contains a bit mask of the required configuration. UART_CONFIG_TYPE
311 indicates that the port requires detection and identification.
312 port->type should be set to the type found, or PORT_UNKNOWN if
313 no port was detected.
314
315 UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
316 which should be probed using standard kernel autoprobing techniques.
317 This is not necessary on platforms where ports have interrupts
318 internally hard wired (eg, system on a chip implementations).
319
320 Locking: none.
321 Interrupts: caller dependent.
322
323 verify_port(port,serinfo)
324 Verify the new serial port information contained within serinfo is
325 suitable for this port type.
326
327 Locking: none.
328 Interrupts: caller dependent.
329
330 ioctl(port,cmd,arg)
331 Perform any port specific IOCTLs. IOCTL commands must be defined
332 using the standard numbering system found in <asm/ioctl.h>
333
334 Locking: none.
335 Interrupts: caller dependent.
336
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800337 poll_init(port)
338 Called by kgdb to perform the minimal hardware initialization needed
339 to support poll_put_char() and poll_get_char(). Unlike ->startup()
340 this should not request interrupts.
341
342 Locking: tty_mutex and tty_port->mutex taken.
343 Interrupts: n/a.
344
345 poll_put_char(port,ch)
346 Called by kgdb to write a single character directly to the serial
347 port. It can and should block until there is space in the TX FIFO.
348
349 Locking: none.
350 Interrupts: caller dependent.
351 This call must not sleep
352
353 poll_get_char(port)
354 Called by kgdb to read a single character directly from the serial
355 port. If data is available, it should be returned; otherwise
356 the function should return NO_POLL_CHAR immediately.
357
358 Locking: none.
359 Interrupts: caller dependent.
360 This call must not sleep
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362Other functions
363---------------
364
Russell King6a8f8d72005-10-31 11:53:19 +0000365uart_update_timeout(port,cflag,baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 Update the FIFO drain timeout, port->timeout, according to the
Russell King6a8f8d72005-10-31 11:53:19 +0000367 number of bits, parity, stop bits and baud rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 Locking: caller is expected to take port->lock
370 Interrupts: n/a
371
Russell King6a8f8d72005-10-31 11:53:19 +0000372uart_get_baud_rate(port,termios,old,min,max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 Return the numeric baud rate for the specified termios, taking
374 account of the special 38400 baud "kludge". The B0 baud rate
375 is mapped to 9600 baud.
376
Russell King6a8f8d72005-10-31 11:53:19 +0000377 If the baud rate is not within min..max, then if old is non-NULL,
378 the original baud rate will be tried. If that exceeds the
379 min..max constraint, 9600 baud will be returned. termios will
380 be updated to the baud rate in use.
381
382 Note: min..max must always allow 9600 baud to be selected.
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 Locking: caller dependent.
385 Interrupts: n/a
386
Russell King6a8f8d72005-10-31 11:53:19 +0000387uart_get_divisor(port,baud)
Geert Uytterhoeven93a2f632016-03-14 16:16:15 +0100388 Return the divisor (baud_base / baud) for the specified baud
Russell King6a8f8d72005-10-31 11:53:19 +0000389 rate, appropriately rounded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 If 38400 baud and custom divisor is selected, return the
392 custom divisor instead.
393
394 Locking: caller dependent.
395 Interrupts: n/a
396
Russell King6a8f8d72005-10-31 11:53:19 +0000397uart_match_port(port1,port2)
398 This utility function can be used to determine whether two
399 uart_port structures describe the same port.
400
401 Locking: n/a
402 Interrupts: n/a
403
404uart_write_wakeup(port)
405 A driver is expected to call this function when the number of
406 characters in the transmit buffer have dropped below a threshold.
407
408 Locking: port->lock should be held.
409 Interrupts: n/a
410
411uart_register_driver(drv)
412 Register a uart driver with the core driver. We in turn register
413 with the tty layer, and initialise the core driver per-port state.
414
415 drv->port should be NULL, and the per-port structures should be
416 registered using uart_add_one_port after this call has succeeded.
417
418 Locking: none
419 Interrupts: enabled
420
421uart_unregister_driver()
422 Remove all references to a driver from the core driver. The low
423 level driver must have removed all its ports via the
424 uart_remove_one_port() if it registered them with uart_add_one_port().
425
426 Locking: none
427 Interrupts: enabled
428
429uart_suspend_port()
430
431uart_resume_port()
432
433uart_add_one_port()
434
435uart_remove_one_port()
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437Other notes
438-----------
439
440It is intended some day to drop the 'unused' entries from uart_port, and
441allow low level drivers to register their own individual uart_port's with
442the core. This will allow drivers to use uart_port as a pointer to a
443structure containing both the uart_port entry with their own extensions,
444thus:
445
446 struct my_port {
447 struct uart_port port;
448 int my_stuff;
449 };
Richard Genoud84130aa2014-05-13 20:20:43 +0200450
451Modem control lines via GPIO
452----------------------------
453
454Some helpers are provided in order to set/get modem control lines via GPIO.
455
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200456mctrl_gpio_init(port, idx):
Richard Genoud84130aa2014-05-13 20:20:43 +0200457 This will get the {cts,rts,...}-gpios from device tree if they are
458 present and request them, set direction etc, and return an
459 allocated structure. devm_* functions are used, so there's no need
460 to call mctrl_gpio_free().
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200461 As this sets up the irq handling make sure to not handle changes to the
462 gpio input lines in your driver, too.
Richard Genoud84130aa2014-05-13 20:20:43 +0200463
464mctrl_gpio_free(dev, gpios):
465 This will free the requested gpios in mctrl_gpio_init().
Geert Uytterhoevend886e7c2016-03-14 16:16:16 +0100466 As devm_* functions are used, there's generally no need to call
Richard Genoud84130aa2014-05-13 20:20:43 +0200467 this function.
468
469mctrl_gpio_to_gpiod(gpios, gidx)
Geert Uytterhoeven4817ebb12016-03-14 16:16:17 +0100470 This returns the gpio_desc structure associated to the modem line
471 index.
Richard Genoud84130aa2014-05-13 20:20:43 +0200472
473mctrl_gpio_set(gpios, mctrl):
474 This will sets the gpios according to the mctrl state.
475
476mctrl_gpio_get(gpios, mctrl):
477 This will update mctrl with the gpios values.
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200478
479mctrl_gpio_enable_ms(gpios):
480 Enables irqs and handling of changes to the ms lines.
481
482mctrl_gpio_disable_ms(gpios):
483 Disables irqs and handling of changes to the ms lines.