pcmciamtd.c 20.6 KB
Newer Older
L
Linus Torvalds 已提交
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
/*
 * pcmciamtd.c - MTD driver for PCMCIA flash memory cards
 *
 * Author: Simon Evans <spse@secret.org.uk>
 *
 * Copyright (C) 2002 Simon Evans
 *
 * Licence: GPL
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/system.h>

#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/ds.h>

#include <linux/mtd/map.h>
#include <linux/mtd/mtd.h>

#ifdef CONFIG_MTD_DEBUG
static int debug = CONFIG_MTD_DEBUG_VERBOSE;
R
Rusty Russell 已提交
28
module_param(debug, int, 0);
L
Linus Torvalds 已提交
29 30 31 32
MODULE_PARM_DESC(debug, "Set Debug Level 0=quiet, 5=noisy");
#undef DEBUG
#define DEBUG(n, format, arg...) \
	if (n <= debug) {	 \
33
		printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __func__ , ## arg); \
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	}

#else
#undef DEBUG
#define DEBUG(n, arg...)
static const int debug = 0;
#endif

#define info(format, arg...) printk(KERN_INFO "pcmciamtd: " format "\n" , ## arg)

#define DRIVER_DESC	"PCMCIA Flash memory card driver"

/* Size of the PCMCIA address space: 26 bits = 64 MB */
#define MAX_PCMCIA_ADDR	0x4000000

struct pcmciamtd_dev {
50
	struct pcmcia_device	*p_dev;
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	caddr_t		win_base;	/* ioremapped address of PCMCIA window */
	unsigned int	win_size;	/* size of window */
	unsigned int	offset;		/* offset into card the window currently points at */
	struct map_info	pcmcia_map;
	struct mtd_info	*mtd_info;
	int		vpp;
	char		mtd_name[sizeof(struct cistpl_vers_1_t)];
};


/* Module parameters */

/* 2 = do 16-bit transfers, 1 = do 8-bit transfers */
static int bankwidth = 2;

/* Speed of memory accesses, in ns */
static int mem_speed;

/* Force the size of an SRAM card */
static int force_size;

/* Force Vpp */
static int vpp;

/* Set Vpp */
static int setvpp;

/* Force card to be treated as FLASH, ROM or RAM */
static int mem_type;

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
MODULE_DESCRIPTION(DRIVER_DESC);
R
Rusty Russell 已提交
84
module_param(bankwidth, int, 0);
L
Linus Torvalds 已提交
85
MODULE_PARM_DESC(bankwidth, "Set bankwidth (1=8 bit, 2=16 bit, default=2)");
R
Rusty Russell 已提交
86
module_param(mem_speed, int, 0);
L
Linus Torvalds 已提交
87
MODULE_PARM_DESC(mem_speed, "Set memory access speed in ns");
R
Rusty Russell 已提交
88
module_param(force_size, int, 0);
L
Linus Torvalds 已提交
89
MODULE_PARM_DESC(force_size, "Force size of card in MiB (1-64)");
R
Rusty Russell 已提交
90
module_param(setvpp, int, 0);
L
Linus Torvalds 已提交
91
MODULE_PARM_DESC(setvpp, "Set Vpp (0=Never, 1=On writes, 2=Always on, default=0)");
R
Rusty Russell 已提交
92
module_param(vpp, int, 0);
L
Linus Torvalds 已提交
93
MODULE_PARM_DESC(vpp, "Vpp value in 1/10ths eg 33=3.3V 120=12V (Dangerous)");
R
Rusty Russell 已提交
94
module_param(mem_type, int, 0);
L
Linus Torvalds 已提交
95 96 97
MODULE_PARM_DESC(mem_type, "Set Memory type (0=Flash, 1=RAM, 2=ROM, default=0)");


98 99 100
/* read/write{8,16} copy_{from,to} routines with window remapping
 * to access whole card
 */
