crisv10.c 125 KB

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