siimage.c 24 KB

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