atmel_serial.c 67 KB

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