crisv10.c 125 KB

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