amba-pl010.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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_PL010_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. #define UART_NR 8
  50. #define SERIAL_AMBA_MAJOR 204
  51. #define SERIAL_AMBA_MINOR 16
  52. #define SERIAL_AMBA_NR UART_NR
  53. #define AMBA_ISR_PASS_LIMIT 256
  54. #define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
  55. #define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
  56. #define UART_DUMMY_RSR_RX 256
  57. #define UART_PORT_SIZE 64
  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. struct amba_device *dev;
  65. struct amba_pl010_data *data;
  66. unsigned int old_status;
  67. };
  68. static void pl010_stop_tx(struct uart_port *port)
  69. {
  70. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  71. unsigned int cr;
  72. cr = readb(uap->port.membase + UART010_CR);
  73. cr &= ~UART010_CR_TIE;
  74. writel(cr, uap->port.membase + UART010_CR);
  75. }
  76. static void pl010_start_tx(struct uart_port *port)
  77. {
  78. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  79. unsigned int cr;
  80. cr = readb(uap->port.membase + UART010_CR);
  81. cr |= UART010_CR_TIE;
  82. writel(cr, uap->port.membase + UART010_CR);
  83. }
  84. static void pl010_stop_rx(struct uart_port *port)
  85. {
  86. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  87. unsigned int cr;
  88. cr = readb(uap->port.membase + UART010_CR);
  89. cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
  90. writel(cr, uap->port.membase + UART010_CR);
  91. }
  92. static void pl010_enable_ms(struct uart_port *port)
  93. {
  94. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  95. unsigned int cr;
  96. cr = readb(uap->port.membase + UART010_CR);
  97. cr |= UART010_CR_MSIE;
  98. writel(cr, uap->port.membase + UART010_CR);
  99. }
  100. static void pl010_rx_chars(struct uart_amba_port *uap)
  101. {
  102. struct tty_struct *tty = uap->port.info->port.tty;
  103. unsigned int status, ch, flag, rsr, max_count = 256;
  104. status = readb(uap->port.membase + UART01x_FR);
  105. while (UART_RX_DATA(status) && max_count--) {
  106. ch = readb(uap->port.membase + UART01x_DR);
  107. flag = TTY_NORMAL;
  108. uap->port.icount.rx++;
  109. /*
  110. * Note that the error handling code is
  111. * out of the main execution path
  112. */
  113. rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
  114. if (unlikely(rsr & UART01x_RSR_ANY)) {
  115. writel(0, uap->port.membase + UART01x_ECR);
  116. if (rsr & UART01x_RSR_BE) {
  117. rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
  118. uap->port.icount.brk++;
  119. if (uart_handle_break(&uap->port))
  120. goto ignore_char;
  121. } else if (rsr & UART01x_RSR_PE)
  122. uap->port.icount.parity++;
  123. else if (rsr & UART01x_RSR_FE)
  124. uap->port.icount.frame++;
  125. if (rsr & UART01x_RSR_OE)
  126. uap->port.icount.overrun++;
  127. rsr &= uap->port.read_status_mask;
  128. if (rsr & UART01x_RSR_BE)
  129. flag = TTY_BREAK;
  130. else if (rsr & UART01x_RSR_PE)
  131. flag = TTY_PARITY;
  132. else if (rsr & UART01x_RSR_FE)
  133. flag = TTY_FRAME;
  134. }
  135. if (uart_handle_sysrq_char(&uap->port, ch))
  136. goto ignore_char;
  137. uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag);
  138. ignore_char:
  139. status = readb(uap->port.membase + UART01x_FR);
  140. }
  141. spin_unlock(&uap->port.lock);
  142. tty_flip_buffer_push(tty);
  143. spin_lock(&uap->port.lock);
  144. }
  145. static void pl010_tx_chars(struct uart_amba_port *uap)
  146. {
  147. struct circ_buf *xmit = &uap->port.info->xmit;
  148. int count;
  149. if (uap->port.x_char) {
  150. writel(uap->port.x_char, uap->port.membase + UART01x_DR);
  151. uap->port.icount.tx++;
  152. uap->port.x_char = 0;
  153. return;
  154. }
  155. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  156. pl010_stop_tx(&uap->port);
  157. return;
  158. }
  159. count = uap->port.fifosize >> 1;
  160. do {
  161. writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
  162. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  163. uap->port.icount.tx++;
  164. if (uart_circ_empty(xmit))
  165. break;
  166. } while (--count > 0);
  167. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  168. uart_write_wakeup(&uap->port);
  169. if (uart_circ_empty(xmit))
  170. pl010_stop_tx(&uap->port);
  171. }
  172. static void pl010_modem_status(struct uart_amba_port *uap)
  173. {
  174. unsigned int status, delta;
  175. writel(0, uap->port.membase + UART010_ICR);
  176. status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  177. delta = status ^ uap->old_status;
  178. uap->old_status = status;
  179. if (!delta)
  180. return;
  181. if (delta & UART01x_FR_DCD)
  182. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  183. if (delta & UART01x_FR_DSR)
  184. uap->port.icount.dsr++;
  185. if (delta & UART01x_FR_CTS)
  186. uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
  187. wake_up_interruptible(&uap->port.info->delta_msr_wait);
  188. }
  189. static irqreturn_t pl010_int(int irq, void *dev_id)
  190. {
  191. struct uart_amba_port *uap = dev_id;
  192. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  193. int handled = 0;
  194. spin_lock(&uap->port.lock);
  195. status = readb(uap->port.membase + UART010_IIR);
  196. if (status) {
  197. do {
  198. if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
  199. pl010_rx_chars(uap);
  200. if (status & UART010_IIR_MIS)
  201. pl010_modem_status(uap);
  202. if (status & UART010_IIR_TIS)
  203. pl010_tx_chars(uap);
  204. if (pass_counter-- == 0)
  205. break;
  206. status = readb(uap->port.membase + UART010_IIR);
  207. } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
  208. UART010_IIR_TIS));
  209. handled = 1;
  210. }
  211. spin_unlock(&uap->port.lock);
  212. return IRQ_RETVAL(handled);
  213. }
  214. static unsigned int pl010_tx_empty(struct uart_port *port)
  215. {
  216. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  217. unsigned int status = readb(uap->port.membase + UART01x_FR);
  218. return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
  219. }
  220. static unsigned int pl010_get_mctrl(struct uart_port *port)
  221. {
  222. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  223. unsigned int result = 0;
  224. unsigned int status;
  225. status = readb(uap->port.membase + UART01x_FR);
  226. if (status & UART01x_FR_DCD)
  227. result |= TIOCM_CAR;
  228. if (status & UART01x_FR_DSR)
  229. result |= TIOCM_DSR;
  230. if (status & UART01x_FR_CTS)
  231. result |= TIOCM_CTS;
  232. return result;
  233. }
  234. static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
  235. {
  236. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  237. if (uap->data)
  238. uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl);
  239. }
  240. static void pl010_break_ctl(struct uart_port *port, int break_state)
  241. {
  242. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  243. unsigned long flags;
  244. unsigned int lcr_h;
  245. spin_lock_irqsave(&uap->port.lock, flags);
  246. lcr_h = readb(uap->port.membase + UART010_LCRH);
  247. if (break_state == -1)
  248. lcr_h |= UART01x_LCRH_BRK;
  249. else
  250. lcr_h &= ~UART01x_LCRH_BRK;
  251. writel(lcr_h, uap->port.membase + UART010_LCRH);
  252. spin_unlock_irqrestore(&uap->port.lock, flags);
  253. }
  254. static int pl010_startup(struct uart_port *port)
  255. {
  256. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  257. int retval;
  258. /*
  259. * Try to enable the clock producer.
  260. */
  261. retval = clk_enable(uap->clk);
  262. if (retval)
  263. goto out;
  264. uap->port.uartclk = clk_get_rate(uap->clk);
  265. /*
  266. * Allocate the IRQ
  267. */
  268. retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap);
  269. if (retval)
  270. goto clk_dis;
  271. /*
  272. * initialise the old status of the modem signals
  273. */
  274. uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  275. /*
  276. * Finally, enable interrupts
  277. */
  278. writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
  279. uap->port.membase + UART010_CR);
  280. return 0;
  281. clk_dis:
  282. clk_disable(uap->clk);
  283. out:
  284. return retval;
  285. }
  286. static void pl010_shutdown(struct uart_port *port)
  287. {
  288. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  289. /*
  290. * Free the interrupt
  291. */
  292. free_irq(uap->port.irq, uap);
  293. /*
  294. * disable all interrupts, disable the port
  295. */
  296. writel(0, uap->port.membase + UART010_CR);
  297. /* disable break condition and fifos */
  298. writel(readb(uap->port.membase + UART010_LCRH) &
  299. ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
  300. uap->port.membase + UART010_LCRH);
  301. /*
  302. * Shut down the clock producer
  303. */
  304. clk_disable(uap->clk);
  305. }
  306. static void
  307. pl010_set_termios(struct uart_port *port, struct ktermios *termios,
  308. struct ktermios *old)
  309. {
  310. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  311. unsigned int lcr_h, old_cr;
  312. unsigned long flags;
  313. unsigned int baud, quot;
  314. /*
  315. * Ask the core to calculate the divisor for us.
  316. */
  317. baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16);
  318. quot = uart_get_divisor(port, baud);
  319. switch (termios->c_cflag & CSIZE) {
  320. case CS5:
  321. lcr_h = UART01x_LCRH_WLEN_5;
  322. break;
  323. case CS6:
  324. lcr_h = UART01x_LCRH_WLEN_6;
  325. break;
  326. case CS7:
  327. lcr_h = UART01x_LCRH_WLEN_7;
  328. break;
  329. default: // CS8
  330. lcr_h = UART01x_LCRH_WLEN_8;
  331. break;
  332. }
  333. if (termios->c_cflag & CSTOPB)
  334. lcr_h |= UART01x_LCRH_STP2;
  335. if (termios->c_cflag & PARENB) {
  336. lcr_h |= UART01x_LCRH_PEN;
  337. if (!(termios->c_cflag & PARODD))
  338. lcr_h |= UART01x_LCRH_EPS;
  339. }
  340. if (uap->port.fifosize > 1)
  341. lcr_h |= UART01x_LCRH_FEN;
  342. spin_lock_irqsave(&uap->port.lock, flags);
  343. /*
  344. * Update the per-port timeout.
  345. */
  346. uart_update_timeout(port, termios->c_cflag, baud);
  347. uap->port.read_status_mask = UART01x_RSR_OE;
  348. if (termios->c_iflag & INPCK)
  349. uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
  350. if (termios->c_iflag & (BRKINT | PARMRK))
  351. uap->port.read_status_mask |= UART01x_RSR_BE;
  352. /*
  353. * Characters to ignore
  354. */
  355. uap->port.ignore_status_mask = 0;
  356. if (termios->c_iflag & IGNPAR)
  357. uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
  358. if (termios->c_iflag & IGNBRK) {
  359. uap->port.ignore_status_mask |= UART01x_RSR_BE;
  360. /*
  361. * If we're ignoring parity and break indicators,
  362. * ignore overruns too (for real raw support).
  363. */
  364. if (termios->c_iflag & IGNPAR)
  365. uap->port.ignore_status_mask |= UART01x_RSR_OE;
  366. }
  367. /*
  368. * Ignore all characters if CREAD is not set.
  369. */
  370. if ((termios->c_cflag & CREAD) == 0)
  371. uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX;
  372. /* first, disable everything */
  373. old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE;
  374. if (UART_ENABLE_MS(port, termios->c_cflag))
  375. old_cr |= UART010_CR_MSIE;
  376. writel(0, uap->port.membase + UART010_CR);
  377. /* Set baud rate */
  378. quot -= 1;
  379. writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM);
  380. writel(quot & 0xff, uap->port.membase + UART010_LCRL);
  381. /*
  382. * ----------v----------v----------v----------v-----
  383. * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
  384. * ----------^----------^----------^----------^-----
  385. */
  386. writel(lcr_h, uap->port.membase + UART010_LCRH);
  387. writel(old_cr, uap->port.membase + UART010_CR);
  388. spin_unlock_irqrestore(&uap->port.lock, flags);
  389. }
  390. static const char *pl010_type(struct uart_port *port)
  391. {
  392. return port->type == PORT_AMBA ? "AMBA" : NULL;
  393. }
  394. /*
  395. * Release the memory region(s) being used by 'port'
  396. */
  397. static void pl010_release_port(struct uart_port *port)
  398. {
  399. release_mem_region(port->mapbase, UART_PORT_SIZE);
  400. }
  401. /*
  402. * Request the memory region(s) being used by 'port'
  403. */
  404. static int pl010_request_port(struct uart_port *port)
  405. {
  406. return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
  407. != NULL ? 0 : -EBUSY;
  408. }
  409. /*
  410. * Configure/autoconfigure the port.
  411. */
  412. static void pl010_config_port(struct uart_port *port, int flags)
  413. {
  414. if (flags & UART_CONFIG_TYPE) {
  415. port->type = PORT_AMBA;
  416. pl010_request_port(port);
  417. }
  418. }
  419. /*
  420. * verify the new serial_struct (for TIOCSSERIAL).
  421. */
  422. static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
  423. {
  424. int ret = 0;
  425. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  426. ret = -EINVAL;
  427. if (ser->irq < 0 || ser->irq >= nr_irqs)
  428. ret = -EINVAL;
  429. if (ser->baud_base < 9600)
  430. ret = -EINVAL;
  431. return ret;
  432. }
  433. static struct uart_ops amba_pl010_pops = {
  434. .tx_empty = pl010_tx_empty,
  435. .set_mctrl = pl010_set_mctrl,
  436. .get_mctrl = pl010_get_mctrl,
  437. .stop_tx = pl010_stop_tx,
  438. .start_tx = pl010_start_tx,
  439. .stop_rx = pl010_stop_rx,
  440. .enable_ms = pl010_enable_ms,
  441. .break_ctl = pl010_break_ctl,
  442. .startup = pl010_startup,
  443. .shutdown = pl010_shutdown,
  444. .set_termios = pl010_set_termios,
  445. .type = pl010_type,
  446. .release_port = pl010_release_port,
  447. .request_port = pl010_request_port,
  448. .config_port = pl010_config_port,
  449. .verify_port = pl010_verify_port,
  450. };
  451. static struct uart_amba_port *amba_ports[UART_NR];
  452. #ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
  453. static void pl010_console_putchar(struct uart_port *port, int ch)
  454. {
  455. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  456. unsigned int status;
  457. do {
  458. status = readb(uap->port.membase + UART01x_FR);
  459. barrier();
  460. } while (!UART_TX_READY(status));
  461. writel(ch, uap->port.membase + UART01x_DR);
  462. }
  463. static void
  464. pl010_console_write(struct console *co, const char *s, unsigned int count)
  465. {
  466. struct uart_amba_port *uap = amba_ports[co->index];
  467. unsigned int status, old_cr;
  468. clk_enable(uap->clk);
  469. /*
  470. * First save the CR then disable the interrupts
  471. */
  472. old_cr = readb(uap->port.membase + UART010_CR);
  473. writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
  474. uart_console_write(&uap->port, s, count, pl010_console_putchar);
  475. /*
  476. * Finally, wait for transmitter to become empty
  477. * and restore the TCR
  478. */
  479. do {
  480. status = readb(uap->port.membase + UART01x_FR);
  481. barrier();
  482. } while (status & UART01x_FR_BUSY);
  483. writel(old_cr, uap->port.membase + UART010_CR);
  484. clk_disable(uap->clk);
  485. }
  486. static void __init
  487. pl010_console_get_options(struct uart_amba_port *uap, int *baud,
  488. int *parity, int *bits)
  489. {
  490. if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
  491. unsigned int lcr_h, quot;
  492. lcr_h = readb(uap->port.membase + UART010_LCRH);
  493. *parity = 'n';
  494. if (lcr_h & UART01x_LCRH_PEN) {
  495. if (lcr_h & UART01x_LCRH_EPS)
  496. *parity = 'e';
  497. else
  498. *parity = 'o';
  499. }
  500. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  501. *bits = 7;
  502. else
  503. *bits = 8;
  504. quot = readb(uap->port.membase + UART010_LCRL) |
  505. readb(uap->port.membase + UART010_LCRM) << 8;
  506. *baud = uap->port.uartclk / (16 * (quot + 1));
  507. }
  508. }
  509. static int __init pl010_console_setup(struct console *co, char *options)
  510. {
  511. struct uart_amba_port *uap;
  512. int baud = 38400;
  513. int bits = 8;
  514. int parity = 'n';
  515. int flow = 'n';
  516. /*
  517. * Check whether an invalid uart number has been specified, and
  518. * if so, search for the first available port that does have
  519. * console support.
  520. */
  521. if (co->index >= UART_NR)
  522. co->index = 0;
  523. uap = amba_ports[co->index];
  524. if (!uap)
  525. return -ENODEV;
  526. uap->port.uartclk = clk_get_rate(uap->clk);
  527. if (options)
  528. uart_parse_options(options, &baud, &parity, &bits, &flow);
  529. else
  530. pl010_console_get_options(uap, &baud, &parity, &bits);
  531. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  532. }
  533. static struct uart_driver amba_reg;
  534. static struct console amba_console = {
  535. .name = "ttyAM",
  536. .write = pl010_console_write,
  537. .device = uart_console_device,
  538. .setup = pl010_console_setup,
  539. .flags = CON_PRINTBUFFER,
  540. .index = -1,
  541. .data = &amba_reg,
  542. };
  543. #define AMBA_CONSOLE &amba_console
  544. #else
  545. #define AMBA_CONSOLE NULL
  546. #endif
  547. static struct uart_driver amba_reg = {
  548. .owner = THIS_MODULE,
  549. .driver_name = "ttyAM",
  550. .dev_name = "ttyAM",
  551. .major = SERIAL_AMBA_MAJOR,
  552. .minor = SERIAL_AMBA_MINOR,
  553. .nr = UART_NR,
  554. .cons = AMBA_CONSOLE,
  555. };
  556. static int pl010_probe(struct amba_device *dev, struct amba_id *id)
  557. {
  558. struct uart_amba_port *uap;
  559. void __iomem *base;
  560. int i, ret;
  561. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  562. if (amba_ports[i] == NULL)
  563. break;
  564. if (i == ARRAY_SIZE(amba_ports)) {
  565. ret = -EBUSY;
  566. goto out;
  567. }
  568. uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
  569. if (!uap) {
  570. ret = -ENOMEM;
  571. goto out;
  572. }
  573. base = ioremap(dev->res.start, resource_size(&dev->res));
  574. if (!base) {
  575. ret = -ENOMEM;
  576. goto free;
  577. }
  578. uap->clk = clk_get(&dev->dev, NULL);
  579. if (IS_ERR(uap->clk)) {
  580. ret = PTR_ERR(uap->clk);
  581. goto unmap;
  582. }
  583. uap->port.dev = &dev->dev;
  584. uap->port.mapbase = dev->res.start;
  585. uap->port.membase = base;
  586. uap->port.iotype = UPIO_MEM;
  587. uap->port.irq = dev->irq[0];
  588. uap->port.fifosize = 16;
  589. uap->port.ops = &amba_pl010_pops;
  590. uap->port.flags = UPF_BOOT_AUTOCONF;
  591. uap->port.line = i;
  592. uap->dev = dev;
  593. uap->data = dev->dev.platform_data;
  594. amba_ports[i] = uap;
  595. amba_set_drvdata(dev, uap);
  596. ret = uart_add_one_port(&amba_reg, &uap->port);
  597. if (ret) {
  598. amba_set_drvdata(dev, NULL);
  599. amba_ports[i] = NULL;
  600. clk_put(uap->clk);
  601. unmap:
  602. iounmap(base);
  603. free:
  604. kfree(uap);
  605. }
  606. out:
  607. return ret;
  608. }
  609. static int pl010_remove(struct amba_device *dev)
  610. {
  611. struct uart_amba_port *uap = amba_get_drvdata(dev);
  612. int i;
  613. amba_set_drvdata(dev, NULL);
  614. uart_remove_one_port(&amba_reg, &uap->port);
  615. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  616. if (amba_ports[i] == uap)
  617. amba_ports[i] = NULL;
  618. iounmap(uap->port.membase);
  619. clk_put(uap->clk);
  620. kfree(uap);
  621. return 0;
  622. }
  623. static int pl010_suspend(struct amba_device *dev, pm_message_t state)
  624. {
  625. struct uart_amba_port *uap = amba_get_drvdata(dev);
  626. if (uap)
  627. uart_suspend_port(&amba_reg, &uap->port);
  628. return 0;
  629. }
  630. static int pl010_resume(struct amba_device *dev)
  631. {
  632. struct uart_amba_port *uap = amba_get_drvdata(dev);
  633. if (uap)
  634. uart_resume_port(&amba_reg, &uap->port);
  635. return 0;
  636. }
  637. static struct amba_id pl010_ids[] __initdata = {
  638. {
  639. .id = 0x00041010,
  640. .mask = 0x000fffff,
  641. },
  642. { 0, 0 },
  643. };
  644. static struct amba_driver pl010_driver = {
  645. .drv = {
  646. .name = "uart-pl010",
  647. },
  648. .id_table = pl010_ids,
  649. .probe = pl010_probe,
  650. .remove = pl010_remove,
  651. .suspend = pl010_suspend,
  652. .resume = pl010_resume,
  653. };
  654. static int __init pl010_init(void)
  655. {
  656. int ret;
  657. printk(KERN_INFO "Serial: AMBA driver\n");
  658. ret = uart_register_driver(&amba_reg);
  659. if (ret == 0) {
  660. ret = amba_driver_register(&pl010_driver);
  661. if (ret)
  662. uart_unregister_driver(&amba_reg);
  663. }
  664. return ret;
  665. }
  666. static void __exit pl010_exit(void)
  667. {
  668. amba_driver_unregister(&pl010_driver);
  669. uart_unregister_driver(&amba_reg);
  670. }
  671. module_init(pl010_init);
  672. module_exit(pl010_exit);
  673. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  674. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  675. MODULE_LICENSE("GPL");