xref: /openbmc/linux/drivers/mtd/maps/pcmciamtd.c (revision 384740dc)
1 /*
2  * pcmciamtd.c - MTD driver for PCMCIA flash memory cards
3  *
4  * Author: Simon Evans <spse@secret.org.uk>
5  *
6  * Copyright (C) 2002 Simon Evans
7  *
8  * Licence: GPL
9  *
10  */
11 
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/timer.h>
15 #include <linux/init.h>
16 #include <asm/io.h>
17 #include <asm/system.h>
18 
19 #include <pcmcia/cs_types.h>
20 #include <pcmcia/cs.h>
21 #include <pcmcia/cistpl.h>
22 #include <pcmcia/ds.h>
23 
24 #include <linux/mtd/map.h>
25 #include <linux/mtd/mtd.h>
26 
27 #ifdef CONFIG_MTD_DEBUG
28 static int debug = CONFIG_MTD_DEBUG_VERBOSE;
29 module_param(debug, int, 0);
30 MODULE_PARM_DESC(debug, "Set Debug Level 0=quiet, 5=noisy");
31 #undef DEBUG
32 #define DEBUG(n, format, arg...) \
33 	if (n <= debug) {	 \
34 		printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __func__ , ## arg); \
35 	}
36 
37 #else
38 #undef DEBUG
39 #define DEBUG(n, arg...)
40 static const int debug = 0;
41 #endif
42 
43 #define err(format, arg...) printk(KERN_ERR "pcmciamtd: " format "\n" , ## arg)
44 #define info(format, arg...) printk(KERN_INFO "pcmciamtd: " format "\n" , ## arg)
45 #define warn(format, arg...) printk(KERN_WARNING "pcmciamtd: " format "\n" , ## arg)
46 
47 
48 #define DRIVER_DESC	"PCMCIA Flash memory card driver"
49 
50 /* Size of the PCMCIA address space: 26 bits = 64 MB */
51 #define MAX_PCMCIA_ADDR	0x4000000
52 
53 struct pcmciamtd_dev {
54 	struct pcmcia_device	*p_dev;
55 	dev_node_t	node;		/* device node */
56 	caddr_t		win_base;	/* ioremapped address of PCMCIA window */
57 	unsigned int	win_size;	/* size of window */
58 	unsigned int	offset;		/* offset into card the window currently points at */
59 	struct map_info	pcmcia_map;
60 	struct mtd_info	*mtd_info;
61 	int		vpp;
62 	char		mtd_name[sizeof(struct cistpl_vers_1_t)];
63 };
64 
65 
66 /* Module parameters */
67 
68 /* 2 = do 16-bit transfers, 1 = do 8-bit transfers */
69 static int bankwidth = 2;
70 
71 /* Speed of memory accesses, in ns */
72 static int mem_speed;
73 
74 /* Force the size of an SRAM card */
75 static int force_size;
76 
77 /* Force Vpp */
78 static int vpp;
79 
80 /* Set Vpp */
81 static int setvpp;
82 
83 /* Force card to be treated as FLASH, ROM or RAM */
84 static int mem_type;
85 
86 MODULE_LICENSE("GPL");
87 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
88 MODULE_DESCRIPTION(DRIVER_DESC);
89 module_param(bankwidth, int, 0);
90 MODULE_PARM_DESC(bankwidth, "Set bankwidth (1=8 bit, 2=16 bit, default=2)");
91 module_param(mem_speed, int, 0);
92 MODULE_PARM_DESC(mem_speed, "Set memory access speed in ns");
93 module_param(force_size, int, 0);
94 MODULE_PARM_DESC(force_size, "Force size of card in MiB (1-64)");
95 module_param(setvpp, int, 0);
96 MODULE_PARM_DESC(setvpp, "Set Vpp (0=Never, 1=On writes, 2=Always on, default=0)");
97 module_param(vpp, int, 0);
98 MODULE_PARM_DESC(vpp, "Vpp value in 1/10ths eg 33=3.3V 120=12V (Dangerous)");
99 module_param(mem_type, int, 0);
100 MODULE_PARM_DESC(mem_type, "Set Memory type (0=Flash, 1=RAM, 2=ROM, default=0)");
101 
102 
103 /* read/write{8,16} copy_{from,to} routines with window remapping to access whole card */
104 static caddr_t remap_window(struct map_info *map, unsigned long to)
105 {
106 	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
107 	window_handle_t win = (window_handle_t)map->map_priv_2;
108 	memreq_t mrq;
109 	int ret;
110 
111 	if (!pcmcia_dev_present(dev->p_dev)) {
112 		DEBUG(1, "device removed");
113 		return 0;
114 	}
115 
116 	mrq.CardOffset = to & ~(dev->win_size-1);
117 	if(mrq.CardOffset != dev->offset) {
118 		DEBUG(2, "Remapping window from 0x%8.8x to 0x%8.8x",
119 		      dev->offset, mrq.CardOffset);
120 		mrq.Page = 0;
121 		if( (ret = pcmcia_map_mem_page(win, &mrq)) != CS_SUCCESS) {
122 			cs_error(dev->p_dev, MapMemPage, ret);
123 			return NULL;
124 		}
125 		dev->offset = mrq.CardOffset;
126 	}
127 	return dev->win_base + (to & (dev->win_size-1));
128 }
129 
130 
131 static map_word pcmcia_read8_remap(struct map_info *map, unsigned long ofs)
132 {
133 	caddr_t addr;
134 	map_word d = {{0}};
135 
136 	addr = remap_window(map, ofs);
137 	if(!addr)
138 		return d;
139 
140 	d.x[0] = readb(addr);
141 	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02x", ofs, addr, d.x[0]);
142 	return d;
143 }
144 
145 
146 static map_word pcmcia_read16_remap(struct map_info *map, unsigned long ofs)
147 {
148 	caddr_t addr;
149 	map_word d = {{0}};
150 
151 	addr = remap_window(map, ofs);
152 	if(!addr)
153 		return d;
154 
155 	d.x[0] = readw(addr);
156 	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04x", ofs, addr, d.x[0]);
157 	return d;
158 }
159 
160 
161 static void pcmcia_copy_from_remap(struct map_info *map, void *to, unsigned long from, ssize_t len)
162 {
163 	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
164 	unsigned long win_size = dev->win_size;
165 
166 	DEBUG(3, "to = %p from = %lu len = %u", to, from, len);
167 	while(len) {
168 		int toread = win_size - (from & (win_size-1));
169 		caddr_t addr;
170 
171 		if(toread > len)
172 			toread = len;
173 
174 		addr = remap_window(map, from);
175 		if(!addr)
176 			return;
177 
178 		DEBUG(4, "memcpy from %p to %p len = %d", addr, to, toread);
179 		memcpy_fromio(to, addr, toread);
180 		len -= toread;
181 		to += toread;
182 		from += toread;
183 	}
184 }
185 
186 
187 static void pcmcia_write8_remap(struct map_info *map, map_word d, unsigned long adr)
188 {
189 	caddr_t addr = remap_window(map, adr);
190 
191 	if(!addr)
192 		return;
193 
194 	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%02x", adr, addr, d.x[0]);
195 	writeb(d.x[0], addr);
196 }
197 
198 
199 static void pcmcia_write16_remap(struct map_info *map, map_word d, unsigned long adr)
200 {
201 	caddr_t addr = remap_window(map, adr);
202 	if(!addr)
203 		return;
204 
205 	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%04x", adr, addr, d.x[0]);
206 	writew(d.x[0], addr);
207 }
208 
209 
210 static void pcmcia_copy_to_remap(struct map_info *map, unsigned long to, const void *from, ssize_t len)
211 {
212 	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
213 	unsigned long win_size = dev->win_size;
214 
215 	DEBUG(3, "to = %lu from = %p len = %u", to, from, len);
216 	while(len) {
217 		int towrite = win_size - (to & (win_size-1));
218 		caddr_t addr;
219 
220 		if(towrite > len)
221 			towrite = len;
222 
223 		addr = remap_window(map, to);
224 		if(!addr)
225 			return;
226 
227 		DEBUG(4, "memcpy from %p to %p len = %d", from, addr, towrite);
228 		memcpy_toio(addr, from, towrite);
229 		len -= towrite;
230 		to += towrite;
231 		from += towrite;
232 	}
233 }
234 
235 
236 /* read/write{8,16} copy_{from,to} routines with direct access */
237 
238 #define DEV_REMOVED(x)  (!(pcmcia_dev_present(((struct pcmciamtd_dev *)map->map_priv_1)->p_dev)))
239 
240 static map_word pcmcia_read8(struct map_info *map, unsigned long ofs)
241 {
242 	caddr_t win_base = (caddr_t)map->map_priv_2;
243 	map_word d = {{0}};
244 
245 	if(DEV_REMOVED(map))
246 		return d;
247 
248 	d.x[0] = readb(win_base + ofs);
249 	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02x", ofs, win_base + ofs, d.x[0]);
250 	return d;
251 }
252 
253 
254 static map_word pcmcia_read16(struct map_info *map, unsigned long ofs)
255 {
256 	caddr_t win_base = (caddr_t)map->map_priv_2;
257 	map_word d = {{0}};
258 
259 	if(DEV_REMOVED(map))
260 		return d;
261 
262 	d.x[0] = readw(win_base + ofs);
263 	DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04x", ofs, win_base + ofs, d.x[0]);
264 	return d;
265 }
266 
267 
268 static void pcmcia_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
269 {
270 	caddr_t win_base = (caddr_t)map->map_priv_2;
271 
272 	if(DEV_REMOVED(map))
273 		return;
274 
275 	DEBUG(3, "to = %p from = %lu len = %u", to, from, len);
276 	memcpy_fromio(to, win_base + from, len);
277 }
278 
279 
280 static void pcmcia_write8(struct map_info *map, u8 d, unsigned long adr)
281 {
282 	caddr_t win_base = (caddr_t)map->map_priv_2;
283 
284 	if(DEV_REMOVED(map))
285 		return;
286 
287 	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%02x", adr, win_base + adr, d);
288 	writeb(d, win_base + adr);
289 }
290 
291 
292 static void pcmcia_write16(struct map_info *map, u16 d, unsigned long adr)
293 {
294 	caddr_t win_base = (caddr_t)map->map_priv_2;
295 
296 	if(DEV_REMOVED(map))
297 		return;
298 
299 	DEBUG(3, "adr = 0x%08lx (%p)  data = 0x%04x", adr, win_base + adr, d);
300 	writew(d, win_base + adr);
301 }
302 
303 
304 static void pcmcia_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
305 {
306 	caddr_t win_base = (caddr_t)map->map_priv_2;
307 
308 	if(DEV_REMOVED(map))
309 		return;
310 
311 	DEBUG(3, "to = %lu from = %p len = %u", to, from, len);
312 	memcpy_toio(win_base + to, from, len);
313 }
314 
315 
316 static void pcmciamtd_set_vpp(struct map_info *map, int on)
317 {
318 	struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
319 	struct pcmcia_device *link = dev->p_dev;
320 	modconf_t mod;
321 	int ret;
322 
323 	mod.Attributes = CONF_VPP1_CHANGE_VALID | CONF_VPP2_CHANGE_VALID;
324 	mod.Vcc = 0;
325 	mod.Vpp1 = mod.Vpp2 = on ? dev->vpp : 0;
326 
327 	DEBUG(2, "dev = %p on = %d vpp = %d\n", dev, on, dev->vpp);
328 	ret = pcmcia_modify_configuration(link, &mod);
329 	if(ret != CS_SUCCESS) {
330 		cs_error(link, ModifyConfiguration, ret);
331 	}
332 }
333 
334 
335 /* After a card is removed, pcmciamtd_release() will unregister the
336  * device, and release the PCMCIA configuration.  If the device is
337  * still open, this will be postponed until it is closed.
338  */
339 
340 static void pcmciamtd_release(struct pcmcia_device *link)
341 {
342 	struct pcmciamtd_dev *dev = link->priv;
343 
344 	DEBUG(3, "link = 0x%p", link);
345 
346 	if (link->win) {
347 		if(dev->win_base) {
348 			iounmap(dev->win_base);
349 			dev->win_base = NULL;
350 		}
351 		pcmcia_release_window(link->win);
352 	}
353 	pcmcia_disable_device(link);
354 }
355 
356 
357 static void card_settings(struct pcmciamtd_dev *dev, struct pcmcia_device *link, int *new_name)
358 {
359 	int rc;
360 	tuple_t tuple;
361 	cisparse_t parse;
362 	u_char buf[64];
363 
364 	tuple.Attributes = 0;
365 	tuple.TupleData = (cisdata_t *)buf;
366 	tuple.TupleDataMax = sizeof(buf);
367 	tuple.TupleOffset = 0;
368 	tuple.DesiredTuple = RETURN_FIRST_TUPLE;
369 
370 	rc = pcmcia_get_first_tuple(link, &tuple);
371 	while(rc == CS_SUCCESS) {
372 		rc = pcmcia_get_tuple_data(link, &tuple);
373 		if(rc != CS_SUCCESS) {
374 			cs_error(link, GetTupleData, rc);
375 			break;
376 		}
377 		rc = pcmcia_parse_tuple(link, &tuple, &parse);
378 		if(rc != CS_SUCCESS) {
379 			cs_error(link, ParseTuple, rc);
380 			break;
381 		}
382 
383 		switch(tuple.TupleCode) {
384 		case  CISTPL_FORMAT: {
385 			cistpl_format_t *t = &parse.format;
386 			(void)t; /* Shut up, gcc */
387 			DEBUG(2, "Format type: %u, Error Detection: %u, offset = %u, length =%u",
388 			      t->type, t->edc, t->offset, t->length);
389 			break;
390 
391 		}
392 
393 		case CISTPL_DEVICE: {
394 			cistpl_device_t *t = &parse.device;
395 			int i;
396 			DEBUG(2, "Common memory:");
397 			dev->pcmcia_map.size = t->dev[0].size;
398 			for(i = 0; i < t->ndev; i++) {
399 				DEBUG(2, "Region %d, type = %u", i, t->dev[i].type);
400 				DEBUG(2, "Region %d, wp = %u", i, t->dev[i].wp);
401 				DEBUG(2, "Region %d, speed = %u ns", i, t->dev[i].speed);
402 				DEBUG(2, "Region %d, size = %u bytes", i, t->dev[i].size);
403 			}
404 			break;
405 		}
406 
407 		case CISTPL_VERS_1: {
408 			cistpl_vers_1_t *t = &parse.version_1;
409 			int i;
410 			if(t->ns) {
411 				dev->mtd_name[0] = '\0';
412 				for(i = 0; i < t->ns; i++) {
413 					if(i)
414 						strcat(dev->mtd_name, " ");
415 					strcat(dev->mtd_name, t->str+t->ofs[i]);
416 				}
417 			}
418 			DEBUG(2, "Found name: %s", dev->mtd_name);
419 			break;
420 		}
421 
422 		case CISTPL_JEDEC_C: {
423 			cistpl_jedec_t *t = &parse.jedec;
424 			int i;
425 			for(i = 0; i < t->nid; i++) {
426 				DEBUG(2, "JEDEC: 0x%02x 0x%02x", t->id[i].mfr, t->id[i].info);
427 			}
428 			break;
429 		}
430 
431 		case CISTPL_DEVICE_GEO: {
432 			cistpl_device_geo_t *t = &parse.device_geo;
433 			int i;
434 			dev->pcmcia_map.bankwidth = t->geo[0].buswidth;
435 			for(i = 0; i < t->ngeo; i++) {
436 				DEBUG(2, "region: %d bankwidth = %u", i, t->geo[i].buswidth);
437 				DEBUG(2, "region: %d erase_block = %u", i, t->geo[i].erase_block);
438 				DEBUG(2, "region: %d read_block = %u", i, t->geo[i].read_block);
439 				DEBUG(2, "region: %d write_block = %u", i, t->geo[i].write_block);
440 				DEBUG(2, "region: %d partition = %u", i, t->geo[i].partition);
441 				DEBUG(2, "region: %d interleave = %u", i, t->geo[i].interleave);
442 			}
443 			break;
444 		}
445 
446 		default:
447 			DEBUG(2, "Unknown tuple code %d", tuple.TupleCode);
448 		}
449 
450 		rc = pcmcia_get_next_tuple(link, &tuple);
451 	}
452 	if(!dev->pcmcia_map.size)
453 		dev->pcmcia_map.size = MAX_PCMCIA_ADDR;
454 
455 	if(!dev->pcmcia_map.bankwidth)
456 		dev->pcmcia_map.bankwidth = 2;
457 
458 	if(force_size) {
459 		dev->pcmcia_map.size = force_size << 20;
460 		DEBUG(2, "size forced to %dM", force_size);
461 	}
462 
463 	if(bankwidth) {
464 		dev->pcmcia_map.bankwidth = bankwidth;
465 		DEBUG(2, "bankwidth forced to %d", bankwidth);
466 	}
467 
468 	dev->pcmcia_map.name = dev->mtd_name;
469 	if(!dev->mtd_name[0]) {
470 		strcpy(dev->mtd_name, "PCMCIA Memory card");
471 		*new_name = 1;
472 	}
473 
474 	DEBUG(1, "Device: Size: %lu Width:%d Name: %s",
475 	      dev->pcmcia_map.size, dev->pcmcia_map.bankwidth << 3, dev->mtd_name);
476 }
477 
478 
479 /* pcmciamtd_config() is scheduled to run after a CARD_INSERTION event
480  * is received, to configure the PCMCIA socket, and to make the
481  * MTD device available to the system.
482  */
483 
484 #define CS_CHECK(fn, ret) \
485 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
486 
487 static int pcmciamtd_config(struct pcmcia_device *link)
488 {
489 	struct pcmciamtd_dev *dev = link->priv;
490 	struct mtd_info *mtd = NULL;
491 	cs_status_t status;
492 	win_req_t req;
493 	int last_ret = 0, last_fn = 0;
494 	int ret;
495 	int i;
496 	config_info_t t;
497 	static char *probes[] = { "jedec_probe", "cfi_probe" };
498 	int new_name = 0;
499 
500 	DEBUG(3, "link=0x%p", link);
501 
502 	DEBUG(2, "Validating CIS");
503 	ret = pcmcia_validate_cis(link, NULL);
504 	if(ret != CS_SUCCESS) {
505 		cs_error(link, GetTupleData, ret);
506 	}
507 
508 	card_settings(dev, link, &new_name);
509 
510 	dev->pcmcia_map.phys = NO_XIP;
511 	dev->pcmcia_map.copy_from = pcmcia_copy_from_remap;
512 	dev->pcmcia_map.copy_to = pcmcia_copy_to_remap;
513 	if (dev->pcmcia_map.bankwidth == 1) {
514 		dev->pcmcia_map.read = pcmcia_read8_remap;
515 		dev->pcmcia_map.write = pcmcia_write8_remap;
516 	} else {
517 		dev->pcmcia_map.read = pcmcia_read16_remap;
518 		dev->pcmcia_map.write = pcmcia_write16_remap;
519 	}
520 	if(setvpp == 1)
521 		dev->pcmcia_map.set_vpp = pcmciamtd_set_vpp;
522 
523 	/* Request a memory window for PCMCIA. Some architeures can map windows upto the maximum
524 	   that PCMCIA can support (64MiB) - this is ideal and we aim for a window the size of the
525 	   whole card - otherwise we try smaller windows until we succeed */
526 
527 	req.Attributes =  WIN_MEMORY_TYPE_CM | WIN_ENABLE;
528 	req.Attributes |= (dev->pcmcia_map.bankwidth == 1) ? WIN_DATA_WIDTH_8 : WIN_DATA_WIDTH_16;
529 	req.Base = 0;
530 	req.AccessSpeed = mem_speed;
531 	link->win = (window_handle_t)link;
532 	req.Size = (force_size) ? force_size << 20 : MAX_PCMCIA_ADDR;
533 	dev->win_size = 0;
534 
535 	do {
536 		int ret;
537 		DEBUG(2, "requesting window with size = %dKiB memspeed = %d",
538 		      req.Size >> 10, req.AccessSpeed);
539 		ret = pcmcia_request_window(&link, &req, &link->win);
540 		DEBUG(2, "ret = %d dev->win_size = %d", ret, dev->win_size);
541 		if(ret) {
542 			req.Size >>= 1;
543 		} else {
544 			DEBUG(2, "Got window of size %dKiB", req.Size >> 10);
545 			dev->win_size = req.Size;
546 			break;
547 		}
548 	} while(req.Size >= 0x1000);
549 
550 	DEBUG(2, "dev->win_size = %d", dev->win_size);
551 
552 	if(!dev->win_size) {
553 		err("Cant allocate memory window");
554 		pcmciamtd_release(link);
555 		return -ENODEV;
556 	}
557 	DEBUG(1, "Allocated a window of %dKiB", dev->win_size >> 10);
558 
559 	/* Get write protect status */
560 	DEBUG(2, "window handle = 0x%8.8lx", (unsigned long)link->win);
561 	dev->win_base = ioremap(req.Base, req.Size);
562 	if(!dev->win_base) {
563 		err("ioremap(%lu, %u) failed", req.Base, req.Size);
564 		pcmciamtd_release(link);
565 		return -ENODEV;
566 	}
567 	DEBUG(1, "mapped window dev = %p req.base = 0x%lx base = %p size = 0x%x",
568 	      dev, req.Base, dev->win_base, req.Size);
569 
570 	dev->offset = 0;
571 	dev->pcmcia_map.map_priv_1 = (unsigned long)dev;
572 	dev->pcmcia_map.map_priv_2 = (unsigned long)link->win;
573 
574 	DEBUG(2, "Getting configuration");
575 	CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(link, &t));
576 	DEBUG(2, "Vcc = %d Vpp1 = %d Vpp2 = %d", t.Vcc, t.Vpp1, t.Vpp2);
577 	dev->vpp = (vpp) ? vpp : t.Vpp1;
578 	link->conf.Attributes = 0;
579 	if(setvpp == 2) {
580 		link->conf.Vpp = dev->vpp;
581 	} else {
582 		link->conf.Vpp = 0;
583 	}
584 
585 	link->conf.IntType = INT_MEMORY;
586 	link->conf.ConfigBase = t.ConfigBase;
587 	link->conf.Status = t.Status;
588 	link->conf.Pin = t.Pin;
589 	link->conf.Copy = t.Copy;
590 	link->conf.ExtStatus = t.ExtStatus;
591 	link->conf.ConfigIndex = 0;
592 	link->conf.Present = t.Present;
593 	DEBUG(2, "Setting Configuration");
594 	ret = pcmcia_request_configuration(link, &link->conf);
595 	if(ret != CS_SUCCESS) {
596 		cs_error(link, RequestConfiguration, ret);
597 		if (dev->win_base) {
598 			iounmap(dev->win_base);
599 			dev->win_base = NULL;
600 		}
601 		return -ENODEV;
602 	}
603 
604 	if(mem_type == 1) {
605 		mtd = do_map_probe("map_ram", &dev->pcmcia_map);
606 	} else if(mem_type == 2) {
607 		mtd = do_map_probe("map_rom", &dev->pcmcia_map);
608 	} else {
609 		for(i = 0; i < ARRAY_SIZE(probes); i++) {
610 			DEBUG(1, "Trying %s", probes[i]);
611 			mtd = do_map_probe(probes[i], &dev->pcmcia_map);
612 			if(mtd)
613 				break;
614 
615 			DEBUG(1, "FAILED: %s", probes[i]);
616 		}
617 	}
618 
619 	if(!mtd) {
620 		DEBUG(1, "Cant find an MTD");
621 		pcmciamtd_release(link);
622 		return -ENODEV;
623 	}
624 
625 	dev->mtd_info = mtd;
626 	mtd->owner = THIS_MODULE;
627 
628 	if(new_name) {
629 		int size = 0;
630 		char unit = ' ';
631 		/* Since we are using a default name, make it better by adding in the
632 		   size */
633 		if(mtd->size < 1048576) { /* <1MiB in size, show size in KiB */
634 			size = mtd->size >> 10;
635 			unit = 'K';
636 		} else {
637 			size = mtd->size >> 20;
638 			unit = 'M';
639 		}
640 		snprintf(dev->mtd_name, sizeof(dev->mtd_name), "%d%ciB %s", size, unit, "PCMCIA Memory card");
641 	}
642 
643 	/* If the memory found is fits completely into the mapped PCMCIA window,
644 	   use the faster non-remapping read/write functions */
645 	if(mtd->size <= dev->win_size) {
646 		DEBUG(1, "Using non remapping memory functions");
647 		dev->pcmcia_map.map_priv_2 = (unsigned long)dev->win_base;
648 		if (dev->pcmcia_map.bankwidth == 1) {
649 			dev->pcmcia_map.read = pcmcia_read8;
650 			dev->pcmcia_map.write = pcmcia_write8;
651 		} else {
652 			dev->pcmcia_map.read = pcmcia_read16;
653 			dev->pcmcia_map.write = pcmcia_write16;
654 		}
655 		dev->pcmcia_map.copy_from = pcmcia_copy_from;
656 		dev->pcmcia_map.copy_to = pcmcia_copy_to;
657 	}
658 
659 	if(add_mtd_device(mtd)) {
660 		map_destroy(mtd);
661 		dev->mtd_info = NULL;
662 		err("Couldnt register MTD device");
663 		pcmciamtd_release(link);
664 		return -ENODEV;
665 	}
666 	snprintf(dev->node.dev_name, sizeof(dev->node.dev_name), "mtd%d", mtd->index);
667 	info("mtd%d: %s", mtd->index, mtd->name);
668 	link->dev_node = &dev->node;
669 	return 0;
670 
671  cs_failed:
672 	cs_error(link, last_fn, last_ret);
673 	err("CS Error, exiting");
674 	pcmciamtd_release(link);
675 	return -ENODEV;
676 }
677 
678 
679 static int pcmciamtd_suspend(struct pcmcia_device *dev)
680 {
681 	DEBUG(2, "EVENT_PM_RESUME");
682 
683 	/* get_lock(link); */
684 
685 	return 0;
686 }
687 
688 static int pcmciamtd_resume(struct pcmcia_device *dev)
689 {
690 	DEBUG(2, "EVENT_PM_SUSPEND");
691 
692 	/* free_lock(link); */
693 
694 	return 0;
695 }
696 
697 
698 /* This deletes a driver "instance".  The device is de-registered
699  * with Card Services.  If it has been released, all local data
700  * structures are freed.  Otherwise, the structures will be freed
701  * when the device is released.
702  */
703 
704 static void pcmciamtd_detach(struct pcmcia_device *link)
705 {
706 	struct pcmciamtd_dev *dev = link->priv;
707 
708 	DEBUG(3, "link=0x%p", link);
709 
710 	if(dev->mtd_info) {
711 		del_mtd_device(dev->mtd_info);
712 		map_destroy(dev->mtd_info);
713 		info("mtd%d: Removed", dev->mtd_info->index);
714 	}
715 
716 	pcmciamtd_release(link);
717 }
718 
719 
720 /* pcmciamtd_attach() creates an "instance" of the driver, allocating
721  * local data structures for one device.  The device is registered
722  * with Card Services.
723  */
724 
725 static int pcmciamtd_probe(struct pcmcia_device *link)
726 {
727 	struct pcmciamtd_dev *dev;
728 
729 	/* Create new memory card device */
730 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
731 	if (!dev) return -ENOMEM;
732 	DEBUG(1, "dev=0x%p", dev);
733 
734 	dev->p_dev = link;
735 	link->priv = dev;
736 
737 	link->conf.Attributes = 0;
738 	link->conf.IntType = INT_MEMORY;
739 
740 	return pcmciamtd_config(link);
741 }
742 
743 static struct pcmcia_device_id pcmciamtd_ids[] = {
744 	PCMCIA_DEVICE_FUNC_ID(1),
745 	PCMCIA_DEVICE_PROD_ID123("IO DATA", "PCS-2M", "2MB SRAM", 0x547e66dc, 0x1fed36cd, 0x36eadd21),
746 	PCMCIA_DEVICE_PROD_ID12("IBM", "2MB SRAM", 0xb569a6e5, 0x36eadd21),
747 	PCMCIA_DEVICE_PROD_ID12("IBM", "4MB FLASH", 0xb569a6e5, 0x8bc54d2a),
748 	PCMCIA_DEVICE_PROD_ID12("IBM", "8MB FLASH", 0xb569a6e5, 0x6df1be3e),
749 	PCMCIA_DEVICE_PROD_ID12("Intel", "S2E20SW", 0x816cc815, 0xd14c9dcf),
750 	PCMCIA_DEVICE_PROD_ID12("Intel", "S2E8 SW", 0x816cc815, 0xa2d7dedb),
751 	PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-02 ", 0x40ade711, 0x145cea5c),
752 	PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-04 ", 0x40ade711, 0x42064dda),
753 	PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-20 ", 0x40ade711, 0x25ee5cb0),
754 	PCMCIA_DEVICE_PROD_ID12("intel", "VALUE SERIES 100 ", 0x40ade711, 0xdf8506d8),
755 	PCMCIA_DEVICE_PROD_ID12("KINGMAX TECHNOLOGY INC.", "SRAM 256K Bytes", 0x54d0c69c, 0xad12c29c),
756 	PCMCIA_DEVICE_PROD_ID12("Maxtor", "MAXFL MobileMax Flash Memory Card", 0xb68968c8, 0x2dfb47b0),
757 	PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB101EN20", 0xf9876baf, 0xad0b207b),
758 	PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB513EN20", 0xf9876baf, 0xe8d884ad),
759 	PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-3000", 0x05ddca47, 0xe7d67bca),
760 	PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-4100", 0x05ddca47, 0x7bc32944),
761 	/* the following was commented out in pcmcia-cs-3.2.7 */
762 	/* PCMCIA_DEVICE_PROD_ID12("RATOC Systems,Inc.", "SmartMedia ADAPTER PC Card", 0xf4a2fefe, 0x5885b2ae), */
763 #ifdef CONFIG_MTD_PCMCIA_ANONYMOUS
764 	{ .match_flags = PCMCIA_DEV_ID_MATCH_ANONYMOUS, },
765 #endif
766 	PCMCIA_DEVICE_NULL
767 };
768 MODULE_DEVICE_TABLE(pcmcia, pcmciamtd_ids);
769 
770 static struct pcmcia_driver pcmciamtd_driver = {
771 	.drv		= {
772 		.name	= "pcmciamtd"
773 	},
774 	.probe		= pcmciamtd_probe,
775 	.remove		= pcmciamtd_detach,
776 	.owner		= THIS_MODULE,
777 	.id_table	= pcmciamtd_ids,
778 	.suspend	= pcmciamtd_suspend,
779 	.resume		= pcmciamtd_resume,
780 };
781 
782 
783 static int __init init_pcmciamtd(void)
784 {
785 	info(DRIVER_DESC);
786 
787 	if(bankwidth && bankwidth != 1 && bankwidth != 2) {
788 		info("bad bankwidth (%d), using default", bankwidth);
789 		bankwidth = 2;
790 	}
791 	if(force_size && (force_size < 1 || force_size > 64)) {
792 		info("bad force_size (%d), using default", force_size);
793 		force_size = 0;
794 	}
795 	if(mem_type && mem_type != 1 && mem_type != 2) {
796 		info("bad mem_type (%d), using default", mem_type);
797 		mem_type = 0;
798 	}
799 	return pcmcia_register_driver(&pcmciamtd_driver);
800 }
801 
802 
803 static void __exit exit_pcmciamtd(void)
804 {
805 	DEBUG(1, DRIVER_DESC " unloading");
806 	pcmcia_unregister_driver(&pcmciamtd_driver);
807 }
808 
809 module_init(init_pcmciamtd);
810 module_exit(exit_pcmciamtd);
811