L
Linus Torvalds 已提交
101 102 103
static caddr_t remap_window(struct map_info *map, unsigned long to)
{
	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
104
	struct resource *win = (struct resource *) map->map_priv_2;
D
Dominik Brodowski 已提交
105
	unsigned int offset;
L
Linus Torvalds 已提交
106 107
	int ret;

108 109
	if (!pcmcia_dev_present(dev->p_dev)) {
		DEBUG(1, "device removed");
L
Linus Torvalds 已提交
110 111 112
		return 0;
	}

D
Dominik Brodowski 已提交
113 114
	offset = to & ~(dev->win_size-1);
	if (offset != dev->offset) {
L
Linus Torvalds 已提交
115
		DEBUG(2, "Remapping window from 0x%8.8x to 0x%8.8x",
D
Dominik Brodowski 已提交
116 117
		      dev->offset, offset);
		ret = pcmcia_map_mem_page(dev->p_dev, win, offset);
118
		if (ret != 0)
L
Linus Torvalds 已提交
119
			return NULL;
D
Dominik Brodowski 已提交
120
		dev->offset = offset;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	}
	return dev->win_base + (to & (dev->win_size-1));
}


static map_word pcmcia_read8_remap(struct map_info *map, unsigned long ofs)
{
	caddr_t addr;
	map_word d = {{0}};

	addr = remap_window(map, ofs);
	if(!addr)
		return d;

	d.x[0] = readb(addr);
136
	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02lx", ofs, addr, d.x[0]);
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150
	return d;
}


static map_word pcmcia_read16_remap(struct map_info *map, unsigned long ofs)
{
	caddr_t addr;
	map_word d = {{0}};

	addr = remap_window(map, ofs);
	if(!addr)
		return d;

	d.x[0] = readw(addr);
151
	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04lx", ofs, addr, d.x[0]);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160
	return d;
}


static void pcmcia_copy_from_remap(struct map_info *map, void *to, unsigned long from, ssize_t len)
{
	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
	unsigned long win_size = dev->win_size;

161
	DEBUG(3, "to = %p from = %lu len = %zd", to, from, len);
L
Linus Torvalds 已提交
162 163 164 165 166 167
	while(len) {
		int toread = win_size - (from & (win_size-1));
		caddr_t addr;

		if(toread > len)
			toread = len;
168

L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		addr = remap_window(map, from);
		if(!addr)
			return;

		DEBUG(4, "memcpy from %p to %p len = %d", addr, to, toread);
		memcpy_fromio(to, addr, toread);
		len -= toread;
		to += toread;
		from += toread;
	}
}


static void pcmcia_write8_remap(struct map_info *map, map_word d, unsigned long adr)
{
	caddr_t addr = remap_window(map, adr);

	if(!addr)
		return;

189
	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%02lx", adr, addr, d.x[0]);
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198 199
	writeb(d.x[0], addr);
}


static void pcmcia_write16_remap(struct map_info *map, map_word d, unsigned long adr)
{
	caddr_t addr = remap_window(map, adr);
	if(!addr)
		return;

200
	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%04lx", adr, addr, d.x[0]);
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209
	writew(d.x[0], addr);
}


static void pcmcia_copy_to_remap(struct map_info *map, unsigned long to, const void *from, ssize_t len)
{
	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
	unsigned long win_size = dev->win_size;

210
	DEBUG(3, "to = %lu from = %p len = %zd", to, from, len);
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	while(len) {
		int towrite = win_size - (to & (win_size-1));
		caddr_t addr;

		if(towrite > len)
			towrite = len;

		addr = remap_window(map, to);
		if(!addr)
			return;

		DEBUG(4, "memcpy from %p to %p len = %d", from, addr, towrite);
		memcpy_toio(addr, from, towrite);
		len -= towrite;
		to += towrite;
		from += towrite;
	}
}


/* read/write{8,16} copy_{from,to} routines with direct access */

233
#define DEV_REMOVED(x)  (!(pcmcia_dev_present(((struct pcmciamtd_dev *)map->map_priv_1)->p_dev)))
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243

static map_word pcmcia_read8(struct map_info *map, unsigned long ofs)
{
	caddr_t win_base = (caddr_t)map->map_priv_2;
	map_word d = {{0}};

	if(DEV_REMOVED(map))
		return d;

	d.x[0] = readb(win_base + ofs);
244 245
	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02lx",
	      ofs, win_base + ofs, d.x[0]);
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258
	return d;
}


