sata_mv.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2005: EMC Corporation, all rights reserved.
  5. * Copyright 2005 Red Hat, Inc. All rights reserved.
  6. *
  7. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/init.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/sched.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/device.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <linux/libata.h>
  36. #include <asm/io.h>
  37. #define DRV_NAME "sata_mv"
  38. #define DRV_VERSION "0.7"
  39. enum {
  40. /* BAR's are enumerated in terms of pci_resource_start() terms */
  41. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  42. MV_IO_BAR = 2, /* offset 0x18: IO space */
  43. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  44. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  45. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  46. MV_PCI_REG_BASE = 0,
  47. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  48. MV_IRQ_COAL_CAUSE = (MV_IRQ_COAL_REG_BASE + 0x08),
  49. MV_IRQ_COAL_CAUSE_LO = (MV_IRQ_COAL_REG_BASE + 0x88),
  50. MV_IRQ_COAL_CAUSE_HI = (MV_IRQ_COAL_REG_BASE + 0x8c),
  51. MV_IRQ_COAL_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xcc),
  52. MV_IRQ_COAL_TIME_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xd0),
  53. MV_SATAHC0_REG_BASE = 0x20000,
  54. MV_FLASH_CTL = 0x1046c,
  55. MV_GPIO_PORT_CTL = 0x104f0,
  56. MV_RESET_CFG = 0x180d8,
  57. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  58. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  59. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  60. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  61. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  62. MV_MAX_Q_DEPTH = 32,
  63. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  64. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  65. * CRPB needs alignment on a 256B boundary. Size == 256B
  66. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  67. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  68. */
  69. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  70. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  71. MV_MAX_SG_CT = 176,
  72. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  73. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  74. MV_PORTS_PER_HC = 4,
  75. /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
  76. MV_PORT_HC_SHIFT = 2,
  77. /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
  78. MV_PORT_MASK = 3,
  79. /* Host Flags */
  80. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  81. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  82. MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  83. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  84. ATA_FLAG_NO_ATAPI | ATA_FLAG_PIO_POLLING),
  85. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  86. CRQB_FLAG_READ = (1 << 0),
  87. CRQB_TAG_SHIFT = 1,
  88. CRQB_CMD_ADDR_SHIFT = 8,
  89. CRQB_CMD_CS = (0x2 << 11),
  90. CRQB_CMD_LAST = (1 << 15),
  91. CRPB_FLAG_STATUS_SHIFT = 8,
  92. EPRD_FLAG_END_OF_TBL = (1 << 31),
  93. /* PCI interface registers */
  94. PCI_COMMAND_OFS = 0xc00,
  95. PCI_MAIN_CMD_STS_OFS = 0xd30,
  96. STOP_PCI_MASTER = (1 << 2),
  97. PCI_MASTER_EMPTY = (1 << 3),
  98. GLOB_SFT_RST = (1 << 4),
  99. MV_PCI_MODE = 0xd00,
  100. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  101. MV_PCI_DISC_TIMER = 0xd04,
  102. MV_PCI_MSI_TRIGGER = 0xc38,
  103. MV_PCI_SERR_MASK = 0xc28,
  104. MV_PCI_XBAR_TMOUT = 0x1d04,
  105. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  106. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  107. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  108. MV_PCI_ERR_COMMAND = 0x1d50,
  109. PCI_IRQ_CAUSE_OFS = 0x1d58,
  110. PCI_IRQ_MASK_OFS = 0x1d5c,
  111. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  112. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  113. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  114. PORT0_ERR = (1 << 0), /* shift by port # */
  115. PORT0_DONE = (1 << 1), /* shift by port # */
  116. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  117. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  118. PCI_ERR = (1 << 18),
  119. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  120. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  121. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  122. GPIO_INT = (1 << 22),
  123. SELF_INT = (1 << 23),
  124. TWSI_INT = (1 << 24),
  125. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  126. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  127. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  128. HC_MAIN_RSVD),
  129. /* SATAHC registers */
  130. HC_CFG_OFS = 0,
  131. HC_IRQ_CAUSE_OFS = 0x14,
  132. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  133. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  134. DEV_IRQ = (1 << 8), /* shift by port # */
  135. /* Shadow block registers */
  136. SHD_BLK_OFS = 0x100,
  137. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  138. /* SATA registers */
  139. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  140. SATA_ACTIVE_OFS = 0x350,
  141. PHY_MODE3 = 0x310,
  142. PHY_MODE4 = 0x314,
  143. PHY_MODE2 = 0x330,
  144. MV5_PHY_MODE = 0x74,
  145. MV5_LT_MODE = 0x30,
  146. MV5_PHY_CTL = 0x0C,
  147. SATA_INTERFACE_CTL = 0x050,
  148. MV_M2_PREAMP_MASK = 0x7e0,
  149. /* Port registers */
  150. EDMA_CFG_OFS = 0,
  151. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  152. EDMA_CFG_NCQ = (1 << 5),
  153. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  154. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  155. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  156. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  157. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  158. EDMA_ERR_D_PAR = (1 << 0),
  159. EDMA_ERR_PRD_PAR = (1 << 1),
  160. EDMA_ERR_DEV = (1 << 2),
  161. EDMA_ERR_DEV_DCON = (1 << 3),
  162. EDMA_ERR_DEV_CON = (1 << 4),
  163. EDMA_ERR_SERR = (1 << 5),
  164. EDMA_ERR_SELF_DIS = (1 << 7),
  165. EDMA_ERR_BIST_ASYNC = (1 << 8),
  166. EDMA_ERR_CRBQ_PAR = (1 << 9),
  167. EDMA_ERR_CRPB_PAR = (1 << 10),
  168. EDMA_ERR_INTRL_PAR = (1 << 11),
  169. EDMA_ERR_IORDY = (1 << 12),
  170. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  171. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  172. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  173. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  174. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  175. EDMA_ERR_TRANS_PROTO = (1 << 31),
  176. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  177. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  178. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  179. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  180. EDMA_ERR_LNK_DATA_RX |
  181. EDMA_ERR_LNK_DATA_TX |
  182. EDMA_ERR_TRANS_PROTO),
  183. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  184. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  185. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  186. EDMA_REQ_Q_PTR_SHIFT = 5,
  187. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  188. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  189. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  190. EDMA_RSP_Q_PTR_SHIFT = 3,
  191. EDMA_CMD_OFS = 0x28,
  192. EDMA_EN = (1 << 0),
  193. EDMA_DS = (1 << 1),
  194. ATA_RST = (1 << 2),
  195. EDMA_IORDY_TMOUT = 0x34,
  196. EDMA_ARB_CFG = 0x38,
  197. /* Host private flags (hp_flags) */
  198. MV_HP_FLAG_MSI = (1 << 0),
  199. MV_HP_ERRATA_50XXB0 = (1 << 1),
  200. MV_HP_ERRATA_50XXB2 = (1 << 2),
  201. MV_HP_ERRATA_60X1B2 = (1 << 3),
  202. MV_HP_ERRATA_60X1C0 = (1 << 4),
  203. MV_HP_ERRATA_XX42A0 = (1 << 5),
  204. MV_HP_50XX = (1 << 6),
  205. MV_HP_GEN_IIE = (1 << 7),
  206. /* Port private flags (pp_flags) */
  207. MV_PP_FLAG_EDMA_EN = (1 << 0),
  208. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  209. };
  210. #define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
  211. #define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
  212. #define IS_GEN_I(hpriv) IS_50XX(hpriv)
  213. #define IS_GEN_II(hpriv) IS_60XX(hpriv)
  214. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  215. enum {
  216. /* Our DMA boundary is determined by an ePRD being unable to handle
  217. * anything larger than 64KB
  218. */
  219. MV_DMA_BOUNDARY = 0xffffU,
  220. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  221. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  222. };
  223. enum chip_type {
  224. chip_504x,
  225. chip_508x,
  226. chip_5080,
  227. chip_604x,
  228. chip_608x,
  229. chip_6042,
  230. chip_7042,
  231. };
  232. /* Command ReQuest Block: 32B */
  233. struct mv_crqb {
  234. __le32 sg_addr;
  235. __le32 sg_addr_hi;
  236. __le16 ctrl_flags;
  237. __le16 ata_cmd[11];
  238. };
  239. struct mv_crqb_iie {
  240. __le32 addr;
  241. __le32 addr_hi;
  242. __le32 flags;
  243. __le32 len;
  244. __le32 ata_cmd[4];
  245. };
  246. /* Command ResPonse Block: 8B */
  247. struct mv_crpb {
  248. __le16 id;
  249. __le16 flags;
  250. __le32 tmstmp;
  251. };
  252. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  253. struct mv_sg {
  254. __le32 addr;
  255. __le32 flags_size;
  256. __le32 addr_hi;
  257. __le32 reserved;
  258. };
  259. struct mv_port_priv {
  260. struct mv_crqb *crqb;
  261. dma_addr_t crqb_dma;
  262. struct mv_crpb *crpb;
  263. dma_addr_t crpb_dma;
  264. struct mv_sg *sg_tbl;
  265. dma_addr_t sg_tbl_dma;
  266. u32 pp_flags;
  267. };
  268. struct mv_port_signal {
  269. u32 amps;
  270. u32 pre;
  271. };
  272. struct mv_host_priv;
  273. struct mv_hw_ops {
  274. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  275. unsigned int port);
  276. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  277. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  278. void __iomem *mmio);
  279. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  280. unsigned int n_hc);
  281. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  282. void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
  283. };
  284. struct mv_host_priv {
  285. u32 hp_flags;
  286. struct mv_port_signal signal[8];
  287. const struct mv_hw_ops *ops;
  288. };
  289. static void mv_irq_clear(struct ata_port *ap);
  290. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  291. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  292. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  293. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  294. static void mv_phy_reset(struct ata_port *ap);
  295. static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
  296. static void mv_host_stop(struct ata_host_set *host_set);
  297. static int mv_port_start(struct ata_port *ap);
  298. static void mv_port_stop(struct ata_port *ap);
  299. static void mv_qc_prep(struct ata_queued_cmd *qc);
  300. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  301. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  302. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  303. struct pt_regs *regs);
  304. static void mv_eng_timeout(struct ata_port *ap);
  305. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  306. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  307. unsigned int port);
  308. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  309. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  310. void __iomem *mmio);
  311. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  312. unsigned int n_hc);
  313. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  314. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
  315. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  316. unsigned int port);
  317. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  318. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  319. void __iomem *mmio);
  320. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  321. unsigned int n_hc);
  322. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  323. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
  324. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  325. unsigned int port_no);
  326. static void mv_stop_and_reset(struct ata_port *ap);
  327. static struct scsi_host_template mv_sht = {
  328. .module = THIS_MODULE,
  329. .name = DRV_NAME,
  330. .ioctl = ata_scsi_ioctl,
  331. .queuecommand = ata_scsi_queuecmd,
  332. .can_queue = MV_USE_Q_DEPTH,
  333. .this_id = ATA_SHT_THIS_ID,
  334. .sg_tablesize = MV_MAX_SG_CT / 2,
  335. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  336. .emulated = ATA_SHT_EMULATED,
  337. .use_clustering = ATA_SHT_USE_CLUSTERING,
  338. .proc_name = DRV_NAME,
  339. .dma_boundary = MV_DMA_BOUNDARY,
  340. .slave_configure = ata_scsi_slave_config,
  341. .slave_destroy = ata_scsi_slave_destroy,
  342. .bios_param = ata_std_bios_param,
  343. };
  344. static const struct ata_port_operations mv5_ops = {
  345. .port_disable = ata_port_disable,
  346. .tf_load = ata_tf_load,
  347. .tf_read = ata_tf_read,
  348. .check_status = ata_check_status,
  349. .exec_command = ata_exec_command,
  350. .dev_select = ata_std_dev_select,
  351. .phy_reset = mv_phy_reset,
  352. .qc_prep = mv_qc_prep,
  353. .qc_issue = mv_qc_issue,
  354. .data_xfer = ata_mmio_data_xfer,
  355. .eng_timeout = mv_eng_timeout,
  356. .irq_handler = mv_interrupt,
  357. .irq_clear = mv_irq_clear,
  358. .scr_read = mv5_scr_read,
  359. .scr_write = mv5_scr_write,
  360. .port_start = mv_port_start,
  361. .port_stop = mv_port_stop,
  362. .host_stop = mv_host_stop,
  363. };
  364. static const struct ata_port_operations mv6_ops = {
  365. .port_disable = ata_port_disable,
  366. .tf_load = ata_tf_load,
  367. .tf_read = ata_tf_read,
  368. .check_status = ata_check_status,
  369. .exec_command = ata_exec_command,
  370. .dev_select = ata_std_dev_select,
  371. .phy_reset = mv_phy_reset,
  372. .qc_prep = mv_qc_prep,
  373. .qc_issue = mv_qc_issue,
  374. .data_xfer = ata_mmio_data_xfer,
  375. .eng_timeout = mv_eng_timeout,
  376. .irq_handler = mv_interrupt,
  377. .irq_clear = mv_irq_clear,
  378. .scr_read = mv_scr_read,
  379. .scr_write = mv_scr_write,
  380. .port_start = mv_port_start,
  381. .port_stop = mv_port_stop,
  382. .host_stop = mv_host_stop,
  383. };
  384. static const struct ata_port_operations mv_iie_ops = {
  385. .port_disable = ata_port_disable,
  386. .tf_load = ata_tf_load,
  387. .tf_read = ata_tf_read,
  388. .check_status = ata_check_status,
  389. .exec_command = ata_exec_command,
  390. .dev_select = ata_std_dev_select,
  391. .phy_reset = mv_phy_reset,
  392. .qc_prep = mv_qc_prep_iie,
  393. .qc_issue = mv_qc_issue,
  394. .eng_timeout = mv_eng_timeout,
  395. .irq_handler = mv_interrupt,
  396. .irq_clear = mv_irq_clear,
  397. .scr_read = mv_scr_read,
  398. .scr_write = mv_scr_write,
  399. .port_start = mv_port_start,
  400. .port_stop = mv_port_stop,
  401. .host_stop = mv_host_stop,
  402. };
  403. static const struct ata_port_info mv_port_info[] = {
  404. { /* chip_504x */
  405. .sht = &mv_sht,
  406. .host_flags = MV_COMMON_FLAGS,
  407. .pio_mask = 0x1f, /* pio0-4 */
  408. .udma_mask = 0x7f, /* udma0-6 */
  409. .port_ops = &mv5_ops,
  410. },
  411. { /* chip_508x */
  412. .sht = &mv_sht,
  413. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  414. .pio_mask = 0x1f, /* pio0-4 */
  415. .udma_mask = 0x7f, /* udma0-6 */
  416. .port_ops = &mv5_ops,
  417. },
  418. { /* chip_5080 */
  419. .sht = &mv_sht,
  420. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  421. .pio_mask = 0x1f, /* pio0-4 */
  422. .udma_mask = 0x7f, /* udma0-6 */
  423. .port_ops = &mv5_ops,
  424. },
  425. { /* chip_604x */
  426. .sht = &mv_sht,
  427. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  428. .pio_mask = 0x1f, /* pio0-4 */
  429. .udma_mask = 0x7f, /* udma0-6 */
  430. .port_ops = &mv6_ops,
  431. },
  432. { /* chip_608x */
  433. .sht = &mv_sht,
  434. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  435. MV_FLAG_DUAL_HC),
  436. .pio_mask = 0x1f, /* pio0-4 */
  437. .udma_mask = 0x7f, /* udma0-6 */
  438. .port_ops = &mv6_ops,
  439. },
  440. { /* chip_6042 */
  441. .sht = &mv_sht,
  442. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  443. .pio_mask = 0x1f, /* pio0-4 */
  444. .udma_mask = 0x7f, /* udma0-6 */
  445. .port_ops = &mv_iie_ops,
  446. },
  447. { /* chip_7042 */
  448. .sht = &mv_sht,
  449. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  450. MV_FLAG_DUAL_HC),
  451. .pio_mask = 0x1f, /* pio0-4 */
  452. .udma_mask = 0x7f, /* udma0-6 */
  453. .port_ops = &mv_iie_ops,
  454. },
  455. };
  456. static const struct pci_device_id mv_pci_tbl[] = {
  457. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
  458. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
  459. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_5080},
  460. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
  461. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
  462. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
  463. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6042), 0, 0, chip_6042},
  464. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
  465. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
  466. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x0241), 0, 0, chip_604x},
  467. {} /* terminate list */
  468. };
  469. static struct pci_driver mv_pci_driver = {
  470. .name = DRV_NAME,
  471. .id_table = mv_pci_tbl,
  472. .probe = mv_init_one,
  473. .remove = ata_pci_remove_one,
  474. };
  475. static const struct mv_hw_ops mv5xxx_ops = {
  476. .phy_errata = mv5_phy_errata,
  477. .enable_leds = mv5_enable_leds,
  478. .read_preamp = mv5_read_preamp,
  479. .reset_hc = mv5_reset_hc,
  480. .reset_flash = mv5_reset_flash,
  481. .reset_bus = mv5_reset_bus,
  482. };
  483. static const struct mv_hw_ops mv6xxx_ops = {
  484. .phy_errata = mv6_phy_errata,
  485. .enable_leds = mv6_enable_leds,
  486. .read_preamp = mv6_read_preamp,
  487. .reset_hc = mv6_reset_hc,
  488. .reset_flash = mv6_reset_flash,
  489. .reset_bus = mv_reset_pci_bus,
  490. };
  491. /*
  492. * module options
  493. */
  494. static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
  495. /*
  496. * Functions
  497. */
  498. static inline void writelfl(unsigned long data, void __iomem *addr)
  499. {
  500. writel(data, addr);
  501. (void) readl(addr); /* flush to avoid PCI posted write */
  502. }
  503. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  504. {
  505. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  506. }
  507. static inline unsigned int mv_hc_from_port(unsigned int port)
  508. {
  509. return port >> MV_PORT_HC_SHIFT;
  510. }
  511. static inline unsigned int mv_hardport_from_port(unsigned int port)
  512. {
  513. return port & MV_PORT_MASK;
  514. }
  515. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  516. unsigned int port)
  517. {
  518. return mv_hc_base(base, mv_hc_from_port(port));
  519. }
  520. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  521. {
  522. return mv_hc_base_from_port(base, port) +
  523. MV_SATAHC_ARBTR_REG_SZ +
  524. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  525. }
  526. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  527. {
  528. return mv_port_base(ap->host_set->mmio_base, ap->port_no);
  529. }
  530. static inline int mv_get_hc_count(unsigned long host_flags)
  531. {
  532. return ((host_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  533. }
  534. static void mv_irq_clear(struct ata_port *ap)
  535. {
  536. }
  537. /**
  538. * mv_start_dma - Enable eDMA engine
  539. * @base: port base address
  540. * @pp: port private data
  541. *
  542. * Verify the local cache of the eDMA state is accurate with a
  543. * WARN_ON.
  544. *
  545. * LOCKING:
  546. * Inherited from caller.
  547. */
  548. static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
  549. {
  550. if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
  551. writelfl(EDMA_EN, base + EDMA_CMD_OFS);
  552. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  553. }
  554. WARN_ON(!(EDMA_EN & readl(base + EDMA_CMD_OFS)));
  555. }
  556. /**
  557. * mv_stop_dma - Disable eDMA engine
  558. * @ap: ATA channel to manipulate
  559. *
  560. * Verify the local cache of the eDMA state is accurate with a
  561. * WARN_ON.
  562. *
  563. * LOCKING:
  564. * Inherited from caller.
  565. */
  566. static void mv_stop_dma(struct ata_port *ap)
  567. {
  568. void __iomem *port_mmio = mv_ap_base(ap);
  569. struct mv_port_priv *pp = ap->private_data;
  570. u32 reg;
  571. int i;
  572. if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
  573. /* Disable EDMA if active. The disable bit auto clears.
  574. */
  575. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  576. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  577. } else {
  578. WARN_ON(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS));
  579. }
  580. /* now properly wait for the eDMA to stop */
  581. for (i = 1000; i > 0; i--) {
  582. reg = readl(port_mmio + EDMA_CMD_OFS);
  583. if (!(EDMA_EN & reg)) {
  584. break;
  585. }
  586. udelay(100);
  587. }
  588. if (EDMA_EN & reg) {
  589. ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
  590. /* FIXME: Consider doing a reset here to recover */
  591. }
  592. }
  593. #ifdef ATA_DEBUG
  594. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  595. {
  596. int b, w;
  597. for (b = 0; b < bytes; ) {
  598. DPRINTK("%p: ", start + b);
  599. for (w = 0; b < bytes && w < 4; w++) {
  600. printk("%08x ",readl(start + b));
  601. b += sizeof(u32);
  602. }
  603. printk("\n");
  604. }
  605. }
  606. #endif
  607. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  608. {
  609. #ifdef ATA_DEBUG
  610. int b, w;
  611. u32 dw;
  612. for (b = 0; b < bytes; ) {
  613. DPRINTK("%02x: ", b);
  614. for (w = 0; b < bytes && w < 4; w++) {
  615. (void) pci_read_config_dword(pdev,b,&dw);
  616. printk("%08x ",dw);
  617. b += sizeof(u32);
  618. }
  619. printk("\n");
  620. }
  621. #endif
  622. }
  623. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  624. struct pci_dev *pdev)
  625. {
  626. #ifdef ATA_DEBUG
  627. void __iomem *hc_base = mv_hc_base(mmio_base,
  628. port >> MV_PORT_HC_SHIFT);
  629. void __iomem *port_base;
  630. int start_port, num_ports, p, start_hc, num_hcs, hc;
  631. if (0 > port) {
  632. start_hc = start_port = 0;
  633. num_ports = 8; /* shld be benign for 4 port devs */
  634. num_hcs = 2;
  635. } else {
  636. start_hc = port >> MV_PORT_HC_SHIFT;
  637. start_port = port;
  638. num_ports = num_hcs = 1;
  639. }
  640. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  641. num_ports > 1 ? num_ports - 1 : start_port);
  642. if (NULL != pdev) {
  643. DPRINTK("PCI config space regs:\n");
  644. mv_dump_pci_cfg(pdev, 0x68);
  645. }
  646. DPRINTK("PCI regs:\n");
  647. mv_dump_mem(mmio_base+0xc00, 0x3c);
  648. mv_dump_mem(mmio_base+0xd00, 0x34);
  649. mv_dump_mem(mmio_base+0xf00, 0x4);
  650. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  651. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  652. hc_base = mv_hc_base(mmio_base, hc);
  653. DPRINTK("HC regs (HC %i):\n", hc);
  654. mv_dump_mem(hc_base, 0x1c);
  655. }
  656. for (p = start_port; p < start_port + num_ports; p++) {
  657. port_base = mv_port_base(mmio_base, p);
  658. DPRINTK("EDMA regs (port %i):\n",p);
  659. mv_dump_mem(port_base, 0x54);
  660. DPRINTK("SATA regs (port %i):\n",p);
  661. mv_dump_mem(port_base+0x300, 0x60);
  662. }
  663. #endif
  664. }
  665. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  666. {
  667. unsigned int ofs;
  668. switch (sc_reg_in) {
  669. case SCR_STATUS:
  670. case SCR_CONTROL:
  671. case SCR_ERROR:
  672. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  673. break;
  674. case SCR_ACTIVE:
  675. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  676. break;
  677. default:
  678. ofs = 0xffffffffU;
  679. break;
  680. }
  681. return ofs;
  682. }
  683. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  684. {
  685. unsigned int ofs = mv_scr_offset(sc_reg_in);
  686. if (0xffffffffU != ofs) {
  687. return readl(mv_ap_base(ap) + ofs);
  688. } else {
  689. return (u32) ofs;
  690. }
  691. }
  692. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  693. {
  694. unsigned int ofs = mv_scr_offset(sc_reg_in);
  695. if (0xffffffffU != ofs) {
  696. writelfl(val, mv_ap_base(ap) + ofs);
  697. }
  698. }
  699. /**
  700. * mv_host_stop - Host specific cleanup/stop routine.
  701. * @host_set: host data structure
  702. *
  703. * Disable ints, cleanup host memory, call general purpose
  704. * host_stop.
  705. *
  706. * LOCKING:
  707. * Inherited from caller.
  708. */
  709. static void mv_host_stop(struct ata_host_set *host_set)
  710. {
  711. struct mv_host_priv *hpriv = host_set->private_data;
  712. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  713. if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
  714. pci_disable_msi(pdev);
  715. } else {
  716. pci_intx(pdev, 0);
  717. }
  718. kfree(hpriv);
  719. ata_host_stop(host_set);
  720. }
  721. static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
  722. {
  723. dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
  724. }
  725. static void mv_edma_cfg(struct mv_host_priv *hpriv, void __iomem *port_mmio)
  726. {
  727. u32 cfg = readl(port_mmio + EDMA_CFG_OFS);
  728. /* set up non-NCQ EDMA configuration */
  729. cfg &= ~0x1f; /* clear queue depth */
  730. cfg &= ~EDMA_CFG_NCQ; /* clear NCQ mode */
  731. cfg &= ~(1 << 9); /* disable equeue */
  732. if (IS_GEN_I(hpriv))
  733. cfg |= (1 << 8); /* enab config burst size mask */
  734. else if (IS_GEN_II(hpriv))
  735. cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
  736. else if (IS_GEN_IIE(hpriv)) {
  737. cfg |= (1 << 23); /* dis RX PM port mask */
  738. cfg &= ~(1 << 16); /* dis FIS-based switching (for now) */
  739. cfg &= ~(1 << 19); /* dis 128-entry queue (for now?) */
  740. cfg |= (1 << 18); /* enab early completion */
  741. cfg |= (1 << 17); /* enab host q cache */
  742. cfg |= (1 << 22); /* enab cutthrough */
  743. }
  744. writelfl(cfg, port_mmio + EDMA_CFG_OFS);
  745. }
  746. /**
  747. * mv_port_start - Port specific init/start routine.
  748. * @ap: ATA channel to manipulate
  749. *
  750. * Allocate and point to DMA memory, init port private memory,
  751. * zero indices.
  752. *
  753. * LOCKING:
  754. * Inherited from caller.
  755. */
  756. static int mv_port_start(struct ata_port *ap)
  757. {
  758. struct device *dev = ap->host_set->dev;
  759. struct mv_host_priv *hpriv = ap->host_set->private_data;
  760. struct mv_port_priv *pp;
  761. void __iomem *port_mmio = mv_ap_base(ap);
  762. void *mem;
  763. dma_addr_t mem_dma;
  764. int rc = -ENOMEM;
  765. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  766. if (!pp)
  767. goto err_out;
  768. memset(pp, 0, sizeof(*pp));
  769. mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
  770. GFP_KERNEL);
  771. if (!mem)
  772. goto err_out_pp;
  773. memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
  774. rc = ata_pad_alloc(ap, dev);
  775. if (rc)
  776. goto err_out_priv;
  777. /* First item in chunk of DMA memory:
  778. * 32-slot command request table (CRQB), 32 bytes each in size
  779. */
  780. pp->crqb = mem;
  781. pp->crqb_dma = mem_dma;
  782. mem += MV_CRQB_Q_SZ;
  783. mem_dma += MV_CRQB_Q_SZ;
  784. /* Second item:
  785. * 32-slot command response table (CRPB), 8 bytes each in size
  786. */
  787. pp->crpb = mem;
  788. pp->crpb_dma = mem_dma;
  789. mem += MV_CRPB_Q_SZ;
  790. mem_dma += MV_CRPB_Q_SZ;
  791. /* Third item:
  792. * Table of scatter-gather descriptors (ePRD), 16 bytes each
  793. */
  794. pp->sg_tbl = mem;
  795. pp->sg_tbl_dma = mem_dma;
  796. mv_edma_cfg(hpriv, port_mmio);
  797. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  798. writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
  799. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  800. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  801. writelfl(pp->crqb_dma & 0xffffffff,
  802. port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  803. else
  804. writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  805. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  806. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  807. writelfl(pp->crpb_dma & 0xffffffff,
  808. port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  809. else
  810. writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  811. writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
  812. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  813. /* Don't turn on EDMA here...do it before DMA commands only. Else
  814. * we'll be unable to send non-data, PIO, etc due to restricted access
  815. * to shadow regs.
  816. */
  817. ap->private_data = pp;
  818. return 0;
  819. err_out_priv:
  820. mv_priv_free(pp, dev);
  821. err_out_pp:
  822. kfree(pp);
  823. err_out:
  824. return rc;
  825. }
  826. /**
  827. * mv_port_stop - Port specific cleanup/stop routine.
  828. * @ap: ATA channel to manipulate
  829. *
  830. * Stop DMA, cleanup port memory.
  831. *
  832. * LOCKING:
  833. * This routine uses the host_set lock to protect the DMA stop.
  834. */
  835. static void mv_port_stop(struct ata_port *ap)
  836. {
  837. struct device *dev = ap->host_set->dev;
  838. struct mv_port_priv *pp = ap->private_data;
  839. unsigned long flags;
  840. spin_lock_irqsave(&ap->host_set->lock, flags);
  841. mv_stop_dma(ap);
  842. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  843. ap->private_data = NULL;
  844. ata_pad_free(ap, dev);
  845. mv_priv_free(pp, dev);
  846. kfree(pp);
  847. }
  848. /**
  849. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  850. * @qc: queued command whose SG list to source from
  851. *
  852. * Populate the SG list and mark the last entry.
  853. *
  854. * LOCKING:
  855. * Inherited from caller.
  856. */
  857. static void mv_fill_sg(struct ata_queued_cmd *qc)
  858. {
  859. struct mv_port_priv *pp = qc->ap->private_data;
  860. unsigned int i = 0;
  861. struct scatterlist *sg;
  862. ata_for_each_sg(sg, qc) {
  863. dma_addr_t addr;
  864. u32 sg_len, len, offset;
  865. addr = sg_dma_address(sg);
  866. sg_len = sg_dma_len(sg);
  867. while (sg_len) {
  868. offset = addr & MV_DMA_BOUNDARY;
  869. len = sg_len;
  870. if ((offset + sg_len) > 0x10000)
  871. len = 0x10000 - offset;
  872. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  873. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  874. pp->sg_tbl[i].flags_size = cpu_to_le32(len & 0xffff);
  875. sg_len -= len;
  876. addr += len;
  877. if (!sg_len && ata_sg_is_last(sg, qc))
  878. pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  879. i++;
  880. }
  881. }
  882. }
  883. static inline unsigned mv_inc_q_index(unsigned index)
  884. {
  885. return (index + 1) & MV_MAX_Q_DEPTH_MASK;
  886. }
  887. static inline void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
  888. {
  889. u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  890. (last ? CRQB_CMD_LAST : 0);
  891. *cmdw = cpu_to_le16(tmp);
  892. }
  893. /**
  894. * mv_qc_prep - Host specific command preparation.
  895. * @qc: queued command to prepare
  896. *
  897. * This routine simply redirects to the general purpose routine
  898. * if command is not DMA. Else, it handles prep of the CRQB
  899. * (command request block), does some sanity checking, and calls
  900. * the SG load routine.
  901. *
  902. * LOCKING:
  903. * Inherited from caller.
  904. */
  905. static void mv_qc_prep(struct ata_queued_cmd *qc)
  906. {
  907. struct ata_port *ap = qc->ap;
  908. struct mv_port_priv *pp = ap->private_data;
  909. __le16 *cw;
  910. struct ata_taskfile *tf;
  911. u16 flags = 0;
  912. unsigned in_index;
  913. if (ATA_PROT_DMA != qc->tf.protocol)
  914. return;
  915. /* Fill in command request block
  916. */
  917. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  918. flags |= CRQB_FLAG_READ;
  919. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  920. flags |= qc->tag << CRQB_TAG_SHIFT;
  921. /* get current queue index from hardware */
  922. in_index = (readl(mv_ap_base(ap) + EDMA_REQ_Q_IN_PTR_OFS)
  923. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  924. pp->crqb[in_index].sg_addr =
  925. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  926. pp->crqb[in_index].sg_addr_hi =
  927. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  928. pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
  929. cw = &pp->crqb[in_index].ata_cmd[0];
  930. tf = &qc->tf;
  931. /* Sadly, the CRQB cannot accomodate all registers--there are
  932. * only 11 bytes...so we must pick and choose required
  933. * registers based on the command. So, we drop feature and
  934. * hob_feature for [RW] DMA commands, but they are needed for
  935. * NCQ. NCQ will drop hob_nsect.
  936. */
  937. switch (tf->command) {
  938. case ATA_CMD_READ:
  939. case ATA_CMD_READ_EXT:
  940. case ATA_CMD_WRITE:
  941. case ATA_CMD_WRITE_EXT:
  942. case ATA_CMD_WRITE_FUA_EXT:
  943. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  944. break;
  945. #ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
  946. case ATA_CMD_FPDMA_READ:
  947. case ATA_CMD_FPDMA_WRITE:
  948. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  949. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  950. break;
  951. #endif /* FIXME: remove this line when NCQ added */
  952. default:
  953. /* The only other commands EDMA supports in non-queued and
  954. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  955. * of which are defined/used by Linux. If we get here, this
  956. * driver needs work.
  957. *
  958. * FIXME: modify libata to give qc_prep a return value and
  959. * return error here.
  960. */
  961. BUG_ON(tf->command);
  962. break;
  963. }
  964. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  965. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  966. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  967. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  968. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  969. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  970. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  971. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  972. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  973. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  974. return;
  975. mv_fill_sg(qc);
  976. }
  977. /**
  978. * mv_qc_prep_iie - Host specific command preparation.
  979. * @qc: queued command to prepare
  980. *
  981. * This routine simply redirects to the general purpose routine
  982. * if command is not DMA. Else, it handles prep of the CRQB
  983. * (command request block), does some sanity checking, and calls
  984. * the SG load routine.
  985. *
  986. * LOCKING:
  987. * Inherited from caller.
  988. */
  989. static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
  990. {
  991. struct ata_port *ap = qc->ap;
  992. struct mv_port_priv *pp = ap->private_data;
  993. struct mv_crqb_iie *crqb;
  994. struct ata_taskfile *tf;
  995. unsigned in_index;
  996. u32 flags = 0;
  997. if (ATA_PROT_DMA != qc->tf.protocol)
  998. return;
  999. /* Fill in Gen IIE command request block
  1000. */
  1001. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1002. flags |= CRQB_FLAG_READ;
  1003. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1004. flags |= qc->tag << CRQB_TAG_SHIFT;
  1005. /* get current queue index from hardware */
  1006. in_index = (readl(mv_ap_base(ap) + EDMA_REQ_Q_IN_PTR_OFS)
  1007. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1008. crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
  1009. crqb->addr = cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  1010. crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  1011. crqb->flags = cpu_to_le32(flags);
  1012. tf = &qc->tf;
  1013. crqb->ata_cmd[0] = cpu_to_le32(
  1014. (tf->command << 16) |
  1015. (tf->feature << 24)
  1016. );
  1017. crqb->ata_cmd[1] = cpu_to_le32(
  1018. (tf->lbal << 0) |
  1019. (tf->lbam << 8) |
  1020. (tf->lbah << 16) |
  1021. (tf->device << 24)
  1022. );
  1023. crqb->ata_cmd[2] = cpu_to_le32(
  1024. (tf->hob_lbal << 0) |
  1025. (tf->hob_lbam << 8) |
  1026. (tf->hob_lbah << 16) |
  1027. (tf->hob_feature << 24)
  1028. );
  1029. crqb->ata_cmd[3] = cpu_to_le32(
  1030. (tf->nsect << 0) |
  1031. (tf->hob_nsect << 8)
  1032. );
  1033. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1034. return;
  1035. mv_fill_sg(qc);
  1036. }
  1037. /**
  1038. * mv_qc_issue - Initiate a command to the host
  1039. * @qc: queued command to start
  1040. *
  1041. * This routine simply redirects to the general purpose routine
  1042. * if command is not DMA. Else, it sanity checks our local
  1043. * caches of the request producer/consumer indices then enables
  1044. * DMA and bumps the request producer index.
  1045. *
  1046. * LOCKING:
  1047. * Inherited from caller.
  1048. */
  1049. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  1050. {
  1051. void __iomem *port_mmio = mv_ap_base(qc->ap);
  1052. struct mv_port_priv *pp = qc->ap->private_data;
  1053. unsigned in_index;
  1054. u32 in_ptr;
  1055. if (ATA_PROT_DMA != qc->tf.protocol) {
  1056. /* We're about to send a non-EDMA capable command to the
  1057. * port. Turn off EDMA so there won't be problems accessing
  1058. * shadow block, etc registers.
  1059. */
  1060. mv_stop_dma(qc->ap);
  1061. return ata_qc_issue_prot(qc);
  1062. }
  1063. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1064. in_index = (in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1065. /* until we do queuing, the queue should be empty at this point */
  1066. WARN_ON(in_index != ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
  1067. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  1068. in_index = mv_inc_q_index(in_index); /* now incr producer index */
  1069. mv_start_dma(port_mmio, pp);
  1070. /* and write the request in pointer to kick the EDMA to life */
  1071. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  1072. in_ptr |= in_index << EDMA_REQ_Q_PTR_SHIFT;
  1073. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1074. return 0;
  1075. }
  1076. /**
  1077. * mv_get_crpb_status - get status from most recently completed cmd
  1078. * @ap: ATA channel to manipulate
  1079. *
  1080. * This routine is for use when the port is in DMA mode, when it
  1081. * will be using the CRPB (command response block) method of
  1082. * returning command completion information. We check indices
  1083. * are good, grab status, and bump the response consumer index to
  1084. * prove that we're up to date.
  1085. *
  1086. * LOCKING:
  1087. * Inherited from caller.
  1088. */
  1089. static u8 mv_get_crpb_status(struct ata_port *ap)
  1090. {
  1091. void __iomem *port_mmio = mv_ap_base(ap);
  1092. struct mv_port_priv *pp = ap->private_data;
  1093. unsigned out_index;
  1094. u32 out_ptr;
  1095. u8 ata_status;
  1096. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1097. out_index = (out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1098. ata_status = le16_to_cpu(pp->crpb[out_index].flags)
  1099. >> CRPB_FLAG_STATUS_SHIFT;
  1100. /* increment our consumer index... */
  1101. out_index = mv_inc_q_index(out_index);
  1102. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  1103. WARN_ON(out_index != ((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
  1104. >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  1105. /* write out our inc'd consumer index so EDMA knows we're caught up */
  1106. out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
  1107. out_ptr |= out_index << EDMA_RSP_Q_PTR_SHIFT;
  1108. writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1109. /* Return ATA status register for completed CRPB */
  1110. return ata_status;
  1111. }
  1112. /**
  1113. * mv_err_intr - Handle error interrupts on the port
  1114. * @ap: ATA channel to manipulate
  1115. * @reset_allowed: bool: 0 == don't trigger from reset here
  1116. *
  1117. * In most cases, just clear the interrupt and move on. However,
  1118. * some cases require an eDMA reset, which is done right before
  1119. * the COMRESET in mv_phy_reset(). The SERR case requires a
  1120. * clear of pending errors in the SATA SERROR register. Finally,
  1121. * if the port disabled DMA, update our cached copy to match.
  1122. *
  1123. * LOCKING:
  1124. * Inherited from caller.
  1125. */
  1126. static void mv_err_intr(struct ata_port *ap, int reset_allowed)
  1127. {
  1128. void __iomem *port_mmio = mv_ap_base(ap);
  1129. u32 edma_err_cause, serr = 0;
  1130. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1131. if (EDMA_ERR_SERR & edma_err_cause) {
  1132. sata_scr_read(ap, SCR_ERROR, &serr);
  1133. sata_scr_write_flush(ap, SCR_ERROR, serr);
  1134. }
  1135. if (EDMA_ERR_SELF_DIS & edma_err_cause) {
  1136. struct mv_port_priv *pp = ap->private_data;
  1137. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1138. }
  1139. DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
  1140. "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
  1141. /* Clear EDMA now that SERR cleanup done */
  1142. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1143. /* check for fatal here and recover if needed */
  1144. if (reset_allowed && (EDMA_ERR_FATAL & edma_err_cause))
  1145. mv_stop_and_reset(ap);
  1146. }
  1147. /**
  1148. * mv_host_intr - Handle all interrupts on the given host controller
  1149. * @host_set: host specific structure
  1150. * @relevant: port error bits relevant to this host controller
  1151. * @hc: which host controller we're to look at
  1152. *
  1153. * Read then write clear the HC interrupt status then walk each
  1154. * port connected to the HC and see if it needs servicing. Port
  1155. * success ints are reported in the HC interrupt status reg, the
  1156. * port error ints are reported in the higher level main
  1157. * interrupt status register and thus are passed in via the
  1158. * 'relevant' argument.
  1159. *
  1160. * LOCKING:
  1161. * Inherited from caller.
  1162. */
  1163. static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
  1164. unsigned int hc)
  1165. {
  1166. void __iomem *mmio = host_set->mmio_base;
  1167. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1168. struct ata_queued_cmd *qc;
  1169. u32 hc_irq_cause;
  1170. int shift, port, port0, hard_port, handled;
  1171. unsigned int err_mask;
  1172. if (hc == 0) {
  1173. port0 = 0;
  1174. } else {
  1175. port0 = MV_PORTS_PER_HC;
  1176. }
  1177. /* we'll need the HC success int register in most cases */
  1178. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  1179. if (hc_irq_cause) {
  1180. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  1181. }
  1182. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  1183. hc,relevant,hc_irq_cause);
  1184. for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
  1185. u8 ata_status = 0;
  1186. struct ata_port *ap = host_set->ports[port];
  1187. struct mv_port_priv *pp = ap->private_data;
  1188. hard_port = mv_hardport_from_port(port); /* range 0..3 */
  1189. handled = 0; /* ensure ata_status is set if handled++ */
  1190. /* Note that DEV_IRQ might happen spuriously during EDMA,
  1191. * and should be ignored in such cases.
  1192. * The cause of this is still under investigation.
  1193. */
  1194. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  1195. /* EDMA: check for response queue interrupt */
  1196. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
  1197. ata_status = mv_get_crpb_status(ap);
  1198. handled = 1;
  1199. }
  1200. } else {
  1201. /* PIO: check for device (drive) interrupt */
  1202. if ((DEV_IRQ << hard_port) & hc_irq_cause) {
  1203. ata_status = readb((void __iomem *)
  1204. ap->ioaddr.status_addr);
  1205. handled = 1;
  1206. /* ignore spurious intr if drive still BUSY */
  1207. if (ata_status & ATA_BUSY) {
  1208. ata_status = 0;
  1209. handled = 0;
  1210. }
  1211. }
  1212. }
  1213. if (ap && (ap->flags & ATA_FLAG_DISABLED))
  1214. continue;
  1215. err_mask = ac_err_mask(ata_status);
  1216. shift = port << 1; /* (port * 2) */
  1217. if (port >= MV_PORTS_PER_HC) {
  1218. shift++; /* skip bit 8 in the HC Main IRQ reg */
  1219. }
  1220. if ((PORT0_ERR << shift) & relevant) {
  1221. mv_err_intr(ap, 1);
  1222. err_mask |= AC_ERR_OTHER;
  1223. handled = 1;
  1224. }
  1225. if (handled) {
  1226. qc = ata_qc_from_tag(ap, ap->active_tag);
  1227. if (qc && (qc->flags & ATA_QCFLAG_ACTIVE)) {
  1228. VPRINTK("port %u IRQ found for qc, "
  1229. "ata_status 0x%x\n", port,ata_status);
  1230. /* mark qc status appropriately */
  1231. if (!(qc->tf.flags & ATA_TFLAG_POLLING)) {
  1232. qc->err_mask |= err_mask;
  1233. ata_qc_complete(qc);
  1234. }
  1235. }
  1236. }
  1237. }
  1238. VPRINTK("EXIT\n");
  1239. }
  1240. /**
  1241. * mv_interrupt -
  1242. * @irq: unused
  1243. * @dev_instance: private data; in this case the host structure
  1244. * @regs: unused
  1245. *
  1246. * Read the read only register to determine if any host
  1247. * controllers have pending interrupts. If so, call lower level
  1248. * routine to handle. Also check for PCI errors which are only
  1249. * reported here.
  1250. *
  1251. * LOCKING:
  1252. * This routine holds the host_set lock while processing pending
  1253. * interrupts.
  1254. */
  1255. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  1256. struct pt_regs *regs)
  1257. {
  1258. struct ata_host_set *host_set = dev_instance;
  1259. unsigned int hc, handled = 0, n_hcs;
  1260. void __iomem *mmio = host_set->mmio_base;
  1261. struct mv_host_priv *hpriv;
  1262. u32 irq_stat;
  1263. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  1264. /* check the cases where we either have nothing pending or have read
  1265. * a bogus register value which can indicate HW removal or PCI fault
  1266. */
  1267. if (!irq_stat || (0xffffffffU == irq_stat)) {
  1268. return IRQ_NONE;
  1269. }
  1270. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  1271. spin_lock(&host_set->lock);
  1272. for (hc = 0; hc < n_hcs; hc++) {
  1273. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1274. if (relevant) {
  1275. mv_host_intr(host_set, relevant, hc);
  1276. handled++;
  1277. }
  1278. }
  1279. hpriv = host_set->private_data;
  1280. if (IS_60XX(hpriv)) {
  1281. /* deal with the interrupt coalescing bits */
  1282. if (irq_stat & (TRAN_LO_DONE | TRAN_HI_DONE | PORTS_0_7_COAL_DONE)) {
  1283. writelfl(0, mmio + MV_IRQ_COAL_CAUSE_LO);
  1284. writelfl(0, mmio + MV_IRQ_COAL_CAUSE_HI);
  1285. writelfl(0, mmio + MV_IRQ_COAL_CAUSE);
  1286. }
  1287. }
  1288. if (PCI_ERR & irq_stat) {
  1289. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  1290. readl(mmio + PCI_IRQ_CAUSE_OFS));
  1291. DPRINTK("All regs @ PCI error\n");
  1292. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  1293. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1294. handled++;
  1295. }
  1296. spin_unlock(&host_set->lock);
  1297. return IRQ_RETVAL(handled);
  1298. }
  1299. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  1300. {
  1301. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  1302. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  1303. return hc_mmio + ofs;
  1304. }
  1305. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1306. {
  1307. unsigned int ofs;
  1308. switch (sc_reg_in) {
  1309. case SCR_STATUS:
  1310. case SCR_ERROR:
  1311. case SCR_CONTROL:
  1312. ofs = sc_reg_in * sizeof(u32);
  1313. break;
  1314. default:
  1315. ofs = 0xffffffffU;
  1316. break;
  1317. }
  1318. return ofs;
  1319. }
  1320. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  1321. {
  1322. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1323. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1324. if (ofs != 0xffffffffU)
  1325. return readl(mmio + ofs);
  1326. else
  1327. return (u32) ofs;
  1328. }
  1329. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1330. {
  1331. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1332. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1333. if (ofs != 0xffffffffU)
  1334. writelfl(val, mmio + ofs);
  1335. }
  1336. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
  1337. {
  1338. u8 rev_id;
  1339. int early_5080;
  1340. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1341. early_5080 = (pdev->device == 0x5080) && (rev_id == 0);
  1342. if (!early_5080) {
  1343. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1344. tmp |= (1 << 0);
  1345. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1346. }
  1347. mv_reset_pci_bus(pdev, mmio);
  1348. }
  1349. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1350. {
  1351. writel(0x0fcfffff, mmio + MV_FLASH_CTL);
  1352. }
  1353. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  1354. void __iomem *mmio)
  1355. {
  1356. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  1357. u32 tmp;
  1358. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1359. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  1360. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  1361. }
  1362. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1363. {
  1364. u32 tmp;
  1365. writel(0, mmio + MV_GPIO_PORT_CTL);
  1366. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  1367. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1368. tmp |= ~(1 << 0);
  1369. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1370. }
  1371. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1372. unsigned int port)
  1373. {
  1374. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  1375. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  1376. u32 tmp;
  1377. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  1378. if (fix_apm_sq) {
  1379. tmp = readl(phy_mmio + MV5_LT_MODE);
  1380. tmp |= (1 << 19);
  1381. writel(tmp, phy_mmio + MV5_LT_MODE);
  1382. tmp = readl(phy_mmio + MV5_PHY_CTL);
  1383. tmp &= ~0x3;
  1384. tmp |= 0x1;
  1385. writel(tmp, phy_mmio + MV5_PHY_CTL);
  1386. }
  1387. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1388. tmp &= ~mask;
  1389. tmp |= hpriv->signal[port].pre;
  1390. tmp |= hpriv->signal[port].amps;
  1391. writel(tmp, phy_mmio + MV5_PHY_MODE);
  1392. }
  1393. #undef ZERO
  1394. #define ZERO(reg) writel(0, port_mmio + (reg))
  1395. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  1396. unsigned int port)
  1397. {
  1398. void __iomem *port_mmio = mv_port_base(mmio, port);
  1399. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1400. mv_channel_reset(hpriv, mmio, port);
  1401. ZERO(0x028); /* command */
  1402. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  1403. ZERO(0x004); /* timer */
  1404. ZERO(0x008); /* irq err cause */
  1405. ZERO(0x00c); /* irq err mask */
  1406. ZERO(0x010); /* rq bah */
  1407. ZERO(0x014); /* rq inp */
  1408. ZERO(0x018); /* rq outp */
  1409. ZERO(0x01c); /* respq bah */
  1410. ZERO(0x024); /* respq outp */
  1411. ZERO(0x020); /* respq inp */
  1412. ZERO(0x02c); /* test control */
  1413. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1414. }
  1415. #undef ZERO
  1416. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1417. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1418. unsigned int hc)
  1419. {
  1420. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1421. u32 tmp;
  1422. ZERO(0x00c);
  1423. ZERO(0x010);
  1424. ZERO(0x014);
  1425. ZERO(0x018);
  1426. tmp = readl(hc_mmio + 0x20);
  1427. tmp &= 0x1c1c1c1c;
  1428. tmp |= 0x03030303;
  1429. writel(tmp, hc_mmio + 0x20);
  1430. }
  1431. #undef ZERO
  1432. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1433. unsigned int n_hc)
  1434. {
  1435. unsigned int hc, port;
  1436. for (hc = 0; hc < n_hc; hc++) {
  1437. for (port = 0; port < MV_PORTS_PER_HC; port++)
  1438. mv5_reset_hc_port(hpriv, mmio,
  1439. (hc * MV_PORTS_PER_HC) + port);
  1440. mv5_reset_one_hc(hpriv, mmio, hc);
  1441. }
  1442. return 0;
  1443. }
  1444. #undef ZERO
  1445. #define ZERO(reg) writel(0, mmio + (reg))
  1446. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio)
  1447. {
  1448. u32 tmp;
  1449. tmp = readl(mmio + MV_PCI_MODE);
  1450. tmp &= 0xff00ffff;
  1451. writel(tmp, mmio + MV_PCI_MODE);
  1452. ZERO(MV_PCI_DISC_TIMER);
  1453. ZERO(MV_PCI_MSI_TRIGGER);
  1454. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  1455. ZERO(HC_MAIN_IRQ_MASK_OFS);
  1456. ZERO(MV_PCI_SERR_MASK);
  1457. ZERO(PCI_IRQ_CAUSE_OFS);
  1458. ZERO(PCI_IRQ_MASK_OFS);
  1459. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  1460. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  1461. ZERO(MV_PCI_ERR_ATTRIBUTE);
  1462. ZERO(MV_PCI_ERR_COMMAND);
  1463. }
  1464. #undef ZERO
  1465. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1466. {
  1467. u32 tmp;
  1468. mv5_reset_flash(hpriv, mmio);
  1469. tmp = readl(mmio + MV_GPIO_PORT_CTL);
  1470. tmp &= 0x3;
  1471. tmp |= (1 << 5) | (1 << 6);
  1472. writel(tmp, mmio + MV_GPIO_PORT_CTL);
  1473. }
  1474. /**
  1475. * mv6_reset_hc - Perform the 6xxx global soft reset
  1476. * @mmio: base address of the HBA
  1477. *
  1478. * This routine only applies to 6xxx parts.
  1479. *
  1480. * LOCKING:
  1481. * Inherited from caller.
  1482. */
  1483. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1484. unsigned int n_hc)
  1485. {
  1486. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  1487. int i, rc = 0;
  1488. u32 t;
  1489. /* Following procedure defined in PCI "main command and status
  1490. * register" table.
  1491. */
  1492. t = readl(reg);
  1493. writel(t | STOP_PCI_MASTER, reg);
  1494. for (i = 0; i < 1000; i++) {
  1495. udelay(1);
  1496. t = readl(reg);
  1497. if (PCI_MASTER_EMPTY & t) {
  1498. break;
  1499. }
  1500. }
  1501. if (!(PCI_MASTER_EMPTY & t)) {
  1502. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  1503. rc = 1;
  1504. goto done;
  1505. }
  1506. /* set reset */
  1507. i = 5;
  1508. do {
  1509. writel(t | GLOB_SFT_RST, reg);
  1510. t = readl(reg);
  1511. udelay(1);
  1512. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  1513. if (!(GLOB_SFT_RST & t)) {
  1514. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  1515. rc = 1;
  1516. goto done;
  1517. }
  1518. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  1519. i = 5;
  1520. do {
  1521. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  1522. t = readl(reg);
  1523. udelay(1);
  1524. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  1525. if (GLOB_SFT_RST & t) {
  1526. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  1527. rc = 1;
  1528. }
  1529. done:
  1530. return rc;
  1531. }
  1532. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  1533. void __iomem *mmio)
  1534. {
  1535. void __iomem *port_mmio;
  1536. u32 tmp;
  1537. tmp = readl(mmio + MV_RESET_CFG);
  1538. if ((tmp & (1 << 0)) == 0) {
  1539. hpriv->signal[idx].amps = 0x7 << 8;
  1540. hpriv->signal[idx].pre = 0x1 << 5;
  1541. return;
  1542. }
  1543. port_mmio = mv_port_base(mmio, idx);
  1544. tmp = readl(port_mmio + PHY_MODE2);
  1545. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1546. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1547. }
  1548. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1549. {
  1550. writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
  1551. }
  1552. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1553. unsigned int port)
  1554. {
  1555. void __iomem *port_mmio = mv_port_base(mmio, port);
  1556. u32 hp_flags = hpriv->hp_flags;
  1557. int fix_phy_mode2 =
  1558. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1559. int fix_phy_mode4 =
  1560. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1561. u32 m2, tmp;
  1562. if (fix_phy_mode2) {
  1563. m2 = readl(port_mmio + PHY_MODE2);
  1564. m2 &= ~(1 << 16);
  1565. m2 |= (1 << 31);
  1566. writel(m2, port_mmio + PHY_MODE2);
  1567. udelay(200);
  1568. m2 = readl(port_mmio + PHY_MODE2);
  1569. m2 &= ~((1 << 16) | (1 << 31));
  1570. writel(m2, port_mmio + PHY_MODE2);
  1571. udelay(200);
  1572. }
  1573. /* who knows what this magic does */
  1574. tmp = readl(port_mmio + PHY_MODE3);
  1575. tmp &= ~0x7F800000;
  1576. tmp |= 0x2A800000;
  1577. writel(tmp, port_mmio + PHY_MODE3);
  1578. if (fix_phy_mode4) {
  1579. u32 m4;
  1580. m4 = readl(port_mmio + PHY_MODE4);
  1581. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1582. tmp = readl(port_mmio + 0x310);
  1583. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  1584. writel(m4, port_mmio + PHY_MODE4);
  1585. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1586. writel(tmp, port_mmio + 0x310);
  1587. }
  1588. /* Revert values of pre-emphasis and signal amps to the saved ones */
  1589. m2 = readl(port_mmio + PHY_MODE2);
  1590. m2 &= ~MV_M2_PREAMP_MASK;
  1591. m2 |= hpriv->signal[port].amps;
  1592. m2 |= hpriv->signal[port].pre;
  1593. m2 &= ~(1 << 16);
  1594. /* according to mvSata 3.6.1, some IIE values are fixed */
  1595. if (IS_GEN_IIE(hpriv)) {
  1596. m2 &= ~0xC30FF01F;
  1597. m2 |= 0x0000900F;
  1598. }
  1599. writel(m2, port_mmio + PHY_MODE2);
  1600. }
  1601. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  1602. unsigned int port_no)
  1603. {
  1604. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  1605. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1606. if (IS_60XX(hpriv)) {
  1607. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1608. ifctl |= (1 << 7); /* enable gen2i speed */
  1609. ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
  1610. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1611. }
  1612. udelay(25); /* allow reset propagation */
  1613. /* Spec never mentions clearing the bit. Marvell's driver does
  1614. * clear the bit, however.
  1615. */
  1616. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1617. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  1618. if (IS_50XX(hpriv))
  1619. mdelay(1);
  1620. }
  1621. static void mv_stop_and_reset(struct ata_port *ap)
  1622. {
  1623. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1624. void __iomem *mmio = ap->host_set->mmio_base;
  1625. mv_stop_dma(ap);
  1626. mv_channel_reset(hpriv, mmio, ap->port_no);
  1627. __mv_phy_reset(ap, 0);
  1628. }
  1629. static inline void __msleep(unsigned int msec, int can_sleep)
  1630. {
  1631. if (can_sleep)
  1632. msleep(msec);
  1633. else
  1634. mdelay(msec);
  1635. }
  1636. /**
  1637. * __mv_phy_reset - Perform eDMA reset followed by COMRESET
  1638. * @ap: ATA channel to manipulate
  1639. *
  1640. * Part of this is taken from __sata_phy_reset and modified to
  1641. * not sleep since this routine gets called from interrupt level.
  1642. *
  1643. * LOCKING:
  1644. * Inherited from caller. This is coded to safe to call at
  1645. * interrupt level, i.e. it does not sleep.
  1646. */
  1647. static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
  1648. {
  1649. struct mv_port_priv *pp = ap->private_data;
  1650. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1651. void __iomem *port_mmio = mv_ap_base(ap);
  1652. struct ata_taskfile tf;
  1653. struct ata_device *dev = &ap->device[0];
  1654. unsigned long timeout;
  1655. int retry = 5;
  1656. u32 sstatus;
  1657. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  1658. DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  1659. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1660. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1661. /* Issue COMRESET via SControl */
  1662. comreset_retry:
  1663. sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
  1664. __msleep(1, can_sleep);
  1665. sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
  1666. __msleep(20, can_sleep);
  1667. timeout = jiffies + msecs_to_jiffies(200);
  1668. do {
  1669. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1670. if (((sstatus & 0x3) == 3) || ((sstatus & 0x3) == 0))
  1671. break;
  1672. __msleep(1, can_sleep);
  1673. } while (time_before(jiffies, timeout));
  1674. /* work around errata */
  1675. if (IS_60XX(hpriv) &&
  1676. (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
  1677. (retry-- > 0))
  1678. goto comreset_retry;
  1679. DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  1680. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1681. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1682. if (ata_port_online(ap)) {
  1683. ata_port_probe(ap);
  1684. } else {
  1685. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1686. ata_port_printk(ap, KERN_INFO,
  1687. "no device found (phy stat %08x)\n", sstatus);
  1688. ata_port_disable(ap);
  1689. return;
  1690. }
  1691. ap->cbl = ATA_CBL_SATA;
  1692. /* even after SStatus reflects that device is ready,
  1693. * it seems to take a while for link to be fully
  1694. * established (and thus Status no longer 0x80/0x7F),
  1695. * so we poll a bit for that, here.
  1696. */
  1697. retry = 20;
  1698. while (1) {
  1699. u8 drv_stat = ata_check_status(ap);
  1700. if ((drv_stat != 0x80) && (drv_stat != 0x7f))
  1701. break;
  1702. __msleep(500, can_sleep);
  1703. if (retry-- <= 0)
  1704. break;
  1705. }
  1706. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  1707. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  1708. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  1709. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  1710. dev->class = ata_dev_classify(&tf);
  1711. if (!ata_dev_enabled(dev)) {
  1712. VPRINTK("Port disabled post-sig: No device present.\n");
  1713. ata_port_disable(ap);
  1714. }
  1715. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1716. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1717. VPRINTK("EXIT\n");
  1718. }
  1719. static void mv_phy_reset(struct ata_port *ap)
  1720. {
  1721. __mv_phy_reset(ap, 1);
  1722. }
  1723. /**
  1724. * mv_eng_timeout - Routine called by libata when SCSI times out I/O
  1725. * @ap: ATA channel to manipulate
  1726. *
  1727. * Intent is to clear all pending error conditions, reset the
  1728. * chip/bus, fail the command, and move on.
  1729. *
  1730. * LOCKING:
  1731. * This routine holds the host_set lock while failing the command.
  1732. */
  1733. static void mv_eng_timeout(struct ata_port *ap)
  1734. {
  1735. struct ata_queued_cmd *qc;
  1736. unsigned long flags;
  1737. ata_port_printk(ap, KERN_ERR, "Entering mv_eng_timeout\n");
  1738. DPRINTK("All regs @ start of eng_timeout\n");
  1739. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  1740. to_pci_dev(ap->host_set->dev));
  1741. qc = ata_qc_from_tag(ap, ap->active_tag);
  1742. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  1743. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  1744. &qc->scsicmd->cmnd);
  1745. spin_lock_irqsave(&ap->host_set->lock, flags);
  1746. mv_err_intr(ap, 0);
  1747. mv_stop_and_reset(ap);
  1748. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1749. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  1750. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1751. qc->err_mask |= AC_ERR_TIMEOUT;
  1752. ata_eh_qc_complete(qc);
  1753. }
  1754. }
  1755. /**
  1756. * mv_port_init - Perform some early initialization on a single port.
  1757. * @port: libata data structure storing shadow register addresses
  1758. * @port_mmio: base address of the port
  1759. *
  1760. * Initialize shadow register mmio addresses, clear outstanding
  1761. * interrupts on the port, and unmask interrupts for the future
  1762. * start of the port.
  1763. *
  1764. * LOCKING:
  1765. * Inherited from caller.
  1766. */
  1767. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  1768. {
  1769. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  1770. unsigned serr_ofs;
  1771. /* PIO related setup
  1772. */
  1773. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  1774. port->error_addr =
  1775. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  1776. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  1777. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  1778. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  1779. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  1780. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  1781. port->status_addr =
  1782. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  1783. /* special case: control/altstatus doesn't have ATA_REG_ address */
  1784. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  1785. /* unused: */
  1786. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  1787. /* Clear any currently outstanding port interrupt conditions */
  1788. serr_ofs = mv_scr_offset(SCR_ERROR);
  1789. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1790. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1791. /* unmask all EDMA error interrupts */
  1792. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1793. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1794. readl(port_mmio + EDMA_CFG_OFS),
  1795. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1796. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1797. }
  1798. static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
  1799. unsigned int board_idx)
  1800. {
  1801. u8 rev_id;
  1802. u32 hp_flags = hpriv->hp_flags;
  1803. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1804. switch(board_idx) {
  1805. case chip_5080:
  1806. hpriv->ops = &mv5xxx_ops;
  1807. hp_flags |= MV_HP_50XX;
  1808. switch (rev_id) {
  1809. case 0x1:
  1810. hp_flags |= MV_HP_ERRATA_50XXB0;
  1811. break;
  1812. case 0x3:
  1813. hp_flags |= MV_HP_ERRATA_50XXB2;
  1814. break;
  1815. default:
  1816. dev_printk(KERN_WARNING, &pdev->dev,
  1817. "Applying 50XXB2 workarounds to unknown rev\n");
  1818. hp_flags |= MV_HP_ERRATA_50XXB2;
  1819. break;
  1820. }
  1821. break;
  1822. case chip_504x:
  1823. case chip_508x:
  1824. hpriv->ops = &mv5xxx_ops;
  1825. hp_flags |= MV_HP_50XX;
  1826. switch (rev_id) {
  1827. case 0x0:
  1828. hp_flags |= MV_HP_ERRATA_50XXB0;
  1829. break;
  1830. case 0x3:
  1831. hp_flags |= MV_HP_ERRATA_50XXB2;
  1832. break;
  1833. default:
  1834. dev_printk(KERN_WARNING, &pdev->dev,
  1835. "Applying B2 workarounds to unknown rev\n");
  1836. hp_flags |= MV_HP_ERRATA_50XXB2;
  1837. break;
  1838. }
  1839. break;
  1840. case chip_604x:
  1841. case chip_608x:
  1842. hpriv->ops = &mv6xxx_ops;
  1843. switch (rev_id) {
  1844. case 0x7:
  1845. hp_flags |= MV_HP_ERRATA_60X1B2;
  1846. break;
  1847. case 0x9:
  1848. hp_flags |= MV_HP_ERRATA_60X1C0;
  1849. break;
  1850. default:
  1851. dev_printk(KERN_WARNING, &pdev->dev,
  1852. "Applying B2 workarounds to unknown rev\n");
  1853. hp_flags |= MV_HP_ERRATA_60X1B2;
  1854. break;
  1855. }
  1856. break;
  1857. case chip_7042:
  1858. case chip_6042:
  1859. hpriv->ops = &mv6xxx_ops;
  1860. hp_flags |= MV_HP_GEN_IIE;
  1861. switch (rev_id) {
  1862. case 0x0:
  1863. hp_flags |= MV_HP_ERRATA_XX42A0;
  1864. break;
  1865. case 0x1:
  1866. hp_flags |= MV_HP_ERRATA_60X1C0;
  1867. break;
  1868. default:
  1869. dev_printk(KERN_WARNING, &pdev->dev,
  1870. "Applying 60X1C0 workarounds to unknown rev\n");
  1871. hp_flags |= MV_HP_ERRATA_60X1C0;
  1872. break;
  1873. }
  1874. break;
  1875. default:
  1876. printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
  1877. return 1;
  1878. }
  1879. hpriv->hp_flags = hp_flags;
  1880. return 0;
  1881. }
  1882. /**
  1883. * mv_init_host - Perform some early initialization of the host.
  1884. * @pdev: host PCI device
  1885. * @probe_ent: early data struct representing the host
  1886. *
  1887. * If possible, do an early global reset of the host. Then do
  1888. * our port init and clear/unmask all/relevant host interrupts.
  1889. *
  1890. * LOCKING:
  1891. * Inherited from caller.
  1892. */
  1893. static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
  1894. unsigned int board_idx)
  1895. {
  1896. int rc = 0, n_hc, port, hc;
  1897. void __iomem *mmio = probe_ent->mmio_base;
  1898. struct mv_host_priv *hpriv = probe_ent->private_data;
  1899. /* global interrupt mask */
  1900. writel(0, mmio + HC_MAIN_IRQ_MASK_OFS);
  1901. rc = mv_chip_id(pdev, hpriv, board_idx);
  1902. if (rc)
  1903. goto done;
  1904. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1905. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1906. for (port = 0; port < probe_ent->n_ports; port++)
  1907. hpriv->ops->read_preamp(hpriv, port, mmio);
  1908. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  1909. if (rc)
  1910. goto done;
  1911. hpriv->ops->reset_flash(hpriv, mmio);
  1912. hpriv->ops->reset_bus(pdev, mmio);
  1913. hpriv->ops->enable_leds(hpriv, mmio);
  1914. for (port = 0; port < probe_ent->n_ports; port++) {
  1915. if (IS_60XX(hpriv)) {
  1916. void __iomem *port_mmio = mv_port_base(mmio, port);
  1917. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1918. ifctl |= (1 << 7); /* enable gen2i speed */
  1919. ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
  1920. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1921. }
  1922. hpriv->ops->phy_errata(hpriv, mmio, port);
  1923. }
  1924. for (port = 0; port < probe_ent->n_ports; port++) {
  1925. void __iomem *port_mmio = mv_port_base(mmio, port);
  1926. mv_port_init(&probe_ent->port[port], port_mmio);
  1927. }
  1928. for (hc = 0; hc < n_hc; hc++) {
  1929. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1930. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1931. "(before clear)=0x%08x\n", hc,
  1932. readl(hc_mmio + HC_CFG_OFS),
  1933. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1934. /* Clear any currently outstanding hc interrupt conditions */
  1935. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1936. }
  1937. /* Clear any currently outstanding host interrupt conditions */
  1938. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1939. /* and unmask interrupt generation for host regs */
  1940. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1941. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1942. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1943. "PCI int cause/mask=0x%08x/0x%08x\n",
  1944. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1945. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1946. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1947. readl(mmio + PCI_IRQ_MASK_OFS));
  1948. done:
  1949. return rc;
  1950. }
  1951. /**
  1952. * mv_print_info - Dump key info to kernel log for perusal.
  1953. * @probe_ent: early data struct representing the host
  1954. *
  1955. * FIXME: complete this.
  1956. *
  1957. * LOCKING:
  1958. * Inherited from caller.
  1959. */
  1960. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1961. {
  1962. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1963. struct mv_host_priv *hpriv = probe_ent->private_data;
  1964. u8 rev_id, scc;
  1965. const char *scc_s;
  1966. /* Use this to determine the HW stepping of the chip so we know
  1967. * what errata to workaround
  1968. */
  1969. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1970. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1971. if (scc == 0)
  1972. scc_s = "SCSI";
  1973. else if (scc == 0x01)
  1974. scc_s = "RAID";
  1975. else
  1976. scc_s = "unknown";
  1977. dev_printk(KERN_INFO, &pdev->dev,
  1978. "%u slots %u ports %s mode IRQ via %s\n",
  1979. (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1980. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1981. }
  1982. /**
  1983. * mv_init_one - handle a positive probe of a Marvell host
  1984. * @pdev: PCI device found
  1985. * @ent: PCI device ID entry for the matched host
  1986. *
  1987. * LOCKING:
  1988. * Inherited from caller.
  1989. */
  1990. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1991. {
  1992. static int printed_version = 0;
  1993. struct ata_probe_ent *probe_ent = NULL;
  1994. struct mv_host_priv *hpriv;
  1995. unsigned int board_idx = (unsigned int)ent->driver_data;
  1996. void __iomem *mmio_base;
  1997. int pci_dev_busy = 0, rc;
  1998. if (!printed_version++)
  1999. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  2000. rc = pci_enable_device(pdev);
  2001. if (rc) {
  2002. return rc;
  2003. }
  2004. pci_set_master(pdev);
  2005. rc = pci_request_regions(pdev, DRV_NAME);
  2006. if (rc) {
  2007. pci_dev_busy = 1;
  2008. goto err_out;
  2009. }
  2010. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  2011. if (probe_ent == NULL) {
  2012. rc = -ENOMEM;
  2013. goto err_out_regions;
  2014. }
  2015. memset(probe_ent, 0, sizeof(*probe_ent));
  2016. probe_ent->dev = pci_dev_to_dev(pdev);
  2017. INIT_LIST_HEAD(&probe_ent->node);
  2018. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  2019. if (mmio_base == NULL) {
  2020. rc = -ENOMEM;
  2021. goto err_out_free_ent;
  2022. }
  2023. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  2024. if (!hpriv) {
  2025. rc = -ENOMEM;
  2026. goto err_out_iounmap;
  2027. }
  2028. memset(hpriv, 0, sizeof(*hpriv));
  2029. probe_ent->sht = mv_port_info[board_idx].sht;
  2030. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  2031. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  2032. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  2033. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  2034. probe_ent->irq = pdev->irq;
  2035. probe_ent->irq_flags = IRQF_SHARED;
  2036. probe_ent->mmio_base = mmio_base;
  2037. probe_ent->private_data = hpriv;
  2038. /* initialize adapter */
  2039. rc = mv_init_host(pdev, probe_ent, board_idx);
  2040. if (rc) {
  2041. goto err_out_hpriv;
  2042. }
  2043. /* Enable interrupts */
  2044. if (msi && pci_enable_msi(pdev) == 0) {
  2045. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  2046. } else {
  2047. pci_intx(pdev, 1);
  2048. }
  2049. mv_dump_pci_cfg(pdev, 0x68);
  2050. mv_print_info(probe_ent);
  2051. if (ata_device_add(probe_ent) == 0) {
  2052. rc = -ENODEV; /* No devices discovered */
  2053. goto err_out_dev_add;
  2054. }
  2055. kfree(probe_ent);
  2056. return 0;
  2057. err_out_dev_add:
  2058. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  2059. pci_disable_msi(pdev);
  2060. } else {
  2061. pci_intx(pdev, 0);
  2062. }
  2063. err_out_hpriv:
  2064. kfree(hpriv);
  2065. err_out_iounmap:
  2066. pci_iounmap(pdev, mmio_base);
  2067. err_out_free_ent:
  2068. kfree(probe_ent);
  2069. err_out_regions:
  2070. pci_release_regions(pdev);
  2071. err_out:
  2072. if (!pci_dev_busy) {
  2073. pci_disable_device(pdev);
  2074. }
  2075. return rc;
  2076. }
  2077. static int __init mv_init(void)
  2078. {
  2079. return pci_module_init(&mv_pci_driver);
  2080. }
  2081. static void __exit mv_exit(void)
  2082. {
  2083. pci_unregister_driver(&mv_pci_driver);
  2084. }
  2085. MODULE_AUTHOR("Brett Russ");
  2086. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  2087. MODULE_LICENSE("GPL");
  2088. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  2089. MODULE_VERSION(DRV_VERSION);
  2090. module_param(msi, int, 0444);
  2091. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  2092. module_init(mv_init);
  2093. module_exit(mv_exit);