sata_mv.c 63 KB

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