pxa.c 21 KB

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