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