ali14xx.c 6.46 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 *  Copyright (C) 1996  Linus Torvalds & author (see below)
 */

/*
 * ALI M14xx chipset EIDE controller
 *
 * Works for ALI M1439/1443/1445/1487/1489 chipsets.
 *
 * Adapted from code developed by derekn@vw.ece.cmu.edu.  -ml
 * Derek's notes follow:
 *
 * I think the code should be pretty understandable,
 * but I'll be happy to (try to) answer questions.
 *
 * The critical part is in the setupDrive function.  The initRegisters
 * function doesn't seem to be necessary, but the DOS driver does it, so
 * I threw it in.
 *
 * I've only tested this on my system, which only has one disk.  I posted
 * it to comp.sys.linux.hardware, so maybe some other people will try it
 * out.
 *
 * Derek Noonburg  (derekn@ece.cmu.edu)
 * 95-sep-26
 *
 * Update 96-jul-13:
 *
 * I've since upgraded to two disks and a CD-ROM, with no trouble, and
 * I've also heard from several others who have used it successfully.
 * This driver appears to work with both the 1443/1445 and the 1487/1489
 * chipsets.  I've added support for PIO mode 4 for the 1487.  This
 * seems to work just fine on the 1443 also, although I'm not sure it's
 * advertised as supporting mode 4.  (I've been running a WDC AC21200 in
 * mode 4 for a while now with no trouble.)  -Derek
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
#include <linux/ide.h>
#include <linux/init.h>

#include <asm/io.h>

51 52
#define DRV_NAME "ali14xx"

Linus Torvalds's avatar
Linus Torvalds committed
53 54
/* port addresses for auto-detection */
#define ALI_NUM_PORTS 4
55
static const int ports[ALI_NUM_PORTS] __initconst =
56
	{ 0x074, 0x0f4, 0x034, 0x0e4 };
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60

/* register initialization data */
typedef struct { u8 reg, data; } RegInitializer;

61
static const RegInitializer initData[] __initconst = {
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	{0x01, 0x0f}, {0x02, 0x00}, {0x03, 0x00}, {0x04, 0x00},
	{0x05, 0x00}, {0x06, 0x00}, {0x07, 0x2b}, {0x0a, 0x0f},
	{0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x00},
	{0x29, 0x00}, {0x2a, 0x00}, {0x2f, 0x00}, {0x2b, 0x00},
	{0x2c, 0x00}, {0x2d, 0x00}, {0x2e, 0x00}, {0x30, 0x00},
	{0x31, 0x00}, {0x32, 0x00}, {0x33, 0x00}, {0x34, 0xff},
	{0x35, 0x03}, {0x00, 0x00}
};

/* timing parameter registers for each drive */
static struct { u8 reg1, reg2, reg3, reg4; } regTab[4] = {
	{0x03, 0x26, 0x04, 0x27},     /* drive 0 */
	{0x05, 0x28, 0x06, 0x29},     /* drive 1 */
	{0x2b, 0x30, 0x2c, 0x31},     /* drive 2 */
	{0x2d, 0x32, 0x2e, 0x33},     /* drive 3 */
};

static int basePort;	/* base port address */
static int regPort;	/* port for register number */
static int dataPort;	/* port for register data */
static u8 regOn;	/* output to base port to access registers */
static u8 regOff;	/* output to base port to close registers */

/*------------------------------------------------------------------------*/

/*
 * Read a controller register.
 */
90
static inline u8 inReg(u8 reg)
Linus Torvalds's avatar
Linus Torvalds committed
91 92 93 94 95 96 97 98
{
	outb_p(reg, regPort);
	return inb(dataPort);
}

/*
 * Write a controller register.
 */
99
static void outReg(u8 data, u8 reg)
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103 104
{
	outb_p(reg, regPort);
	outb_p(data, dataPort);
}

105 106
static DEFINE_SPINLOCK(ali14xx_lock);

Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111
/*
 * Set PIO mode for the specified drive.
 * This function computes timing parameters
 * and sets controller registers accordingly.
 */
