amba-pl011.c 20 KB

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