atmel_serial.c 63 KB

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