cy82c693.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * linux/drivers/ide/pci/cy82c693.c Version 0.40 Sep. 10, 2002
  3. *
  4. * Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer
  5. * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>, Integrator
  6. *
  7. * CYPRESS CY82C693 chipset IDE controller
  8. *
  9. * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
  10. * Writing the driver was quite simple, since most of the job is
  11. * done by the generic pci-ide support.
  12. * The hard part was finding the CY82C693's datasheet on Cypress's
  13. * web page :-(. But Altavista solved this problem :-).
  14. *
  15. *
  16. * Notes:
  17. * - I recently got a 16.8G IBM DTTA, so I was able to test it with
  18. * a large and fast disk - the results look great, so I'd say the
  19. * driver is working fine :-)
  20. * hdparm -t reports 8.17 MB/sec at about 6% CPU usage for the DTTA
  21. * - this is my first linux driver, so there's probably a lot of room
  22. * for optimizations and bug fixing, so feel free to do it.
  23. * - use idebus=xx parameter to set PCI bus speed - needed to calc
  24. * timings for PIO modes (default will be 40)
  25. * - if using PIO mode it's a good idea to set the PIO mode and
  26. * 32-bit I/O support (if possible), e.g. hdparm -p2 -c1 /dev/hda
  27. * - I had some problems with my IBM DHEA with PIO modes < 2
  28. * (lost interrupts) ?????
  29. * - first tests with DMA look okay, they seem to work, but there is a
  30. * problem with sound - the BusMaster IDE TimeOut should fixed this
  31. *
  32. * Ancient History:
  33. * AMH@1999-08-24: v0.34 init_cy82c693_chip moved to pci_init_cy82c693
  34. * ASK@1999-01-23: v0.33 made a few minor code clean ups
  35. * removed DMA clock speed setting by default
  36. * added boot message
  37. * ASK@1998-11-01: v0.32 added support to set BusMaster IDE TimeOut
  38. * added support to set DMA Controller Clock Speed
  39. * ASK@1998-10-31: v0.31 fixed problem with setting to high DMA modes
  40. * on some drives.
  41. * ASK@1998-10-29: v0.3 added support to set DMA modes
  42. * ASK@1998-10-28: v0.2 added support to set PIO modes
  43. * ASK@1998-10-27: v0.1 first version - chipset detection
  44. *
  45. */
  46. #include <linux/config.h>
  47. #include <linux/module.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/delay.h>
  51. #include <linux/ide.h>
  52. #include <linux/init.h>
  53. #include <asm/io.h>
  54. /* the current version */
  55. #define CY82_VERSION "CY82C693U driver v0.34 99-13-12 Andreas S. Krebs (akrebs@altavista.net)"
  56. /*
  57. * The following are used to debug the driver.
  58. */
  59. #define CY82C693_DEBUG_LOGS 0
  60. #define CY82C693_DEBUG_INFO 0
  61. /* define CY82C693_SETDMA_CLOCK to set DMA Controller Clock Speed to ATCLK */
  62. #undef CY82C693_SETDMA_CLOCK
  63. /*
  64. * NOTE: the value for busmaster timeout is tricky and I got it by
  65. * trial and error! By using a to low value will cause DMA timeouts
  66. * and drop IDE performance, and by using a to high value will cause
  67. * audio playback to scatter.
  68. * If you know a better value or how to calc it, please let me know.
  69. */
  70. /* twice the value written in cy82c693ub datasheet */
  71. #define BUSMASTER_TIMEOUT 0x50
  72. /*
  73. * the value above was tested on my machine and it seems to work okay
  74. */
  75. /* here are the offset definitions for the registers */
  76. #define CY82_IDE_CMDREG 0x04
  77. #define CY82_IDE_ADDRSETUP 0x48
  78. #define CY82_IDE_MASTER_IOR 0x4C
  79. #define CY82_IDE_MASTER_IOW 0x4D
  80. #define CY82_IDE_SLAVE_IOR 0x4E
  81. #define CY82_IDE_SLAVE_IOW 0x4F
  82. #define CY82_IDE_MASTER_8BIT 0x50
  83. #define CY82_IDE_SLAVE_8BIT 0x51
  84. #define CY82_INDEX_PORT 0x22
  85. #define CY82_DATA_PORT 0x23
  86. #define CY82_INDEX_CTRLREG1 0x01
  87. #define CY82_INDEX_CHANNEL0 0x30
  88. #define CY82_INDEX_CHANNEL1 0x31
  89. #define CY82_INDEX_TIMEOUT 0x32
  90. /* the max PIO mode - from datasheet */
  91. #define CY82C693_MAX_PIO 4
  92. /* the min and max PCI bus speed in MHz - from datasheet */
  93. #define CY82C963_MIN_BUS_SPEED 25
  94. #define CY82C963_MAX_BUS_SPEED 33
  95. /* the struct for the PIO mode timings */
  96. typedef struct pio_clocks_s {
  97. u8 address_time; /* Address setup (clocks) */
  98. u8 time_16r; /* clocks for 16bit IOR (0xF0=Active/data, 0x0F=Recovery) */
  99. u8 time_16w; /* clocks for 16bit IOW (0xF0=Active/data, 0x0F=Recovery) */
  100. u8 time_8; /* clocks for 8bit (0xF0=Active/data, 0x0F=Recovery) */
  101. } pio_clocks_t;
  102. /*
  103. * calc clocks using bus_speed
  104. * returns (rounded up) time in bus clocks for time in ns
  105. */
  106. static int calc_clk (int time, int bus_speed)
  107. {
  108. int clocks;
  109. clocks = (time*bus_speed+999)/1000 -1;
  110. if (clocks < 0)
  111. clocks = 0;
  112. if (clocks > 0x0F)
  113. clocks = 0x0F;
  114. return clocks;
  115. }
  116. /*
  117. * compute the values for the clock registers for PIO
  118. * mode and pci_clk [MHz] speed
  119. *
  120. * NOTE: for mode 0,1 and 2 drives 8-bit IDE command control registers are used
  121. * for mode 3 and 4 drives 8 and 16-bit timings are the same
  122. *
  123. */
  124. static void compute_clocks (u8 pio, pio_clocks_t *p_pclk)
  125. {
  126. int clk1, clk2;
  127. int bus_speed = system_bus_clock(); /* get speed of PCI bus */
  128. /* we don't check against CY82C693's min and max speed,
  129. * so you can play with the idebus=xx parameter
  130. */
  131. if (pio > CY82C693_MAX_PIO)
  132. pio = CY82C693_MAX_PIO;
  133. /* let's calc the address setup time clocks */
  134. p_pclk->address_time = (u8)calc_clk(ide_pio_timings[pio].setup_time, bus_speed);
  135. /* let's calc the active and recovery time clocks */
  136. clk1 = calc_clk(ide_pio_timings[pio].active_time, bus_speed);
  137. /* calc recovery timing */
  138. clk2 = ide_pio_timings[pio].cycle_time -
  139. ide_pio_timings[pio].active_time -
  140. ide_pio_timings[pio].setup_time;
  141. clk2 = calc_clk(clk2, bus_speed);
  142. clk1 = (clk1<<4)|clk2; /* combine active and recovery clocks */
  143. /* note: we use the same values for 16bit IOR and IOW
  144. * those are all the same, since I don't have other
  145. * timings than those from ide-lib.c
  146. */
  147. p_pclk->time_16r = (u8)clk1;
  148. p_pclk->time_16w = (u8)clk1;
  149. /* what are good values for 8bit ?? */
  150. p_pclk->time_8 = (u8)clk1;
  151. }
  152. /*
  153. * set DMA mode a specific channel for CY82C693
  154. */
  155. static void cy82c693_dma_enable (ide_drive_t *drive, int mode, int single)
  156. {
  157. u8 index = 0, data = 0;
  158. if (mode>2) /* make sure we set a valid mode */
  159. mode = 2;
  160. if (mode > drive->id->tDMA) /* to be absolutly sure we have a valid mode */
  161. mode = drive->id->tDMA;
  162. index = (HWIF(drive)->channel==0) ? CY82_INDEX_CHANNEL0 : CY82_INDEX_CHANNEL1;
  163. #if CY82C693_DEBUG_LOGS
  164. /* for debug let's show the previous values */
  165. HWIF(drive)->OUTB(index, CY82_INDEX_PORT);
  166. data = HWIF(drive)->INB(CY82_DATA_PORT);
  167. printk (KERN_INFO "%s (ch=%d, dev=%d): DMA mode is %d (single=%d)\n",
  168. drive->name, HWIF(drive)->channel, drive->select.b.unit,
  169. (data&0x3), ((data>>2)&1));
  170. #endif /* CY82C693_DEBUG_LOGS */
  171. data = (u8)mode|(u8)(single<<2);
  172. HWIF(drive)->OUTB(index, CY82_INDEX_PORT);
  173. HWIF(drive)->OUTB(data, CY82_DATA_PORT);
  174. #if CY82C693_DEBUG_INFO
  175. printk(KERN_INFO "%s (ch=%d, dev=%d): set DMA mode to %d (single=%d)\n",
  176. drive->name, HWIF(drive)->channel, drive->select.b.unit,
  177. mode, single);
  178. #endif /* CY82C693_DEBUG_INFO */
  179. /*
  180. * note: below we set the value for Bus Master IDE TimeOut Register
  181. * I'm not absolutly sure what this does, but it solved my problem
  182. * with IDE DMA and sound, so I now can play sound and work with
  183. * my IDE driver at the same time :-)
  184. *
  185. * If you know the correct (best) value for this register please
  186. * let me know - ASK
  187. */
  188. data = BUSMASTER_TIMEOUT;
  189. HWIF(drive)->OUTB(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
  190. HWIF(drive)->OUTB(data, CY82_DATA_PORT);
  191. #if CY82C693_DEBUG_INFO
  192. printk (KERN_INFO "%s: Set IDE Bus Master TimeOut Register to 0x%X\n",
  193. drive->name, data);
  194. #endif /* CY82C693_DEBUG_INFO */
  195. }
  196. /*
  197. * used to set DMA mode for CY82C693 (single and multi modes)
  198. */
  199. static int cy82c693_ide_dma_on (ide_drive_t *drive)
  200. {
  201. struct hd_driveid *id = drive->id;
  202. #if CY82C693_DEBUG_INFO
  203. printk (KERN_INFO "dma_on: %s\n", drive->name);
  204. #endif /* CY82C693_DEBUG_INFO */
  205. if (id != NULL) {
  206. /* Enable DMA on any drive that has DMA
  207. * (multi or single) enabled
  208. */
  209. if (id->field_valid & 2) { /* regular DMA */
  210. int mmode, smode;
  211. mmode = id->dma_mword & (id->dma_mword >> 8);
  212. smode = id->dma_1word & (id->dma_1word >> 8);
  213. if (mmode != 0) {
  214. /* enable multi */
  215. cy82c693_dma_enable(drive, (mmode >> 1), 0);
  216. } else if (smode != 0) {
  217. /* enable single */
  218. cy82c693_dma_enable(drive, (smode >> 1), 1);
  219. }
  220. }
  221. }
  222. return __ide_dma_on(drive);
  223. }
  224. /*
  225. * tune ide drive - set PIO mode
  226. */
  227. static void cy82c693_tune_drive (ide_drive_t *drive, u8 pio)
  228. {
  229. ide_hwif_t *hwif = HWIF(drive);
  230. struct pci_dev *dev = hwif->pci_dev;
  231. pio_clocks_t pclk;
  232. unsigned int addrCtrl;
  233. /* select primary or secondary channel */
  234. if (hwif->index > 0) { /* drive is on the secondary channel */
  235. dev = pci_find_slot(dev->bus->number, dev->devfn+1);
  236. if (!dev) {
  237. printk(KERN_ERR "%s: tune_drive: "
  238. "Cannot find secondary interface!\n",
  239. drive->name);
  240. return;
  241. }
  242. }
  243. #if CY82C693_DEBUG_LOGS
  244. /* for debug let's show the register values */
  245. if (drive->select.b.unit == 0) {
  246. /*
  247. * get master drive registers
  248. * address setup control register
  249. * is 32 bit !!!
  250. */
  251. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  252. addrCtrl &= 0x0F;
  253. /* now let's get the remaining registers */
  254. pci_read_config_byte(dev, CY82_IDE_MASTER_IOR, &pclk.time_16r);
  255. pci_read_config_byte(dev, CY82_IDE_MASTER_IOW, &pclk.time_16w);
  256. pci_read_config_byte(dev, CY82_IDE_MASTER_8BIT, &pclk.time_8);
  257. } else {
  258. /*
  259. * set slave drive registers
  260. * address setup control register
  261. * is 32 bit !!!
  262. */
  263. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  264. addrCtrl &= 0xF0;
  265. addrCtrl >>= 4;
  266. /* now let's get the remaining registers */
  267. pci_read_config_byte(dev, CY82_IDE_SLAVE_IOR, &pclk.time_16r);
  268. pci_read_config_byte(dev, CY82_IDE_SLAVE_IOW, &pclk.time_16w);
  269. pci_read_config_byte(dev, CY82_IDE_SLAVE_8BIT, &pclk.time_8);
  270. }
  271. printk(KERN_INFO "%s (ch=%d, dev=%d): PIO timing is "
  272. "(addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)\n",
  273. drive->name, hwif->channel, drive->select.b.unit,
  274. addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
  275. #endif /* CY82C693_DEBUG_LOGS */
  276. /* first let's calc the pio modes */
  277. pio = ide_get_best_pio_mode(drive, pio, CY82C693_MAX_PIO, NULL);
  278. #if CY82C693_DEBUG_INFO
  279. printk (KERN_INFO "%s: Selected PIO mode %d\n", drive->name, pio);
  280. #endif /* CY82C693_DEBUG_INFO */
  281. /* let's calc the values for this PIO mode */
  282. compute_clocks(pio, &pclk);
  283. /* now let's write the clocks registers */
  284. if (drive->select.b.unit == 0) {
  285. /*
  286. * set master drive
  287. * address setup control register
  288. * is 32 bit !!!
  289. */
  290. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  291. addrCtrl &= (~0xF);
  292. addrCtrl |= (unsigned int)pclk.address_time;
  293. pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
  294. /* now let's set the remaining registers */
  295. pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, pclk.time_16r);
  296. pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, pclk.time_16w);
  297. pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, pclk.time_8);
  298. addrCtrl &= 0xF;
  299. } else {
  300. /*
  301. * set slave drive
  302. * address setup control register
  303. * is 32 bit !!!
  304. */
  305. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  306. addrCtrl &= (~0xF0);
  307. addrCtrl |= ((unsigned int)pclk.address_time<<4);
  308. pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
  309. /* now let's set the remaining registers */
  310. pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, pclk.time_16r);
  311. pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, pclk.time_16w);
  312. pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, pclk.time_8);
  313. addrCtrl >>= 4;
  314. addrCtrl &= 0xF;
  315. }
  316. #if CY82C693_DEBUG_INFO
  317. printk(KERN_INFO "%s (ch=%d, dev=%d): set PIO timing to "
  318. "(addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)\n",
  319. drive->name, hwif->channel, drive->select.b.unit,
  320. addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
  321. #endif /* CY82C693_DEBUG_INFO */
  322. }
  323. /*
  324. * this function is called during init and is used to setup the cy82c693 chip
  325. */
  326. static unsigned int __devinit init_chipset_cy82c693(struct pci_dev *dev, const char *name)
  327. {
  328. if (PCI_FUNC(dev->devfn) != 1)
  329. return 0;
  330. #ifdef CY82C693_SETDMA_CLOCK
  331. u8 data = 0;
  332. #endif /* CY82C693_SETDMA_CLOCK */
  333. /* write info about this verion of the driver */
  334. printk(KERN_INFO CY82_VERSION "\n");
  335. #ifdef CY82C693_SETDMA_CLOCK
  336. /* okay let's set the DMA clock speed */
  337. outb(CY82_INDEX_CTRLREG1, CY82_INDEX_PORT);
  338. data = inb(CY82_DATA_PORT);
  339. #if CY82C693_DEBUG_INFO
  340. printk(KERN_INFO "%s: Peripheral Configuration Register: 0x%X\n",
  341. name, data);
  342. #endif /* CY82C693_DEBUG_INFO */
  343. /*
  344. * for some reason sometimes the DMA controller
  345. * speed is set to ATCLK/2 ???? - we fix this here
  346. *
  347. * note: i don't know what causes this strange behaviour,
  348. * but even changing the dma speed doesn't solve it :-(
  349. * the ide performance is still only half the normal speed
  350. *
  351. * if anybody knows what goes wrong with my machine, please
  352. * let me know - ASK
  353. */
  354. data |= 0x03;
  355. outb(CY82_INDEX_CTRLREG1, CY82_INDEX_PORT);
  356. outb(data, CY82_DATA_PORT);
  357. #if CY82C693_DEBUG_INFO
  358. printk (KERN_INFO "%s: New Peripheral Configuration Register: 0x%X\n",
  359. name, data);
  360. #endif /* CY82C693_DEBUG_INFO */
  361. #endif /* CY82C693_SETDMA_CLOCK */
  362. return 0;
  363. }
  364. /*
  365. * the init function - called for each ide channel once
  366. */
  367. static void __devinit init_hwif_cy82c693(ide_hwif_t *hwif)
  368. {
  369. hwif->autodma = 0;
  370. hwif->chipset = ide_cy82c693;
  371. hwif->tuneproc = &cy82c693_tune_drive;
  372. if (!hwif->dma_base) {
  373. hwif->drives[0].autotune = 1;
  374. hwif->drives[1].autotune = 1;
  375. return;
  376. }
  377. hwif->atapi_dma = 1;
  378. hwif->mwdma_mask = 0x04;
  379. hwif->swdma_mask = 0x04;
  380. hwif->ide_dma_on = &cy82c693_ide_dma_on;
  381. if (!noautodma)
  382. hwif->autodma = 1;
  383. hwif->drives[0].autodma = hwif->autodma;
  384. hwif->drives[1].autodma = hwif->autodma;
  385. }
  386. static __devinitdata ide_hwif_t *primary;
  387. void __devinit init_iops_cy82c693(ide_hwif_t *hwif)
  388. {
  389. if (PCI_FUNC(hwif->pci_dev->devfn) == 1)
  390. primary = hwif;
  391. else {
  392. hwif->mate = primary;
  393. hwif->channel = 1;
  394. }
  395. }
  396. static ide_pci_device_t cy82c693_chipsets[] __devinitdata = {
  397. { /* 0 */
  398. .name = "CY82C693",
  399. .init_chipset = init_chipset_cy82c693,
  400. .init_iops = init_iops_cy82c693,
  401. .init_hwif = init_hwif_cy82c693,
  402. .channels = 1,
  403. .autodma = AUTODMA,
  404. .bootable = ON_BOARD,
  405. }
  406. };
  407. static int __devinit cy82c693_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  408. {
  409. ide_pci_device_t *d = &cy82c693_chipsets[id->driver_data];
  410. struct pci_dev *dev2;
  411. int ret = -ENODEV;
  412. /* CY82C693 is more than only a IDE controller.
  413. Function 1 is primary IDE channel, function 2 - secondary. */
  414. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
  415. PCI_FUNC(dev->devfn) == 1) {
  416. dev2 = pci_find_slot(dev->bus->number, dev->devfn + 1);
  417. ret = ide_setup_pci_devices(dev, dev2, d);
  418. }
  419. return ret;
  420. }
  421. static struct pci_device_id cy82c693_pci_tbl[] = {
  422. { PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  423. { 0, },
  424. };
  425. MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
  426. static struct pci_driver driver = {
  427. .name = "Cypress_IDE",
  428. .id_table = cy82c693_pci_tbl,
  429. .probe = cy82c693_init_one,
  430. };
  431. static int cy82c693_ide_init(void)
  432. {
  433. return ide_pci_register_driver(&driver);
  434. }
  435. module_init(cy82c693_ide_init);
  436. MODULE_AUTHOR("Andreas Krebs, Andre Hedrick");
  437. MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
  438. MODULE_LICENSE("GPL");