siimage.c 25 KB

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