atmel_serial.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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. * DMA support added by Chip Coldwell.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/tty.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <linux/serial.h>
  33. #include <linux/clk.h>
  34. #include <linux/console.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/atmel_pdc.h>
  40. #include <linux/atmel_serial.h>
  41. #include <linux/uaccess.h>
  42. #include <asm/io.h>
  43. #include <asm/ioctls.h>
  44. #include <asm/mach/serial_at91.h>
  45. #include <mach/board.h>
  46. #ifdef CONFIG_ARM
  47. #include <mach/cpu.h>
  48. #include <mach/gpio.h>
  49. #endif
  50. #define PDC_BUFFER_SIZE 512
  51. /* Revisit: We should calculate this based on the actual port settings */
  52. #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
  53. #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  54. #define SUPPORT_SYSRQ
  55. #endif
  56. #include <linux/serial_core.h>
  57. static void atmel_start_rx(struct uart_port *port);
  58. static void atmel_stop_rx(struct uart_port *port);
  59. #ifdef CONFIG_SERIAL_ATMEL_TTYAT
  60. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  61. * should coexist with the 8250 driver, such as if we have an external 16C550
  62. * UART. */
  63. #define SERIAL_ATMEL_MAJOR 204
  64. #define MINOR_START 154
  65. #define ATMEL_DEVICENAME "ttyAT"
  66. #else
  67. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  68. * name, but it is legally reserved for the 8250 driver. */
  69. #define SERIAL_ATMEL_MAJOR TTY_MAJOR
  70. #define MINOR_START 64
  71. #define ATMEL_DEVICENAME "ttyS"
  72. #endif
  73. #define ATMEL_ISR_PASS_LIMIT 256
  74. /* UART registers. CR is write-only, hence no GET macro */
  75. #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  76. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  77. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  78. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  79. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  80. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  81. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  82. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  83. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  84. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  85. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  86. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  87. #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
  88. /* PDC registers */
  89. #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
  90. #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
  91. #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
  92. #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
  93. #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
  94. #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
  95. #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
  96. #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
  97. #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
  98. #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
  99. static int (*atmel_open_hook)(struct uart_port *);
  100. static void (*atmel_close_hook)(struct uart_port *);
  101. struct atmel_dma_buffer {
  102. unsigned char *buf;
  103. dma_addr_t dma_addr;
  104. unsigned int dma_size;
  105. unsigned int ofs;
  106. };
  107. struct atmel_uart_char {
  108. u16 status;
  109. u16 ch;
  110. };
  111. #define ATMEL_SERIAL_RINGSIZE 1024
  112. /*
  113. * We wrap our port structure around the generic uart_port.
  114. */
  115. struct atmel_uart_port {
  116. struct uart_port uart; /* uart */
  117. struct clk *clk; /* uart clock */
  118. int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
  119. u32 backup_imr; /* IMR saved during suspend */
  120. int break_active; /* break being received */
  121. short use_dma_rx; /* enable PDC receiver */
  122. short pdc_rx_idx; /* current PDC RX buffer */
  123. struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
  124. short use_dma_tx; /* enable PDC transmitter */
  125. struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
  126. struct tasklet_struct tasklet;
  127. unsigned int irq_status;
  128. unsigned int irq_status_prev;
  129. struct circ_buf rx_ring;
  130. struct serial_rs485 rs485; /* rs485 settings */
  131. unsigned int tx_done_mask;
  132. };
  133. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  134. #ifdef SUPPORT_SYSRQ
  135. static struct console atmel_console;
  136. #endif
  137. static inline struct atmel_uart_port *
  138. to_atmel_uart_port(struct uart_port *uart)
  139. {
  140. return container_of(uart, struct atmel_uart_port, uart);
  141. }
  142. #ifdef CONFIG_SERIAL_ATMEL_PDC
  143. static bool atmel_use_dma_rx(struct uart_port *port)
  144. {
  145. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  146. return atmel_port->use_dma_rx;
  147. }
  148. static bool atmel_use_dma_tx(struct uart_port *port)
  149. {
  150. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  151. return atmel_port->use_dma_tx;
  152. }
  153. #else
  154. static bool atmel_use_dma_rx(struct uart_port *port)
  155. {
  156. return false;
  157. }
  158. static bool atmel_use_dma_tx(struct uart_port *port)
  159. {
  160. return false;
  161. }
  162. #endif
  163. /* Enable or disable the rs485 support */
  164. void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
  165. {
  166. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  167. unsigned int mode;
  168. spin_lock(&port->lock);
  169. /* Disable interrupts */
  170. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  171. mode = UART_GET_MR(port);
  172. /* Resetting serial mode to RS232 (0x0) */
  173. mode &= ~ATMEL_US_USMODE;
  174. atmel_port->rs485 = *rs485conf;
  175. if (rs485conf->flags & SER_RS485_ENABLED) {
  176. dev_dbg(port->dev, "Setting UART to RS485\n");
  177. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  178. UART_PUT_TTGR(port, rs485conf->delay_rts_before_send);
  179. mode |= ATMEL_US_USMODE_RS485;
  180. } else {
  181. dev_dbg(port->dev, "Setting UART to RS232\n");
  182. if (atmel_use_dma_tx(port))
  183. atmel_port->tx_done_mask = ATMEL_US_ENDTX |
  184. ATMEL_US_TXBUFE;
  185. else
  186. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  187. }
  188. UART_PUT_MR(port, mode);
  189. /* Enable interrupts */
  190. UART_PUT_IER(port, atmel_port->tx_done_mask);
  191. spin_unlock(&port->lock);
  192. }
  193. /*
  194. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  195. */
  196. static u_int atmel_tx_empty(struct uart_port *port)
  197. {
  198. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  199. }
  200. /*
  201. * Set state of the modem control output lines
  202. */
  203. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  204. {
  205. unsigned int control = 0;
  206. unsigned int mode;
  207. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  208. #ifdef CONFIG_ARCH_AT91RM9200
  209. if (cpu_is_at91rm9200()) {
  210. /*
  211. * AT91RM9200 Errata #39: RTS0 is not internally connected
  212. * to PA21. We need to drive the pin manually.
  213. */
  214. if (port->mapbase == AT91RM9200_BASE_US0) {
  215. if (mctrl & TIOCM_RTS)
  216. at91_set_gpio_value(AT91_PIN_PA21, 0);
  217. else
  218. at91_set_gpio_value(AT91_PIN_PA21, 1);
  219. }
  220. }
  221. #endif
  222. if (mctrl & TIOCM_RTS)
  223. control |= ATMEL_US_RTSEN;
  224. else
  225. control |= ATMEL_US_RTSDIS;
  226. if (mctrl & TIOCM_DTR)
  227. control |= ATMEL_US_DTREN;
  228. else
  229. control |= ATMEL_US_DTRDIS;
  230. UART_PUT_CR(port, control);
  231. /* Local loopback mode? */
  232. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  233. if (mctrl & TIOCM_LOOP)
  234. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  235. else
  236. mode |= ATMEL_US_CHMODE_NORMAL;
  237. /* Resetting serial mode to RS232 (0x0) */
  238. mode &= ~ATMEL_US_USMODE;
  239. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  240. dev_dbg(port->dev, "Setting UART to RS485\n");
  241. UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
  242. mode |= ATMEL_US_USMODE_RS485;
  243. } else {
  244. dev_dbg(port->dev, "Setting UART to RS232\n");
  245. }
  246. UART_PUT_MR(port, mode);
  247. }
  248. /*
  249. * Get state of the modem control input lines
  250. */
  251. static u_int atmel_get_mctrl(struct uart_port *port)
  252. {
  253. unsigned int status, ret = 0;
  254. status = UART_GET_CSR(port);
  255. /*
  256. * The control signals are active low.
  257. */
  258. if (!(status & ATMEL_US_DCD))
  259. ret |= TIOCM_CD;
  260. if (!(status & ATMEL_US_CTS))
  261. ret |= TIOCM_CTS;
  262. if (!(status & ATMEL_US_DSR))
  263. ret |= TIOCM_DSR;
  264. if (!(status & ATMEL_US_RI))
  265. ret |= TIOCM_RI;
  266. return ret;
  267. }
  268. /*
  269. * Stop transmitting.
  270. */
  271. static void atmel_stop_tx(struct uart_port *port)
  272. {
  273. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  274. if (atmel_use_dma_tx(port)) {
  275. /* disable PDC transmit */
  276. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  277. }
  278. /* Disable interrupts */
  279. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  280. if (atmel_port->rs485.flags & SER_RS485_ENABLED)
  281. atmel_start_rx(port);
  282. }
  283. /*
  284. * Start transmitting.
  285. */
  286. static void atmel_start_tx(struct uart_port *port)
  287. {
  288. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  289. if (atmel_use_dma_tx(port)) {
  290. if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
  291. /* The transmitter is already running. Yes, we
  292. really need this.*/
  293. return;
  294. if (atmel_port->rs485.flags & SER_RS485_ENABLED)
  295. atmel_stop_rx(port);
  296. /* re-enable PDC transmit */
  297. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  298. }
  299. /* Enable interrupts */
  300. UART_PUT_IER(port, atmel_port->tx_done_mask);
  301. }
  302. /*
  303. * start receiving - port is in process of being opened.
  304. */
  305. static void atmel_start_rx(struct uart_port *port)
  306. {
  307. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
  308. if (atmel_use_dma_rx(port)) {
  309. /* enable PDC controller */
  310. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  311. port->read_status_mask);
  312. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  313. } else {
  314. UART_PUT_IER(port, ATMEL_US_RXRDY);
  315. }
  316. }
  317. /*
  318. * Stop receiving - port is in process of being closed.
  319. */
  320. static void atmel_stop_rx(struct uart_port *port)
  321. {
  322. if (atmel_use_dma_rx(port)) {
  323. /* disable PDC receive */
  324. UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
  325. UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  326. port->read_status_mask);
  327. } else {
  328. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  329. }
  330. }
  331. /*
  332. * Enable modem status interrupts
  333. */
  334. static void atmel_enable_ms(struct uart_port *port)
  335. {
  336. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
  337. | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  338. }
  339. /*
  340. * Control the transmission of a break signal
  341. */
  342. static void atmel_break_ctl(struct uart_port *port, int break_state)
  343. {
  344. if (break_state != 0)
  345. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  346. else
  347. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  348. }
  349. /*
  350. * Stores the incoming character in the ring buffer
  351. */
  352. static void
  353. atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
  354. unsigned int ch)
  355. {
  356. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  357. struct circ_buf *ring = &atmel_port->rx_ring;
  358. struct atmel_uart_char *c;
  359. if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
  360. /* Buffer overflow, ignore char */
  361. return;
  362. c = &((struct atmel_uart_char *)ring->buf)[ring->head];
  363. c->status = status;
  364. c->ch = ch;
  365. /* Make sure the character is stored before we update head. */
  366. smp_wmb();
  367. ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  368. }
  369. /*
  370. * Deal with parity, framing and overrun errors.
  371. */
  372. static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
  373. {
  374. /* clear error */
  375. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  376. if (status & ATMEL_US_RXBRK) {
  377. /* ignore side-effect */
  378. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  379. port->icount.brk++;
  380. }
  381. if (status & ATMEL_US_PARE)
  382. port->icount.parity++;
  383. if (status & ATMEL_US_FRAME)
  384. port->icount.frame++;
  385. if (status & ATMEL_US_OVRE)
  386. port->icount.overrun++;
  387. }
  388. /*
  389. * Characters received (called from interrupt handler)
  390. */
  391. static void atmel_rx_chars(struct uart_port *port)
  392. {
  393. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  394. unsigned int status, ch;
  395. status = UART_GET_CSR(port);
  396. while (status & ATMEL_US_RXRDY) {
  397. ch = UART_GET_CHAR(port);
  398. /*
  399. * note that the error handling code is
  400. * out of the main execution path
  401. */
  402. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  403. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  404. || atmel_port->break_active)) {
  405. /* clear error */
  406. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  407. if (status & ATMEL_US_RXBRK
  408. && !atmel_port->break_active) {
  409. atmel_port->break_active = 1;
  410. UART_PUT_IER(port, ATMEL_US_RXBRK);
  411. } else {
  412. /*
  413. * This is either the end-of-break
  414. * condition or we've received at
  415. * least one character without RXBRK
  416. * being set. In both cases, the next
  417. * RXBRK will indicate start-of-break.
  418. */
  419. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  420. status &= ~ATMEL_US_RXBRK;
  421. atmel_port->break_active = 0;
  422. }
  423. }
  424. atmel_buffer_rx_char(port, status, ch);
  425. status = UART_GET_CSR(port);
  426. }
  427. tasklet_schedule(&atmel_port->tasklet);
  428. }
  429. /*
  430. * Transmit characters (called from tasklet with TXRDY interrupt
  431. * disabled)
  432. */
  433. static void atmel_tx_chars(struct uart_port *port)
  434. {
  435. struct circ_buf *xmit = &port->state->xmit;
  436. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  437. if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  438. UART_PUT_CHAR(port, port->x_char);
  439. port->icount.tx++;
  440. port->x_char = 0;
  441. }
  442. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  443. return;
  444. while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  445. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  446. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  447. port->icount.tx++;
  448. if (uart_circ_empty(xmit))
  449. break;
  450. }
  451. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  452. uart_write_wakeup(port);
  453. if (!uart_circ_empty(xmit))
  454. /* Enable interrupts */
  455. UART_PUT_IER(port, atmel_port->tx_done_mask);
  456. }
  457. /*
  458. * receive interrupt handler.
  459. */
  460. static void
  461. atmel_handle_receive(struct uart_port *port, unsigned int pending)
  462. {
  463. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  464. if (atmel_use_dma_rx(port)) {
  465. /*
  466. * PDC receive. Just schedule the tasklet and let it
  467. * figure out the details.
  468. *
  469. * TODO: We're not handling error flags correctly at
  470. * the moment.
  471. */
  472. if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
  473. UART_PUT_IDR(port, (ATMEL_US_ENDRX
  474. | ATMEL_US_TIMEOUT));
  475. tasklet_schedule(&atmel_port->tasklet);
  476. }
  477. if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
  478. ATMEL_US_FRAME | ATMEL_US_PARE))
  479. atmel_pdc_rxerr(port, pending);
  480. }
  481. /* Interrupt receive */
  482. if (pending & ATMEL_US_RXRDY)
  483. atmel_rx_chars(port);
  484. else if (pending & ATMEL_US_RXBRK) {
  485. /*
  486. * End of break detected. If it came along with a
  487. * character, atmel_rx_chars will handle it.
  488. */
  489. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  490. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  491. atmel_port->break_active = 0;
  492. }
  493. }
  494. /*
  495. * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
  496. */
  497. static void
  498. atmel_handle_transmit(struct uart_port *port, unsigned int pending)
  499. {
  500. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  501. if (pending & atmel_port->tx_done_mask) {
  502. /* Either PDC or interrupt transmission */
  503. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  504. tasklet_schedule(&atmel_port->tasklet);
  505. }
  506. }
  507. /*
  508. * status flags interrupt handler.
  509. */
  510. static void
  511. atmel_handle_status(struct uart_port *port, unsigned int pending,
  512. unsigned int status)
  513. {
  514. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  515. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
  516. | ATMEL_US_CTSIC)) {
  517. atmel_port->irq_status = status;
  518. tasklet_schedule(&atmel_port->tasklet);
  519. }
  520. }
  521. /*
  522. * Interrupt handler
  523. */
  524. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  525. {
  526. struct uart_port *port = dev_id;
  527. unsigned int status, pending, pass_counter = 0;
  528. do {
  529. status = UART_GET_CSR(port);
  530. pending = status & UART_GET_IMR(port);
  531. if (!pending)
  532. break;
  533. atmel_handle_receive(port, pending);
  534. atmel_handle_status(port, pending, status);
  535. atmel_handle_transmit(port, pending);
  536. } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
  537. return pass_counter ? IRQ_HANDLED : IRQ_NONE;
  538. }
  539. /*
  540. * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
  541. */
  542. static void atmel_tx_dma(struct uart_port *port)
  543. {
  544. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  545. struct circ_buf *xmit = &port->state->xmit;
  546. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  547. int count;
  548. /* nothing left to transmit? */
  549. if (UART_GET_TCR(port))
  550. return;
  551. xmit->tail += pdc->ofs;
  552. xmit->tail &= UART_XMIT_SIZE - 1;
  553. port->icount.tx += pdc->ofs;
  554. pdc->ofs = 0;
  555. /* more to transmit - setup next transfer */
  556. /* disable PDC transmit */
  557. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  558. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  559. dma_sync_single_for_device(port->dev,
  560. pdc->dma_addr,
  561. pdc->dma_size,
  562. DMA_TO_DEVICE);
  563. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  564. pdc->ofs = count;
  565. UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
  566. UART_PUT_TCR(port, count);
  567. /* re-enable PDC transmit */
  568. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  569. /* Enable interrupts */
  570. UART_PUT_IER(port, atmel_port->tx_done_mask);
  571. } else {
  572. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  573. /* DMA done, stop TX, start RX for RS485 */
  574. atmel_start_rx(port);
  575. }
  576. }
  577. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  578. uart_write_wakeup(port);
  579. }
  580. static void atmel_rx_from_ring(struct uart_port *port)
  581. {
  582. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  583. struct circ_buf *ring = &atmel_port->rx_ring;
  584. unsigned int flg;
  585. unsigned int status;
  586. while (ring->head != ring->tail) {
  587. struct atmel_uart_char c;
  588. /* Make sure c is loaded after head. */
  589. smp_rmb();
  590. c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
  591. ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  592. port->icount.rx++;
  593. status = c.status;
  594. flg = TTY_NORMAL;
  595. /*
  596. * note that the error handling code is
  597. * out of the main execution path
  598. */
  599. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  600. | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  601. if (status & ATMEL_US_RXBRK) {
  602. /* ignore side-effect */
  603. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  604. port->icount.brk++;
  605. if (uart_handle_break(port))
  606. continue;
  607. }
  608. if (status & ATMEL_US_PARE)
  609. port->icount.parity++;
  610. if (status & ATMEL_US_FRAME)
  611. port->icount.frame++;
  612. if (status & ATMEL_US_OVRE)
  613. port->icount.overrun++;
  614. status &= port->read_status_mask;
  615. if (status & ATMEL_US_RXBRK)
  616. flg = TTY_BREAK;
  617. else if (status & ATMEL_US_PARE)
  618. flg = TTY_PARITY;
  619. else if (status & ATMEL_US_FRAME)
  620. flg = TTY_FRAME;
  621. }
  622. if (uart_handle_sysrq_char(port, c.ch))
  623. continue;
  624. uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
  625. }
  626. /*
  627. * Drop the lock here since it might end up calling
  628. * uart_start(), which takes the lock.
  629. */
  630. spin_unlock(&port->lock);
  631. tty_flip_buffer_push(port->state->port.tty);
  632. spin_lock(&port->lock);
  633. }
  634. static void atmel_rx_from_dma(struct uart_port *port)
  635. {
  636. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  637. struct tty_struct *tty = port->state->port.tty;
  638. struct atmel_dma_buffer *pdc;
  639. int rx_idx = atmel_port->pdc_rx_idx;
  640. unsigned int head;
  641. unsigned int tail;
  642. unsigned int count;
  643. do {
  644. /* Reset the UART timeout early so that we don't miss one */
  645. UART_PUT_CR(port, ATMEL_US_STTTO);
  646. pdc = &atmel_port->pdc_rx[rx_idx];
  647. head = UART_GET_RPR(port) - pdc->dma_addr;
  648. tail = pdc->ofs;
  649. /* If the PDC has switched buffers, RPR won't contain
  650. * any address within the current buffer. Since head
  651. * is unsigned, we just need a one-way comparison to
  652. * find out.
  653. *
  654. * In this case, we just need to consume the entire
  655. * buffer and resubmit it for DMA. This will clear the
  656. * ENDRX bit as well, so that we can safely re-enable
  657. * all interrupts below.
  658. */
  659. head = min(head, pdc->dma_size);
  660. if (likely(head != tail)) {
  661. dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
  662. pdc->dma_size, DMA_FROM_DEVICE);
  663. /*
  664. * head will only wrap around when we recycle
  665. * the DMA buffer, and when that happens, we
  666. * explicitly set tail to 0. So head will
  667. * always be greater than tail.
  668. */
  669. count = head - tail;
  670. tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
  671. dma_sync_single_for_device(port->dev, pdc->dma_addr,
  672. pdc->dma_size, DMA_FROM_DEVICE);
  673. port->icount.rx += count;
  674. pdc->ofs = head;
  675. }
  676. /*
  677. * If the current buffer is full, we need to check if
  678. * the next one contains any additional data.
  679. */
  680. if (head >= pdc->dma_size) {
  681. pdc->ofs = 0;
  682. UART_PUT_RNPR(port, pdc->dma_addr);
  683. UART_PUT_RNCR(port, pdc->dma_size);
  684. rx_idx = !rx_idx;
  685. atmel_port->pdc_rx_idx = rx_idx;
  686. }
  687. } while (head >= pdc->dma_size);
  688. /*
  689. * Drop the lock here since it might end up calling
  690. * uart_start(), which takes the lock.
  691. */
  692. spin_unlock(&port->lock);
  693. tty_flip_buffer_push(tty);
  694. spin_lock(&port->lock);
  695. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  696. }
  697. /*
  698. * tasklet handling tty stuff outside the interrupt handler.
  699. */
  700. static void atmel_tasklet_func(unsigned long data)
  701. {
  702. struct uart_port *port = (struct uart_port *)data;
  703. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  704. unsigned int status;
  705. unsigned int status_change;
  706. /* The interrupt handler does not take the lock */
  707. spin_lock(&port->lock);
  708. if (atmel_use_dma_tx(port))
  709. atmel_tx_dma(port);
  710. else
  711. atmel_tx_chars(port);
  712. status = atmel_port->irq_status;
  713. status_change = status ^ atmel_port->irq_status_prev;
  714. if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
  715. | ATMEL_US_DCD | ATMEL_US_CTS)) {
  716. /* TODO: All reads to CSR will clear these interrupts! */
  717. if (status_change & ATMEL_US_RI)
  718. port->icount.rng++;
  719. if (status_change & ATMEL_US_DSR)
  720. port->icount.dsr++;
  721. if (status_change & ATMEL_US_DCD)
  722. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  723. if (status_change & ATMEL_US_CTS)
  724. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  725. wake_up_interruptible(&port->state->port.delta_msr_wait);
  726. atmel_port->irq_status_prev = status;
  727. }
  728. if (atmel_use_dma_rx(port))
  729. atmel_rx_from_dma(port);
  730. else
  731. atmel_rx_from_ring(port);
  732. spin_unlock(&port->lock);
  733. }
  734. /*
  735. * Perform initialization and enable port for reception
  736. */
  737. static int atmel_startup(struct uart_port *port)
  738. {
  739. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  740. struct tty_struct *tty = port->state->port.tty;
  741. int retval;
  742. /*
  743. * Ensure that no interrupts are enabled otherwise when
  744. * request_irq() is called we could get stuck trying to
  745. * handle an unexpected interrupt
  746. */
  747. UART_PUT_IDR(port, -1);
  748. /*
  749. * Allocate the IRQ
  750. */
  751. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
  752. tty ? tty->name : "atmel_serial", port);
  753. if (retval) {
  754. printk("atmel_serial: atmel_startup - Can't get irq\n");
  755. return retval;
  756. }
  757. /*
  758. * Initialize DMA (if necessary)
  759. */
  760. if (atmel_use_dma_rx(port)) {
  761. int i;
  762. for (i = 0; i < 2; i++) {
  763. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  764. pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
  765. if (pdc->buf == NULL) {
  766. if (i != 0) {
  767. dma_unmap_single(port->dev,
  768. atmel_port->pdc_rx[0].dma_addr,
  769. PDC_BUFFER_SIZE,
  770. DMA_FROM_DEVICE);
  771. kfree(atmel_port->pdc_rx[0].buf);
  772. }
  773. free_irq(port->irq, port);
  774. return -ENOMEM;
  775. }
  776. pdc->dma_addr = dma_map_single(port->dev,
  777. pdc->buf,
  778. PDC_BUFFER_SIZE,
  779. DMA_FROM_DEVICE);
  780. pdc->dma_size = PDC_BUFFER_SIZE;
  781. pdc->ofs = 0;
  782. }
  783. atmel_port->pdc_rx_idx = 0;
  784. UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
  785. UART_PUT_RCR(port, PDC_BUFFER_SIZE);
  786. UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
  787. UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
  788. }
  789. if (atmel_use_dma_tx(port)) {
  790. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  791. struct circ_buf *xmit = &port->state->xmit;
  792. pdc->buf = xmit->buf;
  793. pdc->dma_addr = dma_map_single(port->dev,
  794. pdc->buf,
  795. UART_XMIT_SIZE,
  796. DMA_TO_DEVICE);
  797. pdc->dma_size = UART_XMIT_SIZE;
  798. pdc->ofs = 0;
  799. }
  800. /*
  801. * If there is a specific "open" function (to register
  802. * control line interrupts)
  803. */
  804. if (atmel_open_hook) {
  805. retval = atmel_open_hook(port);
  806. if (retval) {
  807. free_irq(port->irq, port);
  808. return retval;
  809. }
  810. }
  811. /* Save current CSR for comparison in atmel_tasklet_func() */
  812. atmel_port->irq_status_prev = UART_GET_CSR(port);
  813. atmel_port->irq_status = atmel_port->irq_status_prev;
  814. /*
  815. * Finally, enable the serial port
  816. */
  817. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  818. /* enable xmit & rcvr */
  819. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  820. if (atmel_use_dma_rx(port)) {
  821. /* set UART timeout */
  822. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  823. UART_PUT_CR(port, ATMEL_US_STTTO);
  824. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  825. /* enable PDC controller */
  826. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  827. } else {
  828. /* enable receive only */
  829. UART_PUT_IER(port, ATMEL_US_RXRDY);
  830. }
  831. return 0;
  832. }
  833. /*
  834. * Disable the port
  835. */
  836. static void atmel_shutdown(struct uart_port *port)
  837. {
  838. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  839. /*
  840. * Ensure everything is stopped.
  841. */
  842. atmel_stop_rx(port);
  843. atmel_stop_tx(port);
  844. /*
  845. * Shut-down the DMA.
  846. */
  847. if (atmel_use_dma_rx(port)) {
  848. int i;
  849. for (i = 0; i < 2; i++) {
  850. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  851. dma_unmap_single(port->dev,
  852. pdc->dma_addr,
  853. pdc->dma_size,
  854. DMA_FROM_DEVICE);
  855. kfree(pdc->buf);
  856. }
  857. }
  858. if (atmel_use_dma_tx(port)) {
  859. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  860. dma_unmap_single(port->dev,
  861. pdc->dma_addr,
  862. pdc->dma_size,
  863. DMA_TO_DEVICE);
  864. }
  865. /*
  866. * Disable all interrupts, port and break condition.
  867. */
  868. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  869. UART_PUT_IDR(port, -1);
  870. /*
  871. * Free the interrupt
  872. */
  873. free_irq(port->irq, port);
  874. /*
  875. * If there is a specific "close" function (to unregister
  876. * control line interrupts)
  877. */
  878. if (atmel_close_hook)
  879. atmel_close_hook(port);
  880. }
  881. /*
  882. * Flush any TX data submitted for DMA. Called when the TX circular
  883. * buffer is reset.
  884. */
  885. static void atmel_flush_buffer(struct uart_port *port)
  886. {
  887. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  888. if (atmel_use_dma_tx(port)) {
  889. UART_PUT_TCR(port, 0);
  890. atmel_port->pdc_tx.ofs = 0;
  891. }
  892. }
  893. /*
  894. * Power / Clock management.
  895. */
  896. static void atmel_serial_pm(struct uart_port *port, unsigned int state,
  897. unsigned int oldstate)
  898. {
  899. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  900. switch (state) {
  901. case 0:
  902. /*
  903. * Enable the peripheral clock for this serial port.
  904. * This is called on uart_open() or a resume event.
  905. */
  906. clk_enable(atmel_port->clk);
  907. /* re-enable interrupts if we disabled some on suspend */
  908. UART_PUT_IER(port, atmel_port->backup_imr);
  909. break;
  910. case 3:
  911. /* Back up the interrupt mask and disable all interrupts */
  912. atmel_port->backup_imr = UART_GET_IMR(port);
  913. UART_PUT_IDR(port, -1);
  914. /*
  915. * Disable the peripheral clock for this serial port.
  916. * This is called on uart_close() or a suspend event.
  917. */
  918. clk_disable(atmel_port->clk);
  919. break;
  920. default:
  921. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  922. }
  923. }
  924. /*
  925. * Change the port parameters
  926. */
  927. static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  928. struct ktermios *old)
  929. {
  930. unsigned long flags;
  931. unsigned int mode, imr, quot, baud;
  932. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  933. /* Get current mode register */
  934. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
  935. | ATMEL_US_NBSTOP | ATMEL_US_PAR
  936. | ATMEL_US_USMODE);
  937. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  938. quot = uart_get_divisor(port, baud);
  939. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  940. quot /= 8;
  941. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  942. }
  943. /* byte size */
  944. switch (termios->c_cflag & CSIZE) {
  945. case CS5:
  946. mode |= ATMEL_US_CHRL_5;
  947. break;
  948. case CS6:
  949. mode |= ATMEL_US_CHRL_6;
  950. break;
  951. case CS7:
  952. mode |= ATMEL_US_CHRL_7;
  953. break;
  954. default:
  955. mode |= ATMEL_US_CHRL_8;
  956. break;
  957. }
  958. /* stop bits */
  959. if (termios->c_cflag & CSTOPB)
  960. mode |= ATMEL_US_NBSTOP_2;
  961. /* parity */
  962. if (termios->c_cflag & PARENB) {
  963. /* Mark or Space parity */
  964. if (termios->c_cflag & CMSPAR) {
  965. if (termios->c_cflag & PARODD)
  966. mode |= ATMEL_US_PAR_MARK;
  967. else
  968. mode |= ATMEL_US_PAR_SPACE;
  969. } else if (termios->c_cflag & PARODD)
  970. mode |= ATMEL_US_PAR_ODD;
  971. else
  972. mode |= ATMEL_US_PAR_EVEN;
  973. } else
  974. mode |= ATMEL_US_PAR_NONE;
  975. /* hardware handshake (RTS/CTS) */
  976. if (termios->c_cflag & CRTSCTS)
  977. mode |= ATMEL_US_USMODE_HWHS;
  978. else
  979. mode |= ATMEL_US_USMODE_NORMAL;
  980. spin_lock_irqsave(&port->lock, flags);
  981. port->read_status_mask = ATMEL_US_OVRE;
  982. if (termios->c_iflag & INPCK)
  983. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  984. if (termios->c_iflag & (BRKINT | PARMRK))
  985. port->read_status_mask |= ATMEL_US_RXBRK;
  986. if (atmel_use_dma_rx(port))
  987. /* need to enable error interrupts */
  988. UART_PUT_IER(port, port->read_status_mask);
  989. /*
  990. * Characters to ignore
  991. */
  992. port->ignore_status_mask = 0;
  993. if (termios->c_iflag & IGNPAR)
  994. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  995. if (termios->c_iflag & IGNBRK) {
  996. port->ignore_status_mask |= ATMEL_US_RXBRK;
  997. /*
  998. * If we're ignoring parity and break indicators,
  999. * ignore overruns too (for real raw support).
  1000. */
  1001. if (termios->c_iflag & IGNPAR)
  1002. port->ignore_status_mask |= ATMEL_US_OVRE;
  1003. }
  1004. /* TODO: Ignore all characters if CREAD is set.*/
  1005. /* update the per-port timeout */
  1006. uart_update_timeout(port, termios->c_cflag, baud);
  1007. /*
  1008. * save/disable interrupts. The tty layer will ensure that the
  1009. * transmitter is empty if requested by the caller, so there's
  1010. * no need to wait for it here.
  1011. */
  1012. imr = UART_GET_IMR(port);
  1013. UART_PUT_IDR(port, -1);
  1014. /* disable receiver and transmitter */
  1015. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  1016. /* Resetting serial mode to RS232 (0x0) */
  1017. mode &= ~ATMEL_US_USMODE;
  1018. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  1019. dev_dbg(port->dev, "Setting UART to RS485\n");
  1020. UART_PUT_TTGR(port, atmel_port->rs485.delay_rts_before_send);
  1021. mode |= ATMEL_US_USMODE_RS485;
  1022. } else {
  1023. dev_dbg(port->dev, "Setting UART to RS232\n");
  1024. }
  1025. /* set the parity, stop bits and data size */
  1026. UART_PUT_MR(port, mode);
  1027. /* set the baud rate */
  1028. UART_PUT_BRGR(port, quot);
  1029. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1030. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1031. /* restore interrupts */
  1032. UART_PUT_IER(port, imr);
  1033. /* CTS flow-control and modem-status interrupts */
  1034. if (UART_ENABLE_MS(port, termios->c_cflag))
  1035. port->ops->enable_ms(port);
  1036. spin_unlock_irqrestore(&port->lock, flags);
  1037. }
  1038. /*
  1039. * Return string describing the specified port
  1040. */
  1041. static const char *atmel_type(struct uart_port *port)
  1042. {
  1043. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  1044. }
  1045. /*
  1046. * Release the memory region(s) being used by 'port'.
  1047. */
  1048. static void atmel_release_port(struct uart_port *port)
  1049. {
  1050. struct platform_device *pdev = to_platform_device(port->dev);
  1051. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1052. release_mem_region(port->mapbase, size);
  1053. if (port->flags & UPF_IOREMAP) {
  1054. iounmap(port->membase);
  1055. port->membase = NULL;
  1056. }
  1057. }
  1058. /*
  1059. * Request the memory region(s) being used by 'port'.
  1060. */
  1061. static int atmel_request_port(struct uart_port *port)
  1062. {
  1063. struct platform_device *pdev = to_platform_device(port->dev);
  1064. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1065. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  1066. return -EBUSY;
  1067. if (port->flags & UPF_IOREMAP) {
  1068. port->membase = ioremap(port->mapbase, size);
  1069. if (port->membase == NULL) {
  1070. release_mem_region(port->mapbase, size);
  1071. return -ENOMEM;
  1072. }
  1073. }
  1074. return 0;
  1075. }
  1076. /*
  1077. * Configure/autoconfigure the port.
  1078. */
  1079. static void atmel_config_port(struct uart_port *port, int flags)
  1080. {
  1081. if (flags & UART_CONFIG_TYPE) {
  1082. port->type = PORT_ATMEL;
  1083. atmel_request_port(port);
  1084. }
  1085. }
  1086. /*
  1087. * Verify the new serial_struct (for TIOCSSERIAL).
  1088. */
  1089. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  1090. {
  1091. int ret = 0;
  1092. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  1093. ret = -EINVAL;
  1094. if (port->irq != ser->irq)
  1095. ret = -EINVAL;
  1096. if (ser->io_type != SERIAL_IO_MEM)
  1097. ret = -EINVAL;
  1098. if (port->uartclk / 16 != ser->baud_base)
  1099. ret = -EINVAL;
  1100. if ((void *)port->mapbase != ser->iomem_base)
  1101. ret = -EINVAL;
  1102. if (port->iobase != ser->port)
  1103. ret = -EINVAL;
  1104. if (ser->hub6 != 0)
  1105. ret = -EINVAL;
  1106. return ret;
  1107. }
  1108. #ifdef CONFIG_CONSOLE_POLL
  1109. static int atmel_poll_get_char(struct uart_port *port)
  1110. {
  1111. while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
  1112. cpu_relax();
  1113. return UART_GET_CHAR(port);
  1114. }
  1115. static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
  1116. {
  1117. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1118. cpu_relax();
  1119. UART_PUT_CHAR(port, ch);
  1120. }
  1121. #endif
  1122. static int
  1123. atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
  1124. {
  1125. struct serial_rs485 rs485conf;
  1126. switch (cmd) {
  1127. case TIOCSRS485:
  1128. if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
  1129. sizeof(rs485conf)))
  1130. return -EFAULT;
  1131. atmel_config_rs485(port, &rs485conf);
  1132. break;
  1133. case TIOCGRS485:
  1134. if (copy_to_user((struct serial_rs485 *) arg,
  1135. &(to_atmel_uart_port(port)->rs485),
  1136. sizeof(rs485conf)))
  1137. return -EFAULT;
  1138. break;
  1139. default:
  1140. return -ENOIOCTLCMD;
  1141. }
  1142. return 0;
  1143. }
  1144. static struct uart_ops atmel_pops = {
  1145. .tx_empty = atmel_tx_empty,
  1146. .set_mctrl = atmel_set_mctrl,
  1147. .get_mctrl = atmel_get_mctrl,
  1148. .stop_tx = atmel_stop_tx,
  1149. .start_tx = atmel_start_tx,
  1150. .stop_rx = atmel_stop_rx,
  1151. .enable_ms = atmel_enable_ms,
  1152. .break_ctl = atmel_break_ctl,
  1153. .startup = atmel_startup,
  1154. .shutdown = atmel_shutdown,
  1155. .flush_buffer = atmel_flush_buffer,
  1156. .set_termios = atmel_set_termios,
  1157. .type = atmel_type,
  1158. .release_port = atmel_release_port,
  1159. .request_port = atmel_request_port,
  1160. .config_port = atmel_config_port,
  1161. .verify_port = atmel_verify_port,
  1162. .pm = atmel_serial_pm,
  1163. .ioctl = atmel_ioctl,
  1164. #ifdef CONFIG_CONSOLE_POLL
  1165. .poll_get_char = atmel_poll_get_char,
  1166. .poll_put_char = atmel_poll_put_char,
  1167. #endif
  1168. };
  1169. /*
  1170. * Configure the port from the platform device resource info.
  1171. */
  1172. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
  1173. struct platform_device *pdev)
  1174. {
  1175. struct uart_port *port = &atmel_port->uart;
  1176. struct atmel_uart_data *data = pdev->dev.platform_data;
  1177. port->iotype = UPIO_MEM;
  1178. port->flags = UPF_BOOT_AUTOCONF;
  1179. port->ops = &atmel_pops;
  1180. port->fifosize = 1;
  1181. port->line = pdev->id;
  1182. port->dev = &pdev->dev;
  1183. port->mapbase = pdev->resource[0].start;
  1184. port->irq = pdev->resource[1].start;
  1185. tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
  1186. (unsigned long)port);
  1187. memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
  1188. if (data->regs)
  1189. /* Already mapped by setup code */
  1190. port->membase = data->regs;
  1191. else {
  1192. port->flags |= UPF_IOREMAP;
  1193. port->membase = NULL;
  1194. }
  1195. /* for console, the clock could already be configured */
  1196. if (!atmel_port->clk) {
  1197. atmel_port->clk = clk_get(&pdev->dev, "usart");
  1198. clk_enable(atmel_port->clk);
  1199. port->uartclk = clk_get_rate(atmel_port->clk);
  1200. clk_disable(atmel_port->clk);
  1201. /* only enable clock when USART is in use */
  1202. }
  1203. atmel_port->use_dma_rx = data->use_dma_rx;
  1204. atmel_port->use_dma_tx = data->use_dma_tx;
  1205. atmel_port->rs485 = data->rs485;
  1206. /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
  1207. if (atmel_port->rs485.flags & SER_RS485_ENABLED)
  1208. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  1209. else if (atmel_use_dma_tx(port)) {
  1210. port->fifosize = PDC_BUFFER_SIZE;
  1211. atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
  1212. } else {
  1213. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  1214. }
  1215. }
  1216. /*
  1217. * Register board-specific modem-control line handlers.
  1218. */
  1219. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  1220. {
  1221. if (fns->enable_ms)
  1222. atmel_pops.enable_ms = fns->enable_ms;
  1223. if (fns->get_mctrl)
  1224. atmel_pops.get_mctrl = fns->get_mctrl;
  1225. if (fns->set_mctrl)
  1226. atmel_pops.set_mctrl = fns->set_mctrl;
  1227. atmel_open_hook = fns->open;
  1228. atmel_close_hook = fns->close;
  1229. atmel_pops.pm = fns->pm;
  1230. atmel_pops.set_wake = fns->set_wake;
  1231. }
  1232. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1233. static void atmel_console_putchar(struct uart_port *port, int ch)
  1234. {
  1235. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1236. cpu_relax();
  1237. UART_PUT_CHAR(port, ch);
  1238. }
  1239. /*
  1240. * Interrupts are disabled on entering
  1241. */
  1242. static void atmel_console_write(struct console *co, const char *s, u_int count)
  1243. {
  1244. struct uart_port *port = &atmel_ports[co->index].uart;
  1245. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1246. unsigned int status, imr;
  1247. unsigned int pdc_tx;
  1248. /*
  1249. * First, save IMR and then disable interrupts
  1250. */
  1251. imr = UART_GET_IMR(port);
  1252. UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
  1253. /* Store PDC transmit status and disable it */
  1254. pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
  1255. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  1256. uart_console_write(port, s, count, atmel_console_putchar);
  1257. /*
  1258. * Finally, wait for transmitter to become empty
  1259. * and restore IMR
  1260. */
  1261. do {
  1262. status = UART_GET_CSR(port);
  1263. } while (!(status & ATMEL_US_TXRDY));
  1264. /* Restore PDC transmit status */
  1265. if (pdc_tx)
  1266. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  1267. /* set interrupts back the way they were */
  1268. UART_PUT_IER(port, imr);
  1269. }
  1270. /*
  1271. * If the port was already initialised (eg, by a boot loader),
  1272. * try to determine the current setup.
  1273. */
  1274. static void __init atmel_console_get_options(struct uart_port *port, int *baud,
  1275. int *parity, int *bits)
  1276. {
  1277. unsigned int mr, quot;
  1278. /*
  1279. * If the baud rate generator isn't running, the port wasn't
  1280. * initialized by the boot loader.
  1281. */
  1282. quot = UART_GET_BRGR(port) & ATMEL_US_CD;
  1283. if (!quot)
  1284. return;
  1285. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  1286. if (mr == ATMEL_US_CHRL_8)
  1287. *bits = 8;
  1288. else
  1289. *bits = 7;
  1290. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  1291. if (mr == ATMEL_US_PAR_EVEN)
  1292. *parity = 'e';
  1293. else if (mr == ATMEL_US_PAR_ODD)
  1294. *parity = 'o';
  1295. /*
  1296. * The serial core only rounds down when matching this to a
  1297. * supported baud rate. Make sure we don't end up slightly
  1298. * lower than one of those, as it would make us fall through
  1299. * to a much lower baud rate than we really want.
  1300. */
  1301. *baud = port->uartclk / (16 * (quot - 1));
  1302. }
  1303. static int __init atmel_console_setup(struct console *co, char *options)
  1304. {
  1305. struct uart_port *port = &atmel_ports[co->index].uart;
  1306. int baud = 115200;
  1307. int bits = 8;
  1308. int parity = 'n';
  1309. int flow = 'n';
  1310. if (port->membase == NULL) {
  1311. /* Port not initialized yet - delay setup */
  1312. return -ENODEV;
  1313. }
  1314. clk_enable(atmel_ports[co->index].clk);
  1315. UART_PUT_IDR(port, -1);
  1316. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1317. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1318. if (options)
  1319. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1320. else
  1321. atmel_console_get_options(port, &baud, &parity, &bits);
  1322. return uart_set_options(port, co, baud, parity, bits, flow);
  1323. }
  1324. static struct uart_driver atmel_uart;
  1325. static struct console atmel_console = {
  1326. .name = ATMEL_DEVICENAME,
  1327. .write = atmel_console_write,
  1328. .device = uart_console_device,
  1329. .setup = atmel_console_setup,
  1330. .flags = CON_PRINTBUFFER,
  1331. .index = -1,
  1332. .data = &atmel_uart,
  1333. };
  1334. #define ATMEL_CONSOLE_DEVICE (&atmel_console)
  1335. /*
  1336. * Early console initialization (before VM subsystem initialized).
  1337. */
  1338. static int __init atmel_console_init(void)
  1339. {
  1340. if (atmel_default_console_device) {
  1341. add_preferred_console(ATMEL_DEVICENAME,
  1342. atmel_default_console_device->id, NULL);
  1343. atmel_init_port(&atmel_ports[atmel_default_console_device->id],
  1344. atmel_default_console_device);
  1345. register_console(&atmel_console);
  1346. }
  1347. return 0;
  1348. }
  1349. console_initcall(atmel_console_init);
  1350. /*
  1351. * Late console initialization.
  1352. */
  1353. static int __init atmel_late_console_init(void)
  1354. {
  1355. if (atmel_default_console_device
  1356. && !(atmel_console.flags & CON_ENABLED))
  1357. register_console(&atmel_console);
  1358. return 0;
  1359. }
  1360. core_initcall(atmel_late_console_init);
  1361. static inline bool atmel_is_console_port(struct uart_port *port)
  1362. {
  1363. return port->cons && port->cons->index == port->line;
  1364. }
  1365. #else
  1366. #define ATMEL_CONSOLE_DEVICE NULL
  1367. static inline bool atmel_is_console_port(struct uart_port *port)
  1368. {
  1369. return false;
  1370. }
  1371. #endif
  1372. static struct uart_driver atmel_uart = {
  1373. .owner = THIS_MODULE,
  1374. .driver_name = "atmel_serial",
  1375. .dev_name = ATMEL_DEVICENAME,
  1376. .major = SERIAL_ATMEL_MAJOR,
  1377. .minor = MINOR_START,
  1378. .nr = ATMEL_MAX_UART,
  1379. .cons = ATMEL_CONSOLE_DEVICE,
  1380. };
  1381. #ifdef CONFIG_PM
  1382. static bool atmel_serial_clk_will_stop(void)
  1383. {
  1384. #ifdef CONFIG_ARCH_AT91
  1385. return at91_suspend_entering_slow_clock();
  1386. #else
  1387. return false;
  1388. #endif
  1389. }
  1390. static int atmel_serial_suspend(struct platform_device *pdev,
  1391. pm_message_t state)
  1392. {
  1393. struct uart_port *port = platform_get_drvdata(pdev);
  1394. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1395. if (atmel_is_console_port(port) && console_suspend_enabled) {
  1396. /* Drain the TX shifter */
  1397. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  1398. cpu_relax();
  1399. }
  1400. /* we can not wake up if we're running on slow clock */
  1401. atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
  1402. if (atmel_serial_clk_will_stop())
  1403. device_set_wakeup_enable(&pdev->dev, 0);
  1404. uart_suspend_port(&atmel_uart, port);
  1405. return 0;
  1406. }
  1407. static int atmel_serial_resume(struct platform_device *pdev)
  1408. {
  1409. struct uart_port *port = platform_get_drvdata(pdev);
  1410. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1411. uart_resume_port(&atmel_uart, port);
  1412. device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
  1413. return 0;
  1414. }
  1415. #else
  1416. #define atmel_serial_suspend NULL
  1417. #define atmel_serial_resume NULL
  1418. #endif
  1419. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  1420. {
  1421. struct atmel_uart_port *port;
  1422. void *data;
  1423. int ret;
  1424. BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
  1425. port = &atmel_ports[pdev->id];
  1426. port->backup_imr = 0;
  1427. atmel_init_port(port, pdev);
  1428. if (!atmel_use_dma_rx(&port->uart)) {
  1429. ret = -ENOMEM;
  1430. data = kmalloc(sizeof(struct atmel_uart_char)
  1431. * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
  1432. if (!data)
  1433. goto err_alloc_ring;
  1434. port->rx_ring.buf = data;
  1435. }
  1436. ret = uart_add_one_port(&atmel_uart, &port->uart);
  1437. if (ret)
  1438. goto err_add_port;
  1439. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1440. if (atmel_is_console_port(&port->uart)
  1441. && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
  1442. /*
  1443. * The serial core enabled the clock for us, so undo
  1444. * the clk_enable() in atmel_console_setup()
  1445. */
  1446. clk_disable(port->clk);
  1447. }
  1448. #endif
  1449. device_init_wakeup(&pdev->dev, 1);
  1450. platform_set_drvdata(pdev, port);
  1451. return 0;
  1452. err_add_port:
  1453. kfree(port->rx_ring.buf);
  1454. port->rx_ring.buf = NULL;
  1455. err_alloc_ring:
  1456. if (!atmel_is_console_port(&port->uart)) {
  1457. clk_put(port->clk);
  1458. port->clk = NULL;
  1459. }
  1460. return ret;
  1461. }
  1462. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  1463. {
  1464. struct uart_port *port = platform_get_drvdata(pdev);
  1465. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1466. int ret = 0;
  1467. device_init_wakeup(&pdev->dev, 0);
  1468. platform_set_drvdata(pdev, NULL);
  1469. ret = uart_remove_one_port(&atmel_uart, port);
  1470. tasklet_kill(&atmel_port->tasklet);
  1471. kfree(atmel_port->rx_ring.buf);
  1472. /* "port" is allocated statically, so we shouldn't free it */
  1473. clk_put(atmel_port->clk);
  1474. return ret;
  1475. }
  1476. static struct platform_driver atmel_serial_driver = {
  1477. .probe = atmel_serial_probe,
  1478. .remove = __devexit_p(atmel_serial_remove),
  1479. .suspend = atmel_serial_suspend,
  1480. .resume = atmel_serial_resume,
  1481. .driver = {
  1482. .name = "atmel_usart",
  1483. .owner = THIS_MODULE,
  1484. },
  1485. };
  1486. static int __init atmel_serial_init(void)
  1487. {
  1488. int ret;
  1489. ret = uart_register_driver(&atmel_uart);
  1490. if (ret)
  1491. return ret;
  1492. ret = platform_driver_register(&atmel_serial_driver);
  1493. if (ret)
  1494. uart_unregister_driver(&atmel_uart);
  1495. return ret;
  1496. }
  1497. static void __exit atmel_serial_exit(void)
  1498. {
  1499. platform_driver_unregister(&atmel_serial_driver);
  1500. uart_unregister_driver(&atmel_uart);
  1501. }
  1502. module_init(atmel_serial_init);
  1503. module_exit(atmel_serial_exit);
  1504. MODULE_AUTHOR("Rick Bronson");
  1505. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  1506. MODULE_LICENSE("GPL");
  1507. MODULE_ALIAS("platform:atmel_usart");