PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/target/linux/amazon/files/drivers/mtd/maps/amazon.c

https://gitlab.com/shinvdu/openwrt
C | 204 lines | 151 code | 31 blank | 22 comment | 10 complexity | 6496379a6fb1128d1eee0efbb4dbb88d MD5 | raw file
  1. /*
  2. * Handle mapping of the flash memory access routines
  3. * on Amazon based devices.
  4. *
  5. * Copyright(C) 2004 peng.liu@infineon.com
  6. *
  7. * This code is GPLed
  8. *
  9. */
  10. // 000005:fchang 2005/6/2 Modified by Bingtao to double check if the EBU is enabled/disabled
  11. // 506231:tc.chen 2005/06/23 increase firmware partition size form 192KB to 256KB
  12. // 050701:linmars 2005/07/01 fix flash size wrong alignment after increase firmware partition
  13. // 165001:henryhsu 2005/8/18 Remove the support for Intel flash because of 2.1 not enough rootfs partition size
  14. // 165001:henryhsu 2005/9/7 Rolback to support INtel flash
  15. // 509071:tc.chen 2005/09/07 Reduced flash writing time
  16. // 511046:linmars 2005/11/04 change bootloader size from 128 into 64
  17. // 511241:linmars 2005/11/24 merge TaiChen's IRM patch
  18. // copyright 2005 infineon
  19. // copyright 2007 john crispin <blogic@openwrt.org>
  20. // copyright 2007 felix fietkau <nbd@openwrt.org>
  21. // copyright 2009 hauke mehrtens <hauke@hauke-m.de>
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <asm/io.h>
  26. #include <linux/init.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/map.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/cfi.h>
  31. #include <linux/mutex.h>
  32. #include <linux/platform_device.h>
  33. #include <asm/amazon/amazon.h>
  34. #define AMAZON_PCI_ARB_CTL_ALT 0xb100205c
  35. #define AMAZON_MTD_REG32( addr ) (*(volatile u32 *)(addr))
  36. static struct map_info amazon_map = {
  37. .name = "AMAZON_FLASH",
  38. .bankwidth = 2,
  39. };
  40. static map_word amazon_read16(struct map_info * map, unsigned long ofs)
  41. {
  42. map_word temp;
  43. ofs ^= 2;
  44. temp.x[0] = *((__u16 *) (map->virt + ofs));
  45. return temp;
  46. }
  47. static void amazon_write16(struct map_info *map, map_word d, unsigned long adr)
  48. {
  49. adr ^= 2;
  50. *((__u16 *) (map->virt + adr)) = d.x[0];
  51. }
  52. void amazon_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  53. {
  54. u8 *p;
  55. u8 *to_8;
  56. from = (unsigned long) (from + map->virt);
  57. p = (u8 *) from;
  58. to_8 = (u8 *) to;
  59. while(len--){
  60. *to_8++ = *p++;
  61. }
  62. }
  63. void amazon_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  64. {
  65. u8 *p = (u8*) from;
  66. u8 *to_8;
  67. to += (unsigned long) map->virt;
  68. to_8 = (u8*)to;
  69. while(len--){
  70. *p++ = *to_8++;
  71. }
  72. }
  73. #define UBOOT_SIZE 0x40000
  74. static struct mtd_partition amazon_partitions[3] = {
  75. {
  76. name:"U-Boot", /* U-Boot firmware */
  77. offset:0x00000000,
  78. size:UBOOT_SIZE , /* 128k */
  79. },
  80. {
  81. name:"kernel", /* firmware */
  82. offset:UBOOT_SIZE,
  83. size:0x00100000, /* 192K */
  84. },
  85. {
  86. name:"rootfs", /* default partition */
  87. offset:0x00200000,
  88. size:0x00200000,
  89. },
  90. };
  91. unsigned long uImage_size = 0x10000d;
  92. int find_uImage_size(unsigned long start_offset)
  93. {
  94. unsigned long magic;
  95. unsigned long temp;
  96. amazon_copy_from(&amazon_map, &magic, start_offset, 4);
  97. if (!(ntohl(magic) == 0x27051956)) {
  98. printk(KERN_INFO "amazon_mtd: invalid magic (0x%08X) of kernel at 0x%08lx \n", ntohl(magic), start_offset);
  99. return 0;
  100. }
  101. amazon_copy_from(&amazon_map, &temp, start_offset + 12, 4);
  102. printk(KERN_INFO "amazon_mtd: kernel size is %ld \n", temp + 0x40);
  103. return temp + 0x40;
  104. }
  105. static int amazon_mtd_probe(struct platform_device *dev)
  106. {
  107. unsigned long uimage_size;
  108. struct mtd_info *mymtd = NULL;
  109. struct mtd_partition *parts = NULL;
  110. *AMAZON_EBU_BUSCON0 = 0x1d7ff;
  111. amazon_map.read = amazon_read16;
  112. amazon_map.write = amazon_write16;
  113. amazon_map.copy_from = amazon_copy_from;
  114. amazon_map.copy_to = amazon_copy_to;
  115. amazon_map.phys = dev->resource->start;
  116. amazon_map.size = dev->resource->end - amazon_map.phys + 1;
  117. amazon_map.virt = ioremap_nocache(amazon_map.phys, amazon_map.size);
  118. if (!amazon_map.virt) {
  119. printk(KERN_WARNING "amazon_mtd: Failed to ioremap!\n");
  120. return -EIO;
  121. }
  122. mymtd = (struct mtd_info *) do_map_probe("cfi_probe", &amazon_map);
  123. if (!mymtd) {
  124. iounmap(amazon_map.virt);
  125. printk(KERN_WARNING "amazon_mtd: probing failed\n");
  126. return -ENXIO;
  127. }
  128. mymtd->owner = THIS_MODULE;
  129. parts = &amazon_partitions[0];
  130. /* Some Samsung devices are containing a 16 MB flash chip with a bigger U-Boot partition. */
  131. if(mymtd->size == 0x01000000 && mymtd->erasesize == 0x00020000) {
  132. printk(KERN_INFO "amazon_mtd: Found big flash chip!\n");
  133. amazon_partitions[0].size = 0x60000;
  134. amazon_partitions[1].offset = 0x60000;
  135. uimage_size = find_uImage_size(amazon_partitions[1].offset);
  136. amazon_partitions[1].size = uimage_size;
  137. amazon_partitions[2].offset = 0x60000 + uimage_size;
  138. amazon_partitions[2].size = mymtd->size - amazon_partitions[2].offset - mymtd->erasesize;
  139. } else {
  140. printk(KERN_INFO "amazon_mtd: Found small flash chip!\n");
  141. uimage_size = find_uImage_size(amazon_partitions[1].offset);
  142. amazon_partitions[1].size = uimage_size;
  143. amazon_partitions[2].offset = UBOOT_SIZE + uimage_size;
  144. amazon_partitions[2].size = mymtd->size - amazon_partitions[2].offset - (2 * mymtd->erasesize);
  145. }
  146. mtd_device_register(mymtd, parts, 3);
  147. printk(KERN_INFO "amazon_mtd: added %s flash with %dMB\n",
  148. amazon_map.name, ((int)mymtd->size) >> 20);
  149. return 0;
  150. }
  151. static struct platform_driver amazon_mtd_driver = {
  152. .probe = amazon_mtd_probe,
  153. .driver = {
  154. .name = "amazon_mtd",
  155. .owner = THIS_MODULE,
  156. },
  157. };
  158. static int __init amazon_mtd_init(void)
  159. {
  160. int ret = platform_driver_register(&amazon_mtd_driver);
  161. if (ret)
  162. printk(KERN_WARNING "amazon_mtd: error registering platfom driver!\n");
  163. return ret;
  164. }
  165. static void __exit amazon_mtd_cleanup(void)
  166. {
  167. platform_driver_unregister(&amazon_mtd_driver);
  168. }
  169. module_init(amazon_mtd_init);
  170. module_exit(amazon_mtd_cleanup);
  171. MODULE_LICENSE("GPL");
  172. MODULE_AUTHOR("john crispin blogic@openwrt.org");
  173. MODULE_DESCRIPTION("MTD map driver for AMAZON boards");