owl-uart.c 18 KB

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