bast-flash.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* linux/drivers/mtd/maps/bast_flash.c
  2. *
  3. * Copyright (c) 2004 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * Simtec Bast (EB2410ITX) NOR MTD Mapping driver
  7. *
  8. * Changelog:
  9. * 20-Sep-2004 BJD Initial version
  10. *
  11. * $Id: bast-flash.c,v 1.1 2004/09/21 14:29:04 bjd Exp $
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/init.h>
  30. #include <linux/kernel.h>
  31. #include <linux/string.h>
  32. #include <linux/ioport.h>
  33. #include <linux/device.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/map.h>
  36. #include <linux/mtd/partitions.h>
  37. #include <asm/io.h>
  38. #include <asm/mach-types.h>
  39. #include <asm/mach/flash.h>
  40. #include <asm/arch/map.h>
  41. #include <asm/arch/bast-map.h>
  42. #include <asm/arch/bast-cpld.h>
  43. #ifdef CONFIG_MTD_BAST_MAXSIZE
  44. #define AREA_MAXSIZE (CONFIG_MTD_BAST_MAXSIZE * (1024*1024))
  45. #else
  46. #define AREA_MAXSIZE (32*1024*1024)
  47. #endif
  48. #define PFX "bast-flash: "
  49. struct bast_flash_info {
  50. struct mtd_info *mtd;
  51. struct map_info map;
  52. struct mtd_partition *partitions;
  53. struct resource *area;
  54. };
  55. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
  56. static struct bast_flash_info *to_bast_info(struct device *dev)
  57. {
  58. return (struct bast_flash_info *)dev_get_drvdata(dev);
  59. }
  60. static void bast_flash_setrw(int to)
  61. {
  62. unsigned int val;
  63. unsigned long flags;
  64. local_irq_save(flags);
  65. val = __raw_readb(BAST_VA_CTRL3);
  66. if (to)
  67. val |= BAST_CPLD_CTRL3_ROMWEN;
  68. else
  69. val &= ~BAST_CPLD_CTRL3_ROMWEN;
  70. pr_debug("new cpld ctrl3=%02x\n", val);
  71. __raw_writeb(val, BAST_VA_CTRL3);
  72. local_irq_restore(flags);
  73. }
  74. static int bast_flash_remove(struct device *dev)
  75. {
  76. struct bast_flash_info *info = to_bast_info(dev);
  77. dev_set_drvdata(dev, NULL);
  78. if (info == NULL)
  79. return 0;
  80. if (info->map.virt != NULL)
  81. iounmap(info->map.virt);
  82. if (info->mtd) {
  83. del_mtd_partitions(info->mtd);
  84. map_destroy(info->mtd);
  85. }
  86. if (info->partitions)
  87. kfree(info->partitions);
  88. if (info->area) {
  89. release_resource(info->area);
  90. kfree(info->area);
  91. }
  92. kfree(info);
  93. return 0;
  94. }
  95. static int bast_flash_probe(struct device *dev)
  96. {
  97. struct platform_device *pdev = to_platform_device(dev);
  98. struct bast_flash_info *info;
  99. struct resource *res;
  100. int err = 0;
  101. info = kmalloc(sizeof(*info), GFP_KERNEL);
  102. if (info == NULL) {
  103. printk(KERN_ERR PFX "no memory for flash info\n");
  104. err = -ENOMEM;
  105. goto exit_error;
  106. }
  107. memzero(info, sizeof(*info));
  108. dev_set_drvdata(dev, info);
  109. res = pdev->resource; /* assume that the flash has one resource */
  110. info->map.phys = res->start;
  111. info->map.size = res->end - res->start + 1;
  112. info->map.name = dev->bus_id;
  113. info->map.bankwidth = 2;
  114. if (info->map.size > AREA_MAXSIZE)
  115. info->map.size = AREA_MAXSIZE;
  116. pr_debug("%s: area %08lx, size %ld\n", __FUNCTION__,
  117. info->map.phys, info->map.size);
  118. info->area = request_mem_region(res->start, info->map.size,
  119. pdev->name);
  120. if (info->area == NULL) {
  121. printk(KERN_ERR PFX "cannot reserve flash memory region\n");
  122. err = -ENOENT;
  123. goto exit_error;
  124. }
  125. info->map.virt = ioremap(res->start, info->map.size);
  126. pr_debug("%s: virt at %08x\n", __FUNCTION__, (int)info->map.virt);
  127. if (info->map.virt == 0) {
  128. printk(KERN_ERR PFX "failed to ioremap() region\n");
  129. err = -EIO;
  130. goto exit_error;
  131. }
  132. simple_map_init(&info->map);
  133. /* enable the write to the flash area */
  134. bast_flash_setrw(1);
  135. /* probe for the device(s) */
  136. info->mtd = do_map_probe("jedec_probe", &info->map);
  137. if (info->mtd == NULL)
  138. info->mtd = do_map_probe("cfi_probe", &info->map);
  139. if (info->mtd == NULL) {
  140. printk(KERN_ERR PFX "map_probe() failed\n");
  141. err = -ENXIO;
  142. goto exit_error;
  143. }
  144. /* mark ourselves as the owner */
  145. info->mtd->owner = THIS_MODULE;
  146. err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
  147. if (err > 0) {
  148. err = add_mtd_partitions(info->mtd, info->partitions, err);
  149. if (err)
  150. printk(KERN_ERR PFX "cannot add/parse partitions\n");
  151. }
  152. if (err == 0)
  153. return 0;
  154. /* fall through to exit error */
  155. exit_error:
  156. bast_flash_remove(dev);
  157. return err;
  158. }
  159. static struct device_driver bast_flash_driver = {
  160. .name = "bast-nor",
  161. .bus = &platform_bus_type,
  162. .probe = bast_flash_probe,
  163. .remove = bast_flash_remove,
  164. };
  165. static int __init bast_flash_init(void)
  166. {
  167. printk("BAST NOR-Flash Driver, (c) 2004 Simtec Electronics\n");
  168. return driver_register(&bast_flash_driver);
  169. }
  170. static void __exit bast_flash_exit(void)
  171. {
  172. driver_unregister(&bast_flash_driver);
  173. }
  174. module_init(bast_flash_init);
  175. module_exit(bast_flash_exit);
  176. MODULE_LICENSE("GPL");
  177. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  178. MODULE_DESCRIPTION("BAST MTD Map driver");