amba-pl011.c 20 KB

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