atmel_serial.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  175. }
  176. /*
  177. * Start transmitting.
  178. */
  179. static void atmel_start_tx(struct uart_port *port)
  180. {
  181. UART_PUT_IER(port, ATMEL_US_TXRDY);
  182. }
  183. /*
  184. * Stop receiving - port is in process of being closed.
  185. */
  186. static void atmel_stop_rx(struct uart_port *port)
  187. {
  188. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  189. }
  190. /*
  191. * Enable modem status interrupts
  192. */
  193. static void atmel_enable_ms(struct uart_port *port)
  194. {
  195. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  196. }
  197. /*
  198. * Control the transmission of a break signal
  199. */
  200. static void atmel_break_ctl(struct uart_port *port, int break_state)
  201. {
  202. if (break_state != 0)
  203. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  204. else
  205. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  206. }
  207. /*
  208. * Characters received (called from interrupt handler)
  209. */
  210. static void atmel_rx_chars(struct uart_port *port)
  211. {
  212. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  213. struct tty_struct *tty = port->info->tty;
  214. unsigned int status, ch, flg;
  215. status = UART_GET_CSR(port);
  216. while (status & ATMEL_US_RXRDY) {
  217. ch = UART_GET_CHAR(port);
  218. port->icount.rx++;
  219. flg = TTY_NORMAL;
  220. /*
  221. * note that the error handling code is
  222. * out of the main execution path
  223. */
  224. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  225. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  226. || atmel_port->break_active)) {
  227. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* clear error */
  228. if (status & ATMEL_US_RXBRK
  229. && !atmel_port->break_active) {
  230. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); /* ignore side-effect */
  231. port->icount.brk++;
  232. atmel_port->break_active = 1;
  233. UART_PUT_IER(port, ATMEL_US_RXBRK);
  234. if (uart_handle_break(port))
  235. goto ignore_char;
  236. } else {
  237. /*
  238. * This is either the end-of-break
  239. * condition or we've received at
  240. * least one character without RXBRK
  241. * being set. In both cases, the next
  242. * RXBRK will indicate start-of-break.
  243. */
  244. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  245. status &= ~ATMEL_US_RXBRK;
  246. atmel_port->break_active = 0;
  247. }
  248. if (status & ATMEL_US_PARE)
  249. port->icount.parity++;
  250. if (status & ATMEL_US_FRAME)
  251. port->icount.frame++;
  252. if (status & ATMEL_US_OVRE)
  253. port->icount.overrun++;
  254. status &= port->read_status_mask;
  255. if (status & ATMEL_US_RXBRK)
  256. flg = TTY_BREAK;
  257. else if (status & ATMEL_US_PARE)
  258. flg = TTY_PARITY;
  259. else if (status & ATMEL_US_FRAME)
  260. flg = TTY_FRAME;
  261. }
  262. if (uart_handle_sysrq_char(port, ch))
  263. goto ignore_char;
  264. uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg);
  265. ignore_char:
  266. status = UART_GET_CSR(port);
  267. }
  268. tty_flip_buffer_push(tty);
  269. }
  270. /*
  271. * Transmit characters (called from interrupt handler)
  272. */
  273. static void atmel_tx_chars(struct uart_port *port)
  274. {
  275. struct circ_buf *xmit = &port->info->xmit;
  276. if (port->x_char) {
  277. UART_PUT_CHAR(port, port->x_char);
  278. port->icount.tx++;
  279. port->x_char = 0;
  280. return;
  281. }
  282. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  283. atmel_stop_tx(port);
  284. return;
  285. }
  286. while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  287. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  288. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  289. port->icount.tx++;
  290. if (uart_circ_empty(xmit))
  291. break;
  292. }
  293. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  294. uart_write_wakeup(port);
  295. if (uart_circ_empty(xmit))
  296. atmel_stop_tx(port);
  297. }
  298. /*
  299. * Interrupt handler
  300. */
  301. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  302. {
  303. struct uart_port *port = dev_id;
  304. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  305. unsigned int status, pending, pass_counter = 0;
  306. status = UART_GET_CSR(port);
  307. pending = status & UART_GET_IMR(port);
  308. while (pending) {
  309. /* Interrupt receive */
  310. if (pending & ATMEL_US_RXRDY)
  311. atmel_rx_chars(port);
  312. else if (pending & ATMEL_US_RXBRK) {
  313. /*
  314. * End of break detected. If it came along
  315. * with a character, atmel_rx_chars will
  316. * handle it.
  317. */
  318. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  319. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  320. atmel_port->break_active = 0;
  321. }
  322. // TODO: All reads to CSR will clear these interrupts!
  323. if (pending & ATMEL_US_RIIC) port->icount.rng++;
  324. if (pending & ATMEL_US_DSRIC) port->icount.dsr++;
  325. if (pending & ATMEL_US_DCDIC)
  326. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  327. if (pending & ATMEL_US_CTSIC)
  328. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  329. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC))
  330. wake_up_interruptible(&port->info->delta_msr_wait);
  331. /* Interrupt transmit */
  332. if (pending & ATMEL_US_TXRDY)
  333. atmel_tx_chars(port);
  334. if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
  335. break;
  336. status = UART_GET_CSR(port);
  337. pending = status & UART_GET_IMR(port);
  338. }
  339. return IRQ_HANDLED;
  340. }
  341. /*
  342. * Perform initialization and enable port for reception
  343. */
  344. static int atmel_startup(struct uart_port *port)
  345. {
  346. int retval;
  347. /*
  348. * Ensure that no interrupts are enabled otherwise when
  349. * request_irq() is called we could get stuck trying to
  350. * handle an unexpected interrupt
  351. */
  352. UART_PUT_IDR(port, -1);
  353. /*
  354. * Allocate the IRQ
  355. */
  356. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, "atmel_serial", port);
  357. if (retval) {
  358. printk("atmel_serial: atmel_startup - Can't get irq\n");
  359. return retval;
  360. }
  361. /*
  362. * If there is a specific "open" function (to register
  363. * control line interrupts)
  364. */
  365. if (atmel_open_hook) {
  366. retval = atmel_open_hook(port);
  367. if (retval) {
  368. free_irq(port->irq, port);
  369. return retval;
  370. }
  371. }
  372. /*
  373. * Finally, enable the serial port
  374. */
  375. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  376. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); /* enable xmit & rcvr */
  377. UART_PUT_IER(port, ATMEL_US_RXRDY); /* enable receive only */
  378. return 0;
  379. }
  380. /*
  381. * Disable the port
  382. */
  383. static void atmel_shutdown(struct uart_port *port)
  384. {
  385. /*
  386. * Disable all interrupts, port and break condition.
  387. */
  388. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  389. UART_PUT_IDR(port, -1);
  390. /*
  391. * Free the interrupt
  392. */
  393. free_irq(port->irq, port);
  394. /*
  395. * If there is a specific "close" function (to unregister
  396. * control line interrupts)
  397. */
  398. if (atmel_close_hook)
  399. atmel_close_hook(port);
  400. }
  401. /*
  402. * Power / Clock management.
  403. */
  404. static void atmel_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  405. {
  406. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  407. switch (state) {
  408. case 0:
  409. /*
  410. * Enable the peripheral clock for this serial port.
  411. * This is called on uart_open() or a resume event.
  412. */
  413. clk_enable(atmel_port->clk);
  414. break;
  415. case 3:
  416. /*
  417. * Disable the peripheral clock for this serial port.
  418. * This is called on uart_close() or a suspend event.
  419. */
  420. clk_disable(atmel_port->clk);
  421. break;
  422. default:
  423. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  424. }
  425. }
  426. /*
  427. * Change the port parameters
  428. */
  429. static void atmel_set_termios(struct uart_port *port, struct ktermios * termios, struct ktermios * old)
  430. {
  431. unsigned long flags;
  432. unsigned int mode, imr, quot, baud;
  433. /* Get current mode register */
  434. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | ATMEL_US_PAR);
  435. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  436. quot = uart_get_divisor(port, baud);
  437. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  438. quot /= 8;
  439. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  440. }
  441. /* byte size */
  442. switch (termios->c_cflag & CSIZE) {
  443. case CS5:
  444. mode |= ATMEL_US_CHRL_5;
  445. break;
  446. case CS6:
  447. mode |= ATMEL_US_CHRL_6;
  448. break;
  449. case CS7:
  450. mode |= ATMEL_US_CHRL_7;
  451. break;
  452. default:
  453. mode |= ATMEL_US_CHRL_8;
  454. break;
  455. }
  456. /* stop bits */
  457. if (termios->c_cflag & CSTOPB)
  458. mode |= ATMEL_US_NBSTOP_2;
  459. /* parity */
  460. if (termios->c_cflag & PARENB) {
  461. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  462. if (termios->c_cflag & PARODD)
  463. mode |= ATMEL_US_PAR_MARK;
  464. else
  465. mode |= ATMEL_US_PAR_SPACE;
  466. }
  467. else if (termios->c_cflag & PARODD)
  468. mode |= ATMEL_US_PAR_ODD;
  469. else
  470. mode |= ATMEL_US_PAR_EVEN;
  471. }
  472. else
  473. mode |= ATMEL_US_PAR_NONE;
  474. spin_lock_irqsave(&port->lock, flags);
  475. port->read_status_mask = ATMEL_US_OVRE;
  476. if (termios->c_iflag & INPCK)
  477. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  478. if (termios->c_iflag & (BRKINT | PARMRK))
  479. port->read_status_mask |= ATMEL_US_RXBRK;
  480. /*
  481. * Characters to ignore
  482. */
  483. port->ignore_status_mask = 0;
  484. if (termios->c_iflag & IGNPAR)
  485. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  486. if (termios->c_iflag & IGNBRK) {
  487. port->ignore_status_mask |= ATMEL_US_RXBRK;
  488. /*
  489. * If we're ignoring parity and break indicators,
  490. * ignore overruns too (for real raw support).
  491. */
  492. if (termios->c_iflag & IGNPAR)
  493. port->ignore_status_mask |= ATMEL_US_OVRE;
  494. }
  495. // TODO: Ignore all characters if CREAD is set.
  496. /* update the per-port timeout */
  497. uart_update_timeout(port, termios->c_cflag, baud);
  498. /* disable interrupts and drain transmitter */
  499. imr = UART_GET_IMR(port); /* get interrupt mask */
  500. UART_PUT_IDR(port, -1); /* disable all interrupts */
  501. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) { barrier(); }
  502. /* disable receiver and transmitter */
  503. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  504. /* set the parity, stop bits and data size */
  505. UART_PUT_MR(port, mode);
  506. /* set the baud rate */
  507. UART_PUT_BRGR(port, quot);
  508. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  509. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  510. /* restore interrupts */
  511. UART_PUT_IER(port, imr);
  512. /* CTS flow-control and modem-status interrupts */
  513. if (UART_ENABLE_MS(port, termios->c_cflag))
  514. port->ops->enable_ms(port);
  515. spin_unlock_irqrestore(&port->lock, flags);
  516. }
  517. /*
  518. * Return string describing the specified port
  519. */
  520. static const char *atmel_type(struct uart_port *port)
  521. {
  522. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  523. }
  524. /*
  525. * Release the memory region(s) being used by 'port'.
  526. */
  527. static void atmel_release_port(struct uart_port *port)
  528. {
  529. struct platform_device *pdev = to_platform_device(port->dev);
  530. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  531. release_mem_region(port->mapbase, size);
  532. if (port->flags & UPF_IOREMAP) {
  533. iounmap(port->membase);
  534. port->membase = NULL;
  535. }
  536. }
  537. /*
  538. * Request the memory region(s) being used by 'port'.
  539. */
  540. static int atmel_request_port(struct uart_port *port)
  541. {
  542. struct platform_device *pdev = to_platform_device(port->dev);
  543. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  544. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  545. return -EBUSY;
  546. if (port->flags & UPF_IOREMAP) {
  547. port->membase = ioremap(port->mapbase, size);
  548. if (port->membase == NULL) {
  549. release_mem_region(port->mapbase, size);
  550. return -ENOMEM;
  551. }
  552. }
  553. return 0;
  554. }
  555. /*
  556. * Configure/autoconfigure the port.
  557. */
  558. static void atmel_config_port(struct uart_port *port, int flags)
  559. {
  560. if (flags & UART_CONFIG_TYPE) {
  561. port->type = PORT_ATMEL;
  562. atmel_request_port(port);
  563. }
  564. }
  565. /*
  566. * Verify the new serial_struct (for TIOCSSERIAL).
  567. */
  568. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  569. {
  570. int ret = 0;
  571. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  572. ret = -EINVAL;
  573. if (port->irq != ser->irq)
  574. ret = -EINVAL;
  575. if (ser->io_type != SERIAL_IO_MEM)
  576. ret = -EINVAL;
  577. if (port->uartclk / 16 != ser->baud_base)
  578. ret = -EINVAL;
  579. if ((void *)port->mapbase != ser->iomem_base)
  580. ret = -EINVAL;
  581. if (port->iobase != ser->port)
  582. ret = -EINVAL;
  583. if (ser->hub6 != 0)
  584. ret = -EINVAL;
  585. return ret;
  586. }
  587. static struct uart_ops atmel_pops = {
  588. .tx_empty = atmel_tx_empty,
  589. .set_mctrl = atmel_set_mctrl,
  590. .get_mctrl = atmel_get_mctrl,
  591. .stop_tx = atmel_stop_tx,
  592. .start_tx = atmel_start_tx,
  593. .stop_rx = atmel_stop_rx,
  594. .enable_ms = atmel_enable_ms,
  595. .break_ctl = atmel_break_ctl,
  596. .startup = atmel_startup,
  597. .shutdown = atmel_shutdown,
  598. .set_termios = atmel_set_termios,
  599. .type = atmel_type,
  600. .release_port = atmel_release_port,
  601. .request_port = atmel_request_port,
  602. .config_port = atmel_config_port,
  603. .verify_port = atmel_verify_port,
  604. .pm = atmel_serial_pm,
  605. };
  606. /*
  607. * Configure the port from the platform device resource info.
  608. */
  609. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, struct platform_device *pdev)
  610. {
  611. struct uart_port *port = &atmel_port->uart;
  612. struct atmel_uart_data *data = pdev->dev.platform_data;
  613. port->iotype = UPIO_MEM;
  614. port->flags = UPF_BOOT_AUTOCONF;
  615. port->ops = &atmel_pops;
  616. port->fifosize = 1;
  617. port->line = pdev->id;
  618. port->dev = &pdev->dev;
  619. port->mapbase = pdev->resource[0].start;
  620. port->irq = pdev->resource[1].start;
  621. if (data->regs)
  622. /* Already mapped by setup code */
  623. port->membase = data->regs;
  624. else {
  625. port->flags |= UPF_IOREMAP;
  626. port->membase = NULL;
  627. }
  628. if (!atmel_port->clk) { /* for console, the clock could already be configured */
  629. atmel_port->clk = clk_get(&pdev->dev, "usart");
  630. clk_enable(atmel_port->clk);
  631. port->uartclk = clk_get_rate(atmel_port->clk);
  632. }
  633. }
  634. /*
  635. * Register board-specific modem-control line handlers.
  636. */
  637. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  638. {
  639. if (fns->enable_ms)
  640. atmel_pops.enable_ms = fns->enable_ms;
  641. if (fns->get_mctrl)
  642. atmel_pops.get_mctrl = fns->get_mctrl;
  643. if (fns->set_mctrl)
  644. atmel_pops.set_mctrl = fns->set_mctrl;
  645. atmel_open_hook = fns->open;
  646. atmel_close_hook = fns->close;
  647. atmel_pops.pm = fns->pm;
  648. atmel_pops.set_wake = fns->set_wake;
  649. }
  650. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  651. static void atmel_console_putchar(struct uart_port *port, int ch)
  652. {
  653. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  654. barrier();
  655. UART_PUT_CHAR(port, ch);
  656. }
  657. /*
  658. * Interrupts are disabled on entering
  659. */
  660. static void atmel_console_write(struct console *co, const char *s, u_int count)
  661. {
  662. struct uart_port *port = &atmel_ports[co->index].uart;
  663. unsigned int status, imr;
  664. /*
  665. * First, save IMR and then disable interrupts
  666. */
  667. imr = UART_GET_IMR(port); /* get interrupt mask */
  668. UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
  669. uart_console_write(port, s, count, atmel_console_putchar);
  670. /*
  671. * Finally, wait for transmitter to become empty
  672. * and restore IMR
  673. */
  674. do {
  675. status = UART_GET_CSR(port);
  676. } while (!(status & ATMEL_US_TXRDY));
  677. UART_PUT_IER(port, imr); /* set interrupts back the way they were */
  678. }
  679. /*
  680. * If the port was already initialised (eg, by a boot loader), try to determine
  681. * the current setup.
  682. */
  683. static void __init atmel_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  684. {
  685. unsigned int mr, quot;
  686. // TODO: CR is a write-only register
  687. // unsigned int cr;
  688. //
  689. // cr = UART_GET_CR(port) & (ATMEL_US_RXEN | ATMEL_US_TXEN);
  690. // if (cr == (ATMEL_US_RXEN | ATMEL_US_TXEN)) {
  691. // /* ok, the port was enabled */
  692. // }
  693. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  694. if (mr == ATMEL_US_CHRL_8)
  695. *bits = 8;
  696. else
  697. *bits = 7;
  698. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  699. if (mr == ATMEL_US_PAR_EVEN)
  700. *parity = 'e';
  701. else if (mr == ATMEL_US_PAR_ODD)
  702. *parity = 'o';
  703. /*
  704. * The serial core only rounds down when matching this to a
  705. * supported baud rate. Make sure we don't end up slightly
  706. * lower than one of those, as it would make us fall through
  707. * to a much lower baud rate than we really want.
  708. */
  709. quot = UART_GET_BRGR(port);
  710. *baud = port->uartclk / (16 * (quot - 1));
  711. }
  712. static int __init atmel_console_setup(struct console *co, char *options)
  713. {
  714. struct uart_port *port = &atmel_ports[co->index].uart;
  715. int baud = 115200;
  716. int bits = 8;
  717. int parity = 'n';
  718. int flow = 'n';
  719. if (port->membase == 0) /* Port not initialized yet - delay setup */
  720. return -ENODEV;
  721. UART_PUT_IDR(port, -1); /* disable interrupts */
  722. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  723. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  724. if (options)
  725. uart_parse_options(options, &baud, &parity, &bits, &flow);
  726. else
  727. atmel_console_get_options(port, &baud, &parity, &bits);
  728. return uart_set_options(port, co, baud, parity, bits, flow);
  729. }
  730. static struct uart_driver atmel_uart;
  731. static struct console atmel_console = {
  732. .name = ATMEL_DEVICENAME,
  733. .write = atmel_console_write,
  734. .device = uart_console_device,
  735. .setup = atmel_console_setup,
  736. .flags = CON_PRINTBUFFER,
  737. .index = -1,
  738. .data = &atmel_uart,
  739. };
  740. #define ATMEL_CONSOLE_DEVICE &atmel_console
  741. /*
  742. * Early console initialization (before VM subsystem initialized).
  743. */
  744. static int __init atmel_console_init(void)
  745. {
  746. if (atmel_default_console_device) {
  747. add_preferred_console(ATMEL_DEVICENAME, atmel_default_console_device->id, NULL);
  748. atmel_init_port(&(atmel_ports[atmel_default_console_device->id]), atmel_default_console_device);
  749. register_console(&atmel_console);
  750. }
  751. return 0;
  752. }
  753. console_initcall(atmel_console_init);
  754. /*
  755. * Late console initialization.
  756. */
  757. static int __init atmel_late_console_init(void)
  758. {
  759. if (atmel_default_console_device && !(atmel_console.flags & CON_ENABLED))
  760. register_console(&atmel_console);
  761. return 0;
  762. }
  763. core_initcall(atmel_late_console_init);
  764. #else
  765. #define ATMEL_CONSOLE_DEVICE NULL
  766. #endif
  767. static struct uart_driver atmel_uart = {
  768. .owner = THIS_MODULE,
  769. .driver_name = "atmel_serial",
  770. .dev_name = ATMEL_DEVICENAME,
  771. .major = SERIAL_ATMEL_MAJOR,
  772. .minor = MINOR_START,
  773. .nr = ATMEL_MAX_UART,
  774. .cons = ATMEL_CONSOLE_DEVICE,
  775. };
  776. #ifdef CONFIG_PM
  777. static int atmel_serial_suspend(struct platform_device *pdev, pm_message_t state)
  778. {
  779. struct uart_port *port = platform_get_drvdata(pdev);
  780. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  781. if (device_may_wakeup(&pdev->dev) && !at91_suspend_entering_slow_clock())
  782. enable_irq_wake(port->irq);
  783. else {
  784. uart_suspend_port(&atmel_uart, port);
  785. atmel_port->suspended = 1;
  786. }
  787. return 0;
  788. }
  789. static int atmel_serial_resume(struct platform_device *pdev)
  790. {
  791. struct uart_port *port = platform_get_drvdata(pdev);
  792. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  793. if (atmel_port->suspended) {
  794. uart_resume_port(&atmel_uart, port);
  795. atmel_port->suspended = 0;
  796. }
  797. else
  798. disable_irq_wake(port->irq);
  799. return 0;
  800. }
  801. #else
  802. #define atmel_serial_suspend NULL
  803. #define atmel_serial_resume NULL
  804. #endif
  805. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  806. {
  807. struct atmel_uart_port *port;
  808. int ret;
  809. port = &atmel_ports[pdev->id];
  810. atmel_init_port(port, pdev);
  811. ret = uart_add_one_port(&atmel_uart, &port->uart);
  812. if (!ret) {
  813. device_init_wakeup(&pdev->dev, 1);
  814. platform_set_drvdata(pdev, port);
  815. }
  816. return ret;
  817. }
  818. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  819. {
  820. struct uart_port *port = platform_get_drvdata(pdev);
  821. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  822. int ret = 0;
  823. clk_disable(atmel_port->clk);
  824. clk_put(atmel_port->clk);
  825. device_init_wakeup(&pdev->dev, 0);
  826. platform_set_drvdata(pdev, NULL);
  827. if (port) {
  828. ret = uart_remove_one_port(&atmel_uart, port);
  829. kfree(port);
  830. }
  831. return ret;
  832. }
  833. static struct platform_driver atmel_serial_driver = {
  834. .probe = atmel_serial_probe,
  835. .remove = __devexit_p(atmel_serial_remove),
  836. .suspend = atmel_serial_suspend,
  837. .resume = atmel_serial_resume,
  838. .driver = {
  839. .name = "atmel_usart",
  840. .owner = THIS_MODULE,
  841. },
  842. };
  843. static int __init atmel_serial_init(void)
  844. {
  845. int ret;
  846. ret = uart_register_driver(&atmel_uart);
  847. if (ret)
  848. return ret;
  849. ret = platform_driver_register(&atmel_serial_driver);
  850. if (ret)
  851. uart_unregister_driver(&atmel_uart);
  852. return ret;
  853. }
  854. static void __exit atmel_serial_exit(void)
  855. {
  856. platform_driver_unregister(&atmel_serial_driver);
  857. uart_unregister_driver(&atmel_uart);
  858. }
  859. module_init(atmel_serial_init);
  860. module_exit(atmel_serial_exit);
  861. MODULE_AUTHOR("Rick Bronson");
  862. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  863. MODULE_LICENSE("GPL");