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