siimage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  4. * Copyright (C) 2007-2008 MontaVista Software, Inc.
  5. * Copyright (C) 2007-2008 Bartlomiej Zolnierkiewicz
  6. *
  7. * May be copied or modified under the terms of the GNU General Public License
  8. *
  9. * Documentation for CMD680:
  10. * http://gkernel.sourceforge.net/specs/sii/sii-0680a-v1.31.pdf.bz2
  11. *
  12. * Documentation for SiI 3112:
  13. * http://gkernel.sourceforge.net/specs/sii/3112A_SiI-DS-0095-B2.pdf.bz2
  14. *
  15. * Errata and other documentation only available under NDA.
  16. *
  17. *
  18. * FAQ Items:
  19. * If you are using Marvell SATA-IDE adapters with Maxtor drives
  20. * ensure the system is set up for ATA100/UDMA5, not UDMA6.
  21. *
  22. * If you are using WD drives with SATA bridges you must set the
  23. * drive to "Single". "Master" will hang.
  24. *
  25. * If you have strange problems with nVidia chipset systems please
  26. * see the SI support documentation and update your system BIOS
  27. * if necessary
  28. *
  29. * The Dell DRAC4 has some interesting features including effectively hot
  30. * unplugging/replugging the virtual CD interface when the DRAC is reset.
  31. * This often causes drivers/ide/siimage to panic but is ok with the rather
  32. * smarter code in libata.
  33. *
  34. * TODO:
  35. * - IORDY fixes
  36. * - VDMA support
  37. */
  38. #include <linux/types.h>
  39. #include <linux/module.h>
  40. #include <linux/pci.h>
  41. #include <linux/hdreg.h>
  42. #include <linux/ide.h>
  43. #include <linux/init.h>
  44. #include <linux/io.h>
  45. /**
  46. * pdev_is_sata - check if device is SATA
  47. * @pdev: PCI device to check
  48. *
  49. * Returns true if this is a SATA controller
  50. */
  51. static int pdev_is_sata(struct pci_dev *pdev)
  52. {
  53. #ifdef CONFIG_BLK_DEV_IDE_SATA
  54. switch (pdev->device) {
  55. case PCI_DEVICE_ID_SII_3112:
  56. case PCI_DEVICE_ID_SII_1210SA:
  57. return 1;
  58. case PCI_DEVICE_ID_SII_680:
  59. return 0;
  60. }
  61. BUG();
  62. #endif
  63. return 0;
  64. }
  65. /**
  66. * is_sata - check if hwif is SATA
  67. * @hwif: interface to check
  68. *
  69. * Returns true if this is a SATA controller
  70. */
  71. static inline int is_sata(ide_hwif_t *hwif)
  72. {
  73. return pdev_is_sata(to_pci_dev(hwif->dev));
  74. }
  75. /**
  76. * siimage_selreg - return register base
  77. * @hwif: interface
  78. * @r: config offset
  79. *
  80. * Turn a config register offset into the right address in either
  81. * PCI space or MMIO space to access the control register in question
  82. * Thankfully this is a configuration operation, so isn't performance
  83. * critical.
  84. */
  85. static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
  86. {
  87. unsigned long base = (unsigned long)hwif->hwif_data;
  88. base += 0xA0 + r;
  89. if (hwif->host_flags & IDE_HFLAG_MMIO)
  90. base += hwif->channel << 6;
  91. else
  92. base += hwif->channel << 4;
  93. return base;
  94. }
  95. /**
  96. * siimage_seldev - return register base
  97. * @hwif: interface
  98. * @r: config offset
  99. *
  100. * Turn a config register offset into the right address in either
  101. * PCI space or MMIO space to access the control register in question
  102. * including accounting for the unit shift.
  103. */
  104. static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
  105. {
  106. ide_hwif_t *hwif = HWIF(drive);
  107. unsigned long base = (unsigned long)hwif->hwif_data;
  108. base += 0xA0 + r;
  109. if (hwif->host_flags & IDE_HFLAG_MMIO)
  110. base += hwif->channel << 6;
  111. else
  112. base += hwif->channel << 4;
  113. base |= drive->select.b.unit << drive->select.b.unit;
  114. return base;
  115. }
  116. static u8 sil_ioread8(struct pci_dev *dev, unsigned long addr)
  117. {
  118. struct ide_host *host = pci_get_drvdata(dev);
  119. u8 tmp = 0;
  120. if (host->host_priv)
  121. tmp = readb((void __iomem *)addr);
  122. else
  123. pci_read_config_byte(dev, addr, &tmp);
  124. return tmp;
  125. }
  126. static u16 sil_ioread16(struct pci_dev *dev, unsigned long addr)
  127. {
  128. struct ide_host *host = pci_get_drvdata(dev);
  129. u16 tmp = 0;
  130. if (host->host_priv)
  131. tmp = readw((void __iomem *)addr);
  132. else
  133. pci_read_config_word(dev, addr, &tmp);
  134. return tmp;
  135. }
  136. static void sil_iowrite8(struct pci_dev *dev, u8 val, unsigned long addr)
  137. {
  138. struct ide_host *host = pci_get_drvdata(dev);
  139. if (host->host_priv)
  140. writeb(val, (void __iomem *)addr);
  141. else
  142. pci_write_config_byte(dev, addr, val);
  143. }
  144. static void sil_iowrite16(struct pci_dev *dev, u16 val, unsigned long addr)
  145. {
  146. struct ide_host *host = pci_get_drvdata(dev);
  147. if (host->host_priv)
  148. writew(val, (void __iomem *)addr);
  149. else
  150. pci_write_config_word(dev, addr, val);
  151. }
  152. static void sil_iowrite32(struct pci_dev *dev, u32 val, unsigned long addr)
  153. {
  154. struct ide_host *host = pci_get_drvdata(dev);
  155. if (host->host_priv)
  156. writel(val, (void __iomem *)addr);
  157. else
  158. pci_write_config_dword(dev, addr, val);
  159. }
  160. /**
  161. * sil_udma_filter - compute UDMA mask
  162. * @drive: IDE device
  163. *
  164. * Compute the available UDMA speeds for the device on the interface.
  165. *
  166. * For the CMD680 this depends on the clocking mode (scsc), for the
  167. * SI3112 SATA controller life is a bit simpler.
  168. */
  169. static u8 sil_pata_udma_filter(ide_drive_t *drive)
  170. {
  171. ide_hwif_t *hwif = drive->hwif;
  172. struct pci_dev *dev = to_pci_dev(hwif->dev);
  173. unsigned long base = (unsigned long)hwif->hwif_data;
  174. u8 scsc, mask = 0;
  175. base += (hwif->host_flags & IDE_HFLAG_MMIO) ? 0x4A : 0x8A;
  176. scsc = sil_ioread8(dev, base);
  177. switch (scsc & 0x30) {
  178. case 0x10: /* 133 */
  179. mask = ATA_UDMA6;
  180. break;
  181. case 0x20: /* 2xPCI */
  182. mask = ATA_UDMA6;
  183. break;
  184. case 0x00: /* 100 */
  185. mask = ATA_UDMA5;
  186. break;
  187. default: /* Disabled ? */
  188. BUG();
  189. }
  190. return mask;
  191. }
  192. static u8 sil_sata_udma_filter(ide_drive_t *drive)
  193. {
  194. return strstr(drive->id->model, "Maxtor") ? ATA_UDMA5 : ATA_UDMA6;
  195. }
  196. /**
  197. * sil_set_pio_mode - set host controller for PIO mode
  198. * @drive: drive
  199. * @pio: PIO mode number
  200. *
  201. * Load the timing settings for this device mode into the
  202. * controller. If we are in PIO mode 3 or 4 turn on IORDY
  203. * monitoring (bit 9). The TF timing is bits 31:16
  204. */
  205. static void sil_set_pio_mode(ide_drive_t *drive, u8 pio)
  206. {
  207. static const u16 tf_speed[] = { 0x328a, 0x2283, 0x1281, 0x10c3, 0x10c1 };
  208. static const u16 data_speed[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
  209. ide_hwif_t *hwif = HWIF(drive);
  210. struct pci_dev *dev = to_pci_dev(hwif->dev);
  211. ide_drive_t *pair = ide_get_paired_drive(drive);
  212. u32 speedt = 0;
  213. u16 speedp = 0;
  214. unsigned long addr = siimage_seldev(drive, 0x04);
  215. unsigned long tfaddr = siimage_selreg(hwif, 0x02);
  216. unsigned long base = (unsigned long)hwif->hwif_data;
  217. u8 tf_pio = pio;
  218. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  219. u8 addr_mask = hwif->channel ? (mmio ? 0xF4 : 0x84)
  220. : (mmio ? 0xB4 : 0x80);
  221. u8 mode = 0;
  222. u8 unit = drive->select.b.unit;
  223. /* trim *taskfile* PIO to the slowest of the master/slave */
  224. if (pair->present) {
  225. u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
  226. if (pair_pio < tf_pio)
  227. tf_pio = pair_pio;
  228. }
  229. /* cheat for now and use the docs */
  230. speedp = data_speed[pio];
  231. speedt = tf_speed[tf_pio];
  232. sil_iowrite16(dev, speedp, addr);
  233. sil_iowrite16(dev, speedt, tfaddr);
  234. /* now set up IORDY */
  235. speedp = sil_ioread16(dev, tfaddr - 2);
  236. speedp &= ~0x200;
  237. if (pio > 2)
  238. speedp |= 0x200;
  239. sil_iowrite16(dev, speedp, tfaddr - 2);
  240. mode = sil_ioread8(dev, base + addr_mask);
  241. mode &= ~(unit ? 0x30 : 0x03);
  242. mode |= unit ? 0x10 : 0x01;
  243. sil_iowrite8(dev, mode, base + addr_mask);
  244. }
  245. /**
  246. * sil_set_dma_mode - set host controller for DMA mode
  247. * @drive: drive
  248. * @speed: DMA mode
  249. *
  250. * Tune the SiI chipset for the desired DMA mode.
  251. */
  252. static void sil_set_dma_mode(ide_drive_t *drive, const u8 speed)
  253. {
  254. static const u8 ultra6[] = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
  255. static const u8 ultra5[] = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
  256. static const u16 dma[] = { 0x2208, 0x10C2, 0x10C1 };
  257. ide_hwif_t *hwif = HWIF(drive);
  258. struct pci_dev *dev = to_pci_dev(hwif->dev);
  259. u16 ultra = 0, multi = 0;
  260. u8 mode = 0, unit = drive->select.b.unit;
  261. unsigned long base = (unsigned long)hwif->hwif_data;
  262. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  263. u8 scsc = 0, addr_mask = hwif->channel ? (mmio ? 0xF4 : 0x84)
  264. : (mmio ? 0xB4 : 0x80);
  265. unsigned long ma = siimage_seldev(drive, 0x08);
  266. unsigned long ua = siimage_seldev(drive, 0x0C);
  267. scsc = sil_ioread8 (dev, base + (mmio ? 0x4A : 0x8A));
  268. mode = sil_ioread8 (dev, base + addr_mask);
  269. multi = sil_ioread16(dev, ma);
  270. ultra = sil_ioread16(dev, ua);
  271. mode &= ~(unit ? 0x30 : 0x03);
  272. ultra &= ~0x3F;
  273. scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
  274. scsc = is_sata(hwif) ? 1 : scsc;
  275. if (speed >= XFER_UDMA_0) {
  276. multi = dma[2];
  277. ultra |= scsc ? ultra6[speed - XFER_UDMA_0] :
  278. ultra5[speed - XFER_UDMA_0];
  279. mode |= unit ? 0x30 : 0x03;
  280. } else {
  281. multi = dma[speed - XFER_MW_DMA_0];
  282. mode |= unit ? 0x20 : 0x02;
  283. }
  284. sil_iowrite8 (dev, mode, base + addr_mask);
  285. sil_iowrite16(dev, multi, ma);
  286. sil_iowrite16(dev, ultra, ua);
  287. }
  288. /* returns 1 if dma irq issued, 0 otherwise */
  289. static int siimage_io_dma_test_irq(ide_drive_t *drive)
  290. {
  291. ide_hwif_t *hwif = HWIF(drive);
  292. struct pci_dev *dev = to_pci_dev(hwif->dev);
  293. u8 dma_altstat = 0;
  294. unsigned long addr = siimage_selreg(hwif, 1);
  295. /* return 1 if INTR asserted */
  296. if (inb(hwif->dma_base + ATA_DMA_STATUS) & 4)
  297. return 1;
  298. /* return 1 if Device INTR asserted */
  299. pci_read_config_byte(dev, addr, &dma_altstat);
  300. if (dma_altstat & 8)
  301. return 0; /* return 1; */
  302. return 0;
  303. }
  304. /**
  305. * siimage_mmio_dma_test_irq - check we caused an IRQ
  306. * @drive: drive we are testing
  307. *
  308. * Check if we caused an IDE DMA interrupt. We may also have caused
  309. * SATA status interrupts, if so we clean them up and continue.
  310. */
  311. static int siimage_mmio_dma_test_irq(ide_drive_t *drive)
  312. {
  313. ide_hwif_t *hwif = HWIF(drive);
  314. unsigned long addr = siimage_selreg(hwif, 0x1);
  315. void __iomem *sata_error_addr
  316. = (void __iomem *)hwif->sata_scr[SATA_ERROR_OFFSET];
  317. if (sata_error_addr) {
  318. unsigned long base = (unsigned long)hwif->hwif_data;
  319. u32 ext_stat = readl((void __iomem *)(base + 0x10));
  320. u8 watchdog = 0;
  321. if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
  322. u32 sata_error = readl(sata_error_addr);
  323. writel(sata_error, sata_error_addr);
  324. watchdog = (sata_error & 0x00680000) ? 1 : 0;
  325. printk(KERN_WARNING "%s: sata_error = 0x%08x, "
  326. "watchdog = %d, %s\n",
  327. drive->name, sata_error, watchdog, __func__);
  328. } else
  329. watchdog = (ext_stat & 0x8000) ? 1 : 0;
  330. ext_stat >>= 16;
  331. if (!(ext_stat & 0x0404) && !watchdog)
  332. return 0;
  333. }
  334. /* return 1 if INTR asserted */
  335. if (readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS)) & 4)
  336. return 1;
  337. /* return 1 if Device INTR asserted */
  338. if (readb((void __iomem *)addr) & 8)
  339. return 0; /* return 1; */
  340. return 0;
  341. }
  342. static int siimage_dma_test_irq(ide_drive_t *drive)
  343. {
  344. if (drive->hwif->host_flags & IDE_HFLAG_MMIO)
  345. return siimage_mmio_dma_test_irq(drive);
  346. else
  347. return siimage_io_dma_test_irq(drive);
  348. }
  349. /**
  350. * sil_sata_reset_poll - wait for SATA reset
  351. * @drive: drive we are resetting
  352. *
  353. * Poll the SATA phy and see whether it has come back from the dead
  354. * yet.
  355. */
  356. static int sil_sata_reset_poll(ide_drive_t *drive)
  357. {
  358. ide_hwif_t *hwif = drive->hwif;
  359. void __iomem *sata_status_addr
  360. = (void __iomem *)hwif->sata_scr[SATA_STATUS_OFFSET];
  361. if (sata_status_addr) {
  362. /* SATA Status is available only when in MMIO mode */
  363. u32 sata_stat = readl(sata_status_addr);
  364. if ((sata_stat & 0x03) != 0x03) {
  365. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  366. hwif->name, sata_stat);
  367. return -ENXIO;
  368. }
  369. }
  370. return 0;
  371. }
  372. /**
  373. * sil_sata_pre_reset - reset hook
  374. * @drive: IDE device being reset
  375. *
  376. * For the SATA devices we need to handle recalibration/geometry
  377. * differently
  378. */
  379. static void sil_sata_pre_reset(ide_drive_t *drive)
  380. {
  381. if (drive->media == ide_disk) {
  382. drive->special.b.set_geometry = 0;
  383. drive->special.b.recalibrate = 0;
  384. }
  385. }
  386. /**
  387. * init_chipset_siimage - set up an SI device
  388. * @dev: PCI device
  389. * @name: device name
  390. *
  391. * Perform the initial PCI set up for this device. Attempt to switch
  392. * to 133 MHz clocking if the system isn't already set up to do it.
  393. */
  394. static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev,
  395. const char *name)
  396. {
  397. struct ide_host *host = pci_get_drvdata(dev);
  398. void __iomem *ioaddr = host->host_priv;
  399. unsigned long base, scsc_addr;
  400. u8 rev = dev->revision, tmp;
  401. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, rev ? 1 : 255);
  402. if (ioaddr)
  403. pci_set_master(dev);
  404. base = (unsigned long)ioaddr;
  405. if (ioaddr && pdev_is_sata(dev)) {
  406. u32 tmp32, irq_mask;
  407. /* make sure IDE0/1 interrupts are not masked */
  408. irq_mask = (1 << 22) | (1 << 23);
  409. tmp32 = readl(ioaddr + 0x48);
  410. if (tmp32 & irq_mask) {
  411. tmp32 &= ~irq_mask;
  412. writel(tmp32, ioaddr + 0x48);
  413. readl(ioaddr + 0x48); /* flush */
  414. }
  415. writel(0, ioaddr + 0x148);
  416. writel(0, ioaddr + 0x1C8);
  417. }
  418. sil_iowrite8(dev, 0, base ? (base + 0xB4) : 0x80);
  419. sil_iowrite8(dev, 0, base ? (base + 0xF4) : 0x84);
  420. scsc_addr = base ? (base + 0x4A) : 0x8A;
  421. tmp = sil_ioread8(dev, scsc_addr);
  422. switch (tmp & 0x30) {
  423. case 0x00:
  424. /* On 100 MHz clocking, try and switch to 133 MHz */
  425. sil_iowrite8(dev, tmp | 0x10, scsc_addr);
  426. break;
  427. case 0x30:
  428. /* Clocking is disabled, attempt to force 133MHz clocking. */
  429. sil_iowrite8(dev, tmp & ~0x20, scsc_addr);
  430. case 0x10:
  431. /* On 133Mhz clocking. */
  432. break;
  433. case 0x20:
  434. /* On PCIx2 clocking. */
  435. break;
  436. }
  437. tmp = sil_ioread8(dev, scsc_addr);
  438. sil_iowrite8 (dev, 0x72, base + 0xA1);
  439. sil_iowrite16(dev, 0x328A, base + 0xA2);
  440. sil_iowrite32(dev, 0x62DD62DD, base + 0xA4);
  441. sil_iowrite32(dev, 0x43924392, base + 0xA8);
  442. sil_iowrite32(dev, 0x40094009, base + 0xAC);
  443. sil_iowrite8 (dev, 0x72, base ? (base + 0xE1) : 0xB1);
  444. sil_iowrite16(dev, 0x328A, base ? (base + 0xE2) : 0xB2);
  445. sil_iowrite32(dev, 0x62DD62DD, base ? (base + 0xE4) : 0xB4);
  446. sil_iowrite32(dev, 0x43924392, base ? (base + 0xE8) : 0xB8);
  447. sil_iowrite32(dev, 0x40094009, base ? (base + 0xEC) : 0xBC);
  448. if (base && pdev_is_sata(dev)) {
  449. writel(0xFFFF0000, ioaddr + 0x108);
  450. writel(0xFFFF0000, ioaddr + 0x188);
  451. writel(0x00680000, ioaddr + 0x148);
  452. writel(0x00680000, ioaddr + 0x1C8);
  453. }
  454. /* report the clocking mode of the controller */
  455. if (!pdev_is_sata(dev)) {
  456. static const char *clk_str[] =
  457. { "== 100", "== 133", "== 2X PCI", "DISABLED!" };
  458. tmp >>= 4;
  459. printk(KERN_INFO "%s: BASE CLOCK %s\n", name, clk_str[tmp & 3]);
  460. }
  461. return 0;
  462. }
  463. /**
  464. * init_mmio_iops_siimage - set up the iops for MMIO
  465. * @hwif: interface to set up
  466. *
  467. * The basic setup here is fairly simple, we can use standard MMIO
  468. * operations. However we do have to set the taskfile register offsets
  469. * by hand as there isn't a standard defined layout for them this time.
  470. *
  471. * The hardware supports buffered taskfiles and also some rather nice
  472. * extended PRD tables. For better SI3112 support use the libata driver
  473. */
  474. static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
  475. {
  476. struct pci_dev *dev = to_pci_dev(hwif->dev);
  477. struct ide_host *host = pci_get_drvdata(dev);
  478. void *addr = host->host_priv;
  479. u8 ch = hwif->channel;
  480. struct ide_io_ports *io_ports = &hwif->io_ports;
  481. unsigned long base;
  482. /*
  483. * Fill in the basic hwif bits
  484. */
  485. hwif->host_flags |= IDE_HFLAG_MMIO;
  486. hwif->hwif_data = addr;
  487. /*
  488. * Now set up the hw. We have to do this ourselves as the
  489. * MMIO layout isn't the same as the standard port based I/O.
  490. */
  491. memset(io_ports, 0, sizeof(*io_ports));
  492. base = (unsigned long)addr;
  493. if (ch)
  494. base += 0xC0;
  495. else
  496. base += 0x80;
  497. /*
  498. * The buffered task file doesn't have status/control, so we
  499. * can't currently use it sanely since we want to use LBA48 mode.
  500. */
  501. io_ports->data_addr = base;
  502. io_ports->error_addr = base + 1;
  503. io_ports->nsect_addr = base + 2;
  504. io_ports->lbal_addr = base + 3;
  505. io_ports->lbam_addr = base + 4;
  506. io_ports->lbah_addr = base + 5;
  507. io_ports->device_addr = base + 6;
  508. io_ports->status_addr = base + 7;
  509. io_ports->ctl_addr = base + 10;
  510. if (pdev_is_sata(dev)) {
  511. base = (unsigned long)addr;
  512. if (ch)
  513. base += 0x80;
  514. hwif->sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
  515. hwif->sata_scr[SATA_ERROR_OFFSET] = base + 0x108;
  516. hwif->sata_scr[SATA_CONTROL_OFFSET] = base + 0x100;
  517. }
  518. hwif->irq = dev->irq;
  519. hwif->dma_base = (unsigned long)addr + (ch ? 0x08 : 0x00);
  520. }
  521. static int is_dev_seagate_sata(ide_drive_t *drive)
  522. {
  523. const char *s = &drive->id->model[0];
  524. unsigned len = strnlen(s, sizeof(drive->id->model));
  525. if ((len > 4) && (!memcmp(s, "ST", 2)))
  526. if ((!memcmp(s + len - 2, "AS", 2)) ||
  527. (!memcmp(s + len - 3, "ASL", 3))) {
  528. printk(KERN_INFO "%s: applying pessimistic Seagate "
  529. "errata fix\n", drive->name);
  530. return 1;
  531. }
  532. return 0;
  533. }
  534. /**
  535. * sil_quirkproc - post probe fixups
  536. * @drive: drive
  537. *
  538. * Called after drive probe we use this to decide whether the
  539. * Seagate fixup must be applied. This used to be in init_iops but
  540. * that can occur before we know what drives are present.
  541. */
  542. static void __devinit sil_quirkproc(ide_drive_t *drive)
  543. {
  544. ide_hwif_t *hwif = drive->hwif;
  545. /* Try and rise the rqsize */
  546. if (!is_sata(hwif) || !is_dev_seagate_sata(drive))
  547. hwif->rqsize = 128;
  548. }
  549. /**
  550. * init_iops_siimage - set up iops
  551. * @hwif: interface to set up
  552. *
  553. * Do the basic setup for the SIIMAGE hardware interface
  554. * and then do the MMIO setup if we can. This is the first
  555. * look in we get for setting up the hwif so that we
  556. * can get the iops right before using them.
  557. */
  558. static void __devinit init_iops_siimage(ide_hwif_t *hwif)
  559. {
  560. struct pci_dev *dev = to_pci_dev(hwif->dev);
  561. struct ide_host *host = pci_get_drvdata(dev);
  562. hwif->hwif_data = NULL;
  563. /* Pessimal until we finish probing */
  564. hwif->rqsize = 15;
  565. if (host->host_priv)
  566. init_mmio_iops_siimage(hwif);
  567. }
  568. /**
  569. * sil_cable_detect - cable detection
  570. * @hwif: interface to check
  571. *
  572. * Check for the presence of an ATA66 capable cable on the interface.
  573. */
  574. static u8 __devinit sil_cable_detect(ide_hwif_t *hwif)
  575. {
  576. struct pci_dev *dev = to_pci_dev(hwif->dev);
  577. unsigned long addr = siimage_selreg(hwif, 0);
  578. u8 ata66 = sil_ioread8(dev, addr);
  579. return (ata66 & 0x01) ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
  580. }
  581. static const struct ide_port_ops sil_pata_port_ops = {
  582. .set_pio_mode = sil_set_pio_mode,
  583. .set_dma_mode = sil_set_dma_mode,
  584. .quirkproc = sil_quirkproc,
  585. .udma_filter = sil_pata_udma_filter,
  586. .cable_detect = sil_cable_detect,
  587. };
  588. static const struct ide_port_ops sil_sata_port_ops = {
  589. .set_pio_mode = sil_set_pio_mode,
  590. .set_dma_mode = sil_set_dma_mode,
  591. .reset_poll = sil_sata_reset_poll,
  592. .pre_reset = sil_sata_pre_reset,
  593. .quirkproc = sil_quirkproc,
  594. .udma_filter = sil_sata_udma_filter,
  595. .cable_detect = sil_cable_detect,
  596. };
  597. static const struct ide_dma_ops sil_dma_ops = {
  598. .dma_host_set = ide_dma_host_set,
  599. .dma_setup = ide_dma_setup,
  600. .dma_exec_cmd = ide_dma_exec_cmd,
  601. .dma_start = ide_dma_start,
  602. .dma_end = __ide_dma_end,
  603. .dma_test_irq = siimage_dma_test_irq,
  604. .dma_timeout = ide_dma_timeout,
  605. .dma_lost_irq = ide_dma_lost_irq,
  606. };
  607. #define DECLARE_SII_DEV(name_str, p_ops) \
  608. { \
  609. .name = name_str, \
  610. .init_chipset = init_chipset_siimage, \
  611. .init_iops = init_iops_siimage, \
  612. .port_ops = p_ops, \
  613. .dma_ops = &sil_dma_ops, \
  614. .pio_mask = ATA_PIO4, \
  615. .mwdma_mask = ATA_MWDMA2, \
  616. .udma_mask = ATA_UDMA6, \
  617. }
  618. static const struct ide_port_info siimage_chipsets[] __devinitdata = {
  619. /* 0 */ DECLARE_SII_DEV("SiI680", &sil_pata_port_ops),
  620. /* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA", &sil_sata_port_ops),
  621. /* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA", &sil_sata_port_ops)
  622. };
  623. /**
  624. * siimage_init_one - PCI layer discovery entry
  625. * @dev: PCI device
  626. * @id: ident table entry
  627. *
  628. * Called by the PCI code when it finds an SiI680 or SiI3112 controller.
  629. * We then use the IDE PCI generic helper to do most of the work.
  630. */
  631. static int __devinit siimage_init_one(struct pci_dev *dev,
  632. const struct pci_device_id *id)
  633. {
  634. void __iomem *ioaddr = NULL;
  635. resource_size_t bar5 = pci_resource_start(dev, 5);
  636. unsigned long barsize = pci_resource_len(dev, 5);
  637. int rc;
  638. struct ide_port_info d;
  639. u8 idx = id->driver_data;
  640. u8 BA5_EN;
  641. d = siimage_chipsets[idx];
  642. if (idx) {
  643. static int first = 1;
  644. if (first) {
  645. printk(KERN_INFO "siimage: For full SATA support you "
  646. "should use the libata sata_sil module.\n");
  647. first = 0;
  648. }
  649. d.host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
  650. }
  651. rc = pci_enable_device(dev);
  652. if (rc)
  653. return rc;
  654. pci_read_config_byte(dev, 0x8A, &BA5_EN);
  655. if ((BA5_EN & 0x01) || bar5) {
  656. /*
  657. * Drop back to PIO if we can't map the MMIO. Some systems
  658. * seem to get terminally confused in the PCI spaces.
  659. */
  660. if (!request_mem_region(bar5, barsize, d.name)) {
  661. printk(KERN_WARNING "siimage: IDE controller MMIO "
  662. "ports not available.\n");
  663. } else {
  664. ioaddr = ioremap(bar5, barsize);
  665. if (ioaddr == NULL)
  666. release_mem_region(bar5, barsize);
  667. }
  668. }
  669. rc = ide_pci_init_one(dev, &d, ioaddr);
  670. if (rc) {
  671. if (ioaddr) {
  672. iounmap(ioaddr);
  673. release_mem_region(bar5, barsize);
  674. }
  675. pci_disable_device(dev);
  676. }
  677. return rc;
  678. }
  679. static void __devexit siimage_remove(struct pci_dev *dev)
  680. {
  681. struct ide_host *host = pci_get_drvdata(dev);
  682. void __iomem *ioaddr = host->host_priv;
  683. ide_pci_remove(dev);
  684. if (ioaddr) {
  685. resource_size_t bar5 = pci_resource_start(dev, 5);
  686. unsigned long barsize = pci_resource_len(dev, 5);
  687. iounmap(ioaddr);
  688. release_mem_region(bar5, barsize);
  689. }
  690. pci_disable_device(dev);
  691. }
  692. static const struct pci_device_id siimage_pci_tbl[] = {
  693. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_680), 0 },
  694. #ifdef CONFIG_BLK_DEV_IDE_SATA
  695. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_3112), 1 },
  696. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_1210SA), 2 },
  697. #endif
  698. { 0, },
  699. };
  700. MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
  701. static struct pci_driver driver = {
  702. .name = "SiI_IDE",
  703. .id_table = siimage_pci_tbl,
  704. .probe = siimage_init_one,
  705. .remove = siimage_remove,
  706. };
  707. static int __init siimage_ide_init(void)
  708. {
  709. return ide_pci_register_driver(&driver);
  710. }
  711. static void __exit siimage_ide_exit(void)
  712. {
  713. pci_unregister_driver(&driver);
  714. }
  715. module_init(siimage_ide_init);
  716. module_exit(siimage_ide_exit);
  717. MODULE_AUTHOR("Andre Hedrick, Alan Cox");
  718. MODULE_DESCRIPTION("PCI driver module for SiI IDE");
  719. MODULE_LICENSE("GPL");