cy82c693.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/ide/cy82c693.c Version 0.34 Dec. 13, 1999
  3.  *
  4.  *  Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer
  5.  *  Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>, Integrater
  6.  *
  7.  * CYPRESS CY82C693 chipset IDE controller
  8.  *
  9.  * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
  10.  * Writting 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.  *
  33.  * History:
  34.  * AMH@1999-08-24: v0.34 init_cy82c693_chip moved to pci_init_cy82c693
  35.  * ASK@1999-01-23: v0.33 made a few minor code clean ups
  36.  *                       removed DMA clock speed setting by default
  37.  *                       added boot message
  38.  * ASK@1998-11-01: v0.32 added support to set BusMaster IDE TimeOut
  39.  *                       added support to set DMA Controller Clock Speed
  40.  * ASK@1998-10-31: v0.31 fixed problem with setting to high DMA modes on some drive
  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/types.h>
  48. #include <linux/pci.h>
  49. #include <linux/delay.h>
  50. #include <linux/ide.h>
  51. #include <linux/init.h>
  52. #include <asm/io.h>
  53. #include "ide_modes.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 trial and error !
  65.  *       using a to low value will cause DMA timeouts and drop IDE performance
  66.  *       using a to high value will cause audio playback to scatter
  67.  *       if you know a better value or how to calc it, please let me know 
  68.  */
  69. #define BUSMASTER_TIMEOUT 0x50 /* twice the value written in cy82c693ub datasheet */
  70. /*
  71.  * the value above was tested on my machine and it seems to work okay
  72.  */
  73. /* here are the offset definitions for the registers */
  74. #define CY82_IDE_CMDREG 0x04
  75. #define CY82_IDE_ADDRSETUP 0x48
  76. #define CY82_IDE_MASTER_IOR 0x4C
  77. #define CY82_IDE_MASTER_IOW 0x4D
  78. #define CY82_IDE_SLAVE_IOR 0x4E
  79. #define CY82_IDE_SLAVE_IOW 0x4F
  80. #define CY82_IDE_MASTER_8BIT 0x50
  81. #define CY82_IDE_SLAVE_8BIT 0x51
  82. #define CY82_INDEX_PORT 0x22
  83. #define CY82_DATA_PORT 0x23
  84. #define CY82_INDEX_CTRLREG1 0x01
  85. #define CY82_INDEX_CHANNEL0 0x30
  86. #define CY82_INDEX_CHANNEL1 0x31
  87. #define CY82_INDEX_TIMEOUT 0x32
  88. /* the max PIO mode - from datasheet */
  89. #define CY82C693_MAX_PIO 4
  90. /* the min and max PCI bus speed in MHz - from datasheet */
  91. #define CY82C963_MIN_BUS_SPEED 25
  92. #define CY82C963_MAX_BUS_SPEED 33
  93. /* the struct for the PIO mode timings */
  94. typedef struct pio_clocks_s {
  95.         byte address_time; /* Address setup (clocks) */
  96. byte time_16r; /* clocks for 16bit IOR (0xF0=Active/data, 0x0F=Recovery) */
  97. byte time_16w; /* clocks for 16bit IOW (0xF0=Active/data, 0x0F=Recovery) */
  98. byte time_8; /* clocks for 8bit (0xF0=Active/data, 0x0F=Recovery) */
  99. } pio_clocks_t;
  100. /*
  101.  * calc clocks using bus_speed
  102.  * returns (rounded up) time in bus clocks for time in ns
  103.  */
  104. static int calc_clk (int time, int bus_speed)
  105. {
  106. int clocks;
  107. clocks = (time*bus_speed+999)/1000 -1;
  108. if (clocks < 0)
  109. clocks = 0;
  110. if (clocks > 0x0F)
  111. clocks = 0x0F;
  112. return clocks;
  113. }
  114. /*
  115.  * compute the values for the clock registers for PIO
  116.  * mode and pci_clk [MHz] speed
  117.  *
  118.  * NOTE: for mode 0,1 and 2 drives 8-bit IDE command control registers are used
  119.  *       for mode 3 and 4 drives 8 and 16-bit timings are the same
  120.  *
  121.  */ 
  122. static void compute_clocks (byte pio, pio_clocks_t *p_pclk)
  123. {
  124. int clk1, clk2;
  125. int bus_speed = system_bus_clock(); /* get speed of PCI bus */
  126. /* we don't check against CY82C693's min and max speed,
  127.  * so you can play with the idebus=xx parameter
  128.  */
  129. if (pio > CY82C693_MAX_PIO)
  130. pio = CY82C693_MAX_PIO;
  131. /* let's calc the address setup time clocks */
  132. p_pclk->address_time = (byte)calc_clk(ide_pio_timings[pio].setup_time, bus_speed);
  133. /* let's calc the active and recovery time clocks */
  134. clk1 = calc_clk(ide_pio_timings[pio].active_time, bus_speed);
  135. /* calc recovery timing */
  136. clk2 = ide_pio_timings[pio].cycle_time -
  137. ide_pio_timings[pio].active_time -
  138. ide_pio_timings[pio].setup_time;
  139. clk2 = calc_clk(clk2, bus_speed);
  140. clk1 = (clk1<<4)|clk2; /* combine active and recovery clocks */
  141. /* note: we use the same values for 16bit IOR and IOW
  142.          * those are all the same, since I don't have other
  143.  * timings than those from ide_modes.h
  144.  */
  145. p_pclk->time_16r = (byte)clk1;
  146. p_pclk->time_16w = (byte)clk1;
  147. /* what are good values for 8bit ?? */
  148. p_pclk->time_8 = (byte)clk1;
  149. }
  150. #ifdef CONFIG_BLK_DEV_IDEDMA
  151. /*
  152.  * set DMA mode a specific channel for CY82C693
  153.  */
  154. static void cy82c693_dma_enable (ide_drive_t *drive, int mode, int single)
  155. {
  156.         byte index;
  157. byte data;
  158.         if (mode>2) /* make sure we set a valid mode */
  159. mode = 2;
  160.    
  161. if (mode > drive->id->tDMA)  /* to be absolutly sure we have a valid mode */
  162. mode = drive->id->tDMA;
  163.         index = (HWIF(drive)->channel==0) ? CY82_INDEX_CHANNEL0 : CY82_INDEX_CHANNEL1;
  164. #if CY82C693_DEBUG_LOGS
  165.         /* for debug let's show the previous values */
  166. OUT_BYTE(index, CY82_INDEX_PORT);
  167. data = IN_BYTE(CY82_DATA_PORT);
  168. printk (KERN_INFO "%s (ch=%d, dev=%d): DMA mode is %d (single=%d)n", drive->name, HWIF(drive)->channel, drive->select.b.unit, (data&0x3), ((data>>2)&1));
  169. #endif /* CY82C693_DEBUG_LOGS */
  170. data = (byte)mode|(byte)(single<<2);
  171. OUT_BYTE(index, CY82_INDEX_PORT);
  172. OUT_BYTE(data, CY82_DATA_PORT);
  173. #if CY82C693_DEBUG_INFO
  174. printk (KERN_INFO "%s (ch=%d, dev=%d): set DMA mode to %d (single=%d)n", drive->name, HWIF(drive)->channel, drive->select.b.unit, mode, single);
  175. #endif /* CY82C693_DEBUG_INFO */
  176. /* 
  177.  * note: below we set the value for Bus Master IDE TimeOut Register
  178.  * I'm not absolutly sure what this does, but it solved my problem
  179.  * with IDE DMA and sound, so I now can play sound and work with
  180.  * my IDE driver at the same time :-)
  181.  *
  182.  * If you know the correct (best) value for this register please
  183.  * let me know - ASK
  184.  */
  185. data = BUSMASTER_TIMEOUT;
  186. OUT_BYTE(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
  187. OUT_BYTE(data, CY82_DATA_PORT);
  188. #if CY82C693_DEBUG_INFO
  189. printk (KERN_INFO "%s: Set IDE Bus Master TimeOut Register to 0x%Xn", drive->name, data);
  190. #endif /* CY82C693_DEBUG_INFO */
  191. }
  192. /* 
  193.  * used to set DMA mode for CY82C693 (single and multi modes)
  194.  */
  195. static int cy82c693_dmaproc(ide_dma_action_t func, ide_drive_t *drive)
  196. {
  197. /*
  198.  * if the function is dma on, set dma mode for drive everything
  199.  * else is done by the defaul func
  200.  */
  201. if (func == ide_dma_on) {
  202. struct hd_driveid *id = drive->id;
  203. #if CY82C693_DEBUG_INFO
  204. printk (KERN_INFO "dma_on: %sn", drive->name);
  205. #endif /* CY82C693_DEBUG_INFO */
  206. if (id != NULL) {
  207.                        /* Enable DMA on any drive that has DMA (multi or single) enabled */
  208.                        if (id->field_valid & 2) {       /* regular DMA */
  209.        int mmode, smode;
  210.        mmode = id->dma_mword & (id->dma_mword >> 8);
  211.        smode = id->dma_1word & (id->dma_1word >> 8);
  212.               
  213.                if (mmode != 0)
  214.      cy82c693_dma_enable(drive, (mmode >> 1), 0); /* enable multi */
  215.        else if (smode != 0)
  216.      cy82c693_dma_enable(drive, (smode >> 1), 1); /* enable single */
  217. }
  218. }
  219. }
  220.         return ide_dmaproc(func, drive);
  221. }
  222. #endif /* CONFIG_BLK_DEV_IDEDMA */
  223. /*
  224.  * tune ide drive - set PIO mode
  225.  */
  226. static void cy82c693_tune_drive (ide_drive_t *drive, byte pio)
  227. {
  228. ide_hwif_t *hwif = HWIF(drive);
  229. struct pci_dev *dev = hwif->pci_dev;
  230. pio_clocks_t pclk;
  231. unsigned int addrCtrl;
  232. /* select primary or secondary channel */
  233. if (hwif->index > 0) {  /* drive is on the secondary channel */
  234. dev = pci_find_slot(dev->bus->number, dev->devfn+1);
  235. if (!dev) {
  236. printk(KERN_ERR "%s: tune_drive: Cannot find secondary interface!n", drive->name);
  237. return;
  238. }
  239. }
  240. #if CY82C693_DEBUG_LOGS
  241. /* for debug let's show the register values */
  242.         if (drive->select.b.unit == 0) {
  243. /*
  244.  * get master drive registers               
  245.  * address setup control register
  246.  * is 32 bit !!!
  247.  */ 
  248.    pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);                
  249. addrCtrl &= 0x0F;
  250. /* now let's get the remaining registers */
  251. pci_read_config_byte(dev, CY82_IDE_MASTER_IOR, &pclk.time_16r);
  252. pci_read_config_byte(dev, CY82_IDE_MASTER_IOW, &pclk.time_16w);
  253. pci_read_config_byte(dev, CY82_IDE_MASTER_8BIT, &pclk.time_8);
  254. } else {
  255. /*
  256.  * set slave drive registers
  257.  * address setup control register
  258.  * is 32 bit !!!
  259.  */ 
  260. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  261. addrCtrl &= 0xF0;
  262. addrCtrl >>= 4;
  263. /* now let's get the remaining registers */
  264. pci_read_config_byte(dev, CY82_IDE_SLAVE_IOR, &pclk.time_16r);
  265. pci_read_config_byte(dev, CY82_IDE_SLAVE_IOW, &pclk.time_16w);
  266. pci_read_config_byte(dev, CY82_IDE_SLAVE_8BIT, &pclk.time_8);
  267. }
  268. printk (KERN_INFO "%s (ch=%d, dev=%d): PIO timing is (addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)n", drive->name, hwif->channel, drive->select.b.unit, addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
  269. #endif /* CY82C693_DEBUG_LOGS */
  270.         /* first let's calc the pio modes */
  271. pio = ide_get_best_pio_mode(drive, pio, CY82C693_MAX_PIO, NULL);
  272. #if CY82C693_DEBUG_INFO
  273. printk (KERN_INFO "%s: Selected PIO mode %dn", drive->name, pio);
  274. #endif /* CY82C693_DEBUG_INFO */
  275.         compute_clocks(pio, &pclk);  /* let's calc the values for this PIO mode */
  276. /* now let's write  the clocks registers */
  277. if (drive->select.b.unit == 0) {
  278. /*
  279.  * set master drive
  280.  * address setup control register
  281.  * is 32 bit !!!
  282.  */ 
  283. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  284. addrCtrl &= (~0xF);
  285. addrCtrl |= (unsigned int)pclk.address_time;
  286. pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
  287. /* now let's set the remaining registers */
  288. pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, pclk.time_16r);
  289. pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, pclk.time_16w);
  290. pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, pclk.time_8);
  291. addrCtrl &= 0xF;
  292. } else {
  293. /*
  294.  * set slave drive
  295.  * address setup control register
  296.  * is 32 bit !!!
  297.  */ 
  298. pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
  299. addrCtrl &= (~0xF0);
  300. addrCtrl |= ((unsigned int)pclk.address_time<<4);
  301. pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
  302. /* now let's set the remaining registers */
  303. pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, pclk.time_16r);
  304. pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, pclk.time_16w);
  305. pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, pclk.time_8);
  306. addrCtrl >>= 4;
  307. addrCtrl &= 0xF;
  308. }
  309. #if CY82C693_DEBUG_INFO
  310. printk (KERN_INFO "%s (ch=%d, dev=%d): set PIO timing to (addr=0x%X, ior=0x%X, iow=0x%X, 8bit=0x%X)n", drive->name, hwif->channel, drive->select.b.unit, addrCtrl, pclk.time_16r, pclk.time_16w, pclk.time_8);
  311. #endif /* CY82C693_DEBUG_INFO */
  312. }
  313. /*
  314.  * this function is called during init and is used to setup the cy82c693 chip
  315.  */
  316. /*
  317.  * FIXME! "pci_init_cy82c693" really should replace
  318.  * the "init_cy82c693_chip", it is the correct location to tinker/setup
  319.  * the device prior to INIT.
  320.  */
  321. unsigned int __init pci_init_cy82c693(struct pci_dev *dev, const char *name)
  322. {
  323. #ifdef CY82C693_SETDMA_CLOCK
  324.         byte data;
  325. #endif /* CY82C693_SETDMA_CLOCK */ 
  326. /* write info about this verion of the driver */
  327. printk (KERN_INFO CY82_VERSION "n");
  328. #ifdef CY82C693_SETDMA_CLOCK
  329.        /* okay let's set the DMA clock speed */
  330.         
  331.         OUT_BYTE(CY82_INDEX_CTRLREG1, CY82_INDEX_PORT);
  332.         data = IN_BYTE(CY82_DATA_PORT);
  333. #if CY82C693_DEBUG_INFO
  334. printk (KERN_INFO "%s: Peripheral Configuration Register: 0x%Xn", name, data);
  335. #endif /* CY82C693_DEBUG_INFO */
  336.         /*
  337.  * for some reason sometimes the DMA controller
  338.  * speed is set to ATCLK/2 ???? - we fix this here
  339.  * 
  340.  * note: i don't know what causes this strange behaviour,
  341.  *       but even changing the dma speed doesn't solve it :-(
  342.  *       the ide performance is still only half the normal speed 
  343.  * 
  344.  *       if anybody knows what goes wrong with my machine, please
  345.  *       let me know - ASK
  346.          */
  347. data |= 0x03;
  348.         OUT_BYTE(CY82_INDEX_CTRLREG1, CY82_INDEX_PORT);
  349.         OUT_BYTE(data, CY82_DATA_PORT);
  350. #if CY82C693_DEBUG_INFO
  351. printk (KERN_INFO "%s: New Peripheral Configuration Register: 0x%Xn", name, data);
  352. #endif /* CY82C693_DEBUG_INFO */
  353. #endif /* CY82C693_SETDMA_CLOCK */
  354. return 0;
  355. }
  356. /*
  357.  * the init function - called for each ide channel once
  358.  */
  359. void __init ide_init_cy82c693(ide_hwif_t *hwif)
  360. {
  361. hwif->chipset = ide_cy82c693;
  362. hwif->tuneproc = &cy82c693_tune_drive;
  363. hwif->drives[0].autotune = 1;
  364. hwif->drives[1].autotune = 1;
  365. hwif->autodma = 0;
  366. #ifdef CONFIG_BLK_DEV_IDEDMA
  367. if (hwif->dma_base) {
  368. hwif->dmaproc = &cy82c693_dmaproc;
  369. if (!noautodma)
  370. hwif->autodma = 1;
  371. }
  372. #endif /* CONFIG_BLK_DEV_IDEDMA */
  373. }