8250.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_reg.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_PARITY BIT(4) /* UART mishandles parity if FIFO enabled */
  79. #define UART_BUG_TXRACE BIT(5) /* UART Tx fails to set remote DR */
  80. #ifdef CONFIG_SERIAL_8250_SHARE_IRQ
  81. #define SERIAL8250_SHARE_IRQS 1
  82. #else
  83. #define SERIAL8250_SHARE_IRQS 0
  84. #endif
  85. #define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \
  86. { \
  87. .iobase = _base, \
  88. .irq = _irq, \
  89. .uartclk = 1843200, \
  90. .iotype = UPIO_PORT, \
  91. .flags = UPF_BOOT_AUTOCONF | (_flags), \
  92. }
  93. #define SERIAL8250_PORT(_base, _irq) SERIAL8250_PORT_FLAGS(_base, _irq, 0)
  94. static inline int serial_in(struct uart_8250_port *up, int offset)
  95. {
  96. return up->port.serial_in(&up->port, offset);
  97. }
  98. static inline void serial_out(struct uart_8250_port *up, int offset, int value)
  99. {
  100. up->port.serial_out(&up->port, offset, value);
  101. }
  102. /**
  103. * serial_lsr_in - Read LSR register and preserve flags across reads
  104. * @up: uart 8250 port
  105. *
  106. * Read LSR register and handle saving non-preserved flags across reads.
  107. * The flags that are not preserved across reads are stored into
  108. * up->lsr_saved_flags.
  109. *
  110. * Returns LSR value or'ed with the preserved flags (if any).
  111. */
  112. static inline u16 serial_lsr_in(struct uart_8250_port *up)
  113. {
  114. u16 lsr = up->lsr_saved_flags;
  115. lsr |= serial_in(up, UART_LSR);
  116. up->lsr_saved_flags = lsr & up->lsr_save_mask;
  117. return lsr;
  118. }
  119. /*
  120. * For the 16C950
  121. */
  122. static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
  123. {
  124. serial_out(up, UART_SCR, offset);
  125. serial_out(up, UART_ICR, value);
  126. }
  127. static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
  128. int offset)
  129. {
  130. unsigned int value;
  131. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  132. serial_out(up, UART_SCR, offset);
  133. value = serial_in(up, UART_ICR);
  134. serial_icr_write(up, UART_ACR, up->acr);
  135. return value;
  136. }
  137. void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
  138. static inline int serial_dl_read(struct uart_8250_port *up)
  139. {
  140. return up->dl_read(up);
  141. }
  142. static inline void serial_dl_write(struct uart_8250_port *up, int value)
  143. {
  144. up->dl_write(up, value);
  145. }
  146. static inline bool serial8250_set_THRI(struct uart_8250_port *up)
  147. {
  148. if (up->ier & UART_IER_THRI)
  149. return false;
  150. up->ier |= UART_IER_THRI;
  151. serial_out(up, UART_IER, up->ier);
  152. return true;
  153. }
  154. static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
  155. {
  156. if (!(up->ier & UART_IER_THRI))
  157. return false;
  158. up->ier &= ~UART_IER_THRI;
  159. serial_out(up, UART_IER, up->ier);
  160. return true;
  161. }
  162. struct uart_8250_port *serial8250_get_port(int line);
  163. void serial8250_rpm_get(struct uart_8250_port *p);
  164. void serial8250_rpm_put(struct uart_8250_port *p);
  165. void serial8250_rpm_get_tx(struct uart_8250_port *p);
  166. void serial8250_rpm_put_tx(struct uart_8250_port *p);
  167. int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
  168. struct serial_rs485 *rs485);
  169. void serial8250_em485_start_tx(struct uart_8250_port *p);
  170. void serial8250_em485_stop_tx(struct uart_8250_port *p);
  171. void serial8250_em485_destroy(struct uart_8250_port *p);
  172. extern struct serial_rs485 serial8250_em485_supported;
  173. /* MCR <-> TIOCM conversion */
  174. static inline int serial8250_TIOCM_to_MCR(int tiocm)
  175. {
  176. int mcr = 0;
  177. if (tiocm & TIOCM_RTS)
  178. mcr |= UART_MCR_RTS;
  179. if (tiocm & TIOCM_DTR)
  180. mcr |= UART_MCR_DTR;
  181. if (tiocm & TIOCM_OUT1)
  182. mcr |= UART_MCR_OUT1;
  183. if (tiocm & TIOCM_OUT2)
  184. mcr |= UART_MCR_OUT2;
  185. if (tiocm & TIOCM_LOOP)
  186. mcr |= UART_MCR_LOOP;
  187. return mcr;
  188. }
  189. static inline int serial8250_MCR_to_TIOCM(int mcr)
  190. {
  191. int tiocm = 0;
  192. if (mcr & UART_MCR_RTS)
  193. tiocm |= TIOCM_RTS;
  194. if (mcr & UART_MCR_DTR)
  195. tiocm |= TIOCM_DTR;
  196. if (mcr & UART_MCR_OUT1)
  197. tiocm |= TIOCM_OUT1;
  198. if (mcr & UART_MCR_OUT2)
  199. tiocm |= TIOCM_OUT2;
  200. if (mcr & UART_MCR_LOOP)
  201. tiocm |= TIOCM_LOOP;
  202. return tiocm;
  203. }
  204. /* MSR <-> TIOCM conversion */
  205. static inline int serial8250_MSR_to_TIOCM(int msr)
  206. {
  207. int tiocm = 0;
  208. if (msr & UART_MSR_DCD)
  209. tiocm |= TIOCM_CAR;
  210. if (msr & UART_MSR_RI)
  211. tiocm |= TIOCM_RNG;
  212. if (msr & UART_MSR_DSR)
  213. tiocm |= TIOCM_DSR;
  214. if (msr & UART_MSR_CTS)
  215. tiocm |= TIOCM_CTS;
  216. return tiocm;
  217. }
  218. static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
  219. {
  220. serial_out(up, UART_MCR, value);
  221. if (up->gpios)
  222. mctrl_gpio_set(up->gpios, serial8250_MCR_to_TIOCM(value));
  223. }
  224. static inline int serial8250_in_MCR(struct uart_8250_port *up)
  225. {
  226. int mctrl;
  227. mctrl = serial_in(up, UART_MCR);
  228. if (up->gpios) {
  229. unsigned int mctrl_gpio = 0;
  230. mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
  231. mctrl |= serial8250_TIOCM_to_MCR(mctrl_gpio);
  232. }
  233. return mctrl;
  234. }
  235. bool alpha_jensen(void);
  236. void alpha_jensen_set_mctrl(struct uart_port *port, unsigned int mctrl);
  237. #ifdef CONFIG_SERIAL_8250_PNP
  238. int serial8250_pnp_init(void);
  239. void serial8250_pnp_exit(void);
  240. #else
  241. static inline int serial8250_pnp_init(void) { return 0; }
  242. static inline void serial8250_pnp_exit(void) { }
  243. #endif
  244. #ifdef CONFIG_SERIAL_8250_FINTEK
  245. int fintek_8250_probe(struct uart_8250_port *uart);
  246. #else
  247. static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
  248. #endif
  249. #ifdef CONFIG_ARCH_OMAP1
  250. #include <linux/soc/ti/omap1-soc.h>
  251. static inline int is_omap1_8250(struct uart_8250_port *pt)
  252. {
  253. int res;
  254. switch (pt->port.mapbase) {
  255. case OMAP1_UART1_BASE:
  256. case OMAP1_UART2_BASE:
  257. case OMAP1_UART3_BASE:
  258. res = 1;
  259. break;
  260. default:
  261. res = 0;
  262. break;
  263. }
  264. return res;
  265. }
  266. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  267. {
  268. if (!cpu_is_omap1510())
  269. return 0;
  270. return is_omap1_8250(pt);
  271. }
  272. #else
  273. static inline int is_omap1_8250(struct uart_8250_port *pt)
  274. {
  275. return 0;
  276. }
  277. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  278. {
  279. return 0;
  280. }
  281. #endif
  282. #ifdef CONFIG_SERIAL_8250_DMA
  283. extern int serial8250_tx_dma(struct uart_8250_port *);
  284. extern int serial8250_rx_dma(struct uart_8250_port *);
  285. extern void serial8250_rx_dma_flush(struct uart_8250_port *);
  286. extern int serial8250_request_dma(struct uart_8250_port *);
  287. extern void serial8250_release_dma(struct uart_8250_port *);
  288. static inline void serial8250_do_prepare_tx_dma(struct uart_8250_port *p)
  289. {
  290. struct uart_8250_dma *dma = p->dma;
  291. if (dma->prepare_tx_dma)
  292. dma->prepare_tx_dma(p);
  293. }
  294. static inline void serial8250_do_prepare_rx_dma(struct uart_8250_port *p)
  295. {
  296. struct uart_8250_dma *dma = p->dma;
  297. if (dma->prepare_rx_dma)
  298. dma->prepare_rx_dma(p);
  299. }
  300. static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
  301. {
  302. struct uart_8250_dma *dma = p->dma;
  303. return dma && dma->tx_running;
  304. }
  305. #else
  306. static inline int serial8250_tx_dma(struct uart_8250_port *p)
  307. {
  308. return -1;
  309. }
  310. static inline int serial8250_rx_dma(struct uart_8250_port *p)
  311. {
  312. return -1;
  313. }
  314. static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
  315. static inline int serial8250_request_dma(struct uart_8250_port *p)
  316. {
  317. return -1;
  318. }
  319. static inline void serial8250_release_dma(struct uart_8250_port *p) { }
  320. static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
  321. {
  322. return false;
  323. }
  324. #endif
  325. static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
  326. {
  327. unsigned char status;
  328. status = serial_in(up, 0x04); /* EXCR2 */
  329. #define PRESL(x) ((x) & 0x30)
  330. if (PRESL(status) == 0x10) {
  331. /* already in high speed mode */
  332. return 0;
  333. } else {
  334. status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  335. status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  336. serial_out(up, 0x04, status);
  337. }
  338. return 1;
  339. }
  340. static inline int serial_index(struct uart_port *port)
  341. {
  342. return port->minor - 64;
  343. }