atmel_serial.c 75 KB

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