amba-pl011.c 20 KB

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