amba-pl010.c 19 KB

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