intel_vr_nor.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/slab.h>
  32. #include <linux/pci.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. struct pci_dev *dev;
  44. };
  45. /* Expansion Bus Configuration and Status Registers are in BAR 0 */
  46. #define EXP_CSR_MBAR 0
  47. /* Expansion Bus Memory Window is BAR 1 */
  48. #define EXP_WIN_MBAR 1
  49. /* Maximum address space for Chip Select 0 is 64MiB */
  50. #define CS0_SIZE 0x04000000
  51. /* Chip Select 0 is at offset 0 in the Memory Window */
  52. #define CS0_START 0x0
  53. /* Chip Select 0 Timing Register is at offset 0 in CSR */
  54. #define EXP_TIMING_CS0 0x00
  55. #define TIMING_CS_EN (1 << 31) /* Chip Select Enable */
  56. #define TIMING_BOOT_ACCEL_DIS (1 << 8) /* Boot Acceleration Disable */
  57. #define TIMING_WR_EN (1 << 1) /* Write Enable */
  58. #define TIMING_BYTE_EN (1 << 0) /* 8-bit vs 16-bit bus */
  59. #define TIMING_MASK 0x3FFF0000
  60. static void vr_nor_destroy_partitions(struct vr_nor_mtd *p)
  61. {
  62. mtd_device_unregister(p->info);
  63. }
  64. static int vr_nor_init_partitions(struct vr_nor_mtd *p)
  65. {
  66. /* register the flash bank */
  67. /* partition the flash bank */
  68. return mtd_device_parse_register(p->info, NULL, NULL, NULL, 0);
  69. }
  70. static void vr_nor_destroy_mtd_setup(struct vr_nor_mtd *p)
  71. {
  72. map_destroy(p->info);
  73. }
  74. static int vr_nor_mtd_setup(struct vr_nor_mtd *p)
  75. {
  76. static const char * const probe_types[] =
  77. { "cfi_probe", "jedec_probe", NULL };
  78. const char * const *type;
  79. for (type = probe_types; !p->info && *type; type++)
  80. p->info = do_map_probe(*type, &p->map);
  81. if (!p->info)
  82. return -ENODEV;
  83. p->info->dev.parent = &p->dev->dev;
  84. return 0;
  85. }
  86. static void vr_nor_destroy_maps(struct vr_nor_mtd *p)
  87. {
  88. unsigned int exp_timing_cs0;
  89. /* write-protect the flash bank */
  90. exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
  91. exp_timing_cs0 &= ~TIMING_WR_EN;
  92. writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
  93. /* unmap the flash window */
  94. iounmap(p->map.virt);
  95. /* unmap the csr window */
  96. iounmap(p->csr_base);
  97. }
  98. /*
  99. * Initialize the map_info structure and map the flash.
  100. * Returns 0 on success, nonzero otherwise.
  101. */
  102. static int vr_nor_init_maps(struct vr_nor_mtd *p)
  103. {
  104. unsigned long csr_phys, csr_len;
  105. unsigned long win_phys, win_len;
  106. unsigned int exp_timing_cs0;
  107. int err;
  108. csr_phys = pci_resource_start(p->dev, EXP_CSR_MBAR);
  109. csr_len = pci_resource_len(p->dev, EXP_CSR_MBAR);
  110. win_phys = pci_resource_start(p->dev, EXP_WIN_MBAR);
  111. win_len = pci_resource_len(p->dev, EXP_WIN_MBAR);
  112. if (!csr_phys || !csr_len || !win_phys || !win_len)
  113. return -ENODEV;
  114. if (win_len < (CS0_START + CS0_SIZE))
  115. return -ENXIO;
  116. p->csr_base = ioremap_nocache(csr_phys, csr_len);
  117. if (!p->csr_base)
  118. return -ENOMEM;
  119. exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
  120. if (!(exp_timing_cs0 & TIMING_CS_EN)) {
  121. dev_warn(&p->dev->dev, "Expansion Bus Chip Select 0 "
  122. "is disabled.\n");
  123. err = -ENODEV;
  124. goto release;
  125. }
  126. if ((exp_timing_cs0 & TIMING_MASK) == TIMING_MASK) {
  127. dev_warn(&p->dev->dev, "Expansion Bus Chip Select 0 "
  128. "is configured for maximally slow access times.\n");
  129. }
  130. p->map.name = DRV_NAME;
  131. p->map.bankwidth = (exp_timing_cs0 & TIMING_BYTE_EN) ? 1 : 2;
  132. p->map.phys = win_phys + CS0_START;
  133. p->map.size = CS0_SIZE;
  134. p->map.virt = ioremap_nocache(p->map.phys, p->map.size);
  135. if (!p->map.virt) {
  136. err = -ENOMEM;
  137. goto release;
  138. }
  139. simple_map_init(&p->map);
  140. /* Enable writes to flash bank */
  141. exp_timing_cs0 |= TIMING_BOOT_ACCEL_DIS | TIMING_WR_EN;
  142. writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
  143. return 0;
  144. release:
  145. iounmap(p->csr_base);
  146. return err;
  147. }
  148. static struct pci_device_id vr_nor_pci_ids[] = {
  149. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x500D)},
  150. {0,}
  151. };
  152. static void vr_nor_pci_remove(struct pci_dev *dev)
  153. {
  154. struct vr_nor_mtd *p = pci_get_drvdata(dev);
  155. vr_nor_destroy_partitions(p);
  156. vr_nor_destroy_mtd_setup(p);
  157. vr_nor_destroy_maps(p);
  158. kfree(p);
  159. pci_release_regions(dev);
  160. pci_disable_device(dev);
  161. }
  162. static int vr_nor_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  163. {
  164. struct vr_nor_mtd *p = NULL;
  165. unsigned int exp_timing_cs0;
  166. int err;
  167. err = pci_enable_device(dev);
  168. if (err)
  169. goto out;
  170. err = pci_request_regions(dev, DRV_NAME);
  171. if (err)
  172. goto disable_dev;
  173. p = kzalloc(sizeof(*p), GFP_KERNEL);
  174. err = -ENOMEM;
  175. if (!p)
  176. goto release;
  177. p->dev = dev;
  178. err = vr_nor_init_maps(p);
  179. if (err)
  180. goto release;
  181. err = vr_nor_mtd_setup(p);
  182. if (err)
  183. goto destroy_maps;
  184. err = vr_nor_init_partitions(p);
  185. if (err)
  186. goto destroy_mtd_setup;
  187. pci_set_drvdata(dev, p);
  188. return 0;
  189. destroy_mtd_setup:
  190. map_destroy(p->info);
  191. destroy_maps:
  192. /* write-protect the flash bank */
  193. exp_timing_cs0 = readl(p->csr_base + EXP_TIMING_CS0);
  194. exp_timing_cs0 &= ~TIMING_WR_EN;
  195. writel(exp_timing_cs0, p->csr_base + EXP_TIMING_CS0);
  196. /* unmap the flash window */
  197. iounmap(p->map.virt);
  198. /* unmap the csr window */
  199. iounmap(p->csr_base);
  200. release:
  201. kfree(p);
  202. pci_release_regions(dev);
  203. disable_dev:
  204. pci_disable_device(dev);
  205. out:
  206. return err;
  207. }
  208. static struct pci_driver vr_nor_pci_driver = {
  209. .name = DRV_NAME,
  210. .probe = vr_nor_pci_probe,
  211. .remove = vr_nor_pci_remove,
  212. .id_table = vr_nor_pci_ids,
  213. };
  214. module_pci_driver(vr_nor_pci_driver);
  215. MODULE_AUTHOR("Andy Lowe");
  216. MODULE_DESCRIPTION("MTD map driver for NOR flash on Intel Vermilion Range");
  217. MODULE_LICENSE("GPL");
  218. MODULE_DEVICE_TABLE(pci, vr_nor_pci_ids);