siimage.c 25 KB

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