atmel_serial.c 70 KB

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