meson_uart.c 19 KB

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