crisv10.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247
  1. /*
  2. * Serial port driver for the ETRAX 100LX chip
  3. *
  4. * Copyright (C) 1998-2007 Axis Communications AB
  5. *
  6. * Many, many authors. Based once upon a time on serial.c for 16x50.
  7. *
  8. */
  9. static char *serial_version = "$Revision: 1.25 $";
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/timer.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/major.h>
  19. #include <linux/string.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mutex.h>
  26. #include <linux/bitops.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/delay.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/io.h>
  31. #include <asm/irq.h>
  32. #include <asm/dma.h>
  33. #include <arch/svinto.h>
  34. #include <arch/system.h>
  35. /* non-arch dependent serial structures are in linux/serial.h */
  36. #include <linux/serial.h>
  37. /* while we keep our own stuff (struct e100_serial) in a local .h file */
  38. #include "crisv10.h"
  39. #include <asm/fasttimer.h>
  40. #include <arch/io_interface_mux.h>
  41. #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
  42. #ifndef CONFIG_ETRAX_FAST_TIMER
  43. #error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
  44. #endif
  45. #endif
  46. #if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
  47. (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
  48. #error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
  49. #endif
  50. /*
  51. * All of the compatibilty code so we can compile serial.c against
  52. * older kernels is hidden in serial_compat.h
  53. */
  54. #if defined(LOCAL_HEADERS)
  55. #include "serial_compat.h"
  56. #endif
  57. struct tty_driver *serial_driver;
  58. /* number of characters left in xmit buffer before we ask for more */
  59. #define WAKEUP_CHARS 256
  60. //#define SERIAL_DEBUG_INTR
  61. //#define SERIAL_DEBUG_OPEN
  62. //#define SERIAL_DEBUG_FLOW
  63. //#define SERIAL_DEBUG_DATA
  64. //#define SERIAL_DEBUG_THROTTLE
  65. //#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
  66. //#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
  67. /* Enable this to use serial interrupts to handle when you
  68. expect the first received event on the serial port to
  69. be an error, break or similar. Used to be able to flash IRMA
  70. from eLinux */
  71. #define SERIAL_HANDLE_EARLY_ERRORS
  72. /* Currently 16 descriptors x 128 bytes = 2048 bytes */
  73. #define SERIAL_DESCR_BUF_SIZE 256
  74. #define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
  75. #define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
  76. /* We don't want to load the system with massive fast timer interrupt
  77. * on high baudrates so limit it to 250 us (4kHz) */
  78. #define MIN_FLUSH_TIME_USEC 250
  79. /* Add an x here to log a lot of timer stuff */
  80. #define TIMERD(x)
  81. /* Debug details of interrupt handling */
  82. #define DINTR1(x) /* irq on/off, errors */
  83. #define DINTR2(x) /* tx and rx */
  84. /* Debug flip buffer stuff */
  85. #define DFLIP(x)
  86. /* Debug flow control and overview of data flow */
  87. #define DFLOW(x)
  88. #define DBAUD(x)
  89. #define DLOG_INT_TRIG(x)
  90. //#define DEBUG_LOG_INCLUDED
  91. #ifndef DEBUG_LOG_INCLUDED
  92. #define DEBUG_LOG(line, string, value)
  93. #else
  94. struct debug_log_info
  95. {
  96. unsigned long time;
  97. unsigned long timer_data;
  98. // int line;
  99. const char *string;
  100. int value;
  101. };
  102. #define DEBUG_LOG_SIZE 4096
  103. struct debug_log_info debug_log[DEBUG_LOG_SIZE];
  104. int debug_log_pos = 0;
  105. #define DEBUG_LOG(_line, _string, _value) do { \
  106. if ((_line) == SERIAL_DEBUG_LINE) {\
  107. debug_log_func(_line, _string, _value); \
  108. }\
  109. }while(0)
  110. void debug_log_func(int line, const char *string, int value)
  111. {
  112. if (debug_log_pos < DEBUG_LOG_SIZE) {
  113. debug_log[debug_log_pos].time = jiffies;
  114. debug_log[debug_log_pos].timer_data = *R_TIMER_DATA;
  115. // debug_log[debug_log_pos].line = line;
  116. debug_log[debug_log_pos].string = string;
  117. debug_log[debug_log_pos].value = value;
  118. debug_log_pos++;
  119. }
  120. /*printk(string, value);*/
  121. }
  122. #endif
  123. #ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
  124. /* Default number of timer ticks before flushing rx fifo
  125. * When using "little data, low latency applications: use 0
  126. * When using "much data applications (PPP)" use ~5
  127. */
  128. #define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
  129. #endif
  130. unsigned long timer_data_to_ns(unsigned long timer_data);
  131. static void change_speed(struct e100_serial *info);
  132. static void rs_throttle(struct tty_struct * tty);
  133. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  134. static int rs_write(struct tty_struct *tty,
  135. const unsigned char *buf, int count);
  136. #ifdef CONFIG_ETRAX_RS485
  137. static int e100_write_rs485(struct tty_struct *tty,
  138. const unsigned char *buf, int count);
  139. #endif
  140. static int get_lsr_info(struct e100_serial *info, unsigned int *value);
  141. #define DEF_BAUD 115200 /* 115.2 kbit/s */
  142. #define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
  143. /* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
  144. #define DEF_TX 0x80 /* or SERIAL_CTRL_B */
  145. /* offsets from R_SERIALx_CTRL */
  146. #define REG_DATA 0
  147. #define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
  148. #define REG_TR_DATA 0
  149. #define REG_STATUS 1
  150. #define REG_TR_CTRL 1
  151. #define REG_REC_CTRL 2
  152. #define REG_BAUD 3
  153. #define REG_XOFF 4 /* this is a 32 bit register */
  154. /* The bitfields are the same for all serial ports */
  155. #define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
  156. #define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
  157. #define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
  158. #define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
  159. #define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
  160. #define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
  161. /* Values for info->errorcode */
  162. #define ERRCODE_SET_BREAK (TTY_BREAK)
  163. #define ERRCODE_INSERT 0x100
  164. #define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
  165. #define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
  166. /*
  167. * General note regarding the use of IO_* macros in this file:
  168. *
  169. * We will use the bits defined for DMA channel 6 when using various
  170. * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
  171. * the same for all channels (which of course they are).
  172. *
  173. * We will also use the bits defined for serial port 0 when writing commands
  174. * to the different ports, as these bits too are the same for all ports.
  175. */
  176. /* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
  177. static const unsigned long e100_ser_int_mask = 0
  178. #ifdef CONFIG_ETRAX_SERIAL_PORT0
  179. | IO_MASK(R_IRQ_MASK1_RD, ser0_data) | IO_MASK(R_IRQ_MASK1_RD, ser0_ready)
  180. #endif
  181. #ifdef CONFIG_ETRAX_SERIAL_PORT1
  182. | IO_MASK(R_IRQ_MASK1_RD, ser1_data) | IO_MASK(R_IRQ_MASK1_RD, ser1_ready)
  183. #endif
  184. #ifdef CONFIG_ETRAX_SERIAL_PORT2
  185. | IO_MASK(R_IRQ_MASK1_RD, ser2_data) | IO_MASK(R_IRQ_MASK1_RD, ser2_ready)
  186. #endif
  187. #ifdef CONFIG_ETRAX_SERIAL_PORT3
  188. | IO_MASK(R_IRQ_MASK1_RD, ser3_data) | IO_MASK(R_IRQ_MASK1_RD, ser3_ready)
  189. #endif
  190. ;
  191. unsigned long r_alt_ser_baudrate_shadow = 0;
  192. /* this is the data for the four serial ports in the etrax100 */
  193. /* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
  194. /* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
  195. static struct e100_serial rs_table[] = {
  196. { .baud = DEF_BAUD,
  197. .ioport = (unsigned char *)R_SERIAL0_CTRL,
  198. .irq = 1U << 12, /* uses DMA 6 and 7 */
  199. .oclrintradr = R_DMA_CH6_CLR_INTR,
  200. .ofirstadr = R_DMA_CH6_FIRST,
  201. .ocmdadr = R_DMA_CH6_CMD,
  202. .ostatusadr = R_DMA_CH6_STATUS,
  203. .iclrintradr = R_DMA_CH7_CLR_INTR,
  204. .ifirstadr = R_DMA_CH7_FIRST,
  205. .icmdadr = R_DMA_CH7_CMD,
  206. .idescradr = R_DMA_CH7_DESCR,
  207. .rx_ctrl = DEF_RX,
  208. .tx_ctrl = DEF_TX,
  209. .iseteop = 2,
  210. .dma_owner = dma_ser0,
  211. .io_if = if_serial_0,
  212. #ifdef CONFIG_ETRAX_SERIAL_PORT0
  213. .enabled = 1,
  214. #ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
  215. .dma_out_enabled = 1,
  216. .dma_out_nbr = SER0_TX_DMA_NBR,
  217. .dma_out_irq_nbr = SER0_DMA_TX_IRQ_NBR,
  218. .dma_out_irq_flags = 0,
  219. .dma_out_irq_description = "serial 0 dma tr",
  220. #else
  221. .dma_out_enabled = 0,
  222. .dma_out_nbr = UINT_MAX,
  223. .dma_out_irq_nbr = 0,
  224. .dma_out_irq_flags = 0,
  225. .dma_out_irq_description = NULL,
  226. #endif
  227. #ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
  228. .dma_in_enabled = 1,
  229. .dma_in_nbr = SER0_RX_DMA_NBR,
  230. .dma_in_irq_nbr = SER0_DMA_RX_IRQ_NBR,
  231. .dma_in_irq_flags = 0,
  232. .dma_in_irq_description = "serial 0 dma rec",
  233. #else
  234. .dma_in_enabled = 0,
  235. .dma_in_nbr = UINT_MAX,
  236. .dma_in_irq_nbr = 0,
  237. .dma_in_irq_flags = 0,
  238. .dma_in_irq_description = NULL,
  239. #endif
  240. #else
  241. .enabled = 0,
  242. .io_if_description = NULL,
  243. .dma_out_enabled = 0,
  244. .dma_in_enabled = 0
  245. #endif
  246. }, /* ttyS0 */
  247. { .baud = DEF_BAUD,
  248. .ioport = (unsigned char *)R_SERIAL1_CTRL,
  249. .irq = 1U << 16, /* uses DMA 8 and 9 */
  250. .oclrintradr = R_DMA_CH8_CLR_INTR,
  251. .ofirstadr = R_DMA_CH8_FIRST,
  252. .ocmdadr = R_DMA_CH8_CMD,
  253. .ostatusadr = R_DMA_CH8_STATUS,
  254. .iclrintradr = R_DMA_CH9_CLR_INTR,
  255. .ifirstadr = R_DMA_CH9_FIRST,
  256. .icmdadr = R_DMA_CH9_CMD,
  257. .idescradr = R_DMA_CH9_DESCR,
  258. .rx_ctrl = DEF_RX,
  259. .tx_ctrl = DEF_TX,
  260. .iseteop = 3,
  261. .dma_owner = dma_ser1,
  262. .io_if = if_serial_1,
  263. #ifdef CONFIG_ETRAX_SERIAL_PORT1
  264. .enabled = 1,
  265. .io_if_description = "ser1",
  266. #ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
  267. .dma_out_enabled = 1,
  268. .dma_out_nbr = SER1_TX_DMA_NBR,
  269. .dma_out_irq_nbr = SER1_DMA_TX_IRQ_NBR,
  270. .dma_out_irq_flags = 0,
  271. .dma_out_irq_description = "serial 1 dma tr",
  272. #else
  273. .dma_out_enabled = 0,
  274. .dma_out_nbr = UINT_MAX,
  275. .dma_out_irq_nbr = 0,
  276. .dma_out_irq_flags = 0,
  277. .dma_out_irq_description = NULL,
  278. #endif
  279. #ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
  280. .dma_in_enabled = 1,
  281. .dma_in_nbr = SER1_RX_DMA_NBR,
  282. .dma_in_irq_nbr = SER1_DMA_RX_IRQ_NBR,
  283. .dma_in_irq_flags = 0,
  284. .dma_in_irq_description = "serial 1 dma rec",
  285. #else
  286. .dma_in_enabled = 0,
  287. .dma_in_enabled = 0,
  288. .dma_in_nbr = UINT_MAX,
  289. .dma_in_irq_nbr = 0,
  290. .dma_in_irq_flags = 0,
  291. .dma_in_irq_description = NULL,
  292. #endif
  293. #else
  294. .enabled = 0,
  295. .io_if_description = NULL,
  296. .dma_in_irq_nbr = 0,
  297. .dma_out_enabled = 0,
  298. .dma_in_enabled = 0
  299. #endif
  300. }, /* ttyS1 */
  301. { .baud = DEF_BAUD,
  302. .ioport = (unsigned char *)R_SERIAL2_CTRL,
  303. .irq = 1U << 4, /* uses DMA 2 and 3 */
  304. .oclrintradr = R_DMA_CH2_CLR_INTR,
  305. .ofirstadr = R_DMA_CH2_FIRST,
  306. .ocmdadr = R_DMA_CH2_CMD,
  307. .ostatusadr = R_DMA_CH2_STATUS,
  308. .iclrintradr = R_DMA_CH3_CLR_INTR,
  309. .ifirstadr = R_DMA_CH3_FIRST,
  310. .icmdadr = R_DMA_CH3_CMD,
  311. .idescradr = R_DMA_CH3_DESCR,
  312. .rx_ctrl = DEF_RX,
  313. .tx_ctrl = DEF_TX,
  314. .iseteop = 0,
  315. .dma_owner = dma_ser2,
  316. .io_if = if_serial_2,
  317. #ifdef CONFIG_ETRAX_SERIAL_PORT2
  318. .enabled = 1,
  319. .io_if_description = "ser2",
  320. #ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
  321. .dma_out_enabled = 1,
  322. .dma_out_nbr = SER2_TX_DMA_NBR,
  323. .dma_out_irq_nbr = SER2_DMA_TX_IRQ_NBR,
  324. .dma_out_irq_flags = 0,
  325. .dma_out_irq_description = "serial 2 dma tr",
  326. #else
  327. .dma_out_enabled = 0,
  328. .dma_out_nbr = UINT_MAX,
  329. .dma_out_irq_nbr = 0,
  330. .dma_out_irq_flags = 0,
  331. .dma_out_irq_description = NULL,
  332. #endif
  333. #ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
  334. .dma_in_enabled = 1,
  335. .dma_in_nbr = SER2_RX_DMA_NBR,
  336. .dma_in_irq_nbr = SER2_DMA_RX_IRQ_NBR,
  337. .dma_in_irq_flags = 0,
  338. .dma_in_irq_description = "serial 2 dma rec",
  339. #else
  340. .dma_in_enabled = 0,
  341. .dma_in_nbr = UINT_MAX,
  342. .dma_in_irq_nbr = 0,
  343. .dma_in_irq_flags = 0,
  344. .dma_in_irq_description = NULL,
  345. #endif
  346. #else
  347. .enabled = 0,
  348. .io_if_description = NULL,
  349. .dma_out_enabled = 0,
  350. .dma_in_enabled = 0
  351. #endif
  352. }, /* ttyS2 */
  353. { .baud = DEF_BAUD,
  354. .ioport = (unsigned char *)R_SERIAL3_CTRL,
  355. .irq = 1U << 8, /* uses DMA 4 and 5 */
  356. .oclrintradr = R_DMA_CH4_CLR_INTR,
  357. .ofirstadr = R_DMA_CH4_FIRST,
  358. .ocmdadr = R_DMA_CH4_CMD,
  359. .ostatusadr = R_DMA_CH4_STATUS,
  360. .iclrintradr = R_DMA_CH5_CLR_INTR,
  361. .ifirstadr = R_DMA_CH5_FIRST,
  362. .icmdadr = R_DMA_CH5_CMD,
  363. .idescradr = R_DMA_CH5_DESCR,
  364. .rx_ctrl = DEF_RX,
  365. .tx_ctrl = DEF_TX,
  366. .iseteop = 1,
  367. .dma_owner = dma_ser3,
  368. .io_if = if_serial_3,
  369. #ifdef CONFIG_ETRAX_SERIAL_PORT3
  370. .enabled = 1,
  371. .io_if_description = "ser3",
  372. #ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
  373. .dma_out_enabled = 1,
  374. .dma_out_nbr = SER3_TX_DMA_NBR,
  375. .dma_out_irq_nbr = SER3_DMA_TX_IRQ_NBR,
  376. .dma_out_irq_flags = 0,
  377. .dma_out_irq_description = "serial 3 dma tr",
  378. #else
  379. .dma_out_enabled = 0,
  380. .dma_out_nbr = UINT_MAX,
  381. .dma_out_irq_nbr = 0,
  382. .dma_out_irq_flags = 0,
  383. .dma_out_irq_description = NULL,
  384. #endif
  385. #ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
  386. .dma_in_enabled = 1,
  387. .dma_in_nbr = SER3_RX_DMA_NBR,
  388. .dma_in_irq_nbr = SER3_DMA_RX_IRQ_NBR,
  389. .dma_in_irq_flags = 0,
  390. .dma_in_irq_description = "serial 3 dma rec",
  391. #else
  392. .dma_in_enabled = 0,
  393. .dma_in_nbr = UINT_MAX,
  394. .dma_in_irq_nbr = 0,
  395. .dma_in_irq_flags = 0,
  396. .dma_in_irq_description = NULL
  397. #endif
  398. #else
  399. .enabled = 0,
  400. .io_if_description = NULL,
  401. .dma_out_enabled = 0,
  402. .dma_in_enabled = 0
  403. #endif
  404. } /* ttyS3 */
  405. };
  406. #define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
  407. #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
  408. static struct fast_timer fast_timers[NR_PORTS];
  409. #endif
  410. /* RS-485 */
  411. #if defined(CONFIG_ETRAX_RS485)
  412. #ifdef CONFIG_ETRAX_FAST_TIMER
  413. static struct fast_timer fast_timers_rs485[NR_PORTS];
  414. #endif
  415. #if defined(CONFIG_ETRAX_RS485_ON_PA)
  416. static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
  417. #endif
  418. #endif
  419. /* Info and macros needed for each ports extra control/status signals. */
  420. #define E100_STRUCT_PORT(line, pinname) \
  421. ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
  422. (R_PORT_PA_DATA): ( \
  423. (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
  424. (R_PORT_PB_DATA):&dummy_ser[line]))
  425. #define E100_STRUCT_SHADOW(line, pinname) \
  426. ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
  427. (&port_pa_data_shadow): ( \
  428. (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
  429. (&port_pb_data_shadow):&dummy_ser[line]))
  430. #define E100_STRUCT_MASK(line, pinname) \
  431. ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
  432. (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
  433. (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
  434. (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
  435. #define DUMMY_DTR_MASK 1
  436. #define DUMMY_RI_MASK 2
  437. #define DUMMY_DSR_MASK 4
  438. #define DUMMY_CD_MASK 8
  439. static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
  440. /* If not all status pins are used or disabled, use mixed mode */
  441. #ifdef CONFIG_ETRAX_SERIAL_PORT0
  442. #define SER0_PA_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PA_BIT+CONFIG_ETRAX_SER0_RI_ON_PA_BIT+CONFIG_ETRAX_SER0_DSR_ON_PA_BIT+CONFIG_ETRAX_SER0_CD_ON_PA_BIT)
  443. #if SER0_PA_BITSUM != -4
  444. # if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
  445. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  446. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  447. # endif
  448. # endif
  449. # if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
  450. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  451. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  452. # endif
  453. # endif
  454. # if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
  455. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  456. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  457. # endif
  458. # endif
  459. # if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
  460. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  461. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  462. # endif
  463. # endif
  464. #endif
  465. #define SER0_PB_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PB_BIT+CONFIG_ETRAX_SER0_RI_ON_PB_BIT+CONFIG_ETRAX_SER0_DSR_ON_PB_BIT+CONFIG_ETRAX_SER0_CD_ON_PB_BIT)
  466. #if SER0_PB_BITSUM != -4
  467. # if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
  468. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  469. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  470. # endif
  471. # endif
  472. # if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
  473. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  474. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  475. # endif
  476. # endif
  477. # if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
  478. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  479. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  480. # endif
  481. # endif
  482. # if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
  483. # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
  484. # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
  485. # endif
  486. # endif
  487. #endif
  488. #endif /* PORT0 */
  489. #ifdef CONFIG_ETRAX_SERIAL_PORT1
  490. #define SER1_PA_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PA_BIT+CONFIG_ETRAX_SER1_RI_ON_PA_BIT+CONFIG_ETRAX_SER1_DSR_ON_PA_BIT+CONFIG_ETRAX_SER1_CD_ON_PA_BIT)
  491. #if SER1_PA_BITSUM != -4
  492. # if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
  493. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  494. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  495. # endif
  496. # endif
  497. # if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
  498. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  499. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  500. # endif
  501. # endif
  502. # if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
  503. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  504. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  505. # endif
  506. # endif
  507. # if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
  508. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  509. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  510. # endif
  511. # endif
  512. #endif
  513. #define SER1_PB_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PB_BIT+CONFIG_ETRAX_SER1_RI_ON_PB_BIT+CONFIG_ETRAX_SER1_DSR_ON_PB_BIT+CONFIG_ETRAX_SER1_CD_ON_PB_BIT)
  514. #if SER1_PB_BITSUM != -4
  515. # if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
  516. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  517. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  518. # endif
  519. # endif
  520. # if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
  521. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  522. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  523. # endif
  524. # endif
  525. # if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
  526. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  527. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  528. # endif
  529. # endif
  530. # if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
  531. # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
  532. # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
  533. # endif
  534. # endif
  535. #endif
  536. #endif /* PORT1 */
  537. #ifdef CONFIG_ETRAX_SERIAL_PORT2
  538. #define SER2_PA_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PA_BIT+CONFIG_ETRAX_SER2_RI_ON_PA_BIT+CONFIG_ETRAX_SER2_DSR_ON_PA_BIT+CONFIG_ETRAX_SER2_CD_ON_PA_BIT)
  539. #if SER2_PA_BITSUM != -4
  540. # if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
  541. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  542. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  543. # endif
  544. # endif
  545. # if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
  546. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  547. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  548. # endif
  549. # endif
  550. # if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
  551. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  552. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  553. # endif
  554. # endif
  555. # if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
  556. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  557. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  558. # endif
  559. # endif
  560. #endif
  561. #define SER2_PB_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PB_BIT+CONFIG_ETRAX_SER2_RI_ON_PB_BIT+CONFIG_ETRAX_SER2_DSR_ON_PB_BIT+CONFIG_ETRAX_SER2_CD_ON_PB_BIT)
  562. #if SER2_PB_BITSUM != -4
  563. # if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
  564. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  565. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  566. # endif
  567. # endif
  568. # if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
  569. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  570. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  571. # endif
  572. # endif
  573. # if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
  574. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  575. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  576. # endif
  577. # endif
  578. # if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
  579. # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
  580. # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
  581. # endif
  582. # endif
  583. #endif
  584. #endif /* PORT2 */
  585. #ifdef CONFIG_ETRAX_SERIAL_PORT3
  586. #define SER3_PA_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PA_BIT+CONFIG_ETRAX_SER3_RI_ON_PA_BIT+CONFIG_ETRAX_SER3_DSR_ON_PA_BIT+CONFIG_ETRAX_SER3_CD_ON_PA_BIT)
  587. #if SER3_PA_BITSUM != -4
  588. # if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
  589. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  590. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  591. # endif
  592. # endif
  593. # if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
  594. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  595. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  596. # endif
  597. # endif
  598. # if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
  599. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  600. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  601. # endif
  602. # endif
  603. # if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
  604. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  605. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  606. # endif
  607. # endif
  608. #endif
  609. #define SER3_PB_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PB_BIT+CONFIG_ETRAX_SER3_RI_ON_PB_BIT+CONFIG_ETRAX_SER3_DSR_ON_PB_BIT+CONFIG_ETRAX_SER3_CD_ON_PB_BIT)
  610. #if SER3_PB_BITSUM != -4
  611. # if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
  612. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  613. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  614. # endif
  615. # endif
  616. # if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
  617. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  618. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  619. # endif
  620. # endif
  621. # if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
  622. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  623. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  624. # endif
  625. # endif
  626. # if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
  627. # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
  628. # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
  629. # endif
  630. # endif
  631. #endif
  632. #endif /* PORT3 */
  633. #if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
  634. defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
  635. defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
  636. defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
  637. #define ETRAX_SERX_DTR_RI_DSR_CD_MIXED
  638. #endif
  639. #ifdef ETRAX_SERX_DTR_RI_DSR_CD_MIXED
  640. /* The pins can be mixed on PA and PB */
  641. #define CONTROL_PINS_PORT_NOT_USED(line) \
  642. &dummy_ser[line], &dummy_ser[line], \
  643. &dummy_ser[line], &dummy_ser[line], \
  644. &dummy_ser[line], &dummy_ser[line], \
  645. &dummy_ser[line], &dummy_ser[line], \
  646. DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
  647. struct control_pins
  648. {
  649. volatile unsigned char *dtr_port;
  650. unsigned char *dtr_shadow;
  651. volatile unsigned char *ri_port;
  652. unsigned char *ri_shadow;
  653. volatile unsigned char *dsr_port;
  654. unsigned char *dsr_shadow;
  655. volatile unsigned char *cd_port;
  656. unsigned char *cd_shadow;
  657. unsigned char dtr_mask;
  658. unsigned char ri_mask;
  659. unsigned char dsr_mask;
  660. unsigned char cd_mask;
  661. };
  662. static const struct control_pins e100_modem_pins[NR_PORTS] =
  663. {
  664. /* Ser 0 */
  665. {
  666. #ifdef CONFIG_ETRAX_SERIAL_PORT0
  667. E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
  668. E100_STRUCT_PORT(0,RI), E100_STRUCT_SHADOW(0,RI),
  669. E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
  670. E100_STRUCT_PORT(0,CD), E100_STRUCT_SHADOW(0,CD),
  671. E100_STRUCT_MASK(0,DTR),
  672. E100_STRUCT_MASK(0,RI),
  673. E100_STRUCT_MASK(0,DSR),
  674. E100_STRUCT_MASK(0,CD)
  675. #else
  676. CONTROL_PINS_PORT_NOT_USED(0)
  677. #endif
  678. },
  679. /* Ser 1 */
  680. {
  681. #ifdef CONFIG_ETRAX_SERIAL_PORT1
  682. E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
  683. E100_STRUCT_PORT(1,RI), E100_STRUCT_SHADOW(1,RI),
  684. E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
  685. E100_STRUCT_PORT(1,CD), E100_STRUCT_SHADOW(1,CD),
  686. E100_STRUCT_MASK(1,DTR),
  687. E100_STRUCT_MASK(1,RI),
  688. E100_STRUCT_MASK(1,DSR),
  689. E100_STRUCT_MASK(1,CD)
  690. #else
  691. CONTROL_PINS_PORT_NOT_USED(1)
  692. #endif
  693. },
  694. /* Ser 2 */
  695. {
  696. #ifdef CONFIG_ETRAX_SERIAL_PORT2
  697. E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
  698. E100_STRUCT_PORT(2,RI), E100_STRUCT_SHADOW(2,RI),
  699. E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
  700. E100_STRUCT_PORT(2,CD), E100_STRUCT_SHADOW(2,CD),
  701. E100_STRUCT_MASK(2,DTR),
  702. E100_STRUCT_MASK(2,RI),
  703. E100_STRUCT_MASK(2,DSR),
  704. E100_STRUCT_MASK(2,CD)
  705. #else
  706. CONTROL_PINS_PORT_NOT_USED(2)
  707. #endif
  708. },
  709. /* Ser 3 */
  710. {
  711. #ifdef CONFIG_ETRAX_SERIAL_PORT3
  712. E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
  713. E100_STRUCT_PORT(3,RI), E100_STRUCT_SHADOW(3,RI),
  714. E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
  715. E100_STRUCT_PORT(3,CD), E100_STRUCT_SHADOW(3,CD),
  716. E100_STRUCT_MASK(3,DTR),
  717. E100_STRUCT_MASK(3,RI),
  718. E100_STRUCT_MASK(3,DSR),
  719. E100_STRUCT_MASK(3,CD)
  720. #else
  721. CONTROL_PINS_PORT_NOT_USED(3)
  722. #endif
  723. }
  724. };
  725. #else /* ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
  726. /* All pins are on either PA or PB for each serial port */
  727. #define CONTROL_PINS_PORT_NOT_USED(line) \
  728. &dummy_ser[line], &dummy_ser[line], \
  729. DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
  730. struct control_pins
  731. {
  732. volatile unsigned char *port;
  733. unsigned char *shadow;
  734. unsigned char dtr_mask;
  735. unsigned char ri_mask;
  736. unsigned char dsr_mask;
  737. unsigned char cd_mask;
  738. };
  739. #define dtr_port port
  740. #define dtr_shadow shadow
  741. #define ri_port port
  742. #define ri_shadow shadow
  743. #define dsr_port port
  744. #define dsr_shadow shadow
  745. #define cd_port port
  746. #define cd_shadow shadow
  747. static const struct control_pins e100_modem_pins[NR_PORTS] =
  748. {
  749. /* Ser 0 */
  750. {
  751. #ifdef CONFIG_ETRAX_SERIAL_PORT0
  752. E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
  753. E100_STRUCT_MASK(0,DTR),
  754. E100_STRUCT_MASK(0,RI),
  755. E100_STRUCT_MASK(0,DSR),
  756. E100_STRUCT_MASK(0,CD)
  757. #else
  758. CONTROL_PINS_PORT_NOT_USED(0)
  759. #endif
  760. },
  761. /* Ser 1 */
  762. {
  763. #ifdef CONFIG_ETRAX_SERIAL_PORT1
  764. E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
  765. E100_STRUCT_MASK(1,DTR),
  766. E100_STRUCT_MASK(1,RI),
  767. E100_STRUCT_MASK(1,DSR),
  768. E100_STRUCT_MASK(1,CD)
  769. #else
  770. CONTROL_PINS_PORT_NOT_USED(1)
  771. #endif
  772. },
  773. /* Ser 2 */
  774. {
  775. #ifdef CONFIG_ETRAX_SERIAL_PORT2
  776. E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
  777. E100_STRUCT_MASK(2,DTR),
  778. E100_STRUCT_MASK(2,RI),
  779. E100_STRUCT_MASK(2,DSR),
  780. E100_STRUCT_MASK(2,CD)
  781. #else
  782. CONTROL_PINS_PORT_NOT_USED(2)
  783. #endif
  784. },
  785. /* Ser 3 */
  786. {
  787. #ifdef CONFIG_ETRAX_SERIAL_PORT3
  788. E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
  789. E100_STRUCT_MASK(3,DTR),
  790. E100_STRUCT_MASK(3,RI),
  791. E100_STRUCT_MASK(3,DSR),
  792. E100_STRUCT_MASK(3,CD)
  793. #else
  794. CONTROL_PINS_PORT_NOT_USED(3)
  795. #endif
  796. }
  797. };
  798. #endif /* !ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
  799. #define E100_RTS_MASK 0x20
  800. #define E100_CTS_MASK 0x40
  801. /* All serial port signals are active low:
  802. * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
  803. * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
  804. *
  805. * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
  806. */
  807. /* Output */
  808. #define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
  809. /* Input */
  810. #define E100_CTS_GET(info) ((info)->ioport[REG_STATUS] & E100_CTS_MASK)
  811. /* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
  812. /* Is an output */
  813. #define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
  814. /* Normally inputs */
  815. #define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
  816. #define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
  817. /* Input */
  818. #define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
  819. /* Calculate the chartime depending on baudrate, numbor of bits etc. */
  820. static void update_char_time(struct e100_serial * info)
  821. {
  822. tcflag_t cflags = info->port.tty->termios.c_cflag;
  823. int bits;
  824. /* calc. number of bits / data byte */
  825. /* databits + startbit and 1 stopbit */
  826. if ((cflags & CSIZE) == CS7)
  827. bits = 9;
  828. else
  829. bits = 10;
  830. if (cflags & CSTOPB) /* 2 stopbits ? */
  831. bits++;
  832. if (cflags & PARENB) /* parity bit ? */
  833. bits++;
  834. /* calc timeout */
  835. info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
  836. info->flush_time_usec = 4*info->char_time_usec;
  837. if (info->flush_time_usec < MIN_FLUSH_TIME_USEC)
  838. info->flush_time_usec = MIN_FLUSH_TIME_USEC;
  839. }
  840. /*
  841. * This function maps from the Bxxxx defines in asm/termbits.h into real
  842. * baud rates.
  843. */
  844. static int
  845. cflag_to_baud(unsigned int cflag)
  846. {
  847. static int baud_table[] = {
  848. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
  849. 4800, 9600, 19200, 38400 };
  850. static int ext_baud_table[] = {
  851. 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
  852. 0, 0, 0, 0, 0, 0, 0, 0 };
  853. if (cflag & CBAUDEX)
  854. return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
  855. else
  856. return baud_table[cflag & CBAUD];
  857. }
  858. /* and this maps to an etrax100 hardware baud constant */
  859. static unsigned char
  860. cflag_to_etrax_baud(unsigned int cflag)
  861. {
  862. char retval;
  863. static char baud_table[] = {
  864. -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
  865. static char ext_baud_table[] = {
  866. -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
  867. if (cflag & CBAUDEX)
  868. retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
  869. else
  870. retval = baud_table[cflag & CBAUD];
  871. if (retval < 0) {
  872. printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
  873. retval = 5; /* choose default 9600 instead */
  874. }
  875. return retval | (retval << 4); /* choose same for both TX and RX */
  876. }
  877. /* Various static support functions */
  878. /* Functions to set or clear DTR/RTS on the requested line */
  879. /* It is complicated by the fact that RTS is a serial port register, while
  880. * DTR might not be implemented in the HW at all, and if it is, it can be on
  881. * any general port.
  882. */
  883. static inline void
  884. e100_dtr(struct e100_serial *info, int set)
  885. {
  886. unsigned char mask = e100_modem_pins[info->line].dtr_mask;
  887. #ifdef SERIAL_DEBUG_IO
  888. printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
  889. printk("ser%i shadow before 0x%02X get: %i\n",
  890. info->line, *e100_modem_pins[info->line].dtr_shadow,
  891. E100_DTR_GET(info));
  892. #endif
  893. /* DTR is active low */
  894. {
  895. unsigned long flags;
  896. local_irq_save(flags);
  897. *e100_modem_pins[info->line].dtr_shadow &= ~mask;
  898. *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask);
  899. *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
  900. local_irq_restore(flags);
  901. }
  902. #ifdef SERIAL_DEBUG_IO
  903. printk("ser%i shadow after 0x%02X get: %i\n",
  904. info->line, *e100_modem_pins[info->line].dtr_shadow,
  905. E100_DTR_GET(info));
  906. #endif
  907. }
  908. /* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
  909. * 0=0V , 1=3.3V
  910. */
  911. static inline void
  912. e100_rts(struct e100_serial *info, int set)
  913. {
  914. unsigned long flags;
  915. local_irq_save(flags);
  916. info->rx_ctrl &= ~E100_RTS_MASK;
  917. info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
  918. info->ioport[REG_REC_CTRL] = info->rx_ctrl;
  919. local_irq_restore(flags);
  920. #ifdef SERIAL_DEBUG_IO
  921. printk("ser%i rts %i\n", info->line, set);
  922. #endif
  923. }
  924. /* If this behaves as a modem, RI and CD is an output */
  925. static inline void
  926. e100_ri_out(struct e100_serial *info, int set)
  927. {
  928. /* RI is active low */
  929. {
  930. unsigned char mask = e100_modem_pins[info->line].ri_mask;
  931. unsigned long flags;
  932. local_irq_save(flags);
  933. *e100_modem_pins[info->line].ri_shadow &= ~mask;
  934. *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask);
  935. *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
  936. local_irq_restore(flags);
  937. }
  938. }
  939. static inline void
  940. e100_cd_out(struct e100_serial *info, int set)
  941. {
  942. /* CD is active low */
  943. {
  944. unsigned char mask = e100_modem_pins[info->line].cd_mask;
  945. unsigned long flags;
  946. local_irq_save(flags);
  947. *e100_modem_pins[info->line].cd_shadow &= ~mask;
  948. *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask);
  949. *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
  950. local_irq_restore(flags);
  951. }
  952. }
  953. static inline void
  954. e100_disable_rx(struct e100_serial *info)
  955. {
  956. /* disable the receiver */
  957. info->ioport[REG_REC_CTRL] =
  958. (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
  959. }
  960. static inline void
  961. e100_enable_rx(struct e100_serial *info)
  962. {
  963. /* enable the receiver */
  964. info->ioport[REG_REC_CTRL] =
  965. (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
  966. }
  967. /* the rx DMA uses both the dma_descr and the dma_eop interrupts */
  968. static inline void
  969. e100_disable_rxdma_irq(struct e100_serial *info)
  970. {
  971. #ifdef SERIAL_DEBUG_INTR
  972. printk("rxdma_irq(%d): 0\n",info->line);
  973. #endif
  974. DINTR1(DEBUG_LOG(info->line,"IRQ disable_rxdma_irq %i\n", info->line));
  975. *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
  976. }
  977. static inline void
  978. e100_enable_rxdma_irq(struct e100_serial *info)
  979. {
  980. #ifdef SERIAL_DEBUG_INTR
  981. printk("rxdma_irq(%d): 1\n",info->line);
  982. #endif
  983. DINTR1(DEBUG_LOG(info->line,"IRQ enable_rxdma_irq %i\n", info->line));
  984. *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
  985. }
  986. /* the tx DMA uses only dma_descr interrupt */
  987. static void e100_disable_txdma_irq(struct e100_serial *info)
  988. {
  989. #ifdef SERIAL_DEBUG_INTR
  990. printk("txdma_irq(%d): 0\n",info->line);
  991. #endif
  992. DINTR1(DEBUG_LOG(info->line,"IRQ disable_txdma_irq %i\n", info->line));
  993. *R_IRQ_MASK2_CLR = info->irq;
  994. }
  995. static void e100_enable_txdma_irq(struct e100_serial *info)
  996. {
  997. #ifdef SERIAL_DEBUG_INTR
  998. printk("txdma_irq(%d): 1\n",info->line);
  999. #endif
  1000. DINTR1(DEBUG_LOG(info->line,"IRQ enable_txdma_irq %i\n", info->line));
  1001. *R_IRQ_MASK2_SET = info->irq;
  1002. }
  1003. static void e100_disable_txdma_channel(struct e100_serial *info)
  1004. {
  1005. unsigned long flags;
  1006. /* Disable output DMA channel for the serial port in question
  1007. * ( set to something other than serialX)
  1008. */
  1009. local_irq_save(flags);
  1010. DFLOW(DEBUG_LOG(info->line, "disable_txdma_channel %i\n", info->line));
  1011. if (info->line == 0) {
  1012. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma6)) ==
  1013. IO_STATE(R_GEN_CONFIG, dma6, serial0)) {
  1014. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
  1015. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
  1016. }
  1017. } else if (info->line == 1) {
  1018. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma8)) ==
  1019. IO_STATE(R_GEN_CONFIG, dma8, serial1)) {
  1020. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
  1021. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
  1022. }
  1023. } else if (info->line == 2) {
  1024. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma2)) ==
  1025. IO_STATE(R_GEN_CONFIG, dma2, serial2)) {
  1026. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
  1027. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
  1028. }
  1029. } else if (info->line == 3) {
  1030. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma4)) ==
  1031. IO_STATE(R_GEN_CONFIG, dma4, serial3)) {
  1032. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
  1033. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
  1034. }
  1035. }
  1036. *R_GEN_CONFIG = genconfig_shadow;
  1037. local_irq_restore(flags);
  1038. }
  1039. static void e100_enable_txdma_channel(struct e100_serial *info)
  1040. {
  1041. unsigned long flags;
  1042. local_irq_save(flags);
  1043. DFLOW(DEBUG_LOG(info->line, "enable_txdma_channel %i\n", info->line));
  1044. /* Enable output DMA channel for the serial port in question */
  1045. if (info->line == 0) {
  1046. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
  1047. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
  1048. } else if (info->line == 1) {
  1049. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
  1050. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
  1051. } else if (info->line == 2) {
  1052. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
  1053. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
  1054. } else if (info->line == 3) {
  1055. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
  1056. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
  1057. }
  1058. *R_GEN_CONFIG = genconfig_shadow;
  1059. local_irq_restore(flags);
  1060. }
  1061. static void e100_disable_rxdma_channel(struct e100_serial *info)
  1062. {
  1063. unsigned long flags;
  1064. /* Disable input DMA channel for the serial port in question
  1065. * ( set to something other than serialX)
  1066. */
  1067. local_irq_save(flags);
  1068. if (info->line == 0) {
  1069. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma7)) ==
  1070. IO_STATE(R_GEN_CONFIG, dma7, serial0)) {
  1071. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
  1072. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, unused);
  1073. }
  1074. } else if (info->line == 1) {
  1075. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma9)) ==
  1076. IO_STATE(R_GEN_CONFIG, dma9, serial1)) {
  1077. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
  1078. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, usb);
  1079. }
  1080. } else if (info->line == 2) {
  1081. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma3)) ==
  1082. IO_STATE(R_GEN_CONFIG, dma3, serial2)) {
  1083. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
  1084. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
  1085. }
  1086. } else if (info->line == 3) {
  1087. if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma5)) ==
  1088. IO_STATE(R_GEN_CONFIG, dma5, serial3)) {
  1089. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
  1090. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
  1091. }
  1092. }
  1093. *R_GEN_CONFIG = genconfig_shadow;
  1094. local_irq_restore(flags);
  1095. }
  1096. static void e100_enable_rxdma_channel(struct e100_serial *info)
  1097. {
  1098. unsigned long flags;
  1099. local_irq_save(flags);
  1100. /* Enable input DMA channel for the serial port in question */
  1101. if (info->line == 0) {
  1102. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
  1103. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, serial0);
  1104. } else if (info->line == 1) {
  1105. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
  1106. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, serial1);
  1107. } else if (info->line == 2) {
  1108. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
  1109. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, serial2);
  1110. } else if (info->line == 3) {
  1111. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
  1112. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, serial3);
  1113. }
  1114. *R_GEN_CONFIG = genconfig_shadow;
  1115. local_irq_restore(flags);
  1116. }
  1117. #ifdef SERIAL_HANDLE_EARLY_ERRORS
  1118. /* in order to detect and fix errors on the first byte
  1119. we have to use the serial interrupts as well. */
  1120. static inline void
  1121. e100_disable_serial_data_irq(struct e100_serial *info)
  1122. {
  1123. #ifdef SERIAL_DEBUG_INTR
  1124. printk("ser_irq(%d): 0\n",info->line);
  1125. #endif
  1126. DINTR1(DEBUG_LOG(info->line,"IRQ disable data_irq %i\n", info->line));
  1127. *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
  1128. }
  1129. static inline void
  1130. e100_enable_serial_data_irq(struct e100_serial *info)
  1131. {
  1132. #ifdef SERIAL_DEBUG_INTR
  1133. printk("ser_irq(%d): 1\n",info->line);
  1134. printk("**** %d = %d\n",
  1135. (8+2*info->line),
  1136. (1U << (8+2*info->line)));
  1137. #endif
  1138. DINTR1(DEBUG_LOG(info->line,"IRQ enable data_irq %i\n", info->line));
  1139. *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
  1140. }
  1141. #endif
  1142. static inline void
  1143. e100_disable_serial_tx_ready_irq(struct e100_serial *info)
  1144. {
  1145. #ifdef SERIAL_DEBUG_INTR
  1146. printk("ser_tx_irq(%d): 0\n",info->line);
  1147. #endif
  1148. DINTR1(DEBUG_LOG(info->line,"IRQ disable ready_irq %i\n", info->line));
  1149. *R_IRQ_MASK1_CLR = (1U << (8+1+2*info->line));
  1150. }
  1151. static inline void
  1152. e100_enable_serial_tx_ready_irq(struct e100_serial *info)
  1153. {
  1154. #ifdef SERIAL_DEBUG_INTR
  1155. printk("ser_tx_irq(%d): 1\n",info->line);
  1156. printk("**** %d = %d\n",
  1157. (8+1+2*info->line),
  1158. (1U << (8+1+2*info->line)));
  1159. #endif
  1160. DINTR2(DEBUG_LOG(info->line,"IRQ enable ready_irq %i\n", info->line));
  1161. *R_IRQ_MASK1_SET = (1U << (8+1+2*info->line));
  1162. }
  1163. static inline void e100_enable_rx_irq(struct e100_serial *info)
  1164. {
  1165. if (info->uses_dma_in)
  1166. e100_enable_rxdma_irq(info);
  1167. else
  1168. e100_enable_serial_data_irq(info);
  1169. }
  1170. static inline void e100_disable_rx_irq(struct e100_serial *info)
  1171. {
  1172. if (info->uses_dma_in)
  1173. e100_disable_rxdma_irq(info);
  1174. else
  1175. e100_disable_serial_data_irq(info);
  1176. }
  1177. #if defined(CONFIG_ETRAX_RS485)
  1178. /* Enable RS-485 mode on selected port. This is UGLY. */
  1179. static int
  1180. e100_enable_rs485(struct tty_struct *tty, struct serial_rs485 *r)
  1181. {
  1182. struct e100_serial * info = (struct e100_serial *)tty->driver_data;
  1183. #if defined(CONFIG_ETRAX_RS485_ON_PA)
  1184. *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
  1185. #endif
  1186. info->rs485 = *r;
  1187. /* Maximum delay before RTS equal to 1000 */
  1188. if (info->rs485.delay_rts_before_send >= 1000)
  1189. info->rs485.delay_rts_before_send = 1000;
  1190. /* printk("rts: on send = %i, after = %i, enabled = %i",
  1191. info->rs485.rts_on_send,
  1192. info->rs485.rts_after_sent,
  1193. info->rs485.enabled
  1194. );
  1195. */
  1196. return 0;
  1197. }
  1198. static int
  1199. e100_write_rs485(struct tty_struct *tty,
  1200. const unsigned char *buf, int count)
  1201. {
  1202. struct e100_serial * info = (struct e100_serial *)tty->driver_data;
  1203. int old_value = (info->rs485.flags) & SER_RS485_ENABLED;
  1204. /* rs485 is always implicitly enabled if we're using the ioctl()
  1205. * but it doesn't have to be set in the serial_rs485
  1206. * (to be backward compatible with old apps)
  1207. * So we store, set and restore it.
  1208. */
  1209. info->rs485.flags |= SER_RS485_ENABLED;
  1210. /* rs_write now deals with RS485 if enabled */
  1211. count = rs_write(tty, buf, count);
  1212. if (!old_value)
  1213. info->rs485.flags &= ~(SER_RS485_ENABLED);
  1214. return count;
  1215. }
  1216. #ifdef CONFIG_ETRAX_FAST_TIMER
  1217. /* Timer function to toggle RTS when using FAST_TIMER */
  1218. static void rs485_toggle_rts_timer_function(unsigned long data)
  1219. {
  1220. struct e100_serial *info = (struct e100_serial *)data;
  1221. fast_timers_rs485[info->line].function = NULL;
  1222. e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
  1223. #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
  1224. e100_enable_rx(info);
  1225. e100_enable_rx_irq(info);
  1226. #endif
  1227. }
  1228. #endif
  1229. #endif /* CONFIG_ETRAX_RS485 */
  1230. /*
  1231. * ------------------------------------------------------------
  1232. * rs_stop() and rs_start()
  1233. *
  1234. * This routines are called before setting or resetting tty->stopped.
  1235. * They enable or disable transmitter using the XOFF registers, as necessary.
  1236. * ------------------------------------------------------------
  1237. */
  1238. static void
  1239. rs_stop(struct tty_struct *tty)
  1240. {
  1241. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  1242. if (info) {
  1243. unsigned long flags;
  1244. unsigned long xoff;
  1245. local_irq_save(flags);
  1246. DFLOW(DEBUG_LOG(info->line, "XOFF rs_stop xmit %i\n",
  1247. CIRC_CNT(info->xmit.head,
  1248. info->xmit.tail,SERIAL_XMIT_SIZE)));
  1249. xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char,
  1250. STOP_CHAR(info->port.tty));
  1251. xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
  1252. if (I_IXON(tty))
  1253. xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
  1254. *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
  1255. local_irq_restore(flags);
  1256. }
  1257. }
  1258. static void
  1259. rs_start(struct tty_struct *tty)
  1260. {
  1261. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  1262. if (info) {
  1263. unsigned long flags;
  1264. unsigned long xoff;
  1265. local_irq_save(flags);
  1266. DFLOW(DEBUG_LOG(info->line, "XOFF rs_start xmit %i\n",
  1267. CIRC_CNT(info->xmit.head,
  1268. info->xmit.tail,SERIAL_XMIT_SIZE)));
  1269. xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
  1270. xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
  1271. if (I_IXON(tty))
  1272. xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
  1273. *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
  1274. if (!info->uses_dma_out &&
  1275. info->xmit.head != info->xmit.tail && info->xmit.buf)
  1276. e100_enable_serial_tx_ready_irq(info);
  1277. local_irq_restore(flags);
  1278. }
  1279. }
  1280. /*
  1281. * ----------------------------------------------------------------------
  1282. *
  1283. * Here starts the interrupt handling routines. All of the following
  1284. * subroutines are declared as inline and are folded into
  1285. * rs_interrupt(). They were separated out for readability's sake.
  1286. *
  1287. * Note: rs_interrupt() is a "fast" interrupt, which means that it
  1288. * runs with interrupts turned off. People who may want to modify
  1289. * rs_interrupt() should try to keep the interrupt handler as fast as
  1290. * possible. After you are done making modifications, it is not a bad
  1291. * idea to do:
  1292. *
  1293. * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  1294. *
  1295. * and look at the resulting assemble code in serial.s.
  1296. *
  1297. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  1298. * -----------------------------------------------------------------------
  1299. */
  1300. /*
  1301. * This routine is used by the interrupt handler to schedule
  1302. * processing in the software interrupt portion of the driver.
  1303. */
  1304. static void rs_sched_event(struct e100_serial *info, int event)
  1305. {
  1306. if (info->event & (1 << event))
  1307. return;
  1308. info->event |= 1 << event;
  1309. schedule_work(&info->work);
  1310. }
  1311. /* The output DMA channel is free - use it to send as many chars as possible
  1312. * NOTES:
  1313. * We don't pay attention to info->x_char, which means if the TTY wants to
  1314. * use XON/XOFF it will set info->x_char but we won't send any X char!
  1315. *
  1316. * To implement this, we'd just start a DMA send of 1 byte pointing at a
  1317. * buffer containing the X char, and skip updating xmit. We'd also have to
  1318. * check if the last sent char was the X char when we enter this function
  1319. * the next time, to avoid updating xmit with the sent X value.
  1320. */
  1321. static void
  1322. transmit_chars_dma(struct e100_serial *info)
  1323. {
  1324. unsigned int c, sentl;
  1325. struct etrax_dma_descr *descr;
  1326. /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
  1327. *info->oclrintradr =
  1328. IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
  1329. IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
  1330. #ifdef SERIAL_DEBUG_INTR
  1331. if (info->line == SERIAL_DEBUG_LINE)
  1332. printk("tc\n");
  1333. #endif
  1334. if (!info->tr_running) {
  1335. /* weirdo... we shouldn't get here! */
  1336. printk(KERN_WARNING "Achtung: transmit_chars_dma with !tr_running\n");
  1337. return;
  1338. }
  1339. descr = &info->tr_descr;
  1340. /* first get the amount of bytes sent during the last DMA transfer,
  1341. and update xmit accordingly */
  1342. /* if the stop bit was not set, all data has been sent */
  1343. if (!(descr->status & d_stop)) {
  1344. sentl = descr->sw_len;
  1345. } else
  1346. /* otherwise we find the amount of data sent here */
  1347. sentl = descr->hw_len;
  1348. DFLOW(DEBUG_LOG(info->line, "TX %i done\n", sentl));
  1349. /* update stats */
  1350. info->icount.tx += sentl;
  1351. /* update xmit buffer */
  1352. info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
  1353. /* if there is only a few chars left in the buf, wake up the blocked
  1354. write if any */
  1355. if (CIRC_CNT(info->xmit.head,
  1356. info->xmit.tail,
  1357. SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  1358. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  1359. /* find out the largest amount of consecutive bytes we want to send now */
  1360. c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  1361. /* Don't send all in one DMA transfer - divide it so we wake up
  1362. * application before all is sent
  1363. */
  1364. if (c >= 4*WAKEUP_CHARS)
  1365. c = c/2;
  1366. if (c <= 0) {
  1367. /* our job here is done, don't schedule any new DMA transfer */
  1368. info->tr_running = 0;
  1369. #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
  1370. if (info->rs485.flags & SER_RS485_ENABLED) {
  1371. /* Set a short timer to toggle RTS */
  1372. start_one_shot_timer(&fast_timers_rs485[info->line],
  1373. rs485_toggle_rts_timer_function,
  1374. (unsigned long)info,
  1375. info->char_time_usec*2,
  1376. "RS-485");
  1377. }
  1378. #endif /* RS485 */
  1379. return;
  1380. }
  1381. /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
  1382. /* set up the descriptor correctly for output */
  1383. DFLOW(DEBUG_LOG(info->line, "TX %i\n", c));
  1384. descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
  1385. descr->sw_len = c;
  1386. descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
  1387. descr->status = 0;
  1388. *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
  1389. *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
  1390. /* DMA is now running (hopefully) */
  1391. } /* transmit_chars_dma */
  1392. static void
  1393. start_transmit(struct e100_serial *info)
  1394. {
  1395. #if 0
  1396. if (info->line == SERIAL_DEBUG_LINE)
  1397. printk("x\n");
  1398. #endif
  1399. info->tr_descr.sw_len = 0;
  1400. info->tr_descr.hw_len = 0;
  1401. info->tr_descr.status = 0;
  1402. info->tr_running = 1;
  1403. if (info->uses_dma_out)
  1404. transmit_chars_dma(info);
  1405. else
  1406. e100_enable_serial_tx_ready_irq(info);
  1407. } /* start_transmit */
  1408. #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
  1409. static int serial_fast_timer_started = 0;
  1410. static int serial_fast_timer_expired = 0;
  1411. static void flush_timeout_function(unsigned long data);
  1412. #define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
  1413. unsigned long timer_flags; \
  1414. local_irq_save(timer_flags); \
  1415. if (fast_timers[info->line].function == NULL) { \
  1416. serial_fast_timer_started++; \
  1417. TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
  1418. TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
  1419. start_one_shot_timer(&fast_timers[info->line], \
  1420. flush_timeout_function, \
  1421. (unsigned long)info, \
  1422. (usec), \
  1423. string); \
  1424. } \
  1425. else { \
  1426. TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
  1427. } \
  1428. local_irq_restore(timer_flags); \
  1429. }
  1430. #define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
  1431. #else
  1432. #define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
  1433. #define START_FLUSH_FAST_TIMER(info, string)
  1434. #endif
  1435. static struct etrax_recv_buffer *
  1436. alloc_recv_buffer(unsigned int size)
  1437. {
  1438. struct etrax_recv_buffer *buffer;
  1439. buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC);
  1440. if (!buffer)
  1441. return NULL;
  1442. buffer->next = NULL;
  1443. buffer->length = 0;
  1444. buffer->error = TTY_NORMAL;
  1445. return buffer;
  1446. }
  1447. static void
  1448. append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
  1449. {
  1450. unsigned long flags;
  1451. local_irq_save(flags);
  1452. if (!info->first_recv_buffer)
  1453. info->first_recv_buffer = buffer;
  1454. else
  1455. info->last_recv_buffer->next = buffer;
  1456. info->last_recv_buffer = buffer;
  1457. info->recv_cnt += buffer->length;
  1458. if (info->recv_cnt > info->max_recv_cnt)
  1459. info->max_recv_cnt = info->recv_cnt;
  1460. local_irq_restore(flags);
  1461. }
  1462. static int
  1463. add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
  1464. {
  1465. struct etrax_recv_buffer *buffer;
  1466. if (info->uses_dma_in) {
  1467. buffer = alloc_recv_buffer(4);
  1468. if (!buffer)
  1469. return 0;
  1470. buffer->length = 1;
  1471. buffer->error = flag;
  1472. buffer->buffer[0] = data;
  1473. append_recv_buffer(info, buffer);
  1474. info->icount.rx++;
  1475. } else {
  1476. tty_insert_flip_char(&info->port, data, flag);
  1477. info->icount.rx++;
  1478. }
  1479. return 1;
  1480. }
  1481. static unsigned int handle_descr_data(struct e100_serial *info,
  1482. struct etrax_dma_descr *descr,
  1483. unsigned int recvl)
  1484. {
  1485. struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
  1486. if (info->recv_cnt + recvl > 65536) {
  1487. printk(KERN_WARNING
  1488. "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __func__, recvl);
  1489. return 0;
  1490. }
  1491. buffer->length = recvl;
  1492. if (info->errorcode == ERRCODE_SET_BREAK)
  1493. buffer->error = TTY_BREAK;
  1494. info->errorcode = 0;
  1495. append_recv_buffer(info, buffer);
  1496. buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE);
  1497. if (!buffer)
  1498. panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
  1499. descr->buf = virt_to_phys(buffer->buffer);
  1500. return recvl;
  1501. }
  1502. static unsigned int handle_all_descr_data(struct e100_serial *info)
  1503. {
  1504. struct etrax_dma_descr *descr;
  1505. unsigned int recvl;
  1506. unsigned int ret = 0;
  1507. while (1)
  1508. {
  1509. descr = &info->rec_descr[info->cur_rec_descr];
  1510. if (descr == phys_to_virt(*info->idescradr))
  1511. break;
  1512. if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
  1513. info->cur_rec_descr = 0;
  1514. /* find out how many bytes were read */
  1515. /* if the eop bit was not set, all data has been received */
  1516. if (!(descr->status & d_eop)) {
  1517. recvl = descr->sw_len;
  1518. } else {
  1519. /* otherwise we find the amount of data received here */
  1520. recvl = descr->hw_len;
  1521. }
  1522. /* Reset the status information */
  1523. descr->status = 0;
  1524. DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
  1525. if (info->port.tty->stopped) {
  1526. unsigned char *buf = phys_to_virt(descr->buf);
  1527. DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
  1528. DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
  1529. DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
  1530. }
  1531. );
  1532. /* update stats */
  1533. info->icount.rx += recvl;
  1534. ret += handle_descr_data(info, descr, recvl);
  1535. }
  1536. return ret;
  1537. }
  1538. static void receive_chars_dma(struct e100_serial *info)
  1539. {
  1540. struct tty_struct *tty;
  1541. unsigned char rstat;
  1542. /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
  1543. *info->iclrintradr =
  1544. IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
  1545. IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
  1546. tty = info->port.tty;
  1547. if (!tty) /* Something wrong... */
  1548. return;
  1549. #ifdef SERIAL_HANDLE_EARLY_ERRORS
  1550. if (info->uses_dma_in)
  1551. e100_enable_serial_data_irq(info);
  1552. #endif
  1553. if (info->errorcode == ERRCODE_INSERT_BREAK)
  1554. add_char_and_flag(info, '\0', TTY_BREAK);
  1555. handle_all_descr_data(info);
  1556. /* Read the status register to detect errors */
  1557. rstat = info->ioport[REG_STATUS];
  1558. if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
  1559. DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
  1560. }
  1561. if (rstat & SER_ERROR_MASK) {
  1562. /* If we got an error, we must reset it by reading the
  1563. * data_in field
  1564. */
  1565. unsigned char data = info->ioport[REG_DATA];
  1566. DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
  1567. ((rstat & SER_ERROR_MASK) << 8) | data);
  1568. if (rstat & SER_PAR_ERR_MASK)
  1569. add_char_and_flag(info, data, TTY_PARITY);
  1570. else if (rstat & SER_OVERRUN_MASK)
  1571. add_char_and_flag(info, data, TTY_OVERRUN);
  1572. else if (rstat & SER_FRAMING_ERR_MASK)
  1573. add_char_and_flag(info, data, TTY_FRAME);
  1574. }
  1575. START_FLUSH_FAST_TIMER(info, "receive_chars");
  1576. /* Restart the receiving DMA */
  1577. *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
  1578. }
  1579. static int start_recv_dma(struct e100_serial *info)
  1580. {
  1581. struct etrax_dma_descr *descr = info->rec_descr;
  1582. struct etrax_recv_buffer *buffer;
  1583. int i;
  1584. /* Set up the receiving descriptors */
  1585. for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
  1586. buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE);
  1587. if (!buffer)
  1588. panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
  1589. descr[i].ctrl = d_int;
  1590. descr[i].buf = virt_to_phys(buffer->buffer);
  1591. descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
  1592. descr[i].hw_len = 0;
  1593. descr[i].status = 0;
  1594. descr[i].next = virt_to_phys(&descr[i+1]);
  1595. }
  1596. /* Link the last descriptor to the first */
  1597. descr[i-1].next = virt_to_phys(&descr[0]);
  1598. /* Start with the first descriptor in the list */
  1599. info->cur_rec_descr = 0;
  1600. /* Start the DMA */
  1601. *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
  1602. *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
  1603. /* Input DMA should be running now */
  1604. return 1;
  1605. }
  1606. static void
  1607. start_receive(struct e100_serial *info)
  1608. {
  1609. if (info->uses_dma_in) {
  1610. /* reset the input dma channel to be sure it works */
  1611. *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
  1612. while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
  1613. IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
  1614. start_recv_dma(info);
  1615. }
  1616. }
  1617. /* the bits in the MASK2 register are laid out like this:
  1618. DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
  1619. where I is the input channel and O is the output channel for the port.
  1620. info->irq is the bit number for the DMAO_DESCR so to check the others we
  1621. shift info->irq to the left.
  1622. */
  1623. /* dma output channel interrupt handler
  1624. this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
  1625. DMA8(ser1) when they have finished a descriptor with the intr flag set.
  1626. */
  1627. static irqreturn_t
  1628. tr_interrupt(int irq, void *dev_id)
  1629. {
  1630. struct e100_serial *info;
  1631. unsigned long ireg;
  1632. int i;
  1633. int handled = 0;
  1634. /* find out the line that caused this irq and get it from rs_table */
  1635. ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
  1636. for (i = 0; i < NR_PORTS; i++) {
  1637. info = rs_table + i;
  1638. if (!info->enabled || !info->uses_dma_out)
  1639. continue;
  1640. /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
  1641. if (ireg & info->irq) {
  1642. handled = 1;
  1643. /* we can send a new dma bunch. make it so. */
  1644. DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
  1645. /* Read jiffies_usec first,
  1646. * we want this time to be as late as possible
  1647. */
  1648. info->last_tx_active_usec = GET_JIFFIES_USEC();
  1649. info->last_tx_active = jiffies;
  1650. transmit_chars_dma(info);
  1651. }
  1652. /* FIXME: here we should really check for a change in the
  1653. status lines and if so call status_handle(info) */
  1654. }
  1655. return IRQ_RETVAL(handled);
  1656. } /* tr_interrupt */
  1657. /* dma input channel interrupt handler */
  1658. static irqreturn_t
  1659. rec_interrupt(int irq, void *dev_id)
  1660. {
  1661. struct e100_serial *info;
  1662. unsigned long ireg;
  1663. int i;
  1664. int handled = 0;
  1665. /* find out the line that caused this irq and get it from rs_table */
  1666. ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
  1667. for (i = 0; i < NR_PORTS; i++) {
  1668. info = rs_table + i;
  1669. if (!info->enabled || !info->uses_dma_in)
  1670. continue;
  1671. /* check for both dma_eop and dma_descr for the input dma channel */
  1672. if (ireg & ((info->irq << 2) | (info->irq << 3))) {
  1673. handled = 1;
  1674. /* we have received something */
  1675. receive_chars_dma(info);
  1676. }
  1677. /* FIXME: here we should really check for a change in the
  1678. status lines and if so call status_handle(info) */
  1679. }
  1680. return IRQ_RETVAL(handled);
  1681. } /* rec_interrupt */
  1682. static int force_eop_if_needed(struct e100_serial *info)
  1683. {
  1684. /* We check data_avail bit to determine if data has
  1685. * arrived since last time
  1686. */
  1687. unsigned char rstat = info->ioport[REG_STATUS];
  1688. /* error or datavail? */
  1689. if (rstat & SER_ERROR_MASK) {
  1690. /* Some error has occurred. If there has been valid data, an
  1691. * EOP interrupt will be made automatically. If no data, the
  1692. * normal ser_interrupt should be enabled and handle it.
  1693. * So do nothing!
  1694. */
  1695. DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
  1696. rstat | (info->line << 8));
  1697. return 0;
  1698. }
  1699. if (rstat & SER_DATA_AVAIL_MASK) {
  1700. /* Ok data, no error, count it */
  1701. TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
  1702. rstat | (info->line << 8)));
  1703. /* Read data to clear status flags */
  1704. (void)info->ioport[REG_DATA];
  1705. info->forced_eop = 0;
  1706. START_FLUSH_FAST_TIMER(info, "magic");
  1707. return 0;
  1708. }
  1709. /* hit the timeout, force an EOP for the input
  1710. * dma channel if we haven't already
  1711. */
  1712. if (!info->forced_eop) {
  1713. info->forced_eop = 1;
  1714. TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
  1715. FORCE_EOP(info);
  1716. }
  1717. return 1;
  1718. }
  1719. static void flush_to_flip_buffer(struct e100_serial *info)
  1720. {
  1721. struct etrax_recv_buffer *buffer;
  1722. unsigned long flags;
  1723. local_irq_save(flags);
  1724. while ((buffer = info->first_recv_buffer) != NULL) {
  1725. unsigned int count = buffer->length;
  1726. tty_insert_flip_string(&info->port, buffer->buffer, count);
  1727. info->recv_cnt -= count;
  1728. if (count == buffer->length) {
  1729. info->first_recv_buffer = buffer->next;
  1730. kfree(buffer);
  1731. } else {
  1732. buffer->length -= count;
  1733. memmove(buffer->buffer, buffer->buffer + count, buffer->length);
  1734. buffer->error = TTY_NORMAL;
  1735. }
  1736. }
  1737. if (!info->first_recv_buffer)
  1738. info->last_recv_buffer = NULL;
  1739. local_irq_restore(flags);
  1740. /* This includes a check for low-latency */
  1741. tty_flip_buffer_push(&info->port);
  1742. }
  1743. static void check_flush_timeout(struct e100_serial *info)
  1744. {
  1745. /* Flip what we've got (if we can) */
  1746. flush_to_flip_buffer(info);
  1747. /* We might need to flip later, but not to fast
  1748. * since the system is busy processing input... */
  1749. if (info->first_recv_buffer)
  1750. START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
  1751. /* Force eop last, since data might have come while we're processing
  1752. * and if we started the slow timer above, we won't start a fast
  1753. * below.
  1754. */
  1755. force_eop_if_needed(info);
  1756. }
  1757. #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
  1758. static void flush_timeout_function(unsigned long data)
  1759. {
  1760. struct e100_serial *info = (struct e100_serial *)data;
  1761. fast_timers[info->line].function = NULL;
  1762. serial_fast_timer_expired++;
  1763. TIMERD(DEBUG_LOG(info->line, "flush_timeout %i ", info->line));
  1764. TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
  1765. check_flush_timeout(info);
  1766. }
  1767. #else
  1768. /* dma fifo/buffer timeout handler
  1769. forces an end-of-packet for the dma input channel if no chars
  1770. have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
  1771. */
  1772. static struct timer_list flush_timer;
  1773. static void
  1774. timed_flush_handler(unsigned long ptr)
  1775. {
  1776. struct e100_serial *info;
  1777. int i;
  1778. for (i = 0; i < NR_PORTS; i++) {
  1779. info = rs_table + i;
  1780. if (info->uses_dma_in)
  1781. check_flush_timeout(info);
  1782. }
  1783. /* restart flush timer */
  1784. mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
  1785. }
  1786. #endif
  1787. #ifdef SERIAL_HANDLE_EARLY_ERRORS
  1788. /* If there is an error (ie break) when the DMA is running and
  1789. * there are no bytes in the fifo the DMA is stopped and we get no
  1790. * eop interrupt. Thus we have to monitor the first bytes on a DMA
  1791. * transfer, and if it is without error we can turn the serial
  1792. * interrupts off.
  1793. */
  1794. /*
  1795. BREAK handling on ETRAX 100:
  1796. ETRAX will generate interrupt although there is no stop bit between the
  1797. characters.
  1798. Depending on how long the break sequence is, the end of the breaksequence
  1799. will look differently:
  1800. | indicates start/end of a character.
  1801. B= Break character (0x00) with framing error.
  1802. E= Error byte with parity error received after B characters.
  1803. F= "Faked" valid byte received immediately after B characters.
  1804. V= Valid byte
  1805. 1.
  1806. B BL ___________________________ V
  1807. .._|__________|__________| |valid data |
  1808. Multiple frame errors with data == 0x00 (B),
  1809. the timing matches up "perfectly" so no extra ending char is detected.
  1810. The RXD pin is 1 in the last interrupt, in that case
  1811. we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
  1812. know if another byte will come and this really is case 2. below
  1813. (e.g F=0xFF or 0xFE)
  1814. If RXD pin is 0 we can expect another character (see 2. below).
  1815. 2.
  1816. B B E or F__________________..__ V
  1817. .._|__________|__________|______ | |valid data
  1818. "valid" or
  1819. parity error
  1820. Multiple frame errors with data == 0x00 (B),
  1821. but the part of the break trigs is interpreted as a start bit (and possibly
  1822. some 0 bits followed by a number of 1 bits and a stop bit).
  1823. Depending on parity settings etc. this last character can be either
  1824. a fake "valid" char (F) or have a parity error (E).
  1825. If the character is valid it will be put in the buffer,
  1826. we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
  1827. will set the flags so the tty will handle it,
  1828. if it's an error byte it will not be put in the buffer
  1829. and we set info->errorcode = ERRCODE_INSERT_BREAK.
  1830. To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
  1831. of the last faulty char (B) and compares it with the current time:
  1832. If the time elapsed time is less then 2*char_time_usec we will assume
  1833. it's a faked F char and not a Valid char and set
  1834. info->errorcode = ERRCODE_SET_BREAK.
  1835. Flaws in the above solution:
  1836. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1837. We use the timer to distinguish a F character from a V character,
  1838. if a V character is to close after the break we might make the wrong decision.
  1839. TODO: The break will be delayed until an F or V character is received.
  1840. */
  1841. static void handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
  1842. {
  1843. unsigned long data_read;
  1844. /* Read data and status at the same time */
  1845. data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
  1846. more_data:
  1847. if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
  1848. DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
  1849. }
  1850. DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
  1851. if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
  1852. IO_MASK(R_SERIAL0_READ, par_err) |
  1853. IO_MASK(R_SERIAL0_READ, overrun) )) {
  1854. /* An error */
  1855. info->last_rx_active_usec = GET_JIFFIES_USEC();
  1856. info->last_rx_active = jiffies;
  1857. DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
  1858. DLOG_INT_TRIG(
  1859. if (!log_int_trig1_pos) {
  1860. log_int_trig1_pos = log_int_pos;
  1861. log_int(rdpc(), 0, 0);
  1862. }
  1863. );
  1864. if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
  1865. (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
  1866. /* Most likely a break, but we get interrupts over and
  1867. * over again.
  1868. */
  1869. if (!info->break_detected_cnt) {
  1870. DEBUG_LOG(info->line, "#BRK start\n", 0);
  1871. }
  1872. if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
  1873. /* The RX pin is high now, so the break
  1874. * must be over, but....
  1875. * we can't really know if we will get another
  1876. * last byte ending the break or not.
  1877. * And we don't know if the byte (if any) will
  1878. * have an error or look valid.
  1879. */
  1880. DEBUG_LOG(info->line, "# BL BRK\n", 0);
  1881. info->errorcode = ERRCODE_INSERT_BREAK;
  1882. }
  1883. info->break_detected_cnt++;
  1884. } else {
  1885. /* The error does not look like a break, but could be
  1886. * the end of one
  1887. */
  1888. if (info->break_detected_cnt) {
  1889. DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
  1890. info->errorcode = ERRCODE_INSERT_BREAK;
  1891. } else {
  1892. unsigned char data = IO_EXTRACT(R_SERIAL0_READ,
  1893. data_in, data_read);
  1894. char flag = TTY_NORMAL;
  1895. if (info->errorcode == ERRCODE_INSERT_BREAK) {
  1896. tty_insert_flip_char(&info->port, 0, flag);
  1897. info->icount.rx++;
  1898. }
  1899. if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
  1900. info->icount.parity++;
  1901. flag = TTY_PARITY;
  1902. } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
  1903. info->icount.overrun++;
  1904. flag = TTY_OVERRUN;
  1905. } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
  1906. info->icount.frame++;
  1907. flag = TTY_FRAME;
  1908. }
  1909. tty_insert_flip_char(&info->port, data, flag);
  1910. info->errorcode = 0;
  1911. }
  1912. info->break_detected_cnt = 0;
  1913. }
  1914. } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
  1915. /* No error */
  1916. DLOG_INT_TRIG(
  1917. if (!log_int_trig1_pos) {
  1918. if (log_int_pos >= log_int_size) {
  1919. log_int_pos = 0;
  1920. }
  1921. log_int_trig0_pos = log_int_pos;
  1922. log_int(rdpc(), 0, 0);
  1923. }
  1924. );
  1925. tty_insert_flip_char(&info->port,
  1926. IO_EXTRACT(R_SERIAL0_READ, data_in, data_read),
  1927. TTY_NORMAL);
  1928. } else {
  1929. DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
  1930. }
  1931. info->icount.rx++;
  1932. data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
  1933. if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
  1934. DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
  1935. goto more_data;
  1936. }
  1937. tty_flip_buffer_push(&info->port);
  1938. }
  1939. static void handle_ser_rx_interrupt(struct e100_serial *info)
  1940. {
  1941. unsigned char rstat;
  1942. #ifdef SERIAL_DEBUG_INTR
  1943. printk("Interrupt from serport %d\n", i);
  1944. #endif
  1945. /* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
  1946. if (!info->uses_dma_in) {
  1947. handle_ser_rx_interrupt_no_dma(info);
  1948. return;
  1949. }
  1950. /* DMA is used */
  1951. rstat = info->ioport[REG_STATUS];
  1952. if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
  1953. DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
  1954. }
  1955. if (rstat & SER_ERROR_MASK) {
  1956. unsigned char data;
  1957. info->last_rx_active_usec = GET_JIFFIES_USEC();
  1958. info->last_rx_active = jiffies;
  1959. /* If we got an error, we must reset it by reading the
  1960. * data_in field
  1961. */
  1962. data = info->ioport[REG_DATA];
  1963. DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
  1964. DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
  1965. if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
  1966. /* Most likely a break, but we get interrupts over and
  1967. * over again.
  1968. */
  1969. if (!info->break_detected_cnt) {
  1970. DEBUG_LOG(info->line, "#BRK start\n", 0);
  1971. }
  1972. if (rstat & SER_RXD_MASK) {
  1973. /* The RX pin is high now, so the break
  1974. * must be over, but....
  1975. * we can't really know if we will get another
  1976. * last byte ending the break or not.
  1977. * And we don't know if the byte (if any) will
  1978. * have an error or look valid.
  1979. */
  1980. DEBUG_LOG(info->line, "# BL BRK\n", 0);
  1981. info->errorcode = ERRCODE_INSERT_BREAK;
  1982. }
  1983. info->break_detected_cnt++;
  1984. } else {
  1985. /* The error does not look like a break, but could be
  1986. * the end of one
  1987. */
  1988. if (info->break_detected_cnt) {
  1989. DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
  1990. info->errorcode = ERRCODE_INSERT_BREAK;
  1991. } else {
  1992. if (info->errorcode == ERRCODE_INSERT_BREAK) {
  1993. info->icount.brk++;
  1994. add_char_and_flag(info, '\0', TTY_BREAK);
  1995. }
  1996. if (rstat & SER_PAR_ERR_MASK) {
  1997. info->icount.parity++;
  1998. add_char_and_flag(info, data, TTY_PARITY);
  1999. } else if (rstat & SER_OVERRUN_MASK) {
  2000. info->icount.overrun++;
  2001. add_char_and_flag(info, data, TTY_OVERRUN);
  2002. } else if (rstat & SER_FRAMING_ERR_MASK) {
  2003. info->icount.frame++;
  2004. add_char_and_flag(info, data, TTY_FRAME);
  2005. }
  2006. info->errorcode = 0;
  2007. }
  2008. info->break_detected_cnt = 0;
  2009. DEBUG_LOG(info->line, "#iERR s d %04X\n",
  2010. ((rstat & SER_ERROR_MASK) << 8) | data);
  2011. }
  2012. } else { /* It was a valid byte, now let the DMA do the rest */
  2013. unsigned long curr_time_u = GET_JIFFIES_USEC();
  2014. unsigned long curr_time = jiffies;
  2015. if (info->break_detected_cnt) {
  2016. /* Detect if this character is a new valid char or the
  2017. * last char in a break sequence: If LSBits are 0 and
  2018. * MSBits are high AND the time is close to the
  2019. * previous interrupt we should discard it.
  2020. */
  2021. long elapsed_usec =
  2022. (curr_time - info->last_rx_active) * (1000000/HZ) +
  2023. curr_time_u - info->last_rx_active_usec;
  2024. if (elapsed_usec < 2*info->char_time_usec) {
  2025. DEBUG_LOG(info->line, "FBRK %i\n", info->line);
  2026. /* Report as BREAK (error) and let
  2027. * receive_chars_dma() handle it
  2028. */
  2029. info->errorcode = ERRCODE_SET_BREAK;
  2030. } else {
  2031. DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
  2032. }
  2033. DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
  2034. }
  2035. #ifdef SERIAL_DEBUG_INTR
  2036. printk("** OK, disabling ser_interrupts\n");
  2037. #endif
  2038. e100_disable_serial_data_irq(info);
  2039. DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
  2040. info->break_detected_cnt = 0;
  2041. }
  2042. /* Restarting the DMA never hurts */
  2043. *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
  2044. START_FLUSH_FAST_TIMER(info, "ser_int");
  2045. } /* handle_ser_rx_interrupt */
  2046. static void handle_ser_tx_interrupt(struct e100_serial *info)
  2047. {
  2048. unsigned long flags;
  2049. if (info->x_char) {
  2050. unsigned char rstat;
  2051. DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
  2052. local_irq_save(flags);
  2053. rstat = info->ioport[REG_STATUS];
  2054. DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
  2055. info->ioport[REG_TR_DATA] = info->x_char;
  2056. info->icount.tx++;
  2057. info->x_char = 0;
  2058. /* We must enable since it is disabled in ser_interrupt */
  2059. e100_enable_serial_tx_ready_irq(info);
  2060. local_irq_restore(flags);
  2061. return;
  2062. }
  2063. if (info->uses_dma_out) {
  2064. unsigned char rstat;
  2065. int i;
  2066. /* We only use normal tx interrupt when sending x_char */
  2067. DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
  2068. local_irq_save(flags);
  2069. rstat = info->ioport[REG_STATUS];
  2070. DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
  2071. e100_disable_serial_tx_ready_irq(info);
  2072. if (info->port.tty->stopped)
  2073. rs_stop(info->port.tty);
  2074. /* Enable the DMA channel and tell it to continue */
  2075. e100_enable_txdma_channel(info);
  2076. /* Wait 12 cycles before doing the DMA command */
  2077. for(i = 6; i > 0; i--)
  2078. nop();
  2079. *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
  2080. local_irq_restore(flags);
  2081. return;
  2082. }
  2083. /* Normal char-by-char interrupt */
  2084. if (info->xmit.head == info->xmit.tail
  2085. || info->port.tty->stopped) {
  2086. DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n",
  2087. info->port.tty->stopped));
  2088. e100_disable_serial_tx_ready_irq(info);
  2089. info->tr_running = 0;
  2090. return;
  2091. }
  2092. DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
  2093. /* Send a byte, rs485 timing is critical so turn of ints */
  2094. local_irq_save(flags);
  2095. info->ioport[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
  2096. info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
  2097. info->icount.tx++;
  2098. if (info->xmit.head == info->xmit.tail) {
  2099. #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
  2100. if (info->rs485.flags & SER_RS485_ENABLED) {
  2101. /* Set a short timer to toggle RTS */
  2102. start_one_shot_timer(&fast_timers_rs485[info->line],
  2103. rs485_toggle_rts_timer_function,
  2104. (unsigned long)info,
  2105. info->char_time_usec*2,
  2106. "RS-485");
  2107. }
  2108. #endif /* RS485 */
  2109. info->last_tx_active_usec = GET_JIFFIES_USEC();
  2110. info->last_tx_active = jiffies;
  2111. e100_disable_serial_tx_ready_irq(info);
  2112. info->tr_running = 0;
  2113. DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
  2114. } else {
  2115. /* We must enable since it is disabled in ser_interrupt */
  2116. e100_enable_serial_tx_ready_irq(info);
  2117. }
  2118. local_irq_restore(flags);
  2119. if (CIRC_CNT(info->xmit.head,
  2120. info->xmit.tail,
  2121. SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  2122. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  2123. } /* handle_ser_tx_interrupt */
  2124. /* result of time measurements:
  2125. * RX duration 54-60 us when doing something, otherwise 6-9 us
  2126. * ser_int duration: just sending: 8-15 us normally, up to 73 us
  2127. */
  2128. static irqreturn_t
  2129. ser_interrupt(int irq, void *dev_id)
  2130. {
  2131. static volatile int tx_started = 0;
  2132. struct e100_serial *info;
  2133. int i;
  2134. unsigned long flags;
  2135. unsigned long irq_mask1_rd;
  2136. unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
  2137. int handled = 0;
  2138. static volatile unsigned long reentered_ready_mask = 0;
  2139. local_irq_save(flags);
  2140. irq_mask1_rd = *R_IRQ_MASK1_RD;
  2141. /* First handle all rx interrupts with ints disabled */
  2142. info = rs_table;
  2143. irq_mask1_rd &= e100_ser_int_mask;
  2144. for (i = 0; i < NR_PORTS; i++) {
  2145. /* Which line caused the data irq? */
  2146. if (irq_mask1_rd & data_mask) {
  2147. handled = 1;
  2148. handle_ser_rx_interrupt(info);
  2149. }
  2150. info += 1;
  2151. data_mask <<= 2;
  2152. }
  2153. /* Handle tx interrupts with interrupts enabled so we
  2154. * can take care of new data interrupts while transmitting
  2155. * We protect the tx part with the tx_started flag.
  2156. * We disable the tr_ready interrupts we are about to handle and
  2157. * unblock the serial interrupt so new serial interrupts may come.
  2158. *
  2159. * If we get a new interrupt:
  2160. * - it migth be due to synchronous serial ports.
  2161. * - serial irq will be blocked by general irq handler.
  2162. * - async data will be handled above (sync will be ignored).
  2163. * - tx_started flag will prevent us from trying to send again and
  2164. * we will exit fast - no need to unblock serial irq.
  2165. * - Next (sync) serial interrupt handler will be runned with
  2166. * disabled interrupt due to restore_flags() at end of function,
  2167. * so sync handler will not be preempted or reentered.
  2168. */
  2169. if (!tx_started) {
  2170. unsigned long ready_mask;
  2171. unsigned long
  2172. tx_started = 1;
  2173. /* Only the tr_ready interrupts left */
  2174. irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
  2175. IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
  2176. IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
  2177. IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
  2178. while (irq_mask1_rd) {
  2179. /* Disable those we are about to handle */
  2180. *R_IRQ_MASK1_CLR = irq_mask1_rd;
  2181. /* Unblock the serial interrupt */
  2182. *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
  2183. local_irq_enable();
  2184. ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
  2185. info = rs_table;
  2186. for (i = 0; i < NR_PORTS; i++) {
  2187. /* Which line caused the ready irq? */
  2188. if (irq_mask1_rd & ready_mask) {
  2189. handled = 1;
  2190. handle_ser_tx_interrupt(info);
  2191. }
  2192. info += 1;
  2193. ready_mask <<= 2;
  2194. }
  2195. /* handle_ser_tx_interrupt enables tr_ready interrupts */
  2196. local_irq_disable();
  2197. /* Handle reentered TX interrupt */
  2198. irq_mask1_rd = reentered_ready_mask;
  2199. }
  2200. local_irq_disable();
  2201. tx_started = 0;
  2202. } else {
  2203. unsigned long ready_mask;
  2204. ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
  2205. IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
  2206. IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
  2207. IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
  2208. if (ready_mask) {
  2209. reentered_ready_mask |= ready_mask;
  2210. /* Disable those we are about to handle */
  2211. *R_IRQ_MASK1_CLR = ready_mask;
  2212. DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
  2213. }
  2214. }
  2215. local_irq_restore(flags);
  2216. return IRQ_RETVAL(handled);
  2217. } /* ser_interrupt */
  2218. #endif
  2219. /*
  2220. * -------------------------------------------------------------------
  2221. * Here ends the serial interrupt routines.
  2222. * -------------------------------------------------------------------
  2223. */
  2224. /*
  2225. * This routine is used to handle the "bottom half" processing for the
  2226. * serial driver, known also the "software interrupt" processing.
  2227. * This processing is done at the kernel interrupt level, after the
  2228. * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
  2229. * is where time-consuming activities which can not be done in the
  2230. * interrupt driver proper are done; the interrupt driver schedules
  2231. * them using rs_sched_event(), and they get done here.
  2232. */
  2233. static void
  2234. do_softint(struct work_struct *work)
  2235. {
  2236. struct e100_serial *info;
  2237. struct tty_struct *tty;
  2238. info = container_of(work, struct e100_serial, work);
  2239. tty = info->port.tty;
  2240. if (!tty)
  2241. return;
  2242. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
  2243. tty_wakeup(tty);
  2244. }
  2245. static int
  2246. startup(struct e100_serial * info)
  2247. {
  2248. unsigned long flags;
  2249. unsigned long xmit_page;
  2250. int i;
  2251. xmit_page = get_zeroed_page(GFP_KERNEL);
  2252. if (!xmit_page)
  2253. return -ENOMEM;
  2254. local_irq_save(flags);
  2255. /* if it was already initialized, skip this */
  2256. if (tty_port_initialized(&info->port)) {
  2257. local_irq_restore(flags);
  2258. free_page(xmit_page);
  2259. return 0;
  2260. }
  2261. if (info->xmit.buf)
  2262. free_page(xmit_page);
  2263. else
  2264. info->xmit.buf = (unsigned char *) xmit_page;
  2265. #ifdef SERIAL_DEBUG_OPEN
  2266. printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
  2267. #endif
  2268. /*
  2269. * Clear the FIFO buffers and disable them
  2270. * (they will be reenabled in change_speed())
  2271. */
  2272. /*
  2273. * Reset the DMA channels and make sure their interrupts are cleared
  2274. */
  2275. if (info->dma_in_enabled) {
  2276. info->uses_dma_in = 1;
  2277. e100_enable_rxdma_channel(info);
  2278. *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
  2279. /* Wait until reset cycle is complete */
  2280. while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
  2281. IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
  2282. /* Make sure the irqs are cleared */
  2283. *info->iclrintradr =
  2284. IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
  2285. IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
  2286. } else {
  2287. e100_disable_rxdma_channel(info);
  2288. }
  2289. if (info->dma_out_enabled) {
  2290. info->uses_dma_out = 1;
  2291. e100_enable_txdma_channel(info);
  2292. *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
  2293. while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
  2294. IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
  2295. /* Make sure the irqs are cleared */
  2296. *info->oclrintradr =
  2297. IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
  2298. IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
  2299. } else {
  2300. e100_disable_txdma_channel(info);
  2301. }
  2302. if (info->port.tty)
  2303. clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2304. info->xmit.head = info->xmit.tail = 0;
  2305. info->first_recv_buffer = info->last_recv_buffer = NULL;
  2306. info->recv_cnt = info->max_recv_cnt = 0;
  2307. for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
  2308. info->rec_descr[i].buf = 0;
  2309. /*
  2310. * and set the speed and other flags of the serial port
  2311. * this will start the rx/tx as well
  2312. */
  2313. #ifdef SERIAL_HANDLE_EARLY_ERRORS
  2314. e100_enable_serial_data_irq(info);
  2315. #endif
  2316. change_speed(info);
  2317. /* dummy read to reset any serial errors */
  2318. (void)info->ioport[REG_DATA];
  2319. /* enable the interrupts */
  2320. if (info->uses_dma_out)
  2321. e100_enable_txdma_irq(info);
  2322. e100_enable_rx_irq(info);
  2323. info->tr_running = 0; /* to be sure we don't lock up the transmitter */
  2324. /* setup the dma input descriptor and start dma */
  2325. start_receive(info);
  2326. /* for safety, make sure the descriptors last result is 0 bytes written */
  2327. info->tr_descr.sw_len = 0;
  2328. info->tr_descr.hw_len = 0;
  2329. info->tr_descr.status = 0;
  2330. /* enable RTS/DTR last */
  2331. e100_rts(info, 1);
  2332. e100_dtr(info, 1);
  2333. tty_port_set_initialized(&info->port, 1);
  2334. local_irq_restore(flags);
  2335. return 0;
  2336. }
  2337. /*
  2338. * This routine will shutdown a serial port; interrupts are disabled, and
  2339. * DTR is dropped if the hangup on close termio flag is on.
  2340. */
  2341. static void
  2342. shutdown(struct e100_serial * info)
  2343. {
  2344. unsigned long flags;
  2345. struct etrax_dma_descr *descr = info->rec_descr;
  2346. struct etrax_recv_buffer *buffer;
  2347. int i;
  2348. /* shut down the transmitter and receiver */
  2349. DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
  2350. e100_disable_rx(info);
  2351. info->ioport[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
  2352. /* disable interrupts, reset dma channels */
  2353. if (info->uses_dma_in) {
  2354. e100_disable_rxdma_irq(info);
  2355. *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
  2356. info->uses_dma_in = 0;
  2357. } else {
  2358. e100_disable_serial_data_irq(info);
  2359. }
  2360. if (info->uses_dma_out) {
  2361. e100_disable_txdma_irq(info);
  2362. info->tr_running = 0;
  2363. *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
  2364. info->uses_dma_out = 0;
  2365. } else {
  2366. e100_disable_serial_tx_ready_irq(info);
  2367. info->tr_running = 0;
  2368. }
  2369. if (!tty_port_initialized(&info->port))
  2370. return;
  2371. #ifdef SERIAL_DEBUG_OPEN
  2372. printk("Shutting down serial port %d (irq %d)....\n", info->line,
  2373. info->irq);
  2374. #endif
  2375. local_irq_save(flags);
  2376. if (info->xmit.buf) {
  2377. free_page((unsigned long)info->xmit.buf);
  2378. info->xmit.buf = NULL;
  2379. }
  2380. for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
  2381. if (descr[i].buf) {
  2382. buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
  2383. kfree(buffer);
  2384. descr[i].buf = 0;
  2385. }
  2386. if (!info->port.tty || (info->port.tty->termios.c_cflag & HUPCL)) {
  2387. /* hang up DTR and RTS if HUPCL is enabled */
  2388. e100_dtr(info, 0);
  2389. e100_rts(info, 0); /* could check CRTSCTS before doing this */
  2390. }
  2391. if (info->port.tty)
  2392. set_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2393. tty_port_set_initialized(&info->port, 0);
  2394. local_irq_restore(flags);
  2395. }
  2396. /* change baud rate and other assorted parameters */
  2397. static void
  2398. change_speed(struct e100_serial *info)
  2399. {
  2400. unsigned int cflag;
  2401. unsigned long xoff;
  2402. unsigned long flags;
  2403. /* first some safety checks */
  2404. if (!info->port.tty)
  2405. return;
  2406. if (!info->ioport)
  2407. return;
  2408. cflag = info->port.tty->termios.c_cflag;
  2409. /* possibly, the tx/rx should be disabled first to do this safely */
  2410. /* change baud-rate and write it to the hardware */
  2411. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
  2412. /* Special baudrate */
  2413. u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
  2414. unsigned long alt_source =
  2415. IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
  2416. IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
  2417. /* R_ALT_SER_BAUDRATE selects the source */
  2418. DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
  2419. (unsigned long)info->baud_base, info->custom_divisor));
  2420. if (info->baud_base == SERIAL_PRESCALE_BASE) {
  2421. /* 0, 2-65535 (0=65536) */
  2422. u16 divisor = info->custom_divisor;
  2423. /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
  2424. /* baudrate is 3.125MHz/custom_divisor */
  2425. alt_source =
  2426. IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
  2427. IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
  2428. alt_source = 0x11;
  2429. DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
  2430. *R_SERIAL_PRESCALE = divisor;
  2431. info->baud = SERIAL_PRESCALE_BASE/divisor;
  2432. }
  2433. else
  2434. {
  2435. /* Bad baudbase, we don't support using timer0
  2436. * for baudrate.
  2437. */
  2438. printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
  2439. (unsigned long)info->baud_base, info->custom_divisor);
  2440. }
  2441. r_alt_ser_baudrate_shadow &= ~mask;
  2442. r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
  2443. *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
  2444. } else {
  2445. /* Normal baudrate */
  2446. /* Make sure we use normal baudrate */
  2447. u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
  2448. unsigned long alt_source =
  2449. IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
  2450. IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
  2451. r_alt_ser_baudrate_shadow &= ~mask;
  2452. r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
  2453. *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
  2454. info->baud = cflag_to_baud(cflag);
  2455. info->ioport[REG_BAUD] = cflag_to_etrax_baud(cflag);
  2456. }
  2457. /* start with default settings and then fill in changes */
  2458. local_irq_save(flags);
  2459. /* 8 bit, no/even parity */
  2460. info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
  2461. IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
  2462. IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
  2463. /* 8 bit, no/even parity, 1 stop bit, no cts */
  2464. info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
  2465. IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
  2466. IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
  2467. IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
  2468. IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
  2469. if ((cflag & CSIZE) == CS7) {
  2470. /* set 7 bit mode */
  2471. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
  2472. info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
  2473. }
  2474. if (cflag & CSTOPB) {
  2475. /* set 2 stop bit mode */
  2476. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
  2477. }
  2478. if (cflag & PARENB) {
  2479. /* enable parity */
  2480. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
  2481. info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
  2482. }
  2483. if (cflag & CMSPAR) {
  2484. /* enable stick parity, PARODD mean Mark which matches ETRAX */
  2485. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
  2486. info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
  2487. }
  2488. if (cflag & PARODD) {
  2489. /* set odd parity (or Mark if CMSPAR) */
  2490. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
  2491. info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
  2492. }
  2493. if (cflag & CRTSCTS) {
  2494. /* enable automatic CTS handling */
  2495. DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
  2496. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
  2497. }
  2498. /* make sure the tx and rx are enabled */
  2499. info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
  2500. info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
  2501. /* actually write the control regs to the hardware */
  2502. info->ioport[REG_TR_CTRL] = info->tx_ctrl;
  2503. info->ioport[REG_REC_CTRL] = info->rx_ctrl;
  2504. xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->port.tty));
  2505. xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
  2506. if (info->port.tty->termios.c_iflag & IXON ) {
  2507. DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n",
  2508. STOP_CHAR(info->port.tty)));
  2509. xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
  2510. }
  2511. *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
  2512. local_irq_restore(flags);
  2513. update_char_time(info);
  2514. } /* change_speed */
  2515. /* start transmitting chars NOW */
  2516. static void
  2517. rs_flush_chars(struct tty_struct *tty)
  2518. {
  2519. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2520. unsigned long flags;
  2521. if (info->tr_running ||
  2522. info->xmit.head == info->xmit.tail ||
  2523. tty->stopped ||
  2524. !info->xmit.buf)
  2525. return;
  2526. #ifdef SERIAL_DEBUG_FLOW
  2527. printk("rs_flush_chars\n");
  2528. #endif
  2529. /* this protection might not exactly be necessary here */
  2530. local_irq_save(flags);
  2531. start_transmit(info);
  2532. local_irq_restore(flags);
  2533. }
  2534. static int rs_raw_write(struct tty_struct *tty,
  2535. const unsigned char *buf, int count)
  2536. {
  2537. int c, ret = 0;
  2538. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2539. unsigned long flags;
  2540. /* first some sanity checks */
  2541. if (!info->xmit.buf)
  2542. return 0;
  2543. #ifdef SERIAL_DEBUG_DATA
  2544. if (info->line == SERIAL_DEBUG_LINE)
  2545. printk("rs_raw_write (%d), status %d\n",
  2546. count, info->ioport[REG_STATUS]);
  2547. #endif
  2548. local_save_flags(flags);
  2549. DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
  2550. DFLOW(DEBUG_LOG(info->line, "ldisc\n"));
  2551. /* The local_irq_disable/restore_flags pairs below are needed
  2552. * because the DMA interrupt handler moves the info->xmit values.
  2553. * the memcpy needs to be in the critical region unfortunately,
  2554. * because we need to read xmit values, memcpy, write xmit values
  2555. * in one atomic operation... this could perhaps be avoided by
  2556. * more clever design.
  2557. */
  2558. local_irq_disable();
  2559. while (count) {
  2560. c = CIRC_SPACE_TO_END(info->xmit.head,
  2561. info->xmit.tail,
  2562. SERIAL_XMIT_SIZE);
  2563. if (count < c)
  2564. c = count;
  2565. if (c <= 0)
  2566. break;
  2567. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  2568. info->xmit.head = (info->xmit.head + c) &
  2569. (SERIAL_XMIT_SIZE-1);
  2570. buf += c;
  2571. count -= c;
  2572. ret += c;
  2573. }
  2574. local_irq_restore(flags);
  2575. /* enable transmitter if not running, unless the tty is stopped
  2576. * this does not need IRQ protection since if tr_running == 0
  2577. * the IRQ's are not running anyway for this port.
  2578. */
  2579. DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
  2580. if (info->xmit.head != info->xmit.tail &&
  2581. !tty->stopped &&
  2582. !info->tr_running) {
  2583. start_transmit(info);
  2584. }
  2585. return ret;
  2586. } /* raw_raw_write() */
  2587. static int
  2588. rs_write(struct tty_struct *tty,
  2589. const unsigned char *buf, int count)
  2590. {
  2591. #if defined(CONFIG_ETRAX_RS485)
  2592. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2593. if (info->rs485.flags & SER_RS485_ENABLED)
  2594. {
  2595. /* If we are in RS-485 mode, we need to toggle RTS and disable
  2596. * the receiver before initiating a DMA transfer
  2597. */
  2598. #ifdef CONFIG_ETRAX_FAST_TIMER
  2599. /* Abort any started timer */
  2600. fast_timers_rs485[info->line].function = NULL;
  2601. del_fast_timer(&fast_timers_rs485[info->line]);
  2602. #endif
  2603. e100_rts(info, (info->rs485.flags & SER_RS485_RTS_ON_SEND));
  2604. #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
  2605. e100_disable_rx(info);
  2606. e100_enable_rx_irq(info);
  2607. #endif
  2608. if (info->rs485.delay_rts_before_send > 0)
  2609. msleep(info->rs485.delay_rts_before_send);
  2610. }
  2611. #endif /* CONFIG_ETRAX_RS485 */
  2612. count = rs_raw_write(tty, buf, count);
  2613. #if defined(CONFIG_ETRAX_RS485)
  2614. if (info->rs485.flags & SER_RS485_ENABLED)
  2615. {
  2616. unsigned int val;
  2617. /* If we are in RS-485 mode the following has to be done:
  2618. * wait until DMA is ready
  2619. * wait on transmit shift register
  2620. * toggle RTS
  2621. * enable the receiver
  2622. */
  2623. /* Sleep until all sent */
  2624. tty_wait_until_sent(tty, 0);
  2625. #ifdef CONFIG_ETRAX_FAST_TIMER
  2626. /* Now sleep a little more so that shift register is empty */
  2627. schedule_usleep(info->char_time_usec * 2);
  2628. #endif
  2629. /* wait on transmit shift register */
  2630. do{
  2631. get_lsr_info(info, &val);
  2632. }while (!(val & TIOCSER_TEMT));
  2633. e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
  2634. #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
  2635. e100_enable_rx(info);
  2636. e100_enable_rxdma_irq(info);
  2637. #endif
  2638. }
  2639. #endif /* CONFIG_ETRAX_RS485 */
  2640. return count;
  2641. } /* rs_write */
  2642. /* how much space is available in the xmit buffer? */
  2643. static int
  2644. rs_write_room(struct tty_struct *tty)
  2645. {
  2646. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2647. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  2648. }
  2649. /* How many chars are in the xmit buffer?
  2650. * This does not include any chars in the transmitter FIFO.
  2651. * Use wait_until_sent for waiting for FIFO drain.
  2652. */
  2653. static int
  2654. rs_chars_in_buffer(struct tty_struct *tty)
  2655. {
  2656. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2657. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  2658. }
  2659. /* discard everything in the xmit buffer */
  2660. static void
  2661. rs_flush_buffer(struct tty_struct *tty)
  2662. {
  2663. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2664. unsigned long flags;
  2665. local_irq_save(flags);
  2666. info->xmit.head = info->xmit.tail = 0;
  2667. local_irq_restore(flags);
  2668. tty_wakeup(tty);
  2669. }
  2670. /*
  2671. * This function is used to send a high-priority XON/XOFF character to
  2672. * the device
  2673. *
  2674. * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
  2675. * but we do it in handle_ser_tx_interrupt().
  2676. * We disable DMA channel and enable tx ready interrupt and write the
  2677. * character when possible.
  2678. */
  2679. static void rs_send_xchar(struct tty_struct *tty, char ch)
  2680. {
  2681. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2682. unsigned long flags;
  2683. local_irq_save(flags);
  2684. if (info->uses_dma_out) {
  2685. /* Put the DMA on hold and disable the channel */
  2686. *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
  2687. while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
  2688. IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
  2689. e100_disable_txdma_channel(info);
  2690. }
  2691. /* Must make sure transmitter is not stopped before we can transmit */
  2692. if (tty->stopped)
  2693. rs_start(tty);
  2694. /* Enable manual transmit interrupt and send from there */
  2695. DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
  2696. info->x_char = ch;
  2697. e100_enable_serial_tx_ready_irq(info);
  2698. local_irq_restore(flags);
  2699. }
  2700. /*
  2701. * ------------------------------------------------------------
  2702. * rs_throttle()
  2703. *
  2704. * This routine is called by the upper-layer tty layer to signal that
  2705. * incoming characters should be throttled.
  2706. * ------------------------------------------------------------
  2707. */
  2708. static void
  2709. rs_throttle(struct tty_struct * tty)
  2710. {
  2711. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2712. #ifdef SERIAL_DEBUG_THROTTLE
  2713. printk("throttle %s ....\n", tty_name(tty));
  2714. #endif
  2715. DFLOW(DEBUG_LOG(info->line,"rs_throttle\n"));
  2716. /* Do RTS before XOFF since XOFF might take some time */
  2717. if (C_CRTSCTS(tty)) {
  2718. /* Turn off RTS line */
  2719. e100_rts(info, 0);
  2720. }
  2721. if (I_IXOFF(tty))
  2722. rs_send_xchar(tty, STOP_CHAR(tty));
  2723. }
  2724. static void
  2725. rs_unthrottle(struct tty_struct * tty)
  2726. {
  2727. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2728. #ifdef SERIAL_DEBUG_THROTTLE
  2729. printk("unthrottle %s ....\n", tty_name(tty));
  2730. #endif
  2731. DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc\n"));
  2732. DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
  2733. /* Do RTS before XOFF since XOFF might take some time */
  2734. if (C_CRTSCTS(tty)) {
  2735. /* Assert RTS line */
  2736. e100_rts(info, 1);
  2737. }
  2738. if (I_IXOFF(tty)) {
  2739. if (info->x_char)
  2740. info->x_char = 0;
  2741. else
  2742. rs_send_xchar(tty, START_CHAR(tty));
  2743. }
  2744. }
  2745. /*
  2746. * ------------------------------------------------------------
  2747. * rs_ioctl() and friends
  2748. * ------------------------------------------------------------
  2749. */
  2750. static int
  2751. get_serial_info(struct e100_serial * info,
  2752. struct serial_struct * retinfo)
  2753. {
  2754. struct serial_struct tmp;
  2755. /* this is all probably wrong, there are a lot of fields
  2756. * here that we don't have in e100_serial and maybe we
  2757. * should set them to something else than 0.
  2758. */
  2759. memset(&tmp, 0, sizeof(tmp));
  2760. tmp.type = info->type;
  2761. tmp.line = info->line;
  2762. tmp.port = (int)info->ioport;
  2763. tmp.irq = info->irq;
  2764. tmp.flags = info->port.flags;
  2765. tmp.baud_base = info->baud_base;
  2766. tmp.close_delay = info->port.close_delay;
  2767. tmp.closing_wait = info->port.closing_wait;
  2768. tmp.custom_divisor = info->custom_divisor;
  2769. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2770. return -EFAULT;
  2771. return 0;
  2772. }
  2773. static int
  2774. set_serial_info(struct e100_serial *info,
  2775. struct serial_struct *new_info)
  2776. {
  2777. struct serial_struct new_serial;
  2778. struct e100_serial old_info;
  2779. int retval = 0;
  2780. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2781. return -EFAULT;
  2782. old_info = *info;
  2783. if (!capable(CAP_SYS_ADMIN)) {
  2784. if ((new_serial.type != info->type) ||
  2785. (new_serial.close_delay != info->port.close_delay) ||
  2786. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  2787. (info->port.flags & ~ASYNC_USR_MASK)))
  2788. return -EPERM;
  2789. info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
  2790. (new_serial.flags & ASYNC_USR_MASK));
  2791. goto check_and_exit;
  2792. }
  2793. if (info->port.count > 1)
  2794. return -EBUSY;
  2795. /*
  2796. * OK, past this point, all the error checking has been done.
  2797. * At this point, we start making changes.....
  2798. */
  2799. info->baud_base = new_serial.baud_base;
  2800. info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
  2801. (new_serial.flags & ASYNC_FLAGS));
  2802. info->custom_divisor = new_serial.custom_divisor;
  2803. info->type = new_serial.type;
  2804. info->port.close_delay = new_serial.close_delay;
  2805. info->port.closing_wait = new_serial.closing_wait;
  2806. info->port.low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2807. check_and_exit:
  2808. if (tty_port_initialized(&info->port))
  2809. change_speed(info);
  2810. else
  2811. retval = startup(info);
  2812. return retval;
  2813. }
  2814. /*
  2815. * get_lsr_info - get line status register info
  2816. *
  2817. * Purpose: Let user call ioctl() to get info when the UART physically
  2818. * is emptied. On bus types like RS485, the transmitter must
  2819. * release the bus after transmitting. This must be done when
  2820. * the transmit shift register is empty, not be done when the
  2821. * transmit holding register is empty. This functionality
  2822. * allows an RS485 driver to be written in user space.
  2823. */
  2824. static int
  2825. get_lsr_info(struct e100_serial * info, unsigned int *value)
  2826. {
  2827. unsigned int result = TIOCSER_TEMT;
  2828. unsigned long curr_time = jiffies;
  2829. unsigned long curr_time_usec = GET_JIFFIES_USEC();
  2830. unsigned long elapsed_usec =
  2831. (curr_time - info->last_tx_active) * 1000000/HZ +
  2832. curr_time_usec - info->last_tx_active_usec;
  2833. if (info->xmit.head != info->xmit.tail ||
  2834. elapsed_usec < 2*info->char_time_usec) {
  2835. result = 0;
  2836. }
  2837. if (copy_to_user(value, &result, sizeof(int)))
  2838. return -EFAULT;
  2839. return 0;
  2840. }
  2841. #ifdef SERIAL_DEBUG_IO
  2842. struct state_str
  2843. {
  2844. int state;
  2845. const char *str;
  2846. };
  2847. const struct state_str control_state_str[] = {
  2848. {TIOCM_DTR, "DTR" },
  2849. {TIOCM_RTS, "RTS"},
  2850. {TIOCM_ST, "ST?" },
  2851. {TIOCM_SR, "SR?" },
  2852. {TIOCM_CTS, "CTS" },
  2853. {TIOCM_CD, "CD" },
  2854. {TIOCM_RI, "RI" },
  2855. {TIOCM_DSR, "DSR" },
  2856. {0, NULL }
  2857. };
  2858. char *get_control_state_str(int MLines, char *s)
  2859. {
  2860. int i = 0;
  2861. s[0]='\0';
  2862. while (control_state_str[i].str != NULL) {
  2863. if (MLines & control_state_str[i].state) {
  2864. if (s[0] != '\0') {
  2865. strcat(s, ", ");
  2866. }
  2867. strcat(s, control_state_str[i].str);
  2868. }
  2869. i++;
  2870. }
  2871. return s;
  2872. }
  2873. #endif
  2874. static int
  2875. rs_break(struct tty_struct *tty, int break_state)
  2876. {
  2877. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2878. unsigned long flags;
  2879. if (!info->ioport)
  2880. return -EIO;
  2881. local_irq_save(flags);
  2882. if (break_state == -1) {
  2883. /* Go to manual mode and set the txd pin to 0 */
  2884. /* Clear bit 7 (txd) and 6 (tr_enable) */
  2885. info->tx_ctrl &= 0x3F;
  2886. } else {
  2887. /* Set bit 7 (txd) and 6 (tr_enable) */
  2888. info->tx_ctrl |= (0x80 | 0x40);
  2889. }
  2890. info->ioport[REG_TR_CTRL] = info->tx_ctrl;
  2891. local_irq_restore(flags);
  2892. return 0;
  2893. }
  2894. static int
  2895. rs_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
  2896. {
  2897. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2898. unsigned long flags;
  2899. local_irq_save(flags);
  2900. if (clear & TIOCM_RTS)
  2901. e100_rts(info, 0);
  2902. if (clear & TIOCM_DTR)
  2903. e100_dtr(info, 0);
  2904. /* Handle FEMALE behaviour */
  2905. if (clear & TIOCM_RI)
  2906. e100_ri_out(info, 0);
  2907. if (clear & TIOCM_CD)
  2908. e100_cd_out(info, 0);
  2909. if (set & TIOCM_RTS)
  2910. e100_rts(info, 1);
  2911. if (set & TIOCM_DTR)
  2912. e100_dtr(info, 1);
  2913. /* Handle FEMALE behaviour */
  2914. if (set & TIOCM_RI)
  2915. e100_ri_out(info, 1);
  2916. if (set & TIOCM_CD)
  2917. e100_cd_out(info, 1);
  2918. local_irq_restore(flags);
  2919. return 0;
  2920. }
  2921. static int
  2922. rs_tiocmget(struct tty_struct *tty)
  2923. {
  2924. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  2925. unsigned int result;
  2926. unsigned long flags;
  2927. local_irq_save(flags);
  2928. result =
  2929. (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
  2930. | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
  2931. | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
  2932. | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
  2933. | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
  2934. | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
  2935. local_irq_restore(flags);
  2936. #ifdef SERIAL_DEBUG_IO
  2937. printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
  2938. info->line, result, result);
  2939. {
  2940. char s[100];
  2941. get_control_state_str(result, s);
  2942. printk(KERN_DEBUG "state: %s\n", s);
  2943. }
  2944. #endif
  2945. return result;
  2946. }
  2947. static int
  2948. rs_ioctl(struct tty_struct *tty,
  2949. unsigned int cmd, unsigned long arg)
  2950. {
  2951. struct e100_serial * info = (struct e100_serial *)tty->driver_data;
  2952. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  2953. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  2954. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  2955. if (tty_io_error(tty))
  2956. return -EIO;
  2957. }
  2958. switch (cmd) {
  2959. case TIOCGSERIAL:
  2960. return get_serial_info(info,
  2961. (struct serial_struct *) arg);
  2962. case TIOCSSERIAL:
  2963. return set_serial_info(info,
  2964. (struct serial_struct *) arg);
  2965. case TIOCSERGETLSR: /* Get line status register */
  2966. return get_lsr_info(info, (unsigned int *) arg);
  2967. case TIOCSERGSTRUCT:
  2968. if (copy_to_user((struct e100_serial *) arg,
  2969. info, sizeof(struct e100_serial)))
  2970. return -EFAULT;
  2971. return 0;
  2972. #if defined(CONFIG_ETRAX_RS485)
  2973. case TIOCSERSETRS485:
  2974. {
  2975. /* In this ioctl we still use the old structure
  2976. * rs485_control for backward compatibility
  2977. * (if we use serial_rs485, then old user-level code
  2978. * wouldn't work anymore...).
  2979. * The use of this ioctl is deprecated: use TIOCSRS485
  2980. * instead.*/
  2981. struct rs485_control rs485ctrl;
  2982. struct serial_rs485 rs485data;
  2983. printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
  2984. if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
  2985. sizeof(rs485ctrl)))
  2986. return -EFAULT;
  2987. rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
  2988. rs485data.flags = 0;
  2989. if (rs485ctrl.enabled)
  2990. rs485data.flags |= SER_RS485_ENABLED;
  2991. else
  2992. rs485data.flags &= ~(SER_RS485_ENABLED);
  2993. if (rs485ctrl.rts_on_send)
  2994. rs485data.flags |= SER_RS485_RTS_ON_SEND;
  2995. else
  2996. rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
  2997. if (rs485ctrl.rts_after_sent)
  2998. rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
  2999. else
  3000. rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
  3001. return e100_enable_rs485(tty, &rs485data);
  3002. }
  3003. case TIOCSRS485:
  3004. {
  3005. /* This is the new version of TIOCSRS485, with new
  3006. * data structure serial_rs485 */
  3007. struct serial_rs485 rs485data;
  3008. if (copy_from_user(&rs485data, (struct rs485_control *)arg,
  3009. sizeof(rs485data)))
  3010. return -EFAULT;
  3011. return e100_enable_rs485(tty, &rs485data);
  3012. }
  3013. case TIOCGRS485:
  3014. {
  3015. struct serial_rs485 *rs485data =
  3016. &(((struct e100_serial *)tty->driver_data)->rs485);
  3017. /* This is the ioctl to get RS485 data from user-space */
  3018. if (copy_to_user((struct serial_rs485 *) arg,
  3019. rs485data,
  3020. sizeof(struct serial_rs485)))
  3021. return -EFAULT;
  3022. break;
  3023. }
  3024. case TIOCSERWRRS485:
  3025. {
  3026. struct rs485_write rs485wr;
  3027. if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
  3028. sizeof(rs485wr)))
  3029. return -EFAULT;
  3030. return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
  3031. }
  3032. #endif
  3033. default:
  3034. return -ENOIOCTLCMD;
  3035. }
  3036. return 0;
  3037. }
  3038. static void
  3039. rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  3040. {
  3041. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  3042. change_speed(info);
  3043. /* Handle turning off CRTSCTS */
  3044. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty))
  3045. rs_start(tty);
  3046. }
  3047. /*
  3048. * ------------------------------------------------------------
  3049. * rs_close()
  3050. *
  3051. * This routine is called when the serial port gets closed. First, we
  3052. * wait for the last remaining data to be sent. Then, we unlink its
  3053. * S structure from the interrupt chain if necessary, and we free
  3054. * that IRQ if nothing is left in the chain.
  3055. * ------------------------------------------------------------
  3056. */
  3057. static void
  3058. rs_close(struct tty_struct *tty, struct file * filp)
  3059. {
  3060. struct e100_serial * info = (struct e100_serial *)tty->driver_data;
  3061. unsigned long flags;
  3062. if (!info)
  3063. return;
  3064. /* interrupts are disabled for this entire function */
  3065. local_irq_save(flags);
  3066. if (tty_hung_up_p(filp)) {
  3067. local_irq_restore(flags);
  3068. return;
  3069. }
  3070. #ifdef SERIAL_DEBUG_OPEN
  3071. printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
  3072. info->line, info->count);
  3073. #endif
  3074. if ((tty->count == 1) && (info->port.count != 1)) {
  3075. /*
  3076. * Uh, oh. tty->count is 1, which means that the tty
  3077. * structure will be freed. Info->count should always
  3078. * be one in these conditions. If it's greater than
  3079. * one, we've got real problems, since it means the
  3080. * serial port won't be shutdown.
  3081. */
  3082. printk(KERN_ERR
  3083. "rs_close: bad serial port count; tty->count is 1, "
  3084. "info->count is %d\n", info->port.count);
  3085. info->port.count = 1;
  3086. }
  3087. if (--info->port.count < 0) {
  3088. printk(KERN_ERR "rs_close: bad serial port count for ttyS%d: %d\n",
  3089. info->line, info->port.count);
  3090. info->port.count = 0;
  3091. }
  3092. if (info->port.count) {
  3093. local_irq_restore(flags);
  3094. return;
  3095. }
  3096. /*
  3097. * Now we wait for the transmit buffer to clear; and we notify
  3098. * the line discipline to only process XON/XOFF characters.
  3099. */
  3100. tty->closing = 1;
  3101. if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
  3102. tty_wait_until_sent(tty, info->port.closing_wait);
  3103. /*
  3104. * At this point we stop accepting input. To do this, we
  3105. * disable the serial receiver and the DMA receive interrupt.
  3106. */
  3107. #ifdef SERIAL_HANDLE_EARLY_ERRORS
  3108. e100_disable_serial_data_irq(info);
  3109. #endif
  3110. e100_disable_rx(info);
  3111. e100_disable_rx_irq(info);
  3112. if (tty_port_initialized(&info->port)) {
  3113. /*
  3114. * Before we drop DTR, make sure the UART transmitter
  3115. * has completely drained; this is especially
  3116. * important as we have a transmit FIFO!
  3117. */
  3118. rs_wait_until_sent(tty, HZ);
  3119. }
  3120. shutdown(info);
  3121. rs_flush_buffer(tty);
  3122. tty_ldisc_flush(tty);
  3123. tty->closing = 0;
  3124. info->event = 0;
  3125. info->port.tty = NULL;
  3126. if (info->port.blocked_open) {
  3127. if (info->port.close_delay)
  3128. schedule_timeout_interruptible(info->port.close_delay);
  3129. wake_up_interruptible(&info->port.open_wait);
  3130. }
  3131. local_irq_restore(flags);
  3132. tty_port_set_active(&info->port, 0);
  3133. /* port closed */
  3134. #if defined(CONFIG_ETRAX_RS485)
  3135. if (info->rs485.flags & SER_RS485_ENABLED) {
  3136. info->rs485.flags &= ~(SER_RS485_ENABLED);
  3137. #if defined(CONFIG_ETRAX_RS485_ON_PA)
  3138. *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
  3139. #endif
  3140. }
  3141. #endif
  3142. /*
  3143. * Release any allocated DMA irq's.
  3144. */
  3145. if (info->dma_in_enabled) {
  3146. free_irq(info->dma_in_irq_nbr, info);
  3147. cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
  3148. info->uses_dma_in = 0;
  3149. #ifdef SERIAL_DEBUG_OPEN
  3150. printk(KERN_DEBUG "DMA irq '%s' freed\n",
  3151. info->dma_in_irq_description);
  3152. #endif
  3153. }
  3154. if (info->dma_out_enabled) {
  3155. free_irq(info->dma_out_irq_nbr, info);
  3156. cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
  3157. info->uses_dma_out = 0;
  3158. #ifdef SERIAL_DEBUG_OPEN
  3159. printk(KERN_DEBUG "DMA irq '%s' freed\n",
  3160. info->dma_out_irq_description);
  3161. #endif
  3162. }
  3163. }
  3164. /*
  3165. * rs_wait_until_sent() --- wait until the transmitter is empty
  3166. */
  3167. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  3168. {
  3169. unsigned long orig_jiffies;
  3170. struct e100_serial *info = (struct e100_serial *)tty->driver_data;
  3171. unsigned long curr_time = jiffies;
  3172. unsigned long curr_time_usec = GET_JIFFIES_USEC();
  3173. long elapsed_usec =
  3174. (curr_time - info->last_tx_active) * (1000000/HZ) +
  3175. curr_time_usec - info->last_tx_active_usec;
  3176. /*
  3177. * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
  3178. * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
  3179. */
  3180. orig_jiffies = jiffies;
  3181. while (info->xmit.head != info->xmit.tail || /* More in send queue */
  3182. (*info->ostatusadr & 0x007f) || /* more in FIFO */
  3183. (elapsed_usec < 2*info->char_time_usec)) {
  3184. schedule_timeout_interruptible(1);
  3185. if (signal_pending(current))
  3186. break;
  3187. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  3188. break;
  3189. curr_time = jiffies;
  3190. curr_time_usec = GET_JIFFIES_USEC();
  3191. elapsed_usec =
  3192. (curr_time - info->last_tx_active) * (1000000/HZ) +
  3193. curr_time_usec - info->last_tx_active_usec;
  3194. }
  3195. set_current_state(TASK_RUNNING);
  3196. }
  3197. /*
  3198. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  3199. */
  3200. void
  3201. rs_hangup(struct tty_struct *tty)
  3202. {
  3203. struct e100_serial * info = (struct e100_serial *)tty->driver_data;
  3204. rs_flush_buffer(tty);
  3205. shutdown(info);
  3206. info->event = 0;
  3207. info->port.count = 0;
  3208. tty_port_set_active(&info->port, 0);
  3209. info->port.tty = NULL;
  3210. wake_up_interruptible(&info->port.open_wait);
  3211. }
  3212. /*
  3213. * ------------------------------------------------------------
  3214. * rs_open() and friends
  3215. * ------------------------------------------------------------
  3216. */
  3217. static int
  3218. block_til_ready(struct tty_struct *tty, struct file * filp,
  3219. struct e100_serial *info)
  3220. {
  3221. DECLARE_WAITQUEUE(wait, current);
  3222. unsigned long flags;
  3223. int retval;
  3224. int do_clocal = 0;
  3225. /*
  3226. * If non-blocking mode is set, or the port is not enabled,
  3227. * then make the check up front and then exit.
  3228. */
  3229. if ((filp->f_flags & O_NONBLOCK) || tty_io_error(tty)) {
  3230. tty_port_set_active(&info->port, 1);
  3231. return 0;
  3232. }
  3233. if (C_CLOCAL(tty))
  3234. do_clocal = 1;
  3235. /*
  3236. * Block waiting for the carrier detect and the line to become
  3237. * free (i.e., not in use by the callout). While we are in
  3238. * this loop, info->port.count is dropped by one, so that
  3239. * rs_close() knows when to free things. We restore it upon
  3240. * exit, either normal or abnormal.
  3241. */
  3242. retval = 0;
  3243. add_wait_queue(&info->port.open_wait, &wait);
  3244. #ifdef SERIAL_DEBUG_OPEN
  3245. printk("block_til_ready before block: ttyS%d, count = %d\n",
  3246. info->line, info->port.count);
  3247. #endif
  3248. local_irq_save(flags);
  3249. info->port.count--;
  3250. local_irq_restore(flags);
  3251. info->port.blocked_open++;
  3252. while (1) {
  3253. local_irq_save(flags);
  3254. /* assert RTS and DTR */
  3255. e100_rts(info, 1);
  3256. e100_dtr(info, 1);
  3257. local_irq_restore(flags);
  3258. set_current_state(TASK_INTERRUPTIBLE);
  3259. if (tty_hung_up_p(filp) || !tty_port_initialized(&info->port)) {
  3260. #ifdef SERIAL_DO_RESTART
  3261. if (info->port.flags & ASYNC_HUP_NOTIFY)
  3262. retval = -EAGAIN;
  3263. else
  3264. retval = -ERESTARTSYS;
  3265. #else
  3266. retval = -EAGAIN;
  3267. #endif
  3268. break;
  3269. }
  3270. if (do_clocal)
  3271. /* && (do_clocal || DCD_IS_ASSERTED) */
  3272. break;
  3273. if (signal_pending(current)) {
  3274. retval = -ERESTARTSYS;
  3275. break;
  3276. }
  3277. #ifdef SERIAL_DEBUG_OPEN
  3278. printk("block_til_ready blocking: ttyS%d, count = %d\n",
  3279. info->line, info->port.count);
  3280. #endif
  3281. tty_unlock(tty);
  3282. schedule();
  3283. tty_lock(tty);
  3284. }
  3285. set_current_state(TASK_RUNNING);
  3286. remove_wait_queue(&info->port.open_wait, &wait);
  3287. if (!tty_hung_up_p(filp))
  3288. info->port.count++;
  3289. info->port.blocked_open--;
  3290. #ifdef SERIAL_DEBUG_OPEN
  3291. printk("block_til_ready after blocking: ttyS%d, count = %d\n",
  3292. info->line, info->port.count);
  3293. #endif
  3294. if (retval)
  3295. return retval;
  3296. tty_port_set_active(&info->port, 1);
  3297. return 0;
  3298. }
  3299. static void
  3300. deinit_port(struct e100_serial *info)
  3301. {
  3302. if (info->dma_out_enabled) {
  3303. cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
  3304. free_irq(info->dma_out_irq_nbr, info);
  3305. }
  3306. if (info->dma_in_enabled) {
  3307. cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
  3308. free_irq(info->dma_in_irq_nbr, info);
  3309. }
  3310. }
  3311. /*
  3312. * This routine is called whenever a serial port is opened.
  3313. * It performs the serial-specific initialization for the tty structure.
  3314. */
  3315. static int
  3316. rs_open(struct tty_struct *tty, struct file * filp)
  3317. {
  3318. struct e100_serial *info;
  3319. int retval;
  3320. int allocated_resources = 0;
  3321. info = rs_table + tty->index;
  3322. if (!info->enabled)
  3323. return -ENODEV;
  3324. #ifdef SERIAL_DEBUG_OPEN
  3325. printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
  3326. info->port.count);
  3327. #endif
  3328. info->port.count++;
  3329. tty->driver_data = info;
  3330. info->port.tty = tty;
  3331. info->port.low_latency = !!(info->port.flags & ASYNC_LOW_LATENCY);
  3332. /*
  3333. * If DMA is enabled try to allocate the irq's.
  3334. */
  3335. if (info->port.count == 1) {
  3336. allocated_resources = 1;
  3337. if (info->dma_in_enabled) {
  3338. if (request_irq(info->dma_in_irq_nbr,
  3339. rec_interrupt,
  3340. info->dma_in_irq_flags,
  3341. info->dma_in_irq_description,
  3342. info)) {
  3343. printk(KERN_WARNING "DMA irq '%s' busy; "
  3344. "falling back to non-DMA mode\n",
  3345. info->dma_in_irq_description);
  3346. /* Make sure we never try to use DMA in */
  3347. /* for the port again. */
  3348. info->dma_in_enabled = 0;
  3349. } else if (cris_request_dma(info->dma_in_nbr,
  3350. info->dma_in_irq_description,
  3351. DMA_VERBOSE_ON_ERROR,
  3352. info->dma_owner)) {
  3353. free_irq(info->dma_in_irq_nbr, info);
  3354. printk(KERN_WARNING "DMA '%s' busy; "
  3355. "falling back to non-DMA mode\n",
  3356. info->dma_in_irq_description);
  3357. /* Make sure we never try to use DMA in */
  3358. /* for the port again. */
  3359. info->dma_in_enabled = 0;
  3360. }
  3361. #ifdef SERIAL_DEBUG_OPEN
  3362. else
  3363. printk(KERN_DEBUG "DMA irq '%s' allocated\n",
  3364. info->dma_in_irq_description);
  3365. #endif
  3366. }
  3367. if (info->dma_out_enabled) {
  3368. if (request_irq(info->dma_out_irq_nbr,
  3369. tr_interrupt,
  3370. info->dma_out_irq_flags,
  3371. info->dma_out_irq_description,
  3372. info)) {
  3373. printk(KERN_WARNING "DMA irq '%s' busy; "
  3374. "falling back to non-DMA mode\n",
  3375. info->dma_out_irq_description);
  3376. /* Make sure we never try to use DMA out */
  3377. /* for the port again. */
  3378. info->dma_out_enabled = 0;
  3379. } else if (cris_request_dma(info->dma_out_nbr,
  3380. info->dma_out_irq_description,
  3381. DMA_VERBOSE_ON_ERROR,
  3382. info->dma_owner)) {
  3383. free_irq(info->dma_out_irq_nbr, info);
  3384. printk(KERN_WARNING "DMA '%s' busy; "
  3385. "falling back to non-DMA mode\n",
  3386. info->dma_out_irq_description);
  3387. /* Make sure we never try to use DMA out */
  3388. /* for the port again. */
  3389. info->dma_out_enabled = 0;
  3390. }
  3391. #ifdef SERIAL_DEBUG_OPEN
  3392. else
  3393. printk(KERN_DEBUG "DMA irq '%s' allocated\n",
  3394. info->dma_out_irq_description);
  3395. #endif
  3396. }
  3397. }
  3398. /*
  3399. * Start up the serial port
  3400. */
  3401. retval = startup(info);
  3402. if (retval) {
  3403. if (allocated_resources)
  3404. deinit_port(info);
  3405. /* FIXME Decrease count info->port.count here too? */
  3406. return retval;
  3407. }
  3408. retval = block_til_ready(tty, filp, info);
  3409. if (retval) {
  3410. #ifdef SERIAL_DEBUG_OPEN
  3411. printk("rs_open returning after block_til_ready with %d\n",
  3412. retval);
  3413. #endif
  3414. if (allocated_resources)
  3415. deinit_port(info);
  3416. return retval;
  3417. }
  3418. #ifdef SERIAL_DEBUG_OPEN
  3419. printk("rs_open ttyS%d successful...\n", info->line);
  3420. #endif
  3421. DLOG_INT_TRIG( log_int_pos = 0);
  3422. DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
  3423. info->icount.rx = 0;
  3424. } );
  3425. return 0;
  3426. }
  3427. #ifdef CONFIG_PROC_FS
  3428. /*
  3429. * /proc fs routines....
  3430. */
  3431. static void seq_line_info(struct seq_file *m, struct e100_serial *info)
  3432. {
  3433. unsigned long tmp;
  3434. seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
  3435. info->line, (unsigned long)info->ioport, info->irq);
  3436. if (!info->ioport || (info->type == PORT_UNKNOWN)) {
  3437. seq_printf(m, "\n");
  3438. return;
  3439. }
  3440. seq_printf(m, " baud:%d", info->baud);
  3441. seq_printf(m, " tx:%lu rx:%lu",
  3442. (unsigned long)info->icount.tx,
  3443. (unsigned long)info->icount.rx);
  3444. tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  3445. if (tmp)
  3446. seq_printf(m, " tx_pend:%lu/%lu",
  3447. (unsigned long)tmp,
  3448. (unsigned long)SERIAL_XMIT_SIZE);
  3449. seq_printf(m, " rx_pend:%lu/%lu",
  3450. (unsigned long)info->recv_cnt,
  3451. (unsigned long)info->max_recv_cnt);
  3452. #if 1
  3453. if (info->port.tty) {
  3454. if (info->port.tty->stopped)
  3455. seq_printf(m, " stopped:%i",
  3456. (int)info->port.tty->stopped);
  3457. }
  3458. {
  3459. unsigned char rstat = info->ioport[REG_STATUS];
  3460. if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
  3461. seq_printf(m, " xoff_detect:1");
  3462. }
  3463. #endif
  3464. if (info->icount.frame)
  3465. seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
  3466. if (info->icount.parity)
  3467. seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
  3468. if (info->icount.brk)
  3469. seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
  3470. if (info->icount.overrun)
  3471. seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
  3472. /*
  3473. * Last thing is the RS-232 status lines
  3474. */
  3475. if (!E100_RTS_GET(info))
  3476. seq_puts(m, "|RTS");
  3477. if (!E100_CTS_GET(info))
  3478. seq_puts(m, "|CTS");
  3479. if (!E100_DTR_GET(info))
  3480. seq_puts(m, "|DTR");
  3481. if (!E100_DSR_GET(info))
  3482. seq_puts(m, "|DSR");
  3483. if (!E100_CD_GET(info))
  3484. seq_puts(m, "|CD");
  3485. if (!E100_RI_GET(info))
  3486. seq_puts(m, "|RI");
  3487. seq_puts(m, "\n");
  3488. }
  3489. static int crisv10_proc_show(struct seq_file *m, void *v)
  3490. {
  3491. int i;
  3492. seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
  3493. for (i = 0; i < NR_PORTS; i++) {
  3494. if (!rs_table[i].enabled)
  3495. continue;
  3496. seq_line_info(m, &rs_table[i]);
  3497. }
  3498. #ifdef DEBUG_LOG_INCLUDED
  3499. for (i = 0; i < debug_log_pos; i++) {
  3500. seq_printf(m, "%-4i %lu.%lu ",
  3501. i, debug_log[i].time,
  3502. timer_data_to_ns(debug_log[i].timer_data));
  3503. seq_printf(m, debug_log[i].string, debug_log[i].value);
  3504. }
  3505. seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
  3506. debug_log_pos = 0;
  3507. #endif
  3508. return 0;
  3509. }
  3510. static int crisv10_proc_open(struct inode *inode, struct file *file)
  3511. {
  3512. return single_open(file, crisv10_proc_show, NULL);
  3513. }
  3514. static const struct file_operations crisv10_proc_fops = {
  3515. .owner = THIS_MODULE,
  3516. .open = crisv10_proc_open,
  3517. .read = seq_read,
  3518. .llseek = seq_lseek,
  3519. .release = single_release,
  3520. };
  3521. #endif
  3522. /* Finally, routines used to initialize the serial driver. */
  3523. static void show_serial_version(void)
  3524. {
  3525. printk(KERN_INFO
  3526. "ETRAX 100LX serial-driver %s, "
  3527. "(c) 2000-2004 Axis Communications AB\r\n",
  3528. &serial_version[11]); /* "$Revision: x.yy" */
  3529. }
  3530. /* rs_init inits the driver at boot (using the initcall chain) */
  3531. static const struct tty_operations rs_ops = {
  3532. .open = rs_open,
  3533. .close = rs_close,
  3534. .write = rs_write,
  3535. .flush_chars = rs_flush_chars,
  3536. .write_room = rs_write_room,
  3537. .chars_in_buffer = rs_chars_in_buffer,
  3538. .flush_buffer = rs_flush_buffer,
  3539. .ioctl = rs_ioctl,
  3540. .throttle = rs_throttle,
  3541. .unthrottle = rs_unthrottle,
  3542. .set_termios = rs_set_termios,
  3543. .stop = rs_stop,
  3544. .start = rs_start,
  3545. .hangup = rs_hangup,
  3546. .break_ctl = rs_break,
  3547. .send_xchar = rs_send_xchar,
  3548. .wait_until_sent = rs_wait_until_sent,
  3549. .tiocmget = rs_tiocmget,
  3550. .tiocmset = rs_tiocmset,
  3551. #ifdef CONFIG_PROC_FS
  3552. .proc_fops = &crisv10_proc_fops,
  3553. #endif
  3554. };
  3555. static int __init rs_init(void)
  3556. {
  3557. int i;
  3558. struct e100_serial *info;
  3559. struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
  3560. if (!driver)
  3561. return -ENOMEM;
  3562. show_serial_version();
  3563. /* Setup the timed flush handler system */
  3564. #if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
  3565. setup_timer(&flush_timer, timed_flush_handler, 0);
  3566. mod_timer(&flush_timer, jiffies + 5);
  3567. #endif
  3568. #if defined(CONFIG_ETRAX_RS485)
  3569. #if defined(CONFIG_ETRAX_RS485_ON_PA)
  3570. if (cris_io_interface_allocate_pins(if_serial_0, 'a', rs485_pa_bit,
  3571. rs485_pa_bit)) {
  3572. printk(KERN_ERR "ETRAX100LX serial: Could not allocate "
  3573. "RS485 pin\n");
  3574. put_tty_driver(driver);
  3575. return -EBUSY;
  3576. }
  3577. #endif
  3578. #endif
  3579. /* Initialize the tty_driver structure */
  3580. driver->driver_name = "serial";
  3581. driver->name = "ttyS";
  3582. driver->major = TTY_MAJOR;
  3583. driver->minor_start = 64;
  3584. driver->type = TTY_DRIVER_TYPE_SERIAL;
  3585. driver->subtype = SERIAL_TYPE_NORMAL;
  3586. driver->init_termios = tty_std_termios;
  3587. driver->init_termios.c_cflag =
  3588. B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
  3589. driver->init_termios.c_ispeed = 115200;
  3590. driver->init_termios.c_ospeed = 115200;
  3591. driver->flags = TTY_DRIVER_REAL_RAW;
  3592. tty_set_operations(driver, &rs_ops);
  3593. serial_driver = driver;
  3594. /* do some initializing for the separate ports */
  3595. for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
  3596. if (info->enabled) {
  3597. if (cris_request_io_interface(info->io_if,
  3598. info->io_if_description)) {
  3599. printk(KERN_ERR "ETRAX100LX async serial: "
  3600. "Could not allocate IO pins for "
  3601. "%s, port %d\n",
  3602. info->io_if_description, i);
  3603. info->enabled = 0;
  3604. }
  3605. }
  3606. tty_port_init(&info->port);
  3607. info->uses_dma_in = 0;
  3608. info->uses_dma_out = 0;
  3609. info->line = i;
  3610. info->port.tty = NULL;
  3611. info->type = PORT_ETRAX;
  3612. info->tr_running = 0;
  3613. info->forced_eop = 0;
  3614. info->baud_base = DEF_BAUD_BASE;
  3615. info->custom_divisor = 0;
  3616. info->x_char = 0;
  3617. info->event = 0;
  3618. info->xmit.buf = NULL;
  3619. info->xmit.tail = info->xmit.head = 0;
  3620. info->first_recv_buffer = info->last_recv_buffer = NULL;
  3621. info->recv_cnt = info->max_recv_cnt = 0;
  3622. info->last_tx_active_usec = 0;
  3623. info->last_tx_active = 0;
  3624. #if defined(CONFIG_ETRAX_RS485)
  3625. /* Set sane defaults */
  3626. info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
  3627. info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
  3628. info->rs485.delay_rts_before_send = 0;
  3629. info->rs485.flags &= ~(SER_RS485_ENABLED);
  3630. #endif
  3631. INIT_WORK(&info->work, do_softint);
  3632. if (info->enabled) {
  3633. printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
  3634. serial_driver->name, info->line, info->ioport);
  3635. }
  3636. tty_port_link_device(&info->port, driver, i);
  3637. }
  3638. if (tty_register_driver(driver))
  3639. panic("Couldn't register serial driver\n");
  3640. #ifdef CONFIG_ETRAX_FAST_TIMER
  3641. #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
  3642. memset(fast_timers, 0, sizeof(fast_timers));
  3643. #endif
  3644. #ifdef CONFIG_ETRAX_RS485
  3645. memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
  3646. #endif
  3647. fast_timer_init();
  3648. #endif
  3649. #ifndef CONFIG_ETRAX_KGDB
  3650. /* Not needed in simulator. May only complicate stuff. */
  3651. /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
  3652. if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
  3653. IRQF_SHARED, "serial ", driver))
  3654. panic("%s: Failed to request irq8", __func__);
  3655. #endif
  3656. return 0;
  3657. }
  3658. /* this makes sure that rs_init is called during kernel boot */
  3659. device_initcall(rs_init);