cyclades.c 110 KB

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