sata_mv.c 59 KB

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