opti621.c 11 KB

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