siimage.c 27 KB

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