crisv10.c 144 KB

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