amba-pl010.c 19 KB

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