amba-pl011.c 20 KB

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