amba-pl011.c 24 KB

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