fsl_linflexuart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale linflexuart serial port driver
  4. *
  5. * Copyright 2012-2016 Freescale Semiconductor, Inc.
  6. * Copyright 2017-2019 NXP
  7. */
  8. #if defined(CONFIG_SERIAL_FSL_LINFLEXUART_CONSOLE) && \
  9. defined(CONFIG_MAGIC_SYSRQ)
  10. #define SUPPORT_SYSRQ
  11. #endif
  12. #include <linux/console.h>
  13. #include <linux/io.h>
  14. #include <linux/irq.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/delay.h>
  22. /* All registers are 32-bit width */
  23. #define LINCR1 0x0000 /* LIN control register */
  24. #define LINIER 0x0004 /* LIN interrupt enable register */
  25. #define LINSR 0x0008 /* LIN status register */
  26. #define LINESR 0x000C /* LIN error status register */
  27. #define UARTCR 0x0010 /* UART mode control register */
  28. #define UARTSR 0x0014 /* UART mode status register */
  29. #define LINTCSR 0x0018 /* LIN timeout control status register */
  30. #define LINOCR 0x001C /* LIN output compare register */
  31. #define LINTOCR 0x0020 /* LIN timeout control register */
  32. #define LINFBRR 0x0024 /* LIN fractional baud rate register */
  33. #define LINIBRR 0x0028 /* LIN integer baud rate register */
  34. #define LINCFR 0x002C /* LIN checksum field register */
  35. #define LINCR2 0x0030 /* LIN control register 2 */
  36. #define BIDR 0x0034 /* Buffer identifier register */
  37. #define BDRL 0x0038 /* Buffer data register least significant */
  38. #define BDRM 0x003C /* Buffer data register most significant */
  39. #define IFER 0x0040 /* Identifier filter enable register */
  40. #define IFMI 0x0044 /* Identifier filter match index */
  41. #define IFMR 0x0048 /* Identifier filter mode register */
  42. #define GCR 0x004C /* Global control register */
  43. #define UARTPTO 0x0050 /* UART preset timeout register */
  44. #define UARTCTO 0x0054 /* UART current timeout register */
  45. /*
  46. * Register field definitions
  47. */
  48. #define LINFLEXD_LINCR1_INIT BIT(0)
  49. #define LINFLEXD_LINCR1_MME BIT(4)
  50. #define LINFLEXD_LINCR1_BF BIT(7)
  51. #define LINFLEXD_LINSR_LINS_INITMODE BIT(12)
  52. #define LINFLEXD_LINSR_LINS_MASK (0xF << 12)
  53. #define LINFLEXD_LINIER_SZIE BIT(15)
  54. #define LINFLEXD_LINIER_OCIE BIT(14)
  55. #define LINFLEXD_LINIER_BEIE BIT(13)
  56. #define LINFLEXD_LINIER_CEIE BIT(12)
  57. #define LINFLEXD_LINIER_HEIE BIT(11)
  58. #define LINFLEXD_LINIER_FEIE BIT(8)
  59. #define LINFLEXD_LINIER_BOIE BIT(7)
  60. #define LINFLEXD_LINIER_LSIE BIT(6)
  61. #define LINFLEXD_LINIER_WUIE BIT(5)
  62. #define LINFLEXD_LINIER_DBFIE BIT(4)
  63. #define LINFLEXD_LINIER_DBEIETOIE BIT(3)
  64. #define LINFLEXD_LINIER_DRIE BIT(2)
  65. #define LINFLEXD_LINIER_DTIE BIT(1)
  66. #define LINFLEXD_LINIER_HRIE BIT(0)
  67. #define LINFLEXD_UARTCR_OSR_MASK (0xF << 24)
  68. #define LINFLEXD_UARTCR_OSR(uartcr) (((uartcr) \
  69. & LINFLEXD_UARTCR_OSR_MASK) >> 24)
  70. #define LINFLEXD_UARTCR_ROSE BIT(23)
  71. #define LINFLEXD_UARTCR_RFBM BIT(9)
  72. #define LINFLEXD_UARTCR_TFBM BIT(8)
  73. #define LINFLEXD_UARTCR_WL1 BIT(7)
  74. #define LINFLEXD_UARTCR_PC1 BIT(6)
  75. #define LINFLEXD_UARTCR_RXEN BIT(5)
  76. #define LINFLEXD_UARTCR_TXEN BIT(4)
  77. #define LINFLEXD_UARTCR_PC0 BIT(3)
  78. #define LINFLEXD_UARTCR_PCE BIT(2)
  79. #define LINFLEXD_UARTCR_WL0 BIT(1)
  80. #define LINFLEXD_UARTCR_UART BIT(0)
  81. #define LINFLEXD_UARTSR_SZF BIT(15)
  82. #define LINFLEXD_UARTSR_OCF BIT(14)
  83. #define LINFLEXD_UARTSR_PE3 BIT(13)
  84. #define LINFLEXD_UARTSR_PE2 BIT(12)
  85. #define LINFLEXD_UARTSR_PE1 BIT(11)
  86. #define LINFLEXD_UARTSR_PE0 BIT(10)
  87. #define LINFLEXD_UARTSR_RMB BIT(9)
  88. #define LINFLEXD_UARTSR_FEF BIT(8)
  89. #define LINFLEXD_UARTSR_BOF BIT(7)
  90. #define LINFLEXD_UARTSR_RPS BIT(6)
  91. #define LINFLEXD_UARTSR_WUF BIT(5)
  92. #define LINFLEXD_UARTSR_4 BIT(4)
  93. #define LINFLEXD_UARTSR_TO BIT(3)
  94. #define LINFLEXD_UARTSR_DRFRFE BIT(2)
  95. #define LINFLEXD_UARTSR_DTFTFF BIT(1)
  96. #define LINFLEXD_UARTSR_NF BIT(0)
  97. #define LINFLEXD_UARTSR_PE (LINFLEXD_UARTSR_PE0 |\
  98. LINFLEXD_UARTSR_PE1 |\
  99. LINFLEXD_UARTSR_PE2 |\
  100. LINFLEXD_UARTSR_PE3)
  101. #define LINFLEX_LDIV_MULTIPLIER (16)
  102. #define DRIVER_NAME "fsl-linflexuart"
  103. #define DEV_NAME "ttyLF"
  104. #define UART_NR 4
  105. #define EARLYCON_BUFFER_INITIAL_CAP 8
  106. #define PREINIT_DELAY 2000 /* us */
  107. static const struct of_device_id linflex_dt_ids[] = {
  108. {
  109. .compatible = "fsl,s32v234-linflexuart",
  110. },
  111. { /* sentinel */ }
  112. };
  113. MODULE_DEVICE_TABLE(of, linflex_dt_ids);
  114. #ifdef CONFIG_SERIAL_FSL_LINFLEXUART_CONSOLE
  115. static struct uart_port *earlycon_port;
  116. static bool linflex_earlycon_same_instance;
  117. static DEFINE_SPINLOCK(init_lock);
  118. static bool during_init;
  119. static struct {
  120. char *content;
  121. unsigned int len, cap;
  122. } earlycon_buf;
  123. #endif
  124. static void linflex_stop_tx(struct uart_port *port)
  125. {
  126. unsigned long ier;
  127. ier = readl(port->membase + LINIER);
  128. ier &= ~(LINFLEXD_LINIER_DTIE);
  129. writel(ier, port->membase + LINIER);
  130. }
  131. static void linflex_stop_rx(struct uart_port *port)
  132. {
  133. unsigned long ier;
  134. ier = readl(port->membase + LINIER);
  135. writel(ier & ~LINFLEXD_LINIER_DRIE, port->membase + LINIER);
  136. }
  137. static inline void linflex_transmit_buffer(struct uart_port *sport)
  138. {
  139. struct circ_buf *xmit = &sport->state->xmit;
  140. unsigned char c;
  141. unsigned long status;
  142. while (!uart_circ_empty(xmit)) {
  143. c = xmit->buf[xmit->tail];
  144. writeb(c, sport->membase + BDRL);
  145. /* Waiting for data transmission completed. */
  146. while (((status = readl(sport->membase + UARTSR)) &
  147. LINFLEXD_UARTSR_DTFTFF) !=
  148. LINFLEXD_UARTSR_DTFTFF)
  149. ;
  150. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  151. sport->icount.tx++;
  152. writel(status | LINFLEXD_UARTSR_DTFTFF,
  153. sport->membase + UARTSR);
  154. }
  155. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  156. uart_write_wakeup(sport);
  157. if (uart_circ_empty(xmit))
  158. linflex_stop_tx(sport);
  159. }
  160. static void linflex_start_tx(struct uart_port *port)
  161. {
  162. unsigned long ier;
  163. linflex_transmit_buffer(port);
  164. ier = readl(port->membase + LINIER);
  165. writel(ier | LINFLEXD_LINIER_DTIE, port->membase + LINIER);
  166. }
  167. static irqreturn_t linflex_txint(int irq, void *dev_id)
  168. {
  169. struct uart_port *sport = dev_id;
  170. struct circ_buf *xmit = &sport->state->xmit;
  171. unsigned long flags;
  172. unsigned long status;
  173. spin_lock_irqsave(&sport->lock, flags);
  174. if (sport->x_char) {
  175. writeb(sport->x_char, sport->membase + BDRL);
  176. /* waiting for data transmission completed */
  177. while (((status = readl(sport->membase + UARTSR)) &
  178. LINFLEXD_UARTSR_DTFTFF) != LINFLEXD_UARTSR_DTFTFF)
  179. ;
  180. writel(status | LINFLEXD_UARTSR_DTFTFF,
  181. sport->membase + UARTSR);
  182. goto out;
  183. }
  184. if (uart_circ_empty(xmit) || uart_tx_stopped(sport)) {
  185. linflex_stop_tx(sport);
  186. goto out;
  187. }
  188. linflex_transmit_buffer(sport);
  189. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  190. uart_write_wakeup(sport);
  191. out:
  192. spin_unlock_irqrestore(&sport->lock, flags);
  193. return IRQ_HANDLED;
  194. }
  195. static irqreturn_t linflex_rxint(int irq, void *dev_id)
  196. {
  197. struct uart_port *sport = dev_id;
  198. unsigned int flg;
  199. struct tty_port *port = &sport->state->port;
  200. unsigned long flags, status;
  201. unsigned char rx;
  202. bool brk;
  203. spin_lock_irqsave(&sport->lock, flags);
  204. status = readl(sport->membase + UARTSR);
  205. while (status & LINFLEXD_UARTSR_RMB) {
  206. rx = readb(sport->membase + BDRM);
  207. brk = false;
  208. flg = TTY_NORMAL;
  209. sport->icount.rx++;
  210. if (status & (LINFLEXD_UARTSR_BOF | LINFLEXD_UARTSR_SZF |
  211. LINFLEXD_UARTSR_FEF | LINFLEXD_UARTSR_PE)) {
  212. if (status & LINFLEXD_UARTSR_SZF)
  213. status |= LINFLEXD_UARTSR_SZF;
  214. if (status & LINFLEXD_UARTSR_BOF)
  215. status |= LINFLEXD_UARTSR_BOF;
  216. if (status & LINFLEXD_UARTSR_FEF) {
  217. if (!rx)
  218. brk = true;
  219. status |= LINFLEXD_UARTSR_FEF;
  220. }
  221. if (status & LINFLEXD_UARTSR_PE)
  222. status |= LINFLEXD_UARTSR_PE;
  223. }
  224. writel(status | LINFLEXD_UARTSR_RMB | LINFLEXD_UARTSR_DRFRFE,
  225. sport->membase + UARTSR);
  226. status = readl(sport->membase + UARTSR);
  227. if (brk) {
  228. uart_handle_break(sport);
  229. } else {
  230. #ifdef SUPPORT_SYSRQ
  231. if (uart_handle_sysrq_char(sport, (unsigned char)rx))
  232. continue;
  233. #endif
  234. tty_insert_flip_char(port, rx, flg);
  235. }
  236. }
  237. spin_unlock_irqrestore(&sport->lock, flags);
  238. tty_flip_buffer_push(port);
  239. return IRQ_HANDLED;
  240. }
  241. static irqreturn_t linflex_int(int irq, void *dev_id)
  242. {
  243. struct uart_port *sport = dev_id;
  244. unsigned long status;
  245. status = readl(sport->membase + UARTSR);
  246. if (status & LINFLEXD_UARTSR_DRFRFE)
  247. linflex_rxint(irq, dev_id);
  248. if (status & LINFLEXD_UARTSR_DTFTFF)
  249. linflex_txint(irq, dev_id);
  250. return IRQ_HANDLED;
  251. }
  252. /* return TIOCSER_TEMT when transmitter is not busy */
  253. static unsigned int linflex_tx_empty(struct uart_port *port)
  254. {
  255. unsigned long status;
  256. status = readl(port->membase + UARTSR) & LINFLEXD_UARTSR_DTFTFF;
  257. return status ? TIOCSER_TEMT : 0;
  258. }
  259. static unsigned int linflex_get_mctrl(struct uart_port *port)
  260. {
  261. return 0;
  262. }
  263. static void linflex_set_mctrl(struct uart_port *port, unsigned int mctrl)
  264. {
  265. }
  266. static void linflex_break_ctl(struct uart_port *port, int break_state)
  267. {
  268. }
  269. static void linflex_setup_watermark(struct uart_port *sport)
  270. {
  271. unsigned long cr, ier, cr1;
  272. /* Disable transmission/reception */
  273. ier = readl(sport->membase + LINIER);
  274. ier &= ~(LINFLEXD_LINIER_DRIE | LINFLEXD_LINIER_DTIE);
  275. writel(ier, sport->membase + LINIER);
  276. cr = readl(sport->membase + UARTCR);
  277. cr &= ~(LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN);
  278. writel(cr, sport->membase + UARTCR);
  279. /* Enter initialization mode by setting INIT bit */
  280. /* set the Linflex in master mode and activate by-pass filter */
  281. cr1 = LINFLEXD_LINCR1_BF | LINFLEXD_LINCR1_MME
  282. | LINFLEXD_LINCR1_INIT;
  283. writel(cr1, sport->membase + LINCR1);
  284. /* wait for init mode entry */
  285. while ((readl(sport->membase + LINSR)
  286. & LINFLEXD_LINSR_LINS_MASK)
  287. != LINFLEXD_LINSR_LINS_INITMODE)
  288. ;
  289. /*
  290. * UART = 0x1; - Linflex working in UART mode
  291. * TXEN = 0x1; - Enable transmission of data now
  292. * RXEn = 0x1; - Receiver enabled
  293. * WL0 = 0x1; - 8 bit data
  294. * PCE = 0x0; - No parity
  295. */
  296. /* set UART bit to allow writing other bits */
  297. writel(LINFLEXD_UARTCR_UART, sport->membase + UARTCR);
  298. cr = (LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN |
  299. LINFLEXD_UARTCR_WL0 | LINFLEXD_UARTCR_UART);
  300. writel(cr, sport->membase + UARTCR);
  301. cr1 &= ~(LINFLEXD_LINCR1_INIT);
  302. writel(cr1, sport->membase + LINCR1);
  303. ier = readl(sport->membase + LINIER);
  304. ier |= LINFLEXD_LINIER_DRIE;
  305. ier |= LINFLEXD_LINIER_DTIE;
  306. writel(ier, sport->membase + LINIER);
  307. }
  308. static int linflex_startup(struct uart_port *port)
  309. {
  310. int ret = 0;
  311. unsigned long flags;
  312. spin_lock_irqsave(&port->lock, flags);
  313. linflex_setup_watermark(port);
  314. spin_unlock_irqrestore(&port->lock, flags);
  315. ret = devm_request_irq(port->dev, port->irq, linflex_int, 0,
  316. DRIVER_NAME, port);
  317. return ret;
  318. }
  319. static void linflex_shutdown(struct uart_port *port)
  320. {
  321. unsigned long ier;
  322. unsigned long flags;
  323. spin_lock_irqsave(&port->lock, flags);
  324. /* disable interrupts */
  325. ier = readl(port->membase + LINIER);
  326. ier &= ~(LINFLEXD_LINIER_DRIE | LINFLEXD_LINIER_DTIE);
  327. writel(ier, port->membase + LINIER);
  328. spin_unlock_irqrestore(&port->lock, flags);
  329. devm_free_irq(port->dev, port->irq, port);
  330. }
  331. static void
  332. linflex_set_termios(struct uart_port *port, struct ktermios *termios,
  333. struct ktermios *old)
  334. {
  335. unsigned long flags;
  336. unsigned long cr, old_cr, cr1;
  337. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  338. cr = readl(port->membase + UARTCR);
  339. old_cr = cr;
  340. /* Enter initialization mode by setting INIT bit */
  341. cr1 = readl(port->membase + LINCR1);
  342. cr1 |= LINFLEXD_LINCR1_INIT;
  343. writel(cr1, port->membase + LINCR1);
  344. /* wait for init mode entry */
  345. while ((readl(port->membase + LINSR)
  346. & LINFLEXD_LINSR_LINS_MASK)
  347. != LINFLEXD_LINSR_LINS_INITMODE)
  348. ;
  349. /*
  350. * only support CS8 and CS7, and for CS7 must enable PE.
  351. * supported mode:
  352. * - (7,e/o,1)
  353. * - (8,n,1)
  354. * - (8,e/o,1)
  355. */
  356. /* enter the UART into configuration mode */
  357. while ((termios->c_cflag & CSIZE) != CS8 &&
  358. (termios->c_cflag & CSIZE) != CS7) {
  359. termios->c_cflag &= ~CSIZE;
  360. termios->c_cflag |= old_csize;
  361. old_csize = CS8;
  362. }
  363. if ((termios->c_cflag & CSIZE) == CS7) {
  364. /* Word length: WL1WL0:00 */
  365. cr = old_cr & ~LINFLEXD_UARTCR_WL1 & ~LINFLEXD_UARTCR_WL0;
  366. }
  367. if ((termios->c_cflag & CSIZE) == CS8) {
  368. /* Word length: WL1WL0:01 */
  369. cr = (old_cr | LINFLEXD_UARTCR_WL0) & ~LINFLEXD_UARTCR_WL1;
  370. }
  371. if (termios->c_cflag & CMSPAR) {
  372. if ((termios->c_cflag & CSIZE) != CS8) {
  373. termios->c_cflag &= ~CSIZE;
  374. termios->c_cflag |= CS8;
  375. }
  376. /* has a space/sticky bit */
  377. cr |= LINFLEXD_UARTCR_WL0;
  378. }
  379. if (termios->c_cflag & CSTOPB)
  380. termios->c_cflag &= ~CSTOPB;
  381. /* parity must be enabled when CS7 to match 8-bits format */
  382. if ((termios->c_cflag & CSIZE) == CS7)
  383. termios->c_cflag |= PARENB;
  384. if ((termios->c_cflag & PARENB)) {
  385. cr |= LINFLEXD_UARTCR_PCE;
  386. if (termios->c_cflag & PARODD)
  387. cr = (cr | LINFLEXD_UARTCR_PC0) &
  388. (~LINFLEXD_UARTCR_PC1);
  389. else
  390. cr = cr & (~LINFLEXD_UARTCR_PC1 &
  391. ~LINFLEXD_UARTCR_PC0);
  392. } else {
  393. cr &= ~LINFLEXD_UARTCR_PCE;
  394. }
  395. spin_lock_irqsave(&port->lock, flags);
  396. port->read_status_mask = 0;
  397. if (termios->c_iflag & INPCK)
  398. port->read_status_mask |= (LINFLEXD_UARTSR_FEF |
  399. LINFLEXD_UARTSR_PE0 |
  400. LINFLEXD_UARTSR_PE1 |
  401. LINFLEXD_UARTSR_PE2 |
  402. LINFLEXD_UARTSR_PE3);
  403. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  404. port->read_status_mask |= LINFLEXD_UARTSR_FEF;
  405. /* characters to ignore */
  406. port->ignore_status_mask = 0;
  407. if (termios->c_iflag & IGNPAR)
  408. port->ignore_status_mask |= LINFLEXD_UARTSR_PE;
  409. if (termios->c_iflag & IGNBRK) {
  410. port->ignore_status_mask |= LINFLEXD_UARTSR_PE;
  411. /*
  412. * if we're ignoring parity and break indicators,
  413. * ignore overruns too (for real raw support).
  414. */
  415. if (termios->c_iflag & IGNPAR)
  416. port->ignore_status_mask |= LINFLEXD_UARTSR_BOF;
  417. }
  418. writel(cr, port->membase + UARTCR);
  419. cr1 &= ~(LINFLEXD_LINCR1_INIT);
  420. writel(cr1, port->membase + LINCR1);
  421. spin_unlock_irqrestore(&port->lock, flags);
  422. }
  423. static const char *linflex_type(struct uart_port *port)
  424. {
  425. return "FSL_LINFLEX";
  426. }
  427. static void linflex_release_port(struct uart_port *port)
  428. {
  429. /* nothing to do */
  430. }
  431. static int linflex_request_port(struct uart_port *port)
  432. {
  433. return 0;
  434. }
  435. /* configure/auto-configure the port */
  436. static void linflex_config_port(struct uart_port *port, int flags)
  437. {
  438. if (flags & UART_CONFIG_TYPE)
  439. port->type = PORT_LINFLEXUART;
  440. }
  441. static const struct uart_ops linflex_pops = {
  442. .tx_empty = linflex_tx_empty,
  443. .set_mctrl = linflex_set_mctrl,
  444. .get_mctrl = linflex_get_mctrl,
  445. .stop_tx = linflex_stop_tx,
  446. .start_tx = linflex_start_tx,
  447. .stop_rx = linflex_stop_rx,
  448. .break_ctl = linflex_break_ctl,
  449. .startup = linflex_startup,
  450. .shutdown = linflex_shutdown,
  451. .set_termios = linflex_set_termios,
  452. .type = linflex_type,
  453. .request_port = linflex_request_port,
  454. .release_port = linflex_release_port,
  455. .config_port = linflex_config_port,
  456. };
  457. static struct uart_port *linflex_ports[UART_NR];
  458. #ifdef CONFIG_SERIAL_FSL_LINFLEXUART_CONSOLE
  459. static void linflex_console_putchar(struct uart_port *port, int ch)
  460. {
  461. unsigned long cr;
  462. cr = readl(port->membase + UARTCR);
  463. writeb(ch, port->membase + BDRL);
  464. if (!(cr & LINFLEXD_UARTCR_TFBM))
  465. while ((readl(port->membase + UARTSR) &
  466. LINFLEXD_UARTSR_DTFTFF)
  467. != LINFLEXD_UARTSR_DTFTFF)
  468. ;
  469. else
  470. while (readl(port->membase + UARTSR) &
  471. LINFLEXD_UARTSR_DTFTFF)
  472. ;
  473. if (!(cr & LINFLEXD_UARTCR_TFBM)) {
  474. writel((readl(port->membase + UARTSR) |
  475. LINFLEXD_UARTSR_DTFTFF),
  476. port->membase + UARTSR);
  477. }
  478. }
  479. static void linflex_earlycon_putchar(struct uart_port *port, int ch)
  480. {
  481. unsigned long flags;
  482. char *ret;
  483. if (!linflex_earlycon_same_instance) {
  484. linflex_console_putchar(port, ch);
  485. return;
  486. }
  487. spin_lock_irqsave(&init_lock, flags);
  488. if (!during_init)
  489. goto outside_init;
  490. if (earlycon_buf.len >= 1 << CONFIG_LOG_BUF_SHIFT)
  491. goto init_release;
  492. if (!earlycon_buf.cap) {
  493. earlycon_buf.content = kmalloc(EARLYCON_BUFFER_INITIAL_CAP,
  494. GFP_ATOMIC);
  495. earlycon_buf.cap = earlycon_buf.content ?
  496. EARLYCON_BUFFER_INITIAL_CAP : 0;
  497. } else if (earlycon_buf.len == earlycon_buf.cap) {
  498. ret = krealloc(earlycon_buf.content, earlycon_buf.cap << 1,
  499. GFP_ATOMIC);
  500. if (ret) {
  501. earlycon_buf.content = ret;
  502. earlycon_buf.cap <<= 1;
  503. }
  504. }
  505. if (earlycon_buf.len < earlycon_buf.cap)
  506. earlycon_buf.content[earlycon_buf.len++] = ch;
  507. goto init_release;
  508. outside_init:
  509. linflex_console_putchar(port, ch);
  510. init_release:
  511. spin_unlock_irqrestore(&init_lock, flags);
  512. }
  513. static void linflex_string_write(struct uart_port *sport, const char *s,
  514. unsigned int count)
  515. {
  516. unsigned long cr, ier = 0;
  517. ier = readl(sport->membase + LINIER);
  518. linflex_stop_tx(sport);
  519. cr = readl(sport->membase + UARTCR);
  520. cr |= (LINFLEXD_UARTCR_TXEN);
  521. writel(cr, sport->membase + UARTCR);
  522. uart_console_write(sport, s, count, linflex_console_putchar);
  523. writel(ier, sport->membase + LINIER);
  524. }
  525. static void
  526. linflex_console_write(struct console *co, const char *s, unsigned int count)
  527. {
  528. struct uart_port *sport = linflex_ports[co->index];
  529. unsigned long flags;
  530. int locked = 1;
  531. if (sport->sysrq)
  532. locked = 0;
  533. else if (oops_in_progress)
  534. locked = spin_trylock_irqsave(&sport->lock, flags);
  535. else
  536. spin_lock_irqsave(&sport->lock, flags);
  537. linflex_string_write(sport, s, count);
  538. if (locked)
  539. spin_unlock_irqrestore(&sport->lock, flags);
  540. }
  541. /*
  542. * if the port was already initialised (eg, by a boot loader),
  543. * try to determine the current setup.
  544. */
  545. static void __init
  546. linflex_console_get_options(struct uart_port *sport, int *parity, int *bits)
  547. {
  548. unsigned long cr;
  549. cr = readl(sport->membase + UARTCR);
  550. cr &= LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN;
  551. if (!cr)
  552. return;
  553. /* ok, the port was enabled */
  554. *parity = 'n';
  555. if (cr & LINFLEXD_UARTCR_PCE) {
  556. if (cr & LINFLEXD_UARTCR_PC0)
  557. *parity = 'o';
  558. else
  559. *parity = 'e';
  560. }
  561. if ((cr & LINFLEXD_UARTCR_WL0) && ((cr & LINFLEXD_UARTCR_WL1) == 0)) {
  562. if (cr & LINFLEXD_UARTCR_PCE)
  563. *bits = 9;
  564. else
  565. *bits = 8;
  566. }
  567. }
  568. static int __init linflex_console_setup(struct console *co, char *options)
  569. {
  570. struct uart_port *sport;
  571. int baud = 115200;
  572. int bits = 8;
  573. int parity = 'n';
  574. int flow = 'n';
  575. int ret;
  576. int i;
  577. unsigned long flags;
  578. /*
  579. * check whether an invalid uart number has been specified, and
  580. * if so, search for the first available port that does have
  581. * console support.
  582. */
  583. if (co->index == -1 || co->index >= ARRAY_SIZE(linflex_ports))
  584. co->index = 0;
  585. sport = linflex_ports[co->index];
  586. if (!sport)
  587. return -ENODEV;
  588. if (options)
  589. uart_parse_options(options, &baud, &parity, &bits, &flow);
  590. else
  591. linflex_console_get_options(sport, &parity, &bits);
  592. if (earlycon_port && sport->mapbase == earlycon_port->mapbase) {
  593. linflex_earlycon_same_instance = true;
  594. spin_lock_irqsave(&init_lock, flags);
  595. during_init = true;
  596. spin_unlock_irqrestore(&init_lock, flags);
  597. /* Workaround for character loss or output of many invalid
  598. * characters, when INIT mode is entered shortly after a
  599. * character has just been printed.
  600. */
  601. udelay(PREINIT_DELAY);
  602. }
  603. linflex_setup_watermark(sport);
  604. ret = uart_set_options(sport, co, baud, parity, bits, flow);
  605. if (!linflex_earlycon_same_instance)
  606. goto done;
  607. spin_lock_irqsave(&init_lock, flags);
  608. /* Emptying buffer */
  609. if (earlycon_buf.len) {
  610. for (i = 0; i < earlycon_buf.len; i++)
  611. linflex_console_putchar(earlycon_port,
  612. earlycon_buf.content[i]);
  613. kfree(earlycon_buf.content);
  614. earlycon_buf.len = 0;
  615. }
  616. during_init = false;
  617. spin_unlock_irqrestore(&init_lock, flags);
  618. done:
  619. return ret;
  620. }
  621. static struct uart_driver linflex_reg;
  622. static struct console linflex_console = {
  623. .name = DEV_NAME,
  624. .write = linflex_console_write,
  625. .device = uart_console_device,
  626. .setup = linflex_console_setup,
  627. .flags = CON_PRINTBUFFER,
  628. .index = -1,
  629. .data = &linflex_reg,
  630. };
  631. static void linflex_earlycon_write(struct console *con, const char *s,
  632. unsigned int n)
  633. {
  634. struct earlycon_device *dev = con->data;
  635. uart_console_write(&dev->port, s, n, linflex_earlycon_putchar);
  636. }
  637. static int __init linflex_early_console_setup(struct earlycon_device *device,
  638. const char *options)
  639. {
  640. if (!device->port.membase)
  641. return -ENODEV;
  642. device->con->write = linflex_earlycon_write;
  643. earlycon_port = &device->port;
  644. return 0;
  645. }
  646. OF_EARLYCON_DECLARE(linflex, "fsl,s32v234-linflexuart",
  647. linflex_early_console_setup);
  648. #define LINFLEX_CONSOLE (&linflex_console)
  649. #else
  650. #define LINFLEX_CONSOLE NULL
  651. #endif
  652. static struct uart_driver linflex_reg = {
  653. .owner = THIS_MODULE,
  654. .driver_name = DRIVER_NAME,
  655. .dev_name = DEV_NAME,
  656. .nr = ARRAY_SIZE(linflex_ports),
  657. .cons = LINFLEX_CONSOLE,
  658. };
  659. static int linflex_probe(struct platform_device *pdev)
  660. {
  661. struct device_node *np = pdev->dev.of_node;
  662. struct uart_port *sport;
  663. struct resource *res;
  664. int ret;
  665. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  666. if (!sport)
  667. return -ENOMEM;
  668. ret = of_alias_get_id(np, "serial");
  669. if (ret < 0) {
  670. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  671. return ret;
  672. }
  673. if (ret >= UART_NR) {
  674. dev_err(&pdev->dev, "driver limited to %d serial ports\n",
  675. UART_NR);
  676. return -ENOMEM;
  677. }
  678. sport->line = ret;
  679. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  680. if (!res)
  681. return -ENODEV;
  682. sport->mapbase = res->start;
  683. sport->membase = devm_ioremap_resource(&pdev->dev, res);
  684. if (IS_ERR(sport->membase))
  685. return PTR_ERR(sport->membase);
  686. sport->dev = &pdev->dev;
  687. sport->type = PORT_LINFLEXUART;
  688. sport->iotype = UPIO_MEM;
  689. sport->irq = platform_get_irq(pdev, 0);
  690. sport->ops = &linflex_pops;
  691. sport->flags = UPF_BOOT_AUTOCONF;
  692. linflex_ports[sport->line] = sport;
  693. platform_set_drvdata(pdev, sport);
  694. ret = uart_add_one_port(&linflex_reg, sport);
  695. if (ret)
  696. return ret;
  697. return 0;
  698. }
  699. static int linflex_remove(struct platform_device *pdev)
  700. {
  701. struct uart_port *sport = platform_get_drvdata(pdev);
  702. uart_remove_one_port(&linflex_reg, sport);
  703. return 0;
  704. }
  705. #ifdef CONFIG_PM_SLEEP
  706. static int linflex_suspend(struct device *dev)
  707. {
  708. struct uart_port *sport = dev_get_drvdata(dev);
  709. uart_suspend_port(&linflex_reg, sport);
  710. return 0;
  711. }
  712. static int linflex_resume(struct device *dev)
  713. {
  714. struct uart_port *sport = dev_get_drvdata(dev);
  715. uart_resume_port(&linflex_reg, sport);
  716. return 0;
  717. }
  718. #endif
  719. static SIMPLE_DEV_PM_OPS(linflex_pm_ops, linflex_suspend, linflex_resume);
  720. static struct platform_driver linflex_driver = {
  721. .probe = linflex_probe,
  722. .remove = linflex_remove,
  723. .driver = {
  724. .name = DRIVER_NAME,
  725. .of_match_table = linflex_dt_ids,
  726. .pm = &linflex_pm_ops,
  727. },
  728. };
  729. static int __init linflex_serial_init(void)
  730. {
  731. int ret;
  732. ret = uart_register_driver(&linflex_reg);
  733. if (ret)
  734. return ret;
  735. ret = platform_driver_register(&linflex_driver);
  736. if (ret)
  737. uart_unregister_driver(&linflex_reg);
  738. return ret;
  739. }
  740. static void __exit linflex_serial_exit(void)
  741. {
  742. platform_driver_unregister(&linflex_driver);
  743. uart_unregister_driver(&linflex_reg);
  744. }
  745. module_init(linflex_serial_init);
  746. module_exit(linflex_serial_exit);
  747. MODULE_DESCRIPTION("Freescale linflex serial port driver");
  748. MODULE_LICENSE("GPL v2");