sata_mv.c 123 KB

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