siimage.c 27 KB

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