siimage.c 28 KB

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