pxa.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Based on drivers/serial/8250.c by Russell King.
  4. *
  5. * Author: Nicolas Pitre
  6. * Created: Feb 20, 2003
  7. * Copyright: (C) 2003 Monta Vista Software, Inc.
  8. *
  9. * Note 1: This driver is made separate from the already too overloaded
  10. * 8250.c because it needs some kirks of its own and that'll make it
  11. * easier to add DMA support.
  12. *
  13. * Note 2: I'm too sick of device allocation policies for serial ports.
  14. * If someone else wants to request an "official" allocation of major/minor
  15. * for this driver please be my guest. And don't forget that new hardware
  16. * to come from Intel might have more than 3 or 4 of those UARTs. Let's
  17. * hope for a better port registration and dynamic device allocation scheme
  18. * with the serial core maintainer satisfaction to appear soon.
  19. */
  20. #include <linux/ioport.h>
  21. #include <linux/init.h>
  22. #include <linux/console.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/serial.h>
  25. #include <linux/serial_reg.h>
  26. #include <linux/circ_buf.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/of.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/serial_core.h>
  34. #include <linux/clk.h>
  35. #include <linux/io.h>
  36. #include <linux/slab.h>
  37. #define PXA_NAME_LEN 8
  38. struct uart_pxa_port {
  39. struct uart_port port;
  40. unsigned char ier;
  41. unsigned char lcr;
  42. unsigned char mcr;
  43. unsigned int lsr_break_flag;
  44. struct clk *clk;
  45. char name[PXA_NAME_LEN];
  46. };
  47. static inline unsigned int serial_in(struct uart_pxa_port *up, int offset)
  48. {
  49. offset <<= 2;
  50. return readl(up->port.membase + offset);
  51. }
  52. static inline void serial_out(struct uart_pxa_port *up, int offset, int value)
  53. {
  54. offset <<= 2;
  55. writel(value, up->port.membase + offset);
  56. }
  57. static void serial_pxa_enable_ms(struct uart_port *port)
  58. {
  59. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  60. up->ier |= UART_IER_MSI;
  61. serial_out(up, UART_IER, up->ier);
  62. }
  63. static void serial_pxa_stop_tx(struct uart_port *port)
  64. {
  65. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  66. if (up->ier & UART_IER_THRI) {
  67. up->ier &= ~UART_IER_THRI;
  68. serial_out(up, UART_IER, up->ier);
  69. }
  70. }
  71. static void serial_pxa_stop_rx(struct uart_port *port)
  72. {
  73. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  74. up->ier &= ~UART_IER_RLSI;
  75. up->port.read_status_mask &= ~UART_LSR_DR;
  76. serial_out(up, UART_IER, up->ier);
  77. }
  78. static inline void receive_chars(struct uart_pxa_port *up, int *status)
  79. {
  80. unsigned int ch, flag;
  81. int max_count = 256;
  82. do {
  83. /* work around Errata #20 according to
  84. * Intel(R) PXA27x Processor Family
  85. * Specification Update (May 2005)
  86. *
  87. * Step 2
  88. * Disable the Reciever Time Out Interrupt via IER[RTOEI]
  89. */
  90. up->ier &= ~UART_IER_RTOIE;
  91. serial_out(up, UART_IER, up->ier);
  92. ch = serial_in(up, UART_RX);
  93. flag = TTY_NORMAL;
  94. up->port.icount.rx++;
  95. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  96. UART_LSR_FE | UART_LSR_OE))) {
  97. /*
  98. * For statistics only
  99. */
  100. if (*status & UART_LSR_BI) {
  101. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  102. up->port.icount.brk++;
  103. /*
  104. * We do the SysRQ and SAK checking
  105. * here because otherwise the break
  106. * may get masked by ignore_status_mask
  107. * or read_status_mask.
  108. */
  109. if (uart_handle_break(&up->port))
  110. goto ignore_char;
  111. } else if (*status & UART_LSR_PE)
  112. up->port.icount.parity++;
  113. else if (*status & UART_LSR_FE)
  114. up->port.icount.frame++;
  115. if (*status & UART_LSR_OE)
  116. up->port.icount.overrun++;
  117. /*
  118. * Mask off conditions which should be ignored.
  119. */
  120. *status &= up->port.read_status_mask;
  121. #ifdef CONFIG_SERIAL_PXA_CONSOLE
  122. if (up->port.line == up->port.cons->index) {
  123. /* Recover the break flag from console xmit */
  124. *status |= up->lsr_break_flag;
  125. up->lsr_break_flag = 0;
  126. }
  127. #endif
  128. if (*status & UART_LSR_BI) {
  129. flag = TTY_BREAK;
  130. } else if (*status & UART_LSR_PE)
  131. flag = TTY_PARITY;
  132. else if (*status & UART_LSR_FE)
  133. flag = TTY_FRAME;
  134. }
  135. if (uart_handle_sysrq_char(&up->port, ch))
  136. goto ignore_char;
  137. uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
  138. ignore_char:
  139. *status = serial_in(up, UART_LSR);
  140. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  141. tty_flip_buffer_push(&up->port.state->port);
  142. /* work around Errata #20 according to
  143. * Intel(R) PXA27x Processor Family
  144. * Specification Update (May 2005)
  145. *
  146. * Step 6:
  147. * No more data in FIFO: Re-enable RTO interrupt via IER[RTOIE]
  148. */
  149. up->ier |= UART_IER_RTOIE;
  150. serial_out(up, UART_IER, up->ier);
  151. }
  152. static void transmit_chars(struct uart_pxa_port *up)
  153. {
  154. struct circ_buf *xmit = &up->port.state->xmit;
  155. int count;
  156. if (up->port.x_char) {
  157. serial_out(up, UART_TX, up->port.x_char);
  158. up->port.icount.tx++;
  159. up->port.x_char = 0;
  160. return;
  161. }
  162. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  163. serial_pxa_stop_tx(&up->port);
  164. return;
  165. }
  166. count = up->port.fifosize / 2;
  167. do {
  168. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  169. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  170. up->port.icount.tx++;
  171. if (uart_circ_empty(xmit))
  172. break;
  173. } while (--count > 0);
  174. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  175. uart_write_wakeup(&up->port);
  176. if (uart_circ_empty(xmit))
  177. serial_pxa_stop_tx(&up->port);
  178. }
  179. static void serial_pxa_start_tx(struct uart_port *port)
  180. {
  181. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  182. if (!(up->ier & UART_IER_THRI)) {
  183. up->ier |= UART_IER_THRI;
  184. serial_out(up, UART_IER, up->ier);
  185. }
  186. }
  187. /* should hold up->port.lock */
  188. static inline void check_modem_status(struct uart_pxa_port *up)
  189. {
  190. int status;
  191. status = serial_in(up, UART_MSR);
  192. if ((status & UART_MSR_ANY_DELTA) == 0)
  193. return;
  194. if (status & UART_MSR_TERI)
  195. up->port.icount.rng++;
  196. if (status & UART_MSR_DDSR)
  197. up->port.icount.dsr++;
  198. if (status & UART_MSR_DDCD)
  199. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  200. if (status & UART_MSR_DCTS)
  201. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  202. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  203. }
  204. /*
  205. * This handles the interrupt from one port.
  206. */
  207. static inline irqreturn_t serial_pxa_irq(int irq, void *dev_id)
  208. {
  209. struct uart_pxa_port *up = dev_id;
  210. unsigned int iir, lsr;
  211. iir = serial_in(up, UART_IIR);
  212. if (iir & UART_IIR_NO_INT)
  213. return IRQ_NONE;
  214. spin_lock(&up->port.lock);
  215. lsr = serial_in(up, UART_LSR);
  216. if (lsr & UART_LSR_DR)
  217. receive_chars(up, &lsr);
  218. check_modem_status(up);
  219. if (lsr & UART_LSR_THRE)
  220. transmit_chars(up);
  221. spin_unlock(&up->port.lock);
  222. return IRQ_HANDLED;
  223. }
  224. static unsigned int serial_pxa_tx_empty(struct uart_port *port)
  225. {
  226. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  227. unsigned long flags;
  228. unsigned int ret;
  229. spin_lock_irqsave(&up->port.lock, flags);
  230. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  231. spin_unlock_irqrestore(&up->port.lock, flags);
  232. return ret;
  233. }
  234. static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
  235. {
  236. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  237. unsigned char status;
  238. unsigned int ret;
  239. status = serial_in(up, UART_MSR);
  240. ret = 0;
  241. if (status & UART_MSR_DCD)
  242. ret |= TIOCM_CAR;
  243. if (status & UART_MSR_RI)
  244. ret |= TIOCM_RNG;
  245. if (status & UART_MSR_DSR)
  246. ret |= TIOCM_DSR;
  247. if (status & UART_MSR_CTS)
  248. ret |= TIOCM_CTS;
  249. return ret;
  250. }
  251. static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
  252. {
  253. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  254. unsigned char mcr = 0;
  255. if (mctrl & TIOCM_RTS)
  256. mcr |= UART_MCR_RTS;
  257. if (mctrl & TIOCM_DTR)
  258. mcr |= UART_MCR_DTR;
  259. if (mctrl & TIOCM_OUT1)
  260. mcr |= UART_MCR_OUT1;
  261. if (mctrl & TIOCM_OUT2)
  262. mcr |= UART_MCR_OUT2;
  263. if (mctrl & TIOCM_LOOP)
  264. mcr |= UART_MCR_LOOP;
  265. mcr |= up->mcr;
  266. serial_out(up, UART_MCR, mcr);
  267. }
  268. static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
  269. {
  270. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  271. unsigned long flags;
  272. spin_lock_irqsave(&up->port.lock, flags);
  273. if (break_state == -1)
  274. up->lcr |= UART_LCR_SBC;
  275. else
  276. up->lcr &= ~UART_LCR_SBC;
  277. serial_out(up, UART_LCR, up->lcr);
  278. spin_unlock_irqrestore(&up->port.lock, flags);
  279. }
  280. static int serial_pxa_startup(struct uart_port *port)
  281. {
  282. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  283. unsigned long flags;
  284. int retval;
  285. if (port->line == 3) /* HWUART */
  286. up->mcr |= UART_MCR_AFE;
  287. else
  288. up->mcr = 0;
  289. up->port.uartclk = clk_get_rate(up->clk);
  290. /*
  291. * Allocate the IRQ
  292. */
  293. retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
  294. if (retval)
  295. return retval;
  296. /*
  297. * Clear the FIFO buffers and disable them.
  298. * (they will be reenabled in set_termios())
  299. */
  300. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  301. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  302. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  303. serial_out(up, UART_FCR, 0);
  304. /*
  305. * Clear the interrupt registers.
  306. */
  307. (void) serial_in(up, UART_LSR);
  308. (void) serial_in(up, UART_RX);
  309. (void) serial_in(up, UART_IIR);
  310. (void) serial_in(up, UART_MSR);
  311. /*
  312. * Now, initialize the UART
  313. */
  314. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  315. spin_lock_irqsave(&up->port.lock, flags);
  316. up->port.mctrl |= TIOCM_OUT2;
  317. serial_pxa_set_mctrl(&up->port, up->port.mctrl);
  318. spin_unlock_irqrestore(&up->port.lock, flags);
  319. /*
  320. * Finally, enable interrupts. Note: Modem status interrupts
  321. * are set via set_termios(), which will be occurring imminently
  322. * anyway, so we don't enable them here.
  323. */
  324. up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
  325. serial_out(up, UART_IER, up->ier);
  326. /*
  327. * And clear the interrupt registers again for luck.
  328. */
  329. (void) serial_in(up, UART_LSR);
  330. (void) serial_in(up, UART_RX);
  331. (void) serial_in(up, UART_IIR);
  332. (void) serial_in(up, UART_MSR);
  333. return 0;
  334. }
  335. static void serial_pxa_shutdown(struct uart_port *port)
  336. {
  337. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  338. unsigned long flags;
  339. free_irq(up->port.irq, up);
  340. /*
  341. * Disable interrupts from this port
  342. */
  343. up->ier = 0;
  344. serial_out(up, UART_IER, 0);
  345. spin_lock_irqsave(&up->port.lock, flags);
  346. up->port.mctrl &= ~TIOCM_OUT2;
  347. serial_pxa_set_mctrl(&up->port, up->port.mctrl);
  348. spin_unlock_irqrestore(&up->port.lock, flags);
  349. /*
  350. * Disable break condition and FIFOs
  351. */
  352. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  353. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  354. UART_FCR_CLEAR_RCVR |
  355. UART_FCR_CLEAR_XMIT);
  356. serial_out(up, UART_FCR, 0);
  357. }
  358. static void
  359. serial_pxa_set_termios(struct uart_port *port, struct ktermios *termios,
  360. const struct ktermios *old)
  361. {
  362. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  363. unsigned char cval, fcr = 0;
  364. unsigned long flags;
  365. unsigned int baud, quot;
  366. unsigned int dll;
  367. cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
  368. if (termios->c_cflag & CSTOPB)
  369. cval |= UART_LCR_STOP;
  370. if (termios->c_cflag & PARENB)
  371. cval |= UART_LCR_PARITY;
  372. if (!(termios->c_cflag & PARODD))
  373. cval |= UART_LCR_EPAR;
  374. /*
  375. * Ask the core to calculate the divisor for us.
  376. */
  377. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  378. quot = uart_get_divisor(port, baud);
  379. if ((up->port.uartclk / quot) < (2400 * 16))
  380. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
  381. else if ((up->port.uartclk / quot) < (230400 * 16))
  382. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
  383. else
  384. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR32;
  385. /*
  386. * Ok, we're now changing the port state. Do it with
  387. * interrupts disabled.
  388. */
  389. spin_lock_irqsave(&up->port.lock, flags);
  390. /*
  391. * Ensure the port will be enabled.
  392. * This is required especially for serial console.
  393. */
  394. up->ier |= UART_IER_UUE;
  395. /*
  396. * Update the per-port timeout.
  397. */
  398. uart_update_timeout(port, termios->c_cflag, baud);
  399. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  400. if (termios->c_iflag & INPCK)
  401. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  402. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  403. up->port.read_status_mask |= UART_LSR_BI;
  404. /*
  405. * Characters to ignore
  406. */
  407. up->port.ignore_status_mask = 0;
  408. if (termios->c_iflag & IGNPAR)
  409. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  410. if (termios->c_iflag & IGNBRK) {
  411. up->port.ignore_status_mask |= UART_LSR_BI;
  412. /*
  413. * If we're ignoring parity and break indicators,
  414. * ignore overruns too (for real raw support).
  415. */
  416. if (termios->c_iflag & IGNPAR)
  417. up->port.ignore_status_mask |= UART_LSR_OE;
  418. }
  419. /*
  420. * ignore all characters if CREAD is not set
  421. */
  422. if ((termios->c_cflag & CREAD) == 0)
  423. up->port.ignore_status_mask |= UART_LSR_DR;
  424. /*
  425. * CTS flow control flag and modem status interrupts
  426. */
  427. up->ier &= ~UART_IER_MSI;
  428. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  429. up->ier |= UART_IER_MSI;
  430. serial_out(up, UART_IER, up->ier);
  431. if (termios->c_cflag & CRTSCTS)
  432. up->mcr |= UART_MCR_AFE;
  433. else
  434. up->mcr &= ~UART_MCR_AFE;
  435. serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
  436. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  437. /*
  438. * work around Errata #75 according to Intel(R) PXA27x Processor Family
  439. * Specification Update (Nov 2005)
  440. */
  441. dll = serial_in(up, UART_DLL);
  442. WARN_ON(dll != (quot & 0xff));
  443. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  444. serial_out(up, UART_LCR, cval); /* reset DLAB */
  445. up->lcr = cval; /* Save LCR */
  446. serial_pxa_set_mctrl(&up->port, up->port.mctrl);
  447. serial_out(up, UART_FCR, fcr);
  448. spin_unlock_irqrestore(&up->port.lock, flags);
  449. }
  450. static void
  451. serial_pxa_pm(struct uart_port *port, unsigned int state,
  452. unsigned int oldstate)
  453. {
  454. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  455. if (!state)
  456. clk_prepare_enable(up->clk);
  457. else
  458. clk_disable_unprepare(up->clk);
  459. }
  460. static void serial_pxa_release_port(struct uart_port *port)
  461. {
  462. }
  463. static int serial_pxa_request_port(struct uart_port *port)
  464. {
  465. return 0;
  466. }
  467. static void serial_pxa_config_port(struct uart_port *port, int flags)
  468. {
  469. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  470. up->port.type = PORT_PXA;
  471. }
  472. static int
  473. serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
  474. {
  475. /* we don't want the core code to modify any port params */
  476. return -EINVAL;
  477. }
  478. static const char *
  479. serial_pxa_type(struct uart_port *port)
  480. {
  481. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  482. return up->name;
  483. }
  484. static struct uart_pxa_port *serial_pxa_ports[4];
  485. static struct uart_driver serial_pxa_reg;
  486. #ifdef CONFIG_SERIAL_PXA_CONSOLE
  487. /*
  488. * Wait for transmitter & holding register to empty
  489. */
  490. static void wait_for_xmitr(struct uart_pxa_port *up)
  491. {
  492. unsigned int status, tmout = 10000;
  493. /* Wait up to 10ms for the character(s) to be sent. */
  494. do {
  495. status = serial_in(up, UART_LSR);
  496. if (status & UART_LSR_BI)
  497. up->lsr_break_flag = UART_LSR_BI;
  498. if (--tmout == 0)
  499. break;
  500. udelay(1);
  501. } while (!uart_lsr_tx_empty(status));
  502. /* Wait up to 1s for flow control if necessary */
  503. if (up->port.flags & UPF_CONS_FLOW) {
  504. tmout = 1000000;
  505. while (--tmout &&
  506. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  507. udelay(1);
  508. }
  509. }
  510. static void serial_pxa_console_putchar(struct uart_port *port, unsigned char ch)
  511. {
  512. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  513. wait_for_xmitr(up);
  514. serial_out(up, UART_TX, ch);
  515. }
  516. /*
  517. * Print a string to the serial port trying not to disturb
  518. * any possible real use of the port...
  519. *
  520. * The console_lock must be held when we get here.
  521. */
  522. static void
  523. serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
  524. {
  525. struct uart_pxa_port *up = serial_pxa_ports[co->index];
  526. unsigned int ier;
  527. unsigned long flags;
  528. int locked = 1;
  529. clk_enable(up->clk);
  530. local_irq_save(flags);
  531. if (up->port.sysrq)
  532. locked = 0;
  533. else if (oops_in_progress)
  534. locked = spin_trylock(&up->port.lock);
  535. else
  536. spin_lock(&up->port.lock);
  537. /*
  538. * First save the IER then disable the interrupts
  539. */
  540. ier = serial_in(up, UART_IER);
  541. serial_out(up, UART_IER, UART_IER_UUE);
  542. uart_console_write(&up->port, s, count, serial_pxa_console_putchar);
  543. /*
  544. * Finally, wait for transmitter to become empty
  545. * and restore the IER
  546. */
  547. wait_for_xmitr(up);
  548. serial_out(up, UART_IER, ier);
  549. if (locked)
  550. spin_unlock(&up->port.lock);
  551. local_irq_restore(flags);
  552. clk_disable(up->clk);
  553. }
  554. #ifdef CONFIG_CONSOLE_POLL
  555. /*
  556. * Console polling routines for writing and reading from the uart while
  557. * in an interrupt or debug context.
  558. */
  559. static int serial_pxa_get_poll_char(struct uart_port *port)
  560. {
  561. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  562. unsigned char lsr = serial_in(up, UART_LSR);
  563. while (!(lsr & UART_LSR_DR))
  564. lsr = serial_in(up, UART_LSR);
  565. return serial_in(up, UART_RX);
  566. }
  567. static void serial_pxa_put_poll_char(struct uart_port *port,
  568. unsigned char c)
  569. {
  570. unsigned int ier;
  571. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  572. /*
  573. * First save the IER then disable the interrupts
  574. */
  575. ier = serial_in(up, UART_IER);
  576. serial_out(up, UART_IER, UART_IER_UUE);
  577. wait_for_xmitr(up);
  578. /*
  579. * Send the character out.
  580. */
  581. serial_out(up, UART_TX, c);
  582. /*
  583. * Finally, wait for transmitter to become empty
  584. * and restore the IER
  585. */
  586. wait_for_xmitr(up);
  587. serial_out(up, UART_IER, ier);
  588. }
  589. #endif /* CONFIG_CONSOLE_POLL */
  590. static int __init
  591. serial_pxa_console_setup(struct console *co, char *options)
  592. {
  593. struct uart_pxa_port *up;
  594. int baud = 9600;
  595. int bits = 8;
  596. int parity = 'n';
  597. int flow = 'n';
  598. if (co->index == -1 || co->index >= serial_pxa_reg.nr)
  599. co->index = 0;
  600. up = serial_pxa_ports[co->index];
  601. if (!up)
  602. return -ENODEV;
  603. if (options)
  604. uart_parse_options(options, &baud, &parity, &bits, &flow);
  605. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  606. }
  607. static struct console serial_pxa_console = {
  608. .name = "ttyS",
  609. .write = serial_pxa_console_write,
  610. .device = uart_console_device,
  611. .setup = serial_pxa_console_setup,
  612. .flags = CON_PRINTBUFFER,
  613. .index = -1,
  614. .data = &serial_pxa_reg,
  615. };
  616. #define PXA_CONSOLE &serial_pxa_console
  617. #else
  618. #define PXA_CONSOLE NULL
  619. #endif
  620. static const struct uart_ops serial_pxa_pops = {
  621. .tx_empty = serial_pxa_tx_empty,
  622. .set_mctrl = serial_pxa_set_mctrl,
  623. .get_mctrl = serial_pxa_get_mctrl,
  624. .stop_tx = serial_pxa_stop_tx,
  625. .start_tx = serial_pxa_start_tx,
  626. .stop_rx = serial_pxa_stop_rx,
  627. .enable_ms = serial_pxa_enable_ms,
  628. .break_ctl = serial_pxa_break_ctl,
  629. .startup = serial_pxa_startup,
  630. .shutdown = serial_pxa_shutdown,
  631. .set_termios = serial_pxa_set_termios,
  632. .pm = serial_pxa_pm,
  633. .type = serial_pxa_type,
  634. .release_port = serial_pxa_release_port,
  635. .request_port = serial_pxa_request_port,
  636. .config_port = serial_pxa_config_port,
  637. .verify_port = serial_pxa_verify_port,
  638. #if defined(CONFIG_CONSOLE_POLL) && defined(CONFIG_SERIAL_PXA_CONSOLE)
  639. .poll_get_char = serial_pxa_get_poll_char,
  640. .poll_put_char = serial_pxa_put_poll_char,
  641. #endif
  642. };
  643. static struct uart_driver serial_pxa_reg = {
  644. .owner = THIS_MODULE,
  645. .driver_name = "PXA serial",
  646. .dev_name = "ttyS",
  647. .major = TTY_MAJOR,
  648. .minor = 64,
  649. .nr = 4,
  650. .cons = PXA_CONSOLE,
  651. };
  652. #ifdef CONFIG_PM
  653. static int serial_pxa_suspend(struct device *dev)
  654. {
  655. struct uart_pxa_port *sport = dev_get_drvdata(dev);
  656. if (sport)
  657. uart_suspend_port(&serial_pxa_reg, &sport->port);
  658. return 0;
  659. }
  660. static int serial_pxa_resume(struct device *dev)
  661. {
  662. struct uart_pxa_port *sport = dev_get_drvdata(dev);
  663. if (sport)
  664. uart_resume_port(&serial_pxa_reg, &sport->port);
  665. return 0;
  666. }
  667. static const struct dev_pm_ops serial_pxa_pm_ops = {
  668. .suspend = serial_pxa_suspend,
  669. .resume = serial_pxa_resume,
  670. };
  671. #endif
  672. static const struct of_device_id serial_pxa_dt_ids[] = {
  673. { .compatible = "mrvl,pxa-uart", },
  674. { .compatible = "mrvl,mmp-uart", },
  675. {}
  676. };
  677. static int serial_pxa_probe_dt(struct platform_device *pdev,
  678. struct uart_pxa_port *sport)
  679. {
  680. struct device_node *np = pdev->dev.of_node;
  681. int ret;
  682. if (!np)
  683. return 1;
  684. ret = of_alias_get_id(np, "serial");
  685. if (ret < 0) {
  686. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  687. return ret;
  688. }
  689. sport->port.line = ret;
  690. return 0;
  691. }
  692. static int serial_pxa_probe(struct platform_device *dev)
  693. {
  694. struct uart_pxa_port *sport;
  695. struct resource *mmres;
  696. int ret;
  697. int irq;
  698. mmres = platform_get_resource(dev, IORESOURCE_MEM, 0);
  699. if (!mmres)
  700. return -ENODEV;
  701. irq = platform_get_irq(dev, 0);
  702. if (irq < 0)
  703. return irq;
  704. sport = kzalloc(sizeof(struct uart_pxa_port), GFP_KERNEL);
  705. if (!sport)
  706. return -ENOMEM;
  707. sport->clk = clk_get(&dev->dev, NULL);
  708. if (IS_ERR(sport->clk)) {
  709. ret = PTR_ERR(sport->clk);
  710. goto err_free;
  711. }
  712. ret = clk_prepare(sport->clk);
  713. if (ret) {
  714. clk_put(sport->clk);
  715. goto err_free;
  716. }
  717. sport->port.type = PORT_PXA;
  718. sport->port.iotype = UPIO_MEM;
  719. sport->port.mapbase = mmres->start;
  720. sport->port.irq = irq;
  721. sport->port.fifosize = 64;
  722. sport->port.ops = &serial_pxa_pops;
  723. sport->port.dev = &dev->dev;
  724. sport->port.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  725. sport->port.uartclk = clk_get_rate(sport->clk);
  726. sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_PXA_CONSOLE);
  727. ret = serial_pxa_probe_dt(dev, sport);
  728. if (ret > 0)
  729. sport->port.line = dev->id;
  730. else if (ret < 0)
  731. goto err_clk;
  732. if (sport->port.line >= ARRAY_SIZE(serial_pxa_ports)) {
  733. dev_err(&dev->dev, "serial%d out of range\n", sport->port.line);
  734. ret = -EINVAL;
  735. goto err_clk;
  736. }
  737. snprintf(sport->name, PXA_NAME_LEN - 1, "UART%d", sport->port.line + 1);
  738. sport->port.membase = ioremap(mmres->start, resource_size(mmres));
  739. if (!sport->port.membase) {
  740. ret = -ENOMEM;
  741. goto err_clk;
  742. }
  743. serial_pxa_ports[sport->port.line] = sport;
  744. uart_add_one_port(&serial_pxa_reg, &sport->port);
  745. platform_set_drvdata(dev, sport);
  746. return 0;
  747. err_clk:
  748. clk_unprepare(sport->clk);
  749. clk_put(sport->clk);
  750. err_free:
  751. kfree(sport);
  752. return ret;
  753. }
  754. static struct platform_driver serial_pxa_driver = {
  755. .probe = serial_pxa_probe,
  756. .driver = {
  757. .name = "pxa2xx-uart",
  758. #ifdef CONFIG_PM
  759. .pm = &serial_pxa_pm_ops,
  760. #endif
  761. .suppress_bind_attrs = true,
  762. .of_match_table = serial_pxa_dt_ids,
  763. },
  764. };
  765. /* 8250 driver for PXA serial ports should be used */
  766. static int __init serial_pxa_init(void)
  767. {
  768. int ret;
  769. ret = uart_register_driver(&serial_pxa_reg);
  770. if (ret != 0)
  771. return ret;
  772. ret = platform_driver_register(&serial_pxa_driver);
  773. if (ret != 0)
  774. uart_unregister_driver(&serial_pxa_reg);
  775. return ret;
  776. }
  777. device_initcall(serial_pxa_init);