amba-pl011.c 20 KB

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