static map_word pcmcia_read16(struct map_info *map, unsigned long ofs)
{
	caddr_t win_base = (caddr_t)map->map_priv_2;
	map_word d = {{0}};

	if(DEV_REMOVED(map))
		return d;

	d.x[0] = readw(win_base + ofs);
259 260
	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04lx",
	      ofs, win_base + ofs, d.x[0]);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271
	return d;
}


static void pcmcia_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
{
	caddr_t win_base = (caddr_t)map->map_priv_2;

	if(DEV_REMOVED(map))
		return;

272
	DEBUG(3, "to = %p from = %lu len = %zd", to, from, len);
L
Linus Torvalds 已提交
273 274 275 276
	memcpy_fromio(to, win_base + from, len);
}


277
static void pcmcia_write8(struct map_info *map, map_word d, unsigned long adr)
L
Linus Torvalds 已提交
278 279 280 281 282 283
{
	caddr_t win_base = (caddr_t)map->map_priv_2;

	if(DEV_REMOVED(map))
		return;

284 285
	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%02lx",
	      adr, win_base + adr, d.x[0]);
286
	writeb(d.x[0], win_base + adr);
L
Linus Torvalds 已提交
287 288 289
}


290
static void pcmcia_write16(struct map_info *map, map_word d, unsigned long adr)
L
Linus Torvalds 已提交
291 292 293 294 295 296
{
	caddr_t win_base = (caddr_t)map->map_priv_2;

	if(DEV_REMOVED(map))
		return;

297 298
	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%04lx",
	      adr, win_base + adr, d.x[0]);
299
	writew(d.x[0], win_base + adr);
L
Linus Torvalds 已提交
300 301 302 303 304 305 306 307 308 309
}


static void pcmcia_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
{
	caddr_t win_base = (caddr_t)map->map_priv_2;

	if(DEV_REMOVED(map))
		return;

310
	DEBUG(3, "to = %lu from = %p len = %zd", to, from, len);
L
Linus Torvalds 已提交
311 312 313 314 315 316 317
	memcpy_toio(win_base + to, from, len);
}


static void pcmciamtd_set_vpp(struct map_info *map, int on)
{
	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
318
	struct pcmcia_device *link = dev->p_dev;
L
Linus Torvalds 已提交
319 320

	DEBUG(2, "dev = %p on = %d vpp = %d\n", dev, on, dev->vpp);
321
	pcmcia_fixup_vpp(link, on ? dev->vpp : 0);
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329
}


/* After a card is removed, pcmciamtd_release() will unregister the
 * device, and release the PCMCIA configuration.  If the device is
 * still open, this will be postponed until it is closed.
 */

330
static void pcmciamtd_release(struct pcmcia_device *link)
L
Linus Torvalds 已提交
331 332 333 334 335
{
	struct pcmciamtd_dev *dev = link->priv;

	DEBUG(3, "link = 0x%p", link);

336
	if (link->resource[2]->end) {
L
Linus Torvalds 已提交
337 338 339 340 341
		if(dev->win_base) {
			iounmap(dev->win_base);
			dev->win_base = NULL;
		}
	}
342
	pcmcia_disable_device(link);
L
Linus Torvalds 已提交
343 344 345
}


346 347 348 349
#ifdef CONFIG_MTD_DEBUG
static int pcmciamtd_cistpl_format(struct pcmcia_device *p_dev,
				tuple_t *tuple,
				void *priv_data)
L
Linus Torvalds 已提交
350 351
{
	cisparse_t parse;
352

353 354 355 356 357 358 359 360
	if (!pcmcia_parse_tuple(tuple, &parse)) {
		cistpl_format_t *t = &parse.format;
		(void)t; /* Shut up, gcc */
		DEBUG(2, "Format type: %u, Error Detection: %u, offset = %u, length =%u",
			t->type, t->edc, t->offset, t->length);
	}
	return -ENOSPC;
}
361

