sata_sil.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * sata_sil.c - Silicon Image SATA
  4. *
  5. * Maintained by: Tejun Heo <tj@kernel.org>
  6. * Please ALWAYS copy linux-ide@vger.kernel.org
  7. * on emails.
  8. *
  9. * Copyright 2003-2005 Red Hat, Inc.
  10. * Copyright 2003 Benjamin Herrenschmidt
  11. *
  12. * libata documentation is available via 'make {ps|pdf}docs',
  13. * as Documentation/driver-api/libata.rst
  14. *
  15. * Documentation for SiI 3112:
  16. * http://gkernel.sourceforge.net/specs/sii/3112A_SiI-DS-0095-B2.pdf.bz2
  17. *
  18. * Other errata and documentation available under NDA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/device.h>
  27. #include <scsi/scsi_host.h>
  28. #include <linux/libata.h>
  29. #include <linux/dmi.h>
  30. #define DRV_NAME "sata_sil"
  31. #define DRV_VERSION "2.4"
  32. #define SIL_DMA_BOUNDARY 0x7fffffffUL
  33. enum {
  34. SIL_MMIO_BAR = 5,
  35. /*
  36. * host flags
  37. */
  38. SIL_FLAG_NO_SATA_IRQ = (1 << 28),
  39. SIL_FLAG_RERR_ON_DMA_ACT = (1 << 29),
  40. SIL_FLAG_MOD15WRITE = (1 << 30),
  41. SIL_DFL_PORT_FLAGS = ATA_FLAG_SATA,
  42. /*
  43. * Controller IDs
  44. */
  45. sil_3112 = 0,
  46. sil_3112_no_sata_irq = 1,
  47. sil_3512 = 2,
  48. sil_3114 = 3,
  49. /*
  50. * Register offsets
  51. */
  52. SIL_SYSCFG = 0x48,
  53. /*
  54. * Register bits
  55. */
  56. /* SYSCFG */
  57. SIL_MASK_IDE0_INT = (1 << 22),
  58. SIL_MASK_IDE1_INT = (1 << 23),
  59. SIL_MASK_IDE2_INT = (1 << 24),
  60. SIL_MASK_IDE3_INT = (1 << 25),
  61. SIL_MASK_2PORT = SIL_MASK_IDE0_INT | SIL_MASK_IDE1_INT,
  62. SIL_MASK_4PORT = SIL_MASK_2PORT |
  63. SIL_MASK_IDE2_INT | SIL_MASK_IDE3_INT,
  64. /* BMDMA/BMDMA2 */
  65. SIL_INTR_STEERING = (1 << 1),
  66. SIL_DMA_ENABLE = (1 << 0), /* DMA run switch */
  67. SIL_DMA_RDWR = (1 << 3), /* DMA Rd-Wr */
  68. SIL_DMA_SATA_IRQ = (1 << 4), /* OR of all SATA IRQs */
  69. SIL_DMA_ACTIVE = (1 << 16), /* DMA running */
  70. SIL_DMA_ERROR = (1 << 17), /* PCI bus error */
  71. SIL_DMA_COMPLETE = (1 << 18), /* cmd complete / IRQ pending */
  72. SIL_DMA_N_SATA_IRQ = (1 << 6), /* SATA_IRQ for the next channel */
  73. SIL_DMA_N_ACTIVE = (1 << 24), /* ACTIVE for the next channel */
  74. SIL_DMA_N_ERROR = (1 << 25), /* ERROR for the next channel */
  75. SIL_DMA_N_COMPLETE = (1 << 26), /* COMPLETE for the next channel */
  76. /* SIEN */
  77. SIL_SIEN_N = (1 << 16), /* triggered by SError.N */
  78. /*
  79. * Others
  80. */
  81. SIL_QUIRK_MOD15WRITE = (1 << 0),
  82. SIL_QUIRK_UDMA5MAX = (1 << 1),
  83. };
  84. static int sil_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  85. #ifdef CONFIG_PM_SLEEP
  86. static int sil_pci_device_resume(struct pci_dev *pdev);
  87. #endif
  88. static void sil_dev_config(struct ata_device *dev);
  89. static int sil_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  90. static int sil_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  91. static int sil_set_mode(struct ata_link *link, struct ata_device **r_failed);
  92. static enum ata_completion_errors sil_qc_prep(struct ata_queued_cmd *qc);
  93. static void sil_bmdma_setup(struct ata_queued_cmd *qc);
  94. static void sil_bmdma_start(struct ata_queued_cmd *qc);
  95. static void sil_bmdma_stop(struct ata_queued_cmd *qc);
  96. static void sil_freeze(struct ata_port *ap);
  97. static void sil_thaw(struct ata_port *ap);
  98. static const struct pci_device_id sil_pci_tbl[] = {
  99. { PCI_VDEVICE(CMD, 0x3112), sil_3112 },
  100. { PCI_VDEVICE(CMD, 0x0240), sil_3112 },
  101. { PCI_VDEVICE(CMD, 0x3512), sil_3512 },
  102. { PCI_VDEVICE(CMD, 0x3114), sil_3114 },
  103. { PCI_VDEVICE(ATI, 0x436e), sil_3112 },
  104. { PCI_VDEVICE(ATI, 0x4379), sil_3112_no_sata_irq },
  105. { PCI_VDEVICE(ATI, 0x437a), sil_3112_no_sata_irq },
  106. { } /* terminate list */
  107. };
  108. /* TODO firmware versions should be added - eric */
  109. static const struct sil_drivelist {
  110. const char *product;
  111. unsigned int quirk;
  112. } sil_blacklist [] = {
  113. { "ST320012AS", SIL_QUIRK_MOD15WRITE },
  114. { "ST330013AS", SIL_QUIRK_MOD15WRITE },
  115. { "ST340017AS", SIL_QUIRK_MOD15WRITE },
  116. { "ST360015AS", SIL_QUIRK_MOD15WRITE },
  117. { "ST380023AS", SIL_QUIRK_MOD15WRITE },
  118. { "ST3120023AS", SIL_QUIRK_MOD15WRITE },
  119. { "ST340014ASL", SIL_QUIRK_MOD15WRITE },
  120. { "ST360014ASL", SIL_QUIRK_MOD15WRITE },
  121. { "ST380011ASL", SIL_QUIRK_MOD15WRITE },
  122. { "ST3120022ASL", SIL_QUIRK_MOD15WRITE },
  123. { "ST3160021ASL", SIL_QUIRK_MOD15WRITE },
  124. { "TOSHIBA MK2561GSYN", SIL_QUIRK_MOD15WRITE },
  125. { "Maxtor 4D060H3", SIL_QUIRK_UDMA5MAX },
  126. { }
  127. };
  128. static struct pci_driver sil_pci_driver = {
  129. .name = DRV_NAME,
  130. .id_table = sil_pci_tbl,
  131. .probe = sil_init_one,
  132. .remove = ata_pci_remove_one,
  133. #ifdef CONFIG_PM_SLEEP
  134. .suspend = ata_pci_device_suspend,
  135. .resume = sil_pci_device_resume,
  136. #endif
  137. };
  138. static struct scsi_host_template sil_sht = {
  139. ATA_BASE_SHT(DRV_NAME),
  140. /** These controllers support Large Block Transfer which allows
  141. transfer chunks up to 2GB and which cross 64KB boundaries,
  142. therefore the DMA limits are more relaxed than standard ATA SFF. */
  143. .dma_boundary = SIL_DMA_BOUNDARY,
  144. .sg_tablesize = ATA_MAX_PRD
  145. };
  146. static struct ata_port_operations sil_ops = {
  147. .inherits = &ata_bmdma32_port_ops,
  148. .dev_config = sil_dev_config,
  149. .set_mode = sil_set_mode,
  150. .bmdma_setup = sil_bmdma_setup,
  151. .bmdma_start = sil_bmdma_start,
  152. .bmdma_stop = sil_bmdma_stop,
  153. .qc_prep = sil_qc_prep,
  154. .freeze = sil_freeze,
  155. .thaw = sil_thaw,
  156. .scr_read = sil_scr_read,
  157. .scr_write = sil_scr_write,
  158. };
  159. static const struct ata_port_info sil_port_info[] = {
  160. /* sil_3112 */
  161. {
  162. .flags = SIL_DFL_PORT_FLAGS | SIL_FLAG_MOD15WRITE,
  163. .pio_mask = ATA_PIO4,
  164. .mwdma_mask = ATA_MWDMA2,
  165. .udma_mask = ATA_UDMA5,
  166. .port_ops = &sil_ops,
  167. },
  168. /* sil_3112_no_sata_irq */
  169. {
  170. .flags = SIL_DFL_PORT_FLAGS | SIL_FLAG_MOD15WRITE |
  171. SIL_FLAG_NO_SATA_IRQ,
  172. .pio_mask = ATA_PIO4,
  173. .mwdma_mask = ATA_MWDMA2,
  174. .udma_mask = ATA_UDMA5,
  175. .port_ops = &sil_ops,
  176. },
  177. /* sil_3512 */
  178. {
  179. .flags = SIL_DFL_PORT_FLAGS | SIL_FLAG_RERR_ON_DMA_ACT,
  180. .pio_mask = ATA_PIO4,
  181. .mwdma_mask = ATA_MWDMA2,
  182. .udma_mask = ATA_UDMA5,
  183. .port_ops = &sil_ops,
  184. },
  185. /* sil_3114 */
  186. {
  187. .flags = SIL_DFL_PORT_FLAGS | SIL_FLAG_RERR_ON_DMA_ACT,
  188. .pio_mask = ATA_PIO4,
  189. .mwdma_mask = ATA_MWDMA2,
  190. .udma_mask = ATA_UDMA5,
  191. .port_ops = &sil_ops,
  192. },
  193. };
  194. /* per-port register offsets */
  195. /* TODO: we can probably calculate rather than use a table */
  196. static const struct {
  197. unsigned long tf; /* ATA taskfile register block */
  198. unsigned long ctl; /* ATA control/altstatus register block */
  199. unsigned long bmdma; /* DMA register block */
  200. unsigned long bmdma2; /* DMA register block #2 */
  201. unsigned long fifo_cfg; /* FIFO Valid Byte Count and Control */
  202. unsigned long scr; /* SATA control register block */
  203. unsigned long sien; /* SATA Interrupt Enable register */
  204. unsigned long xfer_mode;/* data transfer mode register */
  205. unsigned long sfis_cfg; /* SATA FIS reception config register */
  206. } sil_port[] = {
  207. /* port 0 ... */
  208. /* tf ctl bmdma bmdma2 fifo scr sien mode sfis */
  209. { 0x80, 0x8A, 0x0, 0x10, 0x40, 0x100, 0x148, 0xb4, 0x14c },
  210. { 0xC0, 0xCA, 0x8, 0x18, 0x44, 0x180, 0x1c8, 0xf4, 0x1cc },
  211. { 0x280, 0x28A, 0x200, 0x210, 0x240, 0x300, 0x348, 0x2b4, 0x34c },
  212. { 0x2C0, 0x2CA, 0x208, 0x218, 0x244, 0x380, 0x3c8, 0x2f4, 0x3cc },
  213. /* ... port 3 */
  214. };
  215. MODULE_AUTHOR("Jeff Garzik");
  216. MODULE_DESCRIPTION("low-level driver for Silicon Image SATA controller");
  217. MODULE_LICENSE("GPL");
  218. MODULE_DEVICE_TABLE(pci, sil_pci_tbl);
  219. MODULE_VERSION(DRV_VERSION);
  220. static int slow_down;
  221. module_param(slow_down, int, 0444);
  222. MODULE_PARM_DESC(slow_down, "Sledgehammer used to work around random problems, by limiting commands to 15 sectors (0=off, 1=on)");
  223. static void sil_bmdma_stop(struct ata_queued_cmd *qc)
  224. {
  225. struct ata_port *ap = qc->ap;
  226. void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
  227. void __iomem *bmdma2 = mmio_base + sil_port[ap->port_no].bmdma2;
  228. /* clear start/stop bit - can safely always write 0 */
  229. iowrite8(0, bmdma2);
  230. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  231. ata_sff_dma_pause(ap);
  232. }
  233. static void sil_bmdma_setup(struct ata_queued_cmd *qc)
  234. {
  235. struct ata_port *ap = qc->ap;
  236. void __iomem *bmdma = ap->ioaddr.bmdma_addr;
  237. /* load PRD table addr. */
  238. iowrite32(ap->bmdma_prd_dma, bmdma + ATA_DMA_TABLE_OFS);
  239. /* issue r/w command */
  240. ap->ops->sff_exec_command(ap, &qc->tf);
  241. }
  242. static void sil_bmdma_start(struct ata_queued_cmd *qc)
  243. {
  244. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  245. struct ata_port *ap = qc->ap;
  246. void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
  247. void __iomem *bmdma2 = mmio_base + sil_port[ap->port_no].bmdma2;
  248. u8 dmactl = ATA_DMA_START;
  249. /* set transfer direction, start host DMA transaction
  250. Note: For Large Block Transfer to work, the DMA must be started
  251. using the bmdma2 register. */
  252. if (!rw)
  253. dmactl |= ATA_DMA_WR;
  254. iowrite8(dmactl, bmdma2);
  255. }
  256. /* The way God intended PCI IDE scatter/gather lists to look and behave... */
  257. static void sil_fill_sg(struct ata_queued_cmd *qc)
  258. {
  259. struct scatterlist *sg;
  260. struct ata_port *ap = qc->ap;
  261. struct ata_bmdma_prd *prd, *last_prd = NULL;
  262. unsigned int si;
  263. prd = &ap->bmdma_prd[0];
  264. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  265. /* Note h/w doesn't support 64-bit, so we unconditionally
  266. * truncate dma_addr_t to u32.
  267. */
  268. u32 addr = (u32) sg_dma_address(sg);
  269. u32 sg_len = sg_dma_len(sg);
  270. prd->addr = cpu_to_le32(addr);
  271. prd->flags_len = cpu_to_le32(sg_len);
  272. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", si, addr, sg_len);
  273. last_prd = prd;
  274. prd++;
  275. }
  276. if (likely(last_prd))
  277. last_prd->flags_len |= cpu_to_le32(ATA_PRD_EOT);
  278. }
  279. static enum ata_completion_errors sil_qc_prep(struct ata_queued_cmd *qc)
  280. {
  281. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  282. return AC_ERR_OK;
  283. sil_fill_sg(qc);
  284. return AC_ERR_OK;
  285. }
  286. static unsigned char sil_get_device_cache_line(struct pci_dev *pdev)
  287. {
  288. u8 cache_line = 0;
  289. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line);
  290. return cache_line;
  291. }
  292. /**
  293. * sil_set_mode - wrap set_mode functions
  294. * @link: link to set up
  295. * @r_failed: returned device when we fail
  296. *
  297. * Wrap the libata method for device setup as after the setup we need
  298. * to inspect the results and do some configuration work
  299. */
  300. static int sil_set_mode(struct ata_link *link, struct ata_device **r_failed)
  301. {
  302. struct ata_port *ap = link->ap;
  303. void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
  304. void __iomem *addr = mmio_base + sil_port[ap->port_no].xfer_mode;
  305. struct ata_device *dev;
  306. u32 tmp, dev_mode[2] = { };
  307. int rc;
  308. rc = ata_do_set_mode(link, r_failed);
  309. if (rc)
  310. return rc;
  311. ata_for_each_dev(dev, link, ALL) {
  312. if (!ata_dev_enabled(dev))
  313. dev_mode[dev->devno] = 0; /* PIO0/1/2 */
  314. else if (dev->flags & ATA_DFLAG_PIO)
  315. dev_mode[dev->devno] = 1; /* PIO3/4 */
  316. else
  317. dev_mode[dev->devno] = 3; /* UDMA */
  318. /* value 2 indicates MDMA */
  319. }
  320. tmp = readl(addr);
  321. tmp &= ~((1<<5) | (1<<4) | (1<<1) | (1<<0));
  322. tmp |= dev_mode[0];
  323. tmp |= (dev_mode[1] << 4);
  324. writel(tmp, addr);
  325. readl(addr); /* flush */
  326. return 0;
  327. }
  328. static inline void __iomem *sil_scr_addr(struct ata_port *ap,
  329. unsigned int sc_reg)
  330. {
  331. void __iomem *offset = ap->ioaddr.scr_addr;
  332. switch (sc_reg) {
  333. case SCR_STATUS:
  334. return offset + 4;
  335. case SCR_ERROR:
  336. return offset + 8;
  337. case SCR_CONTROL:
  338. return offset;
  339. default:
  340. /* do nothing */
  341. break;
  342. }
  343. return NULL;
  344. }
  345. static int sil_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
  346. {
  347. void __iomem *mmio = sil_scr_addr(link->ap, sc_reg);
  348. if (mmio) {
  349. *val = readl(mmio);
  350. return 0;
  351. }
  352. return -EINVAL;
  353. }
  354. static int sil_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
  355. {
  356. void __iomem *mmio = sil_scr_addr(link->ap, sc_reg);
  357. if (mmio) {
  358. writel(val, mmio);
  359. return 0;
  360. }
  361. return -EINVAL;
  362. }
  363. static void sil_host_intr(struct ata_port *ap, u32 bmdma2)
  364. {
  365. struct ata_eh_info *ehi = &ap->link.eh_info;
  366. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  367. u8 status;
  368. if (unlikely(bmdma2 & SIL_DMA_SATA_IRQ)) {
  369. u32 serror = 0xffffffff;
  370. /* SIEN doesn't mask SATA IRQs on some 3112s. Those
  371. * controllers continue to assert IRQ as long as
  372. * SError bits are pending. Clear SError immediately.
  373. */
  374. sil_scr_read(&ap->link, SCR_ERROR, &serror);
  375. sil_scr_write(&ap->link, SCR_ERROR, serror);
  376. /* Sometimes spurious interrupts occur, double check
  377. * it's PHYRDY CHG.
  378. */
  379. if (serror & SERR_PHYRDY_CHG) {
  380. ap->link.eh_info.serror |= serror;
  381. goto freeze;
  382. }
  383. if (!(bmdma2 & SIL_DMA_COMPLETE))
  384. return;
  385. }
  386. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  387. /* this sometimes happens, just clear IRQ */
  388. ap->ops->sff_check_status(ap);
  389. return;
  390. }
  391. /* Check whether we are expecting interrupt in this state */
  392. switch (ap->hsm_task_state) {
  393. case HSM_ST_FIRST:
  394. /* Some pre-ATAPI-4 devices assert INTRQ
  395. * at this state when ready to receive CDB.
  396. */
  397. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  398. * The flag was turned on only for atapi devices. No
  399. * need to check ata_is_atapi(qc->tf.protocol) again.
  400. */
  401. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  402. goto err_hsm;
  403. break;
  404. case HSM_ST_LAST:
  405. if (ata_is_dma(qc->tf.protocol)) {
  406. /* clear DMA-Start bit */
  407. ap->ops->bmdma_stop(qc);
  408. if (bmdma2 & SIL_DMA_ERROR) {
  409. qc->err_mask |= AC_ERR_HOST_BUS;
  410. ap->hsm_task_state = HSM_ST_ERR;
  411. }
  412. }
  413. break;
  414. case HSM_ST:
  415. break;
  416. default:
  417. goto err_hsm;
  418. }
  419. /* check main status, clearing INTRQ */
  420. status = ap->ops->sff_check_status(ap);
  421. if (unlikely(status & ATA_BUSY))
  422. goto err_hsm;
  423. /* ack bmdma irq events */
  424. ata_bmdma_irq_clear(ap);
  425. /* kick HSM in the ass */
  426. ata_sff_hsm_move(ap, qc, status, 0);
  427. if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
  428. ata_ehi_push_desc(ehi, "BMDMA2 stat 0x%x", bmdma2);
  429. return;
  430. err_hsm:
  431. qc->err_mask |= AC_ERR_HSM;
  432. freeze:
  433. ata_port_freeze(ap);
  434. }
  435. static irqreturn_t sil_interrupt(int irq, void *dev_instance)
  436. {
  437. struct ata_host *host = dev_instance;
  438. void __iomem *mmio_base = host->iomap[SIL_MMIO_BAR];
  439. int handled = 0;
  440. int i;
  441. spin_lock(&host->lock);
  442. for (i = 0; i < host->n_ports; i++) {
  443. struct ata_port *ap = host->ports[i];
  444. u32 bmdma2 = readl(mmio_base + sil_port[ap->port_no].bmdma2);
  445. /* turn off SATA_IRQ if not supported */
  446. if (ap->flags & SIL_FLAG_NO_SATA_IRQ)
  447. bmdma2 &= ~SIL_DMA_SATA_IRQ;
  448. if (bmdma2 == 0xffffffff ||
  449. !(bmdma2 & (SIL_DMA_COMPLETE | SIL_DMA_SATA_IRQ)))
  450. continue;
  451. sil_host_intr(ap, bmdma2);
  452. handled = 1;
  453. }
  454. spin_unlock(&host->lock);
  455. return IRQ_RETVAL(handled);
  456. }
  457. static void sil_freeze(struct ata_port *ap)
  458. {
  459. void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
  460. u32 tmp;
  461. /* global IRQ mask doesn't block SATA IRQ, turn off explicitly */
  462. writel(0, mmio_base + sil_port[ap->port_no].sien);
  463. /* plug IRQ */
  464. tmp = readl(mmio_base + SIL_SYSCFG);
  465. tmp |= SIL_MASK_IDE0_INT << ap->port_no;
  466. writel(tmp, mmio_base + SIL_SYSCFG);
  467. readl(mmio_base + SIL_SYSCFG); /* flush */
  468. /* Ensure DMA_ENABLE is off.
  469. *
  470. * This is because the controller will not give us access to the
  471. * taskfile registers while a DMA is in progress
  472. */
  473. iowrite8(ioread8(ap->ioaddr.bmdma_addr) & ~SIL_DMA_ENABLE,
  474. ap->ioaddr.bmdma_addr);
  475. /* According to ata_bmdma_stop, an HDMA transition requires
  476. * on PIO cycle. But we can't read a taskfile register.
  477. */
  478. ioread8(ap->ioaddr.bmdma_addr);
  479. }
  480. static void sil_thaw(struct ata_port *ap)
  481. {
  482. void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
  483. u32 tmp;
  484. /* clear IRQ */
  485. ap->ops->sff_check_status(ap);
  486. ata_bmdma_irq_clear(ap);
  487. /* turn on SATA IRQ if supported */
  488. if (!(ap->flags & SIL_FLAG_NO_SATA_IRQ))
  489. writel(SIL_SIEN_N, mmio_base + sil_port[ap->port_no].sien);
  490. /* turn on IRQ */
  491. tmp = readl(mmio_base + SIL_SYSCFG);
  492. tmp &= ~(SIL_MASK_IDE0_INT << ap->port_no);
  493. writel(tmp, mmio_base + SIL_SYSCFG);
  494. }
  495. /**
  496. * sil_dev_config - Apply device/host-specific errata fixups
  497. * @dev: Device to be examined
  498. *
  499. * After the IDENTIFY [PACKET] DEVICE step is complete, and a
  500. * device is known to be present, this function is called.
  501. * We apply two errata fixups which are specific to Silicon Image,
  502. * a Seagate and a Maxtor fixup.
  503. *
  504. * For certain Seagate devices, we must limit the maximum sectors
  505. * to under 8K.
  506. *
  507. * For certain Maxtor devices, we must not program the drive
  508. * beyond udma5.
  509. *
  510. * Both fixups are unfairly pessimistic. As soon as I get more
  511. * information on these errata, I will create a more exhaustive
  512. * list, and apply the fixups to only the specific
  513. * devices/hosts/firmwares that need it.
  514. *
  515. * 20040111 - Seagate drives affected by the Mod15Write bug are blacklisted
  516. * The Maxtor quirk is in the blacklist, but I'm keeping the original
  517. * pessimistic fix for the following reasons...
  518. * - There seems to be less info on it, only one device gleaned off the
  519. * Windows driver, maybe only one is affected. More info would be greatly
  520. * appreciated.
  521. * - But then again UDMA5 is hardly anything to complain about
  522. */
  523. static void sil_dev_config(struct ata_device *dev)
  524. {
  525. struct ata_port *ap = dev->link->ap;
  526. int print_info = ap->link.eh_context.i.flags & ATA_EHI_PRINTINFO;
  527. unsigned int n, quirks = 0;
  528. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  529. /* This controller doesn't support trim */
  530. dev->horkage |= ATA_HORKAGE_NOTRIM;
  531. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  532. for (n = 0; sil_blacklist[n].product; n++)
  533. if (!strcmp(sil_blacklist[n].product, model_num)) {
  534. quirks = sil_blacklist[n].quirk;
  535. break;
  536. }
  537. /* limit requests to 15 sectors */
  538. if (slow_down ||
  539. ((ap->flags & SIL_FLAG_MOD15WRITE) &&
  540. (quirks & SIL_QUIRK_MOD15WRITE))) {
  541. if (print_info)
  542. ata_dev_info(dev,
  543. "applying Seagate errata fix (mod15write workaround)\n");
  544. dev->max_sectors = 15;
  545. return;
  546. }
  547. /* limit to udma5 */
  548. if (quirks & SIL_QUIRK_UDMA5MAX) {
  549. if (print_info)
  550. ata_dev_info(dev, "applying Maxtor errata fix %s\n",
  551. model_num);
  552. dev->udma_mask &= ATA_UDMA5;
  553. return;
  554. }
  555. }
  556. static void sil_init_controller(struct ata_host *host)
  557. {
  558. struct pci_dev *pdev = to_pci_dev(host->dev);
  559. void __iomem *mmio_base = host->iomap[SIL_MMIO_BAR];
  560. u8 cls;
  561. u32 tmp;
  562. int i;
  563. /* Initialize FIFO PCI bus arbitration */
  564. cls = sil_get_device_cache_line(pdev);
  565. if (cls) {
  566. cls >>= 3;
  567. cls++; /* cls = (line_size/8)+1 */
  568. for (i = 0; i < host->n_ports; i++)
  569. writew(cls << 8 | cls,
  570. mmio_base + sil_port[i].fifo_cfg);
  571. } else
  572. dev_warn(&pdev->dev,
  573. "cache line size not set. Driver may not function\n");
  574. /* Apply R_ERR on DMA activate FIS errata workaround */
  575. if (host->ports[0]->flags & SIL_FLAG_RERR_ON_DMA_ACT) {
  576. int cnt;
  577. for (i = 0, cnt = 0; i < host->n_ports; i++) {
  578. tmp = readl(mmio_base + sil_port[i].sfis_cfg);
  579. if ((tmp & 0x3) != 0x01)
  580. continue;
  581. if (!cnt)
  582. dev_info(&pdev->dev,
  583. "Applying R_ERR on DMA activate FIS errata fix\n");
  584. writel(tmp & ~0x3, mmio_base + sil_port[i].sfis_cfg);
  585. cnt++;
  586. }
  587. }
  588. if (host->n_ports == 4) {
  589. /* flip the magic "make 4 ports work" bit */
  590. tmp = readl(mmio_base + sil_port[2].bmdma);
  591. if ((tmp & SIL_INTR_STEERING) == 0)
  592. writel(tmp | SIL_INTR_STEERING,
  593. mmio_base + sil_port[2].bmdma);
  594. }
  595. }
  596. static bool sil_broken_system_poweroff(struct pci_dev *pdev)
  597. {
  598. static const struct dmi_system_id broken_systems[] = {
  599. {
  600. .ident = "HP Compaq nx6325",
  601. .matches = {
  602. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  603. DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6325"),
  604. },
  605. /* PCI slot number of the controller */
  606. .driver_data = (void *)0x12UL,
  607. },
  608. { } /* terminate list */
  609. };
  610. const struct dmi_system_id *dmi = dmi_first_match(broken_systems);
  611. if (dmi) {
  612. unsigned long slot = (unsigned long)dmi->driver_data;
  613. /* apply the quirk only to on-board controllers */
  614. return slot == PCI_SLOT(pdev->devfn);
  615. }
  616. return false;
  617. }
  618. static int sil_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  619. {
  620. int board_id = ent->driver_data;
  621. struct ata_port_info pi = sil_port_info[board_id];
  622. const struct ata_port_info *ppi[] = { &pi, NULL };
  623. struct ata_host *host;
  624. void __iomem *mmio_base;
  625. int n_ports, rc;
  626. unsigned int i;
  627. ata_print_version_once(&pdev->dev, DRV_VERSION);
  628. /* allocate host */
  629. n_ports = 2;
  630. if (board_id == sil_3114)
  631. n_ports = 4;
  632. if (sil_broken_system_poweroff(pdev)) {
  633. pi.flags |= ATA_FLAG_NO_POWEROFF_SPINDOWN |
  634. ATA_FLAG_NO_HIBERNATE_SPINDOWN;
  635. dev_info(&pdev->dev, "quirky BIOS, skipping spindown "
  636. "on poweroff and hibernation\n");
  637. }
  638. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  639. if (!host)
  640. return -ENOMEM;
  641. /* acquire resources and fill host */
  642. rc = pcim_enable_device(pdev);
  643. if (rc)
  644. return rc;
  645. rc = pcim_iomap_regions(pdev, 1 << SIL_MMIO_BAR, DRV_NAME);
  646. if (rc == -EBUSY)
  647. pcim_pin_device(pdev);
  648. if (rc)
  649. return rc;
  650. host->iomap = pcim_iomap_table(pdev);
  651. rc = dma_set_mask_and_coherent(&pdev->dev, ATA_DMA_MASK);
  652. if (rc)
  653. return rc;
  654. mmio_base = host->iomap[SIL_MMIO_BAR];
  655. for (i = 0; i < host->n_ports; i++) {
  656. struct ata_port *ap = host->ports[i];
  657. struct ata_ioports *ioaddr = &ap->ioaddr;
  658. ioaddr->cmd_addr = mmio_base + sil_port[i].tf;
  659. ioaddr->altstatus_addr =
  660. ioaddr->ctl_addr = mmio_base + sil_port[i].ctl;
  661. ioaddr->bmdma_addr = mmio_base + sil_port[i].bmdma;
  662. ioaddr->scr_addr = mmio_base + sil_port[i].scr;
  663. ata_sff_std_ports(ioaddr);
  664. ata_port_pbar_desc(ap, SIL_MMIO_BAR, -1, "mmio");
  665. ata_port_pbar_desc(ap, SIL_MMIO_BAR, sil_port[i].tf, "tf");
  666. }
  667. /* initialize and activate */
  668. sil_init_controller(host);
  669. pci_set_master(pdev);
  670. return ata_host_activate(host, pdev->irq, sil_interrupt, IRQF_SHARED,
  671. &sil_sht);
  672. }
  673. #ifdef CONFIG_PM_SLEEP
  674. static int sil_pci_device_resume(struct pci_dev *pdev)
  675. {
  676. struct ata_host *host = pci_get_drvdata(pdev);
  677. int rc;
  678. rc = ata_pci_device_do_resume(pdev);
  679. if (rc)
  680. return rc;
  681. sil_init_controller(host);
  682. ata_host_resume(host);
  683. return 0;
  684. }
  685. #endif
  686. module_pci_driver(sil_pci_driver);