ioc4_serial.c 81 KB

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