amba-pl010.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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.state->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.state->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.state->port.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 void pl010_set_ldisc(struct uart_port *port)
  391. {
  392. int line = port->line;
  393. if (line >= port->state->port.tty->driver->num)
  394. return;
  395. if (port->state->port.tty->ldisc->ops->num == N_PPS) {
  396. port->flags |= UPF_HARDPPS_CD;
  397. pl010_enable_ms(port);
  398. } else
  399. port->flags &= ~UPF_HARDPPS_CD;
  400. }
  401. static const char *pl010_type(struct uart_port *port)
  402. {
  403. return port->type == PORT_AMBA ? "AMBA" : NULL;
  404. }
  405. /*
  406. * Release the memory region(s) being used by 'port'
  407. */
  408. static void pl010_release_port(struct uart_port *port)
  409. {
  410. release_mem_region(port->mapbase, UART_PORT_SIZE);
  411. }
  412. /*
  413. * Request the memory region(s) being used by 'port'
  414. */
  415. static int pl010_request_port(struct uart_port *port)
  416. {
  417. return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
  418. != NULL ? 0 : -EBUSY;
  419. }
  420. /*
  421. * Configure/autoconfigure the port.
  422. */
  423. static void pl010_config_port(struct uart_port *port, int flags)
  424. {
  425. if (flags & UART_CONFIG_TYPE) {
  426. port->type = PORT_AMBA;
  427. pl010_request_port(port);
  428. }
  429. }
  430. /*
  431. * verify the new serial_struct (for TIOCSSERIAL).
  432. */
  433. static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
  434. {
  435. int ret = 0;
  436. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  437. ret = -EINVAL;
  438. if (ser->irq < 0 || ser->irq >= nr_irqs)
  439. ret = -EINVAL;
  440. if (ser->baud_base < 9600)
  441. ret = -EINVAL;
  442. return ret;
  443. }
  444. static struct uart_ops amba_pl010_pops = {
  445. .tx_empty = pl010_tx_empty,
  446. .set_mctrl = pl010_set_mctrl,
  447. .get_mctrl = pl010_get_mctrl,
  448. .stop_tx = pl010_stop_tx,
  449. .start_tx = pl010_start_tx,
  450. .stop_rx = pl010_stop_rx,
  451. .enable_ms = pl010_enable_ms,
  452. .break_ctl = pl010_break_ctl,
  453. .startup = pl010_startup,
  454. .shutdown = pl010_shutdown,
  455. .set_termios = pl010_set_termios,
  456. .set_ldisc = pl010_set_ldisc,
  457. .type = pl010_type,
  458. .release_port = pl010_release_port,
  459. .request_port = pl010_request_port,
  460. .config_port = pl010_config_port,
  461. .verify_port = pl010_verify_port,
  462. };
  463. static struct uart_amba_port *amba_ports[UART_NR];
  464. #ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
  465. static void pl010_console_putchar(struct uart_port *port, int ch)
  466. {
  467. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  468. unsigned int status;
  469. do {
  470. status = readb(uap->port.membase + UART01x_FR);
  471. barrier();
  472. } while (!UART_TX_READY(status));
  473. writel(ch, uap->port.membase + UART01x_DR);
  474. }
  475. static void
  476. pl010_console_write(struct console *co, const char *s, unsigned int count)
  477. {
  478. struct uart_amba_port *uap = amba_ports[co->index];
  479. unsigned int status, old_cr;
  480. clk_enable(uap->clk);
  481. /*
  482. * First save the CR then disable the interrupts
  483. */
  484. old_cr = readb(uap->port.membase + UART010_CR);
  485. writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
  486. uart_console_write(&uap->port, s, count, pl010_console_putchar);
  487. /*
  488. * Finally, wait for transmitter to become empty
  489. * and restore the TCR
  490. */
  491. do {
  492. status = readb(uap->port.membase + UART01x_FR);
  493. barrier();
  494. } while (status & UART01x_FR_BUSY);
  495. writel(old_cr, uap->port.membase + UART010_CR);
  496. clk_disable(uap->clk);
  497. }
  498. static void __init
  499. pl010_console_get_options(struct uart_amba_port *uap, int *baud,
  500. int *parity, int *bits)
  501. {
  502. if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
  503. unsigned int lcr_h, quot;
  504. lcr_h = readb(uap->port.membase + UART010_LCRH);
  505. *parity = 'n';
  506. if (lcr_h & UART01x_LCRH_PEN) {
  507. if (lcr_h & UART01x_LCRH_EPS)
  508. *parity = 'e';
  509. else
  510. *parity = 'o';
  511. }
  512. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  513. *bits = 7;
  514. else
  515. *bits = 8;
  516. quot = readb(uap->port.membase + UART010_LCRL) |
  517. readb(uap->port.membase + UART010_LCRM) << 8;
  518. *baud = uap->port.uartclk / (16 * (quot + 1));
  519. }
  520. }
  521. static int __init pl010_console_setup(struct console *co, char *options)
  522. {
  523. struct uart_amba_port *uap;
  524. int baud = 38400;
  525. int bits = 8;
  526. int parity = 'n';
  527. int flow = 'n';
  528. /*
  529. * Check whether an invalid uart number has been specified, and
  530. * if so, search for the first available port that does have
  531. * console support.
  532. */
  533. if (co->index >= UART_NR)
  534. co->index = 0;
  535. uap = amba_ports[co->index];
  536. if (!uap)
  537. return -ENODEV;
  538. uap->port.uartclk = clk_get_rate(uap->clk);
  539. if (options)
  540. uart_parse_options(options, &baud, &parity, &bits, &flow);
  541. else
  542. pl010_console_get_options(uap, &baud, &parity, &bits);
  543. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  544. }
  545. static struct uart_driver amba_reg;
  546. static struct console amba_console = {
  547. .name = "ttyAM",
  548. .write = pl010_console_write,
  549. .device = uart_console_device,
  550. .setup = pl010_console_setup,
  551. .flags = CON_PRINTBUFFER,
  552. .index = -1,
  553. .data = &amba_reg,
  554. };
  555. #define AMBA_CONSOLE &amba_console
  556. #else
  557. #define AMBA_CONSOLE NULL
  558. #endif
  559. static struct uart_driver amba_reg = {
  560. .owner = THIS_MODULE,
  561. .driver_name = "ttyAM",
  562. .dev_name = "ttyAM",
  563. .major = SERIAL_AMBA_MAJOR,
  564. .minor = SERIAL_AMBA_MINOR,
  565. .nr = UART_NR,
  566. .cons = AMBA_CONSOLE,
  567. };
  568. static int pl010_probe(struct amba_device *dev, struct amba_id *id)
  569. {
  570. struct uart_amba_port *uap;
  571. void __iomem *base;
  572. int i, ret;
  573. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  574. if (amba_ports[i] == NULL)
  575. break;
  576. if (i == ARRAY_SIZE(amba_ports)) {
  577. ret = -EBUSY;
  578. goto out;
  579. }
  580. uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
  581. if (!uap) {
  582. ret = -ENOMEM;
  583. goto out;
  584. }
  585. base = ioremap(dev->res.start, resource_size(&dev->res));
  586. if (!base) {
  587. ret = -ENOMEM;
  588. goto free;
  589. }
  590. uap->clk = clk_get(&dev->dev, NULL);
  591. if (IS_ERR(uap->clk)) {
  592. ret = PTR_ERR(uap->clk);
  593. goto unmap;
  594. }
  595. uap->port.dev = &dev->dev;
  596. uap->port.mapbase = dev->res.start;
  597. uap->port.membase = base;
  598. uap->port.iotype = UPIO_MEM;
  599. uap->port.irq = dev->irq[0];
  600. uap->port.fifosize = 16;
  601. uap->port.ops = &amba_pl010_pops;
  602. uap->port.flags = UPF_BOOT_AUTOCONF;
  603. uap->port.line = i;
  604. uap->dev = dev;
  605. uap->data = dev->dev.platform_data;
  606. amba_ports[i] = uap;
  607. amba_set_drvdata(dev, uap);
  608. ret = uart_add_one_port(&amba_reg, &uap->port);
  609. if (ret) {
  610. amba_set_drvdata(dev, NULL);
  611. amba_ports[i] = NULL;
  612. clk_put(uap->clk);
  613. unmap:
  614. iounmap(base);
  615. free:
  616. kfree(uap);
  617. }
  618. out:
  619. return ret;
  620. }
  621. static int pl010_remove(struct amba_device *dev)
  622. {
  623. struct uart_amba_port *uap = amba_get_drvdata(dev);
  624. int i;
  625. amba_set_drvdata(dev, NULL);
  626. uart_remove_one_port(&amba_reg, &uap->port);
  627. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  628. if (amba_ports[i] == uap)
  629. amba_ports[i] = NULL;
  630. iounmap(uap->port.membase);
  631. clk_put(uap->clk);
  632. kfree(uap);
  633. return 0;
  634. }
  635. static int pl010_suspend(struct amba_device *dev, pm_message_t state)
  636. {
  637. struct uart_amba_port *uap = amba_get_drvdata(dev);
  638. if (uap)
  639. uart_suspend_port(&amba_reg, &uap->port);
  640. return 0;
  641. }
  642. static int pl010_resume(struct amba_device *dev)
  643. {
  644. struct uart_amba_port *uap = amba_get_drvdata(dev);
  645. if (uap)
  646. uart_resume_port(&amba_reg, &uap->port);
  647. return 0;
  648. }
  649. static struct amba_id pl010_ids[] __initdata = {
  650. {
  651. .id = 0x00041010,
  652. .mask = 0x000fffff,
  653. },
  654. { 0, 0 },
  655. };
  656. static struct amba_driver pl010_driver = {
  657. .drv = {
  658. .name = "uart-pl010",
  659. },
  660. .id_table = pl010_ids,
  661. .probe = pl010_probe,
  662. .remove = pl010_remove,
  663. .suspend = pl010_suspend,
  664. .resume = pl010_resume,
  665. };
  666. static int __init pl010_init(void)
  667. {
  668. int ret;
  669. printk(KERN_INFO "Serial: AMBA driver\n");
  670. ret = uart_register_driver(&amba_reg);
  671. if (ret == 0) {
  672. ret = amba_driver_register(&pl010_driver);
  673. if (ret)
  674. uart_unregister_driver(&amba_reg);
  675. }
  676. return ret;
  677. }
  678. static void __exit pl010_exit(void)
  679. {
  680. amba_driver_unregister(&pl010_driver);
  681. uart_unregister_driver(&amba_reg);
  682. }
  683. module_init(pl010_init);
  684. module_exit(pl010_exit);
  685. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  686. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  687. MODULE_LICENSE("GPL");