flash-map.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. diff -Nur linux-2.6.34.orig/drivers/mtd/maps/bcm47xx-flash.c linux-2.6.34/drivers/mtd/maps/bcm47xx-flash.c
  2. --- linux-2.6.34.orig/drivers/mtd/maps/bcm47xx-flash.c 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-2.6.34/drivers/mtd/maps/bcm47xx-flash.c 2010-05-30 15:50:20.921614063 +0200
  4. @@ -0,0 +1,402 @@
  5. +/*
  6. + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
  7. + * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
  8. + * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
  9. + *
  10. + * original functions for finding root filesystem from Mike Baker
  11. + *
  12. + * This program is free software; you can redistribute it and/or modify it
  13. + * under the terms of the GNU General Public License as published by the
  14. + * Free Software Foundation; either version 2 of the License, or (at your
  15. + * option) any later version.
  16. + *
  17. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  20. + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. + *
  28. + * You should have received a copy of the GNU General Public License along
  29. + * with this program; if not, write to the Free Software Foundation, Inc.,
  30. + * 675 Mass Ave, Cambridge, MA 02139, USA.
  31. + *
  32. + * Copyright 2001-2003, Broadcom Corporation
  33. + * All Rights Reserved.
  34. + *
  35. + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
  36. + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
  37. + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
  38. + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
  39. + *
  40. + * Flash mapping for BCM947XX boards
  41. + */
  42. +
  43. +#include <linux/init.h>
  44. +#include <linux/module.h>
  45. +#include <linux/types.h>
  46. +#include <linux/kernel.h>
  47. +#include <linux/sched.h>
  48. +#include <linux/wait.h>
  49. +#include <linux/mtd/mtd.h>
  50. +#include <linux/mtd/map.h>
  51. +#ifdef CONFIG_MTD_PARTITIONS
  52. +#include <linux/mtd/partitions.h>
  53. +#endif
  54. +#include <linux/crc32.h>
  55. +#ifdef CONFIG_SSB
  56. +#include <linux/ssb/ssb.h>
  57. +#endif
  58. +#include <asm/io.h>
  59. +
  60. +
  61. +#define TRX_MAGIC 0x30524448 /* "HDR0" */
  62. +#define TRX_VERSION 1
  63. +#define TRX_MAX_LEN 0x3A0000
  64. +#define TRX_NO_HEADER 1 /* Do not write TRX header */
  65. +#define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
  66. +#define TRX_MAX_OFFSET 3
  67. +
  68. +struct trx_header {
  69. + u32 magic; /* "HDR0" */
  70. + u32 len; /* Length of file including header */
  71. + u32 crc32; /* 32-bit CRC from flag_version to end of file */
  72. + u32 flag_version; /* 0:15 flags, 16:31 version */
  73. + u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
  74. +};
  75. +
  76. +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
  77. +#define NVRAM_SPACE 0x8000
  78. +#define WINDOW_ADDR 0x1fc00000
  79. +#define WINDOW_SIZE 0x400000
  80. +#define BUSWIDTH 2
  81. +
  82. +#ifdef CONFIG_SSB
  83. +extern struct ssb_bus ssb_bcm47xx;
  84. +#endif
  85. +static struct mtd_info *bcm47xx_mtd;
  86. +
  87. +static void bcm47xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  88. +{
  89. + if (len==1) {
  90. + memcpy_fromio(to, map->virt + from, len);
  91. + } else {
  92. + int i;
  93. + u16 *dest = (u16 *) to;
  94. + u16 *src = (u16 *) (map->virt + from);
  95. + for (i = 0; i < (len / 2); i++) {
  96. + dest[i] = src[i];
  97. + }
  98. + if (len & 1)
  99. + *((u8 *)dest+len-1) = src[i] & 0xff;
  100. + }
  101. +}
  102. +
  103. +static struct map_info bcm47xx_map = {
  104. + name: "Physically mapped flash",
  105. + size: WINDOW_SIZE,
  106. + bankwidth: BUSWIDTH,
  107. + phys: WINDOW_ADDR,
  108. +};
  109. +
  110. +#ifdef CONFIG_MTD_PARTITIONS
  111. +
  112. +static struct mtd_partition bcm47xx_parts[] = {
  113. + { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
  114. + { name: "linux", offset: 0, size: 0, },
  115. + { name: "rootfs", offset: 0, size: 0, },
  116. + { name: "nvram", offset: 0, size: 0, },
  117. + { name: NULL, },
  118. +};
  119. +
  120. +static int __init
  121. +find_cfe_size(struct mtd_info *mtd, size_t size)
  122. +{
  123. + struct trx_header *trx;
  124. + unsigned char buf[512];
  125. + int off;
  126. + size_t len;
  127. + int blocksize;
  128. +
  129. + trx = (struct trx_header *) buf;
  130. +
  131. + blocksize = mtd->erasesize;
  132. + if (blocksize < 0x10000)
  133. + blocksize = 0x10000;
  134. +
  135. + for (off = (128*1024); off < size; off += blocksize) {
  136. + memset(buf, 0xe5, sizeof(buf));
  137. +
  138. + /*
  139. + * Read into buffer
  140. + */
  141. + if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
  142. + len != sizeof(buf))
  143. + continue;
  144. +
  145. + /* found a TRX header */
  146. + if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
  147. + goto found;
  148. + }
  149. + }
  150. +
  151. + printk(KERN_NOTICE
  152. + "%s: Couldn't find bootloader size\n",
  153. + mtd->name);
  154. + return -1;
  155. +
  156. + found:
  157. + printk(KERN_NOTICE "bootloader size: %d\n", off);
  158. + return off;
  159. +
  160. +}
  161. +
  162. +/*
  163. + * Copied from mtdblock.c
  164. + *
  165. + * Cache stuff...
  166. + *
  167. + * Since typical flash erasable sectors are much larger than what Linux's
  168. + * buffer cache can handle, we must implement read-modify-write on flash
  169. + * sectors for each block write requests. To avoid over-erasing flash sectors
  170. + * and to speed things up, we locally cache a whole flash sector while it is
  171. + * being written to until a different sector is required.
  172. + */
  173. +
  174. +static void erase_callback(struct erase_info *done)
  175. +{
  176. + wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  177. + wake_up(wait_q);
  178. +}
  179. +
  180. +static int erase_write (struct mtd_info *mtd, unsigned long pos,
  181. + int len, const char *buf)
  182. +{
  183. + struct erase_info erase;
  184. + DECLARE_WAITQUEUE(wait, current);
  185. + wait_queue_head_t wait_q;
  186. + size_t retlen;
  187. + int ret;
  188. +
  189. + /*
  190. + * First, let's erase the flash block.
  191. + */
  192. +
  193. + init_waitqueue_head(&wait_q);
  194. + erase.mtd = mtd;
  195. + erase.callback = erase_callback;
  196. + erase.addr = pos;
  197. + erase.len = len;
  198. + erase.priv = (u_long)&wait_q;
  199. +
  200. + set_current_state(TASK_INTERRUPTIBLE);
  201. + add_wait_queue(&wait_q, &wait);
  202. +
  203. + ret = mtd->erase(mtd, &erase);
  204. + if (ret) {
  205. + set_current_state(TASK_RUNNING);
  206. + remove_wait_queue(&wait_q, &wait);
  207. + printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
  208. + "on \"%s\" failed\n",
  209. + pos, len, mtd->name);
  210. + return ret;
  211. + }
  212. +
  213. + schedule(); /* Wait for erase to finish. */
  214. + remove_wait_queue(&wait_q, &wait);
  215. +
  216. + /*
  217. + * Next, writhe data to flash.
  218. + */
  219. +
  220. + ret = mtd->write (mtd, pos, len, &retlen, buf);
  221. + if (ret)
  222. + return ret;
  223. + if (retlen != len)
  224. + return -EIO;
  225. + return 0;
  226. +}
  227. +
  228. +
  229. +
  230. +
  231. +static int __init
  232. +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
  233. +{
  234. + struct trx_header trx, *trx2;
  235. + unsigned char buf[512], *block;
  236. + int off, blocksize;
  237. + u32 i, crc = ~0;
  238. + size_t len;
  239. + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
  240. +
  241. + blocksize = mtd->erasesize;
  242. + if (blocksize < 0x10000)
  243. + blocksize = 0x10000;
  244. +
  245. + for (off = (128*1024); off < size; off += blocksize) {
  246. + memset(&trx, 0xe5, sizeof(trx));
  247. +
  248. + /*
  249. + * Read into buffer
  250. + */
  251. + if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
  252. + len != sizeof(trx))
  253. + continue;
  254. +
  255. + /* found a TRX header */
  256. + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
  257. + part->offset = le32_to_cpu(trx.offsets[2]) ? :
  258. + le32_to_cpu(trx.offsets[1]);
  259. + part->size = le32_to_cpu(trx.len);
  260. +
  261. + part->size -= part->offset;
  262. + part->offset += off;
  263. +
  264. + goto found;
  265. + }
  266. + }
  267. +
  268. + printk(KERN_NOTICE
  269. + "%s: Couldn't find root filesystem\n",
  270. + mtd->name);
  271. + return -1;
  272. +
  273. + found:
  274. + if (part->size == 0)
  275. + return 0;
  276. +
  277. + if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
  278. + return 0;
  279. +
  280. + return part->size;
  281. +}
  282. +
  283. +struct mtd_partition * __init
  284. +init_mtd_partitions(struct mtd_info *mtd, size_t size)
  285. +{
  286. + int cfe_size;
  287. +
  288. + if ((cfe_size = find_cfe_size(mtd,size)) < 0)
  289. + return NULL;
  290. +
  291. + /* boot loader */
  292. + bcm47xx_parts[0].offset = 0;
  293. + bcm47xx_parts[0].size = cfe_size;
  294. +
  295. + /* nvram */
  296. + if (cfe_size != 384 * 1024) {
  297. + bcm47xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
  298. + bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
  299. + } else {
  300. + /* nvram (old 128kb config partition on netgear wgt634u) */
  301. + bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
  302. + bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
  303. + }
  304. +
  305. + /* linux (kernel and rootfs) */
  306. + if (cfe_size != 384 * 1024) {
  307. + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
  308. + bcm47xx_parts[1].size = bcm47xx_parts[3].offset -
  309. + bcm47xx_parts[1].offset;
  310. + } else {
  311. + /* do not count the elf loader, which is on one block */
  312. + bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
  313. + bcm47xx_parts[3].size + mtd->erasesize;
  314. + bcm47xx_parts[1].size = size -
  315. + bcm47xx_parts[0].size -
  316. + (2*bcm47xx_parts[3].size) -
  317. + mtd->erasesize;
  318. + }
  319. +
  320. + /* find and size rootfs */
  321. + find_root(mtd,size,&bcm47xx_parts[2]);
  322. + bcm47xx_parts[2].size = size - bcm47xx_parts[2].offset - bcm47xx_parts[3].size;
  323. +
  324. + return bcm47xx_parts;
  325. +}
  326. +#endif
  327. +
  328. +int __init init_bcm47xx_map(void)
  329. +{
  330. +#ifdef CONFIG_SSB
  331. + struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
  332. +#endif
  333. + size_t size;
  334. + int ret = 0;
  335. +#ifdef CONFIG_MTD_PARTITIONS
  336. + struct mtd_partition *parts;
  337. + int i;
  338. +#endif
  339. +
  340. +#ifdef CONFIG_SSB
  341. + u32 window = mcore->flash_window;
  342. + u32 window_size = mcore->flash_window_size;
  343. +
  344. + printk("flash init: 0x%08x 0x%08x\n", window, window_size);
  345. + bcm47xx_map.phys = window;
  346. + bcm47xx_map.size = window_size;
  347. + bcm47xx_map.bankwidth = mcore->flash_buswidth;
  348. + bcm47xx_map.virt = ioremap_nocache(window, window_size);
  349. +#else
  350. + printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
  351. + bcm47xx_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
  352. +#endif
  353. +
  354. + if (!bcm47xx_map.virt) {
  355. + printk("Failed to ioremap\n");
  356. + return -EIO;
  357. + }
  358. +
  359. + simple_map_init(&bcm47xx_map);
  360. +
  361. + if (!(bcm47xx_mtd = do_map_probe("cfi_probe", &bcm47xx_map))) {
  362. + printk("Failed to do_map_probe\n");
  363. + iounmap((void *)bcm47xx_map.virt);
  364. + return -ENXIO;
  365. + }
  366. +
  367. + /* override copy_from routine */
  368. + //bcm47xx_map.copy_from = bcm47xx_map_copy_from;
  369. +
  370. + bcm47xx_mtd->owner = THIS_MODULE;
  371. +
  372. + size = bcm47xx_mtd->size;
  373. +
  374. + printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
  375. +
  376. +#ifdef CONFIG_MTD_PARTITIONS
  377. + parts = init_mtd_partitions(bcm47xx_mtd, size);
  378. + for (i = 0; parts[i].name; i++);
  379. + ret = add_mtd_partitions(bcm47xx_mtd, parts, i);
  380. + if (ret) {
  381. + printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
  382. + goto fail;
  383. + }
  384. +#endif
  385. + return 0;
  386. +
  387. + fail:
  388. + if (bcm47xx_mtd)
  389. + map_destroy(bcm47xx_mtd);
  390. + if (bcm47xx_map.virt)
  391. + iounmap((void *)bcm47xx_map.virt);
  392. + bcm47xx_map.virt = 0;
  393. + return ret;
  394. +}
  395. +
  396. +void __exit cleanup_bcm47xx_map(void)
  397. +{
  398. +#ifdef CONFIG_MTD_PARTITIONS
  399. + del_mtd_partitions(bcm47xx_mtd);
  400. +#endif
  401. + map_destroy(bcm47xx_mtd);
  402. + iounmap((void *)bcm47xx_map.virt);
  403. +}
  404. +
  405. +module_init(init_bcm47xx_map);
  406. +module_exit(cleanup_bcm47xx_map);
  407. diff -Nur linux-2.6.34.orig/drivers/mtd/maps/Kconfig linux-2.6.34/drivers/mtd/maps/Kconfig
  408. --- linux-2.6.34.orig/drivers/mtd/maps/Kconfig 2010-05-16 23:17:36.000000000 +0200
  409. +++ linux-2.6.34/drivers/mtd/maps/Kconfig 2010-05-30 15:13:31.141614159 +0200
  410. @@ -319,6 +319,12 @@
  411. Mapping for the Flaga digital module. If you don't have one, ignore
  412. this setting.
  413. +config MTD_BCM47XX
  414. + tristate "BCM47xx flash device"
  415. + depends on MIPS && MTD_CFI && BCM47XX
  416. + help
  417. + Support for the flash chips on the BCM947xx board.
  418. +
  419. config MTD_REDWOOD
  420. tristate "CFI Flash devices mapped on IBM Redwood"
  421. depends on MTD_CFI && ( REDWOOD_4 || REDWOOD_5 || REDWOOD_6 )
  422. diff -Nur linux-2.6.34.orig/drivers/mtd/maps/Makefile linux-2.6.34/drivers/mtd/maps/Makefile
  423. --- linux-2.6.34.orig/drivers/mtd/maps/Makefile 2010-05-16 23:17:36.000000000 +0200
  424. +++ linux-2.6.34/drivers/mtd/maps/Makefile 2010-05-30 15:13:31.141614159 +0200
  425. @@ -29,6 +29,7 @@
  426. obj-$(CONFIG_MTD_PCMCIA) += pcmciamtd.o
  427. obj-$(CONFIG_MTD_RPXLITE) += rpxlite.o
  428. obj-$(CONFIG_MTD_TQM8XXL) += tqm8xxl.o
  429. +obj-$(CONFIG_MTD_BCM47XX) += bcm47xx-flash.o
  430. obj-$(CONFIG_MTD_SA1100) += sa1100-flash.o
  431. obj-$(CONFIG_MTD_SBC_GXX) += sbc_gxx.o
  432. obj-$(CONFIG_MTD_SC520CDP) += sc520cdp.o