bast-flash.c 5.3 KB

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