owl-uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Actions Semi Owl family serial console
  4. *
  5. * Copyright 2013 Actions Semi Inc.
  6. * Author: Actions Semi, Inc.
  7. *
  8. * Copyright (c) 2016-2017 Andreas Färber
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/console.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/serial.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #define OWL_UART_PORT_NUM 7
  22. #define OWL_UART_DEV_NAME "ttyOWL"
  23. #define OWL_UART_CTL 0x000
  24. #define OWL_UART_RXDAT 0x004
  25. #define OWL_UART_TXDAT 0x008
  26. #define OWL_UART_STAT 0x00c
  27. #define OWL_UART_CTL_DWLS_MASK GENMASK(1, 0)
  28. #define OWL_UART_CTL_DWLS_5BITS (0x0 << 0)
  29. #define OWL_UART_CTL_DWLS_6BITS (0x1 << 0)
  30. #define OWL_UART_CTL_DWLS_7BITS (0x2 << 0)
  31. #define OWL_UART_CTL_DWLS_8BITS (0x3 << 0)
  32. #define OWL_UART_CTL_STPS_2BITS BIT(2)
  33. #define OWL_UART_CTL_PRS_MASK GENMASK(6, 4)
  34. #define OWL_UART_CTL_PRS_NONE (0x0 << 4)
  35. #define OWL_UART_CTL_PRS_ODD (0x4 << 4)
  36. #define OWL_UART_CTL_PRS_MARK (0x5 << 4)
  37. #define OWL_UART_CTL_PRS_EVEN (0x6 << 4)
  38. #define OWL_UART_CTL_PRS_SPACE (0x7 << 4)
  39. #define OWL_UART_CTL_AFE BIT(12)
  40. #define OWL_UART_CTL_TRFS_TX BIT(14)
  41. #define OWL_UART_CTL_EN BIT(15)
  42. #define OWL_UART_CTL_RXDE BIT(16)
  43. #define OWL_UART_CTL_TXDE BIT(17)
  44. #define OWL_UART_CTL_RXIE BIT(18)
  45. #define OWL_UART_CTL_TXIE BIT(19)
  46. #define OWL_UART_CTL_LBEN BIT(20)
  47. #define OWL_UART_STAT_RIP BIT(0)
  48. #define OWL_UART_STAT_TIP BIT(1)
  49. #define OWL_UART_STAT_RXER BIT(2)
  50. #define OWL_UART_STAT_TFER BIT(3)
  51. #define OWL_UART_STAT_RXST BIT(4)
  52. #define OWL_UART_STAT_RFEM BIT(5)
  53. #define OWL_UART_STAT_TFFU BIT(6)
  54. #define OWL_UART_STAT_CTSS BIT(7)
  55. #define OWL_UART_STAT_RTSS BIT(8)
  56. #define OWL_UART_STAT_TFES BIT(10)
  57. #define OWL_UART_STAT_TRFL_MASK GENMASK(16, 11)
  58. #define OWL_UART_STAT_UTBB BIT(17)
  59. static struct uart_driver owl_uart_driver;
  60. struct owl_uart_info {
  61. unsigned int tx_fifosize;
  62. };
  63. struct owl_uart_port {
  64. struct uart_port port;
  65. struct clk *clk;
  66. };
  67. #define to_owl_uart_port(prt) container_of(prt, struct owl_uart_port, prt)
  68. static struct owl_uart_port *owl_uart_ports[OWL_UART_PORT_NUM];
  69. static inline void owl_uart_write(struct uart_port *port, u32 val, unsigned int off)
  70. {
  71. writel(val, port->membase + off);
  72. }
  73. static inline u32 owl_uart_read(struct uart_port *port, unsigned int off)
  74. {
  75. return readl(port->membase + off);
  76. }
  77. static void owl_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  78. {
  79. u32 ctl;
  80. ctl = owl_uart_read(port, OWL_UART_CTL);
  81. if (mctrl & TIOCM_LOOP)
  82. ctl |= OWL_UART_CTL_LBEN;
  83. else
  84. ctl &= ~OWL_UART_CTL_LBEN;
  85. owl_uart_write(port, ctl, OWL_UART_CTL);
  86. }
  87. static unsigned int owl_uart_get_mctrl(struct uart_port *port)
  88. {
  89. unsigned int mctrl = TIOCM_CAR | TIOCM_DSR;
  90. u32 stat, ctl;
  91. ctl = owl_uart_read(port, OWL_UART_CTL);
  92. stat = owl_uart_read(port, OWL_UART_STAT);
  93. if (stat & OWL_UART_STAT_RTSS)
  94. mctrl |= TIOCM_RTS;
  95. if ((stat & OWL_UART_STAT_CTSS) || !(ctl & OWL_UART_CTL_AFE))
  96. mctrl |= TIOCM_CTS;
  97. return mctrl;
  98. }
  99. static unsigned int owl_uart_tx_empty(struct uart_port *port)
  100. {
  101. unsigned long flags;
  102. u32 val;
  103. unsigned int ret;
  104. spin_lock_irqsave(&port->lock, flags);
  105. val = owl_uart_read(port, OWL_UART_STAT);
  106. ret = (val & OWL_UART_STAT_TFES) ? TIOCSER_TEMT : 0;
  107. spin_unlock_irqrestore(&port->lock, flags);
  108. return ret;
  109. }
  110. static void owl_uart_stop_rx(struct uart_port *port)
  111. {
  112. u32 val;
  113. val = owl_uart_read(port, OWL_UART_CTL);
  114. val &= ~(OWL_UART_CTL_RXIE | OWL_UART_CTL_RXDE);
  115. owl_uart_write(port, val, OWL_UART_CTL);
  116. val = owl_uart_read(port, OWL_UART_STAT);
  117. val |= OWL_UART_STAT_RIP;
  118. owl_uart_write(port, val, OWL_UART_STAT);
  119. }
  120. static void owl_uart_stop_tx(struct uart_port *port)
  121. {
  122. u32 val;
  123. val = owl_uart_read(port, OWL_UART_CTL);
  124. val &= ~(OWL_UART_CTL_TXIE | OWL_UART_CTL_TXDE);
  125. owl_uart_write(port, val, OWL_UART_CTL);
  126. val = owl_uart_read(port, OWL_UART_STAT);
  127. val |= OWL_UART_STAT_TIP;
  128. owl_uart_write(port, val, OWL_UART_STAT);
  129. }
  130. static void owl_uart_start_tx(struct uart_port *port)
  131. {
  132. u32 val;
  133. if (uart_tx_stopped(port)) {
  134. owl_uart_stop_tx(port);
  135. return;
  136. }
  137. val = owl_uart_read(port, OWL_UART_STAT);
  138. val |= OWL_UART_STAT_TIP;
  139. owl_uart_write(port, val, OWL_UART_STAT);
  140. val = owl_uart_read(port, OWL_UART_CTL);
  141. val |= OWL_UART_CTL_TXIE;
  142. owl_uart_write(port, val, OWL_UART_CTL);
  143. }
  144. static void owl_uart_send_chars(struct uart_port *port)
  145. {
  146. struct circ_buf *xmit = &port->state->xmit;
  147. unsigned int ch;
  148. if (uart_tx_stopped(port))
  149. return;
  150. if (port->x_char) {
  151. while (!(owl_uart_read(port, OWL_UART_STAT) & OWL_UART_STAT_TFFU))
  152. cpu_relax();
  153. owl_uart_write(port, port->x_char, OWL_UART_TXDAT);
  154. port->icount.tx++;
  155. port->x_char = 0;
  156. }
  157. while (!(owl_uart_read(port, OWL_UART_STAT) & OWL_UART_STAT_TFFU)) {
  158. if (uart_circ_empty(xmit))
  159. break;
  160. ch = xmit->buf[xmit->tail];
  161. owl_uart_write(port, ch, OWL_UART_TXDAT);
  162. xmit->tail = (xmit->tail + 1) & (SERIAL_XMIT_SIZE - 1);
  163. port->icount.tx++;
  164. }
  165. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  166. uart_write_wakeup(port);
  167. if (uart_circ_empty(xmit))
  168. owl_uart_stop_tx(port);
  169. }
  170. static void owl_uart_receive_chars(struct uart_port *port)
  171. {
  172. u32 stat, val;
  173. val = owl_uart_read(port, OWL_UART_CTL);
  174. val &= ~OWL_UART_CTL_TRFS_TX;
  175. owl_uart_write(port, val, OWL_UART_CTL);
  176. stat = owl_uart_read(port, OWL_UART_STAT);
  177. while (!(stat & OWL_UART_STAT_RFEM)) {
  178. char flag = TTY_NORMAL;
  179. if (stat & OWL_UART_STAT_RXER)
  180. port->icount.overrun++;
  181. if (stat & OWL_UART_STAT_RXST) {
  182. /* We are not able to distinguish the error type. */
  183. port->icount.brk++;
  184. port->icount.frame++;
  185. stat &= port->read_status_mask;
  186. if (stat & OWL_UART_STAT_RXST)
  187. flag = TTY_PARITY;
  188. } else
  189. port->icount.rx++;
  190. val = owl_uart_read(port, OWL_UART_RXDAT);
  191. val &= 0xff;
  192. if ((stat & port->ignore_status_mask) == 0)
  193. tty_insert_flip_char(&port->state->port, val, flag);
  194. stat = owl_uart_read(port, OWL_UART_STAT);
  195. }
  196. spin_unlock(&port->lock);
  197. tty_flip_buffer_push(&port->state->port);
  198. spin_lock(&port->lock);
  199. }
  200. static irqreturn_t owl_uart_irq(int irq, void *dev_id)
  201. {
  202. struct uart_port *port = dev_id;
  203. unsigned long flags;
  204. u32 stat;
  205. spin_lock_irqsave(&port->lock, flags);
  206. stat = owl_uart_read(port, OWL_UART_STAT);
  207. if (stat & OWL_UART_STAT_RIP)
  208. owl_uart_receive_chars(port);
  209. if (stat & OWL_UART_STAT_TIP)
  210. owl_uart_send_chars(port);
  211. stat = owl_uart_read(port, OWL_UART_STAT);
  212. stat |= OWL_UART_STAT_RIP | OWL_UART_STAT_TIP;
  213. owl_uart_write(port, stat, OWL_UART_STAT);
  214. spin_unlock_irqrestore(&port->lock, flags);
  215. return IRQ_HANDLED;
  216. }
  217. static void owl_uart_shutdown(struct uart_port *port)
  218. {
  219. u32 val;
  220. unsigned long flags;
  221. spin_lock_irqsave(&port->lock, flags);
  222. val = owl_uart_read(port, OWL_UART_CTL);
  223. val &= ~(OWL_UART_CTL_TXIE | OWL_UART_CTL_RXIE
  224. | OWL_UART_CTL_TXDE | OWL_UART_CTL_RXDE | OWL_UART_CTL_EN);
  225. owl_uart_write(port, val, OWL_UART_CTL);
  226. spin_unlock_irqrestore(&port->lock, flags);
  227. free_irq(port->irq, port);
  228. }
  229. static int owl_uart_startup(struct uart_port *port)
  230. {
  231. u32 val;
  232. unsigned long flags;
  233. int ret;
  234. ret = request_irq(port->irq, owl_uart_irq, IRQF_TRIGGER_HIGH,
  235. "owl-uart", port);
  236. if (ret)
  237. return ret;
  238. spin_lock_irqsave(&port->lock, flags);
  239. val = owl_uart_read(port, OWL_UART_STAT);
  240. val |= OWL_UART_STAT_RIP | OWL_UART_STAT_TIP
  241. | OWL_UART_STAT_RXER | OWL_UART_STAT_TFER | OWL_UART_STAT_RXST;
  242. owl_uart_write(port, val, OWL_UART_STAT);
  243. val = owl_uart_read(port, OWL_UART_CTL);
  244. val |= OWL_UART_CTL_RXIE | OWL_UART_CTL_TXIE;
  245. val |= OWL_UART_CTL_EN;
  246. owl_uart_write(port, val, OWL_UART_CTL);
  247. spin_unlock_irqrestore(&port->lock, flags);
  248. return 0;
  249. }
  250. static void owl_uart_change_baudrate(struct owl_uart_port *owl_port,
  251. unsigned long baud)
  252. {
  253. clk_set_rate(owl_port->clk, baud * 8);
  254. }
  255. static void owl_uart_set_termios(struct uart_port *port,
  256. struct ktermios *termios,
  257. struct ktermios *old)
  258. {
  259. struct owl_uart_port *owl_port = to_owl_uart_port(port);
  260. unsigned int baud;
  261. u32 ctl;
  262. unsigned long flags;
  263. spin_lock_irqsave(&port->lock, flags);
  264. ctl = owl_uart_read(port, OWL_UART_CTL);
  265. ctl &= ~OWL_UART_CTL_DWLS_MASK;
  266. switch (termios->c_cflag & CSIZE) {
  267. case CS5:
  268. ctl |= OWL_UART_CTL_DWLS_5BITS;
  269. break;
  270. case CS6:
  271. ctl |= OWL_UART_CTL_DWLS_6BITS;
  272. break;
  273. case CS7:
  274. ctl |= OWL_UART_CTL_DWLS_7BITS;
  275. break;
  276. case CS8:
  277. default:
  278. ctl |= OWL_UART_CTL_DWLS_8BITS;
  279. break;
  280. }
  281. if (termios->c_cflag & CSTOPB)
  282. ctl |= OWL_UART_CTL_STPS_2BITS;
  283. else
  284. ctl &= ~OWL_UART_CTL_STPS_2BITS;
  285. ctl &= ~OWL_UART_CTL_PRS_MASK;
  286. if (termios->c_cflag & PARENB) {
  287. if (termios->c_cflag & CMSPAR) {
  288. if (termios->c_cflag & PARODD)
  289. ctl |= OWL_UART_CTL_PRS_MARK;
  290. else
  291. ctl |= OWL_UART_CTL_PRS_SPACE;
  292. } else if (termios->c_cflag & PARODD)
  293. ctl |= OWL_UART_CTL_PRS_ODD;
  294. else
  295. ctl |= OWL_UART_CTL_PRS_EVEN;
  296. } else
  297. ctl |= OWL_UART_CTL_PRS_NONE;
  298. if (termios->c_cflag & CRTSCTS)
  299. ctl |= OWL_UART_CTL_AFE;
  300. else
  301. ctl &= ~OWL_UART_CTL_AFE;
  302. owl_uart_write(port, ctl, OWL_UART_CTL);
  303. baud = uart_get_baud_rate(port, termios, old, 9600, 3200000);
  304. owl_uart_change_baudrate(owl_port, baud);
  305. /* Don't rewrite B0 */
  306. if (tty_termios_baud_rate(termios))
  307. tty_termios_encode_baud_rate(termios, baud, baud);
  308. port->read_status_mask |= OWL_UART_STAT_RXER;
  309. if (termios->c_iflag & INPCK)
  310. port->read_status_mask |= OWL_UART_STAT_RXST;
  311. uart_update_timeout(port, termios->c_cflag, baud);
  312. spin_unlock_irqrestore(&port->lock, flags);
  313. }
  314. static void owl_uart_release_port(struct uart_port *port)
  315. {
  316. struct platform_device *pdev = to_platform_device(port->dev);
  317. struct resource *res;
  318. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  319. if (!res)
  320. return;
  321. if (port->flags & UPF_IOREMAP) {
  322. devm_release_mem_region(port->dev, port->mapbase,
  323. resource_size(res));
  324. devm_iounmap(port->dev, port->membase);
  325. port->membase = NULL;
  326. }
  327. }
  328. static int owl_uart_request_port(struct uart_port *port)
  329. {
  330. struct platform_device *pdev = to_platform_device(port->dev);
  331. struct resource *res;
  332. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  333. if (!res)
  334. return -ENXIO;
  335. if (!devm_request_mem_region(port->dev, port->mapbase,
  336. resource_size(res), dev_name(port->dev)))
  337. return -EBUSY;
  338. if (port->flags & UPF_IOREMAP) {
  339. port->membase = devm_ioremap_nocache(port->dev, port->mapbase,
  340. resource_size(res));
  341. if (!port->membase)
  342. return -EBUSY;
  343. }
  344. return 0;
  345. }
  346. static const char *owl_uart_type(struct uart_port *port)
  347. {
  348. return (port->type == PORT_OWL) ? "owl-uart" : NULL;
  349. }
  350. static int owl_uart_verify_port(struct uart_port *port,
  351. struct serial_struct *ser)
  352. {
  353. if (port->type != PORT_OWL)
  354. return -EINVAL;
  355. if (port->irq != ser->irq)
  356. return -EINVAL;
  357. return 0;
  358. }
  359. static void owl_uart_config_port(struct uart_port *port, int flags)
  360. {
  361. if (flags & UART_CONFIG_TYPE) {
  362. port->type = PORT_OWL;
  363. owl_uart_request_port(port);
  364. }
  365. }
  366. static const struct uart_ops owl_uart_ops = {
  367. .set_mctrl = owl_uart_set_mctrl,
  368. .get_mctrl = owl_uart_get_mctrl,
  369. .tx_empty = owl_uart_tx_empty,
  370. .start_tx = owl_uart_start_tx,
  371. .stop_rx = owl_uart_stop_rx,
  372. .stop_tx = owl_uart_stop_tx,
  373. .startup = owl_uart_startup,
  374. .shutdown = owl_uart_shutdown,
  375. .set_termios = owl_uart_set_termios,
  376. .type = owl_uart_type,
  377. .config_port = owl_uart_config_port,
  378. .request_port = owl_uart_request_port,
  379. .release_port = owl_uart_release_port,
  380. .verify_port = owl_uart_verify_port,
  381. };
  382. #ifdef CONFIG_SERIAL_OWL_CONSOLE
  383. static void owl_console_putchar(struct uart_port *port, int ch)
  384. {
  385. if (!port->membase)
  386. return;
  387. while (owl_uart_read(port, OWL_UART_STAT) & OWL_UART_STAT_TFFU)
  388. cpu_relax();
  389. owl_uart_write(port, ch, OWL_UART_TXDAT);
  390. }
  391. static void owl_uart_port_write(struct uart_port *port, const char *s,
  392. u_int count)
  393. {
  394. u32 old_ctl, val;
  395. unsigned long flags;
  396. int locked;
  397. local_irq_save(flags);
  398. if (port->sysrq)
  399. locked = 0;
  400. else if (oops_in_progress)
  401. locked = spin_trylock(&port->lock);
  402. else {
  403. spin_lock(&port->lock);
  404. locked = 1;
  405. }
  406. old_ctl = owl_uart_read(port, OWL_UART_CTL);
  407. val = old_ctl | OWL_UART_CTL_TRFS_TX;
  408. /* disable IRQ */
  409. val &= ~(OWL_UART_CTL_RXIE | OWL_UART_CTL_TXIE);
  410. owl_uart_write(port, val, OWL_UART_CTL);
  411. uart_console_write(port, s, count, owl_console_putchar);
  412. /* wait until all contents have been sent out */
  413. while (owl_uart_read(port, OWL_UART_STAT) & OWL_UART_STAT_TRFL_MASK)
  414. cpu_relax();
  415. /* clear IRQ pending */
  416. val = owl_uart_read(port, OWL_UART_STAT);
  417. val |= OWL_UART_STAT_TIP | OWL_UART_STAT_RIP;
  418. owl_uart_write(port, val, OWL_UART_STAT);
  419. owl_uart_write(port, old_ctl, OWL_UART_CTL);
  420. if (locked)
  421. spin_unlock(&port->lock);
  422. local_irq_restore(flags);
  423. }
  424. static void owl_uart_console_write(struct console *co, const char *s,
  425. u_int count)
  426. {
  427. struct owl_uart_port *owl_port;
  428. owl_port = owl_uart_ports[co->index];
  429. if (!owl_port)
  430. return;
  431. owl_uart_port_write(&owl_port->port, s, count);
  432. }
  433. static int owl_uart_console_setup(struct console *co, char *options)
  434. {
  435. struct owl_uart_port *owl_port;
  436. int baud = 115200;
  437. int bits = 8;
  438. int parity = 'n';
  439. int flow = 'n';
  440. if (co->index < 0 || co->index >= OWL_UART_PORT_NUM)
  441. return -EINVAL;
  442. owl_port = owl_uart_ports[co->index];
  443. if (!owl_port || !owl_port->port.membase)
  444. return -ENODEV;
  445. if (options)
  446. uart_parse_options(options, &baud, &parity, &bits, &flow);
  447. return uart_set_options(&owl_port->port, co, baud, parity, bits, flow);
  448. }
  449. static struct console owl_uart_console = {
  450. .name = OWL_UART_DEV_NAME,
  451. .write = owl_uart_console_write,
  452. .device = uart_console_device,
  453. .setup = owl_uart_console_setup,
  454. .flags = CON_PRINTBUFFER,
  455. .index = -1,
  456. .data = &owl_uart_driver,
  457. };
  458. static int __init owl_uart_console_init(void)
  459. {
  460. register_console(&owl_uart_console);
  461. return 0;
  462. }
  463. console_initcall(owl_uart_console_init);
  464. static void owl_uart_early_console_write(struct console *co,
  465. const char *s,
  466. u_int count)
  467. {
  468. struct earlycon_device *dev = co->data;
  469. owl_uart_port_write(&dev->port, s, count);
  470. }
  471. static int __init
  472. owl_uart_early_console_setup(struct earlycon_device *device, const char *opt)
  473. {
  474. if (!device->port.membase)
  475. return -ENODEV;
  476. device->con->write = owl_uart_early_console_write;
  477. return 0;
  478. }
  479. OF_EARLYCON_DECLARE(owl, "actions,owl-uart",
  480. owl_uart_early_console_setup);
  481. #define OWL_UART_CONSOLE (&owl_uart_console)
  482. #else
  483. #define OWL_UART_CONSOLE NULL
  484. #endif
  485. static struct uart_driver owl_uart_driver = {
  486. .owner = THIS_MODULE,
  487. .driver_name = "owl-uart",
  488. .dev_name = OWL_UART_DEV_NAME,
  489. .nr = OWL_UART_PORT_NUM,
  490. .cons = OWL_UART_CONSOLE,
  491. };
  492. static const struct owl_uart_info owl_s500_info = {
  493. .tx_fifosize = 16,
  494. };
  495. static const struct owl_uart_info owl_s900_info = {
  496. .tx_fifosize = 32,
  497. };
  498. static const struct of_device_id owl_uart_dt_matches[] = {
  499. { .compatible = "actions,s500-uart", .data = &owl_s500_info },
  500. { .compatible = "actions,s900-uart", .data = &owl_s900_info },
  501. { }
  502. };
  503. MODULE_DEVICE_TABLE(of, owl_uart_dt_matches);
  504. static int owl_uart_probe(struct platform_device *pdev)
  505. {
  506. const struct of_device_id *match;
  507. const struct owl_uart_info *info = NULL;
  508. struct resource *res_mem;
  509. struct owl_uart_port *owl_port;
  510. int ret, irq;
  511. if (pdev->dev.of_node) {
  512. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  513. match = of_match_node(owl_uart_dt_matches, pdev->dev.of_node);
  514. if (match)
  515. info = match->data;
  516. }
  517. if (pdev->id < 0 || pdev->id >= OWL_UART_PORT_NUM) {
  518. dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
  519. return -EINVAL;
  520. }
  521. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  522. if (!res_mem) {
  523. dev_err(&pdev->dev, "could not get mem\n");
  524. return -ENODEV;
  525. }
  526. irq = platform_get_irq(pdev, 0);
  527. if (irq < 0)
  528. return irq;
  529. if (owl_uart_ports[pdev->id]) {
  530. dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
  531. return -EBUSY;
  532. }
  533. owl_port = devm_kzalloc(&pdev->dev, sizeof(*owl_port), GFP_KERNEL);
  534. if (!owl_port)
  535. return -ENOMEM;
  536. owl_port->clk = devm_clk_get(&pdev->dev, NULL);
  537. if (IS_ERR(owl_port->clk)) {
  538. dev_err(&pdev->dev, "could not get clk\n");
  539. return PTR_ERR(owl_port->clk);
  540. }
  541. ret = clk_prepare_enable(owl_port->clk);
  542. if (ret) {
  543. dev_err(&pdev->dev, "could not enable clk\n");
  544. return ret;
  545. }
  546. owl_port->port.dev = &pdev->dev;
  547. owl_port->port.line = pdev->id;
  548. owl_port->port.type = PORT_OWL;
  549. owl_port->port.iotype = UPIO_MEM;
  550. owl_port->port.mapbase = res_mem->start;
  551. owl_port->port.irq = irq;
  552. owl_port->port.uartclk = clk_get_rate(owl_port->clk);
  553. if (owl_port->port.uartclk == 0) {
  554. dev_err(&pdev->dev, "clock rate is zero\n");
  555. return -EINVAL;
  556. }
  557. owl_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP | UPF_LOW_LATENCY;
  558. owl_port->port.x_char = 0;
  559. owl_port->port.fifosize = (info) ? info->tx_fifosize : 16;
  560. owl_port->port.ops = &owl_uart_ops;
  561. owl_uart_ports[pdev->id] = owl_port;
  562. platform_set_drvdata(pdev, owl_port);
  563. ret = uart_add_one_port(&owl_uart_driver, &owl_port->port);
  564. if (ret)
  565. owl_uart_ports[pdev->id] = NULL;
  566. return ret;
  567. }
  568. static int owl_uart_remove(struct platform_device *pdev)
  569. {
  570. struct owl_uart_port *owl_port = platform_get_drvdata(pdev);
  571. uart_remove_one_port(&owl_uart_driver, &owl_port->port);
  572. owl_uart_ports[pdev->id] = NULL;
  573. clk_disable_unprepare(owl_port->clk);
  574. return 0;
  575. }
  576. static struct platform_driver owl_uart_platform_driver = {
  577. .probe = owl_uart_probe,
  578. .remove = owl_uart_remove,
  579. .driver = {
  580. .name = "owl-uart",
  581. .of_match_table = owl_uart_dt_matches,
  582. },
  583. };
  584. static int __init owl_uart_init(void)
  585. {
  586. int ret;
  587. ret = uart_register_driver(&owl_uart_driver);
  588. if (ret)
  589. return ret;
  590. ret = platform_driver_register(&owl_uart_platform_driver);
  591. if (ret)
  592. uart_unregister_driver(&owl_uart_driver);
  593. return ret;
  594. }
  595. static void __exit owl_uart_exit(void)
  596. {
  597. platform_driver_unregister(&owl_uart_platform_driver);
  598. uart_unregister_driver(&owl_uart_driver);
  599. }
  600. module_init(owl_uart_init);
  601. module_exit(owl_uart_exit);
  602. MODULE_LICENSE("GPL");