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. #define DRV_NAME "siimage"
  46. /**
  47. * pdev_is_sata - check if device is SATA
  48. * @pdev: PCI device to check
  49. *
  50. * Returns true if this is a SATA controller
  51. */
  52. static int pdev_is_sata(struct pci_dev *pdev)
  53. {
  54. #ifdef CONFIG_BLK_DEV_IDE_SATA
  55. switch (pdev->device) {
  56. case PCI_DEVICE_ID_SII_3112:
  57. case PCI_DEVICE_ID_SII_1210SA:
  58. return 1;
  59. case PCI_DEVICE_ID_SII_680:
  60. return 0;
  61. }
  62. BUG();
  63. #endif
  64. return 0;
  65. }
  66. /**
  67. * is_sata - check if hwif is SATA
  68. * @hwif: interface to check
  69. *
  70. * Returns true if this is a SATA controller
  71. */
  72. static inline int is_sata(ide_hwif_t *hwif)
  73. {
  74. return pdev_is_sata(to_pci_dev(hwif->dev));
  75. }
  76. /**
  77. * siimage_selreg - return register base
  78. * @hwif: interface
  79. * @r: config offset
  80. *
  81. * Turn a config register offset into the right address in either
  82. * PCI space or MMIO space to access the control register in question
  83. * Thankfully this is a configuration operation, so isn't performance
  84. * critical.
  85. */
  86. static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
  87. {
  88. unsigned long base = (unsigned long)hwif->hwif_data;
  89. base += 0xA0 + r;
  90. if (hwif->host_flags & IDE_HFLAG_MMIO)
  91. base += hwif->channel << 6;
  92. else
  93. base += hwif->channel << 4;
  94. return base;
  95. }
  96. /**
  97. * siimage_seldev - return register base
  98. * @hwif: interface
  99. * @r: config offset
  100. *
  101. * Turn a config register offset into the right address in either
  102. * PCI space or MMIO space to access the control register in question
  103. * including accounting for the unit shift.
  104. */
  105. static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
  106. {
  107. ide_hwif_t *hwif = HWIF(drive);
  108. unsigned long base = (unsigned long)hwif->hwif_data;
  109. base += 0xA0 + r;
  110. if (hwif->host_flags & IDE_HFLAG_MMIO)
  111. base += hwif->channel << 6;
  112. else
  113. base += hwif->channel << 4;
  114. base |= drive->select.b.unit << drive->select.b.unit;
  115. return base;
  116. }
  117. static u8 sil_ioread8(struct pci_dev *dev, unsigned long addr)
  118. {
  119. struct ide_host *host = pci_get_drvdata(dev);
  120. u8 tmp = 0;
  121. if (host->host_priv)
  122. tmp = readb((void __iomem *)addr);
  123. else
  124. pci_read_config_byte(dev, addr, &tmp);
  125. return tmp;
  126. }
  127. static u16 sil_ioread16(struct pci_dev *dev, unsigned long addr)
  128. {
  129. struct ide_host *host = pci_get_drvdata(dev);
  130. u16 tmp = 0;
  131. if (host->host_priv)
  132. tmp = readw((void __iomem *)addr);
  133. else
  134. pci_read_config_word(dev, addr, &tmp);
  135. return tmp;
  136. }
  137. static void sil_iowrite8(struct pci_dev *dev, u8 val, unsigned long addr)
  138. {
  139. struct ide_host *host = pci_get_drvdata(dev);
  140. if (host->host_priv)
  141. writeb(val, (void __iomem *)addr);
  142. else
  143. pci_write_config_byte(dev, addr, val);
  144. }
  145. static void sil_iowrite16(struct pci_dev *dev, u16 val, unsigned long addr)
  146. {
  147. struct ide_host *host = pci_get_drvdata(dev);
  148. if (host->host_priv)
  149. writew(val, (void __iomem *)addr);
  150. else
  151. pci_write_config_word(dev, addr, val);
  152. }
  153. static void sil_iowrite32(struct pci_dev *dev, u32 val, unsigned long addr)
  154. {
  155. struct ide_host *host = pci_get_drvdata(dev);
  156. if (host->host_priv)
  157. writel(val, (void __iomem *)addr);
  158. else
  159. pci_write_config_dword(dev, addr, val);
  160. }
  161. /**
  162. * sil_udma_filter - compute UDMA mask
  163. * @drive: IDE device
  164. *
  165. * Compute the available UDMA speeds for the device on the interface.
  166. *
  167. * For the CMD680 this depends on the clocking mode (scsc), for the
  168. * SI3112 SATA controller life is a bit simpler.
  169. */
  170. static u8 sil_pata_udma_filter(ide_drive_t *drive)
  171. {
  172. ide_hwif_t *hwif = drive->hwif;
  173. struct pci_dev *dev = to_pci_dev(hwif->dev);
  174. unsigned long base = (unsigned long)hwif->hwif_data;
  175. u8 scsc, mask = 0;
  176. base += (hwif->host_flags & IDE_HFLAG_MMIO) ? 0x4A : 0x8A;
  177. scsc = sil_ioread8(dev, base);
  178. switch (scsc & 0x30) {
  179. case 0x10: /* 133 */
  180. mask = ATA_UDMA6;
  181. break;
  182. case 0x20: /* 2xPCI */
  183. mask = ATA_UDMA6;
  184. break;
  185. case 0x00: /* 100 */
  186. mask = ATA_UDMA5;
  187. break;
  188. default: /* Disabled ? */
  189. BUG();
  190. }
  191. return mask;
  192. }
  193. static u8 sil_sata_udma_filter(ide_drive_t *drive)
  194. {
  195. return strstr(drive->id->model, "Maxtor") ? ATA_UDMA5 : ATA_UDMA6;
  196. }
  197. /**
  198. * sil_set_pio_mode - set host controller for PIO mode
  199. * @drive: drive
  200. * @pio: PIO mode number
  201. *
  202. * Load the timing settings for this device mode into the
  203. * controller. If we are in PIO mode 3 or 4 turn on IORDY
  204. * monitoring (bit 9). The TF timing is bits 31:16
  205. */
  206. static void sil_set_pio_mode(ide_drive_t *drive, u8 pio)
  207. {
  208. static const u16 tf_speed[] = { 0x328a, 0x2283, 0x1281, 0x10c3, 0x10c1 };
  209. static const u16 data_speed[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
  210. ide_hwif_t *hwif = HWIF(drive);
  211. struct pci_dev *dev = to_pci_dev(hwif->dev);
  212. ide_drive_t *pair = ide_get_paired_drive(drive);
  213. u32 speedt = 0;
  214. u16 speedp = 0;
  215. unsigned long addr = siimage_seldev(drive, 0x04);
  216. unsigned long tfaddr = siimage_selreg(hwif, 0x02);
  217. unsigned long base = (unsigned long)hwif->hwif_data;
  218. u8 tf_pio = pio;
  219. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  220. u8 addr_mask = hwif->channel ? (mmio ? 0xF4 : 0x84)
  221. : (mmio ? 0xB4 : 0x80);
  222. u8 mode = 0;
  223. u8 unit = drive->select.b.unit;
  224. /* trim *taskfile* PIO to the slowest of the master/slave */
  225. if (pair->present) {
  226. u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
  227. if (pair_pio < tf_pio)
  228. tf_pio = pair_pio;
  229. }
  230. /* cheat for now and use the docs */
  231. speedp = data_speed[pio];
  232. speedt = tf_speed[tf_pio];
  233. sil_iowrite16(dev, speedp, addr);
  234. sil_iowrite16(dev, speedt, tfaddr);
  235. /* now set up IORDY */
  236. speedp = sil_ioread16(dev, tfaddr - 2);
  237. speedp &= ~0x200;
  238. if (pio > 2)
  239. speedp |= 0x200;
  240. sil_iowrite16(dev, speedp, tfaddr - 2);
  241. mode = sil_ioread8(dev, base + addr_mask);
  242. mode &= ~(unit ? 0x30 : 0x03);
  243. mode |= unit ? 0x10 : 0x01;
  244. sil_iowrite8(dev, mode, base + addr_mask);
  245. }
  246. /**
  247. * sil_set_dma_mode - set host controller for DMA mode
  248. * @drive: drive
  249. * @speed: DMA mode
  250. *
  251. * Tune the SiI chipset for the desired DMA mode.
  252. */
  253. static void sil_set_dma_mode(ide_drive_t *drive, const u8 speed)
  254. {
  255. static const u8 ultra6[] = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
  256. static const u8 ultra5[] = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
  257. static const u16 dma[] = { 0x2208, 0x10C2, 0x10C1 };
  258. ide_hwif_t *hwif = HWIF(drive);
  259. struct pci_dev *dev = to_pci_dev(hwif->dev);
  260. u16 ultra = 0, multi = 0;
  261. u8 mode = 0, unit = drive->select.b.unit;
  262. unsigned long base = (unsigned long)hwif->hwif_data;
  263. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  264. u8 scsc = 0, addr_mask = hwif->channel ? (mmio ? 0xF4 : 0x84)
  265. : (mmio ? 0xB4 : 0x80);
  266. unsigned long ma = siimage_seldev(drive, 0x08);
  267. unsigned long ua = siimage_seldev(drive, 0x0C);
  268. scsc = sil_ioread8 (dev, base + (mmio ? 0x4A : 0x8A));
  269. mode = sil_ioread8 (dev, base + addr_mask);
  270. multi = sil_ioread16(dev, ma);
  271. ultra = sil_ioread16(dev, ua);
  272. mode &= ~(unit ? 0x30 : 0x03);
  273. ultra &= ~0x3F;
  274. scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
  275. scsc = is_sata(hwif) ? 1 : scsc;
  276. if (speed >= XFER_UDMA_0) {
  277. multi = dma[2];
  278. ultra |= scsc ? ultra6[speed - XFER_UDMA_0] :
  279. ultra5[speed - XFER_UDMA_0];
  280. mode |= unit ? 0x30 : 0x03;
  281. } else {
  282. multi = dma[speed - XFER_MW_DMA_0];
  283. mode |= unit ? 0x20 : 0x02;
  284. }
  285. sil_iowrite8 (dev, mode, base + addr_mask);
  286. sil_iowrite16(dev, multi, ma);
  287. sil_iowrite16(dev, ultra, ua);
  288. }
  289. /* returns 1 if dma irq issued, 0 otherwise */
  290. static int siimage_io_dma_test_irq(ide_drive_t *drive)
  291. {
  292. ide_hwif_t *hwif = HWIF(drive);
  293. struct pci_dev *dev = to_pci_dev(hwif->dev);
  294. u8 dma_altstat = 0;
  295. unsigned long addr = siimage_selreg(hwif, 1);
  296. /* return 1 if INTR asserted */
  297. if (inb(hwif->dma_base + ATA_DMA_STATUS) & 4)
  298. return 1;
  299. /* return 1 if Device INTR asserted */
  300. pci_read_config_byte(dev, addr, &dma_altstat);
  301. if (dma_altstat & 8)
  302. return 0; /* return 1; */
  303. return 0;
  304. }
  305. /**
  306. * siimage_mmio_dma_test_irq - check we caused an IRQ
  307. * @drive: drive we are testing
  308. *
  309. * Check if we caused an IDE DMA interrupt. We may also have caused
  310. * SATA status interrupts, if so we clean them up and continue.
  311. */
  312. static int siimage_mmio_dma_test_irq(ide_drive_t *drive)
  313. {
  314. ide_hwif_t *hwif = HWIF(drive);
  315. unsigned long addr = siimage_selreg(hwif, 0x1);
  316. void __iomem *sata_error_addr
  317. = (void __iomem *)hwif->sata_scr[SATA_ERROR_OFFSET];
  318. if (sata_error_addr) {
  319. unsigned long base = (unsigned long)hwif->hwif_data;
  320. u32 ext_stat = readl((void __iomem *)(base + 0x10));
  321. u8 watchdog = 0;
  322. if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
  323. u32 sata_error = readl(sata_error_addr);
  324. writel(sata_error, sata_error_addr);
  325. watchdog = (sata_error & 0x00680000) ? 1 : 0;
  326. printk(KERN_WARNING "%s: sata_error = 0x%08x, "
  327. "watchdog = %d, %s\n",
  328. drive->name, sata_error, watchdog, __func__);
  329. } else
  330. watchdog = (ext_stat & 0x8000) ? 1 : 0;
  331. ext_stat >>= 16;
  332. if (!(ext_stat & 0x0404) && !watchdog)
  333. return 0;
  334. }
  335. /* return 1 if INTR asserted */
  336. if (readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS)) & 4)
  337. return 1;
  338. /* return 1 if Device INTR asserted */
  339. if (readb((void __iomem *)addr) & 8)
  340. return 0; /* return 1; */
  341. return 0;
  342. }
  343. static int siimage_dma_test_irq(ide_drive_t *drive)
  344. {
  345. if (drive->hwif->host_flags & IDE_HFLAG_MMIO)
  346. return siimage_mmio_dma_test_irq(drive);
  347. else
  348. return siimage_io_dma_test_irq(drive);
  349. }
  350. /**
  351. * sil_sata_reset_poll - wait for SATA reset
  352. * @drive: drive we are resetting
  353. *
  354. * Poll the SATA phy and see whether it has come back from the dead
  355. * yet.
  356. */
  357. static int sil_sata_reset_poll(ide_drive_t *drive)
  358. {
  359. ide_hwif_t *hwif = drive->hwif;
  360. void __iomem *sata_status_addr
  361. = (void __iomem *)hwif->sata_scr[SATA_STATUS_OFFSET];
  362. if (sata_status_addr) {
  363. /* SATA Status is available only when in MMIO mode */
  364. u32 sata_stat = readl(sata_status_addr);
  365. if ((sata_stat & 0x03) != 0x03) {
  366. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  367. hwif->name, sata_stat);
  368. return -ENXIO;
  369. }
  370. }
  371. return 0;
  372. }
  373. /**
  374. * sil_sata_pre_reset - reset hook
  375. * @drive: IDE device being reset
  376. *
  377. * For the SATA devices we need to handle recalibration/geometry
  378. * differently
  379. */
  380. static void sil_sata_pre_reset(ide_drive_t *drive)
  381. {
  382. if (drive->media == ide_disk) {
  383. drive->special.b.set_geometry = 0;
  384. drive->special.b.recalibrate = 0;
  385. }
  386. }
  387. /**
  388. * init_chipset_siimage - set up an SI device
  389. * @dev: PCI device
  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. {
  396. struct ide_host *host = pci_get_drvdata(dev);
  397. void __iomem *ioaddr = host->host_priv;
  398. unsigned long base, scsc_addr;
  399. u8 rev = dev->revision, tmp;
  400. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, rev ? 1 : 255);
  401. if (ioaddr)
  402. pci_set_master(dev);
  403. base = (unsigned long)ioaddr;
  404. if (ioaddr && pdev_is_sata(dev)) {
  405. u32 tmp32, irq_mask;
  406. /* make sure IDE0/1 interrupts are not masked */
  407. irq_mask = (1 << 22) | (1 << 23);
  408. tmp32 = readl(ioaddr + 0x48);
  409. if (tmp32 & irq_mask) {
  410. tmp32 &= ~irq_mask;
  411. writel(tmp32, ioaddr + 0x48);
  412. readl(ioaddr + 0x48); /* flush */
  413. }
  414. writel(0, ioaddr + 0x148);
  415. writel(0, ioaddr + 0x1C8);
  416. }
  417. sil_iowrite8(dev, 0, base ? (base + 0xB4) : 0x80);
  418. sil_iowrite8(dev, 0, base ? (base + 0xF4) : 0x84);
  419. scsc_addr = base ? (base + 0x4A) : 0x8A;
  420. tmp = sil_ioread8(dev, scsc_addr);
  421. switch (tmp & 0x30) {
  422. case 0x00:
  423. /* On 100 MHz clocking, try and switch to 133 MHz */
  424. sil_iowrite8(dev, tmp | 0x10, scsc_addr);
  425. break;
  426. case 0x30:
  427. /* Clocking is disabled, attempt to force 133MHz clocking. */
  428. sil_iowrite8(dev, tmp & ~0x20, scsc_addr);
  429. case 0x10:
  430. /* On 133Mhz clocking. */
  431. break;
  432. case 0x20:
  433. /* On PCIx2 clocking. */
  434. break;
  435. }
  436. tmp = sil_ioread8(dev, scsc_addr);
  437. sil_iowrite8 (dev, 0x72, base + 0xA1);
  438. sil_iowrite16(dev, 0x328A, base + 0xA2);
  439. sil_iowrite32(dev, 0x62DD62DD, base + 0xA4);
  440. sil_iowrite32(dev, 0x43924392, base + 0xA8);
  441. sil_iowrite32(dev, 0x40094009, base + 0xAC);
  442. sil_iowrite8 (dev, 0x72, base ? (base + 0xE1) : 0xB1);
  443. sil_iowrite16(dev, 0x328A, base ? (base + 0xE2) : 0xB2);
  444. sil_iowrite32(dev, 0x62DD62DD, base ? (base + 0xE4) : 0xB4);
  445. sil_iowrite32(dev, 0x43924392, base ? (base + 0xE8) : 0xB8);
  446. sil_iowrite32(dev, 0x40094009, base ? (base + 0xEC) : 0xBC);
  447. if (base && pdev_is_sata(dev)) {
  448. writel(0xFFFF0000, ioaddr + 0x108);
  449. writel(0xFFFF0000, ioaddr + 0x188);
  450. writel(0x00680000, ioaddr + 0x148);
  451. writel(0x00680000, ioaddr + 0x1C8);
  452. }
  453. /* report the clocking mode of the controller */
  454. if (!pdev_is_sata(dev)) {
  455. static const char *clk_str[] =
  456. { "== 100", "== 133", "== 2X PCI", "DISABLED!" };
  457. tmp >>= 4;
  458. printk(KERN_INFO DRV_NAME " %s: BASE CLOCK %s\n",
  459. pci_name(dev), 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 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 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(p_ops) \
  608. { \
  609. .name = DRV_NAME, \
  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: SiI680 */ DECLARE_SII_DEV(&sil_pata_port_ops),
  620. /* 1: SiI3112 */ DECLARE_SII_DEV(&sil_sata_port_ops)
  621. };
  622. /**
  623. * siimage_init_one - PCI layer discovery entry
  624. * @dev: PCI device
  625. * @id: ident table entry
  626. *
  627. * Called by the PCI code when it finds an SiI680 or SiI3112 controller.
  628. * We then use the IDE PCI generic helper to do most of the work.
  629. */
  630. static int __devinit siimage_init_one(struct pci_dev *dev,
  631. const struct pci_device_id *id)
  632. {
  633. void __iomem *ioaddr = NULL;
  634. resource_size_t bar5 = pci_resource_start(dev, 5);
  635. unsigned long barsize = pci_resource_len(dev, 5);
  636. int rc;
  637. struct ide_port_info d;
  638. u8 idx = id->driver_data;
  639. u8 BA5_EN;
  640. d = siimage_chipsets[idx];
  641. if (idx) {
  642. static int first = 1;
  643. if (first) {
  644. printk(KERN_INFO DRV_NAME ": For full SATA support you "
  645. "should use the libata sata_sil module.\n");
  646. first = 0;
  647. }
  648. d.host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
  649. }
  650. rc = pci_enable_device(dev);
  651. if (rc)
  652. return rc;
  653. pci_read_config_byte(dev, 0x8A, &BA5_EN);
  654. if ((BA5_EN & 0x01) || bar5) {
  655. /*
  656. * Drop back to PIO if we can't map the MMIO. Some systems
  657. * seem to get terminally confused in the PCI spaces.
  658. */
  659. if (!request_mem_region(bar5, barsize, d.name)) {
  660. printk(KERN_WARNING DRV_NAME " %s: MMIO ports not "
  661. "available\n", pci_name(dev));
  662. } else {
  663. ioaddr = ioremap(bar5, barsize);
  664. if (ioaddr == NULL)
  665. release_mem_region(bar5, barsize);
  666. }
  667. }
  668. rc = ide_pci_init_one(dev, &d, ioaddr);
  669. if (rc) {
  670. if (ioaddr) {
  671. iounmap(ioaddr);
  672. release_mem_region(bar5, barsize);
  673. }
  674. pci_disable_device(dev);
  675. }
  676. return rc;
  677. }
  678. static void __devexit siimage_remove(struct pci_dev *dev)
  679. {
  680. struct ide_host *host = pci_get_drvdata(dev);
  681. void __iomem *ioaddr = host->host_priv;
  682. ide_pci_remove(dev);
  683. if (ioaddr) {
  684. resource_size_t bar5 = pci_resource_start(dev, 5);
  685. unsigned long barsize = pci_resource_len(dev, 5);
  686. iounmap(ioaddr);
  687. release_mem_region(bar5, barsize);
  688. }
  689. pci_disable_device(dev);
  690. }
  691. static const struct pci_device_id siimage_pci_tbl[] = {
  692. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_680), 0 },
  693. #ifdef CONFIG_BLK_DEV_IDE_SATA
  694. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_3112), 1 },
  695. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_SII_1210SA), 1 },
  696. #endif
  697. { 0, },
  698. };
  699. MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
  700. static struct pci_driver driver = {
  701. .name = "SiI_IDE",
  702. .id_table = siimage_pci_tbl,
  703. .probe = siimage_init_one,
  704. .remove = __devexit_p(siimage_remove),
  705. };
  706. static int __init siimage_ide_init(void)
  707. {
  708. return ide_pci_register_driver(&driver);
  709. }
  710. static void __exit siimage_ide_exit(void)
  711. {
  712. pci_unregister_driver(&driver);
  713. }
  714. module_init(siimage_ide_init);
  715. module_exit(siimage_ide_exit);
  716. MODULE_AUTHOR("Andre Hedrick, Alan Cox");
  717. MODULE_DESCRIPTION("PCI driver module for SiI IDE");
  718. MODULE_LICENSE("GPL");