uartlite.c 20 KB

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