sata_mv.c 64 KB

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