siimage.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * linux/drivers/ide/pci/siimage.c Version 1.11 Jan 27, 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. * siimage_ratemask - Compute available modes
  115. * @drive: IDE drive
  116. *
  117. * Compute the available speeds for the devices on the interface.
  118. * For the CMD680 this depends on the clocking mode (scsc), for the
  119. * SI3312 SATA controller life is a bit simpler. Enforce UDMA33
  120. * as a limit if there is no 80pin cable present.
  121. */
  122. static byte siimage_ratemask (ide_drive_t *drive)
  123. {
  124. ide_hwif_t *hwif = HWIF(drive);
  125. u8 mode = 0, scsc = 0;
  126. unsigned long base = (unsigned long) hwif->hwif_data;
  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. {
  133. if(strstr(drive->id->model, "Maxtor"))
  134. return 3;
  135. return 4;
  136. }
  137. if ((scsc & 0x30) == 0x10) /* 133 */
  138. mode = 4;
  139. else if ((scsc & 0x30) == 0x20) /* 2xPCI */
  140. mode = 4;
  141. else if ((scsc & 0x30) == 0x00) /* 100 */
  142. mode = 3;
  143. else /* Disabled ? */
  144. BUG();
  145. if (!eighty_ninty_three(drive))
  146. mode = min(mode, (u8)1);
  147. return mode;
  148. }
  149. /**
  150. * siimage_taskfile_timing - turn timing data to a mode
  151. * @hwif: interface to query
  152. *
  153. * Read the timing data for the interface and return the
  154. * mode that is being used.
  155. */
  156. static byte siimage_taskfile_timing (ide_hwif_t *hwif)
  157. {
  158. u16 timing = 0x328a;
  159. unsigned long addr = siimage_selreg(hwif, 2);
  160. if (hwif->mmio)
  161. timing = hwif->INW(addr);
  162. else
  163. pci_read_config_word(hwif->pci_dev, addr, &timing);
  164. switch (timing) {
  165. case 0x10c1: return 4;
  166. case 0x10c3: return 3;
  167. case 0x1104:
  168. case 0x1281: return 2;
  169. case 0x2283: return 1;
  170. case 0x328a:
  171. default: return 0;
  172. }
  173. }
  174. /**
  175. * simmage_tuneproc - tune a drive
  176. * @drive: drive to tune
  177. * @mode_wanted: the target operating mode
  178. *
  179. * Load the timing settings for this device mode into the
  180. * controller. If we are in PIO mode 3 or 4 turn on IORDY
  181. * monitoring (bit 9). The TF timing is bits 31:16
  182. */
  183. static void siimage_tuneproc (ide_drive_t *drive, byte mode_wanted)
  184. {
  185. ide_hwif_t *hwif = HWIF(drive);
  186. u32 speedt = 0;
  187. u16 speedp = 0;
  188. unsigned long addr = siimage_seldev(drive, 0x04);
  189. unsigned long tfaddr = siimage_selreg(hwif, 0x02);
  190. /* cheat for now and use the docs */
  191. switch (mode_wanted) {
  192. case 4:
  193. speedp = 0x10c1;
  194. speedt = 0x10c1;
  195. break;
  196. case 3:
  197. speedp = 0x10c3;
  198. speedt = 0x10c3;
  199. break;
  200. case 2:
  201. speedp = 0x1104;
  202. speedt = 0x1281;
  203. break;
  204. case 1:
  205. speedp = 0x2283;
  206. speedt = 0x2283;
  207. break;
  208. case 0:
  209. default:
  210. speedp = 0x328a;
  211. speedt = 0x328a;
  212. break;
  213. }
  214. if (hwif->mmio) {
  215. hwif->OUTW(speedp, addr);
  216. hwif->OUTW(speedt, tfaddr);
  217. /* Now set up IORDY */
  218. if(mode_wanted == 3 || mode_wanted == 4)
  219. hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
  220. else
  221. hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
  222. } else {
  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 (siimage_tune_chipset(drive, speed))
  362. return 0;
  363. return ide_dma_enable(drive);
  364. }
  365. /**
  366. * siimage_configure_drive_for_dma - set up for DMA transfers
  367. * @drive: drive we are going to set up
  368. *
  369. * Set up the drive for DMA, tune the controller and drive as
  370. * required. If the drive isn't suitable for DMA or we hit
  371. * other problems then we will drop down to PIO and set up
  372. * PIO appropriately
  373. */
  374. static int siimage_config_drive_for_dma (ide_drive_t *drive)
  375. {
  376. if (ide_use_dma(drive) && config_chipset_for_dma(drive))
  377. return 0;
  378. if (ide_use_fast_pio(drive))
  379. config_chipset_for_pio(drive, 1);
  380. return -1;
  381. }
  382. /* returns 1 if dma irq issued, 0 otherwise */
  383. static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
  384. {
  385. ide_hwif_t *hwif = HWIF(drive);
  386. u8 dma_altstat = 0;
  387. unsigned long addr = siimage_selreg(hwif, 1);
  388. /* return 1 if INTR asserted */
  389. if ((hwif->INB(hwif->dma_status) & 4) == 4)
  390. return 1;
  391. /* return 1 if Device INTR asserted */
  392. pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
  393. if (dma_altstat & 8)
  394. return 0; //return 1;
  395. return 0;
  396. }
  397. /**
  398. * siimage_mmio_ide_dma_test_irq - check we caused an IRQ
  399. * @drive: drive we are testing
  400. *
  401. * Check if we caused an IDE DMA interrupt. We may also have caused
  402. * SATA status interrupts, if so we clean them up and continue.
  403. */
  404. static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
  405. {
  406. ide_hwif_t *hwif = HWIF(drive);
  407. unsigned long base = (unsigned long)hwif->hwif_data;
  408. unsigned long addr = siimage_selreg(hwif, 0x1);
  409. if (SATA_ERROR_REG) {
  410. u32 ext_stat = readl((void __iomem *)(base + 0x10));
  411. u8 watchdog = 0;
  412. if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
  413. u32 sata_error = readl((void __iomem *)SATA_ERROR_REG);
  414. writel(sata_error, (void __iomem *)SATA_ERROR_REG);
  415. watchdog = (sata_error & 0x00680000) ? 1 : 0;
  416. printk(KERN_WARNING "%s: sata_error = 0x%08x, "
  417. "watchdog = %d, %s\n",
  418. drive->name, sata_error, watchdog,
  419. __FUNCTION__);
  420. } else {
  421. watchdog = (ext_stat & 0x8000) ? 1 : 0;
  422. }
  423. ext_stat >>= 16;
  424. if (!(ext_stat & 0x0404) && !watchdog)
  425. return 0;
  426. }
  427. /* return 1 if INTR asserted */
  428. if ((readb((void __iomem *)hwif->dma_status) & 0x04) == 0x04)
  429. return 1;
  430. /* return 1 if Device INTR asserted */
  431. if ((readb((void __iomem *)addr) & 8) == 8)
  432. return 0; //return 1;
  433. return 0;
  434. }
  435. /**
  436. * siimage_busproc - bus isolation ioctl
  437. * @drive: drive to isolate/restore
  438. * @state: bus state to set
  439. *
  440. * Used by the SII3112 to handle bus isolation. As this is a
  441. * SATA controller the work required is quite limited, we
  442. * just have to clean up the statistics
  443. */
  444. static int siimage_busproc (ide_drive_t * drive, int state)
  445. {
  446. ide_hwif_t *hwif = HWIF(drive);
  447. u32 stat_config = 0;
  448. unsigned long addr = siimage_selreg(hwif, 0);
  449. if (hwif->mmio)
  450. stat_config = readl((void __iomem *)addr);
  451. else
  452. pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
  453. switch (state) {
  454. case BUSSTATE_ON:
  455. hwif->drives[0].failures = 0;
  456. hwif->drives[1].failures = 0;
  457. break;
  458. case BUSSTATE_OFF:
  459. hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
  460. hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
  461. break;
  462. case BUSSTATE_TRISTATE:
  463. hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
  464. hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
  465. break;
  466. default:
  467. return -EINVAL;
  468. }
  469. hwif->bus_state = state;
  470. return 0;
  471. }
  472. /**
  473. * siimage_reset_poll - wait for sata reset
  474. * @drive: drive we are resetting
  475. *
  476. * Poll the SATA phy and see whether it has come back from the dead
  477. * yet.
  478. */
  479. static int siimage_reset_poll (ide_drive_t *drive)
  480. {
  481. if (SATA_STATUS_REG) {
  482. ide_hwif_t *hwif = HWIF(drive);
  483. /* SATA_STATUS_REG is valid only when in MMIO mode */
  484. if ((readl((void __iomem *)SATA_STATUS_REG) & 0x03) != 0x03) {
  485. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  486. hwif->name, readl((void __iomem *)SATA_STATUS_REG));
  487. HWGROUP(drive)->polling = 0;
  488. return ide_started;
  489. }
  490. return 0;
  491. } else {
  492. return 0;
  493. }
  494. }
  495. /**
  496. * siimage_pre_reset - reset hook
  497. * @drive: IDE device being reset
  498. *
  499. * For the SATA devices we need to handle recalibration/geometry
  500. * differently
  501. */
  502. static void siimage_pre_reset (ide_drive_t *drive)
  503. {
  504. if (drive->media != ide_disk)
  505. return;
  506. if (is_sata(HWIF(drive)))
  507. {
  508. drive->special.b.set_geometry = 0;
  509. drive->special.b.recalibrate = 0;
  510. }
  511. }
  512. /**
  513. * siimage_reset - reset a device on an siimage controller
  514. * @drive: drive to reset
  515. *
  516. * Perform a controller level reset fo the device. For
  517. * SATA we must also check the PHY.
  518. */
  519. static void siimage_reset (ide_drive_t *drive)
  520. {
  521. ide_hwif_t *hwif = HWIF(drive);
  522. u8 reset = 0;
  523. unsigned long addr = siimage_selreg(hwif, 0);
  524. if (hwif->mmio) {
  525. reset = hwif->INB(addr);
  526. hwif->OUTB((reset|0x03), addr);
  527. /* FIXME:posting */
  528. udelay(25);
  529. hwif->OUTB(reset, addr);
  530. (void) hwif->INB(addr);
  531. } else {
  532. pci_read_config_byte(hwif->pci_dev, addr, &reset);
  533. pci_write_config_byte(hwif->pci_dev, addr, reset|0x03);
  534. udelay(25);
  535. pci_write_config_byte(hwif->pci_dev, addr, reset);
  536. pci_read_config_byte(hwif->pci_dev, addr, &reset);
  537. }
  538. if (SATA_STATUS_REG) {
  539. /* SATA_STATUS_REG is valid only when in MMIO mode */
  540. u32 sata_stat = readl((void __iomem *)SATA_STATUS_REG);
  541. printk(KERN_WARNING "%s: reset phy, status=0x%08x, %s\n",
  542. hwif->name, sata_stat, __FUNCTION__);
  543. if (!(sata_stat)) {
  544. printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
  545. hwif->name, sata_stat);
  546. drive->failures++;
  547. }
  548. }
  549. }
  550. /**
  551. * proc_reports_siimage - add siimage controller to proc
  552. * @dev: PCI device
  553. * @clocking: SCSC value
  554. * @name: controller name
  555. *
  556. * Report the clocking mode of the controller and add it to
  557. * the /proc interface layer
  558. */
  559. static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
  560. {
  561. if (!pdev_is_sata(dev)) {
  562. printk(KERN_INFO "%s: BASE CLOCK ", name);
  563. clocking &= 0x03;
  564. switch (clocking) {
  565. case 0x03: printk("DISABLED!\n"); break;
  566. case 0x02: printk("== 2X PCI\n"); break;
  567. case 0x01: printk("== 133\n"); break;
  568. case 0x00: printk("== 100\n"); break;
  569. }
  570. }
  571. }
  572. /**
  573. * setup_mmio_siimage - switch an SI controller into MMIO
  574. * @dev: PCI device we are configuring
  575. * @name: device name
  576. *
  577. * Attempt to put the device into mmio mode. There are some slight
  578. * complications here with certain systems where the mmio bar isnt
  579. * mapped so we have to be sure we can fall back to I/O.
  580. */
  581. static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
  582. {
  583. unsigned long bar5 = pci_resource_start(dev, 5);
  584. unsigned long barsize = pci_resource_len(dev, 5);
  585. u8 tmpbyte = 0;
  586. void __iomem *ioaddr;
  587. u32 tmp, irq_mask;
  588. /*
  589. * Drop back to PIO if we can't map the mmio. Some
  590. * systems seem to get terminally confused in the PCI
  591. * spaces.
  592. */
  593. if(!request_mem_region(bar5, barsize, name))
  594. {
  595. printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
  596. return 0;
  597. }
  598. ioaddr = ioremap(bar5, barsize);
  599. if (ioaddr == NULL)
  600. {
  601. release_mem_region(bar5, barsize);
  602. return 0;
  603. }
  604. pci_set_master(dev);
  605. pci_set_drvdata(dev, (void *) ioaddr);
  606. if (pdev_is_sata(dev)) {
  607. /* make sure IDE0/1 interrupts are not masked */
  608. irq_mask = (1 << 22) | (1 << 23);
  609. tmp = readl(ioaddr + 0x48);
  610. if (tmp & irq_mask) {
  611. tmp &= ~irq_mask;
  612. writel(tmp, ioaddr + 0x48);
  613. readl(ioaddr + 0x48); /* flush */
  614. }
  615. writel(0, ioaddr + 0x148);
  616. writel(0, ioaddr + 0x1C8);
  617. }
  618. writeb(0, ioaddr + 0xB4);
  619. writeb(0, ioaddr + 0xF4);
  620. tmpbyte = readb(ioaddr + 0x4A);
  621. switch(tmpbyte & 0x30) {
  622. case 0x00:
  623. /* In 100 MHz clocking, try and switch to 133 */
  624. writeb(tmpbyte|0x10, ioaddr + 0x4A);
  625. break;
  626. case 0x10:
  627. /* On 133Mhz clocking */
  628. break;
  629. case 0x20:
  630. /* On PCIx2 clocking */
  631. break;
  632. case 0x30:
  633. /* Clocking is disabled */
  634. /* 133 clock attempt to force it on */
  635. writeb(tmpbyte & ~0x20, ioaddr + 0x4A);
  636. break;
  637. }
  638. writeb( 0x72, ioaddr + 0xA1);
  639. writew( 0x328A, ioaddr + 0xA2);
  640. writel(0x62DD62DD, ioaddr + 0xA4);
  641. writel(0x43924392, ioaddr + 0xA8);
  642. writel(0x40094009, ioaddr + 0xAC);
  643. writeb( 0x72, ioaddr + 0xE1);
  644. writew( 0x328A, ioaddr + 0xE2);
  645. writel(0x62DD62DD, ioaddr + 0xE4);
  646. writel(0x43924392, ioaddr + 0xE8);
  647. writel(0x40094009, ioaddr + 0xEC);
  648. if (pdev_is_sata(dev)) {
  649. writel(0xFFFF0000, ioaddr + 0x108);
  650. writel(0xFFFF0000, ioaddr + 0x188);
  651. writel(0x00680000, ioaddr + 0x148);
  652. writel(0x00680000, ioaddr + 0x1C8);
  653. }
  654. tmpbyte = readb(ioaddr + 0x4A);
  655. proc_reports_siimage(dev, (tmpbyte>>4), name);
  656. return 1;
  657. }
  658. /**
  659. * init_chipset_siimage - set up an SI device
  660. * @dev: PCI device
  661. * @name: device name
  662. *
  663. * Perform the initial PCI set up for this device. Attempt to switch
  664. * to 133MHz clocking if the system isn't already set up to do it.
  665. */
  666. static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev, const char *name)
  667. {
  668. u32 class_rev = 0;
  669. u8 tmpbyte = 0;
  670. u8 BA5_EN = 0;
  671. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  672. class_rev &= 0xff;
  673. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
  674. pci_read_config_byte(dev, 0x8A, &BA5_EN);
  675. if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
  676. if (setup_mmio_siimage(dev, name)) {
  677. return 0;
  678. }
  679. }
  680. pci_write_config_byte(dev, 0x80, 0x00);
  681. pci_write_config_byte(dev, 0x84, 0x00);
  682. pci_read_config_byte(dev, 0x8A, &tmpbyte);
  683. switch(tmpbyte & 0x30) {
  684. case 0x00:
  685. /* 133 clock attempt to force it on */
  686. pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
  687. case 0x30:
  688. /* if clocking is disabled */
  689. /* 133 clock attempt to force it on */
  690. pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
  691. case 0x10:
  692. /* 133 already */
  693. break;
  694. case 0x20:
  695. /* BIOS set PCI x2 clocking */
  696. break;
  697. }
  698. pci_read_config_byte(dev, 0x8A, &tmpbyte);
  699. pci_write_config_byte(dev, 0xA1, 0x72);
  700. pci_write_config_word(dev, 0xA2, 0x328A);
  701. pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
  702. pci_write_config_dword(dev, 0xA8, 0x43924392);
  703. pci_write_config_dword(dev, 0xAC, 0x40094009);
  704. pci_write_config_byte(dev, 0xB1, 0x72);
  705. pci_write_config_word(dev, 0xB2, 0x328A);
  706. pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
  707. pci_write_config_dword(dev, 0xB8, 0x43924392);
  708. pci_write_config_dword(dev, 0xBC, 0x40094009);
  709. proc_reports_siimage(dev, (tmpbyte>>4), name);
  710. return 0;
  711. }
  712. /**
  713. * init_mmio_iops_siimage - set up the iops for MMIO
  714. * @hwif: interface to set up
  715. *
  716. * The basic setup here is fairly simple, we can use standard MMIO
  717. * operations. However we do have to set the taskfile register offsets
  718. * by hand as there isnt a standard defined layout for them this
  719. * time.
  720. *
  721. * The hardware supports buffered taskfiles and also some rather nice
  722. * extended PRD tables. For better SI3112 support use the libata driver
  723. */
  724. static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
  725. {
  726. struct pci_dev *dev = hwif->pci_dev;
  727. void *addr = pci_get_drvdata(dev);
  728. u8 ch = hwif->channel;
  729. hw_regs_t hw;
  730. unsigned long base;
  731. /*
  732. * Fill in the basic HWIF bits
  733. */
  734. default_hwif_mmiops(hwif);
  735. hwif->hwif_data = addr;
  736. /*
  737. * Now set up the hw. We have to do this ourselves as
  738. * the MMIO layout isnt the same as the the standard port
  739. * based I/O
  740. */
  741. memset(&hw, 0, sizeof(hw_regs_t));
  742. base = (unsigned long)addr;
  743. if (ch)
  744. base += 0xC0;
  745. else
  746. base += 0x80;
  747. /*
  748. * The buffered task file doesn't have status/control
  749. * so we can't currently use it sanely since we want to
  750. * use LBA48 mode.
  751. */
  752. hw.io_ports[IDE_DATA_OFFSET] = base;
  753. hw.io_ports[IDE_ERROR_OFFSET] = base + 1;
  754. hw.io_ports[IDE_NSECTOR_OFFSET] = base + 2;
  755. hw.io_ports[IDE_SECTOR_OFFSET] = base + 3;
  756. hw.io_ports[IDE_LCYL_OFFSET] = base + 4;
  757. hw.io_ports[IDE_HCYL_OFFSET] = base + 5;
  758. hw.io_ports[IDE_SELECT_OFFSET] = base + 6;
  759. hw.io_ports[IDE_STATUS_OFFSET] = base + 7;
  760. hw.io_ports[IDE_CONTROL_OFFSET] = base + 10;
  761. hw.io_ports[IDE_IRQ_OFFSET] = 0;
  762. if (pdev_is_sata(dev)) {
  763. base = (unsigned long)addr;
  764. if (ch)
  765. base += 0x80;
  766. hwif->sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
  767. hwif->sata_scr[SATA_ERROR_OFFSET] = base + 0x108;
  768. hwif->sata_scr[SATA_CONTROL_OFFSET] = base + 0x100;
  769. hwif->sata_misc[SATA_MISC_OFFSET] = base + 0x140;
  770. hwif->sata_misc[SATA_PHY_OFFSET] = base + 0x144;
  771. hwif->sata_misc[SATA_IEN_OFFSET] = base + 0x148;
  772. }
  773. hw.irq = hwif->pci_dev->irq;
  774. memcpy(&hwif->hw, &hw, sizeof(hw));
  775. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
  776. hwif->irq = hw.irq;
  777. base = (unsigned long) addr;
  778. hwif->dma_base = base + (ch ? 0x08 : 0x00);
  779. hwif->mmio = 1;
  780. }
  781. static int is_dev_seagate_sata(ide_drive_t *drive)
  782. {
  783. const char *s = &drive->id->model[0];
  784. unsigned len;
  785. if (!drive->present)
  786. return 0;
  787. len = strnlen(s, sizeof(drive->id->model));
  788. if ((len > 4) && (!memcmp(s, "ST", 2))) {
  789. if ((!memcmp(s + len - 2, "AS", 2)) ||
  790. (!memcmp(s + len - 3, "ASL", 3))) {
  791. printk(KERN_INFO "%s: applying pessimistic Seagate "
  792. "errata fix\n", drive->name);
  793. return 1;
  794. }
  795. }
  796. return 0;
  797. }
  798. /**
  799. * siimage_fixup - post probe fixups
  800. * @hwif: interface to fix up
  801. *
  802. * Called after drive probe we use this to decide whether the
  803. * Seagate fixup must be applied. This used to be in init_iops but
  804. * that can occur before we know what drives are present.
  805. */
  806. static void __devinit siimage_fixup(ide_hwif_t *hwif)
  807. {
  808. /* Try and raise the rqsize */
  809. if (!is_sata(hwif) || !is_dev_seagate_sata(&hwif->drives[0]))
  810. hwif->rqsize = 128;
  811. }
  812. /**
  813. * init_iops_siimage - set up iops
  814. * @hwif: interface to set up
  815. *
  816. * Do the basic setup for the SIIMAGE hardware interface
  817. * and then do the MMIO setup if we can. This is the first
  818. * look in we get for setting up the hwif so that we
  819. * can get the iops right before using them.
  820. */
  821. static void __devinit init_iops_siimage(ide_hwif_t *hwif)
  822. {
  823. struct pci_dev *dev = hwif->pci_dev;
  824. u32 class_rev = 0;
  825. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  826. class_rev &= 0xff;
  827. hwif->hwif_data = NULL;
  828. /* Pessimal until we finish probing */
  829. hwif->rqsize = 15;
  830. if (pci_get_drvdata(dev) == NULL)
  831. return;
  832. init_mmio_iops_siimage(hwif);
  833. }
  834. /**
  835. * ata66_siimage - check for 80 pin cable
  836. * @hwif: interface to check
  837. *
  838. * Check for the presence of an ATA66 capable cable on the
  839. * interface.
  840. */
  841. static unsigned int __devinit ata66_siimage(ide_hwif_t *hwif)
  842. {
  843. unsigned long addr = siimage_selreg(hwif, 0);
  844. if (pci_get_drvdata(hwif->pci_dev) == NULL) {
  845. u8 ata66 = 0;
  846. pci_read_config_byte(hwif->pci_dev, addr, &ata66);
  847. return (ata66 & 0x01) ? 1 : 0;
  848. }
  849. return (hwif->INB(addr) & 0x01) ? 1 : 0;
  850. }
  851. /**
  852. * init_hwif_siimage - set up hwif structs
  853. * @hwif: interface to set up
  854. *
  855. * We do the basic set up of the interface structure. The SIIMAGE
  856. * requires several custom handlers so we override the default
  857. * ide DMA handlers appropriately
  858. */
  859. static void __devinit init_hwif_siimage(ide_hwif_t *hwif)
  860. {
  861. hwif->autodma = 0;
  862. hwif->resetproc = &siimage_reset;
  863. hwif->speedproc = &siimage_tune_chipset;
  864. hwif->tuneproc = &siimage_tuneproc;
  865. hwif->reset_poll = &siimage_reset_poll;
  866. hwif->pre_reset = &siimage_pre_reset;
  867. if(is_sata(hwif)) {
  868. static int first = 1;
  869. hwif->busproc = &siimage_busproc;
  870. if (first) {
  871. printk(KERN_INFO "siimage: For full SATA support you should use the libata sata_sil module.\n");
  872. first = 0;
  873. }
  874. }
  875. if (!hwif->dma_base) {
  876. hwif->drives[0].autotune = 1;
  877. hwif->drives[1].autotune = 1;
  878. return;
  879. }
  880. hwif->ultra_mask = 0x7f;
  881. hwif->mwdma_mask = 0x07;
  882. hwif->swdma_mask = 0x07;
  883. if (!is_sata(hwif))
  884. hwif->atapi_dma = 1;
  885. hwif->ide_dma_check = &siimage_config_drive_for_dma;
  886. if (!(hwif->udma_four))
  887. hwif->udma_four = ata66_siimage(hwif);
  888. if (hwif->mmio) {
  889. hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
  890. } else {
  891. hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
  892. }
  893. /*
  894. * The BIOS often doesn't set up DMA on this controller
  895. * so we always do it.
  896. */
  897. hwif->autodma = 1;
  898. hwif->drives[0].autodma = hwif->autodma;
  899. hwif->drives[1].autodma = hwif->autodma;
  900. }
  901. #define DECLARE_SII_DEV(name_str) \
  902. { \
  903. .name = name_str, \
  904. .init_chipset = init_chipset_siimage, \
  905. .init_iops = init_iops_siimage, \
  906. .init_hwif = init_hwif_siimage, \
  907. .fixup = siimage_fixup, \
  908. .channels = 2, \
  909. .autodma = AUTODMA, \
  910. .bootable = ON_BOARD, \
  911. }
  912. static ide_pci_device_t siimage_chipsets[] __devinitdata = {
  913. /* 0 */ DECLARE_SII_DEV("SiI680"),
  914. /* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA"),
  915. /* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA")
  916. };
  917. /**
  918. * siimage_init_one - pci layer discovery entry
  919. * @dev: PCI device
  920. * @id: ident table entry
  921. *
  922. * Called by the PCI code when it finds an SI680 or SI3112 controller.
  923. * We then use the IDE PCI generic helper to do most of the work.
  924. */
  925. static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  926. {
  927. return ide_setup_pci_device(dev, &siimage_chipsets[id->driver_data]);
  928. }
  929. static struct pci_device_id siimage_pci_tbl[] = {
  930. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  931. #ifdef CONFIG_BLK_DEV_IDE_SATA
  932. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  933. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_1210SA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  934. #endif
  935. { 0, },
  936. };
  937. MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
  938. static struct pci_driver driver = {
  939. .name = "SiI_IDE",
  940. .id_table = siimage_pci_tbl,
  941. .probe = siimage_init_one,
  942. };
  943. static int __init siimage_ide_init(void)
  944. {
  945. return ide_pci_register_driver(&driver);
  946. }
  947. module_init(siimage_ide_init);
  948. MODULE_AUTHOR("Andre Hedrick, Alan Cox");
  949. MODULE_DESCRIPTION("PCI driver module for SiI IDE");
  950. MODULE_LICENSE("GPL");