atmel_serial.c 81 KB

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