blob: 689d6a79ffc0ed239b3ccdbae50e035b723c7351 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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
28static int debug = CONFIG_MTD_DEBUG_VERBOSE;
Rusty Russell8d3b33f2006-03-25 03:07:05 -080029module_param(debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030MODULE_PARM_DESC(debug, "Set Debug Level 0=quiet, 5=noisy");
31#undef DEBUG
32#define DEBUG(n, format, arg...) \
33 if (n <= debug) { \
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -070034 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __func__ , ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 }
36
37#else
38#undef DEBUG
39#define DEBUG(n, arg...)
40static 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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Size of the PCMCIA address space: 26 bits = 64 MB */
51#define MAX_PCMCIA_ADDR 0x4000000
52
53struct pcmciamtd_dev {
Dominik Brodowskifd238232006-03-05 10:45:09 +010054 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Module parameters */
67
68/* 2 = do 16-bit transfers, 1 = do 8-bit transfers */
69static int bankwidth = 2;
70
71/* Speed of memory accesses, in ns */
72static int mem_speed;
73
74/* Force the size of an SRAM card */
75static int force_size;
76
77/* Force Vpp */
78static int vpp;
79
80/* Set Vpp */
81static int setvpp;
82
83/* Force card to be treated as FLASH, ROM or RAM */
84static int mem_type;
85
86MODULE_LICENSE("GPL");
87MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
88MODULE_DESCRIPTION(DRIVER_DESC);
Rusty Russell8d3b33f2006-03-25 03:07:05 -080089module_param(bankwidth, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_PARM_DESC(bankwidth, "Set bankwidth (1=8 bit, 2=16 bit, default=2)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080091module_param(mem_speed, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(mem_speed, "Set memory access speed in ns");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080093module_param(force_size, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(force_size, "Force size of card in MiB (1-64)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080095module_param(setvpp, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096MODULE_PARM_DESC(setvpp, "Set Vpp (0=Never, 1=On writes, 2=Always on, default=0)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080097module_param(vpp, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098MODULE_PARM_DESC(vpp, "Vpp value in 1/10ths eg 33=3.3V 120=12V (Dangerous)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080099module_param(mem_type, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100MODULE_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 */
104static 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
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100111 if (!pcmcia_dev_present(dev->p_dev)) {
112 DEBUG(1, "device removed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 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;
Magnus Damm868575d2006-12-13 19:46:43 +0900121 ret = pcmcia_map_mem_page(dev->p_dev, win, &mrq);
Dominik Brodowski9b44de22009-10-24 15:55:39 +0200122 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dev->offset = mrq.CardOffset;
125 }
126 return dev->win_base + (to & (dev->win_size-1));
127}
128
129
130static map_word pcmcia_read8_remap(struct map_info *map, unsigned long ofs)
131{
132 caddr_t addr;
133 map_word d = {{0}};
134
135 addr = remap_window(map, ofs);
136 if(!addr)
137 return d;
138
139 d.x[0] = readb(addr);
140 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02x", ofs, addr, d.x[0]);
141 return d;
142}
143
144
145static map_word pcmcia_read16_remap(struct map_info *map, unsigned long ofs)
146{
147 caddr_t addr;
148 map_word d = {{0}};
149
150 addr = remap_window(map, ofs);
151 if(!addr)
152 return d;
153
154 d.x[0] = readw(addr);
155 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04x", ofs, addr, d.x[0]);
156 return d;
157}
158
159
160static void pcmcia_copy_from_remap(struct map_info *map, void *to, unsigned long from, ssize_t len)
161{
162 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
163 unsigned long win_size = dev->win_size;
164
165 DEBUG(3, "to = %p from = %lu len = %u", to, from, len);
166 while(len) {
167 int toread = win_size - (from & (win_size-1));
168 caddr_t addr;
169
170 if(toread > len)
171 toread = len;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 addr = remap_window(map, from);
174 if(!addr)
175 return;
176
177 DEBUG(4, "memcpy from %p to %p len = %d", addr, to, toread);
178 memcpy_fromio(to, addr, toread);
179 len -= toread;
180 to += toread;
181 from += toread;
182 }
183}
184
185
186static void pcmcia_write8_remap(struct map_info *map, map_word d, unsigned long adr)
187{
188 caddr_t addr = remap_window(map, adr);
189
190 if(!addr)
191 return;
192
193 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%02x", adr, addr, d.x[0]);
194 writeb(d.x[0], addr);
195}
196
197
198static void pcmcia_write16_remap(struct map_info *map, map_word d, unsigned long adr)
199{
200 caddr_t addr = remap_window(map, adr);
201 if(!addr)
202 return;
203
204 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%04x", adr, addr, d.x[0]);
205 writew(d.x[0], addr);
206}
207
208
209static void pcmcia_copy_to_remap(struct map_info *map, unsigned long to, const void *from, ssize_t len)
210{
211 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
212 unsigned long win_size = dev->win_size;
213
214 DEBUG(3, "to = %lu from = %p len = %u", to, from, len);
215 while(len) {
216 int towrite = win_size - (to & (win_size-1));
217 caddr_t addr;
218
219 if(towrite > len)
220 towrite = len;
221
222 addr = remap_window(map, to);
223 if(!addr)
224 return;
225
226 DEBUG(4, "memcpy from %p to %p len = %d", from, addr, towrite);
227 memcpy_toio(addr, from, towrite);
228 len -= towrite;
229 to += towrite;
230 from += towrite;
231 }
232}
233
234
235/* read/write{8,16} copy_{from,to} routines with direct access */
236
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100237#define DEV_REMOVED(x) (!(pcmcia_dev_present(((struct pcmciamtd_dev *)map->map_priv_1)->p_dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239static map_word pcmcia_read8(struct map_info *map, unsigned long ofs)
240{
241 caddr_t win_base = (caddr_t)map->map_priv_2;
242 map_word d = {{0}};
243
244 if(DEV_REMOVED(map))
245 return d;
246
247 d.x[0] = readb(win_base + ofs);
248 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02x", ofs, win_base + ofs, d.x[0]);
249 return d;
250}
251
252
253static map_word pcmcia_read16(struct map_info *map, unsigned long ofs)
254{
255 caddr_t win_base = (caddr_t)map->map_priv_2;
256 map_word d = {{0}};
257
258 if(DEV_REMOVED(map))
259 return d;
260
261 d.x[0] = readw(win_base + ofs);
262 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04x", ofs, win_base + ofs, d.x[0]);
263 return d;
264}
265
266
267static void pcmcia_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
268{
269 caddr_t win_base = (caddr_t)map->map_priv_2;
270
271 if(DEV_REMOVED(map))
272 return;
273
274 DEBUG(3, "to = %p from = %lu len = %u", to, from, len);
275 memcpy_fromio(to, win_base + from, len);
276}
277
278
279static void pcmcia_write8(struct map_info *map, u8 d, unsigned long adr)
280{
281 caddr_t win_base = (caddr_t)map->map_priv_2;
282
283 if(DEV_REMOVED(map))
284 return;
285
286 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%02x", adr, win_base + adr, d);
287 writeb(d, win_base + adr);
288}
289
290
291static void pcmcia_write16(struct map_info *map, u16 d, unsigned long adr)
292{
293 caddr_t win_base = (caddr_t)map->map_priv_2;
294
295 if(DEV_REMOVED(map))
296 return;
297
298 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%04x", adr, win_base + adr, d);
299 writew(d, win_base + adr);
300}
301
302
303static void pcmcia_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
304{
305 caddr_t win_base = (caddr_t)map->map_priv_2;
306
307 if(DEV_REMOVED(map))
308 return;
309
310 DEBUG(3, "to = %lu from = %p len = %u", to, from, len);
311 memcpy_toio(win_base + to, from, len);
312}
313
314
315static void pcmciamtd_set_vpp(struct map_info *map, int on)
316{
317 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200318 struct pcmcia_device *link = dev->p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 modconf_t mod;
320 int ret;
321
322 mod.Attributes = CONF_VPP1_CHANGE_VALID | CONF_VPP2_CHANGE_VALID;
323 mod.Vcc = 0;
324 mod.Vpp1 = mod.Vpp2 = on ? dev->vpp : 0;
325
326 DEBUG(2, "dev = %p on = %d vpp = %d\n", dev, on, dev->vpp);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200327 ret = pcmcia_modify_configuration(link, &mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330
331/* After a card is removed, pcmciamtd_release() will unregister the
332 * device, and release the PCMCIA configuration. If the device is
333 * still open, this will be postponed until it is closed.
334 */
335
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200336static void pcmciamtd_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct pcmciamtd_dev *dev = link->priv;
339
340 DEBUG(3, "link = 0x%p", link);
341
342 if (link->win) {
343 if(dev->win_base) {
344 iounmap(dev->win_base);
345 dev->win_base = NULL;
346 }
Magnus Dammf5560da2006-12-13 19:46:38 +0900347 pcmcia_release_window(link, link->win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200349 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200353#ifdef CONFIG_MTD_DEBUG
354static int pcmciamtd_cistpl_format(struct pcmcia_device *p_dev,
355 tuple_t *tuple,
356 void *priv_data)
357{
358 cisparse_t parse;
359
360 if (!pcmcia_parse_tuple(tuple, &parse)) {
361 cistpl_format_t *t = &parse.format;
362 (void)t; /* Shut up, gcc */
363 DEBUG(2, "Format type: %u, Error Detection: %u, offset = %u, length =%u",
364 t->type, t->edc, t->offset, t->length);
365 }
366 return -ENOSPC;
367}
368
369static int pcmciamtd_cistpl_jedec(struct pcmcia_device *p_dev,
370 tuple_t *tuple,
371 void *priv_data)
372{
373 cisparse_t parse;
374 int i;
375
376 if (!pcmcia_parse_tuple(tuple, &parse)) {
377 cistpl_jedec_t *t = &parse.jedec;
378 for (i = 0; i < t->nid; i++)
379 DEBUG(2, "JEDEC: 0x%02x 0x%02x", t->id[i].mfr, t->id[i].info);
380 }
381 return -ENOSPC;
382}
383#endif
384
385static int pcmciamtd_cistpl_device(struct pcmcia_device *p_dev,
386 tuple_t *tuple,
387 void *priv_data)
388{
389 struct pcmciamtd_dev *dev = priv_data;
390 cisparse_t parse;
391 cistpl_device_t *t = &parse.device;
392 int i;
393
394 if (pcmcia_parse_tuple(tuple, &parse))
395 return -EINVAL;
396
397 DEBUG(2, "Common memory:");
398 dev->pcmcia_map.size = t->dev[0].size;
399 /* from here on: DEBUG only */
400 for (i = 0; i < t->ndev; i++) {
401 DEBUG(2, "Region %d, type = %u", i, t->dev[i].type);
402 DEBUG(2, "Region %d, wp = %u", i, t->dev[i].wp);
403 DEBUG(2, "Region %d, speed = %u ns", i, t->dev[i].speed);
404 DEBUG(2, "Region %d, size = %u bytes", i, t->dev[i].size);
405 }
406 return 0;
407}
408
409static int pcmciamtd_cistpl_geo(struct pcmcia_device *p_dev,
410 tuple_t *tuple,
411 void *priv_data)
412{
413 struct pcmciamtd_dev *dev = priv_data;
414 cisparse_t parse;
415 cistpl_device_geo_t *t = &parse.device_geo;
416 int i;
417
418 if (pcmcia_parse_tuple(tuple, &parse))
419 return -EINVAL;
420
421 dev->pcmcia_map.bankwidth = t->geo[0].buswidth;
422 /* from here on: DEBUG only */
423 for (i = 0; i < t->ngeo; i++) {
424 DEBUG(2, "region: %d bankwidth = %u", i, t->geo[i].buswidth);
425 DEBUG(2, "region: %d erase_block = %u", i, t->geo[i].erase_block);
426 DEBUG(2, "region: %d read_block = %u", i, t->geo[i].read_block);
427 DEBUG(2, "region: %d write_block = %u", i, t->geo[i].write_block);
428 DEBUG(2, "region: %d partition = %u", i, t->geo[i].partition);
429 DEBUG(2, "region: %d interleave = %u", i, t->geo[i].interleave);
430 }
431 return 0;
432}
433
434
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200435static void card_settings(struct pcmciamtd_dev *dev, struct pcmcia_device *link, int *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200437 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200439 if (p_dev->prod_id[0]) {
440 dev->mtd_name[0] = '\0';
441 for (i = 0; i < 4; i++) {
442 if (i)
443 strcat(dev->mtd_name, " ");
444 if (p_dev->prod_id[i])
445 strcat(dev->mtd_name, p_dev->prod_id[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200447 DEBUG(2, "Found name: %s", dev->mtd_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200449
450#ifdef CONFIG_MTD_DEBUG
451 pcmcia_loop_tuple(p_dev, CISTPL_FORMAT, pcmciamtd_cistpl_format, NULL);
452 pcmcia_loop_tuple(p_dev, CISTPL_JEDEC_C, pcmciamtd_cistpl_jedec, NULL);
453#endif
454 pcmcia_loop_tuple(p_dev, CISTPL_DEVICE, pcmciamtd_cistpl_device, dev);
455 pcmcia_loop_tuple(p_dev, CISTPL_DEVICE_GEO, pcmciamtd_cistpl_geo, dev);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if(!dev->pcmcia_map.size)
458 dev->pcmcia_map.size = MAX_PCMCIA_ADDR;
459
460 if(!dev->pcmcia_map.bankwidth)
461 dev->pcmcia_map.bankwidth = 2;
462
463 if(force_size) {
464 dev->pcmcia_map.size = force_size << 20;
465 DEBUG(2, "size forced to %dM", force_size);
466 }
467
468 if(bankwidth) {
469 dev->pcmcia_map.bankwidth = bankwidth;
470 DEBUG(2, "bankwidth forced to %d", bankwidth);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 dev->pcmcia_map.name = dev->mtd_name;
474 if(!dev->mtd_name[0]) {
475 strcpy(dev->mtd_name, "PCMCIA Memory card");
476 *new_name = 1;
477 }
478
479 DEBUG(1, "Device: Size: %lu Width:%d Name: %s",
480 dev->pcmcia_map.size, dev->pcmcia_map.bankwidth << 3, dev->mtd_name);
481}
482
483
484/* pcmciamtd_config() is scheduled to run after a CARD_INSERTION event
485 * is received, to configure the PCMCIA socket, and to make the
486 * MTD device available to the system.
487 */
488
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200489static int pcmciamtd_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct pcmciamtd_dev *dev = link->priv;
492 struct mtd_info *mtd = NULL;
493 cs_status_t status;
494 win_req_t req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int ret;
496 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 static char *probes[] = { "jedec_probe", "cfi_probe" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int new_name = 0;
499
500 DEBUG(3, "link=0x%p", link);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 card_settings(dev, link, &new_name);
503
504 dev->pcmcia_map.phys = NO_XIP;
505 dev->pcmcia_map.copy_from = pcmcia_copy_from_remap;
506 dev->pcmcia_map.copy_to = pcmcia_copy_to_remap;
507 if (dev->pcmcia_map.bankwidth == 1) {
508 dev->pcmcia_map.read = pcmcia_read8_remap;
509 dev->pcmcia_map.write = pcmcia_write8_remap;
510 } else {
511 dev->pcmcia_map.read = pcmcia_read16_remap;
512 dev->pcmcia_map.write = pcmcia_write16_remap;
513 }
514 if(setvpp == 1)
515 dev->pcmcia_map.set_vpp = pcmciamtd_set_vpp;
516
517 /* Request a memory window for PCMCIA. Some architeures can map windows upto the maximum
518 that PCMCIA can support (64MiB) - this is ideal and we aim for a window the size of the
519 whole card - otherwise we try smaller windows until we succeed */
520
521 req.Attributes = WIN_MEMORY_TYPE_CM | WIN_ENABLE;
522 req.Attributes |= (dev->pcmcia_map.bankwidth == 1) ? WIN_DATA_WIDTH_8 : WIN_DATA_WIDTH_16;
523 req.Base = 0;
524 req.AccessSpeed = mem_speed;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200525 link->win = (window_handle_t)link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 req.Size = (force_size) ? force_size << 20 : MAX_PCMCIA_ADDR;
527 dev->win_size = 0;
528
529 do {
530 int ret;
531 DEBUG(2, "requesting window with size = %dKiB memspeed = %d",
532 req.Size >> 10, req.AccessSpeed);
Dominik Brodowski6838b032009-11-03 01:31:52 +0100533 ret = pcmcia_request_window(link, &req, &link->win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 DEBUG(2, "ret = %d dev->win_size = %d", ret, dev->win_size);
535 if(ret) {
536 req.Size >>= 1;
537 } else {
538 DEBUG(2, "Got window of size %dKiB", req.Size >> 10);
539 dev->win_size = req.Size;
540 break;
541 }
542 } while(req.Size >= 0x1000);
543
544 DEBUG(2, "dev->win_size = %d", dev->win_size);
545
546 if(!dev->win_size) {
547 err("Cant allocate memory window");
548 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200549 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 DEBUG(1, "Allocated a window of %dKiB", dev->win_size >> 10);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Get write protect status */
Dominik Brodowski4aeba012008-06-20 13:24:31 +0200554 DEBUG(2, "window handle = 0x%8.8lx", (unsigned long)link->win);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 dev->win_base = ioremap(req.Base, req.Size);
556 if(!dev->win_base) {
557 err("ioremap(%lu, %u) failed", req.Base, req.Size);
558 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200559 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 DEBUG(1, "mapped window dev = %p req.base = 0x%lx base = %p size = 0x%x",
562 dev, req.Base, dev->win_base, req.Size);
563
564 dev->offset = 0;
565 dev->pcmcia_map.map_priv_1 = (unsigned long)dev;
566 dev->pcmcia_map.map_priv_2 = (unsigned long)link->win;
567
Dominik Brodowskief313e32008-08-02 16:25:26 +0200568 dev->vpp = (vpp) ? vpp : link->socket.socket.Vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 link->conf.Attributes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if(setvpp == 2) {
Dominik Brodowski70294b42006-01-15 12:43:16 +0100571 link->conf.Vpp = dev->vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 } else {
Dominik Brodowski70294b42006-01-15 12:43:16 +0100573 link->conf.Vpp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
576 link->conf.IntType = INT_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 link->conf.ConfigIndex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 DEBUG(2, "Setting Configuration");
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200579 ret = pcmcia_request_configuration(link, &link->conf);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200580 if (ret != 0) {
Amol Lad25f0c652006-09-21 18:12:43 +0530581 if (dev->win_base) {
582 iounmap(dev->win_base);
583 dev->win_base = NULL;
584 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200585 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 if(mem_type == 1) {
589 mtd = do_map_probe("map_ram", &dev->pcmcia_map);
590 } else if(mem_type == 2) {
591 mtd = do_map_probe("map_rom", &dev->pcmcia_map);
592 } else {
Tobias Klauser87d10f32006-03-31 02:29:45 -0800593 for(i = 0; i < ARRAY_SIZE(probes); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 DEBUG(1, "Trying %s", probes[i]);
595 mtd = do_map_probe(probes[i], &dev->pcmcia_map);
596 if(mtd)
597 break;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 DEBUG(1, "FAILED: %s", probes[i]);
600 }
601 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if(!mtd) {
604 DEBUG(1, "Cant find an MTD");
605 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200606 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 dev->mtd_info = mtd;
610 mtd->owner = THIS_MODULE;
611
612 if(new_name) {
613 int size = 0;
614 char unit = ' ';
615 /* Since we are using a default name, make it better by adding in the
616 size */
617 if(mtd->size < 1048576) { /* <1MiB in size, show size in KiB */
618 size = mtd->size >> 10;
619 unit = 'K';
620 } else {
621 size = mtd->size >> 20;
622 unit = 'M';
623 }
624 snprintf(dev->mtd_name, sizeof(dev->mtd_name), "%d%ciB %s", size, unit, "PCMCIA Memory card");
625 }
626
627 /* If the memory found is fits completely into the mapped PCMCIA window,
628 use the faster non-remapping read/write functions */
629 if(mtd->size <= dev->win_size) {
630 DEBUG(1, "Using non remapping memory functions");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 dev->pcmcia_map.map_priv_2 = (unsigned long)dev->win_base;
632 if (dev->pcmcia_map.bankwidth == 1) {
633 dev->pcmcia_map.read = pcmcia_read8;
634 dev->pcmcia_map.write = pcmcia_write8;
635 } else {
636 dev->pcmcia_map.read = pcmcia_read16;
637 dev->pcmcia_map.write = pcmcia_write16;
638 }
639 dev->pcmcia_map.copy_from = pcmcia_copy_from;
640 dev->pcmcia_map.copy_to = pcmcia_copy_to;
641 }
642
643 if(add_mtd_device(mtd)) {
644 map_destroy(mtd);
645 dev->mtd_info = NULL;
646 err("Couldnt register MTD device");
647 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200648 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 snprintf(dev->node.dev_name, sizeof(dev->node.dev_name), "mtd%d", mtd->index);
651 info("mtd%d: %s", mtd->index, mtd->name);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100652 link->dev_node = &dev->node;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dominik Brodowski9b44de22009-10-24 15:55:39 +0200655 failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 err("CS Error, exiting");
657 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200658 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100662static int pcmciamtd_suspend(struct pcmcia_device *dev)
663{
664 DEBUG(2, "EVENT_PM_RESUME");
665
666 /* get_lock(link); */
667
668 return 0;
669}
670
671static int pcmciamtd_resume(struct pcmcia_device *dev)
672{
673 DEBUG(2, "EVENT_PM_SUSPEND");
674
675 /* free_lock(link); */
676
677 return 0;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/* This deletes a driver "instance". The device is de-registered
682 * with Card Services. If it has been released, all local data
683 * structures are freed. Otherwise, the structures will be freed
684 * when the device is released.
685 */
686
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200687static void pcmciamtd_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100689 struct pcmciamtd_dev *dev = link->priv;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 DEBUG(3, "link=0x%p", link);
692
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100693 if(dev->mtd_info) {
694 del_mtd_device(dev->mtd_info);
Sean Young599fb322006-05-17 11:47:48 +0100695 map_destroy(dev->mtd_info);
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100696 info("mtd%d: Removed", dev->mtd_info->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100698
699 pcmciamtd_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702
703/* pcmciamtd_attach() creates an "instance" of the driver, allocating
704 * local data structures for one device. The device is registered
705 * with Card Services.
706 */
707
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200708static int pcmciamtd_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct pcmciamtd_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* Create new memory card device */
Burman Yan95b93a02006-11-15 21:10:29 +0200713 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100714 if (!dev) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 DEBUG(1, "dev=0x%p", dev);
716
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200717 dev->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 link->priv = dev;
719
720 link->conf.Attributes = 0;
721 link->conf.IntType = INT_MEMORY;
722
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200723 return pcmciamtd_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700726static struct pcmcia_device_id pcmciamtd_ids[] = {
727 PCMCIA_DEVICE_FUNC_ID(1),
728 PCMCIA_DEVICE_PROD_ID123("IO DATA", "PCS-2M", "2MB SRAM", 0x547e66dc, 0x1fed36cd, 0x36eadd21),
729 PCMCIA_DEVICE_PROD_ID12("IBM", "2MB SRAM", 0xb569a6e5, 0x36eadd21),
730 PCMCIA_DEVICE_PROD_ID12("IBM", "4MB FLASH", 0xb569a6e5, 0x8bc54d2a),
731 PCMCIA_DEVICE_PROD_ID12("IBM", "8MB FLASH", 0xb569a6e5, 0x6df1be3e),
732 PCMCIA_DEVICE_PROD_ID12("Intel", "S2E20SW", 0x816cc815, 0xd14c9dcf),
733 PCMCIA_DEVICE_PROD_ID12("Intel", "S2E8 SW", 0x816cc815, 0xa2d7dedb),
734 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-02 ", 0x40ade711, 0x145cea5c),
735 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-04 ", 0x40ade711, 0x42064dda),
736 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-20 ", 0x40ade711, 0x25ee5cb0),
737 PCMCIA_DEVICE_PROD_ID12("intel", "VALUE SERIES 100 ", 0x40ade711, 0xdf8506d8),
738 PCMCIA_DEVICE_PROD_ID12("KINGMAX TECHNOLOGY INC.", "SRAM 256K Bytes", 0x54d0c69c, 0xad12c29c),
739 PCMCIA_DEVICE_PROD_ID12("Maxtor", "MAXFL MobileMax Flash Memory Card", 0xb68968c8, 0x2dfb47b0),
740 PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB101EN20", 0xf9876baf, 0xad0b207b),
741 PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB513EN20", 0xf9876baf, 0xe8d884ad),
742 PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-3000", 0x05ddca47, 0xe7d67bca),
743 PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-4100", 0x05ddca47, 0x7bc32944),
744 /* the following was commented out in pcmcia-cs-3.2.7 */
745 /* PCMCIA_DEVICE_PROD_ID12("RATOC Systems,Inc.", "SmartMedia ADAPTER PC Card", 0xf4a2fefe, 0x5885b2ae), */
746#ifdef CONFIG_MTD_PCMCIA_ANONYMOUS
747 { .match_flags = PCMCIA_DEV_ID_MATCH_ANONYMOUS, },
748#endif
749 PCMCIA_DEVICE_NULL
750};
751MODULE_DEVICE_TABLE(pcmcia, pcmciamtd_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753static struct pcmcia_driver pcmciamtd_driver = {
754 .drv = {
755 .name = "pcmciamtd"
756 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200757 .probe = pcmciamtd_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100758 .remove = pcmciamtd_detach,
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700759 .owner = THIS_MODULE,
760 .id_table = pcmciamtd_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100761 .suspend = pcmciamtd_suspend,
762 .resume = pcmciamtd_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763};
764
765
766static int __init init_pcmciamtd(void)
767{
Adrian Bunk59018b62008-05-20 01:03:52 +0300768 info(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if(bankwidth && bankwidth != 1 && bankwidth != 2) {
771 info("bad bankwidth (%d), using default", bankwidth);
772 bankwidth = 2;
773 }
774 if(force_size && (force_size < 1 || force_size > 64)) {
775 info("bad force_size (%d), using default", force_size);
776 force_size = 0;
777 }
778 if(mem_type && mem_type != 1 && mem_type != 2) {
779 info("bad mem_type (%d), using default", mem_type);
780 mem_type = 0;
781 }
782 return pcmcia_register_driver(&pcmciamtd_driver);
783}
784
785
786static void __exit exit_pcmciamtd(void)
787{
788 DEBUG(1, DRIVER_DESC " unloading");
789 pcmcia_unregister_driver(&pcmciamtd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792module_init(init_pcmciamtd);
793module_exit(exit_pcmciamtd);