cy82c693.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #define DRV_NAME "cy82c693"
  49. /*
  50. * The following are used to debug the driver.
  51. */
  52. #define CY82C693_DEBUG_LOGS 0
  53. #define CY82C693_DEBUG_INFO 0
  54. /*
  55. * NOTE: the value for busmaster timeout is tricky and I got it by
  56. * trial and error! By using a to low value will cause DMA timeouts
  57. * and drop IDE performance, and by using a to high value will cause
  58. * audio playback to scatter.
  59. * If you know a better value or how to calc it, please let me know.
  60. */
  61. /* twice the value written in cy82c693ub datasheet */
  62. #define BUSMASTER_TIMEOUT 0x50
  63. /*
  64. * the value above was tested on my machine and it seems to work okay
  65. */
  66. /* here are the offset definitions for the registers */
  67. #define CY82_IDE_CMDREG 0x04
  68. #define CY82_IDE_ADDRSETUP 0x48
  69. #define CY82_IDE_MASTER_IOR 0x4C
  70. #define CY82_IDE_MASTER_IOW 0x4D
  71. #define CY82_IDE_SLAVE_IOR 0x4E
  72. #define CY82_IDE_SLAVE_IOW 0x4F
  73. #define CY82_IDE_MASTER_8BIT 0x50
  74. #define CY82_IDE_SLAVE_8BIT 0x51
  75. #define CY82_INDEX_PORT 0x22
  76. #define CY82_DATA_PORT 0x23
  77. #define CY82_INDEX_CHANNEL0 0x30
  78. #define CY82_INDEX_CHANNEL1 0x31
  79. #define CY82_INDEX_TIMEOUT 0x32
  80. /* the min and max PCI bus speed in MHz - from datasheet */
  81. #define CY82C963_MIN_BUS_SPEED 25
  82. #define CY82C963_MAX_BUS_SPEED 33
  83. /* the struct for the PIO mode timings */
  84. typedef struct pio_clocks_s {
  85. u8 address_time; /* Address setup (clocks) */
  86. u8 time_16r; /* clocks for 16bit IOR (0xF0=Active/data, 0x0F=Recovery) */
  87. u8 time_16w; /* clocks for 16bit IOW (0xF0=Active/data, 0x0F=Recovery) */
  88. u8 time_8; /* clocks for 8bit (0xF0=Active/data, 0x0F=Recovery) */
  89. } pio_clocks_t;
  90. /*
  91. * calc clocks using bus_speed
  92. * returns (rounded up) time in bus clocks for time in ns
  93. */
  94. static int calc_clk(int time, int bus_speed)
  95. {
  96. int clocks;
  97. clocks = (time*bus_speed+999)/1000 - 1;
  98. if (clocks < 0)
  99. clocks = 0;
  100. if (clocks > 0x0F)
  101. clocks = 0x0F;
  102. return clocks;
  103. }
  104. /*
  105. * compute the values for the clock registers for PIO
  106. * mode and pci_clk [MHz] speed
  107. *
  108. * NOTE: for mode 0,1 and 2 drives 8-bit IDE command control registers are used
  109. * for mode 3 and 4 drives 8 and 16-bit timings are the same
  110. *
  111. */
  112. static void compute_clocks(u8 pio, pio_clocks_t *p_pclk)
  113. {
  114. struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
  115. int clk1, clk2;
  116. int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
  117. /* we don't check against CY82C693's min and max speed,
  118. * so you can play with the idebus=xx parameter
  119. */
  120. /* let's calc the address setup time clocks */
  121. p_pclk->address_time = (u8)calc_clk(t->setup, bus_speed);
  122. /* let's calc the active and recovery time clocks */
  123. clk1 = calc_clk(t->active, bus_speed);
  124. /* calc recovery timing */
  125. clk2 = t->cycle - t->active - t->setup;
  126. clk2 = calc_clk(clk2, bus_speed);
  127. clk1 = (clk1<<4)|clk2; /* combine active and recovery clocks */
  128. /* note: we use the same values for 16bit IOR and IOW
  129. * those are all the same, since I don't have other
  130. * timings than those from ide-lib.c
  131. */
  132. p_pclk->time_16r = (u8)clk1;
  133. p_pclk->time_16w = (u8)clk1;
  134. /* what are good values for 8bit ?? */
  135. p_pclk->time_8 = (u8)clk1;
  136. }
  137. /*
  138. * set DMA mode a specific channel for CY82C693
  139. */
  140. static void cy82c693_set_dma_mode(ide_drive_t *drive, const u8 mode)
  141. {
  142. ide_hwif_t *hwif = drive->hwif;
  143. u8 single = (mode & 0x10) >> 4, index = 0, data = 0;
  144. index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0;
  145. #if CY82C693_DEBUG_LOGS
  146. /* for debug let's show the previous values */
  147. outb(index, CY82_INDEX_PORT);
  148. data = inb(CY82_DATA_PORT);
  149. printk(KERN_INFO "%s (ch=%d, dev=%d): DMA mode is %d (single=%d)\n",
  150. drive->name, HWIF(drive)->channel, drive->select.b.unit,
  151. (data&0x3), ((data>>2)&1));
  152. #endif /* CY82C693_DEBUG_LOGS */
  153. data = (mode & 3) | (single << 2);
  154. outb(index, CY82_INDEX_PORT);
  155. outb(data, CY82_DATA_PORT);
  156. #if CY82C693_DEBUG_INFO
  157. printk(KERN_INFO "%s (ch=%d, dev=%d): set DMA mode to %d (single=%d)\n",
  158. drive->name, HWIF(drive)->channel, drive->select.b.unit,
  159. mode & 3, single);
  160. #endif /* CY82C693_DEBUG_INFO */
  161. /*
  162. * note: below we set the value for Bus Master IDE TimeOut Register
  163. * I'm not absolutly sure what this does, but it solved my problem
  164. * with IDE DMA and sound, so I now can play sound and work with
  165. * my IDE driver at the same time :-)
  166. *
  167. * If you know the correct (best) value for this register please
  168. * let me know - ASK
  169. */
  170. data = BUSMASTER_TIMEOUT;
  171. outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
  172. outb(data, CY82_DATA_PORT);
  173. #if CY82C693_DEBUG_INFO
  174. printk(KERN_INFO "%s: Set IDE Bus Master TimeOut Register to 0x%X\n",
  175. drive->name, data);
  176. #endif /* CY82C693_DEBUG_INFO */
  177. }
  178. static void cy82c693_set_pio_mode(ide_drive_t *drive, const u8 pio)
  179. {
  180. ide_hwif_t *hwif = HWIF(drive);
  181. struct pci_dev *dev = to_pci_dev(hwif->dev);
  182. pio_clocks_t pclk;
  183. unsigned int addrCtrl;
  184. /* select primary or secondary channel */
  185. if (hwif->index > 0) { /* drive is on the secondary channel */
  186. dev = pci_get_slot(dev->bus, dev->devfn+1);
  187. if (!dev) {
  188. printk(KERN_ERR "%s: tune_drive: "
  189. "Cannot find secondary interface!\n",
  190. drive->name);
  191. return;
  192. }
  193. }
  194. #if CY82C693_DEBUG_LOGS
  195. /* for debug let's show the register values */
  196. if (drive->select.b.unit == 0) {
  197. /*
  198. * get master drive registers
  199. * address setup control register
  200. * is 32 bit !!!
  201. */
  202. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  203. addrCtrl &= 0x0F;
  204. /* now let's get the remaining registers */
  205. pci_read_config_byte(dev, CY82_IDE_MASTER_IOR, &pclk.time_16r);
  206. pci_read_config_byte(dev, CY82_IDE_MASTER_IOW, &pclk.time_16w);
  207. pci_read_config_byte(dev, CY82_IDE_MASTER_8BIT, &pclk.time_8);
  208. } else {
  209. /*
  210. * set slave drive registers
  211. * address setup control register
  212. * is 32 bit !!!
  213. */
  214. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  215. addrCtrl &= 0xF0;
  216. addrCtrl >>= 4;
  217. /* now let's get the remaining registers */
  218. pci_read_config_byte(dev, CY82_IDE_SLAVE_IOR, &pclk.time_16r);
  219. pci_read_config_byte(dev, CY82_IDE_SLAVE_IOW, &pclk.time_16w);
  220. pci_read_config_byte(dev, CY82_IDE_SLAVE_8BIT, &pclk.time_8);
  221. }
  222. printk(KERN_INFO "%s (ch=%d, dev=%d): PIO timing is "
  223. "(addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)\n",
  224. drive->name, hwif->channel, drive->select.b.unit,
  225. addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
  226. #endif /* CY82C693_DEBUG_LOGS */
  227. /* let's calc the values for this PIO mode */
  228. compute_clocks(pio, &pclk);
  229. /* now let's write the clocks registers */
  230. if (drive->select.b.unit == 0) {
  231. /*
  232. * set master drive
  233. * address setup control register
  234. * is 32 bit !!!
  235. */
  236. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  237. addrCtrl &= (~0xF);
  238. addrCtrl |= (unsigned int)pclk.address_time;
  239. pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
  240. /* now let's set the remaining registers */
  241. pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, pclk.time_16r);
  242. pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, pclk.time_16w);
  243. pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, pclk.time_8);
  244. addrCtrl &= 0xF;
  245. } else {
  246. /*
  247. * set slave drive
  248. * address setup control register
  249. * is 32 bit !!!
  250. */
  251. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  252. addrCtrl &= (~0xF0);
  253. addrCtrl |= ((unsigned int)pclk.address_time<<4);
  254. pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
  255. /* now let's set the remaining registers */
  256. pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, pclk.time_16r);
  257. pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, pclk.time_16w);
  258. pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, pclk.time_8);
  259. addrCtrl >>= 4;
  260. addrCtrl &= 0xF;
  261. }
  262. #if CY82C693_DEBUG_INFO
  263. printk(KERN_INFO "%s (ch=%d, dev=%d): set PIO timing to "
  264. "(addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)\n",
  265. drive->name, hwif->channel, drive->select.b.unit,
  266. addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
  267. #endif /* CY82C693_DEBUG_INFO */
  268. }
  269. static void __devinit init_iops_cy82c693(ide_hwif_t *hwif)
  270. {
  271. static ide_hwif_t *primary;
  272. struct pci_dev *dev = to_pci_dev(hwif->dev);
  273. if (PCI_FUNC(dev->devfn) == 1)
  274. primary = hwif;
  275. else {
  276. hwif->mate = primary;
  277. hwif->channel = 1;
  278. }
  279. }
  280. static const struct ide_port_ops cy82c693_port_ops = {
  281. .set_pio_mode = cy82c693_set_pio_mode,
  282. .set_dma_mode = cy82c693_set_dma_mode,
  283. };
  284. static const struct ide_port_info cy82c693_chipset __devinitdata = {
  285. .name = DRV_NAME,
  286. .init_iops = init_iops_cy82c693,
  287. .port_ops = &cy82c693_port_ops,
  288. .chipset = ide_cy82c693,
  289. .host_flags = IDE_HFLAG_SINGLE,
  290. .pio_mask = ATA_PIO4,
  291. .swdma_mask = ATA_SWDMA2,
  292. .mwdma_mask = ATA_MWDMA2,
  293. };
  294. static int __devinit cy82c693_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  295. {
  296. struct pci_dev *dev2;
  297. int ret = -ENODEV;
  298. /* CY82C693 is more than only a IDE controller.
  299. Function 1 is primary IDE channel, function 2 - secondary. */
  300. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
  301. PCI_FUNC(dev->devfn) == 1) {
  302. dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
  303. ret = ide_pci_init_two(dev, dev2, &cy82c693_chipset, NULL);
  304. if (ret)
  305. pci_dev_put(dev2);
  306. }
  307. return ret;
  308. }
  309. static void __devexit cy82c693_remove(struct pci_dev *dev)
  310. {
  311. struct ide_host *host = pci_get_drvdata(dev);
  312. struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
  313. ide_pci_remove(dev);
  314. pci_dev_put(dev2);
  315. }
  316. static const struct pci_device_id cy82c693_pci_tbl[] = {
  317. { PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 },
  318. { 0, },
  319. };
  320. MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
  321. static struct pci_driver driver = {
  322. .name = "Cypress_IDE",
  323. .id_table = cy82c693_pci_tbl,
  324. .probe = cy82c693_init_one,
  325. .remove = __devexit_p(cy82c693_remove),
  326. .suspend = ide_pci_suspend,
  327. .resume = ide_pci_resume,
  328. };
  329. static int __init cy82c693_ide_init(void)
  330. {
  331. return ide_pci_register_driver(&driver);
  332. }
  333. static void __exit cy82c693_ide_exit(void)
  334. {
  335. pci_unregister_driver(&driver);
  336. }
  337. module_init(cy82c693_ide_init);
  338. module_exit(cy82c693_ide_exit);
  339. MODULE_AUTHOR("Andreas Krebs, Andre Hedrick");
  340. MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
  341. MODULE_LICENSE("GPL");