sl82c105.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * linux/drivers/ide/pci/sl82c105.c
  3. *
  4. * SL82C105/Winbond 553 IDE driver
  5. *
  6. * Maintainer unknown.
  7. *
  8. * Drive tuning added from Rebel.com's kernel sources
  9. * -- Russell King (15/11/98) linux@arm.linux.org.uk
  10. *
  11. * Merge in Russell's HW workarounds, fix various problems
  12. * with the timing registers setup.
  13. * -- Benjamin Herrenschmidt (01/11/03) benh@kernel.crashing.org
  14. */
  15. #include <linux/config.h>
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/timer.h>
  20. #include <linux/mm.h>
  21. #include <linux/ioport.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/pci.h>
  26. #include <linux/ide.h>
  27. #include <asm/io.h>
  28. #include <asm/dma.h>
  29. #undef DEBUG
  30. #ifdef DEBUG
  31. #define DBG(arg) printk arg
  32. #else
  33. #define DBG(fmt,...)
  34. #endif
  35. /*
  36. * SL82C105 PCI config register 0x40 bits.
  37. */
  38. #define CTRL_IDE_IRQB (1 << 30)
  39. #define CTRL_IDE_IRQA (1 << 28)
  40. #define CTRL_LEGIRQ (1 << 11)
  41. #define CTRL_P1F16 (1 << 5)
  42. #define CTRL_P1EN (1 << 4)
  43. #define CTRL_P0F16 (1 << 1)
  44. #define CTRL_P0EN (1 << 0)
  45. /*
  46. * Convert a PIO mode and cycle time to the required on/off
  47. * times for the interface. This has protection against run-away
  48. * timings.
  49. */
  50. static unsigned int get_timing_sl82c105(ide_pio_data_t *p)
  51. {
  52. unsigned int cmd_on;
  53. unsigned int cmd_off;
  54. cmd_on = (ide_pio_timings[p->pio_mode].active_time + 29) / 30;
  55. cmd_off = (p->cycle_time - 30 * cmd_on + 29) / 30;
  56. if (cmd_on > 32)
  57. cmd_on = 32;
  58. if (cmd_on == 0)
  59. cmd_on = 1;
  60. if (cmd_off > 32)
  61. cmd_off = 32;
  62. if (cmd_off == 0)
  63. cmd_off = 1;
  64. return (cmd_on - 1) << 8 | (cmd_off - 1) | (p->use_iordy ? 0x40 : 0x00);
  65. }
  66. /*
  67. * Configure the drive and chipset for PIO
  68. */
  69. static void config_for_pio(ide_drive_t *drive, int pio, int report, int chipset_only)
  70. {
  71. ide_hwif_t *hwif = HWIF(drive);
  72. struct pci_dev *dev = hwif->pci_dev;
  73. ide_pio_data_t p;
  74. u16 drv_ctrl = 0x909;
  75. unsigned int xfer_mode, reg;
  76. DBG(("config_for_pio(drive:%s, pio:%d, report:%d, chipset_only:%d)\n",
  77. drive->name, pio, report, chipset_only));
  78. reg = (hwif->channel ? 0x4c : 0x44) + (drive->select.b.unit ? 4 : 0);
  79. pio = ide_get_best_pio_mode(drive, pio, 5, &p);
  80. xfer_mode = XFER_PIO_0 + pio;
  81. if (chipset_only || ide_config_drive_speed(drive, xfer_mode) == 0) {
  82. drv_ctrl = get_timing_sl82c105(&p);
  83. drive->pio_speed = xfer_mode;
  84. } else
  85. drive->pio_speed = XFER_PIO_0;
  86. if (drive->using_dma == 0) {
  87. /*
  88. * If we are actually using MW DMA, then we can not
  89. * reprogram the interface drive control register.
  90. */
  91. pci_write_config_word(dev, reg, drv_ctrl);
  92. pci_read_config_word(dev, reg, &drv_ctrl);
  93. if (report) {
  94. printk("%s: selected %s (%dns) (%04X)\n", drive->name,
  95. ide_xfer_verbose(xfer_mode), p.cycle_time, drv_ctrl);
  96. }
  97. }
  98. }
  99. /*
  100. * Configure the drive and the chipset for DMA
  101. */
  102. static int config_for_dma (ide_drive_t *drive)
  103. {
  104. ide_hwif_t *hwif = HWIF(drive);
  105. struct pci_dev *dev = hwif->pci_dev;
  106. unsigned int reg;
  107. DBG(("config_for_dma(drive:%s)\n", drive->name));
  108. reg = (hwif->channel ? 0x4c : 0x44) + (drive->select.b.unit ? 4 : 0);
  109. if (ide_config_drive_speed(drive, XFER_MW_DMA_2) != 0)
  110. return 1;
  111. pci_write_config_word(dev, reg, 0x0240);
  112. return 0;
  113. }
  114. /*
  115. * Check to see if the drive and
  116. * chipset is capable of DMA mode
  117. */
  118. static int sl82c105_check_drive (ide_drive_t *drive)
  119. {
  120. ide_hwif_t *hwif = HWIF(drive);
  121. DBG(("sl82c105_check_drive(drive:%s)\n", drive->name));
  122. do {
  123. struct hd_driveid *id = drive->id;
  124. if (!drive->autodma)
  125. break;
  126. if (!id || !(id->capability & 1))
  127. break;
  128. /* Consult the list of known "bad" drives */
  129. if (__ide_dma_bad_drive(drive))
  130. break;
  131. if (id->field_valid & 2) {
  132. if ((id->dma_mword & hwif->mwdma_mask) ||
  133. (id->dma_1word & hwif->swdma_mask))
  134. return hwif->ide_dma_on(drive);
  135. }
  136. if (__ide_dma_good_drive(drive))
  137. return hwif->ide_dma_on(drive);
  138. } while (0);
  139. return hwif->ide_dma_off_quietly(drive);
  140. }
  141. /*
  142. * The SL82C105 holds off all IDE interrupts while in DMA mode until
  143. * all DMA activity is completed. Sometimes this causes problems (eg,
  144. * when the drive wants to report an error condition).
  145. *
  146. * 0x7e is a "chip testing" register. Bit 2 resets the DMA controller
  147. * state machine. We need to kick this to work around various bugs.
  148. */
  149. static inline void sl82c105_reset_host(struct pci_dev *dev)
  150. {
  151. u16 val;
  152. pci_read_config_word(dev, 0x7e, &val);
  153. pci_write_config_word(dev, 0x7e, val | (1 << 2));
  154. pci_write_config_word(dev, 0x7e, val & ~(1 << 2));
  155. }
  156. /*
  157. * If we get an IRQ timeout, it might be that the DMA state machine
  158. * got confused. Fix from Todd Inglett. Details from Winbond.
  159. *
  160. * This function is called when the IDE timer expires, the drive
  161. * indicates that it is READY, and we were waiting for DMA to complete.
  162. */
  163. static int sl82c105_ide_dma_lost_irq(ide_drive_t *drive)
  164. {
  165. ide_hwif_t *hwif = HWIF(drive);
  166. struct pci_dev *dev = hwif->pci_dev;
  167. u32 val, mask = hwif->channel ? CTRL_IDE_IRQB : CTRL_IDE_IRQA;
  168. unsigned long dma_base = hwif->dma_base;
  169. printk("sl82c105: lost IRQ: resetting host\n");
  170. /*
  171. * Check the raw interrupt from the drive.
  172. */
  173. pci_read_config_dword(dev, 0x40, &val);
  174. if (val & mask)
  175. printk("sl82c105: drive was requesting IRQ, but host lost it\n");
  176. /*
  177. * Was DMA enabled? If so, disable it - we're resetting the
  178. * host. The IDE layer will be handling the drive for us.
  179. */
  180. val = hwif->INB(dma_base);
  181. if (val & 1) {
  182. outb(val & ~1, dma_base);
  183. printk("sl82c105: DMA was enabled\n");
  184. }
  185. sl82c105_reset_host(dev);
  186. /* ide_dmaproc would return 1, so we do as well */
  187. return 1;
  188. }
  189. /*
  190. * ATAPI devices can cause the SL82C105 DMA state machine to go gaga.
  191. * Winbond recommend that the DMA state machine is reset prior to
  192. * setting the bus master DMA enable bit.
  193. *
  194. * The generic IDE core will have disabled the BMEN bit before this
  195. * function is called.
  196. */
  197. static void sl82c105_ide_dma_start(ide_drive_t *drive)
  198. {
  199. ide_hwif_t *hwif = HWIF(drive);
  200. struct pci_dev *dev = hwif->pci_dev;
  201. sl82c105_reset_host(dev);
  202. ide_dma_start(drive);
  203. }
  204. static int sl82c105_ide_dma_timeout(ide_drive_t *drive)
  205. {
  206. ide_hwif_t *hwif = HWIF(drive);
  207. struct pci_dev *dev = hwif->pci_dev;
  208. DBG(("sl82c105_ide_dma_timeout(drive:%s)\n", drive->name));
  209. sl82c105_reset_host(dev);
  210. return __ide_dma_timeout(drive);
  211. }
  212. static int sl82c105_ide_dma_on (ide_drive_t *drive)
  213. {
  214. DBG(("sl82c105_ide_dma_on(drive:%s)\n", drive->name));
  215. if (config_for_dma(drive)) {
  216. config_for_pio(drive, 4, 0, 0);
  217. return HWIF(drive)->ide_dma_off_quietly(drive);
  218. }
  219. printk(KERN_INFO "%s: DMA enabled\n", drive->name);
  220. return __ide_dma_on(drive);
  221. }
  222. static int sl82c105_ide_dma_off_quietly (ide_drive_t *drive)
  223. {
  224. u8 speed = XFER_PIO_0;
  225. int rc;
  226. DBG(("sl82c105_ide_dma_off_quietly(drive:%s)\n", drive->name));
  227. rc = __ide_dma_off_quietly(drive);
  228. if (drive->pio_speed)
  229. speed = drive->pio_speed - XFER_PIO_0;
  230. config_for_pio(drive, speed, 0, 1);
  231. drive->current_speed = drive->pio_speed;
  232. return rc;
  233. }
  234. /*
  235. * Ok, that is nasty, but we must make sure the DMA timings
  236. * won't be used for a PIO access. The solution here is
  237. * to make sure the 16 bits mode is diabled on the channel
  238. * when DMA is enabled, thus causing the chip to use PIO0
  239. * timings for those operations.
  240. */
  241. static void sl82c105_selectproc(ide_drive_t *drive)
  242. {
  243. ide_hwif_t *hwif = HWIF(drive);
  244. struct pci_dev *dev = hwif->pci_dev;
  245. u32 val, old, mask;
  246. //DBG(("sl82c105_selectproc(drive:%s)\n", drive->name));
  247. mask = hwif->channel ? CTRL_P1F16 : CTRL_P0F16;
  248. old = val = *((u32 *)&hwif->hwif_data);
  249. if (drive->using_dma)
  250. val &= ~mask;
  251. else
  252. val |= mask;
  253. if (old != val) {
  254. pci_write_config_dword(dev, 0x40, val);
  255. *((u32 *)&hwif->hwif_data) = val;
  256. }
  257. }
  258. /*
  259. * ATA reset will clear the 16 bits mode in the control
  260. * register, we need to update our cache
  261. */
  262. static void sl82c105_resetproc(ide_drive_t *drive)
  263. {
  264. ide_hwif_t *hwif = HWIF(drive);
  265. struct pci_dev *dev = hwif->pci_dev;
  266. u32 val;
  267. DBG(("sl82c105_resetproc(drive:%s)\n", drive->name));
  268. pci_read_config_dword(dev, 0x40, &val);
  269. *((u32 *)&hwif->hwif_data) = val;
  270. }
  271. /*
  272. * We only deal with PIO mode here - DMA mode 'using_dma' is not
  273. * initialised at the point that this function is called.
  274. */
  275. static void tune_sl82c105(ide_drive_t *drive, u8 pio)
  276. {
  277. DBG(("tune_sl82c105(drive:%s)\n", drive->name));
  278. config_for_pio(drive, pio, 1, 0);
  279. /*
  280. * We support 32-bit I/O on this interface, and it
  281. * doesn't have problems with interrupts.
  282. */
  283. drive->io_32bit = 1;
  284. drive->unmask = 1;
  285. }
  286. /*
  287. * Return the revision of the Winbond bridge
  288. * which this function is part of.
  289. */
  290. static unsigned int sl82c105_bridge_revision(struct pci_dev *dev)
  291. {
  292. struct pci_dev *bridge;
  293. u8 rev;
  294. /*
  295. * The bridge should be part of the same device, but function 0.
  296. */
  297. bridge = pci_find_slot(dev->bus->number,
  298. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  299. if (!bridge)
  300. return -1;
  301. /*
  302. * Make sure it is a Winbond 553 and is an ISA bridge.
  303. */
  304. if (bridge->vendor != PCI_VENDOR_ID_WINBOND ||
  305. bridge->device != PCI_DEVICE_ID_WINBOND_83C553 ||
  306. bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA)
  307. return -1;
  308. /*
  309. * We need to find function 0's revision, not function 1
  310. */
  311. pci_read_config_byte(bridge, PCI_REVISION_ID, &rev);
  312. return rev;
  313. }
  314. /*
  315. * Enable the PCI device
  316. *
  317. * --BenH: It's arch fixup code that should enable channels that
  318. * have not been enabled by firmware. I decided we can still enable
  319. * channel 0 here at least, but channel 1 has to be enabled by
  320. * firmware or arch code. We still set both to 16 bits mode.
  321. */
  322. static unsigned int __devinit init_chipset_sl82c105(struct pci_dev *dev, const char *msg)
  323. {
  324. u32 val;
  325. DBG(("init_chipset_sl82c105()\n"));
  326. pci_read_config_dword(dev, 0x40, &val);
  327. val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16;
  328. pci_write_config_dword(dev, 0x40, val);
  329. return dev->irq;
  330. }
  331. static void __devinit init_dma_sl82c105(ide_hwif_t *hwif, unsigned long dma_base)
  332. {
  333. unsigned int rev;
  334. u8 dma_state;
  335. DBG(("init_dma_sl82c105(hwif: ide%d, dma_base: 0x%08x)\n", hwif->index, dma_base));
  336. hwif->autodma = 0;
  337. if (!dma_base)
  338. return;
  339. dma_state = hwif->INB(dma_base + 2);
  340. rev = sl82c105_bridge_revision(hwif->pci_dev);
  341. if (rev <= 5) {
  342. printk(" %s: Winbond 553 bridge revision %d, BM-DMA disabled\n",
  343. hwif->name, rev);
  344. dma_state &= ~0x60;
  345. } else {
  346. dma_state |= 0x60;
  347. if (!noautodma)
  348. hwif->autodma = 1;
  349. }
  350. hwif->OUTB(dma_state, dma_base + 2);
  351. ide_setup_dma(hwif, dma_base, 8);
  352. }
  353. /*
  354. * Initialise the chip
  355. */
  356. static void __devinit init_hwif_sl82c105(ide_hwif_t *hwif)
  357. {
  358. struct pci_dev *dev = hwif->pci_dev;
  359. u32 val;
  360. DBG(("init_hwif_sl82c105(hwif: ide%d)\n", hwif->index));
  361. hwif->tuneproc = tune_sl82c105;
  362. hwif->selectproc = sl82c105_selectproc;
  363. hwif->resetproc = sl82c105_resetproc;
  364. /* Default to PIO 0 for fallback unless tuned otherwise,
  365. * we always autotune PIO, this is done before DMA is
  366. * checked, so there is no risk of accidentally disabling
  367. * DMA
  368. */
  369. hwif->drives[0].pio_speed = XFER_PIO_0;
  370. hwif->drives[0].autotune = 1;
  371. hwif->drives[1].pio_speed = XFER_PIO_1;
  372. hwif->drives[1].autotune = 1;
  373. pci_read_config_dword(dev, 0x40, &val);
  374. *((u32 *)&hwif->hwif_data) = val;
  375. if (!hwif->dma_base)
  376. return;
  377. hwif->atapi_dma = 1;
  378. hwif->mwdma_mask = 0x07;
  379. hwif->swdma_mask = 0x07;
  380. #ifdef CONFIG_BLK_DEV_IDEDMA
  381. hwif->ide_dma_check = &sl82c105_check_drive;
  382. hwif->ide_dma_on = &sl82c105_ide_dma_on;
  383. hwif->ide_dma_off_quietly = &sl82c105_ide_dma_off_quietly;
  384. hwif->ide_dma_lostirq = &sl82c105_ide_dma_lost_irq;
  385. hwif->dma_start = &sl82c105_ide_dma_start;
  386. hwif->ide_dma_timeout = &sl82c105_ide_dma_timeout;
  387. if (!noautodma)
  388. hwif->autodma = 1;
  389. hwif->drives[0].autodma = hwif->autodma;
  390. hwif->drives[1].autodma = hwif->autodma;
  391. #endif /* CONFIG_BLK_DEV_IDEDMA */
  392. }
  393. static ide_pci_device_t sl82c105_chipset __devinitdata = {
  394. .name = "W82C105",
  395. .init_chipset = init_chipset_sl82c105,
  396. .init_hwif = init_hwif_sl82c105,
  397. .init_dma = init_dma_sl82c105,
  398. .channels = 2,
  399. .autodma = NOAUTODMA,
  400. .enablebits = {{0x40,0x01,0x01}, {0x40,0x10,0x10}},
  401. .bootable = ON_BOARD,
  402. };
  403. static int __devinit sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  404. {
  405. return ide_setup_pci_device(dev, &sl82c105_chipset);
  406. }
  407. static struct pci_device_id sl82c105_pci_tbl[] = {
  408. { PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  409. { 0, },
  410. };
  411. MODULE_DEVICE_TABLE(pci, sl82c105_pci_tbl);
  412. static struct pci_driver driver = {
  413. .name = "W82C105_IDE",
  414. .id_table = sl82c105_pci_tbl,
  415. .probe = sl82c105_init_one,
  416. };
  417. static int sl82c105_ide_init(void)
  418. {
  419. return ide_pci_register_driver(&driver);
  420. }
  421. module_init(sl82c105_ide_init);
  422. MODULE_DESCRIPTION("PCI driver module for W82C105 IDE");
  423. MODULE_LICENSE("GPL");