cyclades.c 123 KB

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