362 363 364 365 366 367
static int pcmciamtd_cistpl_jedec(struct pcmcia_device *p_dev,
				tuple_t *tuple,
				void *priv_data)
{
	cisparse_t parse;
	int i;
368

369 370 371
	if (!pcmcia_parse_tuple(tuple, &parse)) {
		cistpl_jedec_t *t = &parse.jedec;
		for (i = 0; i < t->nid; i++)
372 373
			DEBUG(2, "JEDEC: 0x%02x 0x%02x",
			      t->id[i].mfr, t->id[i].info);
374 375 376 377
	}
	return -ENOSPC;
}
#endif
378

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
static int pcmciamtd_cistpl_device(struct pcmcia_device *p_dev,
				tuple_t *tuple,
				void *priv_data)
{
	struct pcmciamtd_dev *dev = priv_data;
	cisparse_t parse;
	cistpl_device_t *t = &parse.device;
	int i;

	if (pcmcia_parse_tuple(tuple, &parse))
		return -EINVAL;

	DEBUG(2, "Common memory:");
	dev->pcmcia_map.size = t->dev[0].size;
	/* from here on: DEBUG only */
	for (i = 0; i < t->ndev; i++) {
		DEBUG(2, "Region %d, type = %u", i, t->dev[i].type);
		DEBUG(2, "Region %d, wp = %u", i, t->dev[i].wp);
		DEBUG(2, "Region %d, speed = %u ns", i, t->dev[i].speed);
		DEBUG(2, "Region %d, size = %u bytes", i, t->dev[i].size);
	}
	return 0;
}
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
static int pcmciamtd_cistpl_geo(struct pcmcia_device *p_dev,
				tuple_t *tuple,
				void *priv_data)
{
	struct pcmciamtd_dev *dev = priv_data;
	cisparse_t parse;
	cistpl_device_geo_t *t = &parse.device_geo;
	int i;

	if (pcmcia_parse_tuple(tuple, &parse))
		return -EINVAL;

	dev->pcmcia_map.bankwidth = t->geo[0].buswidth;
	/* from here on: DEBUG only */
	for (i = 0; i < t->ngeo; i++) {
		DEBUG(2, "region: %d bankwidth = %u", i, t->geo[i].buswidth);
		DEBUG(2, "region: %d erase_block = %u", i, t->geo[i].erase_block);
		DEBUG(2, "region: %d read_block = %u", i, t->geo[i].read_block);
		DEBUG(2, "region: %d write_block = %u", i, t->geo[i].write_block);
		DEBUG(2, "region: %d partition = %u", i, t->geo[i].partition);
		DEBUG(2, "region: %d interleave = %u", i, t->geo[i].interleave);
	}
	return 0;
}
427 428


429
static void card_settings(struct pcmciamtd_dev *dev, struct pcmcia_device *p_dev, int *new_name)
430 431 432 433 434 435 436 437 438 439
{
	int i;

	if (p_dev->prod_id[0]) {
		dev->mtd_name[0] = '\0';
		for (i = 0; i < 4; i++) {
			if (i)
				strcat(dev->mtd_name, " ");
			if (p_dev->prod_id[i])
				strcat(dev->mtd_name, p_dev->prod_id[i]);
L
Linus Torvalds 已提交
440
		}
441
		DEBUG(2, "Found name: %s", dev->mtd_name);
L
Linus Torvalds 已提交
442
	}
443 444 445 446 447 448 449 450

#ifdef CONFIG_MTD_DEBUG
	pcmcia_loop_tuple(p_dev, CISTPL_FORMAT, pcmciamtd_cistpl_format, NULL);
	pcmcia_loop_tuple(p_dev, CISTPL_JEDEC_C, pcmciamtd_cistpl_jedec, NULL);
#endif
	pcmcia_loop_tuple(p_dev, CISTPL_DEVICE, pcmciamtd_cistpl_device, dev);
	pcmcia_loop_tuple(p_dev, CISTPL_DEVICE_GEO, pcmciamtd_cistpl_geo, dev);

L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464
	if(!dev->pcmcia_map.size)
		dev->pcmcia_map.size = MAX_PCMCIA_ADDR;

	if(!dev->pcmcia_map.bankwidth)
		dev->pcmcia_map.bankwidth = 2;

	if(force_size) {
		dev->pcmcia_map.size = force_size << 20;
		DEBUG(2, "size forced to %dM", force_size);
	}

	if(bankwidth) {
		dev->pcmcia_map.bankwidth = bankwidth;
		DEBUG(2, "bankwidth forced to %d", bankwidth);
465
	}
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473

	dev->pcmcia_map.name = dev->mtd_name;
	if(!dev->mtd_name[0]) {
		strcpy(dev->mtd_name, "PCMCIA Memory card");
		*new_name = 1;
	}

	DEBUG(1, "Device: Size: %lu Width:%d Name: %s",
474 475
	      dev->pcmcia_map.size,
	      dev->pcmcia_map.bankwidth << 3, dev->mtd_name);
L
Linus Torvalds 已提交
476 477 478 479 480 481 482 483
}


