amba-pl010.c 18 KB

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