cyclades.c 110 KB

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