siimage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 2003 Red Hat
  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/ide.h>
  42. #include <linux/init.h>
  43. #include <linux/io.h>
  44. #define DRV_NAME "siimage"
  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. u8 unit = drive->dn & 1;
  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 |= unit << 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. char *m = (char *)&drive->id[ATA_ID_PROD];
  196. return strstr(m, "Maxtor") ? ATA_UDMA5 : ATA_UDMA6;
  197. }
  198. /**
  199. * sil_set_pio_mode - set host controller for PIO mode
  200. * @drive: drive
  201. * @pio: PIO mode number
  202. *
  203. * Load the timing settings for this device mode into the
  204. * controller. If we are in PIO mode 3 or 4 turn on IORDY
  205. * monitoring (bit 9). The TF timing is bits 31:16
  206. */
  207. static void sil_set_pio_mode(ide_drive_t *drive, u8 pio)
  208. {
  209. static const u16 tf_speed[] = { 0x328a, 0x2283, 0x1281, 0x10c3, 0x10c1 };
  210. static const u16 data_speed[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
  211. ide_hwif_t *hwif = HWIF(drive);
  212. struct pci_dev *dev = to_pci_dev(hwif->dev);
  213. ide_drive_t *pair = ide_get_pair_dev(drive);
  214. u32 speedt = 0;
  215. u16 speedp = 0;
  216. unsigned long addr = siimage_seldev(drive, 0x04);
  217. unsigned long tfaddr = siimage_selreg(hwif, 0x02);
  218. unsigned long base = (unsigned long)hwif->hwif_data;
  219. u8 tf_pio = pio;
  220. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  221. u8 addr_mask = hwif->channel ? (mmio ? 0xF4 : 0x84)
  222. : (mmio ? 0xB4 : 0x80);
  223. u8 mode = 0;
  224. u8 unit = drive->dn & 1;
  225. /* trim *taskfile* PIO to the slowest of the master/slave */
  226. if (pair) {
  227. u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
  228. if (pair_pio < tf_pio)
  229. tf_pio = pair_pio;
  230. }
  231. /* cheat for now and use the docs */
  232. speedp = data_speed[pio];
  233. speedt = tf_speed[tf_pio];
  234. sil_iowrite16(dev, speedp, addr);
  235. sil_iowrite16(dev, speedt, tfaddr);
  236. /* now set up IORDY */
  237. speedp = sil_ioread16(dev, tfaddr - 2);
  238. speedp &= ~0x200;
  239. if (pio > 2)
  240. speedp |= 0x200;
  241. sil_iowrite16(dev, speedp, tfaddr - 2);
  242. mode = sil_ioread8(dev, base + addr_mask);
  243. mode &= ~(unit ? 0x30 : 0x03);
  244. mode |= unit ? 0x10 : 0x01;
  245. sil_iowrite8(dev, mode, base + addr_mask);
  246. }
  247. /**
  248. * sil_set_dma_mode - set host controller for DMA mode
  249. * @drive: drive
  250. * @speed: DMA mode
  251. *
  252. * Tune the SiI chipset for the desired DMA mode.
  253. */
  254. static void sil_set_dma_mode(ide_drive_t *drive, const u8 speed)
  255. {
  256. static const u8 ultra6[] = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
  257. static const u8 ultra5[] = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
  258. static const u16 dma[] = { 0x2208, 0x10C2, 0x10C1 };
  259. ide_hwif_t *hwif = HWIF(drive);
  260. struct pci_dev *dev = to_pci_dev(hwif->dev);
  261. unsigned long base = (unsigned long)hwif->hwif_data;
  262. u16 ultra = 0, multi = 0;
  263. u8 mode = 0, unit = drive->dn & 1;
  264. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  265. u8 scsc = 0, addr_mask = hwif->channel ? (mmio ? 0xF4 : 0x84)
  266. : (mmio ? 0xB4 : 0x80);
  267. unsigned long ma = siimage_seldev(drive, 0x08);
  268. unsigned long ua = siimage_seldev(drive, 0x0C);
  269. scsc = sil_ioread8 (dev, base + (mmio ? 0x4A : 0x8A));
  270. mode = sil_ioread8 (dev, base + addr_mask);
  271. multi = sil_ioread16(dev, ma);
  272. ultra = sil_ioread16(dev, ua);
  273. mode &= ~(unit ? 0x30 : 0x03);
  274. ultra &= ~0x3F;
  275. scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
  276. scsc = is_sata(hwif) ? 1 : scsc;
  277. if (speed >= XFER_UDMA_0) {
  278. multi = dma[2];
  279. ultra |= scsc ? ultra6[speed - XFER_UDMA_0] :
  280. ultra5[speed - XFER_UDMA_0];
  281. mode |= unit ? 0x30 : 0x03;
  282. } else {
  283. multi = dma[speed - XFER_MW_DMA_0];
  284. mode |= unit ? 0x20 : 0x02;
  285. }
  286. sil_iowrite8 (dev, mode, base + addr_mask);
  287. sil_iowrite16(dev, multi, ma);
  288. sil_iowrite16(dev, ultra, ua);
  289. }
  290. /* returns 1 if dma irq issued, 0 otherwise */
  291. static int siimage_io_dma_test_irq(ide_drive_t *drive)
  292. {
  293. ide_hwif_t *hwif = HWIF(drive);
  294. struct pci_dev *dev = to_pci_dev(hwif->dev);
  295. u8 dma_altstat = 0;
  296. unsigned long addr = siimage_selreg(hwif, 1);
  297. /* return 1 if INTR asserted */
  298. if (inb(hwif->dma_base + ATA_DMA_STATUS) & 4)
  299. return 1;
  300. /* return 1 if Device INTR asserted */
  301. pci_read_config_byte(dev, addr, &dma_altstat);
  302. if (dma_altstat & 8)
  303. return 0; /* return 1; */
  304. return 0;
  305. }
  306. /**
  307. * siimage_mmio_dma_test_irq - check we caused an IRQ
  308. * @drive: drive we are testing
  309. *
  310. * Check if we caused an IDE DMA interrupt. We may also have caused
  311. * SATA status interrupts, if so we clean them up and continue.
  312. */
  313. static int siimage_mmio_dma_test_irq(ide_drive_t *drive)
  314. {
  315. ide_hwif_t *hwif = HWIF(drive);
  316. unsigned long addr = siimage_selreg(hwif, 0x1);
  317. void __iomem *sata_error_addr
  318. = (void __iomem *)hwif->sata_scr[SATA_ERROR_OFFSET];
  319. if (sata_error_addr) {
  320. unsigned long base = (unsigned long)hwif->hwif_data;
  321. u32 ext_stat = readl((void __iomem *)(base + 0x10));
  322. u8 watchdog = 0;
  323. if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
  324. u32 sata_error = readl(sata_error_addr);
  325. writel(sata_error, sata_error_addr);
  326. watchdog = (sata_error & 0x00680000) ? 1 : 0;
  327. printk(KERN_WARNING "%s: sata_error = 0x%08x, "
  328. "watchdog = %d, %s\n",
  329. drive->name, sata_error, watchdog, __func__);
  330. } else
  331. watchdog = (ext_stat & 0x8000) ? 1 : 0;
  332. ext_stat >>= 16;
  333. if (!(ext_stat & 0x0404) && !watchdog)
  334. return 0;
  335. }
  336. /* return 1 if INTR asserted */
  337. if (readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS)) & 4)
  338. return 1;
  339. /* return 1 if Device INTR asserted */
  340. if (readb((void __iomem *)addr) & 8)
  341. return 0; /* return 1; */
  342. return 0;
  343. }
  344. static int siimage_dma_test_irq(ide_drive_t *drive)
  345. {
  346. if (drive->hwif->host_flags & IDE_HFLAG_MMIO)
  347. return siimage_mmio_dma_test_irq(drive);
  348. else
  349. return siimage_io_dma_test_irq(drive);
  350. }
  351. /**
  352. * sil_sata_reset_poll - wait for SATA reset
  353. * @drive: drive we are resetting
  354. *
  355. * Poll the SATA phy and see whether it has come back from the dead
  356. * yet.
  357. */
  358. static int sil_sata_reset_poll(ide_drive_t *drive)
  359. {
  360. ide_hwif_t *hwif = drive->hwif;
  361. void __iomem *sata_status_addr
  362. = (void __iomem *)hwif->sata_scr[SATA_STATUS_OFFSET];
  363. if (sata_status_addr) {
  364. /* SATA Status is available only when in MMIO mode */
  365. u32 sata_stat = readl(sata_status_addr);
  366. if ((sata_stat & 0x03) != 0x03) {
  367. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  368. hwif->name, sata_stat);
  369. return -ENXIO;
  370. }
  371. }
  372. return 0;
  373. }
  374. /**
  375. * sil_sata_pre_reset - reset hook
  376. * @drive: IDE device being reset
  377. *
  378. * For the SATA devices we need to handle recalibration/geometry
  379. * differently
  380. */
  381. static void sil_sata_pre_reset(ide_drive_t *drive)
  382. {
  383. if (drive->media == ide_disk) {
  384. drive->special.b.set_geometry = 0;
  385. drive->special.b.recalibrate = 0;
  386. }
  387. }
  388. /**
  389. * init_chipset_siimage - set up an SI device
  390. * @dev: PCI device
  391. *
  392. * Perform the initial PCI set up for this device. Attempt to switch
  393. * to 133 MHz clocking if the system isn't already set up to do it.
  394. */
  395. static unsigned int init_chipset_siimage(struct pci_dev *dev)
  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 DRV_NAME " %s: BASE CLOCK %s\n",
  460. pci_name(dev), clk_str[tmp & 3]);
  461. }
  462. return 0;
  463. }
  464. /**
  465. * init_mmio_iops_siimage - set up the iops for MMIO
  466. * @hwif: interface to set up
  467. *
  468. * The basic setup here is fairly simple, we can use standard MMIO
  469. * operations. However we do have to set the taskfile register offsets
  470. * by hand as there isn't a standard defined layout for them this time.
  471. *
  472. * The hardware supports buffered taskfiles and also some rather nice
  473. * extended PRD tables. For better SI3112 support use the libata driver
  474. */
  475. static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
  476. {
  477. struct pci_dev *dev = to_pci_dev(hwif->dev);
  478. struct ide_host *host = pci_get_drvdata(dev);
  479. void *addr = host->host_priv;
  480. u8 ch = hwif->channel;
  481. struct ide_io_ports *io_ports = &hwif->io_ports;
  482. unsigned long base;
  483. /*
  484. * Fill in the basic hwif bits
  485. */
  486. hwif->host_flags |= IDE_HFLAG_MMIO;
  487. hwif->hwif_data = addr;
  488. /*
  489. * Now set up the hw. We have to do this ourselves as the
  490. * MMIO layout isn't the same as the standard port based I/O.
  491. */
  492. memset(io_ports, 0, sizeof(*io_ports));
  493. base = (unsigned long)addr;
  494. if (ch)
  495. base += 0xC0;
  496. else
  497. base += 0x80;
  498. /*
  499. * The buffered task file doesn't have status/control, so we
  500. * can't currently use it sanely since we want to use LBA48 mode.
  501. */
  502. io_ports->data_addr = base;
  503. io_ports->error_addr = base + 1;
  504. io_ports->nsect_addr = base + 2;
  505. io_ports->lbal_addr = base + 3;
  506. io_ports->lbam_addr = base + 4;
  507. io_ports->lbah_addr = base + 5;
  508. io_ports->device_addr = base + 6;
  509. io_ports->status_addr = base + 7;
  510. io_ports->ctl_addr = base + 10;
  511. if (pdev_is_sata(dev)) {
  512. base = (unsigned long)addr;
  513. if (ch)
  514. base += 0x80;
  515. hwif->sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
  516. hwif->sata_scr[SATA_ERROR_OFFSET] = base + 0x108;
  517. hwif->sata_scr[SATA_CONTROL_OFFSET] = base + 0x100;
  518. }
  519. hwif->irq = dev->irq;
  520. hwif->dma_base = (unsigned long)addr + (ch ? 0x08 : 0x00);
  521. }
  522. static int is_dev_seagate_sata(ide_drive_t *drive)
  523. {
  524. const char *s = (const char *)&drive->id[ATA_ID_PROD];
  525. unsigned len = strnlen(s, ATA_ID_PROD_LEN);
  526. if ((len > 4) && (!memcmp(s, "ST", 2)))
  527. if ((!memcmp(s + len - 2, "AS", 2)) ||
  528. (!memcmp(s + len - 3, "ASL", 3))) {
  529. printk(KERN_INFO "%s: applying pessimistic Seagate "
  530. "errata fix\n", drive->name);
  531. return 1;
  532. }
  533. return 0;
  534. }
  535. /**
  536. * sil_quirkproc - post probe fixups
  537. * @drive: drive
  538. *
  539. * Called after drive probe we use this to decide whether the
  540. * Seagate fixup must be applied. This used to be in init_iops but
  541. * that can occur before we know what drives are present.
  542. */
  543. static void sil_quirkproc(ide_drive_t *drive)
  544. {
  545. ide_hwif_t *hwif = drive->hwif;
  546. /* Try and rise the rqsize */
  547. if (!is_sata(hwif) || !is_dev_seagate_sata(drive))
  548. hwif->rqsize = 128;
  549. }
  550. /**
  551. * init_iops_siimage - set up iops
  552. * @hwif: interface to set up
  553. *
  554. * Do the basic setup for the SIIMAGE hardware interface
  555. * and then do the MMIO setup if we can. This is the first
  556. * look in we get for setting up the hwif so that we
  557. * can get the iops right before using them.
  558. */
  559. static void __devinit init_iops_siimage(ide_hwif_t *hwif)
  560. {
  561. struct pci_dev *dev = to_pci_dev(hwif->dev);
  562. struct ide_host *host = pci_get_drvdata(dev);
  563. hwif->hwif_data = NULL;
  564. /* Pessimal until we finish probing */
  565. hwif->rqsize = 15;
  566. if (host->host_priv)
  567. init_mmio_iops_siimage(hwif);
  568. }
  569. /**
  570. * sil_cable_detect - cable detection
  571. * @hwif: interface to check
  572. *
  573. * Check for the presence of an ATA66 capable cable on the interface.
  574. */
  575. static u8 sil_cable_detect(ide_hwif_t *hwif)
  576. {
  577. struct pci_dev *dev = to_pci_dev(hwif->dev);
  578. unsigned long addr = siimage_selreg(hwif, 0);
  579. u8 ata66 = sil_ioread8(dev, addr);
  580. return (ata66 & 0x01) ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
  581. }
  582. static const struct ide_port_ops sil_pata_port_ops = {
  583. .set_pio_mode = sil_set_pio_mode,
  584. .set_dma_mode = sil_set_dma_mode,
  585. .quirkproc = sil_quirkproc,
  586. .udma_filter = sil_pata_udma_filter,
  587. .cable_detect = sil_cable_detect,
  588. };
  589. static const struct ide_port_ops sil_sata_port_ops = {
  590. .set_pio_mode = sil_set_pio_mode,
  591. .set_dma_mode = sil_set_dma_mode,
  592. .reset_poll = sil_sata_reset_poll,
  593. .pre_reset = sil_sata_pre_reset,
  594. .quirkproc = sil_quirkproc,
  595. .udma_filter = sil_sata_udma_filter,
  596. .cable_detect = sil_cable_detect,
  597. };
  598. static const struct ide_dma_ops sil_dma_ops = {
  599. .dma_host_set = ide_dma_host_set,
  600. .dma_setup = ide_dma_setup,
  601. .dma_exec_cmd = ide_dma_exec_cmd,
  602. .dma_start = ide_dma_start,
  603. .dma_end = ide_dma_end,
  604. .dma_test_irq = siimage_dma_test_irq,
  605. .dma_timeout = ide_dma_timeout,
  606. .dma_lost_irq = ide_dma_lost_irq,
  607. };
  608. #define DECLARE_SII_DEV(p_ops) \
  609. { \
  610. .name = DRV_NAME, \
  611. .init_chipset = init_chipset_siimage, \
  612. .init_iops = init_iops_siimage, \
  613. .port_ops = p_ops, \
  614. .dma_ops = &sil_dma_ops, \
  615. .pio_mask = ATA_PIO4, \
  616. .mwdma_mask = ATA_MWDMA2, \
  617. .udma_mask = ATA_UDMA6, \
  618. }
  619. static const struct ide_port_info siimage_chipsets[] __devinitdata = {
  620. /* 0: SiI680 */ DECLARE_SII_DEV(&sil_pata_port_ops),
  621. /* 1: SiI3112 */ DECLARE_SII_DEV(&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 DRV_NAME ": 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 DRV_NAME " %s: MMIO ports not "
  662. "available\n", pci_name(dev));
  663. } else {
  664. ioaddr = pci_ioremap_bar(dev, 5);
  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), 1 },
  697. #endif
  698. { 0, },
  699. };
  700. MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
  701. static struct pci_driver siimage_pci_driver = {
  702. .name = "SiI_IDE",
  703. .id_table = siimage_pci_tbl,
  704. .probe = siimage_init_one,
  705. .remove = __devexit_p(siimage_remove),
  706. .suspend = ide_pci_suspend,
  707. .resume = ide_pci_resume,
  708. };
  709. static int __init siimage_ide_init(void)
  710. {
  711. return ide_pci_register_driver(&siimage_pci_driver);
  712. }
  713. static void __exit siimage_ide_exit(void)
  714. {
  715. pci_unregister_driver(&siimage_pci_driver);
  716. }
  717. module_init(siimage_ide_init);
  718. module_exit(siimage_ide_exit);
  719. MODULE_AUTHOR("Andre Hedrick, Alan Cox");
  720. MODULE_DESCRIPTION("PCI driver module for SiI IDE");
  721. MODULE_LICENSE("GPL");