atmel_serial.c 77 KB

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