atmel_serial.c 74 KB

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