atmel_serial.c 47 KB

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