uartlite.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * uartlite.c: Serial driver for Xilinx uartlite serial controller
  4. *
  5. * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
  6. * Copyright (C) 2007 Secret Lab Technologies Ltd.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/module.h>
  10. #include <linux/console.h>
  11. #include <linux/serial.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/clk.h>
  24. #define ULITE_NAME "ttyUL"
  25. #define ULITE_MAJOR 204
  26. #define ULITE_MINOR 187
  27. #define ULITE_NR_UARTS CONFIG_SERIAL_UARTLITE_NR_UARTS
  28. /* ---------------------------------------------------------------------
  29. * Register definitions
  30. *
  31. * For register details see datasheet:
  32. * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
  33. */
  34. #define ULITE_RX 0x00
  35. #define ULITE_TX 0x04
  36. #define ULITE_STATUS 0x08
  37. #define ULITE_CONTROL 0x0c
  38. #define ULITE_REGION 16
  39. #define ULITE_STATUS_RXVALID 0x01
  40. #define ULITE_STATUS_RXFULL 0x02
  41. #define ULITE_STATUS_TXEMPTY 0x04
  42. #define ULITE_STATUS_TXFULL 0x08
  43. #define ULITE_STATUS_IE 0x10
  44. #define ULITE_STATUS_OVERRUN 0x20
  45. #define ULITE_STATUS_FRAME 0x40
  46. #define ULITE_STATUS_PARITY 0x80
  47. #define ULITE_CONTROL_RST_TX 0x01
  48. #define ULITE_CONTROL_RST_RX 0x02
  49. #define ULITE_CONTROL_IE 0x10
  50. /* Static pointer to console port */
  51. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  52. static struct uart_port *console_port;
  53. #endif
  54. struct uartlite_data {
  55. const struct uartlite_reg_ops *reg_ops;
  56. struct clk *clk;
  57. };
  58. struct uartlite_reg_ops {
  59. u32 (*in)(void __iomem *addr);
  60. void (*out)(u32 val, void __iomem *addr);
  61. };
  62. static u32 uartlite_inbe32(void __iomem *addr)
  63. {
  64. return ioread32be(addr);
  65. }
  66. static void uartlite_outbe32(u32 val, void __iomem *addr)
  67. {
  68. iowrite32be(val, addr);
  69. }
  70. static const struct uartlite_reg_ops uartlite_be = {
  71. .in = uartlite_inbe32,
  72. .out = uartlite_outbe32,
  73. };
  74. static u32 uartlite_inle32(void __iomem *addr)
  75. {
  76. return ioread32(addr);
  77. }
  78. static void uartlite_outle32(u32 val, void __iomem *addr)
  79. {
  80. iowrite32(val, addr);
  81. }
  82. static const struct uartlite_reg_ops uartlite_le = {
  83. .in = uartlite_inle32,
  84. .out = uartlite_outle32,
  85. };
  86. static inline u32 uart_in32(u32 offset, struct uart_port *port)
  87. {
  88. struct uartlite_data *pdata = port->private_data;
  89. return pdata->reg_ops->in(port->membase + offset);
  90. }
  91. static inline void uart_out32(u32 val, u32 offset, struct uart_port *port)
  92. {
  93. struct uartlite_data *pdata = port->private_data;
  94. pdata->reg_ops->out(val, port->membase + offset);
  95. }
  96. static struct uart_port ulite_ports[ULITE_NR_UARTS];
  97. /* ---------------------------------------------------------------------
  98. * Core UART driver operations
  99. */
  100. static int ulite_receive(struct uart_port *port, int stat)
  101. {
  102. struct tty_port *tport = &port->state->port;
  103. unsigned char ch = 0;
  104. char flag = TTY_NORMAL;
  105. if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
  106. | ULITE_STATUS_FRAME)) == 0)
  107. return 0;
  108. /* stats */
  109. if (stat & ULITE_STATUS_RXVALID) {
  110. port->icount.rx++;
  111. ch = uart_in32(ULITE_RX, port);
  112. if (stat & ULITE_STATUS_PARITY)
  113. port->icount.parity++;
  114. }
  115. if (stat & ULITE_STATUS_OVERRUN)
  116. port->icount.overrun++;
  117. if (stat & ULITE_STATUS_FRAME)
  118. port->icount.frame++;
  119. /* drop byte with parity error if IGNPAR specificed */
  120. if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
  121. stat &= ~ULITE_STATUS_RXVALID;
  122. stat &= port->read_status_mask;
  123. if (stat & ULITE_STATUS_PARITY)
  124. flag = TTY_PARITY;
  125. stat &= ~port->ignore_status_mask;
  126. if (stat & ULITE_STATUS_RXVALID)
  127. tty_insert_flip_char(tport, ch, flag);
  128. if (stat & ULITE_STATUS_FRAME)
  129. tty_insert_flip_char(tport, 0, TTY_FRAME);
  130. if (stat & ULITE_STATUS_OVERRUN)
  131. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  132. return 1;
  133. }
  134. static int ulite_transmit(struct uart_port *port, int stat)
  135. {
  136. struct circ_buf *xmit = &port->state->xmit;
  137. if (stat & ULITE_STATUS_TXFULL)
  138. return 0;
  139. if (port->x_char) {
  140. uart_out32(port->x_char, ULITE_TX, port);
  141. port->x_char = 0;
  142. port->icount.tx++;
  143. return 1;
  144. }
  145. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  146. return 0;
  147. uart_out32(xmit->buf[xmit->tail], ULITE_TX, port);
  148. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
  149. port->icount.tx++;
  150. /* wake up */
  151. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  152. uart_write_wakeup(port);
  153. return 1;
  154. }
  155. static irqreturn_t ulite_isr(int irq, void *dev_id)
  156. {
  157. struct uart_port *port = dev_id;
  158. int stat, busy, n = 0;
  159. unsigned long flags;
  160. do {
  161. spin_lock_irqsave(&port->lock, flags);
  162. stat = uart_in32(ULITE_STATUS, port);
  163. busy = ulite_receive(port, stat);
  164. busy |= ulite_transmit(port, stat);
  165. spin_unlock_irqrestore(&port->lock, flags);
  166. n++;
  167. } while (busy);
  168. /* work done? */
  169. if (n > 1) {
  170. tty_flip_buffer_push(&port->state->port);
  171. return IRQ_HANDLED;
  172. } else {
  173. return IRQ_NONE;
  174. }
  175. }
  176. static unsigned int ulite_tx_empty(struct uart_port *port)
  177. {
  178. unsigned long flags;
  179. unsigned int ret;
  180. spin_lock_irqsave(&port->lock, flags);
  181. ret = uart_in32(ULITE_STATUS, port);
  182. spin_unlock_irqrestore(&port->lock, flags);
  183. return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
  184. }
  185. static unsigned int ulite_get_mctrl(struct uart_port *port)
  186. {
  187. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  188. }
  189. static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
  190. {
  191. /* N/A */
  192. }
  193. static void ulite_stop_tx(struct uart_port *port)
  194. {
  195. /* N/A */
  196. }
  197. static void ulite_start_tx(struct uart_port *port)
  198. {
  199. ulite_transmit(port, uart_in32(ULITE_STATUS, port));
  200. }
  201. static void ulite_stop_rx(struct uart_port *port)
  202. {
  203. /* don't forward any more data (like !CREAD) */
  204. port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
  205. | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
  206. }
  207. static void ulite_break_ctl(struct uart_port *port, int ctl)
  208. {
  209. /* N/A */
  210. }
  211. static int ulite_startup(struct uart_port *port)
  212. {
  213. struct uartlite_data *pdata = port->private_data;
  214. int ret;
  215. ret = clk_enable(pdata->clk);
  216. if (ret) {
  217. dev_err(port->dev, "Failed to enable clock\n");
  218. return ret;
  219. }
  220. ret = request_irq(port->irq, ulite_isr, IRQF_SHARED | IRQF_TRIGGER_RISING,
  221. "uartlite", port);
  222. if (ret)
  223. return ret;
  224. uart_out32(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
  225. ULITE_CONTROL, port);
  226. uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
  227. return 0;
  228. }
  229. static void ulite_shutdown(struct uart_port *port)
  230. {
  231. struct uartlite_data *pdata = port->private_data;
  232. uart_out32(0, ULITE_CONTROL, port);
  233. uart_in32(ULITE_CONTROL, port); /* dummy */
  234. free_irq(port->irq, port);
  235. clk_disable(pdata->clk);
  236. }
  237. static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
  238. struct ktermios *old)
  239. {
  240. unsigned long flags;
  241. unsigned int baud;
  242. spin_lock_irqsave(&port->lock, flags);
  243. port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
  244. | ULITE_STATUS_TXFULL;
  245. if (termios->c_iflag & INPCK)
  246. port->read_status_mask |=
  247. ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
  248. port->ignore_status_mask = 0;
  249. if (termios->c_iflag & IGNPAR)
  250. port->ignore_status_mask |= ULITE_STATUS_PARITY
  251. | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
  252. /* ignore all characters if CREAD is not set */
  253. if ((termios->c_cflag & CREAD) == 0)
  254. port->ignore_status_mask |=
  255. ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
  256. | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
  257. /* update timeout */
  258. baud = uart_get_baud_rate(port, termios, old, 0, 460800);
  259. uart_update_timeout(port, termios->c_cflag, baud);
  260. spin_unlock_irqrestore(&port->lock, flags);
  261. }
  262. static const char *ulite_type(struct uart_port *port)
  263. {
  264. return port->type == PORT_UARTLITE ? "uartlite" : NULL;
  265. }
  266. static void ulite_release_port(struct uart_port *port)
  267. {
  268. release_mem_region(port->mapbase, ULITE_REGION);
  269. iounmap(port->membase);
  270. port->membase = NULL;
  271. }
  272. static int ulite_request_port(struct uart_port *port)
  273. {
  274. struct uartlite_data *pdata = port->private_data;
  275. int ret;
  276. pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
  277. port, (unsigned long long) port->mapbase);
  278. if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
  279. dev_err(port->dev, "Memory region busy\n");
  280. return -EBUSY;
  281. }
  282. port->membase = ioremap(port->mapbase, ULITE_REGION);
  283. if (!port->membase) {
  284. dev_err(port->dev, "Unable to map registers\n");
  285. release_mem_region(port->mapbase, ULITE_REGION);
  286. return -EBUSY;
  287. }
  288. pdata->reg_ops = &uartlite_be;
  289. ret = uart_in32(ULITE_CONTROL, port);
  290. uart_out32(ULITE_CONTROL_RST_TX, ULITE_CONTROL, port);
  291. ret = uart_in32(ULITE_STATUS, port);
  292. /* Endianess detection */
  293. if ((ret & ULITE_STATUS_TXEMPTY) != ULITE_STATUS_TXEMPTY)
  294. pdata->reg_ops = &uartlite_le;
  295. return 0;
  296. }
  297. static void ulite_config_port(struct uart_port *port, int flags)
  298. {
  299. if (!ulite_request_port(port))
  300. port->type = PORT_UARTLITE;
  301. }
  302. static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
  303. {
  304. /* we don't want the core code to modify any port params */
  305. return -EINVAL;
  306. }
  307. static void ulite_pm(struct uart_port *port, unsigned int state,
  308. unsigned int oldstate)
  309. {
  310. struct uartlite_data *pdata = port->private_data;
  311. if (!state)
  312. clk_enable(pdata->clk);
  313. else
  314. clk_disable(pdata->clk);
  315. }
  316. #ifdef CONFIG_CONSOLE_POLL
  317. static int ulite_get_poll_char(struct uart_port *port)
  318. {
  319. if (!(uart_in32(ULITE_STATUS, port) & ULITE_STATUS_RXVALID))
  320. return NO_POLL_CHAR;
  321. return uart_in32(ULITE_RX, port);
  322. }
  323. static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
  324. {
  325. while (uart_in32(ULITE_STATUS, port) & ULITE_STATUS_TXFULL)
  326. cpu_relax();
  327. /* write char to device */
  328. uart_out32(ch, ULITE_TX, port);
  329. }
  330. #endif
  331. static const struct uart_ops ulite_ops = {
  332. .tx_empty = ulite_tx_empty,
  333. .set_mctrl = ulite_set_mctrl,
  334. .get_mctrl = ulite_get_mctrl,
  335. .stop_tx = ulite_stop_tx,
  336. .start_tx = ulite_start_tx,
  337. .stop_rx = ulite_stop_rx,
  338. .break_ctl = ulite_break_ctl,
  339. .startup = ulite_startup,
  340. .shutdown = ulite_shutdown,
  341. .set_termios = ulite_set_termios,
  342. .type = ulite_type,
  343. .release_port = ulite_release_port,
  344. .request_port = ulite_request_port,
  345. .config_port = ulite_config_port,
  346. .verify_port = ulite_verify_port,
  347. .pm = ulite_pm,
  348. #ifdef CONFIG_CONSOLE_POLL
  349. .poll_get_char = ulite_get_poll_char,
  350. .poll_put_char = ulite_put_poll_char,
  351. #endif
  352. };
  353. /* ---------------------------------------------------------------------
  354. * Console driver operations
  355. */
  356. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  357. static void ulite_console_wait_tx(struct uart_port *port)
  358. {
  359. u8 val;
  360. unsigned long timeout;
  361. /*
  362. * Spin waiting for TX fifo to have space available.
  363. * When using the Microblaze Debug Module this can take up to 1s
  364. */
  365. timeout = jiffies + msecs_to_jiffies(1000);
  366. while (1) {
  367. val = uart_in32(ULITE_STATUS, port);
  368. if ((val & ULITE_STATUS_TXFULL) == 0)
  369. break;
  370. if (time_after(jiffies, timeout)) {
  371. dev_warn(port->dev,
  372. "timeout waiting for TX buffer empty\n");
  373. break;
  374. }
  375. cpu_relax();
  376. }
  377. }
  378. static void ulite_console_putchar(struct uart_port *port, int ch)
  379. {
  380. ulite_console_wait_tx(port);
  381. uart_out32(ch, ULITE_TX, port);
  382. }
  383. static void ulite_console_write(struct console *co, const char *s,
  384. unsigned int count)
  385. {
  386. struct uart_port *port = console_port;
  387. unsigned long flags;
  388. unsigned int ier;
  389. int locked = 1;
  390. if (oops_in_progress) {
  391. locked = spin_trylock_irqsave(&port->lock, flags);
  392. } else
  393. spin_lock_irqsave(&port->lock, flags);
  394. /* save and disable interrupt */
  395. ier = uart_in32(ULITE_STATUS, port) & ULITE_STATUS_IE;
  396. uart_out32(0, ULITE_CONTROL, port);
  397. uart_console_write(port, s, count, ulite_console_putchar);
  398. ulite_console_wait_tx(port);
  399. /* restore interrupt state */
  400. if (ier)
  401. uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
  402. if (locked)
  403. spin_unlock_irqrestore(&port->lock, flags);
  404. }
  405. static int ulite_console_setup(struct console *co, char *options)
  406. {
  407. struct uart_port *port;
  408. int baud = 9600;
  409. int bits = 8;
  410. int parity = 'n';
  411. int flow = 'n';
  412. port = console_port;
  413. /* Has the device been initialized yet? */
  414. if (!port->mapbase) {
  415. pr_debug("console on ttyUL%i not present\n", co->index);
  416. return -ENODEV;
  417. }
  418. /* not initialized yet? */
  419. if (!port->membase) {
  420. if (ulite_request_port(port))
  421. return -ENODEV;
  422. }
  423. if (options)
  424. uart_parse_options(options, &baud, &parity, &bits, &flow);
  425. return uart_set_options(port, co, baud, parity, bits, flow);
  426. }
  427. static struct uart_driver ulite_uart_driver;
  428. static struct console ulite_console = {
  429. .name = ULITE_NAME,
  430. .write = ulite_console_write,
  431. .device = uart_console_device,
  432. .setup = ulite_console_setup,
  433. .flags = CON_PRINTBUFFER,
  434. .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
  435. .data = &ulite_uart_driver,
  436. };
  437. static void early_uartlite_putc(struct uart_port *port, int c)
  438. {
  439. /*
  440. * Limit how many times we'll spin waiting for TX FIFO status.
  441. * This will prevent lockups if the base address is incorrectly
  442. * set, or any other issue on the UARTLITE.
  443. * This limit is pretty arbitrary, unless we are at about 10 baud
  444. * we'll never timeout on a working UART.
  445. */
  446. unsigned retries = 1000000;
  447. /* read status bit - 0x8 offset */
  448. while (--retries && (readl(port->membase + 8) & (1 << 3)))
  449. ;
  450. /* Only attempt the iowrite if we didn't timeout */
  451. /* write to TX_FIFO - 0x4 offset */
  452. if (retries)
  453. writel(c & 0xff, port->membase + 4);
  454. }
  455. static void early_uartlite_write(struct console *console,
  456. const char *s, unsigned n)
  457. {
  458. struct earlycon_device *device = console->data;
  459. uart_console_write(&device->port, s, n, early_uartlite_putc);
  460. }
  461. static int __init early_uartlite_setup(struct earlycon_device *device,
  462. const char *options)
  463. {
  464. if (!device->port.membase)
  465. return -ENODEV;
  466. device->con->write = early_uartlite_write;
  467. return 0;
  468. }
  469. EARLYCON_DECLARE(uartlite, early_uartlite_setup);
  470. OF_EARLYCON_DECLARE(uartlite_b, "xlnx,opb-uartlite-1.00.b", early_uartlite_setup);
  471. OF_EARLYCON_DECLARE(uartlite_a, "xlnx,xps-uartlite-1.00.a", early_uartlite_setup);
  472. #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
  473. static struct uart_driver ulite_uart_driver = {
  474. .owner = THIS_MODULE,
  475. .driver_name = "uartlite",
  476. .dev_name = ULITE_NAME,
  477. .major = ULITE_MAJOR,
  478. .minor = ULITE_MINOR,
  479. .nr = ULITE_NR_UARTS,
  480. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  481. .cons = &ulite_console,
  482. #endif
  483. };
  484. /* ---------------------------------------------------------------------
  485. * Port assignment functions (mapping devices to uart_port structures)
  486. */
  487. /** ulite_assign: register a uartlite device with the driver
  488. *
  489. * @dev: pointer to device structure
  490. * @id: requested id number. Pass -1 for automatic port assignment
  491. * @base: base address of uartlite registers
  492. * @irq: irq number for uartlite
  493. * @pdata: private data for uartlite
  494. *
  495. * Returns: 0 on success, <0 otherwise
  496. */
  497. static int ulite_assign(struct device *dev, int id, u32 base, int irq,
  498. struct uartlite_data *pdata)
  499. {
  500. struct uart_port *port;
  501. int rc;
  502. /* if id = -1; then scan for a free id and use that */
  503. if (id < 0) {
  504. for (id = 0; id < ULITE_NR_UARTS; id++)
  505. if (ulite_ports[id].mapbase == 0)
  506. break;
  507. }
  508. if (id < 0 || id >= ULITE_NR_UARTS) {
  509. dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
  510. return -EINVAL;
  511. }
  512. if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
  513. dev_err(dev, "cannot assign to %s%i; it is already in use\n",
  514. ULITE_NAME, id);
  515. return -EBUSY;
  516. }
  517. port = &ulite_ports[id];
  518. spin_lock_init(&port->lock);
  519. port->fifosize = 16;
  520. port->regshift = 2;
  521. port->iotype = UPIO_MEM;
  522. port->iobase = 1; /* mark port in use */
  523. port->mapbase = base;
  524. port->membase = NULL;
  525. port->ops = &ulite_ops;
  526. port->irq = irq;
  527. port->flags = UPF_BOOT_AUTOCONF;
  528. port->dev = dev;
  529. port->type = PORT_UNKNOWN;
  530. port->line = id;
  531. port->private_data = pdata;
  532. dev_set_drvdata(dev, port);
  533. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  534. /*
  535. * If console hasn't been found yet try to assign this port
  536. * because it is required to be assigned for console setup function.
  537. * If register_console() don't assign value, then console_port pointer
  538. * is cleanup.
  539. */
  540. if (ulite_uart_driver.cons->index == -1)
  541. console_port = port;
  542. #endif
  543. /* Register the port */
  544. rc = uart_add_one_port(&ulite_uart_driver, port);
  545. if (rc) {
  546. dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
  547. port->mapbase = 0;
  548. dev_set_drvdata(dev, NULL);
  549. return rc;
  550. }
  551. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  552. /* This is not port which is used for console that's why clean it up */
  553. if (ulite_uart_driver.cons->index == -1)
  554. console_port = NULL;
  555. #endif
  556. return 0;
  557. }
  558. /** ulite_release: register a uartlite device with the driver
  559. *
  560. * @dev: pointer to device structure
  561. */
  562. static int ulite_release(struct device *dev)
  563. {
  564. struct uart_port *port = dev_get_drvdata(dev);
  565. int rc = 0;
  566. if (port) {
  567. rc = uart_remove_one_port(&ulite_uart_driver, port);
  568. dev_set_drvdata(dev, NULL);
  569. port->mapbase = 0;
  570. }
  571. return rc;
  572. }
  573. /**
  574. * ulite_suspend - Stop the device.
  575. *
  576. * @dev: handle to the device structure.
  577. * Return: 0 always.
  578. */
  579. static int __maybe_unused ulite_suspend(struct device *dev)
  580. {
  581. struct uart_port *port = dev_get_drvdata(dev);
  582. if (port)
  583. uart_suspend_port(&ulite_uart_driver, port);
  584. return 0;
  585. }
  586. /**
  587. * ulite_resume - Resume the device.
  588. *
  589. * @dev: handle to the device structure.
  590. * Return: 0 on success, errno otherwise.
  591. */
  592. static int __maybe_unused ulite_resume(struct device *dev)
  593. {
  594. struct uart_port *port = dev_get_drvdata(dev);
  595. if (port)
  596. uart_resume_port(&ulite_uart_driver, port);
  597. return 0;
  598. }
  599. /* ---------------------------------------------------------------------
  600. * Platform bus binding
  601. */
  602. static SIMPLE_DEV_PM_OPS(ulite_pm_ops, ulite_suspend, ulite_resume);
  603. #if defined(CONFIG_OF)
  604. /* Match table for of_platform binding */
  605. static const struct of_device_id ulite_of_match[] = {
  606. { .compatible = "xlnx,opb-uartlite-1.00.b", },
  607. { .compatible = "xlnx,xps-uartlite-1.00.a", },
  608. {}
  609. };
  610. MODULE_DEVICE_TABLE(of, ulite_of_match);
  611. #endif /* CONFIG_OF */
  612. static int ulite_probe(struct platform_device *pdev)
  613. {
  614. struct resource *res;
  615. struct uartlite_data *pdata;
  616. int irq, ret;
  617. int id = pdev->id;
  618. #ifdef CONFIG_OF
  619. const __be32 *prop;
  620. prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
  621. if (prop)
  622. id = be32_to_cpup(prop);
  623. #endif
  624. pdata = devm_kzalloc(&pdev->dev, sizeof(struct uartlite_data),
  625. GFP_KERNEL);
  626. if (!pdata)
  627. return -ENOMEM;
  628. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  629. if (!res)
  630. return -ENODEV;
  631. irq = platform_get_irq(pdev, 0);
  632. if (irq <= 0)
  633. return -ENXIO;
  634. pdata->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
  635. if (IS_ERR(pdata->clk)) {
  636. if (PTR_ERR(pdata->clk) != -ENOENT)
  637. return PTR_ERR(pdata->clk);
  638. /*
  639. * Clock framework support is optional, continue on
  640. * anyways if we don't find a matching clock.
  641. */
  642. pdata->clk = NULL;
  643. }
  644. ret = clk_prepare_enable(pdata->clk);
  645. if (ret) {
  646. dev_err(&pdev->dev, "Failed to prepare clock\n");
  647. return ret;
  648. }
  649. if (!ulite_uart_driver.state) {
  650. dev_dbg(&pdev->dev, "uartlite: calling uart_register_driver()\n");
  651. ret = uart_register_driver(&ulite_uart_driver);
  652. if (ret < 0) {
  653. dev_err(&pdev->dev, "Failed to register driver\n");
  654. return ret;
  655. }
  656. }
  657. ret = ulite_assign(&pdev->dev, id, res->start, irq, pdata);
  658. clk_disable(pdata->clk);
  659. return ret;
  660. }
  661. static int ulite_remove(struct platform_device *pdev)
  662. {
  663. struct uart_port *port = dev_get_drvdata(&pdev->dev);
  664. struct uartlite_data *pdata = port->private_data;
  665. clk_disable_unprepare(pdata->clk);
  666. return ulite_release(&pdev->dev);
  667. }
  668. /* work with hotplug and coldplug */
  669. MODULE_ALIAS("platform:uartlite");
  670. static struct platform_driver ulite_platform_driver = {
  671. .probe = ulite_probe,
  672. .remove = ulite_remove,
  673. .driver = {
  674. .name = "uartlite",
  675. .of_match_table = of_match_ptr(ulite_of_match),
  676. .pm = &ulite_pm_ops,
  677. },
  678. };
  679. /* ---------------------------------------------------------------------
  680. * Module setup/teardown
  681. */
  682. static int __init ulite_init(void)
  683. {
  684. pr_debug("uartlite: calling platform_driver_register()\n");
  685. return platform_driver_register(&ulite_platform_driver);
  686. }
  687. static void __exit ulite_exit(void)
  688. {
  689. platform_driver_unregister(&ulite_platform_driver);
  690. uart_unregister_driver(&ulite_uart_driver);
  691. }
  692. module_init(ulite_init);
  693. module_exit(ulite_exit);
  694. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  695. MODULE_DESCRIPTION("Xilinx uartlite serial driver");
  696. MODULE_LICENSE("GPL");