meson_uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on meson_uart.c, by AMLOGIC, INC.
  4. *
  5. * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
  6. */
  7. #if defined(CONFIG_SERIAL_MESON_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  8. #define SUPPORT_SYSRQ
  9. #endif
  10. #include <linux/clk.h>
  11. #include <linux/console.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/serial.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. /* Register offsets */
  24. #define AML_UART_WFIFO 0x00
  25. #define AML_UART_RFIFO 0x04
  26. #define AML_UART_CONTROL 0x08
  27. #define AML_UART_STATUS 0x0c
  28. #define AML_UART_MISC 0x10
  29. #define AML_UART_REG5 0x14
  30. /* AML_UART_CONTROL bits */
  31. #define AML_UART_TX_EN BIT(12)
  32. #define AML_UART_RX_EN BIT(13)
  33. #define AML_UART_TWO_WIRE_EN BIT(15)
  34. #define AML_UART_STOP_BIT_LEN_MASK (0x03 << 16)
  35. #define AML_UART_STOP_BIT_1SB (0x00 << 16)
  36. #define AML_UART_STOP_BIT_2SB (0x01 << 16)
  37. #define AML_UART_PARITY_TYPE BIT(18)
  38. #define AML_UART_PARITY_EN BIT(19)
  39. #define AML_UART_TX_RST BIT(22)
  40. #define AML_UART_RX_RST BIT(23)
  41. #define AML_UART_CLEAR_ERR BIT(24)
  42. #define AML_UART_RX_INT_EN BIT(27)
  43. #define AML_UART_TX_INT_EN BIT(28)
  44. #define AML_UART_DATA_LEN_MASK (0x03 << 20)
  45. #define AML_UART_DATA_LEN_8BIT (0x00 << 20)
  46. #define AML_UART_DATA_LEN_7BIT (0x01 << 20)
  47. #define AML_UART_DATA_LEN_6BIT (0x02 << 20)
  48. #define AML_UART_DATA_LEN_5BIT (0x03 << 20)
  49. /* AML_UART_STATUS bits */
  50. #define AML_UART_PARITY_ERR BIT(16)
  51. #define AML_UART_FRAME_ERR BIT(17)
  52. #define AML_UART_TX_FIFO_WERR BIT(18)
  53. #define AML_UART_RX_EMPTY BIT(20)
  54. #define AML_UART_TX_FULL BIT(21)
  55. #define AML_UART_TX_EMPTY BIT(22)
  56. #define AML_UART_XMIT_BUSY BIT(25)
  57. #define AML_UART_ERR (AML_UART_PARITY_ERR | \
  58. AML_UART_FRAME_ERR | \
  59. AML_UART_TX_FIFO_WERR)
  60. /* AML_UART_MISC bits */
  61. #define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8)
  62. #define AML_UART_RECV_IRQ(c) ((c) & 0xff)
  63. /* AML_UART_REG5 bits */
  64. #define AML_UART_BAUD_MASK 0x7fffff
  65. #define AML_UART_BAUD_USE BIT(23)
  66. #define AML_UART_BAUD_XTAL BIT(24)
  67. #define AML_UART_PORT_NUM 6
  68. #define AML_UART_DEV_NAME "ttyAML"
  69. static struct uart_driver meson_uart_driver;
  70. static struct uart_port *meson_ports[AML_UART_PORT_NUM];
  71. static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  72. {
  73. }
  74. static unsigned int meson_uart_get_mctrl(struct uart_port *port)
  75. {
  76. return TIOCM_CTS;
  77. }
  78. static unsigned int meson_uart_tx_empty(struct uart_port *port)
  79. {
  80. u32 val;
  81. val = readl(port->membase + AML_UART_STATUS);
  82. val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
  83. return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
  84. }
  85. static void meson_uart_stop_tx(struct uart_port *port)
  86. {
  87. u32 val;
  88. val = readl(port->membase + AML_UART_CONTROL);
  89. val &= ~AML_UART_TX_INT_EN;
  90. writel(val, port->membase + AML_UART_CONTROL);
  91. }
  92. static void meson_uart_stop_rx(struct uart_port *port)
  93. {
  94. u32 val;
  95. val = readl(port->membase + AML_UART_CONTROL);
  96. val &= ~AML_UART_RX_EN;
  97. writel(val, port->membase + AML_UART_CONTROL);
  98. }
  99. static void meson_uart_shutdown(struct uart_port *port)
  100. {
  101. unsigned long flags;
  102. u32 val;
  103. free_irq(port->irq, port);
  104. spin_lock_irqsave(&port->lock, flags);
  105. val = readl(port->membase + AML_UART_CONTROL);
  106. val &= ~AML_UART_RX_EN;
  107. val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
  108. writel(val, port->membase + AML_UART_CONTROL);
  109. spin_unlock_irqrestore(&port->lock, flags);
  110. }
  111. static void meson_uart_start_tx(struct uart_port *port)
  112. {
  113. struct circ_buf *xmit = &port->state->xmit;
  114. unsigned int ch;
  115. u32 val;
  116. if (uart_tx_stopped(port)) {
  117. meson_uart_stop_tx(port);
  118. return;
  119. }
  120. while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
  121. if (port->x_char) {
  122. writel(port->x_char, port->membase + AML_UART_WFIFO);
  123. port->icount.tx++;
  124. port->x_char = 0;
  125. continue;
  126. }
  127. if (uart_circ_empty(xmit))
  128. break;
  129. ch = xmit->buf[xmit->tail];
  130. writel(ch, port->membase + AML_UART_WFIFO);
  131. xmit->tail = (xmit->tail+1) & (SERIAL_XMIT_SIZE - 1);
  132. port->icount.tx++;
  133. }
  134. if (!uart_circ_empty(xmit)) {
  135. val = readl(port->membase + AML_UART_CONTROL);
  136. val |= AML_UART_TX_INT_EN;
  137. writel(val, port->membase + AML_UART_CONTROL);
  138. }
  139. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  140. uart_write_wakeup(port);
  141. }
  142. static void meson_receive_chars(struct uart_port *port)
  143. {
  144. struct tty_port *tport = &port->state->port;
  145. char flag;
  146. u32 ostatus, status, ch, mode;
  147. do {
  148. flag = TTY_NORMAL;
  149. port->icount.rx++;
  150. ostatus = status = readl(port->membase + AML_UART_STATUS);
  151. if (status & AML_UART_ERR) {
  152. if (status & AML_UART_TX_FIFO_WERR)
  153. port->icount.overrun++;
  154. else if (status & AML_UART_FRAME_ERR)
  155. port->icount.frame++;
  156. else if (status & AML_UART_PARITY_ERR)
  157. port->icount.frame++;
  158. mode = readl(port->membase + AML_UART_CONTROL);
  159. mode |= AML_UART_CLEAR_ERR;
  160. writel(mode, port->membase + AML_UART_CONTROL);
  161. /* It doesn't clear to 0 automatically */
  162. mode &= ~AML_UART_CLEAR_ERR;
  163. writel(mode, port->membase + AML_UART_CONTROL);
  164. status &= port->read_status_mask;
  165. if (status & AML_UART_FRAME_ERR)
  166. flag = TTY_FRAME;
  167. else if (status & AML_UART_PARITY_ERR)
  168. flag = TTY_PARITY;
  169. }
  170. ch = readl(port->membase + AML_UART_RFIFO);
  171. ch &= 0xff;
  172. if ((ostatus & AML_UART_FRAME_ERR) && (ch == 0)) {
  173. port->icount.brk++;
  174. flag = TTY_BREAK;
  175. if (uart_handle_break(port))
  176. continue;
  177. }
  178. if (uart_handle_sysrq_char(port, ch))
  179. continue;
  180. if ((status & port->ignore_status_mask) == 0)
  181. tty_insert_flip_char(tport, ch, flag);
  182. if (status & AML_UART_TX_FIFO_WERR)
  183. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  184. } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
  185. spin_unlock(&port->lock);
  186. tty_flip_buffer_push(tport);
  187. spin_lock(&port->lock);
  188. }
  189. static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
  190. {
  191. struct uart_port *port = (struct uart_port *)dev_id;
  192. spin_lock(&port->lock);
  193. if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
  194. meson_receive_chars(port);
  195. if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
  196. if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
  197. meson_uart_start_tx(port);
  198. }
  199. spin_unlock(&port->lock);
  200. return IRQ_HANDLED;
  201. }
  202. static const char *meson_uart_type(struct uart_port *port)
  203. {
  204. return (port->type == PORT_MESON) ? "meson_uart" : NULL;
  205. }
  206. static void meson_uart_reset(struct uart_port *port)
  207. {
  208. u32 val;
  209. val = readl(port->membase + AML_UART_CONTROL);
  210. val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
  211. writel(val, port->membase + AML_UART_CONTROL);
  212. val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
  213. writel(val, port->membase + AML_UART_CONTROL);
  214. }
  215. static int meson_uart_startup(struct uart_port *port)
  216. {
  217. u32 val;
  218. int ret = 0;
  219. val = readl(port->membase + AML_UART_CONTROL);
  220. val |= AML_UART_CLEAR_ERR;
  221. writel(val, port->membase + AML_UART_CONTROL);
  222. val &= ~AML_UART_CLEAR_ERR;
  223. writel(val, port->membase + AML_UART_CONTROL);
  224. val |= (AML_UART_RX_EN | AML_UART_TX_EN);
  225. writel(val, port->membase + AML_UART_CONTROL);
  226. val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
  227. writel(val, port->membase + AML_UART_CONTROL);
  228. val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
  229. writel(val, port->membase + AML_UART_MISC);
  230. ret = request_irq(port->irq, meson_uart_interrupt, 0,
  231. port->name, port);
  232. return ret;
  233. }
  234. static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
  235. {
  236. u32 val;
  237. while (!meson_uart_tx_empty(port))
  238. cpu_relax();
  239. if (port->uartclk == 24000000) {
  240. val = ((port->uartclk / 3) / baud) - 1;
  241. val |= AML_UART_BAUD_XTAL;
  242. } else {
  243. val = ((port->uartclk * 10 / (baud * 4) + 5) / 10) - 1;
  244. }
  245. val |= AML_UART_BAUD_USE;
  246. writel(val, port->membase + AML_UART_REG5);
  247. }
  248. static void meson_uart_set_termios(struct uart_port *port,
  249. struct ktermios *termios,
  250. struct ktermios *old)
  251. {
  252. unsigned int cflags, iflags, baud;
  253. unsigned long flags;
  254. u32 val;
  255. spin_lock_irqsave(&port->lock, flags);
  256. cflags = termios->c_cflag;
  257. iflags = termios->c_iflag;
  258. val = readl(port->membase + AML_UART_CONTROL);
  259. val &= ~AML_UART_DATA_LEN_MASK;
  260. switch (cflags & CSIZE) {
  261. case CS8:
  262. val |= AML_UART_DATA_LEN_8BIT;
  263. break;
  264. case CS7:
  265. val |= AML_UART_DATA_LEN_7BIT;
  266. break;
  267. case CS6:
  268. val |= AML_UART_DATA_LEN_6BIT;
  269. break;
  270. case CS5:
  271. val |= AML_UART_DATA_LEN_5BIT;
  272. break;
  273. }
  274. if (cflags & PARENB)
  275. val |= AML_UART_PARITY_EN;
  276. else
  277. val &= ~AML_UART_PARITY_EN;
  278. if (cflags & PARODD)
  279. val |= AML_UART_PARITY_TYPE;
  280. else
  281. val &= ~AML_UART_PARITY_TYPE;
  282. val &= ~AML_UART_STOP_BIT_LEN_MASK;
  283. if (cflags & CSTOPB)
  284. val |= AML_UART_STOP_BIT_2SB;
  285. else
  286. val |= AML_UART_STOP_BIT_1SB;
  287. if (cflags & CRTSCTS)
  288. val &= ~AML_UART_TWO_WIRE_EN;
  289. else
  290. val |= AML_UART_TWO_WIRE_EN;
  291. writel(val, port->membase + AML_UART_CONTROL);
  292. baud = uart_get_baud_rate(port, termios, old, 50, 4000000);
  293. meson_uart_change_speed(port, baud);
  294. port->read_status_mask = AML_UART_TX_FIFO_WERR;
  295. if (iflags & INPCK)
  296. port->read_status_mask |= AML_UART_PARITY_ERR |
  297. AML_UART_FRAME_ERR;
  298. port->ignore_status_mask = 0;
  299. if (iflags & IGNPAR)
  300. port->ignore_status_mask |= AML_UART_PARITY_ERR |
  301. AML_UART_FRAME_ERR;
  302. uart_update_timeout(port, termios->c_cflag, baud);
  303. spin_unlock_irqrestore(&port->lock, flags);
  304. }
  305. static int meson_uart_verify_port(struct uart_port *port,
  306. struct serial_struct *ser)
  307. {
  308. int ret = 0;
  309. if (port->type != PORT_MESON)
  310. ret = -EINVAL;
  311. if (port->irq != ser->irq)
  312. ret = -EINVAL;
  313. if (ser->baud_base < 9600)
  314. ret = -EINVAL;
  315. return ret;
  316. }
  317. static void meson_uart_release_port(struct uart_port *port)
  318. {
  319. devm_iounmap(port->dev, port->membase);
  320. port->membase = NULL;
  321. devm_release_mem_region(port->dev, port->mapbase, port->mapsize);
  322. }
  323. static int meson_uart_request_port(struct uart_port *port)
  324. {
  325. if (!devm_request_mem_region(port->dev, port->mapbase, port->mapsize,
  326. dev_name(port->dev))) {
  327. dev_err(port->dev, "Memory region busy\n");
  328. return -EBUSY;
  329. }
  330. port->membase = devm_ioremap_nocache(port->dev, port->mapbase,
  331. port->mapsize);
  332. if (!port->membase)
  333. return -ENOMEM;
  334. return 0;
  335. }
  336. static void meson_uart_config_port(struct uart_port *port, int flags)
  337. {
  338. if (flags & UART_CONFIG_TYPE) {
  339. port->type = PORT_MESON;
  340. meson_uart_request_port(port);
  341. }
  342. }
  343. static const struct uart_ops meson_uart_ops = {
  344. .set_mctrl = meson_uart_set_mctrl,
  345. .get_mctrl = meson_uart_get_mctrl,
  346. .tx_empty = meson_uart_tx_empty,
  347. .start_tx = meson_uart_start_tx,
  348. .stop_tx = meson_uart_stop_tx,
  349. .stop_rx = meson_uart_stop_rx,
  350. .startup = meson_uart_startup,
  351. .shutdown = meson_uart_shutdown,
  352. .set_termios = meson_uart_set_termios,
  353. .type = meson_uart_type,
  354. .config_port = meson_uart_config_port,
  355. .request_port = meson_uart_request_port,
  356. .release_port = meson_uart_release_port,
  357. .verify_port = meson_uart_verify_port,
  358. };
  359. #ifdef CONFIG_SERIAL_MESON_CONSOLE
  360. static void meson_uart_enable_tx_engine(struct uart_port *port)
  361. {
  362. u32 val;
  363. val = readl(port->membase + AML_UART_CONTROL);
  364. val |= AML_UART_TX_EN;
  365. writel(val, port->membase + AML_UART_CONTROL);
  366. }
  367. static void meson_console_putchar(struct uart_port *port, int ch)
  368. {
  369. if (!port->membase)
  370. return;
  371. while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
  372. cpu_relax();
  373. writel(ch, port->membase + AML_UART_WFIFO);
  374. }
  375. static void meson_serial_port_write(struct uart_port *port, const char *s,
  376. u_int count)
  377. {
  378. unsigned long flags;
  379. int locked;
  380. u32 val, tmp;
  381. local_irq_save(flags);
  382. if (port->sysrq) {
  383. locked = 0;
  384. } else if (oops_in_progress) {
  385. locked = spin_trylock(&port->lock);
  386. } else {
  387. spin_lock(&port->lock);
  388. locked = 1;
  389. }
  390. val = readl(port->membase + AML_UART_CONTROL);
  391. tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
  392. writel(tmp, port->membase + AML_UART_CONTROL);
  393. uart_console_write(port, s, count, meson_console_putchar);
  394. writel(val, port->membase + AML_UART_CONTROL);
  395. if (locked)
  396. spin_unlock(&port->lock);
  397. local_irq_restore(flags);
  398. }
  399. static void meson_serial_console_write(struct console *co, const char *s,
  400. u_int count)
  401. {
  402. struct uart_port *port;
  403. port = meson_ports[co->index];
  404. if (!port)
  405. return;
  406. meson_serial_port_write(port, s, count);
  407. }
  408. static int meson_serial_console_setup(struct console *co, char *options)
  409. {
  410. struct uart_port *port;
  411. int baud = 115200;
  412. int bits = 8;
  413. int parity = 'n';
  414. int flow = 'n';
  415. if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
  416. return -EINVAL;
  417. port = meson_ports[co->index];
  418. if (!port || !port->membase)
  419. return -ENODEV;
  420. meson_uart_enable_tx_engine(port);
  421. if (options)
  422. uart_parse_options(options, &baud, &parity, &bits, &flow);
  423. return uart_set_options(port, co, baud, parity, bits, flow);
  424. }
  425. static struct console meson_serial_console = {
  426. .name = AML_UART_DEV_NAME,
  427. .write = meson_serial_console_write,
  428. .device = uart_console_device,
  429. .setup = meson_serial_console_setup,
  430. .flags = CON_PRINTBUFFER,
  431. .index = -1,
  432. .data = &meson_uart_driver,
  433. };
  434. static int __init meson_serial_console_init(void)
  435. {
  436. register_console(&meson_serial_console);
  437. return 0;
  438. }
  439. console_initcall(meson_serial_console_init);
  440. static void meson_serial_early_console_write(struct console *co,
  441. const char *s,
  442. u_int count)
  443. {
  444. struct earlycon_device *dev = co->data;
  445. meson_serial_port_write(&dev->port, s, count);
  446. }
  447. static int __init
  448. meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
  449. {
  450. if (!device->port.membase)
  451. return -ENODEV;
  452. meson_uart_enable_tx_engine(&device->port);
  453. device->con->write = meson_serial_early_console_write;
  454. return 0;
  455. }
  456. /* Legacy bindings, should be removed when no more used */
  457. OF_EARLYCON_DECLARE(meson, "amlogic,meson-uart",
  458. meson_serial_early_console_setup);
  459. /* Stable bindings */
  460. OF_EARLYCON_DECLARE(meson, "amlogic,meson-ao-uart",
  461. meson_serial_early_console_setup);
  462. #define MESON_SERIAL_CONSOLE (&meson_serial_console)
  463. #else
  464. #define MESON_SERIAL_CONSOLE NULL
  465. #endif
  466. static struct uart_driver meson_uart_driver = {
  467. .owner = THIS_MODULE,
  468. .driver_name = "meson_uart",
  469. .dev_name = AML_UART_DEV_NAME,
  470. .nr = AML_UART_PORT_NUM,
  471. .cons = MESON_SERIAL_CONSOLE,
  472. };
  473. static inline struct clk *meson_uart_probe_clock(struct device *dev,
  474. const char *id)
  475. {
  476. struct clk *clk = NULL;
  477. int ret;
  478. clk = devm_clk_get(dev, id);
  479. if (IS_ERR(clk))
  480. return clk;
  481. ret = clk_prepare_enable(clk);
  482. if (ret) {
  483. dev_err(dev, "couldn't enable clk\n");
  484. return ERR_PTR(ret);
  485. }
  486. devm_add_action_or_reset(dev,
  487. (void(*)(void *))clk_disable_unprepare,
  488. clk);
  489. return clk;
  490. }
  491. /*
  492. * This function gets clocks in the legacy non-stable DT bindings.
  493. * This code will be remove once all the platforms switch to the
  494. * new DT bindings.
  495. */
  496. static int meson_uart_probe_clocks_legacy(struct platform_device *pdev,
  497. struct uart_port *port)
  498. {
  499. struct clk *clk = NULL;
  500. clk = meson_uart_probe_clock(&pdev->dev, NULL);
  501. if (IS_ERR(clk))
  502. return PTR_ERR(clk);
  503. port->uartclk = clk_get_rate(clk);
  504. return 0;
  505. }
  506. static int meson_uart_probe_clocks(struct platform_device *pdev,
  507. struct uart_port *port)
  508. {
  509. struct clk *clk_xtal = NULL;
  510. struct clk *clk_pclk = NULL;
  511. struct clk *clk_baud = NULL;
  512. clk_pclk = meson_uart_probe_clock(&pdev->dev, "pclk");
  513. if (IS_ERR(clk_pclk))
  514. return PTR_ERR(clk_pclk);
  515. clk_xtal = meson_uart_probe_clock(&pdev->dev, "xtal");
  516. if (IS_ERR(clk_xtal))
  517. return PTR_ERR(clk_xtal);
  518. clk_baud = meson_uart_probe_clock(&pdev->dev, "baud");
  519. if (IS_ERR(clk_baud))
  520. return PTR_ERR(clk_baud);
  521. port->uartclk = clk_get_rate(clk_baud);
  522. return 0;
  523. }
  524. static int meson_uart_probe(struct platform_device *pdev)
  525. {
  526. struct resource *res_mem, *res_irq;
  527. struct uart_port *port;
  528. int ret = 0;
  529. if (pdev->dev.of_node)
  530. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  531. if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
  532. return -EINVAL;
  533. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  534. if (!res_mem)
  535. return -ENODEV;
  536. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  537. if (!res_irq)
  538. return -ENODEV;
  539. if (meson_ports[pdev->id]) {
  540. dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
  541. return -EBUSY;
  542. }
  543. port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
  544. if (!port)
  545. return -ENOMEM;
  546. /* Use legacy way until all platforms switch to new bindings */
  547. if (of_device_is_compatible(pdev->dev.of_node, "amlogic,meson-uart"))
  548. ret = meson_uart_probe_clocks_legacy(pdev, port);
  549. else
  550. ret = meson_uart_probe_clocks(pdev, port);
  551. if (ret)
  552. return ret;
  553. port->iotype = UPIO_MEM;
  554. port->mapbase = res_mem->start;
  555. port->mapsize = resource_size(res_mem);
  556. port->irq = res_irq->start;
  557. port->flags = UPF_BOOT_AUTOCONF | UPF_LOW_LATENCY;
  558. port->dev = &pdev->dev;
  559. port->line = pdev->id;
  560. port->type = PORT_MESON;
  561. port->x_char = 0;
  562. port->ops = &meson_uart_ops;
  563. port->fifosize = 64;
  564. meson_ports[pdev->id] = port;
  565. platform_set_drvdata(pdev, port);
  566. /* reset port before registering (and possibly registering console) */
  567. if (meson_uart_request_port(port) >= 0) {
  568. meson_uart_reset(port);
  569. meson_uart_release_port(port);
  570. }
  571. ret = uart_add_one_port(&meson_uart_driver, port);
  572. if (ret)
  573. meson_ports[pdev->id] = NULL;
  574. return ret;
  575. }
  576. static int meson_uart_remove(struct platform_device *pdev)
  577. {
  578. struct uart_port *port;
  579. port = platform_get_drvdata(pdev);
  580. uart_remove_one_port(&meson_uart_driver, port);
  581. meson_ports[pdev->id] = NULL;
  582. return 0;
  583. }
  584. static const struct of_device_id meson_uart_dt_match[] = {
  585. /* Legacy bindings, should be removed when no more used */
  586. { .compatible = "amlogic,meson-uart" },
  587. /* Stable bindings */
  588. { .compatible = "amlogic,meson6-uart" },
  589. { .compatible = "amlogic,meson8-uart" },
  590. { .compatible = "amlogic,meson8b-uart" },
  591. { .compatible = "amlogic,meson-gx-uart" },
  592. { /* sentinel */ },
  593. };
  594. MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
  595. static struct platform_driver meson_uart_platform_driver = {
  596. .probe = meson_uart_probe,
  597. .remove = meson_uart_remove,
  598. .driver = {
  599. .name = "meson_uart",
  600. .of_match_table = meson_uart_dt_match,
  601. },
  602. };
  603. static int __init meson_uart_init(void)
  604. {
  605. int ret;
  606. ret = uart_register_driver(&meson_uart_driver);
  607. if (ret)
  608. return ret;
  609. ret = platform_driver_register(&meson_uart_platform_driver);
  610. if (ret)
  611. uart_unregister_driver(&meson_uart_driver);
  612. return ret;
  613. }
  614. static void __exit meson_uart_exit(void)
  615. {
  616. platform_driver_unregister(&meson_uart_platform_driver);
  617. uart_unregister_driver(&meson_uart_driver);
  618. }
  619. module_init(meson_uart_init);
  620. module_exit(meson_uart_exit);
  621. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  622. MODULE_DESCRIPTION("Amlogic Meson serial port driver");
  623. MODULE_LICENSE("GPL v2");