PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/mtd/maps/pci.c

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