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