pxa.c 23 KB

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