cyclades.c 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191
  1. #undef BLOCKMOVE
  2. #define Z_WAKE
  3. #undef Z_EXT_CHARS_IN_BUFFER
  4. /*
  5. * linux/drivers/char/cyclades.c
  6. *
  7. * This file contains the driver for the Cyclades async multiport
  8. * serial boards.
  9. *
  10. * Initially written by Randolph Bentson <bentson@grieg.seaslug.org>.
  11. * Modified and maintained by Marcio Saito <marcio@cyclades.com>.
  12. *
  13. * Copyright (C) 2007 Jiri Slaby <jirislaby@gmail.com>
  14. *
  15. * Much of the design and some of the code came from serial.c
  16. * which was copyright (C) 1991, 1992 Linus Torvalds. It was
  17. * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
  18. * and then fixed as suggested by Michael K. Johnson 12/12/92.
  19. * Converted to pci probing and cleaned up by Jiri Slaby.
  20. *
  21. * This version supports shared IRQ's (only for PCI boards).
  22. *
  23. * Prevent users from opening non-existing Z ports.
  24. *
  25. * Revision 2.3.2.8 2000/07/06 18:14:16 ivan
  26. * Fixed the PCI detection function to work properly on Alpha systems.
  27. * Implemented support for TIOCSERGETLSR ioctl.
  28. * Implemented full support for non-standard baud rates.
  29. *
  30. * Revision 2.3.2.7 2000/06/01 18:26:34 ivan
  31. * Request PLX I/O region, although driver doesn't use it, to avoid
  32. * problems with other drivers accessing it.
  33. * Removed count for on-board buffer characters in cy_chars_in_buffer
  34. * (Cyclades-Z only).
  35. *
  36. * Revision 2.3.2.6 2000/05/05 13:56:05 ivan
  37. * Driver now reports physical instead of virtual memory addresses.
  38. * Masks were added to some Cyclades-Z read accesses.
  39. * Implemented workaround for PLX9050 bug that would cause a system lockup
  40. * in certain systems, depending on the MMIO addresses allocated to the
  41. * board.
  42. * Changed the Tx interrupt programming in the CD1400 chips to boost up
  43. * performance (Cyclom-Y only).
  44. * Code is now compliant with the new module interface (module_[init|exit]).
  45. * Make use of the PCI helper functions to access PCI resources.
  46. * Did some code "housekeeping".
  47. *
  48. * Revision 2.3.2.5 2000/01/19 14:35:33 ivan
  49. * Fixed bug in cy_set_termios on CRTSCTS flag turnoff.
  50. *
  51. * Revision 2.3.2.4 2000/01/17 09:19:40 ivan
  52. * Fixed SMP locking in Cyclom-Y interrupt handler.
  53. *
  54. * Revision 2.3.2.3 1999/12/28 12:11:39 ivan
  55. * Added a new cyclades_card field called nports to allow the driver to
  56. * know the exact number of ports found by the Z firmware after its load;
  57. * RX buffer contention prevention logic on interrupt op mode revisited
  58. * (Cyclades-Z only);
  59. * Revisited printk's for Z debug;
  60. * Driver now makes sure that the constant SERIAL_XMIT_SIZE is defined;
  61. *
  62. * Revision 2.3.2.2 1999/10/01 11:27:43 ivan
  63. * Fixed bug in cyz_poll that would make all ports but port 0
  64. * unable to transmit/receive data (Cyclades-Z only);
  65. * Implemented logic to prevent the RX buffer from being stuck with data
  66. * due to a driver / firmware race condition in interrupt op mode
  67. * (Cyclades-Z only);
  68. * Fixed bug in block_til_ready logic that would lead to a system crash;
  69. * Revisited cy_close spinlock usage;
  70. *
  71. * Revision 2.3.2.1 1999/09/28 11:01:22 ivan
  72. * Revisited CONFIG_PCI conditional compilation for PCI board support;
  73. * Implemented TIOCGICOUNT and TIOCMIWAIT ioctl support;
  74. * _Major_ cleanup on the Cyclades-Z interrupt support code / logic;
  75. * Removed CTS handling from the driver -- this is now completely handled
  76. * by the firmware (Cyclades-Z only);
  77. * Flush RX on-board buffers on a port open (Cyclades-Z only);
  78. * Fixed handling of ASYNC_SPD_* TTY flags;
  79. * Module unload now unmaps all memory area allocated by ioremap;
  80. *
  81. * Revision 2.3.1.1 1999/07/15 16:45:53 ivan
  82. * Removed CY_PROC conditional compilation;
  83. * Implemented SMP-awareness for the driver;
  84. * Implemented a new ISA IRQ autoprobe that uses the irq_probe_[on|off]
  85. * functions;
  86. * The driver now accepts memory addresses (maddr=0xMMMMM) and IRQs
  87. * (irq=NN) as parameters (only for ISA boards);
  88. * Fixed bug in set_line_char that would prevent the Cyclades-Z
  89. * ports from being configured at speeds above 115.2Kbps;
  90. * Fixed bug in cy_set_termios that would prevent XON/XOFF flow control
  91. * switching from working properly;
  92. * The driver now only prints IRQ info for the Cyclades-Z if it's
  93. * configured to work in interrupt mode;
  94. *
  95. * Revision 2.2.2.3 1999/06/28 11:13:29 ivan
  96. * Added support for interrupt mode operation for the Z cards;
  97. * Removed the driver inactivity control for the Z;
  98. * Added a missing MOD_DEC_USE_COUNT in the cy_open function for when
  99. * the Z firmware is not loaded yet;
  100. * Replaced the "manual" Z Tx flush buffer by a call to a FW command of
  101. * same functionality;
  102. * Implemented workaround for IRQ setting loss on the PCI configuration
  103. * registers after a PCI bridge EEPROM reload (affects PLX9060 only);
  104. *
  105. * Revision 2.2.2.2 1999/05/14 17:18:15 ivan
  106. * /proc entry location changed to /proc/tty/driver/cyclades;
  107. * Added support to shared IRQ's (only for PCI boards);
  108. * Added support for Cobalt Qube2 systems;
  109. * IRQ [de]allocation scheme revisited;
  110. * BREAK implementation changed in order to make use of the 'break_ctl'
  111. * TTY facility;
  112. * Fixed typo in TTY structure field 'driver_name';
  113. * Included a PCI bridge reset and EEPROM reload in the board
  114. * initialization code (for both Y and Z series).
  115. *
  116. * Revision 2.2.2.1 1999/04/08 16:17:43 ivan
  117. * Fixed a bug in cy_wait_until_sent that was preventing the port to be
  118. * closed properly after a SIGINT;
  119. * Module usage counter scheme revisited;
  120. * Added support to the upcoming Y PCI boards (i.e., support to additional
  121. * PCI Device ID's).
  122. *
  123. * Revision 2.2.1.10 1999/01/20 16:14:29 ivan
  124. * Removed all unnecessary page-alignement operations in ioremap calls
  125. * (ioremap is currently safe for these operations).
  126. *
  127. * Revision 2.2.1.9 1998/12/30 18:18:30 ivan
  128. * Changed access to PLX PCI bridge registers from I/O to MMIO, in
  129. * order to make PLX9050-based boards work with certain motherboards.
  130. *
  131. * Revision 2.2.1.8 1998/11/13 12:46:20 ivan
  132. * cy_close function now resets (correctly) the tty->closing flag;
  133. * JIFFIES_DIFF macro fixed.
  134. *
  135. * Revision 2.2.1.7 1998/09/03 12:07:28 ivan
  136. * Fixed bug in cy_close function, which was not informing HW of
  137. * which port should have the reception disabled before doing so;
  138. * fixed Cyclom-8YoP hardware detection bug.
  139. *
  140. * Revision 2.2.1.6 1998/08/20 17:15:39 ivan
  141. * Fixed bug in cy_close function, which causes malfunction
  142. * of one of the first 4 ports when a higher port is closed
  143. * (Cyclom-Y only).
  144. *
  145. * Revision 2.2.1.5 1998/08/10 18:10:28 ivan
  146. * Fixed Cyclom-4Yo hardware detection bug.
  147. *
  148. * Revision 2.2.1.4 1998/08/04 11:02:50 ivan
  149. * /proc/cyclades implementation with great collaboration of
  150. * Marc Lewis <marc@blarg.net>;
  151. * cyy_interrupt was changed to avoid occurrence of kernel oopses
  152. * during PPP operation.
  153. *
  154. * Revision 2.2.1.3 1998/06/01 12:09:10 ivan
  155. * General code review in order to comply with 2.1 kernel standards;
  156. * data loss prevention for slow devices revisited (cy_wait_until_sent
  157. * was created);
  158. * removed conditional compilation for new/old PCI structure support
  159. * (now the driver only supports the new PCI structure).
  160. *
  161. * Revision 2.2.1.1 1998/03/19 16:43:12 ivan
  162. * added conditional compilation for new/old PCI structure support;
  163. * removed kernel series (2.0.x / 2.1.x) conditional compilation.
  164. *
  165. * Revision 2.1.1.3 1998/03/16 18:01:12 ivan
  166. * cleaned up the data loss fix;
  167. * fixed XON/XOFF handling once more (Cyclades-Z);
  168. * general review of the driver routines;
  169. * introduction of a mechanism to prevent data loss with slow
  170. * printers, by forcing a delay before closing the port.
  171. *
  172. * Revision 2.1.1.2 1998/02/17 16:50:00 ivan
  173. * fixed detection/handling of new CD1400 in Ye boards;
  174. * fixed XON/XOFF handling (Cyclades-Z);
  175. * fixed data loss caused by a premature port close;
  176. * introduction of a flag that holds the CD1400 version ID per port
  177. * (used by the CYGETCD1400VER new ioctl).
  178. *
  179. * Revision 2.1.1.1 1997/12/03 17:31:19 ivan
  180. * Code review for the module cleanup routine;
  181. * fixed RTS and DTR status report for new CD1400's in get_modem_info;
  182. * includes anonymous changes regarding signal_pending.
  183. *
  184. * Revision 2.1 1997/11/01 17:42:41 ivan
  185. * Changes in the driver to support Alpha systems (except 8Zo V_1);
  186. * BREAK fix for the Cyclades-Z boards;
  187. * driver inactivity control by FW implemented;
  188. * introduction of flag that allows driver to take advantage of
  189. * a special CD1400 feature related to HW flow control;
  190. * added support for the CD1400 rev. J (Cyclom-Y boards);
  191. * introduction of ioctls to:
  192. * - control the rtsdtr_inv flag (Cyclom-Y);
  193. * - control the rflow flag (Cyclom-Y);
  194. * - adjust the polling interval (Cyclades-Z);
  195. *
  196. * Revision 1.36.4.33 1997/06/27 19:00:00 ivan
  197. * Fixes related to kernel version conditional
  198. * compilation.
  199. *
  200. * Revision 1.36.4.32 1997/06/14 19:30:00 ivan
  201. * Compatibility issues between kernels 2.0.x and
  202. * 2.1.x (mainly related to clear_bit function).
  203. *
  204. * Revision 1.36.4.31 1997/06/03 15:30:00 ivan
  205. * Changes to define the memory window according to the
  206. * board type.
  207. *
  208. * Revision 1.36.4.30 1997/05/16 15:30:00 daniel
  209. * Changes to support new cycladesZ boards.
  210. *
  211. * Revision 1.36.4.29 1997/05/12 11:30:00 daniel
  212. * Merge of Bentson's and Daniel's version 1.36.4.28.
  213. * Corrects bug in cy_detect_pci: check if there are more
  214. * ports than the number of static structs allocated.
  215. * Warning message during initialization if this driver is
  216. * used with the new generation of cycladesZ boards. Those
  217. * will be supported only in next release of the driver.
  218. * Corrects bug in cy_detect_pci and cy_detect_isa that
  219. * returned wrong number of VALID boards, when a cyclomY
  220. * was found with no serial modules connected.
  221. * Changes to use current (2.1.x) kernel subroutine names
  222. * and created macros for compilation with 2.0.x kernel,
  223. * instead of the other way around.
  224. *
  225. * Revision 1.36.4.28 1997/05/?? ??:00:00 bentson
  226. * Change queue_task_irq_off to queue_task_irq.
  227. * The inline function queue_task_irq_off (tqueue.h)
  228. * was removed from latest releases of 2.1.x kernel.
  229. * Use of macro __init to mark the initialization
  230. * routines, so memory can be reused.
  231. * Also incorporate implementation of critical region
  232. * in function cleanup_module() created by anonymous
  233. * linuxer.
  234. *
  235. * Revision 1.36.4.28 1997/04/25 16:00:00 daniel
  236. * Change to support new firmware that solves DCD problem:
  237. * application could fail to receive SIGHUP signal when DCD
  238. * varying too fast.
  239. *
  240. * Revision 1.36.4.27 1997/03/26 10:30:00 daniel
  241. * Changed for support linux versions 2.1.X.
  242. * Backward compatible with linux versions 2.0.X.
  243. * Corrected illegal use of filler field in
  244. * CH_CTRL struct.
  245. * Deleted some debug messages.
  246. *
  247. * Revision 1.36.4.26 1997/02/27 12:00:00 daniel
  248. * Included check for NULL tty pointer in cyz_poll.
  249. *
  250. * Revision 1.36.4.25 1997/02/26 16:28:30 bentson
  251. * Bill Foster at Blarg! Online services noticed that
  252. * some of the switch elements of -Z modem control
  253. * lacked a closing "break;"
  254. *
  255. * Revision 1.36.4.24 1997/02/24 11:00:00 daniel
  256. * Changed low water threshold for buffer xmit_buf
  257. *
  258. * Revision 1.36.4.23 1996/12/02 21:50:16 bentson
  259. * Marcio provided fix to modem status fetch for -Z
  260. *
  261. * Revision 1.36.4.22 1996/10/28 22:41:17 bentson
  262. * improve mapping of -Z control page (thanks to Steve
  263. * Price <stevep@fa.tdktca.com> for help on this)
  264. *
  265. * Revision 1.36.4.21 1996/09/10 17:00:10 bentson
  266. * shift from CPU-bound to memcopy in cyz_polling operation
  267. *
  268. * Revision 1.36.4.20 1996/09/09 18:30:32 Bentson
  269. * Added support to set and report higher speeds.
  270. *
  271. * Revision 1.36.4.19c 1996/08/09 10:00:00 Marcio Saito
  272. * Some fixes in the HW flow control for the BETA release.
  273. * Don't try to register the IRQ.
  274. *
  275. * Revision 1.36.4.19 1996/08/08 16:23:18 Bentson
  276. * make sure "cyc" appears in all kernel messages; all soft interrupts
  277. * handled by same routine; recognize out-of-band reception; comment
  278. * out some diagnostic messages; leave RTS/CTS flow control to hardware;
  279. * fix race condition in -Z buffer management; only -Y needs to explicitly
  280. * flush chars; tidy up some startup messages;
  281. *
  282. * Revision 1.36.4.18 1996/07/25 18:57:31 bentson
  283. * shift MOD_INC_USE_COUNT location to match
  284. * serial.c; purge some diagnostic messages;
  285. *
  286. * Revision 1.36.4.17 1996/07/25 18:01:08 bentson
  287. * enable modem status messages and fetch & process them; note
  288. * time of last activity type for each port; set_line_char now
  289. * supports more than line 0 and treats 0 baud correctly;
  290. * get_modem_info senses rs_status;
  291. *
  292. * Revision 1.36.4.16 1996/07/20 08:43:15 bentson
  293. * barely works--now's time to turn on
  294. * more features 'til it breaks
  295. *
  296. * Revision 1.36.4.15 1996/07/19 22:30:06 bentson
  297. * check more -Z board status; shorten boot message
  298. *
  299. * Revision 1.36.4.14 1996/07/19 22:20:37 bentson
  300. * fix reference to ch_ctrl in startup; verify return
  301. * values from cyz_issue_cmd and cyz_update_channel;
  302. * more stuff to get modem control correct;
  303. *
  304. * Revision 1.36.4.13 1996/07/11 19:53:33 bentson
  305. * more -Z stuff folded in; re-order changes to put -Z stuff
  306. * after -Y stuff (to make changes clearer)
  307. *
  308. * Revision 1.36.4.12 1996/07/11 15:40:55 bentson
  309. * Add code to poll Cyclades-Z. Add code to get & set RS-232 control.
  310. * Add code to send break. Clear firmware ID word at startup (so
  311. * that other code won't talk to inactive board).
  312. *
  313. * Revision 1.36.4.11 1996/07/09 05:28:29 bentson
  314. * add code for -Z in set_line_char
  315. *
  316. * Revision 1.36.4.10 1996/07/08 19:28:37 bentson
  317. * fold more -Z stuff (or in some cases, error messages)
  318. * into driver; add text to "don't know what to do" messages.
  319. *
  320. * Revision 1.36.4.9 1996/07/08 18:38:38 bentson
  321. * moved compile-time flags near top of file; cosmetic changes
  322. * to narrow text (to allow 2-up printing); changed many declarations
  323. * to "static" to limit external symbols; shuffled code order to
  324. * coalesce -Y and -Z specific code, also to put internal functions
  325. * in order of tty_driver structure; added code to recognize -Z
  326. * ports (and for moment, do nothing or report error); add cy_startup
  327. * to parse boot command line for extra base addresses for ISA probes;
  328. *
  329. * Revision 1.36.4.8 1996/06/25 17:40:19 bentson
  330. * reorder some code, fix types of some vars (int vs. long),
  331. * add cy_setup to support user declared ISA addresses
  332. *
  333. * Revision 1.36.4.7 1996/06/21 23:06:18 bentson
  334. * dump ioctl based firmware load (it's now a user level
  335. * program); ensure uninitialzed ports cannot be used
  336. *
  337. * Revision 1.36.4.6 1996/06/20 23:17:19 bentson
  338. * rename vars and restructure some code
  339. *
  340. * Revision 1.36.4.5 1996/06/14 15:09:44 bentson
  341. * get right status back after boot load
  342. *
  343. * Revision 1.36.4.4 1996/06/13 19:51:44 bentson
  344. * successfully loads firmware
  345. *
  346. * Revision 1.36.4.3 1996/06/13 06:08:33 bentson
  347. * add more of the code for the boot/load ioctls
  348. *
  349. * Revision 1.36.4.2 1996/06/11 21:00:51 bentson
  350. * start to add Z functionality--starting with ioctl
  351. * for loading firmware
  352. *
  353. * Revision 1.36.4.1 1996/06/10 18:03:02 bentson
  354. * added code to recognize Z/PCI card at initialization; report
  355. * presence, but card is not initialized (because firmware needs
  356. * to be loaded)
  357. *
  358. * Revision 1.36.3.8 1996/06/07 16:29:00 bentson
  359. * starting minor number at zero; added missing verify_area
  360. * as noted by Heiko Eißfeldt <heiko@colossus.escape.de>
  361. *
  362. * Revision 1.36.3.7 1996/04/19 21:06:18 bentson
  363. * remove unneeded boot message & fix CLOCAL hardware flow
  364. * control (Miquel van Smoorenburg <miquels@Q.cistron.nl>);
  365. * remove unused diagnostic statements; minor 0 is first;
  366. *
  367. * Revision 1.36.3.6 1996/03/13 13:21:17 marcio
  368. * The kernel function vremap (available only in later 1.3.xx kernels)
  369. * allows the access to memory addresses above the RAM. This revision
  370. * of the driver supports PCI boards below 1Mb (device id 0x100) and
  371. * above 1Mb (device id 0x101).
  372. *
  373. * Revision 1.36.3.5 1996/03/07 15:20:17 bentson
  374. * Some global changes to interrupt handling spilled into
  375. * this driver--mostly unused arguments in system function
  376. * calls. Also added change by Marcio Saito which should
  377. * reduce lost interrupts at startup by fast processors.
  378. *
  379. * Revision 1.36.3.4 1995/11/13 20:45:10 bentson
  380. * Changes by Corey Minyard <minyard@wf-rch.cirr.com> distributed
  381. * in 1.3.41 kernel to remove a possible race condition, extend
  382. * some error messages, and let the driver run as a loadable module
  383. * Change by Alan Wendt <alan@ez0.ezlink.com> to remove a
  384. * possible race condition.
  385. * Change by Marcio Saito <marcio@cyclades.com> to fix PCI addressing.
  386. *
  387. * Revision 1.36.3.3 1995/11/13 19:44:48 bentson
  388. * Changes by Linus Torvalds in 1.3.33 kernel distribution
  389. * required due to reordering of driver initialization.
  390. * Drivers are now initialized *after* memory management.
  391. *
  392. * Revision 1.36.3.2 1995/09/08 22:07:14 bentson
  393. * remove printk from ISR; fix typo
  394. *
  395. * Revision 1.36.3.1 1995/09/01 12:00:42 marcio
  396. * Minor fixes in the PCI board support. PCI function calls in
  397. * conditional compilation (CONFIG_PCI). Thanks to Jim Duncan
  398. * <duncan@okay.com>. "bad serial count" message removed.
  399. *
  400. * Revision 1.36.3 1995/08/22 09:19:42 marcio
  401. * Cyclom-Y/PCI support added. Changes in the cy_init routine and
  402. * board initialization. Changes in the boot messages. The driver
  403. * supports up to 4 boards and 64 ports by default.
  404. *
  405. * Revision 1.36.1.4 1995/03/29 06:14:14 bentson
  406. * disambiguate between Cyclom-16Y and Cyclom-32Ye;
  407. *
  408. * Revision 1.36.1.3 1995/03/23 22:15:35 bentson
  409. * add missing break in modem control block in ioctl switch statement
  410. * (discovered by Michael Edward Chastain <mec@jobe.shell.portal.com>);
  411. *
  412. * Revision 1.36.1.2 1995/03/22 19:16:22 bentson
  413. * make sure CTS flow control is set as soon as possible (thanks
  414. * to note from David Lambert <lambert@chesapeake.rps.slb.com>);
  415. *
  416. * Revision 1.36.1.1 1995/03/13 15:44:43 bentson
  417. * initialize defaults for receive threshold and stale data timeout;
  418. * cosmetic changes;
  419. *
  420. * Revision 1.36 1995/03/10 23:33:53 bentson
  421. * added support of chips 4-7 in 32 port Cyclom-Ye;
  422. * fix cy_interrupt pointer dereference problem
  423. * (Joe Portman <baron@aa.net>);
  424. * give better error response if open is attempted on non-existent port
  425. * (Zachariah Vaum <jchryslr@netcom.com>);
  426. * correct command timeout (Kenneth Lerman <lerman@@seltd.newnet.com>);
  427. * conditional compilation for -16Y on systems with fast, noisy bus;
  428. * comment out diagnostic print function;
  429. * cleaned up table of base addresses;
  430. * set receiver time-out period register to correct value,
  431. * set receive threshold to better default values,
  432. * set chip timer to more accurate 200 Hz ticking,
  433. * add code to monitor and modify receive parameters
  434. * (Rik Faith <faith@cs.unc.edu> Nick Simicich
  435. * <njs@scifi.emi.net>);
  436. *
  437. * Revision 1.35 1994/12/16 13:54:18 steffen
  438. * additional patch by Marcio Saito for board detection
  439. * Accidently left out in 1.34
  440. *
  441. * Revision 1.34 1994/12/10 12:37:12 steffen
  442. * This is the corrected version as suggested by Marcio Saito
  443. *
  444. * Revision 1.33 1994/12/01 22:41:18 bentson
  445. * add hooks to support more high speeds directly; add tytso
  446. * patch regarding CLOCAL wakeups
  447. *
  448. * Revision 1.32 1994/11/23 19:50:04 bentson
  449. * allow direct kernel control of higher signalling rates;
  450. * look for cards at additional locations
  451. *
  452. * Revision 1.31 1994/11/16 04:33:28 bentson
  453. * ANOTHER fix from Corey Minyard, minyard@wf-rch.cirr.com--
  454. * a problem in chars_in_buffer has been resolved by some
  455. * small changes; this should yield smoother output
  456. *
  457. * Revision 1.30 1994/11/16 04:28:05 bentson
  458. * Fix from Corey Minyard, Internet: minyard@metronet.com,
  459. * UUCP: minyard@wf-rch.cirr.com, WORK: minyardbnr.ca, to
  460. * cy_hangup that appears to clear up much (all?) of the
  461. * DTR glitches; also he's added/cleaned-up diagnostic messages
  462. *
  463. * Revision 1.29 1994/11/16 04:16:07 bentson
  464. * add change proposed by Ralph Sims, ralphs@halcyon.com, to
  465. * operate higher speeds in same way as other serial ports;
  466. * add more serial ports (for up to two 16-port muxes).
  467. *
  468. * Revision 1.28 1994/11/04 00:13:16 root
  469. * turn off diagnostic messages
  470. *
  471. * Revision 1.27 1994/11/03 23:46:37 root
  472. * bunch of changes to bring driver into greater conformance
  473. * with the serial.c driver (looking for missed fixes)
  474. *
  475. * Revision 1.26 1994/11/03 22:40:36 root
  476. * automatic interrupt probing fixed.
  477. *
  478. * Revision 1.25 1994/11/03 20:17:02 root
  479. * start to implement auto-irq
  480. *
  481. * Revision 1.24 1994/11/03 18:01:55 root
  482. * still working on modem signals--trying not to drop DTR
  483. * during the getty/login processes
  484. *
  485. * Revision 1.23 1994/11/03 17:51:36 root
  486. * extend baud rate support; set receive threshold as function
  487. * of baud rate; fix some problems with RTS/CTS;
  488. *
  489. * Revision 1.22 1994/11/02 18:05:35 root
  490. * changed arguments to udelay to type long to get
  491. * delays to be of correct duration
  492. *
  493. * Revision 1.21 1994/11/02 17:37:30 root
  494. * employ udelay (after calibrating loops_per_second earlier
  495. * in init/main.c) instead of using home-grown delay routines
  496. *
  497. * Revision 1.20 1994/11/02 03:11:38 root
  498. * cy_chars_in_buffer forces a return value of 0 to let
  499. * login work (don't know why it does); some functions
  500. * that were returning EFAULT, now executes the code;
  501. * more work on deciding when to disable xmit interrupts;
  502. *
  503. * Revision 1.19 1994/11/01 20:10:14 root
  504. * define routine to start transmission interrupts (by enabling
  505. * transmit interrupts); directly enable/disable modem interrupts;
  506. *
  507. * Revision 1.18 1994/11/01 18:40:45 bentson
  508. * Don't always enable transmit interrupts in startup; interrupt on
  509. * TxMpty instead of TxRdy to help characters get out before shutdown;
  510. * restructure xmit interrupt to check for chars first and quit if
  511. * none are ready to go; modem status (MXVRx) is upright, _not_ inverted
  512. * (to my view);
  513. *
  514. * Revision 1.17 1994/10/30 04:39:45 bentson
  515. * rename serial_driver and callout_driver to cy_serial_driver and
  516. * cy_callout_driver to avoid linkage interference; initialize
  517. * info->type to PORT_CIRRUS; ruggedize paranoia test; elide ->port
  518. * from cyclades_port structure; add paranoia check to cy_close;
  519. *
  520. * Revision 1.16 1994/10/30 01:14:33 bentson
  521. * change major numbers; add some _early_ return statements;
  522. *
  523. * Revision 1.15 1994/10/29 06:43:15 bentson
  524. * final tidying up for clean compile; enable some error reporting
  525. *
  526. * Revision 1.14 1994/10/28 20:30:22 Bentson
  527. * lots of changes to drag the driver towards the new tty_io
  528. * structures and operation. not expected to work, but may
  529. * compile cleanly.
  530. *
  531. * Revision 1.13 1994/07/21 23:08:57 Bentson
  532. * add some diagnostic cruft; support 24 lines (for testing
  533. * both -8Y and -16Y cards; be more thorough in servicing all
  534. * chips during interrupt; add "volatile" a few places to
  535. * circumvent compiler optimizations; fix base & offset
  536. * computations in block_til_ready (was causing chip 0 to
  537. * stop operation)
  538. *
  539. * Revision 1.12 1994/07/19 16:42:11 Bentson
  540. * add some hackery for kernel version 1.1.8; expand
  541. * error messages; refine timing for delay loops and
  542. * declare loop params volatile
  543. *
  544. * Revision 1.11 1994/06/11 21:53:10 bentson
  545. * get use of save_car right in transmit interrupt service
  546. *
  547. * Revision 1.10.1.1 1994/06/11 21:31:18 bentson
  548. * add some diagnostic printing; try to fix save_car stuff
  549. *
  550. * Revision 1.10 1994/06/11 20:36:08 bentson
  551. * clean up compiler warnings
  552. *
  553. * Revision 1.9 1994/06/11 19:42:46 bentson
  554. * added a bunch of code to support modem signalling
  555. *
  556. * Revision 1.8 1994/06/11 17:57:07 bentson
  557. * recognize break & parity error
  558. *
  559. * Revision 1.7 1994/06/05 05:51:34 bentson
  560. * Reorder baud table to be monotonic; add cli to CP; discard
  561. * incoming characters and status if the line isn't open; start to
  562. * fold code into cy_throttle; start to port get_serial_info,
  563. * set_serial_info, get_modem_info, set_modem_info, and send_break
  564. * from serial.c; expand cy_ioctl; relocate and expand config_setup;
  565. * get flow control characters from tty struct; invalidate ports w/o
  566. * hardware;
  567. *
  568. * Revision 1.6 1994/05/31 18:42:21 bentson
  569. * add a loop-breaker in the interrupt service routine;
  570. * note when port is initialized so that it can be shut
  571. * down under the right conditions; receive works without
  572. * any obvious errors
  573. *
  574. * Revision 1.5 1994/05/30 00:55:02 bentson
  575. * transmit works without obvious errors
  576. *
  577. * Revision 1.4 1994/05/27 18:46:27 bentson
  578. * incorporated more code from lib_y.c; can now print short
  579. * strings under interrupt control to port zero; seems to
  580. * select ports/channels/lines correctly
  581. *
  582. * Revision 1.3 1994/05/25 22:12:44 bentson
  583. * shifting from multi-port on a card to proper multiplexor
  584. * data structures; added skeletons of most routines
  585. *
  586. * Revision 1.2 1994/05/19 13:21:43 bentson
  587. * start to crib from other sources
  588. *
  589. */
  590. #define CY_VERSION "2.5"
  591. /* If you need to install more boards than NR_CARDS, change the constant
  592. in the definition below. No other change is necessary to support up to
  593. eight boards. Beyond that you'll have to extend cy_isa_addresses. */
  594. #define NR_CARDS 4
  595. /*
  596. If the total number of ports is larger than NR_PORTS, change this
  597. constant in the definition below. No other change is necessary to
  598. support more boards/ports. */
  599. #define NR_PORTS 256
  600. #define ZO_V1 0
  601. #define ZO_V2 1
  602. #define ZE_V1 2
  603. #define SERIAL_PARANOIA_CHECK
  604. #undef CY_DEBUG_OPEN
  605. #undef CY_DEBUG_THROTTLE
  606. #undef CY_DEBUG_OTHER
  607. #undef CY_DEBUG_IO
  608. #undef CY_DEBUG_COUNT
  609. #undef CY_DEBUG_DTR
  610. #undef CY_DEBUG_WAIT_UNTIL_SENT
  611. #undef CY_DEBUG_INTERRUPTS
  612. #undef CY_16Y_HACK
  613. #undef CY_ENABLE_MONITORING
  614. #undef CY_PCI_DEBUG
  615. /*
  616. * Include section
  617. */
  618. #include <linux/module.h>
  619. #include <linux/errno.h>
  620. #include <linux/signal.h>
  621. #include <linux/sched.h>
  622. #include <linux/timer.h>
  623. #include <linux/interrupt.h>
  624. #include <linux/tty.h>
  625. #include <linux/tty_flip.h>
  626. #include <linux/serial.h>
  627. #include <linux/smp_lock.h>
  628. #include <linux/major.h>
  629. #include <linux/string.h>
  630. #include <linux/fcntl.h>
  631. #include <linux/ptrace.h>
  632. #include <linux/cyclades.h>
  633. #include <linux/mm.h>
  634. #include <linux/ioport.h>
  635. #include <linux/init.h>
  636. #include <linux/delay.h>
  637. #include <linux/spinlock.h>
  638. #include <linux/bitops.h>
  639. #include <linux/firmware.h>
  640. #include <linux/device.h>
  641. #include <asm/system.h>
  642. #include <linux/io.h>
  643. #include <asm/irq.h>
  644. #include <linux/uaccess.h>
  645. #include <linux/kernel.h>
  646. #include <linux/pci.h>
  647. #include <linux/stat.h>
  648. #include <linux/proc_fs.h>
  649. #include <linux/seq_file.h>
  650. static void cy_throttle(struct tty_struct *tty);
  651. static void cy_send_xchar(struct tty_struct *tty, char ch);
  652. #ifndef SERIAL_XMIT_SIZE
  653. #define SERIAL_XMIT_SIZE (min(PAGE_SIZE, 4096))
  654. #endif
  655. #define WAKEUP_CHARS 256
  656. #define STD_COM_FLAGS (0)
  657. /* firmware stuff */
  658. #define ZL_MAX_BLOCKS 16
  659. #define DRIVER_VERSION 0x02010203
  660. #define RAM_SIZE 0x80000
  661. enum zblock_type {
  662. ZBLOCK_PRG = 0,
  663. ZBLOCK_FPGA = 1
  664. };
  665. struct zfile_header {
  666. char name[64];
  667. char date[32];
  668. char aux[32];
  669. u32 n_config;
  670. u32 config_offset;
  671. u32 n_blocks;
  672. u32 block_offset;
  673. u32 reserved[9];
  674. } __attribute__ ((packed));
  675. struct zfile_config {
  676. char name[64];
  677. u32 mailbox;
  678. u32 function;
  679. u32 n_blocks;
  680. u32 block_list[ZL_MAX_BLOCKS];
  681. } __attribute__ ((packed));
  682. struct zfile_block {
  683. u32 type;
  684. u32 file_offset;
  685. u32 ram_offset;
  686. u32 size;
  687. } __attribute__ ((packed));
  688. static struct tty_driver *cy_serial_driver;
  689. #ifdef CONFIG_ISA
  690. /* This is the address lookup table. The driver will probe for
  691. Cyclom-Y/ISA boards at all addresses in here. If you want the
  692. driver to probe addresses at a different address, add it to
  693. this table. If the driver is probing some other board and
  694. causing problems, remove the offending address from this table.
  695. */
  696. static unsigned int cy_isa_addresses[] = {
  697. 0xD0000,
  698. 0xD2000,
  699. 0xD4000,
  700. 0xD6000,
  701. 0xD8000,
  702. 0xDA000,
  703. 0xDC000,
  704. 0xDE000,
  705. 0, 0, 0, 0, 0, 0, 0, 0
  706. };
  707. #define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
  708. #ifdef MODULE
  709. static long maddr[NR_CARDS];
  710. static int irq[NR_CARDS];
  711. module_param_array(maddr, long, NULL, 0);
  712. module_param_array(irq, int, NULL, 0);
  713. #endif
  714. #endif /* CONFIG_ISA */
  715. /* This is the per-card data structure containing address, irq, number of
  716. channels, etc. This driver supports a maximum of NR_CARDS cards.
  717. */
  718. static struct cyclades_card cy_card[NR_CARDS];
  719. static int cy_next_channel; /* next minor available */
  720. /*
  721. * This is used to look up the divisor speeds and the timeouts
  722. * We're normally limited to 15 distinct baud rates. The extra
  723. * are accessed via settings in info->port.flags.
  724. * 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  725. * 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  726. * HI VHI
  727. * 20
  728. */
  729. static int baud_table[] = {
  730. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
  731. 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
  732. 230400, 0
  733. };
  734. static char baud_co_25[] = { /* 25 MHz clock option table */
  735. /* value => 00 01 02 03 04 */
  736. /* divide by 8 32 128 512 2048 */
  737. 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
  738. 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  739. };
  740. static char baud_bpr_25[] = { /* 25 MHz baud rate period table */
  741. 0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
  742. 0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
  743. };
  744. static char baud_co_60[] = { /* 60 MHz clock option table (CD1400 J) */
  745. /* value => 00 01 02 03 04 */
  746. /* divide by 8 32 128 512 2048 */
  747. 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
  748. 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  749. 0x00
  750. };
  751. static char baud_bpr_60[] = { /* 60 MHz baud rate period table (CD1400 J) */
  752. 0x00, 0x82, 0x21, 0xff, 0xdb, 0xc3, 0x92, 0x62, 0xc3, 0x62,
  753. 0x41, 0xc3, 0x62, 0xc3, 0x62, 0xc3, 0x82, 0x62, 0x41, 0x32,
  754. 0x21
  755. };
  756. static char baud_cor3[] = { /* receive threshold */
  757. 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  758. 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07,
  759. 0x07
  760. };
  761. /*
  762. * The Cyclades driver implements HW flow control as any serial driver.
  763. * The cyclades_port structure member rflow and the vector rflow_thr
  764. * allows us to take advantage of a special feature in the CD1400 to avoid
  765. * data loss even when the system interrupt latency is too high. These flags
  766. * are to be used only with very special applications. Setting these flags
  767. * requires the use of a special cable (DTR and RTS reversed). In the new
  768. * CD1400-based boards (rev. 6.00 or later), there is no need for special
  769. * cables.
  770. */
  771. static char rflow_thr[] = { /* rflow threshold */
  772. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  773. 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  774. 0x0a
  775. };
  776. /* The Cyclom-Ye has placed the sequential chips in non-sequential
  777. * address order. This look-up table overcomes that problem.
  778. */
  779. static const unsigned int cy_chip_offset[] = { 0x0000,
  780. 0x0400,
  781. 0x0800,
  782. 0x0C00,
  783. 0x0200,
  784. 0x0600,
  785. 0x0A00,
  786. 0x0E00
  787. };
  788. /* PCI related definitions */
  789. #ifdef CONFIG_PCI
  790. static struct pci_device_id cy_pci_dev_id[] __devinitdata = {
  791. /* PCI < 1Mb */
  792. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Lo) },
  793. /* PCI > 1Mb */
  794. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Hi) },
  795. /* 4Y PCI < 1Mb */
  796. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Lo) },
  797. /* 4Y PCI > 1Mb */
  798. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Hi) },
  799. /* 8Y PCI < 1Mb */
  800. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Lo) },
  801. /* 8Y PCI > 1Mb */
  802. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Hi) },
  803. /* Z PCI < 1Mb */
  804. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Lo) },
  805. /* Z PCI > 1Mb */
  806. { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Hi) },
  807. { } /* end of table */
  808. };
  809. MODULE_DEVICE_TABLE(pci, cy_pci_dev_id);
  810. #endif
  811. static void cy_start(struct tty_struct *);
  812. static void cy_set_line_char(struct cyclades_port *, struct tty_struct *);
  813. static int cyz_issue_cmd(struct cyclades_card *, __u32, __u8, __u32);
  814. #ifdef CONFIG_ISA
  815. static unsigned detect_isa_irq(void __iomem *);
  816. #endif /* CONFIG_ISA */
  817. #ifndef CONFIG_CYZ_INTR
  818. static void cyz_poll(unsigned long);
  819. /* The Cyclades-Z polling cycle is defined by this variable */
  820. static long cyz_polling_cycle = CZ_DEF_POLL;
  821. static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
  822. #else /* CONFIG_CYZ_INTR */
  823. static void cyz_rx_restart(unsigned long);
  824. static struct timer_list cyz_rx_full_timer[NR_PORTS];
  825. #endif /* CONFIG_CYZ_INTR */
  826. static inline bool cy_is_Z(struct cyclades_card *card)
  827. {
  828. return card->num_chips == (unsigned int)-1;
  829. }
  830. static inline bool __cyz_fpga_loaded(struct RUNTIME_9060 __iomem *ctl_addr)
  831. {
  832. return readl(&ctl_addr->init_ctrl) & (1 << 17);
  833. }
  834. static inline bool cyz_fpga_loaded(struct cyclades_card *card)
  835. {
  836. return __cyz_fpga_loaded(card->ctl_addr.p9060);
  837. }
  838. static inline bool cyz_is_loaded(struct cyclades_card *card)
  839. {
  840. struct FIRM_ID __iomem *fw_id = card->base_addr + ID_ADDRESS;
  841. return (card->hw_ver == ZO_V1 || cyz_fpga_loaded(card)) &&
  842. readl(&fw_id->signature) == ZFIRM_ID;
  843. }
  844. static inline int serial_paranoia_check(struct cyclades_port *info,
  845. char *name, const char *routine)
  846. {
  847. #ifdef SERIAL_PARANOIA_CHECK
  848. if (!info) {
  849. printk(KERN_WARNING "cyc Warning: null cyclades_port for (%s) "
  850. "in %s\n", name, routine);
  851. return 1;
  852. }
  853. if (info->magic != CYCLADES_MAGIC) {
  854. printk(KERN_WARNING "cyc Warning: bad magic number for serial "
  855. "struct (%s) in %s\n", name, routine);
  856. return 1;
  857. }
  858. #endif
  859. return 0;
  860. } /* serial_paranoia_check */
  861. /***********************************************************/
  862. /********* Start of block of Cyclom-Y specific code ********/
  863. /* This routine waits up to 1000 micro-seconds for the previous
  864. command to the Cirrus chip to complete and then issues the
  865. new command. An error is returned if the previous command
  866. didn't finish within the time limit.
  867. This function is only called from inside spinlock-protected code.
  868. */
  869. static int cyy_issue_cmd(void __iomem *base_addr, u_char cmd, int index)
  870. {
  871. unsigned int i;
  872. /* Check to see that the previous command has completed */
  873. for (i = 0; i < 100; i++) {
  874. if (readb(base_addr + (CyCCR << index)) == 0)
  875. break;
  876. udelay(10L);
  877. }
  878. /* if the CCR never cleared, the previous command
  879. didn't finish within the "reasonable time" */
  880. if (i == 100)
  881. return -1;
  882. /* Issue the new command */
  883. cy_writeb(base_addr + (CyCCR << index), cmd);
  884. return 0;
  885. } /* cyy_issue_cmd */
  886. #ifdef CONFIG_ISA
  887. /* ISA interrupt detection code */
  888. static unsigned detect_isa_irq(void __iomem *address)
  889. {
  890. int irq;
  891. unsigned long irqs, flags;
  892. int save_xir, save_car;
  893. int index = 0; /* IRQ probing is only for ISA */
  894. /* forget possible initially masked and pending IRQ */
  895. irq = probe_irq_off(probe_irq_on());
  896. /* Clear interrupts on the board first */
  897. cy_writeb(address + (Cy_ClrIntr << index), 0);
  898. /* Cy_ClrIntr is 0x1800 */
  899. irqs = probe_irq_on();
  900. /* Wait ... */
  901. udelay(5000L);
  902. /* Enable the Tx interrupts on the CD1400 */
  903. local_irq_save(flags);
  904. cy_writeb(address + (CyCAR << index), 0);
  905. cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
  906. cy_writeb(address + (CyCAR << index), 0);
  907. cy_writeb(address + (CySRER << index),
  908. readb(address + (CySRER << index)) | CyTxRdy);
  909. local_irq_restore(flags);
  910. /* Wait ... */
  911. udelay(5000L);
  912. /* Check which interrupt is in use */
  913. irq = probe_irq_off(irqs);
  914. /* Clean up */
  915. save_xir = (u_char) readb(address + (CyTIR << index));
  916. save_car = readb(address + (CyCAR << index));
  917. cy_writeb(address + (CyCAR << index), (save_xir & 0x3));
  918. cy_writeb(address + (CySRER << index),
  919. readb(address + (CySRER << index)) & ~CyTxRdy);
  920. cy_writeb(address + (CyTIR << index), (save_xir & 0x3f));
  921. cy_writeb(address + (CyCAR << index), (save_car));
  922. cy_writeb(address + (Cy_ClrIntr << index), 0);
  923. /* Cy_ClrIntr is 0x1800 */
  924. return (irq > 0) ? irq : 0;
  925. }
  926. #endif /* CONFIG_ISA */
  927. static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
  928. void __iomem *base_addr)
  929. {
  930. struct cyclades_port *info;
  931. struct tty_struct *tty;
  932. int len, index = cinfo->bus_index;
  933. u8 save_xir, channel, save_car, data, char_count;
  934. #ifdef CY_DEBUG_INTERRUPTS
  935. printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
  936. #endif
  937. /* determine the channel & change to that context */
  938. save_xir = readb(base_addr + (CyRIR << index));
  939. channel = save_xir & CyIRChannel;
  940. info = &cinfo->ports[channel + chip * 4];
  941. save_car = readb(base_addr + (CyCAR << index));
  942. cy_writeb(base_addr + (CyCAR << index), save_xir);
  943. tty = tty_port_tty_get(&info->port);
  944. /* if there is nowhere to put the data, discard it */
  945. if (tty == NULL) {
  946. if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
  947. CyIVRRxEx) { /* exception */
  948. data = readb(base_addr + (CyRDSR << index));
  949. } else { /* normal character reception */
  950. char_count = readb(base_addr + (CyRDCR << index));
  951. while (char_count--)
  952. data = readb(base_addr + (CyRDSR << index));
  953. }
  954. goto end;
  955. }
  956. /* there is an open port for this data */
  957. if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
  958. CyIVRRxEx) { /* exception */
  959. data = readb(base_addr + (CyRDSR << index));
  960. /* For statistics only */
  961. if (data & CyBREAK)
  962. info->icount.brk++;
  963. else if (data & CyFRAME)
  964. info->icount.frame++;
  965. else if (data & CyPARITY)
  966. info->icount.parity++;
  967. else if (data & CyOVERRUN)
  968. info->icount.overrun++;
  969. if (data & info->ignore_status_mask) {
  970. info->icount.rx++;
  971. tty_kref_put(tty);
  972. return;
  973. }
  974. if (tty_buffer_request_room(tty, 1)) {
  975. if (data & info->read_status_mask) {
  976. if (data & CyBREAK) {
  977. tty_insert_flip_char(tty,
  978. readb(base_addr + (CyRDSR <<
  979. index)), TTY_BREAK);
  980. info->icount.rx++;
  981. if (info->port.flags & ASYNC_SAK)
  982. do_SAK(tty);
  983. } else if (data & CyFRAME) {
  984. tty_insert_flip_char(tty,
  985. readb(base_addr + (CyRDSR <<
  986. index)), TTY_FRAME);
  987. info->icount.rx++;
  988. info->idle_stats.frame_errs++;
  989. } else if (data & CyPARITY) {
  990. /* Pieces of seven... */
  991. tty_insert_flip_char(tty,
  992. readb(base_addr + (CyRDSR <<
  993. index)), TTY_PARITY);
  994. info->icount.rx++;
  995. info->idle_stats.parity_errs++;
  996. } else if (data & CyOVERRUN) {
  997. tty_insert_flip_char(tty, 0,
  998. TTY_OVERRUN);
  999. info->icount.rx++;
  1000. /* If the flip buffer itself is
  1001. overflowing, we still lose
  1002. the next incoming character.
  1003. */
  1004. tty_insert_flip_char(tty,
  1005. readb(base_addr + (CyRDSR <<
  1006. index)), TTY_FRAME);
  1007. info->icount.rx++;
  1008. info->idle_stats.overruns++;
  1009. /* These two conditions may imply */
  1010. /* a normal read should be done. */
  1011. /* } else if(data & CyTIMEOUT) { */
  1012. /* } else if(data & CySPECHAR) { */
  1013. } else {
  1014. tty_insert_flip_char(tty, 0,
  1015. TTY_NORMAL);
  1016. info->icount.rx++;
  1017. }
  1018. } else {
  1019. tty_insert_flip_char(tty, 0, TTY_NORMAL);
  1020. info->icount.rx++;
  1021. }
  1022. } else {
  1023. /* there was a software buffer overrun and nothing
  1024. * could be done about it!!! */
  1025. info->icount.buf_overrun++;
  1026. info->idle_stats.overruns++;
  1027. }
  1028. } else { /* normal character reception */
  1029. /* load # chars available from the chip */
  1030. char_count = readb(base_addr + (CyRDCR << index));
  1031. #ifdef CY_ENABLE_MONITORING
  1032. ++info->mon.int_count;
  1033. info->mon.char_count += char_count;
  1034. if (char_count > info->mon.char_max)
  1035. info->mon.char_max = char_count;
  1036. info->mon.char_last = char_count;
  1037. #endif
  1038. len = tty_buffer_request_room(tty, char_count);
  1039. while (len--) {
  1040. data = readb(base_addr + (CyRDSR << index));
  1041. tty_insert_flip_char(tty, data, TTY_NORMAL);
  1042. info->idle_stats.recv_bytes++;
  1043. info->icount.rx++;
  1044. #ifdef CY_16Y_HACK
  1045. udelay(10L);
  1046. #endif
  1047. }
  1048. info->idle_stats.recv_idle = jiffies;
  1049. }
  1050. tty_schedule_flip(tty);
  1051. tty_kref_put(tty);
  1052. end:
  1053. /* end of service */
  1054. cy_writeb(base_addr + (CyRIR << index), save_xir & 0x3f);
  1055. cy_writeb(base_addr + (CyCAR << index), save_car);
  1056. }
  1057. static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
  1058. void __iomem *base_addr)
  1059. {
  1060. struct cyclades_port *info;
  1061. struct tty_struct *tty;
  1062. int char_count, index = cinfo->bus_index;
  1063. u8 save_xir, channel, save_car, outch;
  1064. /* Since we only get here when the transmit buffer
  1065. is empty, we know we can always stuff a dozen
  1066. characters. */
  1067. #ifdef CY_DEBUG_INTERRUPTS
  1068. printk(KERN_DEBUG "cyy_interrupt: xmit intr, chip %d\n", chip);
  1069. #endif
  1070. /* determine the channel & change to that context */
  1071. save_xir = readb(base_addr + (CyTIR << index));
  1072. channel = save_xir & CyIRChannel;
  1073. save_car = readb(base_addr + (CyCAR << index));
  1074. cy_writeb(base_addr + (CyCAR << index), save_xir);
  1075. /* validate the port# (as configured and open) */
  1076. if (channel + chip * 4 >= cinfo->nports) {
  1077. cy_writeb(base_addr + (CySRER << index),
  1078. readb(base_addr + (CySRER << index)) & ~CyTxRdy);
  1079. goto end;
  1080. }
  1081. info = &cinfo->ports[channel + chip * 4];
  1082. tty = tty_port_tty_get(&info->port);
  1083. if (tty == NULL) {
  1084. cy_writeb(base_addr + (CySRER << index),
  1085. readb(base_addr + (CySRER << index)) & ~CyTxRdy);
  1086. goto end;
  1087. }
  1088. /* load the on-chip space for outbound data */
  1089. char_count = info->xmit_fifo_size;
  1090. if (info->x_char) { /* send special char */
  1091. outch = info->x_char;
  1092. cy_writeb(base_addr + (CyTDR << index), outch);
  1093. char_count--;
  1094. info->icount.tx++;
  1095. info->x_char = 0;
  1096. }
  1097. if (info->breakon || info->breakoff) {
  1098. if (info->breakon) {
  1099. cy_writeb(base_addr + (CyTDR << index), 0);
  1100. cy_writeb(base_addr + (CyTDR << index), 0x81);
  1101. info->breakon = 0;
  1102. char_count -= 2;
  1103. }
  1104. if (info->breakoff) {
  1105. cy_writeb(base_addr + (CyTDR << index), 0);
  1106. cy_writeb(base_addr + (CyTDR << index), 0x83);
  1107. info->breakoff = 0;
  1108. char_count -= 2;
  1109. }
  1110. }
  1111. while (char_count-- > 0) {
  1112. if (!info->xmit_cnt) {
  1113. if (readb(base_addr + (CySRER << index)) & CyTxMpty) {
  1114. cy_writeb(base_addr + (CySRER << index),
  1115. readb(base_addr + (CySRER << index)) &
  1116. ~CyTxMpty);
  1117. } else {
  1118. cy_writeb(base_addr + (CySRER << index),
  1119. (readb(base_addr + (CySRER << index)) &
  1120. ~CyTxRdy) | CyTxMpty);
  1121. }
  1122. goto done;
  1123. }
  1124. if (info->port.xmit_buf == NULL) {
  1125. cy_writeb(base_addr + (CySRER << index),
  1126. readb(base_addr + (CySRER << index)) &
  1127. ~CyTxRdy);
  1128. goto done;
  1129. }
  1130. if (tty->stopped || tty->hw_stopped) {
  1131. cy_writeb(base_addr + (CySRER << index),
  1132. readb(base_addr + (CySRER << index)) &
  1133. ~CyTxRdy);
  1134. goto done;
  1135. }
  1136. /* Because the Embedded Transmit Commands have been enabled,
  1137. * we must check to see if the escape character, NULL, is being
  1138. * sent. If it is, we must ensure that there is room for it to
  1139. * be doubled in the output stream. Therefore we no longer
  1140. * advance the pointer when the character is fetched, but
  1141. * rather wait until after the check for a NULL output
  1142. * character. This is necessary because there may not be room
  1143. * for the two chars needed to send a NULL.)
  1144. */
  1145. outch = info->port.xmit_buf[info->xmit_tail];
  1146. if (outch) {
  1147. info->xmit_cnt--;
  1148. info->xmit_tail = (info->xmit_tail + 1) &
  1149. (SERIAL_XMIT_SIZE - 1);
  1150. cy_writeb(base_addr + (CyTDR << index), outch);
  1151. info->icount.tx++;
  1152. } else {
  1153. if (char_count > 1) {
  1154. info->xmit_cnt--;
  1155. info->xmit_tail = (info->xmit_tail + 1) &
  1156. (SERIAL_XMIT_SIZE - 1);
  1157. cy_writeb(base_addr + (CyTDR << index), outch);
  1158. cy_writeb(base_addr + (CyTDR << index), 0);
  1159. info->icount.tx++;
  1160. char_count--;
  1161. }
  1162. }
  1163. }
  1164. done:
  1165. tty_wakeup(tty);
  1166. tty_kref_put(tty);
  1167. end:
  1168. /* end of service */
  1169. cy_writeb(base_addr + (CyTIR << index), save_xir & 0x3f);
  1170. cy_writeb(base_addr + (CyCAR << index), save_car);
  1171. }
  1172. static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
  1173. void __iomem *base_addr)
  1174. {
  1175. struct cyclades_port *info;
  1176. struct tty_struct *tty;
  1177. int index = cinfo->bus_index;
  1178. u8 save_xir, channel, save_car, mdm_change, mdm_status;
  1179. /* determine the channel & change to that context */
  1180. save_xir = readb(base_addr + (CyMIR << index));
  1181. channel = save_xir & CyIRChannel;
  1182. info = &cinfo->ports[channel + chip * 4];
  1183. save_car = readb(base_addr + (CyCAR << index));
  1184. cy_writeb(base_addr + (CyCAR << index), save_xir);
  1185. mdm_change = readb(base_addr + (CyMISR << index));
  1186. mdm_status = readb(base_addr + (CyMSVR1 << index));
  1187. tty = tty_port_tty_get(&info->port);
  1188. if (!tty)
  1189. goto end;
  1190. if (mdm_change & CyANY_DELTA) {
  1191. /* For statistics only */
  1192. if (mdm_change & CyDCD)
  1193. info->icount.dcd++;
  1194. if (mdm_change & CyCTS)
  1195. info->icount.cts++;
  1196. if (mdm_change & CyDSR)
  1197. info->icount.dsr++;
  1198. if (mdm_change & CyRI)
  1199. info->icount.rng++;
  1200. wake_up_interruptible(&info->delta_msr_wait);
  1201. }
  1202. if ((mdm_change & CyDCD) && (info->port.flags & ASYNC_CHECK_CD)) {
  1203. if (mdm_status & CyDCD)
  1204. wake_up_interruptible(&info->port.open_wait);
  1205. else
  1206. tty_hangup(tty);
  1207. }
  1208. if ((mdm_change & CyCTS) && (info->port.flags & ASYNC_CTS_FLOW)) {
  1209. if (tty->hw_stopped) {
  1210. if (mdm_status & CyCTS) {
  1211. /* cy_start isn't used
  1212. because... !!! */
  1213. tty->hw_stopped = 0;
  1214. cy_writeb(base_addr + (CySRER << index),
  1215. readb(base_addr + (CySRER << index)) |
  1216. CyTxRdy);
  1217. tty_wakeup(tty);
  1218. }
  1219. } else {
  1220. if (!(mdm_status & CyCTS)) {
  1221. /* cy_stop isn't used
  1222. because ... !!! */
  1223. tty->hw_stopped = 1;
  1224. cy_writeb(base_addr + (CySRER << index),
  1225. readb(base_addr + (CySRER << index)) &
  1226. ~CyTxRdy);
  1227. }
  1228. }
  1229. }
  1230. /* if (mdm_change & CyDSR) {
  1231. }
  1232. if (mdm_change & CyRI) {
  1233. }*/
  1234. tty_kref_put(tty);
  1235. end:
  1236. /* end of service */
  1237. cy_writeb(base_addr + (CyMIR << index), save_xir & 0x3f);
  1238. cy_writeb(base_addr + (CyCAR << index), save_car);
  1239. }
  1240. /* The real interrupt service routine is called
  1241. whenever the card wants its hand held--chars
  1242. received, out buffer empty, modem change, etc.
  1243. */
  1244. static irqreturn_t cyy_interrupt(int irq, void *dev_id)
  1245. {
  1246. int status;
  1247. struct cyclades_card *cinfo = dev_id;
  1248. void __iomem *base_addr, *card_base_addr;
  1249. unsigned int chip, too_many, had_work;
  1250. int index;
  1251. if (unlikely(cinfo == NULL)) {
  1252. #ifdef CY_DEBUG_INTERRUPTS
  1253. printk(KERN_DEBUG "cyy_interrupt: spurious interrupt %d\n",
  1254. irq);
  1255. #endif
  1256. return IRQ_NONE; /* spurious interrupt */
  1257. }
  1258. card_base_addr = cinfo->base_addr;
  1259. index = cinfo->bus_index;
  1260. /* card was not initialized yet (e.g. DEBUG_SHIRQ) */
  1261. if (unlikely(card_base_addr == NULL))
  1262. return IRQ_HANDLED;
  1263. /* This loop checks all chips in the card. Make a note whenever
  1264. _any_ chip had some work to do, as this is considered an
  1265. indication that there will be more to do. Only when no chip
  1266. has any work does this outermost loop exit.
  1267. */
  1268. do {
  1269. had_work = 0;
  1270. for (chip = 0; chip < cinfo->num_chips; chip++) {
  1271. base_addr = cinfo->base_addr +
  1272. (cy_chip_offset[chip] << index);
  1273. too_many = 0;
  1274. while ((status = readb(base_addr +
  1275. (CySVRR << index))) != 0x00) {
  1276. had_work++;
  1277. /* The purpose of the following test is to ensure that
  1278. no chip can monopolize the driver. This forces the
  1279. chips to be checked in a round-robin fashion (after
  1280. draining each of a bunch (1000) of characters).
  1281. */
  1282. if (1000 < too_many++)
  1283. break;
  1284. spin_lock(&cinfo->card_lock);
  1285. if (status & CySRReceive) /* rx intr */
  1286. cyy_chip_rx(cinfo, chip, base_addr);
  1287. if (status & CySRTransmit) /* tx intr */
  1288. cyy_chip_tx(cinfo, chip, base_addr);
  1289. if (status & CySRModem) /* modem intr */
  1290. cyy_chip_modem(cinfo, chip, base_addr);
  1291. spin_unlock(&cinfo->card_lock);
  1292. }
  1293. }
  1294. } while (had_work);
  1295. /* clear interrupts */
  1296. spin_lock(&cinfo->card_lock);
  1297. cy_writeb(card_base_addr + (Cy_ClrIntr << index), 0);
  1298. /* Cy_ClrIntr is 0x1800 */
  1299. spin_unlock(&cinfo->card_lock);
  1300. return IRQ_HANDLED;
  1301. } /* cyy_interrupt */
  1302. /***********************************************************/
  1303. /********* End of block of Cyclom-Y specific code **********/
  1304. /******** Start of block of Cyclades-Z specific code *******/
  1305. /***********************************************************/
  1306. static int
  1307. cyz_fetch_msg(struct cyclades_card *cinfo,
  1308. __u32 *channel, __u8 *cmd, __u32 *param)
  1309. {
  1310. struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
  1311. unsigned long loc_doorbell;
  1312. loc_doorbell = readl(&cinfo->ctl_addr.p9060->loc_doorbell);
  1313. if (loc_doorbell) {
  1314. *cmd = (char)(0xff & loc_doorbell);
  1315. *channel = readl(&board_ctrl->fwcmd_channel);
  1316. *param = (__u32) readl(&board_ctrl->fwcmd_param);
  1317. cy_writel(&cinfo->ctl_addr.p9060->loc_doorbell, 0xffffffff);
  1318. return 1;
  1319. }
  1320. return 0;
  1321. } /* cyz_fetch_msg */
  1322. static int
  1323. cyz_issue_cmd(struct cyclades_card *cinfo,
  1324. __u32 channel, __u8 cmd, __u32 param)
  1325. {
  1326. struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
  1327. __u32 __iomem *pci_doorbell;
  1328. unsigned int index;
  1329. if (!cyz_is_loaded(cinfo))
  1330. return -1;
  1331. index = 0;
  1332. pci_doorbell = &cinfo->ctl_addr.p9060->pci_doorbell;
  1333. while ((readl(pci_doorbell) & 0xff) != 0) {
  1334. if (index++ == 1000)
  1335. return (int)(readl(pci_doorbell) & 0xff);
  1336. udelay(50L);
  1337. }
  1338. cy_writel(&board_ctrl->hcmd_channel, channel);
  1339. cy_writel(&board_ctrl->hcmd_param, param);
  1340. cy_writel(pci_doorbell, (long)cmd);
  1341. return 0;
  1342. } /* cyz_issue_cmd */
  1343. static void cyz_handle_rx(struct cyclades_port *info, struct tty_struct *tty)
  1344. {
  1345. struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
  1346. struct cyclades_card *cinfo = info->card;
  1347. unsigned int char_count;
  1348. int len;
  1349. #ifdef BLOCKMOVE
  1350. unsigned char *buf;
  1351. #else
  1352. char data;
  1353. #endif
  1354. __u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
  1355. rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
  1356. rx_put = readl(&buf_ctrl->rx_put);
  1357. rx_bufsize = readl(&buf_ctrl->rx_bufsize);
  1358. rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
  1359. if (rx_put >= rx_get)
  1360. char_count = rx_put - rx_get;
  1361. else
  1362. char_count = rx_put - rx_get + rx_bufsize;
  1363. if (char_count) {
  1364. #ifdef CY_ENABLE_MONITORING
  1365. info->mon.int_count++;
  1366. info->mon.char_count += char_count;
  1367. if (char_count > info->mon.char_max)
  1368. info->mon.char_max = char_count;
  1369. info->mon.char_last = char_count;
  1370. #endif
  1371. if (tty == NULL) {
  1372. /* flush received characters */
  1373. new_rx_get = (new_rx_get + char_count) &
  1374. (rx_bufsize - 1);
  1375. info->rflush_count++;
  1376. } else {
  1377. #ifdef BLOCKMOVE
  1378. /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
  1379. for performance, but because of buffer boundaries, there
  1380. may be several steps to the operation */
  1381. while (1) {
  1382. len = tty_prepare_flip_string(tty, &buf,
  1383. char_count);
  1384. if (!len)
  1385. break;
  1386. len = min_t(unsigned int, min(len, char_count),
  1387. rx_bufsize - new_rx_get);
  1388. memcpy_fromio(buf, cinfo->base_addr +
  1389. rx_bufaddr + new_rx_get, len);
  1390. new_rx_get = (new_rx_get + len) &
  1391. (rx_bufsize - 1);
  1392. char_count -= len;
  1393. info->icount.rx += len;
  1394. info->idle_stats.recv_bytes += len;
  1395. }
  1396. #else
  1397. len = tty_buffer_request_room(tty, char_count);
  1398. while (len--) {
  1399. data = readb(cinfo->base_addr + rx_bufaddr +
  1400. new_rx_get);
  1401. new_rx_get = (new_rx_get + 1) &
  1402. (rx_bufsize - 1);
  1403. tty_insert_flip_char(tty, data, TTY_NORMAL);
  1404. info->idle_stats.recv_bytes++;
  1405. info->icount.rx++;
  1406. }
  1407. #endif
  1408. #ifdef CONFIG_CYZ_INTR
  1409. /* Recalculate the number of chars in the RX buffer and issue
  1410. a cmd in case it's higher than the RX high water mark */
  1411. rx_put = readl(&buf_ctrl->rx_put);
  1412. if (rx_put >= rx_get)
  1413. char_count = rx_put - rx_get;
  1414. else
  1415. char_count = rx_put - rx_get + rx_bufsize;
  1416. if (char_count >= readl(&buf_ctrl->rx_threshold) &&
  1417. !timer_pending(&cyz_rx_full_timer[
  1418. info->line]))
  1419. mod_timer(&cyz_rx_full_timer[info->line],
  1420. jiffies + 1);
  1421. #endif
  1422. info->idle_stats.recv_idle = jiffies;
  1423. tty_schedule_flip(tty);
  1424. }
  1425. /* Update rx_get */
  1426. cy_writel(&buf_ctrl->rx_get, new_rx_get);
  1427. }
  1428. }
  1429. static void cyz_handle_tx(struct cyclades_port *info, struct tty_struct *tty)
  1430. {
  1431. struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
  1432. struct cyclades_card *cinfo = info->card;
  1433. u8 data;
  1434. unsigned int char_count;
  1435. #ifdef BLOCKMOVE
  1436. int small_count;
  1437. #endif
  1438. __u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
  1439. if (info->xmit_cnt <= 0) /* Nothing to transmit */
  1440. return;
  1441. tx_get = readl(&buf_ctrl->tx_get);
  1442. tx_put = readl(&buf_ctrl->tx_put);
  1443. tx_bufsize = readl(&buf_ctrl->tx_bufsize);
  1444. tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
  1445. if (tx_put >= tx_get)
  1446. char_count = tx_get - tx_put - 1 + tx_bufsize;
  1447. else
  1448. char_count = tx_get - tx_put - 1;
  1449. if (char_count) {
  1450. if (tty == NULL)
  1451. goto ztxdone;
  1452. if (info->x_char) { /* send special char */
  1453. data = info->x_char;
  1454. cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
  1455. tx_put = (tx_put + 1) & (tx_bufsize - 1);
  1456. info->x_char = 0;
  1457. char_count--;
  1458. info->icount.tx++;
  1459. }
  1460. #ifdef BLOCKMOVE
  1461. while (0 < (small_count = min_t(unsigned int,
  1462. tx_bufsize - tx_put, min_t(unsigned int,
  1463. (SERIAL_XMIT_SIZE - info->xmit_tail),
  1464. min_t(unsigned int, info->xmit_cnt,
  1465. char_count))))) {
  1466. memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
  1467. tx_put),
  1468. &info->port.xmit_buf[info->xmit_tail],
  1469. small_count);
  1470. tx_put = (tx_put + small_count) & (tx_bufsize - 1);
  1471. char_count -= small_count;
  1472. info->icount.tx += small_count;
  1473. info->xmit_cnt -= small_count;
  1474. info->xmit_tail = (info->xmit_tail + small_count) &
  1475. (SERIAL_XMIT_SIZE - 1);
  1476. }
  1477. #else
  1478. while (info->xmit_cnt && char_count) {
  1479. data = info->port.xmit_buf[info->xmit_tail];
  1480. info->xmit_cnt--;
  1481. info->xmit_tail = (info->xmit_tail + 1) &
  1482. (SERIAL_XMIT_SIZE - 1);
  1483. cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
  1484. tx_put = (tx_put + 1) & (tx_bufsize - 1);
  1485. char_count--;
  1486. info->icount.tx++;
  1487. }
  1488. #endif
  1489. tty_wakeup(tty);
  1490. ztxdone:
  1491. /* Update tx_put */
  1492. cy_writel(&buf_ctrl->tx_put, tx_put);
  1493. }
  1494. }
  1495. static void cyz_handle_cmd(struct cyclades_card *cinfo)
  1496. {
  1497. struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
  1498. struct tty_struct *tty;
  1499. struct cyclades_port *info;
  1500. __u32 channel, param, fw_ver;
  1501. __u8 cmd;
  1502. int special_count;
  1503. int delta_count;
  1504. fw_ver = readl(&board_ctrl->fw_version);
  1505. while (cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
  1506. special_count = 0;
  1507. delta_count = 0;
  1508. info = &cinfo->ports[channel];
  1509. tty = tty_port_tty_get(&info->port);
  1510. if (tty == NULL)
  1511. continue;
  1512. switch (cmd) {
  1513. case C_CM_PR_ERROR:
  1514. tty_insert_flip_char(tty, 0, TTY_PARITY);
  1515. info->icount.rx++;
  1516. special_count++;
  1517. break;
  1518. case C_CM_FR_ERROR:
  1519. tty_insert_flip_char(tty, 0, TTY_FRAME);
  1520. info->icount.rx++;
  1521. special_count++;
  1522. break;
  1523. case C_CM_RXBRK:
  1524. tty_insert_flip_char(tty, 0, TTY_BREAK);
  1525. info->icount.rx++;
  1526. special_count++;
  1527. break;
  1528. case C_CM_MDCD:
  1529. info->icount.dcd++;
  1530. delta_count++;
  1531. if (info->port.flags & ASYNC_CHECK_CD) {
  1532. u32 dcd = fw_ver > 241 ? param :
  1533. readl(&info->u.cyz.ch_ctrl->rs_status);
  1534. if (dcd & C_RS_DCD)
  1535. wake_up_interruptible(&info->port.open_wait);
  1536. else
  1537. tty_hangup(tty);
  1538. }
  1539. break;
  1540. case C_CM_MCTS:
  1541. info->icount.cts++;
  1542. delta_count++;
  1543. break;
  1544. case C_CM_MRI:
  1545. info->icount.rng++;
  1546. delta_count++;
  1547. break;
  1548. case C_CM_MDSR:
  1549. info->icount.dsr++;
  1550. delta_count++;
  1551. break;
  1552. #ifdef Z_WAKE
  1553. case C_CM_IOCTLW:
  1554. complete(&info->shutdown_wait);
  1555. break;
  1556. #endif
  1557. #ifdef CONFIG_CYZ_INTR
  1558. case C_CM_RXHIWM:
  1559. case C_CM_RXNNDT:
  1560. case C_CM_INTBACK2:
  1561. /* Reception Interrupt */
  1562. #ifdef CY_DEBUG_INTERRUPTS
  1563. printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
  1564. "port %ld\n", info->card, channel);
  1565. #endif
  1566. cyz_handle_rx(info, tty);
  1567. break;
  1568. case C_CM_TXBEMPTY:
  1569. case C_CM_TXLOWWM:
  1570. case C_CM_INTBACK:
  1571. /* Transmission Interrupt */
  1572. #ifdef CY_DEBUG_INTERRUPTS
  1573. printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
  1574. "port %ld\n", info->card, channel);
  1575. #endif
  1576. cyz_handle_tx(info, tty);
  1577. break;
  1578. #endif /* CONFIG_CYZ_INTR */
  1579. case C_CM_FATAL:
  1580. /* should do something with this !!! */
  1581. break;
  1582. default:
  1583. break;
  1584. }
  1585. if (delta_count)
  1586. wake_up_interruptible(&info->delta_msr_wait);
  1587. if (special_count)
  1588. tty_schedule_flip(tty);
  1589. tty_kref_put(tty);
  1590. }
  1591. }
  1592. #ifdef CONFIG_CYZ_INTR
  1593. static irqreturn_t cyz_interrupt(int irq, void *dev_id)
  1594. {
  1595. struct cyclades_card *cinfo = dev_id;
  1596. if (unlikely(!cyz_is_loaded(cinfo))) {
  1597. #ifdef CY_DEBUG_INTERRUPTS
  1598. printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
  1599. "(IRQ%d).\n", irq);
  1600. #endif
  1601. return IRQ_NONE;
  1602. }
  1603. /* Handle the interrupts */
  1604. cyz_handle_cmd(cinfo);
  1605. return IRQ_HANDLED;
  1606. } /* cyz_interrupt */
  1607. static void cyz_rx_restart(unsigned long arg)
  1608. {
  1609. struct cyclades_port *info = (struct cyclades_port *)arg;
  1610. struct cyclades_card *card = info->card;
  1611. int retval;
  1612. __u32 channel = info->line - card->first_line;
  1613. unsigned long flags;
  1614. spin_lock_irqsave(&card->card_lock, flags);
  1615. retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
  1616. if (retval != 0) {
  1617. printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
  1618. info->line, retval);
  1619. }
  1620. spin_unlock_irqrestore(&card->card_lock, flags);
  1621. }
  1622. #else /* CONFIG_CYZ_INTR */
  1623. static void cyz_poll(unsigned long arg)
  1624. {
  1625. struct cyclades_card *cinfo;
  1626. struct cyclades_port *info;
  1627. unsigned long expires = jiffies + HZ;
  1628. unsigned int port, card;
  1629. for (card = 0; card < NR_CARDS; card++) {
  1630. cinfo = &cy_card[card];
  1631. if (!cy_is_Z(cinfo))
  1632. continue;
  1633. if (!cyz_is_loaded(cinfo))
  1634. continue;
  1635. /* Skip first polling cycle to avoid racing conditions with the FW */
  1636. if (!cinfo->intr_enabled) {
  1637. cinfo->intr_enabled = 1;
  1638. continue;
  1639. }
  1640. cyz_handle_cmd(cinfo);
  1641. for (port = 0; port < cinfo->nports; port++) {
  1642. struct tty_struct *tty;
  1643. info = &cinfo->ports[port];
  1644. tty = tty_port_tty_get(&info->port);
  1645. /* OK to pass NULL to the handle functions below.
  1646. They need to drop the data in that case. */
  1647. if (!info->throttle)
  1648. cyz_handle_rx(info, tty);
  1649. cyz_handle_tx(info, tty);
  1650. tty_kref_put(tty);
  1651. }
  1652. /* poll every 'cyz_polling_cycle' period */
  1653. expires = jiffies + cyz_polling_cycle;
  1654. }
  1655. mod_timer(&cyz_timerlist, expires);
  1656. } /* cyz_poll */
  1657. #endif /* CONFIG_CYZ_INTR */
  1658. /********** End of block of Cyclades-Z specific code *********/
  1659. /***********************************************************/
  1660. /* This is called whenever a port becomes active;
  1661. interrupts are enabled and DTR & RTS are turned on.
  1662. */
  1663. static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
  1664. {
  1665. struct cyclades_card *card;
  1666. unsigned long flags;
  1667. int retval = 0;
  1668. void __iomem *base_addr;
  1669. int chip, channel, index;
  1670. unsigned long page;
  1671. card = info->card;
  1672. channel = info->line - card->first_line;
  1673. page = get_zeroed_page(GFP_KERNEL);
  1674. if (!page)
  1675. return -ENOMEM;
  1676. spin_lock_irqsave(&card->card_lock, flags);
  1677. if (info->port.flags & ASYNC_INITIALIZED) {
  1678. free_page(page);
  1679. goto errout;
  1680. }
  1681. if (!info->type) {
  1682. set_bit(TTY_IO_ERROR, &tty->flags);
  1683. free_page(page);
  1684. goto errout;
  1685. }
  1686. if (info->port.xmit_buf)
  1687. free_page(page);
  1688. else
  1689. info->port.xmit_buf = (unsigned char *)page;
  1690. spin_unlock_irqrestore(&card->card_lock, flags);
  1691. cy_set_line_char(info, tty);
  1692. if (!cy_is_Z(card)) {
  1693. chip = channel >> 2;
  1694. channel &= 0x03;
  1695. index = card->bus_index;
  1696. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  1697. #ifdef CY_DEBUG_OPEN
  1698. printk(KERN_DEBUG "cyc startup card %d, chip %d, channel %d, "
  1699. "base_addr %p\n",
  1700. card, chip, channel, base_addr);
  1701. #endif
  1702. spin_lock_irqsave(&card->card_lock, flags);
  1703. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  1704. cy_writeb(base_addr + (CyRTPR << index),
  1705. (info->default_timeout ? info->default_timeout : 0x02));
  1706. /* 10ms rx timeout */
  1707. cyy_issue_cmd(base_addr, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR,
  1708. index);
  1709. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  1710. cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
  1711. cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
  1712. #ifdef CY_DEBUG_DTR
  1713. printk(KERN_DEBUG "cyc:startup raising DTR\n");
  1714. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  1715. readb(base_addr + (CyMSVR1 << index)),
  1716. readb(base_addr + (CyMSVR2 << index)));
  1717. #endif
  1718. cy_writeb(base_addr + (CySRER << index),
  1719. readb(base_addr + (CySRER << index)) | CyRxData);
  1720. info->port.flags |= ASYNC_INITIALIZED;
  1721. clear_bit(TTY_IO_ERROR, &tty->flags);
  1722. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1723. info->breakon = info->breakoff = 0;
  1724. memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
  1725. info->idle_stats.in_use =
  1726. info->idle_stats.recv_idle =
  1727. info->idle_stats.xmit_idle = jiffies;
  1728. spin_unlock_irqrestore(&card->card_lock, flags);
  1729. } else {
  1730. struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
  1731. if (!cyz_is_loaded(card))
  1732. return -ENODEV;
  1733. #ifdef CY_DEBUG_OPEN
  1734. printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
  1735. "base_addr %p\n", card, channel, card->base_addr);
  1736. #endif
  1737. spin_lock_irqsave(&card->card_lock, flags);
  1738. cy_writel(&ch_ctrl->op_mode, C_CH_ENABLE);
  1739. #ifdef Z_WAKE
  1740. #ifdef CONFIG_CYZ_INTR
  1741. cy_writel(&ch_ctrl->intr_enable,
  1742. C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
  1743. C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
  1744. #else
  1745. cy_writel(&ch_ctrl->intr_enable,
  1746. C_IN_IOCTLW | C_IN_MDCD);
  1747. #endif /* CONFIG_CYZ_INTR */
  1748. #else
  1749. #ifdef CONFIG_CYZ_INTR
  1750. cy_writel(&ch_ctrl->intr_enable,
  1751. C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
  1752. C_IN_RXNNDT | C_IN_MDCD);
  1753. #else
  1754. cy_writel(&ch_ctrl->intr_enable, C_IN_MDCD);
  1755. #endif /* CONFIG_CYZ_INTR */
  1756. #endif /* Z_WAKE */
  1757. retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
  1758. if (retval != 0) {
  1759. printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
  1760. "%x\n", info->line, retval);
  1761. }
  1762. /* Flush RX buffers before raising DTR and RTS */
  1763. retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
  1764. if (retval != 0) {
  1765. printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
  1766. "%x\n", info->line, retval);
  1767. }
  1768. /* set timeout !!! */
  1769. /* set RTS and DTR !!! */
  1770. cy_writel(&ch_ctrl->rs_control, readl(&ch_ctrl->rs_control) |
  1771. C_RS_RTS | C_RS_DTR);
  1772. retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
  1773. if (retval != 0) {
  1774. printk(KERN_ERR "cyc:startup(3) retval on ttyC%d was "
  1775. "%x\n", info->line, retval);
  1776. }
  1777. #ifdef CY_DEBUG_DTR
  1778. printk(KERN_DEBUG "cyc:startup raising Z DTR\n");
  1779. #endif
  1780. /* enable send, recv, modem !!! */
  1781. info->port.flags |= ASYNC_INITIALIZED;
  1782. clear_bit(TTY_IO_ERROR, &tty->flags);
  1783. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1784. info->breakon = info->breakoff = 0;
  1785. memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
  1786. info->idle_stats.in_use =
  1787. info->idle_stats.recv_idle =
  1788. info->idle_stats.xmit_idle = jiffies;
  1789. spin_unlock_irqrestore(&card->card_lock, flags);
  1790. }
  1791. #ifdef CY_DEBUG_OPEN
  1792. printk(KERN_DEBUG "cyc startup done\n");
  1793. #endif
  1794. return 0;
  1795. errout:
  1796. spin_unlock_irqrestore(&card->card_lock, flags);
  1797. return retval;
  1798. } /* startup */
  1799. static void start_xmit(struct cyclades_port *info)
  1800. {
  1801. struct cyclades_card *card;
  1802. unsigned long flags;
  1803. void __iomem *base_addr;
  1804. int chip, channel, index;
  1805. card = info->card;
  1806. channel = info->line - card->first_line;
  1807. if (!cy_is_Z(card)) {
  1808. chip = channel >> 2;
  1809. channel &= 0x03;
  1810. index = card->bus_index;
  1811. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  1812. spin_lock_irqsave(&card->card_lock, flags);
  1813. cy_writeb(base_addr + (CyCAR << index), channel);
  1814. cy_writeb(base_addr + (CySRER << index),
  1815. readb(base_addr + (CySRER << index)) | CyTxRdy);
  1816. spin_unlock_irqrestore(&card->card_lock, flags);
  1817. } else {
  1818. #ifdef CONFIG_CYZ_INTR
  1819. int retval;
  1820. spin_lock_irqsave(&card->card_lock, flags);
  1821. retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
  1822. if (retval != 0) {
  1823. printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
  1824. "%x\n", info->line, retval);
  1825. }
  1826. spin_unlock_irqrestore(&card->card_lock, flags);
  1827. #else /* CONFIG_CYZ_INTR */
  1828. /* Don't have to do anything at this time */
  1829. #endif /* CONFIG_CYZ_INTR */
  1830. }
  1831. } /* start_xmit */
  1832. /*
  1833. * This routine shuts down a serial port; interrupts are disabled,
  1834. * and DTR is dropped if the hangup on close termio flag is on.
  1835. */
  1836. static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
  1837. {
  1838. struct cyclades_card *card;
  1839. unsigned long flags;
  1840. void __iomem *base_addr;
  1841. int chip, channel, index;
  1842. if (!(info->port.flags & ASYNC_INITIALIZED))
  1843. return;
  1844. card = info->card;
  1845. channel = info->line - card->first_line;
  1846. if (!cy_is_Z(card)) {
  1847. chip = channel >> 2;
  1848. channel &= 0x03;
  1849. index = card->bus_index;
  1850. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  1851. #ifdef CY_DEBUG_OPEN
  1852. printk(KERN_DEBUG "cyc shutdown Y card %d, chip %d, "
  1853. "channel %d, base_addr %p\n",
  1854. card, chip, channel, base_addr);
  1855. #endif
  1856. spin_lock_irqsave(&card->card_lock, flags);
  1857. /* Clear delta_msr_wait queue to avoid mem leaks. */
  1858. wake_up_interruptible(&info->delta_msr_wait);
  1859. if (info->port.xmit_buf) {
  1860. unsigned char *temp;
  1861. temp = info->port.xmit_buf;
  1862. info->port.xmit_buf = NULL;
  1863. free_page((unsigned long)temp);
  1864. }
  1865. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  1866. if (tty->termios->c_cflag & HUPCL) {
  1867. cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
  1868. cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
  1869. #ifdef CY_DEBUG_DTR
  1870. printk(KERN_DEBUG "cyc shutdown dropping DTR\n");
  1871. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  1872. readb(base_addr + (CyMSVR1 << index)),
  1873. readb(base_addr + (CyMSVR2 << index)));
  1874. #endif
  1875. }
  1876. cyy_issue_cmd(base_addr, CyCHAN_CTL | CyDIS_RCVR, index);
  1877. /* it may be appropriate to clear _XMIT at
  1878. some later date (after testing)!!! */
  1879. set_bit(TTY_IO_ERROR, &tty->flags);
  1880. info->port.flags &= ~ASYNC_INITIALIZED;
  1881. spin_unlock_irqrestore(&card->card_lock, flags);
  1882. } else {
  1883. struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
  1884. int retval;
  1885. #ifdef CY_DEBUG_OPEN
  1886. printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
  1887. "base_addr %p\n", card, channel, card->base_addr);
  1888. #endif
  1889. if (!cyz_is_loaded(card))
  1890. return;
  1891. spin_lock_irqsave(&card->card_lock, flags);
  1892. if (info->port.xmit_buf) {
  1893. unsigned char *temp;
  1894. temp = info->port.xmit_buf;
  1895. info->port.xmit_buf = NULL;
  1896. free_page((unsigned long)temp);
  1897. }
  1898. if (tty->termios->c_cflag & HUPCL) {
  1899. cy_writel(&ch_ctrl->rs_control,
  1900. readl(&ch_ctrl->rs_control) &
  1901. ~(C_RS_RTS | C_RS_DTR));
  1902. retval = cyz_issue_cmd(info->card, channel,
  1903. C_CM_IOCTLM, 0L);
  1904. if (retval != 0) {
  1905. printk(KERN_ERR"cyc:shutdown retval on ttyC%d "
  1906. "was %x\n", info->line, retval);
  1907. }
  1908. #ifdef CY_DEBUG_DTR
  1909. printk(KERN_DEBUG "cyc:shutdown dropping Z DTR\n");
  1910. #endif
  1911. }
  1912. set_bit(TTY_IO_ERROR, &tty->flags);
  1913. info->port.flags &= ~ASYNC_INITIALIZED;
  1914. spin_unlock_irqrestore(&card->card_lock, flags);
  1915. }
  1916. #ifdef CY_DEBUG_OPEN
  1917. printk(KERN_DEBUG "cyc shutdown done\n");
  1918. #endif
  1919. } /* shutdown */
  1920. /*
  1921. * ------------------------------------------------------------
  1922. * cy_open() and friends
  1923. * ------------------------------------------------------------
  1924. */
  1925. /*
  1926. * This routine is called whenever a serial port is opened. It
  1927. * performs the serial-specific initialization for the tty structure.
  1928. */
  1929. static int cy_open(struct tty_struct *tty, struct file *filp)
  1930. {
  1931. struct cyclades_port *info;
  1932. unsigned int i, line;
  1933. int retval;
  1934. line = tty->index;
  1935. if (tty->index < 0 || NR_PORTS <= line)
  1936. return -ENODEV;
  1937. for (i = 0; i < NR_CARDS; i++)
  1938. if (line < cy_card[i].first_line + cy_card[i].nports &&
  1939. line >= cy_card[i].first_line)
  1940. break;
  1941. if (i >= NR_CARDS)
  1942. return -ENODEV;
  1943. info = &cy_card[i].ports[line - cy_card[i].first_line];
  1944. if (info->line < 0)
  1945. return -ENODEV;
  1946. /* If the card's firmware hasn't been loaded,
  1947. treat it as absent from the system. This
  1948. will make the user pay attention.
  1949. */
  1950. if (cy_is_Z(info->card)) {
  1951. struct cyclades_card *cinfo = info->card;
  1952. struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
  1953. if (!cyz_is_loaded(cinfo)) {
  1954. if (cinfo->hw_ver == ZE_V1 && cyz_fpga_loaded(cinfo) &&
  1955. readl(&firm_id->signature) ==
  1956. ZFIRM_HLT) {
  1957. printk(KERN_ERR "cyc:Cyclades-Z Error: you "
  1958. "need an external power supply for "
  1959. "this number of ports.\nFirmware "
  1960. "halted.\n");
  1961. } else {
  1962. printk(KERN_ERR "cyc:Cyclades-Z firmware not "
  1963. "yet loaded\n");
  1964. }
  1965. return -ENODEV;
  1966. }
  1967. #ifdef CONFIG_CYZ_INTR
  1968. else {
  1969. /* In case this Z board is operating in interrupt mode, its
  1970. interrupts should be enabled as soon as the first open
  1971. happens to one of its ports. */
  1972. if (!cinfo->intr_enabled) {
  1973. u16 intr;
  1974. /* Enable interrupts on the PLX chip */
  1975. intr = readw(&cinfo->ctl_addr.p9060->
  1976. intr_ctrl_stat) | 0x0900;
  1977. cy_writew(&cinfo->ctl_addr.p9060->
  1978. intr_ctrl_stat, intr);
  1979. /* Enable interrupts on the FW */
  1980. retval = cyz_issue_cmd(cinfo, 0,
  1981. C_CM_IRQ_ENBL, 0L);
  1982. if (retval != 0) {
  1983. printk(KERN_ERR "cyc:IRQ enable retval "
  1984. "was %x\n", retval);
  1985. }
  1986. cinfo->intr_enabled = 1;
  1987. }
  1988. }
  1989. #endif /* CONFIG_CYZ_INTR */
  1990. /* Make sure this Z port really exists in hardware */
  1991. if (info->line > (cinfo->first_line + cinfo->nports - 1))
  1992. return -ENODEV;
  1993. }
  1994. #ifdef CY_DEBUG_OTHER
  1995. printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
  1996. #endif
  1997. tty->driver_data = info;
  1998. if (serial_paranoia_check(info, tty->name, "cy_open"))
  1999. return -ENODEV;
  2000. #ifdef CY_DEBUG_OPEN
  2001. printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
  2002. info->port.count);
  2003. #endif
  2004. info->port.count++;
  2005. #ifdef CY_DEBUG_COUNT
  2006. printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
  2007. current->pid, info->port.count);
  2008. #endif
  2009. /*
  2010. * If the port is the middle of closing, bail out now
  2011. */
  2012. if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
  2013. wait_event_interruptible(info->port.close_wait,
  2014. !(info->port.flags & ASYNC_CLOSING));
  2015. return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
  2016. }
  2017. /*
  2018. * Start up serial port
  2019. */
  2020. retval = cy_startup(info, tty);
  2021. if (retval)
  2022. return retval;
  2023. retval = tty_port_block_til_ready(&info->port, tty, filp);
  2024. if (retval) {
  2025. #ifdef CY_DEBUG_OPEN
  2026. printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
  2027. "with %d\n", retval);
  2028. #endif
  2029. return retval;
  2030. }
  2031. info->throttle = 0;
  2032. tty_port_tty_set(&info->port, tty);
  2033. #ifdef CY_DEBUG_OPEN
  2034. printk(KERN_DEBUG "cyc:cy_open done\n");
  2035. #endif
  2036. return 0;
  2037. } /* cy_open */
  2038. /*
  2039. * cy_wait_until_sent() --- wait until the transmitter is empty
  2040. */
  2041. static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
  2042. {
  2043. struct cyclades_card *card;
  2044. struct cyclades_port *info = tty->driver_data;
  2045. void __iomem *base_addr;
  2046. int chip, channel, index;
  2047. unsigned long orig_jiffies;
  2048. int char_time;
  2049. if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
  2050. return;
  2051. if (info->xmit_fifo_size == 0)
  2052. return; /* Just in case.... */
  2053. orig_jiffies = jiffies;
  2054. lock_kernel();
  2055. /*
  2056. * Set the check interval to be 1/5 of the estimated time to
  2057. * send a single character, and make it at least 1. The check
  2058. * interval should also be less than the timeout.
  2059. *
  2060. * Note: we have to use pretty tight timings here to satisfy
  2061. * the NIST-PCTS.
  2062. */
  2063. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  2064. char_time = char_time / 5;
  2065. if (char_time <= 0)
  2066. char_time = 1;
  2067. if (timeout < 0)
  2068. timeout = 0;
  2069. if (timeout)
  2070. char_time = min(char_time, timeout);
  2071. /*
  2072. * If the transmitter hasn't cleared in twice the approximate
  2073. * amount of time to send the entire FIFO, it probably won't
  2074. * ever clear. This assumes the UART isn't doing flow
  2075. * control, which is currently the case. Hence, if it ever
  2076. * takes longer than info->timeout, this is probably due to a
  2077. * UART bug of some kind. So, we clamp the timeout parameter at
  2078. * 2*info->timeout.
  2079. */
  2080. if (!timeout || timeout > 2 * info->timeout)
  2081. timeout = 2 * info->timeout;
  2082. #ifdef CY_DEBUG_WAIT_UNTIL_SENT
  2083. printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
  2084. timeout, char_time, jiffies);
  2085. #endif
  2086. card = info->card;
  2087. channel = (info->line) - (card->first_line);
  2088. if (!cy_is_Z(card)) {
  2089. chip = channel >> 2;
  2090. channel &= 0x03;
  2091. index = card->bus_index;
  2092. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  2093. while (readb(base_addr + (CySRER << index)) & CyTxRdy) {
  2094. #ifdef CY_DEBUG_WAIT_UNTIL_SENT
  2095. printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
  2096. #endif
  2097. if (msleep_interruptible(jiffies_to_msecs(char_time)))
  2098. break;
  2099. if (timeout && time_after(jiffies, orig_jiffies +
  2100. timeout))
  2101. break;
  2102. }
  2103. }
  2104. /* Run one more char cycle */
  2105. msleep_interruptible(jiffies_to_msecs(char_time * 5));
  2106. unlock_kernel();
  2107. #ifdef CY_DEBUG_WAIT_UNTIL_SENT
  2108. printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
  2109. #endif
  2110. }
  2111. static void cy_flush_buffer(struct tty_struct *tty)
  2112. {
  2113. struct cyclades_port *info = tty->driver_data;
  2114. struct cyclades_card *card;
  2115. int channel, retval;
  2116. unsigned long flags;
  2117. #ifdef CY_DEBUG_IO
  2118. printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
  2119. #endif
  2120. if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
  2121. return;
  2122. card = info->card;
  2123. channel = info->line - card->first_line;
  2124. spin_lock_irqsave(&card->card_lock, flags);
  2125. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  2126. spin_unlock_irqrestore(&card->card_lock, flags);
  2127. if (cy_is_Z(card)) { /* If it is a Z card, flush the on-board
  2128. buffers as well */
  2129. spin_lock_irqsave(&card->card_lock, flags);
  2130. retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
  2131. if (retval != 0) {
  2132. printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
  2133. "was %x\n", info->line, retval);
  2134. }
  2135. spin_unlock_irqrestore(&card->card_lock, flags);
  2136. }
  2137. tty_wakeup(tty);
  2138. } /* cy_flush_buffer */
  2139. /*
  2140. * This routine is called when a particular tty device is closed.
  2141. */
  2142. static void cy_close(struct tty_struct *tty, struct file *filp)
  2143. {
  2144. struct cyclades_port *info = tty->driver_data;
  2145. struct cyclades_card *card;
  2146. unsigned long flags;
  2147. #ifdef CY_DEBUG_OTHER
  2148. printk(KERN_DEBUG "cyc:cy_close ttyC%d\n", info->line);
  2149. #endif
  2150. if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
  2151. return;
  2152. card = info->card;
  2153. spin_lock_irqsave(&card->card_lock, flags);
  2154. /* If the TTY is being hung up, nothing to do */
  2155. if (tty_hung_up_p(filp)) {
  2156. spin_unlock_irqrestore(&card->card_lock, flags);
  2157. return;
  2158. }
  2159. #ifdef CY_DEBUG_OPEN
  2160. printk(KERN_DEBUG "cyc:cy_close ttyC%d, count = %d\n", info->line,
  2161. info->port.count);
  2162. #endif
  2163. if ((tty->count == 1) && (info->port.count != 1)) {
  2164. /*
  2165. * Uh, oh. tty->count is 1, which means that the tty
  2166. * structure will be freed. Info->count should always
  2167. * be one in these conditions. If it's greater than
  2168. * one, we've got real problems, since it means the
  2169. * serial port won't be shutdown.
  2170. */
  2171. printk(KERN_ERR "cyc:cy_close: bad serial port count; "
  2172. "tty->count is 1, info->port.count is %d\n", info->port.count);
  2173. info->port.count = 1;
  2174. }
  2175. #ifdef CY_DEBUG_COUNT
  2176. printk(KERN_DEBUG "cyc:cy_close at (%d): decrementing count to %d\n",
  2177. current->pid, info->port.count - 1);
  2178. #endif
  2179. if (--info->port.count < 0) {
  2180. #ifdef CY_DEBUG_COUNT
  2181. printk(KERN_DEBUG "cyc:cyc_close setting count to 0\n");
  2182. #endif
  2183. info->port.count = 0;
  2184. }
  2185. if (info->port.count) {
  2186. spin_unlock_irqrestore(&card->card_lock, flags);
  2187. return;
  2188. }
  2189. info->port.flags |= ASYNC_CLOSING;
  2190. /*
  2191. * Now we wait for the transmit buffer to clear; and we notify
  2192. * the line discipline to only process XON/XOFF characters.
  2193. */
  2194. tty->closing = 1;
  2195. spin_unlock_irqrestore(&card->card_lock, flags);
  2196. if (info->port.closing_wait != CY_CLOSING_WAIT_NONE)
  2197. tty_wait_until_sent(tty, info->port.closing_wait);
  2198. spin_lock_irqsave(&card->card_lock, flags);
  2199. if (!cy_is_Z(card)) {
  2200. int channel = info->line - card->first_line;
  2201. int index = card->bus_index;
  2202. void __iomem *base_addr = card->base_addr +
  2203. (cy_chip_offset[channel >> 2] << index);
  2204. /* Stop accepting input */
  2205. channel &= 0x03;
  2206. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  2207. cy_writeb(base_addr + (CySRER << index),
  2208. readb(base_addr + (CySRER << index)) & ~CyRxData);
  2209. if (info->port.flags & ASYNC_INITIALIZED) {
  2210. /* Waiting for on-board buffers to be empty before
  2211. closing the port */
  2212. spin_unlock_irqrestore(&card->card_lock, flags);
  2213. cy_wait_until_sent(tty, info->timeout);
  2214. spin_lock_irqsave(&card->card_lock, flags);
  2215. }
  2216. } else {
  2217. #ifdef Z_WAKE
  2218. /* Waiting for on-board buffers to be empty before closing
  2219. the port */
  2220. struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
  2221. int channel = info->line - card->first_line;
  2222. int retval;
  2223. if (readl(&ch_ctrl->flow_status) != C_FS_TXIDLE) {
  2224. retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
  2225. if (retval != 0) {
  2226. printk(KERN_DEBUG "cyc:cy_close retval on "
  2227. "ttyC%d was %x\n", info->line, retval);
  2228. }
  2229. spin_unlock_irqrestore(&card->card_lock, flags);
  2230. wait_for_completion_interruptible(&info->shutdown_wait);
  2231. spin_lock_irqsave(&card->card_lock, flags);
  2232. }
  2233. #endif
  2234. }
  2235. spin_unlock_irqrestore(&card->card_lock, flags);
  2236. cy_shutdown(info, tty);
  2237. cy_flush_buffer(tty);
  2238. tty_ldisc_flush(tty);
  2239. spin_lock_irqsave(&card->card_lock, flags);
  2240. tty->closing = 0;
  2241. tty_port_tty_set(&info->port, NULL);
  2242. if (info->port.blocked_open) {
  2243. spin_unlock_irqrestore(&card->card_lock, flags);
  2244. if (info->port.close_delay) {
  2245. msleep_interruptible(jiffies_to_msecs
  2246. (info->port.close_delay));
  2247. }
  2248. wake_up_interruptible(&info->port.open_wait);
  2249. spin_lock_irqsave(&card->card_lock, flags);
  2250. }
  2251. info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  2252. wake_up_interruptible(&info->port.close_wait);
  2253. #ifdef CY_DEBUG_OTHER
  2254. printk(KERN_DEBUG "cyc:cy_close done\n");
  2255. #endif
  2256. spin_unlock_irqrestore(&card->card_lock, flags);
  2257. } /* cy_close */
  2258. /* This routine gets called when tty_write has put something into
  2259. * the write_queue. The characters may come from user space or
  2260. * kernel space.
  2261. *
  2262. * This routine will return the number of characters actually
  2263. * accepted for writing.
  2264. *
  2265. * If the port is not already transmitting stuff, start it off by
  2266. * enabling interrupts. The interrupt service routine will then
  2267. * ensure that the characters are sent.
  2268. * If the port is already active, there is no need to kick it.
  2269. *
  2270. */
  2271. static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2272. {
  2273. struct cyclades_port *info = tty->driver_data;
  2274. unsigned long flags;
  2275. int c, ret = 0;
  2276. #ifdef CY_DEBUG_IO
  2277. printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
  2278. #endif
  2279. if (serial_paranoia_check(info, tty->name, "cy_write"))
  2280. return 0;
  2281. if (!info->port.xmit_buf)
  2282. return 0;
  2283. spin_lock_irqsave(&info->card->card_lock, flags);
  2284. while (1) {
  2285. c = min(count, (int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1));
  2286. c = min(c, (int)(SERIAL_XMIT_SIZE - info->xmit_head));
  2287. if (c <= 0)
  2288. break;
  2289. memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
  2290. info->xmit_head = (info->xmit_head + c) &
  2291. (SERIAL_XMIT_SIZE - 1);
  2292. info->xmit_cnt += c;
  2293. buf += c;
  2294. count -= c;
  2295. ret += c;
  2296. }
  2297. spin_unlock_irqrestore(&info->card->card_lock, flags);
  2298. info->idle_stats.xmit_bytes += ret;
  2299. info->idle_stats.xmit_idle = jiffies;
  2300. if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped)
  2301. start_xmit(info);
  2302. return ret;
  2303. } /* cy_write */
  2304. /*
  2305. * This routine is called by the kernel to write a single
  2306. * character to the tty device. If the kernel uses this routine,
  2307. * it must call the flush_chars() routine (if defined) when it is
  2308. * done stuffing characters into the driver. If there is no room
  2309. * in the queue, the character is ignored.
  2310. */
  2311. static int cy_put_char(struct tty_struct *tty, unsigned char ch)
  2312. {
  2313. struct cyclades_port *info = tty->driver_data;
  2314. unsigned long flags;
  2315. #ifdef CY_DEBUG_IO
  2316. printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
  2317. #endif
  2318. if (serial_paranoia_check(info, tty->name, "cy_put_char"))
  2319. return 0;
  2320. if (!info->port.xmit_buf)
  2321. return 0;
  2322. spin_lock_irqsave(&info->card->card_lock, flags);
  2323. if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
  2324. spin_unlock_irqrestore(&info->card->card_lock, flags);
  2325. return 0;
  2326. }
  2327. info->port.xmit_buf[info->xmit_head++] = ch;
  2328. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  2329. info->xmit_cnt++;
  2330. info->idle_stats.xmit_bytes++;
  2331. info->idle_stats.xmit_idle = jiffies;
  2332. spin_unlock_irqrestore(&info->card->card_lock, flags);
  2333. return 1;
  2334. } /* cy_put_char */
  2335. /*
  2336. * This routine is called by the kernel after it has written a
  2337. * series of characters to the tty device using put_char().
  2338. */
  2339. static void cy_flush_chars(struct tty_struct *tty)
  2340. {
  2341. struct cyclades_port *info = tty->driver_data;
  2342. #ifdef CY_DEBUG_IO
  2343. printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
  2344. #endif
  2345. if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
  2346. return;
  2347. if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  2348. !info->port.xmit_buf)
  2349. return;
  2350. start_xmit(info);
  2351. } /* cy_flush_chars */
  2352. /*
  2353. * This routine returns the numbers of characters the tty driver
  2354. * will accept for queuing to be written. This number is subject
  2355. * to change as output buffers get emptied, or if the output flow
  2356. * control is activated.
  2357. */
  2358. static int cy_write_room(struct tty_struct *tty)
  2359. {
  2360. struct cyclades_port *info = tty->driver_data;
  2361. int ret;
  2362. #ifdef CY_DEBUG_IO
  2363. printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
  2364. #endif
  2365. if (serial_paranoia_check(info, tty->name, "cy_write_room"))
  2366. return 0;
  2367. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  2368. if (ret < 0)
  2369. ret = 0;
  2370. return ret;
  2371. } /* cy_write_room */
  2372. static int cy_chars_in_buffer(struct tty_struct *tty)
  2373. {
  2374. struct cyclades_port *info = tty->driver_data;
  2375. if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
  2376. return 0;
  2377. #ifdef Z_EXT_CHARS_IN_BUFFER
  2378. if (!cy_is_Z(info->card)) {
  2379. #endif /* Z_EXT_CHARS_IN_BUFFER */
  2380. #ifdef CY_DEBUG_IO
  2381. printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
  2382. info->line, info->xmit_cnt);
  2383. #endif
  2384. return info->xmit_cnt;
  2385. #ifdef Z_EXT_CHARS_IN_BUFFER
  2386. } else {
  2387. struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
  2388. int char_count;
  2389. __u32 tx_put, tx_get, tx_bufsize;
  2390. lock_kernel();
  2391. tx_get = readl(&buf_ctrl->tx_get);
  2392. tx_put = readl(&buf_ctrl->tx_put);
  2393. tx_bufsize = readl(&buf_ctrl->tx_bufsize);
  2394. if (tx_put >= tx_get)
  2395. char_count = tx_put - tx_get;
  2396. else
  2397. char_count = tx_put - tx_get + tx_bufsize;
  2398. #ifdef CY_DEBUG_IO
  2399. printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
  2400. info->line, info->xmit_cnt + char_count);
  2401. #endif
  2402. unlock_kernel();
  2403. return info->xmit_cnt + char_count;
  2404. }
  2405. #endif /* Z_EXT_CHARS_IN_BUFFER */
  2406. } /* cy_chars_in_buffer */
  2407. /*
  2408. * ------------------------------------------------------------
  2409. * cy_ioctl() and friends
  2410. * ------------------------------------------------------------
  2411. */
  2412. static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
  2413. {
  2414. int co, co_val, bpr;
  2415. __u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
  2416. 25000000);
  2417. if (baud == 0) {
  2418. info->tbpr = info->tco = info->rbpr = info->rco = 0;
  2419. return;
  2420. }
  2421. /* determine which prescaler to use */
  2422. for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
  2423. if (cy_clock / co_val / baud > 63)
  2424. break;
  2425. }
  2426. bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
  2427. if (bpr > 255)
  2428. bpr = 255;
  2429. info->tbpr = info->rbpr = bpr;
  2430. info->tco = info->rco = co;
  2431. }
  2432. /*
  2433. * This routine finds or computes the various line characteristics.
  2434. * It used to be called config_setup
  2435. */
  2436. static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
  2437. {
  2438. struct cyclades_card *card;
  2439. unsigned long flags;
  2440. void __iomem *base_addr;
  2441. int chip, channel, index;
  2442. unsigned cflag, iflag;
  2443. int baud, baud_rate = 0;
  2444. int i;
  2445. if (!tty->termios) /* XXX can this happen at all? */
  2446. return;
  2447. if (info->line == -1)
  2448. return;
  2449. cflag = tty->termios->c_cflag;
  2450. iflag = tty->termios->c_iflag;
  2451. /*
  2452. * Set up the tty->alt_speed kludge
  2453. */
  2454. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  2455. tty->alt_speed = 57600;
  2456. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  2457. tty->alt_speed = 115200;
  2458. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  2459. tty->alt_speed = 230400;
  2460. if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  2461. tty->alt_speed = 460800;
  2462. card = info->card;
  2463. channel = info->line - card->first_line;
  2464. if (!cy_is_Z(card)) {
  2465. index = card->bus_index;
  2466. /* baud rate */
  2467. baud = tty_get_baud_rate(tty);
  2468. if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
  2469. ASYNC_SPD_CUST) {
  2470. if (info->custom_divisor)
  2471. baud_rate = info->baud / info->custom_divisor;
  2472. else
  2473. baud_rate = info->baud;
  2474. } else if (baud > CD1400_MAX_SPEED) {
  2475. baud = CD1400_MAX_SPEED;
  2476. }
  2477. /* find the baud index */
  2478. for (i = 0; i < 20; i++) {
  2479. if (baud == baud_table[i])
  2480. break;
  2481. }
  2482. if (i == 20)
  2483. i = 19; /* CD1400_MAX_SPEED */
  2484. if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
  2485. ASYNC_SPD_CUST) {
  2486. cyy_baud_calc(info, baud_rate);
  2487. } else {
  2488. if (info->chip_rev >= CD1400_REV_J) {
  2489. /* It is a CD1400 rev. J or later */
  2490. info->tbpr = baud_bpr_60[i]; /* Tx BPR */
  2491. info->tco = baud_co_60[i]; /* Tx CO */
  2492. info->rbpr = baud_bpr_60[i]; /* Rx BPR */
  2493. info->rco = baud_co_60[i]; /* Rx CO */
  2494. } else {
  2495. info->tbpr = baud_bpr_25[i]; /* Tx BPR */
  2496. info->tco = baud_co_25[i]; /* Tx CO */
  2497. info->rbpr = baud_bpr_25[i]; /* Rx BPR */
  2498. info->rco = baud_co_25[i]; /* Rx CO */
  2499. }
  2500. }
  2501. if (baud_table[i] == 134) {
  2502. /* get it right for 134.5 baud */
  2503. info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
  2504. 2;
  2505. } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
  2506. ASYNC_SPD_CUST) {
  2507. info->timeout = (info->xmit_fifo_size * HZ * 15 /
  2508. baud_rate) + 2;
  2509. } else if (baud_table[i]) {
  2510. info->timeout = (info->xmit_fifo_size * HZ * 15 /
  2511. baud_table[i]) + 2;
  2512. /* this needs to be propagated into the card info */
  2513. } else {
  2514. info->timeout = 0;
  2515. }
  2516. /* By tradition (is it a standard?) a baud rate of zero
  2517. implies the line should be/has been closed. A bit
  2518. later in this routine such a test is performed. */
  2519. /* byte size and parity */
  2520. info->cor5 = 0;
  2521. info->cor4 = 0;
  2522. /* receive threshold */
  2523. info->cor3 = (info->default_threshold ?
  2524. info->default_threshold : baud_cor3[i]);
  2525. info->cor2 = CyETC;
  2526. switch (cflag & CSIZE) {
  2527. case CS5:
  2528. info->cor1 = Cy_5_BITS;
  2529. break;
  2530. case CS6:
  2531. info->cor1 = Cy_6_BITS;
  2532. break;
  2533. case CS7:
  2534. info->cor1 = Cy_7_BITS;
  2535. break;
  2536. case CS8:
  2537. info->cor1 = Cy_8_BITS;
  2538. break;
  2539. }
  2540. if (cflag & CSTOPB)
  2541. info->cor1 |= Cy_2_STOP;
  2542. if (cflag & PARENB) {
  2543. if (cflag & PARODD)
  2544. info->cor1 |= CyPARITY_O;
  2545. else
  2546. info->cor1 |= CyPARITY_E;
  2547. } else
  2548. info->cor1 |= CyPARITY_NONE;
  2549. /* CTS flow control flag */
  2550. if (cflag & CRTSCTS) {
  2551. info->port.flags |= ASYNC_CTS_FLOW;
  2552. info->cor2 |= CyCtsAE;
  2553. } else {
  2554. info->port.flags &= ~ASYNC_CTS_FLOW;
  2555. info->cor2 &= ~CyCtsAE;
  2556. }
  2557. if (cflag & CLOCAL)
  2558. info->port.flags &= ~ASYNC_CHECK_CD;
  2559. else
  2560. info->port.flags |= ASYNC_CHECK_CD;
  2561. /***********************************************
  2562. The hardware option, CyRtsAO, presents RTS when
  2563. the chip has characters to send. Since most modems
  2564. use RTS as reverse (inbound) flow control, this
  2565. option is not used. If inbound flow control is
  2566. necessary, DTR can be programmed to provide the
  2567. appropriate signals for use with a non-standard
  2568. cable. Contact Marcio Saito for details.
  2569. ***********************************************/
  2570. chip = channel >> 2;
  2571. channel &= 0x03;
  2572. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  2573. spin_lock_irqsave(&card->card_lock, flags);
  2574. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  2575. /* tx and rx baud rate */
  2576. cy_writeb(base_addr + (CyTCOR << index), info->tco);
  2577. cy_writeb(base_addr + (CyTBPR << index), info->tbpr);
  2578. cy_writeb(base_addr + (CyRCOR << index), info->rco);
  2579. cy_writeb(base_addr + (CyRBPR << index), info->rbpr);
  2580. /* set line characteristics according configuration */
  2581. cy_writeb(base_addr + (CySCHR1 << index), START_CHAR(tty));
  2582. cy_writeb(base_addr + (CySCHR2 << index), STOP_CHAR(tty));
  2583. cy_writeb(base_addr + (CyCOR1 << index), info->cor1);
  2584. cy_writeb(base_addr + (CyCOR2 << index), info->cor2);
  2585. cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
  2586. cy_writeb(base_addr + (CyCOR4 << index), info->cor4);
  2587. cy_writeb(base_addr + (CyCOR5 << index), info->cor5);
  2588. cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
  2589. CyCOR3ch, index);
  2590. /* !!! Is this needed? */
  2591. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  2592. cy_writeb(base_addr + (CyRTPR << index),
  2593. (info->default_timeout ? info->default_timeout : 0x02));
  2594. /* 10ms rx timeout */
  2595. if (C_CLOCAL(tty)) {
  2596. /* without modem intr */
  2597. cy_writeb(base_addr + (CySRER << index),
  2598. readb(base_addr + (CySRER << index)) | CyMdmCh);
  2599. /* act on 1->0 modem transitions */
  2600. if ((cflag & CRTSCTS) && info->rflow) {
  2601. cy_writeb(base_addr + (CyMCOR1 << index),
  2602. (CyCTS | rflow_thr[i]));
  2603. } else {
  2604. cy_writeb(base_addr + (CyMCOR1 << index),
  2605. CyCTS);
  2606. }
  2607. /* act on 0->1 modem transitions */
  2608. cy_writeb(base_addr + (CyMCOR2 << index), CyCTS);
  2609. } else {
  2610. /* without modem intr */
  2611. cy_writeb(base_addr + (CySRER << index),
  2612. readb(base_addr +
  2613. (CySRER << index)) | CyMdmCh);
  2614. /* act on 1->0 modem transitions */
  2615. if ((cflag & CRTSCTS) && info->rflow) {
  2616. cy_writeb(base_addr + (CyMCOR1 << index),
  2617. (CyDSR | CyCTS | CyRI | CyDCD |
  2618. rflow_thr[i]));
  2619. } else {
  2620. cy_writeb(base_addr + (CyMCOR1 << index),
  2621. CyDSR | CyCTS | CyRI | CyDCD);
  2622. }
  2623. /* act on 0->1 modem transitions */
  2624. cy_writeb(base_addr + (CyMCOR2 << index),
  2625. CyDSR | CyCTS | CyRI | CyDCD);
  2626. }
  2627. if (i == 0) { /* baud rate is zero, turn off line */
  2628. if (info->rtsdtr_inv) {
  2629. cy_writeb(base_addr + (CyMSVR1 << index),
  2630. ~CyRTS);
  2631. } else {
  2632. cy_writeb(base_addr + (CyMSVR2 << index),
  2633. ~CyDTR);
  2634. }
  2635. #ifdef CY_DEBUG_DTR
  2636. printk(KERN_DEBUG "cyc:set_line_char dropping DTR\n");
  2637. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  2638. readb(base_addr + (CyMSVR1 << index)),
  2639. readb(base_addr + (CyMSVR2 << index)));
  2640. #endif
  2641. } else {
  2642. if (info->rtsdtr_inv) {
  2643. cy_writeb(base_addr + (CyMSVR1 << index),
  2644. CyRTS);
  2645. } else {
  2646. cy_writeb(base_addr + (CyMSVR2 << index),
  2647. CyDTR);
  2648. }
  2649. #ifdef CY_DEBUG_DTR
  2650. printk(KERN_DEBUG "cyc:set_line_char raising DTR\n");
  2651. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  2652. readb(base_addr + (CyMSVR1 << index)),
  2653. readb(base_addr + (CyMSVR2 << index)));
  2654. #endif
  2655. }
  2656. clear_bit(TTY_IO_ERROR, &tty->flags);
  2657. spin_unlock_irqrestore(&card->card_lock, flags);
  2658. } else {
  2659. struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
  2660. __u32 sw_flow;
  2661. int retval;
  2662. if (!cyz_is_loaded(card))
  2663. return;
  2664. /* baud rate */
  2665. baud = tty_get_baud_rate(tty);
  2666. if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
  2667. ASYNC_SPD_CUST) {
  2668. if (info->custom_divisor)
  2669. baud_rate = info->baud / info->custom_divisor;
  2670. else
  2671. baud_rate = info->baud;
  2672. } else if (baud > CYZ_MAX_SPEED) {
  2673. baud = CYZ_MAX_SPEED;
  2674. }
  2675. cy_writel(&ch_ctrl->comm_baud, baud);
  2676. if (baud == 134) {
  2677. /* get it right for 134.5 baud */
  2678. info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
  2679. 2;
  2680. } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
  2681. ASYNC_SPD_CUST) {
  2682. info->timeout = (info->xmit_fifo_size * HZ * 15 /
  2683. baud_rate) + 2;
  2684. } else if (baud) {
  2685. info->timeout = (info->xmit_fifo_size * HZ * 15 /
  2686. baud) + 2;
  2687. /* this needs to be propagated into the card info */
  2688. } else {
  2689. info->timeout = 0;
  2690. }
  2691. /* byte size and parity */
  2692. switch (cflag & CSIZE) {
  2693. case CS5:
  2694. cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
  2695. break;
  2696. case CS6:
  2697. cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
  2698. break;
  2699. case CS7:
  2700. cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
  2701. break;
  2702. case CS8:
  2703. cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
  2704. break;
  2705. }
  2706. if (cflag & CSTOPB) {
  2707. cy_writel(&ch_ctrl->comm_data_l,
  2708. readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
  2709. } else {
  2710. cy_writel(&ch_ctrl->comm_data_l,
  2711. readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
  2712. }
  2713. if (cflag & PARENB) {
  2714. if (cflag & PARODD)
  2715. cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
  2716. else
  2717. cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
  2718. } else
  2719. cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
  2720. /* CTS flow control flag */
  2721. if (cflag & CRTSCTS) {
  2722. cy_writel(&ch_ctrl->hw_flow,
  2723. readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
  2724. } else {
  2725. cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
  2726. ~(C_RS_CTS | C_RS_RTS));
  2727. }
  2728. /* As the HW flow control is done in firmware, the driver
  2729. doesn't need to care about it */
  2730. info->port.flags &= ~ASYNC_CTS_FLOW;
  2731. /* XON/XOFF/XANY flow control flags */
  2732. sw_flow = 0;
  2733. if (iflag & IXON) {
  2734. sw_flow |= C_FL_OXX;
  2735. if (iflag & IXANY)
  2736. sw_flow |= C_FL_OIXANY;
  2737. }
  2738. cy_writel(&ch_ctrl->sw_flow, sw_flow);
  2739. retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
  2740. if (retval != 0) {
  2741. printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
  2742. "was %x\n", info->line, retval);
  2743. }
  2744. /* CD sensitivity */
  2745. if (cflag & CLOCAL)
  2746. info->port.flags &= ~ASYNC_CHECK_CD;
  2747. else
  2748. info->port.flags |= ASYNC_CHECK_CD;
  2749. if (baud == 0) { /* baud rate is zero, turn off line */
  2750. cy_writel(&ch_ctrl->rs_control,
  2751. readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
  2752. #ifdef CY_DEBUG_DTR
  2753. printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
  2754. #endif
  2755. } else {
  2756. cy_writel(&ch_ctrl->rs_control,
  2757. readl(&ch_ctrl->rs_control) | C_RS_DTR);
  2758. #ifdef CY_DEBUG_DTR
  2759. printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
  2760. #endif
  2761. }
  2762. retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
  2763. if (retval != 0) {
  2764. printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
  2765. "was %x\n", info->line, retval);
  2766. }
  2767. clear_bit(TTY_IO_ERROR, &tty->flags);
  2768. }
  2769. } /* set_line_char */
  2770. static int
  2771. get_serial_info(struct cyclades_port *info,
  2772. struct serial_struct __user *retinfo)
  2773. {
  2774. struct serial_struct tmp;
  2775. struct cyclades_card *cinfo = info->card;
  2776. if (!retinfo)
  2777. return -EFAULT;
  2778. memset(&tmp, 0, sizeof(tmp));
  2779. tmp.type = info->type;
  2780. tmp.line = info->line;
  2781. tmp.port = (info->card - cy_card) * 0x100 + info->line -
  2782. cinfo->first_line;
  2783. tmp.irq = cinfo->irq;
  2784. tmp.flags = info->port.flags;
  2785. tmp.close_delay = info->port.close_delay;
  2786. tmp.closing_wait = info->port.closing_wait;
  2787. tmp.baud_base = info->baud;
  2788. tmp.custom_divisor = info->custom_divisor;
  2789. tmp.hub6 = 0; /*!!! */
  2790. return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
  2791. } /* get_serial_info */
  2792. static int
  2793. cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
  2794. struct serial_struct __user *new_info)
  2795. {
  2796. struct serial_struct new_serial;
  2797. struct cyclades_port old_info;
  2798. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2799. return -EFAULT;
  2800. old_info = *info;
  2801. if (!capable(CAP_SYS_ADMIN)) {
  2802. if (new_serial.close_delay != info->port.close_delay ||
  2803. new_serial.baud_base != info->baud ||
  2804. (new_serial.flags & ASYNC_FLAGS &
  2805. ~ASYNC_USR_MASK) !=
  2806. (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
  2807. return -EPERM;
  2808. info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) |
  2809. (new_serial.flags & ASYNC_USR_MASK);
  2810. info->baud = new_serial.baud_base;
  2811. info->custom_divisor = new_serial.custom_divisor;
  2812. goto check_and_exit;
  2813. }
  2814. /*
  2815. * OK, past this point, all the error checking has been done.
  2816. * At this point, we start making changes.....
  2817. */
  2818. info->baud = new_serial.baud_base;
  2819. info->custom_divisor = new_serial.custom_divisor;
  2820. info->port.flags = (info->port.flags & ~ASYNC_FLAGS) |
  2821. (new_serial.flags & ASYNC_FLAGS);
  2822. info->port.close_delay = new_serial.close_delay * HZ / 100;
  2823. info->port.closing_wait = new_serial.closing_wait * HZ / 100;
  2824. check_and_exit:
  2825. if (info->port.flags & ASYNC_INITIALIZED) {
  2826. cy_set_line_char(info, tty);
  2827. return 0;
  2828. } else {
  2829. return cy_startup(info, tty);
  2830. }
  2831. } /* set_serial_info */
  2832. /*
  2833. * get_lsr_info - get line status register info
  2834. *
  2835. * Purpose: Let user call ioctl() to get info when the UART physically
  2836. * is emptied. On bus types like RS485, the transmitter must
  2837. * release the bus after transmitting. This must be done when
  2838. * the transmit shift register is empty, not be done when the
  2839. * transmit holding register is empty. This functionality
  2840. * allows an RS485 driver to be written in user space.
  2841. */
  2842. static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
  2843. {
  2844. struct cyclades_card *card;
  2845. int chip, channel, index;
  2846. unsigned char status;
  2847. unsigned int result;
  2848. unsigned long flags;
  2849. void __iomem *base_addr;
  2850. card = info->card;
  2851. channel = (info->line) - (card->first_line);
  2852. if (!cy_is_Z(card)) {
  2853. chip = channel >> 2;
  2854. channel &= 0x03;
  2855. index = card->bus_index;
  2856. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  2857. spin_lock_irqsave(&card->card_lock, flags);
  2858. status = readb(base_addr + (CySRER << index)) &
  2859. (CyTxRdy | CyTxMpty);
  2860. spin_unlock_irqrestore(&card->card_lock, flags);
  2861. result = (status ? 0 : TIOCSER_TEMT);
  2862. } else {
  2863. /* Not supported yet */
  2864. return -EINVAL;
  2865. }
  2866. return put_user(result, (unsigned long __user *)value);
  2867. }
  2868. static int cy_tiocmget(struct tty_struct *tty, struct file *file)
  2869. {
  2870. struct cyclades_port *info = tty->driver_data;
  2871. struct cyclades_card *card;
  2872. int chip, channel, index;
  2873. void __iomem *base_addr;
  2874. unsigned long flags;
  2875. unsigned char status;
  2876. unsigned long lstatus;
  2877. unsigned int result;
  2878. if (serial_paranoia_check(info, tty->name, __func__))
  2879. return -ENODEV;
  2880. lock_kernel();
  2881. card = info->card;
  2882. channel = info->line - card->first_line;
  2883. if (!cy_is_Z(card)) {
  2884. chip = channel >> 2;
  2885. channel &= 0x03;
  2886. index = card->bus_index;
  2887. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  2888. spin_lock_irqsave(&card->card_lock, flags);
  2889. cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
  2890. status = readb(base_addr + (CyMSVR1 << index));
  2891. status |= readb(base_addr + (CyMSVR2 << index));
  2892. spin_unlock_irqrestore(&card->card_lock, flags);
  2893. if (info->rtsdtr_inv) {
  2894. result = ((status & CyRTS) ? TIOCM_DTR : 0) |
  2895. ((status & CyDTR) ? TIOCM_RTS : 0);
  2896. } else {
  2897. result = ((status & CyRTS) ? TIOCM_RTS : 0) |
  2898. ((status & CyDTR) ? TIOCM_DTR : 0);
  2899. }
  2900. result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
  2901. ((status & CyRI) ? TIOCM_RNG : 0) |
  2902. ((status & CyDSR) ? TIOCM_DSR : 0) |
  2903. ((status & CyCTS) ? TIOCM_CTS : 0);
  2904. } else {
  2905. if (cyz_is_loaded(card)) {
  2906. lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
  2907. result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
  2908. ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
  2909. ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
  2910. ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
  2911. ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
  2912. ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
  2913. } else {
  2914. result = 0;
  2915. unlock_kernel();
  2916. return -ENODEV;
  2917. }
  2918. }
  2919. unlock_kernel();
  2920. return result;
  2921. } /* cy_tiomget */
  2922. static int
  2923. cy_tiocmset(struct tty_struct *tty, struct file *file,
  2924. unsigned int set, unsigned int clear)
  2925. {
  2926. struct cyclades_port *info = tty->driver_data;
  2927. struct cyclades_card *card;
  2928. int chip, channel, index;
  2929. unsigned long flags;
  2930. int retval;
  2931. if (serial_paranoia_check(info, tty->name, __func__))
  2932. return -ENODEV;
  2933. card = info->card;
  2934. channel = (info->line) - (card->first_line);
  2935. if (!cy_is_Z(card)) {
  2936. void __iomem *base_addr;
  2937. chip = channel >> 2;
  2938. channel &= 0x03;
  2939. index = card->bus_index;
  2940. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  2941. if (set & TIOCM_RTS) {
  2942. spin_lock_irqsave(&card->card_lock, flags);
  2943. cy_writeb(base_addr + (CyCAR << index),
  2944. (u_char) channel);
  2945. if (info->rtsdtr_inv) {
  2946. cy_writeb(base_addr + (CyMSVR2 << index),
  2947. CyDTR);
  2948. } else {
  2949. cy_writeb(base_addr + (CyMSVR1 << index),
  2950. CyRTS);
  2951. }
  2952. spin_unlock_irqrestore(&card->card_lock, flags);
  2953. }
  2954. if (clear & TIOCM_RTS) {
  2955. spin_lock_irqsave(&card->card_lock, flags);
  2956. cy_writeb(base_addr + (CyCAR << index),
  2957. (u_char) channel);
  2958. if (info->rtsdtr_inv) {
  2959. cy_writeb(base_addr + (CyMSVR2 << index),
  2960. ~CyDTR);
  2961. } else {
  2962. cy_writeb(base_addr + (CyMSVR1 << index),
  2963. ~CyRTS);
  2964. }
  2965. spin_unlock_irqrestore(&card->card_lock, flags);
  2966. }
  2967. if (set & TIOCM_DTR) {
  2968. spin_lock_irqsave(&card->card_lock, flags);
  2969. cy_writeb(base_addr + (CyCAR << index),
  2970. (u_char) channel);
  2971. if (info->rtsdtr_inv) {
  2972. cy_writeb(base_addr + (CyMSVR1 << index),
  2973. CyRTS);
  2974. } else {
  2975. cy_writeb(base_addr + (CyMSVR2 << index),
  2976. CyDTR);
  2977. }
  2978. #ifdef CY_DEBUG_DTR
  2979. printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
  2980. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  2981. readb(base_addr + (CyMSVR1 << index)),
  2982. readb(base_addr + (CyMSVR2 << index)));
  2983. #endif
  2984. spin_unlock_irqrestore(&card->card_lock, flags);
  2985. }
  2986. if (clear & TIOCM_DTR) {
  2987. spin_lock_irqsave(&card->card_lock, flags);
  2988. cy_writeb(base_addr + (CyCAR << index),
  2989. (u_char) channel);
  2990. if (info->rtsdtr_inv) {
  2991. cy_writeb(base_addr + (CyMSVR1 << index),
  2992. ~CyRTS);
  2993. } else {
  2994. cy_writeb(base_addr + (CyMSVR2 << index),
  2995. ~CyDTR);
  2996. }
  2997. #ifdef CY_DEBUG_DTR
  2998. printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
  2999. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  3000. readb(base_addr + (CyMSVR1 << index)),
  3001. readb(base_addr + (CyMSVR2 << index)));
  3002. #endif
  3003. spin_unlock_irqrestore(&card->card_lock, flags);
  3004. }
  3005. } else {
  3006. if (cyz_is_loaded(card)) {
  3007. struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
  3008. if (set & TIOCM_RTS) {
  3009. spin_lock_irqsave(&card->card_lock, flags);
  3010. cy_writel(&ch_ctrl->rs_control,
  3011. readl(&ch_ctrl->rs_control) | C_RS_RTS);
  3012. spin_unlock_irqrestore(&card->card_lock, flags);
  3013. }
  3014. if (clear & TIOCM_RTS) {
  3015. spin_lock_irqsave(&card->card_lock, flags);
  3016. cy_writel(&ch_ctrl->rs_control,
  3017. readl(&ch_ctrl->rs_control) &
  3018. ~C_RS_RTS);
  3019. spin_unlock_irqrestore(&card->card_lock, flags);
  3020. }
  3021. if (set & TIOCM_DTR) {
  3022. spin_lock_irqsave(&card->card_lock, flags);
  3023. cy_writel(&ch_ctrl->rs_control,
  3024. readl(&ch_ctrl->rs_control) | C_RS_DTR);
  3025. #ifdef CY_DEBUG_DTR
  3026. printk(KERN_DEBUG "cyc:set_modem_info raising "
  3027. "Z DTR\n");
  3028. #endif
  3029. spin_unlock_irqrestore(&card->card_lock, flags);
  3030. }
  3031. if (clear & TIOCM_DTR) {
  3032. spin_lock_irqsave(&card->card_lock, flags);
  3033. cy_writel(&ch_ctrl->rs_control,
  3034. readl(&ch_ctrl->rs_control) &
  3035. ~C_RS_DTR);
  3036. #ifdef CY_DEBUG_DTR
  3037. printk(KERN_DEBUG "cyc:set_modem_info clearing "
  3038. "Z DTR\n");
  3039. #endif
  3040. spin_unlock_irqrestore(&card->card_lock, flags);
  3041. }
  3042. } else {
  3043. return -ENODEV;
  3044. }
  3045. spin_lock_irqsave(&card->card_lock, flags);
  3046. retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
  3047. if (retval != 0) {
  3048. printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
  3049. "was %x\n", info->line, retval);
  3050. }
  3051. spin_unlock_irqrestore(&card->card_lock, flags);
  3052. }
  3053. return 0;
  3054. } /* cy_tiocmset */
  3055. /*
  3056. * cy_break() --- routine which turns the break handling on or off
  3057. */
  3058. static int cy_break(struct tty_struct *tty, int break_state)
  3059. {
  3060. struct cyclades_port *info = tty->driver_data;
  3061. struct cyclades_card *card;
  3062. unsigned long flags;
  3063. int retval = 0;
  3064. if (serial_paranoia_check(info, tty->name, "cy_break"))
  3065. return -EINVAL;
  3066. card = info->card;
  3067. spin_lock_irqsave(&card->card_lock, flags);
  3068. if (!cy_is_Z(card)) {
  3069. /* Let the transmit ISR take care of this (since it
  3070. requires stuffing characters into the output stream).
  3071. */
  3072. if (break_state == -1) {
  3073. if (!info->breakon) {
  3074. info->breakon = 1;
  3075. if (!info->xmit_cnt) {
  3076. spin_unlock_irqrestore(&card->card_lock, flags);
  3077. start_xmit(info);
  3078. spin_lock_irqsave(&card->card_lock, flags);
  3079. }
  3080. }
  3081. } else {
  3082. if (!info->breakoff) {
  3083. info->breakoff = 1;
  3084. if (!info->xmit_cnt) {
  3085. spin_unlock_irqrestore(&card->card_lock, flags);
  3086. start_xmit(info);
  3087. spin_lock_irqsave(&card->card_lock, flags);
  3088. }
  3089. }
  3090. }
  3091. } else {
  3092. if (break_state == -1) {
  3093. retval = cyz_issue_cmd(card,
  3094. info->line - card->first_line,
  3095. C_CM_SET_BREAK, 0L);
  3096. if (retval != 0) {
  3097. printk(KERN_ERR "cyc:cy_break (set) retval on "
  3098. "ttyC%d was %x\n", info->line, retval);
  3099. }
  3100. } else {
  3101. retval = cyz_issue_cmd(card,
  3102. info->line - card->first_line,
  3103. C_CM_CLR_BREAK, 0L);
  3104. if (retval != 0) {
  3105. printk(KERN_DEBUG "cyc:cy_break (clr) retval "
  3106. "on ttyC%d was %x\n", info->line,
  3107. retval);
  3108. }
  3109. }
  3110. }
  3111. spin_unlock_irqrestore(&card->card_lock, flags);
  3112. return retval;
  3113. } /* cy_break */
  3114. static int get_mon_info(struct cyclades_port *info,
  3115. struct cyclades_monitor __user *mon)
  3116. {
  3117. if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
  3118. return -EFAULT;
  3119. info->mon.int_count = 0;
  3120. info->mon.char_count = 0;
  3121. info->mon.char_max = 0;
  3122. info->mon.char_last = 0;
  3123. return 0;
  3124. } /* get_mon_info */
  3125. static int set_threshold(struct cyclades_port *info, unsigned long value)
  3126. {
  3127. struct cyclades_card *card;
  3128. void __iomem *base_addr;
  3129. int channel, chip, index;
  3130. unsigned long flags;
  3131. card = info->card;
  3132. channel = info->line - card->first_line;
  3133. if (!cy_is_Z(card)) {
  3134. chip = channel >> 2;
  3135. channel &= 0x03;
  3136. index = card->bus_index;
  3137. base_addr =
  3138. card->base_addr + (cy_chip_offset[chip] << index);
  3139. info->cor3 &= ~CyREC_FIFO;
  3140. info->cor3 |= value & CyREC_FIFO;
  3141. spin_lock_irqsave(&card->card_lock, flags);
  3142. cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
  3143. cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR3ch, index);
  3144. spin_unlock_irqrestore(&card->card_lock, flags);
  3145. }
  3146. return 0;
  3147. } /* set_threshold */
  3148. static int get_threshold(struct cyclades_port *info,
  3149. unsigned long __user *value)
  3150. {
  3151. struct cyclades_card *card;
  3152. void __iomem *base_addr;
  3153. int channel, chip, index;
  3154. unsigned long tmp;
  3155. card = info->card;
  3156. channel = info->line - card->first_line;
  3157. if (!cy_is_Z(card)) {
  3158. chip = channel >> 2;
  3159. channel &= 0x03;
  3160. index = card->bus_index;
  3161. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  3162. tmp = readb(base_addr + (CyCOR3 << index)) & CyREC_FIFO;
  3163. return put_user(tmp, value);
  3164. }
  3165. return 0;
  3166. } /* get_threshold */
  3167. static int set_default_threshold(struct cyclades_port *info,
  3168. unsigned long value)
  3169. {
  3170. info->default_threshold = value & 0x0f;
  3171. return 0;
  3172. } /* set_default_threshold */
  3173. static int get_default_threshold(struct cyclades_port *info,
  3174. unsigned long __user *value)
  3175. {
  3176. return put_user(info->default_threshold, value);
  3177. } /* get_default_threshold */
  3178. static int set_timeout(struct cyclades_port *info, unsigned long value)
  3179. {
  3180. struct cyclades_card *card;
  3181. void __iomem *base_addr;
  3182. int channel, chip, index;
  3183. unsigned long flags;
  3184. card = info->card;
  3185. channel = info->line - card->first_line;
  3186. if (!cy_is_Z(card)) {
  3187. chip = channel >> 2;
  3188. channel &= 0x03;
  3189. index = card->bus_index;
  3190. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  3191. spin_lock_irqsave(&card->card_lock, flags);
  3192. cy_writeb(base_addr + (CyRTPR << index), value & 0xff);
  3193. spin_unlock_irqrestore(&card->card_lock, flags);
  3194. }
  3195. return 0;
  3196. } /* set_timeout */
  3197. static int get_timeout(struct cyclades_port *info,
  3198. unsigned long __user *value)
  3199. {
  3200. struct cyclades_card *card;
  3201. void __iomem *base_addr;
  3202. int channel, chip, index;
  3203. unsigned long tmp;
  3204. card = info->card;
  3205. channel = info->line - card->first_line;
  3206. if (!cy_is_Z(card)) {
  3207. chip = channel >> 2;
  3208. channel &= 0x03;
  3209. index = card->bus_index;
  3210. base_addr = card->base_addr + (cy_chip_offset[chip] << index);
  3211. tmp = readb(base_addr + (CyRTPR << index));
  3212. return put_user(tmp, value);
  3213. }
  3214. return 0;
  3215. } /* get_timeout */
  3216. static int set_default_timeout(struct cyclades_port *info, unsigned long value)
  3217. {
  3218. info->default_timeout = value & 0xff;
  3219. return 0;
  3220. } /* set_default_timeout */
  3221. static int get_default_timeout(struct cyclades_port *info,
  3222. unsigned long __user *value)
  3223. {
  3224. return put_user(info->default_timeout, value);
  3225. } /* get_default_timeout */
  3226. /*
  3227. * This routine allows the tty driver to implement device-
  3228. * specific ioctl's. If the ioctl number passed in cmd is
  3229. * not recognized by the driver, it should return ENOIOCTLCMD.
  3230. */
  3231. static int
  3232. cy_ioctl(struct tty_struct *tty, struct file *file,
  3233. unsigned int cmd, unsigned long arg)
  3234. {
  3235. struct cyclades_port *info = tty->driver_data;
  3236. struct cyclades_icount cprev, cnow; /* kernel counter temps */
  3237. struct serial_icounter_struct __user *p_cuser; /* user space */
  3238. int ret_val = 0;
  3239. unsigned long flags;
  3240. void __user *argp = (void __user *)arg;
  3241. if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
  3242. return -ENODEV;
  3243. #ifdef CY_DEBUG_OTHER
  3244. printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
  3245. info->line, cmd, arg);
  3246. #endif
  3247. lock_kernel();
  3248. switch (cmd) {
  3249. case CYGETMON:
  3250. ret_val = get_mon_info(info, argp);
  3251. break;
  3252. case CYGETTHRESH:
  3253. ret_val = get_threshold(info, argp);
  3254. break;
  3255. case CYSETTHRESH:
  3256. ret_val = set_threshold(info, arg);
  3257. break;
  3258. case CYGETDEFTHRESH:
  3259. ret_val = get_default_threshold(info, argp);
  3260. break;
  3261. case CYSETDEFTHRESH:
  3262. ret_val = set_default_threshold(info, arg);
  3263. break;
  3264. case CYGETTIMEOUT:
  3265. ret_val = get_timeout(info, argp);
  3266. break;
  3267. case CYSETTIMEOUT:
  3268. ret_val = set_timeout(info, arg);
  3269. break;
  3270. case CYGETDEFTIMEOUT:
  3271. ret_val = get_default_timeout(info, argp);
  3272. break;
  3273. case CYSETDEFTIMEOUT:
  3274. ret_val = set_default_timeout(info, arg);
  3275. break;
  3276. case CYSETRFLOW:
  3277. info->rflow = (int)arg;
  3278. ret_val = 0;
  3279. break;
  3280. case CYGETRFLOW:
  3281. ret_val = info->rflow;
  3282. break;
  3283. case CYSETRTSDTR_INV:
  3284. info->rtsdtr_inv = (int)arg;
  3285. ret_val = 0;
  3286. break;
  3287. case CYGETRTSDTR_INV:
  3288. ret_val = info->rtsdtr_inv;
  3289. break;
  3290. case CYGETCD1400VER:
  3291. ret_val = info->chip_rev;
  3292. break;
  3293. #ifndef CONFIG_CYZ_INTR
  3294. case CYZSETPOLLCYCLE:
  3295. cyz_polling_cycle = (arg * HZ) / 1000;
  3296. ret_val = 0;
  3297. break;
  3298. case CYZGETPOLLCYCLE:
  3299. ret_val = (cyz_polling_cycle * 1000) / HZ;
  3300. break;
  3301. #endif /* CONFIG_CYZ_INTR */
  3302. case CYSETWAIT:
  3303. info->port.closing_wait = (unsigned short)arg * HZ / 100;
  3304. ret_val = 0;
  3305. break;
  3306. case CYGETWAIT:
  3307. ret_val = info->port.closing_wait / (HZ / 100);
  3308. break;
  3309. case TIOCGSERIAL:
  3310. ret_val = get_serial_info(info, argp);
  3311. break;
  3312. case TIOCSSERIAL:
  3313. ret_val = cy_set_serial_info(info, tty, argp);
  3314. break;
  3315. case TIOCSERGETLSR: /* Get line status register */
  3316. ret_val = get_lsr_info(info, argp);
  3317. break;
  3318. /*
  3319. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  3320. * - mask passed in arg for lines of interest
  3321. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  3322. * Caller should use TIOCGICOUNT to see which one it was
  3323. */
  3324. case TIOCMIWAIT:
  3325. spin_lock_irqsave(&info->card->card_lock, flags);
  3326. /* note the counters on entry */
  3327. cnow = info->icount;
  3328. spin_unlock_irqrestore(&info->card->card_lock, flags);
  3329. ret_val = wait_event_interruptible(info->delta_msr_wait, ({
  3330. cprev = cnow;
  3331. spin_lock_irqsave(&info->card->card_lock, flags);
  3332. cnow = info->icount; /* atomic copy */
  3333. spin_unlock_irqrestore(&info->card->card_lock, flags);
  3334. ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  3335. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  3336. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  3337. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
  3338. }));
  3339. break;
  3340. /*
  3341. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  3342. * Return: write counters to the user passed counter struct
  3343. * NB: both 1->0 and 0->1 transitions are counted except for
  3344. * RI where only 0->1 is counted.
  3345. */
  3346. case TIOCGICOUNT:
  3347. spin_lock_irqsave(&info->card->card_lock, flags);
  3348. cnow = info->icount;
  3349. spin_unlock_irqrestore(&info->card->card_lock, flags);
  3350. p_cuser = argp;
  3351. ret_val = put_user(cnow.cts, &p_cuser->cts);
  3352. if (ret_val)
  3353. break;
  3354. ret_val = put_user(cnow.dsr, &p_cuser->dsr);
  3355. if (ret_val)
  3356. break;
  3357. ret_val = put_user(cnow.rng, &p_cuser->rng);
  3358. if (ret_val)
  3359. break;
  3360. ret_val = put_user(cnow.dcd, &p_cuser->dcd);
  3361. if (ret_val)
  3362. break;
  3363. ret_val = put_user(cnow.rx, &p_cuser->rx);
  3364. if (ret_val)
  3365. break;
  3366. ret_val = put_user(cnow.tx, &p_cuser->tx);
  3367. if (ret_val)
  3368. break;
  3369. ret_val = put_user(cnow.frame, &p_cuser->frame);
  3370. if (ret_val)
  3371. break;
  3372. ret_val = put_user(cnow.overrun, &p_cuser->overrun);
  3373. if (ret_val)
  3374. break;
  3375. ret_val = put_user(cnow.parity, &p_cuser->parity);
  3376. if (ret_val)
  3377. break;
  3378. ret_val = put_user(cnow.brk, &p_cuser->brk);
  3379. if (ret_val)
  3380. break;
  3381. ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
  3382. if (ret_val)
  3383. break;
  3384. ret_val = 0;
  3385. break;
  3386. default:
  3387. ret_val = -ENOIOCTLCMD;
  3388. }
  3389. unlock_kernel();
  3390. #ifdef CY_DEBUG_OTHER
  3391. printk(KERN_DEBUG "cyc:cy_ioctl done\n");
  3392. #endif
  3393. return ret_val;
  3394. } /* cy_ioctl */
  3395. /*
  3396. * This routine allows the tty driver to be notified when
  3397. * device's termios settings have changed. Note that a
  3398. * well-designed tty driver should be prepared to accept the case
  3399. * where old == NULL, and try to do something rational.
  3400. */
  3401. static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  3402. {
  3403. struct cyclades_port *info = tty->driver_data;
  3404. #ifdef CY_DEBUG_OTHER
  3405. printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
  3406. #endif
  3407. cy_set_line_char(info, tty);
  3408. if ((old_termios->c_cflag & CRTSCTS) &&
  3409. !(tty->termios->c_cflag & CRTSCTS)) {
  3410. tty->hw_stopped = 0;
  3411. cy_start(tty);
  3412. }
  3413. #if 0
  3414. /*
  3415. * No need to wake up processes in open wait, since they
  3416. * sample the CLOCAL flag once, and don't recheck it.
  3417. * XXX It's not clear whether the current behavior is correct
  3418. * or not. Hence, this may change.....
  3419. */
  3420. if (!(old_termios->c_cflag & CLOCAL) &&
  3421. (tty->termios->c_cflag & CLOCAL))
  3422. wake_up_interruptible(&info->port.open_wait);
  3423. #endif
  3424. } /* cy_set_termios */
  3425. /* This function is used to send a high-priority XON/XOFF character to
  3426. the device.
  3427. */
  3428. static void cy_send_xchar(struct tty_struct *tty, char ch)
  3429. {
  3430. struct cyclades_port *info = tty->driver_data;
  3431. struct cyclades_card *card;
  3432. int channel;
  3433. if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
  3434. return;
  3435. info->x_char = ch;
  3436. if (ch)
  3437. cy_start(tty);
  3438. card = info->card;
  3439. channel = info->line - card->first_line;
  3440. if (cy_is_Z(card)) {
  3441. if (ch == STOP_CHAR(tty))
  3442. cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
  3443. else if (ch == START_CHAR(tty))
  3444. cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
  3445. }
  3446. }
  3447. /* This routine is called by the upper-layer tty layer to signal
  3448. that incoming characters should be throttled because the input
  3449. buffers are close to full.
  3450. */
  3451. static void cy_throttle(struct tty_struct *tty)
  3452. {
  3453. struct cyclades_port *info = tty->driver_data;
  3454. struct cyclades_card *card;
  3455. unsigned long flags;
  3456. void __iomem *base_addr;
  3457. int chip, channel, index;
  3458. #ifdef CY_DEBUG_THROTTLE
  3459. char buf[64];
  3460. printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
  3461. tty->ldisc.chars_in_buffer(tty), info->line);
  3462. #endif
  3463. if (serial_paranoia_check(info, tty->name, "cy_throttle"))
  3464. return;
  3465. card = info->card;
  3466. if (I_IXOFF(tty)) {
  3467. if (!cy_is_Z(card))
  3468. cy_send_xchar(tty, STOP_CHAR(tty));
  3469. else
  3470. info->throttle = 1;
  3471. }
  3472. if (tty->termios->c_cflag & CRTSCTS) {
  3473. channel = info->line - card->first_line;
  3474. if (!cy_is_Z(card)) {
  3475. chip = channel >> 2;
  3476. channel &= 0x03;
  3477. index = card->bus_index;
  3478. base_addr = card->base_addr +
  3479. (cy_chip_offset[chip] << index);
  3480. spin_lock_irqsave(&card->card_lock, flags);
  3481. cy_writeb(base_addr + (CyCAR << index),
  3482. (u_char) channel);
  3483. if (info->rtsdtr_inv) {
  3484. cy_writeb(base_addr + (CyMSVR2 << index),
  3485. ~CyDTR);
  3486. } else {
  3487. cy_writeb(base_addr + (CyMSVR1 << index),
  3488. ~CyRTS);
  3489. }
  3490. spin_unlock_irqrestore(&card->card_lock, flags);
  3491. } else {
  3492. info->throttle = 1;
  3493. }
  3494. }
  3495. } /* cy_throttle */
  3496. /*
  3497. * This routine notifies the tty driver that it should signal
  3498. * that characters can now be sent to the tty without fear of
  3499. * overrunning the input buffers of the line disciplines.
  3500. */
  3501. static void cy_unthrottle(struct tty_struct *tty)
  3502. {
  3503. struct cyclades_port *info = tty->driver_data;
  3504. struct cyclades_card *card;
  3505. unsigned long flags;
  3506. void __iomem *base_addr;
  3507. int chip, channel, index;
  3508. #ifdef CY_DEBUG_THROTTLE
  3509. char buf[64];
  3510. printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
  3511. tty_name(tty, buf), tty_chars_in_buffer(tty), info->line);
  3512. #endif
  3513. if (serial_paranoia_check(info, tty->name, "cy_unthrottle"))
  3514. return;
  3515. if (I_IXOFF(tty)) {
  3516. if (info->x_char)
  3517. info->x_char = 0;
  3518. else
  3519. cy_send_xchar(tty, START_CHAR(tty));
  3520. }
  3521. if (tty->termios->c_cflag & CRTSCTS) {
  3522. card = info->card;
  3523. channel = info->line - card->first_line;
  3524. if (!cy_is_Z(card)) {
  3525. chip = channel >> 2;
  3526. channel &= 0x03;
  3527. index = card->bus_index;
  3528. base_addr = card->base_addr +
  3529. (cy_chip_offset[chip] << index);
  3530. spin_lock_irqsave(&card->card_lock, flags);
  3531. cy_writeb(base_addr + (CyCAR << index),
  3532. (u_char) channel);
  3533. if (info->rtsdtr_inv) {
  3534. cy_writeb(base_addr + (CyMSVR2 << index),
  3535. CyDTR);
  3536. } else {
  3537. cy_writeb(base_addr + (CyMSVR1 << index),
  3538. CyRTS);
  3539. }
  3540. spin_unlock_irqrestore(&card->card_lock, flags);
  3541. } else {
  3542. info->throttle = 0;
  3543. }
  3544. }
  3545. } /* cy_unthrottle */
  3546. /* cy_start and cy_stop provide software output flow control as a
  3547. function of XON/XOFF, software CTS, and other such stuff.
  3548. */
  3549. static void cy_stop(struct tty_struct *tty)
  3550. {
  3551. struct cyclades_card *cinfo;
  3552. struct cyclades_port *info = tty->driver_data;
  3553. void __iomem *base_addr;
  3554. int chip, channel, index;
  3555. unsigned long flags;
  3556. #ifdef CY_DEBUG_OTHER
  3557. printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
  3558. #endif
  3559. if (serial_paranoia_check(info, tty->name, "cy_stop"))
  3560. return;
  3561. cinfo = info->card;
  3562. channel = info->line - cinfo->first_line;
  3563. if (!cy_is_Z(cinfo)) {
  3564. index = cinfo->bus_index;
  3565. chip = channel >> 2;
  3566. channel &= 0x03;
  3567. base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
  3568. spin_lock_irqsave(&cinfo->card_lock, flags);
  3569. cy_writeb(base_addr + (CyCAR << index),
  3570. (u_char)(channel & 0x0003)); /* index channel */
  3571. cy_writeb(base_addr + (CySRER << index),
  3572. readb(base_addr + (CySRER << index)) & ~CyTxRdy);
  3573. spin_unlock_irqrestore(&cinfo->card_lock, flags);
  3574. }
  3575. } /* cy_stop */
  3576. static void cy_start(struct tty_struct *tty)
  3577. {
  3578. struct cyclades_card *cinfo;
  3579. struct cyclades_port *info = tty->driver_data;
  3580. void __iomem *base_addr;
  3581. int chip, channel, index;
  3582. unsigned long flags;
  3583. #ifdef CY_DEBUG_OTHER
  3584. printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
  3585. #endif
  3586. if (serial_paranoia_check(info, tty->name, "cy_start"))
  3587. return;
  3588. cinfo = info->card;
  3589. channel = info->line - cinfo->first_line;
  3590. index = cinfo->bus_index;
  3591. if (!cy_is_Z(cinfo)) {
  3592. chip = channel >> 2;
  3593. channel &= 0x03;
  3594. base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
  3595. spin_lock_irqsave(&cinfo->card_lock, flags);
  3596. cy_writeb(base_addr + (CyCAR << index),
  3597. (u_char) (channel & 0x0003)); /* index channel */
  3598. cy_writeb(base_addr + (CySRER << index),
  3599. readb(base_addr + (CySRER << index)) | CyTxRdy);
  3600. spin_unlock_irqrestore(&cinfo->card_lock, flags);
  3601. }
  3602. } /* cy_start */
  3603. /*
  3604. * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
  3605. */
  3606. static void cy_hangup(struct tty_struct *tty)
  3607. {
  3608. struct cyclades_port *info = tty->driver_data;
  3609. #ifdef CY_DEBUG_OTHER
  3610. printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
  3611. #endif
  3612. if (serial_paranoia_check(info, tty->name, "cy_hangup"))
  3613. return;
  3614. cy_flush_buffer(tty);
  3615. cy_shutdown(info, tty);
  3616. tty_port_hangup(&info->port);
  3617. } /* cy_hangup */
  3618. static int cyy_carrier_raised(struct tty_port *port)
  3619. {
  3620. struct cyclades_port *info = container_of(port, struct cyclades_port,
  3621. port);
  3622. struct cyclades_card *cinfo = info->card;
  3623. void __iomem *base = cinfo->base_addr;
  3624. unsigned long flags;
  3625. int channel = info->line - cinfo->first_line;
  3626. int chip = channel >> 2, index = cinfo->bus_index;
  3627. u32 cd;
  3628. channel &= 0x03;
  3629. base += cy_chip_offset[chip] << index;
  3630. spin_lock_irqsave(&cinfo->card_lock, flags);
  3631. cy_writeb(base + (CyCAR << index), (u8)channel);
  3632. cd = readb(base + (CyMSVR1 << index)) & CyDCD;
  3633. spin_unlock_irqrestore(&cinfo->card_lock, flags);
  3634. return cd;
  3635. }
  3636. static void cyy_dtr_rts(struct tty_port *port, int raise)
  3637. {
  3638. struct cyclades_port *info = container_of(port, struct cyclades_port,
  3639. port);
  3640. struct cyclades_card *cinfo = info->card;
  3641. void __iomem *base = cinfo->base_addr;
  3642. unsigned long flags;
  3643. int channel = info->line - cinfo->first_line;
  3644. int chip = channel >> 2, index = cinfo->bus_index;
  3645. channel &= 0x03;
  3646. base += cy_chip_offset[chip] << index;
  3647. spin_lock_irqsave(&cinfo->card_lock, flags);
  3648. cy_writeb(base + (CyCAR << index), (u8)channel);
  3649. cy_writeb(base + (CyMSVR1 << index), raise ? CyRTS : ~CyRTS);
  3650. cy_writeb(base + (CyMSVR2 << index), raise ? CyDTR : ~CyDTR);
  3651. #ifdef CY_DEBUG_DTR
  3652. printk(KERN_DEBUG "%s: raising DTR\n", __func__);
  3653. printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
  3654. readb(base + (CyMSVR1 << index)),
  3655. readb(base + (CyMSVR2 << index)));
  3656. #endif
  3657. spin_unlock_irqrestore(&cinfo->card_lock, flags);
  3658. }
  3659. static int cyz_carrier_raised(struct tty_port *port)
  3660. {
  3661. struct cyclades_port *info = container_of(port, struct cyclades_port,
  3662. port);
  3663. return readl(&info->u.cyz.ch_ctrl->rs_status) & C_RS_DCD;
  3664. }
  3665. static void cyz_dtr_rts(struct tty_port *port, int raise)
  3666. {
  3667. struct cyclades_port *info = container_of(port, struct cyclades_port,
  3668. port);
  3669. struct cyclades_card *cinfo = info->card;
  3670. struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
  3671. int ret, channel = info->line - cinfo->first_line;
  3672. u32 rs;
  3673. rs = readl(&ch_ctrl->rs_control);
  3674. if (raise)
  3675. rs |= C_RS_RTS | C_RS_DTR;
  3676. else
  3677. rs &= ~(C_RS_RTS | C_RS_DTR);
  3678. cy_writel(&ch_ctrl->rs_control, rs);
  3679. ret = cyz_issue_cmd(cinfo, channel, C_CM_IOCTLM, 0L);
  3680. if (ret != 0)
  3681. printk(KERN_ERR "%s: retval on ttyC%d was %x\n",
  3682. __func__, info->line, ret);
  3683. #ifdef CY_DEBUG_DTR
  3684. printk(KERN_DEBUG "%s: raising Z DTR\n", __func__);
  3685. #endif
  3686. }
  3687. static const struct tty_port_operations cyy_port_ops = {
  3688. .carrier_raised = cyy_carrier_raised,
  3689. .dtr_rts = cyy_dtr_rts,
  3690. };
  3691. static const struct tty_port_operations cyz_port_ops = {
  3692. .carrier_raised = cyz_carrier_raised,
  3693. .dtr_rts = cyz_dtr_rts,
  3694. };
  3695. /*
  3696. * ---------------------------------------------------------------------
  3697. * cy_init() and friends
  3698. *
  3699. * cy_init() is called at boot-time to initialize the serial driver.
  3700. * ---------------------------------------------------------------------
  3701. */
  3702. static int __devinit cy_init_card(struct cyclades_card *cinfo)
  3703. {
  3704. struct cyclades_port *info;
  3705. unsigned int channel, port;
  3706. spin_lock_init(&cinfo->card_lock);
  3707. cinfo->intr_enabled = 0;
  3708. cinfo->ports = kcalloc(cinfo->nports, sizeof(*cinfo->ports),
  3709. GFP_KERNEL);
  3710. if (cinfo->ports == NULL) {
  3711. printk(KERN_ERR "Cyclades: cannot allocate ports\n");
  3712. return -ENOMEM;
  3713. }
  3714. for (channel = 0, port = cinfo->first_line; channel < cinfo->nports;
  3715. channel++, port++) {
  3716. info = &cinfo->ports[channel];
  3717. tty_port_init(&info->port);
  3718. info->magic = CYCLADES_MAGIC;
  3719. info->card = cinfo;
  3720. info->line = port;
  3721. info->port.closing_wait = CLOSING_WAIT_DELAY;
  3722. info->port.close_delay = 5 * HZ / 10;
  3723. info->port.flags = STD_COM_FLAGS;
  3724. init_completion(&info->shutdown_wait);
  3725. init_waitqueue_head(&info->delta_msr_wait);
  3726. if (cy_is_Z(cinfo)) {
  3727. struct FIRM_ID *firm_id = cinfo->base_addr + ID_ADDRESS;
  3728. struct ZFW_CTRL *zfw_ctrl;
  3729. info->port.ops = &cyz_port_ops;
  3730. info->type = PORT_STARTECH;
  3731. zfw_ctrl = cinfo->base_addr +
  3732. (readl(&firm_id->zfwctrl_addr) & 0xfffff);
  3733. info->u.cyz.ch_ctrl = &zfw_ctrl->ch_ctrl[channel];
  3734. info->u.cyz.buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
  3735. if (cinfo->hw_ver == ZO_V1)
  3736. info->xmit_fifo_size = CYZ_FIFO_SIZE;
  3737. else
  3738. info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
  3739. #ifdef CONFIG_CYZ_INTR
  3740. setup_timer(&cyz_rx_full_timer[port],
  3741. cyz_rx_restart, (unsigned long)info);
  3742. #endif
  3743. } else {
  3744. unsigned short chip_number;
  3745. int index = cinfo->bus_index;
  3746. info->port.ops = &cyy_port_ops;
  3747. info->type = PORT_CIRRUS;
  3748. info->xmit_fifo_size = CyMAX_CHAR_FIFO;
  3749. info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
  3750. info->cor2 = CyETC;
  3751. info->cor3 = 0x08; /* _very_ small rcv threshold */
  3752. chip_number = channel / CyPORTS_PER_CHIP;
  3753. info->chip_rev = readb(cinfo->base_addr +
  3754. (cy_chip_offset[chip_number] << index) +
  3755. (CyGFRCR << index));
  3756. if (info->chip_rev >= CD1400_REV_J) {
  3757. /* It is a CD1400 rev. J or later */
  3758. info->tbpr = baud_bpr_60[13]; /* Tx BPR */
  3759. info->tco = baud_co_60[13]; /* Tx CO */
  3760. info->rbpr = baud_bpr_60[13]; /* Rx BPR */
  3761. info->rco = baud_co_60[13]; /* Rx CO */
  3762. info->rtsdtr_inv = 1;
  3763. } else {
  3764. info->tbpr = baud_bpr_25[13]; /* Tx BPR */
  3765. info->tco = baud_co_25[13]; /* Tx CO */
  3766. info->rbpr = baud_bpr_25[13]; /* Rx BPR */
  3767. info->rco = baud_co_25[13]; /* Rx CO */
  3768. info->rtsdtr_inv = 0;
  3769. }
  3770. info->read_status_mask = CyTIMEOUT | CySPECHAR |
  3771. CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
  3772. }
  3773. }
  3774. #ifndef CONFIG_CYZ_INTR
  3775. if (cy_is_Z(cinfo) && !timer_pending(&cyz_timerlist)) {
  3776. mod_timer(&cyz_timerlist, jiffies + 1);
  3777. #ifdef CY_PCI_DEBUG
  3778. printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
  3779. #endif
  3780. }
  3781. #endif
  3782. return 0;
  3783. }
  3784. /* initialize chips on Cyclom-Y card -- return number of valid
  3785. chips (which is number of ports/4) */
  3786. static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
  3787. int index)
  3788. {
  3789. unsigned int chip_number;
  3790. void __iomem *base_addr;
  3791. cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
  3792. /* Cy_HwReset is 0x1400 */
  3793. cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
  3794. /* Cy_ClrIntr is 0x1800 */
  3795. udelay(500L);
  3796. for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD;
  3797. chip_number++) {
  3798. base_addr =
  3799. true_base_addr + (cy_chip_offset[chip_number] << index);
  3800. mdelay(1);
  3801. if (readb(base_addr + (CyCCR << index)) != 0x00) {
  3802. /*************
  3803. printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
  3804. chip_number, (unsigned long)base_addr);
  3805. *************/
  3806. return chip_number;
  3807. }
  3808. cy_writeb(base_addr + (CyGFRCR << index), 0);
  3809. udelay(10L);
  3810. /* The Cyclom-16Y does not decode address bit 9 and therefore
  3811. cannot distinguish between references to chip 0 and a non-
  3812. existent chip 4. If the preceding clearing of the supposed
  3813. chip 4 GFRCR register appears at chip 0, there is no chip 4
  3814. and this must be a Cyclom-16Y, not a Cyclom-32Ye.
  3815. */
  3816. if (chip_number == 4 && readb(true_base_addr +
  3817. (cy_chip_offset[0] << index) +
  3818. (CyGFRCR << index)) == 0) {
  3819. return chip_number;
  3820. }
  3821. cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
  3822. mdelay(1);
  3823. if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
  3824. /*
  3825. printk(" chip #%d at %#6lx is not responding ",
  3826. chip_number, (unsigned long)base_addr);
  3827. printk("(GFRCR stayed 0)\n",
  3828. */
  3829. return chip_number;
  3830. }
  3831. if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
  3832. 0x40) {
  3833. /*
  3834. printk(" chip #%d at %#6lx is not valid (GFRCR == "
  3835. "%#2x)\n",
  3836. chip_number, (unsigned long)base_addr,
  3837. base_addr[CyGFRCR<<index]);
  3838. */
  3839. return chip_number;
  3840. }
  3841. cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
  3842. if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
  3843. /* It is a CD1400 rev. J or later */
  3844. /* Impossible to reach 5ms with this chip.
  3845. Changed to 2ms instead (f = 500 Hz). */
  3846. cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
  3847. } else {
  3848. /* f = 200 Hz */
  3849. cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
  3850. }
  3851. /*
  3852. printk(" chip #%d at %#6lx is rev 0x%2x\n",
  3853. chip_number, (unsigned long)base_addr,
  3854. readb(base_addr+(CyGFRCR<<index)));
  3855. */
  3856. }
  3857. return chip_number;
  3858. } /* cyy_init_card */
  3859. /*
  3860. * ---------------------------------------------------------------------
  3861. * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
  3862. * sets global variables and return the number of ISA boards found.
  3863. * ---------------------------------------------------------------------
  3864. */
  3865. static int __init cy_detect_isa(void)
  3866. {
  3867. #ifdef CONFIG_ISA
  3868. unsigned short cy_isa_irq, nboard;
  3869. void __iomem *cy_isa_address;
  3870. unsigned short i, j, cy_isa_nchan;
  3871. #ifdef MODULE
  3872. int isparam = 0;
  3873. #endif
  3874. nboard = 0;
  3875. #ifdef MODULE
  3876. /* Check for module parameters */
  3877. for (i = 0; i < NR_CARDS; i++) {
  3878. if (maddr[i] || i) {
  3879. isparam = 1;
  3880. cy_isa_addresses[i] = maddr[i];
  3881. }
  3882. if (!maddr[i])
  3883. break;
  3884. }
  3885. #endif
  3886. /* scan the address table probing for Cyclom-Y/ISA boards */
  3887. for (i = 0; i < NR_ISA_ADDRS; i++) {
  3888. unsigned int isa_address = cy_isa_addresses[i];
  3889. if (isa_address == 0x0000)
  3890. return nboard;
  3891. /* probe for CD1400... */
  3892. cy_isa_address = ioremap_nocache(isa_address, CyISA_Ywin);
  3893. if (cy_isa_address == NULL) {
  3894. printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
  3895. "address\n");
  3896. continue;
  3897. }
  3898. cy_isa_nchan = CyPORTS_PER_CHIP *
  3899. cyy_init_card(cy_isa_address, 0);
  3900. if (cy_isa_nchan == 0) {
  3901. iounmap(cy_isa_address);
  3902. continue;
  3903. }
  3904. #ifdef MODULE
  3905. if (isparam && irq[i])
  3906. cy_isa_irq = irq[i];
  3907. else
  3908. #endif
  3909. /* find out the board's irq by probing */
  3910. cy_isa_irq = detect_isa_irq(cy_isa_address);
  3911. if (cy_isa_irq == 0) {
  3912. printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
  3913. "IRQ could not be detected.\n",
  3914. (unsigned long)cy_isa_address);
  3915. iounmap(cy_isa_address);
  3916. continue;
  3917. }
  3918. if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
  3919. printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
  3920. "more channels are available. Change NR_PORTS "
  3921. "in cyclades.c and recompile kernel.\n",
  3922. (unsigned long)cy_isa_address);
  3923. iounmap(cy_isa_address);
  3924. return nboard;
  3925. }
  3926. /* fill the next cy_card structure available */
  3927. for (j = 0; j < NR_CARDS; j++) {
  3928. if (cy_card[j].base_addr == NULL)
  3929. break;
  3930. }
  3931. if (j == NR_CARDS) { /* no more cy_cards available */
  3932. printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
  3933. "more cards can be used. Change NR_CARDS in "
  3934. "cyclades.c and recompile kernel.\n",
  3935. (unsigned long)cy_isa_address);
  3936. iounmap(cy_isa_address);
  3937. return nboard;
  3938. }
  3939. /* allocate IRQ */
  3940. if (request_irq(cy_isa_irq, cyy_interrupt,
  3941. IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
  3942. printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
  3943. "could not allocate IRQ#%d.\n",
  3944. (unsigned long)cy_isa_address, cy_isa_irq);
  3945. iounmap(cy_isa_address);
  3946. return nboard;
  3947. }
  3948. /* set cy_card */
  3949. cy_card[j].base_addr = cy_isa_address;
  3950. cy_card[j].ctl_addr.p9050 = NULL;
  3951. cy_card[j].irq = (int)cy_isa_irq;
  3952. cy_card[j].bus_index = 0;
  3953. cy_card[j].first_line = cy_next_channel;
  3954. cy_card[j].num_chips = cy_isa_nchan / CyPORTS_PER_CHIP;
  3955. cy_card[j].nports = cy_isa_nchan;
  3956. if (cy_init_card(&cy_card[j])) {
  3957. cy_card[j].base_addr = NULL;
  3958. free_irq(cy_isa_irq, &cy_card[j]);
  3959. iounmap(cy_isa_address);
  3960. continue;
  3961. }
  3962. nboard++;
  3963. printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
  3964. "%d channels starting from port %d\n",
  3965. j + 1, (unsigned long)cy_isa_address,
  3966. (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
  3967. cy_isa_irq, cy_isa_nchan, cy_next_channel);
  3968. for (j = cy_next_channel;
  3969. j < cy_next_channel + cy_isa_nchan; j++)
  3970. tty_register_device(cy_serial_driver, j, NULL);
  3971. cy_next_channel += cy_isa_nchan;
  3972. }
  3973. return nboard;
  3974. #else
  3975. return 0;
  3976. #endif /* CONFIG_ISA */
  3977. } /* cy_detect_isa */
  3978. #ifdef CONFIG_PCI
  3979. static inline int __devinit cyc_isfwstr(const char *str, unsigned int size)
  3980. {
  3981. unsigned int a;
  3982. for (a = 0; a < size && *str; a++, str++)
  3983. if (*str & 0x80)
  3984. return -EINVAL;
  3985. for (; a < size; a++, str++)
  3986. if (*str)
  3987. return -EINVAL;
  3988. return 0;
  3989. }
  3990. static inline void __devinit cyz_fpga_copy(void __iomem *fpga, const u8 *data,
  3991. unsigned int size)
  3992. {
  3993. for (; size > 0; size--) {
  3994. cy_writel(fpga, *data++);
  3995. udelay(10);
  3996. }
  3997. }
  3998. static void __devinit plx_init(struct pci_dev *pdev, int irq,
  3999. struct RUNTIME_9060 __iomem *addr)
  4000. {
  4001. /* Reset PLX */
  4002. cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x40000000);
  4003. udelay(100L);
  4004. cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x40000000);
  4005. /* Reload Config. Registers from EEPROM */
  4006. cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x20000000);
  4007. udelay(100L);
  4008. cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x20000000);
  4009. /* For some yet unknown reason, once the PLX9060 reloads the EEPROM,
  4010. * the IRQ is lost and, thus, we have to re-write it to the PCI config.
  4011. * registers. This will remain here until we find a permanent fix.
  4012. */
  4013. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
  4014. }
  4015. static int __devinit __cyz_load_fw(const struct firmware *fw,
  4016. const char *name, const u32 mailbox, void __iomem *base,
  4017. void __iomem *fpga)
  4018. {
  4019. const void *ptr = fw->data;
  4020. const struct zfile_header *h = ptr;
  4021. const struct zfile_config *c, *cs;
  4022. const struct zfile_block *b, *bs;
  4023. unsigned int a, tmp, len = fw->size;
  4024. #define BAD_FW KERN_ERR "Bad firmware: "
  4025. if (len < sizeof(*h)) {
  4026. printk(BAD_FW "too short: %u<%zu\n", len, sizeof(*h));
  4027. return -EINVAL;
  4028. }
  4029. cs = ptr + h->config_offset;
  4030. bs = ptr + h->block_offset;
  4031. if ((void *)(cs + h->n_config) > ptr + len ||
  4032. (void *)(bs + h->n_blocks) > ptr + len) {
  4033. printk(BAD_FW "too short");
  4034. return -EINVAL;
  4035. }
  4036. if (cyc_isfwstr(h->name, sizeof(h->name)) ||
  4037. cyc_isfwstr(h->date, sizeof(h->date))) {
  4038. printk(BAD_FW "bad formatted header string\n");
  4039. return -EINVAL;
  4040. }
  4041. if (strncmp(name, h->name, sizeof(h->name))) {
  4042. printk(BAD_FW "bad name '%s' (expected '%s')\n", h->name, name);
  4043. return -EINVAL;
  4044. }
  4045. tmp = 0;
  4046. for (c = cs; c < cs + h->n_config; c++) {
  4047. for (a = 0; a < c->n_blocks; a++)
  4048. if (c->block_list[a] > h->n_blocks) {
  4049. printk(BAD_FW "bad block ref number in cfgs\n");
  4050. return -EINVAL;
  4051. }
  4052. if (c->mailbox == mailbox && c->function == 0) /* 0 is normal */
  4053. tmp++;
  4054. }
  4055. if (!tmp) {
  4056. printk(BAD_FW "nothing appropriate\n");
  4057. return -EINVAL;
  4058. }
  4059. for (b = bs; b < bs + h->n_blocks; b++)
  4060. if (b->file_offset + b->size > len) {
  4061. printk(BAD_FW "bad block data offset\n");
  4062. return -EINVAL;
  4063. }
  4064. /* everything is OK, let's seek'n'load it */
  4065. for (c = cs; c < cs + h->n_config; c++)
  4066. if (c->mailbox == mailbox && c->function == 0)
  4067. break;
  4068. for (a = 0; a < c->n_blocks; a++) {
  4069. b = &bs[c->block_list[a]];
  4070. if (b->type == ZBLOCK_FPGA) {
  4071. if (fpga != NULL)
  4072. cyz_fpga_copy(fpga, ptr + b->file_offset,
  4073. b->size);
  4074. } else {
  4075. if (base != NULL)
  4076. memcpy_toio(base + b->ram_offset,
  4077. ptr + b->file_offset, b->size);
  4078. }
  4079. }
  4080. #undef BAD_FW
  4081. return 0;
  4082. }
  4083. static int __devinit cyz_load_fw(struct pci_dev *pdev, void __iomem *base_addr,
  4084. struct RUNTIME_9060 __iomem *ctl_addr, int irq)
  4085. {
  4086. const struct firmware *fw;
  4087. struct FIRM_ID __iomem *fid = base_addr + ID_ADDRESS;
  4088. struct CUSTOM_REG __iomem *cust = base_addr;
  4089. struct ZFW_CTRL __iomem *pt_zfwctrl;
  4090. void __iomem *tmp;
  4091. u32 mailbox, status, nchan;
  4092. unsigned int i;
  4093. int retval;
  4094. retval = request_firmware(&fw, "cyzfirm.bin", &pdev->dev);
  4095. if (retval) {
  4096. dev_err(&pdev->dev, "can't get firmware\n");
  4097. goto err;
  4098. }
  4099. /* Check whether the firmware is already loaded and running. If
  4100. positive, skip this board */
  4101. if (__cyz_fpga_loaded(ctl_addr) && readl(&fid->signature) == ZFIRM_ID) {
  4102. u32 cntval = readl(base_addr + 0x190);
  4103. udelay(100);
  4104. if (cntval != readl(base_addr + 0x190)) {
  4105. /* FW counter is working, FW is running */
  4106. dev_dbg(&pdev->dev, "Cyclades-Z FW already loaded. "
  4107. "Skipping board.\n");
  4108. retval = 0;
  4109. goto err_rel;
  4110. }
  4111. }
  4112. /* start boot */
  4113. cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) &
  4114. ~0x00030800UL);
  4115. mailbox = readl(&ctl_addr->mail_box_0);
  4116. if (mailbox == 0 || __cyz_fpga_loaded(ctl_addr)) {
  4117. /* stops CPU and set window to beginning of RAM */
  4118. cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
  4119. cy_writel(&cust->cpu_stop, 0);
  4120. cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
  4121. udelay(100);
  4122. }
  4123. plx_init(pdev, irq, ctl_addr);
  4124. if (mailbox != 0) {
  4125. /* load FPGA */
  4126. retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, NULL,
  4127. base_addr);
  4128. if (retval)
  4129. goto err_rel;
  4130. if (!__cyz_fpga_loaded(ctl_addr)) {
  4131. dev_err(&pdev->dev, "fw upload successful, but fw is "
  4132. "not loaded\n");
  4133. goto err_rel;
  4134. }
  4135. }
  4136. /* stops CPU and set window to beginning of RAM */
  4137. cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
  4138. cy_writel(&cust->cpu_stop, 0);
  4139. cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
  4140. udelay(100);
  4141. /* clear memory */
  4142. for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
  4143. cy_writeb(tmp, 255);
  4144. if (mailbox != 0) {
  4145. /* set window to last 512K of RAM */
  4146. cy_writel(&ctl_addr->loc_addr_base, WIN_RAM + RAM_SIZE);
  4147. for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
  4148. cy_writeb(tmp, 255);
  4149. /* set window to beginning of RAM */
  4150. cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
  4151. }
  4152. retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, base_addr, NULL);
  4153. release_firmware(fw);
  4154. if (retval)
  4155. goto err;
  4156. /* finish boot and start boards */
  4157. cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
  4158. cy_writel(&cust->cpu_start, 0);
  4159. cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
  4160. i = 0;
  4161. while ((status = readl(&fid->signature)) != ZFIRM_ID && i++ < 40)
  4162. msleep(100);
  4163. if (status != ZFIRM_ID) {
  4164. if (status == ZFIRM_HLT) {
  4165. dev_err(&pdev->dev, "you need an external power supply "
  4166. "for this number of ports. Firmware halted and "
  4167. "board reset.\n");
  4168. retval = -EIO;
  4169. goto err;
  4170. }
  4171. dev_warn(&pdev->dev, "fid->signature = 0x%x... Waiting "
  4172. "some more time\n", status);
  4173. while ((status = readl(&fid->signature)) != ZFIRM_ID &&
  4174. i++ < 200)
  4175. msleep(100);
  4176. if (status != ZFIRM_ID) {
  4177. dev_err(&pdev->dev, "Board not started in 20 seconds! "
  4178. "Giving up. (fid->signature = 0x%x)\n",
  4179. status);
  4180. dev_info(&pdev->dev, "*** Warning ***: if you are "
  4181. "upgrading the FW, please power cycle the "
  4182. "system before loading the new FW to the "
  4183. "Cyclades-Z.\n");
  4184. if (__cyz_fpga_loaded(ctl_addr))
  4185. plx_init(pdev, irq, ctl_addr);
  4186. retval = -EIO;
  4187. goto err;
  4188. }
  4189. dev_dbg(&pdev->dev, "Firmware started after %d seconds.\n",
  4190. i / 10);
  4191. }
  4192. pt_zfwctrl = base_addr + readl(&fid->zfwctrl_addr);
  4193. dev_dbg(&pdev->dev, "fid=> %p, zfwctrl_addr=> %x, npt_zfwctrl=> %p\n",
  4194. base_addr + ID_ADDRESS, readl(&fid->zfwctrl_addr),
  4195. base_addr + readl(&fid->zfwctrl_addr));
  4196. nchan = readl(&pt_zfwctrl->board_ctrl.n_channel);
  4197. dev_info(&pdev->dev, "Cyclades-Z FW loaded: version = %x, ports = %u\n",
  4198. readl(&pt_zfwctrl->board_ctrl.fw_version), nchan);
  4199. if (nchan == 0) {
  4200. dev_warn(&pdev->dev, "no Cyclades-Z ports were found. Please "
  4201. "check the connection between the Z host card and the "
  4202. "serial expanders.\n");
  4203. if (__cyz_fpga_loaded(ctl_addr))
  4204. plx_init(pdev, irq, ctl_addr);
  4205. dev_info(&pdev->dev, "Null number of ports detected. Board "
  4206. "reset.\n");
  4207. retval = 0;
  4208. goto err;
  4209. }
  4210. cy_writel(&pt_zfwctrl->board_ctrl.op_system, C_OS_LINUX);
  4211. cy_writel(&pt_zfwctrl->board_ctrl.dr_version, DRIVER_VERSION);
  4212. /*
  4213. Early firmware failed to start looking for commands.
  4214. This enables firmware interrupts for those commands.
  4215. */
  4216. cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
  4217. (1 << 17));
  4218. cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
  4219. 0x00030800UL);
  4220. return nchan;
  4221. err_rel:
  4222. release_firmware(fw);
  4223. err:
  4224. return retval;
  4225. }
  4226. static int __devinit cy_pci_probe(struct pci_dev *pdev,
  4227. const struct pci_device_id *ent)
  4228. {
  4229. void __iomem *addr0 = NULL, *addr2 = NULL;
  4230. char *card_name = NULL;
  4231. u32 uninitialized_var(mailbox);
  4232. unsigned int device_id, nchan = 0, card_no, i;
  4233. unsigned char plx_ver;
  4234. int retval, irq;
  4235. retval = pci_enable_device(pdev);
  4236. if (retval) {
  4237. dev_err(&pdev->dev, "cannot enable device\n");
  4238. goto err;
  4239. }
  4240. /* read PCI configuration area */
  4241. irq = pdev->irq;
  4242. device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
  4243. #if defined(__alpha__)
  4244. if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
  4245. dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
  4246. "addresses on Alpha systems.\n");
  4247. retval = -EIO;
  4248. goto err_dis;
  4249. }
  4250. #endif
  4251. if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
  4252. dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
  4253. "addresses\n");
  4254. retval = -EIO;
  4255. goto err_dis;
  4256. }
  4257. if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
  4258. dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
  4259. "it...\n");
  4260. pdev->resource[2].flags &= ~IORESOURCE_IO;
  4261. }
  4262. retval = pci_request_regions(pdev, "cyclades");
  4263. if (retval) {
  4264. dev_err(&pdev->dev, "failed to reserve resources\n");
  4265. goto err_dis;
  4266. }
  4267. retval = -EIO;
  4268. if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
  4269. device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
  4270. card_name = "Cyclom-Y";
  4271. addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
  4272. CyPCI_Yctl);
  4273. if (addr0 == NULL) {
  4274. dev_err(&pdev->dev, "can't remap ctl region\n");
  4275. goto err_reg;
  4276. }
  4277. addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
  4278. CyPCI_Ywin);
  4279. if (addr2 == NULL) {
  4280. dev_err(&pdev->dev, "can't remap base region\n");
  4281. goto err_unmap;
  4282. }
  4283. nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
  4284. if (nchan == 0) {
  4285. dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
  4286. "Serial-Modules\n");
  4287. goto err_unmap;
  4288. }
  4289. } else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
  4290. struct RUNTIME_9060 __iomem *ctl_addr;
  4291. ctl_addr = addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
  4292. CyPCI_Zctl);
  4293. if (addr0 == NULL) {
  4294. dev_err(&pdev->dev, "can't remap ctl region\n");
  4295. goto err_reg;
  4296. }
  4297. /* Disable interrupts on the PLX before resetting it */
  4298. cy_writew(&ctl_addr->intr_ctrl_stat,
  4299. readw(&ctl_addr->intr_ctrl_stat) & ~0x0900);
  4300. plx_init(pdev, irq, addr0);
  4301. mailbox = readl(&ctl_addr->mail_box_0);
  4302. addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
  4303. mailbox == ZE_V1 ? CyPCI_Ze_win : CyPCI_Zwin);
  4304. if (addr2 == NULL) {
  4305. dev_err(&pdev->dev, "can't remap base region\n");
  4306. goto err_unmap;
  4307. }
  4308. if (mailbox == ZE_V1) {
  4309. card_name = "Cyclades-Ze";
  4310. } else {
  4311. card_name = "Cyclades-8Zo";
  4312. #ifdef CY_PCI_DEBUG
  4313. if (mailbox == ZO_V1) {
  4314. cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
  4315. dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
  4316. "id %lx, ver %lx\n", (ulong)(0xff &
  4317. readl(&((struct CUSTOM_REG *)addr2)->
  4318. fpga_id)), (ulong)(0xff &
  4319. readl(&((struct CUSTOM_REG *)addr2)->
  4320. fpga_version)));
  4321. cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
  4322. } else {
  4323. dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
  4324. "Cyclades-Z board. FPGA not loaded\n");
  4325. }
  4326. #endif
  4327. /* The following clears the firmware id word. This
  4328. ensures that the driver will not attempt to talk to
  4329. the board until it has been properly initialized.
  4330. */
  4331. if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
  4332. cy_writel(addr2 + ID_ADDRESS, 0L);
  4333. }
  4334. retval = cyz_load_fw(pdev, addr2, addr0, irq);
  4335. if (retval <= 0)
  4336. goto err_unmap;
  4337. nchan = retval;
  4338. }
  4339. if ((cy_next_channel + nchan) > NR_PORTS) {
  4340. dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
  4341. "channels are available. Change NR_PORTS in "
  4342. "cyclades.c and recompile kernel.\n");
  4343. goto err_unmap;
  4344. }
  4345. /* fill the next cy_card structure available */
  4346. for (card_no = 0; card_no < NR_CARDS; card_no++) {
  4347. if (cy_card[card_no].base_addr == NULL)
  4348. break;
  4349. }
  4350. if (card_no == NR_CARDS) { /* no more cy_cards available */
  4351. dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
  4352. "more cards can be used. Change NR_CARDS in "
  4353. "cyclades.c and recompile kernel.\n");
  4354. goto err_unmap;
  4355. }
  4356. if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
  4357. device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
  4358. /* allocate IRQ */
  4359. retval = request_irq(irq, cyy_interrupt,
  4360. IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
  4361. if (retval) {
  4362. dev_err(&pdev->dev, "could not allocate IRQ\n");
  4363. goto err_unmap;
  4364. }
  4365. cy_card[card_no].num_chips = nchan / CyPORTS_PER_CHIP;
  4366. } else {
  4367. struct FIRM_ID __iomem *firm_id = addr2 + ID_ADDRESS;
  4368. struct ZFW_CTRL __iomem *zfw_ctrl;
  4369. zfw_ctrl = addr2 + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
  4370. cy_card[card_no].hw_ver = mailbox;
  4371. cy_card[card_no].num_chips = (unsigned int)-1;
  4372. cy_card[card_no].board_ctrl = &zfw_ctrl->board_ctrl;
  4373. #ifdef CONFIG_CYZ_INTR
  4374. /* allocate IRQ only if board has an IRQ */
  4375. if (irq != 0 && irq != 255) {
  4376. retval = request_irq(irq, cyz_interrupt,
  4377. IRQF_SHARED, "Cyclades-Z",
  4378. &cy_card[card_no]);
  4379. if (retval) {
  4380. dev_err(&pdev->dev, "could not allocate IRQ\n");
  4381. goto err_unmap;
  4382. }
  4383. }
  4384. #endif /* CONFIG_CYZ_INTR */
  4385. }
  4386. /* set cy_card */
  4387. cy_card[card_no].base_addr = addr2;
  4388. cy_card[card_no].ctl_addr.p9050 = addr0;
  4389. cy_card[card_no].irq = irq;
  4390. cy_card[card_no].bus_index = 1;
  4391. cy_card[card_no].first_line = cy_next_channel;
  4392. cy_card[card_no].nports = nchan;
  4393. retval = cy_init_card(&cy_card[card_no]);
  4394. if (retval)
  4395. goto err_null;
  4396. pci_set_drvdata(pdev, &cy_card[card_no]);
  4397. if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
  4398. device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
  4399. /* enable interrupts in the PCI interface */
  4400. plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
  4401. switch (plx_ver) {
  4402. case PLX_9050:
  4403. cy_writeb(addr0 + 0x4c, 0x43);
  4404. break;
  4405. case PLX_9060:
  4406. case PLX_9080:
  4407. default: /* Old boards, use PLX_9060 */
  4408. {
  4409. struct RUNTIME_9060 __iomem *ctl_addr = addr0;
  4410. plx_init(pdev, irq, ctl_addr);
  4411. cy_writew(&ctl_addr->intr_ctrl_stat,
  4412. readw(&ctl_addr->intr_ctrl_stat) | 0x0900);
  4413. break;
  4414. }
  4415. }
  4416. }
  4417. dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
  4418. "port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
  4419. for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
  4420. tty_register_device(cy_serial_driver, i, &pdev->dev);
  4421. cy_next_channel += nchan;
  4422. return 0;
  4423. err_null:
  4424. cy_card[card_no].base_addr = NULL;
  4425. free_irq(irq, &cy_card[card_no]);
  4426. err_unmap:
  4427. iounmap(addr0);
  4428. if (addr2)
  4429. iounmap(addr2);
  4430. err_reg:
  4431. pci_release_regions(pdev);
  4432. err_dis:
  4433. pci_disable_device(pdev);
  4434. err:
  4435. return retval;
  4436. }
  4437. static void __devexit cy_pci_remove(struct pci_dev *pdev)
  4438. {
  4439. struct cyclades_card *cinfo = pci_get_drvdata(pdev);
  4440. unsigned int i;
  4441. /* non-Z with old PLX */
  4442. if (!cy_is_Z(cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
  4443. PLX_9050)
  4444. cy_writeb(cinfo->ctl_addr.p9050 + 0x4c, 0);
  4445. else
  4446. #ifndef CONFIG_CYZ_INTR
  4447. if (!cy_is_Z(cinfo))
  4448. #endif
  4449. cy_writew(&cinfo->ctl_addr.p9060->intr_ctrl_stat,
  4450. readw(&cinfo->ctl_addr.p9060->intr_ctrl_stat) &
  4451. ~0x0900);
  4452. iounmap(cinfo->base_addr);
  4453. if (cinfo->ctl_addr.p9050)
  4454. iounmap(cinfo->ctl_addr.p9050);
  4455. if (cinfo->irq
  4456. #ifndef CONFIG_CYZ_INTR
  4457. && !cy_is_Z(cinfo)
  4458. #endif /* CONFIG_CYZ_INTR */
  4459. )
  4460. free_irq(cinfo->irq, cinfo);
  4461. pci_release_regions(pdev);
  4462. cinfo->base_addr = NULL;
  4463. for (i = cinfo->first_line; i < cinfo->first_line +
  4464. cinfo->nports; i++)
  4465. tty_unregister_device(cy_serial_driver, i);
  4466. cinfo->nports = 0;
  4467. kfree(cinfo->ports);
  4468. }
  4469. static struct pci_driver cy_pci_driver = {
  4470. .name = "cyclades",
  4471. .id_table = cy_pci_dev_id,
  4472. .probe = cy_pci_probe,
  4473. .remove = __devexit_p(cy_pci_remove)
  4474. };
  4475. #endif
  4476. static int cyclades_proc_show(struct seq_file *m, void *v)
  4477. {
  4478. struct cyclades_port *info;
  4479. unsigned int i, j;
  4480. __u32 cur_jifs = jiffies;
  4481. seq_puts(m, "Dev TimeOpen BytesOut IdleOut BytesIn "
  4482. "IdleIn Overruns Ldisc\n");
  4483. /* Output one line for each known port */
  4484. for (i = 0; i < NR_CARDS; i++)
  4485. for (j = 0; j < cy_card[i].nports; j++) {
  4486. info = &cy_card[i].ports[j];
  4487. if (info->port.count) {
  4488. /* XXX is the ldisc num worth this? */
  4489. struct tty_struct *tty;
  4490. struct tty_ldisc *ld;
  4491. int num = 0;
  4492. tty = tty_port_tty_get(&info->port);
  4493. if (tty) {
  4494. ld = tty_ldisc_ref(tty);
  4495. if (ld) {
  4496. num = ld->ops->num;
  4497. tty_ldisc_deref(ld);
  4498. }
  4499. tty_kref_put(tty);
  4500. }
  4501. seq_printf(m, "%3d %8lu %10lu %8lu "
  4502. "%10lu %8lu %9lu %6d\n", info->line,
  4503. (cur_jifs - info->idle_stats.in_use) /
  4504. HZ, info->idle_stats.xmit_bytes,
  4505. (cur_jifs - info->idle_stats.xmit_idle)/
  4506. HZ, info->idle_stats.recv_bytes,
  4507. (cur_jifs - info->idle_stats.recv_idle)/
  4508. HZ, info->idle_stats.overruns,
  4509. num);
  4510. } else
  4511. seq_printf(m, "%3d %8lu %10lu %8lu "
  4512. "%10lu %8lu %9lu %6ld\n",
  4513. info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
  4514. }
  4515. return 0;
  4516. }
  4517. static int cyclades_proc_open(struct inode *inode, struct file *file)
  4518. {
  4519. return single_open(file, cyclades_proc_show, NULL);
  4520. }
  4521. static const struct file_operations cyclades_proc_fops = {
  4522. .owner = THIS_MODULE,
  4523. .open = cyclades_proc_open,
  4524. .read = seq_read,
  4525. .llseek = seq_lseek,
  4526. .release = single_release,
  4527. };
  4528. /* The serial driver boot-time initialization code!
  4529. Hardware I/O ports are mapped to character special devices on a
  4530. first found, first allocated manner. That is, this code searches
  4531. for Cyclom cards in the system. As each is found, it is probed
  4532. to discover how many chips (and thus how many ports) are present.
  4533. These ports are mapped to the tty ports 32 and upward in monotonic
  4534. fashion. If an 8-port card is replaced with a 16-port card, the
  4535. port mapping on a following card will shift.
  4536. This approach is different from what is used in the other serial
  4537. device driver because the Cyclom is more properly a multiplexer,
  4538. not just an aggregation of serial ports on one card.
  4539. If there are more cards with more ports than have been
  4540. statically allocated above, a warning is printed and the
  4541. extra ports are ignored.
  4542. */
  4543. static const struct tty_operations cy_ops = {
  4544. .open = cy_open,
  4545. .close = cy_close,
  4546. .write = cy_write,
  4547. .put_char = cy_put_char,
  4548. .flush_chars = cy_flush_chars,
  4549. .write_room = cy_write_room,
  4550. .chars_in_buffer = cy_chars_in_buffer,
  4551. .flush_buffer = cy_flush_buffer,
  4552. .ioctl = cy_ioctl,
  4553. .throttle = cy_throttle,
  4554. .unthrottle = cy_unthrottle,
  4555. .set_termios = cy_set_termios,
  4556. .stop = cy_stop,
  4557. .start = cy_start,
  4558. .hangup = cy_hangup,
  4559. .break_ctl = cy_break,
  4560. .wait_until_sent = cy_wait_until_sent,
  4561. .tiocmget = cy_tiocmget,
  4562. .tiocmset = cy_tiocmset,
  4563. .proc_fops = &cyclades_proc_fops,
  4564. };
  4565. static int __init cy_init(void)
  4566. {
  4567. unsigned int nboards;
  4568. int retval = -ENOMEM;
  4569. cy_serial_driver = alloc_tty_driver(NR_PORTS);
  4570. if (!cy_serial_driver)
  4571. goto err;
  4572. printk(KERN_INFO "Cyclades driver " CY_VERSION " (built %s %s)\n",
  4573. __DATE__, __TIME__);
  4574. /* Initialize the tty_driver structure */
  4575. cy_serial_driver->owner = THIS_MODULE;
  4576. cy_serial_driver->driver_name = "cyclades";
  4577. cy_serial_driver->name = "ttyC";
  4578. cy_serial_driver->major = CYCLADES_MAJOR;
  4579. cy_serial_driver->minor_start = 0;
  4580. cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  4581. cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
  4582. cy_serial_driver->init_termios = tty_std_termios;
  4583. cy_serial_driver->init_termios.c_cflag =
  4584. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  4585. cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  4586. tty_set_operations(cy_serial_driver, &cy_ops);
  4587. retval = tty_register_driver(cy_serial_driver);
  4588. if (retval) {
  4589. printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
  4590. goto err_frtty;
  4591. }
  4592. /* the code below is responsible to find the boards. Each different
  4593. type of board has its own detection routine. If a board is found,
  4594. the next cy_card structure available is set by the detection
  4595. routine. These functions are responsible for checking the
  4596. availability of cy_card and cy_port data structures and updating
  4597. the cy_next_channel. */
  4598. /* look for isa boards */
  4599. nboards = cy_detect_isa();
  4600. #ifdef CONFIG_PCI
  4601. /* look for pci boards */
  4602. retval = pci_register_driver(&cy_pci_driver);
  4603. if (retval && !nboards) {
  4604. tty_unregister_driver(cy_serial_driver);
  4605. goto err_frtty;
  4606. }
  4607. #endif
  4608. return 0;
  4609. err_frtty:
  4610. put_tty_driver(cy_serial_driver);
  4611. err:
  4612. return retval;
  4613. } /* cy_init */
  4614. static void __exit cy_cleanup_module(void)
  4615. {
  4616. struct cyclades_card *card;
  4617. unsigned int i, e1;
  4618. #ifndef CONFIG_CYZ_INTR
  4619. del_timer_sync(&cyz_timerlist);
  4620. #endif /* CONFIG_CYZ_INTR */
  4621. e1 = tty_unregister_driver(cy_serial_driver);
  4622. if (e1)
  4623. printk(KERN_ERR "failed to unregister Cyclades serial "
  4624. "driver(%d)\n", e1);
  4625. #ifdef CONFIG_PCI
  4626. pci_unregister_driver(&cy_pci_driver);
  4627. #endif
  4628. for (i = 0; i < NR_CARDS; i++) {
  4629. card = &cy_card[i];
  4630. if (card->base_addr) {
  4631. /* clear interrupt */
  4632. cy_writeb(card->base_addr + Cy_ClrIntr, 0);
  4633. iounmap(card->base_addr);
  4634. if (card->ctl_addr.p9050)
  4635. iounmap(card->ctl_addr.p9050);
  4636. if (card->irq
  4637. #ifndef CONFIG_CYZ_INTR
  4638. && !cy_is_Z(card)
  4639. #endif /* CONFIG_CYZ_INTR */
  4640. )
  4641. free_irq(card->irq, card);
  4642. for (e1 = card->first_line; e1 < card->first_line +
  4643. card->nports; e1++)
  4644. tty_unregister_device(cy_serial_driver, e1);
  4645. kfree(card->ports);
  4646. }
  4647. }
  4648. put_tty_driver(cy_serial_driver);
  4649. } /* cy_cleanup_module */
  4650. module_init(cy_init);
  4651. module_exit(cy_cleanup_module);
  4652. MODULE_LICENSE("GPL");
  4653. MODULE_VERSION(CY_VERSION);
  4654. MODULE_ALIAS_CHARDEV_MAJOR(CYCLADES_MAJOR);