blob: ba84d1f38ca1d1e64bb3f15182bdf3a93f62085f [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
31
32Locking
33-------
34
35It is the responsibility of the low level hardware driver to perform the
36necessary locking using port->lock. There are some exceptions (which
37are described in the uart_ops listing below.)
38
Geert Uytterhoeven4895b1d2016-03-14 16:16:10 +010039There are two locks. A per-port spinlock, and an overall semaphore.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41From the core driver perspective, the port->lock locks the following
42data:
43
44 port->mctrl
45 port->icount
46 info->xmit.head (circ->head)
47 info->xmit.tail (circ->tail)
48
49The low level driver is free to use this lock to provide any additional
50locking.
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052The port_sem semaphore is used to protect against ports being added/
Peter Hurley7c8ab962014-10-16 16:54:20 -040053removed or reconfigured at inappropriate times. Since v2.6.27, this
54semaphore has been the 'mutex' member of the tty_port struct, and
55commonly referred to as the port mutex (or port->mutex).
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57
58uart_ops
59--------
60
61The uart_ops structure is the main interface between serial_core and the
62hardware specific driver. It contains all the methods to control the
63hardware.
64
65 tx_empty(port)
66 This function tests whether the transmitter fifo and shifter
67 for the port described by 'port' is empty. If it is empty,
68 this function should return TIOCSER_TEMT, otherwise return 0.
69 If the port does not support this operation, then it should
70 return TIOCSER_TEMT.
71
72 Locking: none.
73 Interrupts: caller dependent.
74 This call must not sleep
75
76 set_mctrl(port, mctrl)
77 This function sets the modem control lines for port described
78 by 'port' to the state described by mctrl. The relevant bits
79 of mctrl are:
80 - TIOCM_RTS RTS signal.
81 - TIOCM_DTR DTR signal.
82 - TIOCM_OUT1 OUT1 signal.
83 - TIOCM_OUT2 OUT2 signal.
Russell King67ab7f52006-04-15 20:46:11 +010084 - TIOCM_LOOP Set the port into loopback mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 If the appropriate bit is set, the signal should be driven
86 active. If the bit is clear, the signal should be driven
87 inactive.
88
89 Locking: port->lock taken.
90 Interrupts: locally disabled.
91 This call must not sleep
92
93 get_mctrl(port)
94 Returns the current state of modem control inputs. The state
95 of the outputs should not be returned, since the core keeps
96 track of their state. The state information should include:
Uwe Kleine-König343fe402012-01-04 15:27:32 -080097 - TIOCM_CAR state of DCD signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 - TIOCM_CTS state of CTS signal
99 - TIOCM_DSR state of DSR signal
100 - TIOCM_RI state of RI signal
101 The bit is set if the signal is currently driven active. If
102 the port does not support CTS, DCD or DSR, the driver should
103 indicate that the signal is permanently active. If RI is
104 not available, the signal should not be indicated as active.
105
Russell Kingc5f46442005-06-29 09:42:38 +0100106 Locking: port->lock taken.
107 Interrupts: locally disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 This call must not sleep
109
Russell Kingb129a8c2005-08-31 10:12:14 +0100110 stop_tx(port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 Stop transmitting characters. This might be due to the CTS
112 line becoming inactive or the tty layer indicating we want
Russell Kingb129a8c2005-08-31 10:12:14 +0100113 to stop transmission due to an XOFF character.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Russell King6a8f8d72005-10-31 11:53:19 +0000115 The driver should stop transmitting characters as soon as
116 possible.
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 Locking: port->lock taken.
119 Interrupts: locally disabled.
120 This call must not sleep
121
Russell Kingb129a8c2005-08-31 10:12:14 +0100122 start_tx(port)
Russell King6a8f8d72005-10-31 11:53:19 +0000123 Start transmitting characters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 Locking: port->lock taken.
126 Interrupts: locally disabled.
127 This call must not sleep
128
Geert Uytterhoeven39c51442016-03-14 16:16:11 +0100129 throttle(port)
130 Notify the serial driver that input buffers for the line discipline are
131 close to full, and it should somehow signal that no more characters
132 should be sent to the serial port.
133
134 Locking: none.
135
Geert Uytterhoevene27585c2016-03-14 16:16:12 +0100136 unthrottle(port)
137 Notify the serial driver that characters can now be sent to the serial
138 port without fear of overrunning the input buffers of the line
139 disciplines.
140
141 Locking: none.
142
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800143 send_xchar(port,ch)
144 Transmit a high priority character, even if the port is stopped.
145 This is used to implement XON/XOFF flow control and tcflow(). If
146 the serial driver does not implement this function, the tty core
147 will append the character to the circular buffer and then call
148 start_tx() / stop_tx() to flush the data out.
149
Peter Hurleydb106df2014-09-02 17:39:15 -0400150 Do not transmit if ch == '\0' (__DISABLED_CHAR).
151
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800152 Locking: none.
153 Interrupts: caller dependent.
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 stop_rx(port)
156 Stop receiving characters; the port is in the process of
157 being closed.
158
159 Locking: port->lock taken.
160 Interrupts: locally disabled.
161 This call must not sleep
162
163 enable_ms(port)
164 Enable the modem status interrupts.
165
Russell King67ab7f52006-04-15 20:46:11 +0100166 This method may be called multiple times. Modem status
167 interrupts should be disabled when the shutdown method is
168 called.
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 Locking: port->lock taken.
171 Interrupts: locally disabled.
172 This call must not sleep
173
174 break_ctl(port,ctl)
175 Control the transmission of a break signal. If ctl is
176 nonzero, the break signal should be transmitted. The signal
177 should be terminated when another call is made with a zero
178 ctl.
179
Geert Uytterhoevenfbe31282016-03-14 16:16:14 +0100180 Locking: caller holds port->mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 This call must not sleep
182
183 startup(port)
184 Grab any interrupt resources and initialise any low level driver
185 state. Enable the port for reception. It should not activate
186 RTS nor DTR; this will be done via a separate call to set_mctrl.
187
Russell King67ab7f52006-04-15 20:46:11 +0100188 This method will only be called when the port is initially opened.
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 Locking: port_sem taken.
191 Interrupts: globally disabled.
192
193 shutdown(port)
194 Disable the port, disable any break condition that may be in
195 effect, and free any interrupt resources. It should not disable
196 RTS nor DTR; this will have already been done via a separate
197 call to set_mctrl.
198
Russell King67ab7f52006-04-15 20:46:11 +0100199 Drivers must not access port->info once this call has completed.
200
201 This method will only be called when there are no more users of
202 this port.
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 Locking: port_sem taken.
205 Interrupts: caller dependent.
206
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100207 flush_buffer(port)
208 Flush any write buffers, reset any DMA state and stop any
209 ongoing DMA transfers.
210
211 This will be called whenever the port->info->xmit circular
212 buffer is cleared.
213
214 Locking: port->lock taken.
215 Interrupts: locally disabled.
216 This call must not sleep
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 set_termios(port,termios,oldtermios)
219 Change the port parameters, including word length, parity, stop
220 bits. Update read_status_mask and ignore_status_mask to indicate
221 the types of events we are interested in receiving. Relevant
222 termios->c_cflag bits are:
223 CSIZE - word size
224 CSTOPB - 2 stop bits
225 PARENB - parity enable
226 PARODD - odd parity (when PARENB is in force)
227 CREAD - enable reception of characters (if not set,
228 still receive characters from the port, but
229 throw them away.
230 CRTSCTS - if set, enable CTS status change reporting
231 CLOCAL - if not set, enable modem status change
232 reporting.
233 Relevant termios->c_iflag bits are:
234 INPCK - enable frame and parity error events to be
235 passed to the TTY layer.
236 BRKINT
237 PARMRK - both of these enable break events to be
238 passed to the TTY layer.
239
240 IGNPAR - ignore parity and framing errors
241 IGNBRK - ignore break errors, If IGNPAR is also
242 set, ignore overrun errors as well.
243 The interaction of the iflag bits is as follows (parity error
244 given as an example):
245 Parity error INPCK IGNPAR
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100246 n/a 0 n/a character received, marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 TTY_NORMAL
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100248 None 1 n/a character received, marked as
249 TTY_NORMAL
250 Yes 1 0 character received, marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 TTY_PARITY
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100252 Yes 1 1 character discarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 Other flags may be used (eg, xon/xoff characters) if your
255 hardware supports hardware "soft" flow control.
256
Peter Hurley7c8ab962014-10-16 16:54:20 -0400257 Locking: caller holds port->mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 Interrupts: caller dependent.
259 This call must not sleep
260
Geert Uytterhoeven0adc3012016-03-14 16:16:13 +0100261 set_ldisc(port,termios)
262 Notifier for discipline change. See Documentation/serial/tty.txt.
263
264 Locking: caller holds port->mutex
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 pm(port,state,oldstate)
267 Perform any power management related activities on the specified
Linus Walleij6f538fe2012-12-07 11:36:08 +0100268 port. State indicates the new state (defined by
269 enum uart_pm_state), oldstate indicates the previous state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 This function should not be used to grab any resources.
272
273 This will be called when the port is initially opened and finally
274 closed, except when the port is also the system console. This
275 will occur even if CONFIG_PM is not set.
276
277 Locking: none.
278 Interrupts: caller dependent.
279
280 type(port)
281 Return a pointer to a string constant describing the specified
282 port, or return NULL, in which case the string 'unknown' is
283 substituted.
284
285 Locking: none.
286 Interrupts: caller dependent.
287
288 release_port(port)
289 Release any memory and IO region resources currently in use by
290 the port.
291
292 Locking: none.
293 Interrupts: caller dependent.
294
295 request_port(port)
296 Request any memory and IO region resources required by the port.
297 If any fail, no resources should be registered when this function
298 returns, and it should return -EBUSY on failure.
299
300 Locking: none.
301 Interrupts: caller dependent.
302
303 config_port(port,type)
304 Perform any autoconfiguration steps required for the port. `type`
305 contains a bit mask of the required configuration. UART_CONFIG_TYPE
306 indicates that the port requires detection and identification.
307 port->type should be set to the type found, or PORT_UNKNOWN if
308 no port was detected.
309
310 UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
311 which should be probed using standard kernel autoprobing techniques.
312 This is not necessary on platforms where ports have interrupts
313 internally hard wired (eg, system on a chip implementations).
314
315 Locking: none.
316 Interrupts: caller dependent.
317
318 verify_port(port,serinfo)
319 Verify the new serial port information contained within serinfo is
320 suitable for this port type.
321
322 Locking: none.
323 Interrupts: caller dependent.
324
325 ioctl(port,cmd,arg)
326 Perform any port specific IOCTLs. IOCTL commands must be defined
327 using the standard numbering system found in <asm/ioctl.h>
328
329 Locking: none.
330 Interrupts: caller dependent.
331
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800332 poll_init(port)
333 Called by kgdb to perform the minimal hardware initialization needed
334 to support poll_put_char() and poll_get_char(). Unlike ->startup()
335 this should not request interrupts.
336
337 Locking: tty_mutex and tty_port->mutex taken.
338 Interrupts: n/a.
339
340 poll_put_char(port,ch)
341 Called by kgdb to write a single character directly to the serial
342 port. It can and should block until there is space in the TX FIFO.
343
344 Locking: none.
345 Interrupts: caller dependent.
346 This call must not sleep
347
348 poll_get_char(port)
349 Called by kgdb to read a single character directly from the serial
350 port. If data is available, it should be returned; otherwise
351 the function should return NO_POLL_CHAR immediately.
352
353 Locking: none.
354 Interrupts: caller dependent.
355 This call must not sleep
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357Other functions
358---------------
359
Russell King6a8f8d72005-10-31 11:53:19 +0000360uart_update_timeout(port,cflag,baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 Update the FIFO drain timeout, port->timeout, according to the
Russell King6a8f8d72005-10-31 11:53:19 +0000362 number of bits, parity, stop bits and baud rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 Locking: caller is expected to take port->lock
365 Interrupts: n/a
366
Russell King6a8f8d72005-10-31 11:53:19 +0000367uart_get_baud_rate(port,termios,old,min,max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 Return the numeric baud rate for the specified termios, taking
369 account of the special 38400 baud "kludge". The B0 baud rate
370 is mapped to 9600 baud.
371
Russell King6a8f8d72005-10-31 11:53:19 +0000372 If the baud rate is not within min..max, then if old is non-NULL,
373 the original baud rate will be tried. If that exceeds the
374 min..max constraint, 9600 baud will be returned. termios will
375 be updated to the baud rate in use.
376
377 Note: min..max must always allow 9600 baud to be selected.
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 Locking: caller dependent.
380 Interrupts: n/a
381
Russell King6a8f8d72005-10-31 11:53:19 +0000382uart_get_divisor(port,baud)
Geert Uytterhoeven93a2f632016-03-14 16:16:15 +0100383 Return the divisor (baud_base / baud) for the specified baud
Russell King6a8f8d72005-10-31 11:53:19 +0000384 rate, appropriately rounded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 If 38400 baud and custom divisor is selected, return the
387 custom divisor instead.
388
389 Locking: caller dependent.
390 Interrupts: n/a
391
Russell King6a8f8d72005-10-31 11:53:19 +0000392uart_match_port(port1,port2)
393 This utility function can be used to determine whether two
394 uart_port structures describe the same port.
395
396 Locking: n/a
397 Interrupts: n/a
398
399uart_write_wakeup(port)
400 A driver is expected to call this function when the number of
401 characters in the transmit buffer have dropped below a threshold.
402
403 Locking: port->lock should be held.
404 Interrupts: n/a
405
406uart_register_driver(drv)
407 Register a uart driver with the core driver. We in turn register
408 with the tty layer, and initialise the core driver per-port state.
409
410 drv->port should be NULL, and the per-port structures should be
411 registered using uart_add_one_port after this call has succeeded.
412
413 Locking: none
414 Interrupts: enabled
415
416uart_unregister_driver()
417 Remove all references to a driver from the core driver. The low
418 level driver must have removed all its ports via the
419 uart_remove_one_port() if it registered them with uart_add_one_port().
420
421 Locking: none
422 Interrupts: enabled
423
424uart_suspend_port()
425
426uart_resume_port()
427
428uart_add_one_port()
429
430uart_remove_one_port()
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432Other notes
433-----------
434
435It is intended some day to drop the 'unused' entries from uart_port, and
436allow low level drivers to register their own individual uart_port's with
437the core. This will allow drivers to use uart_port as a pointer to a
438structure containing both the uart_port entry with their own extensions,
439thus:
440
441 struct my_port {
442 struct uart_port port;
443 int my_stuff;
444 };
Richard Genoud84130aa2014-05-13 20:20:43 +0200445
446Modem control lines via GPIO
447----------------------------
448
449Some helpers are provided in order to set/get modem control lines via GPIO.
450
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200451mctrl_gpio_init(port, idx):
Richard Genoud84130aa2014-05-13 20:20:43 +0200452 This will get the {cts,rts,...}-gpios from device tree if they are
453 present and request them, set direction etc, and return an
454 allocated structure. devm_* functions are used, so there's no need
455 to call mctrl_gpio_free().
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200456 As this sets up the irq handling make sure to not handle changes to the
457 gpio input lines in your driver, too.
Richard Genoud84130aa2014-05-13 20:20:43 +0200458
459mctrl_gpio_free(dev, gpios):
460 This will free the requested gpios in mctrl_gpio_init().
Geert Uytterhoevend886e7c2016-03-14 16:16:16 +0100461 As devm_* functions are used, there's generally no need to call
Richard Genoud84130aa2014-05-13 20:20:43 +0200462 this function.
463
464mctrl_gpio_to_gpiod(gpios, gidx)
465 This returns the gpio structure associated to the modem line index.
466
467mctrl_gpio_set(gpios, mctrl):
468 This will sets the gpios according to the mctrl state.
469
470mctrl_gpio_get(gpios, mctrl):
471 This will update mctrl with the gpios values.
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200472
473mctrl_gpio_enable_ms(gpios):
474 Enables irqs and handling of changes to the ms lines.
475
476mctrl_gpio_disable_ms(gpios):
477 Disables irqs and handling of changes to the ms lines.