sata_mv.c 113 KB

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