pci.c 7.6 KB

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