atmel_serial.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*
  2. * linux/drivers/char/atmel_serial.c
  3. *
  4. * Driver for Atmel AT91 / AT32 Serial ports
  5. * Copyright (C) 2003 Rick Bronson
  6. *
  7. * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
  8. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  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. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/tty.h>
  27. #include <linux/ioport.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/serial.h>
  31. #include <linux/clk.h>
  32. #include <linux/console.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/tty_flip.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/atmel_pdc.h>
  37. #include <asm/io.h>
  38. #include <asm/mach/serial_at91.h>
  39. #include <asm/arch/board.h>
  40. #ifdef CONFIG_ARM
  41. #include <asm/arch/cpu.h>
  42. #include <asm/arch/gpio.h>
  43. #endif
  44. #include "atmel_serial.h"
  45. #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  46. #define SUPPORT_SYSRQ
  47. #endif
  48. #include <linux/serial_core.h>
  49. #ifdef CONFIG_SERIAL_ATMEL_TTYAT
  50. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  51. * should coexist with the 8250 driver, such as if we have an external 16C550
  52. * UART. */
  53. #define SERIAL_ATMEL_MAJOR 204
  54. #define MINOR_START 154
  55. #define ATMEL_DEVICENAME "ttyAT"
  56. #else
  57. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  58. * name, but it is legally reserved for the 8250 driver. */
  59. #define SERIAL_ATMEL_MAJOR TTY_MAJOR
  60. #define MINOR_START 64
  61. #define ATMEL_DEVICENAME "ttyS"
  62. #endif
  63. #define ATMEL_ISR_PASS_LIMIT 256
  64. #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  65. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  66. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  67. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  68. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  69. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  70. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  71. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  72. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  73. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  74. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  75. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  76. // #define UART_GET_CR(port) __raw_readl((port)->membase + ATMEL_US_CR) // is write-only
  77. /* PDC registers */
  78. #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
  79. #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
  80. #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
  81. #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
  82. #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
  83. #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
  84. #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
  85. #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
  86. #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
  87. //#define UART_PUT_TNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNPR)
  88. //#define UART_PUT_TNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNCR)
  89. static int (*atmel_open_hook)(struct uart_port *);
  90. static void (*atmel_close_hook)(struct uart_port *);
  91. /*
  92. * We wrap our port structure around the generic uart_port.
  93. */
  94. struct atmel_uart_port {
  95. struct uart_port uart; /* uart */
  96. struct clk *clk; /* uart clock */
  97. unsigned short suspended; /* is port suspended? */
  98. int break_active; /* break being received */
  99. };
  100. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  101. #ifdef SUPPORT_SYSRQ
  102. static struct console atmel_console;
  103. #endif
  104. /*
  105. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  106. */
  107. static u_int atmel_tx_empty(struct uart_port *port)
  108. {
  109. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  110. }
  111. /*
  112. * Set state of the modem control output lines
  113. */
  114. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  115. {
  116. unsigned int control = 0;
  117. unsigned int mode;
  118. #ifdef CONFIG_ARCH_AT91RM9200
  119. if (cpu_is_at91rm9200()) {
  120. /*
  121. * AT91RM9200 Errata #39: RTS0 is not internally connected to PA21.
  122. * We need to drive the pin manually.
  123. */
  124. if (port->mapbase == AT91RM9200_BASE_US0) {
  125. if (mctrl & TIOCM_RTS)
  126. at91_set_gpio_value(AT91_PIN_PA21, 0);
  127. else
  128. at91_set_gpio_value(AT91_PIN_PA21, 1);
  129. }
  130. }
  131. #endif
  132. if (mctrl & TIOCM_RTS)
  133. control |= ATMEL_US_RTSEN;
  134. else
  135. control |= ATMEL_US_RTSDIS;
  136. if (mctrl & TIOCM_DTR)
  137. control |= ATMEL_US_DTREN;
  138. else
  139. control |= ATMEL_US_DTRDIS;
  140. UART_PUT_CR(port, control);
  141. /* Local loopback mode? */
  142. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  143. if (mctrl & TIOCM_LOOP)
  144. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  145. else
  146. mode |= ATMEL_US_CHMODE_NORMAL;
  147. UART_PUT_MR(port, mode);
  148. }
  149. /*
  150. * Get state of the modem control input lines
  151. */
  152. static u_int atmel_get_mctrl(struct uart_port *port)
  153. {
  154. unsigned int status, ret = 0;
  155. status = UART_GET_CSR(port);
  156. /*
  157. * The control signals are active low.
  158. */
  159. if (!(status & ATMEL_US_DCD))
  160. ret |= TIOCM_CD;
  161. if (!(status & ATMEL_US_CTS))
  162. ret |= TIOCM_CTS;
  163. if (!(status & ATMEL_US_DSR))
  164. ret |= TIOCM_DSR;
  165. if (!(status & ATMEL_US_RI))
  166. ret |= TIOCM_RI;
  167. return ret;
  168. }
  169. /*
  170. * Stop transmitting.
  171. */
  172. static void atmel_stop_tx(struct uart_port *port)
  173. {
  174. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  175. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  176. }
  177. /*
  178. * Start transmitting.
  179. */
  180. static void atmel_start_tx(struct uart_port *port)
  181. {
  182. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  183. UART_PUT_IER(port, ATMEL_US_TXRDY);
  184. }
  185. /*
  186. * Stop receiving - port is in process of being closed.
  187. */
  188. static void atmel_stop_rx(struct uart_port *port)
  189. {
  190. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  191. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  192. }
  193. /*
  194. * Enable modem status interrupts
  195. */
  196. static void atmel_enable_ms(struct uart_port *port)
  197. {
  198. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  199. }
  200. /*
  201. * Control the transmission of a break signal
  202. */
  203. static void atmel_break_ctl(struct uart_port *port, int break_state)
  204. {
  205. if (break_state != 0)
  206. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  207. else
  208. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  209. }
  210. /*
  211. * Characters received (called from interrupt handler)
  212. */
  213. static void atmel_rx_chars(struct uart_port *port)
  214. {
  215. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  216. struct tty_struct *tty = port->info->tty;
  217. unsigned int status, ch, flg;
  218. status = UART_GET_CSR(port);
  219. while (status & ATMEL_US_RXRDY) {
  220. ch = UART_GET_CHAR(port);
  221. port->icount.rx++;
  222. flg = TTY_NORMAL;
  223. /*
  224. * note that the error handling code is
  225. * out of the main execution path
  226. */
  227. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  228. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  229. || atmel_port->break_active)) {
  230. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* clear error */
  231. if (status & ATMEL_US_RXBRK
  232. && !atmel_port->break_active) {
  233. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); /* ignore side-effect */
  234. port->icount.brk++;
  235. atmel_port->break_active = 1;
  236. UART_PUT_IER(port, ATMEL_US_RXBRK);
  237. if (uart_handle_break(port))
  238. goto ignore_char;
  239. } else {
  240. /*
  241. * This is either the end-of-break
  242. * condition or we've received at
  243. * least one character without RXBRK
  244. * being set. In both cases, the next
  245. * RXBRK will indicate start-of-break.
  246. */
  247. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  248. status &= ~ATMEL_US_RXBRK;
  249. atmel_port->break_active = 0;
  250. }
  251. if (status & ATMEL_US_PARE)
  252. port->icount.parity++;
  253. if (status & ATMEL_US_FRAME)
  254. port->icount.frame++;
  255. if (status & ATMEL_US_OVRE)
  256. port->icount.overrun++;
  257. status &= port->read_status_mask;
  258. if (status & ATMEL_US_RXBRK)
  259. flg = TTY_BREAK;
  260. else if (status & ATMEL_US_PARE)
  261. flg = TTY_PARITY;
  262. else if (status & ATMEL_US_FRAME)
  263. flg = TTY_FRAME;
  264. }
  265. if (uart_handle_sysrq_char(port, ch))
  266. goto ignore_char;
  267. uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg);
  268. ignore_char:
  269. status = UART_GET_CSR(port);
  270. }
  271. tty_flip_buffer_push(tty);
  272. }
  273. /*
  274. * Transmit characters (called from interrupt handler)
  275. */
  276. static void atmel_tx_chars(struct uart_port *port)
  277. {
  278. struct circ_buf *xmit = &port->info->xmit;
  279. if (port->x_char) {
  280. UART_PUT_CHAR(port, port->x_char);
  281. port->icount.tx++;
  282. port->x_char = 0;
  283. return;
  284. }
  285. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  286. atmel_stop_tx(port);
  287. return;
  288. }
  289. while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  290. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  291. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  292. port->icount.tx++;
  293. if (uart_circ_empty(xmit))
  294. break;
  295. }
  296. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  297. uart_write_wakeup(port);
  298. if (uart_circ_empty(xmit))
  299. atmel_stop_tx(port);
  300. }
  301. /*
  302. * Interrupt handler
  303. */
  304. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  305. {
  306. struct uart_port *port = dev_id;
  307. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  308. unsigned int status, pending, pass_counter = 0;
  309. status = UART_GET_CSR(port);
  310. pending = status & UART_GET_IMR(port);
  311. while (pending) {
  312. /* Interrupt receive */
  313. if (pending & ATMEL_US_RXRDY)
  314. atmel_rx_chars(port);
  315. else if (pending & ATMEL_US_RXBRK) {
  316. /*
  317. * End of break detected. If it came along
  318. * with a character, atmel_rx_chars will
  319. * handle it.
  320. */
  321. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  322. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  323. atmel_port->break_active = 0;
  324. }
  325. // TODO: All reads to CSR will clear these interrupts!
  326. if (pending & ATMEL_US_RIIC) port->icount.rng++;
  327. if (pending & ATMEL_US_DSRIC) port->icount.dsr++;
  328. if (pending & ATMEL_US_DCDIC)
  329. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  330. if (pending & ATMEL_US_CTSIC)
  331. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  332. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC))
  333. wake_up_interruptible(&port->info->delta_msr_wait);
  334. /* Interrupt transmit */
  335. if (pending & ATMEL_US_TXRDY)
  336. atmel_tx_chars(port);
  337. if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
  338. break;
  339. status = UART_GET_CSR(port);
  340. pending = status & UART_GET_IMR(port);
  341. }
  342. return IRQ_HANDLED;
  343. }
  344. /*
  345. * Perform initialization and enable port for reception
  346. */
  347. static int atmel_startup(struct uart_port *port)
  348. {
  349. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  350. int retval;
  351. /*
  352. * Ensure that no interrupts are enabled otherwise when
  353. * request_irq() is called we could get stuck trying to
  354. * handle an unexpected interrupt
  355. */
  356. UART_PUT_IDR(port, -1);
  357. /*
  358. * Allocate the IRQ
  359. */
  360. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, "atmel_serial", port);
  361. if (retval) {
  362. printk("atmel_serial: atmel_startup - Can't get irq\n");
  363. return retval;
  364. }
  365. /*
  366. * If there is a specific "open" function (to register
  367. * control line interrupts)
  368. */
  369. if (atmel_open_hook) {
  370. retval = atmel_open_hook(port);
  371. if (retval) {
  372. free_irq(port->irq, port);
  373. return retval;
  374. }
  375. }
  376. /*
  377. * Finally, enable the serial port
  378. */
  379. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  380. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); /* enable xmit & rcvr */
  381. UART_PUT_IER(port, ATMEL_US_RXRDY); /* enable receive only */
  382. return 0;
  383. }
  384. /*
  385. * Disable the port
  386. */
  387. static void atmel_shutdown(struct uart_port *port)
  388. {
  389. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  390. /*
  391. * Disable all interrupts, port and break condition.
  392. */
  393. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  394. UART_PUT_IDR(port, -1);
  395. /*
  396. * Free the interrupt
  397. */
  398. free_irq(port->irq, port);
  399. /*
  400. * If there is a specific "close" function (to unregister
  401. * control line interrupts)
  402. */
  403. if (atmel_close_hook)
  404. atmel_close_hook(port);
  405. }
  406. /*
  407. * Power / Clock management.
  408. */
  409. static void atmel_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  410. {
  411. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  412. switch (state) {
  413. case 0:
  414. /*
  415. * Enable the peripheral clock for this serial port.
  416. * This is called on uart_open() or a resume event.
  417. */
  418. clk_enable(atmel_port->clk);
  419. break;
  420. case 3:
  421. /*
  422. * Disable the peripheral clock for this serial port.
  423. * This is called on uart_close() or a suspend event.
  424. */
  425. clk_disable(atmel_port->clk);
  426. break;
  427. default:
  428. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  429. }
  430. }
  431. /*
  432. * Change the port parameters
  433. */
  434. static void atmel_set_termios(struct uart_port *port, struct ktermios * termios, struct ktermios * old)
  435. {
  436. unsigned long flags;
  437. unsigned int mode, imr, quot, baud;
  438. /* Get current mode register */
  439. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | ATMEL_US_PAR);
  440. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  441. quot = uart_get_divisor(port, baud);
  442. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  443. quot /= 8;
  444. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  445. }
  446. /* byte size */
  447. switch (termios->c_cflag & CSIZE) {
  448. case CS5:
  449. mode |= ATMEL_US_CHRL_5;
  450. break;
  451. case CS6:
  452. mode |= ATMEL_US_CHRL_6;
  453. break;
  454. case CS7:
  455. mode |= ATMEL_US_CHRL_7;
  456. break;
  457. default:
  458. mode |= ATMEL_US_CHRL_8;
  459. break;
  460. }
  461. /* stop bits */
  462. if (termios->c_cflag & CSTOPB)
  463. mode |= ATMEL_US_NBSTOP_2;
  464. /* parity */
  465. if (termios->c_cflag & PARENB) {
  466. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  467. if (termios->c_cflag & PARODD)
  468. mode |= ATMEL_US_PAR_MARK;
  469. else
  470. mode |= ATMEL_US_PAR_SPACE;
  471. }
  472. else if (termios->c_cflag & PARODD)
  473. mode |= ATMEL_US_PAR_ODD;
  474. else
  475. mode |= ATMEL_US_PAR_EVEN;
  476. }
  477. else
  478. mode |= ATMEL_US_PAR_NONE;
  479. spin_lock_irqsave(&port->lock, flags);
  480. port->read_status_mask = ATMEL_US_OVRE;
  481. if (termios->c_iflag & INPCK)
  482. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  483. if (termios->c_iflag & (BRKINT | PARMRK))
  484. port->read_status_mask |= ATMEL_US_RXBRK;
  485. /*
  486. * Characters to ignore
  487. */
  488. port->ignore_status_mask = 0;
  489. if (termios->c_iflag & IGNPAR)
  490. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  491. if (termios->c_iflag & IGNBRK) {
  492. port->ignore_status_mask |= ATMEL_US_RXBRK;
  493. /*
  494. * If we're ignoring parity and break indicators,
  495. * ignore overruns too (for real raw support).
  496. */
  497. if (termios->c_iflag & IGNPAR)
  498. port->ignore_status_mask |= ATMEL_US_OVRE;
  499. }
  500. // TODO: Ignore all characters if CREAD is set.
  501. /* update the per-port timeout */
  502. uart_update_timeout(port, termios->c_cflag, baud);
  503. /* disable interrupts and drain transmitter */
  504. imr = UART_GET_IMR(port); /* get interrupt mask */
  505. UART_PUT_IDR(port, -1); /* disable all interrupts */
  506. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) { barrier(); }
  507. /* disable receiver and transmitter */
  508. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  509. /* set the parity, stop bits and data size */
  510. UART_PUT_MR(port, mode);
  511. /* set the baud rate */
  512. UART_PUT_BRGR(port, quot);
  513. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  514. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  515. /* restore interrupts */
  516. UART_PUT_IER(port, imr);
  517. /* CTS flow-control and modem-status interrupts */
  518. if (UART_ENABLE_MS(port, termios->c_cflag))
  519. port->ops->enable_ms(port);
  520. spin_unlock_irqrestore(&port->lock, flags);
  521. }
  522. /*
  523. * Return string describing the specified port
  524. */
  525. static const char *atmel_type(struct uart_port *port)
  526. {
  527. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  528. }
  529. /*
  530. * Release the memory region(s) being used by 'port'.
  531. */
  532. static void atmel_release_port(struct uart_port *port)
  533. {
  534. struct platform_device *pdev = to_platform_device(port->dev);
  535. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  536. release_mem_region(port->mapbase, size);
  537. if (port->flags & UPF_IOREMAP) {
  538. iounmap(port->membase);
  539. port->membase = NULL;
  540. }
  541. }
  542. /*
  543. * Request the memory region(s) being used by 'port'.
  544. */
  545. static int atmel_request_port(struct uart_port *port)
  546. {
  547. struct platform_device *pdev = to_platform_device(port->dev);
  548. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  549. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  550. return -EBUSY;
  551. if (port->flags & UPF_IOREMAP) {
  552. port->membase = ioremap(port->mapbase, size);
  553. if (port->membase == NULL) {
  554. release_mem_region(port->mapbase, size);
  555. return -ENOMEM;
  556. }
  557. }
  558. return 0;
  559. }
  560. /*
  561. * Configure/autoconfigure the port.
  562. */
  563. static void atmel_config_port(struct uart_port *port, int flags)
  564. {
  565. if (flags & UART_CONFIG_TYPE) {
  566. port->type = PORT_ATMEL;
  567. atmel_request_port(port);
  568. }
  569. }
  570. /*
  571. * Verify the new serial_struct (for TIOCSSERIAL).
  572. */
  573. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  574. {
  575. int ret = 0;
  576. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  577. ret = -EINVAL;
  578. if (port->irq != ser->irq)
  579. ret = -EINVAL;
  580. if (ser->io_type != SERIAL_IO_MEM)
  581. ret = -EINVAL;
  582. if (port->uartclk / 16 != ser->baud_base)
  583. ret = -EINVAL;
  584. if ((void *)port->mapbase != ser->iomem_base)
  585. ret = -EINVAL;
  586. if (port->iobase != ser->port)
  587. ret = -EINVAL;
  588. if (ser->hub6 != 0)
  589. ret = -EINVAL;
  590. return ret;
  591. }
  592. static struct uart_ops atmel_pops = {
  593. .tx_empty = atmel_tx_empty,
  594. .set_mctrl = atmel_set_mctrl,
  595. .get_mctrl = atmel_get_mctrl,
  596. .stop_tx = atmel_stop_tx,
  597. .start_tx = atmel_start_tx,
  598. .stop_rx = atmel_stop_rx,
  599. .enable_ms = atmel_enable_ms,
  600. .break_ctl = atmel_break_ctl,
  601. .startup = atmel_startup,
  602. .shutdown = atmel_shutdown,
  603. .set_termios = atmel_set_termios,
  604. .type = atmel_type,
  605. .release_port = atmel_release_port,
  606. .request_port = atmel_request_port,
  607. .config_port = atmel_config_port,
  608. .verify_port = atmel_verify_port,
  609. .pm = atmel_serial_pm,
  610. };
  611. /*
  612. * Configure the port from the platform device resource info.
  613. */
  614. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, struct platform_device *pdev)
  615. {
  616. struct uart_port *port = &atmel_port->uart;
  617. struct atmel_uart_data *data = pdev->dev.platform_data;
  618. port->iotype = UPIO_MEM;
  619. port->flags = UPF_BOOT_AUTOCONF;
  620. port->ops = &atmel_pops;
  621. port->fifosize = 1;
  622. port->line = pdev->id;
  623. port->dev = &pdev->dev;
  624. port->mapbase = pdev->resource[0].start;
  625. port->irq = pdev->resource[1].start;
  626. if (data->regs)
  627. /* Already mapped by setup code */
  628. port->membase = data->regs;
  629. else {
  630. port->flags |= UPF_IOREMAP;
  631. port->membase = NULL;
  632. }
  633. if (!atmel_port->clk) { /* for console, the clock could already be configured */
  634. atmel_port->clk = clk_get(&pdev->dev, "usart");
  635. clk_enable(atmel_port->clk);
  636. port->uartclk = clk_get_rate(atmel_port->clk);
  637. }
  638. }
  639. /*
  640. * Register board-specific modem-control line handlers.
  641. */
  642. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  643. {
  644. if (fns->enable_ms)
  645. atmel_pops.enable_ms = fns->enable_ms;
  646. if (fns->get_mctrl)
  647. atmel_pops.get_mctrl = fns->get_mctrl;
  648. if (fns->set_mctrl)
  649. atmel_pops.set_mctrl = fns->set_mctrl;
  650. atmel_open_hook = fns->open;
  651. atmel_close_hook = fns->close;
  652. atmel_pops.pm = fns->pm;
  653. atmel_pops.set_wake = fns->set_wake;
  654. }
  655. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  656. static void atmel_console_putchar(struct uart_port *port, int ch)
  657. {
  658. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  659. barrier();
  660. UART_PUT_CHAR(port, ch);
  661. }
  662. /*
  663. * Interrupts are disabled on entering
  664. */
  665. static void atmel_console_write(struct console *co, const char *s, u_int count)
  666. {
  667. struct uart_port *port = &atmel_ports[co->index].uart;
  668. unsigned int status, imr;
  669. /*
  670. * First, save IMR and then disable interrupts
  671. */
  672. imr = UART_GET_IMR(port); /* get interrupt mask */
  673. UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
  674. uart_console_write(port, s, count, atmel_console_putchar);
  675. /*
  676. * Finally, wait for transmitter to become empty
  677. * and restore IMR
  678. */
  679. do {
  680. status = UART_GET_CSR(port);
  681. } while (!(status & ATMEL_US_TXRDY));
  682. UART_PUT_IER(port, imr); /* set interrupts back the way they were */
  683. }
  684. /*
  685. * If the port was already initialised (eg, by a boot loader), try to determine
  686. * the current setup.
  687. */
  688. static void __init atmel_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  689. {
  690. unsigned int mr, quot;
  691. // TODO: CR is a write-only register
  692. // unsigned int cr;
  693. //
  694. // cr = UART_GET_CR(port) & (ATMEL_US_RXEN | ATMEL_US_TXEN);
  695. // if (cr == (ATMEL_US_RXEN | ATMEL_US_TXEN)) {
  696. // /* ok, the port was enabled */
  697. // }
  698. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  699. if (mr == ATMEL_US_CHRL_8)
  700. *bits = 8;
  701. else
  702. *bits = 7;
  703. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  704. if (mr == ATMEL_US_PAR_EVEN)
  705. *parity = 'e';
  706. else if (mr == ATMEL_US_PAR_ODD)
  707. *parity = 'o';
  708. /*
  709. * The serial core only rounds down when matching this to a
  710. * supported baud rate. Make sure we don't end up slightly
  711. * lower than one of those, as it would make us fall through
  712. * to a much lower baud rate than we really want.
  713. */
  714. quot = UART_GET_BRGR(port);
  715. *baud = port->uartclk / (16 * (quot - 1));
  716. }
  717. static int __init atmel_console_setup(struct console *co, char *options)
  718. {
  719. struct uart_port *port = &atmel_ports[co->index].uart;
  720. int baud = 115200;
  721. int bits = 8;
  722. int parity = 'n';
  723. int flow = 'n';
  724. if (port->membase == 0) /* Port not initialized yet - delay setup */
  725. return -ENODEV;
  726. UART_PUT_IDR(port, -1); /* disable interrupts */
  727. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  728. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  729. if (options)
  730. uart_parse_options(options, &baud, &parity, &bits, &flow);
  731. else
  732. atmel_console_get_options(port, &baud, &parity, &bits);
  733. return uart_set_options(port, co, baud, parity, bits, flow);
  734. }
  735. static struct uart_driver atmel_uart;
  736. static struct console atmel_console = {
  737. .name = ATMEL_DEVICENAME,
  738. .write = atmel_console_write,
  739. .device = uart_console_device,
  740. .setup = atmel_console_setup,
  741. .flags = CON_PRINTBUFFER,
  742. .index = -1,
  743. .data = &atmel_uart,
  744. };
  745. #define ATMEL_CONSOLE_DEVICE &atmel_console
  746. /*
  747. * Early console initialization (before VM subsystem initialized).
  748. */
  749. static int __init atmel_console_init(void)
  750. {
  751. if (atmel_default_console_device) {
  752. add_preferred_console(ATMEL_DEVICENAME, atmel_default_console_device->id, NULL);
  753. atmel_init_port(&(atmel_ports[atmel_default_console_device->id]), atmel_default_console_device);
  754. register_console(&atmel_console);
  755. }
  756. return 0;
  757. }
  758. console_initcall(atmel_console_init);
  759. /*
  760. * Late console initialization.
  761. */
  762. static int __init atmel_late_console_init(void)
  763. {
  764. if (atmel_default_console_device && !(atmel_console.flags & CON_ENABLED))
  765. register_console(&atmel_console);
  766. return 0;
  767. }
  768. core_initcall(atmel_late_console_init);
  769. #else
  770. #define ATMEL_CONSOLE_DEVICE NULL
  771. #endif
  772. static struct uart_driver atmel_uart = {
  773. .owner = THIS_MODULE,
  774. .driver_name = "atmel_serial",
  775. .dev_name = ATMEL_DEVICENAME,
  776. .major = SERIAL_ATMEL_MAJOR,
  777. .minor = MINOR_START,
  778. .nr = ATMEL_MAX_UART,
  779. .cons = ATMEL_CONSOLE_DEVICE,
  780. };
  781. #ifdef CONFIG_PM
  782. static int atmel_serial_suspend(struct platform_device *pdev, pm_message_t state)
  783. {
  784. struct uart_port *port = platform_get_drvdata(pdev);
  785. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  786. if (device_may_wakeup(&pdev->dev) && !at91_suspend_entering_slow_clock())
  787. enable_irq_wake(port->irq);
  788. else {
  789. uart_suspend_port(&atmel_uart, port);
  790. atmel_port->suspended = 1;
  791. }
  792. return 0;
  793. }
  794. static int atmel_serial_resume(struct platform_device *pdev)
  795. {
  796. struct uart_port *port = platform_get_drvdata(pdev);
  797. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  798. if (atmel_port->suspended) {
  799. uart_resume_port(&atmel_uart, port);
  800. atmel_port->suspended = 0;
  801. }
  802. else
  803. disable_irq_wake(port->irq);
  804. return 0;
  805. }
  806. #else
  807. #define atmel_serial_suspend NULL
  808. #define atmel_serial_resume NULL
  809. #endif
  810. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  811. {
  812. struct atmel_uart_port *port;
  813. int ret;
  814. port = &atmel_ports[pdev->id];
  815. atmel_init_port(port, pdev);
  816. ret = uart_add_one_port(&atmel_uart, &port->uart);
  817. if (!ret) {
  818. device_init_wakeup(&pdev->dev, 1);
  819. platform_set_drvdata(pdev, port);
  820. }
  821. return ret;
  822. }
  823. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  824. {
  825. struct uart_port *port = platform_get_drvdata(pdev);
  826. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  827. int ret = 0;
  828. clk_disable(atmel_port->clk);
  829. clk_put(atmel_port->clk);
  830. device_init_wakeup(&pdev->dev, 0);
  831. platform_set_drvdata(pdev, NULL);
  832. if (port) {
  833. ret = uart_remove_one_port(&atmel_uart, port);
  834. kfree(port);
  835. }
  836. return ret;
  837. }
  838. static struct platform_driver atmel_serial_driver = {
  839. .probe = atmel_serial_probe,
  840. .remove = __devexit_p(atmel_serial_remove),
  841. .suspend = atmel_serial_suspend,
  842. .resume = atmel_serial_resume,
  843. .driver = {
  844. .name = "atmel_usart",
  845. .owner = THIS_MODULE,
  846. },
  847. };
  848. static int __init atmel_serial_init(void)
  849. {
  850. int ret;
  851. ret = uart_register_driver(&atmel_uart);
  852. if (ret)
  853. return ret;
  854. ret = platform_driver_register(&atmel_serial_driver);
  855. if (ret)
  856. uart_unregister_driver(&atmel_uart);
  857. return ret;
  858. }
  859. static void __exit atmel_serial_exit(void)
  860. {
  861. platform_driver_unregister(&atmel_serial_driver);
  862. uart_unregister_driver(&atmel_uart);
  863. }
  864. module_init(atmel_serial_init);
  865. module_exit(atmel_serial_exit);
  866. MODULE_AUTHOR("Rick Bronson");
  867. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  868. MODULE_LICENSE("GPL");