atmel_serial.c 62 KB

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