112
static void ali14xx_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
113 114 115 116 117
{
	int driveNum;
	int time1, time2;
	u8 param1, param2, param3, param4;
	unsigned long flags;
118
	int bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
119
	const u8 pio = drive->pio_mode - XFER_PIO_0;
120
	struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
Linus Torvalds's avatar
Linus Torvalds committed
121 122

	/* calculate timing, according to PIO mode */
123
	time1 = ide_pio_cycle_time(drive, pio);
124
	time2 = t->active;
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134
	param3 = param1 = (time2 * bus_speed + 999) / 1000;
	param4 = param2 = (time1 * bus_speed + 999) / 1000 - param1;
	if (pio < 3) {
		param3 += 8;
		param4 += 8;
	}
	printk(KERN_DEBUG "%s: PIO mode%d, t1=%dns, t2=%dns, cycles = %d+%d, %d+%d\n",
		drive->name, pio, time1, time2, param1, param2, param3, param4);

	/* stuff timing parameters into controller registers */
135
	driveNum = (drive->hwif->index << 1) + (drive->dn & 1);
136
	spin_lock_irqsave(&ali14xx_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142
	outb_p(regOn, basePort);
	outReg(param1, regTab[driveNum].reg1);
	outReg(param2, regTab[driveNum].reg2);
	outReg(param3, regTab[driveNum].reg3);
	outReg(param4, regTab[driveNum].reg4);
	outb_p(regOff, basePort);
143
	spin_unlock_irqrestore(&ali14xx_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
144 145 146 147 148
}

/*
 * Auto-detect the IDE controller port.
 */
149
static int __init findPort(void)
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
{
	int i;
	u8 t;
	unsigned long flags;

	local_irq_save(flags);
	for (i = 0; i < ALI_NUM_PORTS; ++i) {
		basePort = ports[i];
		regOff = inb(basePort);
		for (regOn = 0x30; regOn <= 0x33; ++regOn) {
			outb_p(regOn, basePort);
			if (inb(basePort) == regOn) {
				regPort = basePort + 4;
				dataPort = basePort + 8;
				t = inReg(0) & 0xf0;
				outb_p(regOff, basePort);
				local_irq_restore(flags);
				if (t != 0x50)
					return 0;
				return 1;  /* success */
			}
		}
		outb_p(regOff, basePort);
	}
	local_irq_restore(flags);
	return 0;
}

/*
 * Initialize controller registers with default values.
 */
181 182
static int __init initRegisters(void)
{
183
	const RegInitializer *p;
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197
	u8 t;
	unsigned long flags;

	local_irq_save(flags);
	outb_p(regOn, basePort);
	for (p = initData; p->reg != 0; ++p)
		outReg(p->data, p->reg);
	outb_p(0x01, regPort);
	t = inb(regPort) & 0x01;
	outb_p(regOff, basePort);
	local_irq_restore(flags);
	return t;
}

198 199 200 201
static const struct ide_port_ops ali14xx_port_ops = {
	.set_pio_mode		= ali14xx_set_pio_mode,
};

202
static const struct ide_port_info ali14xx_port_info = {
203
	.name			= DRV_NAME,
204
	.chipset		= ide_ali14xx,
205
	.port_ops		= &ali14xx_port_ops,
206
	.host_flags		= IDE_HFLAG_NO_DMA,
207 208 209
	.pio_mask		= ATA_PIO4,
};

Linus Torvalds's avatar
Linus Torvalds committed
210 211 212 213 214 215 216 217 218 219 220
static int __init ali14xx_probe(void)
{
	printk(KERN_DEBUG "ali14xx: base=0x%03x, regOn=0x%02x.\n",
			  basePort, regOn);

	/* initialize controller registers */
	if (!initRegisters()) {
		printk(KERN_ERR "ali14xx: Chip initialization failed.\n");
		return 1;
	}

221
	return ide_legacy_device_add(&ali14xx_port_info, 0);
Linus Torvalds's avatar
Linus Torvalds committed
222 223
}

224
static bool probe_ali14xx;
225 226 227 228

module_param_named(probe, probe_ali14xx, bool, 0);
MODULE_PARM_DESC(probe, "probe for ALI M14xx chipsets");

229
static int __init ali14xx_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
230
{
231 232 233
	if (probe_ali14xx == 0)
		goto out;

Linus Torvalds's avatar
Linus Torvalds committed
234 235 236 237 238 239 240
	/* auto-detect IDE controller port */
	if (findPort()) {
		if (ali14xx_probe())
			return -ENODEV;
		return 0;
	}
	printk(KERN_ERR "ali14xx: not found.\n");
241
out:
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244 245 246 247 248 249
	return -ENODEV;
}

module_init(ali14xx_init);

MODULE_AUTHOR("see local file");
MODULE_DESCRIPTION("support of ALI 14XX IDE chipsets");
MODULE_LICENSE("GPL");