amba-pl011.c 23 KB

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