crisv10.c 125 KB

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