1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/mtd/mtd.h>
35#include <linux/mtd/map.h>
36#include <linux/mtd/partitions.h>
37#include <linux/mtd/cfi.h>
38#include <linux/mtd/flashchip.h>
39
40#define DRV_NAME "vr_nor"
41
42struct vr_nor_mtd {
43 void __iomem *csr_base;
44 struct map_info map;
45 struct mtd_info *info;
46 struct pci_dev *dev;
47};
48
49
50#define EXP_CSR_MBAR 0
51
52#define EXP_WIN_MBAR 1
53
54#define CS0_SIZE 0x04000000
55
56#define CS0_START 0x0
57
58#define EXP_TIMING_CS0 0x00
59#define TIMING_CS_EN (1 << 31)
60#define TIMING_BOOT_ACCEL_DIS (1 << 8)
61#define TIMING_WR_EN (1 << 1)
62#define TIMING_BYTE_EN (1 << 0)
63#define TIMING_MASK 0x3FFF0000
64
65static void vr_nor_destroy_partitions(struct vr_nor_mtd *p)
66{
67 mtd_device_unregister(p->info);
68}
69
70static int vr_nor_init_partitions(struct vr_nor_mtd *p)
71{
72
73
74 return mtd_device_parse_register(p->info, NULL, NULL, NULL, 0);
75}
76
77static void vr_nor_destroy_mtd_setup(struct vr_nor_mtd *p)
78{
79 map_destroy(p->info);
80}
81
82static int vr_nor_mtd_setup(struct vr_nor_mtd *p)
83{
84 static const char * const probe_types[] =
85 { "cfi_probe", "jedec_probe", NULL };
86 const char * const *type;
87
88 for (type = probe_types; !p->info && *type; type++)
89 p->info = do_map_probe(*type, &p->map);
90 if (!p->info)
91 return -ENODEV;
92
93 p->info->dev.parent = &p->dev->dev;
94
95 return 0;
96}
97
98static void vr_nor_destroy_maps(struct vr_nor_mtd *p)
99{
100 unsigned int exp_timing_cs0;
101
102
103 exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
104 exp_timing_cs0 &= ~TIMING_WR_EN;
105 writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
106
107
108 iounmap(p->map.virt);
109
110
111 iounmap(p->csr_base);
112}
113
114
115
116
117
118static int vr_nor_init_maps(struct vr_nor_mtd *p)
119{
120 unsigned long csr_phys, csr_len;
121 unsigned long win_phys, win_len;
122 unsigned int exp_timing_cs0;
123 int err;
124
125 csr_phys = pci_resource_start(p->dev, EXP_CSR_MBAR);
126 csr_len = pci_resource_len(p->dev, EXP_CSR_MBAR);
127 win_phys = pci_resource_start(p->dev, EXP_WIN_MBAR);
128 win_len = pci_resource_len(p->dev, EXP_WIN_MBAR);
129
130 if (!csr_phys || !csr_len || !win_phys || !win_len)
131 return -ENODEV;
132
133 if (win_len < (CS0_START + CS0_SIZE))
134 return -ENXIO;
135
136 p->csr_base = ioremap_nocache(csr_phys, csr_len);
137 if (!p->csr_base)
138 return -ENOMEM;
139
140 exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
141 if (!(exp_timing_cs0 & TIMING_CS_EN)) {
142 dev_warn(&p->dev->dev, "Expansion Bus Chip Select 0 "
143 "is disabled.\n");
144 err = -ENODEV;
145 goto release;
146 }
147 if ((exp_timing_cs0 & TIMING_MASK) == TIMING_MASK) {
148 dev_warn(&p->dev->dev, "Expansion Bus Chip Select 0 "
149 "is configured for maximally slow access times.\n");
150 }
151 p->map.name = DRV_NAME;
152 p->map.bankwidth = (exp_timing_cs0 & TIMING_BYTE_EN) ? 1 : 2;
153 p->map.phys = win_phys + CS0_START;
154 p->map.size = CS0_SIZE;
155 p->map.virt = ioremap_nocache(p->map.phys, p->map.size);
156 if (!p->map.virt) {
157 err = -ENOMEM;
158 goto release;
159 }
160 simple_map_init(&p->map);
161
162
163 exp_timing_cs0 |= TIMING_BOOT_ACCEL_DIS | TIMING_WR_EN;
164 writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
165
166 return 0;
167
168 release:
169 iounmap(p->csr_base);
170 return err;
171}
172
173static const struct pci_device_id vr_nor_pci_ids[] = {
174 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x500D)},
175 {0,}
176};
177
178static void vr_nor_pci_remove(struct pci_dev *dev)
179{
180 struct vr_nor_mtd *p = pci_get_drvdata(dev);
181
182 vr_nor_destroy_partitions(p);
183 vr_nor_destroy_mtd_setup(p);
184 vr_nor_destroy_maps(p);
185 kfree(p);
186 pci_release_regions(dev);
187 pci_disable_device(dev);
188}
189
190static int vr_nor_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
191{
192 struct vr_nor_mtd *p = NULL;
193 unsigned int exp_timing_cs0;
194 int err;
195
196 err = pci_enable_device(dev);
197 if (err)
198 goto out;
199
200 err = pci_request_regions(dev, DRV_NAME);
201 if (err)
202 goto disable_dev;
203
204 p = kzalloc(sizeof(*p), GFP_KERNEL);
205 err = -ENOMEM;
206 if (!p)
207 goto release;
208
209 p->dev = dev;
210
211 err = vr_nor_init_maps(p);
212 if (err)
213 goto release;
214
215 err = vr_nor_mtd_setup(p);
216 if (err)
217 goto destroy_maps;
218
219 err = vr_nor_init_partitions(p);
220 if (err)
221 goto destroy_mtd_setup;
222
223 pci_set_drvdata(dev, p);
224
225 return 0;
226
227 destroy_mtd_setup:
228 map_destroy(p->info);
229
230 destroy_maps:
231
232 exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
233 exp_timing_cs0 &= ~TIMING_WR_EN;
234 writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
235
236
237 iounmap(p->map.virt);
238
239
240 iounmap(p->csr_base);
241
242 release:
243 kfree(p);
244 pci_release_regions(dev);
245
246 disable_dev:
247 pci_disable_device(dev);
248
249 out:
250 return err;
251}
252
253static struct pci_driver vr_nor_pci_driver = {
254 .name = DRV_NAME,
255 .probe = vr_nor_pci_probe,
256 .remove = vr_nor_pci_remove,
257 .id_table = vr_nor_pci_ids,
258};
259
260module_pci_driver(vr_nor_pci_driver);
261
262MODULE_AUTHOR("Andy Lowe");
263MODULE_DESCRIPTION("MTD map driver for NOR flash on Intel Vermilion Range");
264MODULE_LICENSE("GPL");
265MODULE_DEVICE_TABLE(pci, vr_nor_pci_ids);
266