uartlite.c 18 KB

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