pci.c 7.5 KB

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