ioc4_serial.c 81 KB

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