siimage.c 27 KB

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