atmel_serial.c 74 KB

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