intel_vr_nor.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * drivers/mtd/maps/intel_vr_nor.c
  3. *
  4. * An MTD map driver for a NOR flash bank on the Expansion Bus of the Intel
  5. * Vermilion Range chipset.
  6. *
  7. * The Vermilion Range Expansion Bus supports four chip selects, each of which
  8. * has 64MiB of address space. The 2nd BAR of the Expansion Bus PCI Device
  9. * is a 256MiB memory region containing the address spaces for all four of the
  10. * chip selects, with start addresses hardcoded on 64MiB boundaries.
  11. *
  12. * This map driver only supports NOR flash on chip select 0. The buswidth
  13. * (either 8 bits or 16 bits) is determined by reading the Expansion Bus Timing
  14. * and Control Register for Chip Select 0 (EXP_TIMING_CS0). This driver does
  15. * not modify the value in the EXP_TIMING_CS0 register except to enable writing
  16. * and disable boot acceleration. The timing parameters in the register are
  17. * assumed to have been properly initialized by the BIOS. The reset default
  18. * timing parameters are maximally conservative (slow), so access to the flash
  19. * will be slower than it should be if the BIOS has not initialized the timing
  20. * parameters.
  21. *
  22. * Author: Andy Lowe <alowe@mvista.com>
  23. *
  24. * 2006 (c) MontaVista Software, Inc. This file is licensed under
  25. * the terms of the GNU General Public License version 2. This program
  26. * is licensed "as is" without any warranty of any kind, whether express
  27. * or implied.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/pci.h>
  32. #include <linux/init.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/map.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <linux/mtd/cfi.h>
  37. #include <linux/mtd/flashchip.h>
  38. #define DRV_NAME "vr_nor"
  39. struct vr_nor_mtd {
  40. void __iomem *csr_base;
  41. struct map_info map;
  42. struct mtd_info *info;
  43. int nr_parts;
  44. struct pci_dev *dev;
  45. };
  46. /* Expansion Bus Configuration and Status Registers are in BAR 0 */
  47. #define EXP_CSR_MBAR 0
  48. /* Expansion Bus Memory Window is BAR 1 */
  49. #define EXP_WIN_MBAR 1
  50. /* Maximum address space for Chip Select 0 is 64MiB */
  51. #define CS0_SIZE 0x04000000
  52. /* Chip Select 0 is at offset 0 in the Memory Window */
  53. #define CS0_START 0x0
  54. /* Chip Select 0 Timing Register is at offset 0 in CSR */
  55. #define EXP_TIMING_CS0 0x00
  56. #define TIMING_CS_EN (1 << 31) /* Chip Select Enable */
  57. #define TIMING_BOOT_ACCEL_DIS (1 << 8) /* Boot Acceleration Disable */
  58. #define TIMING_WR_EN (1 << 1) /* Write Enable */
  59. #define TIMING_BYTE_EN (1 << 0) /* 8-bit vs 16-bit bus */
  60. #define TIMING_MASK 0x3FFF0000
  61. static void __devexit vr_nor_destroy_partitions(struct vr_nor_mtd *p)
  62. {
  63. if (p->nr_parts > 0) {
  64. #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
  65. del_mtd_partitions(p->info);
  66. #endif
  67. } else
  68. del_mtd_device(p->info);
  69. }
  70. static int __devinit vr_nor_init_partitions(struct vr_nor_mtd *p)
  71. {
  72. int err = 0;
  73. #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
  74. struct mtd_partition *parts;
  75. static const char *part_probes[] = { "cmdlinepart", NULL };
  76. #endif
  77. /* register the flash bank */
  78. #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
  79. /* partition the flash bank */
  80. p->nr_parts = parse_mtd_partitions(p->info, part_probes, &parts, 0);
  81. if (p->nr_parts > 0)
  82. err = add_mtd_partitions(p->info, parts, p->nr_parts);
  83. #endif
  84. if (p->nr_parts <= 0)
  85. err = add_mtd_device(p->info);
  86. return err;
  87. }
  88. static void __devexit vr_nor_destroy_mtd_setup(struct vr_nor_mtd *p)
  89. {
  90. map_destroy(p->info);
  91. }
  92. static int __devinit vr_nor_mtd_setup(struct vr_nor_mtd *p)
  93. {
  94. static const char *probe_types[] =
  95. { "cfi_probe", "jedec_probe", NULL };
  96. const char **type;
  97. for (type = probe_types; !p->info && *type; type++)
  98. p->info = do_map_probe(*type, &p->map);
  99. if (!p->info)
  100. return -ENODEV;
  101. p->info->owner = THIS_MODULE;
  102. return 0;
  103. }
  104. static void __devexit vr_nor_destroy_maps(struct vr_nor_mtd *p)
  105. {
  106. unsigned int exp_timing_cs0;
  107. /* write-protect the flash bank */
  108. exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
  109. exp_timing_cs0 &= ~TIMING_WR_EN;
  110. writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
  111. /* unmap the flash window */
  112. iounmap(p->map.virt);
  113. /* unmap the csr window */
  114. iounmap(p->csr_base);
  115. }
  116. /*
  117. * Initialize the map_info structure and map the flash.
  118. * Returns 0 on success, nonzero otherwise.
  119. */
  120. static int __devinit vr_nor_init_maps(struct vr_nor_mtd *p)
  121. {
  122. unsigned long csr_phys, csr_len;
  123. unsigned long win_phys, win_len;
  124. unsigned int exp_timing_cs0;
  125. int err;
  126. csr_phys = pci_resource_start(p->dev, EXP_CSR_MBAR);
  127. csr_len = pci_resource_len(p->dev, EXP_CSR_MBAR);
  128. win_phys = pci_resource_start(p->dev, EXP_WIN_MBAR);
  129. win_len = pci_resource_len(p->dev, EXP_WIN_MBAR);
  130. if (!csr_phys || !csr_len || !win_phys || !win_len)
  131. return -ENODEV;
  132. if (win_len < (CS0_START + CS0_SIZE))
  133. return -ENXIO;
  134. p->csr_base = ioremap_nocache(csr_phys, csr_len);
  135. if (!p->csr_base)
  136. return -ENOMEM;
  137. exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
  138. if (!(exp_timing_cs0 & TIMING_CS_EN)) {
  139. dev_warn(&p->dev->dev, "Expansion Bus Chip Select 0 "
  140. "is disabled.\n");
  141. err = -ENODEV;
  142. goto release;
  143. }
  144. if ((exp_timing_cs0 & TIMING_MASK) == TIMING_MASK) {
  145. dev_warn(&p->dev->dev, "Expansion Bus Chip Select 0 "
  146. "is configured for maximally slow access times.\n");
  147. }
  148. p->map.name = DRV_NAME;
  149. p->map.bankwidth = (exp_timing_cs0 & TIMING_BYTE_EN) ? 1 : 2;
  150. p->map.phys = win_phys + CS0_START;
  151. p->map.size = CS0_SIZE;
  152. p->map.virt = ioremap_nocache(p->map.phys, p->map.size);
  153. if (!p->map.virt) {
  154. err = -ENOMEM;
  155. goto release;
  156. }
  157. simple_map_init(&p->map);
  158. /* Enable writes to flash bank */
  159. exp_timing_cs0 |= TIMING_BOOT_ACCEL_DIS | TIMING_WR_EN;
  160. writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
  161. return 0;
  162. release:
  163. iounmap(p->csr_base);
  164. return err;
  165. }
  166. static struct pci_device_id vr_nor_pci_ids[] = {
  167. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x500D)},
  168. {0,}
  169. };
  170. static void __devexit vr_nor_pci_remove(struct pci_dev *dev)
  171. {
  172. struct vr_nor_mtd *p = pci_get_drvdata(dev);
  173. pci_set_drvdata(dev, NULL);
  174. vr_nor_destroy_partitions(p);
  175. vr_nor_destroy_mtd_setup(p);
  176. vr_nor_destroy_maps(p);
  177. kfree(p);
  178. pci_release_regions(dev);
  179. pci_disable_device(dev);
  180. }
  181. static int __devinit
  182. vr_nor_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  183. {
  184. struct vr_nor_mtd *p = NULL;
  185. unsigned int exp_timing_cs0;
  186. int err;
  187. err = pci_enable_device(dev);
  188. if (err)
  189. goto out;
  190. err = pci_request_regions(dev, DRV_NAME);
  191. if (err)
  192. goto disable_dev;
  193. p = kzalloc(sizeof(*p), GFP_KERNEL);
  194. err = -ENOMEM;
  195. if (!p)
  196. goto release;
  197. p->dev = dev;
  198. err = vr_nor_init_maps(p);
  199. if (err)
  200. goto release;
  201. err = vr_nor_mtd_setup(p);
  202. if (err)
  203. goto destroy_maps;
  204. err = vr_nor_init_partitions(p);
  205. if (err)
  206. goto destroy_mtd_setup;
  207. pci_set_drvdata(dev, p);
  208. return 0;
  209. destroy_mtd_setup:
  210. map_destroy(p->info);
  211. destroy_maps:
  212. /* write-protect the flash bank */
  213. exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
  214. exp_timing_cs0 &= ~TIMING_WR_EN;
  215. writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
  216. /* unmap the flash window */
  217. iounmap(p->map.virt);
  218. /* unmap the csr window */
  219. iounmap(p->csr_base);
  220. release:
  221. kfree(p);
  222. pci_release_regions(dev);
  223. disable_dev:
  224. pci_disable_device(dev);
  225. out:
  226. return err;
  227. }
  228. static struct pci_driver vr_nor_pci_driver = {
  229. .name = DRV_NAME,
  230. .probe = vr_nor_pci_probe,
  231. .remove = __devexit_p(vr_nor_pci_remove),
  232. .id_table = vr_nor_pci_ids,
  233. };
  234. static int __init vr_nor_mtd_init(void)
  235. {
  236. return pci_register_driver(&vr_nor_pci_driver);
  237. }
  238. static void __exit vr_nor_mtd_exit(void)
  239. {
  240. pci_unregister_driver(&vr_nor_pci_driver);
  241. }
  242. module_init(vr_nor_mtd_init);
  243. module_exit(vr_nor_mtd_exit);
  244. MODULE_AUTHOR("Andy Lowe");
  245. MODULE_DESCRIPTION("MTD map driver for NOR flash on Intel Vermilion Range");
  246. MODULE_LICENSE("GPL");
  247. MODULE_DEVICE_TABLE(pci, vr_nor_pci_ids);