cyclades.c 109 KB

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