pxa.c 20 KB

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