cyclades.c 112 KB

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