sata_sil24.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * sata_sil24.c - Driver for Silicon Image 3124/3132 SATA-2 controllers
  3. *
  4. * Copyright 2005 Tejun Heo
  5. *
  6. * Based on preview driver from Silicon Image.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/device.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <linux/libata.h>
  30. #define DRV_NAME "sata_sil24"
  31. #define DRV_VERSION "1.0"
  32. /*
  33. * Port request block (PRB) 32 bytes
  34. */
  35. struct sil24_prb {
  36. __le16 ctrl;
  37. __le16 prot;
  38. __le32 rx_cnt;
  39. u8 fis[6 * 4];
  40. };
  41. /*
  42. * Scatter gather entry (SGE) 16 bytes
  43. */
  44. struct sil24_sge {
  45. __le64 addr;
  46. __le32 cnt;
  47. __le32 flags;
  48. };
  49. /*
  50. * Port multiplier
  51. */
  52. struct sil24_port_multiplier {
  53. __le32 diag;
  54. __le32 sactive;
  55. };
  56. enum {
  57. SIL24_HOST_BAR = 0,
  58. SIL24_PORT_BAR = 2,
  59. /*
  60. * Global controller registers (128 bytes @ BAR0)
  61. */
  62. /* 32 bit regs */
  63. HOST_SLOT_STAT = 0x00, /* 32 bit slot stat * 4 */
  64. HOST_CTRL = 0x40,
  65. HOST_IRQ_STAT = 0x44,
  66. HOST_PHY_CFG = 0x48,
  67. HOST_BIST_CTRL = 0x50,
  68. HOST_BIST_PTRN = 0x54,
  69. HOST_BIST_STAT = 0x58,
  70. HOST_MEM_BIST_STAT = 0x5c,
  71. HOST_FLASH_CMD = 0x70,
  72. /* 8 bit regs */
  73. HOST_FLASH_DATA = 0x74,
  74. HOST_TRANSITION_DETECT = 0x75,
  75. HOST_GPIO_CTRL = 0x76,
  76. HOST_I2C_ADDR = 0x78, /* 32 bit */
  77. HOST_I2C_DATA = 0x7c,
  78. HOST_I2C_XFER_CNT = 0x7e,
  79. HOST_I2C_CTRL = 0x7f,
  80. /* HOST_SLOT_STAT bits */
  81. HOST_SSTAT_ATTN = (1 << 31),
  82. /* HOST_CTRL bits */
  83. HOST_CTRL_M66EN = (1 << 16), /* M66EN PCI bus signal */
  84. HOST_CTRL_TRDY = (1 << 17), /* latched PCI TRDY */
  85. HOST_CTRL_STOP = (1 << 18), /* latched PCI STOP */
  86. HOST_CTRL_DEVSEL = (1 << 19), /* latched PCI DEVSEL */
  87. HOST_CTRL_REQ64 = (1 << 20), /* latched PCI REQ64 */
  88. HOST_CTRL_GLOBAL_RST = (1 << 31), /* global reset */
  89. /*
  90. * Port registers
  91. * (8192 bytes @ +0x0000, +0x2000, +0x4000 and +0x6000 @ BAR2)
  92. */
  93. PORT_REGS_SIZE = 0x2000,
  94. PORT_LRAM = 0x0000, /* 31 LRAM slots and PMP regs */
  95. PORT_LRAM_SLOT_SZ = 0x0080, /* 32 bytes PRB + 2 SGE, ACT... */
  96. PORT_PMP = 0x0f80, /* 8 bytes PMP * 16 (128 bytes) */
  97. PORT_PMP_STATUS = 0x0000, /* port device status offset */
  98. PORT_PMP_QACTIVE = 0x0004, /* port device QActive offset */
  99. PORT_PMP_SIZE = 0x0008, /* 8 bytes per PMP */
  100. /* 32 bit regs */
  101. PORT_CTRL_STAT = 0x1000, /* write: ctrl-set, read: stat */
  102. PORT_CTRL_CLR = 0x1004, /* write: ctrl-clear */
  103. PORT_IRQ_STAT = 0x1008, /* high: status, low: interrupt */
  104. PORT_IRQ_ENABLE_SET = 0x1010, /* write: enable-set */
  105. PORT_IRQ_ENABLE_CLR = 0x1014, /* write: enable-clear */
  106. PORT_ACTIVATE_UPPER_ADDR= 0x101c,
  107. PORT_EXEC_FIFO = 0x1020, /* command execution fifo */
  108. PORT_CMD_ERR = 0x1024, /* command error number */
  109. PORT_FIS_CFG = 0x1028,
  110. PORT_FIFO_THRES = 0x102c,
  111. /* 16 bit regs */
  112. PORT_DECODE_ERR_CNT = 0x1040,
  113. PORT_DECODE_ERR_THRESH = 0x1042,
  114. PORT_CRC_ERR_CNT = 0x1044,
  115. PORT_CRC_ERR_THRESH = 0x1046,
  116. PORT_HSHK_ERR_CNT = 0x1048,
  117. PORT_HSHK_ERR_THRESH = 0x104a,
  118. /* 32 bit regs */
  119. PORT_PHY_CFG = 0x1050,
  120. PORT_SLOT_STAT = 0x1800,
  121. PORT_CMD_ACTIVATE = 0x1c00, /* 64 bit cmd activate * 31 (248 bytes) */
  122. PORT_CONTEXT = 0x1e04,
  123. PORT_EXEC_DIAG = 0x1e00, /* 32bit exec diag * 16 (64 bytes, 0-10 used on 3124) */
  124. PORT_PSD_DIAG = 0x1e40, /* 32bit psd diag * 16 (64 bytes, 0-8 used on 3124) */
  125. PORT_SCONTROL = 0x1f00,
  126. PORT_SSTATUS = 0x1f04,
  127. PORT_SERROR = 0x1f08,
  128. PORT_SACTIVE = 0x1f0c,
  129. /* PORT_CTRL_STAT bits */
  130. PORT_CS_PORT_RST = (1 << 0), /* port reset */
  131. PORT_CS_DEV_RST = (1 << 1), /* device reset */
  132. PORT_CS_INIT = (1 << 2), /* port initialize */
  133. PORT_CS_IRQ_WOC = (1 << 3), /* interrupt write one to clear */
  134. PORT_CS_CDB16 = (1 << 5), /* 0=12b cdb, 1=16b cdb */
  135. PORT_CS_PMP_RESUME = (1 << 6), /* PMP resume */
  136. PORT_CS_32BIT_ACTV = (1 << 10), /* 32-bit activation */
  137. PORT_CS_PMP_EN = (1 << 13), /* port multiplier enable */
  138. PORT_CS_RDY = (1 << 31), /* port ready to accept commands */
  139. /* PORT_IRQ_STAT/ENABLE_SET/CLR */
  140. /* bits[11:0] are masked */
  141. PORT_IRQ_COMPLETE = (1 << 0), /* command(s) completed */
  142. PORT_IRQ_ERROR = (1 << 1), /* command execution error */
  143. PORT_IRQ_PORTRDY_CHG = (1 << 2), /* port ready change */
  144. PORT_IRQ_PWR_CHG = (1 << 3), /* power management change */
  145. PORT_IRQ_PHYRDY_CHG = (1 << 4), /* PHY ready change */
  146. PORT_IRQ_COMWAKE = (1 << 5), /* COMWAKE received */
  147. PORT_IRQ_UNK_FIS = (1 << 6), /* unknown FIS received */
  148. PORT_IRQ_DEV_XCHG = (1 << 7), /* device exchanged */
  149. PORT_IRQ_8B10B = (1 << 8), /* 8b/10b decode error threshold */
  150. PORT_IRQ_CRC = (1 << 9), /* CRC error threshold */
  151. PORT_IRQ_HANDSHAKE = (1 << 10), /* handshake error threshold */
  152. PORT_IRQ_SDB_NOTIFY = (1 << 11), /* SDB notify received */
  153. DEF_PORT_IRQ = PORT_IRQ_COMPLETE | PORT_IRQ_ERROR |
  154. PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG |
  155. PORT_IRQ_UNK_FIS,
  156. /* bits[27:16] are unmasked (raw) */
  157. PORT_IRQ_RAW_SHIFT = 16,
  158. PORT_IRQ_MASKED_MASK = 0x7ff,
  159. PORT_IRQ_RAW_MASK = (0x7ff << PORT_IRQ_RAW_SHIFT),
  160. /* ENABLE_SET/CLR specific, intr steering - 2 bit field */
  161. PORT_IRQ_STEER_SHIFT = 30,
  162. PORT_IRQ_STEER_MASK = (3 << PORT_IRQ_STEER_SHIFT),
  163. /* PORT_CMD_ERR constants */
  164. PORT_CERR_DEV = 1, /* Error bit in D2H Register FIS */
  165. PORT_CERR_SDB = 2, /* Error bit in SDB FIS */
  166. PORT_CERR_DATA = 3, /* Error in data FIS not detected by dev */
  167. PORT_CERR_SEND = 4, /* Initial cmd FIS transmission failure */
  168. PORT_CERR_INCONSISTENT = 5, /* Protocol mismatch */
  169. PORT_CERR_DIRECTION = 6, /* Data direction mismatch */
  170. PORT_CERR_UNDERRUN = 7, /* Ran out of SGEs while writing */
  171. PORT_CERR_OVERRUN = 8, /* Ran out of SGEs while reading */
  172. PORT_CERR_PKT_PROT = 11, /* DIR invalid in 1st PIO setup of ATAPI */
  173. PORT_CERR_SGT_BOUNDARY = 16, /* PLD ecode 00 - SGT not on qword boundary */
  174. PORT_CERR_SGT_TGTABRT = 17, /* PLD ecode 01 - target abort */
  175. PORT_CERR_SGT_MSTABRT = 18, /* PLD ecode 10 - master abort */
  176. PORT_CERR_SGT_PCIPERR = 19, /* PLD ecode 11 - PCI parity err while fetching SGT */
  177. PORT_CERR_CMD_BOUNDARY = 24, /* ctrl[15:13] 001 - PRB not on qword boundary */
  178. PORT_CERR_CMD_TGTABRT = 25, /* ctrl[15:13] 010 - target abort */
  179. PORT_CERR_CMD_MSTABRT = 26, /* ctrl[15:13] 100 - master abort */
  180. PORT_CERR_CMD_PCIPERR = 27, /* ctrl[15:13] 110 - PCI parity err while fetching PRB */
  181. PORT_CERR_XFR_UNDEF = 32, /* PSD ecode 00 - undefined */
  182. PORT_CERR_XFR_TGTABRT = 33, /* PSD ecode 01 - target abort */
  183. PORT_CERR_XFR_MSTABRT = 34, /* PSD ecode 10 - master abort */
  184. PORT_CERR_XFR_PCIPERR = 35, /* PSD ecode 11 - PCI prity err during transfer */
  185. PORT_CERR_SENDSERVICE = 36, /* FIS received while sending service */
  186. /* bits of PRB control field */
  187. PRB_CTRL_PROTOCOL = (1 << 0), /* override def. ATA protocol */
  188. PRB_CTRL_PACKET_READ = (1 << 4), /* PACKET cmd read */
  189. PRB_CTRL_PACKET_WRITE = (1 << 5), /* PACKET cmd write */
  190. PRB_CTRL_NIEN = (1 << 6), /* Mask completion irq */
  191. PRB_CTRL_SRST = (1 << 7), /* Soft reset request (ign BSY?) */
  192. /* PRB protocol field */
  193. PRB_PROT_PACKET = (1 << 0),
  194. PRB_PROT_TCQ = (1 << 1),
  195. PRB_PROT_NCQ = (1 << 2),
  196. PRB_PROT_READ = (1 << 3),
  197. PRB_PROT_WRITE = (1 << 4),
  198. PRB_PROT_TRANSPARENT = (1 << 5),
  199. /*
  200. * Other constants
  201. */
  202. SGE_TRM = (1 << 31), /* Last SGE in chain */
  203. SGE_LNK = (1 << 30), /* linked list
  204. Points to SGT, not SGE */
  205. SGE_DRD = (1 << 29), /* discard data read (/dev/null)
  206. data address ignored */
  207. SIL24_MAX_CMDS = 31,
  208. /* board id */
  209. BID_SIL3124 = 0,
  210. BID_SIL3132 = 1,
  211. BID_SIL3131 = 2,
  212. /* host flags */
  213. SIL24_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  214. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  215. ATA_FLAG_NCQ | ATA_FLAG_SKIP_D2H_BSY |
  216. ATA_FLAG_ACPI_SATA,
  217. SIL24_FLAG_PCIX_IRQ_WOC = (1 << 24), /* IRQ loss errata on PCI-X */
  218. IRQ_STAT_4PORTS = 0xf,
  219. };
  220. struct sil24_ata_block {
  221. struct sil24_prb prb;
  222. struct sil24_sge sge[LIBATA_MAX_PRD];
  223. };
  224. struct sil24_atapi_block {
  225. struct sil24_prb prb;
  226. u8 cdb[16];
  227. struct sil24_sge sge[LIBATA_MAX_PRD - 1];
  228. };
  229. union sil24_cmd_block {
  230. struct sil24_ata_block ata;
  231. struct sil24_atapi_block atapi;
  232. };
  233. static struct sil24_cerr_info {
  234. unsigned int err_mask, action;
  235. const char *desc;
  236. } sil24_cerr_db[] = {
  237. [0] = { AC_ERR_DEV, ATA_EH_REVALIDATE,
  238. "device error" },
  239. [PORT_CERR_DEV] = { AC_ERR_DEV, ATA_EH_REVALIDATE,
  240. "device error via D2H FIS" },
  241. [PORT_CERR_SDB] = { AC_ERR_DEV, ATA_EH_REVALIDATE,
  242. "device error via SDB FIS" },
  243. [PORT_CERR_DATA] = { AC_ERR_ATA_BUS, ATA_EH_SOFTRESET,
  244. "error in data FIS" },
  245. [PORT_CERR_SEND] = { AC_ERR_ATA_BUS, ATA_EH_SOFTRESET,
  246. "failed to transmit command FIS" },
  247. [PORT_CERR_INCONSISTENT] = { AC_ERR_HSM, ATA_EH_SOFTRESET,
  248. "protocol mismatch" },
  249. [PORT_CERR_DIRECTION] = { AC_ERR_HSM, ATA_EH_SOFTRESET,
  250. "data directon mismatch" },
  251. [PORT_CERR_UNDERRUN] = { AC_ERR_HSM, ATA_EH_SOFTRESET,
  252. "ran out of SGEs while writing" },
  253. [PORT_CERR_OVERRUN] = { AC_ERR_HSM, ATA_EH_SOFTRESET,
  254. "ran out of SGEs while reading" },
  255. [PORT_CERR_PKT_PROT] = { AC_ERR_HSM, ATA_EH_SOFTRESET,
  256. "invalid data directon for ATAPI CDB" },
  257. [PORT_CERR_SGT_BOUNDARY] = { AC_ERR_SYSTEM, ATA_EH_SOFTRESET,
  258. "SGT no on qword boundary" },
  259. [PORT_CERR_SGT_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  260. "PCI target abort while fetching SGT" },
  261. [PORT_CERR_SGT_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  262. "PCI master abort while fetching SGT" },
  263. [PORT_CERR_SGT_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  264. "PCI parity error while fetching SGT" },
  265. [PORT_CERR_CMD_BOUNDARY] = { AC_ERR_SYSTEM, ATA_EH_SOFTRESET,
  266. "PRB not on qword boundary" },
  267. [PORT_CERR_CMD_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  268. "PCI target abort while fetching PRB" },
  269. [PORT_CERR_CMD_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  270. "PCI master abort while fetching PRB" },
  271. [PORT_CERR_CMD_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  272. "PCI parity error while fetching PRB" },
  273. [PORT_CERR_XFR_UNDEF] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  274. "undefined error while transferring data" },
  275. [PORT_CERR_XFR_TGTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  276. "PCI target abort while transferring data" },
  277. [PORT_CERR_XFR_MSTABRT] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  278. "PCI master abort while transferring data" },
  279. [PORT_CERR_XFR_PCIPERR] = { AC_ERR_HOST_BUS, ATA_EH_SOFTRESET,
  280. "PCI parity error while transferring data" },
  281. [PORT_CERR_SENDSERVICE] = { AC_ERR_HSM, ATA_EH_SOFTRESET,
  282. "FIS received while sending service FIS" },
  283. };
  284. /*
  285. * ap->private_data
  286. *
  287. * The preview driver always returned 0 for status. We emulate it
  288. * here from the previous interrupt.
  289. */
  290. struct sil24_port_priv {
  291. union sil24_cmd_block *cmd_block; /* 32 cmd blocks */
  292. dma_addr_t cmd_block_dma; /* DMA base addr for them */
  293. struct ata_taskfile tf; /* Cached taskfile registers */
  294. };
  295. static void sil24_dev_config(struct ata_device *dev);
  296. static u8 sil24_check_status(struct ata_port *ap);
  297. static int sil24_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val);
  298. static int sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val);
  299. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  300. static void sil24_qc_prep(struct ata_queued_cmd *qc);
  301. static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc);
  302. static void sil24_irq_clear(struct ata_port *ap);
  303. static void sil24_freeze(struct ata_port *ap);
  304. static void sil24_thaw(struct ata_port *ap);
  305. static void sil24_error_handler(struct ata_port *ap);
  306. static void sil24_post_internal_cmd(struct ata_queued_cmd *qc);
  307. static int sil24_port_start(struct ata_port *ap);
  308. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  309. #ifdef CONFIG_PM
  310. static int sil24_pci_device_resume(struct pci_dev *pdev);
  311. #endif
  312. static const struct pci_device_id sil24_pci_tbl[] = {
  313. { PCI_VDEVICE(CMD, 0x3124), BID_SIL3124 },
  314. { PCI_VDEVICE(INTEL, 0x3124), BID_SIL3124 },
  315. { PCI_VDEVICE(CMD, 0x3132), BID_SIL3132 },
  316. { PCI_VDEVICE(CMD, 0x0242), BID_SIL3132 },
  317. { PCI_VDEVICE(CMD, 0x3131), BID_SIL3131 },
  318. { PCI_VDEVICE(CMD, 0x3531), BID_SIL3131 },
  319. { } /* terminate list */
  320. };
  321. static struct pci_driver sil24_pci_driver = {
  322. .name = DRV_NAME,
  323. .id_table = sil24_pci_tbl,
  324. .probe = sil24_init_one,
  325. .remove = ata_pci_remove_one,
  326. #ifdef CONFIG_PM
  327. .suspend = ata_pci_device_suspend,
  328. .resume = sil24_pci_device_resume,
  329. #endif
  330. };
  331. static struct scsi_host_template sil24_sht = {
  332. .module = THIS_MODULE,
  333. .name = DRV_NAME,
  334. .ioctl = ata_scsi_ioctl,
  335. .queuecommand = ata_scsi_queuecmd,
  336. .change_queue_depth = ata_scsi_change_queue_depth,
  337. .can_queue = SIL24_MAX_CMDS,
  338. .this_id = ATA_SHT_THIS_ID,
  339. .sg_tablesize = LIBATA_MAX_PRD,
  340. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  341. .emulated = ATA_SHT_EMULATED,
  342. .use_clustering = ATA_SHT_USE_CLUSTERING,
  343. .proc_name = DRV_NAME,
  344. .dma_boundary = ATA_DMA_BOUNDARY,
  345. .slave_configure = ata_scsi_slave_config,
  346. .slave_destroy = ata_scsi_slave_destroy,
  347. .bios_param = ata_std_bios_param,
  348. };
  349. static const struct ata_port_operations sil24_ops = {
  350. .port_disable = ata_port_disable,
  351. .dev_config = sil24_dev_config,
  352. .check_status = sil24_check_status,
  353. .check_altstatus = sil24_check_status,
  354. .dev_select = ata_noop_dev_select,
  355. .tf_read = sil24_tf_read,
  356. .qc_prep = sil24_qc_prep,
  357. .qc_issue = sil24_qc_issue,
  358. .irq_clear = sil24_irq_clear,
  359. .irq_on = ata_dummy_irq_on,
  360. .irq_ack = ata_dummy_irq_ack,
  361. .scr_read = sil24_scr_read,
  362. .scr_write = sil24_scr_write,
  363. .freeze = sil24_freeze,
  364. .thaw = sil24_thaw,
  365. .error_handler = sil24_error_handler,
  366. .post_internal_cmd = sil24_post_internal_cmd,
  367. .port_start = sil24_port_start,
  368. };
  369. /*
  370. * Use bits 30-31 of port_flags to encode available port numbers.
  371. * Current maxium is 4.
  372. */
  373. #define SIL24_NPORTS2FLAG(nports) ((((unsigned)(nports) - 1) & 0x3) << 30)
  374. #define SIL24_FLAG2NPORTS(flag) ((((flag) >> 30) & 0x3) + 1)
  375. static const struct ata_port_info sil24_port_info[] = {
  376. /* sil_3124 */
  377. {
  378. .flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(4) |
  379. SIL24_FLAG_PCIX_IRQ_WOC,
  380. .pio_mask = 0x1f, /* pio0-4 */
  381. .mwdma_mask = 0x07, /* mwdma0-2 */
  382. .udma_mask = ATA_UDMA5, /* udma0-5 */
  383. .port_ops = &sil24_ops,
  384. },
  385. /* sil_3132 */
  386. {
  387. .flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(2),
  388. .pio_mask = 0x1f, /* pio0-4 */
  389. .mwdma_mask = 0x07, /* mwdma0-2 */
  390. .udma_mask = ATA_UDMA5, /* udma0-5 */
  391. .port_ops = &sil24_ops,
  392. },
  393. /* sil_3131/sil_3531 */
  394. {
  395. .flags = SIL24_COMMON_FLAGS | SIL24_NPORTS2FLAG(1),
  396. .pio_mask = 0x1f, /* pio0-4 */
  397. .mwdma_mask = 0x07, /* mwdma0-2 */
  398. .udma_mask = ATA_UDMA5, /* udma0-5 */
  399. .port_ops = &sil24_ops,
  400. },
  401. };
  402. static int sil24_tag(int tag)
  403. {
  404. if (unlikely(ata_tag_internal(tag)))
  405. return 0;
  406. return tag;
  407. }
  408. static void sil24_dev_config(struct ata_device *dev)
  409. {
  410. void __iomem *port = dev->link->ap->ioaddr.cmd_addr;
  411. if (dev->cdb_len == 16)
  412. writel(PORT_CS_CDB16, port + PORT_CTRL_STAT);
  413. else
  414. writel(PORT_CS_CDB16, port + PORT_CTRL_CLR);
  415. }
  416. static void sil24_read_tf(struct ata_port *ap, int tag, struct ata_taskfile *tf)
  417. {
  418. void __iomem *port = ap->ioaddr.cmd_addr;
  419. struct sil24_prb __iomem *prb;
  420. u8 fis[6 * 4];
  421. prb = port + PORT_LRAM + sil24_tag(tag) * PORT_LRAM_SLOT_SZ;
  422. memcpy_fromio(fis, prb->fis, sizeof(fis));
  423. ata_tf_from_fis(fis, tf);
  424. }
  425. static u8 sil24_check_status(struct ata_port *ap)
  426. {
  427. struct sil24_port_priv *pp = ap->private_data;
  428. return pp->tf.command;
  429. }
  430. static int sil24_scr_map[] = {
  431. [SCR_CONTROL] = 0,
  432. [SCR_STATUS] = 1,
  433. [SCR_ERROR] = 2,
  434. [SCR_ACTIVE] = 3,
  435. };
  436. static int sil24_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val)
  437. {
  438. void __iomem *scr_addr = ap->ioaddr.scr_addr;
  439. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  440. void __iomem *addr;
  441. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  442. *val = readl(scr_addr + sil24_scr_map[sc_reg] * 4);
  443. return 0;
  444. }
  445. return -EINVAL;
  446. }
  447. static int sil24_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  448. {
  449. void __iomem *scr_addr = ap->ioaddr.scr_addr;
  450. if (sc_reg < ARRAY_SIZE(sil24_scr_map)) {
  451. void __iomem *addr;
  452. addr = scr_addr + sil24_scr_map[sc_reg] * 4;
  453. writel(val, scr_addr + sil24_scr_map[sc_reg] * 4);
  454. return 0;
  455. }
  456. return -EINVAL;
  457. }
  458. static void sil24_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  459. {
  460. struct sil24_port_priv *pp = ap->private_data;
  461. *tf = pp->tf;
  462. }
  463. static int sil24_init_port(struct ata_port *ap)
  464. {
  465. void __iomem *port = ap->ioaddr.cmd_addr;
  466. u32 tmp;
  467. writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
  468. ata_wait_register(port + PORT_CTRL_STAT,
  469. PORT_CS_INIT, PORT_CS_INIT, 10, 100);
  470. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  471. PORT_CS_RDY, 0, 10, 100);
  472. if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
  473. return -EIO;
  474. return 0;
  475. }
  476. static int sil24_exec_polled_cmd(struct ata_port *ap, int pmp,
  477. const struct ata_taskfile *tf,
  478. int is_cmd, u32 ctrl,
  479. unsigned long timeout_msec)
  480. {
  481. void __iomem *port = ap->ioaddr.cmd_addr;
  482. struct sil24_port_priv *pp = ap->private_data;
  483. struct sil24_prb *prb = &pp->cmd_block[0].ata.prb;
  484. dma_addr_t paddr = pp->cmd_block_dma;
  485. u32 irq_enabled, irq_mask, irq_stat;
  486. int rc;
  487. prb->ctrl = cpu_to_le16(ctrl);
  488. ata_tf_to_fis(tf, pmp, is_cmd, prb->fis);
  489. /* temporarily plug completion and error interrupts */
  490. irq_enabled = readl(port + PORT_IRQ_ENABLE_SET);
  491. writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
  492. writel((u32)paddr, port + PORT_CMD_ACTIVATE);
  493. writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + 4);
  494. irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
  495. irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask, 0x0,
  496. 10, timeout_msec);
  497. writel(irq_mask, port + PORT_IRQ_STAT); /* clear IRQs */
  498. irq_stat >>= PORT_IRQ_RAW_SHIFT;
  499. if (irq_stat & PORT_IRQ_COMPLETE)
  500. rc = 0;
  501. else {
  502. /* force port into known state */
  503. sil24_init_port(ap);
  504. if (irq_stat & PORT_IRQ_ERROR)
  505. rc = -EIO;
  506. else
  507. rc = -EBUSY;
  508. }
  509. /* restore IRQ enabled */
  510. writel(irq_enabled, port + PORT_IRQ_ENABLE_SET);
  511. return rc;
  512. }
  513. static int sil24_do_softreset(struct ata_port *ap, unsigned int *class,
  514. int pmp, unsigned long deadline)
  515. {
  516. unsigned long timeout_msec = 0;
  517. struct ata_taskfile tf;
  518. const char *reason;
  519. int rc;
  520. DPRINTK("ENTER\n");
  521. if (ata_port_offline(ap)) {
  522. DPRINTK("PHY reports no device\n");
  523. *class = ATA_DEV_NONE;
  524. goto out;
  525. }
  526. /* put the port into known state */
  527. if (sil24_init_port(ap)) {
  528. reason ="port not ready";
  529. goto err;
  530. }
  531. /* do SRST */
  532. if (time_after(deadline, jiffies))
  533. timeout_msec = jiffies_to_msecs(deadline - jiffies);
  534. ata_tf_init(ap->link.device, &tf); /* doesn't really matter */
  535. rc = sil24_exec_polled_cmd(ap, pmp, &tf, 0, PRB_CTRL_SRST,
  536. timeout_msec);
  537. if (rc == -EBUSY) {
  538. reason = "timeout";
  539. goto err;
  540. } else if (rc) {
  541. reason = "SRST command error";
  542. goto err;
  543. }
  544. sil24_read_tf(ap, 0, &tf);
  545. *class = ata_dev_classify(&tf);
  546. if (*class == ATA_DEV_UNKNOWN)
  547. *class = ATA_DEV_NONE;
  548. out:
  549. DPRINTK("EXIT, class=%u\n", *class);
  550. return 0;
  551. err:
  552. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  553. return -EIO;
  554. }
  555. static int sil24_softreset(struct ata_port *ap, unsigned int *class,
  556. unsigned long deadline)
  557. {
  558. return sil24_do_softreset(ap, class, 0, deadline);
  559. }
  560. static int sil24_hardreset(struct ata_port *ap, unsigned int *class,
  561. unsigned long deadline)
  562. {
  563. void __iomem *port = ap->ioaddr.cmd_addr;
  564. const char *reason;
  565. int tout_msec, rc;
  566. u32 tmp;
  567. /* sil24 does the right thing(tm) without any protection */
  568. sata_set_spd(ap);
  569. tout_msec = 100;
  570. if (ata_port_online(ap))
  571. tout_msec = 5000;
  572. writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
  573. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  574. PORT_CS_DEV_RST, PORT_CS_DEV_RST, 10, tout_msec);
  575. /* SStatus oscillates between zero and valid status after
  576. * DEV_RST, debounce it.
  577. */
  578. rc = sata_phy_debounce(ap, sata_deb_timing_long, deadline);
  579. if (rc) {
  580. reason = "PHY debouncing failed";
  581. goto err;
  582. }
  583. if (tmp & PORT_CS_DEV_RST) {
  584. if (ata_port_offline(ap))
  585. return 0;
  586. reason = "link not ready";
  587. goto err;
  588. }
  589. /* Sil24 doesn't store signature FIS after hardreset, so we
  590. * can't wait for BSY to clear. Some devices take a long time
  591. * to get ready and those devices will choke if we don't wait
  592. * for BSY clearance here. Tell libata to perform follow-up
  593. * softreset.
  594. */
  595. return -EAGAIN;
  596. err:
  597. ata_port_printk(ap, KERN_ERR, "hardreset failed (%s)\n", reason);
  598. return -EIO;
  599. }
  600. static inline void sil24_fill_sg(struct ata_queued_cmd *qc,
  601. struct sil24_sge *sge)
  602. {
  603. struct scatterlist *sg;
  604. ata_for_each_sg(sg, qc) {
  605. sge->addr = cpu_to_le64(sg_dma_address(sg));
  606. sge->cnt = cpu_to_le32(sg_dma_len(sg));
  607. if (ata_sg_is_last(sg, qc))
  608. sge->flags = cpu_to_le32(SGE_TRM);
  609. else
  610. sge->flags = 0;
  611. sge++;
  612. }
  613. }
  614. static void sil24_qc_prep(struct ata_queued_cmd *qc)
  615. {
  616. struct ata_port *ap = qc->ap;
  617. struct sil24_port_priv *pp = ap->private_data;
  618. union sil24_cmd_block *cb;
  619. struct sil24_prb *prb;
  620. struct sil24_sge *sge;
  621. u16 ctrl = 0;
  622. cb = &pp->cmd_block[sil24_tag(qc->tag)];
  623. switch (qc->tf.protocol) {
  624. case ATA_PROT_PIO:
  625. case ATA_PROT_DMA:
  626. case ATA_PROT_NCQ:
  627. case ATA_PROT_NODATA:
  628. prb = &cb->ata.prb;
  629. sge = cb->ata.sge;
  630. break;
  631. case ATA_PROT_ATAPI:
  632. case ATA_PROT_ATAPI_DMA:
  633. case ATA_PROT_ATAPI_NODATA:
  634. prb = &cb->atapi.prb;
  635. sge = cb->atapi.sge;
  636. memset(cb->atapi.cdb, 0, 32);
  637. memcpy(cb->atapi.cdb, qc->cdb, qc->dev->cdb_len);
  638. if (qc->tf.protocol != ATA_PROT_ATAPI_NODATA) {
  639. if (qc->tf.flags & ATA_TFLAG_WRITE)
  640. ctrl = PRB_CTRL_PACKET_WRITE;
  641. else
  642. ctrl = PRB_CTRL_PACKET_READ;
  643. }
  644. break;
  645. default:
  646. prb = NULL; /* shut up, gcc */
  647. sge = NULL;
  648. BUG();
  649. }
  650. prb->ctrl = cpu_to_le16(ctrl);
  651. ata_tf_to_fis(&qc->tf, 0, 1, prb->fis);
  652. if (qc->flags & ATA_QCFLAG_DMAMAP)
  653. sil24_fill_sg(qc, sge);
  654. }
  655. static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc)
  656. {
  657. struct ata_port *ap = qc->ap;
  658. struct sil24_port_priv *pp = ap->private_data;
  659. void __iomem *port = ap->ioaddr.cmd_addr;
  660. unsigned int tag = sil24_tag(qc->tag);
  661. dma_addr_t paddr;
  662. void __iomem *activate;
  663. paddr = pp->cmd_block_dma + tag * sizeof(*pp->cmd_block);
  664. activate = port + PORT_CMD_ACTIVATE + tag * 8;
  665. writel((u32)paddr, activate);
  666. writel((u64)paddr >> 32, activate + 4);
  667. return 0;
  668. }
  669. static void sil24_irq_clear(struct ata_port *ap)
  670. {
  671. /* unused */
  672. }
  673. static void sil24_freeze(struct ata_port *ap)
  674. {
  675. void __iomem *port = ap->ioaddr.cmd_addr;
  676. /* Port-wide IRQ mask in HOST_CTRL doesn't really work, clear
  677. * PORT_IRQ_ENABLE instead.
  678. */
  679. writel(0xffff, port + PORT_IRQ_ENABLE_CLR);
  680. }
  681. static void sil24_thaw(struct ata_port *ap)
  682. {
  683. void __iomem *port = ap->ioaddr.cmd_addr;
  684. u32 tmp;
  685. /* clear IRQ */
  686. tmp = readl(port + PORT_IRQ_STAT);
  687. writel(tmp, port + PORT_IRQ_STAT);
  688. /* turn IRQ back on */
  689. writel(DEF_PORT_IRQ, port + PORT_IRQ_ENABLE_SET);
  690. }
  691. static void sil24_error_intr(struct ata_port *ap)
  692. {
  693. void __iomem *port = ap->ioaddr.cmd_addr;
  694. struct sil24_port_priv *pp = ap->private_data;
  695. struct ata_eh_info *ehi = &ap->link.eh_info;
  696. int freeze = 0;
  697. u32 irq_stat;
  698. /* on error, we need to clear IRQ explicitly */
  699. irq_stat = readl(port + PORT_IRQ_STAT);
  700. writel(irq_stat, port + PORT_IRQ_STAT);
  701. /* first, analyze and record host port events */
  702. ata_ehi_clear_desc(ehi);
  703. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  704. if (irq_stat & (PORT_IRQ_PHYRDY_CHG | PORT_IRQ_DEV_XCHG)) {
  705. ata_ehi_hotplugged(ehi);
  706. ata_ehi_push_desc(ehi, "%s",
  707. irq_stat & PORT_IRQ_PHYRDY_CHG ?
  708. "PHY RDY changed" : "device exchanged");
  709. freeze = 1;
  710. }
  711. if (irq_stat & PORT_IRQ_UNK_FIS) {
  712. ehi->err_mask |= AC_ERR_HSM;
  713. ehi->action |= ATA_EH_SOFTRESET;
  714. ata_ehi_push_desc(ehi, "unknown FIS");
  715. freeze = 1;
  716. }
  717. /* deal with command error */
  718. if (irq_stat & PORT_IRQ_ERROR) {
  719. struct sil24_cerr_info *ci = NULL;
  720. unsigned int err_mask = 0, action = 0;
  721. struct ata_queued_cmd *qc;
  722. u32 cerr;
  723. /* analyze CMD_ERR */
  724. cerr = readl(port + PORT_CMD_ERR);
  725. if (cerr < ARRAY_SIZE(sil24_cerr_db))
  726. ci = &sil24_cerr_db[cerr];
  727. if (ci && ci->desc) {
  728. err_mask |= ci->err_mask;
  729. action |= ci->action;
  730. ata_ehi_push_desc(ehi, "%s", ci->desc);
  731. } else {
  732. err_mask |= AC_ERR_OTHER;
  733. action |= ATA_EH_SOFTRESET;
  734. ata_ehi_push_desc(ehi, "unknown command error %d",
  735. cerr);
  736. }
  737. /* record error info */
  738. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  739. if (qc) {
  740. sil24_read_tf(ap, qc->tag, &pp->tf);
  741. qc->err_mask |= err_mask;
  742. } else
  743. ehi->err_mask |= err_mask;
  744. ehi->action |= action;
  745. }
  746. /* freeze or abort */
  747. if (freeze)
  748. ata_port_freeze(ap);
  749. else
  750. ata_port_abort(ap);
  751. }
  752. static void sil24_finish_qc(struct ata_queued_cmd *qc)
  753. {
  754. struct ata_port *ap = qc->ap;
  755. struct sil24_port_priv *pp = ap->private_data;
  756. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  757. sil24_read_tf(ap, qc->tag, &pp->tf);
  758. }
  759. static inline void sil24_host_intr(struct ata_port *ap)
  760. {
  761. void __iomem *port = ap->ioaddr.cmd_addr;
  762. u32 slot_stat, qc_active;
  763. int rc;
  764. /* If PCIX_IRQ_WOC, there's an inherent race window between
  765. * clearing IRQ pending status and reading PORT_SLOT_STAT
  766. * which may cause spurious interrupts afterwards. This is
  767. * unavoidable and much better than losing interrupts which
  768. * happens if IRQ pending is cleared after reading
  769. * PORT_SLOT_STAT.
  770. */
  771. if (ap->flags & SIL24_FLAG_PCIX_IRQ_WOC)
  772. writel(PORT_IRQ_COMPLETE, port + PORT_IRQ_STAT);
  773. slot_stat = readl(port + PORT_SLOT_STAT);
  774. if (unlikely(slot_stat & HOST_SSTAT_ATTN)) {
  775. sil24_error_intr(ap);
  776. return;
  777. }
  778. qc_active = slot_stat & ~HOST_SSTAT_ATTN;
  779. rc = ata_qc_complete_multiple(ap, qc_active, sil24_finish_qc);
  780. if (rc > 0)
  781. return;
  782. if (rc < 0) {
  783. struct ata_eh_info *ehi = &ap->link.eh_info;
  784. ehi->err_mask |= AC_ERR_HSM;
  785. ehi->action |= ATA_EH_SOFTRESET;
  786. ata_port_freeze(ap);
  787. return;
  788. }
  789. /* spurious interrupts are expected if PCIX_IRQ_WOC */
  790. if (!(ap->flags & SIL24_FLAG_PCIX_IRQ_WOC) && ata_ratelimit())
  791. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  792. "(slot_stat 0x%x active_tag %d sactive 0x%x)\n",
  793. slot_stat, ap->link.active_tag, ap->link.sactive);
  794. }
  795. static irqreturn_t sil24_interrupt(int irq, void *dev_instance)
  796. {
  797. struct ata_host *host = dev_instance;
  798. void __iomem *host_base = host->iomap[SIL24_HOST_BAR];
  799. unsigned handled = 0;
  800. u32 status;
  801. int i;
  802. status = readl(host_base + HOST_IRQ_STAT);
  803. if (status == 0xffffffff) {
  804. printk(KERN_ERR DRV_NAME ": IRQ status == 0xffffffff, "
  805. "PCI fault or device removal?\n");
  806. goto out;
  807. }
  808. if (!(status & IRQ_STAT_4PORTS))
  809. goto out;
  810. spin_lock(&host->lock);
  811. for (i = 0; i < host->n_ports; i++)
  812. if (status & (1 << i)) {
  813. struct ata_port *ap = host->ports[i];
  814. if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
  815. sil24_host_intr(ap);
  816. handled++;
  817. } else
  818. printk(KERN_ERR DRV_NAME
  819. ": interrupt from disabled port %d\n", i);
  820. }
  821. spin_unlock(&host->lock);
  822. out:
  823. return IRQ_RETVAL(handled);
  824. }
  825. static void sil24_error_handler(struct ata_port *ap)
  826. {
  827. struct ata_eh_context *ehc = &ap->link.eh_context;
  828. if (sil24_init_port(ap)) {
  829. ata_eh_freeze_port(ap);
  830. ehc->i.action |= ATA_EH_HARDRESET;
  831. }
  832. /* perform recovery */
  833. ata_do_eh(ap, ata_std_prereset, sil24_softreset, sil24_hardreset,
  834. ata_std_postreset);
  835. }
  836. static void sil24_post_internal_cmd(struct ata_queued_cmd *qc)
  837. {
  838. struct ata_port *ap = qc->ap;
  839. /* make DMA engine forget about the failed command */
  840. if (qc->flags & ATA_QCFLAG_FAILED)
  841. sil24_init_port(ap);
  842. }
  843. static int sil24_port_start(struct ata_port *ap)
  844. {
  845. struct device *dev = ap->host->dev;
  846. struct sil24_port_priv *pp;
  847. union sil24_cmd_block *cb;
  848. size_t cb_size = sizeof(*cb) * SIL24_MAX_CMDS;
  849. dma_addr_t cb_dma;
  850. int rc;
  851. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  852. if (!pp)
  853. return -ENOMEM;
  854. pp->tf.command = ATA_DRDY;
  855. cb = dmam_alloc_coherent(dev, cb_size, &cb_dma, GFP_KERNEL);
  856. if (!cb)
  857. return -ENOMEM;
  858. memset(cb, 0, cb_size);
  859. rc = ata_pad_alloc(ap, dev);
  860. if (rc)
  861. return rc;
  862. pp->cmd_block = cb;
  863. pp->cmd_block_dma = cb_dma;
  864. ap->private_data = pp;
  865. return 0;
  866. }
  867. static void sil24_init_controller(struct ata_host *host)
  868. {
  869. void __iomem *host_base = host->iomap[SIL24_HOST_BAR];
  870. void __iomem *port_base = host->iomap[SIL24_PORT_BAR];
  871. u32 tmp;
  872. int i;
  873. /* GPIO off */
  874. writel(0, host_base + HOST_FLASH_CMD);
  875. /* clear global reset & mask interrupts during initialization */
  876. writel(0, host_base + HOST_CTRL);
  877. /* init ports */
  878. for (i = 0; i < host->n_ports; i++) {
  879. void __iomem *port = port_base + i * PORT_REGS_SIZE;
  880. /* Initial PHY setting */
  881. writel(0x20c, port + PORT_PHY_CFG);
  882. /* Clear port RST */
  883. tmp = readl(port + PORT_CTRL_STAT);
  884. if (tmp & PORT_CS_PORT_RST) {
  885. writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
  886. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  887. PORT_CS_PORT_RST,
  888. PORT_CS_PORT_RST, 10, 100);
  889. if (tmp & PORT_CS_PORT_RST)
  890. dev_printk(KERN_ERR, host->dev,
  891. "failed to clear port RST\n");
  892. }
  893. /* Configure IRQ WoC */
  894. if (host->ports[0]->flags & SIL24_FLAG_PCIX_IRQ_WOC)
  895. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_STAT);
  896. else
  897. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
  898. /* Zero error counters. */
  899. writel(0x8000, port + PORT_DECODE_ERR_THRESH);
  900. writel(0x8000, port + PORT_CRC_ERR_THRESH);
  901. writel(0x8000, port + PORT_HSHK_ERR_THRESH);
  902. writel(0x0000, port + PORT_DECODE_ERR_CNT);
  903. writel(0x0000, port + PORT_CRC_ERR_CNT);
  904. writel(0x0000, port + PORT_HSHK_ERR_CNT);
  905. /* Always use 64bit activation */
  906. writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
  907. /* Clear port multiplier enable and resume bits */
  908. writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME,
  909. port + PORT_CTRL_CLR);
  910. }
  911. /* Turn on interrupts */
  912. writel(IRQ_STAT_4PORTS, host_base + HOST_CTRL);
  913. }
  914. static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  915. {
  916. static int printed_version = 0;
  917. struct ata_port_info pi = sil24_port_info[ent->driver_data];
  918. const struct ata_port_info *ppi[] = { &pi, NULL };
  919. void __iomem * const *iomap;
  920. struct ata_host *host;
  921. int i, rc;
  922. u32 tmp;
  923. if (!printed_version++)
  924. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  925. /* acquire resources */
  926. rc = pcim_enable_device(pdev);
  927. if (rc)
  928. return rc;
  929. rc = pcim_iomap_regions(pdev,
  930. (1 << SIL24_HOST_BAR) | (1 << SIL24_PORT_BAR),
  931. DRV_NAME);
  932. if (rc)
  933. return rc;
  934. iomap = pcim_iomap_table(pdev);
  935. /* apply workaround for completion IRQ loss on PCI-X errata */
  936. if (pi.flags & SIL24_FLAG_PCIX_IRQ_WOC) {
  937. tmp = readl(iomap[SIL24_HOST_BAR] + HOST_CTRL);
  938. if (tmp & (HOST_CTRL_TRDY | HOST_CTRL_STOP | HOST_CTRL_DEVSEL))
  939. dev_printk(KERN_INFO, &pdev->dev,
  940. "Applying completion IRQ loss on PCI-X "
  941. "errata fix\n");
  942. else
  943. pi.flags &= ~SIL24_FLAG_PCIX_IRQ_WOC;
  944. }
  945. /* allocate and fill host */
  946. host = ata_host_alloc_pinfo(&pdev->dev, ppi,
  947. SIL24_FLAG2NPORTS(ppi[0]->flags));
  948. if (!host)
  949. return -ENOMEM;
  950. host->iomap = iomap;
  951. for (i = 0; i < host->n_ports; i++) {
  952. void __iomem *port = iomap[SIL24_PORT_BAR] + i * PORT_REGS_SIZE;
  953. host->ports[i]->ioaddr.cmd_addr = port;
  954. host->ports[i]->ioaddr.scr_addr = port + PORT_SCONTROL;
  955. ata_std_ports(&host->ports[i]->ioaddr);
  956. }
  957. /* configure and activate the device */
  958. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  959. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  960. if (rc) {
  961. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  962. if (rc) {
  963. dev_printk(KERN_ERR, &pdev->dev,
  964. "64-bit DMA enable failed\n");
  965. return rc;
  966. }
  967. }
  968. } else {
  969. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  970. if (rc) {
  971. dev_printk(KERN_ERR, &pdev->dev,
  972. "32-bit DMA enable failed\n");
  973. return rc;
  974. }
  975. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  976. if (rc) {
  977. dev_printk(KERN_ERR, &pdev->dev,
  978. "32-bit consistent DMA enable failed\n");
  979. return rc;
  980. }
  981. }
  982. sil24_init_controller(host);
  983. pci_set_master(pdev);
  984. return ata_host_activate(host, pdev->irq, sil24_interrupt, IRQF_SHARED,
  985. &sil24_sht);
  986. }
  987. #ifdef CONFIG_PM
  988. static int sil24_pci_device_resume(struct pci_dev *pdev)
  989. {
  990. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  991. void __iomem *host_base = host->iomap[SIL24_HOST_BAR];
  992. int rc;
  993. rc = ata_pci_device_do_resume(pdev);
  994. if (rc)
  995. return rc;
  996. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND)
  997. writel(HOST_CTRL_GLOBAL_RST, host_base + HOST_CTRL);
  998. sil24_init_controller(host);
  999. ata_host_resume(host);
  1000. return 0;
  1001. }
  1002. #endif
  1003. static int __init sil24_init(void)
  1004. {
  1005. return pci_register_driver(&sil24_pci_driver);
  1006. }
  1007. static void __exit sil24_exit(void)
  1008. {
  1009. pci_unregister_driver(&sil24_pci_driver);
  1010. }
  1011. MODULE_AUTHOR("Tejun Heo");
  1012. MODULE_DESCRIPTION("Silicon Image 3124/3132 SATA low-level driver");
  1013. MODULE_LICENSE("GPL");
  1014. MODULE_DEVICE_TABLE(pci, sil24_pci_tbl);
  1015. module_init(sil24_init);
  1016. module_exit(sil24_exit);