Blame view

drivers/mtd/devices/pmc551.c 25.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
2
   * $Id: pmc551.c,v 1.32 2005/11/07 11:14:25 gleixner Exp $
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
   *
   * PMC551 PCI Mezzanine Ram Device
   *
   * Author:
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
7
8
   *	Mark Ferrell <mferrell@mvista.com>
   *	Copyright 1999,2000 Nortel Networks
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
   *
   * License:
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
11
12
13
   *	As part of this driver was derived from the slram.c driver it
   *	falls under the same license, which is GNU General Public
   *	License v2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
   *
   * Description:
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   *	This driver is intended to support the PMC551 PCI Ram device
   *	from Ramix Inc.  The PMC551 is a PMC Mezzanine module for
   *	cPCI embedded systems.  The device contains a single SROM
   *	that initially programs the V370PDC chipset onboard the
   *	device, and various banks of DRAM/SDRAM onboard.  This driver
   *	implements this PCI Ram device as an MTD (Memory Technology
   *	Device) so that it can be used to hold a file system, or for
   *	added swap space in embedded systems.  Since the memory on
   *	this board isn't as fast as main memory we do not try to hook
   *	it into main memory as that would simply reduce performance
   *	on the system.  Using it as a block device allows us to use
   *	it as high speed swap or for a high speed disk device of some
   *	sort.  Which becomes very useful on diskless systems in the
   *	embedded market I might add.
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
30
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
   * Notes:
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
32
   *	Due to what I assume is more buggy SROM, the 64M PMC551 I
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
33
34
   *	have available claims that all 4 of its DRAM banks have 64MiB
   *	of ram configured (making a grand total of 256MiB onboard).
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
   *	This is slightly annoying since the BAR0 size reflects the
   *	aperture size, not the dram size, and the V370PDC supplies no
   *	other method for memory size discovery.  This problem is
   *	mostly only relevant when compiled as a module, as the
   *	unloading of the module with an aperture size smaller then
   *	the ram will cause the driver to detect the onboard memory
   *	size to be equal to the aperture size when the module is
   *	reloaded.  Soooo, to help, the module supports an msize
   *	option to allow the specification of the onboard memory, and
   *	an asize option, to allow the specification of the aperture
   *	size.  The aperture must be equal to or less then the memory
   *	size, the driver will correct this if you screw it up.  This
   *	problem is not relevant for compiled in drivers as compiled
   *	in drivers only init once.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
   *
   * Credits:
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
51
52
53
   *	Saeed Karamooz <saeed@ramix.com> of Ramix INC. for the
   *	initial example code of how to initialize this device and for
   *	help with questions I had concerning operation of the device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
   *
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
55
56
57
58
59
60
61
62
63
   *	Most of the MTD code for this driver was originally written
   *	for the slram.o module in the MTD drivers package which
   *	allows the mapping of system memory into an MTD device.
   *	Since the PMC551 memory module is accessed in the same
   *	fashion as system memory, the slram.c code became a very nice
   *	fit to the needs of this driver.  All we added was PCI
   *	detection/initialization to the driver and automatically figure
   *	out the size via the PCI detection.o, later changes by Corey
   *	Minyard set up the card to utilize a 1M sliding apature.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
   *
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
65
66
67
68
69
70
71
   *	Corey Minyard <minyard@nortelnetworks.com>
   *	* Modified driver to utilize a sliding aperture instead of
   *	 mapping all memory into kernel space which turned out to
   *	 be very wasteful.
   *	* Located a bug in the SROM's initialization sequence that
   *	 made the memory unusable, added a fix to code to touch up
   *	 the DRAM some.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
   *
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
73
   * Bugs/FIXMEs:
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
74
75
76
77
78
79
80
81
82
   *	* MUST fix the init function to not spin on a register
   *	waiting for it to set .. this does not safely handle busted
   *	devices that never reset the register correctly which will
   *	cause the system to hang w/ a reboot being the only chance at
   *	recover. [sort of fixed, could be better]
   *	* Add I2C handling of the SROM so we can read the SROM's information
   *	about the aperture size.  This should always accurately reflect the
   *	onboard memory size.
   *	* Comb the init routine.  It's still a bit cludgy on a few things.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
87
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <asm/uaccess.h>
  #include <linux/types.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
93
94
95
96
97
98
  #include <linux/init.h>
  #include <linux/ptrace.h>
  #include <linux/slab.h>
  #include <linux/string.h>
  #include <linux/timer.h>
  #include <linux/major.h>
  #include <linux/fs.h>
  #include <linux/ioctl.h>
  #include <asm/io.h>
  #include <asm/system.h>
  #include <linux/pci.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/pmc551.h>
  #include <linux/mtd/compatmac.h>
  
  static struct mtd_info *pmc551list;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
