blob: 91702294839942e8c225ed3f3176011a489d2d61 [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/cistpl.h>
20#include <pcmcia/ds.h>
21
22#include <linux/mtd/map.h>
23#include <linux/mtd/mtd.h>
24
25#ifdef CONFIG_MTD_DEBUG
26static int debug = CONFIG_MTD_DEBUG_VERBOSE;
Rusty Russell8d3b33f2006-03-25 03:07:05 -080027module_param(debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028MODULE_PARM_DESC(debug, "Set Debug Level 0=quiet, 5=noisy");
29#undef DEBUG
30#define DEBUG(n, format, arg...) \
31 if (n <= debug) { \
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -070032 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __func__ , ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 }
34
35#else
36#undef DEBUG
37#define DEBUG(n, arg...)
38static const int debug = 0;
39#endif
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define info(format, arg...) printk(KERN_INFO "pcmciamtd: " format "\n" , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define DRIVER_DESC "PCMCIA Flash memory card driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* Size of the PCMCIA address space: 26 bits = 64 MB */
46#define MAX_PCMCIA_ADDR 0x4000000
47
48struct pcmciamtd_dev {
Dominik Brodowskifd238232006-03-05 10:45:09 +010049 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 caddr_t win_base; /* ioremapped address of PCMCIA window */
51 unsigned int win_size; /* size of window */
52 unsigned int offset; /* offset into card the window currently points at */
53 struct map_info pcmcia_map;
54 struct mtd_info *mtd_info;
55 int vpp;
56 char mtd_name[sizeof(struct cistpl_vers_1_t)];
57};
58
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Module parameters */
61
62/* 2 = do 16-bit transfers, 1 = do 8-bit transfers */
63static int bankwidth = 2;
64
65/* Speed of memory accesses, in ns */
66static int mem_speed;
67
68/* Force the size of an SRAM card */
69static int force_size;
70
71/* Force Vpp */
72static int vpp;
73
74/* Set Vpp */
75static int setvpp;
76
77/* Force card to be treated as FLASH, ROM or RAM */
78static int mem_type;
79
80MODULE_LICENSE("GPL");
81MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
82MODULE_DESCRIPTION(DRIVER_DESC);
Rusty Russell8d3b33f2006-03-25 03:07:05 -080083module_param(bankwidth, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_PARM_DESC(bankwidth, "Set bankwidth (1=8 bit, 2=16 bit, default=2)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080085module_param(mem_speed, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_PARM_DESC(mem_speed, "Set memory access speed in ns");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080087module_param(force_size, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_PARM_DESC(force_size, "Force size of card in MiB (1-64)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080089module_param(setvpp, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090MODULE_PARM_DESC(setvpp, "Set Vpp (0=Never, 1=On writes, 2=Always on, default=0)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080091module_param(vpp, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092MODULE_PARM_DESC(vpp, "Vpp value in 1/10ths eg 33=3.3V 120=12V (Dangerous)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080093module_param(mem_type, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094MODULE_PARM_DESC(mem_type, "Set Memory type (0=Flash, 1=RAM, 2=ROM, default=0)");
95
96
Alexander Kurz9bdde162010-05-13 12:00:00 +020097/* read/write{8,16} copy_{from,to} routines with window remapping
98 * to access whole card
99 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static caddr_t remap_window(struct map_info *map, unsigned long to)
101{
102 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200103 struct resource *win = (struct resource *) map->map_priv_2;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200104 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int ret;
106
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100107 if (!pcmcia_dev_present(dev->p_dev)) {
108 DEBUG(1, "device removed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
110 }
111
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200112 offset = to & ~(dev->win_size-1);
113 if (offset != dev->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 DEBUG(2, "Remapping window from 0x%8.8x to 0x%8.8x",
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200115 dev->offset, offset);
116 ret = pcmcia_map_mem_page(dev->p_dev, win, offset);
Dominik Brodowski9b44de22009-10-24 15:55:39 +0200117 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return NULL;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200119 dev->offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 return dev->win_base + (to & (dev->win_size-1));
122}
123
124
125static map_word pcmcia_read8_remap(struct map_info *map, unsigned long ofs)
126{
127 caddr_t addr;
128 map_word d = {{0}};
129
130 addr = remap_window(map, ofs);
131 if(!addr)
132 return d;
133
134 d.x[0] = readb(addr);
Alexander Kurzf6763c982010-05-13 11:59:59 +0200135 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02lx", ofs, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return d;
137}
138
139
140static map_word pcmcia_read16_remap(struct map_info *map, unsigned long ofs)
141{
142 caddr_t addr;
143 map_word d = {{0}};
144
145 addr = remap_window(map, ofs);
146 if(!addr)
147 return d;
148
149 d.x[0] = readw(addr);
Alexander Kurzf6763c982010-05-13 11:59:59 +0200150 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04lx", ofs, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return d;
152}
153
154
155static void pcmcia_copy_from_remap(struct map_info *map, void *to, unsigned long from, ssize_t len)
156{
157 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
158 unsigned long win_size = dev->win_size;
159
David Woodhouse11c93602010-05-17 06:55:24 +0100160 DEBUG(3, "to = %p from = %lu len = %zd", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 while(len) {
162 int toread = win_size - (from & (win_size-1));
163 caddr_t addr;
164
165 if(toread > len)
166 toread = len;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 addr = remap_window(map, from);
169 if(!addr)
170 return;
171
172 DEBUG(4, "memcpy from %p to %p len = %d", addr, to, toread);
173 memcpy_fromio(to, addr, toread);
174 len -= toread;
175 to += toread;
176 from += toread;
177 }
178}
179
180
181static void pcmcia_write8_remap(struct map_info *map, map_word d, unsigned long adr)
182{
183 caddr_t addr = remap_window(map, adr);
184
185 if(!addr)
186 return;
187
Alexander Kurzf6763c982010-05-13 11:59:59 +0200188 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%02lx", adr, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 writeb(d.x[0], addr);
190}
191
192
193static void pcmcia_write16_remap(struct map_info *map, map_word d, unsigned long adr)
194{
195 caddr_t addr = remap_window(map, adr);
196 if(!addr)
197 return;
198
Alexander Kurzf6763c982010-05-13 11:59:59 +0200199 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%04lx", adr, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 writew(d.x[0], addr);
201}
202
203
204static void pcmcia_copy_to_remap(struct map_info *map, unsigned long to, const void *from, ssize_t len)
205{
206 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
207 unsigned long win_size = dev->win_size;
208
David Woodhouse11c93602010-05-17 06:55:24 +0100209 DEBUG(3, "to = %lu from = %p len = %zd", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 while(len) {
211 int towrite = win_size - (to & (win_size-1));
212 caddr_t addr;
213
214 if(towrite > len)
215 towrite = len;
216
217 addr = remap_window(map, to);
218 if(!addr)
219 return;
220
221 DEBUG(4, "memcpy from %p to %p len = %d", from, addr, towrite);
222 memcpy_toio(addr, from, towrite);
223 len -= towrite;
224 to += towrite;
225 from += towrite;
226 }
227}
228
229
230/* read/write{8,16} copy_{from,to} routines with direct access */
231
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100232#define DEV_REMOVED(x) (!(pcmcia_dev_present(((struct pcmciamtd_dev *)map->map_priv_1)->p_dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234static map_word pcmcia_read8(struct map_info *map, unsigned long ofs)
235{
236 caddr_t win_base = (caddr_t)map->map_priv_2;
237 map_word d = {{0}};
238
239 if(DEV_REMOVED(map))
240 return d;
241
242 d.x[0] = readb(win_base + ofs);
Alexander Kurz9bdde162010-05-13 12:00:00 +0200243 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%02lx",
244 ofs, win_base + ofs, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return d;
246}
247
248
249static map_word pcmcia_read16(struct map_info *map, unsigned long ofs)
250{
251 caddr_t win_base = (caddr_t)map->map_priv_2;
252 map_word d = {{0}};
253
254 if(DEV_REMOVED(map))
255 return d;
256
257 d.x[0] = readw(win_base + ofs);
Alexander Kurz9bdde162010-05-13 12:00:00 +0200258 DEBUG(3, "ofs = 0x%08lx (%p) data = 0x%04lx",
259 ofs, win_base + ofs, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return d;
261}
262
263
264static void pcmcia_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
265{
266 caddr_t win_base = (caddr_t)map->map_priv_2;
267
268 if(DEV_REMOVED(map))
269 return;
270
David Woodhouse11c93602010-05-17 06:55:24 +0100271 DEBUG(3, "to = %p from = %lu len = %zd", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy_fromio(to, win_base + from, len);
273}
274
275
Alexander Kurzf6763c982010-05-13 11:59:59 +0200276static void pcmcia_write8(struct map_info *map, map_word d, unsigned long adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 caddr_t win_base = (caddr_t)map->map_priv_2;
279
280 if(DEV_REMOVED(map))
281 return;
282
Alexander Kurz9bdde162010-05-13 12:00:00 +0200283 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%02lx",
284 adr, win_base + adr, d.x[0]);
Alexander Kurzf6763c982010-05-13 11:59:59 +0200285 writeb(d.x[0], win_base + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288
Alexander Kurzf6763c982010-05-13 11:59:59 +0200289static void pcmcia_write16(struct map_info *map, map_word d, unsigned long adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 caddr_t win_base = (caddr_t)map->map_priv_2;
292
293 if(DEV_REMOVED(map))
294 return;
295
Alexander Kurz9bdde162010-05-13 12:00:00 +0200296 DEBUG(3, "adr = 0x%08lx (%p) data = 0x%04lx",
297 adr, win_base + adr, d.x[0]);
Alexander Kurzf6763c982010-05-13 11:59:59 +0200298 writew(d.x[0], win_base + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301
302static void pcmcia_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
303{
304 caddr_t win_base = (caddr_t)map->map_priv_2;
305
306 if(DEV_REMOVED(map))
307 return;
308
David Woodhouse11c93602010-05-17 06:55:24 +0100309 DEBUG(3, "to = %lu from = %p len = %zd", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 memcpy_toio(win_base + to, from, len);
311}
312
313
314static void pcmciamtd_set_vpp(struct map_info *map, int on)
315{
316 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200317 struct pcmcia_device *link = dev->p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 DEBUG(2, "dev = %p on = %d vpp = %d\n", dev, on, dev->vpp);
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200320 pcmcia_fixup_vpp(link, on ? dev->vpp : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200324static void pcmciamtd_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct pcmciamtd_dev *dev = link->priv;
327
328 DEBUG(3, "link = 0x%p", link);
329
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200330 if (link->resource[2]->end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if(dev->win_base) {
332 iounmap(dev->win_base);
333 dev->win_base = NULL;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200336 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200340#ifdef CONFIG_MTD_DEBUG
341static int pcmciamtd_cistpl_format(struct pcmcia_device *p_dev,
342 tuple_t *tuple,
343 void *priv_data)
344{
345 cisparse_t parse;
346
347 if (!pcmcia_parse_tuple(tuple, &parse)) {
348 cistpl_format_t *t = &parse.format;
349 (void)t; /* Shut up, gcc */
350 DEBUG(2, "Format type: %u, Error Detection: %u, offset = %u, length =%u",
351 t->type, t->edc, t->offset, t->length);
352 }
353 return -ENOSPC;
354}
355
356static int pcmciamtd_cistpl_jedec(struct pcmcia_device *p_dev,
357 tuple_t *tuple,
358 void *priv_data)
359{
360 cisparse_t parse;
361 int i;
362
363 if (!pcmcia_parse_tuple(tuple, &parse)) {
364 cistpl_jedec_t *t = &parse.jedec;
365 for (i = 0; i < t->nid; i++)
Alexander Kurz9bdde162010-05-13 12:00:00 +0200366 DEBUG(2, "JEDEC: 0x%02x 0x%02x",
367 t->id[i].mfr, t->id[i].info);
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200368 }
369 return -ENOSPC;
370}
371#endif
372
373static int pcmciamtd_cistpl_device(struct pcmcia_device *p_dev,
374 tuple_t *tuple,
375 void *priv_data)
376{
377 struct pcmciamtd_dev *dev = priv_data;
378 cisparse_t parse;
379 cistpl_device_t *t = &parse.device;
380 int i;
381
382 if (pcmcia_parse_tuple(tuple, &parse))
383 return -EINVAL;
384
385 DEBUG(2, "Common memory:");
386 dev->pcmcia_map.size = t->dev[0].size;
387 /* from here on: DEBUG only */
388 for (i = 0; i < t->ndev; i++) {
389 DEBUG(2, "Region %d, type = %u", i, t->dev[i].type);
390 DEBUG(2, "Region %d, wp = %u", i, t->dev[i].wp);
391 DEBUG(2, "Region %d, speed = %u ns", i, t->dev[i].speed);
392 DEBUG(2, "Region %d, size = %u bytes", i, t->dev[i].size);
393 }
394 return 0;
395}
396
397static int pcmciamtd_cistpl_geo(struct pcmcia_device *p_dev,
398 tuple_t *tuple,
399 void *priv_data)
400{
401 struct pcmciamtd_dev *dev = priv_data;
402 cisparse_t parse;
403 cistpl_device_geo_t *t = &parse.device_geo;
404 int i;
405
406 if (pcmcia_parse_tuple(tuple, &parse))
407 return -EINVAL;
408
409 dev->pcmcia_map.bankwidth = t->geo[0].buswidth;
410 /* from here on: DEBUG only */
411 for (i = 0; i < t->ngeo; i++) {
412 DEBUG(2, "region: %d bankwidth = %u", i, t->geo[i].buswidth);
413 DEBUG(2, "region: %d erase_block = %u", i, t->geo[i].erase_block);
414 DEBUG(2, "region: %d read_block = %u", i, t->geo[i].read_block);
415 DEBUG(2, "region: %d write_block = %u", i, t->geo[i].write_block);
416 DEBUG(2, "region: %d partition = %u", i, t->geo[i].partition);
417 DEBUG(2, "region: %d interleave = %u", i, t->geo[i].interleave);
418 }
419 return 0;
420}
421
422
Alexander Kurzf6763c982010-05-13 11:59:59 +0200423static void card_settings(struct pcmciamtd_dev *dev, struct pcmcia_device *p_dev, int *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200425 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200427 if (p_dev->prod_id[0]) {
428 dev->mtd_name[0] = '\0';
429 for (i = 0; i < 4; i++) {
430 if (i)
431 strcat(dev->mtd_name, " ");
432 if (p_dev->prod_id[i])
433 strcat(dev->mtd_name, p_dev->prod_id[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200435 DEBUG(2, "Found name: %s", dev->mtd_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200437
438#ifdef CONFIG_MTD_DEBUG
439 pcmcia_loop_tuple(p_dev, CISTPL_FORMAT, pcmciamtd_cistpl_format, NULL);
440 pcmcia_loop_tuple(p_dev, CISTPL_JEDEC_C, pcmciamtd_cistpl_jedec, NULL);
441#endif
442 pcmcia_loop_tuple(p_dev, CISTPL_DEVICE, pcmciamtd_cistpl_device, dev);
443 pcmcia_loop_tuple(p_dev, CISTPL_DEVICE_GEO, pcmciamtd_cistpl_geo, dev);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if(!dev->pcmcia_map.size)
446 dev->pcmcia_map.size = MAX_PCMCIA_ADDR;
447
448 if(!dev->pcmcia_map.bankwidth)
449 dev->pcmcia_map.bankwidth = 2;
450
451 if(force_size) {
452 dev->pcmcia_map.size = force_size << 20;
453 DEBUG(2, "size forced to %dM", force_size);
454 }
455
456 if(bankwidth) {
457 dev->pcmcia_map.bankwidth = bankwidth;
458 DEBUG(2, "bankwidth forced to %d", bankwidth);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 dev->pcmcia_map.name = dev->mtd_name;
462 if(!dev->mtd_name[0]) {
463 strcpy(dev->mtd_name, "PCMCIA Memory card");
464 *new_name = 1;
465 }
466
467 DEBUG(1, "Device: Size: %lu Width:%d Name: %s",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200468 dev->pcmcia_map.size,
469 dev->pcmcia_map.bankwidth << 3, dev->mtd_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200473static int pcmciamtd_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct pcmciamtd_dev *dev = link->priv;
476 struct mtd_info *mtd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int ret;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200478 int i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 static char *probes[] = { "jedec_probe", "cfi_probe" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int new_name = 0;
481
482 DEBUG(3, "link=0x%p", link);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 card_settings(dev, link, &new_name);
485
486 dev->pcmcia_map.phys = NO_XIP;
487 dev->pcmcia_map.copy_from = pcmcia_copy_from_remap;
488 dev->pcmcia_map.copy_to = pcmcia_copy_to_remap;
489 if (dev->pcmcia_map.bankwidth == 1) {
490 dev->pcmcia_map.read = pcmcia_read8_remap;
491 dev->pcmcia_map.write = pcmcia_write8_remap;
492 } else {
493 dev->pcmcia_map.read = pcmcia_read16_remap;
494 dev->pcmcia_map.write = pcmcia_write16_remap;
495 }
496 if(setvpp == 1)
497 dev->pcmcia_map.set_vpp = pcmciamtd_set_vpp;
498
Alexander Kurz9bdde162010-05-13 12:00:00 +0200499 /* Request a memory window for PCMCIA. Some architeures can map windows
500 * upto the maximum that PCMCIA can support (64MiB) - this is ideal and
501 * we aim for a window the size of the whole card - otherwise we try
502 * smaller windows until we succeed
503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200505 link->resource[2]->flags |= WIN_MEMORY_TYPE_CM | WIN_ENABLE;
506 link->resource[2]->flags |= (dev->pcmcia_map.bankwidth == 1) ?
507 WIN_DATA_WIDTH_8 : WIN_DATA_WIDTH_16;
508 link->resource[2]->start = 0;
509 link->resource[2]->end = (force_size) ? force_size << 20 :
510 MAX_PCMCIA_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 dev->win_size = 0;
512
513 do {
514 int ret;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200515 DEBUG(2, "requesting window with size = %luKiB memspeed = %d",
516 (unsigned long) resource_size(link->resource[2]) >> 10,
517 mem_speed);
518 ret = pcmcia_request_window(link, link->resource[2], mem_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 DEBUG(2, "ret = %d dev->win_size = %d", ret, dev->win_size);
520 if(ret) {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200521 j++;
522 link->resource[2]->start = 0;
523 link->resource[2]->end = (force_size) ?
524 force_size << 20 : MAX_PCMCIA_ADDR;
525 link->resource[2]->end >>= j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200527 DEBUG(2, "Got window of size %luKiB", (unsigned long)
528 resource_size(link->resource[2]) >> 10);
529 dev->win_size = resource_size(link->resource[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200532 } while (link->resource[2]->end >= 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 DEBUG(2, "dev->win_size = %d", dev->win_size);
535
536 if(!dev->win_size) {
Alexander Kurz9bdde162010-05-13 12:00:00 +0200537 dev_err(&dev->p_dev->dev, "Cannot allocate memory window\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200539 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 DEBUG(1, "Allocated a window of %dKiB", dev->win_size >> 10);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* Get write protect status */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200544 dev->win_base = ioremap(link->resource[2]->start,
545 resource_size(link->resource[2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if(!dev->win_base) {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200547 dev_err(&dev->p_dev->dev, "ioremap(%pR) failed\n",
548 link->resource[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200550 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200552 DEBUG(1, "mapped window dev = %p @ %pR, base = %p",
553 dev, link->resource[2], dev->win_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 dev->offset = 0;
556 dev->pcmcia_map.map_priv_1 = (unsigned long)dev;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200557 dev->pcmcia_map.map_priv_2 = (unsigned long)link->resource[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Alexander Kurzf6763c982010-05-13 11:59:59 +0200559 dev->vpp = (vpp) ? vpp : link->socket->socket.Vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if(setvpp == 2) {
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200561 link->vpp = dev->vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 } else {
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200563 link->vpp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200566 link->config_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 DEBUG(2, "Setting Configuration");
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200568 ret = pcmcia_enable_device(link);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200569 if (ret != 0) {
Amol Lad25f0c652006-09-21 18:12:43 +0530570 if (dev->win_base) {
571 iounmap(dev->win_base);
572 dev->win_base = NULL;
573 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200574 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 if(mem_type == 1) {
578 mtd = do_map_probe("map_ram", &dev->pcmcia_map);
579 } else if(mem_type == 2) {
580 mtd = do_map_probe("map_rom", &dev->pcmcia_map);
581 } else {
Tobias Klauser87d10f32006-03-31 02:29:45 -0800582 for(i = 0; i < ARRAY_SIZE(probes); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 DEBUG(1, "Trying %s", probes[i]);
584 mtd = do_map_probe(probes[i], &dev->pcmcia_map);
585 if(mtd)
586 break;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 DEBUG(1, "FAILED: %s", probes[i]);
589 }
590 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if(!mtd) {
Alexander Kurz9bdde162010-05-13 12:00:00 +0200593 DEBUG(1, "Can not find an MTD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200595 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
598 dev->mtd_info = mtd;
599 mtd->owner = THIS_MODULE;
600
601 if(new_name) {
602 int size = 0;
603 char unit = ' ';
Alexander Kurz9bdde162010-05-13 12:00:00 +0200604 /* Since we are using a default name, make it better by adding
605 * in the size
606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if(mtd->size < 1048576) { /* <1MiB in size, show size in KiB */
608 size = mtd->size >> 10;
609 unit = 'K';
610 } else {
611 size = mtd->size >> 20;
612 unit = 'M';
613 }
614 snprintf(dev->mtd_name, sizeof(dev->mtd_name), "%d%ciB %s", size, unit, "PCMCIA Memory card");
615 }
616
617 /* If the memory found is fits completely into the mapped PCMCIA window,
618 use the faster non-remapping read/write functions */
619 if(mtd->size <= dev->win_size) {
620 DEBUG(1, "Using non remapping memory functions");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 dev->pcmcia_map.map_priv_2 = (unsigned long)dev->win_base;
622 if (dev->pcmcia_map.bankwidth == 1) {
623 dev->pcmcia_map.read = pcmcia_read8;
624 dev->pcmcia_map.write = pcmcia_write8;
625 } else {
626 dev->pcmcia_map.read = pcmcia_read16;
627 dev->pcmcia_map.write = pcmcia_write16;
628 }
629 dev->pcmcia_map.copy_from = pcmcia_copy_from;
630 dev->pcmcia_map.copy_to = pcmcia_copy_to;
631 }
632
633 if(add_mtd_device(mtd)) {
634 map_destroy(mtd);
635 dev->mtd_info = NULL;
Alexander Kurz9bdde162010-05-13 12:00:00 +0200636 dev_err(&dev->p_dev->dev,
637 "Could not register the MTD device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200639 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Alexander Kurz9bdde162010-05-13 12:00:00 +0200641 dev_info(&dev->p_dev->dev, "mtd%d: %s\n", mtd->index, mtd->name);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100646static int pcmciamtd_suspend(struct pcmcia_device *dev)
647{
648 DEBUG(2, "EVENT_PM_RESUME");
649
650 /* get_lock(link); */
651
652 return 0;
653}
654
655static int pcmciamtd_resume(struct pcmcia_device *dev)
656{
657 DEBUG(2, "EVENT_PM_SUSPEND");
658
659 /* free_lock(link); */
660
661 return 0;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200665static void pcmciamtd_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100667 struct pcmciamtd_dev *dev = link->priv;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 DEBUG(3, "link=0x%p", link);
670
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100671 if(dev->mtd_info) {
672 del_mtd_device(dev->mtd_info);
Alexander Kurz9bdde162010-05-13 12:00:00 +0200673 dev_info(&dev->p_dev->dev, "mtd%d: Removing\n",
674 dev->mtd_info->index);
Sean Young599fb322006-05-17 11:47:48 +0100675 map_destroy(dev->mtd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100677
678 pcmciamtd_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200682static int pcmciamtd_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct pcmciamtd_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Create new memory card device */
Burman Yan95b93a02006-11-15 21:10:29 +0200687 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100688 if (!dev) return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 DEBUG(1, "dev=0x%p", dev);
690
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200691 dev->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 link->priv = dev;
693
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200694 return pcmciamtd_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700697static struct pcmcia_device_id pcmciamtd_ids[] = {
698 PCMCIA_DEVICE_FUNC_ID(1),
699 PCMCIA_DEVICE_PROD_ID123("IO DATA", "PCS-2M", "2MB SRAM", 0x547e66dc, 0x1fed36cd, 0x36eadd21),
700 PCMCIA_DEVICE_PROD_ID12("IBM", "2MB SRAM", 0xb569a6e5, 0x36eadd21),
701 PCMCIA_DEVICE_PROD_ID12("IBM", "4MB FLASH", 0xb569a6e5, 0x8bc54d2a),
702 PCMCIA_DEVICE_PROD_ID12("IBM", "8MB FLASH", 0xb569a6e5, 0x6df1be3e),
703 PCMCIA_DEVICE_PROD_ID12("Intel", "S2E20SW", 0x816cc815, 0xd14c9dcf),
704 PCMCIA_DEVICE_PROD_ID12("Intel", "S2E8 SW", 0x816cc815, 0xa2d7dedb),
705 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-02 ", 0x40ade711, 0x145cea5c),
706 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-04 ", 0x40ade711, 0x42064dda),
707 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-20 ", 0x40ade711, 0x25ee5cb0),
708 PCMCIA_DEVICE_PROD_ID12("intel", "VALUE SERIES 100 ", 0x40ade711, 0xdf8506d8),
709 PCMCIA_DEVICE_PROD_ID12("KINGMAX TECHNOLOGY INC.", "SRAM 256K Bytes", 0x54d0c69c, 0xad12c29c),
710 PCMCIA_DEVICE_PROD_ID12("Maxtor", "MAXFL MobileMax Flash Memory Card", 0xb68968c8, 0x2dfb47b0),
Alexander Kurzf6763c982010-05-13 11:59:59 +0200711 PCMCIA_DEVICE_PROD_ID123("M-Systems", "M-SYS Flash Memory Card", "(c) M-Systems", 0x7ed2ad87, 0x675dc3fb, 0x7aef3965),
Dominik Brodowskib2321ac2010-05-13 12:00:01 +0200712 PCMCIA_DEVICE_PROD_ID12("PRETEC", " 2MB SRAM CARD", 0xebf91155, 0x805360ca),
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700713 PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB101EN20", 0xf9876baf, 0xad0b207b),
714 PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB513EN20", 0xf9876baf, 0xe8d884ad),
Alexander Kurzf6763c982010-05-13 11:59:59 +0200715 PCMCIA_DEVICE_PROD_ID12("SMART Modular Technologies", " 4MB FLASH Card", 0x96fd8277, 0x737a5b05),
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700716 PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-3000", 0x05ddca47, 0xe7d67bca),
717 PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-4100", 0x05ddca47, 0x7bc32944),
718 /* the following was commented out in pcmcia-cs-3.2.7 */
719 /* PCMCIA_DEVICE_PROD_ID12("RATOC Systems,Inc.", "SmartMedia ADAPTER PC Card", 0xf4a2fefe, 0x5885b2ae), */
720#ifdef CONFIG_MTD_PCMCIA_ANONYMOUS
721 { .match_flags = PCMCIA_DEV_ID_MATCH_ANONYMOUS, },
722#endif
723 PCMCIA_DEVICE_NULL
724};
725MODULE_DEVICE_TABLE(pcmcia, pcmciamtd_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727static struct pcmcia_driver pcmciamtd_driver = {
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200728 .name = "pcmciamtd",
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200729 .probe = pcmciamtd_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100730 .remove = pcmciamtd_detach,
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700731 .owner = THIS_MODULE,
732 .id_table = pcmciamtd_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100733 .suspend = pcmciamtd_suspend,
734 .resume = pcmciamtd_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
737
738static int __init init_pcmciamtd(void)
739{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if(bankwidth && bankwidth != 1 && bankwidth != 2) {
741 info("bad bankwidth (%d), using default", bankwidth);
742 bankwidth = 2;
743 }
744 if(force_size && (force_size < 1 || force_size > 64)) {
745 info("bad force_size (%d), using default", force_size);
746 force_size = 0;
747 }
748 if(mem_type && mem_type != 1 && mem_type != 2) {
749 info("bad mem_type (%d), using default", mem_type);
750 mem_type = 0;
751 }
752 return pcmcia_register_driver(&pcmciamtd_driver);
753}
754
755
756static void __exit exit_pcmciamtd(void)
757{
758 DEBUG(1, DRIVER_DESC " unloading");
759 pcmcia_unregister_driver(&pcmciamtd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762module_init(init_pcmciamtd);
763module_exit(exit_pcmciamtd);