atmel_serial.c 75 KB

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