sata_mv.c 94 KB

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