ioc4_serial.c 81 KB

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