pci.c 9.1 KB

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