blob: 6269a434f304a69580dd13f24983f8a77cffd784 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * PMC551 PCI Mezzanine Ram Device
3 *
4 * Author:
Jiri Slabycdf0a7d2006-09-19 21:55:06 +02005 * Mark Ferrell <mferrell@mvista.com>
6 * Copyright 1999,2000 Nortel Networks
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * License:
Jiri Slabycdf0a7d2006-09-19 21:55:06 +02009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Description:
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020014 * 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.
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * Notes:
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020030 * Due to what I assume is more buggy SROM, the 64M PMC551 I
David Woodhouse8e0aedc2007-06-29 14:18:22 +010031 * have available claims that all 4 of its DRAM banks have 64MiB
32 * of ram configured (making a grand total of 256MiB onboard).
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020033 * 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
Frederik Schwarzer025dfda2008-10-16 19:02:37 +020037 * unloading of the module with an aperture size smaller than
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020038 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * Credits:
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020049 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020053 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020063 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
David Woodhouse8e0aedc2007-06-29 14:18:22 +010071 * Bugs/FIXMEs:
Jiri Slabycdf0a7d2006-09-19 21:55:06 +020072 * * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/kernel.h>
84#include <linux/module.h>
85#include <asm/uaccess.h>
86#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#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 <asm/system.h>
97#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/mtd/mtd.h>
Artem Bityutskiyc3faac42012-02-06 13:44:27 +020099
100#define PMC551_VERSION \
101 "Ramix PMC551 PCI Mezzanine Ram Driver. (C) 1999,2000 Nortel Networks.\n"
102
103#define PCI_VENDOR_ID_V3_SEMI 0x11b0
104#define PCI_DEVICE_ID_V3_SEMI_V370PDC 0x0200
105
106#define PMC551_PCI_MEM_MAP0 0x50
107#define PMC551_PCI_MEM_MAP1 0x54
108#define PMC551_PCI_MEM_MAP_MAP_ADDR_MASK 0x3ff00000
109#define PMC551_PCI_MEM_MAP_APERTURE_MASK 0x000000f0
110#define PMC551_PCI_MEM_MAP_REG_EN 0x00000002
111#define PMC551_PCI_MEM_MAP_ENABLE 0x00000001
112
113#define PMC551_SDRAM_MA 0x60
114#define PMC551_SDRAM_CMD 0x62
115#define PMC551_DRAM_CFG 0x64
116#define PMC551_SYS_CTRL_REG 0x78
117
118#define PMC551_DRAM_BLK0 0x68
119#define PMC551_DRAM_BLK1 0x6c
120#define PMC551_DRAM_BLK2 0x70
121#define PMC551_DRAM_BLK3 0x74
122#define PMC551_DRAM_BLK_GET_SIZE(x) (524288 << ((x >> 4) & 0x0f))
123#define PMC551_DRAM_BLK_SET_COL_MUX(x, v) (((x) & ~0x00007000) | (((v) & 0x7) << 12))
124#define PMC551_DRAM_BLK_SET_ROW_MUX(x, v) (((x) & ~0x00000f00) | (((v) & 0xf) << 8))
125
126struct mypriv {
127 struct pci_dev *dev;
128 u_char *start;
129 u32 base_map0;
130 u32 curr_map0;
131 u32 asize;
132 struct mtd_info *nextpmc551;
133};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static struct mtd_info *pmc551list;
136
Artem Bityutskiyc3faac42012-02-06 13:44:27 +0200137static int pmc551_point(struct mtd_info *mtd, loff_t from, size_t len,
138 size_t *retlen, void **virt, resource_size_t *phys);
139
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200140static int pmc551_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200142 struct mypriv *priv = mtd->priv;
143 u32 soff_hi, soff_lo; /* start address offset hi/lo */
144 u32 eoff_hi, eoff_lo; /* end address offset hi/lo */
145 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 u_char *ptr;
147 size_t retlen;
148
149#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200150 printk(KERN_DEBUG "pmc551_erase(pos:%ld, len:%ld)\n", (long)instr->addr,
151 (long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif
153
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200154 end = instr->addr + instr->len - 1;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200155 eoff_hi = end & ~(priv->asize - 1);
156 soff_hi = instr->addr & ~(priv->asize - 1);
157 eoff_lo = end & (priv->asize - 1);
158 soff_lo = instr->addr & (priv->asize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Jared Hulberta98889f2008-04-29 23:26:49 -0700160 pmc551_point(mtd, instr->addr, instr->len, &retlen,
161 (void **)&ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200163 if (soff_hi == eoff_hi || mtd->size == priv->asize) {
164 /* The whole thing fits within one access, so just one shot
165 will do it. */
166 memset(ptr, 0xff, instr->len);
167 } else {
168 /* We have to do multiple writes to get all the data
169 written. */
170 while (soff_hi != eoff_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200172 printk(KERN_DEBUG "pmc551_erase() soff_hi: %ld, "
173 "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200175 memset(ptr, 0xff, priv->asize);
176 if (soff_hi + priv->asize >= mtd->size) {
177 goto out;
178 }
179 soff_hi += priv->asize;
180 pmc551_point(mtd, (priv->base_map0 | soff_hi),
Jared Hulberta98889f2008-04-29 23:26:49 -0700181 priv->asize, &retlen,
182 (void **)&ptr, NULL);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200183 }
184 memset(ptr, 0xff, eoff_lo);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200187 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 instr->state = MTD_ERASE_DONE;
189#ifdef CONFIG_MTD_PMC551_DEBUG
190 printk(KERN_DEBUG "pmc551_erase() done\n");
191#endif
192
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200193 mtd_erase_callback(instr);
194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200197static int pmc551_point(struct mtd_info *mtd, loff_t from, size_t len,
Jared Hulberta98889f2008-04-29 23:26:49 -0700198 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200200 struct mypriv *priv = mtd->priv;
201 u32 soff_hi;
202 u32 soff_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204#ifdef CONFIG_MTD_PMC551_DEBUG
205 printk(KERN_DEBUG "pmc551_point(%ld, %ld)\n", (long)from, (long)len);
206#endif
207
Jared Hulberta98889f2008-04-29 23:26:49 -0700208 /* can we return a physical address with this driver? */
209 if (phys)
210 return -EINVAL;
211
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200212 soff_hi = from & ~(priv->asize - 1);
213 soff_lo = from & (priv->asize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Cheap hack optimization */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200216 if (priv->curr_map0 != from) {
217 pci_write_config_dword(priv->dev, PMC551_PCI_MEM_MAP0,
218 (priv->base_map0 | soff_hi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 priv->curr_map0 = soff_hi;
220 }
221
Jared Hulberta98889f2008-04-29 23:26:49 -0700222 *virt = priv->start + soff_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *retlen = len;
224 return 0;
225}
226
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200227static int pmc551_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229#ifdef CONFIG_MTD_PMC551_DEBUG
230 printk(KERN_DEBUG "pmc551_unpoint()\n");
231#endif
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200235static int pmc551_read(struct mtd_info *mtd, loff_t from, size_t len,
236 size_t * retlen, u_char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200238 struct mypriv *priv = mtd->priv;
239 u32 soff_hi, soff_lo; /* start address offset hi/lo */
240 u32 eoff_hi, eoff_lo; /* end address offset hi/lo */
241 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 u_char *ptr;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200243 u_char *copyto = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200246 printk(KERN_DEBUG "pmc551_read(pos:%ld, len:%ld) asize: %ld\n",
247 (long)from, (long)len, (long)priv->asize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#endif
249
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200250 end = from + len - 1;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200251 soff_hi = from & ~(priv->asize - 1);
252 eoff_hi = end & ~(priv->asize - 1);
253 soff_lo = from & (priv->asize - 1);
254 eoff_lo = end & (priv->asize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jared Hulberta98889f2008-04-29 23:26:49 -0700256 pmc551_point(mtd, from, len, retlen, (void **)&ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200258 if (soff_hi == eoff_hi) {
259 /* The whole thing fits within one access, so just one shot
260 will do it. */
261 memcpy(copyto, ptr, len);
262 copyto += len;
263 } else {
264 /* We have to do multiple writes to get all the data
265 written. */
266 while (soff_hi != eoff_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200268 printk(KERN_DEBUG "pmc551_read() soff_hi: %ld, "
269 "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200271 memcpy(copyto, ptr, priv->asize);
272 copyto += priv->asize;
273 if (soff_hi + priv->asize >= mtd->size) {
274 goto out;
275 }
276 soff_hi += priv->asize;
Jared Hulberta98889f2008-04-29 23:26:49 -0700277 pmc551_point(mtd, soff_hi, priv->asize, retlen,
278 (void **)&ptr, NULL);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200279 }
280 memcpy(copyto, ptr, eoff_lo);
281 copyto += eoff_lo;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200284 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#ifdef CONFIG_MTD_PMC551_DEBUG
286 printk(KERN_DEBUG "pmc551_read() done\n");
287#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200288 *retlen = copyto - buf;
289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200292static int pmc551_write(struct mtd_info *mtd, loff_t to, size_t len,
293 size_t * retlen, const u_char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200295 struct mypriv *priv = mtd->priv;
296 u32 soff_hi, soff_lo; /* start address offset hi/lo */
297 u32 eoff_hi, eoff_lo; /* end address offset hi/lo */
298 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 u_char *ptr;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200300 const u_char *copyfrom = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200303 printk(KERN_DEBUG "pmc551_write(pos:%ld, len:%ld) asize:%ld\n",
304 (long)to, (long)len, (long)priv->asize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
306
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200307 end = to + len - 1;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200308 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jared Hulberta98889f2008-04-29 23:26:49 -0700313 pmc551_point(mtd, to, len, retlen, (void **)&ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200315 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200325 printk(KERN_DEBUG "pmc551_write() soff_hi: %ld, "
326 "eoff_hi: %ld\n", (long)soff_hi, (long)eoff_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200328 memcpy(ptr, copyfrom, priv->asize);
329 copyfrom += priv->asize;
330 if (soff_hi >= mtd->size) {
331 goto out;
332 }
333 soff_hi += priv->asize;
Jared Hulberta98889f2008-04-29 23:26:49 -0700334 pmc551_point(mtd, soff_hi, priv->asize, retlen,
335 (void **)&ptr, NULL);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200336 }
337 memcpy(ptr, copyfrom, eoff_lo);
338 copyfrom += eoff_lo;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200341 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#ifdef CONFIG_MTD_PMC551_DEBUG
343 printk(KERN_DEBUG "pmc551_write() done\n");
344#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200345 *retlen = copyfrom - buf;
346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/*
350 * Fixup routines for the V370PDC
351 * PCI device ID 0x020011b0
352 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300353 * This function basically kick starts the DRAM oboard the card and gets it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * 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 */
Xi Wang2ff5e152012-01-09 16:58:25 -0500361static int fixup_pmc551(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363#ifdef CONFIG_MTD_PMC551_BUGFIX
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200364 u32 dram_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200366 u32 size, dcmd, cfg, dtmp;
367 u16 cmd, tmp, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 u8 bcmd, counter;
369
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200370 /* Sanity Check */
371 if (!dev) {
372 return -ENODEV;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * Attempt to reset the card
377 * FIXME: Stop Spinning registers
378 */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200379 counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* unlock registers */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200381 pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, 0xA5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* read in old data */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200383 pci_read_config_byte(dev, PMC551_SYS_CTRL_REG, &bcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* bang the reset line up and down for a few */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200385 for (i = 0; i < 10; i++) {
386 counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 bcmd &= ~0x80;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200388 while (counter++ < 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
390 }
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200391 counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 bcmd |= 0x80;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200393 while (counter++ < 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
395 }
396 }
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200397 bcmd |= (0x40 | 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 pci_write_config_byte(dev, PMC551_SYS_CTRL_REG, bcmd);
399
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200400 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * Take care and turn off the memory on the device while we
402 * tweak the configurations
403 */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200404 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /*
409 * Disable existing aperture before probing memory size
410 */
411 pci_read_config_dword(dev, PMC551_PCI_MEM_MAP0, &dcmd);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200412 dtmp = (dcmd | PMC551_PCI_MEM_MAP_ENABLE | PMC551_PCI_MEM_MAP_REG_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 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
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000418 * previously assigned to the card by the BIOS or some previous
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * 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 */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200423 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#ifndef CONFIG_MTD_PMC551_BUGFIX
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200425 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#else
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200431 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200445 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200451 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200457 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200463 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200471 if ((cfg & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
472 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200475 /*
476 * Precharge Dram
477 */
478 pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0400);
479 pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x00bf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200481 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200491 /*
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000492 * Turn on auto refresh
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * 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.
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200496 */
497 for (i = 1; i <= 8; i++) {
498 pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0df);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200500 /*
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200512 pci_write_config_word(dev, PMC551_SDRAM_MA, 0x0020);
513 pci_write_config_word(dev, PMC551_SDRAM_CMD, 0x0ff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200515 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200526 pci_read_config_dword(dev, PMC551_DRAM_CFG, &dcmd);
527 dcmd |= 0x02000000;
528 pci_write_config_dword(dev, PMC551_DRAM_CFG, dcmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200530 /*
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200540 /*
541 * Check to make certain the DEVSEL is set correctly, this device
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300542 * has a tendency to assert DEVSEL and TRDY when a write is performed
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200543 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * it's possible that the reset of the V370PDC nuked the original
552 * setup
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200555 cfg |= PCI_BASE_ADDRESS_MEM_PREFETCH;
556 pci_write_config_dword( dev, PCI_BASE_ADDRESS_0, cfg );
557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200559 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200565 /*
566 * Some screen fun
567 */
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100568 printk(KERN_DEBUG "pmc551: %d%sB (0x%x) of %sprefetchable memory at "
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200569 "0x%llx\n", (size < 1024) ? size : (size < 1048576) ?
570 size >> 10 : size >> 20,
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100571 (size < 1024) ? "" : (size < 1048576) ? "Ki" : "Mi", size,
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200572 ((dcmd & (0x1 << 3)) == 0) ? "non-" : "",
Jiri Slaby98aacdf2006-09-19 21:55:28 +0200573 (unsigned long long)pci_resource_start(dev, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200575 /*
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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200588 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200598 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200608 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200618 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200624 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200630 printk(KERN_DEBUG "pmc551: %sFast Back-to-Back\n",
631 ((PCI_COMMAND_FAST_BACK & cmd) == 0) ? "Not " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200633 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#endif
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200640 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/*
644 * Kernel version specific module stuffages
645 */
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647MODULE_LICENSE("GPL");
648MODULE_AUTHOR("Mark Ferrell <mferrell@mvista.com>");
649MODULE_DESCRIPTION(PMC551_VERSION);
650
651/*
652 * Stuff these outside the ifdef so as to not bust compiled in driver support
653 */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200654static int msize = 0;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200655static int asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657module_param(msize, int, 0);
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100658MODULE_PARM_DESC(msize, "memory size in MiB [1 - 1024]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659module_param(asize, int, 0);
660MODULE_PARM_DESC(asize, "aperture size, must be <= memsize [1-1024]");
661
662/*
663 * PMC551 Card Initialization
664 */
665static int __init init_pmc551(void)
666{
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200667 struct pci_dev *PCI_Device = NULL;
668 struct mypriv *priv;
Ben Hutchings24c15492010-01-29 20:58:23 +0000669 int found = 0;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200670 struct mtd_info *mtd;
Xi Wang2ff5e152012-01-09 16:58:25 -0500671 int length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200673 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
679 }
680 }
681
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200682 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EINVAL;
688 }
689 }
690
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200691 printk(KERN_INFO PMC551_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200693 /*
694 * PCU-bus chipset probe.
695 */
Ben Hutchings24c15492010-01-29 20:58:23 +0000696 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200698 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200704 printk(KERN_NOTICE "pmc551: Found PCI V370PDC at 0x%llx\n",
Jiri Slaby98aacdf2006-09-19 21:55:28 +0200705 (unsigned long long)pci_resource_start(PCI_Device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200707 /*
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * This is needed until the driver is capable of reading the
722 * onboard I2C SROM to discover the "real" memory size.
723 */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200724 if (msize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 length = msize;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200726 printk(KERN_NOTICE "pmc551: Using specified memory "
727 "size 0x%x\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } else {
729 msize = length;
730 }
731
Jiri Slaby7fefb922006-09-19 21:55:18 +0200732 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200733 if (!mtd) {
734 printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
735 "device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 }
738
Jiri Slaby7fefb922006-09-19 21:55:18 +0200739 priv = kzalloc(sizeof(struct mypriv), GFP_KERNEL);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200740 if (!priv) {
741 printk(KERN_NOTICE "pmc551: Cannot allocate new MTD "
742 "device.\n");
743 kfree(mtd);
744 break;
745 }
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200746 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 }
Jiri Slaby98aacdf2006-09-19 21:55:28 +0200762 priv->start = pci_iomap(PCI_Device, 0, priv->asize);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200763
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200771 printk(KERN_DEBUG "pmc551: setting aperture to %d\n",
772 ffs(priv->asize >> 20) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#endif
774
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200775 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782#ifdef CONFIG_MTD_PMC551_DEBUG
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200783 printk(KERN_DEBUG "pmc551: aperture set to %d\n",
784 (priv->base_map0 & 0xF0) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#endif
786
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200787 mtd->size = msize;
788 mtd->flags = MTD_CAP_RAM;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200789 mtd->_erase = pmc551_erase;
790 mtd->_read = pmc551_read;
791 mtd->_write = pmc551_write;
792 mtd->_point = pmc551_point;
793 mtd->_unpoint = pmc551_unpoint;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200794 mtd->type = MTD_RAM;
795 mtd->name = "PMC551 RAM board";
796 mtd->erasesize = 0x10000;
797 mtd->writesize = 1;
798 mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100800 if (mtd_device_register(mtd, NULL, 0)) {
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100801 printk(KERN_NOTICE "pmc551: Failed to register new device\n");
Jiri Slaby98aacdf2006-09-19 21:55:28 +0200802 pci_iounmap(PCI_Device, priv->start);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200803 kfree(mtd->priv);
804 kfree(mtd);
805 break;
806 }
Alan Coxdd8e9ed2006-09-22 10:19:20 +0100807
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100808 /* Keep a reference as the mtd_device_register worked */
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200809 pci_dev_get(PCI_Device);
Alan Coxdd8e9ed2006-09-22 10:19:20 +0100810
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200811 printk(KERN_NOTICE "Registered pmc551 memory device.\n");
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100812 printk(KERN_NOTICE "Mapped %dMiB of memory from 0x%p to 0x%p\n",
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200813 priv->asize >> 20,
814 priv->start, priv->start + priv->asize);
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100815 printk(KERN_NOTICE "Total memory is %d%sB\n",
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200816 (length < 1024) ? length :
817 (length < 1048576) ? length >> 10 : length >> 20,
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100818 (length < 1024) ? "" : (length < 1048576) ? "Ki" : "Mi");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 priv->nextpmc551 = pmc551list;
820 pmc551list = mtd;
821 found++;
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200824 /* Exited early, reference left over */
825 if (PCI_Device)
826 pci_dev_put(PCI_Device);
Alan Coxdd8e9ed2006-09-22 10:19:20 +0100827
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200828 if (!pmc551list) {
829 printk(KERN_NOTICE "pmc551: not detected\n");
830 return -ENODEV;
831 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 printk(KERN_NOTICE "pmc551: %d pmc551 devices loaded\n", found);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835}
836
837/*
838 * PMC551 Card Cleanup
839 */
840static void __exit cleanup_pmc551(void)
841{
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200842 int found = 0;
843 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct mypriv *priv;
845
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200846 while ((mtd = pmc551list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 priv = mtd->priv;
848 pmc551list = priv->nextpmc551;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000849
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200850 if (priv->start) {
David Woodhouse8e0aedc2007-06-29 14:18:22 +0100851 printk(KERN_DEBUG "pmc551: unmapping %dMiB starting at "
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200852 "0x%p\n", priv->asize >> 20, priv->start);
Jiri Slaby98aacdf2006-09-19 21:55:28 +0200853 pci_iounmap(priv->dev, priv->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Alan Coxdd8e9ed2006-09-22 10:19:20 +0100855 pci_dev_put(priv->dev);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000856
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200857 kfree(mtd->priv);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100858 mtd_device_unregister(mtd);
Jiri Slabycdf0a7d2006-09-19 21:55:06 +0200859 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 found++;
861 }
862
863 printk(KERN_NOTICE "pmc551: %d pmc551 devices unloaded\n", found);
864}
865
866module_init(init_pmc551);
867module_exit(cleanup_pmc551);