1 /* 2 * linux/drivers/mtd/maps/pci.c 3 * 4 * Copyright (C) 2001 Russell King, All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * Generic PCI memory map driver. We support the following boards: 11 * - Intel IQ80310 ATU. 12 * - Intel EBSA285 (blank rom programming mode). Tested working 27/09/2001 13 */ 14 #include <linux/module.h> 15 #include <linux/kernel.h> 16 #include <linux/pci.h> 17 #include <linux/init.h> 18 #include <linux/slab.h> 19 20 #include <linux/mtd/mtd.h> 21 #include <linux/mtd/map.h> 22 #include <linux/mtd/partitions.h> 23 24 struct map_pci_info; 25 26 struct mtd_pci_info { 27 int (*init)(struct pci_dev *dev, struct map_pci_info *map); 28 void (*exit)(struct pci_dev *dev, struct map_pci_info *map); 29 unsigned long (*translate)(struct map_pci_info *map, unsigned long ofs); 30 const char *map_name; 31 }; 32 33 struct map_pci_info { 34 struct map_info map; 35 void __iomem *base; 36 void (*exit)(struct pci_dev *dev, struct map_pci_info *map); 37 unsigned long (*translate)(struct map_pci_info *map, unsigned long ofs); 38 struct pci_dev *dev; 39 }; 40 41 static map_word mtd_pci_read8(struct map_info *_map, unsigned long ofs) 42 { 43 struct map_pci_info *map = (struct map_pci_info *)_map; 44 map_word val; 45 val.x[0]= readb(map->base + map->translate(map, ofs)); 46 // printk("read8 : %08lx => %02x\n", ofs, val.x[0]); 47 return val; 48 } 49 50 #if 0 51 static map_word mtd_pci_read16(struct map_info *_map, unsigned long ofs) 52 { 53 struct map_pci_info *map = (struct map_pci_info *)_map; 54 map_word val; 55 val.x[0] = readw(map->base + map->translate(map, ofs)); 56 // printk("read16: %08lx => %04x\n", ofs, val.x[0]); 57 return val; 58 } 59 #endif 60 static map_word mtd_pci_read32(struct map_info *_map, unsigned long ofs) 61 { 62 struct map_pci_info *map = (struct map_pci_info *)_map; 63 map_word val; 64 val.x[0] = readl(map->base + map->translate(map, ofs)); 65 // printk("read32: %08lx => %08x\n", ofs, val.x[0]); 66 return val; 67 } 68 69 static void mtd_pci_copyfrom(struct map_info *_map, void *to, unsigned long from, ssize_t len) 70 { 71 struct map_pci_info *map = (struct map_pci_info *)_map; 72 memcpy_fromio(to, map->base + map->translate(map, from), len); 73 } 74 75 static void mtd_pci_write8(struct map_info *_map, map_word val, unsigned long ofs) 76 { 77 struct map_pci_info *map = (struct map_pci_info *)_map; 78 // printk("write8 : %08lx <= %02x\n", ofs, val.x[0]); 79 writeb(val.x[0], map->base + map->translate(map, ofs)); 80 } 81 82 #if 0 83 static void mtd_pci_write16(struct map_info *_map, map_word val, unsigned long ofs) 84 { 85 struct map_pci_info *map = (struct map_pci_info *)_map; 86 // printk("write16: %08lx <= %04x\n", ofs, val.x[0]); 87 writew(val.x[0], map->base + map->translate(map, ofs)); 88 } 89 #endif 90 static void mtd_pci_write32(struct map_info *_map, map_word val, unsigned long ofs) 91 { 92 struct map_pci_info *map = (struct map_pci_info *)_map; 93 // printk("write32: %08lx <= %08x\n", ofs, val.x[0]); 94 writel(val.x[0], map->base + map->translate(map, ofs)); 95 } 96 97 static void mtd_pci_copyto(struct map_info *_map, unsigned long to, const void *from, ssize_t len) 98 { 99 struct map_pci_info *map = (struct map_pci_info *)_map; 100 memcpy_toio(map->base + map->translate(map, to), from, len); 101 } 102 103 static const struct map_info mtd_pci_map = { 104 .phys = NO_XIP, 105 .copy_from = mtd_pci_copyfrom, 106 .copy_to = mtd_pci_copyto, 107 }; 108 109 /* 110 * Intel IOP80310 Flash driver 111 */ 112 113 static int 114 intel_iq80310_init(struct pci_dev *dev, struct map_pci_info *map) 115 { 116 u32 win_base; 117 118 map->map.bankwidth = 1; 119 map->map.read = mtd_pci_read8, 120 map->map.write = mtd_pci_write8, 121 122 map->map.size = 0x00800000; 123 map->base = ioremap_nocache(pci_resource_start(dev, 0), 124 pci_resource_len(dev, 0)); 125 126 if (!map->base) 127 return -ENOMEM; 128 129 /* 130 * We want to base the memory window at Xscale 131 * bus address 0, not 0x1000. 132 */ 133 pci_read_config_dword(dev, 0x44, &win_base); 134 pci_write_config_dword(dev, 0x44, 0); 135 136 map->map.map_priv_2 = win_base; 137 138 return 0; 139 } 140 141 static void 142 intel_iq80310_exit(struct pci_dev *dev, struct map_pci_info *map) 143 { 144 if (map->base) 145 iounmap(map->base); 146 pci_write_config_dword(dev, 0x44, map->map.map_priv_2); 147 } 148 149 static unsigned long 150 intel_iq80310_translate(struct map_pci_info *map, unsigned long ofs) 151 { 152 unsigned long page_addr = ofs & 0x00400000; 153 154 /* 155 * This mundges the flash location so we avoid 156 * the first 80 bytes (they appear to read nonsense). 157 */ 158 if (page_addr) { 159 writel(0x00000008, map->base + 0x1558); 160 writel(0x00000000, map->base + 0x1550); 161 } else { 162 writel(0x00000007, map->base + 0x1558); 163 writel(0x00800000, map->base + 0x1550); 164 ofs += 0x00800000; 165 } 166 167 return ofs; 168 } 169 170 static struct mtd_pci_info intel_iq80310_info = { 171 .init = intel_iq80310_init, 172 .exit = intel_iq80310_exit, 173 .translate = intel_iq80310_translate, 174 .map_name = "cfi_probe", 175 }; 176 177 /* 178 * Intel DC21285 driver 179 */ 180 181 static int 182 intel_dc21285_init(struct pci_dev *dev, struct map_pci_info *map) 183 { 184 unsigned long base, len; 185 186 base = pci_resource_start(dev, PCI_ROM_RESOURCE); 187 len = pci_resource_len(dev, PCI_ROM_RESOURCE); 188 189 if (!len || !base) { 190 /* 191 * No ROM resource 192 */ 193 base = pci_resource_start(dev, 2); 194 len = pci_resource_len(dev, 2); 195 196 /* 197 * We need to re-allocate PCI BAR2 address range to the 198 * PCI ROM BAR, and disable PCI BAR2. 199 */ 200 } else { 201 /* 202 * Hmm, if an address was allocated to the ROM resource, but 203 * not enabled, should we be allocating a new resource for it 204 * or simply enabling it? 205 */ 206 if (!(pci_resource_flags(dev, PCI_ROM_RESOURCE) & 207 IORESOURCE_ROM_ENABLE)) { 208 u32 val; 209 pci_resource_flags(dev, PCI_ROM_RESOURCE) |= IORESOURCE_ROM_ENABLE; 210 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &val); 211 val |= PCI_ROM_ADDRESS_ENABLE; 212 pci_write_config_dword(dev, PCI_ROM_ADDRESS, val); 213 printk("%s: enabling expansion ROM\n", pci_name(dev)); 214 } 215 } 216 217 if (!len || !base) 218 return -ENXIO; 219 220 map->map.bankwidth = 4; 221 map->map.read = mtd_pci_read32, 222 map->map.write = mtd_pci_write32, 223 map->map.size = len; 224 map->base = ioremap_nocache(base, len); 225 226 if (!map->base) 227 return -ENOMEM; 228 229 return 0; 230 } 231 232 static void 233 intel_dc21285_exit(struct pci_dev *dev, struct map_pci_info *map) 234 { 235 u32 val; 236 237 if (map->base) 238 iounmap(map->base); 239 240 /* 241 * We need to undo the PCI BAR2/PCI ROM BAR address alteration. 242 */ 243 pci_resource_flags(dev, PCI_ROM_RESOURCE) &= ~IORESOURCE_ROM_ENABLE; 244 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &val); 245 val &= ~PCI_ROM_ADDRESS_ENABLE; 246 pci_write_config_dword(dev, PCI_ROM_ADDRESS, val); 247 } 248 249 static unsigned long 250 intel_dc21285_translate(struct map_pci_info *map, unsigned long ofs) 251 { 252 return ofs & 0x00ffffc0 ? ofs : (ofs ^ (1 << 5)); 253 } 254 255 static struct mtd_pci_info intel_dc21285_info = { 256 .init = intel_dc21285_init, 257 .exit = intel_dc21285_exit, 258 .translate = intel_dc21285_translate, 259 .map_name = "jedec_probe", 260 }; 261 262 /* 263 * PCI device ID table 264 */ 265 266 static struct pci_device_id mtd_pci_ids[] = { 267 { 268 .vendor = PCI_VENDOR_ID_INTEL, 269 .device = 0x530d, 270 .subvendor = PCI_ANY_ID, 271 .subdevice = PCI_ANY_ID, 272 .class = PCI_CLASS_MEMORY_OTHER << 8, 273 .class_mask = 0xffff00, 274 .driver_data = (unsigned long)&intel_iq80310_info, 275 }, 276 { 277 .vendor = PCI_VENDOR_ID_DEC, 278 .device = PCI_DEVICE_ID_DEC_21285, 279 .subvendor = 0, /* DC21285 defaults to 0 on reset */ 280 .subdevice = 0, /* DC21285 defaults to 0 on reset */ 281 .driver_data = (unsigned long)&intel_dc21285_info, 282 }, 283 { 0, } 284 }; 285 286 /* 287 * Generic code follows. 288 */ 289 290 static int __devinit 291 mtd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) 292 { 293 struct mtd_pci_info *info = (struct mtd_pci_info *)id->driver_data; 294 struct map_pci_info *map = NULL; 295 struct mtd_info *mtd = NULL; 296 int err; 297 298 err = pci_enable_device(dev); 299 if (err) 300 goto out; 301 302 err = pci_request_regions(dev, "pci mtd"); 303 if (err) 304 goto out; 305 306 map = kmalloc(sizeof(*map), GFP_KERNEL); 307 err = -ENOMEM; 308 if (!map) 309 goto release; 310 311 map->map = mtd_pci_map; 312 map->map.name = pci_name(dev); 313 map->dev = dev; 314 map->exit = info->exit; 315 map->translate = info->translate; 316 317 err = info->init(dev, map); 318 if (err) 319 goto release; 320 321 /* tsk - do_map_probe should take const char * */ 322 mtd = do_map_probe((char *)info->map_name, &map->map); 323 err = -ENODEV; 324 if (!mtd) 325 goto release; 326 327 mtd->owner = THIS_MODULE; 328 add_mtd_device(mtd); 329 330 pci_set_drvdata(dev, mtd); 331 332 return 0; 333 334 release: 335 if (map) { 336 map->exit(dev, map); 337 kfree(map); 338 } 339 340 pci_release_regions(dev); 341 out: 342 return err; 343 } 344 345 static void __devexit 346 mtd_pci_remove(struct pci_dev *dev) 347 { 348 struct mtd_info *mtd = pci_get_drvdata(dev); 349 struct map_pci_info *map = mtd->priv; 350 351 del_mtd_device(mtd); 352 map_destroy(mtd); 353 map->exit(dev, map); 354 kfree(map); 355 356 pci_set_drvdata(dev, NULL); 357 pci_release_regions(dev); 358 } 359 360 static struct pci_driver mtd_pci_driver = { 361 .name = "MTD PCI", 362 .probe = mtd_pci_probe, 363 .remove = __devexit_p(mtd_pci_remove), 364 .id_table = mtd_pci_ids, 365 }; 366 367 static int __init mtd_pci_maps_init(void) 368 { 369 return pci_register_driver(&mtd_pci_driver); 370 } 371 372 static void __exit mtd_pci_maps_exit(void) 373 { 374 pci_unregister_driver(&mtd_pci_driver); 375 } 376 377 module_init(mtd_pci_maps_init); 378 module_exit(mtd_pci_maps_exit); 379 380 MODULE_LICENSE("GPL"); 381 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); 382 MODULE_DESCRIPTION("Generic PCI map driver"); 383 MODULE_DEVICE_TABLE(pci, mtd_pci_ids); 384 385