driver 14 KB

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