ioc4_serial.c 78 KB

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