sl82c105.c 12 KB

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