8250.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Driver for 8250/16550-type serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Copyright (C) 2001 Russell King.
  8. */
  9. #include <linux/bits.h>
  10. #include <linux/serial_8250.h>
  11. #include <linux/serial_core.h>
  12. #include <linux/dmaengine.h>
  13. #include "../serial_mctrl_gpio.h"
  14. struct uart_8250_dma {
  15. int (*tx_dma)(struct uart_8250_port *p);
  16. int (*rx_dma)(struct uart_8250_port *p);
  17. void (*prepare_tx_dma)(struct uart_8250_port *p);
  18. void (*prepare_rx_dma)(struct uart_8250_port *p);
  19. /* Filter function */
  20. dma_filter_fn fn;
  21. /* Parameter to the filter function */
  22. void *rx_param;
  23. void *tx_param;
  24. struct dma_slave_config rxconf;
  25. struct dma_slave_config txconf;
  26. struct dma_chan *rxchan;
  27. struct dma_chan *txchan;
  28. /* Device address base for DMA operations */
  29. phys_addr_t rx_dma_addr;
  30. phys_addr_t tx_dma_addr;
  31. /* DMA address of the buffer in memory */
  32. dma_addr_t rx_addr;
  33. dma_addr_t tx_addr;
  34. dma_cookie_t rx_cookie;
  35. dma_cookie_t tx_cookie;
  36. void *rx_buf;
  37. size_t rx_size;
  38. size_t tx_size;
  39. unsigned char tx_running;
  40. unsigned char tx_err;
  41. unsigned char rx_running;
  42. };
  43. struct old_serial_port {
  44. unsigned int uart;
  45. unsigned int baud_base;
  46. unsigned int port;
  47. unsigned int irq;
  48. upf_t flags;
  49. unsigned char io_type;
  50. unsigned char __iomem *iomem_base;
  51. unsigned short iomem_reg_shift;
  52. };
  53. struct serial8250_config {
  54. const char *name;
  55. unsigned short fifo_size;
  56. unsigned short tx_loadsz;
  57. unsigned char fcr;
  58. unsigned char rxtrig_bytes[UART_FCR_R_TRIG_MAX_STATE];
  59. unsigned int flags;
  60. };
  61. #define UART_CAP_FIFO BIT(8) /* UART has FIFO */
  62. #define UART_CAP_EFR BIT(9) /* UART has EFR */
  63. #define UART_CAP_SLEEP BIT(10) /* UART has IER sleep */
  64. #define UART_CAP_AFE BIT(11) /* MCR-based hw flow control */
  65. #define UART_CAP_UUE BIT(12) /* UART needs IER bit 6 set (Xscale) */
  66. #define UART_CAP_RTOIE BIT(13) /* UART needs IER bit 4 set (Xscale, Tegra) */
  67. #define UART_CAP_HFIFO BIT(14) /* UART has a "hidden" FIFO */
  68. #define UART_CAP_RPM BIT(15) /* Runtime PM is active while idle */
  69. #define UART_CAP_IRDA BIT(16) /* UART supports IrDA line discipline */
  70. #define UART_CAP_MINI BIT(17) /* Mini UART on BCM283X family lacks:
  71. * STOP PARITY EPAR SPAR WLEN5 WLEN6
  72. */
  73. #define UART_CAP_NOTEMT BIT(18) /* UART without interrupt on TEMT available */
  74. #define UART_BUG_QUOT BIT(0) /* UART has buggy quot LSB */
  75. #define UART_BUG_TXEN BIT(1) /* UART has buggy TX IIR status */
  76. #define UART_BUG_NOMSR BIT(2) /* UART has buggy MSR status bits (Au1x00) */
  77. #define UART_BUG_THRE BIT(3) /* UART has buggy THRE reassertion */
  78. #define UART_BUG_TXRACE BIT(5) /* UART Tx fails to set remote DR */
  79. /* Module parameters */
  80. #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
  81. extern unsigned int nr_uarts;
  82. #ifdef CONFIG_SERIAL_8250_SHARE_IRQ
  83. #define SERIAL8250_SHARE_IRQS 1
  84. #else
  85. #define SERIAL8250_SHARE_IRQS 0
  86. #endif
  87. extern unsigned int share_irqs;
  88. extern unsigned int skip_txen_test;
  89. #define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \
  90. { \
  91. .iobase = _base, \
  92. .irq = _irq, \
  93. .uartclk = 1843200, \
  94. .iotype = UPIO_PORT, \
  95. .flags = UPF_BOOT_AUTOCONF | (_flags), \
  96. }
  97. #define SERIAL8250_PORT(_base, _irq) SERIAL8250_PORT_FLAGS(_base, _irq, 0)
  98. extern struct uart_driver serial8250_reg;
  99. void serial8250_register_ports(struct uart_driver *drv, struct device *dev);
  100. /* Legacy ISA bus related APIs */
  101. typedef void (*serial8250_isa_config_fn)(int, struct uart_port *, u32 *);
  102. extern serial8250_isa_config_fn serial8250_isa_config;
  103. void serial8250_isa_init_ports(void);
  104. extern struct platform_device *serial8250_isa_devs;
  105. extern const struct uart_ops *univ8250_port_base_ops;
  106. extern struct uart_ops univ8250_port_ops;
  107. static inline int serial_in(struct uart_8250_port *up, int offset)
  108. {
  109. return up->port.serial_in(&up->port, offset);
  110. }
  111. static inline void serial_out(struct uart_8250_port *up, int offset, int value)
  112. {
  113. up->port.serial_out(&up->port, offset, value);
  114. }
  115. /**
  116. * serial_lsr_in - Read LSR register and preserve flags across reads
  117. * @up: uart 8250 port
  118. *
  119. * Read LSR register and handle saving non-preserved flags across reads.
  120. * The flags that are not preserved across reads are stored into
  121. * up->lsr_saved_flags.
  122. *
  123. * Returns LSR value or'ed with the preserved flags (if any).
  124. */
  125. static inline u16 serial_lsr_in(struct uart_8250_port *up)
  126. {
  127. u16 lsr = up->lsr_saved_flags;
  128. lsr |= serial_in(up, UART_LSR);
  129. up->lsr_saved_flags = lsr & up->lsr_save_mask;
  130. return lsr;
  131. }
  132. /*
  133. * For the 16C950
  134. */
  135. static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
  136. {
  137. serial_out(up, UART_SCR, offset);
  138. serial_out(up, UART_ICR, value);
  139. }
  140. static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
  141. int offset)
  142. {
  143. unsigned int value;
  144. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  145. serial_out(up, UART_SCR, offset);
  146. value = serial_in(up, UART_ICR);
  147. serial_icr_write(up, UART_ACR, up->acr);
  148. return value;
  149. }
  150. void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
  151. static inline u32 serial_dl_read(struct uart_8250_port *up)
  152. {
  153. return up->dl_read(up);
  154. }
  155. static inline void serial_dl_write(struct uart_8250_port *up, u32 value)
  156. {
  157. up->dl_write(up, value);
  158. }
  159. static inline bool serial8250_set_THRI(struct uart_8250_port *up)
  160. {
  161. /* Port locked to synchronize UART_IER access against the console. */
  162. lockdep_assert_held_once(&up->port.lock);
  163. if (up->ier & UART_IER_THRI)
  164. return false;
  165. up->ier |= UART_IER_THRI;
  166. serial_out(up, UART_IER, up->ier);
  167. return true;
  168. }
  169. static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
  170. {
  171. /* Port locked to synchronize UART_IER access against the console. */
  172. lockdep_assert_held_once(&up->port.lock);
  173. if (!(up->ier & UART_IER_THRI))
  174. return false;
  175. up->ier &= ~UART_IER_THRI;
  176. serial_out(up, UART_IER, up->ier);
  177. return true;
  178. }
  179. struct uart_8250_port *serial8250_setup_port(int index);
  180. struct uart_8250_port *serial8250_get_port(int line);
  181. void serial8250_rpm_get(struct uart_8250_port *p);
  182. void serial8250_rpm_put(struct uart_8250_port *p);
  183. void serial8250_rpm_get_tx(struct uart_8250_port *p);
  184. void serial8250_rpm_put_tx(struct uart_8250_port *p);
  185. int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
  186. struct serial_rs485 *rs485);
  187. void serial8250_em485_start_tx(struct uart_8250_port *p);
  188. void serial8250_em485_stop_tx(struct uart_8250_port *p);
  189. void serial8250_em485_destroy(struct uart_8250_port *p);
  190. extern struct serial_rs485 serial8250_em485_supported;
  191. /* MCR <-> TIOCM conversion */
  192. static inline int serial8250_TIOCM_to_MCR(int tiocm)
  193. {
  194. int mcr = 0;
  195. if (tiocm & TIOCM_RTS)
  196. mcr |= UART_MCR_RTS;
  197. if (tiocm & TIOCM_DTR)
  198. mcr |= UART_MCR_DTR;
  199. if (tiocm & TIOCM_OUT1)
  200. mcr |= UART_MCR_OUT1;
  201. if (tiocm & TIOCM_OUT2)
  202. mcr |= UART_MCR_OUT2;
  203. if (tiocm & TIOCM_LOOP)
  204. mcr |= UART_MCR_LOOP;
  205. return mcr;
  206. }
  207. static inline int serial8250_MCR_to_TIOCM(int mcr)
  208. {
  209. int tiocm = 0;
  210. if (mcr & UART_MCR_RTS)
  211. tiocm |= TIOCM_RTS;
  212. if (mcr & UART_MCR_DTR)
  213. tiocm |= TIOCM_DTR;
  214. if (mcr & UART_MCR_OUT1)
  215. tiocm |= TIOCM_OUT1;
  216. if (mcr & UART_MCR_OUT2)
  217. tiocm |= TIOCM_OUT2;
  218. if (mcr & UART_MCR_LOOP)
  219. tiocm |= TIOCM_LOOP;
  220. return tiocm;
  221. }
  222. /* MSR <-> TIOCM conversion */
  223. static inline int serial8250_MSR_to_TIOCM(int msr)
  224. {
  225. int tiocm = 0;
  226. if (msr & UART_MSR_DCD)
  227. tiocm |= TIOCM_CAR;
  228. if (msr & UART_MSR_RI)
  229. tiocm |= TIOCM_RNG;
  230. if (msr & UART_MSR_DSR)
  231. tiocm |= TIOCM_DSR;
  232. if (msr & UART_MSR_CTS)
  233. tiocm |= TIOCM_CTS;
  234. return tiocm;
  235. }
  236. static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
  237. {
  238. serial_out(up, UART_MCR, value);
  239. if (up->gpios)
  240. mctrl_gpio_set(up->gpios, serial8250_MCR_to_TIOCM(value));
  241. }
  242. static inline int serial8250_in_MCR(struct uart_8250_port *up)
  243. {
  244. int mctrl;
  245. mctrl = serial_in(up, UART_MCR);
  246. if (up->gpios) {
  247. unsigned int mctrl_gpio = 0;
  248. mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
  249. mctrl |= serial8250_TIOCM_to_MCR(mctrl_gpio);
  250. }
  251. return mctrl;
  252. }
  253. #ifdef CONFIG_SERIAL_8250_PNP
  254. int serial8250_pnp_init(void);
  255. void serial8250_pnp_exit(void);
  256. #else
  257. static inline int serial8250_pnp_init(void) { return 0; }
  258. static inline void serial8250_pnp_exit(void) { }
  259. #endif
  260. #ifdef CONFIG_SERIAL_8250_RSA
  261. void univ8250_rsa_support(struct uart_ops *ops);
  262. #else
  263. static inline void univ8250_rsa_support(struct uart_ops *ops) { }
  264. #endif
  265. #ifdef CONFIG_SERIAL_8250_FINTEK
  266. int fintek_8250_probe(struct uart_8250_port *uart);
  267. #else
  268. static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
  269. #endif
  270. #ifdef CONFIG_ARCH_OMAP1
  271. #include <linux/soc/ti/omap1-soc.h>
  272. static inline int is_omap1_8250(struct uart_8250_port *pt)
  273. {
  274. int res;
  275. switch (pt->port.mapbase) {
  276. case OMAP1_UART1_BASE:
  277. case OMAP1_UART2_BASE:
  278. case OMAP1_UART3_BASE:
  279. res = 1;
  280. break;
  281. default:
  282. res = 0;
  283. break;
  284. }
  285. return res;
  286. }
  287. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  288. {
  289. if (!cpu_is_omap1510())
  290. return 0;
  291. return is_omap1_8250(pt);
  292. }
  293. #else
  294. static inline int is_omap1_8250(struct uart_8250_port *pt)
  295. {
  296. return 0;
  297. }
  298. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  299. {
  300. return 0;
  301. }
  302. #endif
  303. #ifdef CONFIG_SERIAL_8250_DMA
  304. extern int serial8250_tx_dma(struct uart_8250_port *);
  305. extern void serial8250_tx_dma_flush(struct uart_8250_port *);
  306. extern int serial8250_rx_dma(struct uart_8250_port *);
  307. extern void serial8250_rx_dma_flush(struct uart_8250_port *);
  308. extern int serial8250_request_dma(struct uart_8250_port *);
  309. extern void serial8250_release_dma(struct uart_8250_port *);
  310. static inline void serial8250_do_prepare_tx_dma(struct uart_8250_port *p)
  311. {
  312. struct uart_8250_dma *dma = p->dma;
  313. if (dma->prepare_tx_dma)
  314. dma->prepare_tx_dma(p);
  315. }
  316. static inline void serial8250_do_prepare_rx_dma(struct uart_8250_port *p)
  317. {
  318. struct uart_8250_dma *dma = p->dma;
  319. if (dma->prepare_rx_dma)
  320. dma->prepare_rx_dma(p);
  321. }
  322. static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
  323. {
  324. struct uart_8250_dma *dma = p->dma;
  325. return dma && dma->tx_running;
  326. }
  327. #else
  328. static inline int serial8250_tx_dma(struct uart_8250_port *p)
  329. {
  330. return -1;
  331. }
  332. static inline void serial8250_tx_dma_flush(struct uart_8250_port *p) { }
  333. static inline int serial8250_rx_dma(struct uart_8250_port *p)
  334. {
  335. return -1;
  336. }
  337. static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
  338. static inline int serial8250_request_dma(struct uart_8250_port *p)
  339. {
  340. return -1;
  341. }
  342. static inline void serial8250_release_dma(struct uart_8250_port *p) { }
  343. static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
  344. {
  345. return false;
  346. }
  347. #endif
  348. static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
  349. {
  350. unsigned char status;
  351. status = serial_in(up, 0x04); /* EXCR2 */
  352. #define PRESL(x) ((x) & 0x30)
  353. if (PRESL(status) == 0x10) {
  354. /* already in high speed mode */
  355. return 0;
  356. } else {
  357. status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  358. status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  359. serial_out(up, 0x04, status);
  360. }
  361. return 1;
  362. }
  363. static inline int serial_index(struct uart_port *port)
  364. {
  365. return port->minor - 64;
  366. }