atmel_serial.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  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 <linux/platform_data/atmel.h>
  43. #include <asm/io.h>
  44. #include <asm/ioctls.h>
  45. #ifdef CONFIG_ARM
  46. #include <mach/cpu.h>
  47. #include <asm/gpio.h>
  48. #endif
  49. #define PDC_BUFFER_SIZE 512
  50. /* Revisit: We should calculate this based on the actual port settings */
  51. #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
  52. #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  53. #define SUPPORT_SYSRQ
  54. #endif
  55. #include <linux/serial_core.h>
  56. static void atmel_start_rx(struct uart_port *port);
  57. static void atmel_stop_rx(struct uart_port *port);
  58. #ifdef CONFIG_SERIAL_ATMEL_TTYAT
  59. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  60. * should coexist with the 8250 driver, such as if we have an external 16C550
  61. * UART. */
  62. #define SERIAL_ATMEL_MAJOR 204
  63. #define MINOR_START 154
  64. #define ATMEL_DEVICENAME "ttyAT"
  65. #else
  66. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  67. * name, but it is legally reserved for the 8250 driver. */
  68. #define SERIAL_ATMEL_MAJOR TTY_MAJOR
  69. #define MINOR_START 64
  70. #define ATMEL_DEVICENAME "ttyS"
  71. #endif
  72. #define ATMEL_ISR_PASS_LIMIT 256
  73. /* UART registers. CR is write-only, hence no GET macro */
  74. #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  75. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  76. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  77. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  78. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  79. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  80. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  81. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  82. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  83. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  84. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  85. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  86. #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
  87. #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
  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. bool use_dma_rx; /* enable DMA receiver */
  122. bool use_pdc_rx; /* enable PDC receiver */
  123. short pdc_rx_idx; /* current PDC RX buffer */
  124. struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
  125. bool use_dma_tx; /* enable DMA transmitter */
  126. bool use_pdc_tx; /* enable PDC transmitter */
  127. struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
  128. spinlock_t lock_tx; /* port lock */
  129. spinlock_t lock_rx; /* port lock */
  130. struct dma_chan *chan_tx;
  131. struct dma_chan *chan_rx;
  132. struct dma_async_tx_descriptor *desc_tx;
  133. struct dma_async_tx_descriptor *desc_rx;
  134. dma_cookie_t cookie_tx;
  135. dma_cookie_t cookie_rx;
  136. struct scatterlist sg_tx;
  137. struct scatterlist sg_rx;
  138. struct tasklet_struct tasklet;
  139. unsigned int irq_status;
  140. unsigned int irq_status_prev;
  141. struct circ_buf rx_ring;
  142. struct serial_rs485 rs485; /* rs485 settings */
  143. unsigned int tx_done_mask;
  144. bool is_usart; /* usart or uart */
  145. int (*prepare_rx)(struct uart_port *port);
  146. int (*prepare_tx)(struct uart_port *port);
  147. void (*schedule_rx)(struct uart_port *port);
  148. void (*schedule_tx)(struct uart_port *port);
  149. void (*release_rx)(struct uart_port *port);
  150. void (*release_tx)(struct uart_port *port);
  151. };
  152. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  153. static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
  154. #ifdef SUPPORT_SYSRQ
  155. static struct console atmel_console;
  156. #endif
  157. #if defined(CONFIG_OF)
  158. static const struct of_device_id atmel_serial_dt_ids[] = {
  159. { .compatible = "atmel,at91rm9200-usart" },
  160. { .compatible = "atmel,at91sam9260-usart" },
  161. { /* sentinel */ }
  162. };
  163. MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
  164. #endif
  165. static inline struct atmel_uart_port *
  166. to_atmel_uart_port(struct uart_port *uart)
  167. {
  168. return container_of(uart, struct atmel_uart_port, uart);
  169. }
  170. #ifdef CONFIG_SERIAL_ATMEL_PDC
  171. static bool atmel_use_pdc_rx(struct uart_port *port)
  172. {
  173. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  174. return atmel_port->use_pdc_rx;
  175. }
  176. static bool atmel_use_pdc_tx(struct uart_port *port)
  177. {
  178. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  179. return atmel_port->use_pdc_tx;
  180. }
  181. #else
  182. static bool atmel_use_pdc_rx(struct uart_port *port)
  183. {
  184. return false;
  185. }
  186. static bool atmel_use_pdc_tx(struct uart_port *port)
  187. {
  188. return false;
  189. }
  190. #endif
  191. static bool atmel_use_dma_tx(struct uart_port *port)
  192. {
  193. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  194. return atmel_port->use_dma_tx;
  195. }
  196. static bool atmel_use_dma_rx(struct uart_port *port)
  197. {
  198. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  199. return atmel_port->use_dma_rx;
  200. }
  201. /* Enable or disable the rs485 support */
  202. void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
  203. {
  204. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  205. unsigned int mode;
  206. unsigned long flags;
  207. spin_lock_irqsave(&port->lock, flags);
  208. /* Disable interrupts */
  209. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  210. mode = UART_GET_MR(port);
  211. /* Resetting serial mode to RS232 (0x0) */
  212. mode &= ~ATMEL_US_USMODE;
  213. atmel_port->rs485 = *rs485conf;
  214. if (rs485conf->flags & SER_RS485_ENABLED) {
  215. dev_dbg(port->dev, "Setting UART to RS485\n");
  216. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  217. if ((rs485conf->delay_rts_after_send) > 0)
  218. UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
  219. mode |= ATMEL_US_USMODE_RS485;
  220. } else {
  221. dev_dbg(port->dev, "Setting UART to RS232\n");
  222. if (atmel_use_pdc_tx(port))
  223. atmel_port->tx_done_mask = ATMEL_US_ENDTX |
  224. ATMEL_US_TXBUFE;
  225. else
  226. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  227. }
  228. UART_PUT_MR(port, mode);
  229. /* Enable interrupts */
  230. UART_PUT_IER(port, atmel_port->tx_done_mask);
  231. spin_unlock_irqrestore(&port->lock, flags);
  232. }
  233. /*
  234. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  235. */
  236. static u_int atmel_tx_empty(struct uart_port *port)
  237. {
  238. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  239. }
  240. /*
  241. * Set state of the modem control output lines
  242. */
  243. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  244. {
  245. unsigned int control = 0;
  246. unsigned int mode;
  247. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  248. #ifdef CONFIG_ARCH_AT91RM9200
  249. if (cpu_is_at91rm9200()) {
  250. /*
  251. * AT91RM9200 Errata #39: RTS0 is not internally connected
  252. * to PA21. We need to drive the pin manually.
  253. */
  254. if (port->mapbase == AT91RM9200_BASE_US0) {
  255. if (mctrl & TIOCM_RTS)
  256. at91_set_gpio_value(AT91_PIN_PA21, 0);
  257. else
  258. at91_set_gpio_value(AT91_PIN_PA21, 1);
  259. }
  260. }
  261. #endif
  262. if (mctrl & TIOCM_RTS)
  263. control |= ATMEL_US_RTSEN;
  264. else
  265. control |= ATMEL_US_RTSDIS;
  266. if (mctrl & TIOCM_DTR)
  267. control |= ATMEL_US_DTREN;
  268. else
  269. control |= ATMEL_US_DTRDIS;
  270. UART_PUT_CR(port, control);
  271. /* Local loopback mode? */
  272. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  273. if (mctrl & TIOCM_LOOP)
  274. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  275. else
  276. mode |= ATMEL_US_CHMODE_NORMAL;
  277. /* Resetting serial mode to RS232 (0x0) */
  278. mode &= ~ATMEL_US_USMODE;
  279. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  280. dev_dbg(port->dev, "Setting UART to RS485\n");
  281. if ((atmel_port->rs485.delay_rts_after_send) > 0)
  282. UART_PUT_TTGR(port,
  283. atmel_port->rs485.delay_rts_after_send);
  284. mode |= ATMEL_US_USMODE_RS485;
  285. } else {
  286. dev_dbg(port->dev, "Setting UART to RS232\n");
  287. }
  288. UART_PUT_MR(port, mode);
  289. }
  290. /*
  291. * Get state of the modem control input lines
  292. */
  293. static u_int atmel_get_mctrl(struct uart_port *port)
  294. {
  295. unsigned int status, ret = 0;
  296. status = UART_GET_CSR(port);
  297. /*
  298. * The control signals are active low.
  299. */
  300. if (!(status & ATMEL_US_DCD))
  301. ret |= TIOCM_CD;
  302. if (!(status & ATMEL_US_CTS))
  303. ret |= TIOCM_CTS;
  304. if (!(status & ATMEL_US_DSR))
  305. ret |= TIOCM_DSR;
  306. if (!(status & ATMEL_US_RI))
  307. ret |= TIOCM_RI;
  308. return ret;
  309. }
  310. /*
  311. * Stop transmitting.
  312. */
  313. static void atmel_stop_tx(struct uart_port *port)
  314. {
  315. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  316. if (atmel_use_pdc_tx(port)) {
  317. /* disable PDC transmit */
  318. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  319. }
  320. /* Disable interrupts */
  321. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  322. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  323. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
  324. atmel_start_rx(port);
  325. }
  326. /*
  327. * Start transmitting.
  328. */
  329. static void atmel_start_tx(struct uart_port *port)
  330. {
  331. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  332. if (atmel_use_pdc_tx(port)) {
  333. if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
  334. /* The transmitter is already running. Yes, we
  335. really need this.*/
  336. return;
  337. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  338. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
  339. atmel_stop_rx(port);
  340. /* re-enable PDC transmit */
  341. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  342. }
  343. /* Enable interrupts */
  344. UART_PUT_IER(port, atmel_port->tx_done_mask);
  345. }
  346. /*
  347. * start receiving - port is in process of being opened.
  348. */
  349. static void atmel_start_rx(struct uart_port *port)
  350. {
  351. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
  352. UART_PUT_CR(port, ATMEL_US_RXEN);
  353. if (atmel_use_pdc_rx(port)) {
  354. /* enable PDC controller */
  355. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  356. port->read_status_mask);
  357. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  358. } else {
  359. UART_PUT_IER(port, ATMEL_US_RXRDY);
  360. }
  361. }
  362. /*
  363. * Stop receiving - port is in process of being closed.
  364. */
  365. static void atmel_stop_rx(struct uart_port *port)
  366. {
  367. UART_PUT_CR(port, ATMEL_US_RXDIS);
  368. if (atmel_use_pdc_rx(port)) {
  369. /* disable PDC receive */
  370. UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
  371. UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  372. port->read_status_mask);
  373. } else {
  374. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  375. }
  376. }
  377. /*
  378. * Enable modem status interrupts
  379. */
  380. static void atmel_enable_ms(struct uart_port *port)
  381. {
  382. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
  383. | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  384. }
  385. /*
  386. * Control the transmission of a break signal
  387. */
  388. static void atmel_break_ctl(struct uart_port *port, int break_state)
  389. {
  390. if (break_state != 0)
  391. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  392. else
  393. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  394. }
  395. /*
  396. * Stores the incoming character in the ring buffer
  397. */
  398. static void
  399. atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
  400. unsigned int ch)
  401. {
  402. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  403. struct circ_buf *ring = &atmel_port->rx_ring;
  404. struct atmel_uart_char *c;
  405. if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
  406. /* Buffer overflow, ignore char */
  407. return;
  408. c = &((struct atmel_uart_char *)ring->buf)[ring->head];
  409. c->status = status;
  410. c->ch = ch;
  411. /* Make sure the character is stored before we update head. */
  412. smp_wmb();
  413. ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  414. }
  415. /*
  416. * Deal with parity, framing and overrun errors.
  417. */
  418. static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
  419. {
  420. /* clear error */
  421. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  422. if (status & ATMEL_US_RXBRK) {
  423. /* ignore side-effect */
  424. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  425. port->icount.brk++;
  426. }
  427. if (status & ATMEL_US_PARE)
  428. port->icount.parity++;
  429. if (status & ATMEL_US_FRAME)
  430. port->icount.frame++;
  431. if (status & ATMEL_US_OVRE)
  432. port->icount.overrun++;
  433. }
  434. /*
  435. * Characters received (called from interrupt handler)
  436. */
  437. static void atmel_rx_chars(struct uart_port *port)
  438. {
  439. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  440. unsigned int status, ch;
  441. status = UART_GET_CSR(port);
  442. while (status & ATMEL_US_RXRDY) {
  443. ch = UART_GET_CHAR(port);
  444. /*
  445. * note that the error handling code is
  446. * out of the main execution path
  447. */
  448. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  449. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  450. || atmel_port->break_active)) {
  451. /* clear error */
  452. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  453. if (status & ATMEL_US_RXBRK
  454. && !atmel_port->break_active) {
  455. atmel_port->break_active = 1;
  456. UART_PUT_IER(port, ATMEL_US_RXBRK);
  457. } else {
  458. /*
  459. * This is either the end-of-break
  460. * condition or we've received at
  461. * least one character without RXBRK
  462. * being set. In both cases, the next
  463. * RXBRK will indicate start-of-break.
  464. */
  465. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  466. status &= ~ATMEL_US_RXBRK;
  467. atmel_port->break_active = 0;
  468. }
  469. }
  470. atmel_buffer_rx_char(port, status, ch);
  471. status = UART_GET_CSR(port);
  472. }
  473. tasklet_schedule(&atmel_port->tasklet);
  474. }
  475. /*
  476. * Transmit characters (called from tasklet with TXRDY interrupt
  477. * disabled)
  478. */
  479. static void atmel_tx_chars(struct uart_port *port)
  480. {
  481. struct circ_buf *xmit = &port->state->xmit;
  482. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  483. if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  484. UART_PUT_CHAR(port, port->x_char);
  485. port->icount.tx++;
  486. port->x_char = 0;
  487. }
  488. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  489. return;
  490. while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  491. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  492. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  493. port->icount.tx++;
  494. if (uart_circ_empty(xmit))
  495. break;
  496. }
  497. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  498. uart_write_wakeup(port);
  499. if (!uart_circ_empty(xmit))
  500. /* Enable interrupts */
  501. UART_PUT_IER(port, atmel_port->tx_done_mask);
  502. }
  503. static void atmel_complete_tx_dma(void *arg)
  504. {
  505. struct atmel_uart_port *atmel_port = arg;
  506. struct uart_port *port = &atmel_port->uart;
  507. struct circ_buf *xmit = &port->state->xmit;
  508. struct dma_chan *chan = atmel_port->chan_tx;
  509. unsigned long flags;
  510. spin_lock_irqsave(&port->lock, flags);
  511. if (chan)
  512. dmaengine_terminate_all(chan);
  513. xmit->tail += sg_dma_len(&atmel_port->sg_tx);
  514. xmit->tail &= UART_XMIT_SIZE - 1;
  515. port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
  516. spin_lock_irq(&atmel_port->lock_tx);
  517. async_tx_ack(atmel_port->desc_tx);
  518. atmel_port->cookie_tx = -EINVAL;
  519. atmel_port->desc_tx = NULL;
  520. spin_unlock_irq(&atmel_port->lock_tx);
  521. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  522. uart_write_wakeup(port);
  523. /* Do we really need this? */
  524. if (!uart_circ_empty(xmit))
  525. tasklet_schedule(&atmel_port->tasklet);
  526. spin_unlock_irqrestore(&port->lock, flags);
  527. }
  528. static void atmel_release_tx_dma(struct uart_port *port)
  529. {
  530. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  531. struct dma_chan *chan = atmel_port->chan_tx;
  532. if (chan) {
  533. dmaengine_terminate_all(chan);
  534. dma_release_channel(chan);
  535. dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
  536. DMA_MEM_TO_DEV);
  537. }
  538. atmel_port->desc_tx = NULL;
  539. atmel_port->chan_tx = NULL;
  540. atmel_port->cookie_tx = -EINVAL;
  541. }
  542. /*
  543. * Called from tasklet with TXRDY interrupt is disabled.
  544. */
  545. static void atmel_tx_dma(struct uart_port *port)
  546. {
  547. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  548. struct circ_buf *xmit = &port->state->xmit;
  549. struct dma_chan *chan = atmel_port->chan_tx;
  550. struct dma_async_tx_descriptor *desc;
  551. struct scatterlist *sg = &atmel_port->sg_tx;
  552. /* Make sure we have an idle channel */
  553. if (atmel_port->desc_tx != NULL)
  554. return;
  555. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  556. /*
  557. * DMA is idle now.
  558. * Port xmit buffer is already mapped,
  559. * and it is one page... Just adjust
  560. * offsets and lengths. Since it is a circular buffer,
  561. * we have to transmit till the end, and then the rest.
  562. * Take the port lock to get a
  563. * consistent xmit buffer state.
  564. */
  565. sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
  566. sg_dma_address(sg) = (sg_dma_address(sg) &
  567. ~(UART_XMIT_SIZE - 1))
  568. + sg->offset;
  569. sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
  570. xmit->tail,
  571. UART_XMIT_SIZE);
  572. BUG_ON(!sg_dma_len(sg));
  573. desc = dmaengine_prep_slave_sg(chan,
  574. sg,
  575. 1,
  576. DMA_MEM_TO_DEV,
  577. DMA_PREP_INTERRUPT |
  578. DMA_CTRL_ACK);
  579. if (!desc) {
  580. dev_err(port->dev, "Failed to send via dma!\n");
  581. return;
  582. }
  583. dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
  584. atmel_port->desc_tx = desc;
  585. desc->callback = atmel_complete_tx_dma;
  586. desc->callback_param = atmel_port;
  587. atmel_port->cookie_tx = dmaengine_submit(desc);
  588. } else {
  589. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  590. /* DMA done, stop TX, start RX for RS485 */
  591. atmel_start_rx(port);
  592. }
  593. }
  594. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  595. uart_write_wakeup(port);
  596. }
  597. static int atmel_prepare_tx_dma(struct uart_port *port)
  598. {
  599. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  600. dma_cap_mask_t mask;
  601. struct dma_slave_config config;
  602. int ret, nent;
  603. dma_cap_zero(mask);
  604. dma_cap_set(DMA_SLAVE, mask);
  605. atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
  606. if (atmel_port->chan_tx == NULL)
  607. goto chan_err;
  608. dev_info(port->dev, "using %s for tx DMA transfers\n",
  609. dma_chan_name(atmel_port->chan_tx));
  610. spin_lock_init(&atmel_port->lock_tx);
  611. sg_init_table(&atmel_port->sg_tx, 1);
  612. /* UART circular tx buffer is an aligned page. */
  613. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  614. sg_set_page(&atmel_port->sg_tx,
  615. virt_to_page(port->state->xmit.buf),
  616. UART_XMIT_SIZE,
  617. (int)port->state->xmit.buf & ~PAGE_MASK);
  618. nent = dma_map_sg(port->dev,
  619. &atmel_port->sg_tx,
  620. 1,
  621. DMA_MEM_TO_DEV);
  622. if (!nent) {
  623. dev_dbg(port->dev, "need to release resource of dma\n");
  624. goto chan_err;
  625. } else {
  626. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  627. sg_dma_len(&atmel_port->sg_tx),
  628. port->state->xmit.buf,
  629. sg_dma_address(&atmel_port->sg_tx));
  630. }
  631. /* Configure the slave DMA */
  632. memset(&config, 0, sizeof(config));
  633. config.direction = DMA_MEM_TO_DEV;
  634. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  635. config.dst_addr = port->mapbase + ATMEL_US_THR;
  636. ret = dmaengine_device_control(atmel_port->chan_tx,
  637. DMA_SLAVE_CONFIG,
  638. (unsigned long)&config);
  639. if (ret) {
  640. dev_err(port->dev, "DMA tx slave configuration failed\n");
  641. goto chan_err;
  642. }
  643. return 0;
  644. chan_err:
  645. dev_err(port->dev, "TX channel not available, switch to pio\n");
  646. atmel_port->use_dma_tx = 0;
  647. if (atmel_port->chan_tx)
  648. atmel_release_tx_dma(port);
  649. return -EINVAL;
  650. }
  651. static void atmel_flip_buffer_rx_dma(struct uart_port *port,
  652. char *buf, size_t count)
  653. {
  654. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  655. struct tty_port *tport = &port->state->port;
  656. dma_sync_sg_for_cpu(port->dev,
  657. &atmel_port->sg_rx,
  658. 1,
  659. DMA_DEV_TO_MEM);
  660. tty_insert_flip_string(tport, buf, count);
  661. dma_sync_sg_for_device(port->dev,
  662. &atmel_port->sg_rx,
  663. 1,
  664. DMA_DEV_TO_MEM);
  665. /*
  666. * Drop the lock here since it might end up calling
  667. * uart_start(), which takes the lock.
  668. */
  669. spin_unlock(&port->lock);
  670. tty_flip_buffer_push(tport);
  671. spin_lock(&port->lock);
  672. }
  673. static void atmel_complete_rx_dma(void *arg)
  674. {
  675. struct uart_port *port = arg;
  676. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  677. tasklet_schedule(&atmel_port->tasklet);
  678. }
  679. static void atmel_release_rx_dma(struct uart_port *port)
  680. {
  681. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  682. struct dma_chan *chan = atmel_port->chan_rx;
  683. if (chan) {
  684. dmaengine_terminate_all(chan);
  685. dma_release_channel(chan);
  686. dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
  687. DMA_DEV_TO_MEM);
  688. }
  689. atmel_port->desc_rx = NULL;
  690. atmel_port->chan_rx = NULL;
  691. atmel_port->cookie_rx = -EINVAL;
  692. }
  693. static void atmel_rx_from_dma(struct uart_port *port)
  694. {
  695. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  696. struct circ_buf *ring = &atmel_port->rx_ring;
  697. struct dma_chan *chan = atmel_port->chan_rx;
  698. struct dma_tx_state state;
  699. enum dma_status dmastat;
  700. size_t pending, count;
  701. /* Reset the UART timeout early so that we don't miss one */
  702. UART_PUT_CR(port, ATMEL_US_STTTO);
  703. dmastat = dmaengine_tx_status(chan,
  704. atmel_port->cookie_rx,
  705. &state);
  706. /* Restart a new tasklet if DMA status is error */
  707. if (dmastat == DMA_ERROR) {
  708. dev_dbg(port->dev, "Get residue error, restart tasklet\n");
  709. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  710. tasklet_schedule(&atmel_port->tasklet);
  711. return;
  712. }
  713. /* current transfer size should no larger than dma buffer */
  714. pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
  715. BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
  716. /*
  717. * This will take the chars we have so far,
  718. * ring->head will record the transfer size, only new bytes come
  719. * will insert into the framework.
  720. */
  721. if (pending > ring->head) {
  722. count = pending - ring->head;
  723. atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
  724. ring->head += count;
  725. if (ring->head == sg_dma_len(&atmel_port->sg_rx))
  726. ring->head = 0;
  727. port->icount.rx += count;
  728. }
  729. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  730. }
  731. static int atmel_prepare_rx_dma(struct uart_port *port)
  732. {
  733. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  734. struct dma_async_tx_descriptor *desc;
  735. dma_cap_mask_t mask;
  736. struct dma_slave_config config;
  737. struct circ_buf *ring;
  738. int ret, nent;
  739. ring = &atmel_port->rx_ring;
  740. dma_cap_zero(mask);
  741. dma_cap_set(DMA_CYCLIC, mask);
  742. atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
  743. if (atmel_port->chan_rx == NULL)
  744. goto chan_err;
  745. dev_info(port->dev, "using %s for rx DMA transfers\n",
  746. dma_chan_name(atmel_port->chan_rx));
  747. spin_lock_init(&atmel_port->lock_rx);
  748. sg_init_table(&atmel_port->sg_rx, 1);
  749. /* UART circular rx buffer is an aligned page. */
  750. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  751. sg_set_page(&atmel_port->sg_rx,
  752. virt_to_page(ring->buf),
  753. ATMEL_SERIAL_RINGSIZE,
  754. (int)ring->buf & ~PAGE_MASK);
  755. nent = dma_map_sg(port->dev,
  756. &atmel_port->sg_rx,
  757. 1,
  758. DMA_DEV_TO_MEM);
  759. if (!nent) {
  760. dev_dbg(port->dev, "need to release resource of dma\n");
  761. goto chan_err;
  762. } else {
  763. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  764. sg_dma_len(&atmel_port->sg_rx),
  765. ring->buf,
  766. sg_dma_address(&atmel_port->sg_rx));
  767. }
  768. /* Configure the slave DMA */
  769. memset(&config, 0, sizeof(config));
  770. config.direction = DMA_DEV_TO_MEM;
  771. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  772. config.src_addr = port->mapbase + ATMEL_US_RHR;
  773. ret = dmaengine_device_control(atmel_port->chan_rx,
  774. DMA_SLAVE_CONFIG,
  775. (unsigned long)&config);
  776. if (ret) {
  777. dev_err(port->dev, "DMA rx slave configuration failed\n");
  778. goto chan_err;
  779. }
  780. /*
  781. * Prepare a cyclic dma transfer, assign 2 descriptors,
  782. * each one is half ring buffer size
  783. */
  784. desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
  785. sg_dma_address(&atmel_port->sg_rx),
  786. sg_dma_len(&atmel_port->sg_rx),
  787. sg_dma_len(&atmel_port->sg_rx)/2,
  788. DMA_DEV_TO_MEM,
  789. DMA_PREP_INTERRUPT);
  790. desc->callback = atmel_complete_rx_dma;
  791. desc->callback_param = port;
  792. atmel_port->desc_rx = desc;
  793. atmel_port->cookie_rx = dmaengine_submit(desc);
  794. return 0;
  795. chan_err:
  796. dev_err(port->dev, "RX channel not available, switch to pio\n");
  797. atmel_port->use_dma_rx = 0;
  798. if (atmel_port->chan_rx)
  799. atmel_release_rx_dma(port);
  800. return -EINVAL;
  801. }
  802. /*
  803. * receive interrupt handler.
  804. */
  805. static void
  806. atmel_handle_receive(struct uart_port *port, unsigned int pending)
  807. {
  808. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  809. if (atmel_use_pdc_rx(port)) {
  810. /*
  811. * PDC receive. Just schedule the tasklet and let it
  812. * figure out the details.
  813. *
  814. * TODO: We're not handling error flags correctly at
  815. * the moment.
  816. */
  817. if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
  818. UART_PUT_IDR(port, (ATMEL_US_ENDRX
  819. | ATMEL_US_TIMEOUT));
  820. tasklet_schedule(&atmel_port->tasklet);
  821. }
  822. if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
  823. ATMEL_US_FRAME | ATMEL_US_PARE))
  824. atmel_pdc_rxerr(port, pending);
  825. }
  826. if (atmel_use_dma_rx(port)) {
  827. if (pending & ATMEL_US_TIMEOUT) {
  828. UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
  829. tasklet_schedule(&atmel_port->tasklet);
  830. }
  831. }
  832. /* Interrupt receive */
  833. if (pending & ATMEL_US_RXRDY)
  834. atmel_rx_chars(port);
  835. else if (pending & ATMEL_US_RXBRK) {
  836. /*
  837. * End of break detected. If it came along with a
  838. * character, atmel_rx_chars will handle it.
  839. */
  840. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  841. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  842. atmel_port->break_active = 0;
  843. }
  844. }
  845. /*
  846. * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
  847. */
  848. static void
  849. atmel_handle_transmit(struct uart_port *port, unsigned int pending)
  850. {
  851. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  852. if (pending & atmel_port->tx_done_mask) {
  853. /* Either PDC or interrupt transmission */
  854. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  855. tasklet_schedule(&atmel_port->tasklet);
  856. }
  857. }
  858. /*
  859. * status flags interrupt handler.
  860. */
  861. static void
  862. atmel_handle_status(struct uart_port *port, unsigned int pending,
  863. unsigned int status)
  864. {
  865. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  866. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
  867. | ATMEL_US_CTSIC)) {
  868. atmel_port->irq_status = status;
  869. tasklet_schedule(&atmel_port->tasklet);
  870. }
  871. }
  872. /*
  873. * Interrupt handler
  874. */
  875. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  876. {
  877. struct uart_port *port = dev_id;
  878. unsigned int status, pending, pass_counter = 0;
  879. do {
  880. status = UART_GET_CSR(port);
  881. pending = status & UART_GET_IMR(port);
  882. if (!pending)
  883. break;
  884. atmel_handle_receive(port, pending);
  885. atmel_handle_status(port, pending, status);
  886. atmel_handle_transmit(port, pending);
  887. } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
  888. return pass_counter ? IRQ_HANDLED : IRQ_NONE;
  889. }
  890. static void atmel_release_tx_pdc(struct uart_port *port)
  891. {
  892. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  893. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  894. dma_unmap_single(port->dev,
  895. pdc->dma_addr,
  896. pdc->dma_size,
  897. DMA_TO_DEVICE);
  898. }
  899. /*
  900. * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
  901. */
  902. static void atmel_tx_pdc(struct uart_port *port)
  903. {
  904. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  905. struct circ_buf *xmit = &port->state->xmit;
  906. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  907. int count;
  908. /* nothing left to transmit? */
  909. if (UART_GET_TCR(port))
  910. return;
  911. xmit->tail += pdc->ofs;
  912. xmit->tail &= UART_XMIT_SIZE - 1;
  913. port->icount.tx += pdc->ofs;
  914. pdc->ofs = 0;
  915. /* more to transmit - setup next transfer */
  916. /* disable PDC transmit */
  917. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  918. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  919. dma_sync_single_for_device(port->dev,
  920. pdc->dma_addr,
  921. pdc->dma_size,
  922. DMA_TO_DEVICE);
  923. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  924. pdc->ofs = count;
  925. UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
  926. UART_PUT_TCR(port, count);
  927. /* re-enable PDC transmit */
  928. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  929. /* Enable interrupts */
  930. UART_PUT_IER(port, atmel_port->tx_done_mask);
  931. } else {
  932. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  933. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
  934. /* DMA done, stop TX, start RX for RS485 */
  935. atmel_start_rx(port);
  936. }
  937. }
  938. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  939. uart_write_wakeup(port);
  940. }
  941. static int atmel_prepare_tx_pdc(struct uart_port *port)
  942. {
  943. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  944. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  945. struct circ_buf *xmit = &port->state->xmit;
  946. pdc->buf = xmit->buf;
  947. pdc->dma_addr = dma_map_single(port->dev,
  948. pdc->buf,
  949. UART_XMIT_SIZE,
  950. DMA_TO_DEVICE);
  951. pdc->dma_size = UART_XMIT_SIZE;
  952. pdc->ofs = 0;
  953. return 0;
  954. }
  955. static void atmel_rx_from_ring(struct uart_port *port)
  956. {
  957. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  958. struct circ_buf *ring = &atmel_port->rx_ring;
  959. unsigned int flg;
  960. unsigned int status;
  961. while (ring->head != ring->tail) {
  962. struct atmel_uart_char c;
  963. /* Make sure c is loaded after head. */
  964. smp_rmb();
  965. c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
  966. ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  967. port->icount.rx++;
  968. status = c.status;
  969. flg = TTY_NORMAL;
  970. /*
  971. * note that the error handling code is
  972. * out of the main execution path
  973. */
  974. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  975. | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  976. if (status & ATMEL_US_RXBRK) {
  977. /* ignore side-effect */
  978. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  979. port->icount.brk++;
  980. if (uart_handle_break(port))
  981. continue;
  982. }
  983. if (status & ATMEL_US_PARE)
  984. port->icount.parity++;
  985. if (status & ATMEL_US_FRAME)
  986. port->icount.frame++;
  987. if (status & ATMEL_US_OVRE)
  988. port->icount.overrun++;
  989. status &= port->read_status_mask;
  990. if (status & ATMEL_US_RXBRK)
  991. flg = TTY_BREAK;
  992. else if (status & ATMEL_US_PARE)
  993. flg = TTY_PARITY;
  994. else if (status & ATMEL_US_FRAME)
  995. flg = TTY_FRAME;
  996. }
  997. if (uart_handle_sysrq_char(port, c.ch))
  998. continue;
  999. uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
  1000. }
  1001. /*
  1002. * Drop the lock here since it might end up calling
  1003. * uart_start(), which takes the lock.
  1004. */
  1005. spin_unlock(&port->lock);
  1006. tty_flip_buffer_push(&port->state->port);
  1007. spin_lock(&port->lock);
  1008. }
  1009. static void atmel_release_rx_pdc(struct uart_port *port)
  1010. {
  1011. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1012. int i;
  1013. for (i = 0; i < 2; i++) {
  1014. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  1015. dma_unmap_single(port->dev,
  1016. pdc->dma_addr,
  1017. pdc->dma_size,
  1018. DMA_FROM_DEVICE);
  1019. kfree(pdc->buf);
  1020. }
  1021. }
  1022. static void atmel_rx_from_pdc(struct uart_port *port)
  1023. {
  1024. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1025. struct tty_port *tport = &port->state->port;
  1026. struct atmel_dma_buffer *pdc;
  1027. int rx_idx = atmel_port->pdc_rx_idx;
  1028. unsigned int head;
  1029. unsigned int tail;
  1030. unsigned int count;
  1031. do {
  1032. /* Reset the UART timeout early so that we don't miss one */
  1033. UART_PUT_CR(port, ATMEL_US_STTTO);
  1034. pdc = &atmel_port->pdc_rx[rx_idx];
  1035. head = UART_GET_RPR(port) - pdc->dma_addr;
  1036. tail = pdc->ofs;
  1037. /* If the PDC has switched buffers, RPR won't contain
  1038. * any address within the current buffer. Since head
  1039. * is unsigned, we just need a one-way comparison to
  1040. * find out.
  1041. *
  1042. * In this case, we just need to consume the entire
  1043. * buffer and resubmit it for DMA. This will clear the
  1044. * ENDRX bit as well, so that we can safely re-enable
  1045. * all interrupts below.
  1046. */
  1047. head = min(head, pdc->dma_size);
  1048. if (likely(head != tail)) {
  1049. dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
  1050. pdc->dma_size, DMA_FROM_DEVICE);
  1051. /*
  1052. * head will only wrap around when we recycle
  1053. * the DMA buffer, and when that happens, we
  1054. * explicitly set tail to 0. So head will
  1055. * always be greater than tail.
  1056. */
  1057. count = head - tail;
  1058. tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
  1059. count);
  1060. dma_sync_single_for_device(port->dev, pdc->dma_addr,
  1061. pdc->dma_size, DMA_FROM_DEVICE);
  1062. port->icount.rx += count;
  1063. pdc->ofs = head;
  1064. }
  1065. /*
  1066. * If the current buffer is full, we need to check if
  1067. * the next one contains any additional data.
  1068. */
  1069. if (head >= pdc->dma_size) {
  1070. pdc->ofs = 0;
  1071. UART_PUT_RNPR(port, pdc->dma_addr);
  1072. UART_PUT_RNCR(port, pdc->dma_size);
  1073. rx_idx = !rx_idx;
  1074. atmel_port->pdc_rx_idx = rx_idx;
  1075. }
  1076. } while (head >= pdc->dma_size);
  1077. /*
  1078. * Drop the lock here since it might end up calling
  1079. * uart_start(), which takes the lock.
  1080. */
  1081. spin_unlock(&port->lock);
  1082. tty_flip_buffer_push(tport);
  1083. spin_lock(&port->lock);
  1084. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  1085. }
  1086. static int atmel_prepare_rx_pdc(struct uart_port *port)
  1087. {
  1088. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1089. int i;
  1090. for (i = 0; i < 2; i++) {
  1091. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  1092. pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
  1093. if (pdc->buf == NULL) {
  1094. if (i != 0) {
  1095. dma_unmap_single(port->dev,
  1096. atmel_port->pdc_rx[0].dma_addr,
  1097. PDC_BUFFER_SIZE,
  1098. DMA_FROM_DEVICE);
  1099. kfree(atmel_port->pdc_rx[0].buf);
  1100. }
  1101. atmel_port->use_pdc_rx = 0;
  1102. return -ENOMEM;
  1103. }
  1104. pdc->dma_addr = dma_map_single(port->dev,
  1105. pdc->buf,
  1106. PDC_BUFFER_SIZE,
  1107. DMA_FROM_DEVICE);
  1108. pdc->dma_size = PDC_BUFFER_SIZE;
  1109. pdc->ofs = 0;
  1110. }
  1111. atmel_port->pdc_rx_idx = 0;
  1112. UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
  1113. UART_PUT_RCR(port, PDC_BUFFER_SIZE);
  1114. UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
  1115. UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
  1116. return 0;
  1117. }
  1118. /*
  1119. * tasklet handling tty stuff outside the interrupt handler.
  1120. */
  1121. static void atmel_tasklet_func(unsigned long data)
  1122. {
  1123. struct uart_port *port = (struct uart_port *)data;
  1124. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1125. unsigned int status;
  1126. unsigned int status_change;
  1127. /* The interrupt handler does not take the lock */
  1128. spin_lock(&port->lock);
  1129. atmel_port->schedule_tx(port);
  1130. status = atmel_port->irq_status;
  1131. status_change = status ^ atmel_port->irq_status_prev;
  1132. if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
  1133. | ATMEL_US_DCD | ATMEL_US_CTS)) {
  1134. /* TODO: All reads to CSR will clear these interrupts! */
  1135. if (status_change & ATMEL_US_RI)
  1136. port->icount.rng++;
  1137. if (status_change & ATMEL_US_DSR)
  1138. port->icount.dsr++;
  1139. if (status_change & ATMEL_US_DCD)
  1140. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  1141. if (status_change & ATMEL_US_CTS)
  1142. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  1143. wake_up_interruptible(&port->state->port.delta_msr_wait);
  1144. atmel_port->irq_status_prev = status;
  1145. }
  1146. atmel_port->schedule_rx(port);
  1147. spin_unlock(&port->lock);
  1148. }
  1149. static int atmel_init_property(struct atmel_uart_port *atmel_port,
  1150. struct platform_device *pdev)
  1151. {
  1152. struct device_node *np = pdev->dev.of_node;
  1153. struct atmel_uart_data *pdata = pdev->dev.platform_data;
  1154. if (np) {
  1155. /* DMA/PDC usage specification */
  1156. if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
  1157. if (of_get_property(np, "dmas", NULL)) {
  1158. atmel_port->use_dma_rx = true;
  1159. atmel_port->use_pdc_rx = false;
  1160. } else {
  1161. atmel_port->use_dma_rx = false;
  1162. atmel_port->use_pdc_rx = true;
  1163. }
  1164. } else {
  1165. atmel_port->use_dma_rx = false;
  1166. atmel_port->use_pdc_rx = false;
  1167. }
  1168. if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
  1169. if (of_get_property(np, "dmas", NULL)) {
  1170. atmel_port->use_dma_tx = true;
  1171. atmel_port->use_pdc_tx = false;
  1172. } else {
  1173. atmel_port->use_dma_tx = false;
  1174. atmel_port->use_pdc_tx = true;
  1175. }
  1176. } else {
  1177. atmel_port->use_dma_tx = false;
  1178. atmel_port->use_pdc_tx = false;
  1179. }
  1180. } else {
  1181. atmel_port->use_pdc_rx = pdata->use_dma_rx;
  1182. atmel_port->use_pdc_tx = pdata->use_dma_tx;
  1183. atmel_port->use_dma_rx = false;
  1184. atmel_port->use_dma_tx = false;
  1185. }
  1186. return 0;
  1187. }
  1188. static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
  1189. struct platform_device *pdev)
  1190. {
  1191. struct device_node *np = pdev->dev.of_node;
  1192. struct atmel_uart_data *pdata = pdev->dev.platform_data;
  1193. if (np) {
  1194. u32 rs485_delay[2];
  1195. /* rs485 properties */
  1196. if (of_property_read_u32_array(np, "rs485-rts-delay",
  1197. rs485_delay, 2) == 0) {
  1198. struct serial_rs485 *rs485conf = &atmel_port->rs485;
  1199. rs485conf->delay_rts_before_send = rs485_delay[0];
  1200. rs485conf->delay_rts_after_send = rs485_delay[1];
  1201. rs485conf->flags = 0;
  1202. if (of_get_property(np, "rs485-rx-during-tx", NULL))
  1203. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1204. if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
  1205. NULL))
  1206. rs485conf->flags |= SER_RS485_ENABLED;
  1207. }
  1208. } else {
  1209. atmel_port->rs485 = pdata->rs485;
  1210. }
  1211. }
  1212. static void atmel_set_ops(struct uart_port *port)
  1213. {
  1214. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1215. if (atmel_use_dma_rx(port)) {
  1216. atmel_port->prepare_rx = &atmel_prepare_rx_dma;
  1217. atmel_port->schedule_rx = &atmel_rx_from_dma;
  1218. atmel_port->release_rx = &atmel_release_rx_dma;
  1219. } else if (atmel_use_pdc_rx(port)) {
  1220. atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
  1221. atmel_port->schedule_rx = &atmel_rx_from_pdc;
  1222. atmel_port->release_rx = &atmel_release_rx_pdc;
  1223. } else {
  1224. atmel_port->prepare_rx = NULL;
  1225. atmel_port->schedule_rx = &atmel_rx_from_ring;
  1226. atmel_port->release_rx = NULL;
  1227. }
  1228. if (atmel_use_dma_tx(port)) {
  1229. atmel_port->prepare_tx = &atmel_prepare_tx_dma;
  1230. atmel_port->schedule_tx = &atmel_tx_dma;
  1231. atmel_port->release_tx = &atmel_release_tx_dma;
  1232. } else if (atmel_use_pdc_tx(port)) {
  1233. atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
  1234. atmel_port->schedule_tx = &atmel_tx_pdc;
  1235. atmel_port->release_tx = &atmel_release_tx_pdc;
  1236. } else {
  1237. atmel_port->prepare_tx = NULL;
  1238. atmel_port->schedule_tx = &atmel_tx_chars;
  1239. atmel_port->release_tx = NULL;
  1240. }
  1241. }
  1242. /*
  1243. * Get ip name usart or uart
  1244. */
  1245. static int atmel_get_ip_name(struct uart_port *port)
  1246. {
  1247. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1248. int name = UART_GET_IP_NAME(port);
  1249. int usart, uart;
  1250. /* usart and uart ascii */
  1251. usart = 0x55534152;
  1252. uart = 0x44424755;
  1253. atmel_port->is_usart = false;
  1254. if (name == usart) {
  1255. dev_dbg(port->dev, "This is usart\n");
  1256. atmel_port->is_usart = true;
  1257. } else if (name == uart) {
  1258. dev_dbg(port->dev, "This is uart\n");
  1259. atmel_port->is_usart = false;
  1260. } else {
  1261. dev_err(port->dev, "Not supported ip name, set to uart\n");
  1262. return -EINVAL;
  1263. }
  1264. return 0;
  1265. }
  1266. /*
  1267. * Perform initialization and enable port for reception
  1268. */
  1269. static int atmel_startup(struct uart_port *port)
  1270. {
  1271. struct platform_device *pdev = to_platform_device(port->dev);
  1272. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1273. struct tty_struct *tty = port->state->port.tty;
  1274. int retval;
  1275. /*
  1276. * Ensure that no interrupts are enabled otherwise when
  1277. * request_irq() is called we could get stuck trying to
  1278. * handle an unexpected interrupt
  1279. */
  1280. UART_PUT_IDR(port, -1);
  1281. /*
  1282. * Allocate the IRQ
  1283. */
  1284. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
  1285. tty ? tty->name : "atmel_serial", port);
  1286. if (retval) {
  1287. printk("atmel_serial: atmel_startup - Can't get irq\n");
  1288. return retval;
  1289. }
  1290. /*
  1291. * Initialize DMA (if necessary)
  1292. */
  1293. atmel_init_property(atmel_port, pdev);
  1294. if (atmel_port->prepare_rx) {
  1295. retval = atmel_port->prepare_rx(port);
  1296. if (retval < 0)
  1297. atmel_set_ops(port);
  1298. }
  1299. if (atmel_port->prepare_tx) {
  1300. retval = atmel_port->prepare_tx(port);
  1301. if (retval < 0)
  1302. atmel_set_ops(port);
  1303. }
  1304. /*
  1305. * If there is a specific "open" function (to register
  1306. * control line interrupts)
  1307. */
  1308. if (atmel_open_hook) {
  1309. retval = atmel_open_hook(port);
  1310. if (retval) {
  1311. free_irq(port->irq, port);
  1312. return retval;
  1313. }
  1314. }
  1315. /* Save current CSR for comparison in atmel_tasklet_func() */
  1316. atmel_port->irq_status_prev = UART_GET_CSR(port);
  1317. atmel_port->irq_status = atmel_port->irq_status_prev;
  1318. /*
  1319. * Finally, enable the serial port
  1320. */
  1321. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1322. /* enable xmit & rcvr */
  1323. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1324. if (atmel_use_pdc_rx(port)) {
  1325. /* set UART timeout */
  1326. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  1327. UART_PUT_CR(port, ATMEL_US_STTTO);
  1328. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  1329. /* enable PDC controller */
  1330. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  1331. } else if (atmel_use_dma_rx(port)) {
  1332. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  1333. UART_PUT_CR(port, ATMEL_US_STTTO);
  1334. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  1335. } else {
  1336. /* enable receive only */
  1337. UART_PUT_IER(port, ATMEL_US_RXRDY);
  1338. }
  1339. return 0;
  1340. }
  1341. /*
  1342. * Disable the port
  1343. */
  1344. static void atmel_shutdown(struct uart_port *port)
  1345. {
  1346. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1347. /*
  1348. * Ensure everything is stopped.
  1349. */
  1350. atmel_stop_rx(port);
  1351. atmel_stop_tx(port);
  1352. /*
  1353. * Shut-down the DMA.
  1354. */
  1355. if (atmel_port->release_rx)
  1356. atmel_port->release_rx(port);
  1357. if (atmel_port->release_tx)
  1358. atmel_port->release_tx(port);
  1359. /*
  1360. * Disable all interrupts, port and break condition.
  1361. */
  1362. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  1363. UART_PUT_IDR(port, -1);
  1364. /*
  1365. * Free the interrupt
  1366. */
  1367. free_irq(port->irq, port);
  1368. /*
  1369. * If there is a specific "close" function (to unregister
  1370. * control line interrupts)
  1371. */
  1372. if (atmel_close_hook)
  1373. atmel_close_hook(port);
  1374. }
  1375. /*
  1376. * Flush any TX data submitted for DMA. Called when the TX circular
  1377. * buffer is reset.
  1378. */
  1379. static void atmel_flush_buffer(struct uart_port *port)
  1380. {
  1381. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1382. if (atmel_use_pdc_tx(port)) {
  1383. UART_PUT_TCR(port, 0);
  1384. atmel_port->pdc_tx.ofs = 0;
  1385. }
  1386. }
  1387. /*
  1388. * Power / Clock management.
  1389. */
  1390. static void atmel_serial_pm(struct uart_port *port, unsigned int state,
  1391. unsigned int oldstate)
  1392. {
  1393. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1394. switch (state) {
  1395. case 0:
  1396. /*
  1397. * Enable the peripheral clock for this serial port.
  1398. * This is called on uart_open() or a resume event.
  1399. */
  1400. clk_prepare_enable(atmel_port->clk);
  1401. /* re-enable interrupts if we disabled some on suspend */
  1402. UART_PUT_IER(port, atmel_port->backup_imr);
  1403. break;
  1404. case 3:
  1405. /* Back up the interrupt mask and disable all interrupts */
  1406. atmel_port->backup_imr = UART_GET_IMR(port);
  1407. UART_PUT_IDR(port, -1);
  1408. /*
  1409. * Disable the peripheral clock for this serial port.
  1410. * This is called on uart_close() or a suspend event.
  1411. */
  1412. clk_disable_unprepare(atmel_port->clk);
  1413. break;
  1414. default:
  1415. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  1416. }
  1417. }
  1418. /*
  1419. * Change the port parameters
  1420. */
  1421. static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  1422. struct ktermios *old)
  1423. {
  1424. unsigned long flags;
  1425. unsigned int mode, imr, quot, baud;
  1426. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1427. /* Get current mode register */
  1428. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
  1429. | ATMEL_US_NBSTOP | ATMEL_US_PAR
  1430. | ATMEL_US_USMODE);
  1431. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1432. quot = uart_get_divisor(port, baud);
  1433. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  1434. quot /= 8;
  1435. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  1436. }
  1437. /* byte size */
  1438. switch (termios->c_cflag & CSIZE) {
  1439. case CS5:
  1440. mode |= ATMEL_US_CHRL_5;
  1441. break;
  1442. case CS6:
  1443. mode |= ATMEL_US_CHRL_6;
  1444. break;
  1445. case CS7:
  1446. mode |= ATMEL_US_CHRL_7;
  1447. break;
  1448. default:
  1449. mode |= ATMEL_US_CHRL_8;
  1450. break;
  1451. }
  1452. /* stop bits */
  1453. if (termios->c_cflag & CSTOPB)
  1454. mode |= ATMEL_US_NBSTOP_2;
  1455. /* parity */
  1456. if (termios->c_cflag & PARENB) {
  1457. /* Mark or Space parity */
  1458. if (termios->c_cflag & CMSPAR) {
  1459. if (termios->c_cflag & PARODD)
  1460. mode |= ATMEL_US_PAR_MARK;
  1461. else
  1462. mode |= ATMEL_US_PAR_SPACE;
  1463. } else if (termios->c_cflag & PARODD)
  1464. mode |= ATMEL_US_PAR_ODD;
  1465. else
  1466. mode |= ATMEL_US_PAR_EVEN;
  1467. } else
  1468. mode |= ATMEL_US_PAR_NONE;
  1469. /* hardware handshake (RTS/CTS) */
  1470. if (termios->c_cflag & CRTSCTS)
  1471. mode |= ATMEL_US_USMODE_HWHS;
  1472. else
  1473. mode |= ATMEL_US_USMODE_NORMAL;
  1474. spin_lock_irqsave(&port->lock, flags);
  1475. port->read_status_mask = ATMEL_US_OVRE;
  1476. if (termios->c_iflag & INPCK)
  1477. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  1478. if (termios->c_iflag & (BRKINT | PARMRK))
  1479. port->read_status_mask |= ATMEL_US_RXBRK;
  1480. if (atmel_use_pdc_rx(port))
  1481. /* need to enable error interrupts */
  1482. UART_PUT_IER(port, port->read_status_mask);
  1483. /*
  1484. * Characters to ignore
  1485. */
  1486. port->ignore_status_mask = 0;
  1487. if (termios->c_iflag & IGNPAR)
  1488. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  1489. if (termios->c_iflag & IGNBRK) {
  1490. port->ignore_status_mask |= ATMEL_US_RXBRK;
  1491. /*
  1492. * If we're ignoring parity and break indicators,
  1493. * ignore overruns too (for real raw support).
  1494. */
  1495. if (termios->c_iflag & IGNPAR)
  1496. port->ignore_status_mask |= ATMEL_US_OVRE;
  1497. }
  1498. /* TODO: Ignore all characters if CREAD is set.*/
  1499. /* update the per-port timeout */
  1500. uart_update_timeout(port, termios->c_cflag, baud);
  1501. /*
  1502. * save/disable interrupts. The tty layer will ensure that the
  1503. * transmitter is empty if requested by the caller, so there's
  1504. * no need to wait for it here.
  1505. */
  1506. imr = UART_GET_IMR(port);
  1507. UART_PUT_IDR(port, -1);
  1508. /* disable receiver and transmitter */
  1509. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  1510. /* Resetting serial mode to RS232 (0x0) */
  1511. mode &= ~ATMEL_US_USMODE;
  1512. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  1513. dev_dbg(port->dev, "Setting UART to RS485\n");
  1514. if ((atmel_port->rs485.delay_rts_after_send) > 0)
  1515. UART_PUT_TTGR(port,
  1516. atmel_port->rs485.delay_rts_after_send);
  1517. mode |= ATMEL_US_USMODE_RS485;
  1518. } else {
  1519. dev_dbg(port->dev, "Setting UART to RS232\n");
  1520. }
  1521. /* set the parity, stop bits and data size */
  1522. UART_PUT_MR(port, mode);
  1523. /* set the baud rate */
  1524. UART_PUT_BRGR(port, quot);
  1525. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1526. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1527. /* restore interrupts */
  1528. UART_PUT_IER(port, imr);
  1529. /* CTS flow-control and modem-status interrupts */
  1530. if (UART_ENABLE_MS(port, termios->c_cflag))
  1531. port->ops->enable_ms(port);
  1532. spin_unlock_irqrestore(&port->lock, flags);
  1533. }
  1534. static void atmel_set_ldisc(struct uart_port *port, int new)
  1535. {
  1536. if (new == N_PPS) {
  1537. port->flags |= UPF_HARDPPS_CD;
  1538. atmel_enable_ms(port);
  1539. } else {
  1540. port->flags &= ~UPF_HARDPPS_CD;
  1541. }
  1542. }
  1543. /*
  1544. * Return string describing the specified port
  1545. */
  1546. static const char *atmel_type(struct uart_port *port)
  1547. {
  1548. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  1549. }
  1550. /*
  1551. * Release the memory region(s) being used by 'port'.
  1552. */
  1553. static void atmel_release_port(struct uart_port *port)
  1554. {
  1555. struct platform_device *pdev = to_platform_device(port->dev);
  1556. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1557. release_mem_region(port->mapbase, size);
  1558. if (port->flags & UPF_IOREMAP) {
  1559. iounmap(port->membase);
  1560. port->membase = NULL;
  1561. }
  1562. }
  1563. /*
  1564. * Request the memory region(s) being used by 'port'.
  1565. */
  1566. static int atmel_request_port(struct uart_port *port)
  1567. {
  1568. struct platform_device *pdev = to_platform_device(port->dev);
  1569. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1570. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  1571. return -EBUSY;
  1572. if (port->flags & UPF_IOREMAP) {
  1573. port->membase = ioremap(port->mapbase, size);
  1574. if (port->membase == NULL) {
  1575. release_mem_region(port->mapbase, size);
  1576. return -ENOMEM;
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. /*
  1582. * Configure/autoconfigure the port.
  1583. */
  1584. static void atmel_config_port(struct uart_port *port, int flags)
  1585. {
  1586. if (flags & UART_CONFIG_TYPE) {
  1587. port->type = PORT_ATMEL;
  1588. atmel_request_port(port);
  1589. }
  1590. }
  1591. /*
  1592. * Verify the new serial_struct (for TIOCSSERIAL).
  1593. */
  1594. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  1595. {
  1596. int ret = 0;
  1597. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  1598. ret = -EINVAL;
  1599. if (port->irq != ser->irq)
  1600. ret = -EINVAL;
  1601. if (ser->io_type != SERIAL_IO_MEM)
  1602. ret = -EINVAL;
  1603. if (port->uartclk / 16 != ser->baud_base)
  1604. ret = -EINVAL;
  1605. if ((void *)port->mapbase != ser->iomem_base)
  1606. ret = -EINVAL;
  1607. if (port->iobase != ser->port)
  1608. ret = -EINVAL;
  1609. if (ser->hub6 != 0)
  1610. ret = -EINVAL;
  1611. return ret;
  1612. }
  1613. #ifdef CONFIG_CONSOLE_POLL
  1614. static int atmel_poll_get_char(struct uart_port *port)
  1615. {
  1616. while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
  1617. cpu_relax();
  1618. return UART_GET_CHAR(port);
  1619. }
  1620. static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
  1621. {
  1622. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1623. cpu_relax();
  1624. UART_PUT_CHAR(port, ch);
  1625. }
  1626. #endif
  1627. static int
  1628. atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
  1629. {
  1630. struct serial_rs485 rs485conf;
  1631. switch (cmd) {
  1632. case TIOCSRS485:
  1633. if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
  1634. sizeof(rs485conf)))
  1635. return -EFAULT;
  1636. atmel_config_rs485(port, &rs485conf);
  1637. break;
  1638. case TIOCGRS485:
  1639. if (copy_to_user((struct serial_rs485 *) arg,
  1640. &(to_atmel_uart_port(port)->rs485),
  1641. sizeof(rs485conf)))
  1642. return -EFAULT;
  1643. break;
  1644. default:
  1645. return -ENOIOCTLCMD;
  1646. }
  1647. return 0;
  1648. }
  1649. static struct uart_ops atmel_pops = {
  1650. .tx_empty = atmel_tx_empty,
  1651. .set_mctrl = atmel_set_mctrl,
  1652. .get_mctrl = atmel_get_mctrl,
  1653. .stop_tx = atmel_stop_tx,
  1654. .start_tx = atmel_start_tx,
  1655. .stop_rx = atmel_stop_rx,
  1656. .enable_ms = atmel_enable_ms,
  1657. .break_ctl = atmel_break_ctl,
  1658. .startup = atmel_startup,
  1659. .shutdown = atmel_shutdown,
  1660. .flush_buffer = atmel_flush_buffer,
  1661. .set_termios = atmel_set_termios,
  1662. .set_ldisc = atmel_set_ldisc,
  1663. .type = atmel_type,
  1664. .release_port = atmel_release_port,
  1665. .request_port = atmel_request_port,
  1666. .config_port = atmel_config_port,
  1667. .verify_port = atmel_verify_port,
  1668. .pm = atmel_serial_pm,
  1669. .ioctl = atmel_ioctl,
  1670. #ifdef CONFIG_CONSOLE_POLL
  1671. .poll_get_char = atmel_poll_get_char,
  1672. .poll_put_char = atmel_poll_put_char,
  1673. #endif
  1674. };
  1675. /*
  1676. * Configure the port from the platform device resource info.
  1677. */
  1678. static int atmel_init_port(struct atmel_uart_port *atmel_port,
  1679. struct platform_device *pdev)
  1680. {
  1681. int ret;
  1682. struct uart_port *port = &atmel_port->uart;
  1683. struct atmel_uart_data *pdata = pdev->dev.platform_data;
  1684. if (!atmel_init_property(atmel_port, pdev))
  1685. atmel_set_ops(port);
  1686. atmel_init_rs485(atmel_port, pdev);
  1687. port->iotype = UPIO_MEM;
  1688. port->flags = UPF_BOOT_AUTOCONF;
  1689. port->ops = &atmel_pops;
  1690. port->fifosize = 1;
  1691. port->dev = &pdev->dev;
  1692. port->mapbase = pdev->resource[0].start;
  1693. port->irq = pdev->resource[1].start;
  1694. tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
  1695. (unsigned long)port);
  1696. memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
  1697. if (pdata && pdata->regs) {
  1698. /* Already mapped by setup code */
  1699. port->membase = pdata->regs;
  1700. } else {
  1701. port->flags |= UPF_IOREMAP;
  1702. port->membase = NULL;
  1703. }
  1704. /* for console, the clock could already be configured */
  1705. if (!atmel_port->clk) {
  1706. atmel_port->clk = clk_get(&pdev->dev, "usart");
  1707. if (IS_ERR(atmel_port->clk)) {
  1708. ret = PTR_ERR(atmel_port->clk);
  1709. atmel_port->clk = NULL;
  1710. return ret;
  1711. }
  1712. ret = clk_prepare_enable(atmel_port->clk);
  1713. if (ret) {
  1714. clk_put(atmel_port->clk);
  1715. atmel_port->clk = NULL;
  1716. return ret;
  1717. }
  1718. port->uartclk = clk_get_rate(atmel_port->clk);
  1719. clk_disable_unprepare(atmel_port->clk);
  1720. /* only enable clock when USART is in use */
  1721. }
  1722. /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
  1723. if (atmel_port->rs485.flags & SER_RS485_ENABLED)
  1724. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  1725. else if (atmel_use_pdc_tx(port)) {
  1726. port->fifosize = PDC_BUFFER_SIZE;
  1727. atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
  1728. } else {
  1729. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  1730. }
  1731. return 0;
  1732. }
  1733. struct platform_device *atmel_default_console_device; /* the serial console device */
  1734. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1735. static void atmel_console_putchar(struct uart_port *port, int ch)
  1736. {
  1737. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1738. cpu_relax();
  1739. UART_PUT_CHAR(port, ch);
  1740. }
  1741. /*
  1742. * Interrupts are disabled on entering
  1743. */
  1744. static void atmel_console_write(struct console *co, const char *s, u_int count)
  1745. {
  1746. struct uart_port *port = &atmel_ports[co->index].uart;
  1747. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1748. unsigned int status, imr;
  1749. unsigned int pdc_tx;
  1750. /*
  1751. * First, save IMR and then disable interrupts
  1752. */
  1753. imr = UART_GET_IMR(port);
  1754. UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
  1755. /* Store PDC transmit status and disable it */
  1756. pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
  1757. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  1758. uart_console_write(port, s, count, atmel_console_putchar);
  1759. /*
  1760. * Finally, wait for transmitter to become empty
  1761. * and restore IMR
  1762. */
  1763. do {
  1764. status = UART_GET_CSR(port);
  1765. } while (!(status & ATMEL_US_TXRDY));
  1766. /* Restore PDC transmit status */
  1767. if (pdc_tx)
  1768. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  1769. /* set interrupts back the way they were */
  1770. UART_PUT_IER(port, imr);
  1771. }
  1772. /*
  1773. * If the port was already initialised (eg, by a boot loader),
  1774. * try to determine the current setup.
  1775. */
  1776. static void __init atmel_console_get_options(struct uart_port *port, int *baud,
  1777. int *parity, int *bits)
  1778. {
  1779. unsigned int mr, quot;
  1780. /*
  1781. * If the baud rate generator isn't running, the port wasn't
  1782. * initialized by the boot loader.
  1783. */
  1784. quot = UART_GET_BRGR(port) & ATMEL_US_CD;
  1785. if (!quot)
  1786. return;
  1787. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  1788. if (mr == ATMEL_US_CHRL_8)
  1789. *bits = 8;
  1790. else
  1791. *bits = 7;
  1792. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  1793. if (mr == ATMEL_US_PAR_EVEN)
  1794. *parity = 'e';
  1795. else if (mr == ATMEL_US_PAR_ODD)
  1796. *parity = 'o';
  1797. /*
  1798. * The serial core only rounds down when matching this to a
  1799. * supported baud rate. Make sure we don't end up slightly
  1800. * lower than one of those, as it would make us fall through
  1801. * to a much lower baud rate than we really want.
  1802. */
  1803. *baud = port->uartclk / (16 * (quot - 1));
  1804. }
  1805. static int __init atmel_console_setup(struct console *co, char *options)
  1806. {
  1807. int ret;
  1808. struct uart_port *port = &atmel_ports[co->index].uart;
  1809. int baud = 115200;
  1810. int bits = 8;
  1811. int parity = 'n';
  1812. int flow = 'n';
  1813. if (port->membase == NULL) {
  1814. /* Port not initialized yet - delay setup */
  1815. return -ENODEV;
  1816. }
  1817. ret = clk_prepare_enable(atmel_ports[co->index].clk);
  1818. if (ret)
  1819. return ret;
  1820. UART_PUT_IDR(port, -1);
  1821. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1822. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1823. if (options)
  1824. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1825. else
  1826. atmel_console_get_options(port, &baud, &parity, &bits);
  1827. return uart_set_options(port, co, baud, parity, bits, flow);
  1828. }
  1829. static struct uart_driver atmel_uart;
  1830. static struct console atmel_console = {
  1831. .name = ATMEL_DEVICENAME,
  1832. .write = atmel_console_write,
  1833. .device = uart_console_device,
  1834. .setup = atmel_console_setup,
  1835. .flags = CON_PRINTBUFFER,
  1836. .index = -1,
  1837. .data = &atmel_uart,
  1838. };
  1839. #define ATMEL_CONSOLE_DEVICE (&atmel_console)
  1840. /*
  1841. * Early console initialization (before VM subsystem initialized).
  1842. */
  1843. static int __init atmel_console_init(void)
  1844. {
  1845. int ret;
  1846. if (atmel_default_console_device) {
  1847. struct atmel_uart_data *pdata =
  1848. atmel_default_console_device->dev.platform_data;
  1849. int id = pdata->num;
  1850. struct atmel_uart_port *port = &atmel_ports[id];
  1851. port->backup_imr = 0;
  1852. port->uart.line = id;
  1853. add_preferred_console(ATMEL_DEVICENAME, id, NULL);
  1854. ret = atmel_init_port(port, atmel_default_console_device);
  1855. if (ret)
  1856. return ret;
  1857. register_console(&atmel_console);
  1858. }
  1859. return 0;
  1860. }
  1861. console_initcall(atmel_console_init);
  1862. /*
  1863. * Late console initialization.
  1864. */
  1865. static int __init atmel_late_console_init(void)
  1866. {
  1867. if (atmel_default_console_device
  1868. && !(atmel_console.flags & CON_ENABLED))
  1869. register_console(&atmel_console);
  1870. return 0;
  1871. }
  1872. core_initcall(atmel_late_console_init);
  1873. static inline bool atmel_is_console_port(struct uart_port *port)
  1874. {
  1875. return port->cons && port->cons->index == port->line;
  1876. }
  1877. #else
  1878. #define ATMEL_CONSOLE_DEVICE NULL
  1879. static inline bool atmel_is_console_port(struct uart_port *port)
  1880. {
  1881. return false;
  1882. }
  1883. #endif
  1884. static struct uart_driver atmel_uart = {
  1885. .owner = THIS_MODULE,
  1886. .driver_name = "atmel_serial",
  1887. .dev_name = ATMEL_DEVICENAME,
  1888. .major = SERIAL_ATMEL_MAJOR,
  1889. .minor = MINOR_START,
  1890. .nr = ATMEL_MAX_UART,
  1891. .cons = ATMEL_CONSOLE_DEVICE,
  1892. };
  1893. #ifdef CONFIG_PM
  1894. static bool atmel_serial_clk_will_stop(void)
  1895. {
  1896. #ifdef CONFIG_ARCH_AT91
  1897. return at91_suspend_entering_slow_clock();
  1898. #else
  1899. return false;
  1900. #endif
  1901. }
  1902. static int atmel_serial_suspend(struct platform_device *pdev,
  1903. pm_message_t state)
  1904. {
  1905. struct uart_port *port = platform_get_drvdata(pdev);
  1906. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1907. if (atmel_is_console_port(port) && console_suspend_enabled) {
  1908. /* Drain the TX shifter */
  1909. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  1910. cpu_relax();
  1911. }
  1912. /* we can not wake up if we're running on slow clock */
  1913. atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
  1914. if (atmel_serial_clk_will_stop())
  1915. device_set_wakeup_enable(&pdev->dev, 0);
  1916. uart_suspend_port(&atmel_uart, port);
  1917. return 0;
  1918. }
  1919. static int atmel_serial_resume(struct platform_device *pdev)
  1920. {
  1921. struct uart_port *port = platform_get_drvdata(pdev);
  1922. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1923. uart_resume_port(&atmel_uart, port);
  1924. device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
  1925. return 0;
  1926. }
  1927. #else
  1928. #define atmel_serial_suspend NULL
  1929. #define atmel_serial_resume NULL
  1930. #endif
  1931. static int atmel_serial_probe(struct platform_device *pdev)
  1932. {
  1933. struct atmel_uart_port *port;
  1934. struct device_node *np = pdev->dev.of_node;
  1935. struct atmel_uart_data *pdata = pdev->dev.platform_data;
  1936. void *data;
  1937. int ret = -ENODEV;
  1938. BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
  1939. if (np)
  1940. ret = of_alias_get_id(np, "serial");
  1941. else
  1942. if (pdata)
  1943. ret = pdata->num;
  1944. if (ret < 0)
  1945. /* port id not found in platform data nor device-tree aliases:
  1946. * auto-enumerate it */
  1947. ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
  1948. if (ret >= ATMEL_MAX_UART) {
  1949. ret = -ENODEV;
  1950. goto err;
  1951. }
  1952. if (test_and_set_bit(ret, atmel_ports_in_use)) {
  1953. /* port already in use */
  1954. ret = -EBUSY;
  1955. goto err;
  1956. }
  1957. port = &atmel_ports[ret];
  1958. port->backup_imr = 0;
  1959. port->uart.line = ret;
  1960. ret = atmel_init_port(port, pdev);
  1961. if (ret)
  1962. goto err;
  1963. if (!atmel_use_pdc_rx(&port->uart)) {
  1964. ret = -ENOMEM;
  1965. data = kmalloc(sizeof(struct atmel_uart_char)
  1966. * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
  1967. if (!data)
  1968. goto err_alloc_ring;
  1969. port->rx_ring.buf = data;
  1970. }
  1971. ret = uart_add_one_port(&atmel_uart, &port->uart);
  1972. if (ret)
  1973. goto err_add_port;
  1974. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1975. if (atmel_is_console_port(&port->uart)
  1976. && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
  1977. /*
  1978. * The serial core enabled the clock for us, so undo
  1979. * the clk_prepare_enable() in atmel_console_setup()
  1980. */
  1981. clk_disable_unprepare(port->clk);
  1982. }
  1983. #endif
  1984. device_init_wakeup(&pdev->dev, 1);
  1985. platform_set_drvdata(pdev, port);
  1986. if (port->rs485.flags & SER_RS485_ENABLED) {
  1987. UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
  1988. UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
  1989. }
  1990. /*
  1991. * Get port name of usart or uart
  1992. */
  1993. ret = atmel_get_ip_name(&port->uart);
  1994. if (ret < 0)
  1995. goto err_add_port;
  1996. return 0;
  1997. err_add_port:
  1998. kfree(port->rx_ring.buf);
  1999. port->rx_ring.buf = NULL;
  2000. err_alloc_ring:
  2001. if (!atmel_is_console_port(&port->uart)) {
  2002. clk_put(port->clk);
  2003. port->clk = NULL;
  2004. }
  2005. err:
  2006. return ret;
  2007. }
  2008. static int atmel_serial_remove(struct platform_device *pdev)
  2009. {
  2010. struct uart_port *port = platform_get_drvdata(pdev);
  2011. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  2012. int ret = 0;
  2013. device_init_wakeup(&pdev->dev, 0);
  2014. ret = uart_remove_one_port(&atmel_uart, port);
  2015. tasklet_kill(&atmel_port->tasklet);
  2016. kfree(atmel_port->rx_ring.buf);
  2017. /* "port" is allocated statically, so we shouldn't free it */
  2018. clear_bit(port->line, atmel_ports_in_use);
  2019. clk_put(atmel_port->clk);
  2020. return ret;
  2021. }
  2022. static struct platform_driver atmel_serial_driver = {
  2023. .probe = atmel_serial_probe,
  2024. .remove = atmel_serial_remove,
  2025. .suspend = atmel_serial_suspend,
  2026. .resume = atmel_serial_resume,
  2027. .driver = {
  2028. .name = "atmel_usart",
  2029. .owner = THIS_MODULE,
  2030. .of_match_table = of_match_ptr(atmel_serial_dt_ids),
  2031. },
  2032. };
  2033. static int __init atmel_serial_init(void)
  2034. {
  2035. int ret;
  2036. ret = uart_register_driver(&atmel_uart);
  2037. if (ret)
  2038. return ret;
  2039. ret = platform_driver_register(&atmel_serial_driver);
  2040. if (ret)
  2041. uart_unregister_driver(&atmel_uart);
  2042. return ret;
  2043. }
  2044. static void __exit atmel_serial_exit(void)
  2045. {
  2046. platform_driver_unregister(&atmel_serial_driver);
  2047. uart_unregister_driver(&atmel_uart);
  2048. }
  2049. module_init(atmel_serial_init);
  2050. module_exit(atmel_serial_exit);
  2051. MODULE_AUTHOR("Rick Bronson");
  2052. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  2053. MODULE_LICENSE("GPL");
  2054. MODULE_ALIAS("platform:atmel_usart");