/* pcmciamtd_config() is scheduled to run after a CARD_INSERTION event
 * is received, to configure the PCMCIA socket, and to make the
 * MTD device available to the system.
 */

484
static int pcmciamtd_config(struct pcmcia_device *link)
L
Linus Torvalds 已提交
485 486 487 488
{
	struct pcmciamtd_dev *dev = link->priv;
	struct mtd_info *mtd = NULL;
	int ret;
489
	int i, j = 0;
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	static char *probes[] = { "jedec_probe", "cfi_probe" };
	int new_name = 0;

	DEBUG(3, "link=0x%p", link);

	card_settings(dev, link, &new_name);

	dev->pcmcia_map.phys = NO_XIP;
	dev->pcmcia_map.copy_from = pcmcia_copy_from_remap;
	dev->pcmcia_map.copy_to = pcmcia_copy_to_remap;
	if (dev->pcmcia_map.bankwidth == 1) {
		dev->pcmcia_map.read = pcmcia_read8_remap;
		dev->pcmcia_map.write = pcmcia_write8_remap;
	} else {
		dev->pcmcia_map.read = pcmcia_read16_remap;
		dev->pcmcia_map.write = pcmcia_write16_remap;
	}
	if(setvpp == 1)
		dev->pcmcia_map.set_vpp = pcmciamtd_set_vpp;

510 511 512 513 514
	/* Request a memory window for PCMCIA. Some architeures can map windows
	 * upto the maximum that PCMCIA can support (64MiB) - this is ideal and
	 * we aim for a window the size of the whole card - otherwise we try
	 * smaller windows until we succeed
	 */
L
Linus Torvalds 已提交
515

516 517 518 519 520 521
	link->resource[2]->flags |=  WIN_MEMORY_TYPE_CM | WIN_ENABLE;
	link->resource[2]->flags |= (dev->pcmcia_map.bankwidth == 1) ?
					WIN_DATA_WIDTH_8 : WIN_DATA_WIDTH_16;
	link->resource[2]->start = 0;
	link->resource[2]->end = (force_size) ? force_size << 20 :
					MAX_PCMCIA_ADDR;
L
Linus Torvalds 已提交
522 523 524 525
	dev->win_size = 0;

	do {
		int ret;
526 527 528 529
		DEBUG(2, "requesting window with size = %luKiB memspeed = %d",
			(unsigned long) resource_size(link->resource[2]) >> 10,
			mem_speed);
		ret = pcmcia_request_window(link, link->resource[2], mem_speed);
L
Linus Torvalds 已提交
530 531
		DEBUG(2, "ret = %d dev->win_size = %d", ret, dev->win_size);
		if(ret) {
532 533 534 535 536
			j++;
			link->resource[2]->start = 0;
			link->resource[2]->end = (force_size) ?
					force_size << 20 : MAX_PCMCIA_ADDR;
			link->resource[2]->end >>= j;
L
Linus Torvalds 已提交
537
		} else {
538 539 540
			DEBUG(2, "Got window of size %luKiB", (unsigned long)
				resource_size(link->resource[2]) >> 10);
			dev->win_size = resource_size(link->resource[2]);
L
Linus Torvalds 已提交
541 542
			break;
		}
543
	} while (link->resource[2]->end >= 0x1000);
L
Linus Torvalds 已提交
544 545 546 547

	DEBUG(2, "dev->win_size = %d", dev->win_size);

	if(!dev->win_size) {
548
		dev_err(&dev->p_dev->dev, "Cannot allocate memory window\n");
L
Linus Torvalds 已提交
549
		pcmciamtd_release(link);
550
		return -ENODEV;
L
Linus Torvalds 已提交
551 552
	}
	DEBUG(1, "Allocated a window of %dKiB", dev->win_size >> 10);
553

L
Linus Torvalds 已提交
554
	/* Get write protect status */
555 556
	dev->win_base = ioremap(link->resource[2]->start,
				resource_size(link->resource[2]));
L
Linus Torvalds 已提交
557
	if(!dev->win_base) {
558 559
		dev_err(&dev->p_dev->dev, "ioremap(%pR) failed\n",
			link->resource[2]);
L
Linus Torvalds 已提交
560
		pcmciamtd_release(link);
561
		return -ENODEV;
L
Linus Torvalds 已提交
562
	}
563 564
	DEBUG(1, "mapped window dev = %p @ %pR, base = %p",
	      dev, link->resource[2], dev->win_base);
L
Linus Torvalds 已提交
565 566 567

	dev->offset = 0;
	dev->pcmcia_map.map_priv_1 = (unsigned long)dev;
568
	dev->pcmcia_map.map_priv_2 = (unsigned long)link->resource[2];
L
Linus Torvalds 已提交
569

570
	dev->vpp = (vpp) ? vpp : link->socket->socket.Vpp;
L
Linus Torvalds 已提交
571 572
	link->conf.Attributes = 0;
	if(setvpp == 2) {
573
		link->vpp = dev->vpp;
L
Linus Torvalds 已提交
574
	} else {
575
		link->vpp = 0;
L
Linus Torvalds 已提交
576 577 578 579
	}

	link->conf.ConfigIndex = 0;
	DEBUG(2, "Setting Configuration");
580
	ret = pcmcia_request_configuration(link, &link->conf);
D
Dominik Brodowski 已提交
581
	if (ret != 0) {
582 583 584 585
		if (dev->win_base) {
			iounmap(dev->win_base);
			dev->win_base = NULL;
		}
586
		return -ENODEV;
L
Linus Torvalds 已提交
587 588 589 590 591 592 593
	}

	if(mem_type == 1) {
		mtd = do_map_probe("map_ram", &dev->pcmcia_map);
	} else if(mem_type == 2) {
		mtd = do_map_probe("map_rom", &dev->pcmcia_map);
	} else {
594
		for(i = 0; i < ARRAY_SIZE(probes); i++) {
L
Linus Torvalds 已提交
595 596 597 598
			DEBUG(1, "Trying %s", probes[i]);
			mtd = do_map_probe(probes[i], &dev->pcmcia_map);
			if(mtd)
				break;
599

L
Linus Torvalds 已提交
600 601 602
			DEBUG(1, "FAILED: %s", probes[i]);
		}
	}
603

L
Linus Torvalds 已提交
604
	if(!mtd) {
605
		DEBUG(1, "Can not find an MTD");
L
Linus Torvalds 已提交
606
		pcmciamtd_release(link);
607
		return -ENODEV;
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615
	}

	dev->mtd_info = mtd;
	mtd->owner = THIS_MODULE;

	if(new_name) {
		int size = 0;
		char unit = ' ';
616 617 618
		/* Since we are using a default name, make it better by adding
		 * in the size
		 */
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		if(mtd->size < 1048576) { /* <1MiB in size, show size in KiB */
			size = mtd->size >> 10;
			unit = 'K';
		} else {
			size = mtd->size >> 20;
			unit = 'M';
		}
		snprintf(dev->mtd_name, sizeof(dev->mtd_name), "%d%ciB %s", size, unit, "PCMCIA Memory card");
	}

	/* If the memory found is fits completely into the mapped PCMCIA window,
	   use the faster non-remapping read/write functions */
	if(mtd->size <= dev->win_size) {
		DEBUG(1, "Using non remapping memory functions");
		dev->pcmcia_map.map_priv_2 = (unsigned long)dev->win_base;
		if (dev->pcmcia_map.bankwidth == 1) {
			dev->pcmcia_map.read = pcmcia_read8;
			dev->pcmcia_map.write = pcmcia_write8;
		} else {
			dev->pcmcia_map.read = pcmcia_read16;
			dev->pcmcia_map.write = pcmcia_write16;
		}
		dev->pcmcia_map.copy_from = pcmcia_copy_from;
		dev->pcmcia_map.copy_to = pcmcia_copy_to;
	}

	if(add_mtd_device(mtd)) {
		map_destroy(mtd);
		dev->mtd_info = NULL;
648 649
		dev_err(&dev->p_dev->dev,
			"Could not register the MTD device\n");
L
Linus Torvalds 已提交
650
		pcmciamtd_release(link);
651
		return -ENODEV;
L
Linus Torvalds 已提交
652
	}
653
	dev_info(&dev->p_dev->dev, "mtd%d: %s\n", mtd->index, mtd->name);
654
	return 0;
L
Linus Torvalds 已提交
655

656
	dev_err(&dev->p_dev->dev, "CS Error, exiting\n");
L
Linus Torvalds 已提交
657
	pcmciamtd_release(link);
658
	return -ENODEV;
L
Linus Torvalds 已提交
659 660 661
}


