atmel_serial.c 80 KB

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