opti621.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * linux/drivers/ide/pci/opti621.c Version 0.7 Sept 10, 2002
  3. *
  4. * Copyright (C) 1996-1998 Linus Torvalds & authors (see below)
  5. */
  6. /*
  7. * Authors:
  8. * Jaromir Koutek <miri@punknet.cz>,
  9. * Jan Harkes <jaharkes@cwi.nl>,
  10. * Mark Lord <mlord@pobox.com>
  11. * Some parts of code are from ali14xx.c and from rz1000.c.
  12. *
  13. * OPTi is trademark of OPTi, Octek is trademark of Octek.
  14. *
  15. * I used docs from OPTi databook, from ftp.opti.com, file 9123-0002.ps
  16. * and disassembled/traced setupvic.exe (DOS program).
  17. * It increases kernel code about 2 kB.
  18. * I don't have this card no more, but I hope I can get some in case
  19. * of needed development.
  20. * My card is Octek PIDE 1.01 (on card) or OPTiViC (program).
  21. * It has a place for a secondary connector in circuit, but nothing
  22. * is there. Also BIOS says no address for
  23. * secondary controller (see bellow in ide_init_opti621).
  24. * I've only tested this on my system, which only has one disk.
  25. * It's Western Digital WDAC2850, with PIO mode 3. The PCI bus
  26. * is at 20 MHz (I have DX2/80, I tried PCI at 40, but I got random
  27. * lockups). I tried the OCTEK double speed CD-ROM and
  28. * it does not work! But I can't boot DOS also, so it's probably
  29. * hardware fault. I have connected Conner 80MB, the Seagate 850MB (no
  30. * problems) and Seagate 1GB (as slave, WD as master). My experiences
  31. * with the third, 1GB drive: I got 3MB/s (hdparm), but sometimes
  32. * it slows to about 100kB/s! I don't know why and I have
  33. * not this drive now, so I can't try it again.
  34. * I write this driver because I lost the paper ("manual") with
  35. * settings of jumpers on the card and I have to boot Linux with
  36. * Loadlin except LILO, cause I have to run the setupvic.exe program
  37. * already or I get disk errors (my test: rpm -Vf
  38. * /usr/X11R6/bin/XF86_SVGA - or any big file).
  39. * Some numbers from hdparm -t /dev/hda:
  40. * Timing buffer-cache reads: 32 MB in 3.02 seconds =10.60 MB/sec
  41. * Timing buffered disk reads: 16 MB in 5.52 seconds = 2.90 MB/sec
  42. * I have 4 Megs/s before, but I don't know why (maybe changes
  43. * in hdparm test).
  44. * After release of 0.1, I got some successful reports, so it might work.
  45. *
  46. * The main problem with OPTi is that some timings for master
  47. * and slave must be the same. For example, if you have master
  48. * PIO 3 and slave PIO 0, driver have to set some timings of
  49. * master for PIO 0. Second problem is that opti621_tune_drive
  50. * got only one drive to set, but have to set both drives.
  51. * This is solved in compute_pios. If you don't set
  52. * the second drive, compute_pios use ide_get_best_pio_mode
  53. * for autoselect mode (you can change it to PIO 0, if you want).
  54. * If you then set the second drive to another PIO, the old value
  55. * (automatically selected) will be overrided by yours.
  56. * There is a 25/33MHz switch in configuration
  57. * register, but driver is written for use at any frequency which get
  58. * (use idebus=xx to select PCI bus speed).
  59. * Use ide0=autotune for automatical tune of the PIO modes.
  60. * If you get strange results, do not use this and set PIO manually
  61. * by hdparm.
  62. *
  63. * Version 0.1, Nov 8, 1996
  64. * by Jaromir Koutek, for 2.1.8.
  65. * Initial version of driver.
  66. *
  67. * Version 0.2
  68. * Number 0.2 skipped.
  69. *
  70. * Version 0.3, Nov 29, 1997
  71. * by Mark Lord (probably), for 2.1.68
  72. * Updates for use with new IDE block driver.
  73. *
  74. * Version 0.4, Dec 14, 1997
  75. * by Jan Harkes
  76. * Fixed some errors and cleaned the code.
  77. *
  78. * Version 0.5, Jan 2, 1998
  79. * by Jaromir Koutek
  80. * Updates for use with (again) new IDE block driver.
  81. * Update of documentation.
  82. *
  83. * Version 0.6, Jan 2, 1999
  84. * by Jaromir Koutek
  85. * Reversed to version 0.3 of the driver, because
  86. * 0.5 doesn't work.
  87. */
  88. #undef REALLY_SLOW_IO /* most systems can safely undef this */
  89. #define OPTI621_DEBUG /* define for debug messages */
  90. #include <linux/types.h>
  91. #include <linux/module.h>
  92. #include <linux/kernel.h>
  93. #include <linux/delay.h>
  94. #include <linux/timer.h>
  95. #include <linux/mm.h>
  96. #include <linux/ioport.h>
  97. #include <linux/blkdev.h>
  98. #include <linux/pci.h>
  99. #include <linux/hdreg.h>
  100. #include <linux/ide.h>
  101. #include <asm/io.h>
  102. #define OPTI621_MAX_PIO 3
  103. /* In fact, I do not have any PIO 4 drive
  104. * (address: 25 ns, data: 70 ns, recovery: 35 ns),
  105. * but OPTi 82C621 is programmable and it can do (minimal values):
  106. * on 40MHz PCI bus (pulse 25 ns):
  107. * address: 25 ns, data: 25 ns, recovery: 50 ns;
  108. * on 20MHz PCI bus (pulse 50 ns):
  109. * address: 50 ns, data: 50 ns, recovery: 100 ns.
  110. */
  111. /* #define READ_PREFETCH 0 */
  112. /* Uncomment for disable read prefetch.
  113. * There is some readprefetch capatibility in hdparm,
  114. * but when I type hdparm -P 1 /dev/hda, I got errors
  115. * and till reset drive is inaccessible.
  116. * This (hw) read prefetch is safe on my drive.
  117. */
  118. #ifndef READ_PREFETCH
  119. #define READ_PREFETCH 0x40 /* read prefetch is enabled */
  120. #endif /* else read prefetch is disabled */
  121. #define READ_REG 0 /* index of Read cycle timing register */
  122. #define WRITE_REG 1 /* index of Write cycle timing register */
  123. #define CNTRL_REG 3 /* index of Control register */
  124. #define STRAP_REG 5 /* index of Strap register */
  125. #define MISC_REG 6 /* index of Miscellaneous register */
  126. static int reg_base;
  127. #define PIO_NOT_EXIST 254
  128. #define PIO_DONT_KNOW 255
  129. /* there are stored pio numbers from other calls of opti621_tune_drive */
  130. static void compute_pios(ide_drive_t *drive, u8 pio)
  131. /* Store values into drive->drive_data
  132. * second_contr - 0 for primary controller, 1 for secondary
  133. * slave_drive - 0 -> pio is for master, 1 -> pio is for slave
  134. * pio - PIO mode for selected drive (for other we don't know)
  135. */
  136. {
  137. int d;
  138. ide_hwif_t *hwif = HWIF(drive);
  139. drive->drive_data = ide_get_best_pio_mode(drive, pio, OPTI621_MAX_PIO, NULL);
  140. for (d = 0; d < 2; ++d) {
  141. drive = &hwif->drives[d];
  142. if (drive->present) {
  143. if (drive->drive_data == PIO_DONT_KNOW)
  144. drive->drive_data = ide_get_best_pio_mode(drive, 255, OPTI621_MAX_PIO, NULL);
  145. #ifdef OPTI621_DEBUG
  146. printk("%s: Selected PIO mode %d\n",
  147. drive->name, drive->drive_data);
  148. #endif
  149. } else {
  150. drive->drive_data = PIO_NOT_EXIST;
  151. }
  152. }
  153. }
  154. static int cmpt_clk(int time, int bus_speed)
  155. /* Returns (rounded up) time in clocks for time in ns,
  156. * with bus_speed in MHz.
  157. * Example: bus_speed = 40 MHz, time = 80 ns
  158. * 1000/40 = 25 ns (clk value),
  159. * 80/25 = 3.2, rounded up to 4 (I hope ;-)).
  160. * Use idebus=xx to select right frequency.
  161. */
  162. {
  163. return ((time*bus_speed+999)/1000);
  164. }
  165. static void write_reg(ide_hwif_t *hwif, u8 value, int reg)
  166. /* Write value to register reg, base of register
  167. * is at reg_base (0x1f0 primary, 0x170 secondary,
  168. * if not changed by PCI configuration).
  169. * This is from setupvic.exe program.
  170. */
  171. {
  172. hwif->INW(reg_base+1);
  173. hwif->INW(reg_base+1);
  174. hwif->OUTB(3, reg_base+2);
  175. hwif->OUTB(value, reg_base+reg);
  176. hwif->OUTB(0x83, reg_base+2);
  177. }
  178. static u8 read_reg(ide_hwif_t *hwif, int reg)
  179. /* Read value from register reg, base of register
  180. * is at reg_base (0x1f0 primary, 0x170 secondary,
  181. * if not changed by PCI configuration).
  182. * This is from setupvic.exe program.
  183. */
  184. {
  185. u8 ret = 0;
  186. hwif->INW(reg_base+1);
  187. hwif->INW(reg_base+1);
  188. hwif->OUTB(3, reg_base+2);
  189. ret = hwif->INB(reg_base+reg);
  190. hwif->OUTB(0x83, reg_base+2);
  191. return ret;
  192. }
  193. typedef struct pio_clocks_s {
  194. int address_time; /* Address setup (clocks) */
  195. int data_time; /* Active/data pulse (clocks) */
  196. int recovery_time; /* Recovery time (clocks) */
  197. } pio_clocks_t;
  198. static void compute_clocks(int pio, pio_clocks_t *clks)
  199. {
  200. if (pio != PIO_NOT_EXIST) {
  201. int adr_setup, data_pls;
  202. int bus_speed = system_bus_clock();
  203. adr_setup = ide_pio_timings[pio].setup_time;
  204. data_pls = ide_pio_timings[pio].active_time;
  205. clks->address_time = cmpt_clk(adr_setup, bus_speed);
  206. clks->data_time = cmpt_clk(data_pls, bus_speed);
  207. clks->recovery_time = cmpt_clk(ide_pio_timings[pio].cycle_time
  208. - adr_setup-data_pls, bus_speed);
  209. if (clks->address_time<1) clks->address_time = 1;
  210. if (clks->address_time>4) clks->address_time = 4;
  211. if (clks->data_time<1) clks->data_time = 1;
  212. if (clks->data_time>16) clks->data_time = 16;
  213. if (clks->recovery_time<2) clks->recovery_time = 2;
  214. if (clks->recovery_time>17) clks->recovery_time = 17;
  215. } else {
  216. clks->address_time = 1;
  217. clks->data_time = 1;
  218. clks->recovery_time = 2;
  219. /* minimal values */
  220. }
  221. }
  222. /* Main tune procedure, called from tuneproc. */
  223. static void opti621_tune_drive (ide_drive_t *drive, u8 pio)
  224. {
  225. /* primary and secondary drives share some registers,
  226. * so we have to program both drives
  227. */
  228. unsigned long flags;
  229. u8 pio1 = 0, pio2 = 0;
  230. pio_clocks_t first, second;
  231. int ax, drdy;
  232. u8 cycle1, cycle2, misc;
  233. ide_hwif_t *hwif = HWIF(drive);
  234. /* sets drive->drive_data for both drives */
  235. compute_pios(drive, pio);
  236. pio1 = hwif->drives[0].drive_data;
  237. pio2 = hwif->drives[1].drive_data;
  238. compute_clocks(pio1, &first);
  239. compute_clocks(pio2, &second);
  240. /* ax = max(a1,a2) */
  241. ax = (first.address_time < second.address_time) ? second.address_time : first.address_time;
  242. drdy = 2; /* DRDY is default 2 (by OPTi Databook) */
  243. cycle1 = ((first.data_time-1)<<4) | (first.recovery_time-2);
  244. cycle2 = ((second.data_time-1)<<4) | (second.recovery_time-2);
  245. misc = READ_PREFETCH | ((ax-1)<<4) | ((drdy-2)<<1);
  246. #ifdef OPTI621_DEBUG
  247. printk("%s: master: address: %d, data: %d, "
  248. "recovery: %d, drdy: %d [clk]\n",
  249. hwif->name, ax, first.data_time,
  250. first.recovery_time, drdy);
  251. printk("%s: slave: address: %d, data: %d, "
  252. "recovery: %d, drdy: %d [clk]\n",
  253. hwif->name, ax, second.data_time,
  254. second.recovery_time, drdy);
  255. #endif
  256. spin_lock_irqsave(&ide_lock, flags);
  257. reg_base = hwif->io_ports[IDE_DATA_OFFSET];
  258. /* allow Register-B */
  259. hwif->OUTB(0xc0, reg_base+CNTRL_REG);
  260. /* hmm, setupvic.exe does this ;-) */
  261. hwif->OUTB(0xff, reg_base+5);
  262. /* if reads 0xff, adapter not exist? */
  263. (void) hwif->INB(reg_base+CNTRL_REG);
  264. /* if reads 0xc0, no interface exist? */
  265. read_reg(hwif, CNTRL_REG);
  266. /* read version, probably 0 */
  267. read_reg(hwif, STRAP_REG);
  268. /* program primary drive */
  269. /* select Index-0 for Register-A */
  270. write_reg(hwif, 0, MISC_REG);
  271. /* set read cycle timings */
  272. write_reg(hwif, cycle1, READ_REG);
  273. /* set write cycle timings */
  274. write_reg(hwif, cycle1, WRITE_REG);
  275. /* program secondary drive */
  276. /* select Index-1 for Register-B */
  277. write_reg(hwif, 1, MISC_REG);
  278. /* set read cycle timings */
  279. write_reg(hwif, cycle2, READ_REG);
  280. /* set write cycle timings */
  281. write_reg(hwif, cycle2, WRITE_REG);
  282. /* use Register-A for drive 0 */
  283. /* use Register-B for drive 1 */
  284. write_reg(hwif, 0x85, CNTRL_REG);
  285. /* set address setup, DRDY timings, */
  286. /* and read prefetch for both drives */
  287. write_reg(hwif, misc, MISC_REG);
  288. spin_unlock_irqrestore(&ide_lock, flags);
  289. }
  290. /*
  291. * init_hwif_opti621() is called once for each hwif found at boot.
  292. */
  293. static void __devinit init_hwif_opti621 (ide_hwif_t *hwif)
  294. {
  295. hwif->autodma = 0;
  296. hwif->drives[0].drive_data = PIO_DONT_KNOW;
  297. hwif->drives[1].drive_data = PIO_DONT_KNOW;
  298. hwif->tuneproc = &opti621_tune_drive;
  299. if (!(hwif->dma_base))
  300. return;
  301. hwif->atapi_dma = 1;
  302. hwif->mwdma_mask = 0x07;
  303. hwif->swdma_mask = 0x07;
  304. if (!noautodma)
  305. hwif->autodma = 1;
  306. hwif->drives[0].autodma = hwif->autodma;
  307. hwif->drives[1].autodma = hwif->autodma;
  308. }
  309. static ide_pci_device_t opti621_chipsets[] __devinitdata = {
  310. { /* 0 */
  311. .name = "OPTI621",
  312. .init_hwif = init_hwif_opti621,
  313. .channels = 2,
  314. .autodma = AUTODMA,
  315. .enablebits = {{0x45,0x80,0x00}, {0x40,0x08,0x00}},
  316. .bootable = ON_BOARD,
  317. },{ /* 1 */
  318. .name = "OPTI621X",
  319. .init_hwif = init_hwif_opti621,
  320. .channels = 2,
  321. .autodma = AUTODMA,
  322. .enablebits = {{0x45,0x80,0x00}, {0x40,0x08,0x00}},
  323. .bootable = ON_BOARD,
  324. }
  325. };
  326. static int __devinit opti621_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  327. {
  328. return ide_setup_pci_device(dev, &opti621_chipsets[id->driver_data]);
  329. }
  330. static struct pci_device_id opti621_pci_tbl[] = {
  331. { PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C621, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  332. { PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C825, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  333. { 0, },
  334. };
  335. MODULE_DEVICE_TABLE(pci, opti621_pci_tbl);
  336. static struct pci_driver driver = {
  337. .name = "Opti621_IDE",
  338. .id_table = opti621_pci_tbl,
  339. .probe = opti621_init_one,
  340. };
  341. static int opti621_ide_init(void)
  342. {
  343. return ide_pci_register_driver(&driver);
  344. }
  345. module_init(opti621_ide_init);
  346. MODULE_AUTHOR("Jaromir Koutek, Jan Harkes, Mark Lord");
  347. MODULE_DESCRIPTION("PCI driver module for Opti621 IDE");
  348. MODULE_LICENSE("GPL");