siimage.c 28 KB

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