cyclades.c 117 KB

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