amba-pl011.c 21 KB

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