ioc4_serial.c 78 KB

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