sata_mv.c 50 KB

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