cy82c693.c 13 KB

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