crisv10.c 144 KB

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