amba-pl010.c 19 KB

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