662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
static int pcmciamtd_suspend(struct pcmcia_device *dev)
{
	DEBUG(2, "EVENT_PM_RESUME");

	/* get_lock(link); */

	return 0;
}

static int pcmciamtd_resume(struct pcmcia_device *dev)
{
	DEBUG(2, "EVENT_PM_SUSPEND");

	/* free_lock(link); */

	return 0;
}

L
Linus Torvalds 已提交
680 681 682 683 684 685 686

/* This deletes a driver "instance".  The device is de-registered
 * with Card Services.  If it has been released, all local data
 * structures are freed.  Otherwise, the structures will be freed
 * when the device is released.
 */

687
static void pcmciamtd_detach(struct pcmcia_device *link)
L
Linus Torvalds 已提交
688
{
689
	struct pcmciamtd_dev *dev = link->priv;
690

L
Linus Torvalds 已提交
691 692
	DEBUG(3, "link=0x%p", link);

693 694
	if(dev->mtd_info) {
		del_mtd_device(dev->mtd_info);
695 696
		dev_info(&dev->p_dev->dev, "mtd%d: Removing\n",
			 dev->mtd_info->index);
697
		map_destroy(dev->mtd_info);
L
Linus Torvalds 已提交
698
	}
699 700

	pcmciamtd_release(link);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708
}


