blob: afa9bd81220b14d5ef458e6b0a0e76042b2ce210 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define info(format, arg...) printk(KERN_INFO "pcmciamtd: " format "\n" , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define DRIVER_DESC "PCMCIA Flash memory card driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/* Size of the PCMCIA address space: 26 bits = 64 MB */
30#define MAX_PCMCIA_ADDR 0x4000000
31
32struct pcmciamtd_dev {
Dominik Brodowskifd238232006-03-05 10:45:09 +010033 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 caddr_t win_base; /* ioremapped address of PCMCIA window */
35 unsigned int win_size; /* size of window */
36 unsigned int offset; /* offset into card the window currently points at */
37 struct map_info pcmcia_map;
38 struct mtd_info *mtd_info;
39 int vpp;
40 char mtd_name[sizeof(struct cistpl_vers_1_t)];
41};
42
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* Module parameters */
45
46/* 2 = do 16-bit transfers, 1 = do 8-bit transfers */
47static int bankwidth = 2;
48
49/* Speed of memory accesses, in ns */
50static int mem_speed;
51
52/* Force the size of an SRAM card */
53static int force_size;
54
55/* Force Vpp */
56static int vpp;
57
58/* Set Vpp */
59static int setvpp;
60
61/* Force card to be treated as FLASH, ROM or RAM */
62static int mem_type;
63
64MODULE_LICENSE("GPL");
65MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
66MODULE_DESCRIPTION(DRIVER_DESC);
Rusty Russell8d3b33f2006-03-25 03:07:05 -080067module_param(bankwidth, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068MODULE_PARM_DESC(bankwidth, "Set bankwidth (1=8 bit, 2=16 bit, default=2)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080069module_param(mem_speed, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070MODULE_PARM_DESC(mem_speed, "Set memory access speed in ns");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080071module_param(force_size, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_PARM_DESC(force_size, "Force size of card in MiB (1-64)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080073module_param(setvpp, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_PARM_DESC(setvpp, "Set Vpp (0=Never, 1=On writes, 2=Always on, default=0)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080075module_param(vpp, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_PARM_DESC(vpp, "Vpp value in 1/10ths eg 33=3.3V 120=12V (Dangerous)");
Rusty Russell8d3b33f2006-03-25 03:07:05 -080077module_param(mem_type, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078MODULE_PARM_DESC(mem_type, "Set Memory type (0=Flash, 1=RAM, 2=ROM, default=0)");
79
80
Alexander Kurz9bdde162010-05-13 12:00:00 +020081/* read/write{8,16} copy_{from,to} routines with window remapping
82 * to access whole card
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static caddr_t remap_window(struct map_info *map, unsigned long to)
85{
86 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
Dominik Brodowskicdb13802010-07-28 10:59:06 +020087 struct resource *win = (struct resource *) map->map_priv_2;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +020088 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int ret;
90
Dominik Brodowskie2d40962006-03-02 00:09:29 +010091 if (!pcmcia_dev_present(dev->p_dev)) {
Brian Norrise6453522011-07-19 10:06:11 -070092 pr_debug("device removed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94 }
95
Dominik Brodowskib5cb2592010-07-24 18:46:42 +020096 offset = to & ~(dev->win_size-1);
97 if (offset != dev->offset) {
Brian Norrise6453522011-07-19 10:06:11 -070098 pr_debug("Remapping window from 0x%8.8x to 0x%8.8x\n",
Dominik Brodowskib5cb2592010-07-24 18:46:42 +020099 dev->offset, offset);
100 ret = pcmcia_map_mem_page(dev->p_dev, win, offset);
Dominik Brodowski9b44de22009-10-24 15:55:39 +0200101 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return NULL;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200103 dev->offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 return dev->win_base + (to & (dev->win_size-1));
106}
107
108
109static map_word pcmcia_read8_remap(struct map_info *map, unsigned long ofs)
110{
111 caddr_t addr;
112 map_word d = {{0}};
113
114 addr = remap_window(map, ofs);
115 if(!addr)
116 return d;
117
118 d.x[0] = readb(addr);
Brian Norrise6453522011-07-19 10:06:11 -0700119 pr_debug("ofs = 0x%08lx (%p) data = 0x%02lx\n", ofs, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return d;
121}
122
123
124static map_word pcmcia_read16_remap(struct map_info *map, unsigned long ofs)
125{
126 caddr_t addr;
127 map_word d = {{0}};
128
129 addr = remap_window(map, ofs);
130 if(!addr)
131 return d;
132
133 d.x[0] = readw(addr);
Brian Norrise6453522011-07-19 10:06:11 -0700134 pr_debug("ofs = 0x%08lx (%p) data = 0x%04lx\n", ofs, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return d;
136}
137
138
139static void pcmcia_copy_from_remap(struct map_info *map, void *to, unsigned long from, ssize_t len)
140{
141 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
142 unsigned long win_size = dev->win_size;
143
Brian Norrise6453522011-07-19 10:06:11 -0700144 pr_debug("to = %p from = %lu len = %zd\n", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 while(len) {
146 int toread = win_size - (from & (win_size-1));
147 caddr_t addr;
148
149 if(toread > len)
150 toread = len;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 addr = remap_window(map, from);
153 if(!addr)
154 return;
155
Brian Norrise6453522011-07-19 10:06:11 -0700156 pr_debug("memcpy from %p to %p len = %d\n", addr, to, toread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 memcpy_fromio(to, addr, toread);
158 len -= toread;
159 to += toread;
160 from += toread;
161 }
162}
163
164
165static void pcmcia_write8_remap(struct map_info *map, map_word d, unsigned long adr)
166{
167 caddr_t addr = remap_window(map, adr);
168
169 if(!addr)
170 return;
171
Brian Norrise6453522011-07-19 10:06:11 -0700172 pr_debug("adr = 0x%08lx (%p) data = 0x%02lx\n", adr, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 writeb(d.x[0], addr);
174}
175
176
177static void pcmcia_write16_remap(struct map_info *map, map_word d, unsigned long adr)
178{
179 caddr_t addr = remap_window(map, adr);
180 if(!addr)
181 return;
182
Brian Norrise6453522011-07-19 10:06:11 -0700183 pr_debug("adr = 0x%08lx (%p) data = 0x%04lx\n", adr, addr, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 writew(d.x[0], addr);
185}
186
187
188static void pcmcia_copy_to_remap(struct map_info *map, unsigned long to, const void *from, ssize_t len)
189{
190 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
191 unsigned long win_size = dev->win_size;
192
Brian Norrise6453522011-07-19 10:06:11 -0700193 pr_debug("to = %lu from = %p len = %zd\n", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 while(len) {
195 int towrite = win_size - (to & (win_size-1));
196 caddr_t addr;
197
198 if(towrite > len)
199 towrite = len;
200
201 addr = remap_window(map, to);
202 if(!addr)
203 return;
204
Brian Norrise6453522011-07-19 10:06:11 -0700205 pr_debug("memcpy from %p to %p len = %d\n", from, addr, towrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memcpy_toio(addr, from, towrite);
207 len -= towrite;
208 to += towrite;
209 from += towrite;
210 }
211}
212
213
214/* read/write{8,16} copy_{from,to} routines with direct access */
215
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100216#define DEV_REMOVED(x) (!(pcmcia_dev_present(((struct pcmciamtd_dev *)map->map_priv_1)->p_dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218static map_word pcmcia_read8(struct map_info *map, unsigned long ofs)
219{
220 caddr_t win_base = (caddr_t)map->map_priv_2;
221 map_word d = {{0}};
222
223 if(DEV_REMOVED(map))
224 return d;
225
226 d.x[0] = readb(win_base + ofs);
Brian Norrise6453522011-07-19 10:06:11 -0700227 pr_debug("ofs = 0x%08lx (%p) data = 0x%02lx\n",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200228 ofs, win_base + ofs, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return d;
230}
231
232
233static map_word pcmcia_read16(struct map_info *map, unsigned long ofs)
234{
235 caddr_t win_base = (caddr_t)map->map_priv_2;
236 map_word d = {{0}};
237
238 if(DEV_REMOVED(map))
239 return d;
240
241 d.x[0] = readw(win_base + ofs);
Brian Norrise6453522011-07-19 10:06:11 -0700242 pr_debug("ofs = 0x%08lx (%p) data = 0x%04lx\n",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200243 ofs, win_base + ofs, d.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return d;
245}
246
247
248static void pcmcia_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
249{
250 caddr_t win_base = (caddr_t)map->map_priv_2;
251
252 if(DEV_REMOVED(map))
253 return;
254
Brian Norrise6453522011-07-19 10:06:11 -0700255 pr_debug("to = %p from = %lu len = %zd\n", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 memcpy_fromio(to, win_base + from, len);
257}
258
259
Alexander Kurzf6763c982010-05-13 11:59:59 +0200260static void pcmcia_write8(struct map_info *map, map_word d, unsigned long adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 caddr_t win_base = (caddr_t)map->map_priv_2;
263
264 if(DEV_REMOVED(map))
265 return;
266
Brian Norrise6453522011-07-19 10:06:11 -0700267 pr_debug("adr = 0x%08lx (%p) data = 0x%02lx\n",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200268 adr, win_base + adr, d.x[0]);
Alexander Kurzf6763c982010-05-13 11:59:59 +0200269 writeb(d.x[0], win_base + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272
Alexander Kurzf6763c982010-05-13 11:59:59 +0200273static void pcmcia_write16(struct map_info *map, map_word d, unsigned long adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 caddr_t win_base = (caddr_t)map->map_priv_2;
276
277 if(DEV_REMOVED(map))
278 return;
279
Brian Norrise6453522011-07-19 10:06:11 -0700280 pr_debug("adr = 0x%08lx (%p) data = 0x%04lx\n",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200281 adr, win_base + adr, d.x[0]);
Alexander Kurzf6763c982010-05-13 11:59:59 +0200282 writew(d.x[0], win_base + adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285
286static void pcmcia_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
287{
288 caddr_t win_base = (caddr_t)map->map_priv_2;
289
290 if(DEV_REMOVED(map))
291 return;
292
Brian Norrise6453522011-07-19 10:06:11 -0700293 pr_debug("to = %lu from = %p len = %zd\n", to, from, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 memcpy_toio(win_base + to, from, len);
295}
296
297
298static void pcmciamtd_set_vpp(struct map_info *map, int on)
299{
300 struct pcmciamtd_dev *dev = (struct pcmciamtd_dev *)map->map_priv_1;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200301 struct pcmcia_device *link = dev->p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Brian Norrise6453522011-07-19 10:06:11 -0700303 pr_debug("dev = %p on = %d vpp = %d\n\n", dev, on, dev->vpp);
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200304 pcmcia_fixup_vpp(link, on ? dev->vpp : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200308static void pcmciamtd_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct pcmciamtd_dev *dev = link->priv;
311
Brian Norrise6453522011-07-19 10:06:11 -0700312 pr_debug("link = 0x%p\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200314 if (link->resource[2]->end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if(dev->win_base) {
316 iounmap(dev->win_base);
317 dev->win_base = NULL;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200320 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200324#ifdef CONFIG_MTD_DEBUG
325static int pcmciamtd_cistpl_format(struct pcmcia_device *p_dev,
326 tuple_t *tuple,
327 void *priv_data)
328{
329 cisparse_t parse;
330
331 if (!pcmcia_parse_tuple(tuple, &parse)) {
332 cistpl_format_t *t = &parse.format;
333 (void)t; /* Shut up, gcc */
Brian Norrise6453522011-07-19 10:06:11 -0700334 pr_debug("Format type: %u, Error Detection: %u, offset = %u, length =%u\n",
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200335 t->type, t->edc, t->offset, t->length);
336 }
337 return -ENOSPC;
338}
339
340static int pcmciamtd_cistpl_jedec(struct pcmcia_device *p_dev,
341 tuple_t *tuple,
342 void *priv_data)
343{
344 cisparse_t parse;
345 int i;
346
347 if (!pcmcia_parse_tuple(tuple, &parse)) {
348 cistpl_jedec_t *t = &parse.jedec;
349 for (i = 0; i < t->nid; i++)
Brian Norrise6453522011-07-19 10:06:11 -0700350 pr_debug("JEDEC: 0x%02x 0x%02x\n",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200351 t->id[i].mfr, t->id[i].info);
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200352 }
353 return -ENOSPC;
354}
355#endif
356
357static int pcmciamtd_cistpl_device(struct pcmcia_device *p_dev,
358 tuple_t *tuple,
359 void *priv_data)
360{
361 struct pcmciamtd_dev *dev = priv_data;
362 cisparse_t parse;
363 cistpl_device_t *t = &parse.device;
364 int i;
365
366 if (pcmcia_parse_tuple(tuple, &parse))
367 return -EINVAL;
368
Brian Norrise6453522011-07-19 10:06:11 -0700369 pr_debug("Common memory:\n");
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200370 dev->pcmcia_map.size = t->dev[0].size;
371 /* from here on: DEBUG only */
372 for (i = 0; i < t->ndev; i++) {
Brian Norrise6453522011-07-19 10:06:11 -0700373 pr_debug("Region %d, type = %u\n", i, t->dev[i].type);
374 pr_debug("Region %d, wp = %u\n", i, t->dev[i].wp);
375 pr_debug("Region %d, speed = %u ns\n", i, t->dev[i].speed);
376 pr_debug("Region %d, size = %u bytes\n", i, t->dev[i].size);
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200377 }
378 return 0;
379}
380
381static int pcmciamtd_cistpl_geo(struct pcmcia_device *p_dev,
382 tuple_t *tuple,
383 void *priv_data)
384{
385 struct pcmciamtd_dev *dev = priv_data;
386 cisparse_t parse;
387 cistpl_device_geo_t *t = &parse.device_geo;
388 int i;
389
390 if (pcmcia_parse_tuple(tuple, &parse))
391 return -EINVAL;
392
393 dev->pcmcia_map.bankwidth = t->geo[0].buswidth;
394 /* from here on: DEBUG only */
395 for (i = 0; i < t->ngeo; i++) {
Brian Norrise6453522011-07-19 10:06:11 -0700396 pr_debug("region: %d bankwidth = %u\n", i, t->geo[i].buswidth);
397 pr_debug("region: %d erase_block = %u\n", i, t->geo[i].erase_block);
398 pr_debug("region: %d read_block = %u\n", i, t->geo[i].read_block);
399 pr_debug("region: %d write_block = %u\n", i, t->geo[i].write_block);
400 pr_debug("region: %d partition = %u\n", i, t->geo[i].partition);
401 pr_debug("region: %d interleave = %u\n", i, t->geo[i].interleave);
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200402 }
403 return 0;
404}
405
406
Alexander Kurzf6763c982010-05-13 11:59:59 +0200407static void card_settings(struct pcmciamtd_dev *dev, struct pcmcia_device *p_dev, int *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200409 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200411 if (p_dev->prod_id[0]) {
412 dev->mtd_name[0] = '\0';
413 for (i = 0; i < 4; i++) {
414 if (i)
415 strcat(dev->mtd_name, " ");
416 if (p_dev->prod_id[i])
417 strcat(dev->mtd_name, p_dev->prod_id[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Brian Norrise6453522011-07-19 10:06:11 -0700419 pr_debug("Found name: %s\n", dev->mtd_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Dominik Brodowski18b61b92009-10-18 23:57:58 +0200421
422#ifdef CONFIG_MTD_DEBUG
423 pcmcia_loop_tuple(p_dev, CISTPL_FORMAT, pcmciamtd_cistpl_format, NULL);
424 pcmcia_loop_tuple(p_dev, CISTPL_JEDEC_C, pcmciamtd_cistpl_jedec, NULL);
425#endif
426 pcmcia_loop_tuple(p_dev, CISTPL_DEVICE, pcmciamtd_cistpl_device, dev);
427 pcmcia_loop_tuple(p_dev, CISTPL_DEVICE_GEO, pcmciamtd_cistpl_geo, dev);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if(!dev->pcmcia_map.size)
430 dev->pcmcia_map.size = MAX_PCMCIA_ADDR;
431
432 if(!dev->pcmcia_map.bankwidth)
433 dev->pcmcia_map.bankwidth = 2;
434
435 if(force_size) {
436 dev->pcmcia_map.size = force_size << 20;
Brian Norrise6453522011-07-19 10:06:11 -0700437 pr_debug("size forced to %dM\n", force_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 if(bankwidth) {
441 dev->pcmcia_map.bankwidth = bankwidth;
Brian Norrise6453522011-07-19 10:06:11 -0700442 pr_debug("bankwidth forced to %d\n", bankwidth);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 dev->pcmcia_map.name = dev->mtd_name;
446 if(!dev->mtd_name[0]) {
447 strcpy(dev->mtd_name, "PCMCIA Memory card");
448 *new_name = 1;
449 }
450
Brian Norrise6453522011-07-19 10:06:11 -0700451 pr_debug("Device: Size: %lu Width:%d Name: %s\n",
Alexander Kurz9bdde162010-05-13 12:00:00 +0200452 dev->pcmcia_map.size,
453 dev->pcmcia_map.bankwidth << 3, dev->mtd_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200457static int pcmciamtd_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct pcmciamtd_dev *dev = link->priv;
460 struct mtd_info *mtd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int ret;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200462 int i, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 static char *probes[] = { "jedec_probe", "cfi_probe" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int new_name = 0;
465
Brian Norrise6453522011-07-19 10:06:11 -0700466 pr_debug("link=0x%p\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 card_settings(dev, link, &new_name);
469
470 dev->pcmcia_map.phys = NO_XIP;
471 dev->pcmcia_map.copy_from = pcmcia_copy_from_remap;
472 dev->pcmcia_map.copy_to = pcmcia_copy_to_remap;
473 if (dev->pcmcia_map.bankwidth == 1) {
474 dev->pcmcia_map.read = pcmcia_read8_remap;
475 dev->pcmcia_map.write = pcmcia_write8_remap;
476 } else {
477 dev->pcmcia_map.read = pcmcia_read16_remap;
478 dev->pcmcia_map.write = pcmcia_write16_remap;
479 }
480 if(setvpp == 1)
481 dev->pcmcia_map.set_vpp = pcmciamtd_set_vpp;
482
Alexander Kurz9bdde162010-05-13 12:00:00 +0200483 /* Request a memory window for PCMCIA. Some architeures can map windows
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300484 * up to the maximum that PCMCIA can support (64MiB) - this is ideal and
Alexander Kurz9bdde162010-05-13 12:00:00 +0200485 * we aim for a window the size of the whole card - otherwise we try
486 * smaller windows until we succeed
487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200489 link->resource[2]->flags |= WIN_MEMORY_TYPE_CM | WIN_ENABLE;
490 link->resource[2]->flags |= (dev->pcmcia_map.bankwidth == 1) ?
491 WIN_DATA_WIDTH_8 : WIN_DATA_WIDTH_16;
492 link->resource[2]->start = 0;
493 link->resource[2]->end = (force_size) ? force_size << 20 :
494 MAX_PCMCIA_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dev->win_size = 0;
496
497 do {
498 int ret;
Brian Norrise6453522011-07-19 10:06:11 -0700499 pr_debug("requesting window with size = %luKiB memspeed = %d\n",
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200500 (unsigned long) resource_size(link->resource[2]) >> 10,
501 mem_speed);
502 ret = pcmcia_request_window(link, link->resource[2], mem_speed);
Brian Norrise6453522011-07-19 10:06:11 -0700503 pr_debug("ret = %d dev->win_size = %d\n", ret, dev->win_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if(ret) {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200505 j++;
506 link->resource[2]->start = 0;
507 link->resource[2]->end = (force_size) ?
508 force_size << 20 : MAX_PCMCIA_ADDR;
509 link->resource[2]->end >>= j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else {
Brian Norrise6453522011-07-19 10:06:11 -0700511 pr_debug("Got window of size %luKiB\n", (unsigned long)
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200512 resource_size(link->resource[2]) >> 10);
513 dev->win_size = resource_size(link->resource[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200516 } while (link->resource[2]->end >= 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Brian Norrise6453522011-07-19 10:06:11 -0700518 pr_debug("dev->win_size = %d\n", dev->win_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if(!dev->win_size) {
Alexander Kurz9bdde162010-05-13 12:00:00 +0200521 dev_err(&dev->p_dev->dev, "Cannot allocate memory window\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200523 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Brian Norrise6453522011-07-19 10:06:11 -0700525 pr_debug("Allocated a window of %dKiB\n", dev->win_size >> 10);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Get write protect status */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200528 dev->win_base = ioremap(link->resource[2]->start,
529 resource_size(link->resource[2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if(!dev->win_base) {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200531 dev_err(&dev->p_dev->dev, "ioremap(%pR) failed\n",
532 link->resource[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200534 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Brian Norrise6453522011-07-19 10:06:11 -0700536 pr_debug("mapped window dev = %p @ %pR, base = %p\n",
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200537 dev, link->resource[2], dev->win_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 dev->offset = 0;
540 dev->pcmcia_map.map_priv_1 = (unsigned long)dev;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200541 dev->pcmcia_map.map_priv_2 = (unsigned long)link->resource[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Alexander Kurzf6763c982010-05-13 11:59:59 +0200543 dev->vpp = (vpp) ? vpp : link->socket->socket.Vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if(setvpp == 2) {
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200545 link->vpp = dev->vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 } else {
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200547 link->vpp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200550 link->config_index = 0;
Brian Norrise6453522011-07-19 10:06:11 -0700551 pr_debug("Setting Configuration\n");
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200552 ret = pcmcia_enable_device(link);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200553 if (ret != 0) {
Amol Lad25f0c652006-09-21 18:12:43 +0530554 if (dev->win_base) {
555 iounmap(dev->win_base);
556 dev->win_base = NULL;
557 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200558 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 if(mem_type == 1) {
562 mtd = do_map_probe("map_ram", &dev->pcmcia_map);
563 } else if(mem_type == 2) {
564 mtd = do_map_probe("map_rom", &dev->pcmcia_map);
565 } else {
Tobias Klauser87d10f32006-03-31 02:29:45 -0800566 for(i = 0; i < ARRAY_SIZE(probes); i++) {
Brian Norrise6453522011-07-19 10:06:11 -0700567 pr_debug("Trying %s\n", probes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 mtd = do_map_probe(probes[i], &dev->pcmcia_map);
569 if(mtd)
570 break;
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000571
Brian Norrise6453522011-07-19 10:06:11 -0700572 pr_debug("FAILED: %s\n", probes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if(!mtd) {
Brian Norrise6453522011-07-19 10:06:11 -0700577 pr_debug("Can not find an MTD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200579 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 dev->mtd_info = mtd;
583 mtd->owner = THIS_MODULE;
584
585 if(new_name) {
586 int size = 0;
587 char unit = ' ';
Alexander Kurz9bdde162010-05-13 12:00:00 +0200588 /* Since we are using a default name, make it better by adding
589 * in the size
590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if(mtd->size < 1048576) { /* <1MiB in size, show size in KiB */
592 size = mtd->size >> 10;
593 unit = 'K';
594 } else {
595 size = mtd->size >> 20;
596 unit = 'M';
597 }
598 snprintf(dev->mtd_name, sizeof(dev->mtd_name), "%d%ciB %s", size, unit, "PCMCIA Memory card");
599 }
600
601 /* If the memory found is fits completely into the mapped PCMCIA window,
602 use the faster non-remapping read/write functions */
603 if(mtd->size <= dev->win_size) {
Brian Norrise6453522011-07-19 10:06:11 -0700604 pr_debug("Using non remapping memory functions\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dev->pcmcia_map.map_priv_2 = (unsigned long)dev->win_base;
606 if (dev->pcmcia_map.bankwidth == 1) {
607 dev->pcmcia_map.read = pcmcia_read8;
608 dev->pcmcia_map.write = pcmcia_write8;
609 } else {
610 dev->pcmcia_map.read = pcmcia_read16;
611 dev->pcmcia_map.write = pcmcia_write16;
612 }
613 dev->pcmcia_map.copy_from = pcmcia_copy_from;
614 dev->pcmcia_map.copy_to = pcmcia_copy_to;
615 }
616
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100617 if (mtd_device_register(mtd, NULL, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 map_destroy(mtd);
619 dev->mtd_info = NULL;
Alexander Kurz9bdde162010-05-13 12:00:00 +0200620 dev_err(&dev->p_dev->dev,
621 "Could not register the MTD device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 pcmciamtd_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200623 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Alexander Kurz9bdde162010-05-13 12:00:00 +0200625 dev_info(&dev->p_dev->dev, "mtd%d: %s\n", mtd->index, mtd->name);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100630static int pcmciamtd_suspend(struct pcmcia_device *dev)
631{
Brian Norrise6453522011-07-19 10:06:11 -0700632 pr_debug("EVENT_PM_RESUME\n");
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100633
634 /* get_lock(link); */
635
636 return 0;
637}
638
639static int pcmciamtd_resume(struct pcmcia_device *dev)
640{
Brian Norrise6453522011-07-19 10:06:11 -0700641 pr_debug("EVENT_PM_SUSPEND\n");
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100642
643 /* free_lock(link); */
644
645 return 0;
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200649static void pcmciamtd_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100651 struct pcmciamtd_dev *dev = link->priv;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100652
Brian Norrise6453522011-07-19 10:06:11 -0700653 pr_debug("link=0x%p\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100655 if(dev->mtd_info) {
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100656 mtd_device_unregister(dev->mtd_info);
Alexander Kurz9bdde162010-05-13 12:00:00 +0200657 dev_info(&dev->p_dev->dev, "mtd%d: Removing\n",
658 dev->mtd_info->index);
Sean Young599fb322006-05-17 11:47:48 +0100659 map_destroy(dev->mtd_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100661
662 pcmciamtd_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200666static int pcmciamtd_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct pcmciamtd_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* Create new memory card device */
Burman Yan95b93a02006-11-15 21:10:29 +0200671 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100672 if (!dev) return -ENOMEM;
Brian Norrise6453522011-07-19 10:06:11 -0700673 pr_debug("dev=0x%p\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200675 dev->p_dev = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 link->priv = dev;
677
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200678 return pcmciamtd_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Joe Perches25f8f542011-05-03 19:29:01 -0700681static const struct pcmcia_device_id pcmciamtd_ids[] = {
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700682 PCMCIA_DEVICE_FUNC_ID(1),
683 PCMCIA_DEVICE_PROD_ID123("IO DATA", "PCS-2M", "2MB SRAM", 0x547e66dc, 0x1fed36cd, 0x36eadd21),
684 PCMCIA_DEVICE_PROD_ID12("IBM", "2MB SRAM", 0xb569a6e5, 0x36eadd21),
685 PCMCIA_DEVICE_PROD_ID12("IBM", "4MB FLASH", 0xb569a6e5, 0x8bc54d2a),
686 PCMCIA_DEVICE_PROD_ID12("IBM", "8MB FLASH", 0xb569a6e5, 0x6df1be3e),
687 PCMCIA_DEVICE_PROD_ID12("Intel", "S2E20SW", 0x816cc815, 0xd14c9dcf),
688 PCMCIA_DEVICE_PROD_ID12("Intel", "S2E8 SW", 0x816cc815, 0xa2d7dedb),
689 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-02 ", 0x40ade711, 0x145cea5c),
690 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-04 ", 0x40ade711, 0x42064dda),
691 PCMCIA_DEVICE_PROD_ID12("intel", "SERIES2-20 ", 0x40ade711, 0x25ee5cb0),
692 PCMCIA_DEVICE_PROD_ID12("intel", "VALUE SERIES 100 ", 0x40ade711, 0xdf8506d8),
693 PCMCIA_DEVICE_PROD_ID12("KINGMAX TECHNOLOGY INC.", "SRAM 256K Bytes", 0x54d0c69c, 0xad12c29c),
694 PCMCIA_DEVICE_PROD_ID12("Maxtor", "MAXFL MobileMax Flash Memory Card", 0xb68968c8, 0x2dfb47b0),
Alexander Kurzf6763c982010-05-13 11:59:59 +0200695 PCMCIA_DEVICE_PROD_ID123("M-Systems", "M-SYS Flash Memory Card", "(c) M-Systems", 0x7ed2ad87, 0x675dc3fb, 0x7aef3965),
Dominik Brodowskib2321ac2010-05-13 12:00:01 +0200696 PCMCIA_DEVICE_PROD_ID12("PRETEC", " 2MB SRAM CARD", 0xebf91155, 0x805360ca),
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700697 PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB101EN20", 0xf9876baf, 0xad0b207b),
698 PCMCIA_DEVICE_PROD_ID12("SEIKO EPSON", "WWB513EN20", 0xf9876baf, 0xe8d884ad),
Alexander Kurzf6763c982010-05-13 11:59:59 +0200699 PCMCIA_DEVICE_PROD_ID12("SMART Modular Technologies", " 4MB FLASH Card", 0x96fd8277, 0x737a5b05),
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700700 PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-3000", 0x05ddca47, 0xe7d67bca),
701 PCMCIA_DEVICE_PROD_ID12("Starfish, Inc.", "REX-4100", 0x05ddca47, 0x7bc32944),
702 /* the following was commented out in pcmcia-cs-3.2.7 */
703 /* PCMCIA_DEVICE_PROD_ID12("RATOC Systems,Inc.", "SmartMedia ADAPTER PC Card", 0xf4a2fefe, 0x5885b2ae), */
704#ifdef CONFIG_MTD_PCMCIA_ANONYMOUS
705 { .match_flags = PCMCIA_DEV_ID_MATCH_ANONYMOUS, },
706#endif
707 PCMCIA_DEVICE_NULL
708};
709MODULE_DEVICE_TABLE(pcmcia, pcmciamtd_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711static struct pcmcia_driver pcmciamtd_driver = {
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200712 .name = "pcmciamtd",
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200713 .probe = pcmciamtd_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100714 .remove = pcmciamtd_detach,
Dominik Brodowski11d28a32005-06-27 16:28:32 -0700715 .owner = THIS_MODULE,
716 .id_table = pcmciamtd_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100717 .suspend = pcmciamtd_suspend,
718 .resume = pcmciamtd_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719};
720
721
722static int __init init_pcmciamtd(void)
723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if(bankwidth && bankwidth != 1 && bankwidth != 2) {
725 info("bad bankwidth (%d), using default", bankwidth);
726 bankwidth = 2;
727 }
728 if(force_size && (force_size < 1 || force_size > 64)) {
729 info("bad force_size (%d), using default", force_size);
730 force_size = 0;
731 }
732 if(mem_type && mem_type != 1 && mem_type != 2) {
733 info("bad mem_type (%d), using default", mem_type);
734 mem_type = 0;
735 }
736 return pcmcia_register_driver(&pcmciamtd_driver);
737}
738
739
740static void __exit exit_pcmciamtd(void)
741{
Brian Norrise6453522011-07-19 10:06:11 -0700742 pr_debug(DRIVER_DESC " unloading");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 pcmcia_unregister_driver(&pcmciamtd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746module_init(init_pcmciamtd);
747module_exit(exit_pcmciamtd);