siimage.c 21 KB

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