pci.c 7.4 KB

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