crisv10.c 143 KB

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