pxa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. u8 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_prepare_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. u8 ch;
  155. uart_port_tx_limited(&up->port, ch, up->port.fifosize / 2,
  156. true,
  157. serial_out(up, UART_TX, ch),
  158. ({}));
  159. }
  160. static void serial_pxa_start_tx(struct uart_port *port)
  161. {
  162. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  163. if (!(up->ier & UART_IER_THRI)) {
  164. up->ier |= UART_IER_THRI;
  165. serial_out(up, UART_IER, up->ier);
  166. }
  167. }
  168. /* should hold up->port.lock */
  169. static inline void check_modem_status(struct uart_pxa_port *up)
  170. {
  171. int status;
  172. status = serial_in(up, UART_MSR);
  173. if ((status & UART_MSR_ANY_DELTA) == 0)
  174. return;
  175. if (status & UART_MSR_TERI)
  176. up->port.icount.rng++;
  177. if (status & UART_MSR_DDSR)
  178. up->port.icount.dsr++;
  179. if (status & UART_MSR_DDCD)
  180. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  181. if (status & UART_MSR_DCTS)
  182. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  183. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  184. }
  185. /*
  186. * This handles the interrupt from one port.
  187. */
  188. static inline irqreturn_t serial_pxa_irq(int irq, void *dev_id)
  189. {
  190. struct uart_pxa_port *up = dev_id;
  191. unsigned int iir, lsr;
  192. iir = serial_in(up, UART_IIR);
  193. if (iir & UART_IIR_NO_INT)
  194. return IRQ_NONE;
  195. uart_port_lock(&up->port);
  196. lsr = serial_in(up, UART_LSR);
  197. if (lsr & UART_LSR_DR)
  198. receive_chars(up, &lsr);
  199. check_modem_status(up);
  200. if (lsr & UART_LSR_THRE)
  201. transmit_chars(up);
  202. uart_unlock_and_check_sysrq(&up->port);
  203. return IRQ_HANDLED;
  204. }
  205. static unsigned int serial_pxa_tx_empty(struct uart_port *port)
  206. {
  207. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  208. unsigned long flags;
  209. unsigned int ret;
  210. uart_port_lock_irqsave(&up->port, &flags);
  211. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  212. uart_port_unlock_irqrestore(&up->port, flags);
  213. return ret;
  214. }
  215. static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
  216. {
  217. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  218. unsigned char status;
  219. unsigned int ret;
  220. status = serial_in(up, UART_MSR);
  221. ret = 0;
  222. if (status & UART_MSR_DCD)
  223. ret |= TIOCM_CAR;
  224. if (status & UART_MSR_RI)
  225. ret |= TIOCM_RNG;
  226. if (status & UART_MSR_DSR)
  227. ret |= TIOCM_DSR;
  228. if (status & UART_MSR_CTS)
  229. ret |= TIOCM_CTS;
  230. return ret;
  231. }
  232. static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
  233. {
  234. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  235. unsigned char mcr = 0;
  236. if (mctrl & TIOCM_RTS)
  237. mcr |= UART_MCR_RTS;
  238. if (mctrl & TIOCM_DTR)
  239. mcr |= UART_MCR_DTR;
  240. if (mctrl & TIOCM_OUT1)
  241. mcr |= UART_MCR_OUT1;
  242. if (mctrl & TIOCM_OUT2)
  243. mcr |= UART_MCR_OUT2;
  244. if (mctrl & TIOCM_LOOP)
  245. mcr |= UART_MCR_LOOP;
  246. mcr |= up->mcr;
  247. serial_out(up, UART_MCR, mcr);
  248. }
  249. static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
  250. {
  251. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  252. unsigned long flags;
  253. uart_port_lock_irqsave(&up->port, &flags);
  254. if (break_state == -1)
  255. up->lcr |= UART_LCR_SBC;
  256. else
  257. up->lcr &= ~UART_LCR_SBC;
  258. serial_out(up, UART_LCR, up->lcr);
  259. uart_port_unlock_irqrestore(&up->port, flags);
  260. }
  261. static int serial_pxa_startup(struct uart_port *port)
  262. {
  263. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  264. unsigned long flags;
  265. int retval;
  266. if (port->line == 3) /* HWUART */
  267. up->mcr |= UART_MCR_AFE;
  268. else
  269. up->mcr = 0;
  270. up->port.uartclk = clk_get_rate(up->clk);
  271. /*
  272. * Allocate the IRQ
  273. */
  274. retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
  275. if (retval)
  276. return retval;
  277. /*
  278. * Clear the FIFO buffers and disable them.
  279. * (they will be reenabled in set_termios())
  280. */
  281. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  282. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  283. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  284. serial_out(up, UART_FCR, 0);
  285. /*
  286. * Clear the interrupt registers.
  287. */
  288. (void) serial_in(up, UART_LSR);
  289. (void) serial_in(up, UART_RX);
  290. (void) serial_in(up, UART_IIR);
  291. (void) serial_in(up, UART_MSR);
  292. /*
  293. * Now, initialize the UART
  294. */
  295. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  296. uart_port_lock_irqsave(&up->port, &flags);
  297. up->port.mctrl |= TIOCM_OUT2;
  298. serial_pxa_set_mctrl(&up->port, up->port.mctrl);
  299. uart_port_unlock_irqrestore(&up->port, flags);
  300. /*
  301. * Finally, enable interrupts. Note: Modem status interrupts
  302. * are set via set_termios(), which will be occurring imminently
  303. * anyway, so we don't enable them here.
  304. */
  305. up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
  306. serial_out(up, UART_IER, up->ier);
  307. /*
  308. * And clear the interrupt registers again for luck.
  309. */
  310. (void) serial_in(up, UART_LSR);
  311. (void) serial_in(up, UART_RX);
  312. (void) serial_in(up, UART_IIR);
  313. (void) serial_in(up, UART_MSR);
  314. return 0;
  315. }
  316. static void serial_pxa_shutdown(struct uart_port *port)
  317. {
  318. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  319. unsigned long flags;
  320. free_irq(up->port.irq, up);
  321. /*
  322. * Disable interrupts from this port
  323. */
  324. up->ier = 0;
  325. serial_out(up, UART_IER, 0);
  326. uart_port_lock_irqsave(&up->port, &flags);
  327. up->port.mctrl &= ~TIOCM_OUT2;
  328. serial_pxa_set_mctrl(&up->port, up->port.mctrl);
  329. uart_port_unlock_irqrestore(&up->port, flags);
  330. /*
  331. * Disable break condition and FIFOs
  332. */
  333. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  334. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  335. UART_FCR_CLEAR_RCVR |
  336. UART_FCR_CLEAR_XMIT);
  337. serial_out(up, UART_FCR, 0);
  338. }
  339. static void
  340. serial_pxa_set_termios(struct uart_port *port, struct ktermios *termios,
  341. const struct ktermios *old)
  342. {
  343. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  344. unsigned char cval, fcr = 0;
  345. unsigned long flags;
  346. unsigned int baud, quot;
  347. unsigned int dll;
  348. cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
  349. if (termios->c_cflag & CSTOPB)
  350. cval |= UART_LCR_STOP;
  351. if (termios->c_cflag & PARENB)
  352. cval |= UART_LCR_PARITY;
  353. if (!(termios->c_cflag & PARODD))
  354. cval |= UART_LCR_EPAR;
  355. /*
  356. * Ask the core to calculate the divisor for us.
  357. */
  358. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  359. quot = uart_get_divisor(port, baud);
  360. if ((up->port.uartclk / quot) < (2400 * 16))
  361. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
  362. else if ((up->port.uartclk / quot) < (230400 * 16))
  363. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
  364. else
  365. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR32;
  366. /*
  367. * Ok, we're now changing the port state. Do it with
  368. * interrupts disabled.
  369. */
  370. uart_port_lock_irqsave(&up->port, &flags);
  371. /*
  372. * Ensure the port will be enabled.
  373. * This is required especially for serial console.
  374. */
  375. up->ier |= UART_IER_UUE;
  376. /*
  377. * Update the per-port timeout.
  378. */
  379. uart_update_timeout(port, termios->c_cflag, baud);
  380. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  381. if (termios->c_iflag & INPCK)
  382. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  383. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  384. up->port.read_status_mask |= UART_LSR_BI;
  385. /*
  386. * Characters to ignore
  387. */
  388. up->port.ignore_status_mask = 0;
  389. if (termios->c_iflag & IGNPAR)
  390. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  391. if (termios->c_iflag & IGNBRK) {
  392. up->port.ignore_status_mask |= UART_LSR_BI;
  393. /*
  394. * If we're ignoring parity and break indicators,
  395. * ignore overruns too (for real raw support).
  396. */
  397. if (termios->c_iflag & IGNPAR)
  398. up->port.ignore_status_mask |= UART_LSR_OE;
  399. }
  400. /*
  401. * ignore all characters if CREAD is not set
  402. */
  403. if ((termios->c_cflag & CREAD) == 0)
  404. up->port.ignore_status_mask |= UART_LSR_DR;
  405. /*
  406. * CTS flow control flag and modem status interrupts
  407. */
  408. up->ier &= ~UART_IER_MSI;
  409. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  410. up->ier |= UART_IER_MSI;
  411. serial_out(up, UART_IER, up->ier);
  412. if (termios->c_cflag & CRTSCTS)
  413. up->mcr |= UART_MCR_AFE;
  414. else
  415. up->mcr &= ~UART_MCR_AFE;
  416. serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
  417. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  418. /*
  419. * work around Errata #75 according to Intel(R) PXA27x Processor Family
  420. * Specification Update (Nov 2005)
  421. */
  422. dll = serial_in(up, UART_DLL);
  423. WARN_ON(dll != (quot & 0xff));
  424. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  425. serial_out(up, UART_LCR, cval); /* reset DLAB */
  426. up->lcr = cval; /* Save LCR */
  427. serial_pxa_set_mctrl(&up->port, up->port.mctrl);
  428. serial_out(up, UART_FCR, fcr);
  429. uart_port_unlock_irqrestore(&up->port, flags);
  430. }
  431. static void
  432. serial_pxa_pm(struct uart_port *port, unsigned int state,
  433. unsigned int oldstate)
  434. {
  435. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  436. if (!state)
  437. clk_prepare_enable(up->clk);
  438. else
  439. clk_disable_unprepare(up->clk);
  440. }
  441. static void serial_pxa_release_port(struct uart_port *port)
  442. {
  443. }
  444. static int serial_pxa_request_port(struct uart_port *port)
  445. {
  446. return 0;
  447. }
  448. static void serial_pxa_config_port(struct uart_port *port, int flags)
  449. {
  450. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  451. up->port.type = PORT_PXA;
  452. }
  453. static int
  454. serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
  455. {
  456. /* we don't want the core code to modify any port params */
  457. return -EINVAL;
  458. }
  459. static const char *
  460. serial_pxa_type(struct uart_port *port)
  461. {
  462. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  463. return up->name;
  464. }
  465. static struct uart_pxa_port *serial_pxa_ports[4];
  466. static struct uart_driver serial_pxa_reg;
  467. #ifdef CONFIG_SERIAL_PXA_CONSOLE
  468. /*
  469. * Wait for transmitter & holding register to empty
  470. */
  471. static void wait_for_xmitr(struct uart_pxa_port *up)
  472. {
  473. unsigned int status, tmout = 10000;
  474. /* Wait up to 10ms for the character(s) to be sent. */
  475. do {
  476. status = serial_in(up, UART_LSR);
  477. if (status & UART_LSR_BI)
  478. up->lsr_break_flag = UART_LSR_BI;
  479. if (--tmout == 0)
  480. break;
  481. udelay(1);
  482. } while (!uart_lsr_tx_empty(status));
  483. /* Wait up to 1s for flow control if necessary */
  484. if (up->port.flags & UPF_CONS_FLOW) {
  485. tmout = 1000000;
  486. while (--tmout &&
  487. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  488. udelay(1);
  489. }
  490. }
  491. static void serial_pxa_console_putchar(struct uart_port *port, unsigned char ch)
  492. {
  493. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  494. wait_for_xmitr(up);
  495. serial_out(up, UART_TX, ch);
  496. }
  497. /*
  498. * Print a string to the serial port trying not to disturb
  499. * any possible real use of the port...
  500. *
  501. * The console_lock must be held when we get here.
  502. */
  503. static void
  504. serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
  505. {
  506. struct uart_pxa_port *up = serial_pxa_ports[co->index];
  507. unsigned int ier;
  508. unsigned long flags;
  509. int locked = 1;
  510. clk_enable(up->clk);
  511. if (oops_in_progress)
  512. locked = uart_port_trylock_irqsave(&up->port, &flags);
  513. else
  514. uart_port_lock_irqsave(&up->port, &flags);
  515. /*
  516. * First save the IER then disable the interrupts
  517. */
  518. ier = serial_in(up, UART_IER);
  519. serial_out(up, UART_IER, UART_IER_UUE);
  520. uart_console_write(&up->port, s, count, serial_pxa_console_putchar);
  521. /*
  522. * Finally, wait for transmitter to become empty
  523. * and restore the IER
  524. */
  525. wait_for_xmitr(up);
  526. serial_out(up, UART_IER, ier);
  527. if (locked)
  528. uart_port_unlock_irqrestore(&up->port, flags);
  529. clk_disable(up->clk);
  530. }
  531. #ifdef CONFIG_CONSOLE_POLL
  532. /*
  533. * Console polling routines for writing and reading from the uart while
  534. * in an interrupt or debug context.
  535. */
  536. static int serial_pxa_get_poll_char(struct uart_port *port)
  537. {
  538. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  539. unsigned char lsr = serial_in(up, UART_LSR);
  540. while (!(lsr & UART_LSR_DR))
  541. lsr = serial_in(up, UART_LSR);
  542. return serial_in(up, UART_RX);
  543. }
  544. static void serial_pxa_put_poll_char(struct uart_port *port,
  545. unsigned char c)
  546. {
  547. unsigned int ier;
  548. struct uart_pxa_port *up = (struct uart_pxa_port *)port;
  549. /*
  550. * First save the IER then disable the interrupts
  551. */
  552. ier = serial_in(up, UART_IER);
  553. serial_out(up, UART_IER, UART_IER_UUE);
  554. wait_for_xmitr(up);
  555. /*
  556. * Send the character out.
  557. */
  558. serial_out(up, UART_TX, c);
  559. /*
  560. * Finally, wait for transmitter to become empty
  561. * and restore the IER
  562. */
  563. wait_for_xmitr(up);
  564. serial_out(up, UART_IER, ier);
  565. }
  566. #endif /* CONFIG_CONSOLE_POLL */
  567. static int __init
  568. serial_pxa_console_setup(struct console *co, char *options)
  569. {
  570. struct uart_pxa_port *up;
  571. int baud = 9600;
  572. int bits = 8;
  573. int parity = 'n';
  574. int flow = 'n';
  575. if (co->index == -1 || co->index >= serial_pxa_reg.nr)
  576. co->index = 0;
  577. up = serial_pxa_ports[co->index];
  578. if (!up)
  579. return -ENODEV;
  580. if (options)
  581. uart_parse_options(options, &baud, &parity, &bits, &flow);
  582. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  583. }
  584. static struct console serial_pxa_console = {
  585. .name = "ttyS",
  586. .write = serial_pxa_console_write,
  587. .device = uart_console_device,
  588. .setup = serial_pxa_console_setup,
  589. .flags = CON_PRINTBUFFER,
  590. .index = -1,
  591. .data = &serial_pxa_reg,
  592. };
  593. #define PXA_CONSOLE &serial_pxa_console
  594. #else
  595. #define PXA_CONSOLE NULL
  596. #endif
  597. static const struct uart_ops serial_pxa_pops = {
  598. .tx_empty = serial_pxa_tx_empty,
  599. .set_mctrl = serial_pxa_set_mctrl,
  600. .get_mctrl = serial_pxa_get_mctrl,
  601. .stop_tx = serial_pxa_stop_tx,
  602. .start_tx = serial_pxa_start_tx,
  603. .stop_rx = serial_pxa_stop_rx,
  604. .enable_ms = serial_pxa_enable_ms,
  605. .break_ctl = serial_pxa_break_ctl,
  606. .startup = serial_pxa_startup,
  607. .shutdown = serial_pxa_shutdown,
  608. .set_termios = serial_pxa_set_termios,
  609. .pm = serial_pxa_pm,
  610. .type = serial_pxa_type,
  611. .release_port = serial_pxa_release_port,
  612. .request_port = serial_pxa_request_port,
  613. .config_port = serial_pxa_config_port,
  614. .verify_port = serial_pxa_verify_port,
  615. #if defined(CONFIG_CONSOLE_POLL) && defined(CONFIG_SERIAL_PXA_CONSOLE)
  616. .poll_get_char = serial_pxa_get_poll_char,
  617. .poll_put_char = serial_pxa_put_poll_char,
  618. #endif
  619. };
  620. static struct uart_driver serial_pxa_reg = {
  621. .owner = THIS_MODULE,
  622. .driver_name = "PXA serial",
  623. .dev_name = "ttyS",
  624. .major = TTY_MAJOR,
  625. .minor = 64,
  626. .nr = 4,
  627. .cons = PXA_CONSOLE,
  628. };
  629. #ifdef CONFIG_PM
  630. static int serial_pxa_suspend(struct device *dev)
  631. {
  632. struct uart_pxa_port *sport = dev_get_drvdata(dev);
  633. if (sport)
  634. uart_suspend_port(&serial_pxa_reg, &sport->port);
  635. return 0;
  636. }
  637. static int serial_pxa_resume(struct device *dev)
  638. {
  639. struct uart_pxa_port *sport = dev_get_drvdata(dev);
  640. if (sport)
  641. uart_resume_port(&serial_pxa_reg, &sport->port);
  642. return 0;
  643. }
  644. static const struct dev_pm_ops serial_pxa_pm_ops = {
  645. .suspend = serial_pxa_suspend,
  646. .resume = serial_pxa_resume,
  647. };
  648. #endif
  649. static const struct of_device_id serial_pxa_dt_ids[] = {
  650. { .compatible = "mrvl,pxa-uart", },
  651. { .compatible = "mrvl,mmp-uart", },
  652. {}
  653. };
  654. static int serial_pxa_probe_dt(struct platform_device *pdev,
  655. struct uart_pxa_port *sport)
  656. {
  657. struct device_node *np = pdev->dev.of_node;
  658. int ret;
  659. if (!np)
  660. return 1;
  661. ret = of_alias_get_id(np, "serial");
  662. if (ret < 0) {
  663. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  664. return ret;
  665. }
  666. sport->port.line = ret;
  667. return 0;
  668. }
  669. static int serial_pxa_probe(struct platform_device *dev)
  670. {
  671. struct uart_pxa_port *sport;
  672. struct resource *mmres;
  673. int ret;
  674. int irq;
  675. mmres = platform_get_resource(dev, IORESOURCE_MEM, 0);
  676. if (!mmres)
  677. return -ENODEV;
  678. irq = platform_get_irq(dev, 0);
  679. if (irq < 0)
  680. return irq;
  681. sport = kzalloc_obj(struct uart_pxa_port);
  682. if (!sport)
  683. return -ENOMEM;
  684. sport->clk = clk_get(&dev->dev, NULL);
  685. if (IS_ERR(sport->clk)) {
  686. ret = PTR_ERR(sport->clk);
  687. goto err_free;
  688. }
  689. ret = clk_prepare(sport->clk);
  690. if (ret) {
  691. clk_put(sport->clk);
  692. goto err_free;
  693. }
  694. sport->port.type = PORT_PXA;
  695. sport->port.iotype = UPIO_MEM;
  696. sport->port.mapbase = mmres->start;
  697. sport->port.irq = irq;
  698. sport->port.fifosize = 64;
  699. sport->port.ops = &serial_pxa_pops;
  700. sport->port.dev = &dev->dev;
  701. sport->port.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  702. sport->port.uartclk = clk_get_rate(sport->clk);
  703. sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_PXA_CONSOLE);
  704. ret = serial_pxa_probe_dt(dev, sport);
  705. if (ret > 0)
  706. sport->port.line = dev->id;
  707. else if (ret < 0)
  708. goto err_clk;
  709. if (sport->port.line >= ARRAY_SIZE(serial_pxa_ports)) {
  710. dev_err(&dev->dev, "serial%d out of range\n", sport->port.line);
  711. ret = -EINVAL;
  712. goto err_clk;
  713. }
  714. snprintf(sport->name, PXA_NAME_LEN - 1, "UART%d", sport->port.line + 1);
  715. sport->port.membase = ioremap(mmres->start, resource_size(mmres));
  716. if (!sport->port.membase) {
  717. ret = -ENOMEM;
  718. goto err_clk;
  719. }
  720. serial_pxa_ports[sport->port.line] = sport;
  721. uart_add_one_port(&serial_pxa_reg, &sport->port);
  722. platform_set_drvdata(dev, sport);
  723. return 0;
  724. err_clk:
  725. clk_unprepare(sport->clk);
  726. clk_put(sport->clk);
  727. err_free:
  728. kfree(sport);
  729. return ret;
  730. }
  731. static struct platform_driver serial_pxa_driver = {
  732. .probe = serial_pxa_probe,
  733. .driver = {
  734. .name = "pxa2xx-uart",
  735. #ifdef CONFIG_PM
  736. .pm = &serial_pxa_pm_ops,
  737. #endif
  738. .suppress_bind_attrs = true,
  739. .of_match_table = serial_pxa_dt_ids,
  740. },
  741. };
  742. /* 8250 driver for PXA serial ports should be used */
  743. static int __init serial_pxa_init(void)
  744. {
  745. int ret;
  746. ret = uart_register_driver(&serial_pxa_reg);
  747. if (ret != 0)
  748. return ret;
  749. ret = platform_driver_register(&serial_pxa_driver);
  750. if (ret != 0)
  751. uart_unregister_driver(&serial_pxa_reg);
  752. return ret;
  753. }
  754. device_initcall(serial_pxa_init);