Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1995-1996 Linus Torvalds & authors (see below) |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * Original authors: abramov@cecmow.enet.dec.com (Igor Abramov) |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 7 | * mlord@pobox.com (Mark Lord) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * See linux/MAINTAINERS for address of current maintainer. |
| 10 | * |
| 11 | * This file provides support for the advanced features and bugs |
| 12 | * of IDE interfaces using the CMD Technologies 0640 IDE interface chip. |
| 13 | * |
| 14 | * These chips are basically fucked by design, and getting this driver |
| 15 | * to work on every motherboard design that uses this screwed chip seems |
| 16 | * bloody well impossible. However, we're still trying. |
| 17 | * |
| 18 | * Version 0.97 worked for everybody. |
| 19 | * |
| 20 | * User feedback is essential. Many thanks to the beta test team: |
| 21 | * |
| 22 | * A.Hartgers@stud.tue.nl, JZDQC@CUNYVM.CUNY.edu, abramov@cecmow.enet.dec.com, |
| 23 | * bardj@utopia.ppp.sn.no, bart@gaga.tue.nl, bbol001@cs.auckland.ac.nz, |
| 24 | * chrisc@dbass.demon.co.uk, dalecki@namu26.Num.Math.Uni-Goettingen.de, |
| 25 | * derekn@vw.ece.cmu.edu, florian@btp2x3.phy.uni-bayreuth.de, |
| 26 | * flynn@dei.unipd.it, gadio@netvision.net.il, godzilla@futuris.net, |
| 27 | * j@pobox.com, jkemp1@mises.uni-paderborn.de, jtoppe@hiwaay.net, |
| 28 | * kerouac@ssnet.com, meskes@informatik.rwth-aachen.de, hzoli@cs.elte.hu, |
| 29 | * peter@udgaard.isgtec.com, phil@tazenda.demon.co.uk, roadcapw@cfw.com, |
| 30 | * s0033las@sun10.vsz.bme.hu, schaffer@tam.cornell.edu, sjd@slip.net, |
| 31 | * steve@ei.org, ulrpeg@bigcomm.gun.de, ism@tardis.ed.ac.uk, mack@cray.com |
| 32 | * liug@mama.indstate.edu, and others. |
| 33 | * |
| 34 | * Version 0.01 Initial version, hacked out of ide.c, |
| 35 | * and #include'd rather than compiled separately. |
| 36 | * This will get cleaned up in a subsequent release. |
| 37 | * |
| 38 | * Version 0.02 Fixes for vlb initialization code, enable prefetch |
| 39 | * for versions 'B' and 'C' of chip by default, |
| 40 | * some code cleanup. |
| 41 | * |
| 42 | * Version 0.03 Added reset of secondary interface, |
| 43 | * and black list for devices which are not compatible |
| 44 | * with prefetch mode. Separate function for setting |
| 45 | * prefetch is added, possibly it will be called some |
| 46 | * day from ioctl processing code. |
| 47 | * |
| 48 | * Version 0.04 Now configs/compiles separate from ide.c |
| 49 | * |
| 50 | * Version 0.05 Major rewrite of interface timing code. |
| 51 | * Added new function cmd640_set_mode to set PIO mode |
| 52 | * from ioctl call. New drives added to black list. |
| 53 | * |
| 54 | * Version 0.06 More code cleanup. Prefetch is enabled only for |
| 55 | * detected hard drives, not included in prefetch |
| 56 | * black list. |
| 57 | * |
| 58 | * Version 0.07 Changed to more conservative drive tuning policy. |
| 59 | * Unknown drives, which report PIO < 4 are set to |
| 60 | * (reported_PIO - 1) if it is supported, or to PIO0. |
| 61 | * List of known drives extended by info provided by |
| 62 | * CMD at their ftp site. |
| 63 | * |
| 64 | * Version 0.08 Added autotune/noautotune support. |
| 65 | * |
| 66 | * Version 0.09 Try to be smarter about 2nd port enabling. |
| 67 | * Version 0.10 Be nice and don't reset 2nd port. |
| 68 | * Version 0.11 Try to handle more weird situations. |
| 69 | * |
| 70 | * Version 0.12 Lots of bug fixes from Laszlo Peter |
| 71 | * irq unmasking disabled for reliability. |
| 72 | * try to be even smarter about the second port. |
| 73 | * tidy up source code formatting. |
| 74 | * Version 0.13 permit irq unmasking again. |
| 75 | * Version 0.90 massive code cleanup, some bugs fixed. |
| 76 | * defaults all drives to PIO mode0, prefetch off. |
| 77 | * autotune is OFF by default, with compile time flag. |
| 78 | * prefetch can be turned OFF/ON using "hdparm -p8/-p9" |
| 79 | * (requires hdparm-3.1 or newer) |
| 80 | * Version 0.91 first release to linux-kernel list. |
| 81 | * Version 0.92 move initial reg dump to separate callable function |
| 82 | * change "readahead" to "prefetch" to avoid confusion |
| 83 | * Version 0.95 respect original BIOS timings unless autotuning. |
| 84 | * tons of code cleanup and rearrangement. |
| 85 | * added CONFIG_BLK_DEV_CMD640_ENHANCED option |
| 86 | * prevent use of unmask when prefetch is on |
| 87 | * Version 0.96 prevent use of io_32bit when prefetch is off |
| 88 | * Version 0.97 fix VLB secondary interface for sjd@slip.net |
| 89 | * other minor tune-ups: 0.96 was very good. |
| 90 | * Version 0.98 ignore PCI version when disabled by BIOS |
| 91 | * Version 0.99 display setup/active/recovery clocks with PIO mode |
| 92 | * Version 1.00 Mmm.. cannot depend on PCMD_ENA in all systems |
| 93 | * Version 1.01 slow/fast devsel can be selected with "hdparm -p6/-p7" |
| 94 | * ("fast" is necessary for 32bit I/O in some systems) |
| 95 | * Version 1.02 fix bug that resulted in slow "setup times" |
| 96 | * (patch courtesy of Zoltan Hidvegi) |
| 97 | */ |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #define CMD640_PREFETCH_MASKS 1 |
| 100 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 101 | /*#define CMD640_DUMP_REGS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #include <linux/types.h> |
| 104 | #include <linux/kernel.h> |
| 105 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #include <linux/hdreg.h> |
| 107 | #include <linux/ide.h> |
| 108 | #include <linux/init.h> |
| 109 | |
| 110 | #include <asm/io.h> |
| 111 | |
| 112 | /* |
| 113 | * This flag is set in ide.c by the parameter: ide0=cmd640_vlb |
| 114 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 115 | int cmd640_vlb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * CMD640 specific registers definition. |
| 119 | */ |
| 120 | |
| 121 | #define VID 0x00 |
| 122 | #define DID 0x02 |
| 123 | #define PCMD 0x04 |
| 124 | #define PCMD_ENA 0x01 |
| 125 | #define PSTTS 0x06 |
| 126 | #define REVID 0x08 |
| 127 | #define PROGIF 0x09 |
| 128 | #define SUBCL 0x0a |
| 129 | #define BASCL 0x0b |
| 130 | #define BaseA0 0x10 |
| 131 | #define BaseA1 0x14 |
| 132 | #define BaseA2 0x18 |
| 133 | #define BaseA3 0x1c |
| 134 | #define INTLINE 0x3c |
| 135 | #define INPINE 0x3d |
| 136 | |
| 137 | #define CFR 0x50 |
| 138 | #define CFR_DEVREV 0x03 |
| 139 | #define CFR_IDE01INTR 0x04 |
| 140 | #define CFR_DEVID 0x18 |
| 141 | #define CFR_AT_VESA_078h 0x20 |
| 142 | #define CFR_DSA1 0x40 |
| 143 | #define CFR_DSA0 0x80 |
| 144 | |
| 145 | #define CNTRL 0x51 |
| 146 | #define CNTRL_DIS_RA0 0x40 |
| 147 | #define CNTRL_DIS_RA1 0x80 |
| 148 | #define CNTRL_ENA_2ND 0x08 |
| 149 | |
| 150 | #define CMDTIM 0x52 |
| 151 | #define ARTTIM0 0x53 |
| 152 | #define DRWTIM0 0x54 |
| 153 | #define ARTTIM1 0x55 |
| 154 | #define DRWTIM1 0x56 |
| 155 | #define ARTTIM23 0x57 |
| 156 | #define ARTTIM23_DIS_RA2 0x04 |
| 157 | #define ARTTIM23_DIS_RA3 0x08 |
| 158 | #define DRWTIM23 0x58 |
| 159 | #define BRST 0x59 |
| 160 | |
| 161 | /* |
| 162 | * Registers and masks for easy access by drive index: |
| 163 | */ |
| 164 | static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23}; |
| 165 | static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3}; |
| 166 | |
| 167 | #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED |
| 168 | |
| 169 | static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23}; |
| 170 | static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23}; |
| 171 | |
| 172 | /* |
| 173 | * Current cmd640 timing values for each drive. |
| 174 | * The defaults for each are the slowest possible timings. |
| 175 | */ |
| 176 | static u8 setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */ |
| 177 | static u8 active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */ |
| 178 | static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */ |
| 179 | |
| 180 | #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */ |
| 181 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 182 | static DEFINE_SPINLOCK(cmd640_lock); |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | /* |
| 185 | * These are initialized to point at the devices we control |
| 186 | */ |
| 187 | static ide_hwif_t *cmd_hwif0, *cmd_hwif1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * Interface to access cmd640x registers |
| 191 | */ |
| 192 | static unsigned int cmd640_key; |
| 193 | static void (*__put_cmd640_reg)(u16 reg, u8 val); |
| 194 | static u8 (*__get_cmd640_reg)(u16 reg); |
| 195 | |
| 196 | /* |
| 197 | * This is read from the CFR reg, and is used in several places. |
| 198 | */ |
| 199 | static unsigned int cmd640_chip_version; |
| 200 | |
| 201 | /* |
| 202 | * The CMD640x chip does not support DWORD config write cycles, but some |
| 203 | * of the BIOSes use them to implement the config services. |
| 204 | * Therefore, we must use direct IO instead. |
| 205 | */ |
| 206 | |
| 207 | /* PCI method 1 access */ |
| 208 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 209 | static void put_cmd640_reg_pci1(u16 reg, u8 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | outl_p((reg & 0xfc) | cmd640_key, 0xcf8); |
| 212 | outb_p(val, (reg & 3) | 0xcfc); |
| 213 | } |
| 214 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 215 | static u8 get_cmd640_reg_pci1(u16 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | outl_p((reg & 0xfc) | cmd640_key, 0xcf8); |
| 218 | return inb_p((reg & 3) | 0xcfc); |
| 219 | } |
| 220 | |
| 221 | /* PCI method 2 access (from CMD datasheet) */ |
| 222 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 223 | static void put_cmd640_reg_pci2(u16 reg, u8 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | outb_p(0x10, 0xcf8); |
| 226 | outb_p(val, cmd640_key + reg); |
| 227 | outb_p(0, 0xcf8); |
| 228 | } |
| 229 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 230 | static u8 get_cmd640_reg_pci2(u16 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | u8 b; |
| 233 | |
| 234 | outb_p(0x10, 0xcf8); |
| 235 | b = inb_p(cmd640_key + reg); |
| 236 | outb_p(0, 0xcf8); |
| 237 | return b; |
| 238 | } |
| 239 | |
| 240 | /* VLB access */ |
| 241 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 242 | static void put_cmd640_reg_vlb(u16 reg, u8 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
| 244 | outb_p(reg, cmd640_key); |
| 245 | outb_p(val, cmd640_key + 4); |
| 246 | } |
| 247 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 248 | static u8 get_cmd640_reg_vlb(u16 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | outb_p(reg, cmd640_key); |
| 251 | return inb_p(cmd640_key + 4); |
| 252 | } |
| 253 | |
| 254 | static u8 get_cmd640_reg(u16 reg) |
| 255 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | unsigned long flags; |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 257 | u8 b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 259 | spin_lock_irqsave(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | b = __get_cmd640_reg(reg); |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 261 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return b; |
| 263 | } |
| 264 | |
| 265 | static void put_cmd640_reg(u16 reg, u8 val) |
| 266 | { |
| 267 | unsigned long flags; |
| 268 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 269 | spin_lock_irqsave(&cmd640_lock, flags); |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 270 | __put_cmd640_reg(reg, val); |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 271 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 274 | static int __init match_pci_cmd640_device(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06}; |
| 277 | unsigned int i; |
| 278 | for (i = 0; i < 4; i++) { |
| 279 | if (get_cmd640_reg(i) != ven_dev[i]) |
| 280 | return 0; |
| 281 | } |
| 282 | #ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT |
| 283 | if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) { |
| 284 | printk("ide: cmd640 on PCI disabled by BIOS\n"); |
| 285 | return 0; |
| 286 | } |
| 287 | #endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */ |
| 288 | return 1; /* success */ |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Probe for CMD640x -- pci method 1 |
| 293 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 294 | static int __init probe_for_cmd640_pci1(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
| 296 | __get_cmd640_reg = get_cmd640_reg_pci1; |
| 297 | __put_cmd640_reg = put_cmd640_reg_pci1; |
| 298 | for (cmd640_key = 0x80000000; |
| 299 | cmd640_key <= 0x8000f800; |
| 300 | cmd640_key += 0x800) { |
| 301 | if (match_pci_cmd640_device()) |
| 302 | return 1; /* success */ |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Probe for CMD640x -- pci method 2 |
| 309 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 310 | static int __init probe_for_cmd640_pci2(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
| 312 | __get_cmd640_reg = get_cmd640_reg_pci2; |
| 313 | __put_cmd640_reg = put_cmd640_reg_pci2; |
| 314 | for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) { |
| 315 | if (match_pci_cmd640_device()) |
| 316 | return 1; /* success */ |
| 317 | } |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Probe for CMD640x -- vlb |
| 323 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 324 | static int __init probe_for_cmd640_vlb(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | u8 b; |
| 327 | |
| 328 | __get_cmd640_reg = get_cmd640_reg_vlb; |
| 329 | __put_cmd640_reg = put_cmd640_reg_vlb; |
| 330 | cmd640_key = 0x178; |
| 331 | b = get_cmd640_reg(CFR); |
| 332 | if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) { |
| 333 | cmd640_key = 0x78; |
| 334 | b = get_cmd640_reg(CFR); |
| 335 | if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h)) |
| 336 | return 0; |
| 337 | } |
| 338 | return 1; /* success */ |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Returns 1 if an IDE interface/drive exists at 0x170, |
| 343 | * Returns 0 otherwise. |
| 344 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 345 | static int __init secondary_port_responding(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | unsigned long flags; |
| 348 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 349 | spin_lock_irqsave(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET); /* select drive0 */ |
| 352 | udelay(100); |
| 353 | if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) { |
| 354 | outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */ |
| 355 | udelay(100); |
| 356 | if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) { |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 357 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return 0; /* nothing responded */ |
| 359 | } |
| 360 | } |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 361 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return 1; /* success */ |
| 363 | } |
| 364 | |
| 365 | #ifdef CMD640_DUMP_REGS |
| 366 | /* |
| 367 | * Dump out all cmd640 registers. May be called from ide.c |
| 368 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 369 | static void cmd640_dump_regs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | unsigned int reg = cmd640_vlb ? 0x50 : 0x00; |
| 372 | |
| 373 | /* Dump current state of chip registers */ |
| 374 | printk("ide: cmd640 internal register dump:"); |
| 375 | for (; reg <= 0x59; reg++) { |
| 376 | if (!(reg & 0x0f)) |
| 377 | printk("\n%04x:", reg); |
| 378 | printk(" %02x", get_cmd640_reg(reg)); |
| 379 | } |
| 380 | printk("\n"); |
| 381 | } |
| 382 | #endif |
| 383 | |
| 384 | /* |
| 385 | * Check whether prefetch is on for a drive, |
| 386 | * and initialize the unmask flags for safe operation. |
| 387 | */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 388 | static void __init check_prefetch(ide_drive_t *drive, unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | u8 b = get_cmd640_reg(prefetch_regs[index]); |
| 391 | |
| 392 | if (b & prefetch_masks[index]) { /* is prefetch off? */ |
| 393 | drive->no_unmask = 0; |
| 394 | drive->no_io_32bit = 1; |
| 395 | drive->io_32bit = 0; |
| 396 | } else { |
| 397 | #if CMD640_PREFETCH_MASKS |
| 398 | drive->no_unmask = 1; |
| 399 | drive->unmask = 0; |
| 400 | #endif |
| 401 | drive->no_io_32bit = 0; |
| 402 | } |
| 403 | } |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED |
| 406 | |
| 407 | /* |
| 408 | * Sets prefetch mode for a drive. |
| 409 | */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 410 | static void set_prefetch_mode(ide_drive_t *drive, unsigned int index, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 412 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | int reg = prefetch_regs[index]; |
| 414 | u8 b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 416 | spin_lock_irqsave(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | b = __get_cmd640_reg(reg); |
| 418 | if (mode) { /* want prefetch on? */ |
| 419 | #if CMD640_PREFETCH_MASKS |
| 420 | drive->no_unmask = 1; |
| 421 | drive->unmask = 0; |
| 422 | #endif |
| 423 | drive->no_io_32bit = 0; |
| 424 | b &= ~prefetch_masks[index]; /* enable prefetch */ |
| 425 | } else { |
| 426 | drive->no_unmask = 0; |
| 427 | drive->no_io_32bit = 1; |
| 428 | drive->io_32bit = 0; |
| 429 | b |= prefetch_masks[index]; /* disable prefetch */ |
| 430 | } |
| 431 | __put_cmd640_reg(reg, b); |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 432 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Dump out current drive clocks settings |
| 437 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 438 | static void display_clocks(unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | u8 active_count, recovery_count; |
| 441 | |
| 442 | active_count = active_counts[index]; |
| 443 | if (active_count == 1) |
| 444 | ++active_count; |
| 445 | recovery_count = recovery_counts[index]; |
| 446 | if (active_count > 3 && recovery_count == 1) |
| 447 | ++recovery_count; |
| 448 | if (cmd640_chip_version > 1) |
| 449 | recovery_count += 1; /* cmd640b uses (count + 1)*/ |
| 450 | printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count); |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Pack active and recovery counts into single byte representation |
| 455 | * used by controller |
| 456 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 457 | static inline u8 pack_nibbles(u8 upper, u8 lower) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | return ((upper & 0x0f) << 4) | (lower & 0x0f); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * This routine retrieves the initial drive timings from the chipset. |
| 464 | */ |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 465 | static void __init retrieve_drive_counts(unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
| 467 | u8 b; |
| 468 | |
| 469 | /* |
| 470 | * Get the internal setup timing, and convert to clock count |
| 471 | */ |
| 472 | b = get_cmd640_reg(arttim_regs[index]) & ~0x3f; |
| 473 | switch (b) { |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 474 | case 0x00: b = 4; break; |
| 475 | case 0x80: b = 3; break; |
| 476 | case 0x40: b = 2; break; |
| 477 | default: b = 5; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | setup_counts[index] = b; |
| 480 | |
| 481 | /* |
| 482 | * Get the active/recovery counts |
| 483 | */ |
| 484 | b = get_cmd640_reg(drwtim_regs[index]); |
| 485 | active_counts[index] = (b >> 4) ? (b >> 4) : 0x10; |
| 486 | recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /* |
| 491 | * This routine writes the prepared setup/active/recovery counts |
| 492 | * for a drive into the cmd640 chipset registers to active them. |
| 493 | */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 494 | static void program_drive_counts(ide_drive_t *drive, unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | unsigned long flags; |
| 497 | u8 setup_count = setup_counts[index]; |
| 498 | u8 active_count = active_counts[index]; |
| 499 | u8 recovery_count = recovery_counts[index]; |
| 500 | |
| 501 | /* |
| 502 | * Set up address setup count and drive read/write timing registers. |
| 503 | * Primary interface has individual count/timing registers for |
| 504 | * each drive. Secondary interface has one common set of registers, |
| 505 | * so we merge the timings, using the slowest value for each timing. |
| 506 | */ |
| 507 | if (index > 1) { |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 508 | ide_hwif_t *hwif = drive->hwif; |
| 509 | ide_drive_t *peer = &hwif->drives[!drive->select.b.unit]; |
| 510 | unsigned int mate = index ^ 1; |
| 511 | |
| 512 | if (peer->present) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (setup_count < setup_counts[mate]) |
| 514 | setup_count = setup_counts[mate]; |
| 515 | if (active_count < active_counts[mate]) |
| 516 | active_count = active_counts[mate]; |
| 517 | if (recovery_count < recovery_counts[mate]) |
| 518 | recovery_count = recovery_counts[mate]; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Convert setup_count to internal chipset representation |
| 524 | */ |
| 525 | switch (setup_count) { |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 526 | case 4: setup_count = 0x00; break; |
| 527 | case 3: setup_count = 0x80; break; |
| 528 | case 1: |
| 529 | case 2: setup_count = 0x40; break; |
| 530 | default: setup_count = 0xc0; /* case 5 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Now that everything is ready, program the new timings |
| 535 | */ |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 536 | spin_lock_irqsave(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | /* |
| 538 | * Program the address_setup clocks into ARTTIM reg, |
| 539 | * and then the active/recovery counts into the DRWTIM reg |
| 540 | * (this converts counts of 16 into counts of zero -- okay). |
| 541 | */ |
| 542 | setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f; |
| 543 | __put_cmd640_reg(arttim_regs[index], setup_count); |
| 544 | __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count)); |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 545 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Set a specific pio_mode for a drive |
| 550 | */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 551 | static void cmd640_set_mode(ide_drive_t *drive, unsigned int index, |
| 552 | u8 pio_mode, unsigned int cycle_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
| 554 | int setup_time, active_time, recovery_time, clock_time; |
| 555 | u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count; |
| 556 | int bus_speed = system_bus_clock(); |
| 557 | |
| 558 | if (pio_mode > 5) |
| 559 | pio_mode = 5; |
| 560 | setup_time = ide_pio_timings[pio_mode].setup_time; |
| 561 | active_time = ide_pio_timings[pio_mode].active_time; |
| 562 | recovery_time = cycle_time - (setup_time + active_time); |
| 563 | clock_time = 1000 / bus_speed; |
Julia Lawall | 00fe8b7 | 2008-04-26 17:36:35 +0200 | [diff] [blame] | 564 | cycle_count = DIV_ROUND_UP(cycle_time, clock_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Julia Lawall | 00fe8b7 | 2008-04-26 17:36:35 +0200 | [diff] [blame] | 566 | setup_count = DIV_ROUND_UP(setup_time, clock_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Julia Lawall | 00fe8b7 | 2008-04-26 17:36:35 +0200 | [diff] [blame] | 568 | active_count = DIV_ROUND_UP(active_time, clock_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (active_count < 2) |
| 570 | active_count = 2; /* minimum allowed by cmd640 */ |
| 571 | |
Julia Lawall | 00fe8b7 | 2008-04-26 17:36:35 +0200 | [diff] [blame] | 572 | recovery_count = DIV_ROUND_UP(recovery_time, clock_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | recovery_count2 = cycle_count - (setup_count + active_count); |
| 574 | if (recovery_count2 > recovery_count) |
| 575 | recovery_count = recovery_count2; |
| 576 | if (recovery_count < 2) |
| 577 | recovery_count = 2; /* minimum allowed by cmd640 */ |
| 578 | if (recovery_count > 17) { |
| 579 | active_count += recovery_count - 17; |
| 580 | recovery_count = 17; |
| 581 | } |
| 582 | if (active_count > 16) |
| 583 | active_count = 16; /* maximum allowed by cmd640 */ |
| 584 | if (cmd640_chip_version > 1) |
| 585 | recovery_count -= 1; /* cmd640b uses (count + 1)*/ |
| 586 | if (recovery_count > 16) |
| 587 | recovery_count = 16; /* maximum allowed by cmd640 */ |
| 588 | |
| 589 | setup_counts[index] = setup_count; |
| 590 | active_counts[index] = active_count; |
| 591 | recovery_counts[index] = recovery_count; |
| 592 | |
| 593 | /* |
| 594 | * In a perfect world, we might set the drive pio mode here |
| 595 | * (using WIN_SETFEATURE) before continuing. |
| 596 | * |
| 597 | * But we do not, because: |
| 598 | * 1) this is the wrong place to do it (proper is do_special() in ide.c) |
| 599 | * 2) in practice this is rarely, if ever, necessary |
| 600 | */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 601 | program_drive_counts(drive, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Bartlomiej Zolnierkiewicz | 26bcb87 | 2007-10-11 23:54:00 +0200 | [diff] [blame] | 604 | static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
Bartlomiej Zolnierkiewicz | 7dd0008 | 2007-07-20 01:11:56 +0200 | [diff] [blame] | 606 | unsigned int index = 0, cycle_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | u8 b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Bartlomiej Zolnierkiewicz | 26bcb87 | 2007-10-11 23:54:00 +0200 | [diff] [blame] | 609 | switch (pio) { |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 610 | case 6: /* set fast-devsel off */ |
| 611 | case 7: /* set fast-devsel on */ |
| 612 | b = get_cmd640_reg(CNTRL) & ~0x27; |
| 613 | if (pio & 1) |
| 614 | b |= 0x27; |
| 615 | put_cmd640_reg(CNTRL, b); |
| 616 | printk("%s: %sabled cmd640 fast host timing (devsel)\n", |
| 617 | drive->name, (pio & 1) ? "en" : "dis"); |
| 618 | return; |
| 619 | case 8: /* set prefetch off */ |
| 620 | case 9: /* set prefetch on */ |
| 621 | set_prefetch_mode(drive, index, pio & 1); |
| 622 | printk("%s: %sabled cmd640 prefetch\n", |
| 623 | drive->name, (pio & 1) ? "en" : "dis"); |
| 624 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Bartlomiej Zolnierkiewicz | 26bcb87 | 2007-10-11 23:54:00 +0200 | [diff] [blame] | 627 | cycle_time = ide_pio_cycle_time(drive, pio); |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 628 | cmd640_set_mode(drive, index, pio, cycle_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Bartlomiej Zolnierkiewicz | 342cdb6 | 2007-07-20 01:11:55 +0200 | [diff] [blame] | 630 | printk("%s: selected cmd640 PIO mode%d (%dns)", |
Bartlomiej Zolnierkiewicz | 26bcb87 | 2007-10-11 23:54:00 +0200 | [diff] [blame] | 631 | drive->name, pio, cycle_time); |
Bartlomiej Zolnierkiewicz | 342cdb6 | 2007-07-20 01:11:55 +0200 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | display_clocks(index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame^] | 636 | static const struct ide_port_ops cmd640_port_ops = { |
| 637 | .set_pio_mode = cmd640_set_pio_mode, |
| 638 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */ |
| 640 | |
| 641 | static int pci_conf1(void) |
| 642 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | unsigned long flags; |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 644 | u32 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 646 | spin_lock_irqsave(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | outb(0x01, 0xCFB); |
| 648 | tmp = inl(0xCF8); |
| 649 | outl(0x80000000, 0xCF8); |
| 650 | if (inl(0xCF8) == 0x80000000) { |
| 651 | outl(tmp, 0xCF8); |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 652 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return 1; |
| 654 | } |
| 655 | outl(tmp, 0xCF8); |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 656 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int pci_conf2(void) |
| 661 | { |
| 662 | unsigned long flags; |
| 663 | |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 664 | spin_lock_irqsave(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | outb(0x00, 0xCFB); |
| 666 | outb(0x00, 0xCF8); |
| 667 | outb(0x00, 0xCFA); |
| 668 | if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) { |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 669 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return 1; |
| 671 | } |
Bartlomiej Zolnierkiewicz | 5bbcf92 | 2007-10-20 00:32:35 +0200 | [diff] [blame] | 672 | spin_unlock_irqrestore(&cmd640_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
Bartlomiej Zolnierkiewicz | c413b9b | 2008-02-02 19:56:31 +0100 | [diff] [blame] | 676 | static const struct ide_port_info cmd640_port_info __initdata = { |
| 677 | .chipset = ide_cmd640, |
| 678 | .host_flags = IDE_HFLAG_SERIALIZE | |
| 679 | IDE_HFLAG_NO_DMA | |
| 680 | IDE_HFLAG_NO_AUTOTUNE | |
| 681 | IDE_HFLAG_ABUSE_PREFETCH | |
| 682 | IDE_HFLAG_ABUSE_FAST_DEVSEL, |
| 683 | #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame^] | 684 | .port_ops = &cmd640_port_ops, |
Bartlomiej Zolnierkiewicz | c413b9b | 2008-02-02 19:56:31 +0100 | [diff] [blame] | 685 | .pio_mask = ATA_PIO5, |
| 686 | #endif |
| 687 | }; |
| 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | /* |
Bartlomiej Zolnierkiewicz | ade2daf | 2008-01-26 20:13:07 +0100 | [diff] [blame] | 690 | * Probe for a cmd640 chipset, and initialize it if found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | */ |
Bartlomiej Zolnierkiewicz | ade2daf | 2008-01-26 20:13:07 +0100 | [diff] [blame] | 692 | static int __init cmd640x_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED |
| 695 | int second_port_toggled = 0; |
| 696 | #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */ |
| 697 | int second_port_cmd640 = 0; |
| 698 | const char *bus_type, *port2; |
| 699 | unsigned int index; |
| 700 | u8 b, cfr; |
Bartlomiej Zolnierkiewicz | 8ac4ce7 | 2008-01-26 20:13:06 +0100 | [diff] [blame] | 701 | u8 idx[4] = { 0xff, 0xff, 0xff, 0xff }; |
Bartlomiej Zolnierkiewicz | 6d3803b | 2008-04-18 00:46:34 +0200 | [diff] [blame] | 702 | hw_regs_t hw[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | if (cmd640_vlb && probe_for_cmd640_vlb()) { |
| 705 | bus_type = "VLB"; |
| 706 | } else { |
| 707 | cmd640_vlb = 0; |
| 708 | /* Find out what kind of PCI probing is supported otherwise |
| 709 | Justin Gibbs will sulk.. */ |
| 710 | if (pci_conf1() && probe_for_cmd640_pci1()) |
| 711 | bus_type = "PCI (type1)"; |
| 712 | else if (pci_conf2() && probe_for_cmd640_pci2()) |
| 713 | bus_type = "PCI (type2)"; |
| 714 | else |
| 715 | return 0; |
| 716 | } |
| 717 | /* |
| 718 | * Undocumented magic (there is no 0x5b reg in specs) |
| 719 | */ |
| 720 | put_cmd640_reg(0x5b, 0xbd); |
| 721 | if (get_cmd640_reg(0x5b) != 0xbd) { |
| 722 | printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n"); |
| 723 | return 0; |
| 724 | } |
| 725 | put_cmd640_reg(0x5b, 0); |
| 726 | |
| 727 | #ifdef CMD640_DUMP_REGS |
| 728 | cmd640_dump_regs(); |
| 729 | #endif |
| 730 | |
| 731 | /* |
| 732 | * Documented magic begins here |
| 733 | */ |
| 734 | cfr = get_cmd640_reg(CFR); |
| 735 | cmd640_chip_version = cfr & CFR_DEVREV; |
| 736 | if (cmd640_chip_version == 0) { |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 737 | printk("ide: bad cmd640 revision: %d\n", cmd640_chip_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Bartlomiej Zolnierkiewicz | 6d3803b | 2008-04-18 00:46:34 +0200 | [diff] [blame] | 741 | memset(&hw, 0, sizeof(hw)); |
| 742 | |
| 743 | ide_std_init_ports(&hw[0], 0x1f0, 0x3f6); |
| 744 | hw[0].irq = 14; |
| 745 | |
| 746 | ide_std_init_ports(&hw[1], 0x170, 0x376); |
| 747 | hw[1].irq = 15; |
| 748 | |
| 749 | printk(KERN_INFO "cmd640: buggy cmd640%c interface on %s, config=0x%02x" |
| 750 | "\n", 'a' + cmd640_chip_version - 1, bus_type, cfr); |
| 751 | |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 752 | cmd_hwif0 = ide_find_port(); |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | /* |
| 755 | * Initialize data for primary port |
| 756 | */ |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 757 | if (cmd_hwif0) { |
| 758 | ide_init_port_hw(cmd_hwif0, &hw[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 760 | idx[0] = cmd_hwif0->index; |
| 761 | } |
Bartlomiej Zolnierkiewicz | 8ac4ce7 | 2008-01-26 20:13:06 +0100 | [diff] [blame] | 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
| 764 | * Ensure compatibility by always using the slowest timings |
| 765 | * for access to the drive's command register block, |
| 766 | * and reset the prefetch burstsize to default (512 bytes). |
| 767 | * |
| 768 | * Maybe we need a way to NOT do these on *some* systems? |
| 769 | */ |
| 770 | put_cmd640_reg(CMDTIM, 0); |
| 771 | put_cmd640_reg(BRST, 0x40); |
| 772 | |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 773 | cmd_hwif1 = ide_find_port(); |
| 774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | /* |
| 776 | * Try to enable the secondary interface, if not already enabled |
| 777 | */ |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 778 | if (cmd_hwif1 && |
| 779 | cmd_hwif1->drives[0].noprobe && cmd_hwif1->drives[1].noprobe) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | port2 = "not probed"; |
| 781 | } else { |
| 782 | b = get_cmd640_reg(CNTRL); |
| 783 | if (secondary_port_responding()) { |
| 784 | if ((b & CNTRL_ENA_2ND)) { |
| 785 | second_port_cmd640 = 1; |
| 786 | port2 = "okay"; |
| 787 | } else if (cmd640_vlb) { |
| 788 | second_port_cmd640 = 1; |
| 789 | port2 = "alive"; |
| 790 | } else |
| 791 | port2 = "not cmd640"; |
| 792 | } else { |
| 793 | put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */ |
| 794 | if (secondary_port_responding()) { |
| 795 | second_port_cmd640 = 1; |
| 796 | #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED |
| 797 | second_port_toggled = 1; |
| 798 | #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */ |
| 799 | port2 = "enabled"; |
| 800 | } else { |
| 801 | put_cmd640_reg(CNTRL, b); /* restore original setting */ |
| 802 | port2 = "not responding"; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Initialize data for secondary cmd640 port, if enabled |
| 809 | */ |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 810 | if (second_port_cmd640 && cmd_hwif1) { |
Bartlomiej Zolnierkiewicz | 6d3803b | 2008-04-18 00:46:34 +0200 | [diff] [blame] | 811 | ide_init_port_hw(cmd_hwif1, &hw[1]); |
Bartlomiej Zolnierkiewicz | 8ac4ce7 | 2008-01-26 20:13:06 +0100 | [diff] [blame] | 812 | |
| 813 | idx[1] = cmd_hwif1->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 815 | printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n", |
Bartlomiej Zolnierkiewicz | c413b9b | 2008-02-02 19:56:31 +0100 | [diff] [blame] | 816 | second_port_cmd640 ? "" : "not ", port2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | /* |
| 819 | * Establish initial timings/prefetch for all drives. |
| 820 | * Do not unnecessarily disturb any prior BIOS setup of these. |
| 821 | */ |
| 822 | for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) { |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 823 | ide_drive_t *drive; |
| 824 | |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 825 | if (index > 1) { |
| 826 | if (cmd_hwif1 == NULL) |
| 827 | continue; |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 828 | drive = &cmd_hwif1->drives[index & 1]; |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 829 | } else { |
| 830 | if (cmd_hwif0 == NULL) |
| 831 | continue; |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 832 | drive = &cmd_hwif0->drives[index & 1]; |
Bartlomiej Zolnierkiewicz | 84f05df | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 833 | } |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED |
| 836 | if (drive->autotune || ((index > 1) && second_port_toggled)) { |
Paolo Ciarrocchi | 4752b5e | 2008-04-26 17:36:41 +0200 | [diff] [blame] | 837 | /* |
| 838 | * Reset timing to the slowest speed and turn off |
| 839 | * prefetch. This way, the drive identify code has |
| 840 | * a better chance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | */ |
| 842 | setup_counts [index] = 4; /* max possible */ |
| 843 | active_counts [index] = 16; /* max possible */ |
| 844 | recovery_counts [index] = 16; /* max possible */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 845 | program_drive_counts(drive, index); |
| 846 | set_prefetch_mode(drive, index, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | printk("cmd640: drive%d timings/prefetch cleared\n", index); |
| 848 | } else { |
| 849 | /* |
| 850 | * Record timings/prefetch without changing them. |
| 851 | * This preserves any prior BIOS setup. |
| 852 | */ |
| 853 | retrieve_drive_counts (index); |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 854 | check_prefetch(drive, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | printk("cmd640: drive%d timings/prefetch(%s) preserved", |
| 856 | index, drive->no_io_32bit ? "off" : "on"); |
| 857 | display_clocks(index); |
| 858 | } |
| 859 | #else |
| 860 | /* |
| 861 | * Set the drive unmask flags to match the prefetch setting |
| 862 | */ |
Bartlomiej Zolnierkiewicz | 9523076 | 2008-04-26 17:36:33 +0200 | [diff] [blame] | 863 | check_prefetch(drive, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | printk("cmd640: drive%d timings/prefetch(%s) preserved\n", |
| 865 | index, drive->no_io_32bit ? "off" : "on"); |
| 866 | #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */ |
| 867 | } |
| 868 | |
| 869 | #ifdef CMD640_DUMP_REGS |
| 870 | cmd640_dump_regs(); |
| 871 | #endif |
Bartlomiej Zolnierkiewicz | 8ac4ce7 | 2008-01-26 20:13:06 +0100 | [diff] [blame] | 872 | |
Bartlomiej Zolnierkiewicz | c413b9b | 2008-02-02 19:56:31 +0100 | [diff] [blame] | 873 | ide_device_add(idx, &cmd640_port_info); |
Bartlomiej Zolnierkiewicz | 8ac4ce7 | 2008-01-26 20:13:06 +0100 | [diff] [blame] | 874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | return 1; |
| 876 | } |
| 877 | |
Bartlomiej Zolnierkiewicz | ade2daf | 2008-01-26 20:13:07 +0100 | [diff] [blame] | 878 | module_param_named(probe_vlb, cmd640_vlb, bool, 0); |
| 879 | MODULE_PARM_DESC(probe_vlb, "probe for VLB version of CMD640 chipset"); |
| 880 | |
| 881 | module_init(cmd640x_init); |
Adrian Bunk | 776c0bc | 2008-04-02 21:22:02 +0200 | [diff] [blame] | 882 | |
| 883 | MODULE_LICENSE("GPL"); |