mcf.c 17 KB

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