amba-pl010.c 19 KB

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