amba-pl010.c 19 KB

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