altera_uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * altera_uart.c -- Altera UART driver
  3. *
  4. * Based on mcf.c -- Freescale ColdFire UART driver
  5. *
  6. * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
  7. * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
  8. * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/timer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/console.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/serial.h>
  24. #include <linux/serial_core.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/of.h>
  27. #include <linux/io.h>
  28. #include <linux/altera_uart.h>
  29. #define DRV_NAME "altera_uart"
  30. #define SERIAL_ALTERA_MAJOR 204
  31. #define SERIAL_ALTERA_MINOR 213
  32. /*
  33. * Altera UART register definitions according to the Nios UART datasheet:
  34. * http://www.altera.com/literature/ds/ds_nios_uart.pdf
  35. */
  36. #define ALTERA_UART_SIZE 32
  37. #define ALTERA_UART_RXDATA_REG 0
  38. #define ALTERA_UART_TXDATA_REG 4
  39. #define ALTERA_UART_STATUS_REG 8
  40. #define ALTERA_UART_CONTROL_REG 12
  41. #define ALTERA_UART_DIVISOR_REG 16
  42. #define ALTERA_UART_EOP_REG 20
  43. #define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
  44. #define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
  45. #define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
  46. #define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
  47. #define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
  48. #define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
  49. #define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
  50. #define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
  51. #define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
  52. #define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
  53. #define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
  54. #define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
  55. /* Enable interrupt on... */
  56. #define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
  57. #define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
  58. #define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
  59. #define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
  60. #define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
  61. #define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
  62. #define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
  63. #define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
  64. #define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
  65. #define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
  66. #define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
  67. #define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
  68. #define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
  69. /*
  70. * Local per-uart structure.
  71. */
  72. struct altera_uart {
  73. struct uart_port port;
  74. struct timer_list tmr;
  75. unsigned int sigs; /* Local copy of line sigs */
  76. unsigned short imr; /* Local IMR mirror */
  77. };
  78. static u32 altera_uart_readl(struct uart_port *port, int reg)
  79. {
  80. return readl(port->membase + (reg << port->regshift));
  81. }
  82. static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
  83. {
  84. writel(dat, port->membase + (reg << port->regshift));
  85. }
  86. static unsigned int altera_uart_tx_empty(struct uart_port *port)
  87. {
  88. return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
  89. ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
  90. }
  91. static unsigned int altera_uart_get_mctrl(struct uart_port *port)
  92. {
  93. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  94. unsigned int sigs;
  95. sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
  96. ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
  97. sigs |= (pp->sigs & TIOCM_RTS);
  98. return sigs;
  99. }
  100. static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
  101. {
  102. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  103. pp->sigs = sigs;
  104. if (sigs & TIOCM_RTS)
  105. pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
  106. else
  107. pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
  108. altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
  109. }
  110. static void altera_uart_start_tx(struct uart_port *port)
  111. {
  112. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  113. pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
  114. altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
  115. }
  116. static void altera_uart_stop_tx(struct uart_port *port)
  117. {
  118. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  119. pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
  120. altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
  121. }
  122. static void altera_uart_stop_rx(struct uart_port *port)
  123. {
  124. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  125. pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
  126. altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
  127. }
  128. static void altera_uart_break_ctl(struct uart_port *port, int break_state)
  129. {
  130. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  131. unsigned long flags;
  132. spin_lock_irqsave(&port->lock, flags);
  133. if (break_state == -1)
  134. pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
  135. else
  136. pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
  137. altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
  138. spin_unlock_irqrestore(&port->lock, flags);
  139. }
  140. static void altera_uart_enable_ms(struct uart_port *port)
  141. {
  142. }
  143. static void altera_uart_set_termios(struct uart_port *port,
  144. struct ktermios *termios,
  145. struct ktermios *old)
  146. {
  147. unsigned long flags;
  148. unsigned int baud, baudclk;
  149. baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  150. baudclk = port->uartclk / baud;
  151. if (old)
  152. tty_termios_copy_hw(termios, old);
  153. tty_termios_encode_baud_rate(termios, baud, baud);
  154. spin_lock_irqsave(&port->lock, flags);
  155. uart_update_timeout(port, termios->c_cflag, baud);
  156. altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
  157. spin_unlock_irqrestore(&port->lock, flags);
  158. /*
  159. * FIXME: port->read_status_mask and port->ignore_status_mask
  160. * need to be initialized based on termios settings for
  161. * INPCK, IGNBRK, IGNPAR, PARMRK, BRKINT
  162. */
  163. }
  164. static void altera_uart_rx_chars(struct altera_uart *pp)
  165. {
  166. struct uart_port *port = &pp->port;
  167. unsigned char ch, flag;
  168. unsigned short status;
  169. while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
  170. ALTERA_UART_STATUS_RRDY_MSK) {
  171. ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
  172. flag = TTY_NORMAL;
  173. port->icount.rx++;
  174. if (status & ALTERA_UART_STATUS_E_MSK) {
  175. altera_uart_writel(port, status,
  176. ALTERA_UART_STATUS_REG);
  177. if (status & ALTERA_UART_STATUS_BRK_MSK) {
  178. port->icount.brk++;
  179. if (uart_handle_break(port))
  180. continue;
  181. } else if (status & ALTERA_UART_STATUS_PE_MSK) {
  182. port->icount.parity++;
  183. } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
  184. port->icount.overrun++;
  185. } else if (status & ALTERA_UART_STATUS_FE_MSK) {
  186. port->icount.frame++;
  187. }
  188. status &= port->read_status_mask;
  189. if (status & ALTERA_UART_STATUS_BRK_MSK)
  190. flag = TTY_BREAK;
  191. else if (status & ALTERA_UART_STATUS_PE_MSK)
  192. flag = TTY_PARITY;
  193. else if (status & ALTERA_UART_STATUS_FE_MSK)
  194. flag = TTY_FRAME;
  195. }
  196. if (uart_handle_sysrq_char(port, ch))
  197. continue;
  198. uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
  199. flag);
  200. }
  201. spin_unlock(&port->lock);
  202. tty_flip_buffer_push(&port->state->port);
  203. spin_lock(&port->lock);
  204. }
  205. static void altera_uart_tx_chars(struct altera_uart *pp)
  206. {
  207. struct uart_port *port = &pp->port;
  208. struct circ_buf *xmit = &port->state->xmit;
  209. if (port->x_char) {
  210. /* Send special char - probably flow control */
  211. altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
  212. port->x_char = 0;
  213. port->icount.tx++;
  214. return;
  215. }
  216. while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
  217. ALTERA_UART_STATUS_TRDY_MSK) {
  218. if (xmit->head == xmit->tail)
  219. break;
  220. altera_uart_writel(port, xmit->buf[xmit->tail],
  221. ALTERA_UART_TXDATA_REG);
  222. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  223. port->icount.tx++;
  224. }
  225. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  226. uart_write_wakeup(port);
  227. if (xmit->head == xmit->tail) {
  228. pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
  229. altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
  230. }
  231. }
  232. static irqreturn_t altera_uart_interrupt(int irq, void *data)
  233. {
  234. struct uart_port *port = data;
  235. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  236. unsigned int isr;
  237. isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
  238. spin_lock(&port->lock);
  239. if (isr & ALTERA_UART_STATUS_RRDY_MSK)
  240. altera_uart_rx_chars(pp);
  241. if (isr & ALTERA_UART_STATUS_TRDY_MSK)
  242. altera_uart_tx_chars(pp);
  243. spin_unlock(&port->lock);
  244. return IRQ_RETVAL(isr);
  245. }
  246. static void altera_uart_timer(unsigned long data)
  247. {
  248. struct uart_port *port = (void *)data;
  249. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  250. altera_uart_interrupt(0, port);
  251. mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
  252. }
  253. static void altera_uart_config_port(struct uart_port *port, int flags)
  254. {
  255. port->type = PORT_ALTERA_UART;
  256. /* Clear mask, so no surprise interrupts. */
  257. altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
  258. /* Clear status register */
  259. altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
  260. }
  261. static int altera_uart_startup(struct uart_port *port)
  262. {
  263. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  264. unsigned long flags;
  265. int ret;
  266. if (!port->irq) {
  267. setup_timer(&pp->tmr, altera_uart_timer, (unsigned long)port);
  268. mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
  269. return 0;
  270. }
  271. ret = request_irq(port->irq, altera_uart_interrupt, 0,
  272. DRV_NAME, port);
  273. if (ret) {
  274. pr_err(DRV_NAME ": unable to attach Altera UART %d "
  275. "interrupt vector=%d\n", port->line, port->irq);
  276. return ret;
  277. }
  278. spin_lock_irqsave(&port->lock, flags);
  279. /* Enable RX interrupts now */
  280. pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
  281. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  282. spin_unlock_irqrestore(&port->lock, flags);
  283. return 0;
  284. }
  285. static void altera_uart_shutdown(struct uart_port *port)
  286. {
  287. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  288. unsigned long flags;
  289. spin_lock_irqsave(&port->lock, flags);
  290. /* Disable all interrupts now */
  291. pp->imr = 0;
  292. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  293. spin_unlock_irqrestore(&port->lock, flags);
  294. if (port->irq)
  295. free_irq(port->irq, port);
  296. else
  297. del_timer_sync(&pp->tmr);
  298. }
  299. static const char *altera_uart_type(struct uart_port *port)
  300. {
  301. return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
  302. }
  303. static int altera_uart_request_port(struct uart_port *port)
  304. {
  305. /* UARTs always present */
  306. return 0;
  307. }
  308. static void altera_uart_release_port(struct uart_port *port)
  309. {
  310. /* Nothing to release... */
  311. }
  312. static int altera_uart_verify_port(struct uart_port *port,
  313. struct serial_struct *ser)
  314. {
  315. if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
  316. return -EINVAL;
  317. return 0;
  318. }
  319. #ifdef CONFIG_CONSOLE_POLL
  320. static int altera_uart_poll_get_char(struct uart_port *port)
  321. {
  322. while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
  323. ALTERA_UART_STATUS_RRDY_MSK))
  324. cpu_relax();
  325. return altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
  326. }
  327. static void altera_uart_poll_put_char(struct uart_port *port, unsigned char c)
  328. {
  329. while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
  330. ALTERA_UART_STATUS_TRDY_MSK))
  331. cpu_relax();
  332. altera_uart_writel(port, c, ALTERA_UART_TXDATA_REG);
  333. }
  334. #endif
  335. /*
  336. * Define the basic serial functions we support.
  337. */
  338. static struct uart_ops altera_uart_ops = {
  339. .tx_empty = altera_uart_tx_empty,
  340. .get_mctrl = altera_uart_get_mctrl,
  341. .set_mctrl = altera_uart_set_mctrl,
  342. .start_tx = altera_uart_start_tx,
  343. .stop_tx = altera_uart_stop_tx,
  344. .stop_rx = altera_uart_stop_rx,
  345. .enable_ms = altera_uart_enable_ms,
  346. .break_ctl = altera_uart_break_ctl,
  347. .startup = altera_uart_startup,
  348. .shutdown = altera_uart_shutdown,
  349. .set_termios = altera_uart_set_termios,
  350. .type = altera_uart_type,
  351. .request_port = altera_uart_request_port,
  352. .release_port = altera_uart_release_port,
  353. .config_port = altera_uart_config_port,
  354. .verify_port = altera_uart_verify_port,
  355. #ifdef CONFIG_CONSOLE_POLL
  356. .poll_get_char = altera_uart_poll_get_char,
  357. .poll_put_char = altera_uart_poll_put_char,
  358. #endif
  359. };
  360. static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
  361. #if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
  362. static void altera_uart_console_putc(struct uart_port *port, const char c)
  363. {
  364. while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
  365. ALTERA_UART_STATUS_TRDY_MSK))
  366. cpu_relax();
  367. writel(c, port->membase + ALTERA_UART_TXDATA_REG);
  368. }
  369. static void altera_uart_console_write(struct console *co, const char *s,
  370. unsigned int count)
  371. {
  372. struct uart_port *port = &(altera_uart_ports + co->index)->port;
  373. for (; count; count--, s++) {
  374. altera_uart_console_putc(port, *s);
  375. if (*s == '\n')
  376. altera_uart_console_putc(port, '\r');
  377. }
  378. }
  379. static int __init altera_uart_console_setup(struct console *co, char *options)
  380. {
  381. struct uart_port *port;
  382. int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
  383. int bits = 8;
  384. int parity = 'n';
  385. int flow = 'n';
  386. if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
  387. return -EINVAL;
  388. port = &altera_uart_ports[co->index].port;
  389. if (!port->membase)
  390. return -ENODEV;
  391. if (options)
  392. uart_parse_options(options, &baud, &parity, &bits, &flow);
  393. return uart_set_options(port, co, baud, parity, bits, flow);
  394. }
  395. static struct uart_driver altera_uart_driver;
  396. static struct console altera_uart_console = {
  397. .name = "ttyAL",
  398. .write = altera_uart_console_write,
  399. .device = uart_console_device,
  400. .setup = altera_uart_console_setup,
  401. .flags = CON_PRINTBUFFER,
  402. .index = -1,
  403. .data = &altera_uart_driver,
  404. };
  405. static int __init altera_uart_console_init(void)
  406. {
  407. register_console(&altera_uart_console);
  408. return 0;
  409. }
  410. console_initcall(altera_uart_console_init);
  411. #define ALTERA_UART_CONSOLE (&altera_uart_console)
  412. #else
  413. #define ALTERA_UART_CONSOLE NULL
  414. #endif /* CONFIG_ALTERA_UART_CONSOLE */
  415. /*
  416. * Define the altera_uart UART driver structure.
  417. */
  418. static struct uart_driver altera_uart_driver = {
  419. .owner = THIS_MODULE,
  420. .driver_name = DRV_NAME,
  421. .dev_name = "ttyAL",
  422. .major = SERIAL_ALTERA_MAJOR,
  423. .minor = SERIAL_ALTERA_MINOR,
  424. .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
  425. .cons = ALTERA_UART_CONSOLE,
  426. };
  427. #ifdef CONFIG_OF
  428. static int altera_uart_get_of_uartclk(struct platform_device *pdev,
  429. struct uart_port *port)
  430. {
  431. int len;
  432. const __be32 *clk;
  433. clk = of_get_property(pdev->dev.of_node, "clock-frequency", &len);
  434. if (!clk || len < sizeof(__be32))
  435. return -ENODEV;
  436. port->uartclk = be32_to_cpup(clk);
  437. return 0;
  438. }
  439. #else
  440. static int altera_uart_get_of_uartclk(struct platform_device *pdev,
  441. struct uart_port *port)
  442. {
  443. return -ENODEV;
  444. }
  445. #endif /* CONFIG_OF */
  446. static int altera_uart_probe(struct platform_device *pdev)
  447. {
  448. struct altera_uart_platform_uart *platp = dev_get_platdata(&pdev->dev);
  449. struct uart_port *port;
  450. struct resource *res_mem;
  451. struct resource *res_irq;
  452. int i = pdev->id;
  453. int ret;
  454. /* if id is -1 scan for a free id and use that one */
  455. if (i == -1) {
  456. for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++)
  457. if (altera_uart_ports[i].port.mapbase == 0)
  458. break;
  459. }
  460. if (i < 0 || i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
  461. return -EINVAL;
  462. port = &altera_uart_ports[i].port;
  463. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  464. if (res_mem)
  465. port->mapbase = res_mem->start;
  466. else if (platp)
  467. port->mapbase = platp->mapbase;
  468. else
  469. return -EINVAL;
  470. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  471. if (res_irq)
  472. port->irq = res_irq->start;
  473. else if (platp)
  474. port->irq = platp->irq;
  475. /* Check platform data first so we can override device node data */
  476. if (platp)
  477. port->uartclk = platp->uartclk;
  478. else {
  479. ret = altera_uart_get_of_uartclk(pdev, port);
  480. if (ret)
  481. return ret;
  482. }
  483. port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
  484. if (!port->membase)
  485. return -ENOMEM;
  486. if (platp)
  487. port->regshift = platp->bus_shift;
  488. else
  489. port->regshift = 0;
  490. port->line = i;
  491. port->type = PORT_ALTERA_UART;
  492. port->iotype = SERIAL_IO_MEM;
  493. port->ops = &altera_uart_ops;
  494. port->flags = UPF_BOOT_AUTOCONF;
  495. platform_set_drvdata(pdev, port);
  496. uart_add_one_port(&altera_uart_driver, port);
  497. return 0;
  498. }
  499. static int altera_uart_remove(struct platform_device *pdev)
  500. {
  501. struct uart_port *port = platform_get_drvdata(pdev);
  502. if (port) {
  503. uart_remove_one_port(&altera_uart_driver, port);
  504. port->mapbase = 0;
  505. }
  506. return 0;
  507. }
  508. #ifdef CONFIG_OF
  509. static struct of_device_id altera_uart_match[] = {
  510. { .compatible = "ALTR,uart-1.0", },
  511. { .compatible = "altr,uart-1.0", },
  512. {},
  513. };
  514. MODULE_DEVICE_TABLE(of, altera_uart_match);
  515. #endif /* CONFIG_OF */
  516. static struct platform_driver altera_uart_platform_driver = {
  517. .probe = altera_uart_probe,
  518. .remove = altera_uart_remove,
  519. .driver = {
  520. .name = DRV_NAME,
  521. .owner = THIS_MODULE,
  522. .of_match_table = of_match_ptr(altera_uart_match),
  523. },
  524. };
  525. static int __init altera_uart_init(void)
  526. {
  527. int rc;
  528. rc = uart_register_driver(&altera_uart_driver);
  529. if (rc)
  530. return rc;
  531. rc = platform_driver_register(&altera_uart_platform_driver);
  532. if (rc)
  533. uart_unregister_driver(&altera_uart_driver);
  534. return rc;
  535. }
  536. static void __exit altera_uart_exit(void)
  537. {
  538. platform_driver_unregister(&altera_uart_platform_driver);
  539. uart_unregister_driver(&altera_uart_driver);
  540. }
  541. module_init(altera_uart_init);
  542. module_exit(altera_uart_exit);
  543. MODULE_DESCRIPTION("Altera UART driver");
  544. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  545. MODULE_LICENSE("GPL");
  546. MODULE_ALIAS("platform:" DRV_NAME);
  547. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR);