cyclades.c 109 KB

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