owl-uart.c 18 KB

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