amba-pl010.c 19 KB

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