sata_sil.c 22 KB

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