sata_mv.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2005: EMC Corporation, all rights reserved.
  5. * Copyright 2005 Red Hat, Inc. All rights reserved.
  6. *
  7. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /*
  24. sata_mv TODO list:
  25. 1) Needs a full errata audit for all chipsets. I implemented most
  26. of the errata workarounds found in the Marvell vendor driver, but
  27. I distinctly remember a couple workarounds (one related to PCI-X)
  28. are still needed.
  29. 2) Improve/fix IRQ and error handling sequences.
  30. 3) ATAPI support (Marvell claims the 60xx/70xx chips can do it).
  31. 4) Think about TCQ support here, and for libata in general
  32. with controllers that suppport it via host-queuing hardware
  33. (a software-only implementation could be a nightmare).
  34. 5) Investigate problems with PCI Message Signalled Interrupts (MSI).
  35. 6) Add port multiplier support (intermediate)
  36. 8) Develop a low-power-consumption strategy, and implement it.
  37. 9) [Experiment, low priority] See if ATAPI can be supported using
  38. "unknown FIS" or "vendor-specific FIS" support, or something creative
  39. like that.
  40. 10) [Experiment, low priority] Investigate interrupt coalescing.
  41. Quite often, especially with PCI Message Signalled Interrupts (MSI),
  42. the overhead reduced by interrupt mitigation is quite often not
  43. worth the latency cost.
  44. 11) [Experiment, Marvell value added] Is it possible to use target
  45. mode to cross-connect two Linux boxes with Marvell cards? If so,
  46. creating LibATA target mode support would be very interesting.
  47. Target mode, for those without docs, is the ability to directly
  48. connect two SATA controllers.
  49. */
  50. #include <linux/kernel.h>
  51. #include <linux/module.h>
  52. #include <linux/pci.h>
  53. #include <linux/init.h>
  54. #include <linux/blkdev.h>
  55. #include <linux/delay.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/dmapool.h>
  58. #include <linux/dma-mapping.h>
  59. #include <linux/device.h>
  60. #include <linux/platform_device.h>
  61. #include <linux/ata_platform.h>
  62. #include <scsi/scsi_host.h>
  63. #include <scsi/scsi_cmnd.h>
  64. #include <scsi/scsi_device.h>
  65. #include <linux/libata.h>
  66. #define DRV_NAME "sata_mv"
  67. #define DRV_VERSION "1.20"
  68. enum {
  69. /* BAR's are enumerated in terms of pci_resource_start() terms */
  70. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  71. MV_IO_BAR = 2, /* offset 0x18: IO space */
  72. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  73. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  74. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  75. MV_PCI_REG_BASE = 0,
  76. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  77. MV_IRQ_COAL_CAUSE = (MV_IRQ_COAL_REG_BASE + 0x08),
  78. MV_IRQ_COAL_CAUSE_LO = (MV_IRQ_COAL_REG_BASE + 0x88),
  79. MV_IRQ_COAL_CAUSE_HI = (MV_IRQ_COAL_REG_BASE + 0x8c),
  80. MV_IRQ_COAL_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xcc),
  81. MV_IRQ_COAL_TIME_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xd0),
  82. MV_SATAHC0_REG_BASE = 0x20000,
  83. MV_FLASH_CTL = 0x1046c,
  84. MV_GPIO_PORT_CTL = 0x104f0,
  85. MV_RESET_CFG = 0x180d8,
  86. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  87. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  88. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  89. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  90. MV_MAX_Q_DEPTH = 32,
  91. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  92. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  93. * CRPB needs alignment on a 256B boundary. Size == 256B
  94. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  95. */
  96. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  97. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  98. MV_MAX_SG_CT = 256,
  99. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  100. MV_PORTS_PER_HC = 4,
  101. /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
  102. MV_PORT_HC_SHIFT = 2,
  103. /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
  104. MV_PORT_MASK = 3,
  105. /* Host Flags */
  106. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  107. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  108. /* SoC integrated controllers, no PCI interface */
  109. MV_FLAG_SOC = (1 << 28),
  110. MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  111. ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
  112. ATA_FLAG_PIO_POLLING,
  113. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  114. CRQB_FLAG_READ = (1 << 0),
  115. CRQB_TAG_SHIFT = 1,
  116. CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
  117. CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
  118. CRQB_CMD_ADDR_SHIFT = 8,
  119. CRQB_CMD_CS = (0x2 << 11),
  120. CRQB_CMD_LAST = (1 << 15),
  121. CRPB_FLAG_STATUS_SHIFT = 8,
  122. CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
  123. CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
  124. EPRD_FLAG_END_OF_TBL = (1 << 31),
  125. /* PCI interface registers */
  126. PCI_COMMAND_OFS = 0xc00,
  127. PCI_MAIN_CMD_STS_OFS = 0xd30,
  128. STOP_PCI_MASTER = (1 << 2),
  129. PCI_MASTER_EMPTY = (1 << 3),
  130. GLOB_SFT_RST = (1 << 4),
  131. MV_PCI_MODE = 0xd00,
  132. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  133. MV_PCI_DISC_TIMER = 0xd04,
  134. MV_PCI_MSI_TRIGGER = 0xc38,
  135. MV_PCI_SERR_MASK = 0xc28,
  136. MV_PCI_XBAR_TMOUT = 0x1d04,
  137. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  138. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  139. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  140. MV_PCI_ERR_COMMAND = 0x1d50,
  141. PCI_IRQ_CAUSE_OFS = 0x1d58,
  142. PCI_IRQ_MASK_OFS = 0x1d5c,
  143. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  144. PCIE_IRQ_CAUSE_OFS = 0x1900,
  145. PCIE_IRQ_MASK_OFS = 0x1910,
  146. PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
  147. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  148. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  149. HC_SOC_MAIN_IRQ_CAUSE_OFS = 0x20020,
  150. HC_SOC_MAIN_IRQ_MASK_OFS = 0x20024,
  151. PORT0_ERR = (1 << 0), /* shift by port # */
  152. PORT0_DONE = (1 << 1), /* shift by port # */
  153. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  154. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  155. PCI_ERR = (1 << 18),
  156. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  157. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  158. PORTS_0_3_COAL_DONE = (1 << 8),
  159. PORTS_4_7_COAL_DONE = (1 << 17),
  160. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  161. GPIO_INT = (1 << 22),
  162. SELF_INT = (1 << 23),
  163. TWSI_INT = (1 << 24),
  164. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  165. HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
  166. HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
  167. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  168. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  169. HC_MAIN_RSVD),
  170. HC_MAIN_MASKED_IRQS_5 = (PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
  171. HC_MAIN_RSVD_5),
  172. HC_MAIN_MASKED_IRQS_SOC = (PORTS_0_3_COAL_DONE | HC_MAIN_RSVD_SOC),
  173. /* SATAHC registers */
  174. HC_CFG_OFS = 0,
  175. HC_IRQ_CAUSE_OFS = 0x14,
  176. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  177. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  178. DEV_IRQ = (1 << 8), /* shift by port # */
  179. /* Shadow block registers */
  180. SHD_BLK_OFS = 0x100,
  181. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  182. /* SATA registers */
  183. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  184. SATA_ACTIVE_OFS = 0x350,
  185. SATA_FIS_IRQ_CAUSE_OFS = 0x364,
  186. PHY_MODE3 = 0x310,
  187. PHY_MODE4 = 0x314,
  188. PHY_MODE2 = 0x330,
  189. MV5_PHY_MODE = 0x74,
  190. MV5_LT_MODE = 0x30,
  191. MV5_PHY_CTL = 0x0C,
  192. SATA_INTERFACE_CTL = 0x050,
  193. MV_M2_PREAMP_MASK = 0x7e0,
  194. /* Port registers */
  195. EDMA_CFG_OFS = 0,
  196. EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
  197. EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
  198. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  199. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  200. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  201. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  202. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  203. EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
  204. EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
  205. EDMA_ERR_DEV = (1 << 2), /* device error */
  206. EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
  207. EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
  208. EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
  209. EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
  210. EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
  211. EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
  212. EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
  213. EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
  214. EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
  215. EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
  216. EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
  217. EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
  218. EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
  219. EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
  220. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
  221. EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
  222. EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
  223. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
  224. EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
  225. EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
  226. EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
  227. EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
  228. EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
  229. EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
  230. EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
  231. EDMA_ERR_OVERRUN_5 = (1 << 5),
  232. EDMA_ERR_UNDERRUN_5 = (1 << 6),
  233. EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
  234. EDMA_ERR_LNK_CTRL_RX_1 |
  235. EDMA_ERR_LNK_CTRL_RX_3 |
  236. EDMA_ERR_LNK_CTRL_TX,
  237. EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
  238. EDMA_ERR_PRD_PAR |
  239. EDMA_ERR_DEV_DCON |
  240. EDMA_ERR_DEV_CON |
  241. EDMA_ERR_SERR |
  242. EDMA_ERR_SELF_DIS |
  243. EDMA_ERR_CRQB_PAR |
  244. EDMA_ERR_CRPB_PAR |
  245. EDMA_ERR_INTRL_PAR |
  246. EDMA_ERR_IORDY |
  247. EDMA_ERR_LNK_CTRL_RX_2 |
  248. EDMA_ERR_LNK_DATA_RX |
  249. EDMA_ERR_LNK_DATA_TX |
  250. EDMA_ERR_TRANS_PROTO,
  251. EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
  252. EDMA_ERR_PRD_PAR |
  253. EDMA_ERR_DEV_DCON |
  254. EDMA_ERR_DEV_CON |
  255. EDMA_ERR_OVERRUN_5 |
  256. EDMA_ERR_UNDERRUN_5 |
  257. EDMA_ERR_SELF_DIS_5 |
  258. EDMA_ERR_CRQB_PAR |
  259. EDMA_ERR_CRPB_PAR |
  260. EDMA_ERR_INTRL_PAR |
  261. EDMA_ERR_IORDY,
  262. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  263. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  264. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  265. EDMA_REQ_Q_PTR_SHIFT = 5,
  266. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  267. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  268. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  269. EDMA_RSP_Q_PTR_SHIFT = 3,
  270. EDMA_CMD_OFS = 0x28, /* EDMA command register */
  271. EDMA_EN = (1 << 0), /* enable EDMA */
  272. EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
  273. ATA_RST = (1 << 2), /* reset trans/link/phy */
  274. EDMA_IORDY_TMOUT = 0x34,
  275. EDMA_ARB_CFG = 0x38,
  276. /* Host private flags (hp_flags) */
  277. MV_HP_FLAG_MSI = (1 << 0),
  278. MV_HP_ERRATA_50XXB0 = (1 << 1),
  279. MV_HP_ERRATA_50XXB2 = (1 << 2),
  280. MV_HP_ERRATA_60X1B2 = (1 << 3),
  281. MV_HP_ERRATA_60X1C0 = (1 << 4),
  282. MV_HP_ERRATA_XX42A0 = (1 << 5),
  283. MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
  284. MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
  285. MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
  286. MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
  287. /* Port private flags (pp_flags) */
  288. MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
  289. MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
  290. MV_PP_FLAG_HAD_A_RESET = (1 << 2), /* 1st hard reset complete? */
  291. };
  292. #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
  293. #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
  294. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  295. #define HAS_PCI(host) (!((host)->ports[0]->flags & MV_FLAG_SOC))
  296. enum {
  297. /* DMA boundary 0xffff is required by the s/g splitting
  298. * we need on /length/ in mv_fill-sg().
  299. */
  300. MV_DMA_BOUNDARY = 0xffffU,
  301. /* mask of register bits containing lower 32 bits
  302. * of EDMA request queue DMA address
  303. */
  304. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  305. /* ditto, for response queue */
  306. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  307. };
  308. enum chip_type {
  309. chip_504x,
  310. chip_508x,
  311. chip_5080,
  312. chip_604x,
  313. chip_608x,
  314. chip_6042,
  315. chip_7042,
  316. chip_soc,
  317. };
  318. /* Command ReQuest Block: 32B */
  319. struct mv_crqb {
  320. __le32 sg_addr;
  321. __le32 sg_addr_hi;
  322. __le16 ctrl_flags;
  323. __le16 ata_cmd[11];
  324. };
  325. struct mv_crqb_iie {
  326. __le32 addr;
  327. __le32 addr_hi;
  328. __le32 flags;
  329. __le32 len;
  330. __le32 ata_cmd[4];
  331. };
  332. /* Command ResPonse Block: 8B */
  333. struct mv_crpb {
  334. __le16 id;
  335. __le16 flags;
  336. __le32 tmstmp;
  337. };
  338. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  339. struct mv_sg {
  340. __le32 addr;
  341. __le32 flags_size;
  342. __le32 addr_hi;
  343. __le32 reserved;
  344. };
  345. struct mv_port_priv {
  346. struct mv_crqb *crqb;
  347. dma_addr_t crqb_dma;
  348. struct mv_crpb *crpb;
  349. dma_addr_t crpb_dma;
  350. struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
  351. dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
  352. unsigned int req_idx;
  353. unsigned int resp_idx;
  354. u32 pp_flags;
  355. };
  356. struct mv_port_signal {
  357. u32 amps;
  358. u32 pre;
  359. };
  360. struct mv_host_priv {
  361. u32 hp_flags;
  362. struct mv_port_signal signal[8];
  363. const struct mv_hw_ops *ops;
  364. int n_ports;
  365. void __iomem *base;
  366. void __iomem *main_cause_reg_addr;
  367. void __iomem *main_mask_reg_addr;
  368. u32 irq_cause_ofs;
  369. u32 irq_mask_ofs;
  370. u32 unmask_all_irqs;
  371. /*
  372. * These consistent DMA memory pools give us guaranteed
  373. * alignment for hardware-accessed data structures,
  374. * and less memory waste in accomplishing the alignment.
  375. */
  376. struct dma_pool *crqb_pool;
  377. struct dma_pool *crpb_pool;
  378. struct dma_pool *sg_tbl_pool;
  379. };
  380. struct mv_hw_ops {
  381. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  382. unsigned int port);
  383. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  384. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  385. void __iomem *mmio);
  386. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  387. unsigned int n_hc);
  388. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  389. void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
  390. };
  391. static void mv_irq_clear(struct ata_port *ap);
  392. static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
  393. static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  394. static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
  395. static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  396. static int mv_port_start(struct ata_port *ap);
  397. static void mv_port_stop(struct ata_port *ap);
  398. static void mv_qc_prep(struct ata_queued_cmd *qc);
  399. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  400. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  401. static void mv_error_handler(struct ata_port *ap);
  402. static void mv_eh_freeze(struct ata_port *ap);
  403. static void mv_eh_thaw(struct ata_port *ap);
  404. static void mv6_dev_config(struct ata_device *dev);
  405. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  406. unsigned int port);
  407. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  408. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  409. void __iomem *mmio);
  410. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  411. unsigned int n_hc);
  412. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  413. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
  414. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  415. unsigned int port);
  416. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  417. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  418. void __iomem *mmio);
  419. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  420. unsigned int n_hc);
  421. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  422. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  423. void __iomem *mmio);
  424. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  425. void __iomem *mmio);
  426. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  427. void __iomem *mmio, unsigned int n_hc);
  428. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  429. void __iomem *mmio);
  430. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
  431. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
  432. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  433. unsigned int port_no);
  434. static void mv_edma_cfg(struct mv_port_priv *pp, struct mv_host_priv *hpriv,
  435. void __iomem *port_mmio, int want_ncq);
  436. static int __mv_stop_dma(struct ata_port *ap);
  437. /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
  438. * because we have to allow room for worst case splitting of
  439. * PRDs for 64K boundaries in mv_fill_sg().
  440. */
  441. static struct scsi_host_template mv5_sht = {
  442. .module = THIS_MODULE,
  443. .name = DRV_NAME,
  444. .ioctl = ata_scsi_ioctl,
  445. .queuecommand = ata_scsi_queuecmd,
  446. .can_queue = ATA_DEF_QUEUE,
  447. .this_id = ATA_SHT_THIS_ID,
  448. .sg_tablesize = MV_MAX_SG_CT / 2,
  449. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  450. .emulated = ATA_SHT_EMULATED,
  451. .use_clustering = 1,
  452. .proc_name = DRV_NAME,
  453. .dma_boundary = MV_DMA_BOUNDARY,
  454. .slave_configure = ata_scsi_slave_config,
  455. .slave_destroy = ata_scsi_slave_destroy,
  456. .bios_param = ata_std_bios_param,
  457. };
  458. static struct scsi_host_template mv6_sht = {
  459. .module = THIS_MODULE,
  460. .name = DRV_NAME,
  461. .ioctl = ata_scsi_ioctl,
  462. .queuecommand = ata_scsi_queuecmd,
  463. .change_queue_depth = ata_scsi_change_queue_depth,
  464. .can_queue = MV_MAX_Q_DEPTH - 1,
  465. .this_id = ATA_SHT_THIS_ID,
  466. .sg_tablesize = MV_MAX_SG_CT / 2,
  467. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  468. .emulated = ATA_SHT_EMULATED,
  469. .use_clustering = 1,
  470. .proc_name = DRV_NAME,
  471. .dma_boundary = MV_DMA_BOUNDARY,
  472. .slave_configure = ata_scsi_slave_config,
  473. .slave_destroy = ata_scsi_slave_destroy,
  474. .bios_param = ata_std_bios_param,
  475. };
  476. static const struct ata_port_operations mv5_ops = {
  477. .tf_load = ata_tf_load,
  478. .tf_read = ata_tf_read,
  479. .check_status = ata_check_status,
  480. .exec_command = ata_exec_command,
  481. .dev_select = ata_std_dev_select,
  482. .cable_detect = ata_cable_sata,
  483. .qc_prep = mv_qc_prep,
  484. .qc_issue = mv_qc_issue,
  485. .data_xfer = ata_data_xfer,
  486. .irq_clear = mv_irq_clear,
  487. .irq_on = ata_irq_on,
  488. .error_handler = mv_error_handler,
  489. .freeze = mv_eh_freeze,
  490. .thaw = mv_eh_thaw,
  491. .scr_read = mv5_scr_read,
  492. .scr_write = mv5_scr_write,
  493. .port_start = mv_port_start,
  494. .port_stop = mv_port_stop,
  495. };
  496. static const struct ata_port_operations mv6_ops = {
  497. .dev_config = mv6_dev_config,
  498. .tf_load = ata_tf_load,
  499. .tf_read = ata_tf_read,
  500. .check_status = ata_check_status,
  501. .exec_command = ata_exec_command,
  502. .dev_select = ata_std_dev_select,
  503. .cable_detect = ata_cable_sata,
  504. .qc_prep = mv_qc_prep,
  505. .qc_issue = mv_qc_issue,
  506. .data_xfer = ata_data_xfer,
  507. .irq_clear = mv_irq_clear,
  508. .irq_on = ata_irq_on,
  509. .error_handler = mv_error_handler,
  510. .freeze = mv_eh_freeze,
  511. .thaw = mv_eh_thaw,
  512. .qc_defer = ata_std_qc_defer,
  513. .scr_read = mv_scr_read,
  514. .scr_write = mv_scr_write,
  515. .port_start = mv_port_start,
  516. .port_stop = mv_port_stop,
  517. };
  518. static const struct ata_port_operations mv_iie_ops = {
  519. .tf_load = ata_tf_load,
  520. .tf_read = ata_tf_read,
  521. .check_status = ata_check_status,
  522. .exec_command = ata_exec_command,
  523. .dev_select = ata_std_dev_select,
  524. .cable_detect = ata_cable_sata,
  525. .qc_prep = mv_qc_prep_iie,
  526. .qc_issue = mv_qc_issue,
  527. .data_xfer = ata_data_xfer,
  528. .irq_clear = mv_irq_clear,
  529. .irq_on = ata_irq_on,
  530. .error_handler = mv_error_handler,
  531. .freeze = mv_eh_freeze,
  532. .thaw = mv_eh_thaw,
  533. .qc_defer = ata_std_qc_defer,
  534. .scr_read = mv_scr_read,
  535. .scr_write = mv_scr_write,
  536. .port_start = mv_port_start,
  537. .port_stop = mv_port_stop,
  538. };
  539. static const struct ata_port_info mv_port_info[] = {
  540. { /* chip_504x */
  541. .flags = MV_COMMON_FLAGS,
  542. .pio_mask = 0x1f, /* pio0-4 */
  543. .udma_mask = ATA_UDMA6,
  544. .port_ops = &mv5_ops,
  545. },
  546. { /* chip_508x */
  547. .flags = MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
  548. .pio_mask = 0x1f, /* pio0-4 */
  549. .udma_mask = ATA_UDMA6,
  550. .port_ops = &mv5_ops,
  551. },
  552. { /* chip_5080 */
  553. .flags = MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
  554. .pio_mask = 0x1f, /* pio0-4 */
  555. .udma_mask = ATA_UDMA6,
  556. .port_ops = &mv5_ops,
  557. },
  558. { /* chip_604x */
  559. .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  560. ATA_FLAG_NCQ,
  561. .pio_mask = 0x1f, /* pio0-4 */
  562. .udma_mask = ATA_UDMA6,
  563. .port_ops = &mv6_ops,
  564. },
  565. { /* chip_608x */
  566. .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  567. ATA_FLAG_NCQ | MV_FLAG_DUAL_HC,
  568. .pio_mask = 0x1f, /* pio0-4 */
  569. .udma_mask = ATA_UDMA6,
  570. .port_ops = &mv6_ops,
  571. },
  572. { /* chip_6042 */
  573. .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  574. ATA_FLAG_NCQ,
  575. .pio_mask = 0x1f, /* pio0-4 */
  576. .udma_mask = ATA_UDMA6,
  577. .port_ops = &mv_iie_ops,
  578. },
  579. { /* chip_7042 */
  580. .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  581. ATA_FLAG_NCQ,
  582. .pio_mask = 0x1f, /* pio0-4 */
  583. .udma_mask = ATA_UDMA6,
  584. .port_ops = &mv_iie_ops,
  585. },
  586. { /* chip_soc */
  587. .flags = MV_COMMON_FLAGS | MV_FLAG_SOC,
  588. .pio_mask = 0x1f, /* pio0-4 */
  589. .udma_mask = ATA_UDMA6,
  590. .port_ops = &mv_iie_ops,
  591. },
  592. };
  593. static const struct pci_device_id mv_pci_tbl[] = {
  594. { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
  595. { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
  596. { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
  597. { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
  598. /* RocketRAID 1740/174x have different identifiers */
  599. { PCI_VDEVICE(TTI, 0x1740), chip_508x },
  600. { PCI_VDEVICE(TTI, 0x1742), chip_508x },
  601. { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
  602. { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
  603. { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
  604. { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
  605. { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
  606. { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
  607. /* Adaptec 1430SA */
  608. { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
  609. /* Marvell 7042 support */
  610. { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
  611. /* Highpoint RocketRAID PCIe series */
  612. { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
  613. { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
  614. { } /* terminate list */
  615. };
  616. static const struct mv_hw_ops mv5xxx_ops = {
  617. .phy_errata = mv5_phy_errata,
  618. .enable_leds = mv5_enable_leds,
  619. .read_preamp = mv5_read_preamp,
  620. .reset_hc = mv5_reset_hc,
  621. .reset_flash = mv5_reset_flash,
  622. .reset_bus = mv5_reset_bus,
  623. };
  624. static const struct mv_hw_ops mv6xxx_ops = {
  625. .phy_errata = mv6_phy_errata,
  626. .enable_leds = mv6_enable_leds,
  627. .read_preamp = mv6_read_preamp,
  628. .reset_hc = mv6_reset_hc,
  629. .reset_flash = mv6_reset_flash,
  630. .reset_bus = mv_reset_pci_bus,
  631. };
  632. static const struct mv_hw_ops mv_soc_ops = {
  633. .phy_errata = mv6_phy_errata,
  634. .enable_leds = mv_soc_enable_leds,
  635. .read_preamp = mv_soc_read_preamp,
  636. .reset_hc = mv_soc_reset_hc,
  637. .reset_flash = mv_soc_reset_flash,
  638. .reset_bus = mv_soc_reset_bus,
  639. };
  640. /*
  641. * Functions
  642. */
  643. static inline void writelfl(unsigned long data, void __iomem *addr)
  644. {
  645. writel(data, addr);
  646. (void) readl(addr); /* flush to avoid PCI posted write */
  647. }
  648. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  649. {
  650. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  651. }
  652. static inline unsigned int mv_hc_from_port(unsigned int port)
  653. {
  654. return port >> MV_PORT_HC_SHIFT;
  655. }
  656. static inline unsigned int mv_hardport_from_port(unsigned int port)
  657. {
  658. return port & MV_PORT_MASK;
  659. }
  660. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  661. unsigned int port)
  662. {
  663. return mv_hc_base(base, mv_hc_from_port(port));
  664. }
  665. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  666. {
  667. return mv_hc_base_from_port(base, port) +
  668. MV_SATAHC_ARBTR_REG_SZ +
  669. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  670. }
  671. static inline void __iomem *mv_host_base(struct ata_host *host)
  672. {
  673. struct mv_host_priv *hpriv = host->private_data;
  674. return hpriv->base;
  675. }
  676. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  677. {
  678. return mv_port_base(mv_host_base(ap->host), ap->port_no);
  679. }
  680. static inline int mv_get_hc_count(unsigned long port_flags)
  681. {
  682. return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  683. }
  684. static void mv_irq_clear(struct ata_port *ap)
  685. {
  686. }
  687. static void mv_set_edma_ptrs(void __iomem *port_mmio,
  688. struct mv_host_priv *hpriv,
  689. struct mv_port_priv *pp)
  690. {
  691. u32 index;
  692. /*
  693. * initialize request queue
  694. */
  695. index = (pp->req_idx & MV_MAX_Q_DEPTH_MASK) << EDMA_REQ_Q_PTR_SHIFT;
  696. WARN_ON(pp->crqb_dma & 0x3ff);
  697. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  698. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
  699. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  700. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  701. writelfl((pp->crqb_dma & 0xffffffff) | index,
  702. port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  703. else
  704. writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  705. /*
  706. * initialize response queue
  707. */
  708. index = (pp->resp_idx & MV_MAX_Q_DEPTH_MASK) << EDMA_RSP_Q_PTR_SHIFT;
  709. WARN_ON(pp->crpb_dma & 0xff);
  710. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  711. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  712. writelfl((pp->crpb_dma & 0xffffffff) | index,
  713. port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  714. else
  715. writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  716. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
  717. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  718. }
  719. /**
  720. * mv_start_dma - Enable eDMA engine
  721. * @base: port base address
  722. * @pp: port private data
  723. *
  724. * Verify the local cache of the eDMA state is accurate with a
  725. * WARN_ON.
  726. *
  727. * LOCKING:
  728. * Inherited from caller.
  729. */
  730. static void mv_start_dma(struct ata_port *ap, void __iomem *port_mmio,
  731. struct mv_port_priv *pp, u8 protocol)
  732. {
  733. int want_ncq = (protocol == ATA_PROT_NCQ);
  734. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  735. int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
  736. if (want_ncq != using_ncq)
  737. __mv_stop_dma(ap);
  738. }
  739. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
  740. struct mv_host_priv *hpriv = ap->host->private_data;
  741. int hard_port = mv_hardport_from_port(ap->port_no);
  742. void __iomem *hc_mmio = mv_hc_base_from_port(
  743. ap->host->iomap[MV_PRIMARY_BAR], hard_port);
  744. u32 hc_irq_cause, ipending;
  745. /* clear EDMA event indicators, if any */
  746. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  747. /* clear EDMA interrupt indicator, if any */
  748. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  749. ipending = (DEV_IRQ << hard_port) |
  750. (CRPB_DMA_DONE << hard_port);
  751. if (hc_irq_cause & ipending) {
  752. writelfl(hc_irq_cause & ~ipending,
  753. hc_mmio + HC_IRQ_CAUSE_OFS);
  754. }
  755. mv_edma_cfg(pp, hpriv, port_mmio, want_ncq);
  756. /* clear FIS IRQ Cause */
  757. writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  758. mv_set_edma_ptrs(port_mmio, hpriv, pp);
  759. writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
  760. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  761. }
  762. WARN_ON(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
  763. }
  764. /**
  765. * __mv_stop_dma - Disable eDMA engine
  766. * @ap: ATA channel to manipulate
  767. *
  768. * Verify the local cache of the eDMA state is accurate with a
  769. * WARN_ON.
  770. *
  771. * LOCKING:
  772. * Inherited from caller.
  773. */
  774. static int __mv_stop_dma(struct ata_port *ap)
  775. {
  776. void __iomem *port_mmio = mv_ap_base(ap);
  777. struct mv_port_priv *pp = ap->private_data;
  778. u32 reg;
  779. int i, err = 0;
  780. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  781. /* Disable EDMA if active. The disable bit auto clears.
  782. */
  783. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  784. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  785. } else {
  786. WARN_ON(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS));
  787. }
  788. /* now properly wait for the eDMA to stop */
  789. for (i = 1000; i > 0; i--) {
  790. reg = readl(port_mmio + EDMA_CMD_OFS);
  791. if (!(reg & EDMA_EN))
  792. break;
  793. udelay(100);
  794. }
  795. if (reg & EDMA_EN) {
  796. ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
  797. err = -EIO;
  798. }
  799. return err;
  800. }
  801. static int mv_stop_dma(struct ata_port *ap)
  802. {
  803. unsigned long flags;
  804. int rc;
  805. spin_lock_irqsave(&ap->host->lock, flags);
  806. rc = __mv_stop_dma(ap);
  807. spin_unlock_irqrestore(&ap->host->lock, flags);
  808. return rc;
  809. }
  810. #ifdef ATA_DEBUG
  811. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  812. {
  813. int b, w;
  814. for (b = 0; b < bytes; ) {
  815. DPRINTK("%p: ", start + b);
  816. for (w = 0; b < bytes && w < 4; w++) {
  817. printk("%08x ", readl(start + b));
  818. b += sizeof(u32);
  819. }
  820. printk("\n");
  821. }
  822. }
  823. #endif
  824. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  825. {
  826. #ifdef ATA_DEBUG
  827. int b, w;
  828. u32 dw;
  829. for (b = 0; b < bytes; ) {
  830. DPRINTK("%02x: ", b);
  831. for (w = 0; b < bytes && w < 4; w++) {
  832. (void) pci_read_config_dword(pdev, b, &dw);
  833. printk("%08x ", dw);
  834. b += sizeof(u32);
  835. }
  836. printk("\n");
  837. }
  838. #endif
  839. }
  840. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  841. struct pci_dev *pdev)
  842. {
  843. #ifdef ATA_DEBUG
  844. void __iomem *hc_base = mv_hc_base(mmio_base,
  845. port >> MV_PORT_HC_SHIFT);
  846. void __iomem *port_base;
  847. int start_port, num_ports, p, start_hc, num_hcs, hc;
  848. if (0 > port) {
  849. start_hc = start_port = 0;
  850. num_ports = 8; /* shld be benign for 4 port devs */
  851. num_hcs = 2;
  852. } else {
  853. start_hc = port >> MV_PORT_HC_SHIFT;
  854. start_port = port;
  855. num_ports = num_hcs = 1;
  856. }
  857. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  858. num_ports > 1 ? num_ports - 1 : start_port);
  859. if (NULL != pdev) {
  860. DPRINTK("PCI config space regs:\n");
  861. mv_dump_pci_cfg(pdev, 0x68);
  862. }
  863. DPRINTK("PCI regs:\n");
  864. mv_dump_mem(mmio_base+0xc00, 0x3c);
  865. mv_dump_mem(mmio_base+0xd00, 0x34);
  866. mv_dump_mem(mmio_base+0xf00, 0x4);
  867. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  868. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  869. hc_base = mv_hc_base(mmio_base, hc);
  870. DPRINTK("HC regs (HC %i):\n", hc);
  871. mv_dump_mem(hc_base, 0x1c);
  872. }
  873. for (p = start_port; p < start_port + num_ports; p++) {
  874. port_base = mv_port_base(mmio_base, p);
  875. DPRINTK("EDMA regs (port %i):\n", p);
  876. mv_dump_mem(port_base, 0x54);
  877. DPRINTK("SATA regs (port %i):\n", p);
  878. mv_dump_mem(port_base+0x300, 0x60);
  879. }
  880. #endif
  881. }
  882. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  883. {
  884. unsigned int ofs;
  885. switch (sc_reg_in) {
  886. case SCR_STATUS:
  887. case SCR_CONTROL:
  888. case SCR_ERROR:
  889. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  890. break;
  891. case SCR_ACTIVE:
  892. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  893. break;
  894. default:
  895. ofs = 0xffffffffU;
  896. break;
  897. }
  898. return ofs;
  899. }
  900. static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
  901. {
  902. unsigned int ofs = mv_scr_offset(sc_reg_in);
  903. if (ofs != 0xffffffffU) {
  904. *val = readl(mv_ap_base(ap) + ofs);
  905. return 0;
  906. } else
  907. return -EINVAL;
  908. }
  909. static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  910. {
  911. unsigned int ofs = mv_scr_offset(sc_reg_in);
  912. if (ofs != 0xffffffffU) {
  913. writelfl(val, mv_ap_base(ap) + ofs);
  914. return 0;
  915. } else
  916. return -EINVAL;
  917. }
  918. static void mv6_dev_config(struct ata_device *adev)
  919. {
  920. /*
  921. * We don't have hob_nsect when doing NCQ commands on Gen-II.
  922. * See mv_qc_prep() for more info.
  923. */
  924. if (adev->flags & ATA_DFLAG_NCQ)
  925. if (adev->max_sectors > ATA_MAX_SECTORS)
  926. adev->max_sectors = ATA_MAX_SECTORS;
  927. }
  928. static void mv_edma_cfg(struct mv_port_priv *pp, struct mv_host_priv *hpriv,
  929. void __iomem *port_mmio, int want_ncq)
  930. {
  931. u32 cfg;
  932. /* set up non-NCQ EDMA configuration */
  933. cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
  934. if (IS_GEN_I(hpriv))
  935. cfg |= (1 << 8); /* enab config burst size mask */
  936. else if (IS_GEN_II(hpriv))
  937. cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
  938. else if (IS_GEN_IIE(hpriv)) {
  939. cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
  940. cfg |= (1 << 22); /* enab 4-entry host queue cache */
  941. cfg |= (1 << 18); /* enab early completion */
  942. cfg |= (1 << 17); /* enab cut-through (dis stor&forwrd) */
  943. }
  944. if (want_ncq) {
  945. cfg |= EDMA_CFG_NCQ;
  946. pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
  947. } else
  948. pp->pp_flags &= ~MV_PP_FLAG_NCQ_EN;
  949. writelfl(cfg, port_mmio + EDMA_CFG_OFS);
  950. }
  951. static void mv_port_free_dma_mem(struct ata_port *ap)
  952. {
  953. struct mv_host_priv *hpriv = ap->host->private_data;
  954. struct mv_port_priv *pp = ap->private_data;
  955. int tag;
  956. if (pp->crqb) {
  957. dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
  958. pp->crqb = NULL;
  959. }
  960. if (pp->crpb) {
  961. dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
  962. pp->crpb = NULL;
  963. }
  964. /*
  965. * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
  966. * For later hardware, we have one unique sg_tbl per NCQ tag.
  967. */
  968. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  969. if (pp->sg_tbl[tag]) {
  970. if (tag == 0 || !IS_GEN_I(hpriv))
  971. dma_pool_free(hpriv->sg_tbl_pool,
  972. pp->sg_tbl[tag],
  973. pp->sg_tbl_dma[tag]);
  974. pp->sg_tbl[tag] = NULL;
  975. }
  976. }
  977. }
  978. /**
  979. * mv_port_start - Port specific init/start routine.
  980. * @ap: ATA channel to manipulate
  981. *
  982. * Allocate and point to DMA memory, init port private memory,
  983. * zero indices.
  984. *
  985. * LOCKING:
  986. * Inherited from caller.
  987. */
  988. static int mv_port_start(struct ata_port *ap)
  989. {
  990. struct device *dev = ap->host->dev;
  991. struct mv_host_priv *hpriv = ap->host->private_data;
  992. struct mv_port_priv *pp;
  993. void __iomem *port_mmio = mv_ap_base(ap);
  994. unsigned long flags;
  995. int tag, rc;
  996. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  997. if (!pp)
  998. return -ENOMEM;
  999. ap->private_data = pp;
  1000. rc = ata_pad_alloc(ap, dev);
  1001. if (rc)
  1002. return rc;
  1003. pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
  1004. if (!pp->crqb)
  1005. return -ENOMEM;
  1006. memset(pp->crqb, 0, MV_CRQB_Q_SZ);
  1007. pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
  1008. if (!pp->crpb)
  1009. goto out_port_free_dma_mem;
  1010. memset(pp->crpb, 0, MV_CRPB_Q_SZ);
  1011. /*
  1012. * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
  1013. * For later hardware, we need one unique sg_tbl per NCQ tag.
  1014. */
  1015. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1016. if (tag == 0 || !IS_GEN_I(hpriv)) {
  1017. pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
  1018. GFP_KERNEL, &pp->sg_tbl_dma[tag]);
  1019. if (!pp->sg_tbl[tag])
  1020. goto out_port_free_dma_mem;
  1021. } else {
  1022. pp->sg_tbl[tag] = pp->sg_tbl[0];
  1023. pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
  1024. }
  1025. }
  1026. spin_lock_irqsave(&ap->host->lock, flags);
  1027. mv_edma_cfg(pp, hpriv, port_mmio, 0);
  1028. mv_set_edma_ptrs(port_mmio, hpriv, pp);
  1029. spin_unlock_irqrestore(&ap->host->lock, flags);
  1030. /* Don't turn on EDMA here...do it before DMA commands only. Else
  1031. * we'll be unable to send non-data, PIO, etc due to restricted access
  1032. * to shadow regs.
  1033. */
  1034. return 0;
  1035. out_port_free_dma_mem:
  1036. mv_port_free_dma_mem(ap);
  1037. return -ENOMEM;
  1038. }
  1039. /**
  1040. * mv_port_stop - Port specific cleanup/stop routine.
  1041. * @ap: ATA channel to manipulate
  1042. *
  1043. * Stop DMA, cleanup port memory.
  1044. *
  1045. * LOCKING:
  1046. * This routine uses the host lock to protect the DMA stop.
  1047. */
  1048. static void mv_port_stop(struct ata_port *ap)
  1049. {
  1050. mv_stop_dma(ap);
  1051. mv_port_free_dma_mem(ap);
  1052. }
  1053. /**
  1054. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  1055. * @qc: queued command whose SG list to source from
  1056. *
  1057. * Populate the SG list and mark the last entry.
  1058. *
  1059. * LOCKING:
  1060. * Inherited from caller.
  1061. */
  1062. static void mv_fill_sg(struct ata_queued_cmd *qc)
  1063. {
  1064. struct mv_port_priv *pp = qc->ap->private_data;
  1065. struct scatterlist *sg;
  1066. struct mv_sg *mv_sg, *last_sg = NULL;
  1067. unsigned int si;
  1068. mv_sg = pp->sg_tbl[qc->tag];
  1069. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1070. dma_addr_t addr = sg_dma_address(sg);
  1071. u32 sg_len = sg_dma_len(sg);
  1072. while (sg_len) {
  1073. u32 offset = addr & 0xffff;
  1074. u32 len = sg_len;
  1075. if ((offset + sg_len > 0x10000))
  1076. len = 0x10000 - offset;
  1077. mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1078. mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1079. mv_sg->flags_size = cpu_to_le32(len & 0xffff);
  1080. sg_len -= len;
  1081. addr += len;
  1082. last_sg = mv_sg;
  1083. mv_sg++;
  1084. }
  1085. }
  1086. if (likely(last_sg))
  1087. last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  1088. }
  1089. static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
  1090. {
  1091. u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  1092. (last ? CRQB_CMD_LAST : 0);
  1093. *cmdw = cpu_to_le16(tmp);
  1094. }
  1095. /**
  1096. * mv_qc_prep - Host specific command preparation.
  1097. * @qc: queued command to prepare
  1098. *
  1099. * This routine simply redirects to the general purpose routine
  1100. * if command is not DMA. Else, it handles prep of the CRQB
  1101. * (command request block), does some sanity checking, and calls
  1102. * the SG load routine.
  1103. *
  1104. * LOCKING:
  1105. * Inherited from caller.
  1106. */
  1107. static void mv_qc_prep(struct ata_queued_cmd *qc)
  1108. {
  1109. struct ata_port *ap = qc->ap;
  1110. struct mv_port_priv *pp = ap->private_data;
  1111. __le16 *cw;
  1112. struct ata_taskfile *tf;
  1113. u16 flags = 0;
  1114. unsigned in_index;
  1115. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1116. (qc->tf.protocol != ATA_PROT_NCQ))
  1117. return;
  1118. /* Fill in command request block
  1119. */
  1120. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1121. flags |= CRQB_FLAG_READ;
  1122. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1123. flags |= qc->tag << CRQB_TAG_SHIFT;
  1124. /* get current queue index from software */
  1125. in_index = pp->req_idx & MV_MAX_Q_DEPTH_MASK;
  1126. pp->crqb[in_index].sg_addr =
  1127. cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1128. pp->crqb[in_index].sg_addr_hi =
  1129. cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1130. pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
  1131. cw = &pp->crqb[in_index].ata_cmd[0];
  1132. tf = &qc->tf;
  1133. /* Sadly, the CRQB cannot accomodate all registers--there are
  1134. * only 11 bytes...so we must pick and choose required
  1135. * registers based on the command. So, we drop feature and
  1136. * hob_feature for [RW] DMA commands, but they are needed for
  1137. * NCQ. NCQ will drop hob_nsect.
  1138. */
  1139. switch (tf->command) {
  1140. case ATA_CMD_READ:
  1141. case ATA_CMD_READ_EXT:
  1142. case ATA_CMD_WRITE:
  1143. case ATA_CMD_WRITE_EXT:
  1144. case ATA_CMD_WRITE_FUA_EXT:
  1145. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  1146. break;
  1147. case ATA_CMD_FPDMA_READ:
  1148. case ATA_CMD_FPDMA_WRITE:
  1149. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  1150. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  1151. break;
  1152. default:
  1153. /* The only other commands EDMA supports in non-queued and
  1154. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  1155. * of which are defined/used by Linux. If we get here, this
  1156. * driver needs work.
  1157. *
  1158. * FIXME: modify libata to give qc_prep a return value and
  1159. * return error here.
  1160. */
  1161. BUG_ON(tf->command);
  1162. break;
  1163. }
  1164. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  1165. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  1166. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  1167. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  1168. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  1169. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  1170. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  1171. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  1172. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  1173. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1174. return;
  1175. mv_fill_sg(qc);
  1176. }
  1177. /**
  1178. * mv_qc_prep_iie - Host specific command preparation.
  1179. * @qc: queued command to prepare
  1180. *
  1181. * This routine simply redirects to the general purpose routine
  1182. * if command is not DMA. Else, it handles prep of the CRQB
  1183. * (command request block), does some sanity checking, and calls
  1184. * the SG load routine.
  1185. *
  1186. * LOCKING:
  1187. * Inherited from caller.
  1188. */
  1189. static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
  1190. {
  1191. struct ata_port *ap = qc->ap;
  1192. struct mv_port_priv *pp = ap->private_data;
  1193. struct mv_crqb_iie *crqb;
  1194. struct ata_taskfile *tf;
  1195. unsigned in_index;
  1196. u32 flags = 0;
  1197. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1198. (qc->tf.protocol != ATA_PROT_NCQ))
  1199. return;
  1200. /* Fill in Gen IIE command request block
  1201. */
  1202. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1203. flags |= CRQB_FLAG_READ;
  1204. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1205. flags |= qc->tag << CRQB_TAG_SHIFT;
  1206. flags |= qc->tag << CRQB_HOSTQ_SHIFT;
  1207. /* get current queue index from software */
  1208. in_index = pp->req_idx & MV_MAX_Q_DEPTH_MASK;
  1209. crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
  1210. crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1211. crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1212. crqb->flags = cpu_to_le32(flags);
  1213. tf = &qc->tf;
  1214. crqb->ata_cmd[0] = cpu_to_le32(
  1215. (tf->command << 16) |
  1216. (tf->feature << 24)
  1217. );
  1218. crqb->ata_cmd[1] = cpu_to_le32(
  1219. (tf->lbal << 0) |
  1220. (tf->lbam << 8) |
  1221. (tf->lbah << 16) |
  1222. (tf->device << 24)
  1223. );
  1224. crqb->ata_cmd[2] = cpu_to_le32(
  1225. (tf->hob_lbal << 0) |
  1226. (tf->hob_lbam << 8) |
  1227. (tf->hob_lbah << 16) |
  1228. (tf->hob_feature << 24)
  1229. );
  1230. crqb->ata_cmd[3] = cpu_to_le32(
  1231. (tf->nsect << 0) |
  1232. (tf->hob_nsect << 8)
  1233. );
  1234. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1235. return;
  1236. mv_fill_sg(qc);
  1237. }
  1238. /**
  1239. * mv_qc_issue - Initiate a command to the host
  1240. * @qc: queued command to start
  1241. *
  1242. * This routine simply redirects to the general purpose routine
  1243. * if command is not DMA. Else, it sanity checks our local
  1244. * caches of the request producer/consumer indices then enables
  1245. * DMA and bumps the request producer index.
  1246. *
  1247. * LOCKING:
  1248. * Inherited from caller.
  1249. */
  1250. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  1251. {
  1252. struct ata_port *ap = qc->ap;
  1253. void __iomem *port_mmio = mv_ap_base(ap);
  1254. struct mv_port_priv *pp = ap->private_data;
  1255. u32 in_index;
  1256. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1257. (qc->tf.protocol != ATA_PROT_NCQ)) {
  1258. /* We're about to send a non-EDMA capable command to the
  1259. * port. Turn off EDMA so there won't be problems accessing
  1260. * shadow block, etc registers.
  1261. */
  1262. __mv_stop_dma(ap);
  1263. return ata_qc_issue_prot(qc);
  1264. }
  1265. mv_start_dma(ap, port_mmio, pp, qc->tf.protocol);
  1266. pp->req_idx++;
  1267. in_index = (pp->req_idx & MV_MAX_Q_DEPTH_MASK) << EDMA_REQ_Q_PTR_SHIFT;
  1268. /* and write the request in pointer to kick the EDMA to life */
  1269. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
  1270. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1271. return 0;
  1272. }
  1273. /**
  1274. * mv_err_intr - Handle error interrupts on the port
  1275. * @ap: ATA channel to manipulate
  1276. * @reset_allowed: bool: 0 == don't trigger from reset here
  1277. *
  1278. * In most cases, just clear the interrupt and move on. However,
  1279. * some cases require an eDMA reset, which is done right before
  1280. * the COMRESET in mv_phy_reset(). The SERR case requires a
  1281. * clear of pending errors in the SATA SERROR register. Finally,
  1282. * if the port disabled DMA, update our cached copy to match.
  1283. *
  1284. * LOCKING:
  1285. * Inherited from caller.
  1286. */
  1287. static void mv_err_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
  1288. {
  1289. void __iomem *port_mmio = mv_ap_base(ap);
  1290. u32 edma_err_cause, eh_freeze_mask, serr = 0;
  1291. struct mv_port_priv *pp = ap->private_data;
  1292. struct mv_host_priv *hpriv = ap->host->private_data;
  1293. unsigned int edma_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
  1294. unsigned int action = 0, err_mask = 0;
  1295. struct ata_eh_info *ehi = &ap->link.eh_info;
  1296. ata_ehi_clear_desc(ehi);
  1297. if (!edma_enabled) {
  1298. /* just a guess: do we need to do this? should we
  1299. * expand this, and do it in all cases?
  1300. */
  1301. sata_scr_read(&ap->link, SCR_ERROR, &serr);
  1302. sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
  1303. }
  1304. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1305. ata_ehi_push_desc(ehi, "edma_err 0x%08x", edma_err_cause);
  1306. /*
  1307. * all generations share these EDMA error cause bits
  1308. */
  1309. if (edma_err_cause & EDMA_ERR_DEV)
  1310. err_mask |= AC_ERR_DEV;
  1311. if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  1312. EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
  1313. EDMA_ERR_INTRL_PAR)) {
  1314. err_mask |= AC_ERR_ATA_BUS;
  1315. action |= ATA_EH_HARDRESET;
  1316. ata_ehi_push_desc(ehi, "parity error");
  1317. }
  1318. if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
  1319. ata_ehi_hotplugged(ehi);
  1320. ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
  1321. "dev disconnect" : "dev connect");
  1322. action |= ATA_EH_HARDRESET;
  1323. }
  1324. if (IS_GEN_I(hpriv)) {
  1325. eh_freeze_mask = EDMA_EH_FREEZE_5;
  1326. if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
  1327. struct mv_port_priv *pp = ap->private_data;
  1328. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1329. ata_ehi_push_desc(ehi, "EDMA self-disable");
  1330. }
  1331. } else {
  1332. eh_freeze_mask = EDMA_EH_FREEZE;
  1333. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  1334. struct mv_port_priv *pp = ap->private_data;
  1335. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1336. ata_ehi_push_desc(ehi, "EDMA self-disable");
  1337. }
  1338. if (edma_err_cause & EDMA_ERR_SERR) {
  1339. sata_scr_read(&ap->link, SCR_ERROR, &serr);
  1340. sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
  1341. err_mask = AC_ERR_ATA_BUS;
  1342. action |= ATA_EH_HARDRESET;
  1343. }
  1344. }
  1345. /* Clear EDMA now that SERR cleanup done */
  1346. writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1347. if (!err_mask) {
  1348. err_mask = AC_ERR_OTHER;
  1349. action |= ATA_EH_HARDRESET;
  1350. }
  1351. ehi->serror |= serr;
  1352. ehi->action |= action;
  1353. if (qc)
  1354. qc->err_mask |= err_mask;
  1355. else
  1356. ehi->err_mask |= err_mask;
  1357. if (edma_err_cause & eh_freeze_mask)
  1358. ata_port_freeze(ap);
  1359. else
  1360. ata_port_abort(ap);
  1361. }
  1362. static void mv_intr_pio(struct ata_port *ap)
  1363. {
  1364. struct ata_queued_cmd *qc;
  1365. u8 ata_status;
  1366. /* ignore spurious intr if drive still BUSY */
  1367. ata_status = readb(ap->ioaddr.status_addr);
  1368. if (unlikely(ata_status & ATA_BUSY))
  1369. return;
  1370. /* get active ATA command */
  1371. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1372. if (unlikely(!qc)) /* no active tag */
  1373. return;
  1374. if (qc->tf.flags & ATA_TFLAG_POLLING) /* polling; we don't own qc */
  1375. return;
  1376. /* and finally, complete the ATA command */
  1377. qc->err_mask |= ac_err_mask(ata_status);
  1378. ata_qc_complete(qc);
  1379. }
  1380. static void mv_intr_edma(struct ata_port *ap)
  1381. {
  1382. void __iomem *port_mmio = mv_ap_base(ap);
  1383. struct mv_host_priv *hpriv = ap->host->private_data;
  1384. struct mv_port_priv *pp = ap->private_data;
  1385. struct ata_queued_cmd *qc;
  1386. u32 out_index, in_index;
  1387. bool work_done = false;
  1388. /* get h/w response queue pointer */
  1389. in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
  1390. >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1391. while (1) {
  1392. u16 status;
  1393. unsigned int tag;
  1394. /* get s/w response queue last-read pointer, and compare */
  1395. out_index = pp->resp_idx & MV_MAX_Q_DEPTH_MASK;
  1396. if (in_index == out_index)
  1397. break;
  1398. /* 50xx: get active ATA command */
  1399. if (IS_GEN_I(hpriv))
  1400. tag = ap->link.active_tag;
  1401. /* Gen II/IIE: get active ATA command via tag, to enable
  1402. * support for queueing. this works transparently for
  1403. * queued and non-queued modes.
  1404. */
  1405. else
  1406. tag = le16_to_cpu(pp->crpb[out_index].id) & 0x1f;
  1407. qc = ata_qc_from_tag(ap, tag);
  1408. /* For non-NCQ mode, the lower 8 bits of status
  1409. * are from EDMA_ERR_IRQ_CAUSE_OFS,
  1410. * which should be zero if all went well.
  1411. */
  1412. status = le16_to_cpu(pp->crpb[out_index].flags);
  1413. if ((status & 0xff) && !(pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
  1414. mv_err_intr(ap, qc);
  1415. return;
  1416. }
  1417. /* and finally, complete the ATA command */
  1418. if (qc) {
  1419. qc->err_mask |=
  1420. ac_err_mask(status >> CRPB_FLAG_STATUS_SHIFT);
  1421. ata_qc_complete(qc);
  1422. }
  1423. /* advance software response queue pointer, to
  1424. * indicate (after the loop completes) to hardware
  1425. * that we have consumed a response queue entry.
  1426. */
  1427. work_done = true;
  1428. pp->resp_idx++;
  1429. }
  1430. if (work_done)
  1431. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
  1432. (out_index << EDMA_RSP_Q_PTR_SHIFT),
  1433. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1434. }
  1435. /**
  1436. * mv_host_intr - Handle all interrupts on the given host controller
  1437. * @host: host specific structure
  1438. * @relevant: port error bits relevant to this host controller
  1439. * @hc: which host controller we're to look at
  1440. *
  1441. * Read then write clear the HC interrupt status then walk each
  1442. * port connected to the HC and see if it needs servicing. Port
  1443. * success ints are reported in the HC interrupt status reg, the
  1444. * port error ints are reported in the higher level main
  1445. * interrupt status register and thus are passed in via the
  1446. * 'relevant' argument.
  1447. *
  1448. * LOCKING:
  1449. * Inherited from caller.
  1450. */
  1451. static void mv_host_intr(struct ata_host *host, u32 relevant, unsigned int hc)
  1452. {
  1453. struct mv_host_priv *hpriv = host->private_data;
  1454. void __iomem *mmio = hpriv->base;
  1455. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1456. u32 hc_irq_cause;
  1457. int port, port0, last_port;
  1458. if (hc == 0)
  1459. port0 = 0;
  1460. else
  1461. port0 = MV_PORTS_PER_HC;
  1462. if (HAS_PCI(host))
  1463. last_port = port0 + MV_PORTS_PER_HC;
  1464. else
  1465. last_port = port0 + hpriv->n_ports;
  1466. /* we'll need the HC success int register in most cases */
  1467. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  1468. if (!hc_irq_cause)
  1469. return;
  1470. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  1471. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  1472. hc, relevant, hc_irq_cause);
  1473. for (port = port0; port < last_port; port++) {
  1474. struct ata_port *ap = host->ports[port];
  1475. struct mv_port_priv *pp;
  1476. int have_err_bits, hard_port, shift;
  1477. if ((!ap) || (ap->flags & ATA_FLAG_DISABLED))
  1478. continue;
  1479. pp = ap->private_data;
  1480. shift = port << 1; /* (port * 2) */
  1481. if (port >= MV_PORTS_PER_HC) {
  1482. shift++; /* skip bit 8 in the HC Main IRQ reg */
  1483. }
  1484. have_err_bits = ((PORT0_ERR << shift) & relevant);
  1485. if (unlikely(have_err_bits)) {
  1486. struct ata_queued_cmd *qc;
  1487. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1488. if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
  1489. continue;
  1490. mv_err_intr(ap, qc);
  1491. continue;
  1492. }
  1493. hard_port = mv_hardport_from_port(port); /* range 0..3 */
  1494. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  1495. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause)
  1496. mv_intr_edma(ap);
  1497. } else {
  1498. if ((DEV_IRQ << hard_port) & hc_irq_cause)
  1499. mv_intr_pio(ap);
  1500. }
  1501. }
  1502. VPRINTK("EXIT\n");
  1503. }
  1504. static void mv_pci_error(struct ata_host *host, void __iomem *mmio)
  1505. {
  1506. struct mv_host_priv *hpriv = host->private_data;
  1507. struct ata_port *ap;
  1508. struct ata_queued_cmd *qc;
  1509. struct ata_eh_info *ehi;
  1510. unsigned int i, err_mask, printed = 0;
  1511. u32 err_cause;
  1512. err_cause = readl(mmio + hpriv->irq_cause_ofs);
  1513. dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
  1514. err_cause);
  1515. DPRINTK("All regs @ PCI error\n");
  1516. mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
  1517. writelfl(0, mmio + hpriv->irq_cause_ofs);
  1518. for (i = 0; i < host->n_ports; i++) {
  1519. ap = host->ports[i];
  1520. if (!ata_link_offline(&ap->link)) {
  1521. ehi = &ap->link.eh_info;
  1522. ata_ehi_clear_desc(ehi);
  1523. if (!printed++)
  1524. ata_ehi_push_desc(ehi,
  1525. "PCI err cause 0x%08x", err_cause);
  1526. err_mask = AC_ERR_HOST_BUS;
  1527. ehi->action = ATA_EH_HARDRESET;
  1528. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1529. if (qc)
  1530. qc->err_mask |= err_mask;
  1531. else
  1532. ehi->err_mask |= err_mask;
  1533. ata_port_freeze(ap);
  1534. }
  1535. }
  1536. }
  1537. /**
  1538. * mv_interrupt - Main interrupt event handler
  1539. * @irq: unused
  1540. * @dev_instance: private data; in this case the host structure
  1541. *
  1542. * Read the read only register to determine if any host
  1543. * controllers have pending interrupts. If so, call lower level
  1544. * routine to handle. Also check for PCI errors which are only
  1545. * reported here.
  1546. *
  1547. * LOCKING:
  1548. * This routine holds the host lock while processing pending
  1549. * interrupts.
  1550. */
  1551. static irqreturn_t mv_interrupt(int irq, void *dev_instance)
  1552. {
  1553. struct ata_host *host = dev_instance;
  1554. struct mv_host_priv *hpriv = host->private_data;
  1555. unsigned int hc, handled = 0, n_hcs;
  1556. void __iomem *mmio = hpriv->base;
  1557. u32 irq_stat, irq_mask;
  1558. spin_lock(&host->lock);
  1559. irq_stat = readl(hpriv->main_cause_reg_addr);
  1560. irq_mask = readl(hpriv->main_mask_reg_addr);
  1561. /* check the cases where we either have nothing pending or have read
  1562. * a bogus register value which can indicate HW removal or PCI fault
  1563. */
  1564. if (!(irq_stat & irq_mask) || (0xffffffffU == irq_stat))
  1565. goto out_unlock;
  1566. n_hcs = mv_get_hc_count(host->ports[0]->flags);
  1567. if (unlikely((irq_stat & PCI_ERR) && HAS_PCI(host))) {
  1568. mv_pci_error(host, mmio);
  1569. handled = 1;
  1570. goto out_unlock; /* skip all other HC irq handling */
  1571. }
  1572. for (hc = 0; hc < n_hcs; hc++) {
  1573. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1574. if (relevant) {
  1575. mv_host_intr(host, relevant, hc);
  1576. handled = 1;
  1577. }
  1578. }
  1579. out_unlock:
  1580. spin_unlock(&host->lock);
  1581. return IRQ_RETVAL(handled);
  1582. }
  1583. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  1584. {
  1585. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  1586. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  1587. return hc_mmio + ofs;
  1588. }
  1589. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1590. {
  1591. unsigned int ofs;
  1592. switch (sc_reg_in) {
  1593. case SCR_STATUS:
  1594. case SCR_ERROR:
  1595. case SCR_CONTROL:
  1596. ofs = sc_reg_in * sizeof(u32);
  1597. break;
  1598. default:
  1599. ofs = 0xffffffffU;
  1600. break;
  1601. }
  1602. return ofs;
  1603. }
  1604. static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
  1605. {
  1606. struct mv_host_priv *hpriv = ap->host->private_data;
  1607. void __iomem *mmio = hpriv->base;
  1608. void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
  1609. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1610. if (ofs != 0xffffffffU) {
  1611. *val = readl(addr + ofs);
  1612. return 0;
  1613. } else
  1614. return -EINVAL;
  1615. }
  1616. static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1617. {
  1618. struct mv_host_priv *hpriv = ap->host->private_data;
  1619. void __iomem *mmio = hpriv->base;
  1620. void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
  1621. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1622. if (ofs != 0xffffffffU) {
  1623. writelfl(val, addr + ofs);
  1624. return 0;
  1625. } else
  1626. return -EINVAL;
  1627. }
  1628. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
  1629. {
  1630. struct pci_dev *pdev = to_pci_dev(host->dev);
  1631. int early_5080;
  1632. early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
  1633. if (!early_5080) {
  1634. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1635. tmp |= (1 << 0);
  1636. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1637. }
  1638. mv_reset_pci_bus(host, mmio);
  1639. }
  1640. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1641. {
  1642. writel(0x0fcfffff, mmio + MV_FLASH_CTL);
  1643. }
  1644. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  1645. void __iomem *mmio)
  1646. {
  1647. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  1648. u32 tmp;
  1649. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1650. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  1651. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  1652. }
  1653. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1654. {
  1655. u32 tmp;
  1656. writel(0, mmio + MV_GPIO_PORT_CTL);
  1657. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  1658. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1659. tmp |= ~(1 << 0);
  1660. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1661. }
  1662. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1663. unsigned int port)
  1664. {
  1665. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  1666. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  1667. u32 tmp;
  1668. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  1669. if (fix_apm_sq) {
  1670. tmp = readl(phy_mmio + MV5_LT_MODE);
  1671. tmp |= (1 << 19);
  1672. writel(tmp, phy_mmio + MV5_LT_MODE);
  1673. tmp = readl(phy_mmio + MV5_PHY_CTL);
  1674. tmp &= ~0x3;
  1675. tmp |= 0x1;
  1676. writel(tmp, phy_mmio + MV5_PHY_CTL);
  1677. }
  1678. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1679. tmp &= ~mask;
  1680. tmp |= hpriv->signal[port].pre;
  1681. tmp |= hpriv->signal[port].amps;
  1682. writel(tmp, phy_mmio + MV5_PHY_MODE);
  1683. }
  1684. #undef ZERO
  1685. #define ZERO(reg) writel(0, port_mmio + (reg))
  1686. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  1687. unsigned int port)
  1688. {
  1689. void __iomem *port_mmio = mv_port_base(mmio, port);
  1690. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1691. mv_channel_reset(hpriv, mmio, port);
  1692. ZERO(0x028); /* command */
  1693. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  1694. ZERO(0x004); /* timer */
  1695. ZERO(0x008); /* irq err cause */
  1696. ZERO(0x00c); /* irq err mask */
  1697. ZERO(0x010); /* rq bah */
  1698. ZERO(0x014); /* rq inp */
  1699. ZERO(0x018); /* rq outp */
  1700. ZERO(0x01c); /* respq bah */
  1701. ZERO(0x024); /* respq outp */
  1702. ZERO(0x020); /* respq inp */
  1703. ZERO(0x02c); /* test control */
  1704. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1705. }
  1706. #undef ZERO
  1707. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1708. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1709. unsigned int hc)
  1710. {
  1711. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1712. u32 tmp;
  1713. ZERO(0x00c);
  1714. ZERO(0x010);
  1715. ZERO(0x014);
  1716. ZERO(0x018);
  1717. tmp = readl(hc_mmio + 0x20);
  1718. tmp &= 0x1c1c1c1c;
  1719. tmp |= 0x03030303;
  1720. writel(tmp, hc_mmio + 0x20);
  1721. }
  1722. #undef ZERO
  1723. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1724. unsigned int n_hc)
  1725. {
  1726. unsigned int hc, port;
  1727. for (hc = 0; hc < n_hc; hc++) {
  1728. for (port = 0; port < MV_PORTS_PER_HC; port++)
  1729. mv5_reset_hc_port(hpriv, mmio,
  1730. (hc * MV_PORTS_PER_HC) + port);
  1731. mv5_reset_one_hc(hpriv, mmio, hc);
  1732. }
  1733. return 0;
  1734. }
  1735. #undef ZERO
  1736. #define ZERO(reg) writel(0, mmio + (reg))
  1737. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
  1738. {
  1739. struct mv_host_priv *hpriv = host->private_data;
  1740. u32 tmp;
  1741. tmp = readl(mmio + MV_PCI_MODE);
  1742. tmp &= 0xff00ffff;
  1743. writel(tmp, mmio + MV_PCI_MODE);
  1744. ZERO(MV_PCI_DISC_TIMER);
  1745. ZERO(MV_PCI_MSI_TRIGGER);
  1746. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  1747. ZERO(HC_MAIN_IRQ_MASK_OFS);
  1748. ZERO(MV_PCI_SERR_MASK);
  1749. ZERO(hpriv->irq_cause_ofs);
  1750. ZERO(hpriv->irq_mask_ofs);
  1751. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  1752. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  1753. ZERO(MV_PCI_ERR_ATTRIBUTE);
  1754. ZERO(MV_PCI_ERR_COMMAND);
  1755. }
  1756. #undef ZERO
  1757. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1758. {
  1759. u32 tmp;
  1760. mv5_reset_flash(hpriv, mmio);
  1761. tmp = readl(mmio + MV_GPIO_PORT_CTL);
  1762. tmp &= 0x3;
  1763. tmp |= (1 << 5) | (1 << 6);
  1764. writel(tmp, mmio + MV_GPIO_PORT_CTL);
  1765. }
  1766. /**
  1767. * mv6_reset_hc - Perform the 6xxx global soft reset
  1768. * @mmio: base address of the HBA
  1769. *
  1770. * This routine only applies to 6xxx parts.
  1771. *
  1772. * LOCKING:
  1773. * Inherited from caller.
  1774. */
  1775. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1776. unsigned int n_hc)
  1777. {
  1778. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  1779. int i, rc = 0;
  1780. u32 t;
  1781. /* Following procedure defined in PCI "main command and status
  1782. * register" table.
  1783. */
  1784. t = readl(reg);
  1785. writel(t | STOP_PCI_MASTER, reg);
  1786. for (i = 0; i < 1000; i++) {
  1787. udelay(1);
  1788. t = readl(reg);
  1789. if (PCI_MASTER_EMPTY & t)
  1790. break;
  1791. }
  1792. if (!(PCI_MASTER_EMPTY & t)) {
  1793. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  1794. rc = 1;
  1795. goto done;
  1796. }
  1797. /* set reset */
  1798. i = 5;
  1799. do {
  1800. writel(t | GLOB_SFT_RST, reg);
  1801. t = readl(reg);
  1802. udelay(1);
  1803. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  1804. if (!(GLOB_SFT_RST & t)) {
  1805. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  1806. rc = 1;
  1807. goto done;
  1808. }
  1809. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  1810. i = 5;
  1811. do {
  1812. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  1813. t = readl(reg);
  1814. udelay(1);
  1815. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  1816. if (GLOB_SFT_RST & t) {
  1817. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  1818. rc = 1;
  1819. }
  1820. done:
  1821. return rc;
  1822. }
  1823. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  1824. void __iomem *mmio)
  1825. {
  1826. void __iomem *port_mmio;
  1827. u32 tmp;
  1828. tmp = readl(mmio + MV_RESET_CFG);
  1829. if ((tmp & (1 << 0)) == 0) {
  1830. hpriv->signal[idx].amps = 0x7 << 8;
  1831. hpriv->signal[idx].pre = 0x1 << 5;
  1832. return;
  1833. }
  1834. port_mmio = mv_port_base(mmio, idx);
  1835. tmp = readl(port_mmio + PHY_MODE2);
  1836. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1837. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1838. }
  1839. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1840. {
  1841. writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
  1842. }
  1843. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1844. unsigned int port)
  1845. {
  1846. void __iomem *port_mmio = mv_port_base(mmio, port);
  1847. u32 hp_flags = hpriv->hp_flags;
  1848. int fix_phy_mode2 =
  1849. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1850. int fix_phy_mode4 =
  1851. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1852. u32 m2, tmp;
  1853. if (fix_phy_mode2) {
  1854. m2 = readl(port_mmio + PHY_MODE2);
  1855. m2 &= ~(1 << 16);
  1856. m2 |= (1 << 31);
  1857. writel(m2, port_mmio + PHY_MODE2);
  1858. udelay(200);
  1859. m2 = readl(port_mmio + PHY_MODE2);
  1860. m2 &= ~((1 << 16) | (1 << 31));
  1861. writel(m2, port_mmio + PHY_MODE2);
  1862. udelay(200);
  1863. }
  1864. /* who knows what this magic does */
  1865. tmp = readl(port_mmio + PHY_MODE3);
  1866. tmp &= ~0x7F800000;
  1867. tmp |= 0x2A800000;
  1868. writel(tmp, port_mmio + PHY_MODE3);
  1869. if (fix_phy_mode4) {
  1870. u32 m4;
  1871. m4 = readl(port_mmio + PHY_MODE4);
  1872. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1873. tmp = readl(port_mmio + 0x310);
  1874. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  1875. writel(m4, port_mmio + PHY_MODE4);
  1876. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1877. writel(tmp, port_mmio + 0x310);
  1878. }
  1879. /* Revert values of pre-emphasis and signal amps to the saved ones */
  1880. m2 = readl(port_mmio + PHY_MODE2);
  1881. m2 &= ~MV_M2_PREAMP_MASK;
  1882. m2 |= hpriv->signal[port].amps;
  1883. m2 |= hpriv->signal[port].pre;
  1884. m2 &= ~(1 << 16);
  1885. /* according to mvSata 3.6.1, some IIE values are fixed */
  1886. if (IS_GEN_IIE(hpriv)) {
  1887. m2 &= ~0xC30FF01F;
  1888. m2 |= 0x0000900F;
  1889. }
  1890. writel(m2, port_mmio + PHY_MODE2);
  1891. }
  1892. /* TODO: use the generic LED interface to configure the SATA Presence */
  1893. /* & Acitivy LEDs on the board */
  1894. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  1895. void __iomem *mmio)
  1896. {
  1897. return;
  1898. }
  1899. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  1900. void __iomem *mmio)
  1901. {
  1902. void __iomem *port_mmio;
  1903. u32 tmp;
  1904. port_mmio = mv_port_base(mmio, idx);
  1905. tmp = readl(port_mmio + PHY_MODE2);
  1906. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1907. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1908. }
  1909. #undef ZERO
  1910. #define ZERO(reg) writel(0, port_mmio + (reg))
  1911. static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
  1912. void __iomem *mmio, unsigned int port)
  1913. {
  1914. void __iomem *port_mmio = mv_port_base(mmio, port);
  1915. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1916. mv_channel_reset(hpriv, mmio, port);
  1917. ZERO(0x028); /* command */
  1918. writel(0x101f, port_mmio + EDMA_CFG_OFS);
  1919. ZERO(0x004); /* timer */
  1920. ZERO(0x008); /* irq err cause */
  1921. ZERO(0x00c); /* irq err mask */
  1922. ZERO(0x010); /* rq bah */
  1923. ZERO(0x014); /* rq inp */
  1924. ZERO(0x018); /* rq outp */
  1925. ZERO(0x01c); /* respq bah */
  1926. ZERO(0x024); /* respq outp */
  1927. ZERO(0x020); /* respq inp */
  1928. ZERO(0x02c); /* test control */
  1929. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1930. }
  1931. #undef ZERO
  1932. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1933. static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
  1934. void __iomem *mmio)
  1935. {
  1936. void __iomem *hc_mmio = mv_hc_base(mmio, 0);
  1937. ZERO(0x00c);
  1938. ZERO(0x010);
  1939. ZERO(0x014);
  1940. }
  1941. #undef ZERO
  1942. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  1943. void __iomem *mmio, unsigned int n_hc)
  1944. {
  1945. unsigned int port;
  1946. for (port = 0; port < hpriv->n_ports; port++)
  1947. mv_soc_reset_hc_port(hpriv, mmio, port);
  1948. mv_soc_reset_one_hc(hpriv, mmio);
  1949. return 0;
  1950. }
  1951. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  1952. void __iomem *mmio)
  1953. {
  1954. return;
  1955. }
  1956. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
  1957. {
  1958. return;
  1959. }
  1960. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  1961. unsigned int port_no)
  1962. {
  1963. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  1964. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1965. if (IS_GEN_II(hpriv)) {
  1966. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1967. ifctl |= (1 << 7); /* enable gen2i speed */
  1968. ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
  1969. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1970. }
  1971. udelay(25); /* allow reset propagation */
  1972. /* Spec never mentions clearing the bit. Marvell's driver does
  1973. * clear the bit, however.
  1974. */
  1975. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1976. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  1977. if (IS_GEN_I(hpriv))
  1978. mdelay(1);
  1979. }
  1980. /**
  1981. * mv_phy_reset - Perform eDMA reset followed by COMRESET
  1982. * @ap: ATA channel to manipulate
  1983. *
  1984. * Part of this is taken from __sata_phy_reset and modified to
  1985. * not sleep since this routine gets called from interrupt level.
  1986. *
  1987. * LOCKING:
  1988. * Inherited from caller. This is coded to safe to call at
  1989. * interrupt level, i.e. it does not sleep.
  1990. */
  1991. static void mv_phy_reset(struct ata_port *ap, unsigned int *class,
  1992. unsigned long deadline)
  1993. {
  1994. struct mv_port_priv *pp = ap->private_data;
  1995. struct mv_host_priv *hpriv = ap->host->private_data;
  1996. void __iomem *port_mmio = mv_ap_base(ap);
  1997. int retry = 5;
  1998. u32 sstatus;
  1999. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  2000. #ifdef DEBUG
  2001. {
  2002. u32 sstatus, serror, scontrol;
  2003. mv_scr_read(ap, SCR_STATUS, &sstatus);
  2004. mv_scr_read(ap, SCR_ERROR, &serror);
  2005. mv_scr_read(ap, SCR_CONTROL, &scontrol);
  2006. DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  2007. "SCtrl 0x%08x\n", sstatus, serror, scontrol);
  2008. }
  2009. #endif
  2010. /* Issue COMRESET via SControl */
  2011. comreset_retry:
  2012. sata_scr_write_flush(&ap->link, SCR_CONTROL, 0x301);
  2013. msleep(1);
  2014. sata_scr_write_flush(&ap->link, SCR_CONTROL, 0x300);
  2015. msleep(20);
  2016. do {
  2017. sata_scr_read(&ap->link, SCR_STATUS, &sstatus);
  2018. if (((sstatus & 0x3) == 3) || ((sstatus & 0x3) == 0))
  2019. break;
  2020. msleep(1);
  2021. } while (time_before(jiffies, deadline));
  2022. /* work around errata */
  2023. if (IS_GEN_II(hpriv) &&
  2024. (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
  2025. (retry-- > 0))
  2026. goto comreset_retry;
  2027. #ifdef DEBUG
  2028. {
  2029. u32 sstatus, serror, scontrol;
  2030. mv_scr_read(ap, SCR_STATUS, &sstatus);
  2031. mv_scr_read(ap, SCR_ERROR, &serror);
  2032. mv_scr_read(ap, SCR_CONTROL, &scontrol);
  2033. DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  2034. "SCtrl 0x%08x\n", sstatus, serror, scontrol);
  2035. }
  2036. #endif
  2037. if (ata_link_offline(&ap->link)) {
  2038. *class = ATA_DEV_NONE;
  2039. return;
  2040. }
  2041. /* even after SStatus reflects that device is ready,
  2042. * it seems to take a while for link to be fully
  2043. * established (and thus Status no longer 0x80/0x7F),
  2044. * so we poll a bit for that, here.
  2045. */
  2046. retry = 20;
  2047. while (1) {
  2048. u8 drv_stat = ata_check_status(ap);
  2049. if ((drv_stat != 0x80) && (drv_stat != 0x7f))
  2050. break;
  2051. msleep(500);
  2052. if (retry-- <= 0)
  2053. break;
  2054. if (time_after(jiffies, deadline))
  2055. break;
  2056. }
  2057. /* FIXME: if we passed the deadline, the following
  2058. * code probably produces an invalid result
  2059. */
  2060. /* finally, read device signature from TF registers */
  2061. *class = ata_dev_try_classify(ap->link.device, 1, NULL);
  2062. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2063. WARN_ON(pp->pp_flags & MV_PP_FLAG_EDMA_EN);
  2064. VPRINTK("EXIT\n");
  2065. }
  2066. static int mv_prereset(struct ata_link *link, unsigned long deadline)
  2067. {
  2068. struct ata_port *ap = link->ap;
  2069. struct mv_port_priv *pp = ap->private_data;
  2070. struct ata_eh_context *ehc = &link->eh_context;
  2071. int rc;
  2072. rc = mv_stop_dma(ap);
  2073. if (rc)
  2074. ehc->i.action |= ATA_EH_HARDRESET;
  2075. if (!(pp->pp_flags & MV_PP_FLAG_HAD_A_RESET)) {
  2076. pp->pp_flags |= MV_PP_FLAG_HAD_A_RESET;
  2077. ehc->i.action |= ATA_EH_HARDRESET;
  2078. }
  2079. /* if we're about to do hardreset, nothing more to do */
  2080. if (ehc->i.action & ATA_EH_HARDRESET)
  2081. return 0;
  2082. if (ata_link_online(link))
  2083. rc = ata_wait_ready(ap, deadline);
  2084. else
  2085. rc = -ENODEV;
  2086. return rc;
  2087. }
  2088. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  2089. unsigned long deadline)
  2090. {
  2091. struct ata_port *ap = link->ap;
  2092. struct mv_host_priv *hpriv = ap->host->private_data;
  2093. void __iomem *mmio = hpriv->base;
  2094. mv_stop_dma(ap);
  2095. mv_channel_reset(hpriv, mmio, ap->port_no);
  2096. mv_phy_reset(ap, class, deadline);
  2097. return 0;
  2098. }
  2099. static void mv_postreset(struct ata_link *link, unsigned int *classes)
  2100. {
  2101. struct ata_port *ap = link->ap;
  2102. u32 serr;
  2103. /* print link status */
  2104. sata_print_link_status(link);
  2105. /* clear SError */
  2106. sata_scr_read(link, SCR_ERROR, &serr);
  2107. sata_scr_write_flush(link, SCR_ERROR, serr);
  2108. /* bail out if no device is present */
  2109. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2110. DPRINTK("EXIT, no device\n");
  2111. return;
  2112. }
  2113. /* set up device control */
  2114. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  2115. }
  2116. static void mv_error_handler(struct ata_port *ap)
  2117. {
  2118. ata_do_eh(ap, mv_prereset, ata_std_softreset,
  2119. mv_hardreset, mv_postreset);
  2120. }
  2121. static void mv_eh_freeze(struct ata_port *ap)
  2122. {
  2123. struct mv_host_priv *hpriv = ap->host->private_data;
  2124. unsigned int hc = (ap->port_no > 3) ? 1 : 0;
  2125. u32 tmp, mask;
  2126. unsigned int shift;
  2127. /* FIXME: handle coalescing completion events properly */
  2128. shift = ap->port_no * 2;
  2129. if (hc > 0)
  2130. shift++;
  2131. mask = 0x3 << shift;
  2132. /* disable assertion of portN err, done events */
  2133. tmp = readl(hpriv->main_mask_reg_addr);
  2134. writelfl(tmp & ~mask, hpriv->main_mask_reg_addr);
  2135. }
  2136. static void mv_eh_thaw(struct ata_port *ap)
  2137. {
  2138. struct mv_host_priv *hpriv = ap->host->private_data;
  2139. void __iomem *mmio = hpriv->base;
  2140. unsigned int hc = (ap->port_no > 3) ? 1 : 0;
  2141. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2142. void __iomem *port_mmio = mv_ap_base(ap);
  2143. u32 tmp, mask, hc_irq_cause;
  2144. unsigned int shift, hc_port_no = ap->port_no;
  2145. /* FIXME: handle coalescing completion events properly */
  2146. shift = ap->port_no * 2;
  2147. if (hc > 0) {
  2148. shift++;
  2149. hc_port_no -= 4;
  2150. }
  2151. mask = 0x3 << shift;
  2152. /* clear EDMA errors on this port */
  2153. writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2154. /* clear pending irq events */
  2155. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  2156. hc_irq_cause &= ~(1 << hc_port_no); /* clear CRPB-done */
  2157. hc_irq_cause &= ~(1 << (hc_port_no + 8)); /* clear Device int */
  2158. writel(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  2159. /* enable assertion of portN err, done events */
  2160. tmp = readl(hpriv->main_mask_reg_addr);
  2161. writelfl(tmp | mask, hpriv->main_mask_reg_addr);
  2162. }
  2163. /**
  2164. * mv_port_init - Perform some early initialization on a single port.
  2165. * @port: libata data structure storing shadow register addresses
  2166. * @port_mmio: base address of the port
  2167. *
  2168. * Initialize shadow register mmio addresses, clear outstanding
  2169. * interrupts on the port, and unmask interrupts for the future
  2170. * start of the port.
  2171. *
  2172. * LOCKING:
  2173. * Inherited from caller.
  2174. */
  2175. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  2176. {
  2177. void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
  2178. unsigned serr_ofs;
  2179. /* PIO related setup
  2180. */
  2181. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  2182. port->error_addr =
  2183. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  2184. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  2185. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  2186. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  2187. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  2188. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  2189. port->status_addr =
  2190. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  2191. /* special case: control/altstatus doesn't have ATA_REG_ address */
  2192. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  2193. /* unused: */
  2194. port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
  2195. /* Clear any currently outstanding port interrupt conditions */
  2196. serr_ofs = mv_scr_offset(SCR_ERROR);
  2197. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  2198. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2199. /* unmask all non-transient EDMA error interrupts */
  2200. writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  2201. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  2202. readl(port_mmio + EDMA_CFG_OFS),
  2203. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  2204. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  2205. }
  2206. static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
  2207. {
  2208. struct pci_dev *pdev = to_pci_dev(host->dev);
  2209. struct mv_host_priv *hpriv = host->private_data;
  2210. u32 hp_flags = hpriv->hp_flags;
  2211. switch (board_idx) {
  2212. case chip_5080:
  2213. hpriv->ops = &mv5xxx_ops;
  2214. hp_flags |= MV_HP_GEN_I;
  2215. switch (pdev->revision) {
  2216. case 0x1:
  2217. hp_flags |= MV_HP_ERRATA_50XXB0;
  2218. break;
  2219. case 0x3:
  2220. hp_flags |= MV_HP_ERRATA_50XXB2;
  2221. break;
  2222. default:
  2223. dev_printk(KERN_WARNING, &pdev->dev,
  2224. "Applying 50XXB2 workarounds to unknown rev\n");
  2225. hp_flags |= MV_HP_ERRATA_50XXB2;
  2226. break;
  2227. }
  2228. break;
  2229. case chip_504x:
  2230. case chip_508x:
  2231. hpriv->ops = &mv5xxx_ops;
  2232. hp_flags |= MV_HP_GEN_I;
  2233. switch (pdev->revision) {
  2234. case 0x0:
  2235. hp_flags |= MV_HP_ERRATA_50XXB0;
  2236. break;
  2237. case 0x3:
  2238. hp_flags |= MV_HP_ERRATA_50XXB2;
  2239. break;
  2240. default:
  2241. dev_printk(KERN_WARNING, &pdev->dev,
  2242. "Applying B2 workarounds to unknown rev\n");
  2243. hp_flags |= MV_HP_ERRATA_50XXB2;
  2244. break;
  2245. }
  2246. break;
  2247. case chip_604x:
  2248. case chip_608x:
  2249. hpriv->ops = &mv6xxx_ops;
  2250. hp_flags |= MV_HP_GEN_II;
  2251. switch (pdev->revision) {
  2252. case 0x7:
  2253. hp_flags |= MV_HP_ERRATA_60X1B2;
  2254. break;
  2255. case 0x9:
  2256. hp_flags |= MV_HP_ERRATA_60X1C0;
  2257. break;
  2258. default:
  2259. dev_printk(KERN_WARNING, &pdev->dev,
  2260. "Applying B2 workarounds to unknown rev\n");
  2261. hp_flags |= MV_HP_ERRATA_60X1B2;
  2262. break;
  2263. }
  2264. break;
  2265. case chip_7042:
  2266. hp_flags |= MV_HP_PCIE;
  2267. if (pdev->vendor == PCI_VENDOR_ID_TTI &&
  2268. (pdev->device == 0x2300 || pdev->device == 0x2310))
  2269. {
  2270. /*
  2271. * Highpoint RocketRAID PCIe 23xx series cards:
  2272. *
  2273. * Unconfigured drives are treated as "Legacy"
  2274. * by the BIOS, and it overwrites sector 8 with
  2275. * a "Lgcy" metadata block prior to Linux boot.
  2276. *
  2277. * Configured drives (RAID or JBOD) leave sector 8
  2278. * alone, but instead overwrite a high numbered
  2279. * sector for the RAID metadata. This sector can
  2280. * be determined exactly, by truncating the physical
  2281. * drive capacity to a nice even GB value.
  2282. *
  2283. * RAID metadata is at: (dev->n_sectors & ~0xfffff)
  2284. *
  2285. * Warn the user, lest they think we're just buggy.
  2286. */
  2287. printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
  2288. " BIOS CORRUPTS DATA on all attached drives,"
  2289. " regardless of if/how they are configured."
  2290. " BEWARE!\n");
  2291. printk(KERN_WARNING DRV_NAME ": For data safety, do not"
  2292. " use sectors 8-9 on \"Legacy\" drives,"
  2293. " and avoid the final two gigabytes on"
  2294. " all RocketRAID BIOS initialized drives.\n");
  2295. }
  2296. case chip_6042:
  2297. hpriv->ops = &mv6xxx_ops;
  2298. hp_flags |= MV_HP_GEN_IIE;
  2299. switch (pdev->revision) {
  2300. case 0x0:
  2301. hp_flags |= MV_HP_ERRATA_XX42A0;
  2302. break;
  2303. case 0x1:
  2304. hp_flags |= MV_HP_ERRATA_60X1C0;
  2305. break;
  2306. default:
  2307. dev_printk(KERN_WARNING, &pdev->dev,
  2308. "Applying 60X1C0 workarounds to unknown rev\n");
  2309. hp_flags |= MV_HP_ERRATA_60X1C0;
  2310. break;
  2311. }
  2312. break;
  2313. case chip_soc:
  2314. hpriv->ops = &mv_soc_ops;
  2315. hp_flags |= MV_HP_ERRATA_60X1C0;
  2316. break;
  2317. default:
  2318. dev_printk(KERN_ERR, host->dev,
  2319. "BUG: invalid board index %u\n", board_idx);
  2320. return 1;
  2321. }
  2322. hpriv->hp_flags = hp_flags;
  2323. if (hp_flags & MV_HP_PCIE) {
  2324. hpriv->irq_cause_ofs = PCIE_IRQ_CAUSE_OFS;
  2325. hpriv->irq_mask_ofs = PCIE_IRQ_MASK_OFS;
  2326. hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
  2327. } else {
  2328. hpriv->irq_cause_ofs = PCI_IRQ_CAUSE_OFS;
  2329. hpriv->irq_mask_ofs = PCI_IRQ_MASK_OFS;
  2330. hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
  2331. }
  2332. return 0;
  2333. }
  2334. /**
  2335. * mv_init_host - Perform some early initialization of the host.
  2336. * @host: ATA host to initialize
  2337. * @board_idx: controller index
  2338. *
  2339. * If possible, do an early global reset of the host. Then do
  2340. * our port init and clear/unmask all/relevant host interrupts.
  2341. *
  2342. * LOCKING:
  2343. * Inherited from caller.
  2344. */
  2345. static int mv_init_host(struct ata_host *host, unsigned int board_idx)
  2346. {
  2347. int rc = 0, n_hc, port, hc;
  2348. struct mv_host_priv *hpriv = host->private_data;
  2349. void __iomem *mmio = hpriv->base;
  2350. rc = mv_chip_id(host, board_idx);
  2351. if (rc)
  2352. goto done;
  2353. if (HAS_PCI(host)) {
  2354. hpriv->main_cause_reg_addr = hpriv->base +
  2355. HC_MAIN_IRQ_CAUSE_OFS;
  2356. hpriv->main_mask_reg_addr = hpriv->base + HC_MAIN_IRQ_MASK_OFS;
  2357. } else {
  2358. hpriv->main_cause_reg_addr = hpriv->base +
  2359. HC_SOC_MAIN_IRQ_CAUSE_OFS;
  2360. hpriv->main_mask_reg_addr = hpriv->base +
  2361. HC_SOC_MAIN_IRQ_MASK_OFS;
  2362. }
  2363. /* global interrupt mask */
  2364. writel(0, hpriv->main_mask_reg_addr);
  2365. n_hc = mv_get_hc_count(host->ports[0]->flags);
  2366. for (port = 0; port < host->n_ports; port++)
  2367. hpriv->ops->read_preamp(hpriv, port, mmio);
  2368. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  2369. if (rc)
  2370. goto done;
  2371. hpriv->ops->reset_flash(hpriv, mmio);
  2372. hpriv->ops->reset_bus(host, mmio);
  2373. hpriv->ops->enable_leds(hpriv, mmio);
  2374. for (port = 0; port < host->n_ports; port++) {
  2375. if (IS_GEN_II(hpriv)) {
  2376. void __iomem *port_mmio = mv_port_base(mmio, port);
  2377. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  2378. ifctl |= (1 << 7); /* enable gen2i speed */
  2379. ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
  2380. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  2381. }
  2382. hpriv->ops->phy_errata(hpriv, mmio, port);
  2383. }
  2384. for (port = 0; port < host->n_ports; port++) {
  2385. struct ata_port *ap = host->ports[port];
  2386. void __iomem *port_mmio = mv_port_base(mmio, port);
  2387. mv_port_init(&ap->ioaddr, port_mmio);
  2388. #ifdef CONFIG_PCI
  2389. if (HAS_PCI(host)) {
  2390. unsigned int offset = port_mmio - mmio;
  2391. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
  2392. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
  2393. }
  2394. #endif
  2395. }
  2396. for (hc = 0; hc < n_hc; hc++) {
  2397. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2398. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  2399. "(before clear)=0x%08x\n", hc,
  2400. readl(hc_mmio + HC_CFG_OFS),
  2401. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  2402. /* Clear any currently outstanding hc interrupt conditions */
  2403. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  2404. }
  2405. if (HAS_PCI(host)) {
  2406. /* Clear any currently outstanding host interrupt conditions */
  2407. writelfl(0, mmio + hpriv->irq_cause_ofs);
  2408. /* and unmask interrupt generation for host regs */
  2409. writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
  2410. if (IS_GEN_I(hpriv))
  2411. writelfl(~HC_MAIN_MASKED_IRQS_5,
  2412. hpriv->main_mask_reg_addr);
  2413. else
  2414. writelfl(~HC_MAIN_MASKED_IRQS,
  2415. hpriv->main_mask_reg_addr);
  2416. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  2417. "PCI int cause/mask=0x%08x/0x%08x\n",
  2418. readl(hpriv->main_cause_reg_addr),
  2419. readl(hpriv->main_mask_reg_addr),
  2420. readl(mmio + hpriv->irq_cause_ofs),
  2421. readl(mmio + hpriv->irq_mask_ofs));
  2422. } else {
  2423. writelfl(~HC_MAIN_MASKED_IRQS_SOC,
  2424. hpriv->main_mask_reg_addr);
  2425. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x\n",
  2426. readl(hpriv->main_cause_reg_addr),
  2427. readl(hpriv->main_mask_reg_addr));
  2428. }
  2429. done:
  2430. return rc;
  2431. }
  2432. static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
  2433. {
  2434. hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
  2435. MV_CRQB_Q_SZ, 0);
  2436. if (!hpriv->crqb_pool)
  2437. return -ENOMEM;
  2438. hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
  2439. MV_CRPB_Q_SZ, 0);
  2440. if (!hpriv->crpb_pool)
  2441. return -ENOMEM;
  2442. hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
  2443. MV_SG_TBL_SZ, 0);
  2444. if (!hpriv->sg_tbl_pool)
  2445. return -ENOMEM;
  2446. return 0;
  2447. }
  2448. /**
  2449. * mv_platform_probe - handle a positive probe of an soc Marvell
  2450. * host
  2451. * @pdev: platform device found
  2452. *
  2453. * LOCKING:
  2454. * Inherited from caller.
  2455. */
  2456. static int mv_platform_probe(struct platform_device *pdev)
  2457. {
  2458. static int printed_version;
  2459. const struct mv_sata_platform_data *mv_platform_data;
  2460. const struct ata_port_info *ppi[] =
  2461. { &mv_port_info[chip_soc], NULL };
  2462. struct ata_host *host;
  2463. struct mv_host_priv *hpriv;
  2464. struct resource *res;
  2465. int n_ports, rc;
  2466. if (!printed_version++)
  2467. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  2468. /*
  2469. * Simple resource validation ..
  2470. */
  2471. if (unlikely(pdev->num_resources != 2)) {
  2472. dev_err(&pdev->dev, "invalid number of resources\n");
  2473. return -EINVAL;
  2474. }
  2475. /*
  2476. * Get the register base first
  2477. */
  2478. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2479. if (res == NULL)
  2480. return -EINVAL;
  2481. /* allocate host */
  2482. mv_platform_data = pdev->dev.platform_data;
  2483. n_ports = mv_platform_data->n_ports;
  2484. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  2485. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  2486. if (!host || !hpriv)
  2487. return -ENOMEM;
  2488. host->private_data = hpriv;
  2489. hpriv->n_ports = n_ports;
  2490. host->iomap = NULL;
  2491. hpriv->base = ioremap(res->start, res->end - res->start + 1);
  2492. hpriv->base -= MV_SATAHC0_REG_BASE;
  2493. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  2494. if (rc)
  2495. return rc;
  2496. /* initialize adapter */
  2497. rc = mv_init_host(host, chip_soc);
  2498. if (rc)
  2499. return rc;
  2500. dev_printk(KERN_INFO, &pdev->dev,
  2501. "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
  2502. host->n_ports);
  2503. return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
  2504. IRQF_SHARED, &mv6_sht);
  2505. }
  2506. /*
  2507. *
  2508. * mv_platform_remove - unplug a platform interface
  2509. * @pdev: platform device
  2510. *
  2511. * A platform bus SATA device has been unplugged. Perform the needed
  2512. * cleanup. Also called on module unload for any active devices.
  2513. */
  2514. static int __devexit mv_platform_remove(struct platform_device *pdev)
  2515. {
  2516. struct device *dev = &pdev->dev;
  2517. struct ata_host *host = dev_get_drvdata(dev);
  2518. struct mv_host_priv *hpriv = host->private_data;
  2519. void __iomem *base = hpriv->base;
  2520. ata_host_detach(host);
  2521. iounmap(base);
  2522. return 0;
  2523. }
  2524. static struct platform_driver mv_platform_driver = {
  2525. .probe = mv_platform_probe,
  2526. .remove = __devexit_p(mv_platform_remove),
  2527. .driver = {
  2528. .name = DRV_NAME,
  2529. .owner = THIS_MODULE,
  2530. },
  2531. };
  2532. #ifdef CONFIG_PCI
  2533. static int mv_pci_init_one(struct pci_dev *pdev,
  2534. const struct pci_device_id *ent);
  2535. static struct pci_driver mv_pci_driver = {
  2536. .name = DRV_NAME,
  2537. .id_table = mv_pci_tbl,
  2538. .probe = mv_pci_init_one,
  2539. .remove = ata_pci_remove_one,
  2540. };
  2541. /*
  2542. * module options
  2543. */
  2544. static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
  2545. /* move to PCI layer or libata core? */
  2546. static int pci_go_64(struct pci_dev *pdev)
  2547. {
  2548. int rc;
  2549. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  2550. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  2551. if (rc) {
  2552. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  2553. if (rc) {
  2554. dev_printk(KERN_ERR, &pdev->dev,
  2555. "64-bit DMA enable failed\n");
  2556. return rc;
  2557. }
  2558. }
  2559. } else {
  2560. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  2561. if (rc) {
  2562. dev_printk(KERN_ERR, &pdev->dev,
  2563. "32-bit DMA enable failed\n");
  2564. return rc;
  2565. }
  2566. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  2567. if (rc) {
  2568. dev_printk(KERN_ERR, &pdev->dev,
  2569. "32-bit consistent DMA enable failed\n");
  2570. return rc;
  2571. }
  2572. }
  2573. return rc;
  2574. }
  2575. /**
  2576. * mv_print_info - Dump key info to kernel log for perusal.
  2577. * @host: ATA host to print info about
  2578. *
  2579. * FIXME: complete this.
  2580. *
  2581. * LOCKING:
  2582. * Inherited from caller.
  2583. */
  2584. static void mv_print_info(struct ata_host *host)
  2585. {
  2586. struct pci_dev *pdev = to_pci_dev(host->dev);
  2587. struct mv_host_priv *hpriv = host->private_data;
  2588. u8 scc;
  2589. const char *scc_s, *gen;
  2590. /* Use this to determine the HW stepping of the chip so we know
  2591. * what errata to workaround
  2592. */
  2593. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  2594. if (scc == 0)
  2595. scc_s = "SCSI";
  2596. else if (scc == 0x01)
  2597. scc_s = "RAID";
  2598. else
  2599. scc_s = "?";
  2600. if (IS_GEN_I(hpriv))
  2601. gen = "I";
  2602. else if (IS_GEN_II(hpriv))
  2603. gen = "II";
  2604. else if (IS_GEN_IIE(hpriv))
  2605. gen = "IIE";
  2606. else
  2607. gen = "?";
  2608. dev_printk(KERN_INFO, &pdev->dev,
  2609. "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
  2610. gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
  2611. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  2612. }
  2613. /**
  2614. * mv_pci_init_one - handle a positive probe of a PCI Marvell host
  2615. * @pdev: PCI device found
  2616. * @ent: PCI device ID entry for the matched host
  2617. *
  2618. * LOCKING:
  2619. * Inherited from caller.
  2620. */
  2621. static int mv_pci_init_one(struct pci_dev *pdev,
  2622. const struct pci_device_id *ent)
  2623. {
  2624. static int printed_version;
  2625. unsigned int board_idx = (unsigned int)ent->driver_data;
  2626. const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
  2627. struct ata_host *host;
  2628. struct mv_host_priv *hpriv;
  2629. int n_ports, rc;
  2630. if (!printed_version++)
  2631. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  2632. /* allocate host */
  2633. n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
  2634. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  2635. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  2636. if (!host || !hpriv)
  2637. return -ENOMEM;
  2638. host->private_data = hpriv;
  2639. hpriv->n_ports = n_ports;
  2640. /* acquire resources */
  2641. rc = pcim_enable_device(pdev);
  2642. if (rc)
  2643. return rc;
  2644. rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
  2645. if (rc == -EBUSY)
  2646. pcim_pin_device(pdev);
  2647. if (rc)
  2648. return rc;
  2649. host->iomap = pcim_iomap_table(pdev);
  2650. hpriv->base = host->iomap[MV_PRIMARY_BAR];
  2651. rc = pci_go_64(pdev);
  2652. if (rc)
  2653. return rc;
  2654. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  2655. if (rc)
  2656. return rc;
  2657. /* initialize adapter */
  2658. rc = mv_init_host(host, board_idx);
  2659. if (rc)
  2660. return rc;
  2661. /* Enable interrupts */
  2662. if (msi && pci_enable_msi(pdev))
  2663. pci_intx(pdev, 1);
  2664. mv_dump_pci_cfg(pdev, 0x68);
  2665. mv_print_info(host);
  2666. pci_set_master(pdev);
  2667. pci_try_set_mwi(pdev);
  2668. return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
  2669. IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
  2670. }
  2671. #endif
  2672. static int mv_platform_probe(struct platform_device *pdev);
  2673. static int __devexit mv_platform_remove(struct platform_device *pdev);
  2674. static int __init mv_init(void)
  2675. {
  2676. int rc = -ENODEV;
  2677. #ifdef CONFIG_PCI
  2678. rc = pci_register_driver(&mv_pci_driver);
  2679. if (rc < 0)
  2680. return rc;
  2681. #endif
  2682. rc = platform_driver_register(&mv_platform_driver);
  2683. #ifdef CONFIG_PCI
  2684. if (rc < 0)
  2685. pci_unregister_driver(&mv_pci_driver);
  2686. #endif
  2687. return rc;
  2688. }
  2689. static void __exit mv_exit(void)
  2690. {
  2691. #ifdef CONFIG_PCI
  2692. pci_unregister_driver(&mv_pci_driver);
  2693. #endif
  2694. platform_driver_unregister(&mv_platform_driver);
  2695. }
  2696. MODULE_AUTHOR("Brett Russ");
  2697. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  2698. MODULE_LICENSE("GPL");
  2699. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  2700. MODULE_VERSION(DRV_VERSION);
  2701. #ifdef CONFIG_PCI
  2702. module_param(msi, int, 0444);
  2703. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  2704. #endif
  2705. module_init(mv_init);
  2706. module_exit(mv_exit);