uartlite.c 21 KB

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