cyclades.c 112 KB

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