siimage.c 27 KB

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