bcm963xx-flash.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright © 2006-2008 Florian Fainelli <florian@openwrt.org>
  3. * Mike Albon <malbon@openwrt.org>
  4. * Copyright © 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/mtd/map.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/io.h>
  30. #include <asm/mach-bcm63xx/bcm963xx_tag.h>
  31. #define BCM63XX_BUSWIDTH 2 /* Buswidth */
  32. #define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
  33. #define PFX KBUILD_MODNAME ": "
  34. static struct mtd_partition *parsed_parts;
  35. static struct mtd_info *bcm963xx_mtd_info;
  36. static struct map_info bcm963xx_map = {
  37. .name = "bcm963xx",
  38. .bankwidth = BCM63XX_BUSWIDTH,
  39. };
  40. static int parse_cfe_partitions(struct mtd_info *master,
  41. struct mtd_partition **pparts)
  42. {
  43. /* CFE, NVRAM and global Linux are always present */
  44. int nrparts = 3, curpart = 0;
  45. struct bcm_tag *buf;
  46. struct mtd_partition *parts;
  47. int ret;
  48. size_t retlen;
  49. unsigned int rootfsaddr, kerneladdr, spareaddr;
  50. unsigned int rootfslen, kernellen, sparelen, totallen;
  51. int namelen = 0;
  52. int i;
  53. char *boardid;
  54. char *tagversion;
  55. /* Allocate memory for buffer */
  56. buf = vmalloc(sizeof(struct bcm_tag));
  57. if (!buf)
  58. return -ENOMEM;
  59. /* Get the tag */
  60. ret = master->read(master, master->erasesize, sizeof(struct bcm_tag),
  61. &retlen, (void *)buf);
  62. if (retlen != sizeof(struct bcm_tag)) {
  63. vfree(buf);
  64. return -EIO;
  65. }
  66. sscanf(buf->kernel_address, "%u", &kerneladdr);
  67. sscanf(buf->kernel_length, "%u", &kernellen);
  68. sscanf(buf->total_length, "%u", &totallen);
  69. tagversion = &(buf->tag_version[0]);
  70. boardid = &(buf->board_id[0]);
  71. printk(KERN_INFO PFX "CFE boot tag found with version %s "
  72. "and board type %s\n", tagversion, boardid);
  73. kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
  74. rootfsaddr = kerneladdr + kernellen;
  75. spareaddr = roundup(totallen, master->erasesize) + master->erasesize;
  76. sparelen = master->size - spareaddr - master->erasesize;
  77. rootfslen = spareaddr - rootfsaddr;
  78. /* Determine number of partitions */
  79. namelen = 8;
  80. if (rootfslen > 0) {
  81. nrparts++;
  82. namelen += 6;
  83. };
  84. if (kernellen > 0) {
  85. nrparts++;
  86. namelen += 6;
  87. };
  88. /* Ask kernel for more memory */
  89. parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
  90. if (!parts) {
  91. vfree(buf);
  92. return -ENOMEM;
  93. };
  94. /* Start building partition list */
  95. parts[curpart].name = "CFE";
  96. parts[curpart].offset = 0;
  97. parts[curpart].size = master->erasesize;
  98. curpart++;
  99. if (kernellen > 0) {
  100. parts[curpart].name = "kernel";
  101. parts[curpart].offset = kerneladdr;
  102. parts[curpart].size = kernellen;
  103. curpart++;
  104. };
  105. if (rootfslen > 0) {
  106. parts[curpart].name = "rootfs";
  107. parts[curpart].offset = rootfsaddr;
  108. parts[curpart].size = rootfslen;
  109. if (sparelen > 0)
  110. parts[curpart].size += sparelen;
  111. curpart++;
  112. };
  113. parts[curpart].name = "nvram";
  114. parts[curpart].offset = master->size - master->erasesize;
  115. parts[curpart].size = master->erasesize;
  116. /* Global partition "linux" to make easy firmware upgrade */
  117. curpart++;
  118. parts[curpart].name = "linux";
  119. parts[curpart].offset = parts[0].size;
  120. parts[curpart].size = master->size - parts[0].size - parts[3].size;
  121. for (i = 0; i < nrparts; i++)
  122. printk(KERN_INFO PFX "Partition %d is %s offset %lx and "
  123. "length %lx\n", i, parts[i].name,
  124. (long unsigned int)(parts[i].offset),
  125. (long unsigned int)(parts[i].size));
  126. printk(KERN_INFO PFX "Spare partition is %x offset and length %x\n",
  127. spareaddr, sparelen);
  128. *pparts = parts;
  129. vfree(buf);
  130. return nrparts;
  131. };
  132. static int bcm963xx_detect_cfe(struct mtd_info *master)
  133. {
  134. int idoffset = 0x4e0;
  135. static char idstring[8] = "CFE1CFE1";
  136. char buf[9];
  137. int ret;
  138. size_t retlen;
  139. ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
  140. buf[retlen] = 0;
  141. printk(KERN_INFO PFX "Read Signature value of %s\n", buf);
  142. return strncmp(idstring, buf, 8);
  143. }
  144. static int bcm963xx_probe(struct platform_device *pdev)
  145. {
  146. int err = 0;
  147. int parsed_nr_parts = 0;
  148. char *part_type;
  149. struct resource *r;
  150. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  151. if (!r) {
  152. dev_err(&pdev->dev, "no resource supplied\n");
  153. return -ENODEV;
  154. }
  155. bcm963xx_map.phys = r->start;
  156. bcm963xx_map.size = resource_size(r);
  157. bcm963xx_map.virt = ioremap(r->start, resource_size(r));
  158. if (!bcm963xx_map.virt) {
  159. dev_err(&pdev->dev, "failed to ioremap\n");
  160. return -EIO;
  161. }
  162. dev_info(&pdev->dev, "0x%08lx at 0x%08x\n",
  163. bcm963xx_map.size, bcm963xx_map.phys);
  164. simple_map_init(&bcm963xx_map);
  165. bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
  166. if (!bcm963xx_mtd_info) {
  167. dev_err(&pdev->dev, "failed to probe using CFI\n");
  168. bcm963xx_mtd_info = do_map_probe("jedec_probe", &bcm963xx_map);
  169. if (bcm963xx_mtd_info)
  170. goto probe_ok;
  171. dev_err(&pdev->dev, "failed to probe using JEDEC\n");
  172. err = -EIO;
  173. goto err_probe;
  174. }
  175. probe_ok:
  176. bcm963xx_mtd_info->owner = THIS_MODULE;
  177. /* This is mutually exclusive */
  178. if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0) {
  179. dev_info(&pdev->dev, "CFE bootloader detected\n");
  180. if (parsed_nr_parts == 0) {
  181. int ret = parse_cfe_partitions(bcm963xx_mtd_info,
  182. &parsed_parts);
  183. if (ret > 0) {
  184. part_type = "CFE";
  185. parsed_nr_parts = ret;
  186. }
  187. }
  188. } else {
  189. dev_info(&pdev->dev, "unsupported bootloader\n");
  190. err = -ENODEV;
  191. goto err_probe;
  192. }
  193. return mtd_device_register(bcm963xx_mtd_info, parsed_parts,
  194. parsed_nr_parts);
  195. err_probe:
  196. iounmap(bcm963xx_map.virt);
  197. return err;
  198. }
  199. static int bcm963xx_remove(struct platform_device *pdev)
  200. {
  201. if (bcm963xx_mtd_info) {
  202. mtd_device_unregister(bcm963xx_mtd_info);
  203. map_destroy(bcm963xx_mtd_info);
  204. }
  205. if (bcm963xx_map.virt) {
  206. iounmap(bcm963xx_map.virt);
  207. bcm963xx_map.virt = 0;
  208. }
  209. return 0;
  210. }
  211. static struct platform_driver bcm63xx_mtd_dev = {
  212. .probe = bcm963xx_probe,
  213. .remove = bcm963xx_remove,
  214. .driver = {
  215. .name = "bcm963xx-flash",
  216. .owner = THIS_MODULE,
  217. },
  218. };
  219. static int __init bcm963xx_mtd_init(void)
  220. {
  221. return platform_driver_register(&bcm63xx_mtd_dev);
  222. }
  223. static void __exit bcm963xx_mtd_exit(void)
  224. {
  225. platform_driver_unregister(&bcm63xx_mtd_dev);
  226. }
  227. module_init(bcm963xx_mtd_init);
  228. module_exit(bcm963xx_mtd_exit);
  229. MODULE_LICENSE("GPL");
  230. MODULE_DESCRIPTION("Broadcom BCM63xx MTD driver for CFE and RedBoot");
  231. MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
  232. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  233. MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");