autcpu12-nvram.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * NV-RAM memory access on autcpu12 
  3.  * (C) 2002 Thomas Gleixner (gleixner@autronix.de)
  4.  *
  5.  * $Id: autcpu12-nvram.c,v 1.1 2002/02/22 09:30:24 gleixner Exp $ 
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  *
  21.  */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/ioport.h>
  26. #include <asm/io.h>
  27. #include <asm/sizes.h>
  28. #include <asm/hardware.h>
  29. #include <asm/arch/autcpu12.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/map.h>
  32. #include <linux/mtd/partitions.h>
  33. __u8 autcpu12_read8(struct map_info *map, unsigned long ofs)
  34. {
  35. return __raw_readb(map->map_priv_1 + ofs);
  36. }
  37. __u16 autcpu12_read16(struct map_info *map, unsigned long ofs)
  38. {
  39. return __raw_readw(map->map_priv_1 + ofs);
  40. }
  41. __u32 autcpu12_read32(struct map_info *map, unsigned long ofs)
  42. {
  43. return __raw_readl(map->map_priv_1 + ofs);
  44. }
  45. void autcpu12_write8(struct map_info *map, __u8 d, unsigned long adr)
  46. {
  47. __raw_writeb(d, map->map_priv_1 + adr);
  48. mb();
  49. }
  50. void autcpu12_write16(struct map_info *map, __u16 d, unsigned long adr)
  51. {
  52. __raw_writew(d, map->map_priv_1 + adr);
  53. mb();
  54. }
  55. void autcpu12_write32(struct map_info *map, __u32 d, unsigned long adr)
  56. {
  57. __raw_writel(d, map->map_priv_1 + adr);
  58. mb();
  59. }
  60. void autcpu12_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  61. {
  62. memcpy_fromio(to, map->map_priv_1 + from, len);
  63. }
  64. void autcpu12_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  65. {
  66. while(len) {
  67. __raw_writeb(*(unsigned char *) from, map->map_priv_1 + to);
  68. from++;
  69. to++;
  70. len--;
  71. }
  72. }
  73. static struct mtd_info *sram_mtd;
  74. struct map_info autcpu12_sram_map = {
  75. name: "SRAM",
  76. size: 32768,
  77. buswidth: 8,
  78. read8: autcpu12_read8,
  79. read16: autcpu12_read16,
  80. read32: autcpu12_read32,
  81. copy_from: autcpu12_copy_from,
  82. write8: autcpu12_write8,
  83. write16: autcpu12_write16,
  84. write32: autcpu12_write32,
  85. copy_to: autcpu12_copy_to
  86. };
  87. static int __init init_autcpu12_sram (void)
  88. {
  89. int err, save0, save1;
  90. autcpu12_sram_map.map_priv_1 = (unsigned long)ioremap(0x12000000, SZ_128K);
  91. if (!autcpu12_sram_map.map_priv_1) {
  92. printk("Failed to ioremap autcpu12 NV-RAM spacen");
  93. err = -EIO;
  94. goto out;
  95. }
  96. /* 
  97.  * Check for 32K/128K 
  98.  * read ofs 0 
  99.  * read ofs 0x10000 
  100.  * Write complement to ofs 0x100000
  101.  * Read and check result on ofs 0x0
  102.  * Restore contents
  103.  */
  104. save0 = autcpu12_read32(&autcpu12_sram_map,0);
  105. save1 = autcpu12_read32(&autcpu12_sram_map,0x10000);
  106. autcpu12_write32(&autcpu12_sram_map,~save0,0x10000);
  107. /* if we find this pattern on 0x0, we have 32K size 
  108.  * restore contents and exit
  109.  */
  110. if ( autcpu12_read32(&autcpu12_sram_map,0) != save0) {
  111. autcpu12_write32(&autcpu12_sram_map,save0,0x0);
  112. goto map;
  113. }
  114. /* We have a 128K found, restore 0x10000 and set size
  115.  * to 128K
  116.  */
  117. autcpu12_write32(&autcpu12_sram_map,save1,0x10000);
  118. autcpu12_sram_map.size = SZ_128K;
  119. map:
  120. sram_mtd = do_map_probe("map_ram", &autcpu12_sram_map);
  121. if (!sram_mtd) {
  122. printk("NV-RAM probe failedn");
  123. err = -ENXIO;
  124. goto out_ioremap;
  125. }
  126. sram_mtd->module = THIS_MODULE;
  127. sram_mtd->erasesize = 16;
  128. if (add_mtd_device(sram_mtd)) {
  129. printk("NV-RAM device addition failedn");
  130. err = -ENOMEM;
  131. goto out_probe;
  132. }
  133. printk("NV-RAM device size %ldK registered on AUTCPU12n",autcpu12_sram_map.size/SZ_1K);
  134. return 0;
  135. out_probe:
  136. map_destroy(sram_mtd);
  137. sram_mtd = 0;
  138. out_ioremap:
  139. iounmap((void *)autcpu12_sram_map.map_priv_1);
  140. out:
  141. return err;
  142. }
  143. static void __exit cleanup_autcpu12_maps(void)
  144. {
  145. if (sram_mtd) {
  146. del_mtd_device(sram_mtd);
  147. map_destroy(sram_mtd);
  148. iounmap((void *)autcpu12_sram_map.map_priv_1);
  149. }
  150. }
  151. module_init(init_autcpu12_sram);
  152. module_exit(cleanup_autcpu12_maps);
  153. MODULE_AUTHOR("Thomas Gleixner");
  154. MODULE_DESCRIPTION("autcpu12 NV-RAM map driver");
  155. MODULE_LICENSE("GPL");