atmel_serial.c 72 KB

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