sata_mv.c 79 KB

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