ioc4_serial.c 81 KB

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