crisv10.c 127 KB

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