sl82c105.c 11 KB

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