adm8668.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
  3. * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
  4. * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
  5. * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
  6. *
  7. * original functions for finding root filesystem from Mike Baker
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  17. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  20. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. *
  30. * Copyright 2004, Broadcom Corporation
  31. * All Rights Reserved.
  32. *
  33. * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
  34. * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
  35. * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
  36. * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
  37. *
  38. * Flash mapping for adm8668 boards
  39. *
  40. */
  41. #include <linux/module.h>
  42. #include <linux/types.h>
  43. #include <linux/kernel.h>
  44. #include <linux/sched.h>
  45. #include <linux/wait.h>
  46. #include <linux/mtd/mtd.h>
  47. #include <linux/mtd/map.h>
  48. #include <linux/slab.h>
  49. #include <linux/mtd/partitions.h>
  50. #include <linux/crc32.h>
  51. #include <linux/magic.h>
  52. #include <asm/io.h>
  53. #define WINDOW_ADDR 0x10000000
  54. #define WINDOW_SIZE 0x800000
  55. #define BANKWIDTH 2
  56. /* first a little bit about the headers i need.. */
  57. /* just interested in part of the full struct */
  58. struct squashfs_super_block {
  59. __le32 s_magic;
  60. __le32 pad0[9]; /* it's not really padding */
  61. __le64 bytes_used;
  62. };
  63. #define IH_MAGIC 0x56190527 /* Image Magic Number */
  64. struct uboot_header {
  65. uint32_t ih_magic; /* Image Header Magic Number */
  66. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  67. uint32_t ih_time; /* Image Creation Timestamp */
  68. uint32_t ih_size; /* Image Data Size */
  69. uint32_t ih_load; /* Data Load Address */
  70. uint32_t ih_ep; /* Entry Point Address */
  71. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  72. uint8_t ih_os; /* Operating System */
  73. uint8_t ih_arch; /* CPU architecture */
  74. uint8_t ih_type; /* Image Type */
  75. uint8_t ih_comp; /* Compression Type */
  76. char ih_name[32]; /* image name */
  77. };
  78. /************************************************/
  79. static struct mtd_info *adm8668_mtd;
  80. struct map_info adm8668_map = {
  81. name: "adm8668-nor",
  82. size: WINDOW_SIZE,
  83. phys: WINDOW_ADDR,
  84. bankwidth: BANKWIDTH,
  85. };
  86. /*
  87. * Copied from mtdblock.c
  88. *
  89. * Cache stuff...
  90. *
  91. * Since typical flash erasable sectors are much larger than what Linux's
  92. * buffer cache can handle, we must implement read-modify-write on flash
  93. * sectors for each block write requests. To avoid over-erasing flash sectors
  94. * and to speed things up, we locally cache a whole flash sector while it is
  95. * being written to until a different sector is required.
  96. */
  97. static void erase_callback(struct erase_info *done)
  98. {
  99. wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  100. wake_up(wait_q);
  101. }
  102. static int erase_write (struct mtd_info *mtd, unsigned long pos,
  103. int len, const char *buf)
  104. {
  105. struct erase_info erase;
  106. DECLARE_WAITQUEUE(wait, current);
  107. wait_queue_head_t wait_q;
  108. size_t retlen;
  109. int ret;
  110. /*
  111. * First, let's erase the flash block.
  112. */
  113. init_waitqueue_head(&wait_q);
  114. erase.mtd = mtd;
  115. erase.callback = erase_callback;
  116. erase.addr = pos;
  117. erase.len = len;
  118. erase.priv = (u_long)&wait_q;
  119. set_current_state(TASK_INTERRUPTIBLE);
  120. add_wait_queue(&wait_q, &wait);
  121. ret = mtd->_erase(mtd, &erase);
  122. if (ret) {
  123. set_current_state(TASK_RUNNING);
  124. remove_wait_queue(&wait_q, &wait);
  125. printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
  126. "on \"%s\" failed\n",
  127. pos, len, mtd->name);
  128. return ret;
  129. }
  130. schedule(); /* Wait for erase to finish. */
  131. remove_wait_queue(&wait_q, &wait);
  132. /*
  133. * Next, write data to flash.
  134. */
  135. ret = mtd->_write (mtd, pos, len, &retlen, buf);
  136. if (ret)
  137. return ret;
  138. if (retlen != len)
  139. return -EIO;
  140. return 0;
  141. }
  142. /* decent defaults in case... shrug */
  143. static struct mtd_partition adm8668_parts[] = {
  144. { name: "linux", offset: 0x40000, size: WINDOW_SIZE-0x40000, },
  145. { name: "rootfs", offset: 0xe0000, size: 0x140000, },
  146. { name: "uboot_env", offset: 0x20000, size: 0x20000, },
  147. { name: NULL, },
  148. };
  149. /* in case i wanna change stuff later, and to clarify the math section... */
  150. #define PART_LINUX 0
  151. #define PART_ROOTFS 1
  152. #define NR_PARTS 3
  153. static int __init
  154. init_mtd_partitions(struct mtd_info *mtd, size_t size)
  155. {
  156. struct uboot_header uhdr;
  157. int off, blocksize;
  158. size_t len, linux_len;
  159. struct squashfs_super_block shdr;
  160. blocksize = mtd->erasesize;
  161. if (blocksize < 0x10000)
  162. blocksize = 0x10000;
  163. /* now find squashfs */
  164. memset(&shdr, 0xe5, sizeof(shdr));
  165. for (off = adm8668_parts[PART_LINUX].offset; off < size; off += blocksize) {
  166. /*
  167. * Read into buffer
  168. */
  169. if (mtd->_read(mtd, off, sizeof(shdr), &len, (char *)&shdr) ||
  170. len != sizeof(shdr))
  171. continue;
  172. if (shdr.s_magic == SQUASHFS_MAGIC) {
  173. uint32_t fs_size = (uint32_t)shdr.bytes_used;
  174. printk(KERN_INFO "%s: Filesystem type: squashfs, size=%dkB\n",
  175. mtd->name, fs_size>>10);
  176. /* Update rootfs based on the superblock info, and
  177. * stretch to end of MTD. rootfs_split will split it */
  178. adm8668_parts[PART_ROOTFS].offset = off;
  179. adm8668_parts[PART_ROOTFS].size = mtd->size -
  180. adm8668_parts[PART_ROOTFS].offset;
  181. /* kernel ends where rootfs starts
  182. * but we'll keep it full-length for upgrades */
  183. linux_len = adm8668_parts[PART_LINUX+1].offset -
  184. adm8668_parts[PART_LINUX].offset;
  185. #if 1
  186. adm8668_parts[PART_LINUX].size = mtd->size -
  187. adm8668_parts[PART_LINUX].offset;
  188. #else
  189. adm8668_parts[PART_LINUX].size = linux_len;
  190. #endif
  191. goto found;
  192. }
  193. }
  194. printk(KERN_NOTICE
  195. "%s: Couldn't find root filesystem\n",
  196. mtd->name);
  197. return NR_PARTS;
  198. found:
  199. if (mtd->_read(mtd, adm8668_parts[PART_LINUX].offset, sizeof(uhdr), &len, (char *)&uhdr) ||
  200. len != sizeof(uhdr))
  201. return NR_PARTS;
  202. /* that's odd. how'd ya boot it then */
  203. if (uhdr.ih_magic != IH_MAGIC)
  204. return NR_PARTS;
  205. if (be32_to_cpu(uhdr.ih_size) != (linux_len - sizeof(uhdr))) {
  206. unsigned char *block, *data;
  207. unsigned int offset;
  208. offset = adm8668_parts[PART_LINUX].offset +
  209. sizeof(struct uboot_header);
  210. data = (unsigned char *)(WINDOW_ADDR | 0xA0000000 | offset);
  211. printk(KERN_NOTICE "Updating U-boot image:\n");
  212. printk(KERN_NOTICE " old: [size: %8d crc32: 0x%08x]\n",
  213. be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
  214. /* Update the data length & crc32 */
  215. uhdr.ih_size = cpu_to_be32(linux_len - sizeof(uhdr));
  216. uhdr.ih_dcrc = crc32_le(~0, data, linux_len - sizeof(uhdr)) ^ (~0);
  217. uhdr.ih_dcrc = cpu_to_be32(uhdr.ih_dcrc);
  218. printk(KERN_NOTICE " new: [size: %8d crc32: 0x%08x]\n",
  219. be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
  220. /* update header's crc... */
  221. uhdr.ih_hcrc = 0;
  222. uhdr.ih_hcrc = crc32_le(~0, (unsigned char *)&uhdr,
  223. sizeof(uhdr)) ^ (~0);
  224. uhdr.ih_hcrc = cpu_to_be32(uhdr.ih_hcrc);
  225. /* read first eraseblock from the image */
  226. block = kmalloc(mtd->erasesize, GFP_KERNEL);
  227. if (mtd->_read(mtd, adm8668_parts[PART_LINUX].offset, mtd->erasesize, &len, block) || len != mtd->erasesize) {
  228. printk("Error copying first eraseblock\n");
  229. return 0;
  230. }
  231. /* Write updated header to the flash */
  232. memcpy(block, &uhdr, sizeof(uhdr));
  233. if (mtd->_unlock)
  234. mtd->_unlock(mtd, off, mtd->erasesize);
  235. erase_write(mtd, adm8668_parts[PART_LINUX].offset, mtd->erasesize, block);
  236. if (mtd->_sync)
  237. mtd->_sync(mtd);
  238. kfree(block);
  239. printk(KERN_NOTICE "Done\n");
  240. }
  241. return NR_PARTS;
  242. }
  243. int __init init_adm8668_map(void)
  244. {
  245. int nr_parts, ret;
  246. adm8668_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
  247. if (!adm8668_map.virt) {
  248. printk(KERN_ERR "Failed to ioremap\n");
  249. return -EIO;
  250. }
  251. simple_map_init(&adm8668_map);
  252. if (!(adm8668_mtd = do_map_probe("cfi_probe", &adm8668_map))) {
  253. printk(KERN_ERR "cfi_probe failed\n");
  254. iounmap((void *)adm8668_map.virt);
  255. return -ENXIO;
  256. }
  257. adm8668_mtd->owner = THIS_MODULE;
  258. nr_parts = init_mtd_partitions(adm8668_mtd, adm8668_mtd->size);
  259. ret = mtd_device_register(adm8668_mtd, adm8668_parts, nr_parts);
  260. if (ret) {
  261. printk(KERN_ERR "Flash: mtd_device_register failed\n");
  262. goto fail;
  263. }
  264. return 0;
  265. fail:
  266. if (adm8668_mtd)
  267. map_destroy(adm8668_mtd);
  268. if (adm8668_map.virt)
  269. iounmap((void *) adm8668_map.virt);
  270. adm8668_map.virt = 0;
  271. return ret;
  272. }
  273. void __exit cleanup_adm8668_map(void)
  274. {
  275. mtd_device_unregister(adm8668_mtd);
  276. map_destroy(adm8668_mtd);
  277. iounmap((void *) adm8668_map.virt);
  278. adm8668_map.virt = 0;
  279. }
  280. module_init(init_adm8668_map);
  281. module_exit(cleanup_adm8668_map);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
  284. MODULE_DESCRIPTION("MTD map driver for ADM8668 NOR Flash");