pxa.c 21 KB

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