sl82c105.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 hwif->ide_dma_on(drive);
  134. }
  135. if (__ide_dma_good_drive(drive))
  136. return hwif->ide_dma_on(drive);
  137. } while (0);
  138. return hwif->ide_dma_off_quietly(drive);
  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 = hwif->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. config_for_pio(drive, 4, 0, 0);
  216. return HWIF(drive)->ide_dma_off_quietly(drive);
  217. }
  218. printk(KERN_INFO "%s: DMA enabled\n", drive->name);
  219. return __ide_dma_on(drive);
  220. }
  221. static int sl82c105_ide_dma_off_quietly (ide_drive_t *drive)
  222. {
  223. u8 speed = XFER_PIO_0;
  224. int rc;
  225. DBG(("sl82c105_ide_dma_off_quietly(drive:%s)\n", drive->name));
  226. rc = __ide_dma_off_quietly(drive);
  227. if (drive->pio_speed)
  228. speed = drive->pio_speed - XFER_PIO_0;
  229. config_for_pio(drive, speed, 0, 1);
  230. drive->current_speed = drive->pio_speed;
  231. return rc;
  232. }
  233. /*
  234. * Ok, that is nasty, but we must make sure the DMA timings
  235. * won't be used for a PIO access. The solution here is
  236. * to make sure the 16 bits mode is diabled on the channel
  237. * when DMA is enabled, thus causing the chip to use PIO0
  238. * timings for those operations.
  239. */
  240. static void sl82c105_selectproc(ide_drive_t *drive)
  241. {
  242. ide_hwif_t *hwif = HWIF(drive);
  243. struct pci_dev *dev = hwif->pci_dev;
  244. u32 val, old, mask;
  245. //DBG(("sl82c105_selectproc(drive:%s)\n", drive->name));
  246. mask = hwif->channel ? CTRL_P1F16 : CTRL_P0F16;
  247. old = val = *((u32 *)&hwif->hwif_data);
  248. if (drive->using_dma)
  249. val &= ~mask;
  250. else
  251. val |= mask;
  252. if (old != val) {
  253. pci_write_config_dword(dev, 0x40, val);
  254. *((u32 *)&hwif->hwif_data) = val;
  255. }
  256. }
  257. /*
  258. * ATA reset will clear the 16 bits mode in the control
  259. * register, we need to update our cache
  260. */
  261. static void sl82c105_resetproc(ide_drive_t *drive)
  262. {
  263. ide_hwif_t *hwif = HWIF(drive);
  264. struct pci_dev *dev = hwif->pci_dev;
  265. u32 val;
  266. DBG(("sl82c105_resetproc(drive:%s)\n", drive->name));
  267. pci_read_config_dword(dev, 0x40, &val);
  268. *((u32 *)&hwif->hwif_data) = val;
  269. }
  270. /*
  271. * We only deal with PIO mode here - DMA mode 'using_dma' is not
  272. * initialised at the point that this function is called.
  273. */
  274. static void tune_sl82c105(ide_drive_t *drive, u8 pio)
  275. {
  276. DBG(("tune_sl82c105(drive:%s)\n", drive->name));
  277. config_for_pio(drive, pio, 1, 0);
  278. /*
  279. * We support 32-bit I/O on this interface, and it
  280. * doesn't have problems with interrupts.
  281. */
  282. drive->io_32bit = 1;
  283. drive->unmask = 1;
  284. }
  285. /*
  286. * Return the revision of the Winbond bridge
  287. * which this function is part of.
  288. */
  289. static unsigned int sl82c105_bridge_revision(struct pci_dev *dev)
  290. {
  291. struct pci_dev *bridge;
  292. u8 rev;
  293. /*
  294. * The bridge should be part of the same device, but function 0.
  295. */
  296. bridge = pci_find_slot(dev->bus->number,
  297. PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
  298. if (!bridge)
  299. return -1;
  300. /*
  301. * Make sure it is a Winbond 553 and is an ISA bridge.
  302. */
  303. if (bridge->vendor != PCI_VENDOR_ID_WINBOND ||
  304. bridge->device != PCI_DEVICE_ID_WINBOND_83C553 ||
  305. bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA)
  306. return -1;
  307. /*
  308. * We need to find function 0's revision, not function 1
  309. */
  310. pci_read_config_byte(bridge, PCI_REVISION_ID, &rev);
  311. return rev;
  312. }
  313. /*
  314. * Enable the PCI device
  315. *
  316. * --BenH: It's arch fixup code that should enable channels that
  317. * have not been enabled by firmware. I decided we can still enable
  318. * channel 0 here at least, but channel 1 has to be enabled by
  319. * firmware or arch code. We still set both to 16 bits mode.
  320. */
  321. static unsigned int __devinit init_chipset_sl82c105(struct pci_dev *dev, const char *msg)
  322. {
  323. u32 val;
  324. DBG(("init_chipset_sl82c105()\n"));
  325. pci_read_config_dword(dev, 0x40, &val);
  326. val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16;
  327. pci_write_config_dword(dev, 0x40, val);
  328. return dev->irq;
  329. }
  330. /*
  331. * Initialise the chip
  332. */
  333. static void __devinit init_hwif_sl82c105(ide_hwif_t *hwif)
  334. {
  335. struct pci_dev *dev = hwif->pci_dev;
  336. unsigned int rev;
  337. u8 dma_state;
  338. u32 val;
  339. DBG(("init_hwif_sl82c105(hwif: ide%d)\n", hwif->index));
  340. hwif->tuneproc = tune_sl82c105;
  341. hwif->selectproc = sl82c105_selectproc;
  342. hwif->resetproc = sl82c105_resetproc;
  343. /* Default to PIO 0 for fallback unless tuned otherwise,
  344. * we always autotune PIO, this is done before DMA is
  345. * checked, so there is no risk of accidentally disabling
  346. * DMA
  347. */
  348. hwif->drives[0].pio_speed = XFER_PIO_0;
  349. hwif->drives[0].autotune = 1;
  350. hwif->drives[1].pio_speed = XFER_PIO_1;
  351. hwif->drives[1].autotune = 1;
  352. pci_read_config_dword(dev, 0x40, &val);
  353. *((u32 *)&hwif->hwif_data) = val;
  354. hwif->atapi_dma = 0;
  355. hwif->mwdma_mask = 0;
  356. hwif->swdma_mask = 0;
  357. hwif->autodma = 0;
  358. if (!hwif->dma_base)
  359. return;
  360. dma_state = hwif->INB(hwif->dma_base + 2) & ~0x60;
  361. rev = sl82c105_bridge_revision(hwif->pci_dev);
  362. if (rev <= 5) {
  363. /*
  364. * Never ever EVER under any circumstances enable
  365. * DMA when the bridge is this old.
  366. */
  367. printk(" %s: Winbond 553 bridge revision %d, BM-DMA disabled\n",
  368. hwif->name, rev);
  369. } else {
  370. dma_state |= 0x60;
  371. hwif->atapi_dma = 1;
  372. hwif->mwdma_mask = 0x07;
  373. hwif->swdma_mask = 0x07;
  374. hwif->ide_dma_check = &sl82c105_check_drive;
  375. hwif->ide_dma_on = &sl82c105_ide_dma_on;
  376. hwif->ide_dma_off_quietly = &sl82c105_ide_dma_off_quietly;
  377. hwif->ide_dma_lostirq = &sl82c105_ide_dma_lost_irq;
  378. hwif->dma_start = &sl82c105_ide_dma_start;
  379. hwif->ide_dma_timeout = &sl82c105_ide_dma_timeout;
  380. if (!noautodma)
  381. hwif->autodma = 1;
  382. hwif->drives[0].autodma = hwif->autodma;
  383. hwif->drives[1].autodma = hwif->autodma;
  384. if (hwif->mate)
  385. hwif->serialized = hwif->mate->serialized = 1;
  386. }
  387. hwif->OUTB(dma_state, hwif->dma_base + 2);
  388. }
  389. static ide_pci_device_t sl82c105_chipset __devinitdata = {
  390. .name = "W82C105",
  391. .init_chipset = init_chipset_sl82c105,
  392. .init_hwif = init_hwif_sl82c105,
  393. .channels = 2,
  394. .autodma = NOAUTODMA,
  395. .enablebits = {{0x40,0x01,0x01}, {0x40,0x10,0x10}},
  396. .bootable = ON_BOARD,
  397. };
  398. static int __devinit sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  399. {
  400. return ide_setup_pci_device(dev, &sl82c105_chipset);
  401. }
  402. static struct pci_device_id sl82c105_pci_tbl[] = {
  403. { PCI_DEVICE(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105), 0},
  404. { 0, },
  405. };
  406. MODULE_DEVICE_TABLE(pci, sl82c105_pci_tbl);
  407. static struct pci_driver driver = {
  408. .name = "W82C105_IDE",
  409. .id_table = sl82c105_pci_tbl,
  410. .probe = sl82c105_init_one,
  411. };
  412. static int sl82c105_ide_init(void)
  413. {
  414. return ide_pci_register_driver(&driver);
  415. }
  416. module_init(sl82c105_ide_init);
  417. MODULE_DESCRIPTION("PCI driver module for W82C105 IDE");
  418. MODULE_LICENSE("GPL");