sata_mv.c 62 KB

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