atmel_serial.c 77 KB

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