pxa.c 23 KB

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