altera_uart.c 18 KB

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