crisv10.c 143 KB

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