siimage.c 27 KB

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