amba-pl011.c 22 KB

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