amba-pl011.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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_PL011_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/clock.h>
  52. #include <asm/hardware/amba_serial.h>
  53. #define UART_NR 14
  54. #define SERIAL_AMBA_MAJOR 204
  55. #define SERIAL_AMBA_MINOR 64
  56. #define SERIAL_AMBA_NR UART_NR
  57. #define AMBA_ISR_PASS_LIMIT 256
  58. #define UART_DUMMY_RSR_RX 256
  59. /*
  60. * We wrap our port structure around the generic uart_port.
  61. */
  62. struct uart_amba_port {
  63. struct uart_port port;
  64. struct clk *clk;
  65. unsigned int im; /* interrupt mask */
  66. unsigned int old_status;
  67. };
  68. static void pl011_stop_tx(struct uart_port *port, unsigned int tty_stop)
  69. {
  70. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  71. uap->im &= ~UART011_TXIM;
  72. writew(uap->im, uap->port.membase + UART011_IMSC);
  73. }
  74. static void pl011_start_tx(struct uart_port *port, unsigned int tty_start)
  75. {
  76. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  77. uap->im |= UART011_TXIM;
  78. writew(uap->im, uap->port.membase + UART011_IMSC);
  79. }
  80. static void pl011_stop_rx(struct uart_port *port)
  81. {
  82. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  83. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  84. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  85. writew(uap->im, uap->port.membase + UART011_IMSC);
  86. }
  87. static void pl011_enable_ms(struct uart_port *port)
  88. {
  89. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  90. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  91. writew(uap->im, uap->port.membase + UART011_IMSC);
  92. }
  93. static void
  94. #ifdef SUPPORT_SYSRQ
  95. pl011_rx_chars(struct uart_amba_port *uap, struct pt_regs *regs)
  96. #else
  97. pl011_rx_chars(struct uart_amba_port *uap)
  98. #endif
  99. {
  100. struct tty_struct *tty = uap->port.info->tty;
  101. unsigned int status, ch, flag, rsr, max_count = 256;
  102. status = readw(uap->port.membase + UART01x_FR);
  103. while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
  104. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  105. if (tty->low_latency)
  106. tty_flip_buffer_push(tty);
  107. /*
  108. * If this failed then we will throw away the
  109. * bytes but must do so to clear interrupts
  110. */
  111. }
  112. ch = readw(uap->port.membase + UART01x_DR);
  113. flag = TTY_NORMAL;
  114. uap->port.icount.rx++;
  115. /*
  116. * Note that the error handling code is
  117. * out of the main execution path
  118. */
  119. rsr = readw(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
  120. if (unlikely(rsr & UART01x_RSR_ANY)) {
  121. if (rsr & UART01x_RSR_BE) {
  122. rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
  123. uap->port.icount.brk++;
  124. if (uart_handle_break(&uap->port))
  125. goto ignore_char;
  126. } else if (rsr & UART01x_RSR_PE)
  127. uap->port.icount.parity++;
  128. else if (rsr & UART01x_RSR_FE)
  129. uap->port.icount.frame++;
  130. if (rsr & UART01x_RSR_OE)
  131. uap->port.icount.overrun++;
  132. rsr &= uap->port.read_status_mask;
  133. if (rsr & UART01x_RSR_BE)
  134. flag = TTY_BREAK;
  135. else if (rsr & UART01x_RSR_PE)
  136. flag = TTY_PARITY;
  137. else if (rsr & UART01x_RSR_FE)
  138. flag = TTY_FRAME;
  139. }
  140. if (uart_handle_sysrq_char(&uap->port, ch, regs))
  141. goto ignore_char;
  142. if ((rsr & uap->port.ignore_status_mask) == 0) {
  143. tty_insert_flip_char(tty, ch, flag);
  144. }
  145. if ((rsr & UART01x_RSR_OE) &&
  146. tty->flip.count < TTY_FLIPBUF_SIZE) {
  147. /*
  148. * Overrun is special, since it's reported
  149. * immediately, and doesn't affect the current
  150. * character
  151. */
  152. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  153. }
  154. ignore_char:
  155. status = readw(uap->port.membase + UART01x_FR);
  156. }
  157. tty_flip_buffer_push(tty);
  158. return;
  159. }
  160. static void pl011_tx_chars(struct uart_amba_port *uap)
  161. {
  162. struct circ_buf *xmit = &uap->port.info->xmit;
  163. int count;
  164. if (uap->port.x_char) {
  165. writew(uap->port.x_char, uap->port.membase + UART01x_DR);
  166. uap->port.icount.tx++;
  167. uap->port.x_char = 0;
  168. return;
  169. }
  170. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  171. pl011_stop_tx(&uap->port, 0);
  172. return;
  173. }
  174. count = uap->port.fifosize >> 1;
  175. do {
  176. writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
  177. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  178. uap->port.icount.tx++;
  179. if (uart_circ_empty(xmit))
  180. break;
  181. } while (--count > 0);
  182. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  183. uart_write_wakeup(&uap->port);
  184. if (uart_circ_empty(xmit))
  185. pl011_stop_tx(&uap->port, 0);
  186. }
  187. static void pl011_modem_status(struct uart_amba_port *uap)
  188. {
  189. unsigned int status, delta;
  190. status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  191. delta = status ^ uap->old_status;
  192. uap->old_status = status;
  193. if (!delta)
  194. return;
  195. if (delta & UART01x_FR_DCD)
  196. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  197. if (delta & UART01x_FR_DSR)
  198. uap->port.icount.dsr++;
  199. if (delta & UART01x_FR_CTS)
  200. uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
  201. wake_up_interruptible(&uap->port.info->delta_msr_wait);
  202. }
  203. static irqreturn_t pl011_int(int irq, void *dev_id, struct pt_regs *regs)
  204. {
  205. struct uart_amba_port *uap = dev_id;
  206. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  207. int handled = 0;
  208. spin_lock(&uap->port.lock);
  209. status = readw(uap->port.membase + UART011_MIS);
  210. if (status) {
  211. do {
  212. writew(status & ~(UART011_TXIS|UART011_RTIS|
  213. UART011_RXIS),
  214. uap->port.membase + UART011_ICR);
  215. if (status & (UART011_RTIS|UART011_RXIS))
  216. #ifdef SUPPORT_SYSRQ
  217. pl011_rx_chars(uap, regs);
  218. #else
  219. pl011_rx_chars(uap);
  220. #endif
  221. if (status & (UART011_DSRMIS|UART011_DCDMIS|
  222. UART011_CTSMIS|UART011_RIMIS))
  223. pl011_modem_status(uap);
  224. if (status & UART011_TXIS)
  225. pl011_tx_chars(uap);
  226. if (pass_counter-- == 0)
  227. break;
  228. status = readw(uap->port.membase + UART011_MIS);
  229. } while (status != 0);
  230. handled = 1;
  231. }
  232. spin_unlock(&uap->port.lock);
  233. return IRQ_RETVAL(handled);
  234. }
  235. static unsigned int pl01x_tx_empty(struct uart_port *port)
  236. {
  237. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  238. unsigned int status = readw(uap->port.membase + UART01x_FR);
  239. return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
  240. }
  241. static unsigned int pl01x_get_mctrl(struct uart_port *port)
  242. {
  243. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  244. unsigned int result = 0;
  245. unsigned int status = readw(uap->port.membase + UART01x_FR);
  246. #define BIT(uartbit, tiocmbit) \
  247. if (status & uartbit) \
  248. result |= tiocmbit
  249. BIT(UART01x_FR_DCD, TIOCM_CAR);
  250. BIT(UART01x_FR_DSR, TIOCM_DSR);
  251. BIT(UART01x_FR_CTS, TIOCM_CTS);
  252. BIT(UART011_FR_RI, TIOCM_RNG);
  253. #undef BIT
  254. return result;
  255. }
  256. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  257. {
  258. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  259. unsigned int cr;
  260. cr = readw(uap->port.membase + UART011_CR);
  261. #define BIT(tiocmbit, uartbit) \
  262. if (mctrl & tiocmbit) \
  263. cr |= uartbit; \
  264. else \
  265. cr &= ~uartbit
  266. BIT(TIOCM_RTS, UART011_CR_RTS);
  267. BIT(TIOCM_DTR, UART011_CR_DTR);
  268. BIT(TIOCM_OUT1, UART011_CR_OUT1);
  269. BIT(TIOCM_OUT2, UART011_CR_OUT2);
  270. BIT(TIOCM_LOOP, UART011_CR_LBE);
  271. #undef BIT
  272. writew(cr, uap->port.membase + UART011_CR);
  273. }
  274. static void pl011_break_ctl(struct uart_port *port, int break_state)
  275. {
  276. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  277. unsigned long flags;
  278. unsigned int lcr_h;
  279. spin_lock_irqsave(&uap->port.lock, flags);
  280. lcr_h = readw(uap->port.membase + UART011_LCRH);
  281. if (break_state == -1)
  282. lcr_h |= UART01x_LCRH_BRK;
  283. else
  284. lcr_h &= ~UART01x_LCRH_BRK;
  285. writew(lcr_h, uap->port.membase + UART011_LCRH);
  286. spin_unlock_irqrestore(&uap->port.lock, flags);
  287. }
  288. static int pl011_startup(struct uart_port *port)
  289. {
  290. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  291. unsigned int cr;
  292. int retval;
  293. /*
  294. * Try to enable the clock producer.
  295. */
  296. retval = clk_enable(uap->clk);
  297. if (retval)
  298. goto out;
  299. uap->port.uartclk = clk_get_rate(uap->clk);
  300. /*
  301. * Allocate the IRQ
  302. */
  303. retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  304. if (retval)
  305. goto clk_dis;
  306. writew(UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
  307. uap->port.membase + UART011_IFLS);
  308. /*
  309. * Provoke TX FIFO interrupt into asserting.
  310. */
  311. cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
  312. writew(cr, uap->port.membase + UART011_CR);
  313. writew(0, uap->port.membase + UART011_FBRD);
  314. writew(1, uap->port.membase + UART011_IBRD);
  315. writew(0, uap->port.membase + UART011_LCRH);
  316. writew(0, uap->port.membase + UART01x_DR);
  317. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  318. barrier();
  319. cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  320. writew(cr, uap->port.membase + UART011_CR);
  321. /*
  322. * initialise the old status of the modem signals
  323. */
  324. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  325. /*
  326. * Finally, enable interrupts
  327. */
  328. spin_lock_irq(&uap->port.lock);
  329. uap->im = UART011_RXIM | UART011_RTIM;
  330. writew(uap->im, uap->port.membase + UART011_IMSC);
  331. spin_unlock_irq(&uap->port.lock);
  332. return 0;
  333. clk_dis:
  334. clk_disable(uap->clk);
  335. out:
  336. return retval;
  337. }
  338. static void pl011_shutdown(struct uart_port *port)
  339. {
  340. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  341. unsigned long val;
  342. /*
  343. * disable all interrupts
  344. */
  345. spin_lock_irq(&uap->port.lock);
  346. uap->im = 0;
  347. writew(uap->im, uap->port.membase + UART011_IMSC);
  348. writew(0xffff, uap->port.membase + UART011_ICR);
  349. spin_unlock_irq(&uap->port.lock);
  350. /*
  351. * Free the interrupt
  352. */
  353. free_irq(uap->port.irq, uap);
  354. /*
  355. * disable the port
  356. */
  357. writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
  358. /*
  359. * disable break condition and fifos
  360. */
  361. val = readw(uap->port.membase + UART011_LCRH);
  362. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  363. writew(val, uap->port.membase + UART011_LCRH);
  364. /*
  365. * Shut down the clock producer
  366. */
  367. clk_disable(uap->clk);
  368. }
  369. static void
  370. pl011_set_termios(struct uart_port *port, struct termios *termios,
  371. struct termios *old)
  372. {
  373. unsigned int lcr_h, old_cr;
  374. unsigned long flags;
  375. unsigned int baud, quot;
  376. /*
  377. * Ask the core to calculate the divisor for us.
  378. */
  379. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  380. quot = port->uartclk * 4 / baud;
  381. switch (termios->c_cflag & CSIZE) {
  382. case CS5:
  383. lcr_h = UART01x_LCRH_WLEN_5;
  384. break;
  385. case CS6:
  386. lcr_h = UART01x_LCRH_WLEN_6;
  387. break;
  388. case CS7:
  389. lcr_h = UART01x_LCRH_WLEN_7;
  390. break;
  391. default: // CS8
  392. lcr_h = UART01x_LCRH_WLEN_8;
  393. break;
  394. }
  395. if (termios->c_cflag & CSTOPB)
  396. lcr_h |= UART01x_LCRH_STP2;
  397. if (termios->c_cflag & PARENB) {
  398. lcr_h |= UART01x_LCRH_PEN;
  399. if (!(termios->c_cflag & PARODD))
  400. lcr_h |= UART01x_LCRH_EPS;
  401. }
  402. if (port->fifosize > 1)
  403. lcr_h |= UART01x_LCRH_FEN;
  404. spin_lock_irqsave(&port->lock, flags);
  405. /*
  406. * Update the per-port timeout.
  407. */
  408. uart_update_timeout(port, termios->c_cflag, baud);
  409. port->read_status_mask = UART01x_RSR_OE;
  410. if (termios->c_iflag & INPCK)
  411. port->read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
  412. if (termios->c_iflag & (BRKINT | PARMRK))
  413. port->read_status_mask |= UART01x_RSR_BE;
  414. /*
  415. * Characters to ignore
  416. */
  417. port->ignore_status_mask = 0;
  418. if (termios->c_iflag & IGNPAR)
  419. port->ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
  420. if (termios->c_iflag & IGNBRK) {
  421. port->ignore_status_mask |= UART01x_RSR_BE;
  422. /*
  423. * If we're ignoring parity and break indicators,
  424. * ignore overruns too (for real raw support).
  425. */
  426. if (termios->c_iflag & IGNPAR)
  427. port->ignore_status_mask |= UART01x_RSR_OE;
  428. }
  429. /*
  430. * Ignore all characters if CREAD is not set.
  431. */
  432. if ((termios->c_cflag & CREAD) == 0)
  433. port->ignore_status_mask |= UART_DUMMY_RSR_RX;
  434. if (UART_ENABLE_MS(port, termios->c_cflag))
  435. pl011_enable_ms(port);
  436. /* first, disable everything */
  437. old_cr = readw(port->membase + UART011_CR);
  438. writew(0, port->membase + UART011_CR);
  439. /* Set baud rate */
  440. writew(quot & 0x3f, port->membase + UART011_FBRD);
  441. writew(quot >> 6, port->membase + UART011_IBRD);
  442. /*
  443. * ----------v----------v----------v----------v-----
  444. * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
  445. * ----------^----------^----------^----------^-----
  446. */
  447. writew(lcr_h, port->membase + UART011_LCRH);
  448. writew(old_cr, port->membase + UART011_CR);
  449. spin_unlock_irqrestore(&port->lock, flags);
  450. }
  451. static const char *pl011_type(struct uart_port *port)
  452. {
  453. return port->type == PORT_AMBA ? "AMBA/PL011" : NULL;
  454. }
  455. /*
  456. * Release the memory region(s) being used by 'port'
  457. */
  458. static void pl010_release_port(struct uart_port *port)
  459. {
  460. release_mem_region(port->mapbase, SZ_4K);
  461. }
  462. /*
  463. * Request the memory region(s) being used by 'port'
  464. */
  465. static int pl010_request_port(struct uart_port *port)
  466. {
  467. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  468. != NULL ? 0 : -EBUSY;
  469. }
  470. /*
  471. * Configure/autoconfigure the port.
  472. */
  473. static void pl010_config_port(struct uart_port *port, int flags)
  474. {
  475. if (flags & UART_CONFIG_TYPE) {
  476. port->type = PORT_AMBA;
  477. pl010_request_port(port);
  478. }
  479. }
  480. /*
  481. * verify the new serial_struct (for TIOCSSERIAL).
  482. */
  483. static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
  484. {
  485. int ret = 0;
  486. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  487. ret = -EINVAL;
  488. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  489. ret = -EINVAL;
  490. if (ser->baud_base < 9600)
  491. ret = -EINVAL;
  492. return ret;
  493. }
  494. static struct uart_ops amba_pl011_pops = {
  495. .tx_empty = pl01x_tx_empty,
  496. .set_mctrl = pl011_set_mctrl,
  497. .get_mctrl = pl01x_get_mctrl,
  498. .stop_tx = pl011_stop_tx,
  499. .start_tx = pl011_start_tx,
  500. .stop_rx = pl011_stop_rx,
  501. .enable_ms = pl011_enable_ms,
  502. .break_ctl = pl011_break_ctl,
  503. .startup = pl011_startup,
  504. .shutdown = pl011_shutdown,
  505. .set_termios = pl011_set_termios,
  506. .type = pl011_type,
  507. .release_port = pl010_release_port,
  508. .request_port = pl010_request_port,
  509. .config_port = pl010_config_port,
  510. .verify_port = pl010_verify_port,
  511. };
  512. static struct uart_amba_port *amba_ports[UART_NR];
  513. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  514. static inline void
  515. pl011_console_write_char(struct uart_amba_port *uap, char ch)
  516. {
  517. unsigned int status;
  518. do {
  519. status = readw(uap->port.membase + UART01x_FR);
  520. } while (status & UART01x_FR_TXFF);
  521. writew(ch, uap->port.membase + UART01x_DR);
  522. }
  523. static void
  524. pl011_console_write(struct console *co, const char *s, unsigned int count)
  525. {
  526. struct uart_amba_port *uap = amba_ports[co->index];
  527. unsigned int status, old_cr, new_cr;
  528. int i;
  529. clk_enable(uap->clk);
  530. /*
  531. * First save the CR then disable the interrupts
  532. */
  533. old_cr = readw(uap->port.membase + UART011_CR);
  534. new_cr = old_cr & ~UART011_CR_CTSEN;
  535. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  536. writew(new_cr, uap->port.membase + UART011_CR);
  537. /*
  538. * Now, do each character
  539. */
  540. for (i = 0; i < count; i++) {
  541. pl011_console_write_char(uap, s[i]);
  542. if (s[i] == '\n')
  543. pl011_console_write_char(uap, '\r');
  544. }
  545. /*
  546. * Finally, wait for transmitter to become empty
  547. * and restore the TCR
  548. */
  549. do {
  550. status = readw(uap->port.membase + UART01x_FR);
  551. } while (status & UART01x_FR_BUSY);
  552. writew(old_cr, uap->port.membase + UART011_CR);
  553. clk_disable(uap->clk);
  554. }
  555. static void __init
  556. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  557. int *parity, int *bits)
  558. {
  559. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  560. unsigned int lcr_h, ibrd, fbrd;
  561. lcr_h = readw(uap->port.membase + UART011_LCRH);
  562. *parity = 'n';
  563. if (lcr_h & UART01x_LCRH_PEN) {
  564. if (lcr_h & UART01x_LCRH_EPS)
  565. *parity = 'e';
  566. else
  567. *parity = 'o';
  568. }
  569. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  570. *bits = 7;
  571. else
  572. *bits = 8;
  573. ibrd = readw(uap->port.membase + UART011_IBRD);
  574. fbrd = readw(uap->port.membase + UART011_FBRD);
  575. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  576. }
  577. }
  578. static int __init pl011_console_setup(struct console *co, char *options)
  579. {
  580. struct uart_amba_port *uap;
  581. int baud = 38400;
  582. int bits = 8;
  583. int parity = 'n';
  584. int flow = 'n';
  585. /*
  586. * Check whether an invalid uart number has been specified, and
  587. * if so, search for the first available port that does have
  588. * console support.
  589. */
  590. if (co->index >= UART_NR)
  591. co->index = 0;
  592. uap = amba_ports[co->index];
  593. uap->port.uartclk = clk_get_rate(uap->clk);
  594. if (options)
  595. uart_parse_options(options, &baud, &parity, &bits, &flow);
  596. else
  597. pl011_console_get_options(uap, &baud, &parity, &bits);
  598. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  599. }
  600. extern struct uart_driver amba_reg;
  601. static struct console amba_console = {
  602. .name = "ttyAMA",
  603. .write = pl011_console_write,
  604. .device = uart_console_device,
  605. .setup = pl011_console_setup,
  606. .flags = CON_PRINTBUFFER,
  607. .index = -1,
  608. .data = &amba_reg,
  609. };
  610. #define AMBA_CONSOLE (&amba_console)
  611. #else
  612. #define AMBA_CONSOLE NULL
  613. #endif
  614. static struct uart_driver amba_reg = {
  615. .owner = THIS_MODULE,
  616. .driver_name = "ttyAMA",
  617. .dev_name = "ttyAMA",
  618. .major = SERIAL_AMBA_MAJOR,
  619. .minor = SERIAL_AMBA_MINOR,
  620. .nr = UART_NR,
  621. .cons = AMBA_CONSOLE,
  622. };
  623. static int pl011_probe(struct amba_device *dev, void *id)
  624. {
  625. struct uart_amba_port *uap;
  626. void __iomem *base;
  627. int i, ret;
  628. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  629. if (amba_ports[i] == NULL)
  630. break;
  631. if (i == ARRAY_SIZE(amba_ports)) {
  632. ret = -EBUSY;
  633. goto out;
  634. }
  635. uap = kmalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
  636. if (uap == NULL) {
  637. ret = -ENOMEM;
  638. goto out;
  639. }
  640. base = ioremap(dev->res.start, PAGE_SIZE);
  641. if (!base) {
  642. ret = -ENOMEM;
  643. goto free;
  644. }
  645. memset(uap, 0, sizeof(struct uart_amba_port));
  646. uap->clk = clk_get(&dev->dev, "UARTCLK");
  647. if (IS_ERR(uap->clk)) {
  648. ret = PTR_ERR(uap->clk);
  649. goto unmap;
  650. }
  651. ret = clk_use(uap->clk);
  652. if (ret)
  653. goto putclk;
  654. uap->port.dev = &dev->dev;
  655. uap->port.mapbase = dev->res.start;
  656. uap->port.membase = base;
  657. uap->port.iotype = UPIO_MEM;
  658. uap->port.irq = dev->irq[0];
  659. uap->port.fifosize = 16;
  660. uap->port.ops = &amba_pl011_pops;
  661. uap->port.flags = UPF_BOOT_AUTOCONF;
  662. uap->port.line = i;
  663. amba_ports[i] = uap;
  664. amba_set_drvdata(dev, uap);
  665. ret = uart_add_one_port(&amba_reg, &uap->port);
  666. if (ret) {
  667. amba_set_drvdata(dev, NULL);
  668. amba_ports[i] = NULL;
  669. clk_unuse(uap->clk);
  670. putclk:
  671. clk_put(uap->clk);
  672. unmap:
  673. iounmap(base);
  674. free:
  675. kfree(uap);
  676. }
  677. out:
  678. return ret;
  679. }
  680. static int pl011_remove(struct amba_device *dev)
  681. {
  682. struct uart_amba_port *uap = amba_get_drvdata(dev);
  683. int i;
  684. amba_set_drvdata(dev, NULL);
  685. uart_remove_one_port(&amba_reg, &uap->port);
  686. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  687. if (amba_ports[i] == uap)
  688. amba_ports[i] = NULL;
  689. iounmap(uap->port.membase);
  690. clk_unuse(uap->clk);
  691. clk_put(uap->clk);
  692. kfree(uap);
  693. return 0;
  694. }
  695. static struct amba_id pl011_ids[] __initdata = {
  696. {
  697. .id = 0x00041011,
  698. .mask = 0x000fffff,
  699. },
  700. { 0, 0 },
  701. };
  702. static struct amba_driver pl011_driver = {
  703. .drv = {
  704. .name = "uart-pl011",
  705. },
  706. .id_table = pl011_ids,
  707. .probe = pl011_probe,
  708. .remove = pl011_remove,
  709. };
  710. static int __init pl011_init(void)
  711. {
  712. int ret;
  713. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  714. ret = uart_register_driver(&amba_reg);
  715. if (ret == 0) {
  716. ret = amba_driver_register(&pl011_driver);
  717. if (ret)
  718. uart_unregister_driver(&amba_reg);
  719. }
  720. return ret;
  721. }
  722. static void __exit pl011_exit(void)
  723. {
  724. amba_driver_unregister(&pl011_driver);
  725. uart_unregister_driver(&amba_reg);
  726. }
  727. module_init(pl011_init);
  728. module_exit(pl011_exit);
  729. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  730. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  731. MODULE_LICENSE("GPL");