driver 15 KB

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