sata_mv.c 52 KB

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