siimage.c 25 KB

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