siimage.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * linux/drivers/ide/pci/siimage.c Version 1.16 Jul 13 2007
  3. *
  4. * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
  5. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  6. * Copyright (C) 2007 MontaVista Software, Inc.
  7. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  8. *
  9. * May be copied or modified under the terms of the GNU General Public License
  10. *
  11. * Documentation for CMD680:
  12. * http://gkernel.sourceforge.net/specs/sii/sii-0680a-v1.31.pdf.bz2
  13. *
  14. * Documentation for SiI 3112:
  15. * http://gkernel.sourceforge.net/specs/sii/3112A_SiI-DS-0095-B2.pdf.bz2
  16. *
  17. * Errata and other documentation only available under NDA.
  18. *
  19. *
  20. * FAQ Items:
  21. * If you are using Marvell SATA-IDE adapters with Maxtor drives
  22. * ensure the system is set up for ATA100/UDMA5 not UDMA6.
  23. *
  24. * If you are using WD drives with SATA bridges you must set the
  25. * drive to "Single". "Master" will hang
  26. *
  27. * If you have strange problems with nVidia chipset systems please
  28. * see the SI support documentation and update your system BIOS
  29. * if neccessary
  30. *
  31. * The Dell DRAC4 has some interesting features including effectively hot
  32. * unplugging/replugging the virtual CD interface when the DRAC is reset.
  33. * This often causes drivers/ide/siimage to panic but is ok with the rather
  34. * smarter code in libata.
  35. *
  36. * TODO:
  37. * - IORDY fixes
  38. * - VDMA support
  39. */
  40. #include <linux/types.h>
  41. #include <linux/module.h>
  42. #include <linux/pci.h>
  43. #include <linux/delay.h>
  44. #include <linux/hdreg.h>
  45. #include <linux/ide.h>
  46. #include <linux/init.h>
  47. #include <asm/io.h>
  48. /**
  49. * pdev_is_sata - check if device is SATA
  50. * @pdev: PCI device to check
  51. *
  52. * Returns true if this is a SATA controller
  53. */
  54. static int pdev_is_sata(struct pci_dev *pdev)
  55. {
  56. switch(pdev->device)
  57. {
  58. case PCI_DEVICE_ID_SII_3112:
  59. case PCI_DEVICE_ID_SII_1210SA:
  60. return 1;
  61. case PCI_DEVICE_ID_SII_680:
  62. return 0;
  63. }
  64. BUG();
  65. return 0;
  66. }
  67. /**
  68. * is_sata - check if hwif is SATA
  69. * @hwif: interface to check
  70. *
  71. * Returns true if this is a SATA controller
  72. */
  73. static inline int is_sata(ide_hwif_t *hwif)
  74. {
  75. return pdev_is_sata(hwif->pci_dev);
  76. }
  77. /**
  78. * siimage_selreg - return register base
  79. * @hwif: interface
  80. * @r: config offset
  81. *
  82. * Turn a config register offset into the right address in either
  83. * PCI space or MMIO space to access the control register in question
  84. * Thankfully this is a configuration operation so isnt performance
  85. * criticial.
  86. */
  87. static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
  88. {
  89. unsigned long base = (unsigned long)hwif->hwif_data;
  90. base += 0xA0 + r;
  91. if(hwif->mmio)
  92. base += (hwif->channel << 6);
  93. else
  94. base += (hwif->channel << 4);
  95. return base;
  96. }
  97. /**
  98. * siimage_seldev - return register base
  99. * @hwif: interface
  100. * @r: config offset
  101. *
  102. * Turn a config register offset into the right address in either
  103. * PCI space or MMIO space to access the control register in question
  104. * including accounting for the unit shift.
  105. */
  106. static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
  107. {
  108. ide_hwif_t *hwif = HWIF(drive);
  109. unsigned long base = (unsigned long)hwif->hwif_data;
  110. base += 0xA0 + r;
  111. if(hwif->mmio)
  112. base += (hwif->channel << 6);
  113. else
  114. base += (hwif->channel << 4);
  115. base |= drive->select.b.unit << drive->select.b.unit;
  116. return base;
  117. }
  118. /**
  119. * sil_udma_filter - compute UDMA mask
  120. * @drive: IDE device
  121. *
  122. * Compute the available UDMA speeds for the device on the interface.
  123. *
  124. * For the CMD680 this depends on the clocking mode (scsc), for the
  125. * SI3112 SATA controller life is a bit simpler.
  126. */
  127. static u8 sil_udma_filter(ide_drive_t *drive)
  128. {
  129. ide_hwif_t *hwif = drive->hwif;
  130. unsigned long base = (unsigned long) hwif->hwif_data;
  131. u8 mask = 0, scsc = 0;
  132. if (hwif->mmio)
  133. scsc = hwif->INB(base + 0x4A);
  134. else
  135. pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
  136. if (is_sata(hwif)) {
  137. mask = strstr(drive->id->model, "Maxtor") ? 0x3f : 0x7f;
  138. goto out;
  139. }
  140. if ((scsc & 0x30) == 0x10) /* 133 */
  141. mask = 0x7f;
  142. else if ((scsc & 0x30) == 0x20) /* 2xPCI */
  143. mask = 0x7f;
  144. else if ((scsc & 0x30) == 0x00) /* 100 */
  145. mask = 0x3f;
  146. else /* Disabled ? */
  147. BUG();
  148. out:
  149. return mask;
  150. }
  151. /**
  152. * sil_set_pio_mode - set host controller for PIO mode
  153. * @drive: drive
  154. * @pio: PIO mode number
  155. *
  156. * Load the timing settings for this device mode into the
  157. * controller. If we are in PIO mode 3 or 4 turn on IORDY
  158. * monitoring (bit 9). The TF timing is bits 31:16
  159. */
  160. static void sil_set_pio_mode(ide_drive_t *drive, u8 pio)
  161. {
  162. const u16 tf_speed[] = { 0x328a, 0x2283, 0x1281, 0x10c3, 0x10c1 };
  163. const u16 data_speed[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
  164. ide_hwif_t *hwif = HWIF(drive);
  165. ide_drive_t *pair = &hwif->drives[drive->dn ^ 1];
  166. u32 speedt = 0;
  167. u16 speedp = 0;
  168. unsigned long addr = siimage_seldev(drive, 0x04);
  169. unsigned long tfaddr = siimage_selreg(hwif, 0x02);
  170. unsigned long base = (unsigned long)hwif->hwif_data;
  171. u8 tf_pio = pio;
  172. u8 addr_mask = hwif->channel ? (hwif->mmio ? 0xF4 : 0x84)
  173. : (hwif->mmio ? 0xB4 : 0x80);
  174. u8 mode = 0;
  175. u8 unit = drive->select.b.unit;
  176. /* trim *taskfile* PIO to the slowest of the master/slave */
  177. if (pair->present) {
  178. u8 pair_pio = ide_get_best_pio_mode(pair, 255, 4);
  179. if (pair_pio < tf_pio)
  180. tf_pio = pair_pio;
  181. }
  182. /* cheat for now and use the docs */
  183. speedp = data_speed[pio];
  184. speedt = tf_speed[tf_pio];
  185. if (hwif->mmio) {
  186. hwif->OUTW(speedp, addr);
  187. hwif->OUTW(speedt, tfaddr);
  188. /* Now set up IORDY */
  189. if (pio > 2)
  190. hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
  191. else
  192. hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
  193. mode = hwif->INB(base + addr_mask);
  194. mode &= ~(unit ? 0x30 : 0x03);
  195. mode |= (unit ? 0x10 : 0x01);
  196. hwif->OUTB(mode, base + addr_mask);
  197. } else {
  198. pci_write_config_word(hwif->pci_dev, addr, speedp);
  199. pci_write_config_word(hwif->pci_dev, tfaddr, speedt);
  200. pci_read_config_word(hwif->pci_dev, tfaddr-2, &speedp);
  201. speedp &= ~0x200;
  202. /* Set IORDY for mode 3 or 4 */
  203. if (pio > 2)
  204. speedp |= 0x200;
  205. pci_write_config_word(hwif->pci_dev, tfaddr-2, speedp);
  206. pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
  207. mode &= ~(unit ? 0x30 : 0x03);
  208. mode |= (unit ? 0x10 : 0x01);
  209. pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
  210. }
  211. }
  212. /**
  213. * sil_set_dma_mode - set host controller for DMA mode
  214. * @drive: drive
  215. * @speed: DMA mode
  216. *
  217. * Tune the SiI chipset for the desired DMA mode.
  218. */
  219. static void sil_set_dma_mode(ide_drive_t *drive, const u8 speed)
  220. {
  221. u8 ultra6[] = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
  222. u8 ultra5[] = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
  223. u16 dma[] = { 0x2208, 0x10C2, 0x10C1 };
  224. ide_hwif_t *hwif = HWIF(drive);
  225. u16 ultra = 0, multi = 0;
  226. u8 mode = 0, unit = drive->select.b.unit;
  227. unsigned long base = (unsigned long)hwif->hwif_data;
  228. u8 scsc = 0, addr_mask = ((hwif->channel) ?
  229. ((hwif->mmio) ? 0xF4 : 0x84) :
  230. ((hwif->mmio) ? 0xB4 : 0x80));
  231. unsigned long ma = siimage_seldev(drive, 0x08);
  232. unsigned long ua = siimage_seldev(drive, 0x0C);
  233. if (hwif->mmio) {
  234. scsc = hwif->INB(base + 0x4A);
  235. mode = hwif->INB(base + addr_mask);
  236. multi = hwif->INW(ma);
  237. ultra = hwif->INW(ua);
  238. } else {
  239. pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
  240. pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
  241. pci_read_config_word(hwif->pci_dev, ma, &multi);
  242. pci_read_config_word(hwif->pci_dev, ua, &ultra);
  243. }
  244. mode &= ~((unit) ? 0x30 : 0x03);
  245. ultra &= ~0x3F;
  246. scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
  247. scsc = is_sata(hwif) ? 1 : scsc;
  248. switch(speed) {
  249. case XFER_MW_DMA_2:
  250. case XFER_MW_DMA_1:
  251. case XFER_MW_DMA_0:
  252. multi = dma[speed - XFER_MW_DMA_0];
  253. mode |= ((unit) ? 0x20 : 0x02);
  254. break;
  255. case XFER_UDMA_6:
  256. case XFER_UDMA_5:
  257. case XFER_UDMA_4:
  258. case XFER_UDMA_3:
  259. case XFER_UDMA_2:
  260. case XFER_UDMA_1:
  261. case XFER_UDMA_0:
  262. multi = dma[2];
  263. ultra |= ((scsc) ? (ultra6[speed - XFER_UDMA_0]) :
  264. (ultra5[speed - XFER_UDMA_0]));
  265. mode |= ((unit) ? 0x30 : 0x03);
  266. break;
  267. default:
  268. return;
  269. }
  270. if (hwif->mmio) {
  271. hwif->OUTB(mode, base + addr_mask);
  272. hwif->OUTW(multi, ma);
  273. hwif->OUTW(ultra, ua);
  274. } else {
  275. pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
  276. pci_write_config_word(hwif->pci_dev, ma, multi);
  277. pci_write_config_word(hwif->pci_dev, ua, ultra);
  278. }
  279. }
  280. /**
  281. * siimage_configure_drive_for_dma - set up for DMA transfers
  282. * @drive: drive we are going to set up
  283. *
  284. * Set up the drive for DMA, tune the controller and drive as
  285. * required. If the drive isn't suitable for DMA or we hit
  286. * other problems then we will drop down to PIO and set up
  287. * PIO appropriately
  288. */
  289. static int siimage_config_drive_for_dma (ide_drive_t *drive)
  290. {
  291. if (ide_tune_dma(drive))
  292. return 0;
  293. if (ide_use_fast_pio(drive))
  294. ide_set_max_pio(drive);
  295. return -1;
  296. }
  297. /* returns 1 if dma irq issued, 0 otherwise */
  298. static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
  299. {
  300. ide_hwif_t *hwif = HWIF(drive);
  301. u8 dma_altstat = 0;
  302. unsigned long addr = siimage_selreg(hwif, 1);
  303. /* return 1 if INTR asserted */
  304. if ((hwif->INB(hwif->dma_status) & 4) == 4)
  305. return 1;
  306. /* return 1 if Device INTR asserted */
  307. pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
  308. if (dma_altstat & 8)
  309. return 0; //return 1;
  310. return 0;
  311. }
  312. /**
  313. * siimage_mmio_ide_dma_test_irq - check we caused an IRQ
  314. * @drive: drive we are testing
  315. *
  316. * Check if we caused an IDE DMA interrupt. We may also have caused
  317. * SATA status interrupts, if so we clean them up and continue.
  318. */
  319. static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
  320. {
  321. ide_hwif_t *hwif = HWIF(drive);
  322. unsigned long base = (unsigned long)hwif->hwif_data;
  323. unsigned long addr = siimage_selreg(hwif, 0x1);
  324. if (SATA_ERROR_REG) {
  325. u32 ext_stat = readl((void __iomem *)(base + 0x10));
  326. u8 watchdog = 0;
  327. if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
  328. u32 sata_error = readl((void __iomem *)SATA_ERROR_REG);
  329. writel(sata_error, (void __iomem *)SATA_ERROR_REG);
  330. watchdog = (sata_error & 0x00680000) ? 1 : 0;
  331. printk(KERN_WARNING "%s: sata_error = 0x%08x, "
  332. "watchdog = %d, %s\n",
  333. drive->name, sata_error, watchdog,
  334. __FUNCTION__);
  335. } else {
  336. watchdog = (ext_stat & 0x8000) ? 1 : 0;
  337. }
  338. ext_stat >>= 16;
  339. if (!(ext_stat & 0x0404) && !watchdog)
  340. return 0;
  341. }
  342. /* return 1 if INTR asserted */
  343. if ((readb((void __iomem *)hwif->dma_status) & 0x04) == 0x04)
  344. return 1;
  345. /* return 1 if Device INTR asserted */
  346. if ((readb((void __iomem *)addr) & 8) == 8)
  347. return 0; //return 1;
  348. return 0;
  349. }
  350. /**
  351. * siimage_busproc - bus isolation ioctl
  352. * @drive: drive to isolate/restore
  353. * @state: bus state to set
  354. *
  355. * Used by the SII3112 to handle bus isolation. As this is a
  356. * SATA controller the work required is quite limited, we
  357. * just have to clean up the statistics
  358. */
  359. static int siimage_busproc (ide_drive_t * drive, int state)
  360. {
  361. ide_hwif_t *hwif = HWIF(drive);
  362. u32 stat_config = 0;
  363. unsigned long addr = siimage_selreg(hwif, 0);
  364. if (hwif->mmio)
  365. stat_config = readl((void __iomem *)addr);
  366. else
  367. pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
  368. switch (state) {
  369. case BUSSTATE_ON:
  370. hwif->drives[0].failures = 0;
  371. hwif->drives[1].failures = 0;
  372. break;
  373. case BUSSTATE_OFF:
  374. hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
  375. hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
  376. break;
  377. case BUSSTATE_TRISTATE:
  378. hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
  379. hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. hwif->bus_state = state;
  385. return 0;
  386. }
  387. /**
  388. * siimage_reset_poll - wait for sata reset
  389. * @drive: drive we are resetting
  390. *
  391. * Poll the SATA phy and see whether it has come back from the dead
  392. * yet.
  393. */
  394. static int siimage_reset_poll (ide_drive_t *drive)
  395. {
  396. if (SATA_STATUS_REG) {
  397. ide_hwif_t *hwif = HWIF(drive);
  398. /* SATA_STATUS_REG is valid only when in MMIO mode */
  399. if ((readl((void __iomem *)SATA_STATUS_REG) & 0x03) != 0x03) {
  400. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  401. hwif->name, readl((void __iomem *)SATA_STATUS_REG));
  402. HWGROUP(drive)->polling = 0;
  403. return ide_started;
  404. }
  405. return 0;
  406. } else {
  407. return 0;
  408. }
  409. }
  410. /**
  411. * siimage_pre_reset - reset hook
  412. * @drive: IDE device being reset
  413. *
  414. * For the SATA devices we need to handle recalibration/geometry
  415. * differently
  416. */
  417. static void siimage_pre_reset (ide_drive_t *drive)
  418. {
  419. if (drive->media != ide_disk)
  420. return;
  421. if (is_sata(HWIF(drive)))
  422. {
  423. drive->special.b.set_geometry = 0;
  424. drive->special.b.recalibrate = 0;
  425. }
  426. }
  427. /**
  428. * siimage_reset - reset a device on an siimage controller
  429. * @drive: drive to reset
  430. *
  431. * Perform a controller level reset fo the device. For
  432. * SATA we must also check the PHY.
  433. */
  434. static void siimage_reset (ide_drive_t *drive)
  435. {
  436. ide_hwif_t *hwif = HWIF(drive);
  437. u8 reset = 0;
  438. unsigned long addr = siimage_selreg(hwif, 0);
  439. if (hwif->mmio) {
  440. reset = hwif->INB(addr);
  441. hwif->OUTB((reset|0x03), addr);
  442. /* FIXME:posting */
  443. udelay(25);
  444. hwif->OUTB(reset, addr);
  445. (void) hwif->INB(addr);
  446. } else {
  447. pci_read_config_byte(hwif->pci_dev, addr, &reset);
  448. pci_write_config_byte(hwif->pci_dev, addr, reset|0x03);
  449. udelay(25);
  450. pci_write_config_byte(hwif->pci_dev, addr, reset);
  451. pci_read_config_byte(hwif->pci_dev, addr, &reset);
  452. }
  453. if (SATA_STATUS_REG) {
  454. /* SATA_STATUS_REG is valid only when in MMIO mode */
  455. u32 sata_stat = readl((void __iomem *)SATA_STATUS_REG);
  456. printk(KERN_WARNING "%s: reset phy, status=0x%08x, %s\n",
  457. hwif->name, sata_stat, __FUNCTION__);
  458. if (!(sata_stat)) {
  459. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  460. hwif->name, sata_stat);
  461. drive->failures++;
  462. }
  463. }
  464. }
  465. /**
  466. * proc_reports_siimage - add siimage controller to proc
  467. * @dev: PCI device
  468. * @clocking: SCSC value
  469. * @name: controller name
  470. *
  471. * Report the clocking mode of the controller and add it to
  472. * the /proc interface layer
  473. */
  474. static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
  475. {
  476. if (!pdev_is_sata(dev)) {
  477. printk(KERN_INFO "%s: BASE CLOCK ", name);
  478. clocking &= 0x03;
  479. switch (clocking) {
  480. case 0x03: printk("DISABLED!\n"); break;
  481. case 0x02: printk("== 2X PCI\n"); break;
  482. case 0x01: printk("== 133\n"); break;
  483. case 0x00: printk("== 100\n"); break;
  484. }
  485. }
  486. }
  487. /**
  488. * setup_mmio_siimage - switch an SI controller into MMIO
  489. * @dev: PCI device we are configuring
  490. * @name: device name
  491. *
  492. * Attempt to put the device into mmio mode. There are some slight
  493. * complications here with certain systems where the mmio bar isnt
  494. * mapped so we have to be sure we can fall back to I/O.
  495. */
  496. static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
  497. {
  498. unsigned long bar5 = pci_resource_start(dev, 5);
  499. unsigned long barsize = pci_resource_len(dev, 5);
  500. u8 tmpbyte = 0;
  501. void __iomem *ioaddr;
  502. u32 tmp, irq_mask;
  503. /*
  504. * Drop back to PIO if we can't map the mmio. Some
  505. * systems seem to get terminally confused in the PCI
  506. * spaces.
  507. */
  508. if(!request_mem_region(bar5, barsize, name))
  509. {
  510. printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
  511. return 0;
  512. }
  513. ioaddr = ioremap(bar5, barsize);
  514. if (ioaddr == NULL)
  515. {
  516. release_mem_region(bar5, barsize);
  517. return 0;
  518. }
  519. pci_set_master(dev);
  520. pci_set_drvdata(dev, (void *) ioaddr);
  521. if (pdev_is_sata(dev)) {
  522. /* make sure IDE0/1 interrupts are not masked */
  523. irq_mask = (1 << 22) | (1 << 23);
  524. tmp = readl(ioaddr + 0x48);
  525. if (tmp & irq_mask) {
  526. tmp &= ~irq_mask;
  527. writel(tmp, ioaddr + 0x48);
  528. readl(ioaddr + 0x48); /* flush */
  529. }
  530. writel(0, ioaddr + 0x148);
  531. writel(0, ioaddr + 0x1C8);
  532. }
  533. writeb(0, ioaddr + 0xB4);
  534. writeb(0, ioaddr + 0xF4);
  535. tmpbyte = readb(ioaddr + 0x4A);
  536. switch(tmpbyte & 0x30) {
  537. case 0x00:
  538. /* In 100 MHz clocking, try and switch to 133 */
  539. writeb(tmpbyte|0x10, ioaddr + 0x4A);
  540. break;
  541. case 0x10:
  542. /* On 133Mhz clocking */
  543. break;
  544. case 0x20:
  545. /* On PCIx2 clocking */
  546. break;
  547. case 0x30:
  548. /* Clocking is disabled */
  549. /* 133 clock attempt to force it on */
  550. writeb(tmpbyte & ~0x20, ioaddr + 0x4A);
  551. break;
  552. }
  553. writeb( 0x72, ioaddr + 0xA1);
  554. writew( 0x328A, ioaddr + 0xA2);
  555. writel(0x62DD62DD, ioaddr + 0xA4);
  556. writel(0x43924392, ioaddr + 0xA8);
  557. writel(0x40094009, ioaddr + 0xAC);
  558. writeb( 0x72, ioaddr + 0xE1);
  559. writew( 0x328A, ioaddr + 0xE2);
  560. writel(0x62DD62DD, ioaddr + 0xE4);
  561. writel(0x43924392, ioaddr + 0xE8);
  562. writel(0x40094009, ioaddr + 0xEC);
  563. if (pdev_is_sata(dev)) {
  564. writel(0xFFFF0000, ioaddr + 0x108);
  565. writel(0xFFFF0000, ioaddr + 0x188);
  566. writel(0x00680000, ioaddr + 0x148);
  567. writel(0x00680000, ioaddr + 0x1C8);
  568. }
  569. tmpbyte = readb(ioaddr + 0x4A);
  570. proc_reports_siimage(dev, (tmpbyte>>4), name);
  571. return 1;
  572. }
  573. /**
  574. * init_chipset_siimage - set up an SI device
  575. * @dev: PCI device
  576. * @name: device name
  577. *
  578. * Perform the initial PCI set up for this device. Attempt to switch
  579. * to 133MHz clocking if the system isn't already set up to do it.
  580. */
  581. static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev, const char *name)
  582. {
  583. u32 class_rev = 0;
  584. u8 tmpbyte = 0;
  585. u8 BA5_EN = 0;
  586. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  587. class_rev &= 0xff;
  588. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
  589. pci_read_config_byte(dev, 0x8A, &BA5_EN);
  590. if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
  591. if (setup_mmio_siimage(dev, name)) {
  592. return 0;
  593. }
  594. }
  595. pci_write_config_byte(dev, 0x80, 0x00);
  596. pci_write_config_byte(dev, 0x84, 0x00);
  597. pci_read_config_byte(dev, 0x8A, &tmpbyte);
  598. switch(tmpbyte & 0x30) {
  599. case 0x00:
  600. /* 133 clock attempt to force it on */
  601. pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
  602. case 0x30:
  603. /* if clocking is disabled */
  604. /* 133 clock attempt to force it on */
  605. pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
  606. case 0x10:
  607. /* 133 already */
  608. break;
  609. case 0x20:
  610. /* BIOS set PCI x2 clocking */
  611. break;
  612. }
  613. pci_read_config_byte(dev, 0x8A, &tmpbyte);
  614. pci_write_config_byte(dev, 0xA1, 0x72);
  615. pci_write_config_word(dev, 0xA2, 0x328A);
  616. pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
  617. pci_write_config_dword(dev, 0xA8, 0x43924392);
  618. pci_write_config_dword(dev, 0xAC, 0x40094009);
  619. pci_write_config_byte(dev, 0xB1, 0x72);
  620. pci_write_config_word(dev, 0xB2, 0x328A);
  621. pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
  622. pci_write_config_dword(dev, 0xB8, 0x43924392);
  623. pci_write_config_dword(dev, 0xBC, 0x40094009);
  624. proc_reports_siimage(dev, (tmpbyte>>4), name);
  625. return 0;
  626. }
  627. /**
  628. * init_mmio_iops_siimage - set up the iops for MMIO
  629. * @hwif: interface to set up
  630. *
  631. * The basic setup here is fairly simple, we can use standard MMIO
  632. * operations. However we do have to set the taskfile register offsets
  633. * by hand as there isnt a standard defined layout for them this
  634. * time.
  635. *
  636. * The hardware supports buffered taskfiles and also some rather nice
  637. * extended PRD tables. For better SI3112 support use the libata driver
  638. */
  639. static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
  640. {
  641. struct pci_dev *dev = hwif->pci_dev;
  642. void *addr = pci_get_drvdata(dev);
  643. u8 ch = hwif->channel;
  644. hw_regs_t hw;
  645. unsigned long base;
  646. /*
  647. * Fill in the basic HWIF bits
  648. */
  649. default_hwif_mmiops(hwif);
  650. hwif->hwif_data = addr;
  651. /*
  652. * Now set up the hw. We have to do this ourselves as
  653. * the MMIO layout isnt the same as the standard port
  654. * based I/O
  655. */
  656. memset(&hw, 0, sizeof(hw_regs_t));
  657. base = (unsigned long)addr;
  658. if (ch)
  659. base += 0xC0;
  660. else
  661. base += 0x80;
  662. /*
  663. * The buffered task file doesn't have status/control
  664. * so we can't currently use it sanely since we want to
  665. * use LBA48 mode.
  666. */
  667. hw.io_ports[IDE_DATA_OFFSET] = base;
  668. hw.io_ports[IDE_ERROR_OFFSET] = base + 1;
  669. hw.io_ports[IDE_NSECTOR_OFFSET] = base + 2;
  670. hw.io_ports[IDE_SECTOR_OFFSET] = base + 3;
  671. hw.io_ports[IDE_LCYL_OFFSET] = base + 4;
  672. hw.io_ports[IDE_HCYL_OFFSET] = base + 5;
  673. hw.io_ports[IDE_SELECT_OFFSET] = base + 6;
  674. hw.io_ports[IDE_STATUS_OFFSET] = base + 7;
  675. hw.io_ports[IDE_CONTROL_OFFSET] = base + 10;
  676. hw.io_ports[IDE_IRQ_OFFSET] = 0;
  677. if (pdev_is_sata(dev)) {
  678. base = (unsigned long)addr;
  679. if (ch)
  680. base += 0x80;
  681. hwif->sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
  682. hwif->sata_scr[SATA_ERROR_OFFSET] = base + 0x108;
  683. hwif->sata_scr[SATA_CONTROL_OFFSET] = base + 0x100;
  684. hwif->sata_misc[SATA_MISC_OFFSET] = base + 0x140;
  685. hwif->sata_misc[SATA_PHY_OFFSET] = base + 0x144;
  686. hwif->sata_misc[SATA_IEN_OFFSET] = base + 0x148;
  687. }
  688. hw.irq = hwif->pci_dev->irq;
  689. memcpy(&hwif->hw, &hw, sizeof(hw));
  690. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
  691. hwif->irq = hw.irq;
  692. base = (unsigned long) addr;
  693. hwif->dma_base = base + (ch ? 0x08 : 0x00);
  694. hwif->mmio = 1;
  695. }
  696. static int is_dev_seagate_sata(ide_drive_t *drive)
  697. {
  698. const char *s = &drive->id->model[0];
  699. unsigned len;
  700. if (!drive->present)
  701. return 0;
  702. len = strnlen(s, sizeof(drive->id->model));
  703. if ((len > 4) && (!memcmp(s, "ST", 2))) {
  704. if ((!memcmp(s + len - 2, "AS", 2)) ||
  705. (!memcmp(s + len - 3, "ASL", 3))) {
  706. printk(KERN_INFO "%s: applying pessimistic Seagate "
  707. "errata fix\n", drive->name);
  708. return 1;
  709. }
  710. }
  711. return 0;
  712. }
  713. /**
  714. * siimage_fixup - post probe fixups
  715. * @hwif: interface to fix up
  716. *
  717. * Called after drive probe we use this to decide whether the
  718. * Seagate fixup must be applied. This used to be in init_iops but
  719. * that can occur before we know what drives are present.
  720. */
  721. static void __devinit siimage_fixup(ide_hwif_t *hwif)
  722. {
  723. /* Try and raise the rqsize */
  724. if (!is_sata(hwif) || !is_dev_seagate_sata(&hwif->drives[0]))
  725. hwif->rqsize = 128;
  726. }
  727. /**
  728. * init_iops_siimage - set up iops
  729. * @hwif: interface to set up
  730. *
  731. * Do the basic setup for the SIIMAGE hardware interface
  732. * and then do the MMIO setup if we can. This is the first
  733. * look in we get for setting up the hwif so that we
  734. * can get the iops right before using them.
  735. */
  736. static void __devinit init_iops_siimage(ide_hwif_t *hwif)
  737. {
  738. struct pci_dev *dev = hwif->pci_dev;
  739. u32 class_rev = 0;
  740. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  741. class_rev &= 0xff;
  742. hwif->hwif_data = NULL;
  743. /* Pessimal until we finish probing */
  744. hwif->rqsize = 15;
  745. if (pci_get_drvdata(dev) == NULL)
  746. return;
  747. init_mmio_iops_siimage(hwif);
  748. }
  749. /**
  750. * ata66_siimage - check for 80 pin cable
  751. * @hwif: interface to check
  752. *
  753. * Check for the presence of an ATA66 capable cable on the
  754. * interface.
  755. */
  756. static u8 __devinit ata66_siimage(ide_hwif_t *hwif)
  757. {
  758. unsigned long addr = siimage_selreg(hwif, 0);
  759. u8 ata66 = 0;
  760. if (pci_get_drvdata(hwif->pci_dev) == NULL)
  761. pci_read_config_byte(hwif->pci_dev, addr, &ata66);
  762. else
  763. ata66 = hwif->INB(addr);
  764. return (ata66 & 0x01) ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
  765. }
  766. /**
  767. * init_hwif_siimage - set up hwif structs
  768. * @hwif: interface to set up
  769. *
  770. * We do the basic set up of the interface structure. The SIIMAGE
  771. * requires several custom handlers so we override the default
  772. * ide DMA handlers appropriately
  773. */
  774. static void __devinit init_hwif_siimage(ide_hwif_t *hwif)
  775. {
  776. hwif->autodma = 0;
  777. hwif->resetproc = &siimage_reset;
  778. hwif->set_pio_mode = &sil_set_pio_mode;
  779. hwif->set_dma_mode = &sil_set_dma_mode;
  780. hwif->reset_poll = &siimage_reset_poll;
  781. hwif->pre_reset = &siimage_pre_reset;
  782. hwif->udma_filter = &sil_udma_filter;
  783. if(is_sata(hwif)) {
  784. static int first = 1;
  785. hwif->busproc = &siimage_busproc;
  786. if (first) {
  787. printk(KERN_INFO "siimage: For full SATA support you should use the libata sata_sil module.\n");
  788. first = 0;
  789. }
  790. }
  791. hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
  792. if (hwif->dma_base == 0)
  793. return;
  794. hwif->ultra_mask = 0x7f;
  795. hwif->mwdma_mask = 0x07;
  796. if (!is_sata(hwif))
  797. hwif->atapi_dma = 1;
  798. hwif->ide_dma_check = &siimage_config_drive_for_dma;
  799. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  800. hwif->cbl = ata66_siimage(hwif);
  801. if (hwif->mmio) {
  802. hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
  803. } else {
  804. hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
  805. }
  806. /*
  807. * The BIOS often doesn't set up DMA on this controller
  808. * so we always do it.
  809. */
  810. hwif->autodma = 1;
  811. hwif->drives[0].autodma = hwif->autodma;
  812. hwif->drives[1].autodma = hwif->autodma;
  813. }
  814. #define DECLARE_SII_DEV(name_str) \
  815. { \
  816. .name = name_str, \
  817. .init_chipset = init_chipset_siimage, \
  818. .init_iops = init_iops_siimage, \
  819. .init_hwif = init_hwif_siimage, \
  820. .fixup = siimage_fixup, \
  821. .autodma = AUTODMA, \
  822. .bootable = ON_BOARD, \
  823. .pio_mask = ATA_PIO4, \
  824. }
  825. static ide_pci_device_t siimage_chipsets[] __devinitdata = {
  826. /* 0 */ DECLARE_SII_DEV("SiI680"),
  827. /* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA"),
  828. /* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA")
  829. };
  830. /**
  831. * siimage_init_one - pci layer discovery entry
  832. * @dev: PCI device
  833. * @id: ident table entry
  834. *
  835. * Called by the PCI code when it finds an SI680 or SI3112 controller.
  836. * We then use the IDE PCI generic helper to do most of the work.
  837. */
  838. static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  839. {
  840. return ide_setup_pci_device(dev, &siimage_chipsets[id->driver_data]);
  841. }
  842. static struct pci_device_id siimage_pci_tbl[] = {
  843. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  844. #ifdef CONFIG_BLK_DEV_IDE_SATA
  845. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  846. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_1210SA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  847. #endif
  848. { 0, },
  849. };
  850. MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
  851. static struct pci_driver driver = {
  852. .name = "SiI_IDE",
  853. .id_table = siimage_pci_tbl,
  854. .probe = siimage_init_one,
  855. };
  856. static int __init siimage_ide_init(void)
  857. {
  858. return ide_pci_register_driver(&driver);
  859. }
  860. module_init(siimage_ide_init);
  861. MODULE_AUTHOR("Andre Hedrick, Alan Cox");
  862. MODULE_DESCRIPTION("PCI driver module for SiI IDE");
  863. MODULE_LICENSE("GPL");