/* pcmciamtd_attach() creates an "instance" of the driver, allocating
 * local data structures for one device.  The device is registered
 * with Card Services.
 */

709
static int pcmciamtd_probe(struct pcmcia_device *link)
L
Linus Torvalds 已提交
710 711 712 713
{
	struct pcmciamtd_dev *dev;

	/* Create new memory card device */
714
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
715
	if (!dev) return -ENOMEM;
L
Linus Torvalds 已提交
716 717
	DEBUG(1, "dev=0x%p", dev);

718
	dev->p_dev = link;
L
Linus Torvalds 已提交
719 720 721 722
	link->priv = dev;

	link->conf.Attributes = 0;

723
	return pcmciamtd_config(link);
L
Linus Torvalds 已提交
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739
static struct pcmcia_device_id pcmciamtd_ids[] = {
	PCMCIA_DEVICE_FUNC_ID(1),
	PCMCIA_DEVICE_PROD_ID123("IO DATA", "PCS-2M", "2MB SRAM", 0x547e66dc, 0x1fed36cd, 0x36eadd21),
	PCMCIA_DEVICE_PROD_ID12("IBM", "2MB SRAM", 0xb569a6e5, 0x36eadd21),
	PCMCIA_DEVICE_PROD_ID12("IBM", "4MB FLASH", 0xb569a6e5, 0x8bc54d2a),
	PCMCIA_DEVICE_PROD_ID12("IBM", "8MB FLASH", 0xb569a6e5, 0x6df1be3e),
	PCMCIA_DEVICE_PROD_ID12("Intel", "S2E20SW", 0x816cc815, 0xd14c9dcf),
	PCMCIA_DEVICE_PROD_ID12("Intel", "S2E8 SW", 0x816cc815, 0xa2d7dedb),
	PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-02 ", 0x40ade711, 0x145cea5c),
	PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-04 ", 0x40ade711, 0x42064dda),
	PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-20 ", 0x40ade711, 0x25ee5cb0),
	PCMCIA_DEVICE_PROD_ID12("intel", "VALUE SERIES 100 ", 0x40ade711, 0xdf8506d8),
	PCMCIA_DEVICE_PROD_ID12("KINGMAX TECHNOLOGY INC.", "SRAM 256K Bytes", 0x54d0c69c, 0xad12c29c),
	PCMCIA_DEVICE_PROD_ID12("Maxtor", "MAXFL MobileMax Flash Memory Card", 0xb68968c8, 0x2dfb47b0),
740
	PCMCIA_DEVICE_PROD_ID123("M-Systems", "M-SYS Flash Memory Card", "(c) M-Systems", 0x7ed2ad87, 0x675dc3fb, 0x7aef3965),
D
Dominik Brodowski 已提交
741
	PCMCIA_DEVICE_PROD_ID12("PRETEC", "  2MB SRAM CARD", 0xebf91155, 0x805360ca),
742 743
	PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB101EN20", 0xf9876baf, 0xad0b207b),
	PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB513EN20", 0xf9876baf, 0xe8d884ad),
744
	PCMCIA_DEVICE_PROD_ID12("SMART Modular Technologies", " 4MB FLASH Card", 0x96fd8277, 0x737a5b05),
745 746 747 748 749 750 751 752 753 754
	PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-3000", 0x05ddca47, 0xe7d67bca),
	PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-4100", 0x05ddca47, 0x7bc32944),
	/* the following was commented out in pcmcia-cs-3.2.7 */
	/* PCMCIA_DEVICE_PROD_ID12("RATOC Systems,Inc.", "SmartMedia ADAPTER PC Card", 0xf4a2fefe, 0x5885b2ae), */
#ifdef CONFIG_MTD_PCMCIA_ANONYMOUS
	{ .match_flags = PCMCIA_DEV_ID_MATCH_ANONYMOUS, },
#endif
	PCMCIA_DEVICE_NULL
};
MODULE_DEVICE_TABLE(pcmcia, pcmciamtd_ids);
L
Linus Torvalds 已提交
755 756 757 758 759

static struct pcmcia_driver pcmciamtd_driver = {
	.drv		= {
		.name	= "pcmciamtd"
	},
760
	.probe		= pcmciamtd_probe,
761
	.remove		= pcmciamtd_detach,
762 763
	.owner		= THIS_MODULE,
	.id_table	= pcmciamtd_ids,
764 765
	.suspend	= pcmciamtd_suspend,
	.resume		= pcmciamtd_resume,
L
Linus Torvalds 已提交
766 767 768 769 770
};


static int __init init_pcmciamtd(void)
{
A
Adrian Bunk 已提交
771
	info(DRIVER_DESC);
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

	if(bankwidth && bankwidth != 1 && bankwidth != 2) {
		info("bad bankwidth (%d), using default", bankwidth);
		bankwidth = 2;
	}
	if(force_size && (force_size < 1 || force_size > 64)) {
		info("bad force_size (%d), using default", force_size);
		force_size = 0;
	}
	if(mem_type && mem_type != 1 && mem_type != 2) {
		info("bad mem_type (%d), using default", mem_type);
		mem_type = 0;
	}
	return pcmcia_register_driver(&pcmciamtd_driver);
}


static void __exit exit_pcmciamtd(void)
{
	DEBUG(1, DRIVER_DESC " unloading");
	pcmcia_unregister_driver(&pcmciamtd_driver);
}

module_init(init_pcmciamtd);
module_exit(exit_pcmciamtd);