amba-pl010.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * linux/drivers/char/amba.c
  3. *
  4. * Driver for AMBA serial ports
  5. *
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * Copyright 1999 ARM Limited
  9. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $
  26. *
  27. * This is a generic driver for ARM AMBA-type serial ports. They
  28. * have a lot of 16550-like features, but are not register compatible.
  29. * Note that although they do have CTS, DCD and DSR inputs, they do
  30. * not have an RI input, nor do they have DTR or RTS outputs. If
  31. * required, these have to be supplied via some other means (eg, GPIO)
  32. * and hooked into this driver.
  33. */
  34. #include <linux/config.h>
  35. #if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  36. #define SUPPORT_SYSRQ
  37. #endif
  38. #include <linux/module.h>
  39. #include <linux/ioport.h>
  40. #include <linux/init.h>
  41. #include <linux/console.h>
  42. #include <linux/sysrq.h>
  43. #include <linux/device.h>
  44. #include <linux/tty.h>
  45. #include <linux/tty_flip.h>
  46. #include <linux/serial_core.h>
  47. #include <linux/serial.h>
  48. #include <linux/amba/bus.h>
  49. #include <linux/amba/serial.h>
  50. #include <asm/io.h>
  51. #include <asm/irq.h>
  52. #include <asm/hardware.h>
  53. #define UART_NR 2
  54. #define SERIAL_AMBA_MAJOR 204
  55. #define SERIAL_AMBA_MINOR 16
  56. #define SERIAL_AMBA_NR UART_NR
  57. #define AMBA_ISR_PASS_LIMIT 256
  58. /*
  59. * Access macros for the AMBA UARTs
  60. */
  61. #define UART_GET_INT_STATUS(p) readb((p)->membase + UART010_IIR)
  62. #define UART_PUT_ICR(p, c) writel((c), (p)->membase + UART010_ICR)
  63. #define UART_GET_FR(p) readb((p)->membase + UART01x_FR)
  64. #define UART_GET_CHAR(p) readb((p)->membase + UART01x_DR)
  65. #define UART_PUT_CHAR(p, c) writel((c), (p)->membase + UART01x_DR)
  66. #define UART_GET_RSR(p) readb((p)->membase + UART01x_RSR)
  67. #define UART_GET_CR(p) readb((p)->membase + UART010_CR)
  68. #define UART_PUT_CR(p,c) writel((c), (p)->membase + UART010_CR)
  69. #define UART_GET_LCRL(p) readb((p)->membase + UART010_LCRL)
  70. #define UART_PUT_LCRL(p,c) writel((c), (p)->membase + UART010_LCRL)
  71. #define UART_GET_LCRM(p) readb((p)->membase + UART010_LCRM)
  72. #define UART_PUT_LCRM(p,c) writel((c), (p)->membase + UART010_LCRM)
  73. #define UART_GET_LCRH(p) readb((p)->membase + UART010_LCRH)
  74. #define UART_PUT_LCRH(p,c) writel((c), (p)->membase + UART010_LCRH)
  75. #define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
  76. #define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
  77. #define UART_TX_EMPTY(p) ((UART_GET_FR(p) & UART01x_FR_TMSK) == 0)
  78. #define UART_DUMMY_RSR_RX /*256*/0
  79. #define UART_PORT_SIZE 64
  80. /*
  81. * On the Integrator platform, the port RTS and DTR are provided by
  82. * bits in the following SC_CTRLS register bits:
  83. * RTS DTR
  84. * UART0 7 6
  85. * UART1 5 4
  86. */
  87. #define SC_CTRLC (IO_ADDRESS(INTEGRATOR_SC_BASE) + INTEGRATOR_SC_CTRLC_OFFSET)
  88. #define SC_CTRLS (IO_ADDRESS(INTEGRATOR_SC_BASE) + INTEGRATOR_SC_CTRLS_OFFSET)
  89. /*
  90. * We wrap our port structure around the generic uart_port.
  91. */
  92. struct uart_amba_port {
  93. struct uart_port port;
  94. unsigned int dtr_mask;
  95. unsigned int rts_mask;
  96. unsigned int old_status;
  97. };
  98. static void pl010_stop_tx(struct uart_port *port)
  99. {
  100. unsigned int cr;
  101. cr = UART_GET_CR(port);
  102. cr &= ~UART010_CR_TIE;
  103. UART_PUT_CR(port, cr);
  104. }
  105. static void pl010_start_tx(struct uart_port *port)
  106. {
  107. unsigned int cr;
  108. cr = UART_GET_CR(port);
  109. cr |= UART010_CR_TIE;
  110. UART_PUT_CR(port, cr);
  111. }
  112. static void pl010_stop_rx(struct uart_port *port)
  113. {
  114. unsigned int cr;
  115. cr = UART_GET_CR(port);
  116. cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
  117. UART_PUT_CR(port, cr);
  118. }
  119. static void pl010_enable_ms(struct uart_port *port)
  120. {
  121. unsigned int cr;
  122. cr = UART_GET_CR(port);
  123. cr |= UART010_CR_MSIE;
  124. UART_PUT_CR(port, cr);
  125. }
  126. static void
  127. #ifdef SUPPORT_SYSRQ
  128. pl010_rx_chars(struct uart_port *port, struct pt_regs *regs)
  129. #else
  130. pl010_rx_chars(struct uart_port *port)
  131. #endif
  132. {
  133. struct tty_struct *tty = port->info->tty;
  134. unsigned int status, ch, flag, rsr, max_count = 256;
  135. status = UART_GET_FR(port);
  136. while (UART_RX_DATA(status) && max_count--) {
  137. ch = UART_GET_CHAR(port);
  138. flag = TTY_NORMAL;
  139. port->icount.rx++;
  140. /*
  141. * Note that the error handling code is
  142. * out of the main execution path
  143. */
  144. rsr = UART_GET_RSR(port) | UART_DUMMY_RSR_RX;
  145. if (unlikely(rsr & UART01x_RSR_ANY)) {
  146. if (rsr & UART01x_RSR_BE) {
  147. rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
  148. port->icount.brk++;
  149. if (uart_handle_break(port))
  150. goto ignore_char;
  151. } else if (rsr & UART01x_RSR_PE)
  152. port->icount.parity++;
  153. else if (rsr & UART01x_RSR_FE)
  154. port->icount.frame++;
  155. if (rsr & UART01x_RSR_OE)
  156. port->icount.overrun++;
  157. rsr &= port->read_status_mask;
  158. if (rsr & UART01x_RSR_BE)
  159. flag = TTY_BREAK;
  160. else if (rsr & UART01x_RSR_PE)
  161. flag = TTY_PARITY;
  162. else if (rsr & UART01x_RSR_FE)
  163. flag = TTY_FRAME;
  164. }
  165. if (uart_handle_sysrq_char(port, ch, regs))
  166. goto ignore_char;
  167. uart_insert_char(port, rsr, UART01x_RSR_OE, ch, flag);
  168. ignore_char:
  169. status = UART_GET_FR(port);
  170. }
  171. tty_flip_buffer_push(tty);
  172. return;
  173. }
  174. static void pl010_tx_chars(struct uart_port *port)
  175. {
  176. struct circ_buf *xmit = &port->info->xmit;
  177. int count;
  178. if (port->x_char) {
  179. UART_PUT_CHAR(port, port->x_char);
  180. port->icount.tx++;
  181. port->x_char = 0;
  182. return;
  183. }
  184. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  185. pl010_stop_tx(port);
  186. return;
  187. }
  188. count = port->fifosize >> 1;
  189. do {
  190. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  191. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  192. port->icount.tx++;
  193. if (uart_circ_empty(xmit))
  194. break;
  195. } while (--count > 0);
  196. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  197. uart_write_wakeup(port);
  198. if (uart_circ_empty(xmit))
  199. pl010_stop_tx(port);
  200. }
  201. static void pl010_modem_status(struct uart_port *port)
  202. {
  203. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  204. unsigned int status, delta;
  205. UART_PUT_ICR(&uap->port, 0);
  206. status = UART_GET_FR(&uap->port) & UART01x_FR_MODEM_ANY;
  207. delta = status ^ uap->old_status;
  208. uap->old_status = status;
  209. if (!delta)
  210. return;
  211. if (delta & UART01x_FR_DCD)
  212. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  213. if (delta & UART01x_FR_DSR)
  214. uap->port.icount.dsr++;
  215. if (delta & UART01x_FR_CTS)
  216. uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
  217. wake_up_interruptible(&uap->port.info->delta_msr_wait);
  218. }
  219. static irqreturn_t pl010_int(int irq, void *dev_id, struct pt_regs *regs)
  220. {
  221. struct uart_port *port = dev_id;
  222. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  223. int handled = 0;
  224. spin_lock(&port->lock);
  225. status = UART_GET_INT_STATUS(port);
  226. if (status) {
  227. do {
  228. if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
  229. #ifdef SUPPORT_SYSRQ
  230. pl010_rx_chars(port, regs);
  231. #else
  232. pl010_rx_chars(port);
  233. #endif
  234. if (status & UART010_IIR_MIS)
  235. pl010_modem_status(port);
  236. if (status & UART010_IIR_TIS)
  237. pl010_tx_chars(port);
  238. if (pass_counter-- == 0)
  239. break;
  240. status = UART_GET_INT_STATUS(port);
  241. } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
  242. UART010_IIR_TIS));
  243. handled = 1;
  244. }
  245. spin_unlock(&port->lock);
  246. return IRQ_RETVAL(handled);
  247. }
  248. static unsigned int pl010_tx_empty(struct uart_port *port)
  249. {
  250. return UART_GET_FR(port) & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
  251. }
  252. static unsigned int pl010_get_mctrl(struct uart_port *port)
  253. {
  254. unsigned int result = 0;
  255. unsigned int status;
  256. status = UART_GET_FR(port);
  257. if (status & UART01x_FR_DCD)
  258. result |= TIOCM_CAR;
  259. if (status & UART01x_FR_DSR)
  260. result |= TIOCM_DSR;
  261. if (status & UART01x_FR_CTS)
  262. result |= TIOCM_CTS;
  263. return result;
  264. }
  265. static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
  266. {
  267. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  268. unsigned int ctrls = 0, ctrlc = 0;
  269. if (mctrl & TIOCM_RTS)
  270. ctrlc |= uap->rts_mask;
  271. else
  272. ctrls |= uap->rts_mask;
  273. if (mctrl & TIOCM_DTR)
  274. ctrlc |= uap->dtr_mask;
  275. else
  276. ctrls |= uap->dtr_mask;
  277. __raw_writel(ctrls, SC_CTRLS);
  278. __raw_writel(ctrlc, SC_CTRLC);
  279. }
  280. static void pl010_break_ctl(struct uart_port *port, int break_state)
  281. {
  282. unsigned long flags;
  283. unsigned int lcr_h;
  284. spin_lock_irqsave(&port->lock, flags);
  285. lcr_h = UART_GET_LCRH(port);
  286. if (break_state == -1)
  287. lcr_h |= UART01x_LCRH_BRK;
  288. else
  289. lcr_h &= ~UART01x_LCRH_BRK;
  290. UART_PUT_LCRH(port, lcr_h);
  291. spin_unlock_irqrestore(&port->lock, flags);
  292. }
  293. static int pl010_startup(struct uart_port *port)
  294. {
  295. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  296. int retval;
  297. /*
  298. * Allocate the IRQ
  299. */
  300. retval = request_irq(port->irq, pl010_int, 0, "uart-pl010", port);
  301. if (retval)
  302. return retval;
  303. /*
  304. * initialise the old status of the modem signals
  305. */
  306. uap->old_status = UART_GET_FR(port) & UART01x_FR_MODEM_ANY;
  307. /*
  308. * Finally, enable interrupts
  309. */
  310. UART_PUT_CR(port, UART01x_CR_UARTEN | UART010_CR_RIE |
  311. UART010_CR_RTIE);
  312. return 0;
  313. }
  314. static void pl010_shutdown(struct uart_port *port)
  315. {
  316. /*
  317. * Free the interrupt
  318. */
  319. free_irq(port->irq, port);
  320. /*
  321. * disable all interrupts, disable the port
  322. */
  323. UART_PUT_CR(port, 0);
  324. /* disable break condition and fifos */
  325. UART_PUT_LCRH(port, UART_GET_LCRH(port) &
  326. ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN));
  327. }
  328. static void
  329. pl010_set_termios(struct uart_port *port, struct termios *termios,
  330. struct termios *old)
  331. {
  332. unsigned int lcr_h, old_cr;
  333. unsigned long flags;
  334. unsigned int baud, quot;
  335. /*
  336. * Ask the core to calculate the divisor for us.
  337. */
  338. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  339. quot = uart_get_divisor(port, baud);
  340. switch (termios->c_cflag & CSIZE) {
  341. case CS5:
  342. lcr_h = UART01x_LCRH_WLEN_5;
  343. break;
  344. case CS6:
  345. lcr_h = UART01x_LCRH_WLEN_6;
  346. break;
  347. case CS7:
  348. lcr_h = UART01x_LCRH_WLEN_7;
  349. break;
  350. default: // CS8
  351. lcr_h = UART01x_LCRH_WLEN_8;
  352. break;
  353. }
  354. if (termios->c_cflag & CSTOPB)
  355. lcr_h |= UART01x_LCRH_STP2;
  356. if (termios->c_cflag & PARENB) {
  357. lcr_h |= UART01x_LCRH_PEN;
  358. if (!(termios->c_cflag & PARODD))
  359. lcr_h |= UART01x_LCRH_EPS;
  360. }
  361. if (port->fifosize > 1)
  362. lcr_h |= UART01x_LCRH_FEN;
  363. spin_lock_irqsave(&port->lock, flags);
  364. /*
  365. * Update the per-port timeout.
  366. */
  367. uart_update_timeout(port, termios->c_cflag, baud);
  368. port->read_status_mask = UART01x_RSR_OE;
  369. if (termios->c_iflag & INPCK)
  370. port->read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
  371. if (termios->c_iflag & (BRKINT | PARMRK))
  372. port->read_status_mask |= UART01x_RSR_BE;
  373. /*
  374. * Characters to ignore
  375. */
  376. port->ignore_status_mask = 0;
  377. if (termios->c_iflag & IGNPAR)
  378. port->ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
  379. if (termios->c_iflag & IGNBRK) {
  380. port->ignore_status_mask |= UART01x_RSR_BE;
  381. /*
  382. * If we're ignoring parity and break indicators,
  383. * ignore overruns too (for real raw support).
  384. */
  385. if (termios->c_iflag & IGNPAR)
  386. port->ignore_status_mask |= UART01x_RSR_OE;
  387. }
  388. /*
  389. * Ignore all characters if CREAD is not set.
  390. */
  391. if ((termios->c_cflag & CREAD) == 0)
  392. port->ignore_status_mask |= UART_DUMMY_RSR_RX;
  393. /* first, disable everything */
  394. old_cr = UART_GET_CR(port) & ~UART010_CR_MSIE;
  395. if (UART_ENABLE_MS(port, termios->c_cflag))
  396. old_cr |= UART010_CR_MSIE;
  397. UART_PUT_CR(port, 0);
  398. /* Set baud rate */
  399. quot -= 1;
  400. UART_PUT_LCRM(port, ((quot & 0xf00) >> 8));
  401. UART_PUT_LCRL(port, (quot & 0xff));
  402. /*
  403. * ----------v----------v----------v----------v-----
  404. * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
  405. * ----------^----------^----------^----------^-----
  406. */
  407. UART_PUT_LCRH(port, lcr_h);
  408. UART_PUT_CR(port, old_cr);
  409. spin_unlock_irqrestore(&port->lock, flags);
  410. }
  411. static const char *pl010_type(struct uart_port *port)
  412. {
  413. return port->type == PORT_AMBA ? "AMBA" : NULL;
  414. }
  415. /*
  416. * Release the memory region(s) being used by 'port'
  417. */
  418. static void pl010_release_port(struct uart_port *port)
  419. {
  420. release_mem_region(port->mapbase, UART_PORT_SIZE);
  421. }
  422. /*
  423. * Request the memory region(s) being used by 'port'
  424. */
  425. static int pl010_request_port(struct uart_port *port)
  426. {
  427. return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
  428. != NULL ? 0 : -EBUSY;
  429. }
  430. /*
  431. * Configure/autoconfigure the port.
  432. */
  433. static void pl010_config_port(struct uart_port *port, int flags)
  434. {
  435. if (flags & UART_CONFIG_TYPE) {
  436. port->type = PORT_AMBA;
  437. pl010_request_port(port);
  438. }
  439. }
  440. /*
  441. * verify the new serial_struct (for TIOCSSERIAL).
  442. */
  443. static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
  444. {
  445. int ret = 0;
  446. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  447. ret = -EINVAL;
  448. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  449. ret = -EINVAL;
  450. if (ser->baud_base < 9600)
  451. ret = -EINVAL;
  452. return ret;
  453. }
  454. static struct uart_ops amba_pl010_pops = {
  455. .tx_empty = pl010_tx_empty,
  456. .set_mctrl = pl010_set_mctrl,
  457. .get_mctrl = pl010_get_mctrl,
  458. .stop_tx = pl010_stop_tx,
  459. .start_tx = pl010_start_tx,
  460. .stop_rx = pl010_stop_rx,
  461. .enable_ms = pl010_enable_ms,
  462. .break_ctl = pl010_break_ctl,
  463. .startup = pl010_startup,
  464. .shutdown = pl010_shutdown,
  465. .set_termios = pl010_set_termios,
  466. .type = pl010_type,
  467. .release_port = pl010_release_port,
  468. .request_port = pl010_request_port,
  469. .config_port = pl010_config_port,
  470. .verify_port = pl010_verify_port,
  471. };
  472. static struct uart_amba_port amba_ports[UART_NR] = {
  473. {
  474. .port = {
  475. .membase = (void *)IO_ADDRESS(INTEGRATOR_UART0_BASE),
  476. .mapbase = INTEGRATOR_UART0_BASE,
  477. .iotype = SERIAL_IO_MEM,
  478. .irq = IRQ_UARTINT0,
  479. .uartclk = 14745600,
  480. .fifosize = 16,
  481. .ops = &amba_pl010_pops,
  482. .flags = UPF_BOOT_AUTOCONF,
  483. .line = 0,
  484. },
  485. .dtr_mask = 1 << 5,
  486. .rts_mask = 1 << 4,
  487. },
  488. {
  489. .port = {
  490. .membase = (void *)IO_ADDRESS(INTEGRATOR_UART1_BASE),
  491. .mapbase = INTEGRATOR_UART1_BASE,
  492. .iotype = SERIAL_IO_MEM,
  493. .irq = IRQ_UARTINT1,
  494. .uartclk = 14745600,
  495. .fifosize = 16,
  496. .ops = &amba_pl010_pops,
  497. .flags = UPF_BOOT_AUTOCONF,
  498. .line = 1,
  499. },
  500. .dtr_mask = 1 << 7,
  501. .rts_mask = 1 << 6,
  502. }
  503. };
  504. #ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
  505. static void
  506. pl010_console_write(struct console *co, const char *s, unsigned int count)
  507. {
  508. struct uart_port *port = &amba_ports[co->index].port;
  509. unsigned int status, old_cr;
  510. int i;
  511. /*
  512. * First save the CR then disable the interrupts
  513. */
  514. old_cr = UART_GET_CR(port);
  515. UART_PUT_CR(port, UART01x_CR_UARTEN);
  516. /*
  517. * Now, do each character
  518. */
  519. for (i = 0; i < count; i++) {
  520. do {
  521. status = UART_GET_FR(port);
  522. } while (!UART_TX_READY(status));
  523. UART_PUT_CHAR(port, s[i]);
  524. if (s[i] == '\n') {
  525. do {
  526. status = UART_GET_FR(port);
  527. } while (!UART_TX_READY(status));
  528. UART_PUT_CHAR(port, '\r');
  529. }
  530. }
  531. /*
  532. * Finally, wait for transmitter to become empty
  533. * and restore the TCR
  534. */
  535. do {
  536. status = UART_GET_FR(port);
  537. } while (status & UART01x_FR_BUSY);
  538. UART_PUT_CR(port, old_cr);
  539. }
  540. static void __init
  541. pl010_console_get_options(struct uart_port *port, int *baud,
  542. int *parity, int *bits)
  543. {
  544. if (UART_GET_CR(port) & UART01x_CR_UARTEN) {
  545. unsigned int lcr_h, quot;
  546. lcr_h = UART_GET_LCRH(port);
  547. *parity = 'n';
  548. if (lcr_h & UART01x_LCRH_PEN) {
  549. if (lcr_h & UART01x_LCRH_EPS)
  550. *parity = 'e';
  551. else
  552. *parity = 'o';
  553. }
  554. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  555. *bits = 7;
  556. else
  557. *bits = 8;
  558. quot = UART_GET_LCRL(port) | UART_GET_LCRM(port) << 8;
  559. *baud = port->uartclk / (16 * (quot + 1));
  560. }
  561. }
  562. static int __init pl010_console_setup(struct console *co, char *options)
  563. {
  564. struct uart_port *port;
  565. int baud = 38400;
  566. int bits = 8;
  567. int parity = 'n';
  568. int flow = 'n';
  569. /*
  570. * Check whether an invalid uart number has been specified, and
  571. * if so, search for the first available port that does have
  572. * console support.
  573. */
  574. if (co->index >= UART_NR)
  575. co->index = 0;
  576. port = &amba_ports[co->index].port;
  577. if (options)
  578. uart_parse_options(options, &baud, &parity, &bits, &flow);
  579. else
  580. pl010_console_get_options(port, &baud, &parity, &bits);
  581. return uart_set_options(port, co, baud, parity, bits, flow);
  582. }
  583. static struct uart_driver amba_reg;
  584. static struct console amba_console = {
  585. .name = "ttyAM",
  586. .write = pl010_console_write,
  587. .device = uart_console_device,
  588. .setup = pl010_console_setup,
  589. .flags = CON_PRINTBUFFER,
  590. .index = -1,
  591. .data = &amba_reg,
  592. };
  593. static int __init amba_console_init(void)
  594. {
  595. /*
  596. * All port initializations are done statically
  597. */
  598. register_console(&amba_console);
  599. return 0;
  600. }
  601. console_initcall(amba_console_init);
  602. static int __init amba_late_console_init(void)
  603. {
  604. if (!(amba_console.flags & CON_ENABLED))
  605. register_console(&amba_console);
  606. return 0;
  607. }
  608. late_initcall(amba_late_console_init);
  609. #define AMBA_CONSOLE &amba_console
  610. #else
  611. #define AMBA_CONSOLE NULL
  612. #endif
  613. static struct uart_driver amba_reg = {
  614. .owner = THIS_MODULE,
  615. .driver_name = "ttyAM",
  616. .dev_name = "ttyAM",
  617. .major = SERIAL_AMBA_MAJOR,
  618. .minor = SERIAL_AMBA_MINOR,
  619. .nr = UART_NR,
  620. .cons = AMBA_CONSOLE,
  621. };
  622. static int pl010_probe(struct amba_device *dev, void *id)
  623. {
  624. int i;
  625. for (i = 0; i < UART_NR; i++) {
  626. if (amba_ports[i].port.mapbase != dev->res.start)
  627. continue;
  628. amba_ports[i].port.dev = &dev->dev;
  629. uart_add_one_port(&amba_reg, &amba_ports[i].port);
  630. amba_set_drvdata(dev, &amba_ports[i]);
  631. break;
  632. }
  633. return 0;
  634. }
  635. static int pl010_remove(struct amba_device *dev)
  636. {
  637. struct uart_amba_port *uap = amba_get_drvdata(dev);
  638. if (uap)
  639. uart_remove_one_port(&amba_reg, &uap->port);
  640. amba_set_drvdata(dev, NULL);
  641. return 0;
  642. }
  643. static int pl010_suspend(struct amba_device *dev, pm_message_t state)
  644. {
  645. struct uart_amba_port *uap = amba_get_drvdata(dev);
  646. if (uap)
  647. uart_suspend_port(&amba_reg, &uap->port);
  648. return 0;
  649. }
  650. static int pl010_resume(struct amba_device *dev)
  651. {
  652. struct uart_amba_port *uap = amba_get_drvdata(dev);
  653. if (uap)
  654. uart_resume_port(&amba_reg, &uap->port);
  655. return 0;
  656. }
  657. static struct amba_id pl010_ids[] __initdata = {
  658. {
  659. .id = 0x00041010,
  660. .mask = 0x000fffff,
  661. },
  662. { 0, 0 },
  663. };
  664. static struct amba_driver pl010_driver = {
  665. .drv = {
  666. .name = "uart-pl010",
  667. },
  668. .id_table = pl010_ids,
  669. .probe = pl010_probe,
  670. .remove = pl010_remove,
  671. .suspend = pl010_suspend,
  672. .resume = pl010_resume,
  673. };
  674. static int __init pl010_init(void)
  675. {
  676. int ret;
  677. printk(KERN_INFO "Serial: AMBA driver $Revision: 1.41 $\n");
  678. ret = uart_register_driver(&amba_reg);
  679. if (ret == 0) {
  680. ret = amba_driver_register(&pl010_driver);
  681. if (ret)
  682. uart_unregister_driver(&amba_reg);
  683. }
  684. return ret;
  685. }
  686. static void __exit pl010_exit(void)
  687. {
  688. amba_driver_unregister(&pl010_driver);
  689. uart_unregister_driver(&amba_reg);
  690. }
  691. module_init(pl010_init);
  692. module_exit(pl010_exit);
  693. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  694. MODULE_DESCRIPTION("ARM AMBA serial port driver $Revision: 1.41 $");
  695. MODULE_LICENSE("GPL");