pmcmsp-flash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Mapping of a custom board with both AMD CFI and JEDEC flash in partitions.
  3. * Config with both CFI and JEDEC device support.
  4. *
  5. * Basically physmap.c with the addition of partitions and
  6. * an array of mapping info to accomodate more than one flash type per board.
  7. *
  8. * Copyright 2005-2007 PMC-Sierra, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  18. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  21. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/map.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <asm/io.h>
  37. #include <msp_prom.h>
  38. #include <msp_regs.h>
  39. static struct mtd_info **msp_flash;
  40. static struct mtd_partition **msp_parts;
  41. static struct map_info *msp_maps;
  42. static int fcnt;
  43. #define DEBUG_MARKER printk(KERN_NOTICE "%s[%d]\n", __func__, __LINE__)
  44. static int __init init_msp_flash(void)
  45. {
  46. int i, j, ret = -ENOMEM;
  47. int offset, coff;
  48. char *env;
  49. int pcnt;
  50. char flash_name[] = "flash0";
  51. char part_name[] = "flash0_0";
  52. unsigned addr, size;
  53. /* If ELB is disabled by "ful-mux" mode, we can't get at flash */
  54. if ((*DEV_ID_REG & DEV_ID_SINGLE_PC) &&
  55. (*ELB_1PC_EN_REG & SINGLE_PCCARD)) {
  56. printk(KERN_NOTICE "Single PC Card mode: no flash access\n");
  57. return -ENXIO;
  58. }
  59. /* examine the prom environment for flash devices */
  60. for (fcnt = 0; (env = prom_getenv(flash_name)); fcnt++)
  61. flash_name[5] = '0' + fcnt + 1;
  62. if (fcnt < 1)
  63. return -ENXIO;
  64. printk(KERN_NOTICE "Found %d PMC flash devices\n", fcnt);
  65. msp_flash = kmalloc(fcnt * sizeof(struct map_info *), GFP_KERNEL);
  66. if (!msp_flash)
  67. return -ENOMEM;
  68. msp_parts = kmalloc(fcnt * sizeof(struct mtd_partition *), GFP_KERNEL);
  69. if (!msp_parts)
  70. goto free_msp_flash;
  71. msp_maps = kcalloc(fcnt, sizeof(struct mtd_info), GFP_KERNEL);
  72. if (!msp_maps)
  73. goto free_msp_parts;
  74. /* loop over the flash devices, initializing each */
  75. for (i = 0; i < fcnt; i++) {
  76. /* examine the prom environment for flash partititions */
  77. part_name[5] = '0' + i;
  78. part_name[7] = '0';
  79. for (pcnt = 0; (env = prom_getenv(part_name)); pcnt++)
  80. part_name[7] = '0' + pcnt + 1;
  81. if (pcnt == 0) {
  82. printk(KERN_NOTICE "Skipping flash device %d "
  83. "(no partitions defined)\n", i);
  84. continue;
  85. }
  86. msp_parts[i] = kcalloc(pcnt, sizeof(struct mtd_partition),
  87. GFP_KERNEL);
  88. if (!msp_parts[i])
  89. goto cleanup_loop;
  90. /* now initialize the devices proper */
  91. flash_name[5] = '0' + i;
  92. env = prom_getenv(flash_name);
  93. if (sscanf(env, "%x:%x", &addr, &size) < 2) {
  94. ret = -ENXIO;
  95. kfree(msp_parts[i]);
  96. goto cleanup_loop;
  97. }
  98. addr = CPHYSADDR(addr);
  99. printk(KERN_NOTICE
  100. "MSP flash device \"%s\": 0x%08x at 0x%08x\n",
  101. flash_name, size, addr);
  102. /* This must matchs the actual size of the flash chip */
  103. msp_maps[i].size = size;
  104. msp_maps[i].phys = addr;
  105. /*
  106. * Platforms have a specific limit of the size of memory
  107. * which may be mapped for flash:
  108. */
  109. if (size > CONFIG_MSP_FLASH_MAP_LIMIT)
  110. size = CONFIG_MSP_FLASH_MAP_LIMIT;
  111. msp_maps[i].virt = ioremap(addr, size);
  112. if (msp_maps[i].virt == NULL) {
  113. ret = -ENXIO;
  114. kfree(msp_parts[i]);
  115. goto cleanup_loop;
  116. }
  117. msp_maps[i].bankwidth = 1;
  118. msp_maps[i].name = kmalloc(7, GFP_KERNEL);
  119. if (!msp_maps[i].name) {
  120. iounmap(msp_maps[i].virt);
  121. kfree(msp_parts[i]);
  122. goto cleanup_loop;
  123. }
  124. msp_maps[i].name = strncpy(msp_maps[i].name, flash_name, 7);
  125. for (j = 0; j < pcnt; j++) {
  126. part_name[5] = '0' + i;
  127. part_name[7] = '0' + j;
  128. env = prom_getenv(part_name);
  129. if (sscanf(env, "%x:%x:%n", &offset, &size,
  130. &coff) < 2) {
  131. ret = -ENXIO;
  132. kfree(msp_maps[i].name);
  133. iounmap(msp_maps[i].virt);
  134. kfree(msp_parts[i]);
  135. goto cleanup_loop;
  136. }
  137. msp_parts[i][j].size = size;
  138. msp_parts[i][j].offset = offset;
  139. msp_parts[i][j].name = env + coff;
  140. }
  141. /* now probe and add the device */
  142. simple_map_init(&msp_maps[i]);
  143. msp_flash[i] = do_map_probe("cfi_probe", &msp_maps[i]);
  144. if (msp_flash[i]) {
  145. msp_flash[i]->owner = THIS_MODULE;
  146. add_mtd_partitions(msp_flash[i], msp_parts[i], pcnt);
  147. } else {
  148. printk(KERN_ERR "map probe failed for flash\n");
  149. ret = -ENXIO;
  150. kfree(msp_maps[i].name);
  151. iounmap(msp_maps[i].virt);
  152. kfree(msp_parts[i]);
  153. goto cleanup_loop;
  154. }
  155. }
  156. return 0;
  157. cleanup_loop:
  158. while (i--) {
  159. del_mtd_partitions(msp_flash[i]);
  160. map_destroy(msp_flash[i]);
  161. kfree(msp_maps[i].name);
  162. iounmap(msp_maps[i].virt);
  163. kfree(msp_parts[i]);
  164. }
  165. kfree(msp_maps);
  166. free_msp_parts:
  167. kfree(msp_parts);
  168. free_msp_flash:
  169. kfree(msp_flash);
  170. return ret;
  171. }
  172. static void __exit cleanup_msp_flash(void)
  173. {
  174. int i;
  175. for (i = 0; i < fcnt; i++) {
  176. del_mtd_partitions(msp_flash[i]);
  177. map_destroy(msp_flash[i]);
  178. iounmap((void *)msp_maps[i].virt);
  179. /* free the memory */
  180. kfree(msp_maps[i].name);
  181. kfree(msp_parts[i]);
  182. }
  183. kfree(msp_flash);
  184. kfree(msp_parts);
  185. kfree(msp_maps);
  186. }
  187. MODULE_AUTHOR("PMC-Sierra, Inc");
  188. MODULE_DESCRIPTION("MTD map driver for PMC-Sierra MSP boards");
  189. MODULE_LICENSE("GPL");
  190. module_init(init_msp_flash);
  191. module_exit(cleanup_msp_flash);