siimage.c 27 KB

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