Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6-block.git] / drivers / mtd / devices / pmc551.c
1 /*
2  * PMC551 PCI Mezzanine Ram Device
3  *
4  * Author:
5  *      Mark Ferrell <mferrell@mvista.com>
6  *      Copyright 1999,2000 Nortel Networks
7  *
8  * License:
9  *      As part of this driver was derived from the slram.c driver it
10  *      falls under the same license, which is GNU General Public
11  *      License v2
12  *
13  * Description:
14  *      This driver is intended to support the PMC551 PCI Ram device
15  *      from Ramix Inc.  The PMC551 is a PMC Mezzanine module for
16  *      cPCI embedded systems.  The device contains a single SROM
17  *      that initially programs the V370PDC chipset onboard the
18  *      device, and various banks of DRAM/SDRAM onboard.  This driver
19  *      implements this PCI Ram device as an MTD (Memory Technology
20  *      Device) so that it can be used to hold a file system, or for
21  *      added swap space in embedded systems.  Since the memory on
22  *      this board isn't as fast as main memory we do not try to hook
23  *      it into main memory as that would simply reduce performance
24  *      on the system.  Using it as a block device allows us to use
25  *      it as high speed swap or for a high speed disk device of some
26  *      sort.  Which becomes very useful on diskless systems in the
27  *      embedded market I might add.
28  *
29  * Notes:
30  *      Due to what I assume is more buggy SROM, the 64M PMC551 I
31  *      have available claims that all 4 of its DRAM banks have 64MiB
32  *      of ram configured (making a grand total of 256MiB onboard).
33  *      This is slightly annoying since the BAR0 size reflects the
34  *      aperture size, not the dram size, and the V370PDC supplies no
35  *      other method for memory size discovery.  This problem is
36  *      mostly only relevant when compiled as a module, as the
37  *      unloading of the module with an aperture size smaller than
38  *      the ram will cause the driver to detect the onboard memory
39  *      size to be equal to the aperture size when the module is
40  *      reloaded.  Soooo, to help, the module supports an msize
41  *      option to allow the specification of the onboard memory, and
42  *      an asize option, to allow the specification of the aperture
43  *      size.  The aperture must be equal to or less then the memory
44  *      size, the driver will correct this if you screw it up.  This
45  *      problem is not relevant for compiled in drivers as compiled
46  *      in drivers only init once.
47  *
48  * Credits:
49  *      Saeed Karamooz <saeed@ramix.com> of Ramix INC. for the
50  *      initial example code of how to initialize this device and for
51  *      help with questions I had concerning operation of the device.
52  *
53  *      Most of the MTD code for this driver was originally written
54  *      for the slram.o module in the MTD drivers package which
55  *      allows the mapping of system memory into an MTD device.
56  *      Since the PMC551 memory module is accessed in the same
57  *      fashion as system memory, the slram.c code became a very nice
58  *      fit to the needs of this driver.  All we added was PCI
59  *      detection/initialization to the driver and automatically figure
60  *      out the size via the PCI detection.o, later changes by Corey
61  *      Minyard set up the card to utilize a 1M sliding apature.
62  *
63  *      Corey Minyard <minyard@nortelnetworks.com>
64  *      * Modified driver to utilize a sliding aperture instead of
65  *       mapping all memory into kernel space which turned out to
66  *       be very wasteful.
67  *      * Located a bug in the SROM's initialization sequence that
68  *       made the memory unusable, added a fix to code to touch up
69  *       the DRAM some.
70  *
71  * Bugs/FIXMEs:
72  *      * MUST fix the init function to not spin on a register
73  *      waiting for it to set .. this does not safely handle busted
74  *      devices that never reset the register correctly which will
75  *      cause the system to hang w/ a reboot being the only chance at
76  *      recover. [sort of fixed, could be better]
77  *      * Add I2C handling of the SROM so we can read the SROM's information
78  *      about the aperture size.  This should always accurately reflect the
79  *      onboard memory size.
80  *      * Comb the init routine.  It's still a bit cludgy on a few things.
81  */
82
83 #include <linux/kernel.h>
84 #include <linux/module.h>
85 #include <asm/uaccess.h>
86 #include <linux/types.h>
87 #include <linux/init.h>
88 #include <linux/ptrace.h>
89 #include <linux/slab.h>
90 #include <linux/string.h>
91 #include <linux/timer.h>
92 #include <linux/major.h>
93 #include <linux/fs.h>
94 #include <linux/ioctl.h>
95 #include <asm/io.h>
96 #include <linux/pci.h>
97
98 #include <linux/mtd/mtd.h>
99 #include <linux/mtd/pmc551.h>
100
101 static struct mtd_info *pmc551list;
102
103 static int pmc551_erase(struct mtd_info *mtd, struct erase_info *instr)
104 {
105         struct mypriv *priv = mtd->priv;
106         u32 soff_hi, soff_lo;   /* start address offset hi/lo */
107         u32 eoff_hi, eoff_lo;   /* end address offset hi/lo */
108         unsigned long end;
109         u_char *ptr;
110         size_t retlen;
111
112 #ifdef CONFIG_MTD_PMC551_DEBUG
113         printk(KERN_DEBUG "pmc551_erase(pos:%ld, len:%ld)\n", (long)instr->addr,
114                 (long)instr->len);
115 #endif
116
117         end = instr->addr + instr->len - 1;
118
119         /* Is it past the end? */
120         if (end > mtd->size) {
121 #ifdef CONFIG_MTD_PMC551_DEBUG
122                 printk(KERN_DEBUG "pmc551_erase() out of bounds (%ld > %ld)\n",
123                         (long)end, (long)mtd->size);
124 #endif
125                 return -EINVAL;
126         }
127
128         eoff_hi = end & ~(priv->asize - 1);
129         soff_hi = instr->addr & ~(priv->asize - 1);
130         eoff_lo = end & (priv->asize - 1);
131         soff_lo = instr->addr & (priv->asize - 1);
132
133         pmc551_point(mtd, instr->addr, instr->len, &retlen,
134                      (void **)&ptr, NULL);
135
136         if (soff_hi == eoff_hi || mtd->size == priv->asize) {
137                 /* The whole thing fits within one access, so just one shot
138                    will do it. */
139                 memset(ptr, 0xff, instr->len);
140         } else {
141                 /* We have to do multiple writes to get all the data
142                    written. */
143                 while (soff_hi != eoff_hi) {
144 #ifdef CONFIG_MTD_PMC551_DEBUG
145                         printk(KERN_DEBUG "pmc551_erase() soff_hi: %ld, "
146                                 "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
147 #endif
148                         memset(ptr, 0xff, priv->asize);
149                         if (soff_hi + priv->asize >= mtd->size) {
150                                 goto out;
151                         }
152                         soff_hi += priv->asize;
153                         pmc551_point(mtd, (priv->base_map0 | soff_hi),
154                                      priv->asize, &retlen,
155                                      (void **)&ptr, NULL);
156                 }
157                 memset(ptr, 0xff, eoff_lo);
158         }
159
160       out:
161         instr->state = MTD_ERASE_DONE;
162 #ifdef CONFIG_MTD_PMC551_DEBUG
163         printk(KERN_DEBUG "pmc551_erase() done\n");
164 #endif
165
166         mtd_erase_callback(instr);
167         return 0;
168 }
169
170 static int pmc551_point(struct mtd_info *mtd, loff_t from, size_t len,
171                         size_t *retlen, void **virt, resource_size_t *phys)
172 {
173         struct mypriv *priv = mtd->priv;
174         u32 soff_hi;
175         u32 soff_lo;
176
177 #ifdef CONFIG_MTD_PMC551_DEBUG
178         printk(KERN_DEBUG "pmc551_point(%ld, %ld)\n", (long)from, (long)len);
179 #endif
180
181         if (from + len > mtd->size) {
182 #ifdef CONFIG_MTD_PMC551_DEBUG
183                 printk(KERN_DEBUG "pmc551_point() out of bounds (%ld > %ld)\n",
184                         (long)from + len, (long)mtd->size);
185 #endif
186                 return -EINVAL;
187         }
188
189         /* can we return a physical address with this driver? */
190         if (phys)
191                 return -EINVAL;
192
193         soff_hi = from & ~(priv->asize - 1);
194         soff_lo = from & (priv->asize - 1);
195
196         /* Cheap hack optimization */
197         if (priv->curr_map0 != from) {
198                 pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
199                                         (priv->base_map0 | soff_hi));
200                 priv->curr_map0 = soff_hi;
201         }
202
203         *virt = priv->start + soff_lo;
204         *retlen = len;
205         return 0;
206 }
207
208 static void pmc551_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
209 {
210 #ifdef CONFIG_MTD_PMC551_DEBUG
211         printk(KERN_DEBUG "pmc551_unpoint()\n");
212 #endif
213 }
214
215 static int pmc551_read(struct mtd_info *mtd, loff_t from, size_t len,
216                         size_t * retlen, u_char * buf)
217 {
218         struct mypriv *priv = mtd->priv;
219         u32 soff_hi, soff_lo;   /* start address offset hi/lo */
220         u32 eoff_hi, eoff_lo;   /* end address offset hi/lo */
221         unsigned long end;
222         u_char *ptr;
223         u_char *copyto = buf;
224
225 #ifdef CONFIG_MTD_PMC551_DEBUG
226         printk(KERN_DEBUG "pmc551_read(pos:%ld, len:%ld) asize: %ld\n",
227                 (long)from, (long)len, (long)priv->asize);
228 #endif
229
230         end = from + len - 1;
231
232         /* Is it past the end? */
233         if (end > mtd->size) {
234 #ifdef CONFIG_MTD_PMC551_DEBUG
235                 printk(KERN_DEBUG "pmc551_read() out of bounds (%ld > %ld)\n",
236                         (long)end, (long)mtd->size);
237 #endif
238                 return -EINVAL;
239         }
240
241         soff_hi = from & ~(priv->asize - 1);
242         eoff_hi = end & ~(priv->asize - 1);
243         soff_lo = from & (priv->asize - 1);
244         eoff_lo = end & (priv->asize - 1);
245
246         pmc551_point(mtd, from, len, retlen, (void **)&ptr, NULL);
247
248         if (soff_hi == eoff_hi) {
249                 /* The whole thing fits within one access, so just one shot
250                    will do it. */
251                 memcpy(copyto, ptr, len);
252                 copyto += len;
253         } else {
254                 /* We have to do multiple writes to get all the data
255                    written. */
256                 while (soff_hi != eoff_hi) {
257 #ifdef CONFIG_MTD_PMC551_DEBUG
258                         printk(KERN_DEBUG "pmc551_read() soff_hi: %ld, "
259                                 "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
260 #endif
261                         memcpy(copyto, ptr, priv->asize);
262                         copyto += priv->asize;
263                         if (soff_hi + priv->asize >= mtd->size) {
264                                 goto out;
265                         }
266                         soff_hi += priv->asize;
267                         pmc551_point(mtd, soff_hi, priv->asize, retlen,
268                                      (void **)&ptr, NULL);
269                 }
270                 memcpy(copyto, ptr, eoff_lo);
271                 copyto += eoff_lo;
272         }
273
274       out:
275 #ifdef CONFIG_MTD_PMC551_DEBUG
276         printk(KERN_DEBUG "pmc551_read() done\n");
277 #endif
278         *retlen = copyto - buf;
279         return 0;
280 }
281
282 static int pmc551_write(struct mtd_info *mtd, loff_t to, size_t len,
283                         size_t * retlen, const u_char * buf)
284 {
285         struct mypriv *priv = mtd->priv;
286         u32 soff_hi, soff_lo;   /* start address offset hi/lo */
287         u32 eoff_hi, eoff_lo;   /* end address offset hi/lo */
288         unsigned long end;
289         u_char *ptr;
290         const u_char *copyfrom = buf;
291
292 #ifdef CONFIG_MTD_PMC551_DEBUG
293         printk(KERN_DEBUG "pmc551_write(pos:%ld, len:%ld) asize:%ld\n",
294                 (long)to, (long)len, (long)priv->asize);
295 #endif
296
297         end = to + len - 1;
298         /* Is it past the end?  or did the u32 wrap? */
299         if (end > mtd->size) {
300 #ifdef CONFIG_MTD_PMC551_DEBUG
301                 printk(KERN_DEBUG "pmc551_write() out of bounds (end: %ld, "
302                         "size: %ld, to: %ld)\n", (long)end, (long)mtd->size,
303                         (long)to);
304 #endif
305                 return -EINVAL;
306         }
307
308         soff_hi = to & ~(priv->asize - 1);
309         eoff_hi = end & ~(priv->asize - 1);
310         soff_lo = to & (priv->asize - 1);
311         eoff_lo = end & (priv->asize - 1);
312
313         pmc551_point(mtd, to, len, retlen, (void **)&ptr, NULL);
314
315         if (soff_hi == eoff_hi) {
316                 /* The whole thing fits within one access, so just one shot
317                    will do it. */
318                 memcpy(ptr, copyfrom, len);
319                 copyfrom += len;
320         } else {
321                 /* We have to do multiple writes to get all the data
322                    written. */
323                 while (soff_hi != eoff_hi) {
324 #ifdef CONFIG_MTD_PMC551_DEBUG
325                         printk(KERN_DEBUG "pmc551_write() soff_hi: %ld, "
326                                 "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
327 #endif
328                         memcpy(ptr, copyfrom, priv->asize);
329                         copyfrom += priv->asize;
330                         if (soff_hi >= mtd->size) {
331                                 goto out;
332                         }
333                         soff_hi += priv->asize;
334                         pmc551_point(mtd, soff_hi, priv->asize, retlen,
335                                      (void **)&ptr, NULL);
336                 }
337                 memcpy(ptr, copyfrom, eoff_lo);
338                 copyfrom += eoff_lo;
339         }
340
341       out:
342 #ifdef CONFIG_MTD_PMC551_DEBUG
343         printk(KERN_DEBUG "pmc551_write() done\n");
344 #endif
345         *retlen = copyfrom - buf;
346         return 0;
347 }
348
349 /*
350  * Fixup routines for the V370PDC
351  * PCI device ID 0x020011b0
352  *
353  * This function basically kick starts the DRAM oboard the card and gets it
354  * ready to be used.  Before this is done the device reads VERY erratic, so
355  * much that it can crash the Linux 2.2.x series kernels when a user cat's
356  * /proc/pci .. though that is mainly a kernel bug in handling the PCI DEVSEL
357  * register.  FIXME: stop spinning on registers .. must implement a timeout
358  * mechanism
359  * returns the size of the memory region found.
360  */
361 static u32 fixup_pmc551(struct pci_dev *dev)
362 {
363 #ifdef CONFIG_MTD_PMC551_BUGFIX
364         u32 dram_data;
365 #endif
366         u32 size, dcmd, cfg, dtmp;
367         u16 cmd, tmp, i;
368         u8 bcmd, counter;
369
370         /* Sanity Check */
371         if (!dev) {
372                 return -ENODEV;
373         }
374
375         /*
376          * Attempt to reset the card
377          * FIXME: Stop Spinning registers
378          */
379         counter = 0;
380         /* unlock registers */
381         pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, 0xA5);
382         /* read in old data */
383         pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
384         /* bang the reset line up and down for a few */
385         for (i = 0; i < 10; i++) {
386                 counter = 0;
387                 bcmd &= ~0x80;
388                 while (counter++ < 100) {
389                         pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
390                 }
391                 counter = 0;
392                 bcmd |= 0x80;
393                 while (counter++ < 100) {
394                         pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
395                 }
396         }
397         bcmd |= (0x40 | 0x20);
398         pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
399
400         /*
401          * Take care and turn off the memory on the device while we
402          * tweak the configurations
403          */
404         pci_read_config_word(dev, PCI_COMMAND, &cmd);
405         tmp = cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
406         pci_write_config_word(dev, PCI_COMMAND, tmp);
407
408         /*
409          * Disable existing aperture before probing memory size
410          */
411         pci_read_config_dword(dev, PMC551_PCI_MEM_MAP0, &dcmd);
412         dtmp = (dcmd | PMC551_PCI_MEM_MAP_ENABLE | PMC551_PCI_MEM_MAP_REG_EN);
413         pci_write_config_dword(dev, PMC551_PCI_MEM_MAP0, dtmp);
414         /*
415          * Grab old BAR0 config so that we can figure out memory size
416          * This is another bit of kludge going on.  The reason for the
417          * redundancy is I am hoping to retain the original configuration
418          * previously assigned to the card by the BIOS or some previous
419          * fixup routine in the kernel.  So we read the old config into cfg,
420          * then write all 1's to the memory space, read back the result into
421          * "size", and then write back all the old config.
422          */
423         pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &cfg);
424 #ifndef CONFIG_MTD_PMC551_BUGFIX
425         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, ~0);
426         pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &size);
427         size = (size & PCI_BASE_ADDRESS_MEM_MASK);
428         size &= ~(size - 1);
429         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, cfg);
430 #else
431         /*
432          * Get the size of the memory by reading all the DRAM size values
433          * and adding them up.
434          *
435          * KLUDGE ALERT: the boards we are using have invalid column and
436          * row mux values.  We fix them here, but this will break other
437          * memory configurations.
438          */
439         pci_read_config_dword(dev, PMC551_DRAM_BLK0, &dram_data);
440         size = PMC551_DRAM_BLK_GET_SIZE(dram_data);
441         dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
442         dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
443         pci_write_config_dword(dev, PMC551_DRAM_BLK0, dram_data);
444
445         pci_read_config_dword(dev, PMC551_DRAM_BLK1, &dram_data);
446         size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
447         dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
448         dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
449         pci_write_config_dword(dev, PMC551_DRAM_BLK1, dram_data);
450
451         pci_read_config_dword(dev, PMC551_DRAM_BLK2, &dram_data);
452         size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
453         dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
454         dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
455         pci_write_config_dword(dev, PMC551_DRAM_BLK2, dram_data);
456
457         pci_read_config_dword(dev, PMC551_DRAM_BLK3, &dram_data);
458         size += PMC551_DRAM_BLK_GET_SIZE(dram_data);
459         dram_data = PMC551_DRAM_BLK_SET_COL_MUX(dram_data, 0x5);
460         dram_data = PMC551_DRAM_BLK_SET_ROW_MUX(dram_data, 0x9);
461         pci_write_config_dword(dev, PMC551_DRAM_BLK3, dram_data);
462
463         /*
464          * Oops .. something went wrong
465          */
466         if ((size &= PCI_BASE_ADDRESS_MEM_MASK) == 0) {
467                 return -ENODEV;
468         }
469 #endif                          /* CONFIG_MTD_PMC551_BUGFIX */
470
471         if ((cfg & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
472                 return -ENODEV;
473         }
474
475         /*
476          * Precharge Dram
477          */
478         pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0400);
479         pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x00bf);
480
481         /*
482          * Wait until command has gone through
483          * FIXME: register spinning issue
484          */
485         do {
486                 pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
487                 if (counter++ > 100)
488                         break;
489         } while ((PCI_COMMAND_IO) & cmd);
490
491         /*
492          * Turn on auto refresh
493          * The loop is taken directly from Ramix's example code.  I assume that
494          * this must be held high for some duration of time, but I can find no
495          * documentation refrencing the reasons why.
496          */
497         for (i = 1; i <= 8; i++) {
498                 pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0df);
499
500                 /*
501                  * Make certain command has gone through
502                  * FIXME: register spinning issue
503                  */
504                 counter = 0;
505                 do {
506                         pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
507                         if (counter++ > 100)
508                                 break;
509                 } while ((PCI_COMMAND_IO) & cmd);
510         }
511
512         pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0020);
513         pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0ff);
514
515         /*
516          * Wait until command completes
517          * FIXME: register spinning issue
518          */
519         counter = 0;
520         do {
521                 pci_read_config_word(dev, PMC551_SDRAM_CMD, &cmd);
522                 if (counter++ > 100)
523                         break;
524         } while ((PCI_COMMAND_IO) & cmd);
525
526         pci_read_config_dword(dev, PMC551_DRAM_CFG, &dcmd);
527         dcmd |= 0x02000000;
528         pci_write_config_dword(dev, PMC551_DRAM_CFG, dcmd);
529
530         /*
531          * Check to make certain fast back-to-back, if not
532          * then set it so
533          */
534         pci_read_config_word(dev, PCI_STATUS, &cmd);
535         if ((cmd & PCI_COMMAND_FAST_BACK) == 0) {
536                 cmd |= PCI_COMMAND_FAST_BACK;
537                 pci_write_config_word(dev, PCI_STATUS, cmd);
538         }
539
540         /*
541          * Check to make certain the DEVSEL is set correctly, this device
542          * has a tendency to assert DEVSEL and TRDY when a write is performed
543          * to the memory when memory is read-only
544          */
545         if ((cmd & PCI_STATUS_DEVSEL_MASK) != 0x0) {
546                 cmd &= ~PCI_STATUS_DEVSEL_MASK;
547                 pci_write_config_word(dev, PCI_STATUS, cmd);
548         }
549         /*
550          * Set to be prefetchable and put everything back based on old cfg.
551          * it's possible that the reset of the V370PDC nuked the original
552          * setup
553          */
554         /*
555            cfg |= PCI_BASE_ADDRESS_MEM_PREFETCH;
556            pci_write_config_dword( dev, PCI_BASE_ADDRESS_0, cfg );
557          */
558
559         /*
560          * Turn PCI memory and I/O bus access back on
561          */
562         pci_write_config_word(dev, PCI_COMMAND,
563                               PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
564 #ifdef CONFIG_MTD_PMC551_DEBUG
565         /*
566          * Some screen fun
567          */
568         printk(KERN_DEBUG "pmc551: %d%sB (0x%x) of %sprefetchable memory at "
569                 "0x%llx\n", (size < 1024) ? size : (size < 1048576) ?
570                 size >> 10 : size >> 20,
571                 (size < 1024) ? "" : (size < 1048576) ? "Ki" : "Mi", size,
572                 ((dcmd & (0x1 << 3)) == 0) ? "non-" : "",
573                 (unsigned long long)pci_resource_start(dev, 0));
574
575         /*
576          * Check to see the state of the memory
577          */
578         pci_read_config_dword(dev, PMC551_DRAM_BLK0, &dcmd);
579         printk(KERN_DEBUG "pmc551: DRAM_BLK0 Flags: %s,%s\n"
580                 "pmc551: DRAM_BLK0 Size: %d at %d\n"
581                 "pmc551: DRAM_BLK0 Row MUX: %d, Col MUX: %d\n",
582                 (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
583                 (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
584                 PMC551_DRAM_BLK_GET_SIZE(dcmd),
585                 ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
586                 ((dcmd >> 9) & 0xF));
587
588         pci_read_config_dword(dev, PMC551_DRAM_BLK1, &dcmd);
589         printk(KERN_DEBUG "pmc551: DRAM_BLK1 Flags: %s,%s\n"
590                 "pmc551: DRAM_BLK1 Size: %d at %d\n"
591                 "pmc551: DRAM_BLK1 Row MUX: %d, Col MUX: %d\n",
592                 (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
593                 (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
594                 PMC551_DRAM_BLK_GET_SIZE(dcmd),
595                 ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
596                 ((dcmd >> 9) & 0xF));
597
598         pci_read_config_dword(dev, PMC551_DRAM_BLK2, &dcmd);
599         printk(KERN_DEBUG "pmc551: DRAM_BLK2 Flags: %s,%s\n"
600                 "pmc551: DRAM_BLK2 Size: %d at %d\n"
601                 "pmc551: DRAM_BLK2 Row MUX: %d, Col MUX: %d\n",
602                 (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
603                 (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
604                 PMC551_DRAM_BLK_GET_SIZE(dcmd),
605                 ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
606                 ((dcmd >> 9) & 0xF));
607
608         pci_read_config_dword(dev, PMC551_DRAM_BLK3, &dcmd);
609         printk(KERN_DEBUG "pmc551: DRAM_BLK3 Flags: %s,%s\n"
610                 "pmc551: DRAM_BLK3 Size: %d at %d\n"
611                 "pmc551: DRAM_BLK3 Row MUX: %d, Col MUX: %d\n",
612                 (((0x1 << 1) & dcmd) == 0) ? "RW" : "RO",
613                 (((0x1 << 0) & dcmd) == 0) ? "Off" : "On",
614                 PMC551_DRAM_BLK_GET_SIZE(dcmd),
615                 ((dcmd >> 20) & 0x7FF), ((dcmd >> 13) & 0x7),
616                 ((dcmd >> 9) & 0xF));
617
618         pci_read_config_word(dev, PCI_COMMAND, &cmd);
619         printk(KERN_DEBUG "pmc551: Memory Access %s\n",
620                 (((0x1 << 1) & cmd) == 0) ? "off" : "on");
621         printk(KERN_DEBUG "pmc551: I/O Access %s\n",
622                 (((0x1 << 0) & cmd) == 0) ? "off" : "on");
623
624         pci_read_config_word(dev, PCI_STATUS, &cmd);
625         printk(KERN_DEBUG "pmc551: Devsel %s\n",
626                 ((PCI_STATUS_DEVSEL_MASK & cmd) == 0x000) ? "Fast" :
627                 ((PCI_STATUS_DEVSEL_MASK & cmd) == 0x200) ? "Medium" :
628                 ((PCI_STATUS_DEVSEL_MASK & cmd) == 0x400) ? "Slow" : "Invalid");
629
630         printk(KERN_DEBUG "pmc551: %sFast Back-to-Back\n",
631                 ((PCI_COMMAND_FAST_BACK & cmd) == 0) ? "Not " : "");
632
633         pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
634         printk(KERN_DEBUG "pmc551: EEPROM is under %s control\n"
635                 "pmc551: System Control Register is %slocked to PCI access\n"
636                 "pmc551: System Control Register is %slocked to EEPROM access\n",
637                 (bcmd & 0x1) ? "software" : "hardware",
638                 (bcmd & 0x20) ? "" : "un", (bcmd & 0x40) ? "" : "un");
639 #endif
640         return size;
641 }
642
643 /*
644  * Kernel version specific module stuffages
645  */
646
647 MODULE_LICENSE("GPL");
648 MODULE_AUTHOR("Mark Ferrell <mferrell@mvista.com>");
649 MODULE_DESCRIPTION(PMC551_VERSION);
650
651 /*
652  * Stuff these outside the ifdef so as to not bust compiled in driver support
653  */
654 static int msize = 0;
655 static int asize = 0;
656
657 module_param(msize, int, 0);
658 MODULE_PARM_DESC(msize, "memory size in MiB [1 - 1024]");
659 module_param(asize, int, 0);
660 MODULE_PARM_DESC(asize, "aperture size, must be <= memsize [1-1024]");
661
662 /*
663  * PMC551 Card Initialization
664  */
665 static int __init init_pmc551(void)
666 {
667         struct pci_dev *PCI_Device = NULL;
668         struct mypriv *priv;
669         int found = 0;
670         struct mtd_info *mtd;
671         u32 length = 0;
672
673         if (msize) {
674                 msize = (1 << (ffs(msize) - 1)) << 20;
675                 if (msize > (1 << 30)) {
676                         printk(KERN_NOTICE "pmc551: Invalid memory size [%d]\n",
677                                 msize);
678                         return -EINVAL;
679                 }
680         }
681
682         if (asize) {
683                 asize = (1 << (ffs(asize) - 1)) << 20;
684                 if (asize > (1 << 30)) {
685                         printk(KERN_NOTICE "pmc551: Invalid aperture size "
686                                 "[%d]\n", asize);
687                         return -EINVAL;
688                 }
689         }
690
691         printk(KERN_INFO PMC551_VERSION);
692
693         /*
694          * PCU-bus chipset probe.
695          */
696         for (;;) {
697
698                 if ((PCI_Device = pci_get_device(PCI_VENDOR_ID_V3_SEMI,
699                                                   PCI_DEVICE_ID_V3_SEMI_V370PDC,
700                                                   PCI_Device)) == NULL) {
701                         break;
702                 }
703
704                 printk(KERN_NOTICE "pmc551: Found PCI V370PDC at 0x%llx\n",
705                         (unsigned long long)pci_resource_start(PCI_Device, 0));
706
707                 /*
708                  * The PMC551 device acts VERY weird if you don't init it
709                  * first.  i.e. it will not correctly report devsel.  If for
710                  * some reason the sdram is in a wrote-protected state the
711                  * device will DEVSEL when it is written to causing problems
712                  * with the oldproc.c driver in
713                  * some kernels (2.2.*)
714                  */
715                 if ((length = fixup_pmc551(PCI_Device)) <= 0) {
716                         printk(KERN_NOTICE "pmc551: Cannot init SDRAM\n");
717                         break;
718                 }
719
720                 /*
721                  * This is needed until the driver is capable of reading the
722                  * onboard I2C SROM to discover the "real" memory size.
723                  */
724                 if (msize) {
725                         length = msize;
726                         printk(KERN_NOTICE "pmc551: Using specified memory "
727                                 "size 0x%x\n", length);
728                 } else {
729                         msize = length;
730                 }
731
732                 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
733                 if (!mtd) {
734                         printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
735                                 "device.\n");
736                         break;
737                 }
738
739                 priv = kzalloc(sizeof(struct mypriv), GFP_KERNEL);
740                 if (!priv) {
741                         printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
742                                 "device.\n");
743                         kfree(mtd);
744                         break;
745                 }
746                 mtd->priv = priv;
747                 priv->dev = PCI_Device;
748
749                 if (asize > length) {
750                         printk(KERN_NOTICE "pmc551: reducing aperture size to "
751                                 "fit %dM\n", length >> 20);
752                         priv->asize = asize = length;
753                 } else if (asize == 0 || asize == length) {
754                         printk(KERN_NOTICE "pmc551: Using existing aperture "
755                                 "size %dM\n", length >> 20);
756                         priv->asize = asize = length;
757                 } else {
758                         printk(KERN_NOTICE "pmc551: Using specified aperture "
759                                 "size %dM\n", asize >> 20);
760                         priv->asize = asize;
761                 }
762                 priv->start = pci_iomap(PCI_Device, 0, priv->asize);
763
764                 if (!priv->start) {
765                         printk(KERN_NOTICE "pmc551: Unable to map IO space\n");
766                         kfree(mtd->priv);
767                         kfree(mtd);
768                         break;
769                 }
770 #ifdef CONFIG_MTD_PMC551_DEBUG
771                 printk(KERN_DEBUG "pmc551: setting aperture to %d\n",
772                         ffs(priv->asize >> 20) - 1);
773 #endif
774
775                 priv->base_map0 = (PMC551_PCI_MEM_MAP_REG_EN
776                                    | PMC551_PCI_MEM_MAP_ENABLE
777                                    | (ffs(priv->asize >> 20) - 1) << 4);
778                 priv->curr_map0 = priv->base_map0;
779                 pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
780                                         priv->curr_map0);
781
782 #ifdef CONFIG_MTD_PMC551_DEBUG
783                 printk(KERN_DEBUG "pmc551: aperture set to %d\n",
784                         (priv->base_map0 & 0xF0) >> 4);
785 #endif
786
787                 mtd->size = msize;
788                 mtd->flags = MTD_CAP_RAM;
789                 mtd->erase = pmc551_erase;
790                 mtd->read = pmc551_read;
791                 mtd->write = pmc551_write;
792                 mtd->point = pmc551_point;
793                 mtd->unpoint = pmc551_unpoint;
794                 mtd->type = MTD_RAM;
795                 mtd->name = "PMC551 RAM board";
796                 mtd->erasesize = 0x10000;
797                 mtd->writesize = 1;
798                 mtd->owner = THIS_MODULE;
799
800                 if (mtd_device_register(mtd, NULL, 0)) {
801                         printk(KERN_NOTICE "pmc551: Failed to register new device\n");
802                         pci_iounmap(PCI_Device, priv->start);
803                         kfree(mtd->priv);
804                         kfree(mtd);
805                         break;
806                 }
807
808                 /* Keep a reference as the mtd_device_register worked */
809                 pci_dev_get(PCI_Device);
810
811                 printk(KERN_NOTICE "Registered pmc551 memory device.\n");
812                 printk(KERN_NOTICE "Mapped %dMiB of memory from 0x%p to 0x%p\n",
813                         priv->asize >> 20,
814                         priv->start, priv->start + priv->asize);
815                 printk(KERN_NOTICE "Total memory is %d%sB\n",
816                         (length < 1024) ? length :
817                         (length < 1048576) ? length >> 10 : length >> 20,
818                         (length < 1024) ? "" : (length < 1048576) ? "Ki" : "Mi");
819                 priv->nextpmc551 = pmc551list;
820                 pmc551list = mtd;
821                 found++;
822         }
823
824         /* Exited early, reference left over */
825         if (PCI_Device)
826                 pci_dev_put(PCI_Device);
827
828         if (!pmc551list) {
829                 printk(KERN_NOTICE "pmc551: not detected\n");
830                 return -ENODEV;
831         } else {
832                 printk(KERN_NOTICE "pmc551: %d pmc551 devices loaded\n", found);
833                 return 0;
834         }
835 }
836
837 /*
838  * PMC551 Card Cleanup
839  */
840 static void __exit cleanup_pmc551(void)
841 {
842         int found = 0;
843         struct mtd_info *mtd;
844         struct mypriv *priv;
845
846         while ((mtd = pmc551list)) {
847                 priv = mtd->priv;
848                 pmc551list = priv->nextpmc551;
849
850                 if (priv->start) {
851                         printk(KERN_DEBUG "pmc551: unmapping %dMiB starting at "
852                                 "0x%p\n", priv->asize >> 20, priv->start);
853                         pci_iounmap(priv->dev, priv->start);
854                 }
855                 pci_dev_put(priv->dev);
856
857                 kfree(mtd->priv);
858                 mtd_device_unregister(mtd);
859                 kfree(mtd);
860                 found++;
861         }
862
863         printk(KERN_NOTICE "pmc551: %d pmc551 devices unloaded\n", found);
864 }
865
866 module_init(init_pmc551);
867 module_exit(cleanup_pmc551);