104
  static int pmc551_erase(struct mtd_info *mtd, struct erase_info *instr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
  {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
106
107
108
109
  	struct mypriv *priv = mtd->priv;
  	u32 soff_hi, soff_lo;	/* start address offset hi/lo */
  	u32 eoff_hi, eoff_lo;	/* end address offset hi/lo */
  	unsigned long end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
  	u_char *ptr;
  	size_t retlen;
  
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
114
115
116
  	printk(KERN_DEBUG "pmc551_erase(pos:%ld, len:%ld)
  ", (long)instr->addr,
  		(long)instr->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
118
  	end = instr->addr + instr->len - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
120
121
  	/* Is it past the end? */
  	if (end > mtd->size) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
123
124
125
  		printk(KERN_DEBUG "pmc551_erase() out of bounds (%ld > %ld)
  ",
  			(long)end, (long)mtd->size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
127
128
129
130
131
132
133
  		return -EINVAL;
  	}
  
  	eoff_hi = end & ~(priv->asize - 1);
  	soff_hi = instr->addr & ~(priv->asize - 1);
  	eoff_lo = end & (priv->asize - 1);
  	soff_lo = instr->addr & (priv->asize - 1);
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
134
135
  	pmc551_point(mtd, instr->addr, instr->len, &retlen,
  		     (void **)&ptr, NULL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
136
137
138
139
140
141
142
143
144
  
  	if (soff_hi == eoff_hi || mtd->size == priv->asize) {
  		/* The whole thing fits within one access, so just one shot
  		   will do it. */
  		memset(ptr, 0xff, instr->len);
  	} else {
  		/* We have to do multiple writes to get all the data
  		   written. */
  		while (soff_hi != eoff_hi) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
146
147
148
  			printk(KERN_DEBUG "pmc551_erase() soff_hi: %ld, "
  				"eoff_hi: %ld
  ", (long)soff_hi, (long)eoff_hi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
150
151
152
153
154
155
  			memset(ptr, 0xff, priv->asize);
  			if (soff_hi + priv->asize >= mtd->size) {
  				goto out;
  			}
  			soff_hi += priv->asize;
  			pmc551_point(mtd, (priv->base_map0 | soff_hi),
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
156
157
  				     priv->asize, &retlen,
  				     (void **)&ptr, NULL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
158
159
160
161
162
  		}
  		memset(ptr, 0xff, eoff_lo);
  	}
  
        out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
  	instr->state = MTD_ERASE_DONE;
  #ifdef CONFIG_MTD_PMC551_DEBUG
  	printk(KERN_DEBUG "pmc551_erase() done
  ");
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
168
169
  	mtd_erase_callback(instr);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  }
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
171
  static int pmc551_point(struct mtd_info *mtd, loff_t from, size_t len,
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
172
  			size_t *retlen, void **virt, resource_size_t *phys)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
174
175
176
  	struct mypriv *priv = mtd->priv;
  	u32 soff_hi;
  	u32 soff_lo;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
181
182
183
184
  
  #ifdef CONFIG_MTD_PMC551_DEBUG
  	printk(KERN_DEBUG "pmc551_point(%ld, %ld)
  ", (long)from, (long)len);
  #endif
  
  	if (from + len > mtd->size) {
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
185
186
187
  		printk(KERN_DEBUG "pmc551_point() out of bounds (%ld > %ld)
  ",
  			(long)from + len, (long)mtd->size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
  #endif
  		return -EINVAL;
  	}
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
191
192
193
  	/* can we return a physical address with this driver? */
  	if (phys)
  		return -EINVAL;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
194
195
  	soff_hi = from & ~(priv->asize - 1);
  	soff_lo = from & (priv->asize - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
  
  	/* Cheap hack optimization */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
198
199
200
  	if (priv->curr_map0 != from) {
  		pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
  					(priv->base_map0 | soff_hi));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
  		priv->curr_map0 = soff_hi;
  	}
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
203
  	*virt = priv->start + soff_lo;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
  	*retlen = len;
  	return 0;
  }
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
207
  static void pmc551_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
  {
  #ifdef CONFIG_MTD_PMC551_DEBUG
  	printk(KERN_DEBUG "pmc551_unpoint()
  ");
  #endif
  }
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
214
215
  static int pmc551_read(struct mtd_info *mtd, loff_t from, size_t len,
  			size_t * retlen, u_char * buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
217
218
219
220
  	struct mypriv *priv = mtd->priv;
  	u32 soff_hi, soff_lo;	/* start address offset hi/lo */
  	u32 eoff_hi, eoff_lo;	/* end address offset hi/lo */
  	unsigned long end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
  	u_char *ptr;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
222
  	u_char *copyto = buf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
  
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
225
226
227
  	printk(KERN_DEBUG "pmc551_read(pos:%ld, len:%ld) asize: %ld
  ",
  		(long)from, (long)len, (long)priv->asize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
229
  	end = from + len - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
231
232
  	/* Is it past the end? */
  	if (end > mtd->size) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
234
235
236
  		printk(KERN_DEBUG "pmc551_read() out of bounds (%ld > %ld)
  ",
  			(long)end, (long)mtd->size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
238
239
240
241
242
243
244
  		return -EINVAL;
  	}
  
  	soff_hi = from & ~(priv->asize - 1);
  	eoff_hi = end & ~(priv->asize - 1);
  	soff_lo = from & (priv->asize - 1);
  	eoff_lo = end & (priv->asize - 1);
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
245
  	pmc551_point(mtd, from, len, retlen, (void **)&ptr, NULL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
246
247
248
249
250
251
252
253
254
255
  
  	if (soff_hi == eoff_hi) {
  		/* The whole thing fits within one access, so just one shot
  		   will do it. */
  		memcpy(copyto, ptr, len);
  		copyto += len;
  	} else {
  		/* We have to do multiple writes to get all the data
  		   written. */
  		while (soff_hi != eoff_hi) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
257
258
259
  			printk(KERN_DEBUG "pmc551_read() soff_hi: %ld, "
  				"eoff_hi: %ld
  ", (long)soff_hi, (long)eoff_hi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
261
262
263
264
265
266
  			memcpy(copyto, ptr, priv->asize);
  			copyto += priv->asize;
  			if (soff_hi + priv->asize >= mtd->size) {
  				goto out;
  			}
  			soff_hi += priv->asize;
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
267
268
  			pmc551_point(mtd, soff_hi, priv->asize, retlen,
  				     (void **)&ptr, NULL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
269
270
271
272
273
274
  		}
  		memcpy(copyto, ptr, eoff_lo);
  		copyto += eoff_lo;
  	}
  
        out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
277
278
  #ifdef CONFIG_MTD_PMC551_DEBUG
  	printk(KERN_DEBUG "pmc551_read() done
  ");
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
279
280
  	*retlen = copyto - buf;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  }
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
282
283
  static int pmc551_write(struct mtd_info *mtd, loff_t to, size_t len,
  			size_t * retlen, const u_char * buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
285
286
287
288
  	struct mypriv *priv = mtd->priv;
  	u32 soff_hi, soff_lo;	/* start address offset hi/lo */
  	u32 eoff_hi, eoff_lo;	/* end address offset hi/lo */
  	unsigned long end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  	u_char *ptr;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
290
  	const u_char *copyfrom = buf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
292
  
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
293
294
295
  	printk(KERN_DEBUG "pmc551_write(pos:%ld, len:%ld) asize:%ld
  ",
  		(long)to, (long)len, (long)priv->asize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
297
298
299
  	end = to + len - 1;
  	/* Is it past the end?  or did the u32 wrap? */
  	if (end > mtd->size) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
301
302
303
304
  		printk(KERN_DEBUG "pmc551_write() out of bounds (end: %ld, "
  			"size: %ld, to: %ld)
  ", (long)end, (long)mtd->size,
  			(long)to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
306
307
308
309
310
311
312
  		return -EINVAL;
  	}
  
  	soff_hi = to & ~(priv->asize - 1);
  	eoff_hi = end & ~(priv->asize - 1);
  	soff_lo = to & (priv->asize - 1);
  	eoff_lo = end & (priv->asize - 1);
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
313
  	pmc551_point(mtd, to, len, retlen, (void **)&ptr, NULL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
314
315
316
317
318
319
320
321
322
323
  
  	if (soff_hi == eoff_hi) {
  		/* The whole thing fits within one access, so just one shot
  		   will do it. */
  		memcpy(ptr, copyfrom, len);
  		copyfrom += len;
  	} else {
  		/* We have to do multiple writes to get all the data
  		   written. */
  		while (soff_hi != eoff_hi) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
325
326
327
  			printk(KERN_DEBUG "pmc551_write() soff_hi: %ld, "
  				"eoff_hi: %ld
  ", (long)soff_hi, (long)eoff_hi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
329
330
331
332
333
334
  			memcpy(ptr, copyfrom, priv->asize);
  			copyfrom += priv->asize;
  			if (soff_hi >= mtd->size) {
  				goto out;
  			}
  			soff_hi += priv->asize;
a98889f3d   Jared Hulbert   [MTD][NOR] Add ph...
335
336
  			pmc551_point(mtd, soff_hi, priv->asize, retlen,
  				     (void **)&ptr, NULL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
337
338
339
340
341
342
  		}
  		memcpy(ptr, copyfrom, eoff_lo);
  		copyfrom += eoff_lo;
  	}
  
        out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
  #ifdef CONFIG_MTD_PMC551_DEBUG
  	printk(KERN_DEBUG "pmc551_write() done
  ");
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
347
348
  	*retlen = copyfrom - buf;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  }
  
  /*
   * Fixup routines for the V370PDC
   * PCI device ID 0x020011b0
   *
   * This function basicly kick starts the DRAM oboard the card and gets it
   * ready to be used.  Before this is done the device reads VERY erratic, so
   * much that it can crash the Linux 2.2.x series kernels when a user cat's
   * /proc/pci .. though that is mainly a kernel bug in handling the PCI DEVSEL
   * register.  FIXME: stop spinning on registers .. must implement a timeout
   * mechanism
   * returns the size of the memory region found.
   */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
363
  static u32 fixup_pmc551(struct pci_dev *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
  {
  #ifdef CONFIG_MTD_PMC551_BUGFIX
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
366
  	u32 dram_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
368
369
  	u32 size, dcmd, cfg, dtmp;
  	u16 cmd, tmp, i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
  	u8 bcmd, counter;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
371
372
373
374
  	/* Sanity Check */
  	if (!dev) {
  		return -ENODEV;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
377
378
379
  
  	/*
  	 * Attempt to reset the card
  	 * FIXME: Stop Spinning registers
  	 */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
380
  	counter = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  	/* unlock registers */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
382
  	pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, 0xA5);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
  	/* read in old data */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
384
  	pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
  	/* bang the reset line up and down for a few */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
386
387
  	for (i = 0; i < 10; i++) {
  		counter = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
  		bcmd &= ~0x80;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
389
  		while (counter++ < 100) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
  			pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
  		}
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
392
  		counter = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
  		bcmd |= 0x80;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
394
  		while (counter++ < 100) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395
396
397
  			pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
  		}
  	}
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
398
  	bcmd |= (0x40 | 0x20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  	pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
400
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
402
403
  	 * Take care and turn off the memory on the device while we
  	 * tweak the configurations
  	 */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
404
405
406
  	pci_read_config_word(dev, PCI_COMMAND, &cmd);
  	tmp = cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  	pci_write_config_word(dev, PCI_COMMAND, tmp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
410
411
  
  	/*
  	 * Disable existing aperture before probing memory size
  	 */
  	pci_read_config_dword(dev, PMC551_PCI_MEM_MAP0, &dcmd);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
412
  	dtmp = (dcmd | PMC551_PCI_MEM_MAP_ENABLE | PMC551_PCI_MEM_MAP_REG_EN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
417
  	pci_write_config_dword(dev, PMC551_PCI_MEM_MAP0, dtmp);
  	/*
  	 * Grab old BAR0 config so that we can figure out memory size
  	 * This is another bit of kludge going on.  The reason for the
  	 * redundancy is I am hoping to retain the original configuration
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
418
  	 * previously assigned to the card by the BIOS or some previous
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
420
421
422
  	 * fixup routine in the kernel.  So we read the old config into cfg,
  	 * then write all 1's to the memory space, read back the result into
  	 * "size", and then write back all the old config.
  	 */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
423
  	pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &cfg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424
  #ifndef CONFIG_MTD_PMC551_BUGFIX
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
425
426
427
428
429
  	pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, ~0);
  	pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &size);
  	size = (size & PCI_BASE_ADDRESS_MEM_MASK);
  	size &= ~(size - 1);
  	pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, cfg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
  #else
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
  	/*
  	 * Get the size of the memory by reading all the DRAM size values
  	 * and adding them up.
  	 *
  	 * KLUDGE ALERT: the boards we are using have invalid column and
  	 * row mux values.  We fix them here, but this will break other
  	 * memory configurations.
  	 */
  	pci_read_config_dword(dev, PMC551_DRAM_BLK0, &dram_data);
  	size = PMC551_DRAM_BLK_GET_SIZE(dram_data);
  	dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  	dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  	pci_write_config_dword(dev, PMC551_DRAM_BLK0, dram_data);
  
  	pci_read_config_dword(dev, PMC551_DRAM_BLK1, &dram_data);
  	size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
  	dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  	dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  	pci_write_config_dword(dev, PMC551_DRAM_BLK1, dram_data);
  
  	pci_read_config_dword(dev, PMC551_DRAM_BLK2, &dram_data);
  	size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
  	dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  	dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  	pci_write_config_dword(dev, PMC551_DRAM_BLK2, dram_data);
  
  	pci_read_config_dword(dev, PMC551_DRAM_BLK3, &dram_data);
  	size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
  	dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
  	dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
  	pci_write_config_dword(dev, PMC551_DRAM_BLK3, dram_data);
  
  	/*
  	 * Oops .. something went wrong
  	 */
  	if ((size &= PCI_BASE_ADDRESS_MEM_MASK) == 0) {
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
  	}
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
469
  #endif				/* CONFIG_MTD_PMC551_BUGFIX */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  	if ((cfg & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
  		return -ENODEV;
  	}
  
  	/*
  	 * Precharge Dram
  	 */
  	pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0400);
  	pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x00bf);
  
  	/*
  	 * Wait until command has gone through
  	 * FIXME: register spinning issue
  	 */
  	do {
  		pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
  		if (counter++ > 100)
  			break;
  	} while ((PCI_COMMAND_IO) & cmd);
  
  	/*
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
492
  	 * Turn on auto refresh
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
494
495
  	 * The loop is taken directly from Ramix's example code.  I assume that
  	 * this must be held high for some duration of time, but I can find no
  	 * documentation refrencing the reasons why.
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  	 */
  	for (i = 1; i <= 8; i++) {
  		pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0df);
  
  		/*
  		 * Make certain command has gone through
  		 * FIXME: register spinning issue
  		 */
  		counter = 0;
  		do {
  			pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
  			if (counter++ > 100)
  				break;
  		} while ((PCI_COMMAND_IO) & cmd);
  	}
  
  	pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0020);
  	pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0ff);
  
  	/*
  	 * Wait until command completes
  	 * FIXME: register spinning issue
  	 */
  	counter = 0;
  	do {
  		pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
  		if (counter++ > 100)
  			break;
  	} while ((PCI_COMMAND_IO) & cmd);
  
  	pci_read_config_dword(dev, PMC551_DRAM_CFG, &dcmd);
  	dcmd |= 0x02000000;
  	pci_write_config_dword(dev, PMC551_DRAM_CFG, dcmd);
  
  	/*
  	 * Check to make certain fast back-to-back, if not
  	 * then set it so
  	 */
  	pci_read_config_word(dev, PCI_STATUS, &cmd);
  	if ((cmd & PCI_COMMAND_FAST_BACK) == 0) {
  		cmd |= PCI_COMMAND_FAST_BACK;
  		pci_write_config_word(dev, PCI_STATUS, cmd);
  	}
  
  	/*
  	 * Check to make certain the DEVSEL is set correctly, this device
  	 * has a tendancy to assert DEVSEL and TRDY when a write is performed
  	 * to the memory when memory is read-only
  	 */
  	if ((cmd & PCI_STATUS_DEVSEL_MASK) != 0x0) {
  		cmd &= ~PCI_STATUS_DEVSEL_MASK;
  		pci_write_config_word(dev, PCI_STATUS, cmd);
  	}
  	/*
  	 * Set to be prefetchable and put everything back based on old cfg.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
  	 * it's possible that the reset of the V370PDC nuked the original
  	 * setup
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
553
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554
  	/*
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
555
556
557
558
559
560
561
562
563
  	   cfg |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  	   pci_write_config_dword( dev, PCI_BASE_ADDRESS_0, cfg );
  	 */
  
  	/*
  	 * Turn PCI memory and I/O bus access back on
  	 */
  	pci_write_config_word(dev, PCI_COMMAND,
  			      PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
565
566
567
  	/*
  	 * Some screen fun
  	 */
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
568
  	printk(KERN_DEBUG "pmc551: %d%sB (0x%x) of %sprefetchable memory at "
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
569
570
571
  		"0x%llx
  ", (size < 1024) ? size : (size < 1048576) ?
  		size >> 10 : size >> 20,
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
572
  		(size < 1024) ? "" : (size < 1048576) ? "Ki" : "Mi", size,
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
573
  		((dcmd & (0x1 << 3)) == 0) ? "non-" : "",
98aacdfde   Jiri Slaby   [MTD] pmc551 pci ...
574
  		(unsigned long long)pci_resource_start(dev, 0));
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  
  	/*
  	 * Check to see the state of the memory
  	 */
  	pci_read_config_dword(dev, PMC551_DRAM_BLK0, &dcmd);
  	printk(KERN_DEBUG "pmc551: DRAM_BLK0 Flags: %s,%s
  "
  		"pmc551: DRAM_BLK0 Size: %d at %d
  "
  		"pmc551: DRAM_BLK0 Row MUX: %d, Col MUX: %d
  ",
  		(((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  		(((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  		PMC551_DRAM_BLK_GET_SIZE(dcmd),
  		((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  		((dcmd >> 9) & 0xF));
  
  	pci_read_config_dword(dev, PMC551_DRAM_BLK1, &dcmd);
  	printk(KERN_DEBUG "pmc551: DRAM_BLK1 Flags: %s,%s
  "
  		"pmc551: DRAM_BLK1 Size: %d at %d
  "
  		"pmc551: DRAM_BLK1 Row MUX: %d, Col MUX: %d
  ",
  		(((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  		(((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  		PMC551_DRAM_BLK_GET_SIZE(dcmd),
  		((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  		((dcmd >> 9) & 0xF));
  
  	pci_read_config_dword(dev, PMC551_DRAM_BLK2, &dcmd);
  	printk(KERN_DEBUG "pmc551: DRAM_BLK2 Flags: %s,%s
  "
  		"pmc551: DRAM_BLK2 Size: %d at %d
  "
  		"pmc551: DRAM_BLK2 Row MUX: %d, Col MUX: %d
  ",
  		(((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  		(((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  		PMC551_DRAM_BLK_GET_SIZE(dcmd),
  		((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  		((dcmd >> 9) & 0xF));
  
  	pci_read_config_dword(dev, PMC551_DRAM_BLK3, &dcmd);
  	printk(KERN_DEBUG "pmc551: DRAM_BLK3 Flags: %s,%s
  "
  		"pmc551: DRAM_BLK3 Size: %d at %d
  "
  		"pmc551: DRAM_BLK3 Row MUX: %d, Col MUX: %d
  ",
  		(((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
  		(((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
  		PMC551_DRAM_BLK_GET_SIZE(dcmd),
  		((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
  		((dcmd >> 9) & 0xF));
  
  	pci_read_config_word(dev, PCI_COMMAND, &cmd);
  	printk(KERN_DEBUG "pmc551: Memory Access %s
  ",
  		(((0x1 << 1) & cmd) == 0) ? "off" : "on");
  	printk(KERN_DEBUG "pmc551: I/O Access %s
  ",
  		(((0x1 << 0) & cmd) == 0) ? "off" : "on");
  
  	pci_read_config_word(dev, PCI_STATUS, &cmd);
  	printk(KERN_DEBUG "pmc551: Devsel %s
  ",
  		((PCI_STATUS_DEVSEL_MASK & cmd) == 0x000) ? "Fast" :
  		((PCI_STATUS_DEVSEL_MASK & cmd) == 0x200) ? "Medium" :
  		((PCI_STATUS_DEVSEL_MASK & cmd) == 0x400) ? "Slow" : "Invalid");
  
  	printk(KERN_DEBUG "pmc551: %sFast Back-to-Back
  ",
  		((PCI_COMMAND_FAST_BACK & cmd) == 0) ? "Not " : "");
  
  	pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
  	printk(KERN_DEBUG "pmc551: EEPROM is under %s control
  "
  		"pmc551: System Control Register is %slocked to PCI access
  "
  		"pmc551: System Control Register is %slocked to EEPROM access
  ",
  		(bcmd & 0x1) ? "software" : "hardware",
  		(bcmd & 0x20) ? "" : "un", (bcmd & 0x40) ? "" : "un");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
660
  	return size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
662
663
664
665
  }
  
  /*
   * Kernel version specific module stuffages
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
668
669
670
671
672
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Mark Ferrell <mferrell@mvista.com>");
  MODULE_DESCRIPTION(PMC551_VERSION);
  
  /*
   * Stuff these outside the ifdef so as to not bust compiled in driver support
   */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
673
  static int msize = 0;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
674
  static int asize = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
676
  
  module_param(msize, int, 0);
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
677
  MODULE_PARM_DESC(msize, "memory size in MiB [1 - 1024]");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
681
682
683
684
685
  module_param(asize, int, 0);
  MODULE_PARM_DESC(asize, "aperture size, must be <= memsize [1-1024]");
  
  /*
   * PMC551 Card Initialization
   */
  static int __init init_pmc551(void)
  {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
686
687
688
689
690
691
692
693
694
695
696
697
  	struct pci_dev *PCI_Device = NULL;
  	struct mypriv *priv;
  	int count, found = 0;
  	struct mtd_info *mtd;
  	u32 length = 0;
  
  	if (msize) {
  		msize = (1 << (ffs(msize) - 1)) << 20;
  		if (msize > (1 << 30)) {
  			printk(KERN_NOTICE "pmc551: Invalid memory size [%d]
  ",
  				msize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698
699
700
  			return -EINVAL;
  		}
  	}
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
701
702
703
704
705
706
  	if (asize) {
  		asize = (1 << (ffs(asize) - 1)) << 20;
  		if (asize > (1 << 30)) {
  			printk(KERN_NOTICE "pmc551: Invalid aperture size "
  				"[%d]
  ", asize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707
708
709
  			return -EINVAL;
  		}
  	}
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  	printk(KERN_INFO PMC551_VERSION);
  
  	/*
  	 * PCU-bus chipset probe.
  	 */
  	for (count = 0; count < MAX_MTD_DEVICES; count++) {
  
  		if ((PCI_Device = pci_get_device(PCI_VENDOR_ID_V3_SEMI,
  						  PCI_DEVICE_ID_V3_SEMI_V370PDC,
  						  PCI_Device)) == NULL) {
  			break;
  		}
  
  		printk(KERN_NOTICE "pmc551: Found PCI V370PDC at 0x%llx
  ",
98aacdfde   Jiri Slaby   [MTD] pmc551 pci ...
725
  			(unsigned long long)pci_resource_start(PCI_Device, 0));
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
726
727
728
729
730
731
732
733
734
735
736
737
738
739
  
  		/*
  		 * The PMC551 device acts VERY weird if you don't init it
  		 * first.  i.e. it will not correctly report devsel.  If for
  		 * some reason the sdram is in a wrote-protected state the
  		 * device will DEVSEL when it is written to causing problems
  		 * with the oldproc.c driver in
  		 * some kernels (2.2.*)
  		 */
  		if ((length = fixup_pmc551(PCI_Device)) <= 0) {
  			printk(KERN_NOTICE "pmc551: Cannot init SDRAM
  ");
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
741
742
743
744
  
  		/*
  		 * This is needed until the driver is capable of reading the
  		 * onboard I2C SROM to discover the "real" memory size.
  		 */
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
745
  		if (msize) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
  			length = msize;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
747
748
749
  			printk(KERN_NOTICE "pmc551: Using specified memory "
  				"size 0x%x
  ", length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
751
752
  		} else {
  			msize = length;
  		}
7fefb924d   Jiri Slaby   [MTD] pmc551 use ...
753
  		mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
754
755
756
757
758
759
  		if (!mtd) {
  			printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
  				"device.
  ");
  			break;
  		}
7fefb924d   Jiri Slaby   [MTD] pmc551 use ...
760
  		priv = kzalloc(sizeof(struct mypriv), GFP_KERNEL);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
761
762
763
764
765
766
767
  		if (!priv) {
  			printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
  				"device.
  ");
  			kfree(mtd);
  			break;
  		}
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
768
769
770
771
772
773
774
  		mtd->priv = priv;
  		priv->dev = PCI_Device;
  
  		if (asize > length) {
  			printk(KERN_NOTICE "pmc551: reducing aperture size to "
  				"fit %dM
  ", length >> 20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
776
  			priv->asize = asize = length;
  		} else if (asize == 0 || asize == length) {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
777
778
779
  			printk(KERN_NOTICE "pmc551: Using existing aperture "
  				"size %dM
  ", length >> 20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
781
  			priv->asize = asize = length;
  		} else {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
782
783
784
  			printk(KERN_NOTICE "pmc551: Using specified aperture "
  				"size %dM
  ", asize >> 20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
786
  			priv->asize = asize;
  		}
98aacdfde   Jiri Slaby   [MTD] pmc551 pci ...
787
  		priv->start = pci_iomap(PCI_Device, 0, priv->asize);
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
788

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
790
791
  		if (!priv->start) {
  			printk(KERN_NOTICE "pmc551: Unable to map IO space
  ");
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
792
793
  			kfree(mtd->priv);
  			kfree(mtd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
797
798
799
  		printk(KERN_DEBUG "pmc551: setting aperture to %d
  ",
  			ffs(priv->asize >> 20) - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
800
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
801
802
803
804
805
806
  		priv->base_map0 = (PMC551_PCI_MEM_MAP_REG_EN
  				   | PMC551_PCI_MEM_MAP_ENABLE
  				   | (ffs(priv->asize >> 20) - 1) << 4);
  		priv->curr_map0 = priv->base_map0;
  		pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
  					priv->curr_map0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
808
  
  #ifdef CONFIG_MTD_PMC551_DEBUG
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
809
810
811
  		printk(KERN_DEBUG "pmc551: aperture set to %d
  ",
  			(priv->base_map0 & 0xF0) >> 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
  #endif
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  		mtd->size = msize;
  		mtd->flags = MTD_CAP_RAM;
  		mtd->erase = pmc551_erase;
  		mtd->read = pmc551_read;
  		mtd->write = pmc551_write;
  		mtd->point = pmc551_point;
  		mtd->unpoint = pmc551_unpoint;
  		mtd->type = MTD_RAM;
  		mtd->name = "PMC551 RAM board";
  		mtd->erasesize = 0x10000;
  		mtd->writesize = 1;
  		mtd->owner = THIS_MODULE;
  
  		if (add_mtd_device(mtd)) {
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
827
828
  			printk(KERN_NOTICE "pmc551: Failed to register new device
  ");
98aacdfde   Jiri Slaby   [MTD] pmc551 pci ...
829
  			pci_iounmap(PCI_Device, priv->start);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
830
831
832
833
834
835
836
837
838
839
  			kfree(mtd->priv);
  			kfree(mtd);
  			break;
  		}
  
  		/* Keep a reference as the add_mtd_device worked */
  		pci_dev_get(PCI_Device);
  
  		printk(KERN_NOTICE "Registered pmc551 memory device.
  ");
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
840
841
  		printk(KERN_NOTICE "Mapped %dMiB of memory from 0x%p to 0x%p
  ",
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
842
843
  			priv->asize >> 20,
  			priv->start, priv->start + priv->asize);
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
844
845
  		printk(KERN_NOTICE "Total memory is %d%sB
  ",
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
846
847
  			(length < 1024) ? length :
  			(length < 1048576) ? length >> 10 : length >> 20,
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
848
  			(length < 1024) ? "" : (length < 1048576) ? "Ki" : "Mi");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
850
851
  		priv->nextpmc551 = pmc551list;
  		pmc551list = mtd;
  		found++;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
852
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
853

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
854
855
856
  	/* Exited early, reference left over */
  	if (PCI_Device)
  		pci_dev_put(PCI_Device);
dd8e9ed6e   Alan Cox   [MTD] Switch to p...
857

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
858
859
860
861
862
  	if (!pmc551list) {
  		printk(KERN_NOTICE "pmc551: not detected
  ");
  		return -ENODEV;
  	} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863
864
  		printk(KERN_NOTICE "pmc551: %d pmc551 devices loaded
  ", found);
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
865
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866
867
868
869
870
871
872
873
  	}
  }
  
  /*
   * PMC551 Card Cleanup
   */
  static void __exit cleanup_pmc551(void)
  {
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
874
875
  	int found = 0;
  	struct mtd_info *mtd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
876
  	struct mypriv *priv;
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
877
  	while ((mtd = pmc551list)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
879
  		priv = mtd->priv;
  		pmc551list = priv->nextpmc551;
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
880

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
881
  		if (priv->start) {
8e0aedc5b   David Woodhouse   [MTD] Use proper ...
882
  			printk(KERN_DEBUG "pmc551: unmapping %dMiB starting at "
cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
883
884
  				"0x%p
  ", priv->asize >> 20, priv->start);
98aacdfde   Jiri Slaby   [MTD] pmc551 pci ...
885
  			pci_iounmap(priv->dev, priv->start);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
886
  		}
dd8e9ed6e   Alan Cox   [MTD] Switch to p...
887
  		pci_dev_put(priv->dev);
e5580fbe8   Thomas Gleixner   [MTD] devices: Cl...
888

cdf0a7d16   Jiri Slaby   [MTD] pmc551 whit...
889
890
891
  		kfree(mtd->priv);
  		del_mtd_device(mtd);
  		kfree(mtd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
893
894
895
896
897
898
899
900
  		found++;
  	}
  
  	printk(KERN_NOTICE "pmc551: %d pmc551 devices unloaded
  ", found);
  }
  
  module_init(init_pmc551);
  module_exit(cleanup_pmc551);