fsl_linflexuart.c 22 KB

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