mcf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /****************************************************************************/
  2. /*
  3. * mcf.c -- Freescale ColdFire UART driver
  4. *
  5. * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /****************************************************************************/
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/console.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/serial.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/io.h>
  23. #include <asm/coldfire.h>
  24. #include <asm/mcfsim.h>
  25. #include <asm/mcfuart.h>
  26. #include <asm/nettel.h>
  27. /****************************************************************************/
  28. /*
  29. * Some boards implement the DTR/DCD lines using GPIO lines, most
  30. * don't. Dummy out the access macros for those that don't. Those
  31. * that do should define these macros somewhere in there board
  32. * specific inlude files.
  33. */
  34. #if !defined(mcf_getppdcd)
  35. #define mcf_getppdcd(p) (1)
  36. #endif
  37. #if !defined(mcf_getppdtr)
  38. #define mcf_getppdtr(p) (1)
  39. #endif
  40. #if !defined(mcf_setppdtr)
  41. #define mcf_setppdtr(p, v) do { } while (0)
  42. #endif
  43. /****************************************************************************/
  44. /*
  45. * Local per-uart structure.
  46. */
  47. struct mcf_uart {
  48. struct uart_port port;
  49. unsigned int sigs; /* Local copy of line sigs */
  50. unsigned char imr; /* Local IMR mirror */
  51. };
  52. /****************************************************************************/
  53. static unsigned int mcf_tx_empty(struct uart_port *port)
  54. {
  55. return (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXEMPTY) ?
  56. TIOCSER_TEMT : 0;
  57. }
  58. /****************************************************************************/
  59. static unsigned int mcf_get_mctrl(struct uart_port *port)
  60. {
  61. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  62. unsigned int sigs;
  63. sigs = (readb(port->membase + MCFUART_UIPR) & MCFUART_UIPR_CTS) ?
  64. 0 : TIOCM_CTS;
  65. sigs |= (pp->sigs & TIOCM_RTS);
  66. sigs |= (mcf_getppdcd(port->line) ? TIOCM_CD : 0);
  67. sigs |= (mcf_getppdtr(port->line) ? TIOCM_DTR : 0);
  68. return sigs;
  69. }
  70. /****************************************************************************/
  71. static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
  72. {
  73. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  74. pp->sigs = sigs;
  75. mcf_setppdtr(port->line, (sigs & TIOCM_DTR));
  76. if (sigs & TIOCM_RTS)
  77. writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP1);
  78. else
  79. writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP0);
  80. }
  81. /****************************************************************************/
  82. static void mcf_start_tx(struct uart_port *port)
  83. {
  84. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  85. pp->imr |= MCFUART_UIR_TXREADY;
  86. writeb(pp->imr, port->membase + MCFUART_UIMR);
  87. }
  88. /****************************************************************************/
  89. static void mcf_stop_tx(struct uart_port *port)
  90. {
  91. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  92. pp->imr &= ~MCFUART_UIR_TXREADY;
  93. writeb(pp->imr, port->membase + MCFUART_UIMR);
  94. }
  95. /****************************************************************************/
  96. static void mcf_stop_rx(struct uart_port *port)
  97. {
  98. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  99. pp->imr &= ~MCFUART_UIR_RXREADY;
  100. writeb(pp->imr, port->membase + MCFUART_UIMR);
  101. }
  102. /****************************************************************************/
  103. static void mcf_break_ctl(struct uart_port *port, int break_state)
  104. {
  105. unsigned long flags;
  106. spin_lock_irqsave(&port->lock, flags);
  107. if (break_state == -1)
  108. writeb(MCFUART_UCR_CMDBREAKSTART, port->membase + MCFUART_UCR);
  109. else
  110. writeb(MCFUART_UCR_CMDBREAKSTOP, port->membase + MCFUART_UCR);
  111. spin_unlock_irqrestore(&port->lock, flags);
  112. }
  113. /****************************************************************************/
  114. static void mcf_enable_ms(struct uart_port *port)
  115. {
  116. }
  117. /****************************************************************************/
  118. static int mcf_startup(struct uart_port *port)
  119. {
  120. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  121. unsigned long flags;
  122. spin_lock_irqsave(&port->lock, flags);
  123. /* Reset UART, get it into known state... */
  124. writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
  125. writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
  126. /* Enable the UART transmitter and receiver */
  127. writeb(MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE,
  128. port->membase + MCFUART_UCR);
  129. /* Enable RX interrupts now */
  130. pp->imr = MCFUART_UIR_RXREADY;
  131. writeb(pp->imr, port->membase + MCFUART_UIMR);
  132. spin_unlock_irqrestore(&port->lock, flags);
  133. return 0;
  134. }
  135. /****************************************************************************/
  136. static void mcf_shutdown(struct uart_port *port)
  137. {
  138. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  139. unsigned long flags;
  140. spin_lock_irqsave(&port->lock, flags);
  141. /* Disable all interrupts now */
  142. pp->imr = 0;
  143. writeb(pp->imr, port->membase + MCFUART_UIMR);
  144. /* Disable UART transmitter and receiver */
  145. writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
  146. writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
  147. spin_unlock_irqrestore(&port->lock, flags);
  148. }
  149. /****************************************************************************/
  150. static void mcf_set_termios(struct uart_port *port, struct ktermios *termios,
  151. struct ktermios *old)
  152. {
  153. unsigned long flags;
  154. unsigned int baud, baudclk;
  155. #if defined(CONFIG_M5272)
  156. unsigned int baudfr;
  157. #endif
  158. unsigned char mr1, mr2;
  159. baud = uart_get_baud_rate(port, termios, old, 0, 230400);
  160. #if defined(CONFIG_M5272)
  161. baudclk = (MCF_BUSCLK / baud) / 32;
  162. baudfr = (((MCF_BUSCLK / baud) + 1) / 2) % 16;
  163. #else
  164. baudclk = ((MCF_BUSCLK / baud) + 16) / 32;
  165. #endif
  166. mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
  167. mr2 = 0;
  168. switch (termios->c_cflag & CSIZE) {
  169. case CS5: mr1 |= MCFUART_MR1_CS5; break;
  170. case CS6: mr1 |= MCFUART_MR1_CS6; break;
  171. case CS7: mr1 |= MCFUART_MR1_CS7; break;
  172. case CS8:
  173. default: mr1 |= MCFUART_MR1_CS8; break;
  174. }
  175. if (termios->c_cflag & PARENB) {
  176. if (termios->c_cflag & CMSPAR) {
  177. if (termios->c_cflag & PARODD)
  178. mr1 |= MCFUART_MR1_PARITYMARK;
  179. else
  180. mr1 |= MCFUART_MR1_PARITYSPACE;
  181. } else {
  182. if (termios->c_cflag & PARODD)
  183. mr1 |= MCFUART_MR1_PARITYODD;
  184. else
  185. mr1 |= MCFUART_MR1_PARITYEVEN;
  186. }
  187. } else {
  188. mr1 |= MCFUART_MR1_PARITYNONE;
  189. }
  190. if (termios->c_cflag & CSTOPB)
  191. mr2 |= MCFUART_MR2_STOP2;
  192. else
  193. mr2 |= MCFUART_MR2_STOP1;
  194. if (termios->c_cflag & CRTSCTS) {
  195. mr1 |= MCFUART_MR1_RXRTS;
  196. mr2 |= MCFUART_MR2_TXCTS;
  197. }
  198. spin_lock_irqsave(&port->lock, flags);
  199. uart_update_timeout(port, termios->c_cflag, baud);
  200. writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
  201. writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
  202. writeb(MCFUART_UCR_CMDRESETMRPTR, port->membase + MCFUART_UCR);
  203. writeb(mr1, port->membase + MCFUART_UMR);
  204. writeb(mr2, port->membase + MCFUART_UMR);
  205. writeb((baudclk & 0xff00) >> 8, port->membase + MCFUART_UBG1);
  206. writeb((baudclk & 0xff), port->membase + MCFUART_UBG2);
  207. #if defined(CONFIG_M5272)
  208. writeb((baudfr & 0x0f), port->membase + MCFUART_UFPD);
  209. #endif
  210. writeb(MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER,
  211. port->membase + MCFUART_UCSR);
  212. writeb(MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE,
  213. port->membase + MCFUART_UCR);
  214. spin_unlock_irqrestore(&port->lock, flags);
  215. }
  216. /****************************************************************************/
  217. static void mcf_rx_chars(struct mcf_uart *pp)
  218. {
  219. struct uart_port *port = &pp->port;
  220. unsigned char status, ch, flag;
  221. while ((status = readb(port->membase + MCFUART_USR)) & MCFUART_USR_RXREADY) {
  222. ch = readb(port->membase + MCFUART_URB);
  223. flag = TTY_NORMAL;
  224. port->icount.rx++;
  225. if (status & MCFUART_USR_RXERR) {
  226. writeb(MCFUART_UCR_CMDRESETERR,
  227. port->membase + MCFUART_UCR);
  228. if (status & MCFUART_USR_RXBREAK) {
  229. port->icount.brk++;
  230. if (uart_handle_break(port))
  231. continue;
  232. } else if (status & MCFUART_USR_RXPARITY) {
  233. port->icount.parity++;
  234. } else if (status & MCFUART_USR_RXOVERRUN) {
  235. port->icount.overrun++;
  236. } else if (status & MCFUART_USR_RXFRAMING) {
  237. port->icount.frame++;
  238. }
  239. status &= port->read_status_mask;
  240. if (status & MCFUART_USR_RXBREAK)
  241. flag = TTY_BREAK;
  242. else if (status & MCFUART_USR_RXPARITY)
  243. flag = TTY_PARITY;
  244. else if (status & MCFUART_USR_RXFRAMING)
  245. flag = TTY_FRAME;
  246. }
  247. if (uart_handle_sysrq_char(port, ch))
  248. continue;
  249. uart_insert_char(port, status, MCFUART_USR_RXOVERRUN, ch, flag);
  250. }
  251. tty_flip_buffer_push(port->state->port.tty);
  252. }
  253. /****************************************************************************/
  254. static void mcf_tx_chars(struct mcf_uart *pp)
  255. {
  256. struct uart_port *port = &pp->port;
  257. struct circ_buf *xmit = &port->state->xmit;
  258. if (port->x_char) {
  259. /* Send special char - probably flow control */
  260. writeb(port->x_char, port->membase + MCFUART_UTB);
  261. port->x_char = 0;
  262. port->icount.tx++;
  263. return;
  264. }
  265. while (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY) {
  266. if (xmit->head == xmit->tail)
  267. break;
  268. writeb(xmit->buf[xmit->tail], port->membase + MCFUART_UTB);
  269. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
  270. port->icount.tx++;
  271. }
  272. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  273. uart_write_wakeup(port);
  274. if (xmit->head == xmit->tail) {
  275. pp->imr &= ~MCFUART_UIR_TXREADY;
  276. writeb(pp->imr, port->membase + MCFUART_UIMR);
  277. }
  278. }
  279. /****************************************************************************/
  280. static irqreturn_t mcf_interrupt(int irq, void *data)
  281. {
  282. struct uart_port *port = data;
  283. struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
  284. unsigned int isr;
  285. irqreturn_t ret = IRQ_NONE;
  286. isr = readb(port->membase + MCFUART_UISR) & pp->imr;
  287. spin_lock(&port->lock);
  288. if (isr & MCFUART_UIR_RXREADY) {
  289. mcf_rx_chars(pp);
  290. ret = IRQ_HANDLED;
  291. }
  292. if (isr & MCFUART_UIR_TXREADY) {
  293. mcf_tx_chars(pp);
  294. ret = IRQ_HANDLED;
  295. }
  296. spin_unlock(&port->lock);
  297. return ret;
  298. }
  299. /****************************************************************************/
  300. static void mcf_config_port(struct uart_port *port, int flags)
  301. {
  302. port->type = PORT_MCF;
  303. port->fifosize = MCFUART_TXFIFOSIZE;
  304. /* Clear mask, so no surprise interrupts. */
  305. writeb(0, port->membase + MCFUART_UIMR);
  306. if (request_irq(port->irq, mcf_interrupt, IRQF_DISABLED, "UART", port))
  307. printk(KERN_ERR "MCF: unable to attach ColdFire UART %d "
  308. "interrupt vector=%d\n", port->line, port->irq);
  309. }
  310. /****************************************************************************/
  311. static const char *mcf_type(struct uart_port *port)
  312. {
  313. return (port->type == PORT_MCF) ? "ColdFire UART" : NULL;
  314. }
  315. /****************************************************************************/
  316. static int mcf_request_port(struct uart_port *port)
  317. {
  318. /* UARTs always present */
  319. return 0;
  320. }
  321. /****************************************************************************/
  322. static void mcf_release_port(struct uart_port *port)
  323. {
  324. /* Nothing to release... */
  325. }
  326. /****************************************************************************/
  327. static int mcf_verify_port(struct uart_port *port, struct serial_struct *ser)
  328. {
  329. if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_MCF))
  330. return -EINVAL;
  331. return 0;
  332. }
  333. /****************************************************************************/
  334. /*
  335. * Define the basic serial functions we support.
  336. */
  337. static const struct uart_ops mcf_uart_ops = {
  338. .tx_empty = mcf_tx_empty,
  339. .get_mctrl = mcf_get_mctrl,
  340. .set_mctrl = mcf_set_mctrl,
  341. .start_tx = mcf_start_tx,
  342. .stop_tx = mcf_stop_tx,
  343. .stop_rx = mcf_stop_rx,
  344. .enable_ms = mcf_enable_ms,
  345. .break_ctl = mcf_break_ctl,
  346. .startup = mcf_startup,
  347. .shutdown = mcf_shutdown,
  348. .set_termios = mcf_set_termios,
  349. .type = mcf_type,
  350. .request_port = mcf_request_port,
  351. .release_port = mcf_release_port,
  352. .config_port = mcf_config_port,
  353. .verify_port = mcf_verify_port,
  354. };
  355. static struct mcf_uart mcf_ports[4];
  356. #define MCF_MAXPORTS ARRAY_SIZE(mcf_ports)
  357. /****************************************************************************/
  358. #if defined(CONFIG_SERIAL_MCF_CONSOLE)
  359. /****************************************************************************/
  360. int __init early_mcf_setup(struct mcf_platform_uart *platp)
  361. {
  362. struct uart_port *port;
  363. int i;
  364. for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
  365. port = &mcf_ports[i].port;
  366. port->line = i;
  367. port->type = PORT_MCF;
  368. port->mapbase = platp[i].mapbase;
  369. port->membase = (platp[i].membase) ? platp[i].membase :
  370. (unsigned char __iomem *) port->mapbase;
  371. port->iotype = SERIAL_IO_MEM;
  372. port->irq = platp[i].irq;
  373. port->uartclk = MCF_BUSCLK;
  374. port->flags = ASYNC_BOOT_AUTOCONF;
  375. port->ops = &mcf_uart_ops;
  376. }
  377. return 0;
  378. }
  379. /****************************************************************************/
  380. static void mcf_console_putc(struct console *co, const char c)
  381. {
  382. struct uart_port *port = &(mcf_ports + co->index)->port;
  383. int i;
  384. for (i = 0; (i < 0x10000); i++) {
  385. if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
  386. break;
  387. }
  388. writeb(c, port->membase + MCFUART_UTB);
  389. for (i = 0; (i < 0x10000); i++) {
  390. if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
  391. break;
  392. }
  393. }
  394. /****************************************************************************/
  395. static void mcf_console_write(struct console *co, const char *s, unsigned int count)
  396. {
  397. for (; (count); count--, s++) {
  398. mcf_console_putc(co, *s);
  399. if (*s == '\n')
  400. mcf_console_putc(co, '\r');
  401. }
  402. }
  403. /****************************************************************************/
  404. static int __init mcf_console_setup(struct console *co, char *options)
  405. {
  406. struct uart_port *port;
  407. int baud = CONFIG_SERIAL_MCF_BAUDRATE;
  408. int bits = 8;
  409. int parity = 'n';
  410. int flow = 'n';
  411. if ((co->index < 0) || (co->index >= MCF_MAXPORTS))
  412. co->index = 0;
  413. port = &mcf_ports[co->index].port;
  414. if (port->membase == 0)
  415. return -ENODEV;
  416. if (options)
  417. uart_parse_options(options, &baud, &parity, &bits, &flow);
  418. return uart_set_options(port, co, baud, parity, bits, flow);
  419. }
  420. /****************************************************************************/
  421. static struct uart_driver mcf_driver;
  422. static struct console mcf_console = {
  423. .name = "ttyS",
  424. .write = mcf_console_write,
  425. .device = uart_console_device,
  426. .setup = mcf_console_setup,
  427. .flags = CON_PRINTBUFFER,
  428. .index = -1,
  429. .data = &mcf_driver,
  430. };
  431. static int __init mcf_console_init(void)
  432. {
  433. register_console(&mcf_console);
  434. return 0;
  435. }
  436. console_initcall(mcf_console_init);
  437. #define MCF_CONSOLE &mcf_console
  438. /****************************************************************************/
  439. #else
  440. /****************************************************************************/
  441. #define MCF_CONSOLE NULL
  442. /****************************************************************************/
  443. #endif /* CONFIG_MCF_CONSOLE */
  444. /****************************************************************************/
  445. /*
  446. * Define the mcf UART driver structure.
  447. */
  448. static struct uart_driver mcf_driver = {
  449. .owner = THIS_MODULE,
  450. .driver_name = "mcf",
  451. .dev_name = "ttyS",
  452. .major = TTY_MAJOR,
  453. .minor = 64,
  454. .nr = MCF_MAXPORTS,
  455. .cons = MCF_CONSOLE,
  456. };
  457. /****************************************************************************/
  458. static int __devinit mcf_probe(struct platform_device *pdev)
  459. {
  460. struct mcf_platform_uart *platp = pdev->dev.platform_data;
  461. struct uart_port *port;
  462. int i;
  463. for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
  464. port = &mcf_ports[i].port;
  465. port->line = i;
  466. port->type = PORT_MCF;
  467. port->mapbase = platp[i].mapbase;
  468. port->membase = (platp[i].membase) ? platp[i].membase :
  469. (unsigned char __iomem *) platp[i].mapbase;
  470. port->iotype = SERIAL_IO_MEM;
  471. port->irq = platp[i].irq;
  472. port->uartclk = MCF_BUSCLK;
  473. port->ops = &mcf_uart_ops;
  474. port->flags = ASYNC_BOOT_AUTOCONF;
  475. uart_add_one_port(&mcf_driver, port);
  476. }
  477. return 0;
  478. }
  479. /****************************************************************************/
  480. static int __devexit mcf_remove(struct platform_device *pdev)
  481. {
  482. struct uart_port *port;
  483. int i;
  484. for (i = 0; (i < MCF_MAXPORTS); i++) {
  485. port = &mcf_ports[i].port;
  486. if (port)
  487. uart_remove_one_port(&mcf_driver, port);
  488. }
  489. return 0;
  490. }
  491. /****************************************************************************/
  492. static struct platform_driver mcf_platform_driver = {
  493. .probe = mcf_probe,
  494. .remove = __devexit_p(mcf_remove),
  495. .driver = {
  496. .name = "mcfuart",
  497. .owner = THIS_MODULE,
  498. },
  499. };
  500. /****************************************************************************/
  501. static int __init mcf_init(void)
  502. {
  503. int rc;
  504. printk("ColdFire internal UART serial driver\n");
  505. rc = uart_register_driver(&mcf_driver);
  506. if (rc)
  507. return rc;
  508. rc = platform_driver_register(&mcf_platform_driver);
  509. if (rc)
  510. return rc;
  511. return 0;
  512. }
  513. /****************************************************************************/
  514. static void __exit mcf_exit(void)
  515. {
  516. platform_driver_unregister(&mcf_platform_driver);
  517. uart_unregister_driver(&mcf_driver);
  518. }
  519. /****************************************************************************/
  520. module_init(mcf_init);
  521. module_exit(mcf_exit);
  522. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>");
  523. MODULE_DESCRIPTION("Freescale ColdFire UART driver");
  524. MODULE_LICENSE("GPL");
  525. MODULE_ALIAS("platform:mcfuart");
  526. /****************************************************************************/