cyclades.c 119 KB

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