crisv10.c 144 KB

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