cyclades.c 110 KB

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