ioc4_serial.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2003-2006 Silicon Graphics, Inc. All Rights Reserved.
  4. */
  5. /*
  6. * This file contains a module version of the ioc4 serial driver. This
  7. * includes all the support functions needed (support functions, etc.)
  8. * and the serial driver itself.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_flip.h>
  13. #include <linux/serial.h>
  14. #include <linux/circ_buf.h>
  15. #include <linux/serial_reg.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <linux/ioc4.h>
  19. #include <linux/serial_core.h>
  20. #include <linux/slab.h>
  21. /*
  22. * interesting things about the ioc4
  23. */
  24. #define IOC4_NUM_SERIAL_PORTS 4 /* max ports per card */
  25. #define IOC4_NUM_CARDS 8 /* max cards per partition */
  26. #define GET_SIO_IR(_n) (_n == 0) ? (IOC4_SIO_IR_S0) : \
  27. (_n == 1) ? (IOC4_SIO_IR_S1) : \
  28. (_n == 2) ? (IOC4_SIO_IR_S2) : \
  29. (IOC4_SIO_IR_S3)
  30. #define GET_OTHER_IR(_n) (_n == 0) ? (IOC4_OTHER_IR_S0_MEMERR) : \
  31. (_n == 1) ? (IOC4_OTHER_IR_S1_MEMERR) : \
  32. (_n == 2) ? (IOC4_OTHER_IR_S2_MEMERR) : \
  33. (IOC4_OTHER_IR_S3_MEMERR)
  34. /*
  35. * All IOC4 registers are 32 bits wide.
  36. */
  37. /*
  38. * PCI Memory Space Map
  39. */
  40. #define IOC4_PCI_ERR_ADDR_L 0x000 /* Low Error Address */
  41. #define IOC4_PCI_ERR_ADDR_VLD (0x1 << 0)
  42. #define IOC4_PCI_ERR_ADDR_MST_ID_MSK (0xf << 1)
  43. #define IOC4_PCI_ERR_ADDR_MST_NUM_MSK (0xe << 1)
  44. #define IOC4_PCI_ERR_ADDR_MST_TYP_MSK (0x1 << 1)
  45. #define IOC4_PCI_ERR_ADDR_MUL_ERR (0x1 << 5)
  46. #define IOC4_PCI_ERR_ADDR_ADDR_MSK (0x3ffffff << 6)
  47. /* Interrupt types */
  48. #define IOC4_SIO_INTR_TYPE 0
  49. #define IOC4_OTHER_INTR_TYPE 1
  50. #define IOC4_NUM_INTR_TYPES 2
  51. /* Bitmasks for IOC4_SIO_IR, IOC4_SIO_IEC, and IOC4_SIO_IES */
  52. #define IOC4_SIO_IR_S0_TX_MT 0x00000001 /* Serial port 0 TX empty */
  53. #define IOC4_SIO_IR_S0_RX_FULL 0x00000002 /* Port 0 RX buf full */
  54. #define IOC4_SIO_IR_S0_RX_HIGH 0x00000004 /* Port 0 RX hiwat */
  55. #define IOC4_SIO_IR_S0_RX_TIMER 0x00000008 /* Port 0 RX timeout */
  56. #define IOC4_SIO_IR_S0_DELTA_DCD 0x00000010 /* Port 0 delta DCD */
  57. #define IOC4_SIO_IR_S0_DELTA_CTS 0x00000020 /* Port 0 delta CTS */
  58. #define IOC4_SIO_IR_S0_INT 0x00000040 /* Port 0 pass-thru intr */
  59. #define IOC4_SIO_IR_S0_TX_EXPLICIT 0x00000080 /* Port 0 explicit TX thru */
  60. #define IOC4_SIO_IR_S1_TX_MT 0x00000100 /* Serial port 1 */
  61. #define IOC4_SIO_IR_S1_RX_FULL 0x00000200 /* */
  62. #define IOC4_SIO_IR_S1_RX_HIGH 0x00000400 /* */
  63. #define IOC4_SIO_IR_S1_RX_TIMER 0x00000800 /* */
  64. #define IOC4_SIO_IR_S1_DELTA_DCD 0x00001000 /* */
  65. #define IOC4_SIO_IR_S1_DELTA_CTS 0x00002000 /* */
  66. #define IOC4_SIO_IR_S1_INT 0x00004000 /* */
  67. #define IOC4_SIO_IR_S1_TX_EXPLICIT 0x00008000 /* */
  68. #define IOC4_SIO_IR_S2_TX_MT 0x00010000 /* Serial port 2 */
  69. #define IOC4_SIO_IR_S2_RX_FULL 0x00020000 /* */
  70. #define IOC4_SIO_IR_S2_RX_HIGH 0x00040000 /* */
  71. #define IOC4_SIO_IR_S2_RX_TIMER 0x00080000 /* */
  72. #define IOC4_SIO_IR_S2_DELTA_DCD 0x00100000 /* */
  73. #define IOC4_SIO_IR_S2_DELTA_CTS 0x00200000 /* */
  74. #define IOC4_SIO_IR_S2_INT 0x00400000 /* */
  75. #define IOC4_SIO_IR_S2_TX_EXPLICIT 0x00800000 /* */
  76. #define IOC4_SIO_IR_S3_TX_MT 0x01000000 /* Serial port 3 */
  77. #define IOC4_SIO_IR_S3_RX_FULL 0x02000000 /* */
  78. #define IOC4_SIO_IR_S3_RX_HIGH 0x04000000 /* */
  79. #define IOC4_SIO_IR_S3_RX_TIMER 0x08000000 /* */
  80. #define IOC4_SIO_IR_S3_DELTA_DCD 0x10000000 /* */
  81. #define IOC4_SIO_IR_S3_DELTA_CTS 0x20000000 /* */
  82. #define IOC4_SIO_IR_S3_INT 0x40000000 /* */
  83. #define IOC4_SIO_IR_S3_TX_EXPLICIT 0x80000000 /* */
  84. /* Per device interrupt masks */
  85. #define IOC4_SIO_IR_S0 (IOC4_SIO_IR_S0_TX_MT | \
  86. IOC4_SIO_IR_S0_RX_FULL | \
  87. IOC4_SIO_IR_S0_RX_HIGH | \
  88. IOC4_SIO_IR_S0_RX_TIMER | \
  89. IOC4_SIO_IR_S0_DELTA_DCD | \
  90. IOC4_SIO_IR_S0_DELTA_CTS | \
  91. IOC4_SIO_IR_S0_INT | \
  92. IOC4_SIO_IR_S0_TX_EXPLICIT)
  93. #define IOC4_SIO_IR_S1 (IOC4_SIO_IR_S1_TX_MT | \
  94. IOC4_SIO_IR_S1_RX_FULL | \
  95. IOC4_SIO_IR_S1_RX_HIGH | \
  96. IOC4_SIO_IR_S1_RX_TIMER | \
  97. IOC4_SIO_IR_S1_DELTA_DCD | \
  98. IOC4_SIO_IR_S1_DELTA_CTS | \
  99. IOC4_SIO_IR_S1_INT | \
  100. IOC4_SIO_IR_S1_TX_EXPLICIT)
  101. #define IOC4_SIO_IR_S2 (IOC4_SIO_IR_S2_TX_MT | \
  102. IOC4_SIO_IR_S2_RX_FULL | \
  103. IOC4_SIO_IR_S2_RX_HIGH | \
  104. IOC4_SIO_IR_S2_RX_TIMER | \
  105. IOC4_SIO_IR_S2_DELTA_DCD | \
  106. IOC4_SIO_IR_S2_DELTA_CTS | \
  107. IOC4_SIO_IR_S2_INT | \
  108. IOC4_SIO_IR_S2_TX_EXPLICIT)
  109. #define IOC4_SIO_IR_S3 (IOC4_SIO_IR_S3_TX_MT | \
  110. IOC4_SIO_IR_S3_RX_FULL | \
  111. IOC4_SIO_IR_S3_RX_HIGH | \
  112. IOC4_SIO_IR_S3_RX_TIMER | \
  113. IOC4_SIO_IR_S3_DELTA_DCD | \
  114. IOC4_SIO_IR_S3_DELTA_CTS | \
  115. IOC4_SIO_IR_S3_INT | \
  116. IOC4_SIO_IR_S3_TX_EXPLICIT)
  117. /* Bitmasks for IOC4_OTHER_IR, IOC4_OTHER_IEC, and IOC4_OTHER_IES */
  118. #define IOC4_OTHER_IR_ATA_INT 0x00000001 /* ATAPI intr pass-thru */
  119. #define IOC4_OTHER_IR_ATA_MEMERR 0x00000002 /* ATAPI DMA PCI error */
  120. #define IOC4_OTHER_IR_S0_MEMERR 0x00000004 /* Port 0 PCI error */
  121. #define IOC4_OTHER_IR_S1_MEMERR 0x00000008 /* Port 1 PCI error */
  122. #define IOC4_OTHER_IR_S2_MEMERR 0x00000010 /* Port 2 PCI error */
  123. #define IOC4_OTHER_IR_S3_MEMERR 0x00000020 /* Port 3 PCI error */
  124. #define IOC4_OTHER_IR_KBD_INT 0x00000040 /* Keyboard/mouse */
  125. #define IOC4_OTHER_IR_RESERVED 0x007fff80 /* Reserved */
  126. #define IOC4_OTHER_IR_RT_INT 0x00800000 /* INT_OUT section output */
  127. #define IOC4_OTHER_IR_GEN_INT 0xff000000 /* Generic pins */
  128. #define IOC4_OTHER_IR_SER_MEMERR (IOC4_OTHER_IR_S0_MEMERR | IOC4_OTHER_IR_S1_MEMERR | \
  129. IOC4_OTHER_IR_S2_MEMERR | IOC4_OTHER_IR_S3_MEMERR)
  130. /* Bitmasks for IOC4_SIO_CR */
  131. #define IOC4_SIO_CR_CMD_PULSE_SHIFT 0 /* byte bus strobe shift */
  132. #define IOC4_SIO_CR_ARB_DIAG_TX0 0x00000000
  133. #define IOC4_SIO_CR_ARB_DIAG_RX0 0x00000010
  134. #define IOC4_SIO_CR_ARB_DIAG_TX1 0x00000020
  135. #define IOC4_SIO_CR_ARB_DIAG_RX1 0x00000030
  136. #define IOC4_SIO_CR_ARB_DIAG_TX2 0x00000040
  137. #define IOC4_SIO_CR_ARB_DIAG_RX2 0x00000050
  138. #define IOC4_SIO_CR_ARB_DIAG_TX3 0x00000060
  139. #define IOC4_SIO_CR_ARB_DIAG_RX3 0x00000070
  140. #define IOC4_SIO_CR_SIO_DIAG_IDLE 0x00000080 /* 0 -> active request among
  141. serial ports (ro) */
  142. /* Defs for some of the generic I/O pins */
  143. #define IOC4_GPCR_UART0_MODESEL 0x10 /* Pin is output to port 0
  144. mode sel */
  145. #define IOC4_GPCR_UART1_MODESEL 0x20 /* Pin is output to port 1
  146. mode sel */
  147. #define IOC4_GPCR_UART2_MODESEL 0x40 /* Pin is output to port 2
  148. mode sel */
  149. #define IOC4_GPCR_UART3_MODESEL 0x80 /* Pin is output to port 3
  150. mode sel */
  151. #define IOC4_GPPR_UART0_MODESEL_PIN 4 /* GIO pin controlling
  152. uart 0 mode select */
  153. #define IOC4_GPPR_UART1_MODESEL_PIN 5 /* GIO pin controlling
  154. uart 1 mode select */
  155. #define IOC4_GPPR_UART2_MODESEL_PIN 6 /* GIO pin controlling
  156. uart 2 mode select */
  157. #define IOC4_GPPR_UART3_MODESEL_PIN 7 /* GIO pin controlling
  158. uart 3 mode select */
  159. /* Bitmasks for serial RX status byte */
  160. #define IOC4_RXSB_OVERRUN 0x01 /* Char(s) lost */
  161. #define IOC4_RXSB_PAR_ERR 0x02 /* Parity error */
  162. #define IOC4_RXSB_FRAME_ERR 0x04 /* Framing error */
  163. #define IOC4_RXSB_BREAK 0x08 /* Break character */
  164. #define IOC4_RXSB_CTS 0x10 /* State of CTS */
  165. #define IOC4_RXSB_DCD 0x20 /* State of DCD */
  166. #define IOC4_RXSB_MODEM_VALID 0x40 /* DCD, CTS, and OVERRUN are valid */
  167. #define IOC4_RXSB_DATA_VALID 0x80 /* Data byte, FRAME_ERR PAR_ERR
  168. * & BREAK valid */
  169. /* Bitmasks for serial TX control byte */
  170. #define IOC4_TXCB_INT_WHEN_DONE 0x20 /* Interrupt after this byte is sent */
  171. #define IOC4_TXCB_INVALID 0x00 /* Byte is invalid */
  172. #define IOC4_TXCB_VALID 0x40 /* Byte is valid */
  173. #define IOC4_TXCB_MCR 0x80 /* Data<7:0> to modem control reg */
  174. #define IOC4_TXCB_DELAY 0xc0 /* Delay data<7:0> mSec */
  175. /* Bitmasks for IOC4_SBBR_L */
  176. #define IOC4_SBBR_L_SIZE 0x00000001 /* 0 == 1KB rings, 1 == 4KB rings */
  177. /* Bitmasks for IOC4_SSCR_<3:0> */
  178. #define IOC4_SSCR_RX_THRESHOLD 0x000001ff /* Hiwater mark */
  179. #define IOC4_SSCR_TX_TIMER_BUSY 0x00010000 /* TX timer in progress */
  180. #define IOC4_SSCR_HFC_EN 0x00020000 /* Hardware flow control enabled */
  181. #define IOC4_SSCR_RX_RING_DCD 0x00040000 /* Post RX record on delta-DCD */
  182. #define IOC4_SSCR_RX_RING_CTS 0x00080000 /* Post RX record on delta-CTS */
  183. #define IOC4_SSCR_DIAG 0x00200000 /* Bypass clock divider for sim */
  184. #define IOC4_SSCR_RX_DRAIN 0x08000000 /* Drain RX buffer to memory */
  185. #define IOC4_SSCR_DMA_EN 0x10000000 /* Enable ring buffer DMA */
  186. #define IOC4_SSCR_DMA_PAUSE 0x20000000 /* Pause DMA */
  187. #define IOC4_SSCR_PAUSE_STATE 0x40000000 /* Sets when PAUSE takes effect */
  188. #define IOC4_SSCR_RESET 0x80000000 /* Reset DMA channels */
  189. /* All producer/consumer pointers are the same bitfield */
  190. #define IOC4_PROD_CONS_PTR_4K 0x00000ff8 /* For 4K buffers */
  191. #define IOC4_PROD_CONS_PTR_1K 0x000003f8 /* For 1K buffers */
  192. #define IOC4_PROD_CONS_PTR_OFF 3
  193. /* Bitmasks for IOC4_SRCIR_<3:0> */
  194. #define IOC4_SRCIR_ARM 0x80000000 /* Arm RX timer */
  195. /* Bitmasks for IOC4_SHADOW_<3:0> */
  196. #define IOC4_SHADOW_DR 0x00000001 /* Data ready */
  197. #define IOC4_SHADOW_OE 0x00000002 /* Overrun error */
  198. #define IOC4_SHADOW_PE 0x00000004 /* Parity error */
  199. #define IOC4_SHADOW_FE 0x00000008 /* Framing error */
  200. #define IOC4_SHADOW_BI 0x00000010 /* Break interrupt */
  201. #define IOC4_SHADOW_THRE 0x00000020 /* Xmit holding register empty */
  202. #define IOC4_SHADOW_TEMT 0x00000040 /* Xmit shift register empty */
  203. #define IOC4_SHADOW_RFCE 0x00000080 /* Char in RX fifo has an error */
  204. #define IOC4_SHADOW_DCTS 0x00010000 /* Delta clear to send */
  205. #define IOC4_SHADOW_DDCD 0x00080000 /* Delta data carrier detect */
  206. #define IOC4_SHADOW_CTS 0x00100000 /* Clear to send */
  207. #define IOC4_SHADOW_DCD 0x00800000 /* Data carrier detect */
  208. #define IOC4_SHADOW_DTR 0x01000000 /* Data terminal ready */
  209. #define IOC4_SHADOW_RTS 0x02000000 /* Request to send */
  210. #define IOC4_SHADOW_OUT1 0x04000000 /* 16550 OUT1 bit */
  211. #define IOC4_SHADOW_OUT2 0x08000000 /* 16550 OUT2 bit */
  212. #define IOC4_SHADOW_LOOP 0x10000000 /* Loopback enabled */
  213. /* Bitmasks for IOC4_SRTR_<3:0> */
  214. #define IOC4_SRTR_CNT 0x00000fff /* Reload value for RX timer */
  215. #define IOC4_SRTR_CNT_VAL 0x0fff0000 /* Current value of RX timer */
  216. #define IOC4_SRTR_CNT_VAL_SHIFT 16
  217. #define IOC4_SRTR_HZ 16000 /* SRTR clock frequency */
  218. /* Serial port register map used for DMA and PIO serial I/O */
  219. struct ioc4_serialregs {
  220. uint32_t sscr;
  221. uint32_t stpir;
  222. uint32_t stcir;
  223. uint32_t srpir;
  224. uint32_t srcir;
  225. uint32_t srtr;
  226. uint32_t shadow;
  227. };
  228. /* IOC4 UART register map */
  229. struct ioc4_uartregs {
  230. char i4u_lcr;
  231. union {
  232. char iir; /* read only */
  233. char fcr; /* write only */
  234. } u3;
  235. union {
  236. char ier; /* DLAB == 0 */
  237. char dlm; /* DLAB == 1 */
  238. } u2;
  239. union {
  240. char rbr; /* read only, DLAB == 0 */
  241. char thr; /* write only, DLAB == 0 */
  242. char dll; /* DLAB == 1 */
  243. } u1;
  244. char i4u_scr;
  245. char i4u_msr;
  246. char i4u_lsr;
  247. char i4u_mcr;
  248. };
  249. /* short names */
  250. #define i4u_dll u1.dll
  251. #define i4u_ier u2.ier
  252. #define i4u_dlm u2.dlm
  253. #define i4u_fcr u3.fcr
  254. /* Serial port registers used for DMA serial I/O */
  255. struct ioc4_serial {
  256. uint32_t sbbr01_l;
  257. uint32_t sbbr01_h;
  258. uint32_t sbbr23_l;
  259. uint32_t sbbr23_h;
  260. struct ioc4_serialregs port_0;
  261. struct ioc4_serialregs port_1;
  262. struct ioc4_serialregs port_2;
  263. struct ioc4_serialregs port_3;
  264. struct ioc4_uartregs uart_0;
  265. struct ioc4_uartregs uart_1;
  266. struct ioc4_uartregs uart_2;
  267. struct ioc4_uartregs uart_3;
  268. };
  269. /* UART clock speed */
  270. #define IOC4_SER_XIN_CLK_66 66666667
  271. #define IOC4_SER_XIN_CLK_33 33333333
  272. #define IOC4_W_IES 0
  273. #define IOC4_W_IEC 1
  274. typedef void ioc4_intr_func_f(void *, uint32_t);
  275. typedef ioc4_intr_func_f *ioc4_intr_func_t;
  276. static unsigned int Num_of_ioc4_cards;
  277. /* defining this will get you LOTS of great debug info */
  278. //#define DEBUG_INTERRUPTS
  279. #define DPRINT_CONFIG(_x...) ;
  280. //#define DPRINT_CONFIG(_x...) printk _x
  281. /* number of characters left in xmit buffer before we ask for more */
  282. #define WAKEUP_CHARS 256
  283. /* number of characters we want to transmit to the lower level at a time */
  284. #define IOC4_MAX_CHARS 256
  285. #define IOC4_FIFO_CHARS 255
  286. /* Device name we're using */
  287. #define DEVICE_NAME_RS232 "ttyIOC"
  288. #define DEVICE_NAME_RS422 "ttyAIOC"
  289. #define DEVICE_MAJOR 204
  290. #define DEVICE_MINOR_RS232 50
  291. #define DEVICE_MINOR_RS422 84
  292. /* register offsets */
  293. #define IOC4_SERIAL_OFFSET 0x300
  294. /* flags for next_char_state */
  295. #define NCS_BREAK 0x1
  296. #define NCS_PARITY 0x2
  297. #define NCS_FRAMING 0x4
  298. #define NCS_OVERRUN 0x8
  299. /* cause we need SOME parameters ... */
  300. #define MIN_BAUD_SUPPORTED 1200
  301. #define MAX_BAUD_SUPPORTED 115200
  302. /* protocol types supported */
  303. #define PROTO_RS232 3
  304. #define PROTO_RS422 7
  305. /* Notification types */
  306. #define N_DATA_READY 0x01
  307. #define N_OUTPUT_LOWAT 0x02
  308. #define N_BREAK 0x04
  309. #define N_PARITY_ERROR 0x08
  310. #define N_FRAMING_ERROR 0x10
  311. #define N_OVERRUN_ERROR 0x20
  312. #define N_DDCD 0x40
  313. #define N_DCTS 0x80
  314. #define N_ALL_INPUT (N_DATA_READY | N_BREAK | \
  315. N_PARITY_ERROR | N_FRAMING_ERROR | \
  316. N_OVERRUN_ERROR | N_DDCD | N_DCTS)
  317. #define N_ALL_OUTPUT N_OUTPUT_LOWAT
  318. #define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR | N_OVERRUN_ERROR)
  319. #define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK | \
  320. N_PARITY_ERROR | N_FRAMING_ERROR | \
  321. N_OVERRUN_ERROR | N_DDCD | N_DCTS)
  322. #define SER_DIVISOR(_x, clk) (((clk) + (_x) * 8) / ((_x) * 16))
  323. #define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
  324. /* Some masks */
  325. #define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
  326. | UART_LCR_WLEN7 | UART_LCR_WLEN8)
  327. #define LCR_MASK_STOP_BITS (UART_LCR_STOP)
  328. #define PENDING(_p) (readl(&(_p)->ip_mem->sio_ir.raw) & _p->ip_ienb)
  329. #define READ_SIO_IR(_p) readl(&(_p)->ip_mem->sio_ir.raw)
  330. /* Default to 4k buffers */
  331. #ifdef IOC4_1K_BUFFERS
  332. #define RING_BUF_SIZE 1024
  333. #define IOC4_BUF_SIZE_BIT 0
  334. #define PROD_CONS_MASK IOC4_PROD_CONS_PTR_1K
  335. #else
  336. #define RING_BUF_SIZE 4096
  337. #define IOC4_BUF_SIZE_BIT IOC4_SBBR_L_SIZE
  338. #define PROD_CONS_MASK IOC4_PROD_CONS_PTR_4K
  339. #endif
  340. #define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
  341. /*
  342. * This is the entry saved by the driver - one per card
  343. */
  344. #define UART_PORT_MIN 0
  345. #define UART_PORT_RS232 UART_PORT_MIN
  346. #define UART_PORT_RS422 1
  347. #define UART_PORT_COUNT 2 /* one for each mode */
  348. struct ioc4_control {
  349. int ic_irq;
  350. struct {
  351. /* uart ports are allocated here - 1 for rs232, 1 for rs422 */
  352. struct uart_port icp_uart_port[UART_PORT_COUNT];
  353. /* Handy reference material */
  354. struct ioc4_port *icp_port;
  355. } ic_port[IOC4_NUM_SERIAL_PORTS];
  356. struct ioc4_soft *ic_soft;
  357. };
  358. /*
  359. * per-IOC4 data structure
  360. */
  361. #define MAX_IOC4_INTR_ENTS (8 * sizeof(uint32_t))
  362. struct ioc4_soft {
  363. struct ioc4_misc_regs __iomem *is_ioc4_misc_addr;
  364. struct ioc4_serial __iomem *is_ioc4_serial_addr;
  365. /* Each interrupt type has an entry in the array */
  366. struct ioc4_intr_type {
  367. /*
  368. * Each in-use entry in this array contains at least
  369. * one nonzero bit in sd_bits; no two entries in this
  370. * array have overlapping sd_bits values.
  371. */
  372. struct ioc4_intr_info {
  373. uint32_t sd_bits;
  374. ioc4_intr_func_f *sd_intr;
  375. void *sd_info;
  376. } is_intr_info[MAX_IOC4_INTR_ENTS];
  377. /* Number of entries active in the above array */
  378. atomic_t is_num_intrs;
  379. } is_intr_type[IOC4_NUM_INTR_TYPES];
  380. /* is_ir_lock must be held while
  381. * modifying sio_ie values, so
  382. * we can be sure that sio_ie is
  383. * not changing when we read it
  384. * along with sio_ir.
  385. */
  386. spinlock_t is_ir_lock; /* SIO_IE[SC] mod lock */
  387. };
  388. /* Local port info for each IOC4 serial ports */
  389. struct ioc4_port {
  390. struct uart_port *ip_port; /* current active port ptr */
  391. /* Ptrs for all ports */
  392. struct uart_port *ip_all_ports[UART_PORT_COUNT];
  393. /* Back ptrs for this port */
  394. struct ioc4_control *ip_control;
  395. struct pci_dev *ip_pdev;
  396. struct ioc4_soft *ip_ioc4_soft;
  397. /* pci mem addresses */
  398. struct ioc4_misc_regs __iomem *ip_mem;
  399. struct ioc4_serial __iomem *ip_serial;
  400. struct ioc4_serialregs __iomem *ip_serial_regs;
  401. struct ioc4_uartregs __iomem *ip_uart_regs;
  402. /* Ring buffer page for this port */
  403. dma_addr_t ip_dma_ringbuf;
  404. /* vaddr of ring buffer */
  405. struct ring_buffer *ip_cpu_ringbuf;
  406. /* Rings for this port */
  407. struct ring *ip_inring;
  408. struct ring *ip_outring;
  409. /* Hook to port specific values */
  410. struct hooks *ip_hooks;
  411. spinlock_t ip_lock;
  412. /* Various rx/tx parameters */
  413. int ip_baud;
  414. int ip_tx_lowat;
  415. int ip_rx_timeout;
  416. /* Copy of notification bits */
  417. int ip_notify;
  418. /* Shadow copies of various registers so we don't need to PIO
  419. * read them constantly
  420. */
  421. uint32_t ip_ienb; /* Enabled interrupts */
  422. uint32_t ip_sscr;
  423. uint32_t ip_tx_prod;
  424. uint32_t ip_rx_cons;
  425. int ip_pci_bus_speed;
  426. unsigned char ip_flags;
  427. };
  428. /* tx low water mark. We need to notify the driver whenever tx is getting
  429. * close to empty so it can refill the tx buffer and keep things going.
  430. * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
  431. * have no trouble getting in more chars in time (I certainly hope so).
  432. */
  433. #define TX_LOWAT_LATENCY 1000
  434. #define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
  435. #define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
  436. /* Flags per port */
  437. #define INPUT_HIGH 0x01
  438. #define DCD_ON 0x02
  439. #define LOWAT_WRITTEN 0x04
  440. #define READ_ABORTED 0x08
  441. #define PORT_ACTIVE 0x10
  442. #define PORT_INACTIVE 0 /* This is the value when "off" */
  443. /* Since each port has different register offsets and bitmasks
  444. * for everything, we'll store those that we need in tables so we
  445. * don't have to be constantly checking the port we are dealing with.
  446. */
  447. struct hooks {
  448. uint32_t intr_delta_dcd;
  449. uint32_t intr_delta_cts;
  450. uint32_t intr_tx_mt;
  451. uint32_t intr_rx_timer;
  452. uint32_t intr_rx_high;
  453. uint32_t intr_tx_explicit;
  454. uint32_t intr_dma_error;
  455. uint32_t intr_clear;
  456. uint32_t intr_all;
  457. int rs422_select_pin;
  458. };
  459. static struct hooks hooks_array[IOC4_NUM_SERIAL_PORTS] = {
  460. /* Values for port 0 */
  461. {
  462. IOC4_SIO_IR_S0_DELTA_DCD, IOC4_SIO_IR_S0_DELTA_CTS,
  463. IOC4_SIO_IR_S0_TX_MT, IOC4_SIO_IR_S0_RX_TIMER,
  464. IOC4_SIO_IR_S0_RX_HIGH, IOC4_SIO_IR_S0_TX_EXPLICIT,
  465. IOC4_OTHER_IR_S0_MEMERR,
  466. (IOC4_SIO_IR_S0_TX_MT | IOC4_SIO_IR_S0_RX_FULL |
  467. IOC4_SIO_IR_S0_RX_HIGH | IOC4_SIO_IR_S0_RX_TIMER |
  468. IOC4_SIO_IR_S0_DELTA_DCD | IOC4_SIO_IR_S0_DELTA_CTS |
  469. IOC4_SIO_IR_S0_INT | IOC4_SIO_IR_S0_TX_EXPLICIT),
  470. IOC4_SIO_IR_S0, IOC4_GPPR_UART0_MODESEL_PIN,
  471. },
  472. /* Values for port 1 */
  473. {
  474. IOC4_SIO_IR_S1_DELTA_DCD, IOC4_SIO_IR_S1_DELTA_CTS,
  475. IOC4_SIO_IR_S1_TX_MT, IOC4_SIO_IR_S1_RX_TIMER,
  476. IOC4_SIO_IR_S1_RX_HIGH, IOC4_SIO_IR_S1_TX_EXPLICIT,
  477. IOC4_OTHER_IR_S1_MEMERR,
  478. (IOC4_SIO_IR_S1_TX_MT | IOC4_SIO_IR_S1_RX_FULL |
  479. IOC4_SIO_IR_S1_RX_HIGH | IOC4_SIO_IR_S1_RX_TIMER |
  480. IOC4_SIO_IR_S1_DELTA_DCD | IOC4_SIO_IR_S1_DELTA_CTS |
  481. IOC4_SIO_IR_S1_INT | IOC4_SIO_IR_S1_TX_EXPLICIT),
  482. IOC4_SIO_IR_S1, IOC4_GPPR_UART1_MODESEL_PIN,
  483. },
  484. /* Values for port 2 */
  485. {
  486. IOC4_SIO_IR_S2_DELTA_DCD, IOC4_SIO_IR_S2_DELTA_CTS,
  487. IOC4_SIO_IR_S2_TX_MT, IOC4_SIO_IR_S2_RX_TIMER,
  488. IOC4_SIO_IR_S2_RX_HIGH, IOC4_SIO_IR_S2_TX_EXPLICIT,
  489. IOC4_OTHER_IR_S2_MEMERR,
  490. (IOC4_SIO_IR_S2_TX_MT | IOC4_SIO_IR_S2_RX_FULL |
  491. IOC4_SIO_IR_S2_RX_HIGH | IOC4_SIO_IR_S2_RX_TIMER |
  492. IOC4_SIO_IR_S2_DELTA_DCD | IOC4_SIO_IR_S2_DELTA_CTS |
  493. IOC4_SIO_IR_S2_INT | IOC4_SIO_IR_S2_TX_EXPLICIT),
  494. IOC4_SIO_IR_S2, IOC4_GPPR_UART2_MODESEL_PIN,
  495. },
  496. /* Values for port 3 */
  497. {
  498. IOC4_SIO_IR_S3_DELTA_DCD, IOC4_SIO_IR_S3_DELTA_CTS,
  499. IOC4_SIO_IR_S3_TX_MT, IOC4_SIO_IR_S3_RX_TIMER,
  500. IOC4_SIO_IR_S3_RX_HIGH, IOC4_SIO_IR_S3_TX_EXPLICIT,
  501. IOC4_OTHER_IR_S3_MEMERR,
  502. (IOC4_SIO_IR_S3_TX_MT | IOC4_SIO_IR_S3_RX_FULL |
  503. IOC4_SIO_IR_S3_RX_HIGH | IOC4_SIO_IR_S3_RX_TIMER |
  504. IOC4_SIO_IR_S3_DELTA_DCD | IOC4_SIO_IR_S3_DELTA_CTS |
  505. IOC4_SIO_IR_S3_INT | IOC4_SIO_IR_S3_TX_EXPLICIT),
  506. IOC4_SIO_IR_S3, IOC4_GPPR_UART3_MODESEL_PIN,
  507. }
  508. };
  509. /* A ring buffer entry */
  510. struct ring_entry {
  511. union {
  512. struct {
  513. uint32_t alldata;
  514. uint32_t allsc;
  515. } all;
  516. struct {
  517. char data[4]; /* data bytes */
  518. char sc[4]; /* status/control */
  519. } s;
  520. } u;
  521. };
  522. /* Test the valid bits in any of the 4 sc chars using "allsc" member */
  523. #define RING_ANY_VALID \
  524. ((uint32_t)(IOC4_RXSB_MODEM_VALID | IOC4_RXSB_DATA_VALID) * 0x01010101)
  525. #define ring_sc u.s.sc
  526. #define ring_data u.s.data
  527. #define ring_allsc u.all.allsc
  528. /* Number of entries per ring buffer. */
  529. #define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
  530. /* An individual ring */
  531. struct ring {
  532. struct ring_entry entries[ENTRIES_PER_RING];
  533. };
  534. /* The whole enchilada */
  535. struct ring_buffer {
  536. struct ring TX_0_OR_2;
  537. struct ring RX_0_OR_2;
  538. struct ring TX_1_OR_3;
  539. struct ring RX_1_OR_3;
  540. };
  541. /* Get a ring from a port struct */
  542. #define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
  543. /* Infinite loop detection.
  544. */
  545. #define MAXITER 10000000
  546. /* Prototypes */
  547. static void receive_chars(struct uart_port *);
  548. static void handle_intr(void *arg, uint32_t sio_ir);
  549. /*
  550. * port_is_active - determines if this port is currently active
  551. * @port: ptr to soft struct for this port
  552. * @uart_port: uart port to test for
  553. */
  554. static inline int port_is_active(struct ioc4_port *port,
  555. struct uart_port *uart_port)
  556. {
  557. if (port) {
  558. if ((port->ip_flags & PORT_ACTIVE)
  559. && (port->ip_port == uart_port))
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. /**
  565. * write_ireg - write the interrupt regs
  566. * @ioc4_soft: ptr to soft struct for this port
  567. * @val: value to write
  568. * @which: which register
  569. * @type: which ireg set
  570. */
  571. static inline void
  572. write_ireg(struct ioc4_soft *ioc4_soft, uint32_t val, int which, int type)
  573. {
  574. struct ioc4_misc_regs __iomem *mem = ioc4_soft->is_ioc4_misc_addr;
  575. unsigned long flags;
  576. spin_lock_irqsave(&ioc4_soft->is_ir_lock, flags);
  577. switch (type) {
  578. case IOC4_SIO_INTR_TYPE:
  579. switch (which) {
  580. case IOC4_W_IES:
  581. writel(val, &mem->sio_ies.raw);
  582. break;
  583. case IOC4_W_IEC:
  584. writel(val, &mem->sio_iec.raw);
  585. break;
  586. }
  587. break;
  588. case IOC4_OTHER_INTR_TYPE:
  589. switch (which) {
  590. case IOC4_W_IES:
  591. writel(val, &mem->other_ies.raw);
  592. break;
  593. case IOC4_W_IEC:
  594. writel(val, &mem->other_iec.raw);
  595. break;
  596. }
  597. break;
  598. default:
  599. break;
  600. }
  601. spin_unlock_irqrestore(&ioc4_soft->is_ir_lock, flags);
  602. }
  603. /**
  604. * set_baud - Baud rate setting code
  605. * @port: port to set
  606. * @baud: baud rate to use
  607. */
  608. static int set_baud(struct ioc4_port *port, int baud)
  609. {
  610. int actual_baud;
  611. int diff;
  612. int lcr;
  613. unsigned short divisor;
  614. struct ioc4_uartregs __iomem *uart;
  615. divisor = SER_DIVISOR(baud, port->ip_pci_bus_speed);
  616. if (!divisor)
  617. return 1;
  618. actual_baud = DIVISOR_TO_BAUD(divisor, port->ip_pci_bus_speed);
  619. diff = actual_baud - baud;
  620. if (diff < 0)
  621. diff = -diff;
  622. /* If we're within 1%, we've found a match */
  623. if (diff * 100 > actual_baud)
  624. return 1;
  625. uart = port->ip_uart_regs;
  626. lcr = readb(&uart->i4u_lcr);
  627. writeb(lcr | UART_LCR_DLAB, &uart->i4u_lcr);
  628. writeb((unsigned char)divisor, &uart->i4u_dll);
  629. writeb((unsigned char)(divisor >> 8), &uart->i4u_dlm);
  630. writeb(lcr, &uart->i4u_lcr);
  631. return 0;
  632. }
  633. /**
  634. * get_ioc4_port - given a uart port, return the control structure
  635. * @port: uart port
  636. * @set: set this port as current
  637. */
  638. static struct ioc4_port *get_ioc4_port(struct uart_port *the_port, int set)
  639. {
  640. struct ioc4_driver_data *idd = dev_get_drvdata(the_port->dev);
  641. struct ioc4_control *control = idd->idd_serial_data;
  642. struct ioc4_port *port;
  643. int port_num, port_type;
  644. if (control) {
  645. for ( port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS;
  646. port_num++ ) {
  647. port = control->ic_port[port_num].icp_port;
  648. if (!port)
  649. continue;
  650. for (port_type = UART_PORT_MIN;
  651. port_type < UART_PORT_COUNT;
  652. port_type++) {
  653. if (the_port == port->ip_all_ports
  654. [port_type]) {
  655. /* set local copy */
  656. if (set) {
  657. port->ip_port = the_port;
  658. }
  659. return port;
  660. }
  661. }
  662. }
  663. }
  664. return NULL;
  665. }
  666. /* The IOC4 hardware provides no atomic way to determine if interrupts
  667. * are pending since two reads are required to do so. The handler must
  668. * read the SIO_IR and the SIO_IES, and take the logical and of the
  669. * two. When this value is zero, all interrupts have been serviced and
  670. * the handler may return.
  671. *
  672. * This has the unfortunate "hole" that, if some other CPU or
  673. * some other thread or some higher level interrupt manages to
  674. * modify SIO_IE between our reads of SIO_IR and SIO_IE, we may
  675. * think we have observed SIO_IR&SIO_IE==0 when in fact this
  676. * condition never really occurred.
  677. *
  678. * To solve this, we use a simple spinlock that must be held
  679. * whenever modifying SIO_IE; holding this lock while observing
  680. * both SIO_IR and SIO_IE guarantees that we do not falsely
  681. * conclude that no enabled interrupts are pending.
  682. */
  683. static inline uint32_t
  684. pending_intrs(struct ioc4_soft *soft, int type)
  685. {
  686. struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
  687. unsigned long flag;
  688. uint32_t intrs = 0;
  689. BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
  690. || (type == IOC4_OTHER_INTR_TYPE)));
  691. spin_lock_irqsave(&soft->is_ir_lock, flag);
  692. switch (type) {
  693. case IOC4_SIO_INTR_TYPE:
  694. intrs = readl(&mem->sio_ir.raw) & readl(&mem->sio_ies.raw);
  695. break;
  696. case IOC4_OTHER_INTR_TYPE:
  697. intrs = readl(&mem->other_ir.raw) & readl(&mem->other_ies.raw);
  698. /* Don't process any ATA interrupte */
  699. intrs &= ~(IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
  700. break;
  701. default:
  702. break;
  703. }
  704. spin_unlock_irqrestore(&soft->is_ir_lock, flag);
  705. return intrs;
  706. }
  707. /**
  708. * port_init - Initialize the sio and ioc4 hardware for a given port
  709. * called per port from attach...
  710. * @port: port to initialize
  711. */
  712. static inline int port_init(struct ioc4_port *port)
  713. {
  714. uint32_t sio_cr;
  715. struct hooks *hooks = port->ip_hooks;
  716. struct ioc4_uartregs __iomem *uart;
  717. /* Idle the IOC4 serial interface */
  718. writel(IOC4_SSCR_RESET, &port->ip_serial_regs->sscr);
  719. /* Wait until any pending bus activity for this port has ceased */
  720. do
  721. sio_cr = readl(&port->ip_mem->sio_cr.raw);
  722. while (!(sio_cr & IOC4_SIO_CR_SIO_DIAG_IDLE));
  723. /* Finish reset sequence */
  724. writel(0, &port->ip_serial_regs->sscr);
  725. /* Once RESET is done, reload cached tx_prod and rx_cons values
  726. * and set rings to empty by making prod == cons
  727. */
  728. port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
  729. writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
  730. port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
  731. writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
  732. /* Disable interrupts for this 16550 */
  733. uart = port->ip_uart_regs;
  734. writeb(0, &uart->i4u_lcr);
  735. writeb(0, &uart->i4u_ier);
  736. /* Set the default baud */
  737. set_baud(port, port->ip_baud);
  738. /* Set line control to 8 bits no parity */
  739. writeb(UART_LCR_WLEN8 | 0, &uart->i4u_lcr);
  740. /* UART_LCR_STOP == 1 stop */
  741. /* Enable the FIFOs */
  742. writeb(UART_FCR_ENABLE_FIFO, &uart->i4u_fcr);
  743. /* then reset 16550 FIFOs */
  744. writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
  745. &uart->i4u_fcr);
  746. /* Clear modem control register */
  747. writeb(0, &uart->i4u_mcr);
  748. /* Clear deltas in modem status register */
  749. readb(&uart->i4u_msr);
  750. /* Only do this once per port pair */
  751. if (port->ip_hooks == &hooks_array[0]
  752. || port->ip_hooks == &hooks_array[2]) {
  753. unsigned long ring_pci_addr;
  754. uint32_t __iomem *sbbr_l;
  755. uint32_t __iomem *sbbr_h;
  756. if (port->ip_hooks == &hooks_array[0]) {
  757. sbbr_l = &port->ip_serial->sbbr01_l;
  758. sbbr_h = &port->ip_serial->sbbr01_h;
  759. } else {
  760. sbbr_l = &port->ip_serial->sbbr23_l;
  761. sbbr_h = &port->ip_serial->sbbr23_h;
  762. }
  763. ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
  764. DPRINT_CONFIG(("%s: ring_pci_addr 0x%lx\n",
  765. __func__, ring_pci_addr));
  766. writel((unsigned int)((uint64_t)ring_pci_addr >> 32), sbbr_h);
  767. writel((unsigned int)ring_pci_addr | IOC4_BUF_SIZE_BIT, sbbr_l);
  768. }
  769. /* Set the receive timeout value to 10 msec */
  770. writel(IOC4_SRTR_HZ / 100, &port->ip_serial_regs->srtr);
  771. /* Set rx threshold, enable DMA */
  772. /* Set high water mark at 3/4 of full ring */
  773. port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
  774. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  775. /* Disable and clear all serial related interrupt bits */
  776. write_ireg(port->ip_ioc4_soft, hooks->intr_clear,
  777. IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
  778. port->ip_ienb &= ~hooks->intr_clear;
  779. writel(hooks->intr_clear, &port->ip_mem->sio_ir.raw);
  780. return 0;
  781. }
  782. /**
  783. * handle_dma_error_intr - service any pending DMA error interrupts for the
  784. * given port - 2nd level called via sd_intr
  785. * @arg: handler arg
  786. * @other_ir: ioc4regs
  787. */
  788. static void handle_dma_error_intr(void *arg, uint32_t other_ir)
  789. {
  790. struct ioc4_port *port = (struct ioc4_port *)arg;
  791. struct hooks *hooks = port->ip_hooks;
  792. unsigned long flags;
  793. spin_lock_irqsave(&port->ip_lock, flags);
  794. /* ACK the interrupt */
  795. writel(hooks->intr_dma_error, &port->ip_mem->other_ir.raw);
  796. if (readl(&port->ip_mem->pci_err_addr_l.raw) & IOC4_PCI_ERR_ADDR_VLD) {
  797. printk(KERN_ERR
  798. "PCI error address is 0x%llx, "
  799. "master is serial port %c %s\n",
  800. (((uint64_t)readl(&port->ip_mem->pci_err_addr_h)
  801. << 32)
  802. | readl(&port->ip_mem->pci_err_addr_l.raw))
  803. & IOC4_PCI_ERR_ADDR_ADDR_MSK, '1' +
  804. ((char)(readl(&port->ip_mem->pci_err_addr_l.raw) &
  805. IOC4_PCI_ERR_ADDR_MST_NUM_MSK) >> 1),
  806. (readl(&port->ip_mem->pci_err_addr_l.raw)
  807. & IOC4_PCI_ERR_ADDR_MST_TYP_MSK)
  808. ? "RX" : "TX");
  809. if (readl(&port->ip_mem->pci_err_addr_l.raw)
  810. & IOC4_PCI_ERR_ADDR_MUL_ERR) {
  811. printk(KERN_ERR
  812. "Multiple errors occurred\n");
  813. }
  814. }
  815. spin_unlock_irqrestore(&port->ip_lock, flags);
  816. /* Re-enable DMA error interrupts */
  817. write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error, IOC4_W_IES,
  818. IOC4_OTHER_INTR_TYPE);
  819. }
  820. /**
  821. * intr_connect - interrupt connect function
  822. * @soft: soft struct for this card
  823. * @type: interrupt type
  824. * @intrbits: bit pattern to set
  825. * @intr: handler function
  826. * @info: handler arg
  827. */
  828. static void
  829. intr_connect(struct ioc4_soft *soft, int type,
  830. uint32_t intrbits, ioc4_intr_func_f * intr, void *info)
  831. {
  832. int i;
  833. struct ioc4_intr_info *intr_ptr;
  834. BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
  835. || (type == IOC4_OTHER_INTR_TYPE)));
  836. i = atomic_inc_return(&soft-> is_intr_type[type].is_num_intrs) - 1;
  837. BUG_ON(!(i < MAX_IOC4_INTR_ENTS || (printk("i %d\n", i), 0)));
  838. /* Save off the lower level interrupt handler */
  839. intr_ptr = &soft->is_intr_type[type].is_intr_info[i];
  840. intr_ptr->sd_bits = intrbits;
  841. intr_ptr->sd_intr = intr;
  842. intr_ptr->sd_info = info;
  843. }
  844. /**
  845. * ioc4_intr - Top level IOC4 interrupt handler.
  846. * @irq: irq value
  847. * @arg: handler arg
  848. */
  849. static irqreturn_t ioc4_intr(int irq, void *arg)
  850. {
  851. struct ioc4_soft *soft;
  852. uint32_t this_ir, this_mir;
  853. int xx, num_intrs = 0;
  854. int intr_type;
  855. int handled = 0;
  856. struct ioc4_intr_info *intr_info;
  857. soft = arg;
  858. for (intr_type = 0; intr_type < IOC4_NUM_INTR_TYPES; intr_type++) {
  859. num_intrs = (int)atomic_read(
  860. &soft->is_intr_type[intr_type].is_num_intrs);
  861. this_mir = this_ir = pending_intrs(soft, intr_type);
  862. /* Farm out the interrupt to the various drivers depending on
  863. * which interrupt bits are set.
  864. */
  865. for (xx = 0; xx < num_intrs; xx++) {
  866. intr_info = &soft->is_intr_type[intr_type].is_intr_info[xx];
  867. this_mir = this_ir & intr_info->sd_bits;
  868. if (this_mir) {
  869. /* Disable owned interrupts, call handler */
  870. handled++;
  871. write_ireg(soft, intr_info->sd_bits, IOC4_W_IEC,
  872. intr_type);
  873. intr_info->sd_intr(intr_info->sd_info, this_mir);
  874. this_ir &= ~this_mir;
  875. }
  876. }
  877. }
  878. #ifdef DEBUG_INTERRUPTS
  879. {
  880. struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
  881. unsigned long flag;
  882. spin_lock_irqsave(&soft->is_ir_lock, flag);
  883. printk ("%s : %d : mem 0x%p sio_ir 0x%x sio_ies 0x%x "
  884. "other_ir 0x%x other_ies 0x%x mask 0x%x\n",
  885. __func__, __LINE__,
  886. (void *)mem, readl(&mem->sio_ir.raw),
  887. readl(&mem->sio_ies.raw),
  888. readl(&mem->other_ir.raw),
  889. readl(&mem->other_ies.raw),
  890. IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
  891. spin_unlock_irqrestore(&soft->is_ir_lock, flag);
  892. }
  893. #endif
  894. return handled ? IRQ_HANDLED : IRQ_NONE;
  895. }
  896. /**
  897. * ioc4_attach_local - Device initialization.
  898. * Called at *_attach() time for each
  899. * IOC4 with serial ports in the system.
  900. * @idd: Master module data for this IOC4
  901. */
  902. static inline int ioc4_attach_local(struct ioc4_driver_data *idd)
  903. {
  904. struct ioc4_port *port;
  905. struct ioc4_port *ports[IOC4_NUM_SERIAL_PORTS];
  906. int port_number;
  907. uint16_t ioc4_revid_min = 62;
  908. uint16_t ioc4_revid;
  909. struct pci_dev *pdev = idd->idd_pdev;
  910. struct ioc4_control* control = idd->idd_serial_data;
  911. struct ioc4_soft *soft = control->ic_soft;
  912. void __iomem *ioc4_misc = idd->idd_misc_regs;
  913. void __iomem *ioc4_serial = soft->is_ioc4_serial_addr;
  914. /* IOC4 firmware must be at least rev 62 */
  915. pci_read_config_word(pdev, PCI_COMMAND_SPECIAL, &ioc4_revid);
  916. printk(KERN_INFO "IOC4 firmware revision %d\n", ioc4_revid);
  917. if (ioc4_revid < ioc4_revid_min) {
  918. printk(KERN_WARNING
  919. "IOC4 serial not supported on firmware rev %d, "
  920. "please upgrade to rev %d or higher\n",
  921. ioc4_revid, ioc4_revid_min);
  922. return -EPERM;
  923. }
  924. BUG_ON(ioc4_misc == NULL);
  925. BUG_ON(ioc4_serial == NULL);
  926. /* Create port structures for each port */
  927. for (port_number = 0; port_number < IOC4_NUM_SERIAL_PORTS;
  928. port_number++) {
  929. port = kzalloc(sizeof(struct ioc4_port), GFP_KERNEL);
  930. if (!port) {
  931. printk(KERN_WARNING
  932. "IOC4 serial memory not available for port\n");
  933. goto free;
  934. }
  935. spin_lock_init(&port->ip_lock);
  936. /* we need to remember the previous ones, to point back to
  937. * them farther down - setting up the ring buffers.
  938. */
  939. ports[port_number] = port;
  940. /* Allocate buffers and jumpstart the hardware. */
  941. control->ic_port[port_number].icp_port = port;
  942. port->ip_ioc4_soft = soft;
  943. port->ip_pdev = pdev;
  944. port->ip_ienb = 0;
  945. /* Use baud rate calculations based on detected PCI
  946. * bus speed. Simply test whether the PCI clock is
  947. * running closer to 66MHz or 33MHz.
  948. */
  949. if (idd->count_period/IOC4_EXTINT_COUNT_DIVISOR < 20) {
  950. port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_66;
  951. } else {
  952. port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_33;
  953. }
  954. port->ip_baud = 9600;
  955. port->ip_control = control;
  956. port->ip_mem = ioc4_misc;
  957. port->ip_serial = ioc4_serial;
  958. /* point to the right hook */
  959. port->ip_hooks = &hooks_array[port_number];
  960. /* Get direct hooks to the serial regs and uart regs
  961. * for this port
  962. */
  963. switch (port_number) {
  964. case 0:
  965. port->ip_serial_regs = &(port->ip_serial->port_0);
  966. port->ip_uart_regs = &(port->ip_serial->uart_0);
  967. break;
  968. case 1:
  969. port->ip_serial_regs = &(port->ip_serial->port_1);
  970. port->ip_uart_regs = &(port->ip_serial->uart_1);
  971. break;
  972. case 2:
  973. port->ip_serial_regs = &(port->ip_serial->port_2);
  974. port->ip_uart_regs = &(port->ip_serial->uart_2);
  975. break;
  976. default:
  977. case 3:
  978. port->ip_serial_regs = &(port->ip_serial->port_3);
  979. port->ip_uart_regs = &(port->ip_serial->uart_3);
  980. break;
  981. }
  982. /* ring buffers are 1 to a pair of ports */
  983. if (port_number && (port_number & 1)) {
  984. /* odd use the evens buffer */
  985. port->ip_dma_ringbuf =
  986. ports[port_number - 1]->ip_dma_ringbuf;
  987. port->ip_cpu_ringbuf =
  988. ports[port_number - 1]->ip_cpu_ringbuf;
  989. port->ip_inring = RING(port, RX_1_OR_3);
  990. port->ip_outring = RING(port, TX_1_OR_3);
  991. } else {
  992. if (port->ip_dma_ringbuf == 0) {
  993. port->ip_cpu_ringbuf = pci_alloc_consistent
  994. (pdev, TOTAL_RING_BUF_SIZE,
  995. &port->ip_dma_ringbuf);
  996. }
  997. BUG_ON(!((((int64_t)port->ip_dma_ringbuf) &
  998. (TOTAL_RING_BUF_SIZE - 1)) == 0));
  999. DPRINT_CONFIG(("%s : ip_cpu_ringbuf 0x%p "
  1000. "ip_dma_ringbuf 0x%p\n",
  1001. __func__,
  1002. (void *)port->ip_cpu_ringbuf,
  1003. (void *)port->ip_dma_ringbuf));
  1004. port->ip_inring = RING(port, RX_0_OR_2);
  1005. port->ip_outring = RING(port, TX_0_OR_2);
  1006. }
  1007. DPRINT_CONFIG(("%s : port %d [addr 0x%p] control 0x%p",
  1008. __func__,
  1009. port_number, (void *)port, (void *)control));
  1010. DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
  1011. (void *)port->ip_serial_regs,
  1012. (void *)port->ip_uart_regs));
  1013. /* Initialize the hardware for IOC4 */
  1014. port_init(port);
  1015. DPRINT_CONFIG(("%s: port_number %d port 0x%p inring 0x%p "
  1016. "outring 0x%p\n",
  1017. __func__,
  1018. port_number, (void *)port,
  1019. (void *)port->ip_inring,
  1020. (void *)port->ip_outring));
  1021. /* Attach interrupt handlers */
  1022. intr_connect(soft, IOC4_SIO_INTR_TYPE,
  1023. GET_SIO_IR(port_number),
  1024. handle_intr, port);
  1025. intr_connect(soft, IOC4_OTHER_INTR_TYPE,
  1026. GET_OTHER_IR(port_number),
  1027. handle_dma_error_intr, port);
  1028. }
  1029. return 0;
  1030. free:
  1031. while (port_number)
  1032. kfree(ports[--port_number]);
  1033. return -ENOMEM;
  1034. }
  1035. /**
  1036. * enable_intrs - enable interrupts
  1037. * @port: port to enable
  1038. * @mask: mask to use
  1039. */
  1040. static void enable_intrs(struct ioc4_port *port, uint32_t mask)
  1041. {
  1042. struct hooks *hooks = port->ip_hooks;
  1043. if ((port->ip_ienb & mask) != mask) {
  1044. write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IES,
  1045. IOC4_SIO_INTR_TYPE);
  1046. port->ip_ienb |= mask;
  1047. }
  1048. if (port->ip_ienb)
  1049. write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
  1050. IOC4_W_IES, IOC4_OTHER_INTR_TYPE);
  1051. }
  1052. /**
  1053. * local_open - local open a port
  1054. * @port: port to open
  1055. */
  1056. static inline int local_open(struct ioc4_port *port)
  1057. {
  1058. int spiniter = 0;
  1059. port->ip_flags = PORT_ACTIVE;
  1060. /* Pause the DMA interface if necessary */
  1061. if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
  1062. writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
  1063. &port->ip_serial_regs->sscr);
  1064. while((readl(&port->ip_serial_regs-> sscr)
  1065. & IOC4_SSCR_PAUSE_STATE) == 0) {
  1066. spiniter++;
  1067. if (spiniter > MAXITER) {
  1068. port->ip_flags = PORT_INACTIVE;
  1069. return -1;
  1070. }
  1071. }
  1072. }
  1073. /* Reset the input fifo. If the uart received chars while the port
  1074. * was closed and DMA is not enabled, the uart may have a bunch of
  1075. * chars hanging around in its rx fifo which will not be discarded
  1076. * by rclr in the upper layer. We must get rid of them here.
  1077. */
  1078. writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
  1079. &port->ip_uart_regs->i4u_fcr);
  1080. writeb(UART_LCR_WLEN8, &port->ip_uart_regs->i4u_lcr);
  1081. /* UART_LCR_STOP == 1 stop */
  1082. /* Re-enable DMA, set default threshold to intr whenever there is
  1083. * data available.
  1084. */
  1085. port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
  1086. port->ip_sscr |= 1; /* default threshold */
  1087. /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
  1088. * flag if it was set above
  1089. */
  1090. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1091. port->ip_tx_lowat = 1;
  1092. return 0;
  1093. }
  1094. /**
  1095. * set_rx_timeout - Set rx timeout and threshold values.
  1096. * @port: port to use
  1097. * @timeout: timeout value in ticks
  1098. */
  1099. static inline int set_rx_timeout(struct ioc4_port *port, int timeout)
  1100. {
  1101. int threshold;
  1102. port->ip_rx_timeout = timeout;
  1103. /* Timeout is in ticks. Let's figure out how many chars we
  1104. * can receive at the current baud rate in that interval
  1105. * and set the rx threshold to that amount. There are 4 chars
  1106. * per ring entry, so we'll divide the number of chars that will
  1107. * arrive in timeout by 4.
  1108. * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
  1109. */
  1110. threshold = timeout * port->ip_baud / 4000;
  1111. if (threshold == 0)
  1112. threshold = 1; /* otherwise we'll intr all the time! */
  1113. if ((unsigned)threshold > (unsigned)IOC4_SSCR_RX_THRESHOLD)
  1114. return 1;
  1115. port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
  1116. port->ip_sscr |= threshold;
  1117. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1118. /* Now set the rx timeout to the given value
  1119. * again timeout * IOC4_SRTR_HZ / HZ
  1120. */
  1121. timeout = timeout * IOC4_SRTR_HZ / 100;
  1122. if (timeout > IOC4_SRTR_CNT)
  1123. timeout = IOC4_SRTR_CNT;
  1124. writel(timeout, &port->ip_serial_regs->srtr);
  1125. return 0;
  1126. }
  1127. /**
  1128. * config_port - config the hardware
  1129. * @port: port to config
  1130. * @baud: baud rate for the port
  1131. * @byte_size: data size
  1132. * @stop_bits: number of stop bits
  1133. * @parenb: parity enable ?
  1134. * @parodd: odd parity ?
  1135. */
  1136. static inline int
  1137. config_port(struct ioc4_port *port,
  1138. int baud, int byte_size, int stop_bits, int parenb, int parodd)
  1139. {
  1140. char lcr, sizebits;
  1141. int spiniter = 0;
  1142. DPRINT_CONFIG(("%s: baud %d byte_size %d stop %d parenb %d parodd %d\n",
  1143. __func__, baud, byte_size, stop_bits, parenb, parodd));
  1144. if (set_baud(port, baud))
  1145. return 1;
  1146. switch (byte_size) {
  1147. case 5:
  1148. sizebits = UART_LCR_WLEN5;
  1149. break;
  1150. case 6:
  1151. sizebits = UART_LCR_WLEN6;
  1152. break;
  1153. case 7:
  1154. sizebits = UART_LCR_WLEN7;
  1155. break;
  1156. case 8:
  1157. sizebits = UART_LCR_WLEN8;
  1158. break;
  1159. default:
  1160. return 1;
  1161. }
  1162. /* Pause the DMA interface if necessary */
  1163. if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
  1164. writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
  1165. &port->ip_serial_regs->sscr);
  1166. while((readl(&port->ip_serial_regs->sscr)
  1167. & IOC4_SSCR_PAUSE_STATE) == 0) {
  1168. spiniter++;
  1169. if (spiniter > MAXITER)
  1170. return -1;
  1171. }
  1172. }
  1173. /* Clear relevant fields in lcr */
  1174. lcr = readb(&port->ip_uart_regs->i4u_lcr);
  1175. lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
  1176. UART_LCR_PARITY | LCR_MASK_STOP_BITS);
  1177. /* Set byte size in lcr */
  1178. lcr |= sizebits;
  1179. /* Set parity */
  1180. if (parenb) {
  1181. lcr |= UART_LCR_PARITY;
  1182. if (!parodd)
  1183. lcr |= UART_LCR_EPAR;
  1184. }
  1185. /* Set stop bits */
  1186. if (stop_bits)
  1187. lcr |= UART_LCR_STOP /* 2 stop bits */ ;
  1188. writeb(lcr, &port->ip_uart_regs->i4u_lcr);
  1189. /* Re-enable the DMA interface if necessary */
  1190. if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
  1191. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1192. }
  1193. port->ip_baud = baud;
  1194. /* When we get within this number of ring entries of filling the
  1195. * entire ring on tx, place an EXPLICIT intr to generate a lowat
  1196. * notification when output has drained.
  1197. */
  1198. port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
  1199. if (port->ip_tx_lowat == 0)
  1200. port->ip_tx_lowat = 1;
  1201. set_rx_timeout(port, 2);
  1202. return 0;
  1203. }
  1204. /**
  1205. * do_write - Write bytes to the port. Returns the number of bytes
  1206. * actually written. Called from transmit_chars
  1207. * @port: port to use
  1208. * @buf: the stuff to write
  1209. * @len: how many bytes in 'buf'
  1210. */
  1211. static inline int do_write(struct ioc4_port *port, char *buf, int len)
  1212. {
  1213. int prod_ptr, cons_ptr, total = 0;
  1214. struct ring *outring;
  1215. struct ring_entry *entry;
  1216. struct hooks *hooks = port->ip_hooks;
  1217. BUG_ON(!(len >= 0));
  1218. prod_ptr = port->ip_tx_prod;
  1219. cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
  1220. outring = port->ip_outring;
  1221. /* Maintain a 1-entry red-zone. The ring buffer is full when
  1222. * (cons - prod) % ring_size is 1. Rather than do this subtraction
  1223. * in the body of the loop, I'll do it now.
  1224. */
  1225. cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
  1226. /* Stuff the bytes into the output */
  1227. while ((prod_ptr != cons_ptr) && (len > 0)) {
  1228. int xx;
  1229. /* Get 4 bytes (one ring entry) at a time */
  1230. entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
  1231. /* Invalidate all entries */
  1232. entry->ring_allsc = 0;
  1233. /* Copy in some bytes */
  1234. for (xx = 0; (xx < 4) && (len > 0); xx++) {
  1235. entry->ring_data[xx] = *buf++;
  1236. entry->ring_sc[xx] = IOC4_TXCB_VALID;
  1237. len--;
  1238. total++;
  1239. }
  1240. /* If we are within some small threshold of filling up the
  1241. * entire ring buffer, we must place an EXPLICIT intr here
  1242. * to generate a lowat interrupt in case we subsequently
  1243. * really do fill up the ring and the caller goes to sleep.
  1244. * No need to place more than one though.
  1245. */
  1246. if (!(port->ip_flags & LOWAT_WRITTEN) &&
  1247. ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
  1248. <= port->ip_tx_lowat
  1249. * (int)sizeof(struct ring_entry)) {
  1250. port->ip_flags |= LOWAT_WRITTEN;
  1251. entry->ring_sc[0] |= IOC4_TXCB_INT_WHEN_DONE;
  1252. }
  1253. /* Go on to next entry */
  1254. prod_ptr += sizeof(struct ring_entry);
  1255. prod_ptr &= PROD_CONS_MASK;
  1256. }
  1257. /* If we sent something, start DMA if necessary */
  1258. if (total > 0 && !(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
  1259. port->ip_sscr |= IOC4_SSCR_DMA_EN;
  1260. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1261. }
  1262. /* Store the new producer pointer. If tx is disabled, we stuff the
  1263. * data into the ring buffer, but we don't actually start tx.
  1264. */
  1265. if (!uart_tx_stopped(port->ip_port)) {
  1266. writel(prod_ptr, &port->ip_serial_regs->stpir);
  1267. /* If we are now transmitting, enable tx_mt interrupt so we
  1268. * can disable DMA if necessary when the tx finishes.
  1269. */
  1270. if (total > 0)
  1271. enable_intrs(port, hooks->intr_tx_mt);
  1272. }
  1273. port->ip_tx_prod = prod_ptr;
  1274. return total;
  1275. }
  1276. /**
  1277. * disable_intrs - disable interrupts
  1278. * @port: port to enable
  1279. * @mask: mask to use
  1280. */
  1281. static void disable_intrs(struct ioc4_port *port, uint32_t mask)
  1282. {
  1283. struct hooks *hooks = port->ip_hooks;
  1284. if (port->ip_ienb & mask) {
  1285. write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IEC,
  1286. IOC4_SIO_INTR_TYPE);
  1287. port->ip_ienb &= ~mask;
  1288. }
  1289. if (!port->ip_ienb)
  1290. write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
  1291. IOC4_W_IEC, IOC4_OTHER_INTR_TYPE);
  1292. }
  1293. /**
  1294. * set_notification - Modify event notification
  1295. * @port: port to use
  1296. * @mask: events mask
  1297. * @set_on: set ?
  1298. */
  1299. static int set_notification(struct ioc4_port *port, int mask, int set_on)
  1300. {
  1301. struct hooks *hooks = port->ip_hooks;
  1302. uint32_t intrbits, sscrbits;
  1303. BUG_ON(!mask);
  1304. intrbits = sscrbits = 0;
  1305. if (mask & N_DATA_READY)
  1306. intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
  1307. if (mask & N_OUTPUT_LOWAT)
  1308. intrbits |= hooks->intr_tx_explicit;
  1309. if (mask & N_DDCD) {
  1310. intrbits |= hooks->intr_delta_dcd;
  1311. sscrbits |= IOC4_SSCR_RX_RING_DCD;
  1312. }
  1313. if (mask & N_DCTS)
  1314. intrbits |= hooks->intr_delta_cts;
  1315. if (set_on) {
  1316. enable_intrs(port, intrbits);
  1317. port->ip_notify |= mask;
  1318. port->ip_sscr |= sscrbits;
  1319. } else {
  1320. disable_intrs(port, intrbits);
  1321. port->ip_notify &= ~mask;
  1322. port->ip_sscr &= ~sscrbits;
  1323. }
  1324. /* We require DMA if either DATA_READY or DDCD notification is
  1325. * currently requested. If neither of these is requested and
  1326. * there is currently no tx in progress, DMA may be disabled.
  1327. */
  1328. if (port->ip_notify & (N_DATA_READY | N_DDCD))
  1329. port->ip_sscr |= IOC4_SSCR_DMA_EN;
  1330. else if (!(port->ip_ienb & hooks->intr_tx_mt))
  1331. port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
  1332. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1333. return 0;
  1334. }
  1335. /**
  1336. * set_mcr - set the master control reg
  1337. * @the_port: port to use
  1338. * @mask1: mcr mask
  1339. * @mask2: shadow mask
  1340. */
  1341. static inline int set_mcr(struct uart_port *the_port,
  1342. int mask1, int mask2)
  1343. {
  1344. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  1345. uint32_t shadow;
  1346. int spiniter = 0;
  1347. char mcr;
  1348. if (!port)
  1349. return -1;
  1350. /* Pause the DMA interface if necessary */
  1351. if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
  1352. writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
  1353. &port->ip_serial_regs->sscr);
  1354. while ((readl(&port->ip_serial_regs->sscr)
  1355. & IOC4_SSCR_PAUSE_STATE) == 0) {
  1356. spiniter++;
  1357. if (spiniter > MAXITER)
  1358. return -1;
  1359. }
  1360. }
  1361. shadow = readl(&port->ip_serial_regs->shadow);
  1362. mcr = (shadow & 0xff000000) >> 24;
  1363. /* Set new value */
  1364. mcr |= mask1;
  1365. shadow |= mask2;
  1366. writeb(mcr, &port->ip_uart_regs->i4u_mcr);
  1367. writel(shadow, &port->ip_serial_regs->shadow);
  1368. /* Re-enable the DMA interface if necessary */
  1369. if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
  1370. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1371. }
  1372. return 0;
  1373. }
  1374. /**
  1375. * ioc4_set_proto - set the protocol for the port
  1376. * @port: port to use
  1377. * @proto: protocol to use
  1378. */
  1379. static int ioc4_set_proto(struct ioc4_port *port, int proto)
  1380. {
  1381. struct hooks *hooks = port->ip_hooks;
  1382. switch (proto) {
  1383. case PROTO_RS232:
  1384. /* Clear the appropriate GIO pin */
  1385. writel(0, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
  1386. break;
  1387. case PROTO_RS422:
  1388. /* Set the appropriate GIO pin */
  1389. writel(1, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
  1390. break;
  1391. default:
  1392. return 1;
  1393. }
  1394. return 0;
  1395. }
  1396. /**
  1397. * transmit_chars - upper level write, called with ip_lock
  1398. * @the_port: port to write
  1399. */
  1400. static void transmit_chars(struct uart_port *the_port)
  1401. {
  1402. int xmit_count, tail, head;
  1403. int result;
  1404. char *start;
  1405. struct tty_struct *tty;
  1406. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  1407. struct uart_state *state;
  1408. if (!the_port)
  1409. return;
  1410. if (!port)
  1411. return;
  1412. state = the_port->state;
  1413. tty = state->port.tty;
  1414. if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
  1415. /* Nothing to do or hw stopped */
  1416. set_notification(port, N_ALL_OUTPUT, 0);
  1417. return;
  1418. }
  1419. head = state->xmit.head;
  1420. tail = state->xmit.tail;
  1421. start = (char *)&state->xmit.buf[tail];
  1422. /* write out all the data or until the end of the buffer */
  1423. xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
  1424. if (xmit_count > 0) {
  1425. result = do_write(port, start, xmit_count);
  1426. if (result > 0) {
  1427. /* booking */
  1428. xmit_count -= result;
  1429. the_port->icount.tx += result;
  1430. /* advance the pointers */
  1431. tail += result;
  1432. tail &= UART_XMIT_SIZE - 1;
  1433. state->xmit.tail = tail;
  1434. start = (char *)&state->xmit.buf[tail];
  1435. }
  1436. }
  1437. if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
  1438. uart_write_wakeup(the_port);
  1439. if (uart_circ_empty(&state->xmit)) {
  1440. set_notification(port, N_OUTPUT_LOWAT, 0);
  1441. } else {
  1442. set_notification(port, N_OUTPUT_LOWAT, 1);
  1443. }
  1444. }
  1445. /**
  1446. * ioc4_change_speed - change the speed of the port
  1447. * @the_port: port to change
  1448. * @new_termios: new termios settings
  1449. * @old_termios: old termios settings
  1450. */
  1451. static void
  1452. ioc4_change_speed(struct uart_port *the_port,
  1453. struct ktermios *new_termios, struct ktermios *old_termios)
  1454. {
  1455. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  1456. int baud, bits;
  1457. unsigned cflag, iflag;
  1458. int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
  1459. struct uart_state *state = the_port->state;
  1460. cflag = new_termios->c_cflag;
  1461. iflag = new_termios->c_iflag;
  1462. switch (cflag & CSIZE) {
  1463. case CS5:
  1464. new_data = 5;
  1465. bits = 7;
  1466. break;
  1467. case CS6:
  1468. new_data = 6;
  1469. bits = 8;
  1470. break;
  1471. case CS7:
  1472. new_data = 7;
  1473. bits = 9;
  1474. break;
  1475. case CS8:
  1476. new_data = 8;
  1477. bits = 10;
  1478. break;
  1479. default:
  1480. /* cuz we always need a default ... */
  1481. new_data = 5;
  1482. bits = 7;
  1483. break;
  1484. }
  1485. if (cflag & CSTOPB) {
  1486. bits++;
  1487. new_stop = 1;
  1488. }
  1489. if (cflag & PARENB) {
  1490. bits++;
  1491. new_parity_enable = 1;
  1492. if (cflag & PARODD)
  1493. new_parity = 1;
  1494. }
  1495. baud = uart_get_baud_rate(the_port, new_termios, old_termios,
  1496. MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
  1497. DPRINT_CONFIG(("%s: returned baud %d\n", __func__, baud));
  1498. /* default is 9600 */
  1499. if (!baud)
  1500. baud = 9600;
  1501. if (!the_port->fifosize)
  1502. the_port->fifosize = IOC4_FIFO_CHARS;
  1503. the_port->timeout = ((the_port->fifosize * HZ * bits) / (baud / 10));
  1504. the_port->timeout += HZ / 50; /* Add .02 seconds of slop */
  1505. the_port->ignore_status_mask = N_ALL_INPUT;
  1506. state->port.low_latency = 1;
  1507. if (iflag & IGNPAR)
  1508. the_port->ignore_status_mask &= ~(N_PARITY_ERROR
  1509. | N_FRAMING_ERROR);
  1510. if (iflag & IGNBRK) {
  1511. the_port->ignore_status_mask &= ~N_BREAK;
  1512. if (iflag & IGNPAR)
  1513. the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
  1514. }
  1515. if (!(cflag & CREAD)) {
  1516. /* ignore everything */
  1517. the_port->ignore_status_mask &= ~N_DATA_READY;
  1518. }
  1519. if (cflag & CRTSCTS) {
  1520. port->ip_sscr |= IOC4_SSCR_HFC_EN;
  1521. }
  1522. else {
  1523. port->ip_sscr &= ~IOC4_SSCR_HFC_EN;
  1524. }
  1525. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1526. /* Set the configuration and proper notification call */
  1527. DPRINT_CONFIG(("%s : port 0x%p cflag 0%o "
  1528. "config_port(baud %d data %d stop %d p enable %d parity %d),"
  1529. " notification 0x%x\n",
  1530. __func__, (void *)port, cflag, baud, new_data, new_stop,
  1531. new_parity_enable, new_parity, the_port->ignore_status_mask));
  1532. if ((config_port(port, baud, /* baud */
  1533. new_data, /* byte size */
  1534. new_stop, /* stop bits */
  1535. new_parity_enable, /* set parity */
  1536. new_parity)) >= 0) { /* parity 1==odd */
  1537. set_notification(port, the_port->ignore_status_mask, 1);
  1538. }
  1539. }
  1540. /**
  1541. * ic4_startup_local - Start up the serial port - returns >= 0 if no errors
  1542. * @the_port: Port to operate on
  1543. */
  1544. static inline int ic4_startup_local(struct uart_port *the_port)
  1545. {
  1546. struct ioc4_port *port;
  1547. struct uart_state *state;
  1548. if (!the_port)
  1549. return -1;
  1550. port = get_ioc4_port(the_port, 0);
  1551. if (!port)
  1552. return -1;
  1553. state = the_port->state;
  1554. local_open(port);
  1555. /* set the protocol - mapbase has the port type */
  1556. ioc4_set_proto(port, the_port->mapbase);
  1557. /* set the speed of the serial port */
  1558. ioc4_change_speed(the_port, &state->port.tty->termios,
  1559. (struct ktermios *)0);
  1560. return 0;
  1561. }
  1562. /*
  1563. * ioc4_cb_output_lowat - called when the output low water mark is hit
  1564. * @the_port: port to output
  1565. */
  1566. static void ioc4_cb_output_lowat(struct uart_port *the_port)
  1567. {
  1568. unsigned long pflags;
  1569. /* ip_lock is set on the call here */
  1570. if (the_port) {
  1571. spin_lock_irqsave(&the_port->lock, pflags);
  1572. transmit_chars(the_port);
  1573. spin_unlock_irqrestore(&the_port->lock, pflags);
  1574. }
  1575. }
  1576. /**
  1577. * handle_intr - service any interrupts for the given port - 2nd level
  1578. * called via sd_intr
  1579. * @arg: handler arg
  1580. * @sio_ir: ioc4regs
  1581. */
  1582. static void handle_intr(void *arg, uint32_t sio_ir)
  1583. {
  1584. struct ioc4_port *port = (struct ioc4_port *)arg;
  1585. struct hooks *hooks = port->ip_hooks;
  1586. unsigned int rx_high_rd_aborted = 0;
  1587. unsigned long flags;
  1588. struct uart_port *the_port;
  1589. int loop_counter;
  1590. /* Possible race condition here: The tx_mt interrupt bit may be
  1591. * cleared without the intervention of the interrupt handler,
  1592. * e.g. by a write. If the top level interrupt handler reads a
  1593. * tx_mt, then some other processor does a write, starting up
  1594. * output, then we come in here, see the tx_mt and stop DMA, the
  1595. * output started by the other processor will hang. Thus we can
  1596. * only rely on tx_mt being legitimate if it is read while the
  1597. * port lock is held. Therefore this bit must be ignored in the
  1598. * passed in interrupt mask which was read by the top level
  1599. * interrupt handler since the port lock was not held at the time
  1600. * it was read. We can only rely on this bit being accurate if it
  1601. * is read while the port lock is held. So we'll clear it for now,
  1602. * and reload it later once we have the port lock.
  1603. */
  1604. sio_ir &= ~(hooks->intr_tx_mt);
  1605. spin_lock_irqsave(&port->ip_lock, flags);
  1606. loop_counter = MAXITER; /* to avoid hangs */
  1607. do {
  1608. uint32_t shadow;
  1609. if ( loop_counter-- <= 0 ) {
  1610. printk(KERN_WARNING "IOC4 serial: "
  1611. "possible hang condition/"
  1612. "port stuck on interrupt.\n");
  1613. break;
  1614. }
  1615. /* Handle a DCD change */
  1616. if (sio_ir & hooks->intr_delta_dcd) {
  1617. /* ACK the interrupt */
  1618. writel(hooks->intr_delta_dcd,
  1619. &port->ip_mem->sio_ir.raw);
  1620. shadow = readl(&port->ip_serial_regs->shadow);
  1621. if ((port->ip_notify & N_DDCD)
  1622. && (shadow & IOC4_SHADOW_DCD)
  1623. && (port->ip_port)) {
  1624. the_port = port->ip_port;
  1625. the_port->icount.dcd = 1;
  1626. wake_up_interruptible
  1627. (&the_port->state->port.delta_msr_wait);
  1628. } else if ((port->ip_notify & N_DDCD)
  1629. && !(shadow & IOC4_SHADOW_DCD)) {
  1630. /* Flag delta DCD/no DCD */
  1631. port->ip_flags |= DCD_ON;
  1632. }
  1633. }
  1634. /* Handle a CTS change */
  1635. if (sio_ir & hooks->intr_delta_cts) {
  1636. /* ACK the interrupt */
  1637. writel(hooks->intr_delta_cts,
  1638. &port->ip_mem->sio_ir.raw);
  1639. shadow = readl(&port->ip_serial_regs->shadow);
  1640. if ((port->ip_notify & N_DCTS)
  1641. && (port->ip_port)) {
  1642. the_port = port->ip_port;
  1643. the_port->icount.cts =
  1644. (shadow & IOC4_SHADOW_CTS) ? 1 : 0;
  1645. wake_up_interruptible
  1646. (&the_port->state->port.delta_msr_wait);
  1647. }
  1648. }
  1649. /* rx timeout interrupt. Must be some data available. Put this
  1650. * before the check for rx_high since servicing this condition
  1651. * may cause that condition to clear.
  1652. */
  1653. if (sio_ir & hooks->intr_rx_timer) {
  1654. /* ACK the interrupt */
  1655. writel(hooks->intr_rx_timer,
  1656. &port->ip_mem->sio_ir.raw);
  1657. if ((port->ip_notify & N_DATA_READY)
  1658. && (port->ip_port)) {
  1659. /* ip_lock is set on call here */
  1660. receive_chars(port->ip_port);
  1661. }
  1662. }
  1663. /* rx high interrupt. Must be after rx_timer. */
  1664. else if (sio_ir & hooks->intr_rx_high) {
  1665. /* Data available, notify upper layer */
  1666. if ((port->ip_notify & N_DATA_READY)
  1667. && port->ip_port) {
  1668. /* ip_lock is set on call here */
  1669. receive_chars(port->ip_port);
  1670. }
  1671. /* We can't ACK this interrupt. If receive_chars didn't
  1672. * cause the condition to clear, we'll have to disable
  1673. * the interrupt until the data is drained.
  1674. * If the read was aborted, don't disable the interrupt
  1675. * as this may cause us to hang indefinitely. An
  1676. * aborted read generally means that this interrupt
  1677. * hasn't been delivered to the cpu yet anyway, even
  1678. * though we see it as asserted when we read the sio_ir.
  1679. */
  1680. if ((sio_ir = PENDING(port)) & hooks->intr_rx_high) {
  1681. if ((port->ip_flags & READ_ABORTED) == 0) {
  1682. port->ip_ienb &= ~hooks->intr_rx_high;
  1683. port->ip_flags |= INPUT_HIGH;
  1684. } else {
  1685. rx_high_rd_aborted++;
  1686. }
  1687. }
  1688. }
  1689. /* We got a low water interrupt: notify upper layer to
  1690. * send more data. Must come before tx_mt since servicing
  1691. * this condition may cause that condition to clear.
  1692. */
  1693. if (sio_ir & hooks->intr_tx_explicit) {
  1694. port->ip_flags &= ~LOWAT_WRITTEN;
  1695. /* ACK the interrupt */
  1696. writel(hooks->intr_tx_explicit,
  1697. &port->ip_mem->sio_ir.raw);
  1698. if (port->ip_notify & N_OUTPUT_LOWAT)
  1699. ioc4_cb_output_lowat(port->ip_port);
  1700. }
  1701. /* Handle tx_mt. Must come after tx_explicit. */
  1702. else if (sio_ir & hooks->intr_tx_mt) {
  1703. /* If we are expecting a lowat notification
  1704. * and we get to this point it probably means that for
  1705. * some reason the tx_explicit didn't work as expected
  1706. * (that can legitimately happen if the output buffer is
  1707. * filled up in just the right way).
  1708. * So send the notification now.
  1709. */
  1710. if (port->ip_notify & N_OUTPUT_LOWAT) {
  1711. ioc4_cb_output_lowat(port->ip_port);
  1712. /* We need to reload the sio_ir since the lowat
  1713. * call may have caused another write to occur,
  1714. * clearing the tx_mt condition.
  1715. */
  1716. sio_ir = PENDING(port);
  1717. }
  1718. /* If the tx_mt condition still persists even after the
  1719. * lowat call, we've got some work to do.
  1720. */
  1721. if (sio_ir & hooks->intr_tx_mt) {
  1722. /* If we are not currently expecting DMA input,
  1723. * and the transmitter has just gone idle,
  1724. * there is no longer any reason for DMA, so
  1725. * disable it.
  1726. */
  1727. if (!(port->ip_notify
  1728. & (N_DATA_READY | N_DDCD))) {
  1729. BUG_ON(!(port->ip_sscr
  1730. & IOC4_SSCR_DMA_EN));
  1731. port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
  1732. writel(port->ip_sscr,
  1733. &port->ip_serial_regs->sscr);
  1734. }
  1735. /* Prevent infinite tx_mt interrupt */
  1736. port->ip_ienb &= ~hooks->intr_tx_mt;
  1737. }
  1738. }
  1739. sio_ir = PENDING(port);
  1740. /* if the read was aborted and only hooks->intr_rx_high,
  1741. * clear hooks->intr_rx_high, so we do not loop forever.
  1742. */
  1743. if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
  1744. sio_ir &= ~hooks->intr_rx_high;
  1745. }
  1746. } while (sio_ir & hooks->intr_all);
  1747. spin_unlock_irqrestore(&port->ip_lock, flags);
  1748. /* Re-enable interrupts before returning from interrupt handler.
  1749. * Getting interrupted here is okay. It'll just v() our semaphore, and
  1750. * we'll come through the loop again.
  1751. */
  1752. write_ireg(port->ip_ioc4_soft, port->ip_ienb, IOC4_W_IES,
  1753. IOC4_SIO_INTR_TYPE);
  1754. }
  1755. /*
  1756. * ioc4_cb_post_ncs - called for some basic errors
  1757. * @port: port to use
  1758. * @ncs: event
  1759. */
  1760. static void ioc4_cb_post_ncs(struct uart_port *the_port, int ncs)
  1761. {
  1762. struct uart_icount *icount;
  1763. icount = &the_port->icount;
  1764. if (ncs & NCS_BREAK)
  1765. icount->brk++;
  1766. if (ncs & NCS_FRAMING)
  1767. icount->frame++;
  1768. if (ncs & NCS_OVERRUN)
  1769. icount->overrun++;
  1770. if (ncs & NCS_PARITY)
  1771. icount->parity++;
  1772. }
  1773. /**
  1774. * do_read - Read in bytes from the port. Return the number of bytes
  1775. * actually read.
  1776. * @the_port: port to use
  1777. * @buf: place to put the stuff we read
  1778. * @len: how big 'buf' is
  1779. */
  1780. static inline int do_read(struct uart_port *the_port, unsigned char *buf,
  1781. int len)
  1782. {
  1783. int prod_ptr, cons_ptr, total;
  1784. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  1785. struct ring *inring;
  1786. struct ring_entry *entry;
  1787. struct hooks *hooks;
  1788. int byte_num;
  1789. char *sc;
  1790. int loop_counter;
  1791. BUG_ON(!(len >= 0));
  1792. BUG_ON(!port);
  1793. hooks = port->ip_hooks;
  1794. /* There is a nasty timing issue in the IOC4. When the rx_timer
  1795. * expires or the rx_high condition arises, we take an interrupt.
  1796. * At some point while servicing the interrupt, we read bytes from
  1797. * the ring buffer and re-arm the rx_timer. However the rx_timer is
  1798. * not started until the first byte is received *after* it is armed,
  1799. * and any bytes pending in the rx construction buffers are not drained
  1800. * to memory until either there are 4 bytes available or the rx_timer
  1801. * expires. This leads to a potential situation where data is left
  1802. * in the construction buffers forever - 1 to 3 bytes were received
  1803. * after the interrupt was generated but before the rx_timer was
  1804. * re-armed. At that point as long as no subsequent bytes are received
  1805. * the timer will never be started and the bytes will remain in the
  1806. * construction buffer forever. The solution is to execute a DRAIN
  1807. * command after rearming the timer. This way any bytes received before
  1808. * the DRAIN will be drained to memory, and any bytes received after
  1809. * the DRAIN will start the TIMER and be drained when it expires.
  1810. * Luckily, this only needs to be done when the DMA buffer is empty
  1811. * since there is no requirement that this function return all
  1812. * available data as long as it returns some.
  1813. */
  1814. /* Re-arm the timer */
  1815. writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
  1816. prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
  1817. cons_ptr = port->ip_rx_cons;
  1818. if (prod_ptr == cons_ptr) {
  1819. int reset_dma = 0;
  1820. /* Input buffer appears empty, do a flush. */
  1821. /* DMA must be enabled for this to work. */
  1822. if (!(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
  1823. port->ip_sscr |= IOC4_SSCR_DMA_EN;
  1824. reset_dma = 1;
  1825. }
  1826. /* Potential race condition: we must reload the srpir after
  1827. * issuing the drain command, otherwise we could think the rx
  1828. * buffer is empty, then take a very long interrupt, and when
  1829. * we come back it's full and we wait forever for the drain to
  1830. * complete.
  1831. */
  1832. writel(port->ip_sscr | IOC4_SSCR_RX_DRAIN,
  1833. &port->ip_serial_regs->sscr);
  1834. prod_ptr = readl(&port->ip_serial_regs->srpir)
  1835. & PROD_CONS_MASK;
  1836. /* We must not wait for the DRAIN to complete unless there are
  1837. * at least 8 bytes (2 ring entries) available to receive the
  1838. * data otherwise the DRAIN will never complete and we'll
  1839. * deadlock here.
  1840. * In fact, to make things easier, I'll just ignore the flush if
  1841. * there is any data at all now available.
  1842. */
  1843. if (prod_ptr == cons_ptr) {
  1844. loop_counter = 0;
  1845. while (readl(&port->ip_serial_regs->sscr) &
  1846. IOC4_SSCR_RX_DRAIN) {
  1847. loop_counter++;
  1848. if (loop_counter > MAXITER)
  1849. return -1;
  1850. }
  1851. /* SIGH. We have to reload the prod_ptr *again* since
  1852. * the drain may have caused it to change
  1853. */
  1854. prod_ptr = readl(&port->ip_serial_regs->srpir)
  1855. & PROD_CONS_MASK;
  1856. }
  1857. if (reset_dma) {
  1858. port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
  1859. writel(port->ip_sscr, &port->ip_serial_regs->sscr);
  1860. }
  1861. }
  1862. inring = port->ip_inring;
  1863. port->ip_flags &= ~READ_ABORTED;
  1864. total = 0;
  1865. loop_counter = 0xfffff; /* to avoid hangs */
  1866. /* Grab bytes from the hardware */
  1867. while ((prod_ptr != cons_ptr) && (len > 0)) {
  1868. entry = (struct ring_entry *)((caddr_t)inring + cons_ptr);
  1869. if ( loop_counter-- <= 0 ) {
  1870. printk(KERN_WARNING "IOC4 serial: "
  1871. "possible hang condition/"
  1872. "port stuck on read.\n");
  1873. break;
  1874. }
  1875. /* According to the producer pointer, this ring entry
  1876. * must contain some data. But if the PIO happened faster
  1877. * than the DMA, the data may not be available yet, so let's
  1878. * wait until it arrives.
  1879. */
  1880. if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
  1881. /* Indicate the read is aborted so we don't disable
  1882. * the interrupt thinking that the consumer is
  1883. * congested.
  1884. */
  1885. port->ip_flags |= READ_ABORTED;
  1886. len = 0;
  1887. break;
  1888. }
  1889. /* Load the bytes/status out of the ring entry */
  1890. for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
  1891. sc = &(entry->ring_sc[byte_num]);
  1892. /* Check for change in modem state or overrun */
  1893. if ((*sc & IOC4_RXSB_MODEM_VALID)
  1894. && (port->ip_notify & N_DDCD)) {
  1895. /* Notify upper layer if DCD dropped */
  1896. if ((port->ip_flags & DCD_ON)
  1897. && !(*sc & IOC4_RXSB_DCD)) {
  1898. /* If we have already copied some data,
  1899. * return it. We'll pick up the carrier
  1900. * drop on the next pass. That way we
  1901. * don't throw away the data that has
  1902. * already been copied back to
  1903. * the caller's buffer.
  1904. */
  1905. if (total > 0) {
  1906. len = 0;
  1907. break;
  1908. }
  1909. port->ip_flags &= ~DCD_ON;
  1910. /* Turn off this notification so the
  1911. * carrier drop protocol won't see it
  1912. * again when it does a read.
  1913. */
  1914. *sc &= ~IOC4_RXSB_MODEM_VALID;
  1915. /* To keep things consistent, we need
  1916. * to update the consumer pointer so
  1917. * the next reader won't come in and
  1918. * try to read the same ring entries
  1919. * again. This must be done here before
  1920. * the dcd change.
  1921. */
  1922. if ((entry->ring_allsc & RING_ANY_VALID)
  1923. == 0) {
  1924. cons_ptr += (int)sizeof
  1925. (struct ring_entry);
  1926. cons_ptr &= PROD_CONS_MASK;
  1927. }
  1928. writel(cons_ptr,
  1929. &port->ip_serial_regs->srcir);
  1930. port->ip_rx_cons = cons_ptr;
  1931. /* Notify upper layer of carrier drop */
  1932. if ((port->ip_notify & N_DDCD)
  1933. && port->ip_port) {
  1934. the_port->icount.dcd = 0;
  1935. wake_up_interruptible
  1936. (&the_port->state->
  1937. port.delta_msr_wait);
  1938. }
  1939. /* If we had any data to return, we
  1940. * would have returned it above.
  1941. */
  1942. return 0;
  1943. }
  1944. }
  1945. if (*sc & IOC4_RXSB_MODEM_VALID) {
  1946. /* Notify that an input overrun occurred */
  1947. if ((*sc & IOC4_RXSB_OVERRUN)
  1948. && (port->ip_notify & N_OVERRUN_ERROR)) {
  1949. ioc4_cb_post_ncs(the_port, NCS_OVERRUN);
  1950. }
  1951. /* Don't look at this byte again */
  1952. *sc &= ~IOC4_RXSB_MODEM_VALID;
  1953. }
  1954. /* Check for valid data or RX errors */
  1955. if ((*sc & IOC4_RXSB_DATA_VALID) &&
  1956. ((*sc & (IOC4_RXSB_PAR_ERR
  1957. | IOC4_RXSB_FRAME_ERR
  1958. | IOC4_RXSB_BREAK))
  1959. && (port->ip_notify & (N_PARITY_ERROR
  1960. | N_FRAMING_ERROR
  1961. | N_BREAK)))) {
  1962. /* There is an error condition on the next byte.
  1963. * If we have already transferred some bytes,
  1964. * we'll stop here. Otherwise if this is the
  1965. * first byte to be read, we'll just transfer
  1966. * it alone after notifying the
  1967. * upper layer of its status.
  1968. */
  1969. if (total > 0) {
  1970. len = 0;
  1971. break;
  1972. } else {
  1973. if ((*sc & IOC4_RXSB_PAR_ERR) &&
  1974. (port->ip_notify & N_PARITY_ERROR)) {
  1975. ioc4_cb_post_ncs(the_port,
  1976. NCS_PARITY);
  1977. }
  1978. if ((*sc & IOC4_RXSB_FRAME_ERR) &&
  1979. (port->ip_notify & N_FRAMING_ERROR)){
  1980. ioc4_cb_post_ncs(the_port,
  1981. NCS_FRAMING);
  1982. }
  1983. if ((*sc & IOC4_RXSB_BREAK)
  1984. && (port->ip_notify & N_BREAK)) {
  1985. ioc4_cb_post_ncs
  1986. (the_port,
  1987. NCS_BREAK);
  1988. }
  1989. len = 1;
  1990. }
  1991. }
  1992. if (*sc & IOC4_RXSB_DATA_VALID) {
  1993. *sc &= ~IOC4_RXSB_DATA_VALID;
  1994. *buf = entry->ring_data[byte_num];
  1995. buf++;
  1996. len--;
  1997. total++;
  1998. }
  1999. }
  2000. /* If we used up this entry entirely, go on to the next one,
  2001. * otherwise we must have run out of buffer space, so
  2002. * leave the consumer pointer here for the next read in case
  2003. * there are still unread bytes in this entry.
  2004. */
  2005. if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
  2006. cons_ptr += (int)sizeof(struct ring_entry);
  2007. cons_ptr &= PROD_CONS_MASK;
  2008. }
  2009. }
  2010. /* Update consumer pointer and re-arm rx timer interrupt */
  2011. writel(cons_ptr, &port->ip_serial_regs->srcir);
  2012. port->ip_rx_cons = cons_ptr;
  2013. /* If we have now dipped below the rx high water mark and we have
  2014. * rx_high interrupt turned off, we can now turn it back on again.
  2015. */
  2016. if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
  2017. & PROD_CONS_MASK) < ((port->ip_sscr &
  2018. IOC4_SSCR_RX_THRESHOLD)
  2019. << IOC4_PROD_CONS_PTR_OFF))) {
  2020. port->ip_flags &= ~INPUT_HIGH;
  2021. enable_intrs(port, hooks->intr_rx_high);
  2022. }
  2023. return total;
  2024. }
  2025. /**
  2026. * receive_chars - upper level read. Called with ip_lock.
  2027. * @the_port: port to read from
  2028. */
  2029. static void receive_chars(struct uart_port *the_port)
  2030. {
  2031. unsigned char ch[IOC4_MAX_CHARS];
  2032. int read_count, request_count = IOC4_MAX_CHARS;
  2033. struct uart_icount *icount;
  2034. struct uart_state *state = the_port->state;
  2035. unsigned long pflags;
  2036. /* Make sure all the pointers are "good" ones */
  2037. if (!state)
  2038. return;
  2039. spin_lock_irqsave(&the_port->lock, pflags);
  2040. request_count = tty_buffer_request_room(&state->port, IOC4_MAX_CHARS);
  2041. if (request_count > 0) {
  2042. icount = &the_port->icount;
  2043. read_count = do_read(the_port, ch, request_count);
  2044. if (read_count > 0) {
  2045. tty_insert_flip_string(&state->port, ch, read_count);
  2046. icount->rx += read_count;
  2047. }
  2048. }
  2049. spin_unlock_irqrestore(&the_port->lock, pflags);
  2050. tty_flip_buffer_push(&state->port);
  2051. }
  2052. /**
  2053. * ic4_type - What type of console are we?
  2054. * @port: Port to operate with (we ignore since we only have one port)
  2055. *
  2056. */
  2057. static const char *ic4_type(struct uart_port *the_port)
  2058. {
  2059. if (the_port->mapbase == PROTO_RS232)
  2060. return "SGI IOC4 Serial [rs232]";
  2061. else
  2062. return "SGI IOC4 Serial [rs422]";
  2063. }
  2064. /**
  2065. * ic4_tx_empty - Is the transmitter empty?
  2066. * @port: Port to operate on
  2067. *
  2068. */
  2069. static unsigned int ic4_tx_empty(struct uart_port *the_port)
  2070. {
  2071. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  2072. unsigned int ret = 0;
  2073. if (port_is_active(port, the_port)) {
  2074. if (readl(&port->ip_serial_regs->shadow) & IOC4_SHADOW_TEMT)
  2075. ret = TIOCSER_TEMT;
  2076. }
  2077. return ret;
  2078. }
  2079. /**
  2080. * ic4_stop_tx - stop the transmitter
  2081. * @port: Port to operate on
  2082. *
  2083. */
  2084. static void ic4_stop_tx(struct uart_port *the_port)
  2085. {
  2086. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  2087. if (port_is_active(port, the_port))
  2088. set_notification(port, N_OUTPUT_LOWAT, 0);
  2089. }
  2090. /**
  2091. * null_void_function -
  2092. * @port: Port to operate on
  2093. *
  2094. */
  2095. static void null_void_function(struct uart_port *the_port)
  2096. {
  2097. }
  2098. /**
  2099. * ic4_shutdown - shut down the port - free irq and disable
  2100. * @port: Port to shut down
  2101. *
  2102. */
  2103. static void ic4_shutdown(struct uart_port *the_port)
  2104. {
  2105. unsigned long port_flags;
  2106. struct ioc4_port *port;
  2107. struct uart_state *state;
  2108. port = get_ioc4_port(the_port, 0);
  2109. if (!port)
  2110. return;
  2111. state = the_port->state;
  2112. port->ip_port = NULL;
  2113. wake_up_interruptible(&state->port.delta_msr_wait);
  2114. if (state->port.tty)
  2115. set_bit(TTY_IO_ERROR, &state->port.tty->flags);
  2116. spin_lock_irqsave(&the_port->lock, port_flags);
  2117. set_notification(port, N_ALL, 0);
  2118. port->ip_flags = PORT_INACTIVE;
  2119. spin_unlock_irqrestore(&the_port->lock, port_flags);
  2120. }
  2121. /**
  2122. * ic4_set_mctrl - set control lines (dtr, rts, etc)
  2123. * @port: Port to operate on
  2124. * @mctrl: Lines to set/unset
  2125. *
  2126. */
  2127. static void ic4_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
  2128. {
  2129. unsigned char mcr = 0;
  2130. struct ioc4_port *port;
  2131. port = get_ioc4_port(the_port, 0);
  2132. if (!port_is_active(port, the_port))
  2133. return;
  2134. if (mctrl & TIOCM_RTS)
  2135. mcr |= UART_MCR_RTS;
  2136. if (mctrl & TIOCM_DTR)
  2137. mcr |= UART_MCR_DTR;
  2138. if (mctrl & TIOCM_OUT1)
  2139. mcr |= UART_MCR_OUT1;
  2140. if (mctrl & TIOCM_OUT2)
  2141. mcr |= UART_MCR_OUT2;
  2142. if (mctrl & TIOCM_LOOP)
  2143. mcr |= UART_MCR_LOOP;
  2144. set_mcr(the_port, mcr, IOC4_SHADOW_DTR);
  2145. }
  2146. /**
  2147. * ic4_get_mctrl - get control line info
  2148. * @port: port to operate on
  2149. *
  2150. */
  2151. static unsigned int ic4_get_mctrl(struct uart_port *the_port)
  2152. {
  2153. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  2154. uint32_t shadow;
  2155. unsigned int ret = 0;
  2156. if (!port_is_active(port, the_port))
  2157. return 0;
  2158. shadow = readl(&port->ip_serial_regs->shadow);
  2159. if (shadow & IOC4_SHADOW_DCD)
  2160. ret |= TIOCM_CAR;
  2161. if (shadow & IOC4_SHADOW_DR)
  2162. ret |= TIOCM_DSR;
  2163. if (shadow & IOC4_SHADOW_CTS)
  2164. ret |= TIOCM_CTS;
  2165. return ret;
  2166. }
  2167. /**
  2168. * ic4_start_tx - Start transmitter, flush any output
  2169. * @port: Port to operate on
  2170. *
  2171. */
  2172. static void ic4_start_tx(struct uart_port *the_port)
  2173. {
  2174. struct ioc4_port *port = get_ioc4_port(the_port, 0);
  2175. if (port_is_active(port, the_port)) {
  2176. set_notification(port, N_OUTPUT_LOWAT, 1);
  2177. enable_intrs(port, port->ip_hooks->intr_tx_mt);
  2178. }
  2179. }
  2180. /**
  2181. * ic4_break_ctl - handle breaks
  2182. * @port: Port to operate on
  2183. * @break_state: Break state
  2184. *
  2185. */
  2186. static void ic4_break_ctl(struct uart_port *the_port, int break_state)
  2187. {
  2188. }
  2189. /**
  2190. * ic4_startup - Start up the serial port
  2191. * @port: Port to operate on
  2192. *
  2193. */
  2194. static int ic4_startup(struct uart_port *the_port)
  2195. {
  2196. int retval;
  2197. struct ioc4_port *port;
  2198. struct ioc4_control *control;
  2199. struct uart_state *state;
  2200. unsigned long port_flags;
  2201. if (!the_port)
  2202. return -ENODEV;
  2203. port = get_ioc4_port(the_port, 1);
  2204. if (!port)
  2205. return -ENODEV;
  2206. state = the_port->state;
  2207. control = port->ip_control;
  2208. if (!control) {
  2209. port->ip_port = NULL;
  2210. return -ENODEV;
  2211. }
  2212. /* Start up the serial port */
  2213. spin_lock_irqsave(&the_port->lock, port_flags);
  2214. retval = ic4_startup_local(the_port);
  2215. spin_unlock_irqrestore(&the_port->lock, port_flags);
  2216. return retval;
  2217. }
  2218. /**
  2219. * ic4_set_termios - set termios stuff
  2220. * @port: port to operate on
  2221. * @termios: New settings
  2222. * @termios: Old
  2223. *
  2224. */
  2225. static void
  2226. ic4_set_termios(struct uart_port *the_port,
  2227. struct ktermios *termios, struct ktermios *old_termios)
  2228. {
  2229. unsigned long port_flags;
  2230. spin_lock_irqsave(&the_port->lock, port_flags);
  2231. ioc4_change_speed(the_port, termios, old_termios);
  2232. spin_unlock_irqrestore(&the_port->lock, port_flags);
  2233. }
  2234. /**
  2235. * ic4_request_port - allocate resources for port - no op....
  2236. * @port: port to operate on
  2237. *
  2238. */
  2239. static int ic4_request_port(struct uart_port *port)
  2240. {
  2241. return 0;
  2242. }
  2243. /* Associate the uart functions above - given to serial core */
  2244. static const struct uart_ops ioc4_ops = {
  2245. .tx_empty = ic4_tx_empty,
  2246. .set_mctrl = ic4_set_mctrl,
  2247. .get_mctrl = ic4_get_mctrl,
  2248. .stop_tx = ic4_stop_tx,
  2249. .start_tx = ic4_start_tx,
  2250. .stop_rx = null_void_function,
  2251. .break_ctl = ic4_break_ctl,
  2252. .startup = ic4_startup,
  2253. .shutdown = ic4_shutdown,
  2254. .set_termios = ic4_set_termios,
  2255. .type = ic4_type,
  2256. .release_port = null_void_function,
  2257. .request_port = ic4_request_port,
  2258. };
  2259. /*
  2260. * Boot-time initialization code
  2261. */
  2262. static struct uart_driver ioc4_uart_rs232 = {
  2263. .owner = THIS_MODULE,
  2264. .driver_name = "ioc4_serial_rs232",
  2265. .dev_name = DEVICE_NAME_RS232,
  2266. .major = DEVICE_MAJOR,
  2267. .minor = DEVICE_MINOR_RS232,
  2268. .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
  2269. };
  2270. static struct uart_driver ioc4_uart_rs422 = {
  2271. .owner = THIS_MODULE,
  2272. .driver_name = "ioc4_serial_rs422",
  2273. .dev_name = DEVICE_NAME_RS422,
  2274. .major = DEVICE_MAJOR,
  2275. .minor = DEVICE_MINOR_RS422,
  2276. .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
  2277. };
  2278. /**
  2279. * ioc4_serial_remove_one - detach function
  2280. *
  2281. * @idd: IOC4 master module data for this IOC4
  2282. */
  2283. static int ioc4_serial_remove_one(struct ioc4_driver_data *idd)
  2284. {
  2285. int port_num, port_type;
  2286. struct ioc4_control *control;
  2287. struct uart_port *the_port;
  2288. struct ioc4_port *port;
  2289. struct ioc4_soft *soft;
  2290. /* If serial driver did not attach, don't try to detach */
  2291. control = idd->idd_serial_data;
  2292. if (!control)
  2293. return 0;
  2294. for (port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS; port_num++) {
  2295. for (port_type = UART_PORT_MIN;
  2296. port_type < UART_PORT_COUNT;
  2297. port_type++) {
  2298. the_port = &control->ic_port[port_num].icp_uart_port
  2299. [port_type];
  2300. if (the_port) {
  2301. switch (port_type) {
  2302. case UART_PORT_RS422:
  2303. uart_remove_one_port(&ioc4_uart_rs422,
  2304. the_port);
  2305. break;
  2306. default:
  2307. case UART_PORT_RS232:
  2308. uart_remove_one_port(&ioc4_uart_rs232,
  2309. the_port);
  2310. break;
  2311. }
  2312. }
  2313. }
  2314. port = control->ic_port[port_num].icp_port;
  2315. /* we allocate in pairs */
  2316. if (!(port_num & 1) && port) {
  2317. pci_free_consistent(port->ip_pdev,
  2318. TOTAL_RING_BUF_SIZE,
  2319. port->ip_cpu_ringbuf,
  2320. port->ip_dma_ringbuf);
  2321. kfree(port);
  2322. }
  2323. }
  2324. soft = control->ic_soft;
  2325. if (soft) {
  2326. free_irq(control->ic_irq, soft);
  2327. if (soft->is_ioc4_serial_addr) {
  2328. iounmap(soft->is_ioc4_serial_addr);
  2329. release_mem_region((unsigned long)
  2330. soft->is_ioc4_serial_addr,
  2331. sizeof(struct ioc4_serial));
  2332. }
  2333. kfree(soft);
  2334. }
  2335. kfree(control);
  2336. idd->idd_serial_data = NULL;
  2337. return 0;
  2338. }
  2339. /**
  2340. * ioc4_serial_core_attach_rs232 - register with serial core
  2341. * This is done during pci probing
  2342. * @pdev: handle for this card
  2343. */
  2344. static inline int
  2345. ioc4_serial_core_attach(struct pci_dev *pdev, int port_type)
  2346. {
  2347. struct ioc4_port *port;
  2348. struct uart_port *the_port;
  2349. struct ioc4_driver_data *idd = pci_get_drvdata(pdev);
  2350. struct ioc4_control *control = idd->idd_serial_data;
  2351. int port_num;
  2352. int port_type_idx;
  2353. struct uart_driver *u_driver;
  2354. DPRINT_CONFIG(("%s: attach pdev 0x%p - control 0x%p\n",
  2355. __func__, pdev, (void *)control));
  2356. if (!control)
  2357. return -ENODEV;
  2358. port_type_idx = (port_type == PROTO_RS232) ? UART_PORT_RS232
  2359. : UART_PORT_RS422;
  2360. u_driver = (port_type == PROTO_RS232) ? &ioc4_uart_rs232
  2361. : &ioc4_uart_rs422;
  2362. /* once around for each port on this card */
  2363. for (port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS; port_num++) {
  2364. the_port = &control->ic_port[port_num].icp_uart_port
  2365. [port_type_idx];
  2366. port = control->ic_port[port_num].icp_port;
  2367. port->ip_all_ports[port_type_idx] = the_port;
  2368. DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p : type %s\n",
  2369. __func__, (void *)the_port,
  2370. (void *)port,
  2371. port_type == PROTO_RS232 ? "rs232" : "rs422"));
  2372. /* membase, iobase and mapbase just need to be non-0 */
  2373. the_port->membase = (unsigned char __iomem *)1;
  2374. the_port->iobase = (pdev->bus->number << 16) | port_num;
  2375. the_port->line = (Num_of_ioc4_cards << 2) | port_num;
  2376. the_port->mapbase = port_type;
  2377. the_port->type = PORT_16550A;
  2378. the_port->fifosize = IOC4_FIFO_CHARS;
  2379. the_port->ops = &ioc4_ops;
  2380. the_port->irq = control->ic_irq;
  2381. the_port->dev = &pdev->dev;
  2382. spin_lock_init(&the_port->lock);
  2383. if (uart_add_one_port(u_driver, the_port) < 0) {
  2384. printk(KERN_WARNING
  2385. "%s: unable to add port %d bus %d\n",
  2386. __func__, the_port->line, pdev->bus->number);
  2387. } else {
  2388. DPRINT_CONFIG(
  2389. ("IOC4 serial port %d irq = %d, bus %d\n",
  2390. the_port->line, the_port->irq, pdev->bus->number));
  2391. }
  2392. }
  2393. return 0;
  2394. }
  2395. /**
  2396. * ioc4_serial_attach_one - register attach function
  2397. * called per card found from IOC4 master module.
  2398. * @idd: Master module data for this IOC4
  2399. */
  2400. static int
  2401. ioc4_serial_attach_one(struct ioc4_driver_data *idd)
  2402. {
  2403. unsigned long tmp_addr1;
  2404. struct ioc4_serial __iomem *serial;
  2405. struct ioc4_soft *soft;
  2406. struct ioc4_control *control;
  2407. int ret = 0;
  2408. DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, idd->idd_pdev,
  2409. idd->idd_pci_id));
  2410. /* PCI-RT does not bring out serial connections.
  2411. * Do not attach to this particular IOC4.
  2412. */
  2413. if (idd->idd_variant == IOC4_VARIANT_PCI_RT)
  2414. return 0;
  2415. /* request serial registers */
  2416. tmp_addr1 = idd->idd_bar0 + IOC4_SERIAL_OFFSET;
  2417. if (!request_mem_region(tmp_addr1, sizeof(struct ioc4_serial),
  2418. "sioc4_uart")) {
  2419. printk(KERN_WARNING
  2420. "ioc4 (%p): unable to get request region for "
  2421. "uart space\n", (void *)idd->idd_pdev);
  2422. ret = -ENODEV;
  2423. goto out1;
  2424. }
  2425. serial = ioremap(tmp_addr1, sizeof(struct ioc4_serial));
  2426. if (!serial) {
  2427. printk(KERN_WARNING
  2428. "ioc4 (%p) : unable to remap ioc4 serial register\n",
  2429. (void *)idd->idd_pdev);
  2430. ret = -ENODEV;
  2431. goto out2;
  2432. }
  2433. DPRINT_CONFIG(("%s : mem 0x%p, serial 0x%p\n",
  2434. __func__, (void *)idd->idd_misc_regs,
  2435. (void *)serial));
  2436. /* Get memory for the new card */
  2437. control = kzalloc(sizeof(struct ioc4_control), GFP_KERNEL);
  2438. if (!control) {
  2439. printk(KERN_WARNING "ioc4_attach_one"
  2440. ": unable to get memory for the IOC4\n");
  2441. ret = -ENOMEM;
  2442. goto out2;
  2443. }
  2444. idd->idd_serial_data = control;
  2445. /* Allocate the soft structure */
  2446. soft = kzalloc(sizeof(struct ioc4_soft), GFP_KERNEL);
  2447. if (!soft) {
  2448. printk(KERN_WARNING
  2449. "ioc4 (%p): unable to get memory for the soft struct\n",
  2450. (void *)idd->idd_pdev);
  2451. ret = -ENOMEM;
  2452. goto out3;
  2453. }
  2454. spin_lock_init(&soft->is_ir_lock);
  2455. soft->is_ioc4_misc_addr = idd->idd_misc_regs;
  2456. soft->is_ioc4_serial_addr = serial;
  2457. /* Init the IOC4 */
  2458. writel(0xf << IOC4_SIO_CR_CMD_PULSE_SHIFT,
  2459. &idd->idd_misc_regs->sio_cr.raw);
  2460. /* Enable serial port mode select generic PIO pins as outputs */
  2461. writel(IOC4_GPCR_UART0_MODESEL | IOC4_GPCR_UART1_MODESEL
  2462. | IOC4_GPCR_UART2_MODESEL | IOC4_GPCR_UART3_MODESEL,
  2463. &idd->idd_misc_regs->gpcr_s.raw);
  2464. /* Clear and disable all serial interrupts */
  2465. write_ireg(soft, ~0, IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
  2466. writel(~0, &idd->idd_misc_regs->sio_ir.raw);
  2467. write_ireg(soft, IOC4_OTHER_IR_SER_MEMERR, IOC4_W_IEC,
  2468. IOC4_OTHER_INTR_TYPE);
  2469. writel(IOC4_OTHER_IR_SER_MEMERR, &idd->idd_misc_regs->other_ir.raw);
  2470. control->ic_soft = soft;
  2471. /* Hook up interrupt handler */
  2472. if (!request_irq(idd->idd_pdev->irq, ioc4_intr, IRQF_SHARED,
  2473. "sgi-ioc4serial", soft)) {
  2474. control->ic_irq = idd->idd_pdev->irq;
  2475. } else {
  2476. printk(KERN_WARNING
  2477. "%s : request_irq fails for IRQ 0x%x\n ",
  2478. __func__, idd->idd_pdev->irq);
  2479. }
  2480. ret = ioc4_attach_local(idd);
  2481. if (ret)
  2482. goto out4;
  2483. /* register port with the serial core - 1 rs232, 1 rs422 */
  2484. ret = ioc4_serial_core_attach(idd->idd_pdev, PROTO_RS232);
  2485. if (ret)
  2486. goto out4;
  2487. ret = ioc4_serial_core_attach(idd->idd_pdev, PROTO_RS422);
  2488. if (ret)
  2489. goto out5;
  2490. Num_of_ioc4_cards++;
  2491. return ret;
  2492. /* error exits that give back resources */
  2493. out5:
  2494. ioc4_serial_remove_one(idd);
  2495. return ret;
  2496. out4:
  2497. kfree(soft);
  2498. out3:
  2499. kfree(control);
  2500. out2:
  2501. if (serial)
  2502. iounmap(serial);
  2503. release_mem_region(tmp_addr1, sizeof(struct ioc4_serial));
  2504. out1:
  2505. return ret;
  2506. }
  2507. static struct ioc4_submodule ioc4_serial_submodule = {
  2508. .is_name = "IOC4_serial",
  2509. .is_owner = THIS_MODULE,
  2510. .is_probe = ioc4_serial_attach_one,
  2511. .is_remove = ioc4_serial_remove_one,
  2512. };
  2513. /**
  2514. * ioc4_serial_init - module init
  2515. */
  2516. static int __init ioc4_serial_init(void)
  2517. {
  2518. int ret;
  2519. /* register with serial core */
  2520. if ((ret = uart_register_driver(&ioc4_uart_rs232)) < 0) {
  2521. printk(KERN_WARNING
  2522. "%s: Couldn't register rs232 IOC4 serial driver\n",
  2523. __func__);
  2524. goto out;
  2525. }
  2526. if ((ret = uart_register_driver(&ioc4_uart_rs422)) < 0) {
  2527. printk(KERN_WARNING
  2528. "%s: Couldn't register rs422 IOC4 serial driver\n",
  2529. __func__);
  2530. goto out_uart_rs232;
  2531. }
  2532. /* register with IOC4 main module */
  2533. ret = ioc4_register_submodule(&ioc4_serial_submodule);
  2534. if (ret)
  2535. goto out_uart_rs422;
  2536. return 0;
  2537. out_uart_rs422:
  2538. uart_unregister_driver(&ioc4_uart_rs422);
  2539. out_uart_rs232:
  2540. uart_unregister_driver(&ioc4_uart_rs232);
  2541. out:
  2542. return ret;
  2543. }
  2544. static void __exit ioc4_serial_exit(void)
  2545. {
  2546. ioc4_unregister_submodule(&ioc4_serial_submodule);
  2547. uart_unregister_driver(&ioc4_uart_rs232);
  2548. uart_unregister_driver(&ioc4_uart_rs422);
  2549. }
  2550. late_initcall(ioc4_serial_init); /* Call only after tty init is done */
  2551. module_exit(ioc4_serial_exit);
  2552. MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
  2553. MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC4 Base-IO Card");
  2554. MODULE_LICENSE("GPL");