blob: 3b08df5bcc17e9447187ac56f6699f5ff0033655 [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
180 Locking: none.
181 Interrupts: caller dependent.
182 This call must not sleep
183
184 startup(port)
185 Grab any interrupt resources and initialise any low level driver
186 state. Enable the port for reception. It should not activate
187 RTS nor DTR; this will be done via a separate call to set_mctrl.
188
Russell King67ab7f52006-04-15 20:46:11 +0100189 This method will only be called when the port is initially opened.
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 Locking: port_sem taken.
192 Interrupts: globally disabled.
193
194 shutdown(port)
195 Disable the port, disable any break condition that may be in
196 effect, and free any interrupt resources. It should not disable
197 RTS nor DTR; this will have already been done via a separate
198 call to set_mctrl.
199
Russell King67ab7f52006-04-15 20:46:11 +0100200 Drivers must not access port->info once this call has completed.
201
202 This method will only be called when there are no more users of
203 this port.
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 Locking: port_sem taken.
206 Interrupts: caller dependent.
207
Haavard Skinnemoen6bb0e3a2008-07-16 21:52:36 +0100208 flush_buffer(port)
209 Flush any write buffers, reset any DMA state and stop any
210 ongoing DMA transfers.
211
212 This will be called whenever the port->info->xmit circular
213 buffer is cleared.
214
215 Locking: port->lock taken.
216 Interrupts: locally disabled.
217 This call must not sleep
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 set_termios(port,termios,oldtermios)
220 Change the port parameters, including word length, parity, stop
221 bits. Update read_status_mask and ignore_status_mask to indicate
222 the types of events we are interested in receiving. Relevant
223 termios->c_cflag bits are:
224 CSIZE - word size
225 CSTOPB - 2 stop bits
226 PARENB - parity enable
227 PARODD - odd parity (when PARENB is in force)
228 CREAD - enable reception of characters (if not set,
229 still receive characters from the port, but
230 throw them away.
231 CRTSCTS - if set, enable CTS status change reporting
232 CLOCAL - if not set, enable modem status change
233 reporting.
234 Relevant termios->c_iflag bits are:
235 INPCK - enable frame and parity error events to be
236 passed to the TTY layer.
237 BRKINT
238 PARMRK - both of these enable break events to be
239 passed to the TTY layer.
240
241 IGNPAR - ignore parity and framing errors
242 IGNBRK - ignore break errors, If IGNPAR is also
243 set, ignore overrun errors as well.
244 The interaction of the iflag bits is as follows (parity error
245 given as an example):
246 Parity error INPCK IGNPAR
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100247 n/a 0 n/a character received, marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 TTY_NORMAL
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100249 None 1 n/a character received, marked as
250 TTY_NORMAL
251 Yes 1 0 character received, marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 TTY_PARITY
Peter Korsgaard89f3da32006-06-02 17:47:26 +0100253 Yes 1 1 character discarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 Other flags may be used (eg, xon/xoff characters) if your
256 hardware supports hardware "soft" flow control.
257
Peter Hurley7c8ab962014-10-16 16:54:20 -0400258 Locking: caller holds port->mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 Interrupts: caller dependent.
260 This call must not sleep
261
Geert Uytterhoeven0adc3012016-03-14 16:16:13 +0100262 set_ldisc(port,termios)
263 Notifier for discipline change. See Documentation/serial/tty.txt.
264
265 Locking: caller holds port->mutex
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 pm(port,state,oldstate)
268 Perform any power management related activities on the specified
Linus Walleij6f538fe2012-12-07 11:36:08 +0100269 port. State indicates the new state (defined by
270 enum uart_pm_state), oldstate indicates the previous state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 This function should not be used to grab any resources.
273
274 This will be called when the port is initially opened and finally
275 closed, except when the port is also the system console. This
276 will occur even if CONFIG_PM is not set.
277
278 Locking: none.
279 Interrupts: caller dependent.
280
281 type(port)
282 Return a pointer to a string constant describing the specified
283 port, or return NULL, in which case the string 'unknown' is
284 substituted.
285
286 Locking: none.
287 Interrupts: caller dependent.
288
289 release_port(port)
290 Release any memory and IO region resources currently in use by
291 the port.
292
293 Locking: none.
294 Interrupts: caller dependent.
295
296 request_port(port)
297 Request any memory and IO region resources required by the port.
298 If any fail, no resources should be registered when this function
299 returns, and it should return -EBUSY on failure.
300
301 Locking: none.
302 Interrupts: caller dependent.
303
304 config_port(port,type)
305 Perform any autoconfiguration steps required for the port. `type`
306 contains a bit mask of the required configuration. UART_CONFIG_TYPE
307 indicates that the port requires detection and identification.
308 port->type should be set to the type found, or PORT_UNKNOWN if
309 no port was detected.
310
311 UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
312 which should be probed using standard kernel autoprobing techniques.
313 This is not necessary on platforms where ports have interrupts
314 internally hard wired (eg, system on a chip implementations).
315
316 Locking: none.
317 Interrupts: caller dependent.
318
319 verify_port(port,serinfo)
320 Verify the new serial port information contained within serinfo is
321 suitable for this port type.
322
323 Locking: none.
324 Interrupts: caller dependent.
325
326 ioctl(port,cmd,arg)
327 Perform any port specific IOCTLs. IOCTL commands must be defined
328 using the standard numbering system found in <asm/ioctl.h>
329
330 Locking: none.
331 Interrupts: caller dependent.
332
Kevin Cernekeee759d7c2012-12-26 20:43:42 -0800333 poll_init(port)
334 Called by kgdb to perform the minimal hardware initialization needed
335 to support poll_put_char() and poll_get_char(). Unlike ->startup()
336 this should not request interrupts.
337
338 Locking: tty_mutex and tty_port->mutex taken.
339 Interrupts: n/a.
340
341 poll_put_char(port,ch)
342 Called by kgdb to write a single character directly to the serial
343 port. It can and should block until there is space in the TX FIFO.
344
345 Locking: none.
346 Interrupts: caller dependent.
347 This call must not sleep
348
349 poll_get_char(port)
350 Called by kgdb to read a single character directly from the serial
351 port. If data is available, it should be returned; otherwise
352 the function should return NO_POLL_CHAR immediately.
353
354 Locking: none.
355 Interrupts: caller dependent.
356 This call must not sleep
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358Other functions
359---------------
360
Russell King6a8f8d72005-10-31 11:53:19 +0000361uart_update_timeout(port,cflag,baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 Update the FIFO drain timeout, port->timeout, according to the
Russell King6a8f8d72005-10-31 11:53:19 +0000363 number of bits, parity, stop bits and baud rate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 Locking: caller is expected to take port->lock
366 Interrupts: n/a
367
Russell King6a8f8d72005-10-31 11:53:19 +0000368uart_get_baud_rate(port,termios,old,min,max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 Return the numeric baud rate for the specified termios, taking
370 account of the special 38400 baud "kludge". The B0 baud rate
371 is mapped to 9600 baud.
372
Russell King6a8f8d72005-10-31 11:53:19 +0000373 If the baud rate is not within min..max, then if old is non-NULL,
374 the original baud rate will be tried. If that exceeds the
375 min..max constraint, 9600 baud will be returned. termios will
376 be updated to the baud rate in use.
377
378 Note: min..max must always allow 9600 baud to be selected.
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 Locking: caller dependent.
381 Interrupts: n/a
382
Russell King6a8f8d72005-10-31 11:53:19 +0000383uart_get_divisor(port,baud)
384 Return the divsor (baud_base / baud) for the specified baud
385 rate, appropriately rounded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 If 38400 baud and custom divisor is selected, return the
388 custom divisor instead.
389
390 Locking: caller dependent.
391 Interrupts: n/a
392
Russell King6a8f8d72005-10-31 11:53:19 +0000393uart_match_port(port1,port2)
394 This utility function can be used to determine whether two
395 uart_port structures describe the same port.
396
397 Locking: n/a
398 Interrupts: n/a
399
400uart_write_wakeup(port)
401 A driver is expected to call this function when the number of
402 characters in the transmit buffer have dropped below a threshold.
403
404 Locking: port->lock should be held.
405 Interrupts: n/a
406
407uart_register_driver(drv)
408 Register a uart driver with the core driver. We in turn register
409 with the tty layer, and initialise the core driver per-port state.
410
411 drv->port should be NULL, and the per-port structures should be
412 registered using uart_add_one_port after this call has succeeded.
413
414 Locking: none
415 Interrupts: enabled
416
417uart_unregister_driver()
418 Remove all references to a driver from the core driver. The low
419 level driver must have removed all its ports via the
420 uart_remove_one_port() if it registered them with uart_add_one_port().
421
422 Locking: none
423 Interrupts: enabled
424
425uart_suspend_port()
426
427uart_resume_port()
428
429uart_add_one_port()
430
431uart_remove_one_port()
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433Other notes
434-----------
435
436It is intended some day to drop the 'unused' entries from uart_port, and
437allow low level drivers to register their own individual uart_port's with
438the core. This will allow drivers to use uart_port as a pointer to a
439structure containing both the uart_port entry with their own extensions,
440thus:
441
442 struct my_port {
443 struct uart_port port;
444 int my_stuff;
445 };
Richard Genoud84130aa2014-05-13 20:20:43 +0200446
447Modem control lines via GPIO
448----------------------------
449
450Some helpers are provided in order to set/get modem control lines via GPIO.
451
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200452mctrl_gpio_init(port, idx):
Richard Genoud84130aa2014-05-13 20:20:43 +0200453 This will get the {cts,rts,...}-gpios from device tree if they are
454 present and request them, set direction etc, and return an
455 allocated structure. devm_* functions are used, so there's no need
456 to call mctrl_gpio_free().
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200457 As this sets up the irq handling make sure to not handle changes to the
458 gpio input lines in your driver, too.
Richard Genoud84130aa2014-05-13 20:20:43 +0200459
460mctrl_gpio_free(dev, gpios):
461 This will free the requested gpios in mctrl_gpio_init().
462 As devm_* function are used, there's generally no need to call
463 this function.
464
465mctrl_gpio_to_gpiod(gpios, gidx)
466 This returns the gpio structure associated to the modem line index.
467
468mctrl_gpio_set(gpios, mctrl):
469 This will sets the gpios according to the mctrl state.
470
471mctrl_gpio_get(gpios, mctrl):
472 This will update mctrl with the gpios values.
Uwe Kleine-Königce59e482015-09-30 10:19:41 +0200473
474mctrl_gpio_enable_ms(gpios):
475 Enables irqs and handling of changes to the ms lines.
476
477mctrl_gpio_disable_ms(gpios):
478 Disables irqs and handling of changes to the ms lines.