atmel_serial.c 74 KB

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